mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-17 02:47:51 +01:00
Blockchain: Add functions GetBlockRaw and DecodeBlockRaw
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user