2023-08-04 11:10:09 +00:00
|
|
|
import eth/[rlp, common], core_db
|
2020-06-10 05:54:15 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
headerPrefix = 'h'.byte # headerPrefix + num (uint64 big endian) + hash -> header
|
|
|
|
headerHashSuffix = 'n'.byte # headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash
|
|
|
|
blockBodyPrefix = 'b'.byte # blockBodyPrefix + num (uint64 big endian) + hash -> block body
|
|
|
|
|
2023-08-04 11:10:09 +00:00
|
|
|
proc headerHash*(db: CoreDbRef, number: uint64): Hash256 =
|
2020-06-10 05:54:15 +00:00
|
|
|
var key: array[10, byte]
|
|
|
|
key[0] = headerPrefix
|
|
|
|
key[1..8] = toBytesBE(number)[0..^1]
|
|
|
|
key[^1] = headerHashSuffix
|
2023-08-04 11:10:09 +00:00
|
|
|
let res = db.kvt.get(key)
|
2020-06-10 05:54:15 +00:00
|
|
|
doAssert(res.len == 32)
|
|
|
|
result.data[0..31] = res[0..31]
|
|
|
|
|
2023-08-04 11:10:09 +00:00
|
|
|
proc blockHeader*(db: CoreDbRef, hash: Hash256, number: uint64): BlockHeader =
|
2020-06-10 05:54:15 +00:00
|
|
|
var key: array[41, byte]
|
|
|
|
key[0] = headerPrefix
|
|
|
|
key[1..8] = toBytesBE(number)[0..^1]
|
|
|
|
key[9..40] = hash.data[0..^1]
|
2023-08-04 11:10:09 +00:00
|
|
|
let res = db.kvt.get(key)
|
2020-06-10 05:54:15 +00:00
|
|
|
result = rlp.decode(res, BlockHeader)
|
|
|
|
|
2023-08-04 11:10:09 +00:00
|
|
|
proc blockHeader*(db: CoreDbRef, number: uint64): BlockHeader =
|
2020-06-10 05:54:15 +00:00
|
|
|
let hash = db.headerHash(number)
|
|
|
|
db.blockHeader(hash, number)
|
|
|
|
|
2023-08-04 11:10:09 +00:00
|
|
|
proc blockBody*(db: CoreDbRef, hash: Hash256, number: uint64): BlockBody =
|
2020-06-10 05:54:15 +00:00
|
|
|
var key: array[41, byte]
|
|
|
|
key[0] = blockBodyPrefix
|
|
|
|
key[1..8] = toBytesBE(number)[0..^1]
|
|
|
|
key[9..40] = hash.data[0..^1]
|
2023-08-04 11:10:09 +00:00
|
|
|
let res = db.kvt.get(key)
|
2020-06-10 05:54:15 +00:00
|
|
|
result = rlp.decode(res, BlockBody)
|