public override int Read(byte[] buffer, int offset, int count) { while ( unreadBytes.Count < count && this.unspentTransactions.MoveNext() ) { var unspentTx = this.unspentTransactions.Current; unreadBytes.AddRange(DataEncoder.EncodeUnspentTx(unspentTx)); } var available = unreadBytes.Count; if (available >= count) { unreadBytes.CopyTo(0, buffer, offset, count); unreadBytes.RemoveRange(0, count); this.totalBytes += count; return(count); } else { unreadBytes.CopyTo(0, buffer, offset, available); unreadBytes.Clear(); this.totalBytes += available; return(available); } }
public static UInt256 ComputeHash(IChainState chainState) { using (var sha256 = new SHA256Managed()) { // add each unspent tx to hash foreach (var unspentTx in chainState.ReadUnspentTransactions()) { var unspentTxBytes = DataEncoder.EncodeUnspentTx(unspentTx); sha256.TransformBlock(unspentTxBytes, 0, unspentTxBytes.Length, unspentTxBytes, 0); } // finalize hash sha256.TransformFinalBlock(new byte[0], 0, 0); // hash again to return double-hashed utxo committment return(new UInt256(SHA256Static.ComputeHash(sha256.Hash))); } }
private void InitWorkQueueDictionaries() { unspentTxes = CreateWorkQueueDictionary <UInt256, UnspentTx>(MakeUnspentTxKey, x => DataDecoder.DecodeUnspentTx(x)); unspentTxesApplier = CreateApplier <UInt256, UnspentTx>(MakeUnspentTxKey, x => DataEncoder.EncodeUnspentTx(x)); unspentTxes.WorkQueue.LinkTo(unspentTxesApplier, new DataflowLinkOptions { PropagateCompletion = true }); unspentTxOutputs = CreateWorkQueueDictionary <TxOutputKey, TxOutput>(MakeUnspentTxOutputKey, x => DataDecoder.DecodeTxOutput(x)); unspentTxOutputsApplier = CreateApplier <TxOutputKey, TxOutput>(MakeUnspentTxOutputKey, x => DataEncoder.EncodeTxOutput(x)); unspentTxOutputs.WorkQueue.LinkTo(unspentTxOutputsApplier, new DataflowLinkOptions { PropagateCompletion = true }); globals = CreateWorkQueueDictionary <GlobalValue, Slice>(MakeGlobalKey, x => x); globalsApplier = CreateApplier <GlobalValue, Slice>(MakeGlobalKey, x => x.ToArray()); globals.WorkQueue.LinkTo(globalsApplier, new DataflowLinkOptions { PropagateCompletion = true }); headers = CreateWorkQueueDictionary <UInt256, ChainedHeader>(MakeHeaderKey, x => DataDecoder.DecodeChainedHeader(x)); headersApplier = CreateApplier <UInt256, ChainedHeader>(MakeHeaderKey, x => DataEncoder.EncodeChainedHeader(x)); headers.WorkQueue.LinkTo(headersApplier, new DataflowLinkOptions { PropagateCompletion = true }); spentTxes = CreateWorkQueueDictionary <int, BlockSpentTxes>(MakeSpentTxesKey, x => DataDecoder.DecodeBlockSpentTxes(x)); spentTxesApplier = CreateApplier <int, BlockSpentTxes>(MakeSpentTxesKey, x => DataEncoder.EncodeBlockSpentTxes(x)); spentTxes.WorkQueue.LinkTo(spentTxesApplier, new DataflowLinkOptions { PropagateCompletion = true }); unmintedTxes = CreateWorkQueueDictionary <UInt256, IImmutableList <UnmintedTx> >(MakeUnmintedTxesKey, x => DataDecoder.DecodeUnmintedTxList(x)); unmintedTxesApplier = CreateApplier <UInt256, IImmutableList <UnmintedTx> >(MakeUnmintedTxesKey, x => DataEncoder.EncodeUnmintedTxList(x)); unmintedTxes.WorkQueue.LinkTo(unmintedTxesApplier, new DataflowLinkOptions { PropagateCompletion = true }); }