dao: improve PutCurrentHeader logic

Move serialization out of the core.
This commit is contained in:
Roman Khimov 2022-02-18 15:04:57 +03:00
parent 522229d731
commit 7223caf369
2 changed files with 7 additions and 11 deletions

View file

@ -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

View file

@ -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.