Skip to content

Commit 7221cb1

Browse files
committed
core, eth, les, light: scope receipt functionality a bit cleaner
1 parent 6b0ddd1 commit 7221cb1

16 files changed

Lines changed: 250 additions & 287 deletions

‎accounts/abi/bind/backends/simulated.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func (b *SimulatedBackend) StorageAt(ctx context.Context, contract common.Addres
160160

161161
// TransactionReceipt returns the receipt of a transaction.
162162
func (b *SimulatedBackend) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
163-
receipt, _, _, _ := rawdb.ReadReceipt(b.database, txHash)
163+
receipt, _, _, _ := rawdb.ReadReceipt(b.database, txHash, b.config)
164164
return receipt, nil
165165
}
166166

@@ -464,15 +464,15 @@ func (fb *filterBackend) GetReceipts(ctx context.Context, hash common.Hash) (typ
464464
if number == nil {
465465
return nil, nil
466466
}
467-
return rawdb.ReadReceipts(fb.db, hash, *number), nil
467+
return rawdb.ReadReceipts(fb.db, hash, *number, fb.bc.Config()), nil
468468
}
469469

470470
func (fb *filterBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) {
471471
number := rawdb.ReadHeaderNumber(fb.db, hash)
472472
if number == nil {
473473
return nil, nil
474474
}
475-
receipts := rawdb.ReadReceipts(fb.db, hash, *number)
475+
receipts := rawdb.ReadReceipts(fb.db, hash, *number, fb.bc.Config())
476476
if receipts == nil {
477477
return nil, nil
478478
}

‎core/bench_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ func benchReadChain(b *testing.B, full bool, count uint64) {
297297
if full {
298298
hash := header.Hash()
299299
rawdb.ReadBody(db, hash, n)
300-
rawdb.ReadReceipts(db, hash, n)
300+
rawdb.ReadReceipts(db, hash, n, chain.Config())
301301
}
302302
}
303303
chain.Stop()

‎core/blockchain.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ func (bc *BlockChain) GetReceiptsByHash(hash common.Hash) types.Receipts {
653653
if number == nil {
654654
return nil
655655
}
656-
receipts := rawdb.ReadReceipts(bc.db, hash, *number)
656+
receipts := rawdb.ReadReceipts(bc.db, hash, *number, bc.chainConfig)
657657
if receipts == nil {
658658
return nil
659659
}
@@ -828,8 +828,8 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
828828
continue
829829
}
830830
// Compute all the non-consensus fields of the receipts
831-
if err := rawdb.SetReceiptsData(bc.chainConfig, block.Hash(), block.Number(), block.Body(), receipts); err != nil {
832-
return i, fmt.Errorf("failed to set receipts data: %v", err)
831+
if err := receipts.DeriveFields(bc.chainConfig, block.Hash(), block.NumberU64(), block.Transactions()); err != nil {
832+
return i, fmt.Errorf("failed to derive receipts data: %v", err)
833833
}
834834
// Write all the data out into the database
835835
rawdb.WriteBody(batch, block.Hash(), block.NumberU64(), block.Body())
@@ -1451,7 +1451,7 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
14511451
if number == nil {
14521452
return
14531453
}
1454-
receipts := rawdb.ReadReceipts(bc.db, hash, *number)
1454+
receipts := rawdb.ReadReceipts(bc.db, hash, *number, bc.chainConfig)
14551455
for _, receipt := range receipts {
14561456
for _, log := range receipt.Logs {
14571457
l := *log

‎core/blockchain_test.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ func TestFastVsFullChains(t *testing.T) {
658658
} else if types.CalcUncleHash(fblock.Uncles()) != types.CalcUncleHash(ablock.Uncles()) {
659659
t.Errorf("block #%d [%x]: uncles mismatch: have %v, want %v", num, hash, fblock.Uncles(), ablock.Uncles())
660660
}
661-
if freceipts, areceipts := rawdb.ReadReceipts(fastDb, hash, *rawdb.ReadHeaderNumber(fastDb, hash)), rawdb.ReadReceipts(archiveDb, hash, *rawdb.ReadHeaderNumber(archiveDb, hash)); types.DeriveSha(freceipts) != types.DeriveSha(areceipts) {
661+
if freceipts, areceipts := rawdb.ReadReceipts(fastDb, hash, *rawdb.ReadHeaderNumber(fastDb, hash), fast.Config()), rawdb.ReadReceipts(archiveDb, hash, *rawdb.ReadHeaderNumber(archiveDb, hash), archive.Config()); types.DeriveSha(freceipts) != types.DeriveSha(areceipts) {
662662
t.Errorf("block #%d [%x]: receipts mismatch: have %v, want %v", num, hash, freceipts, areceipts)
663663
}
664664
}
@@ -843,7 +843,7 @@ func TestChainTxReorgs(t *testing.T) {
843843
if txn, _, _, _ := rawdb.ReadTransaction(db, tx.Hash()); txn != nil {
844844
t.Errorf("drop %d: tx %v found while shouldn't have been", i, txn)
845845
}
846-
if rcpt, _, _, _ := rawdb.ReadReceipt(db, tx.Hash()); rcpt != nil {
846+
if rcpt, _, _, _ := rawdb.ReadReceipt(db, tx.Hash(), blockchain.Config()); rcpt != nil {
847847
t.Errorf("drop %d: receipt %v found while shouldn't have been", i, rcpt)
848848
}
849849
}
@@ -852,7 +852,7 @@ func TestChainTxReorgs(t *testing.T) {
852852
if txn, _, _, _ := rawdb.ReadTransaction(db, tx.Hash()); txn == nil {
853853
t.Errorf("add %d: expected tx to be found", i)
854854
}
855-
if rcpt, _, _, _ := rawdb.ReadReceipt(db, tx.Hash()); rcpt == nil {
855+
if rcpt, _, _, _ := rawdb.ReadReceipt(db, tx.Hash(), blockchain.Config()); rcpt == nil {
856856
t.Errorf("add %d: expected receipt to be found", i)
857857
}
858858
}
@@ -861,7 +861,7 @@ func TestChainTxReorgs(t *testing.T) {
861861
if txn, _, _, _ := rawdb.ReadTransaction(db, tx.Hash()); txn == nil {
862862
t.Errorf("share %d: expected tx to be found", i)
863863
}
864-
if rcpt, _, _, _ := rawdb.ReadReceipt(db, tx.Hash()); rcpt == nil {
864+
if rcpt, _, _, _ := rawdb.ReadReceipt(db, tx.Hash(), blockchain.Config()); rcpt == nil {
865865
t.Errorf("share %d: expected receipt to be found", i)
866866
}
867867
}

‎core/rawdb/accessors_chain.go‎

Lines changed: 8 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ package rawdb
1919
import (
2020
"bytes"
2121
"encoding/binary"
22-
"errors"
2322
"math/big"
2423

2524
"github.com/ethereum/go-ethereum/common"
2625
"github.com/ethereum/go-ethereum/core/types"
27-
"github.com/ethereum/go-ethereum/crypto"
2826
"github.com/ethereum/go-ethereum/ethdb"
2927
"github.com/ethereum/go-ethereum/log"
3028
"github.com/ethereum/go-ethereum/params"
@@ -311,20 +309,16 @@ func ReadRawReceipts(db ethdb.Reader, hash common.Hash, number uint64) types.Rec
311309
if len(data) == 0 {
312310
return nil
313311
}
314-
315312
// Convert the receipts from their storage form to their internal representation
316313
storageReceipts := []*types.ReceiptForStorage{}
317314
if err := rlp.DecodeBytes(data, &storageReceipts); err != nil {
318315
log.Error("Invalid receipt array RLP", "hash", hash, "err", err)
319316
return nil
320317
}
321-
322-
var receipts types.Receipts
323-
for _, storageReceipt := range storageReceipts {
324-
receipt := (*types.Receipt)(storageReceipt)
325-
receipts = append(receipts, receipt)
318+
receipts := make(types.Receipts, len(storageReceipts))
319+
for i, storageReceipt := range storageReceipts {
320+
receipts[i] = (*types.Receipt)(storageReceipt)
326321
}
327-
328322
return receipts
329323
}
330324

@@ -335,79 +329,24 @@ func ReadRawReceipts(db ethdb.Reader, hash common.Hash, number uint64) types.Rec
335329
// The current implementation populates these metadata fields by reading the receipts'
336330
// corresponding block body, so if the block body is not found it will return nil even
337331
// if the receipt itself is stored.
338-
func ReadReceipts(db ethdb.Reader, hash common.Hash, number uint64) types.Receipts {
332+
func ReadReceipts(db ethdb.Reader, hash common.Hash, number uint64, config *params.ChainConfig) types.Receipts {
333+
// We're deriving many fields from the block body, retrieve beside the receipt
339334
receipts := ReadRawReceipts(db, hash, number)
340335
if receipts == nil {
341-
return receipts
336+
return nil
342337
}
343-
344-
// Retrieve the block body to populate missing fields for receipts and logs
345338
body := ReadBody(db, hash, number)
346339
if body == nil {
347340
log.Error("Missing body but have receipt", "hash", hash, "number", number)
348341
return nil
349342
}
350-
351-
genesisHash := ReadCanonicalHash(db, 0)
352-
if genesisHash == (common.Hash{}) {
353-
log.Error("Missing genesis hash")
343+
if err := receipts.DeriveFields(config, hash, number, body.Transactions); err != nil {
344+
log.Error("Failed to derive block receipts fields", "hash", hash, "number", number, "err", err)
354345
return nil
355346
}
356-
357-
config := ReadChainConfig(db, genesisHash)
358-
if config == nil {
359-
log.Error("Missing chain config ", "hash", hash)
360-
return nil
361-
}
362-
363-
SetReceiptsData(config, hash, big.NewInt(int64(number)), body, receipts)
364-
365347
return receipts
366348
}
367349

368-
// SetReceiptsData computes all the non-consensus fields of the receipts
369-
func SetReceiptsData(config *params.ChainConfig, blockHash common.Hash, blockNumber *big.Int, body *types.Body, receipts types.Receipts) error {
370-
signer := types.MakeSigner(config, blockNumber)
371-
372-
transactions, logIndex := body.Transactions, uint(0)
373-
if len(transactions) != len(receipts) {
374-
return errors.New("transaction and receipt count mismatch")
375-
}
376-
377-
for j := 0; j < len(receipts); j++ {
378-
// The transaction hash can be retrieved from the transaction itself
379-
receipts[j].TxHash = transactions[j].Hash()
380-
381-
// block location fields
382-
receipts[j].BlockHash = blockHash
383-
receipts[j].BlockNumber = blockNumber
384-
receipts[j].TransactionIndex = uint(j)
385-
386-
// The contract address can be derived from the transaction itself
387-
if transactions[j].To() == nil {
388-
// Deriving the signer is expensive, only do if it's actually needed
389-
from, _ := types.Sender(signer, transactions[j])
390-
receipts[j].ContractAddress = crypto.CreateAddress(from, transactions[j].Nonce())
391-
}
392-
// The used gas can be calculated based on previous receipts
393-
if j == 0 {
394-
receipts[j].GasUsed = receipts[j].CumulativeGasUsed
395-
} else {
396-
receipts[j].GasUsed = receipts[j].CumulativeGasUsed - receipts[j-1].CumulativeGasUsed
397-
}
398-
// The derived log fields can simply be set from the block and transaction
399-
for k := 0; k < len(receipts[j].Logs); k++ {
400-
receipts[j].Logs[k].BlockNumber = blockNumber.Uint64()
401-
receipts[j].Logs[k].BlockHash = blockHash
402-
receipts[j].Logs[k].TxHash = receipts[j].TxHash
403-
receipts[j].Logs[k].TxIndex = uint(j)
404-
receipts[j].Logs[k].Index = logIndex
405-
logIndex++
406-
}
407-
}
408-
return nil
409-
}
410-
411350
// WriteReceipts stores all the transaction receipts belonging to a block.
412351
func WriteReceipts(db ethdb.Writer, hash common.Hash, number uint64, receipts types.Receipts) {
413352
// Convert the receipts into their storage form and serialize them

0 commit comments

Comments
 (0)