From 6d50f37a12908efaa575c1a7727c0209c8ff61aa Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Thu, 20 May 2021 00:53:58 +0300 Subject: [PATCH] Implement storing latest block hash in blockpool storage --- blockchain/pool/blockpool.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/blockchain/pool/blockpool.go b/blockchain/pool/blockpool.go index f8938f1..e9060aa 100644 --- a/blockchain/pool/blockpool.go +++ b/blockchain/pool/blockpool.go @@ -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)