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()); } }) { }
public bool TryGetOutput(TxOutputKey txOutputKey, out TxOutput txOutput) { Api.JetBeginTransaction2(this.jetSession, BeginTransactionGrbit.ReadOnly); try { //Api.JetSetCurrentIndex(this.jetSession, this.unspentTxOutputsTableId, "IX_TxOutputKey"); Api.MakeKey(this.jetSession, this.unspentTxOutputsTableId, DataEncoder.EncodeTxOutputKey(txOutputKey), MakeKeyGrbit.NewKey); if (Api.TrySeek(this.jetSession, this.unspentTxOutputsTableId, SeekGrbit.SeekEQ)) { var txOutputBytes = Api.RetrieveColumn(this.jetSession, this.unspentTxOutputsTableId, this.txOutputSmallColumnId); if (txOutputBytes == null) { txOutputBytes = Api.RetrieveColumn(this.jetSession, this.unspentTxOutputsTableId, this.txOutputLargeColumnId); } txOutput = DataEncoder.DecodeTxOutput(txOutputBytes); return(true); } else { txOutput = default(TxOutput); return(false); } } finally { Api.JetCommitTransaction(this.jetSession, CommitTransactionGrbit.LazyFlush); } }
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); } }
public void TestWireDecodeTransactionOut() { var actual = DataEncoder.EncodeTxOutput(DataEncoder.DecodeTxOutput(TRANSACTION_OUTPUT_1_BYTES.ToArray())); CollectionAssert.AreEqual(TRANSACTION_OUTPUT_1_BYTES.ToList(), actual.ToList()); }