From 65f1d4eab7209344854de25165c9b167345fdac8 Mon Sep 17 00:00:00 2001 From: Kleissner Date: Sat, 4 Dec 2021 03:09:27 +0100 Subject: [PATCH] Blockchain: Add functions GetBlockRaw and DecodeBlockRaw --- blockchain/Blockchain.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/blockchain/Blockchain.go b/blockchain/Blockchain.go index 12027bd..ec71e4e 100644 --- a/blockchain/Blockchain.go +++ b/blockchain/Blockchain.go @@ -394,3 +394,32 @@ func (blockchain *Blockchain) DeleteBlockchain() (status int, err error) { return StatusOK, nil } + +// GetBlockRaw returns the encoded block from the blockchain. Status is StatusX. +func (blockchain *Blockchain) GetBlockRaw(number uint64) (data []byte, status int, err error) { + if number >= blockchain.height { + return nil, StatusBlockNotFound, errors.New("block number exceeds blockchain height") + } + + blockRaw, found := blockchain.database.Get(blockNumberToKey(number)) + if !found || len(blockRaw) == 0 { + return nil, StatusBlockNotFound, errors.New("block not found") + } + + return blockRaw, StatusOK, nil +} + +// DecodeBlockRaw decodes the raw block. Status is StatusX. +func DecodeBlockRaw(blockRaw []byte) (decoded *BlockDecoded, status int, err error) { + block, err := decodeBlock(blockRaw) + if err != nil { + return nil, StatusCorruptBlock, err + } + + decoded, err = decodeBlockRecords(block) + if err != nil { + return nil, StatusCorruptBlock, err + } + + return decoded, StatusOK, nil +}