dao: deduplicate header->KV conversion
This commit is contained in:
parent
d8cf879499
commit
b60d4ff191
2 changed files with 25 additions and 18 deletions
|
@ -932,7 +932,6 @@ func (bc *Blockchain) addHeaders(verify bool, headers ...*block.Header) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := io.NewBufBinWriter()
|
|
||||||
bc.headerHashesLock.Lock()
|
bc.headerHashesLock.Lock()
|
||||||
defer bc.headerHashesLock.Unlock()
|
defer bc.headerHashesLock.Unlock()
|
||||||
oldlen := len(bc.headerHashes)
|
oldlen := len(bc.headerHashes)
|
||||||
|
@ -941,21 +940,16 @@ func (bc *Blockchain) addHeaders(verify bool, headers ...*block.Header) error {
|
||||||
if int(h.Index) != len(bc.headerHashes) {
|
if int(h.Index) != len(bc.headerHashes) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
bc.headerHashes = append(bc.headerHashes, h.Hash())
|
err = batch.StoreHeader(h)
|
||||||
buf.WriteB(storage.ExecBlock)
|
if err != nil {
|
||||||
h.EncodeBinary(buf.BinWriter)
|
return err
|
||||||
buf.BinWriter.WriteB(0)
|
|
||||||
if buf.Err != nil {
|
|
||||||
return buf.Err
|
|
||||||
}
|
}
|
||||||
|
bc.headerHashes = append(bc.headerHashes, h.Hash())
|
||||||
key := storage.AppendPrefix(storage.DataExecutable, h.Hash().BytesBE())
|
|
||||||
batch.Store.Put(key, buf.Bytes())
|
|
||||||
buf.Reset()
|
|
||||||
lastHeader = h
|
lastHeader = h
|
||||||
}
|
}
|
||||||
|
|
||||||
if oldlen != len(bc.headerHashes) {
|
if oldlen != len(bc.headerHashes) {
|
||||||
|
buf := io.NewBufBinWriter()
|
||||||
for int(lastHeader.Index)-headerBatchCount >= int(bc.storedHeaderCount) {
|
for int(lastHeader.Index)-headerBatchCount >= int(bc.storedHeaderCount) {
|
||||||
buf.WriteArray(bc.headerHashes[bc.storedHeaderCount : bc.storedHeaderCount+headerBatchCount])
|
buf.WriteArray(bc.headerHashes[bc.storedHeaderCount : bc.storedHeaderCount+headerBatchCount])
|
||||||
if buf.Err != nil {
|
if buf.Err != nil {
|
||||||
|
|
|
@ -668,14 +668,10 @@ func (dao *Simple) DeleteBlock(h util.Uint256) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
w := dao.getDataBuf()
|
err = dao.storeHeader(key, &b.Header)
|
||||||
w.WriteB(storage.ExecBlock)
|
if err != nil {
|
||||||
b.Header.EncodeBinary(w.BinWriter)
|
return err
|
||||||
w.BinWriter.WriteB(0)
|
|
||||||
if w.Err != nil {
|
|
||||||
return w.Err
|
|
||||||
}
|
}
|
||||||
dao.Store.Put(key, w.Bytes())
|
|
||||||
|
|
||||||
for _, tx := range b.Transactions {
|
for _, tx := range b.Transactions {
|
||||||
copy(key[1:], tx.Hash().BytesBE())
|
copy(key[1:], tx.Hash().BytesBE())
|
||||||
|
@ -692,6 +688,23 @@ func (dao *Simple) DeleteBlock(h util.Uint256) error {
|
||||||
return nil
|
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
|
// StoreAsCurrentBlock stores a hash of the given block with prefix
|
||||||
// SYSCurrentBlock. It can reuse given buffer for the purpose of value
|
// SYSCurrentBlock. It can reuse given buffer for the purpose of value
|
||||||
// serialization.
|
// serialization.
|
||||||
|
|
Loading…
Reference in a new issue