Exemple #1
0
        public SpentTransactionsStorage(string baseDirectory)
            : base(baseDirectory, "SpentTransactions",
                   keyPairs =>
        {
            using (var stream = new MemoryStream())
            {
                foreach (var keyPair in keyPairs)
                {
                    DataEncoder.EncodeUInt256(stream, keyPair.Key);
                    DataEncoder.EncodeSpentTx(stream, keyPair.Value);
                }

                return(stream.ToArray());
            }
        },
                   (blockHash, bytes) =>
        {
            using (var stream = new MemoryStream(bytes))
            {
                var keyPairs = ImmutableList.CreateBuilder <KeyValuePair <UInt256, SpentTx> >();

                while (stream.Position < stream.Length)
                {
                    var txHash  = DataEncoder.DecodeUInt256(stream);
                    var spentTx = DataEncoder.DecodeSpentTx(stream);

                    keyPairs.Add(new KeyValuePair <UInt256, SpentTx>(txHash, spentTx));
                }

                return(keyPairs.ToImmutable());
            }
        })
        { }
        public bool TryAddBlockSpentTxes(int blockIndex, BlockSpentTxes spentTxes)
        {
            CheckWriteTransaction();

            try
            {
                using (SetSessionContext())
                    using (var jetUpdate = this.jetSession.BeginUpdate(this.spentTxTableId, JET_prep.Insert))
                    {
                        byte[] spentTxesBytes;
                        using (var stream = new MemoryStream())
                            using (var writer = new BinaryWriter(stream))
                            {
                                writer.WriteList <SpentTx>(spentTxes, spentTx => DataEncoder.EncodeSpentTx(writer, spentTx));
                                spentTxesBytes = stream.ToArray();
                            }

                        Api.SetColumns(this.jetSession, this.spentTxTableId,
                                       new Int32ColumnValue {
                            Columnid = this.spentSpentBlockIndexColumnId, Value = blockIndex
                        },
                                       new BytesColumnValue {
                            Columnid = this.spentDataColumnId, Value = spentTxesBytes
                        });

                        jetUpdate.Save();
                    }

                return(true);
            }
            catch (EsentKeyDuplicateException)
            {
                return(false);
            }
        }
Exemple #3
0
        public bool TryAddBlockSpentTxes(int blockIndex, IImmutableList <SpentTx> spentTxes)
        {
            try
            {
                using (var jetUpdate = this.jetSession.BeginUpdate(this.spentTxTableId, JET_prep.Insert))
                {
                    byte[] spentTxesBytes;
                    using (var stream = new MemoryStream())
                        using (var writer = new BinaryWriter(stream))
                        {
                            writer.WriteList(spentTxes.ToImmutableArray(), spentTx => DataEncoder.EncodeSpentTx(stream, spentTx));
                            spentTxesBytes = stream.ToArray();
                        }

                    Api.SetColumn(this.jetSession, this.spentTxTableId, this.spentSpentBlockIndexColumnId, blockIndex);
                    Api.SetColumn(this.jetSession, this.spentTxTableId, this.spentDataColumnId, spentTxesBytes);

                    jetUpdate.Save();
                }

                return(true);
            }
            catch (EsentKeyDuplicateException)
            {
                return(false);
            }
        }