Exemplo n.º 1
0
        public bool TryAddUnspentTxOutput(TxOutputKey txOutputKey, TxOutput txOutput)
        {
            CheckWriteTransaction();

            using (SetSessionContext())
            {
                try
                {
                    using (var jetUpdate = this.jetSession.BeginUpdate(this.unspentTxOutputTableId, JET_prep.Insert))
                    {
                        Api.SetColumns(this.jetSession, this.unspentTxOutputTableId,
                                       new BytesColumnValue {
                            Columnid = this.txOutputKeyColumnId, Value = DbEncoder.EncodeTxOutputKey(txOutputKey)
                        },
                                       new BytesColumnValue {
                            Columnid = this.txOutputBytesColumnId, Value = DataEncoder.EncodeTxOutput(txOutput)
                        });

                        jetUpdate.Save();
                    }

                    return(true);
                }
                catch (EsentKeyDuplicateException)
                {
                    return(false);
                }
            }
        }
Exemplo n.º 2
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());
            }
        })
        { }
Exemplo n.º 3
0
        public void AddOutput(TxOutputKey txOutputKey, TxOutput txOutput)
        {
            Api.JetBeginTransaction(this.jetSession);
            try
            {
                Api.JetPrepareUpdate(this.jetSession, this.unspentTxOutputsTableId, JET_prep.Insert);
                try
                {
                    var txOutputBytes = DataEncoder.EncodeTxOutput(txOutput);

                    Api.SetColumn(this.jetSession, this.unspentTxOutputsTableId, this.txOutputKeyColumnId, DataEncoder.EncodeTxOutputKey(txOutputKey));
                    if (txOutputBytes.Length <= 255)
                    {
                        Api.SetColumn(this.jetSession, this.unspentTxOutputsTableId, this.txOutputSmallColumnId, txOutputBytes);
                    }
                    else
                    {
                        Api.SetColumn(this.jetSession, this.unspentTxOutputsTableId, this.txOutputLargeColumnId, txOutputBytes);
                    }

                    if (IndexOutputs)
                    {
                        var sha256 = new SHA256Managed();
                        Api.SetColumn(this.jetSession, this.unspentTxOutputsTableId, this.outputScriptHashColumnId, sha256.ComputeHash(txOutput.ScriptPublicKey.ToArray()));
                    }

                    Api.JetUpdate(this.jetSession, this.unspentTxOutputsTableId);
                    Api.JetCommitTransaction(this.jetSession, CommitTransactionGrbit.LazyFlush);
                    this.unspentTxOutputsCount++;
                }
                catch (Exception)
                {
                    Api.JetPrepareUpdate(this.jetSession, this.unspentTxOutputsTableId, JET_prep.Cancel);
                    throw;
                }
            }
            catch (Exception)
            {
                Api.JetRollback(this.jetSession, RollbackTransactionGrbit.None);
                throw;
            }
        }
Exemplo n.º 4
0
        public void TestWireDecodeTransactionOut()
        {
            var actual = DataEncoder.EncodeTxOutput(DataEncoder.DecodeTxOutput(TRANSACTION_OUTPUT_1_BYTES.ToArray()));

            CollectionAssert.AreEqual(TRANSACTION_OUTPUT_1_BYTES.ToList(), actual.ToList());
        }
Exemplo n.º 5
0
        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
            });
        }