|
|
@ -29,7 +29,7 @@ import ( |
|
|
|
|
|
|
|
|
|
|
|
// ReadCanonicalHash retrieves the hash assigned to a canonical block number.
|
|
|
|
// ReadCanonicalHash retrieves the hash assigned to a canonical block number.
|
|
|
|
func ReadCanonicalHash(db DatabaseReader, number uint64) common.Hash { |
|
|
|
func ReadCanonicalHash(db DatabaseReader, number uint64) common.Hash { |
|
|
|
data, _ := db.Get(append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)) |
|
|
|
data, _ := db.Get(headerHashKey(number)) |
|
|
|
if len(data) == 0 { |
|
|
|
if len(data) == 0 { |
|
|
|
return common.Hash{} |
|
|
|
return common.Hash{} |
|
|
|
} |
|
|
|
} |
|
|
@ -38,22 +38,21 @@ func ReadCanonicalHash(db DatabaseReader, number uint64) common.Hash { |
|
|
|
|
|
|
|
|
|
|
|
// WriteCanonicalHash stores the hash assigned to a canonical block number.
|
|
|
|
// WriteCanonicalHash stores the hash assigned to a canonical block number.
|
|
|
|
func WriteCanonicalHash(db DatabaseWriter, hash common.Hash, number uint64) { |
|
|
|
func WriteCanonicalHash(db DatabaseWriter, hash common.Hash, number uint64) { |
|
|
|
key := append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...) |
|
|
|
if err := db.Put(headerHashKey(number), hash.Bytes()); err != nil { |
|
|
|
if err := db.Put(key, hash.Bytes()); err != nil { |
|
|
|
|
|
|
|
log.Crit("Failed to store number to hash mapping", "err", err) |
|
|
|
log.Crit("Failed to store number to hash mapping", "err", err) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// DeleteCanonicalHash removes the number to hash canonical mapping.
|
|
|
|
// DeleteCanonicalHash removes the number to hash canonical mapping.
|
|
|
|
func DeleteCanonicalHash(db DatabaseDeleter, number uint64) { |
|
|
|
func DeleteCanonicalHash(db DatabaseDeleter, number uint64) { |
|
|
|
if err := db.Delete(append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)); err != nil { |
|
|
|
if err := db.Delete(headerHashKey(number)); err != nil { |
|
|
|
log.Crit("Failed to delete number to hash mapping", "err", err) |
|
|
|
log.Crit("Failed to delete number to hash mapping", "err", err) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 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(append(headerNumberPrefix, hash.Bytes()...)) |
|
|
|
data, _ := db.Get(headerNumberKey(hash)) |
|
|
|
if len(data) != 8 { |
|
|
|
if len(data) != 8 { |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
@ -129,14 +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(append(append(headerPrefix, encodeBlockNumber(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 { |
|
|
|
key := append(append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)) |
|
|
|
if has, err := db.Has(headerKey(number, hash)); !has || err != nil { |
|
|
|
if has, err := db.Has(key); !has || err != nil { |
|
|
|
|
|
|
|
return false |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
return true |
|
|
|
return true |
|
|
@ -161,11 +159,11 @@ 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) |
|
|
|
) |
|
|
|
) |
|
|
|
key := append(headerNumberPrefix, hash...) |
|
|
|
key := headerNumberKey(hash) |
|
|
|
if err := db.Put(key, encoded); err != nil { |
|
|
|
if err := db.Put(key, encoded); err != nil { |
|
|
|
log.Crit("Failed to store hash to number mapping", "err", err) |
|
|
|
log.Crit("Failed to store hash to number mapping", "err", err) |
|
|
|
} |
|
|
|
} |
|
|
@ -174,7 +172,7 @@ func WriteHeader(db DatabaseWriter, header *types.Header) { |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Crit("Failed to RLP encode header", "err", err) |
|
|
|
log.Crit("Failed to RLP encode header", "err", err) |
|
|
|
} |
|
|
|
} |
|
|
|
key = append(append(headerPrefix, encoded...), hash...) |
|
|
|
key = headerKey(number, hash) |
|
|
|
if err := db.Put(key, data); err != nil { |
|
|
|
if err := db.Put(key, data); err != nil { |
|
|
|
log.Crit("Failed to store header", "err", err) |
|
|
|
log.Crit("Failed to store header", "err", err) |
|
|
|
} |
|
|
|
} |
|
|
@ -182,32 +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(append(append(headerPrefix, encodeBlockNumber(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(append(headerNumberPrefix, 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(append(append(blockBodyPrefix, encodeBlockNumber(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) { |
|
|
|
key := append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...) |
|
|
|
if err := db.Put(blockBodyKey(number, hash), rlp); err != nil { |
|
|
|
if err := db.Put(key, 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 { |
|
|
|
key := append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...) |
|
|
|
if has, err := db.Has(blockBodyKey(number, hash)); !has || err != nil { |
|
|
|
if has, err := db.Has(key); !has || err != nil { |
|
|
|
|
|
|
|
return false |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
return true |
|
|
|
return true |
|
|
@ -238,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(append(append(blockBodyPrefix, encodeBlockNumber(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(append(append(append(headerPrefix, encodeBlockNumber(number)...), hash[:]...), headerTDSuffix...)) |
|
|
|
data, _ := db.Get(headerTDKey(number, hash)) |
|
|
|
if len(data) == 0 { |
|
|
|
if len(data) == 0 { |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
@ -263,15 +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) |
|
|
|
} |
|
|
|
} |
|
|
|
key := append(append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...), headerTDSuffix...) |
|
|
|
if err := db.Put(headerTDKey(number, hash), data); err != nil { |
|
|
|
if err := db.Put(key, 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(append(append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...), headerTDSuffix...)); 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) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -279,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(append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash[:]...)) |
|
|
|
data, _ := db.Get(blockReceiptsKey(number, hash)) |
|
|
|
if len(data) == 0 { |
|
|
|
if len(data) == 0 { |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
@ -308,15 +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
|
|
|
|
key := append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...) |
|
|
|
if err := db.Put(blockReceiptsKey(number, hash), bytes); err != nil { |
|
|
|
if err := db.Put(key, 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(append(append(blockReceiptsPrefix, encodeBlockNumber(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) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|