From 5417e5c0c084a538f0ed24838c74f125d1f4a985 Mon Sep 17 00:00:00 2001 From: ucwong Date: Wed, 14 Dec 2022 05:54:21 +0800 Subject: [PATCH] add block number in logs --- core/state/statedb.go | 3 ++- core/state/statedb_test.go | 4 ++-- core/state_processor.go | 8 ++++---- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index 0c298fb019..f20848ced7 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -197,9 +197,10 @@ func (s *StateDB) AddLog(log *types.Log) { s.logSize++ } -func (s *StateDB) GetLogs(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 s.logs[hash] diff --git a/core/state/statedb_test.go b/core/state/statedb_test.go index d693d73025..c9f231fa0a 100644 --- a/core/state/statedb_test.go +++ b/core/state/statedb_test.go @@ -420,9 +420,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 79279261cb..477d73a63e 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -84,7 +84,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg return nil, nil, 0, err } statedb.Prepare(tx.Hash(), i) - receipt, _, err := applyTransaction(msg, p.config, nil, gp, qp, statedb, header, blockNumber, blockHash, tx, usedGas, vmenv) + receipt, _, err := applyTransaction(msg, p.config, gp, qp, statedb, header, 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) } @@ -106,7 +106,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg // and uses the input parameters for its environment. It returns the receipt // for the transaction, gas used and an error if the transaction failed, // indicating the block was invalid. -func applyTransaction(msg types.Message, config *params.ChainConfig, author *common.Address, gp *GasPool, qp *QuotaPool, statedb *state.StateDB, header *types.Header, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, cvm *vm.CVM) (*types.Receipt, uint64, error) { +func applyTransaction(msg types.Message, config *params.ChainConfig, gp *GasPool, qp *QuotaPool, statedb *state.StateDB, header *types.Header, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, cvm *vm.CVM) (*types.Receipt, uint64, error) { // Create a new context to be used in the CVM environment txContext := NewCVMTxContext(msg) // Update the evm with the new transaction context. @@ -146,7 +146,7 @@ func applyTransaction(msg types.Message, config *params.ChainConfig, author *com receipt.ContractAddress = crypto.CreateAddress(cvm.TxContext.Origin, tx.Nonce()) } // Set the receipt logs and create a bloom for filtering - 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 @@ -169,5 +169,5 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *commo // Create a new context to be used in the CVM environment blockContext := NewCVMBlockContext(header, bc, author) vmenv := vm.NewCVM(blockContext, vm.TxContext{}, statedb, config, cfg) - return applyTransaction(msg, config, author, gp, qp, statedb, header, header.Number, header.Hash(), tx, usedGas, vmenv) + return applyTransaction(msg, config, gp, qp, statedb, header, header.Number, header.Hash(), tx, usedGas, vmenv) }