ETH源码阅读(区块hash的存储)

区块的hash在db中存储:

key:h + number(区块高度) + n

value:block hash

1.通过区块高度生成key

go-ethereum/core/rawdb/schema.go

//h + number + n
// headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
func headerHashKey(number uint64) []byte {
    return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)
}
2.通过高度获得区块hash

go-ethereum/core/rawdb/accessors_chain.go

// ReadCanonicalHash retrieves the hash assigned to a canonical block number.
func ReadCanonicalHash(db DatabaseReader, number uint64) common.Hash {
    data, _ := db.Get(headerHashKey(number)) //通过key获得区块hash
    if len(data) == 0 {
        return common.Hash{}
    }
    return common.BytesToHash(data)
}

你可能感兴趣的:(ETH源码阅读(区块hash的存储))