mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-24 21:56:43 +00:00
core/rawdb: allow skipping isCanon check
This commit is contained in:
parent
9a8da8a76e
commit
7a5c0f10a0
4 changed files with 40 additions and 7 deletions
|
|
@ -450,8 +450,8 @@ func ReadBodyRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawValue
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadCanonicalBodyRLP retrieves the block body (transactions and uncles) for the canonical
|
// ReadCanonicalBodyRLP retrieves the block body (transactions and uncles) for the canonical
|
||||||
// block at number, in RLP encoding.
|
// block at number, in RLP encoding. Optionally it takes the block hash to avoid looking it up
|
||||||
func ReadCanonicalBodyRLP(db ethdb.Reader, number uint64) rlp.RawValue {
|
func ReadCanonicalBodyRLP(db ethdb.Reader, number uint64, hash *common.Hash) rlp.RawValue {
|
||||||
var data []byte
|
var data []byte
|
||||||
db.ReadAncients(func(reader ethdb.AncientReaderOp) error {
|
db.ReadAncients(func(reader ethdb.AncientReaderOp) error {
|
||||||
data, _ = reader.Ancient(ChainFreezerBodiesTable, number)
|
data, _ = reader.Ancient(ChainFreezerBodiesTable, number)
|
||||||
|
|
@ -461,8 +461,12 @@ func ReadCanonicalBodyRLP(db ethdb.Reader, number uint64) rlp.RawValue {
|
||||||
// Block is not in ancients, read from leveldb by hash and number.
|
// Block is not in ancients, read from leveldb by hash and number.
|
||||||
// Note: ReadCanonicalHash cannot be used here because it also
|
// Note: ReadCanonicalHash cannot be used here because it also
|
||||||
// calls ReadAncients internally.
|
// calls ReadAncients internally.
|
||||||
hash, _ := db.Get(headerHashKey(number))
|
if hash != nil {
|
||||||
data, _ = db.Get(blockBodyKey(number, common.BytesToHash(hash)))
|
data, _ = db.Get(blockBodyKey(number, *hash))
|
||||||
|
} else {
|
||||||
|
hashBytes, _ := db.Get(headerHashKey(number))
|
||||||
|
data, _ = db.Get(blockBodyKey(number, common.BytesToHash(hashBytes)))
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return data
|
return data
|
||||||
|
|
@ -544,6 +548,29 @@ func ReadReceiptsRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawVa
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadCanonicalReceiptsRLP retrieves the receipts RLP for the canonical
|
||||||
|
// block at number, in RLP encoding. Optionally it takes the block hash to avoid looking it up
|
||||||
|
func ReadCanonicalReceiptsRLP(db ethdb.Reader, number uint64, hash *common.Hash) rlp.RawValue {
|
||||||
|
var data []byte
|
||||||
|
db.ReadAncients(func(reader ethdb.AncientReaderOp) error {
|
||||||
|
data, _ = reader.Ancient(ChainFreezerReceiptTable, number)
|
||||||
|
if len(data) > 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// Block is not in ancients, read from leveldb by hash and number.
|
||||||
|
// Note: ReadCanonicalHash cannot be used here because it also
|
||||||
|
// calls ReadAncients internally.
|
||||||
|
if hash != nil {
|
||||||
|
data, _ = db.Get(blockReceiptsKey(number, *hash))
|
||||||
|
} else {
|
||||||
|
hashBytes, _ := db.Get(headerHashKey(number))
|
||||||
|
data, _ = db.Get(blockReceiptsKey(number, common.BytesToHash(hashBytes)))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
// ReadRawReceipts retrieves all the transaction receipts belonging to a block.
|
// ReadRawReceipts retrieves all the transaction receipts belonging to a block.
|
||||||
// The receipt metadata fields and the Bloom are not guaranteed to be populated,
|
// The receipt metadata fields and the Bloom are not guaranteed to be populated,
|
||||||
// so they should not be used. Use ReadReceipts instead if the metadata is needed.
|
// so they should not be used. Use ReadReceipts instead if the metadata is needed.
|
||||||
|
|
|
||||||
|
|
@ -441,6 +441,9 @@ func TestAncientStorage(t *testing.T) {
|
||||||
if blob := ReadReceiptsRLP(db, hash, number); len(blob) > 0 {
|
if blob := ReadReceiptsRLP(db, hash, number); len(blob) > 0 {
|
||||||
t.Fatalf("non existent receipts returned")
|
t.Fatalf("non existent receipts returned")
|
||||||
}
|
}
|
||||||
|
if blob := ReadCanonicalReceiptsRLP(db, number, &hash); len(blob) > 0 {
|
||||||
|
t.Fatalf("non existent receipts returned")
|
||||||
|
}
|
||||||
|
|
||||||
// Write and verify the header in the database
|
// Write and verify the header in the database
|
||||||
WriteAncientBlocks(db, []*types.Block{block}, types.EncodeBlockReceiptLists([]types.Receipts{nil}))
|
WriteAncientBlocks(db, []*types.Block{block}, types.EncodeBlockReceiptLists([]types.Receipts{nil}))
|
||||||
|
|
@ -454,6 +457,9 @@ func TestAncientStorage(t *testing.T) {
|
||||||
if blob := ReadReceiptsRLP(db, hash, number); len(blob) == 0 {
|
if blob := ReadReceiptsRLP(db, hash, number); len(blob) == 0 {
|
||||||
t.Fatalf("no receipts returned")
|
t.Fatalf("no receipts returned")
|
||||||
}
|
}
|
||||||
|
if blob := ReadCanonicalReceiptsRLP(db, number, &hash); len(blob) == 0 {
|
||||||
|
t.Fatalf("no receipts returned")
|
||||||
|
}
|
||||||
|
|
||||||
// Use a fake hash for data retrieval, nothing should be returned.
|
// Use a fake hash for data retrieval, nothing should be returned.
|
||||||
fakeHash := common.BytesToHash([]byte{0x01, 0x02, 0x03})
|
fakeHash := common.BytesToHash([]byte{0x01, 0x02, 0x03})
|
||||||
|
|
|
||||||
|
|
@ -181,7 +181,7 @@ func ReadTransaction(db ethdb.Reader, hash common.Hash) (*types.Transaction, com
|
||||||
if blockHash == (common.Hash{}) {
|
if blockHash == (common.Hash{}) {
|
||||||
return nil, common.Hash{}, 0, 0
|
return nil, common.Hash{}, 0, 0
|
||||||
}
|
}
|
||||||
bodyRLP := ReadBodyRLP(db, blockHash, *blockNumber)
|
bodyRLP := ReadCanonicalBodyRLP(db, *blockNumber, &blockHash)
|
||||||
if bodyRLP == nil {
|
if bodyRLP == nil {
|
||||||
log.Error("Transaction referenced missing", "number", *blockNumber, "hash", blockHash)
|
log.Error("Transaction referenced missing", "number", *blockNumber, "hash", blockHash)
|
||||||
return nil, common.Hash{}, 0, 0
|
return nil, common.Hash{}, 0, 0
|
||||||
|
|
@ -265,7 +265,7 @@ type RawReceiptContext struct {
|
||||||
// the gas used by the associated transaction and the starting index of the logs
|
// the gas used by the associated transaction and the starting index of the logs
|
||||||
// within the block.
|
// within the block.
|
||||||
func ReadRawReceipt(db ethdb.Reader, blockHash common.Hash, blockNumber, txIndex uint64) (*types.Receipt, RawReceiptContext, error) {
|
func ReadRawReceipt(db ethdb.Reader, blockHash common.Hash, blockNumber, txIndex uint64) (*types.Receipt, RawReceiptContext, error) {
|
||||||
receiptIt, err := rlp.NewListIterator(ReadReceiptsRLP(db, blockHash, blockNumber))
|
receiptIt, err := rlp.NewListIterator(ReadCanonicalReceiptsRLP(db, blockNumber, &blockHash))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, RawReceiptContext{}, err
|
return nil, RawReceiptContext{}, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -118,7 +118,7 @@ func iterateTransactions(db ethdb.Database, from uint64, to uint64, reverse bool
|
||||||
}
|
}
|
||||||
defer close(rlpCh)
|
defer close(rlpCh)
|
||||||
for n != end {
|
for n != end {
|
||||||
data := ReadCanonicalBodyRLP(db, n)
|
data := ReadCanonicalBodyRLP(db, n, nil)
|
||||||
// Feed the block to the aggregator, or abort on interrupt
|
// Feed the block to the aggregator, or abort on interrupt
|
||||||
select {
|
select {
|
||||||
case rlpCh <- &numberRlp{n, data}:
|
case rlpCh <- &numberRlp{n, data}:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue