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)