From 064314921d2c3decbfb0ebf4bc817cdd63e00be6 Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Mon, 12 Dec 2022 11:35:46 +0800 Subject: [PATCH 1/4] core, cmd: fill blockNumber in logs --- cmd/evm/internal/t8ntool/execution.go | 2 +- core/state/statedb.go | 3 ++- core/state/statedb_test.go | 4 ++-- core/state_processor.go | 8 ++++---- core/types/log.go | 4 ++-- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/cmd/evm/internal/t8ntool/execution.go b/cmd/evm/internal/t8ntool/execution.go index a05dbedea700..08d829d7db70 100644 --- a/cmd/evm/internal/t8ntool/execution.go +++ b/cmd/evm/internal/t8ntool/execution.go @@ -218,7 +218,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig, } // Set the receipt logs and create the bloom filter. - receipt.Logs = statedb.GetLogs(tx.Hash(), blockHash) + receipt.Logs = statedb.GetLogs(tx.Hash(), vmContext.BlockNumber.Uint64(), blockHash) receipt.Bloom = types.CreateBloom(types.Receipts{receipt}) // These three are non-consensus fields: //receipt.BlockHash diff --git a/core/state/statedb.go b/core/state/statedb.go index b85c94850b40..e316ebf18e73 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -206,9 +206,10 @@ func (s *StateDB) AddLog(log *types.Log) { s.logSize++ } -func (s *StateDB) GetLogs(hash common.Hash, blockHash common.Hash) []*types.Log { +func (s *StateDB) GetLogs(hash common.Hash, blockNumber uint64, blockHash common.Hash) []*types.Log { logs := s.logs[hash] for _, l := range logs { + l.BlockNumber = blockNumber l.BlockHash = blockHash } return logs diff --git a/core/state/statedb_test.go b/core/state/statedb_test.go index 5e134043d243..a39c83d2d1a1 100644 --- a/core/state/statedb_test.go +++ b/core/state/statedb_test.go @@ -473,9 +473,9 @@ func (test *snapshotTest) checkEqual(state, checkstate *StateDB) error { return fmt.Errorf("got GetRefund() == %d, want GetRefund() == %d", state.GetRefund(), checkstate.GetRefund()) } - if !reflect.DeepEqual(state.GetLogs(common.Hash{}, common.Hash{}), checkstate.GetLogs(common.Hash{}, common.Hash{})) { + if !reflect.DeepEqual(state.GetLogs(common.Hash{}, 0, common.Hash{}), checkstate.GetLogs(common.Hash{}, 0, common.Hash{})) { return fmt.Errorf("got GetLogs(common.Hash{}) == %v, want GetLogs(common.Hash{}) == %v", - state.GetLogs(common.Hash{}, common.Hash{}), checkstate.GetLogs(common.Hash{}, common.Hash{})) + state.GetLogs(common.Hash{}, 0, common.Hash{}), checkstate.GetLogs(common.Hash{}, 0, common.Hash{})) } return nil } diff --git a/core/state_processor.go b/core/state_processor.go index db17481804ab..da886781e322 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -79,7 +79,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err) } statedb.SetTxContext(tx.Hash(), i) - receipt, err := applyTransaction(msg, p.config, nil, gp, statedb, blockNumber, blockHash, tx, usedGas, vmenv) + receipt, err := applyTransaction(msg, p.config, gp, statedb, blockNumber, blockHash, tx, usedGas, vmenv) if err != nil { return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err) } @@ -92,7 +92,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg return receipts, allLogs, *usedGas, nil } -func applyTransaction(msg types.Message, config *params.ChainConfig, author *common.Address, gp *GasPool, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM) (*types.Receipt, error) { +func applyTransaction(msg types.Message, config *params.ChainConfig, gp *GasPool, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM) (*types.Receipt, error) { // Create a new context to be used in the EVM environment. txContext := NewEVMTxContext(msg) evm.Reset(txContext, statedb) @@ -129,7 +129,7 @@ func applyTransaction(msg types.Message, config *params.ChainConfig, author *com } // Set the receipt logs and create the bloom filter. - receipt.Logs = statedb.GetLogs(tx.Hash(), blockHash) + receipt.Logs = statedb.GetLogs(tx.Hash(), blockNumber.Uint64(), blockHash) receipt.Bloom = types.CreateBloom(types.Receipts{receipt}) receipt.BlockHash = blockHash receipt.BlockNumber = blockNumber @@ -149,5 +149,5 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *commo // Create a new context to be used in the EVM environment blockContext := NewEVMBlockContext(header, bc, author) vmenv := vm.NewEVM(blockContext, vm.TxContext{}, statedb, config, cfg) - return applyTransaction(msg, config, author, gp, statedb, header.Number, header.Hash(), tx, usedGas, vmenv) + return applyTransaction(msg, config, gp, statedb, header.Number, header.Hash(), tx, usedGas, vmenv) } diff --git a/core/types/log.go b/core/types/log.go index e48919136889..178bf8bb4e44 100644 --- a/core/types/log.go +++ b/core/types/log.go @@ -41,12 +41,12 @@ type Log struct { // but not secured by consensus. // block in which the transaction was included BlockNumber uint64 `json:"blockNumber"` + // hash of the block in which the transaction was included + BlockHash common.Hash `json:"blockHash"` // hash of the transaction TxHash common.Hash `json:"transactionHash" gencodec:"required"` // index of the transaction in the block TxIndex uint `json:"transactionIndex"` - // hash of the block in which the transaction was included - BlockHash common.Hash `json:"blockHash"` // index of the log in the block Index uint `json:"logIndex"` From 9eb1b7efee454d99199d070abb4932f4ea9d32e5 Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Mon, 12 Dec 2022 16:19:28 +0800 Subject: [PATCH 2/4] Update core/state/statedb.go Co-authored-by: Martin Holst Swende --- core/state/statedb.go | 1 + 1 file changed, 1 insertion(+) diff --git a/core/state/statedb.go b/core/state/statedb.go index e316ebf18e73..a7567dbe761b 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -206,6 +206,7 @@ func (s *StateDB) AddLog(log *types.Log) { s.logSize++ } +// GetLogs returns the logs matching the specified transaction hash, and annotates them with the given blocknumber/blockhash func (s *StateDB) GetLogs(hash common.Hash, blockNumber uint64, blockHash common.Hash) []*types.Log { logs := s.logs[hash] for _, l := range logs { From f8ddceb6224096199f59c7d0880411bd3303b437 Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Mon, 12 Dec 2022 19:41:46 +0800 Subject: [PATCH 3/4] core/types: revert --- core/types/log.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/types/log.go b/core/types/log.go index 178bf8bb4e44..e48919136889 100644 --- a/core/types/log.go +++ b/core/types/log.go @@ -41,12 +41,12 @@ type Log struct { // but not secured by consensus. // block in which the transaction was included BlockNumber uint64 `json:"blockNumber"` - // hash of the block in which the transaction was included - BlockHash common.Hash `json:"blockHash"` // hash of the transaction TxHash common.Hash `json:"transactionHash" gencodec:"required"` // index of the transaction in the block TxIndex uint `json:"transactionIndex"` + // hash of the block in which the transaction was included + BlockHash common.Hash `json:"blockHash"` // index of the log in the block Index uint `json:"logIndex"` From 5baf17473e3e0e35af42aed31731acaa729c5043 Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Tue, 13 Dec 2022 16:58:51 +0800 Subject: [PATCH 4/4] core/state: improve comments --- core/state/statedb.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index a7567dbe761b..91eb07a22800 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -206,7 +206,8 @@ func (s *StateDB) AddLog(log *types.Log) { s.logSize++ } -// GetLogs returns the logs matching the specified transaction hash, and annotates them with the given blocknumber/blockhash +// GetLogs returns the logs matching the specified transaction hash, and annotates +// them with the given blockNumber and blockHash. func (s *StateDB) GetLogs(hash common.Hash, blockNumber uint64, blockHash common.Hash) []*types.Log { logs := s.logs[hash] for _, l := range logs {