/// <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); }
/// <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); }
/// <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); }
/// <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 }); }
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); }
public uint256 GetHash(NetworkOptions options) { // Block's hash is his header's hash. return(this.header.GetHash(options)); }
public NoSqlRepository(NetworkOptions options = null) { this.TransactionOptions = options ?? NetworkOptions.TemporaryOptions; }
public BlockTransactionMapStore(NetworkOptions options = null) : this(new InMemoryNoSqlRepository(options)) { }
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) { }
public NoSqlBlockRepository(NetworkOptions options = null) : this(new InMemoryNoSqlRepository(options)) { }