|
@@ -33,21 +33,41 @@ import (
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+// DatabaseReader wraps the Get method of a backing data store.
|
|
|
|
|
+type DatabaseReader interface {
|
|
|
|
|
+ Get(key []byte) (value []byte, err error)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// DatabaseWriter wraps the Put method of a backing data store.
|
|
|
|
|
+type DatabaseWriter interface {
|
|
|
|
|
+ Put(key, value []byte) error
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// DatabaseDeleter wraps the Delete method of a backing data store.
|
|
|
|
|
+type DatabaseDeleter interface {
|
|
|
|
|
+ Delete(key []byte) error
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
var (
|
|
var (
|
|
|
headHeaderKey = []byte("LastHeader")
|
|
headHeaderKey = []byte("LastHeader")
|
|
|
headBlockKey = []byte("LastBlock")
|
|
headBlockKey = []byte("LastBlock")
|
|
|
headFastKey = []byte("LastFast")
|
|
headFastKey = []byte("LastFast")
|
|
|
|
|
|
|
|
- headerPrefix = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
|
|
|
|
|
- tdSuffix = []byte("t") // headerPrefix + num (uint64 big endian) + hash + tdSuffix -> td
|
|
|
|
|
- numSuffix = []byte("n") // headerPrefix + num (uint64 big endian) + numSuffix -> hash
|
|
|
|
|
- blockHashPrefix = []byte("H") // blockHashPrefix + hash -> num (uint64 big endian)
|
|
|
|
|
- bodyPrefix = []byte("b") // bodyPrefix + num (uint64 big endian) + hash -> block body
|
|
|
|
|
- blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
|
|
|
|
|
- lookupPrefix = []byte("l") // lookupPrefix + hash -> transaction/receipt lookup metadata
|
|
|
|
|
- preimagePrefix = "secure-key-" // preimagePrefix + hash -> preimage
|
|
|
|
|
|
|
+ // Data item prefixes (use single byte to avoid mixing data types, avoid `i`).
|
|
|
|
|
+ headerPrefix = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
|
|
|
|
|
+ tdSuffix = []byte("t") // headerPrefix + num (uint64 big endian) + hash + tdSuffix -> td
|
|
|
|
|
+ numSuffix = []byte("n") // headerPrefix + num (uint64 big endian) + numSuffix -> hash
|
|
|
|
|
+ blockHashPrefix = []byte("H") // blockHashPrefix + hash -> num (uint64 big endian)
|
|
|
|
|
+ bodyPrefix = []byte("b") // bodyPrefix + num (uint64 big endian) + hash -> block body
|
|
|
|
|
+ blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
|
|
|
|
|
+ lookupPrefix = []byte("l") // lookupPrefix + hash -> transaction/receipt lookup metadata
|
|
|
|
|
+ bloomBitsPrefix = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
|
|
|
|
|
|
|
|
- configPrefix = []byte("ethereum-config-") // config prefix for the db
|
|
|
|
|
|
|
+ preimagePrefix = "secure-key-" // preimagePrefix + hash -> preimage
|
|
|
|
|
+ configPrefix = []byte("ethereum-config-") // config prefix for the db
|
|
|
|
|
+
|
|
|
|
|
+ // Chain index prefixes (use `i` + single byte to avoid mixing data types).
|
|
|
|
|
+ BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
|
|
|
|
|
|
|
|
// used by old db, now only used for conversion
|
|
// used by old db, now only used for conversion
|
|
|
oldReceiptsPrefix = []byte("receipts-")
|
|
oldReceiptsPrefix = []byte("receipts-")
|
|
@@ -57,8 +77,6 @@ var (
|
|
|
|
|
|
|
|
preimageCounter = metrics.NewCounter("db/preimage/total")
|
|
preimageCounter = metrics.NewCounter("db/preimage/total")
|
|
|
preimageHitCounter = metrics.NewCounter("db/preimage/hits")
|
|
preimageHitCounter = metrics.NewCounter("db/preimage/hits")
|
|
|
-
|
|
|
|
|
- bloomBitsPrefix = []byte("bloomBits-")
|
|
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
// txLookupEntry is a positional metadata to help looking up the data content of
|
|
// txLookupEntry is a positional metadata to help looking up the data content of
|
|
@@ -77,7 +95,7 @@ func encodeBlockNumber(number uint64) []byte {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// GetCanonicalHash retrieves a hash assigned to a canonical block number.
|
|
// GetCanonicalHash retrieves a hash assigned to a canonical block number.
|
|
|
-func GetCanonicalHash(db ethdb.Database, number uint64) common.Hash {
|
|
|
|
|
|
|
+func GetCanonicalHash(db DatabaseReader, number uint64) common.Hash {
|
|
|
data, _ := db.Get(append(append(headerPrefix, encodeBlockNumber(number)...), numSuffix...))
|
|
data, _ := db.Get(append(append(headerPrefix, encodeBlockNumber(number)...), numSuffix...))
|
|
|
if len(data) == 0 {
|
|
if len(data) == 0 {
|
|
|
return common.Hash{}
|
|
return common.Hash{}
|
|
@@ -91,7 +109,7 @@ const missingNumber = uint64(0xffffffffffffffff)
|
|
|
|
|
|
|
|
// GetBlockNumber returns the block number assigned to a block hash
|
|
// GetBlockNumber returns the block number assigned to a block hash
|
|
|
// if the corresponding header is present in the database
|
|
// if the corresponding header is present in the database
|
|
|
-func GetBlockNumber(db ethdb.Database, hash common.Hash) uint64 {
|
|
|
|
|
|
|
+func GetBlockNumber(db DatabaseReader, hash common.Hash) uint64 {
|
|
|
data, _ := db.Get(append(blockHashPrefix, hash.Bytes()...))
|
|
data, _ := db.Get(append(blockHashPrefix, hash.Bytes()...))
|
|
|
if len(data) != 8 {
|
|
if len(data) != 8 {
|
|
|
return missingNumber
|
|
return missingNumber
|
|
@@ -104,7 +122,7 @@ func GetBlockNumber(db ethdb.Database, hash common.Hash) uint64 {
|
|
|
// last block hash is only updated upon a full block import, the last header
|
|
// last block hash is only updated upon a full block import, the last header
|
|
|
// hash is updated already at header import, allowing head tracking for the
|
|
// hash is updated already at header import, allowing head tracking for the
|
|
|
// light synchronization mechanism.
|
|
// light synchronization mechanism.
|
|
|
-func GetHeadHeaderHash(db ethdb.Database) common.Hash {
|
|
|
|
|
|
|
+func GetHeadHeaderHash(db DatabaseReader) common.Hash {
|
|
|
data, _ := db.Get(headHeaderKey)
|
|
data, _ := db.Get(headHeaderKey)
|
|
|
if len(data) == 0 {
|
|
if len(data) == 0 {
|
|
|
return common.Hash{}
|
|
return common.Hash{}
|
|
@@ -113,7 +131,7 @@ func GetHeadHeaderHash(db ethdb.Database) common.Hash {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// GetHeadBlockHash retrieves the hash of the current canonical head block.
|
|
// GetHeadBlockHash retrieves the hash of the current canonical head block.
|
|
|
-func GetHeadBlockHash(db ethdb.Database) common.Hash {
|
|
|
|
|
|
|
+func GetHeadBlockHash(db DatabaseReader) common.Hash {
|
|
|
data, _ := db.Get(headBlockKey)
|
|
data, _ := db.Get(headBlockKey)
|
|
|
if len(data) == 0 {
|
|
if len(data) == 0 {
|
|
|
return common.Hash{}
|
|
return common.Hash{}
|
|
@@ -125,7 +143,7 @@ func GetHeadBlockHash(db ethdb.Database) common.Hash {
|
|
|
// fast synchronization. The difference between this and GetHeadBlockHash is that
|
|
// fast synchronization. The difference between this and GetHeadBlockHash is that
|
|
|
// whereas the last block hash is only updated upon a full block import, the last
|
|
// whereas the last block hash is only updated upon a full block import, the last
|
|
|
// fast hash is updated when importing pre-processed blocks.
|
|
// fast hash is updated when importing pre-processed blocks.
|
|
|
-func GetHeadFastBlockHash(db ethdb.Database) common.Hash {
|
|
|
|
|
|
|
+func GetHeadFastBlockHash(db DatabaseReader) common.Hash {
|
|
|
data, _ := db.Get(headFastKey)
|
|
data, _ := db.Get(headFastKey)
|
|
|
if len(data) == 0 {
|
|
if len(data) == 0 {
|
|
|
return common.Hash{}
|
|
return common.Hash{}
|
|
@@ -135,14 +153,14 @@ func GetHeadFastBlockHash(db ethdb.Database) common.Hash {
|
|
|
|
|
|
|
|
// GetHeaderRLP retrieves a block header in its raw RLP database encoding, or nil
|
|
// GetHeaderRLP retrieves a block header in its raw RLP database encoding, or nil
|
|
|
// if the header's not found.
|
|
// if the header's not found.
|
|
|
-func GetHeaderRLP(db ethdb.Database, hash common.Hash, number uint64) rlp.RawValue {
|
|
|
|
|
|
|
+func GetHeaderRLP(db DatabaseReader, hash common.Hash, number uint64) rlp.RawValue {
|
|
|
data, _ := db.Get(append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...))
|
|
data, _ := db.Get(append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...))
|
|
|
return data
|
|
return data
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// GetHeader retrieves the block header corresponding to the hash, nil if none
|
|
// GetHeader retrieves the block header corresponding to the hash, nil if none
|
|
|
// found.
|
|
// found.
|
|
|
-func GetHeader(db ethdb.Database, hash common.Hash, number uint64) *types.Header {
|
|
|
|
|
|
|
+func GetHeader(db DatabaseReader, hash common.Hash, number uint64) *types.Header {
|
|
|
data := GetHeaderRLP(db, hash, number)
|
|
data := GetHeaderRLP(db, hash, number)
|
|
|
if len(data) == 0 {
|
|
if len(data) == 0 {
|
|
|
return nil
|
|
return nil
|
|
@@ -156,14 +174,14 @@ func GetHeader(db ethdb.Database, hash common.Hash, number uint64) *types.Header
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// GetBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
|
|
// GetBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
|
|
|
-func GetBodyRLP(db ethdb.Database, hash common.Hash, number uint64) rlp.RawValue {
|
|
|
|
|
|
|
+func GetBodyRLP(db DatabaseReader, hash common.Hash, number uint64) rlp.RawValue {
|
|
|
data, _ := db.Get(append(append(bodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...))
|
|
data, _ := db.Get(append(append(bodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...))
|
|
|
return data
|
|
return data
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// GetBody retrieves the block body (transactons, uncles) corresponding to the
|
|
// GetBody retrieves the block body (transactons, uncles) corresponding to the
|
|
|
// hash, nil if none found.
|
|
// hash, nil if none found.
|
|
|
-func GetBody(db ethdb.Database, hash common.Hash, number uint64) *types.Body {
|
|
|
|
|
|
|
+func GetBody(db DatabaseReader, hash common.Hash, number uint64) *types.Body {
|
|
|
data := GetBodyRLP(db, hash, number)
|
|
data := GetBodyRLP(db, hash, number)
|
|
|
if len(data) == 0 {
|
|
if len(data) == 0 {
|
|
|
return nil
|
|
return nil
|
|
@@ -178,7 +196,7 @@ func GetBody(db ethdb.Database, hash common.Hash, number uint64) *types.Body {
|
|
|
|
|
|
|
|
// GetTd retrieves a block's total difficulty corresponding to the hash, nil if
|
|
// GetTd retrieves a block's total difficulty corresponding to the hash, nil if
|
|
|
// none found.
|
|
// none found.
|
|
|
-func GetTd(db ethdb.Database, hash common.Hash, number uint64) *big.Int {
|
|
|
|
|
|
|
+func GetTd(db DatabaseReader, hash common.Hash, number uint64) *big.Int {
|
|
|
data, _ := db.Get(append(append(append(headerPrefix, encodeBlockNumber(number)...), hash[:]...), tdSuffix...))
|
|
data, _ := db.Get(append(append(append(headerPrefix, encodeBlockNumber(number)...), hash[:]...), tdSuffix...))
|
|
|
if len(data) == 0 {
|
|
if len(data) == 0 {
|
|
|
return nil
|
|
return nil
|
|
@@ -197,7 +215,7 @@ func GetTd(db ethdb.Database, hash common.Hash, number uint64) *big.Int {
|
|
|
//
|
|
//
|
|
|
// Note, due to concurrent download of header and block body the header and thus
|
|
// Note, due to concurrent download of header and block body the header and thus
|
|
|
// canonical hash can be stored in the database but the body data not (yet).
|
|
// canonical hash can be stored in the database but the body data not (yet).
|
|
|
-func GetBlock(db ethdb.Database, hash common.Hash, number uint64) *types.Block {
|
|
|
|
|
|
|
+func GetBlock(db DatabaseReader, hash common.Hash, number uint64) *types.Block {
|
|
|
// Retrieve the block header and body contents
|
|
// Retrieve the block header and body contents
|
|
|
header := GetHeader(db, hash, number)
|
|
header := GetHeader(db, hash, number)
|
|
|
if header == nil {
|
|
if header == nil {
|
|
@@ -213,7 +231,7 @@ func GetBlock(db ethdb.Database, hash common.Hash, number uint64) *types.Block {
|
|
|
|
|
|
|
|
// GetBlockReceipts retrieves the receipts generated by the transactions included
|
|
// GetBlockReceipts retrieves the receipts generated by the transactions included
|
|
|
// in a block given by its hash.
|
|
// in a block given by its hash.
|
|
|
-func GetBlockReceipts(db ethdb.Database, hash common.Hash, number uint64) types.Receipts {
|
|
|
|
|
|
|
+func GetBlockReceipts(db DatabaseReader, hash common.Hash, number uint64) types.Receipts {
|
|
|
data, _ := db.Get(append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash[:]...))
|
|
data, _ := db.Get(append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash[:]...))
|
|
|
if len(data) == 0 {
|
|
if len(data) == 0 {
|
|
|
return nil
|
|
return nil
|
|
@@ -232,7 +250,7 @@ func GetBlockReceipts(db ethdb.Database, hash common.Hash, number uint64) types.
|
|
|
|
|
|
|
|
// GetTxLookupEntry retrieves the positional metadata associated with a transaction
|
|
// GetTxLookupEntry 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 GetTxLookupEntry(db ethdb.Database, hash common.Hash) (common.Hash, uint64, uint64) {
|
|
|
|
|
|
|
+func GetTxLookupEntry(db DatabaseReader, hash common.Hash) (common.Hash, uint64, uint64) {
|
|
|
// Load the positional metadata from disk and bail if it fails
|
|
// Load the positional metadata from disk and bail if it fails
|
|
|
data, _ := db.Get(append(lookupPrefix, hash.Bytes()...))
|
|
data, _ := db.Get(append(lookupPrefix, hash.Bytes()...))
|
|
|
if len(data) == 0 {
|
|
if len(data) == 0 {
|
|
@@ -249,7 +267,7 @@ func GetTxLookupEntry(db ethdb.Database, hash common.Hash) (common.Hash, uint64,
|
|
|
|
|
|
|
|
// GetTransaction retrieves a specific transaction from the database, along with
|
|
// GetTransaction retrieves a specific transaction from the database, along with
|
|
|
// its added positional metadata.
|
|
// its added positional metadata.
|
|
|
-func GetTransaction(db ethdb.Database, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
|
|
|
|
|
|
|
+func GetTransaction(db DatabaseReader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
|
|
|
// Retrieve the lookup metadata and resolve the transaction from the body
|
|
// Retrieve the lookup metadata and resolve the transaction from the body
|
|
|
blockHash, blockNumber, txIndex := GetTxLookupEntry(db, hash)
|
|
blockHash, blockNumber, txIndex := GetTxLookupEntry(db, hash)
|
|
|
|
|
|
|
@@ -284,7 +302,7 @@ func GetTransaction(db ethdb.Database, hash common.Hash) (*types.Transaction, co
|
|
|
|
|
|
|
|
// GetReceipt retrieves a specific transaction receipt from the database, along with
|
|
// GetReceipt retrieves a specific transaction receipt from the database, along with
|
|
|
// its added positional metadata.
|
|
// its added positional metadata.
|
|
|
-func GetReceipt(db ethdb.Database, hash common.Hash) (*types.Receipt, common.Hash, uint64, uint64) {
|
|
|
|
|
|
|
+func GetReceipt(db DatabaseReader, hash common.Hash) (*types.Receipt, common.Hash, uint64, uint64) {
|
|
|
// Retrieve the lookup metadata and resolve the receipt from the receipts
|
|
// Retrieve the lookup metadata and resolve the receipt from the receipts
|
|
|
blockHash, blockNumber, receiptIndex := GetTxLookupEntry(db, hash)
|
|
blockHash, blockNumber, receiptIndex := GetTxLookupEntry(db, hash)
|
|
|
|
|
|
|
@@ -309,8 +327,20 @@ func GetReceipt(db ethdb.Database, hash common.Hash) (*types.Receipt, common.Has
|
|
|
return (*types.Receipt)(&receipt), common.Hash{}, 0, 0
|
|
return (*types.Receipt)(&receipt), common.Hash{}, 0, 0
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// GetBloomBits retrieves the compressed bloom bit vector belonging to the given
|
|
|
|
|
+// section and bit index from the.
|
|
|
|
|
+func GetBloomBits(db DatabaseReader, bit uint, section uint64, head common.Hash) []byte {
|
|
|
|
|
+ key := append(append(bloomBitsPrefix, make([]byte, 10)...), head.Bytes()...)
|
|
|
|
|
+
|
|
|
|
|
+ binary.BigEndian.PutUint16(key[1:], uint16(bit))
|
|
|
|
|
+ binary.BigEndian.PutUint64(key[3:], section)
|
|
|
|
|
+
|
|
|
|
|
+ bits, _ := db.Get(key)
|
|
|
|
|
+ return bits
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// WriteCanonicalHash stores the canonical hash for the given block number.
|
|
// WriteCanonicalHash stores the canonical hash for the given block number.
|
|
|
-func WriteCanonicalHash(db ethdb.Database, hash common.Hash, number uint64) error {
|
|
|
|
|
|
|
+func WriteCanonicalHash(db DatabaseWriter, hash common.Hash, number uint64) error {
|
|
|
key := append(append(headerPrefix, encodeBlockNumber(number)...), numSuffix...)
|
|
key := append(append(headerPrefix, encodeBlockNumber(number)...), numSuffix...)
|
|
|
if err := db.Put(key, 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)
|
|
@@ -319,7 +349,7 @@ func WriteCanonicalHash(db ethdb.Database, hash common.Hash, number uint64) erro
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// WriteHeadHeaderHash stores the head header's hash.
|
|
// WriteHeadHeaderHash stores the head header's hash.
|
|
|
-func WriteHeadHeaderHash(db ethdb.Database, hash common.Hash) error {
|
|
|
|
|
|
|
+func WriteHeadHeaderHash(db DatabaseWriter, hash common.Hash) error {
|
|
|
if err := db.Put(headHeaderKey, hash.Bytes()); err != nil {
|
|
if err := db.Put(headHeaderKey, hash.Bytes()); err != nil {
|
|
|
log.Crit("Failed to store last header's hash", "err", err)
|
|
log.Crit("Failed to store last header's hash", "err", err)
|
|
|
}
|
|
}
|
|
@@ -327,7 +357,7 @@ func WriteHeadHeaderHash(db ethdb.Database, hash common.Hash) error {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// WriteHeadBlockHash stores the head block's hash.
|
|
// WriteHeadBlockHash stores the head block's hash.
|
|
|
-func WriteHeadBlockHash(db ethdb.Database, hash common.Hash) error {
|
|
|
|
|
|
|
+func WriteHeadBlockHash(db DatabaseWriter, hash common.Hash) error {
|
|
|
if err := db.Put(headBlockKey, hash.Bytes()); err != nil {
|
|
if err := db.Put(headBlockKey, hash.Bytes()); err != nil {
|
|
|
log.Crit("Failed to store last block's hash", "err", err)
|
|
log.Crit("Failed to store last block's hash", "err", err)
|
|
|
}
|
|
}
|
|
@@ -335,7 +365,7 @@ func WriteHeadBlockHash(db ethdb.Database, hash common.Hash) error {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// WriteHeadFastBlockHash stores the fast head block's hash.
|
|
// WriteHeadFastBlockHash stores the fast head block's hash.
|
|
|
-func WriteHeadFastBlockHash(db ethdb.Database, hash common.Hash) error {
|
|
|
|
|
|
|
+func WriteHeadFastBlockHash(db DatabaseWriter, hash common.Hash) error {
|
|
|
if err := db.Put(headFastKey, hash.Bytes()); err != nil {
|
|
if err := db.Put(headFastKey, hash.Bytes()); err != nil {
|
|
|
log.Crit("Failed to store last fast block's hash", "err", err)
|
|
log.Crit("Failed to store last fast block's hash", "err", err)
|
|
|
}
|
|
}
|
|
@@ -343,7 +373,7 @@ func WriteHeadFastBlockHash(db ethdb.Database, hash common.Hash) error {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// WriteHeader serializes a block header into the database.
|
|
// WriteHeader serializes a block header into the database.
|
|
|
-func WriteHeader(db ethdb.Database, header *types.Header) error {
|
|
|
|
|
|
|
+func WriteHeader(db DatabaseWriter, header *types.Header) error {
|
|
|
data, err := rlp.EncodeToBytes(header)
|
|
data, err := rlp.EncodeToBytes(header)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return err
|
|
return err
|
|
@@ -363,7 +393,7 @@ func WriteHeader(db ethdb.Database, header *types.Header) error {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// WriteBody serializes the body of a block into the database.
|
|
// WriteBody serializes the body of a block into the database.
|
|
|
-func WriteBody(db ethdb.Database, hash common.Hash, number uint64, body *types.Body) error {
|
|
|
|
|
|
|
+func WriteBody(db DatabaseWriter, hash common.Hash, number uint64, body *types.Body) error {
|
|
|
data, err := rlp.EncodeToBytes(body)
|
|
data, err := rlp.EncodeToBytes(body)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return err
|
|
return err
|
|
@@ -372,7 +402,7 @@ func WriteBody(db ethdb.Database, hash common.Hash, number uint64, body *types.B
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// WriteBodyRLP writes a serialized body of a block into the database.
|
|
// WriteBodyRLP writes a serialized body of a block into the database.
|
|
|
-func WriteBodyRLP(db ethdb.Database, hash common.Hash, number uint64, rlp rlp.RawValue) error {
|
|
|
|
|
|
|
+func WriteBodyRLP(db DatabaseWriter, hash common.Hash, number uint64, rlp rlp.RawValue) error {
|
|
|
key := append(append(bodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
|
|
key := append(append(bodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
|
|
|
if err := db.Put(key, 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)
|
|
@@ -381,7 +411,7 @@ func WriteBodyRLP(db ethdb.Database, hash common.Hash, number uint64, rlp rlp.Ra
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// WriteTd serializes the total difficulty of a block into the database.
|
|
// WriteTd serializes the total difficulty of a block into the database.
|
|
|
-func WriteTd(db ethdb.Database, hash common.Hash, number uint64, td *big.Int) error {
|
|
|
|
|
|
|
+func WriteTd(db DatabaseWriter, hash common.Hash, number uint64, td *big.Int) error {
|
|
|
data, err := rlp.EncodeToBytes(td)
|
|
data, err := rlp.EncodeToBytes(td)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return err
|
|
return err
|
|
@@ -394,7 +424,7 @@ func WriteTd(db ethdb.Database, hash common.Hash, number uint64, td *big.Int) er
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// WriteBlock serializes a block into the database, header and body separately.
|
|
// WriteBlock serializes a block into the database, header and body separately.
|
|
|
-func WriteBlock(db ethdb.Database, block *types.Block) error {
|
|
|
|
|
|
|
+func WriteBlock(db DatabaseWriter, block *types.Block) error {
|
|
|
// Store the body first to retain database consistency
|
|
// Store the body first to retain database consistency
|
|
|
if err := WriteBody(db, block.Hash(), block.NumberU64(), block.Body()); err != nil {
|
|
if err := WriteBody(db, block.Hash(), block.NumberU64(), block.Body()); err != nil {
|
|
|
return err
|
|
return err
|
|
@@ -409,7 +439,7 @@ func WriteBlock(db ethdb.Database, block *types.Block) error {
|
|
|
// WriteBlockReceipts stores all the transaction receipts belonging to a block
|
|
// WriteBlockReceipts stores all the transaction receipts belonging to a block
|
|
|
// as a single receipt slice. This is used during chain reorganisations for
|
|
// as a single receipt slice. This is used during chain reorganisations for
|
|
|
// rescheduling dropped transactions.
|
|
// rescheduling dropped transactions.
|
|
|
-func WriteBlockReceipts(db ethdb.Database, hash common.Hash, number uint64, receipts types.Receipts) error {
|
|
|
|
|
|
|
+func WriteBlockReceipts(db DatabaseWriter, hash common.Hash, number uint64, receipts types.Receipts) error {
|
|
|
// Convert the receipts into their storage form and serialize them
|
|
// Convert the receipts into their storage form and serialize them
|
|
|
storageReceipts := make([]*types.ReceiptForStorage, len(receipts))
|
|
storageReceipts := make([]*types.ReceiptForStorage, len(receipts))
|
|
|
for i, receipt := range receipts {
|
|
for i, receipt := range receipts {
|
|
@@ -454,29 +484,42 @@ func WriteTxLookupEntries(db ethdb.Database, block *types.Block) error {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// WriteBloomBits writes the compressed bloom bits vector belonging to the given
|
|
|
|
|
+// section and bit index.
|
|
|
|
|
+func WriteBloomBits(db DatabaseWriter, bit uint, section uint64, head common.Hash, bits []byte) {
|
|
|
|
|
+ key := append(append(bloomBitsPrefix, make([]byte, 10)...), head.Bytes()...)
|
|
|
|
|
+
|
|
|
|
|
+ binary.BigEndian.PutUint16(key[1:], uint16(bit))
|
|
|
|
|
+ binary.BigEndian.PutUint64(key[3:], section)
|
|
|
|
|
+
|
|
|
|
|
+ if err := db.Put(key, bits); err != nil {
|
|
|
|
|
+ log.Crit("Failed to store bloom bits", "err", err)
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// DeleteCanonicalHash removes the number to hash canonical mapping.
|
|
// DeleteCanonicalHash removes the number to hash canonical mapping.
|
|
|
-func DeleteCanonicalHash(db ethdb.Database, number uint64) {
|
|
|
|
|
|
|
+func DeleteCanonicalHash(db DatabaseDeleter, number uint64) {
|
|
|
db.Delete(append(append(headerPrefix, encodeBlockNumber(number)...), numSuffix...))
|
|
db.Delete(append(append(headerPrefix, encodeBlockNumber(number)...), numSuffix...))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// DeleteHeader removes all block header data associated with a hash.
|
|
// DeleteHeader removes all block header data associated with a hash.
|
|
|
-func DeleteHeader(db ethdb.Database, hash common.Hash, number uint64) {
|
|
|
|
|
|
|
+func DeleteHeader(db DatabaseDeleter, hash common.Hash, number uint64) {
|
|
|
db.Delete(append(blockHashPrefix, hash.Bytes()...))
|
|
db.Delete(append(blockHashPrefix, hash.Bytes()...))
|
|
|
db.Delete(append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...))
|
|
db.Delete(append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// DeleteBody removes all block body data associated with a hash.
|
|
// DeleteBody removes all block body data associated with a hash.
|
|
|
-func DeleteBody(db ethdb.Database, hash common.Hash, number uint64) {
|
|
|
|
|
|
|
+func DeleteBody(db DatabaseDeleter, hash common.Hash, number uint64) {
|
|
|
db.Delete(append(append(bodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...))
|
|
db.Delete(append(append(bodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 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 ethdb.Database, hash common.Hash, number uint64) {
|
|
|
|
|
|
|
+func DeleteTd(db DatabaseDeleter, hash common.Hash, number uint64) {
|
|
|
db.Delete(append(append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...), tdSuffix...))
|
|
db.Delete(append(append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...), tdSuffix...))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// DeleteBlock removes all block data associated with a hash.
|
|
// DeleteBlock removes all block data associated with a hash.
|
|
|
-func DeleteBlock(db ethdb.Database, hash common.Hash, number uint64) {
|
|
|
|
|
|
|
+func DeleteBlock(db DatabaseDeleter, hash common.Hash, number uint64) {
|
|
|
DeleteBlockReceipts(db, hash, number)
|
|
DeleteBlockReceipts(db, hash, number)
|
|
|
DeleteHeader(db, hash, number)
|
|
DeleteHeader(db, hash, number)
|
|
|
DeleteBody(db, hash, number)
|
|
DeleteBody(db, hash, number)
|
|
@@ -484,12 +527,12 @@ func DeleteBlock(db ethdb.Database, hash common.Hash, number uint64) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// DeleteBlockReceipts removes all receipt data associated with a block hash.
|
|
// DeleteBlockReceipts removes all receipt data associated with a block hash.
|
|
|
-func DeleteBlockReceipts(db ethdb.Database, hash common.Hash, number uint64) {
|
|
|
|
|
|
|
+func DeleteBlockReceipts(db DatabaseDeleter, hash common.Hash, number uint64) {
|
|
|
db.Delete(append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...))
|
|
db.Delete(append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// DeleteTxLookupEntry removes all transaction data associated with a hash.
|
|
// DeleteTxLookupEntry removes all transaction data associated with a hash.
|
|
|
-func DeleteTxLookupEntry(db ethdb.Database, hash common.Hash) {
|
|
|
|
|
|
|
+func DeleteTxLookupEntry(db DatabaseDeleter, hash common.Hash) {
|
|
|
db.Delete(append(lookupPrefix, hash.Bytes()...))
|
|
db.Delete(append(lookupPrefix, hash.Bytes()...))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -521,7 +564,7 @@ func WritePreimages(db ethdb.Database, number uint64, preimages map[common.Hash]
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// GetBlockChainVersion reads the version number from db.
|
|
// GetBlockChainVersion reads the version number from db.
|
|
|
-func GetBlockChainVersion(db ethdb.Database) int {
|
|
|
|
|
|
|
+func GetBlockChainVersion(db DatabaseReader) int {
|
|
|
var vsn uint
|
|
var vsn uint
|
|
|
enc, _ := db.Get([]byte("BlockchainVersion"))
|
|
enc, _ := db.Get([]byte("BlockchainVersion"))
|
|
|
rlp.DecodeBytes(enc, &vsn)
|
|
rlp.DecodeBytes(enc, &vsn)
|
|
@@ -529,13 +572,13 @@ func GetBlockChainVersion(db ethdb.Database) int {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// WriteBlockChainVersion writes vsn as the version number to db.
|
|
// WriteBlockChainVersion writes vsn as the version number to db.
|
|
|
-func WriteBlockChainVersion(db ethdb.Database, vsn int) {
|
|
|
|
|
|
|
+func WriteBlockChainVersion(db DatabaseWriter, vsn int) {
|
|
|
enc, _ := rlp.EncodeToBytes(uint(vsn))
|
|
enc, _ := rlp.EncodeToBytes(uint(vsn))
|
|
|
db.Put([]byte("BlockchainVersion"), enc)
|
|
db.Put([]byte("BlockchainVersion"), enc)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// WriteChainConfig writes the chain config settings to the database.
|
|
// WriteChainConfig writes the chain config settings to the database.
|
|
|
-func WriteChainConfig(db ethdb.Database, hash common.Hash, cfg *params.ChainConfig) error {
|
|
|
|
|
|
|
+func WriteChainConfig(db DatabaseWriter, hash common.Hash, cfg *params.ChainConfig) error {
|
|
|
// short circuit and ignore if nil config. GetChainConfig
|
|
// short circuit and ignore if nil config. GetChainConfig
|
|
|
// will return a default.
|
|
// will return a default.
|
|
|
if cfg == nil {
|
|
if cfg == nil {
|
|
@@ -551,7 +594,7 @@ func WriteChainConfig(db ethdb.Database, hash common.Hash, cfg *params.ChainConf
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// GetChainConfig will fetch the network settings based on the given hash.
|
|
// GetChainConfig will fetch the network settings based on the given hash.
|
|
|
-func GetChainConfig(db ethdb.Database, hash common.Hash) (*params.ChainConfig, error) {
|
|
|
|
|
|
|
+func GetChainConfig(db DatabaseReader, hash common.Hash) (*params.ChainConfig, error) {
|
|
|
jsonChainConfig, _ := db.Get(append(configPrefix, hash[:]...))
|
|
jsonChainConfig, _ := db.Get(append(configPrefix, hash[:]...))
|
|
|
if len(jsonChainConfig) == 0 {
|
|
if len(jsonChainConfig) == 0 {
|
|
|
return nil, ErrChainConfigNotFound
|
|
return nil, ErrChainConfigNotFound
|
|
@@ -566,7 +609,7 @@ func GetChainConfig(db ethdb.Database, hash common.Hash) (*params.ChainConfig, e
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// FindCommonAncestor returns the last common ancestor of two block headers
|
|
// FindCommonAncestor returns the last common ancestor of two block headers
|
|
|
-func FindCommonAncestor(db ethdb.Database, a, b *types.Header) *types.Header {
|
|
|
|
|
|
|
+func FindCommonAncestor(db DatabaseReader, a, b *types.Header) *types.Header {
|
|
|
for bn := b.Number.Uint64(); a.Number.Uint64() > bn; {
|
|
for bn := b.Number.Uint64(); a.Number.Uint64() > bn; {
|
|
|
a = GetHeader(db, a.ParentHash, a.Number.Uint64()-1)
|
|
a = GetHeader(db, a.ParentHash, a.Number.Uint64()-1)
|
|
|
if a == nil {
|
|
if a == nil {
|
|
@@ -591,22 +634,3 @@ func FindCommonAncestor(db ethdb.Database, a, b *types.Header) *types.Header {
|
|
|
}
|
|
}
|
|
|
return a
|
|
return a
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
-// GetBloomBits reads the compressed bloomBits vector belonging to the given section and bit index from the db
|
|
|
|
|
-func GetBloomBits(db ethdb.Database, bitIdx, sectionIdx uint64, sectionHead common.Hash) ([]byte, error) {
|
|
|
|
|
- var encKey [10]byte
|
|
|
|
|
- binary.BigEndian.PutUint16(encKey[0:2], uint16(bitIdx))
|
|
|
|
|
- binary.BigEndian.PutUint64(encKey[2:10], sectionIdx)
|
|
|
|
|
- key := append(append(bloomBitsPrefix, encKey[:]...), sectionHead.Bytes()...)
|
|
|
|
|
- bloomBits, err := db.Get(key)
|
|
|
|
|
- return bloomBits, err
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-// StoreBloomBits writes the compressed bloomBits vector belonging to the given section and bit index to the db
|
|
|
|
|
-func StoreBloomBits(db ethdb.Database, bitIdx, sectionIdx uint64, sectionHead common.Hash, bloomBits []byte) {
|
|
|
|
|
- var encKey [10]byte
|
|
|
|
|
- binary.BigEndian.PutUint16(encKey[0:2], uint16(bitIdx))
|
|
|
|
|
- binary.BigEndian.PutUint64(encKey[2:10], sectionIdx)
|
|
|
|
|
- key := append(append(bloomBitsPrefix, encKey[:]...), sectionHead.Bytes()...)
|
|
|
|
|
- db.Put(key, bloomBits)
|
|
|
|
|
-}
|
|
|