Implement storing latest block hash in blockpool storage

This commit is contained in:
ChronosX88 2021-05-20 00:53:58 +03:00
parent 5e48c0d176
commit 6d50f37a12
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A

View File

@ -13,6 +13,7 @@ import (
const (
DefaultBlockPrefix = "block_"
DefaultBlockHeaderPrefix = "header_"
LatestBlockKey = "latest_block"
)
var (
@ -59,6 +60,28 @@ func NewBlockPool(path string) (*BlockPool, error) {
return pool, nil
}
func (bp *BlockPool) SetLatestBlock(hash []byte) error {
return bp.dbEnv.Update(func(txn *lmdb.Txn) error {
return txn.Put(bp.db, []byte(LatestBlockKey), hash, 0)
})
}
func (bp *BlockPool) GetLatestBlock() ([]byte, error) {
var hash []byte
err := bp.dbEnv.View(func(txn *lmdb.Txn) error {
data, err := txn.Get(bp.db, []byte(LatestBlockKey))
if err != nil {
if lmdb.IsNotFound(err) {
return nil
}
return err
}
hash = data
return nil
})
return hash, err
}
func (bp *BlockPool) StoreBlock(block *types2.Block) error {
return bp.dbEnv.Update(func(txn *lmdb.Txn) error {
data, err := cbor.Marshal(block)