From 6b0ddd141e2e12bb09b33e14e67aed4d5a68d66c Mon Sep 17 00:00:00 2001 From: Matthew Halpern Date: Wed, 27 Mar 2019 09:11:24 -0700 Subject: [PATCH 1/3] core, eth, les, light: store transaction receipts without txHash and gasCost --- core/blockchain.go | 67 +++------ core/chain_makers.go | 9 ++ core/rawdb/accessors_chain.go | 112 ++++++++++++--- core/rawdb/accessors_chain_test.go | 210 +++++++++++++++++++++++++++-- core/types/log.go | 6 +- core/types/receipt.go | 109 +++++++++++---- core/types/receipt_test.go | 133 ++++++++++++++++++ eth/filters/filter_test.go | 5 + les/handler_test.go | 2 +- light/odr_test.go | 2 +- light/odr_util.go | 20 ++- 11 files changed, 552 insertions(+), 123 deletions(-) create mode 100644 core/types/receipt_test.go diff --git a/core/blockchain.go b/core/blockchain.go index 4a347ec81..cb28335be 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -35,7 +35,6 @@ import ( "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/log" @@ -43,7 +42,7 @@ import ( "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" - lru "github.com/hashicorp/golang-lru" + "github.com/hashicorp/golang-lru" ) var ( @@ -79,12 +78,19 @@ const ( // BlockChainVersion ensures that an incompatible database forces a resync from scratch. // - // During the process of upgrading the database version from 3 to 4, - // the following incompatible database changes were added. - // * the `BlockNumber`, `TxHash`, `TxIndex`, `BlockHash` and `Index` fields of log are deleted - // * the `Bloom` field of receipt is deleted - // * the `BlockIndex` and `TxIndex` fields of txlookup are deleted - BlockChainVersion uint64 = 4 + // Changelog: + // + // - Version 4 + // The following incompatible database changes were added: + // * the `BlockNumber`, `TxHash`, `TxIndex`, `BlockHash` and `Index` fields of log are deleted + // * the `Bloom` field of receipt is deleted + // * the `BlockIndex` and `TxIndex` fields of txlookup are deleted + // - Version 5 + // The following incompatible database changes were added: + // * the `TxHash`, `GasCost`, and `ContractAddress` fields are no longer stored for a receipt + // * the `TxHash`, `GasCost`, and `ContractAddress` fields are computed by looking up the + // receipts' corresponding block + BlockChainVersion uint64 = 5 ) // CacheConfig contains the configuration values for the trie caching/pruning @@ -784,49 +790,6 @@ func (bc *BlockChain) Rollback(chain []common.Hash) { } } -// SetReceiptsData computes all the non-consensus fields of the receipts -func SetReceiptsData(config *params.ChainConfig, block *types.Block, receipts types.Receipts) error { - signer := types.MakeSigner(config, block.Number()) - - transactions, logIndex := block.Transactions(), uint(0) - if len(transactions) != len(receipts) { - return errors.New("transaction and receipt count mismatch") - } - - for j := 0; j < len(receipts); j++ { - // The transaction hash can be retrieved from the transaction itself - receipts[j].TxHash = transactions[j].Hash() - - // block location fields - receipts[j].BlockHash = block.Hash() - receipts[j].BlockNumber = block.Number() - receipts[j].TransactionIndex = uint(j) - - // The contract address can be derived from the transaction itself - if transactions[j].To() == nil { - // Deriving the signer is expensive, only do if it's actually needed - from, _ := types.Sender(signer, transactions[j]) - receipts[j].ContractAddress = crypto.CreateAddress(from, transactions[j].Nonce()) - } - // The used gas can be calculated based on previous receipts - if j == 0 { - receipts[j].GasUsed = receipts[j].CumulativeGasUsed - } else { - receipts[j].GasUsed = receipts[j].CumulativeGasUsed - receipts[j-1].CumulativeGasUsed - } - // The derived log fields can simply be set from the block and transaction - for k := 0; k < len(receipts[j].Logs); k++ { - receipts[j].Logs[k].BlockNumber = block.NumberU64() - receipts[j].Logs[k].BlockHash = block.Hash() - receipts[j].Logs[k].TxHash = receipts[j].TxHash - receipts[j].Logs[k].TxIndex = uint(j) - receipts[j].Logs[k].Index = logIndex - logIndex++ - } - } - return nil -} - // InsertReceiptChain attempts to complete an already existing header chain with // transaction and receipt data. func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain []types.Receipts) (int, error) { @@ -865,7 +828,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [ continue } // Compute all the non-consensus fields of the receipts - if err := SetReceiptsData(bc.chainConfig, block, receipts); err != nil { + if err := rawdb.SetReceiptsData(bc.chainConfig, block.Hash(), block.Number(), block.Body(), receipts); err != nil { return i, fmt.Errorf("failed to set receipts data: %v", err) } // Write all the data out into the database diff --git a/core/chain_makers.go b/core/chain_makers.go index d563d85ee..b80788d19 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -104,6 +104,15 @@ func (b *BlockGen) AddTxWithChain(bc *BlockChain, tx *types.Transaction) { b.receipts = append(b.receipts, receipt) } +// AddUncheckedTx forcefully adds a transaction to the block without any +// validation. +// +// AddUncheckedTx will cause consensus failures when used during real +// chain processing. This is best used in conjunction with raw block insertion. +func (b *BlockGen) AddUncheckedTx(tx *types.Transaction) { + b.txs = append(b.txs, tx) +} + // Number returns the block number of the block being generated. func (b *BlockGen) Number() *big.Int { return new(big.Int).Set(b.header.Number) diff --git a/core/rawdb/accessors_chain.go b/core/rawdb/accessors_chain.go index 647839997..8a5e95bc7 100644 --- a/core/rawdb/accessors_chain.go +++ b/core/rawdb/accessors_chain.go @@ -19,12 +19,15 @@ package rawdb import ( "bytes" "encoding/binary" + "errors" "math/big" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" ) @@ -299,39 +302,112 @@ func ReadReceiptsRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawVa return data } -// ReadReceipts retrieves all the transaction receipts belonging to a block. -func ReadReceipts(db ethdb.Reader, hash common.Hash, number uint64) types.Receipts { +// ReadRawReceipts retrieves all the transaction receipts belonging to a block. +// The receipt metadata fields are not guaranteed to be populated, so they +// should not be used. Use ReadReceipts instead if the metadata is needed. +func ReadRawReceipts(db ethdb.Reader, hash common.Hash, number uint64) types.Receipts { // Retrieve the flattened receipt slice data := ReadReceiptsRLP(db, hash, number) if len(data) == 0 { return nil } + // Convert the receipts from their storage form to their internal representation storageReceipts := []*types.ReceiptForStorage{} if err := rlp.DecodeBytes(data, &storageReceipts); err != nil { log.Error("Invalid receipt array RLP", "hash", hash, "err", err) return nil } - receipts := make(types.Receipts, len(storageReceipts)) - logIndex := uint(0) - for i, receipt := range storageReceipts { - // Assemble deriving fields for log. - for _, log := range receipt.Logs { - log.TxHash = receipt.TxHash - log.BlockHash = hash - log.BlockNumber = number - log.TxIndex = uint(i) - log.Index = logIndex - logIndex += 1 - } - receipts[i] = (*types.Receipt)(receipt) - receipts[i].BlockHash = hash - receipts[i].BlockNumber = big.NewInt(0).SetUint64(number) - receipts[i].TransactionIndex = uint(i) + + var receipts types.Receipts + for _, storageReceipt := range storageReceipts { + receipt := (*types.Receipt)(storageReceipt) + receipts = append(receipts, receipt) } + return receipts } +// ReadReceipts retrieves all the transaction receipts belonging to a block, including +// its correspoinding metadata fields. If it is unable to populate these metadata +// fields then nil is returned. +// +// The current implementation populates these metadata fields by reading the receipts' +// corresponding block body, so if the block body is not found it will return nil even +// if the receipt itself is stored. +func ReadReceipts(db ethdb.Reader, hash common.Hash, number uint64) types.Receipts { + receipts := ReadRawReceipts(db, hash, number) + if receipts == nil { + return receipts + } + + // Retrieve the block body to populate missing fields for receipts and logs + body := ReadBody(db, hash, number) + if body == nil { + log.Error("Missing body but have receipt", "hash", hash, "number", number) + return nil + } + + genesisHash := ReadCanonicalHash(db, 0) + if genesisHash == (common.Hash{}) { + log.Error("Missing genesis hash") + return nil + } + + config := ReadChainConfig(db, genesisHash) + if config == nil { + log.Error("Missing chain config ", "hash", hash) + return nil + } + + SetReceiptsData(config, hash, big.NewInt(int64(number)), body, receipts) + + return receipts +} + +// SetReceiptsData computes all the non-consensus fields of the receipts +func SetReceiptsData(config *params.ChainConfig, blockHash common.Hash, blockNumber *big.Int, body *types.Body, receipts types.Receipts) error { + signer := types.MakeSigner(config, blockNumber) + + transactions, logIndex := body.Transactions, uint(0) + if len(transactions) != len(receipts) { + return errors.New("transaction and receipt count mismatch") + } + + for j := 0; j < len(receipts); j++ { + // The transaction hash can be retrieved from the transaction itself + receipts[j].TxHash = transactions[j].Hash() + + // block location fields + receipts[j].BlockHash = blockHash + receipts[j].BlockNumber = blockNumber + receipts[j].TransactionIndex = uint(j) + + // The contract address can be derived from the transaction itself + if transactions[j].To() == nil { + // Deriving the signer is expensive, only do if it's actually needed + from, _ := types.Sender(signer, transactions[j]) + receipts[j].ContractAddress = crypto.CreateAddress(from, transactions[j].Nonce()) + } + // The used gas can be calculated based on previous receipts + if j == 0 { + receipts[j].GasUsed = receipts[j].CumulativeGasUsed + } else { + receipts[j].GasUsed = receipts[j].CumulativeGasUsed - receipts[j-1].CumulativeGasUsed + } + // The derived log fields can simply be set from the block and transaction + for k := 0; k < len(receipts[j].Logs); k++ { + receipts[j].Logs[k].BlockNumber = blockNumber.Uint64() + receipts[j].Logs[k].BlockHash = blockHash + receipts[j].Logs[k].TxHash = receipts[j].TxHash + receipts[j].Logs[k].TxIndex = uint(j) + receipts[j].Logs[k].Index = logIndex + logIndex++ + } + } + return nil +} + // WriteReceipts stores all the transaction receipts belonging to a block. func WriteReceipts(db ethdb.Writer, hash common.Hash, number uint64, receipts types.Receipts) { // Convert the receipts into their storage form and serialize them diff --git a/core/rawdb/accessors_chain_test.go b/core/rawdb/accessors_chain_test.go index 9f6e9cdb3..9ee896ba7 100644 --- a/core/rawdb/accessors_chain_test.go +++ b/core/rawdb/accessors_chain_test.go @@ -18,6 +18,11 @@ package rawdb import ( "bytes" + "encoding/hex" + "fmt" + "github.com/ethereum/go-ethereum/common/math" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/params" "math/big" "testing" @@ -267,6 +272,11 @@ func TestHeadStorage(t *testing.T) { func TestBlockReceiptStorage(t *testing.T) { db := NewMemoryDatabase() + tx1 := types.NewTransaction(1, common.HexToAddress("0x1"), big.NewInt(1), 1, big.NewInt(1), nil) + tx2 := types.NewTransaction(2, common.HexToAddress("0x2"), big.NewInt(2), 2, big.NewInt(2), nil) + // Include block needed to read metadata. + body := &types.Body{Transactions: types.Transactions{tx1, tx2}} + receipt1 := &types.Receipt{ Status: types.ReceiptStatusFailed, CumulativeGasUsed: 1, @@ -274,7 +284,7 @@ func TestBlockReceiptStorage(t *testing.T) { {Address: common.BytesToAddress([]byte{0x11})}, {Address: common.BytesToAddress([]byte{0x01, 0x11})}, }, - TxHash: common.BytesToHash([]byte{0x11, 0x11}), + TxHash: tx1.Hash(), ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}), GasUsed: 111111, } @@ -286,7 +296,7 @@ func TestBlockReceiptStorage(t *testing.T) { {Address: common.BytesToAddress([]byte{0x22})}, {Address: common.BytesToAddress([]byte{0x02, 0x22})}, }, - TxHash: common.BytesToHash([]byte{0x22, 0x22}), + TxHash: tx2.Hash(), ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}), GasUsed: 222222, } @@ -298,23 +308,205 @@ func TestBlockReceiptStorage(t *testing.T) { if rs := ReadReceipts(db, hash, 0); len(rs) != 0 { t.Fatalf("non existent receipts returned: %v", rs) } + // Insert the body that corresponds to the receipts. + WriteBody(db, hash, 0, body) // Insert the receipt slice into the database and check presence WriteReceipts(db, hash, 0, receipts) + // Insert canonical hash that the chain configuration will be mapped to. + WriteCanonicalHash(db, hash, 0) + // Insert the chain configuration. + WriteChainConfig(db, hash, params.MainnetChainConfig) if rs := ReadReceipts(db, hash, 0); len(rs) == 0 { t.Fatalf("no receipts returned") } else { - for i := 0; i < len(receipts); i++ { - rlpHave, _ := rlp.EncodeToBytes(rs[i]) - rlpWant, _ := rlp.EncodeToBytes(receipts[i]) - - if !bytes.Equal(rlpHave, rlpWant) { - t.Fatalf("receipt #%d: receipt mismatch: have %v, want %v", i, rs[i], receipts[i]) - } + if err := checkReceiptsRLP(rs, receipts); err != nil { + t.Fatalf(err.Error()) } } + + DeleteBody(db, hash, 0) + // Check that receipts are no longer returned when metadata cannot be recomputed. + if rs := ReadReceipts(db, hash, 0); rs != nil { + t.Fatalf("receipts returned when body was deleted: %v", rs) + } + // Check that receipts without metadata can be returned when specifically + rs := ReadRawReceipts(db, hash, 0) + if err := checkReceiptsRLP(rs, receipts); err != nil { + t.Fatalf(err.Error()) + } + // Re-insert the body that corresponds to the receipts. + WriteBody(db, hash, 0, body) + // Delete the receipt slice and check purge DeleteReceipts(db, hash, 0) if rs := ReadReceipts(db, hash, 0); len(rs) != 0 { t.Fatalf("deleted receipts returned: %v", rs) } } + +func checkReceiptsRLP(have, want types.Receipts) error { + if len(have) != len(want) { + return fmt.Errorf("receipts sizes mismatch: have %d, want %d", len(have), len(want)) + } + + for i := 0; i < len(want); i++ { + rlpHave, err := rlp.EncodeToBytes(have[i]) + if err != nil { + return err + } + rlpWant, err := rlp.EncodeToBytes(want[i]) + if err != nil { + return err + } + + if !bytes.Equal(rlpHave, rlpWant) { + return fmt.Errorf("receipt #%d: receipt mismatch: have %s, want %s", i, hex.EncodeToString(rlpHave), hex.EncodeToString(rlpWant)) + } + } + + return nil +} + +// Tests that receipts associated with a single block can be stored and retrieved. +func TestSetReceiptsData(t *testing.T) { + tx0 := types.NewContractCreation(1, big.NewInt(1), 1, big.NewInt(1), nil) + tx1 := types.NewTransaction(2, common.HexToAddress("0x2"), big.NewInt(2), 2, big.NewInt(2), nil) + txs := types.Transactions{tx0, tx1} + // Include block needed to read metadata. + body := &types.Body{Transactions: txs} + + receipt1 := &types.Receipt{ + Status: types.ReceiptStatusFailed, + CumulativeGasUsed: 1, + Logs: []*types.Log{ + {Address: common.BytesToAddress([]byte{0x11})}, + {Address: common.BytesToAddress([]byte{0x01, 0x11})}, + }, + TxHash: tx0.Hash(), + ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}), + GasUsed: 1, + } + receipt2 := &types.Receipt{ + PostState: common.Hash{2}.Bytes(), + CumulativeGasUsed: 3, + Logs: []*types.Log{ + {Address: common.BytesToAddress([]byte{0x22})}, + {Address: common.BytesToAddress([]byte{0x02, 0x22})}, + }, + TxHash: tx1.Hash(), + ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}), + GasUsed: 2, + } + receipt2.Bloom = types.CreateBloom(types.Receipts{receipt2}) + receipts := []*types.Receipt{receipt1, receipt2} + + blockNumber := big.NewInt(1) + blockHash := common.BytesToHash([]byte{0x03, 0x14}) + + clearComputedFieldsOnReceipts(t, receipts) + + if err := SetReceiptsData(params.MainnetChainConfig, blockHash, blockNumber, body, receipts); err != nil { + t.Fatalf("SetReceiptsData(...) = %v, want ", err) + } + + signer := types.MakeSigner(params.MainnetChainConfig, blockNumber) + logIndex := uint(0) + for i := range receipts { + if receipts[i].TxHash != txs[i].Hash() { + t.Errorf("receipts[%d].TxHash = %s, want %s", i, receipts[i].TxHash.String(), txs[i].Hash().String()) + } + + if receipts[i].BlockHash != blockHash { + t.Errorf("receipts[%d].BlockHash = %s, want %s", i, receipts[i].BlockHash.String(), blockHash.String()) + } + + if receipts[i].BlockNumber.Cmp(blockNumber) != 0 { + t.Errorf("receipts[%c].BlockNumber = %s, want %s", i, receipts[i].BlockNumber.String(), blockNumber.String()) + } + + if receipts[i].TransactionIndex != uint(i) { + t.Errorf("receipts[%d].TransactionIndex = %d, want %d", i, receipts[i].TransactionIndex, i) + } + + if receipts[i].GasUsed != txs[i].Gas() { + t.Errorf("receipts[%d].GasUsed = %d, want %d", i, receipts[i].GasUsed, txs[i].Gas()) + } + + if txs[i].To() != nil && receipts[i].ContractAddress != (common.Address{}) { + t.Errorf("receipts[%d].ContractAddress = %s, want %s", i, receipts[i].ContractAddress, (common.Address{}).String()) + } + + from, _ := types.Sender(signer, txs[i]) + contractAddress := crypto.CreateAddress(from, txs[i].Nonce()) + if txs[i].To() == nil && receipts[i].ContractAddress != contractAddress { + t.Errorf("receipts[%d].ContractAddress = %s, want %s", i, receipts[i].ContractAddress.String(), contractAddress.String()) + } + + for j := range receipts[i].Logs { + if receipts[i].Logs[j].BlockNumber != blockNumber.Uint64() { + t.Errorf("receipts[%d].Logs[%d].BlockNumber = %d, want %d", i, j, receipts[i].Logs[j].BlockNumber, blockNumber.Uint64()) + } + + if receipts[i].Logs[j].BlockHash != blockHash { + t.Errorf("receipts[%d].Logs[%d].BlockHash = %s, want %s", i, j, receipts[i].Logs[j].BlockHash.String(), blockHash.String()) + } + + if receipts[i].Logs[j].TxHash != txs[i].Hash() { + t.Errorf("receipts[%d].Logs[%d].TxHash = %s, want %s", i, j, receipts[i].Logs[j].TxHash.String(), txs[i].Hash().String()) + } + + if receipts[i].Logs[j].TxHash != txs[i].Hash() { + t.Errorf("receipts[%d].Logs[%d].TxHash = %s, want %s", i, j, receipts[i].Logs[j].TxHash.String(), txs[i].Hash().String()) + } + + if receipts[i].Logs[j].TxIndex != uint(i) { + t.Errorf("receipts[%d].Logs[%d].TransactionIndex = %d, want %d", i, j, receipts[i].Logs[j].TxIndex, i) + } + + if receipts[i].Logs[j].Index != logIndex { + t.Errorf("receipts[%d].Logs[%d].Index = %d, want %d", i, j, receipts[i].Logs[j].Index, logIndex) + } + + logIndex++ + } + } +} + +func clearComputedFieldsOnReceipts(t *testing.T, receipts types.Receipts) { + t.Helper() + + for _, receipt := range receipts { + clearComputedFieldsOnReceipt(t, receipt) + } +} + +func clearComputedFieldsOnReceipt(t *testing.T, receipt *types.Receipt) { + t.Helper() + + receipt.TxHash = common.Hash{} + receipt.BlockHash = common.Hash{} + receipt.BlockNumber = big.NewInt(math.MaxUint32) + receipt.TransactionIndex = math.MaxUint32 + receipt.ContractAddress = common.Address{} + receipt.GasUsed = 0 + + clearComputedFieldsOnLogs(t, receipt.Logs) +} + +func clearComputedFieldsOnLogs(t *testing.T, logs []*types.Log) { + t.Helper() + + for _, log := range logs { + clearComputedFieldsOnLog(t, log) + } +} + +func clearComputedFieldsOnLog(t *testing.T, log *types.Log) { + t.Helper() + + log.BlockNumber = math.MaxUint32 + log.BlockHash = common.Hash{} + log.TxHash = common.Hash{} + log.TxIndex = math.MaxUint32 + log.Index = math.MaxUint32 +} diff --git a/core/types/log.go b/core/types/log.go index 864af5ef4..006f62bbf 100644 --- a/core/types/log.go +++ b/core/types/log.go @@ -71,8 +71,8 @@ type rlpLog struct { // rlpStorageLog is the storage encoding of a log. type rlpStorageLog rlpLog -// LegacyRlpStorageLog is the previous storage encoding of a log including some redundant fields. -type LegacyRlpStorageLog struct { +// legacyRlpStorageLog is the previous storage encoding of a log including some redundant fields. +type legacyRlpStorageLog struct { Address common.Address Topics []common.Hash Data []byte @@ -129,7 +129,7 @@ func (l *LogForStorage) DecodeRLP(s *rlp.Stream) error { } } else { // Try to decode log with previous definition. - var dec LegacyRlpStorageLog + var dec legacyRlpStorageLog err = rlp.DecodeBytes(blob, &dec) if err == nil { *l = LogForStorage{ diff --git a/core/types/receipt.go b/core/types/receipt.go index 72d85a1e8..dc8177079 100644 --- a/core/types/receipt.go +++ b/core/types/receipt.go @@ -82,8 +82,15 @@ type receiptRLP struct { Logs []*Log } -// receiptStorageRLP is the storage encoding of a receipt. -type receiptStorageRLP struct { +// storedReceiptRLP is the storage encoding of a receipt. +type storedReceiptRLP struct { + PostStateOrStatus []byte + CumulativeGasUsed uint64 + Logs []*LogForStorage +} + +// v4StoredReceiptRLP is the storage encoding of a receipt used in database version 4. +type v4StoredReceiptRLP struct { PostStateOrStatus []byte CumulativeGasUsed uint64 TxHash common.Hash @@ -92,8 +99,8 @@ type receiptStorageRLP struct { GasUsed uint64 } -// LegacyReceiptStorageRLP is the previous storage encoding of a receipt including some unnecessary fields. -type LegacyReceiptStorageRLP struct { +// legacyStoredReceiptRLP is the original storage encoding of a receipt including some unnecessary fields. +type legacyStoredReceiptRLP struct { PostStateOrStatus []byte CumulativeGasUsed uint64 Bloom Bloom @@ -177,13 +184,10 @@ type ReceiptForStorage Receipt // EncodeRLP implements rlp.Encoder, and flattens all content fields of a receipt // into an RLP stream. func (r *ReceiptForStorage) EncodeRLP(w io.Writer) error { - enc := &receiptStorageRLP{ + enc := &storedReceiptRLP{ PostStateOrStatus: (*Receipt)(r).statusEncoding(), CumulativeGasUsed: r.CumulativeGasUsed, - TxHash: r.TxHash, - ContractAddress: r.ContractAddress, Logs: make([]*LogForStorage, len(r.Logs)), - GasUsed: r.GasUsed, } for i, log := range r.Logs { enc.Logs[i] = (*LogForStorage)(log) @@ -198,32 +202,81 @@ func (r *ReceiptForStorage) DecodeRLP(s *rlp.Stream) error { if err != nil { return err } - var dec receiptStorageRLP - if err := rlp.DecodeBytes(blob, &dec); err != nil { - var sdec LegacyReceiptStorageRLP - if err := rlp.DecodeBytes(blob, &sdec); err != nil { - return err - } - dec.PostStateOrStatus = common.CopyBytes(sdec.PostStateOrStatus) - dec.CumulativeGasUsed = sdec.CumulativeGasUsed - dec.TxHash = sdec.TxHash - dec.ContractAddress = sdec.ContractAddress - dec.Logs = sdec.Logs - dec.GasUsed = sdec.GasUsed + + if err := decodeStoredReceiptRLP(r, blob); err == nil { + return nil } - if err := (*Receipt)(r).setStatus(dec.PostStateOrStatus); err != nil { + + if err := decodeV4StoredReceiptRLP(r, blob); err == nil { + return nil + } + + return decodeLegacyStoredReceiptRLP(r, blob) +} + +func decodeStoredReceiptRLP(r *ReceiptForStorage, blob []byte) error { + var stored storedReceiptRLP + if err := rlp.DecodeBytes(blob, &stored); err != nil { return err } - // Assign the consensus fields - r.CumulativeGasUsed = dec.CumulativeGasUsed - r.Logs = make([]*Log, len(dec.Logs)) - for i, log := range dec.Logs { + + if err := (*Receipt)(r).setStatus(stored.PostStateOrStatus); err != nil { + return err + } + + r.CumulativeGasUsed = stored.CumulativeGasUsed + r.Logs = make([]*Log, len(stored.Logs)) + for i, log := range stored.Logs { + r.Logs[i] = (*Log)(log) + } + r.Bloom = CreateBloom(Receipts{(*Receipt)(r)}) + + return nil +} + +func decodeV4StoredReceiptRLP(r *ReceiptForStorage, blob []byte) error { + var stored v4StoredReceiptRLP + if err := rlp.DecodeBytes(blob, &stored); err != nil { + return err + } + + if err := (*Receipt)(r).setStatus(stored.PostStateOrStatus); err != nil { + return err + } + + r.CumulativeGasUsed = stored.CumulativeGasUsed + r.TxHash = stored.TxHash + r.ContractAddress = stored.ContractAddress + r.GasUsed = stored.GasUsed + r.Logs = make([]*Log, len(stored.Logs)) + for i, log := range stored.Logs { + r.Logs[i] = (*Log)(log) + } + r.Bloom = CreateBloom(Receipts{(*Receipt)(r)}) + + return nil +} + +func decodeLegacyStoredReceiptRLP(r *ReceiptForStorage, blob []byte) error { + var stored legacyStoredReceiptRLP + if err := rlp.DecodeBytes(blob, &stored); err != nil { + return err + } + + if err := (*Receipt)(r).setStatus(stored.PostStateOrStatus); err != nil { + return err + } + + r.CumulativeGasUsed = stored.GasUsed + r.Bloom = stored.Bloom + r.TxHash = stored.TxHash + r.ContractAddress = stored.ContractAddress + r.GasUsed = stored.GasUsed + r.Logs = make([]*Log, len(stored.Logs)) + for i, log := range stored.Logs { r.Logs[i] = (*Log)(log) } - r.Bloom = CreateBloom(Receipts{(*Receipt)(r)}) - // Assign the implementation fields - r.TxHash, r.ContractAddress, r.GasUsed = dec.TxHash, dec.ContractAddress, dec.GasUsed return nil } diff --git a/core/types/receipt_test.go b/core/types/receipt_test.go new file mode 100644 index 000000000..b76f30b5c --- /dev/null +++ b/core/types/receipt_test.go @@ -0,0 +1,133 @@ +// Copyright 2019 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package types + +import ( + "bytes" + "encoding/hex" + "encoding/json" + "math/big" + "testing" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/rlp" +) + +func TestLegacyReceiptDecoding(t *testing.T) { + tests := []struct { + name string + encode func(*Receipt) ([]byte, error) + }{ + { + "StoredReceiptRLP", + encodeAsStoredReceiptRLP, + }, + { + "V4StoredReceiptRLP", + encodeAsV4StoredReceiptRLP, + }, + { + "LegacyStoredReceiptRLP", + encodeAsLegacyStoredReceiptRLP, + }, + } + + tx := NewTransaction(1, common.HexToAddress("0x1"), big.NewInt(1), 1, big.NewInt(1), nil) + receipt := &Receipt{ + Status: ReceiptStatusFailed, + CumulativeGasUsed: 1, + Logs: []*Log{ + {Address: common.BytesToAddress([]byte{0x11})}, + {Address: common.BytesToAddress([]byte{0x01, 0x11})}, + }, + TxHash: tx.Hash(), + ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}), + GasUsed: 111111, + } + receipt.Bloom = CreateBloom(Receipts{receipt}) + + want, err := json.Marshal(receipt) + if err != nil { + t.Fatalf("Error encoding reference receipt to JSON: %v", err) + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + enc, err := tc.encode(receipt) + if err != nil { + t.Fatalf("Error encoding receipt: %v", err) + } + + var dec ReceiptForStorage + if err := rlp.DecodeBytes(enc, &dec); err != nil { + t.Fatalf("Error decoding RLP receipt: %v", err) + } + + have, err := rlp.EncodeToBytes((*Receipt)(receipt)) + if err != nil { + t.Fatalf("Error encoding receipt: %v", err) + } + + if !bytes.Equal(have, want) { + t.Fatalf("receipt mismatch: have %s, want %s", hex.EncodeToString(have), hex.EncodeToString(want)) + } + }) + } +} + +func encodeAsStoredReceiptRLP(want *Receipt) ([]byte, error) { + stored := &storedReceiptRLP{ + PostStateOrStatus: want.statusEncoding(), + CumulativeGasUsed: want.CumulativeGasUsed, + Logs: make([]*LogForStorage, len(want.Logs)), + } + for i, log := range want.Logs { + stored.Logs[i] = (*LogForStorage)(log) + } + return rlp.EncodeToBytes(stored) +} + +func encodeAsV4StoredReceiptRLP(want *Receipt) ([]byte, error) { + stored := &v4StoredReceiptRLP{ + PostStateOrStatus: want.statusEncoding(), + CumulativeGasUsed: want.CumulativeGasUsed, + TxHash: want.TxHash, + ContractAddress: want.ContractAddress, + Logs: make([]*LogForStorage, len(want.Logs)), + GasUsed: want.GasUsed, + } + for i, log := range want.Logs { + stored.Logs[i] = (*LogForStorage)(log) + } + return rlp.EncodeToBytes(stored) +} + +func encodeAsLegacyStoredReceiptRLP(want *Receipt) ([]byte, error) { + stored := &legacyStoredReceiptRLP{ + PostStateOrStatus: want.statusEncoding(), + CumulativeGasUsed: want.CumulativeGasUsed, + Bloom: want.Bloom, + TxHash: want.TxHash, + ContractAddress: want.ContractAddress, + Logs: make([]*LogForStorage, len(want.Logs)), + GasUsed: want.GasUsed, + } + for i, log := range want.Logs { + stored.Logs[i] = (*LogForStorage)(log) + } + return rlp.EncodeToBytes(stored) +} diff --git a/eth/filters/filter_test.go b/eth/filters/filter_test.go index 96d27bcf8..eafa19cdf 100644 --- a/eth/filters/filter_test.go +++ b/eth/filters/filter_test.go @@ -138,6 +138,7 @@ func TestFilters(t *testing.T) { }, } gen.AddUncheckedReceipt(receipt) + gen.AddUncheckedTx(types.NewTransaction(1, common.HexToAddress("0x1"), big.NewInt(1), 1, big.NewInt(1), nil)) case 2: receipt := types.NewReceipt(nil, false, 0) receipt.Logs = []*types.Log{ @@ -147,6 +148,8 @@ func TestFilters(t *testing.T) { }, } gen.AddUncheckedReceipt(receipt) + gen.AddUncheckedTx(types.NewTransaction(2, common.HexToAddress("0x2"), big.NewInt(2), 2, big.NewInt(2), nil)) + case 998: receipt := types.NewReceipt(nil, false, 0) receipt.Logs = []*types.Log{ @@ -156,6 +159,7 @@ func TestFilters(t *testing.T) { }, } gen.AddUncheckedReceipt(receipt) + gen.AddUncheckedTx(types.NewTransaction(998, common.HexToAddress("0x998"), big.NewInt(998), 998, big.NewInt(998), nil)) case 999: receipt := types.NewReceipt(nil, false, 0) receipt.Logs = []*types.Log{ @@ -165,6 +169,7 @@ func TestFilters(t *testing.T) { }, } gen.AddUncheckedReceipt(receipt) + gen.AddUncheckedTx(types.NewTransaction(999, common.HexToAddress("0x999"), big.NewInt(999), 999, big.NewInt(999), nil)) } }) for i, block := range chain { diff --git a/les/handler_test.go b/les/handler_test.go index c1db65cf3..c659f8088 100644 --- a/les/handler_test.go +++ b/les/handler_test.go @@ -292,7 +292,7 @@ func testGetReceipt(t *testing.T, protocol int) { block := bc.GetBlockByNumber(i) hashes = append(hashes, block.Hash()) - receipts = append(receipts, rawdb.ReadReceipts(server.db, block.Hash(), block.NumberU64())) + receipts = append(receipts, rawdb.ReadRawReceipts(server.db, block.Hash(), block.NumberU64())) } // Send the hash request and verify the response cost := server.tPeer.GetRequestCost(GetReceiptsMsg, len(hashes)) diff --git a/light/odr_test.go b/light/odr_test.go index 55725d84e..57b377c41 100644 --- a/light/odr_test.go +++ b/light/odr_test.go @@ -79,7 +79,7 @@ func (odr *testOdr) Retrieve(ctx context.Context, req OdrRequest) error { case *ReceiptsRequest: number := rawdb.ReadHeaderNumber(odr.sdb, req.Hash) if number != nil { - req.Receipts = rawdb.ReadReceipts(odr.sdb, req.Hash, *number) + req.Receipts = rawdb.ReadRawReceipts(odr.sdb, req.Hash, *number) } case *TrieRequest: t, _ := trie.New(req.Id.Root, trie.NewDatabase(odr.sdb)) diff --git a/light/odr_util.go b/light/odr_util.go index 00103a76b..f84de6671 100644 --- a/light/odr_util.go +++ b/light/odr_util.go @@ -21,7 +21,6 @@ import ( "context" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" @@ -127,8 +126,8 @@ func GetBlock(ctx context.Context, odr OdrBackend, hash common.Hash, number uint // GetBlockReceipts retrieves the receipts generated by the transactions included // in a block given by its hash. func GetBlockReceipts(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) (types.Receipts, error) { - // Retrieve the potentially incomplete receipts from disk or network - receipts := rawdb.ReadReceipts(odr.Database(), hash, number) + // Assume receipts are already stored locally and attempt to retrieve. + receipts := rawdb.ReadRawReceipts(odr.Database(), hash, number) if receipts == nil { r := &ReceiptsRequest{Hash: hash, Number: number} if err := odr.Retrieve(ctx, r); err != nil { @@ -136,6 +135,7 @@ func GetBlockReceipts(ctx context.Context, odr OdrBackend, hash common.Hash, num } receipts = r.Receipts } + // If the receipts are incomplete, fill the derived fields if len(receipts) > 0 && receipts[0].TxHash == (common.Hash{}) { block, err := GetBlock(ctx, odr, hash, number) @@ -145,11 +145,12 @@ func GetBlockReceipts(ctx context.Context, odr OdrBackend, hash common.Hash, num genesis := rawdb.ReadCanonicalHash(odr.Database(), 0) config := rawdb.ReadChainConfig(odr.Database(), genesis) - if err := core.SetReceiptsData(config, block, receipts); err != nil { + if err := rawdb.SetReceiptsData(config, block.Hash(), block.Number(), block.Body(), receipts); err != nil { return nil, err } rawdb.WriteReceipts(odr.Database(), hash, number, receipts) } + return receipts, nil } @@ -157,14 +158,11 @@ func GetBlockReceipts(ctx context.Context, odr OdrBackend, hash common.Hash, num // block given by its hash. func GetBlockLogs(ctx context.Context, odr OdrBackend, hash common.Hash, number uint64) ([][]*types.Log, error) { // Retrieve the potentially incomplete receipts from disk or network - receipts := rawdb.ReadReceipts(odr.Database(), hash, number) - if receipts == nil { - r := &ReceiptsRequest{Hash: hash, Number: number} - if err := odr.Retrieve(ctx, r); err != nil { - return nil, err - } - receipts = r.Receipts + receipts, err := GetBlockReceipts(ctx, odr, hash, number) + if err != nil { + return nil, err } + // Return the logs without deriving any computed fields on the receipts logs := make([][]*types.Log, len(receipts)) for i, receipt := range receipts { From 7221cb1434eebce879a0bd1bf3867cd8a24bcc99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Mon, 15 Apr 2019 12:36:27 +0300 Subject: [PATCH 2/3] core, eth, les, light: scope receipt functionality a bit cleaner --- accounts/abi/bind/backends/simulated.go | 6 +- core/bench_test.go | 2 +- core/blockchain.go | 8 +- core/blockchain_test.go | 8 +- core/rawdb/accessors_chain.go | 77 ++-------- core/rawdb/accessors_chain_test.go | 182 ++---------------------- core/rawdb/accessors_indexes.go | 7 +- core/types/block_test.go | 5 - core/types/receipt.go | 72 +++++++--- core/types/receipt_test.go | 144 ++++++++++++++++++- eth/downloader/fakepeer.go | 2 +- eth/filters/filter_system_test.go | 4 +- les/handler.go | 2 +- les/odr_test.go | 11 +- light/odr_test.go | 2 +- light/odr_util.go | 5 +- 16 files changed, 250 insertions(+), 287 deletions(-) diff --git a/accounts/abi/bind/backends/simulated.go b/accounts/abi/bind/backends/simulated.go index 7371dfb1f..ac4602fe3 100644 --- a/accounts/abi/bind/backends/simulated.go +++ b/accounts/abi/bind/backends/simulated.go @@ -160,7 +160,7 @@ func (b *SimulatedBackend) StorageAt(ctx context.Context, contract common.Addres // TransactionReceipt returns the receipt of a transaction. func (b *SimulatedBackend) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) { - receipt, _, _, _ := rawdb.ReadReceipt(b.database, txHash) + receipt, _, _, _ := rawdb.ReadReceipt(b.database, txHash, b.config) return receipt, nil } @@ -464,7 +464,7 @@ func (fb *filterBackend) GetReceipts(ctx context.Context, hash common.Hash) (typ if number == nil { return nil, nil } - return rawdb.ReadReceipts(fb.db, hash, *number), nil + return rawdb.ReadReceipts(fb.db, hash, *number, fb.bc.Config()), nil } func (fb *filterBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types.Log, error) { @@ -472,7 +472,7 @@ func (fb *filterBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*ty if number == nil { return nil, nil } - receipts := rawdb.ReadReceipts(fb.db, hash, *number) + receipts := rawdb.ReadReceipts(fb.db, hash, *number, fb.bc.Config()) if receipts == nil { return nil, nil } diff --git a/core/bench_test.go b/core/bench_test.go index e0ccef788..79c369d5f 100644 --- a/core/bench_test.go +++ b/core/bench_test.go @@ -297,7 +297,7 @@ func benchReadChain(b *testing.B, full bool, count uint64) { if full { hash := header.Hash() rawdb.ReadBody(db, hash, n) - rawdb.ReadReceipts(db, hash, n) + rawdb.ReadReceipts(db, hash, n, chain.Config()) } } chain.Stop() diff --git a/core/blockchain.go b/core/blockchain.go index cb28335be..bcfd406ea 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -653,7 +653,7 @@ func (bc *BlockChain) GetReceiptsByHash(hash common.Hash) types.Receipts { if number == nil { return nil } - receipts := rawdb.ReadReceipts(bc.db, hash, *number) + receipts := rawdb.ReadReceipts(bc.db, hash, *number, bc.chainConfig) if receipts == nil { return nil } @@ -828,8 +828,8 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [ continue } // Compute all the non-consensus fields of the receipts - if err := rawdb.SetReceiptsData(bc.chainConfig, block.Hash(), block.Number(), block.Body(), receipts); err != nil { - return i, fmt.Errorf("failed to set receipts data: %v", err) + if err := receipts.DeriveFields(bc.chainConfig, block.Hash(), block.NumberU64(), block.Transactions()); err != nil { + return i, fmt.Errorf("failed to derive receipts data: %v", err) } // Write all the data out into the database rawdb.WriteBody(batch, block.Hash(), block.NumberU64(), block.Body()) @@ -1451,7 +1451,7 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error { if number == nil { return } - receipts := rawdb.ReadReceipts(bc.db, hash, *number) + receipts := rawdb.ReadReceipts(bc.db, hash, *number, bc.chainConfig) for _, receipt := range receipts { for _, log := range receipt.Logs { l := *log diff --git a/core/blockchain_test.go b/core/blockchain_test.go index d6be6c7e8..5ee1d9f8e 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -658,7 +658,7 @@ func TestFastVsFullChains(t *testing.T) { } else if types.CalcUncleHash(fblock.Uncles()) != types.CalcUncleHash(ablock.Uncles()) { t.Errorf("block #%d [%x]: uncles mismatch: have %v, want %v", num, hash, fblock.Uncles(), ablock.Uncles()) } - 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) { + 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) { t.Errorf("block #%d [%x]: receipts mismatch: have %v, want %v", num, hash, freceipts, areceipts) } } @@ -843,7 +843,7 @@ func TestChainTxReorgs(t *testing.T) { if txn, _, _, _ := rawdb.ReadTransaction(db, tx.Hash()); txn != nil { t.Errorf("drop %d: tx %v found while shouldn't have been", i, txn) } - if rcpt, _, _, _ := rawdb.ReadReceipt(db, tx.Hash()); rcpt != nil { + if rcpt, _, _, _ := rawdb.ReadReceipt(db, tx.Hash(), blockchain.Config()); rcpt != nil { t.Errorf("drop %d: receipt %v found while shouldn't have been", i, rcpt) } } @@ -852,7 +852,7 @@ func TestChainTxReorgs(t *testing.T) { if txn, _, _, _ := rawdb.ReadTransaction(db, tx.Hash()); txn == nil { t.Errorf("add %d: expected tx to be found", i) } - if rcpt, _, _, _ := rawdb.ReadReceipt(db, tx.Hash()); rcpt == nil { + if rcpt, _, _, _ := rawdb.ReadReceipt(db, tx.Hash(), blockchain.Config()); rcpt == nil { t.Errorf("add %d: expected receipt to be found", i) } } @@ -861,7 +861,7 @@ func TestChainTxReorgs(t *testing.T) { if txn, _, _, _ := rawdb.ReadTransaction(db, tx.Hash()); txn == nil { t.Errorf("share %d: expected tx to be found", i) } - if rcpt, _, _, _ := rawdb.ReadReceipt(db, tx.Hash()); rcpt == nil { + if rcpt, _, _, _ := rawdb.ReadReceipt(db, tx.Hash(), blockchain.Config()); rcpt == nil { t.Errorf("share %d: expected receipt to be found", i) } } diff --git a/core/rawdb/accessors_chain.go b/core/rawdb/accessors_chain.go index 8a5e95bc7..8bbf464d1 100644 --- a/core/rawdb/accessors_chain.go +++ b/core/rawdb/accessors_chain.go @@ -19,12 +19,10 @@ package rawdb import ( "bytes" "encoding/binary" - "errors" "math/big" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" @@ -311,20 +309,16 @@ func ReadRawReceipts(db ethdb.Reader, hash common.Hash, number uint64) types.Rec if len(data) == 0 { return nil } - // Convert the receipts from their storage form to their internal representation storageReceipts := []*types.ReceiptForStorage{} if err := rlp.DecodeBytes(data, &storageReceipts); err != nil { log.Error("Invalid receipt array RLP", "hash", hash, "err", err) return nil } - - var receipts types.Receipts - for _, storageReceipt := range storageReceipts { - receipt := (*types.Receipt)(storageReceipt) - receipts = append(receipts, receipt) + receipts := make(types.Receipts, len(storageReceipts)) + for i, storageReceipt := range storageReceipts { + receipts[i] = (*types.Receipt)(storageReceipt) } - return receipts } @@ -335,79 +329,24 @@ func ReadRawReceipts(db ethdb.Reader, hash common.Hash, number uint64) types.Rec // The current implementation populates these metadata fields by reading the receipts' // corresponding block body, so if the block body is not found it will return nil even // if the receipt itself is stored. -func ReadReceipts(db ethdb.Reader, hash common.Hash, number uint64) types.Receipts { +func ReadReceipts(db ethdb.Reader, hash common.Hash, number uint64, config *params.ChainConfig) types.Receipts { + // We're deriving many fields from the block body, retrieve beside the receipt receipts := ReadRawReceipts(db, hash, number) if receipts == nil { - return receipts + return nil } - - // Retrieve the block body to populate missing fields for receipts and logs body := ReadBody(db, hash, number) if body == nil { log.Error("Missing body but have receipt", "hash", hash, "number", number) return nil } - - genesisHash := ReadCanonicalHash(db, 0) - if genesisHash == (common.Hash{}) { - log.Error("Missing genesis hash") + if err := receipts.DeriveFields(config, hash, number, body.Transactions); err != nil { + log.Error("Failed to derive block receipts fields", "hash", hash, "number", number, "err", err) return nil } - - config := ReadChainConfig(db, genesisHash) - if config == nil { - log.Error("Missing chain config ", "hash", hash) - return nil - } - - SetReceiptsData(config, hash, big.NewInt(int64(number)), body, receipts) - return receipts } -// SetReceiptsData computes all the non-consensus fields of the receipts -func SetReceiptsData(config *params.ChainConfig, blockHash common.Hash, blockNumber *big.Int, body *types.Body, receipts types.Receipts) error { - signer := types.MakeSigner(config, blockNumber) - - transactions, logIndex := body.Transactions, uint(0) - if len(transactions) != len(receipts) { - return errors.New("transaction and receipt count mismatch") - } - - for j := 0; j < len(receipts); j++ { - // The transaction hash can be retrieved from the transaction itself - receipts[j].TxHash = transactions[j].Hash() - - // block location fields - receipts[j].BlockHash = blockHash - receipts[j].BlockNumber = blockNumber - receipts[j].TransactionIndex = uint(j) - - // The contract address can be derived from the transaction itself - if transactions[j].To() == nil { - // Deriving the signer is expensive, only do if it's actually needed - from, _ := types.Sender(signer, transactions[j]) - receipts[j].ContractAddress = crypto.CreateAddress(from, transactions[j].Nonce()) - } - // The used gas can be calculated based on previous receipts - if j == 0 { - receipts[j].GasUsed = receipts[j].CumulativeGasUsed - } else { - receipts[j].GasUsed = receipts[j].CumulativeGasUsed - receipts[j-1].CumulativeGasUsed - } - // The derived log fields can simply be set from the block and transaction - for k := 0; k < len(receipts[j].Logs); k++ { - receipts[j].Logs[k].BlockNumber = blockNumber.Uint64() - receipts[j].Logs[k].BlockHash = blockHash - receipts[j].Logs[k].TxHash = receipts[j].TxHash - receipts[j].Logs[k].TxIndex = uint(j) - receipts[j].Logs[k].Index = logIndex - logIndex++ - } - } - return nil -} - // WriteReceipts stores all the transaction receipts belonging to a block. func WriteReceipts(db ethdb.Writer, hash common.Hash, number uint64, receipts types.Receipts) { // Convert the receipts into their storage form and serialize them diff --git a/core/rawdb/accessors_chain_test.go b/core/rawdb/accessors_chain_test.go index 9ee896ba7..8c8affffd 100644 --- a/core/rawdb/accessors_chain_test.go +++ b/core/rawdb/accessors_chain_test.go @@ -20,14 +20,12 @@ import ( "bytes" "encoding/hex" "fmt" - "github.com/ethereum/go-ethereum/common/math" - "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/params" "math/big" "testing" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" "golang.org/x/crypto/sha3" ) @@ -272,11 +270,13 @@ func TestHeadStorage(t *testing.T) { func TestBlockReceiptStorage(t *testing.T) { db := NewMemoryDatabase() + // Create a live block since we need metadata to reconstruct the receipt tx1 := types.NewTransaction(1, common.HexToAddress("0x1"), big.NewInt(1), 1, big.NewInt(1), nil) tx2 := types.NewTransaction(2, common.HexToAddress("0x2"), big.NewInt(2), 2, big.NewInt(2), nil) - // Include block needed to read metadata. + body := &types.Body{Transactions: types.Transactions{tx1, tx2}} + // Create the two receipts to manage afterwards receipt1 := &types.Receipt{ Status: types.ReceiptStatusFailed, CumulativeGasUsed: 1, @@ -289,6 +289,7 @@ func TestBlockReceiptStorage(t *testing.T) { GasUsed: 111111, } receipt1.Bloom = types.CreateBloom(types.Receipts{receipt1}) + receipt2 := &types.Receipt{ PostState: common.Hash{2}.Bytes(), CumulativeGasUsed: 2, @@ -305,41 +306,35 @@ func TestBlockReceiptStorage(t *testing.T) { // Check that no receipt entries are in a pristine database hash := common.BytesToHash([]byte{0x03, 0x14}) - if rs := ReadReceipts(db, hash, 0); len(rs) != 0 { + if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); len(rs) != 0 { t.Fatalf("non existent receipts returned: %v", rs) } - // Insert the body that corresponds to the receipts. + // Insert the body that corresponds to the receipts WriteBody(db, hash, 0, body) + // Insert the receipt slice into the database and check presence WriteReceipts(db, hash, 0, receipts) - // Insert canonical hash that the chain configuration will be mapped to. - WriteCanonicalHash(db, hash, 0) - // Insert the chain configuration. - WriteChainConfig(db, hash, params.MainnetChainConfig) - if rs := ReadReceipts(db, hash, 0); len(rs) == 0 { + if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); len(rs) == 0 { t.Fatalf("no receipts returned") } else { if err := checkReceiptsRLP(rs, receipts); err != nil { t.Fatalf(err.Error()) } } - + // Delete the body and ensure that the receipts are no longer returned (metadata can't be recomputed) DeleteBody(db, hash, 0) - // Check that receipts are no longer returned when metadata cannot be recomputed. - if rs := ReadReceipts(db, hash, 0); rs != nil { + if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); rs != nil { t.Fatalf("receipts returned when body was deleted: %v", rs) } - // Check that receipts without metadata can be returned when specifically - rs := ReadRawReceipts(db, hash, 0) - if err := checkReceiptsRLP(rs, receipts); err != nil { + // Ensure that receipts without metadata can be returned without the block body too + if err := checkReceiptsRLP(ReadRawReceipts(db, hash, 0), receipts); err != nil { t.Fatalf(err.Error()) } - // Re-insert the body that corresponds to the receipts. + // Sanity check that body alone without the receipt is a full purge WriteBody(db, hash, 0, body) - // Delete the receipt slice and check purge DeleteReceipts(db, hash, 0) - if rs := ReadReceipts(db, hash, 0); len(rs) != 0 { + if rs := ReadReceipts(db, hash, 0, params.TestChainConfig); len(rs) != 0 { t.Fatalf("deleted receipts returned: %v", rs) } } @@ -348,7 +343,6 @@ func checkReceiptsRLP(have, want types.Receipts) error { if len(have) != len(want) { return fmt.Errorf("receipts sizes mismatch: have %d, want %d", len(have), len(want)) } - for i := 0; i < len(want); i++ { rlpHave, err := rlp.EncodeToBytes(have[i]) if err != nil { @@ -358,155 +352,9 @@ func checkReceiptsRLP(have, want types.Receipts) error { if err != nil { return err } - if !bytes.Equal(rlpHave, rlpWant) { return fmt.Errorf("receipt #%d: receipt mismatch: have %s, want %s", i, hex.EncodeToString(rlpHave), hex.EncodeToString(rlpWant)) } } - return nil } - -// Tests that receipts associated with a single block can be stored and retrieved. -func TestSetReceiptsData(t *testing.T) { - tx0 := types.NewContractCreation(1, big.NewInt(1), 1, big.NewInt(1), nil) - tx1 := types.NewTransaction(2, common.HexToAddress("0x2"), big.NewInt(2), 2, big.NewInt(2), nil) - txs := types.Transactions{tx0, tx1} - // Include block needed to read metadata. - body := &types.Body{Transactions: txs} - - receipt1 := &types.Receipt{ - Status: types.ReceiptStatusFailed, - CumulativeGasUsed: 1, - Logs: []*types.Log{ - {Address: common.BytesToAddress([]byte{0x11})}, - {Address: common.BytesToAddress([]byte{0x01, 0x11})}, - }, - TxHash: tx0.Hash(), - ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}), - GasUsed: 1, - } - receipt2 := &types.Receipt{ - PostState: common.Hash{2}.Bytes(), - CumulativeGasUsed: 3, - Logs: []*types.Log{ - {Address: common.BytesToAddress([]byte{0x22})}, - {Address: common.BytesToAddress([]byte{0x02, 0x22})}, - }, - TxHash: tx1.Hash(), - ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}), - GasUsed: 2, - } - receipt2.Bloom = types.CreateBloom(types.Receipts{receipt2}) - receipts := []*types.Receipt{receipt1, receipt2} - - blockNumber := big.NewInt(1) - blockHash := common.BytesToHash([]byte{0x03, 0x14}) - - clearComputedFieldsOnReceipts(t, receipts) - - if err := SetReceiptsData(params.MainnetChainConfig, blockHash, blockNumber, body, receipts); err != nil { - t.Fatalf("SetReceiptsData(...) = %v, want ", err) - } - - signer := types.MakeSigner(params.MainnetChainConfig, blockNumber) - logIndex := uint(0) - for i := range receipts { - if receipts[i].TxHash != txs[i].Hash() { - t.Errorf("receipts[%d].TxHash = %s, want %s", i, receipts[i].TxHash.String(), txs[i].Hash().String()) - } - - if receipts[i].BlockHash != blockHash { - t.Errorf("receipts[%d].BlockHash = %s, want %s", i, receipts[i].BlockHash.String(), blockHash.String()) - } - - if receipts[i].BlockNumber.Cmp(blockNumber) != 0 { - t.Errorf("receipts[%c].BlockNumber = %s, want %s", i, receipts[i].BlockNumber.String(), blockNumber.String()) - } - - if receipts[i].TransactionIndex != uint(i) { - t.Errorf("receipts[%d].TransactionIndex = %d, want %d", i, receipts[i].TransactionIndex, i) - } - - if receipts[i].GasUsed != txs[i].Gas() { - t.Errorf("receipts[%d].GasUsed = %d, want %d", i, receipts[i].GasUsed, txs[i].Gas()) - } - - if txs[i].To() != nil && receipts[i].ContractAddress != (common.Address{}) { - t.Errorf("receipts[%d].ContractAddress = %s, want %s", i, receipts[i].ContractAddress, (common.Address{}).String()) - } - - from, _ := types.Sender(signer, txs[i]) - contractAddress := crypto.CreateAddress(from, txs[i].Nonce()) - if txs[i].To() == nil && receipts[i].ContractAddress != contractAddress { - t.Errorf("receipts[%d].ContractAddress = %s, want %s", i, receipts[i].ContractAddress.String(), contractAddress.String()) - } - - for j := range receipts[i].Logs { - if receipts[i].Logs[j].BlockNumber != blockNumber.Uint64() { - t.Errorf("receipts[%d].Logs[%d].BlockNumber = %d, want %d", i, j, receipts[i].Logs[j].BlockNumber, blockNumber.Uint64()) - } - - if receipts[i].Logs[j].BlockHash != blockHash { - t.Errorf("receipts[%d].Logs[%d].BlockHash = %s, want %s", i, j, receipts[i].Logs[j].BlockHash.String(), blockHash.String()) - } - - if receipts[i].Logs[j].TxHash != txs[i].Hash() { - t.Errorf("receipts[%d].Logs[%d].TxHash = %s, want %s", i, j, receipts[i].Logs[j].TxHash.String(), txs[i].Hash().String()) - } - - if receipts[i].Logs[j].TxHash != txs[i].Hash() { - t.Errorf("receipts[%d].Logs[%d].TxHash = %s, want %s", i, j, receipts[i].Logs[j].TxHash.String(), txs[i].Hash().String()) - } - - if receipts[i].Logs[j].TxIndex != uint(i) { - t.Errorf("receipts[%d].Logs[%d].TransactionIndex = %d, want %d", i, j, receipts[i].Logs[j].TxIndex, i) - } - - if receipts[i].Logs[j].Index != logIndex { - t.Errorf("receipts[%d].Logs[%d].Index = %d, want %d", i, j, receipts[i].Logs[j].Index, logIndex) - } - - logIndex++ - } - } -} - -func clearComputedFieldsOnReceipts(t *testing.T, receipts types.Receipts) { - t.Helper() - - for _, receipt := range receipts { - clearComputedFieldsOnReceipt(t, receipt) - } -} - -func clearComputedFieldsOnReceipt(t *testing.T, receipt *types.Receipt) { - t.Helper() - - receipt.TxHash = common.Hash{} - receipt.BlockHash = common.Hash{} - receipt.BlockNumber = big.NewInt(math.MaxUint32) - receipt.TransactionIndex = math.MaxUint32 - receipt.ContractAddress = common.Address{} - receipt.GasUsed = 0 - - clearComputedFieldsOnLogs(t, receipt.Logs) -} - -func clearComputedFieldsOnLogs(t *testing.T, logs []*types.Log) { - t.Helper() - - for _, log := range logs { - clearComputedFieldsOnLog(t, log) - } -} - -func clearComputedFieldsOnLog(t *testing.T, log *types.Log) { - t.Helper() - - log.BlockNumber = math.MaxUint32 - log.BlockHash = common.Hash{} - log.TxHash = common.Hash{} - log.TxIndex = math.MaxUint32 - log.Index = math.MaxUint32 -} diff --git a/core/rawdb/accessors_indexes.go b/core/rawdb/accessors_indexes.go index 5c7ad6934..530989850 100644 --- a/core/rawdb/accessors_indexes.go +++ b/core/rawdb/accessors_indexes.go @@ -21,6 +21,7 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" ) @@ -85,7 +86,8 @@ func ReadTransaction(db ethdb.Reader, hash common.Hash) (*types.Transaction, com // ReadReceipt retrieves a specific transaction receipt from the database, along with // its added positional metadata. -func ReadReceipt(db ethdb.Reader, hash common.Hash) (*types.Receipt, common.Hash, uint64, uint64) { +func ReadReceipt(db ethdb.Reader, hash common.Hash, config *params.ChainConfig) (*types.Receipt, common.Hash, uint64, uint64) { + // Retrieve the context of the receipt based on the transaction hash blockHash := ReadTxLookupEntry(db, hash) if blockHash == (common.Hash{}) { return nil, common.Hash{}, 0, 0 @@ -94,7 +96,8 @@ func ReadReceipt(db ethdb.Reader, hash common.Hash) (*types.Receipt, common.Hash if blockNumber == nil { return nil, common.Hash{}, 0, 0 } - receipts := ReadReceipts(db, blockHash, *blockNumber) + // Read all the receipts from the block and return the one with the matching hash + receipts := ReadReceipts(db, blockHash, *blockNumber, config) for receiptIndex, receipt := range receipts { if receipt.TxHash == hash { return receipt, blockHash, *blockNumber, uint64(receiptIndex) diff --git a/core/types/block_test.go b/core/types/block_test.go index bdd0b6571..ff0a641e5 100644 --- a/core/types/block_test.go +++ b/core/types/block_test.go @@ -18,7 +18,6 @@ package types import ( "bytes" - "fmt" "math/big" "reflect" "testing" @@ -52,11 +51,7 @@ func TestBlockEncoding(t *testing.T) { check("Size", block.Size(), common.StorageSize(len(blockEnc))) tx1 := NewTransaction(0, common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"), big.NewInt(10), 50000, big.NewInt(10), nil) - tx1, _ = tx1.WithSignature(HomesteadSigner{}, common.Hex2Bytes("9bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094f8a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b100")) - fmt.Println(block.Transactions()[0].Hash()) - fmt.Println(tx1.data) - fmt.Println(tx1.Hash()) check("len(Transactions)", len(block.Transactions()), 1) check("Transactions[0].Hash", block.Transactions()[0].Hash(), tx1.Hash()) diff --git a/core/types/receipt.go b/core/types/receipt.go index dc8177079..578daba3f 100644 --- a/core/types/receipt.go +++ b/core/types/receipt.go @@ -18,6 +18,7 @@ package types import ( "bytes" + "errors" "fmt" "io" "math/big" @@ -25,6 +26,8 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" ) @@ -99,8 +102,8 @@ type v4StoredReceiptRLP struct { GasUsed uint64 } -// legacyStoredReceiptRLP is the original storage encoding of a receipt including some unnecessary fields. -type legacyStoredReceiptRLP struct { +// v3StoredReceiptRLP is the original storage encoding of a receipt including some unnecessary fields. +type v3StoredReceiptRLP struct { PostStateOrStatus []byte CumulativeGasUsed uint64 Bloom Bloom @@ -198,20 +201,21 @@ func (r *ReceiptForStorage) EncodeRLP(w io.Writer) error { // DecodeRLP implements rlp.Decoder, and loads both consensus and implementation // fields of a receipt from an RLP stream. func (r *ReceiptForStorage) DecodeRLP(s *rlp.Stream) error { + // Retrieve the entire receipt blob as we need to try multiple decoders blob, err := s.Raw() if err != nil { return err } - + // Try decoding from the newest format for future proofness, then the older one + // for old nodes that just upgraded. V4 was an intermediate unreleased format so + // we do need to decode it, but it's not common (try last). if err := decodeStoredReceiptRLP(r, blob); err == nil { return nil } - - if err := decodeV4StoredReceiptRLP(r, blob); err == nil { + if err := decodeV3StoredReceiptRLP(r, blob); err == nil { return nil } - - return decodeLegacyStoredReceiptRLP(r, blob) + return decodeV4StoredReceiptRLP(r, blob) } func decodeStoredReceiptRLP(r *ReceiptForStorage, blob []byte) error { @@ -219,11 +223,9 @@ func decodeStoredReceiptRLP(r *ReceiptForStorage, blob []byte) error { if err := rlp.DecodeBytes(blob, &stored); err != nil { return err } - if err := (*Receipt)(r).setStatus(stored.PostStateOrStatus); err != nil { return err } - r.CumulativeGasUsed = stored.CumulativeGasUsed r.Logs = make([]*Log, len(stored.Logs)) for i, log := range stored.Logs { @@ -239,11 +241,9 @@ func decodeV4StoredReceiptRLP(r *ReceiptForStorage, blob []byte) error { if err := rlp.DecodeBytes(blob, &stored); err != nil { return err } - if err := (*Receipt)(r).setStatus(stored.PostStateOrStatus); err != nil { return err } - r.CumulativeGasUsed = stored.CumulativeGasUsed r.TxHash = stored.TxHash r.ContractAddress = stored.ContractAddress @@ -257,16 +257,14 @@ func decodeV4StoredReceiptRLP(r *ReceiptForStorage, blob []byte) error { return nil } -func decodeLegacyStoredReceiptRLP(r *ReceiptForStorage, blob []byte) error { - var stored legacyStoredReceiptRLP +func decodeV3StoredReceiptRLP(r *ReceiptForStorage, blob []byte) error { + var stored v3StoredReceiptRLP if err := rlp.DecodeBytes(blob, &stored); err != nil { return err } - if err := (*Receipt)(r).setStatus(stored.PostStateOrStatus); err != nil { return err } - r.CumulativeGasUsed = stored.GasUsed r.Bloom = stored.Bloom r.TxHash = stored.TxHash @@ -276,7 +274,6 @@ func decodeLegacyStoredReceiptRLP(r *ReceiptForStorage, blob []byte) error { for i, log := range stored.Logs { r.Logs[i] = (*Log)(log) } - return nil } @@ -294,3 +291,46 @@ func (r Receipts) GetRlp(i int) []byte { } return bytes } + +// DeriveFields fills the receipts with their computed fields based on consensus +// data and contextual infos like containing block and transactions. +func (r Receipts) DeriveFields(config *params.ChainConfig, hash common.Hash, number uint64, txs Transactions) error { + signer := MakeSigner(config, new(big.Int).SetUint64(number)) + + logIndex := uint(0) + if len(txs) != len(r) { + return errors.New("transaction and receipt count mismatch") + } + for i := 0; i < len(r); i++ { + // The transaction hash can be retrieved from the transaction itself + r[i].TxHash = txs[i].Hash() + + // block location fields + r[i].BlockHash = hash + r[i].BlockNumber = new(big.Int).SetUint64(number) + r[i].TransactionIndex = uint(i) + + // The contract address can be derived from the transaction itself + if txs[i].To() == nil { + // Deriving the signer is expensive, only do if it's actually needed + from, _ := Sender(signer, txs[i]) + r[i].ContractAddress = crypto.CreateAddress(from, txs[i].Nonce()) + } + // The used gas can be calculated based on previous r + if i == 0 { + r[i].GasUsed = r[i].CumulativeGasUsed + } else { + r[i].GasUsed = r[i].CumulativeGasUsed - r[i-1].CumulativeGasUsed + } + // The derived log fields can simply be set from the block and transaction + for j := 0; j < len(r[i].Logs); j++ { + r[i].Logs[j].BlockNumber = number + r[i].Logs[j].BlockHash = hash + r[i].Logs[j].TxHash = r[i].TxHash + r[i].Logs[j].TxIndex = uint(i) + r[i].Logs[j].Index = logIndex + logIndex++ + } + } + return nil +} diff --git a/core/types/receipt_test.go b/core/types/receipt_test.go index b76f30b5c..5af340b07 100644 --- a/core/types/receipt_test.go +++ b/core/types/receipt_test.go @@ -20,10 +20,13 @@ import ( "bytes" "encoding/hex" "encoding/json" + "math" "math/big" "testing" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" ) @@ -41,8 +44,8 @@ func TestLegacyReceiptDecoding(t *testing.T) { encodeAsV4StoredReceiptRLP, }, { - "LegacyStoredReceiptRLP", - encodeAsLegacyStoredReceiptRLP, + "V3StoredReceiptRLP", + encodeAsV3StoredReceiptRLP, }, } @@ -116,8 +119,8 @@ func encodeAsV4StoredReceiptRLP(want *Receipt) ([]byte, error) { return rlp.EncodeToBytes(stored) } -func encodeAsLegacyStoredReceiptRLP(want *Receipt) ([]byte, error) { - stored := &legacyStoredReceiptRLP{ +func encodeAsV3StoredReceiptRLP(want *Receipt) ([]byte, error) { + stored := &v3StoredReceiptRLP{ PostStateOrStatus: want.statusEncoding(), CumulativeGasUsed: want.CumulativeGasUsed, Bloom: want.Bloom, @@ -131,3 +134,136 @@ func encodeAsLegacyStoredReceiptRLP(want *Receipt) ([]byte, error) { } return rlp.EncodeToBytes(stored) } + +// Tests that receipt data can be correctly derived from the contextual infos +func TestSetReceiptsData(t *testing.T) { + // Create a few transactions to have receipts for + txs := Transactions{ + NewContractCreation(1, big.NewInt(1), 1, big.NewInt(1), nil), + NewTransaction(2, common.HexToAddress("0x2"), big.NewInt(2), 2, big.NewInt(2), nil), + } + // Create the corresponding receipts + receipts := Receipts{ + &Receipt{ + Status: ReceiptStatusFailed, + CumulativeGasUsed: 1, + Logs: []*Log{ + {Address: common.BytesToAddress([]byte{0x11})}, + {Address: common.BytesToAddress([]byte{0x01, 0x11})}, + }, + TxHash: txs[0].Hash(), + ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}), + GasUsed: 1, + }, + &Receipt{ + PostState: common.Hash{2}.Bytes(), + CumulativeGasUsed: 3, + Logs: []*Log{ + {Address: common.BytesToAddress([]byte{0x22})}, + {Address: common.BytesToAddress([]byte{0x02, 0x22})}, + }, + TxHash: txs[1].Hash(), + ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}), + GasUsed: 2, + }, + } + receipts[1].Bloom = CreateBloom(Receipts{receipts[1]}) + + // Clear all the computed fields and re-derive them + number := big.NewInt(1) + hash := common.BytesToHash([]byte{0x03, 0x14}) + + clearComputedFieldsOnReceipts(t, receipts) + if err := receipts.DeriveFields(params.TestChainConfig, hash, number.Uint64(), txs); err != nil { + t.Fatalf("DeriveFields(...) = %v, want ", err) + } + // Iterate over all the computed fields and check that they're correct + signer := MakeSigner(params.TestChainConfig, number) + + logIndex := uint(0) + for i := range receipts { + if receipts[i].TxHash != txs[i].Hash() { + t.Errorf("receipts[%d].TxHash = %s, want %s", i, receipts[i].TxHash.String(), txs[i].Hash().String()) + } + if receipts[i].BlockHash != hash { + t.Errorf("receipts[%d].BlockHash = %s, want %s", i, receipts[i].BlockHash.String(), hash.String()) + } + if receipts[i].BlockNumber.Cmp(number) != 0 { + t.Errorf("receipts[%c].BlockNumber = %s, want %s", i, receipts[i].BlockNumber.String(), number.String()) + } + if receipts[i].TransactionIndex != uint(i) { + t.Errorf("receipts[%d].TransactionIndex = %d, want %d", i, receipts[i].TransactionIndex, i) + } + if receipts[i].GasUsed != txs[i].Gas() { + t.Errorf("receipts[%d].GasUsed = %d, want %d", i, receipts[i].GasUsed, txs[i].Gas()) + } + if txs[i].To() != nil && receipts[i].ContractAddress != (common.Address{}) { + t.Errorf("receipts[%d].ContractAddress = %s, want %s", i, receipts[i].ContractAddress, (common.Address{}).String()) + } + from, _ := Sender(signer, txs[i]) + contractAddress := crypto.CreateAddress(from, txs[i].Nonce()) + if txs[i].To() == nil && receipts[i].ContractAddress != contractAddress { + t.Errorf("receipts[%d].ContractAddress = %s, want %s", i, receipts[i].ContractAddress.String(), contractAddress.String()) + } + for j := range receipts[i].Logs { + if receipts[i].Logs[j].BlockNumber != number.Uint64() { + t.Errorf("receipts[%d].Logs[%d].BlockNumber = %d, want %d", i, j, receipts[i].Logs[j].BlockNumber, number.Uint64()) + } + if receipts[i].Logs[j].BlockHash != hash { + t.Errorf("receipts[%d].Logs[%d].BlockHash = %s, want %s", i, j, receipts[i].Logs[j].BlockHash.String(), hash.String()) + } + if receipts[i].Logs[j].TxHash != txs[i].Hash() { + t.Errorf("receipts[%d].Logs[%d].TxHash = %s, want %s", i, j, receipts[i].Logs[j].TxHash.String(), txs[i].Hash().String()) + } + if receipts[i].Logs[j].TxHash != txs[i].Hash() { + t.Errorf("receipts[%d].Logs[%d].TxHash = %s, want %s", i, j, receipts[i].Logs[j].TxHash.String(), txs[i].Hash().String()) + } + if receipts[i].Logs[j].TxIndex != uint(i) { + t.Errorf("receipts[%d].Logs[%d].TransactionIndex = %d, want %d", i, j, receipts[i].Logs[j].TxIndex, i) + } + if receipts[i].Logs[j].Index != logIndex { + t.Errorf("receipts[%d].Logs[%d].Index = %d, want %d", i, j, receipts[i].Logs[j].Index, logIndex) + } + logIndex++ + } + } +} + +func clearComputedFieldsOnReceipts(t *testing.T, receipts Receipts) { + t.Helper() + + for _, receipt := range receipts { + clearComputedFieldsOnReceipt(t, receipt) + } +} + +func clearComputedFieldsOnReceipt(t *testing.T, receipt *Receipt) { + t.Helper() + + receipt.TxHash = common.Hash{} + receipt.BlockHash = common.Hash{} + receipt.BlockNumber = big.NewInt(math.MaxUint32) + receipt.TransactionIndex = math.MaxUint32 + receipt.ContractAddress = common.Address{} + receipt.GasUsed = 0 + + clearComputedFieldsOnLogs(t, receipt.Logs) +} + +func clearComputedFieldsOnLogs(t *testing.T, logs []*Log) { + t.Helper() + + for _, log := range logs { + clearComputedFieldsOnLog(t, log) + } +} + +func clearComputedFieldsOnLog(t *testing.T, log *Log) { + t.Helper() + + log.BlockNumber = math.MaxUint32 + log.BlockHash = common.Hash{} + log.TxHash = common.Hash{} + log.TxIndex = math.MaxUint32 + log.Index = math.MaxUint32 +} diff --git a/eth/downloader/fakepeer.go b/eth/downloader/fakepeer.go index 59832faca..3ec90bc9e 100644 --- a/eth/downloader/fakepeer.go +++ b/eth/downloader/fakepeer.go @@ -141,7 +141,7 @@ func (p *FakePeer) RequestBodies(hashes []common.Hash) error { func (p *FakePeer) RequestReceipts(hashes []common.Hash) error { var receipts [][]*types.Receipt for _, hash := range hashes { - receipts = append(receipts, rawdb.ReadReceipts(p.db, hash, *p.hc.GetBlockNumber(hash))) + receipts = append(receipts, rawdb.ReadRawReceipts(p.db, hash, *p.hc.GetBlockNumber(hash))) } p.dl.DeliverReceipts(p.id, receipts) return nil diff --git a/eth/filters/filter_system_test.go b/eth/filters/filter_system_test.go index e0c2a6a95..93cb43123 100644 --- a/eth/filters/filter_system_test.go +++ b/eth/filters/filter_system_test.go @@ -85,7 +85,7 @@ func (b *testBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*type func (b *testBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { if number := rawdb.ReadHeaderNumber(b.db, hash); number != nil { - return rawdb.ReadReceipts(b.db, hash, *number), nil + return rawdb.ReadReceipts(b.db, hash, *number, params.TestChainConfig), nil } return nil, nil } @@ -95,7 +95,7 @@ func (b *testBackend) GetLogs(ctx context.Context, hash common.Hash) ([][]*types if number == nil { return nil, nil } - receipts := rawdb.ReadReceipts(b.db, hash, *number) + receipts := rawdb.ReadReceipts(b.db, hash, *number, params.TestChainConfig) logs := make([][]*types.Log, len(receipts)) for i, receipt := range receipts { diff --git a/les/handler.go b/les/handler.go index 9c72c6b13..732cb6042 100644 --- a/les/handler.go +++ b/les/handler.go @@ -733,7 +733,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { // Retrieve the requested block's receipts, skipping if unknown to us var results types.Receipts if number := rawdb.ReadHeaderNumber(pm.chainDb, hash); number != nil { - results = rawdb.ReadReceipts(pm.chainDb, hash, *number) + results = rawdb.ReadRawReceipts(pm.chainDb, hash, *number) } if results == nil { if header := pm.blockchain.GetHeaderByHash(hash); header == nil || header.ReceiptHash != types.EmptyRootHash { diff --git a/les/odr_test.go b/les/odr_test.go index bc587a183..2cc28e384 100644 --- a/les/odr_test.go +++ b/les/odr_test.go @@ -60,7 +60,7 @@ func odrGetReceipts(ctx context.Context, db ethdb.Database, config *params.Chain var receipts types.Receipts if bc != nil { if number := rawdb.ReadHeaderNumber(db, bhash); number != nil { - receipts = rawdb.ReadReceipts(db, bhash, *number) + receipts = rawdb.ReadReceipts(db, bhash, *number, config) } } else { if number := rawdb.ReadHeaderNumber(db, bhash); number != nil { @@ -159,6 +159,9 @@ func testOdr(t *testing.T, protocol int, expFail uint64, fn odrTestFn) { client.pm.synchronise(client.rPeer) test := func(expFail uint64) { + // Mark this as a helper to put the failures at the correct lines + t.Helper() + for i := uint64(0); i <= server.pm.blockchain.CurrentHeader().Number.Uint64(); i++ { bhash := rawdb.ReadCanonicalHash(server.db, i) b1 := fn(light.NoOdr, server.db, server.pm.chainConfig, server.pm.blockchain.(*core.BlockChain), nil, bhash) @@ -170,10 +173,10 @@ func testOdr(t *testing.T, protocol int, expFail uint64, fn odrTestFn) { eq := bytes.Equal(b1, b2) exp := i < expFail if exp && !eq { - t.Errorf("odr mismatch") + t.Fatalf("odr mismatch: have %x, want %x", b2, b1) } if !exp && eq { - t.Errorf("unexpected odr match") + t.Fatalf("unexpected odr match") } } } @@ -182,6 +185,7 @@ func testOdr(t *testing.T, protocol int, expFail uint64, fn odrTestFn) { client.peers.Unregister(client.rPeer.id) time.Sleep(time.Millisecond * 10) // ensure that all peerSetNotify callbacks are executed test(expFail) + // expect all retrievals to pass client.peers.Register(client.rPeer) time.Sleep(time.Millisecond * 10) // ensure that all peerSetNotify callbacks are executed @@ -189,6 +193,7 @@ func testOdr(t *testing.T, protocol int, expFail uint64, fn odrTestFn) { client.rPeer.hasBlock = func(common.Hash, uint64, bool) bool { return true } client.peers.lock.Unlock() test(5) + // still expect all retrievals to pass, now data should be cached locally client.peers.Unregister(client.rPeer.id) time.Sleep(time.Millisecond * 10) // ensure that all peerSetNotify callbacks are executed diff --git a/light/odr_test.go b/light/odr_test.go index 57b377c41..912a0cbdd 100644 --- a/light/odr_test.go +++ b/light/odr_test.go @@ -122,7 +122,7 @@ func odrGetReceipts(ctx context.Context, db ethdb.Database, bc *core.BlockChain, if bc != nil { number := rawdb.ReadHeaderNumber(db, bhash) if number != nil { - receipts = rawdb.ReadReceipts(db, bhash, *number) + receipts = rawdb.ReadReceipts(db, bhash, *number, bc.Config()) } } else { number := rawdb.ReadHeaderNumber(db, bhash) diff --git a/light/odr_util.go b/light/odr_util.go index f84de6671..cce532f8e 100644 --- a/light/odr_util.go +++ b/light/odr_util.go @@ -135,7 +135,6 @@ func GetBlockReceipts(ctx context.Context, odr OdrBackend, hash common.Hash, num } receipts = r.Receipts } - // If the receipts are incomplete, fill the derived fields if len(receipts) > 0 && receipts[0].TxHash == (common.Hash{}) { block, err := GetBlock(ctx, odr, hash, number) @@ -145,12 +144,11 @@ func GetBlockReceipts(ctx context.Context, odr OdrBackend, hash common.Hash, num genesis := rawdb.ReadCanonicalHash(odr.Database(), 0) config := rawdb.ReadChainConfig(odr.Database(), genesis) - if err := rawdb.SetReceiptsData(config, block.Hash(), block.Number(), block.Body(), receipts); err != nil { + if err := receipts.DeriveFields(config, block.Hash(), block.NumberU64(), block.Transactions()); err != nil { return nil, err } rawdb.WriteReceipts(odr.Database(), hash, number, receipts) } - return receipts, nil } @@ -162,7 +160,6 @@ func GetBlockLogs(ctx context.Context, odr OdrBackend, hash common.Hash, number if err != nil { return nil, err } - // Return the logs without deriving any computed fields on the receipts logs := make([][]*types.Log, len(receipts)) for i, receipt := range receipts { From ce9a289fa48e0d2593c4aaa7e207c8a5dd3eaa8a Mon Sep 17 00:00:00 2001 From: gary rong Date: Mon, 15 Apr 2019 13:15:05 +0300 Subject: [PATCH 3/3] core/types: fix cummulative gas bug and legacy decoding tests --- core/types/receipt.go | 2 +- core/types/receipt_test.go | 56 ++++++++++++++++++++++++-------------- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/core/types/receipt.go b/core/types/receipt.go index 578daba3f..a96c7525e 100644 --- a/core/types/receipt.go +++ b/core/types/receipt.go @@ -265,7 +265,7 @@ func decodeV3StoredReceiptRLP(r *ReceiptForStorage, blob []byte) error { if err := (*Receipt)(r).setStatus(stored.PostStateOrStatus); err != nil { return err } - r.CumulativeGasUsed = stored.GasUsed + r.CumulativeGasUsed = stored.CumulativeGasUsed r.Bloom = stored.Bloom r.TxHash = stored.TxHash r.ContractAddress = stored.ContractAddress diff --git a/core/types/receipt_test.go b/core/types/receipt_test.go index 5af340b07..806b3dd2a 100644 --- a/core/types/receipt_test.go +++ b/core/types/receipt_test.go @@ -18,10 +18,9 @@ package types import ( "bytes" - "encoding/hex" - "encoding/json" "math" "math/big" + "reflect" "testing" "github.com/ethereum/go-ethereum/common" @@ -54,8 +53,16 @@ func TestLegacyReceiptDecoding(t *testing.T) { Status: ReceiptStatusFailed, CumulativeGasUsed: 1, Logs: []*Log{ - {Address: common.BytesToAddress([]byte{0x11})}, - {Address: common.BytesToAddress([]byte{0x01, 0x11})}, + { + Address: common.BytesToAddress([]byte{0x11}), + Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")}, + Data: []byte{0x01, 0x00, 0xff}, + }, + { + Address: common.BytesToAddress([]byte{0x01, 0x11}), + Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")}, + Data: []byte{0x01, 0x00, 0xff}, + }, }, TxHash: tx.Hash(), ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}), @@ -63,30 +70,39 @@ func TestLegacyReceiptDecoding(t *testing.T) { } receipt.Bloom = CreateBloom(Receipts{receipt}) - want, err := json.Marshal(receipt) - if err != nil { - t.Fatalf("Error encoding reference receipt to JSON: %v", err) - } - for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { enc, err := tc.encode(receipt) if err != nil { t.Fatalf("Error encoding receipt: %v", err) } - var dec ReceiptForStorage if err := rlp.DecodeBytes(enc, &dec); err != nil { t.Fatalf("Error decoding RLP receipt: %v", err) } - - have, err := rlp.EncodeToBytes((*Receipt)(receipt)) - if err != nil { - t.Fatalf("Error encoding receipt: %v", err) + // Check whether all consensus fields are correct. + if dec.Status != receipt.Status { + t.Fatalf("Receipt status mismatch, want %v, have %v", receipt.Status, dec.Status) } - - if !bytes.Equal(have, want) { - t.Fatalf("receipt mismatch: have %s, want %s", hex.EncodeToString(have), hex.EncodeToString(want)) + if dec.CumulativeGasUsed != receipt.CumulativeGasUsed { + t.Fatalf("Receipt CumulativeGasUsed mismatch, want %v, have %v", receipt.CumulativeGasUsed, dec.CumulativeGasUsed) + } + if dec.Bloom != receipt.Bloom { + t.Fatalf("Bloom data mismatch, want %v, have %v", receipt.Bloom, dec.Bloom) + } + if len(dec.Logs) != len(receipt.Logs) { + t.Fatalf("Receipt log number mismatch, want %v, have %v", len(receipt.Logs), len(dec.Logs)) + } + for i := 0; i < len(dec.Logs); i++ { + if dec.Logs[i].Address != receipt.Logs[i].Address { + t.Fatalf("Receipt log %d address mismatch, want %v, have %v", i, receipt.Logs[i].Address, dec.Logs[i].Address) + } + if !reflect.DeepEqual(dec.Logs[i].Topics, receipt.Logs[i].Topics) { + t.Fatalf("Receipt log %d topics mismatch, want %v, have %v", i, receipt.Logs[i].Topics, dec.Logs[i].Topics) + } + if !bytes.Equal(dec.Logs[i].Data, receipt.Logs[i].Data) { + t.Fatalf("Receipt log %d data mismatch, want %v, have %v", i, receipt.Logs[i].Data, dec.Logs[i].Data) + } } }) } @@ -136,7 +152,7 @@ func encodeAsV3StoredReceiptRLP(want *Receipt) ([]byte, error) { } // Tests that receipt data can be correctly derived from the contextual infos -func TestSetReceiptsData(t *testing.T) { +func TestDeriveFields(t *testing.T) { // Create a few transactions to have receipts for txs := Transactions{ NewContractCreation(1, big.NewInt(1), 1, big.NewInt(1), nil), @@ -167,8 +183,6 @@ func TestSetReceiptsData(t *testing.T) { GasUsed: 2, }, } - receipts[1].Bloom = CreateBloom(Receipts{receipts[1]}) - // Clear all the computed fields and re-derive them number := big.NewInt(1) hash := common.BytesToHash([]byte{0x03, 0x14}) @@ -198,7 +212,7 @@ func TestSetReceiptsData(t *testing.T) { t.Errorf("receipts[%d].GasUsed = %d, want %d", i, receipts[i].GasUsed, txs[i].Gas()) } if txs[i].To() != nil && receipts[i].ContractAddress != (common.Address{}) { - t.Errorf("receipts[%d].ContractAddress = %s, want %s", i, receipts[i].ContractAddress, (common.Address{}).String()) + t.Errorf("receipts[%d].ContractAddress = %s, want %s", i, receipts[i].ContractAddress.String(), (common.Address{}).String()) } from, _ := Sender(signer, txs[i]) contractAddress := crypto.CreateAddress(from, txs[i].Nonce())