From d2db58d7481acb8b4a317487c8719850279a5aa6 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 18 Feb 2022 14:54:05 +0300 Subject: [PATCH] dao: move header hash store logic out of the core Which allows for more efficient buffer use along the way. --- pkg/core/blockchain.go | 11 ++++------- pkg/core/dao/dao.go | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 28e7715f4..346905870 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -949,15 +949,12 @@ func (bc *Blockchain) addHeaders(verify bool, headers ...*block.Header) error { } if oldlen != len(bc.headerHashes) { - buf := io.NewBufBinWriter() for int(lastHeader.Index)-headerBatchCount >= int(bc.storedHeaderCount) { - buf.WriteArray(bc.headerHashes[bc.storedHeaderCount : bc.storedHeaderCount+headerBatchCount]) - if buf.Err != nil { - return buf.Err + err = batch.StoreHeaderHashes(bc.headerHashes[bc.storedHeaderCount:bc.storedHeaderCount+headerBatchCount], + bc.storedHeaderCount) + if err != nil { + return err } - - key := storage.AppendPrefixInt(storage.IXHeaderHashList, int(bc.storedHeaderCount)) - batch.Store.Put(key, buf.Bytes()) bc.storedHeaderCount += headerBatchCount } diff --git a/pkg/core/dao/dao.go b/pkg/core/dao/dao.go index 505e589dc..af781d713 100644 --- a/pkg/core/dao/dao.go +++ b/pkg/core/dao/dao.go @@ -590,6 +590,25 @@ func read2000Uint256Hashes(b []byte) ([]util.Uint256, error) { return hashes, nil } +func (dao *Simple) mkHeaderHashKey(h uint32) []byte { + b := dao.getKeyBuf(1 + 4) + b[0] = byte(storage.IXHeaderHashList) + binary.BigEndian.PutUint32(b[1:], h) + return b +} + +// StoreHeaderHashes pushes a batch of header hashes into the store. +func (dao *Simple) StoreHeaderHashes(hashes []util.Uint256, height uint32) error { + key := dao.mkHeaderHashKey(height) + buf := dao.getDataBuf() + buf.WriteArray(hashes) + if buf.Err != nil { + return buf.Err + } + dao.Store.Put(key, buf.Bytes()) + return nil +} + // HasTransaction returns nil if the given store does not contain the given // Transaction hash. It returns an error in case if transaction is in chain // or in the list of conflicting transactions.