From 7223caf36953efd597303f3c7cd7a2aa91831c6c Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 18 Feb 2022 15:04:57 +0300 Subject: [PATCH] dao: improve PutCurrentHeader logic Move serialization out of the core. --- pkg/core/blockchain.go | 11 ++--------- pkg/core/dao/dao.go | 7 +++++-- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 346905870..624b9932c 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -325,7 +325,7 @@ func (bc *Blockchain) init() error { return err } bc.headerHashes = []util.Uint256{genesisBlock.Hash()} - bc.dao.PutCurrentHeader(hashAndIndexToBytes(genesisBlock.Hash(), genesisBlock.Index)) + bc.dao.PutCurrentHeader(genesisBlock.Hash(), genesisBlock.Index) if err := bc.stateRoot.Init(0); err != nil { return fmt.Errorf("can't init MPT: %w", err) } @@ -958,7 +958,7 @@ func (bc *Blockchain) addHeaders(verify bool, headers ...*block.Header) error { bc.storedHeaderCount += headerBatchCount } - batch.Store.Put(storage.SYSCurrentHeader.Bytes(), hashAndIndexToBytes(lastHeader.Hash(), lastHeader.Index)) + batch.PutCurrentHeader(lastHeader.Hash(), lastHeader.Index) updateHeaderHeightMetric(len(bc.headerHashes) - 1) if _, err = batch.Persist(); err != nil { return err @@ -2299,13 +2299,6 @@ func (bc *Blockchain) ManagementContractHash() util.Uint160 { return bc.contracts.Management.Hash } -func hashAndIndexToBytes(h util.Uint256, index uint32) []byte { - buf := io.NewBufBinWriter() - buf.WriteBytes(h.BytesLE()) - buf.WriteU32LE(index) - return buf.Bytes() -} - func (bc *Blockchain) newInteropContext(trigger trigger.Type, d *dao.Simple, block *block.Block, tx *transaction.Transaction) *interop.Context { ic := interop.NewContext(trigger, bc, d, bc.contracts.Management.GetContract, bc.contracts.Contracts, block, tx, bc.log) ic.Functions = systemInterops diff --git a/pkg/core/dao/dao.go b/pkg/core/dao/dao.go index af781d713..4b7520d45 100644 --- a/pkg/core/dao/dao.go +++ b/pkg/core/dao/dao.go @@ -559,8 +559,11 @@ func (dao *Simple) PutVersion(v Version) { } // PutCurrentHeader stores current header. -func (dao *Simple) PutCurrentHeader(hashAndIndex []byte) { - dao.Store.Put(storage.SYSCurrentHeader.Bytes(), hashAndIndex) +func (dao *Simple) PutCurrentHeader(h util.Uint256, index uint32) { + buf := dao.getDataBuf() + buf.WriteBytes(h.BytesLE()) + buf.WriteU32LE(index) + dao.Store.Put(storage.SYSCurrentHeader.Bytes(), buf.Bytes()) } // PutStateSyncPoint stores current state synchronisation point P.