public bool TryAddChainedHeader(ChainedHeader chainedHeader) { var key = MakeHeaderKey(chainedHeader.Hash); Slice existingValue; if (db.TryGet(ReadOptions.Default, key, out existingValue)) { return(false); } var writeBatch = new WriteBatch(); try { writeBatch.Put(key, DataEncoder.EncodeChainedHeader(chainedHeader)); writeBatch.Put(MakeTotalWorkKey(chainedHeader.Hash, chainedHeader.TotalWork), new byte[1]); db.Write(WriteOptions.Default, writeBatch); } finally { writeBatch.Dispose(); } return(true); }
public bool TryAddHeader(ChainedHeader header) { CheckWriteTransaction(); try { using (SetSessionContext()) using (var jetUpdate = this.jetSession.BeginUpdate(this.headersTableId, JET_prep.Insert)) { Api.SetColumns(this.jetSession, this.headersTableId, new BytesColumnValue { Columnid = this.headerBlockHashColumnId, Value = DbEncoder.EncodeUInt256(header.Hash) }, new BytesColumnValue { Columnid = this.headerBytesColumnId, Value = DataEncoder.EncodeChainedHeader(header) }); jetUpdate.Save(); } return(true); } catch (EsentKeyDuplicateException) { return(false); } }
public bool TryAddChainedHeader(ChainedHeader chainedHeader) { try { using (var handle = this.cursorCache.TakeItem()) { var cursor = handle.Item; using (var jetTx = cursor.jetSession.BeginTransaction()) { using (var jetUpdate = cursor.jetSession.BeginUpdate(cursor.blockHeadersTableId, JET_prep.Insert)) { Api.SetColumns(cursor.jetSession, cursor.blockHeadersTableId, new BytesColumnValue { Columnid = cursor.blockHeaderHashColumnId, Value = DbEncoder.EncodeUInt256(chainedHeader.Hash) }, new BytesColumnValue { Columnid = cursor.blockHeaderPreviousHashColumnId, Value = DbEncoder.EncodeUInt256(chainedHeader.PreviousBlockHash) }, new Int32ColumnValue { Columnid = cursor.blockHeaderHeightColumnId, Value = chainedHeader.Height }, new BytesColumnValue { Columnid = cursor.blockHeaderTotalWorkColumnId, Value = DataEncoder.EncodeTotalWork(chainedHeader.TotalWork) }, new BytesColumnValue { Columnid = cursor.blockHeaderBytesColumnId, Value = DataEncoder.EncodeChainedHeader(chainedHeader) }); jetUpdate.Save(); } jetTx.CommitLazy(); return(true); } } } catch (EsentKeyDuplicateException) { return(false); } }
public void AddChainedHeader(ChainedHeader chainedHeader) { if (!this.inTransaction) { throw new InvalidOperationException(); } try { using (var jetUpdate = this.jetSession.BeginUpdate(this.chainTableId, JET_prep.Insert)) { Api.SetColumn(this.jetSession, this.chainTableId, this.blockHeightColumnId, chainedHeader.Height); Api.SetColumn(this.jetSession, this.chainTableId, this.chainedHeaderBytesColumnId, DataEncoder.EncodeChainedHeader(chainedHeader)); jetUpdate.Save(); } } catch (Exception e) { throw new InvalidOperationException("Failed to add chained header.", e); } }
public ChainedHeaderStorage(string baseDirectory) : base(baseDirectory, "ChainedHeaders", chainedHeader => DataEncoder.EncodeChainedHeader(chainedHeader), (blockHash, bytes) => DataEncoder.DecodeChainedHeader(bytes)) { }
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 }); }