core/rawdb: rewrite the wrapper, pass common.Hash

This commit is contained in:
Delweng Zheng 2018-06-07 19:27:02 +08:00
parent 863d755854
commit e9ba0b3f40
4 changed files with 44 additions and 44 deletions

View file

@ -52,7 +52,7 @@ func DeleteCanonicalHash(db DatabaseDeleter, number uint64) {
// ReadHeaderNumber returns the header number assigned to a hash. // ReadHeaderNumber returns the header number assigned to a hash.
func ReadHeaderNumber(db DatabaseReader, hash common.Hash) *uint64 { func ReadHeaderNumber(db DatabaseReader, hash common.Hash) *uint64 {
data, _ := db.Get(headerNumberKey(hash.Bytes())) data, _ := db.Get(headerNumberKey(hash))
if len(data) != 8 { if len(data) != 8 {
return nil return nil
} }
@ -128,13 +128,13 @@ func WriteFastTrieProgress(db DatabaseWriter, count uint64) {
// ReadHeaderRLP retrieves a block header in its raw RLP database encoding. // ReadHeaderRLP retrieves a block header in its raw RLP database encoding.
func ReadHeaderRLP(db DatabaseReader, hash common.Hash, number uint64) rlp.RawValue { func ReadHeaderRLP(db DatabaseReader, hash common.Hash, number uint64) rlp.RawValue {
data, _ := db.Get(headerKey(number, hash.Bytes())) data, _ := db.Get(headerKey(number, hash))
return data return data
} }
// HasHeader verifies the existence of a block header corresponding to the hash. // HasHeader verifies the existence of a block header corresponding to the hash.
func HasHeader(db DatabaseReader, hash common.Hash, number uint64) bool { func HasHeader(db DatabaseReader, hash common.Hash, number uint64) bool {
if has, err := db.Has(headerKey(number, hash.Bytes())); !has || err != nil { if has, err := db.Has(headerKey(number, hash)); !has || err != nil {
return false return false
} }
return true return true
@ -159,7 +159,7 @@ func ReadHeader(db DatabaseReader, hash common.Hash, number uint64) *types.Heade
func WriteHeader(db DatabaseWriter, header *types.Header) { func WriteHeader(db DatabaseWriter, header *types.Header) {
// Write the hash -> number mapping // Write the hash -> number mapping
var ( var (
hash = header.Hash().Bytes() hash = header.Hash()
number = header.Number.Uint64() number = header.Number.Uint64()
encoded = encodeBlockNumber(number) encoded = encodeBlockNumber(number)
) )
@ -180,30 +180,30 @@ func WriteHeader(db DatabaseWriter, header *types.Header) {
// DeleteHeader removes all block header data associated with a hash. // DeleteHeader removes all block header data associated with a hash.
func DeleteHeader(db DatabaseDeleter, hash common.Hash, number uint64) { func DeleteHeader(db DatabaseDeleter, hash common.Hash, number uint64) {
if err := db.Delete(headerKey(number, hash.Bytes())); err != nil { if err := db.Delete(headerKey(number, hash)); err != nil {
log.Crit("Failed to delete header", "err", err) log.Crit("Failed to delete header", "err", err)
} }
if err := db.Delete(headerNumberKey(hash.Bytes())); err != nil { if err := db.Delete(headerNumberKey(hash)); err != nil {
log.Crit("Failed to delete hash to number mapping", "err", err) log.Crit("Failed to delete hash to number mapping", "err", err)
} }
} }
// ReadBodyRLP retrieves the block body (transactions and uncles) in RLP encoding. // ReadBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
func ReadBodyRLP(db DatabaseReader, hash common.Hash, number uint64) rlp.RawValue { func ReadBodyRLP(db DatabaseReader, hash common.Hash, number uint64) rlp.RawValue {
data, _ := db.Get(blockBodyKey(number, hash.Bytes())) data, _ := db.Get(blockBodyKey(number, hash))
return data return data
} }
// WriteBodyRLP stores an RLP encoded block body into the database. // WriteBodyRLP stores an RLP encoded block body into the database.
func WriteBodyRLP(db DatabaseWriter, hash common.Hash, number uint64, rlp rlp.RawValue) { func WriteBodyRLP(db DatabaseWriter, hash common.Hash, number uint64, rlp rlp.RawValue) {
if err := db.Put(blockBodyKey(number, hash.Bytes()), rlp); err != nil { if err := db.Put(blockBodyKey(number, hash), rlp); err != nil {
log.Crit("Failed to store block body", "err", err) log.Crit("Failed to store block body", "err", err)
} }
} }
// HasBody verifies the existence of a block body corresponding to the hash. // HasBody verifies the existence of a block body corresponding to the hash.
func HasBody(db DatabaseReader, hash common.Hash, number uint64) bool { func HasBody(db DatabaseReader, hash common.Hash, number uint64) bool {
if has, err := db.Has(blockBodyKey(number, hash.Bytes())); !has || err != nil { if has, err := db.Has(blockBodyKey(number, hash)); !has || err != nil {
return false return false
} }
return true return true
@ -234,14 +234,14 @@ func WriteBody(db DatabaseWriter, hash common.Hash, number uint64, body *types.B
// DeleteBody removes all block body data associated with a hash. // DeleteBody removes all block body data associated with a hash.
func DeleteBody(db DatabaseDeleter, hash common.Hash, number uint64) { func DeleteBody(db DatabaseDeleter, hash common.Hash, number uint64) {
if err := db.Delete(blockBodyKey(number, hash.Bytes())); err != nil { if err := db.Delete(blockBodyKey(number, hash)); err != nil {
log.Crit("Failed to delete block body", "err", err) log.Crit("Failed to delete block body", "err", err)
} }
} }
// ReadTd retrieves a block's total difficulty corresponding to the hash. // ReadTd retrieves a block's total difficulty corresponding to the hash.
func ReadTd(db DatabaseReader, hash common.Hash, number uint64) *big.Int { func ReadTd(db DatabaseReader, hash common.Hash, number uint64) *big.Int {
data, _ := db.Get(headerTDKey(number, hash.Bytes())) data, _ := db.Get(headerTDKey(number, hash))
if len(data) == 0 { if len(data) == 0 {
return nil return nil
} }
@ -259,14 +259,14 @@ func WriteTd(db DatabaseWriter, hash common.Hash, number uint64, td *big.Int) {
if err != nil { if err != nil {
log.Crit("Failed to RLP encode block total difficulty", "err", err) log.Crit("Failed to RLP encode block total difficulty", "err", err)
} }
if err := db.Put(headerTDKey(number, hash.Bytes()), data); err != nil { if err := db.Put(headerTDKey(number, hash), data); err != nil {
log.Crit("Failed to store block total difficulty", "err", err) log.Crit("Failed to store block total difficulty", "err", err)
} }
} }
// DeleteTd removes all block total difficulty data associated with a hash. // DeleteTd removes all block total difficulty data associated with a hash.
func DeleteTd(db DatabaseDeleter, hash common.Hash, number uint64) { func DeleteTd(db DatabaseDeleter, hash common.Hash, number uint64) {
if err := db.Delete(headerTDKey(number, hash.Bytes())); err != nil { if err := db.Delete(headerTDKey(number, hash)); err != nil {
log.Crit("Failed to delete block total difficulty", "err", err) log.Crit("Failed to delete block total difficulty", "err", err)
} }
} }
@ -274,7 +274,7 @@ func DeleteTd(db DatabaseDeleter, hash common.Hash, number uint64) {
// ReadReceipts retrieves all the transaction receipts belonging to a block. // ReadReceipts retrieves all the transaction receipts belonging to a block.
func ReadReceipts(db DatabaseReader, hash common.Hash, number uint64) types.Receipts { func ReadReceipts(db DatabaseReader, hash common.Hash, number uint64) types.Receipts {
// Retrieve the flattened receipt slice // Retrieve the flattened receipt slice
data, _ := db.Get(blockReceiptsKey(number, hash.Bytes())) data, _ := db.Get(blockReceiptsKey(number, hash))
if len(data) == 0 { if len(data) == 0 {
return nil return nil
} }
@ -303,14 +303,14 @@ func WriteReceipts(db DatabaseWriter, hash common.Hash, number uint64, receipts
log.Crit("Failed to encode block receipts", "err", err) log.Crit("Failed to encode block receipts", "err", err)
} }
// Store the flattened receipt slice // Store the flattened receipt slice
if err := db.Put(blockReceiptsKey(number, hash.Bytes()), bytes); err != nil { if err := db.Put(blockReceiptsKey(number, hash), bytes); err != nil {
log.Crit("Failed to store block receipts", "err", err) log.Crit("Failed to store block receipts", "err", err)
} }
} }
// DeleteReceipts removes all receipt data associated with a block hash. // DeleteReceipts removes all receipt data associated with a block hash.
func DeleteReceipts(db DatabaseDeleter, hash common.Hash, number uint64) { func DeleteReceipts(db DatabaseDeleter, hash common.Hash, number uint64) {
if err := db.Delete(blockReceiptsKey(number, hash.Bytes())); err != nil { if err := db.Delete(blockReceiptsKey(number, hash)); err != nil {
log.Crit("Failed to delete block receipts", "err", err) log.Crit("Failed to delete block receipts", "err", err)
} }
} }

View file

@ -26,7 +26,7 @@ import (
// ReadTxLookupEntry retrieves the positional metadata associated with a transaction // ReadTxLookupEntry retrieves the positional metadata associated with a transaction
// hash to allow retrieving the transaction or receipt by hash. // hash to allow retrieving the transaction or receipt by hash.
func ReadTxLookupEntry(db DatabaseReader, hash common.Hash) (common.Hash, uint64, uint64) { func ReadTxLookupEntry(db DatabaseReader, hash common.Hash) (common.Hash, uint64, uint64) {
data, _ := db.Get(txLookupKey(hash.Bytes())) data, _ := db.Get(txLookupKey(hash))
if len(data) == 0 { if len(data) == 0 {
return common.Hash{}, 0, 0 return common.Hash{}, 0, 0
} }
@ -51,7 +51,7 @@ func WriteTxLookupEntries(db DatabaseWriter, block *types.Block) {
if err != nil { if err != nil {
log.Crit("Failed to encode transaction lookup entry", "err", err) log.Crit("Failed to encode transaction lookup entry", "err", err)
} }
if err := db.Put(txLookupKey(tx.Hash().Bytes()), data); err != nil { if err := db.Put(txLookupKey(tx.Hash()), data); err != nil {
log.Crit("Failed to store transaction lookup entry", "err", err) log.Crit("Failed to store transaction lookup entry", "err", err)
} }
} }
@ -59,7 +59,7 @@ func WriteTxLookupEntries(db DatabaseWriter, block *types.Block) {
// DeleteTxLookupEntry removes all transaction data associated with a hash. // DeleteTxLookupEntry removes all transaction data associated with a hash.
func DeleteTxLookupEntry(db DatabaseDeleter, hash common.Hash) { func DeleteTxLookupEntry(db DatabaseDeleter, hash common.Hash) {
db.Delete(txLookupKey(hash.Bytes())) db.Delete(txLookupKey(hash))
} }
// ReadTransaction retrieves a specific transaction from the database, along with // ReadTransaction retrieves a specific transaction from the database, along with
@ -95,13 +95,13 @@ func ReadReceipt(db DatabaseReader, hash common.Hash) (*types.Receipt, common.Ha
// ReadBloomBits retrieves the compressed bloom bit vector belonging to the given // ReadBloomBits retrieves the compressed bloom bit vector belonging to the given
// section and bit index from the. // section and bit index from the.
func ReadBloomBits(db DatabaseReader, bit uint, section uint64, head common.Hash) ([]byte, error) { func ReadBloomBits(db DatabaseReader, bit uint, section uint64, head common.Hash) ([]byte, error) {
return db.Get(bloomBitsKey(bit, section, head.Bytes())) return db.Get(bloomBitsKey(bit, section, head))
} }
// WriteBloomBits stores the compressed bloom bits vector belonging to the given // WriteBloomBits stores the compressed bloom bits vector belonging to the given
// section and bit index. // section and bit index.
func WriteBloomBits(db DatabaseWriter, bit uint, section uint64, head common.Hash, bits []byte) { func WriteBloomBits(db DatabaseWriter, bit uint, section uint64, head common.Hash, bits []byte) {
if err := db.Put(bloomBitsKey(bit, section, head.Bytes()), bits); err != nil { if err := db.Put(bloomBitsKey(bit, section, head), bits); err != nil {
log.Crit("Failed to store bloom bits", "err", err) log.Crit("Failed to store bloom bits", "err", err)
} }
} }

View file

@ -45,7 +45,7 @@ func WriteDatabaseVersion(db DatabaseWriter, version int) {
// ReadChainConfig retrieves the consensus settings based on the given genesis hash. // ReadChainConfig retrieves the consensus settings based on the given genesis hash.
func ReadChainConfig(db DatabaseReader, hash common.Hash) *params.ChainConfig { func ReadChainConfig(db DatabaseReader, hash common.Hash) *params.ChainConfig {
data, _ := db.Get(configKey(hash.Bytes())) data, _ := db.Get(configKey(hash))
if len(data) == 0 { if len(data) == 0 {
return nil return nil
} }
@ -66,14 +66,14 @@ func WriteChainConfig(db DatabaseWriter, hash common.Hash, cfg *params.ChainConf
if err != nil { if err != nil {
log.Crit("Failed to JSON encode chain config", "err", err) log.Crit("Failed to JSON encode chain config", "err", err)
} }
if err := db.Put(configKey(hash.Bytes()), data); err != nil { if err := db.Put(configKey(hash), data); err != nil {
log.Crit("Failed to store chain config", "err", err) log.Crit("Failed to store chain config", "err", err)
} }
} }
// ReadPreimage retrieves a single preimage of the provided hash. // ReadPreimage retrieves a single preimage of the provided hash.
func ReadPreimage(db DatabaseReader, hash common.Hash) []byte { func ReadPreimage(db DatabaseReader, hash common.Hash) []byte {
data, _ := db.Get(preimageKey(hash.Bytes())) data, _ := db.Get(preimageKey(hash))
return data return data
} }
@ -81,7 +81,7 @@ func ReadPreimage(db DatabaseReader, hash common.Hash) []byte {
// current block number, and is used for debug messages only. // current block number, and is used for debug messages only.
func WritePreimages(db DatabaseWriter, number uint64, preimages map[common.Hash][]byte) { func WritePreimages(db DatabaseWriter, number uint64, preimages map[common.Hash][]byte) {
for hash, preimage := range preimages { for hash, preimage := range preimages {
if err := db.Put(preimageKey(hash.Bytes()), preimage); err != nil { if err := db.Put(preimageKey(hash), preimage); err != nil {
log.Crit("Failed to store trie preimage", "err", err) log.Crit("Failed to store trie preimage", "err", err)
} }
} }

View file

@ -79,43 +79,43 @@ func encodeBlockNumber(number uint64) []byte {
} }
// headerKey = headerPrefix + num (uint64 big endian) + hash // headerKey = headerPrefix + num (uint64 big endian) + hash
func headerKey(number uint64, suffix []byte) []byte { func headerKey(number uint64, hash common.Hash) []byte {
return append(append(headerPrefix, encodeBlockNumber(number)...), suffix...) return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
} }
// headerTDKey = headerPrefix + num (uint64 big endian) + hash + headerTDSuffix // headerTDKey = headerPrefix + num (uint64 big endian) + hash + headerTDSuffix
func headerTDKey(number uint64, hashBytes []byte) []byte { func headerTDKey(number uint64, hash common.Hash) []byte {
return append(headerKey(number, hashBytes), headerTDSuffix...) return append(headerKey(number, hash), headerTDSuffix...)
} }
// headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix // headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
func headerHashKey(number uint64) []byte { func headerHashKey(number uint64) []byte {
return headerKey(number, headerHashSuffix) return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)
} }
// headerNumberKey = headerNumberPrefix + hash // headerNumberKey = headerNumberPrefix + hash
func headerNumberKey(hashBytes []byte) []byte { func headerNumberKey(hash common.Hash) []byte {
return append(headerNumberPrefix, hashBytes...) return append(headerNumberPrefix, hash.Bytes()...)
} }
// blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash // blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
func blockBodyKey(number uint64, hashBytes []byte) []byte { func blockBodyKey(number uint64, hash common.Hash) []byte {
return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hashBytes...) return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
} }
// blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash // blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
func blockReceiptsKey(number uint64, hashBytes []byte) []byte { func blockReceiptsKey(number uint64, hash common.Hash) []byte {
return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hashBytes...) return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
} }
// txLookupKey = txLookupPrefix + hash // txLookupKey = txLookupPrefix + hash
func txLookupKey(hashBytes []byte) []byte { func txLookupKey(hash common.Hash) []byte {
return append(txLookupPrefix, hashBytes...) return append(txLookupPrefix, hash.Bytes()...)
} }
// bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash // bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash
func bloomBitsKey(bit uint, section uint64, hashBytes []byte) []byte { func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte {
key := append(append(bloomBitsPrefix, make([]byte, 10)...), hashBytes...) key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...)
binary.BigEndian.PutUint16(key[1:], uint16(bit)) binary.BigEndian.PutUint16(key[1:], uint16(bit))
binary.BigEndian.PutUint64(key[3:], section) binary.BigEndian.PutUint64(key[3:], section)
@ -124,11 +124,11 @@ func bloomBitsKey(bit uint, section uint64, hashBytes []byte) []byte {
} }
// preimageKey = preimagePrefix + hash // preimageKey = preimagePrefix + hash
func preimageKey(hashBytes []byte) []byte { func preimageKey(hash common.Hash) []byte {
return append(preimagePrefix, hashBytes...) return append(preimagePrefix, hash.Bytes()...)
} }
// configKey = configPrefix + hash // configKey = configPrefix + hash
func configKey(hashBytes []byte) []byte { func configKey(hash common.Hash) []byte {
return append(configPrefix, hashBytes...) return append(configPrefix, hash.Bytes()...)
} }