Ejemplo n.º 1
0
        public SpentOutputsStorage(string baseDirectory)
            : base(baseDirectory, "SpentOutputs",
                   keyPairs =>
        {
            using (var stream = new MemoryStream())
            {
                foreach (var keyPair in keyPairs)
                {
                    DataEncoder.EncodeTxOutputKey(stream, keyPair.Key);
                    DataEncoder.EncodeTxOutput(stream, keyPair.Value);
                }

                return(stream.ToArray());
            }
        },
                   (blockHash, bytes) =>
        {
            using (var stream = new MemoryStream(bytes))
            {
                var keyPairs = ImmutableList.CreateBuilder <KeyValuePair <TxOutputKey, TxOutput> >();
                while (stream.Position < stream.Length)
                {
                    var txOutputKey = DataEncoder.DecodeTxOutputKey(stream);
                    var txOutput    = DataEncoder.DecodeTxOutput(stream);
                    keyPairs.Add(new KeyValuePair <TxOutputKey, TxOutput>(txOutputKey, txOutput));
                }

                return(keyPairs.ToImmutable());
            }
        })
        { }
Ejemplo n.º 2
0
        public IEnumerable <KeyValuePair <TxOutputKey, TxOutput> > UnspentOutputs()
        {
            Api.JetBeginTransaction2(this.jetSession, BeginTransactionGrbit.ReadOnly);
            try
            {
                //Api.JetSetCurrentIndex(this.jetSession, this.unspentTxOutputsTableId, "IX_TxOutputKey");
                Api.MoveBeforeFirst(this.jetSession, this.unspentTxOutputsTableId);
                while (Api.TryMoveNext(this.jetSession, this.unspentTxOutputsTableId))
                {
                    var txOutputKey   = DataEncoder.DecodeTxOutputKey(Api.RetrieveColumn(this.jetSession, this.unspentTxOutputsTableId, this.txOutputKeyColumnId));
                    var txOutputBytes = Api.RetrieveColumn(this.jetSession, this.unspentTxOutputsTableId, this.txOutputSmallColumnId);
                    if (txOutputBytes == null)
                    {
                        txOutputBytes = Api.RetrieveColumn(this.jetSession, this.unspentTxOutputsTableId, this.txOutputLargeColumnId);
                    }
                    var txOutput = DataEncoder.DecodeTxOutput(txOutputBytes);

                    yield return(new KeyValuePair <TxOutputKey, TxOutput>(txOutputKey, txOutput));
                }
            }
            finally
            {
                Api.JetCommitTransaction(this.jetSession, CommitTransactionGrbit.LazyFlush);
            }
        }