dao: deduplicate header->KV conversion

This commit is contained in:
Roman Khimov 2022-02-18 11:55:06 +03:00
parent d8cf879499
commit b60d4ff191
2 changed files with 25 additions and 18 deletions

View file

@ -668,14 +668,10 @@ func (dao *Simple) DeleteBlock(h util.Uint256) error {
return err
}
w := dao.getDataBuf()
w.WriteB(storage.ExecBlock)
b.Header.EncodeBinary(w.BinWriter)
w.BinWriter.WriteB(0)
if w.Err != nil {
return w.Err
err = dao.storeHeader(key, &b.Header)
if err != nil {
return err
}
dao.Store.Put(key, w.Bytes())
for _, tx := range b.Transactions {
copy(key[1:], tx.Hash().BytesBE())
@ -692,6 +688,23 @@ func (dao *Simple) DeleteBlock(h util.Uint256) error {
return nil
}
// StoreHeader saves block header into the store.
func (dao *Simple) StoreHeader(h *block.Header) error {
return dao.storeHeader(dao.makeExecutableKey(h.Hash()), h)
}
func (dao *Simple) storeHeader(key []byte, h *block.Header) error {
buf := dao.getDataBuf()
buf.WriteB(storage.ExecBlock)
h.EncodeBinary(buf.BinWriter)
buf.BinWriter.WriteB(0)
if buf.Err != nil {
return buf.Err
}
dao.Store.Put(key, buf.Bytes())
return nil
}
// StoreAsCurrentBlock stores a hash of the given block with prefix
// SYSCurrentBlock. It can reuse given buffer for the purpose of value
// serialization.