Ejemplo n.º 1
0
        /// <summary>
        /// Create a block with the specified option only. (useful for stripping data from a block).
        /// </summary>
        /// <param name="options">Options to keep.</param>
        /// <returns>A new block with only the options wanted.</returns>
        public Block WithOptions(NetworkOptions options)
        {
            if (this.Transactions.Count == 0)
            {
                return(this);
            }

            if ((options == NetworkOptions.Witness) && this.Transactions[0].HasWitness)
            {
                return(this);
            }

            if ((options == NetworkOptions.None) && !this.Transactions[0].HasWitness)
            {
                return(this);
            }

            var instance = new Block();
            var ms       = new MemoryStream();
            var bms      = new BitcoinStream(ms, true)
            {
                TransactionOptions = options
            };

            this.ReadWrite(bms);
            ms.Position = 0;
            bms         = new BitcoinStream(ms, false)
            {
                TransactionOptions = options
            };

            instance.ReadWrite(bms);
            return(instance);
        }
        public static int GetSerializedSize(this IBitcoinSerializable serializable, NetworkOptions options)
        {
            var bms = new BitcoinStream(Stream.Null, true);

            bms.TransactionOptions = options;
            serializable.ReadWrite(bms);
            return((int)bms.Counter.WrittenBytes);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Clones the NetworkOptions object.
        /// </summary>
        /// <returns>The cloned NetworkOptions object.</returns>
        public virtual NetworkOptions Clone()
        {
            var clone = new NetworkOptions();

            clone.flags          = this.flags;
            clone.IsProofOfStake = this.IsProofOfStake;
            return(clone);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Provides the '&' (and) operator between two NetworkOptions objects.
        /// </summary>
        /// <param name="left">The left NetworkOptions object.</param>
        /// <param name="right">The right NetworkOptions object.</param>
        /// <returns>A NetworkOptions object that represents the intersection of the input object.</returns>
        public static NetworkOptions operator &(NetworkOptions left, NetworkOptions right)
        {
            if (ReferenceEquals(null, left))
            {
                return(right?.Clone());
            }
            NetworkOptions clone = left.Clone();

            clone.flags &= (right?.flags ?? All);
            return(clone);
        }
        public static void FromBytes(this IBitcoinSerializable serializable, byte[] bytes, ProtocolVersion version = ProtocolVersion.PROTOCOL_VERSION,
                                     NetworkOptions options = null)
        {
            var bms = new BitcoinStream(bytes)
            {
                ProtocolVersion    = version,
                TransactionOptions = options
            };

            serializable.ReadWrite(bms);
        }
 public void CopyParameters(BitcoinStream stream)
 {
     if (stream == null)
     {
         throw new ArgumentNullException("stream");
     }
     this.ProtocolVersion    = stream.ProtocolVersion;
     this.TransactionOptions = stream.TransactionOptions.Clone();
     this.IsBigEndian        = stream.IsBigEndian;
     this.MaxArraySize       = stream.MaxArraySize;
     this.Type = stream.Type;
 }
        public static T Clone <T>(this T serializable, ProtocolVersion version = ProtocolVersion.PROTOCOL_VERSION) where T : IBitcoinSerializable, new()
        {
            NetworkOptions options  = NetworkOptions.All;
            var            instance = new T();

            if (serializable is IHaveNetworkOptions haveNetworkOptions)
            {
                options = haveNetworkOptions.GetNetworkOptions();
            }
            instance.FromBytes(serializable.ToBytes(version, options), version, options);
            return(instance);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Provides the '|' (or) operator between two NetworkOptions objects.
        /// </summary>
        /// <param name="left">The left NetworkOptions object.</param>
        /// <param name="right">The right NetworkOptions object.</param>
        /// <returns>A NetworkOptions object that represents the union of the input object.</returns>
        public static NetworkOptions operator |(NetworkOptions left, NetworkOptions right)
        {
            if (ReferenceEquals(null, left))
            {
                Swap(ref left, ref right);
            }
            NetworkOptions clone = left?.Clone();

            if (!ReferenceEquals(null, clone))
            {
                clone.flags |= (right?.flags ?? All);
            }
            return(clone);
        }
        /// <summary>
        /// Provides the '&' (and) operator between two NetworkOptions objects.
        /// </summary>
        /// <param name="left">The left NetworkOptions object.</param>
        /// <param name="right">The right NetworkOptions object.</param>
        /// <returns>A NetworkOptions object that represents the intersection of the input object.</returns>
        public static NetworkOptions operator &(NetworkOptions left, NetworkOptions right)
        {
            if (left == null)
            {
                Swap(ref left, ref right);
            }
            NetworkOptions clone = left?.Clone();

            if (clone != null)
            {
                clone.flags &= (right?.flags ?? All);
            }
            return(clone);
        }
 public static byte[] ToBytes(this IBitcoinSerializable serializable, ProtocolVersion version = ProtocolVersion.PROTOCOL_VERSION,
                              NetworkOptions options = null)
 {
     using (MemoryStream ms = new MemoryStream())
     {
         var bms = new BitcoinStream(ms, true)
         {
             ProtocolVersion = version,
             // If no options have been provided then take the options from the serializable (or default)
             TransactionOptions = options ?? ((serializable as IHaveNetworkOptions)?.GetNetworkOptions() ?? NetworkOptions.TemporaryOptions)
         };
         serializable.ReadWrite(bms);
         return(ToArrayEfficient(ms));
     }
 }
        public static void ReadWrite(this IBitcoinSerializable serializable, Stream stream, bool serializing, ProtocolVersion version = ProtocolVersion.PROTOCOL_VERSION,
                                     NetworkOptions options = null)
        {
            // If no options have been provided then take the options from the serializable
            if (options == null && serializing && serializable is IHaveNetworkOptions)
            {
                options = (serializable as IHaveNetworkOptions).GetNetworkOptions();
            }

            serializable.ReadWrite(new BitcoinStream(stream, serializing)
            {
                ProtocolVersion    = version,
                TransactionOptions = options
            });
        }
Ejemplo n.º 12
0
        public uint256 GetHash(NetworkOptions options)
        {
            uint256 hash = null;

            uint256[] hashes = this.hashes;

            if (hashes != null)
            {
                hash = hashes[0];
            }

            if (hash != null)
            {
                return(hash);
            }

            if (options.IsProofOfStake)
            {
                if (this.version > 6)
                {
                    hash = Hashes.Hash256(this.ToBytes());
                }
                else
                {
                    hash = this.GetPoWHash();
                }
            }
            else
            {
                using (HashStream hs = new HashStream())
                {
                    this.ReadWrite(new BitcoinStream(hs, true));
                    hash = hs.GetHash();
                }
            }

            hashes = this.hashes;
            if (hashes != null)
            {
                hashes[0] = hash;
            }

            return(hash);
        }
Ejemplo n.º 13
0
 public uint256 GetHash(NetworkOptions options)
 {
     // Block's hash is his header's hash.
     return(this.header.GetHash(options));
 }
Ejemplo n.º 14
0
 public NoSqlRepository(NetworkOptions options = null)
 {
     this.TransactionOptions = options ?? NetworkOptions.TemporaryOptions;
 }
Ejemplo n.º 15
0
 public BlockTransactionMapStore(NetworkOptions options = null)
     : this(new InMemoryNoSqlRepository(options))
 {
 }
Ejemplo n.º 16
0
 public NoSqlTransactionRepository(NetworkOptions options = null)
     : this(new InMemoryNoSqlRepository(options))
 {
 }
 public static void ReadWrite(this IBitcoinSerializable serializable, byte[] bytes, ProtocolVersion version = ProtocolVersion.PROTOCOL_VERSION,
                              NetworkOptions options = null)
 {
     ReadWrite(serializable, new MemoryStream(bytes), false, version, options);
 }
 public InMemoryNoSqlRepository(NetworkOptions options = null)
     : base(options)
 {
 }
Ejemplo n.º 19
0
 public NoSqlBlockRepository(NetworkOptions options = null)
     : this(new InMemoryNoSqlRepository(options))
 {
 }