Skip to content

Commit 686b288

Browse files
all: removed blockhash from statedb (#23126)
This PR removes the blockhash from the statedb
1 parent e7c8693 commit 686b288

File tree

11 files changed

+41
-41
lines changed

11 files changed

+41
-41
lines changed

cmd/evm/internal/t8ntool/execution.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
152152
}
153153
vmConfig.Tracer = tracer
154154
vmConfig.Debug = (tracer != nil)
155-
statedb.Prepare(tx.Hash(), blockHash, txIndex)
155+
statedb.Prepare(tx.Hash(), txIndex)
156156
txContext := core.NewEVMTxContext(msg)
157157
snapshot := statedb.Snapshot()
158158
evm := vm.NewEVM(vmContext, txContext, statedb, chainConfig, vmConfig)
@@ -197,7 +197,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
197197
}
198198

199199
// Set the receipt logs and create the bloom filter.
200-
receipt.Logs = statedb.GetLogs(tx.Hash())
200+
receipt.Logs = statedb.GetLogs(tx.Hash(), blockHash)
201201
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
202202
// These three are non-consensus fields:
203203
//receipt.BlockHash

core/chain_makers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func (b *BlockGen) AddTxWithChain(bc *BlockChain, tx *types.Transaction) {
102102
if b.gasPool == nil {
103103
b.SetCoinbase(common.Address{})
104104
}
105-
b.statedb.Prepare(tx.Hash(), common.Hash{}, len(b.txs))
105+
b.statedb.Prepare(tx.Hash(), len(b.txs))
106106
receipt, err := ApplyTransaction(b.config, bc, &b.header.Coinbase, b.gasPool, b.statedb, b.header, tx, &b.header.GasUsed, vm.Config{})
107107
if err != nil {
108108
panic(err)

core/state/statedb.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ type StateDB struct {
8989
// The refund counter, also used by state transitioning.
9090
refund uint64
9191

92-
thash, bhash common.Hash
93-
txIndex int
94-
logs map[common.Hash][]*types.Log
95-
logSize uint
92+
thash common.Hash
93+
txIndex int
94+
logs map[common.Hash][]*types.Log
95+
logSize uint
9696

9797
preimages map[common.Hash][]byte
9898

@@ -186,15 +186,18 @@ func (s *StateDB) AddLog(log *types.Log) {
186186
s.journal.append(addLogChange{txhash: s.thash})
187187

188188
log.TxHash = s.thash
189-
log.BlockHash = s.bhash
190189
log.TxIndex = uint(s.txIndex)
191190
log.Index = s.logSize
192191
s.logs[s.thash] = append(s.logs[s.thash], log)
193192
s.logSize++
194193
}
195194

196-
func (s *StateDB) GetLogs(hash common.Hash) []*types.Log {
197-
return s.logs[hash]
195+
func (s *StateDB) GetLogs(hash common.Hash, blockHash common.Hash) []*types.Log {
196+
logs := s.logs[hash]
197+
for _, l := range logs {
198+
l.BlockHash = blockHash
199+
}
200+
return logs
198201
}
199202

200203
func (s *StateDB) Logs() []*types.Log {
@@ -272,11 +275,6 @@ func (s *StateDB) TxIndex() int {
272275
return s.txIndex
273276
}
274277

275-
// BlockHash returns the current block hash set by Prepare.
276-
func (s *StateDB) BlockHash() common.Hash {
277-
return s.bhash
278-
}
279-
280278
func (s *StateDB) GetCode(addr common.Address) []byte {
281279
stateObject := s.getStateObject(addr)
282280
if stateObject != nil {
@@ -882,9 +880,8 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
882880

883881
// Prepare sets the current transaction hash and index and block hash which is
884882
// used when the EVM emits new state logs.
885-
func (s *StateDB) Prepare(thash, bhash common.Hash, ti int) {
883+
func (s *StateDB) Prepare(thash common.Hash, ti int) {
886884
s.thash = thash
887-
s.bhash = bhash
888885
s.txIndex = ti
889886
s.accessList = newAccessList()
890887
}

core/state/statedb_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,9 +463,9 @@ func (test *snapshotTest) checkEqual(state, checkstate *StateDB) error {
463463
return fmt.Errorf("got GetRefund() == %d, want GetRefund() == %d",
464464
state.GetRefund(), checkstate.GetRefund())
465465
}
466-
if !reflect.DeepEqual(state.GetLogs(common.Hash{}), checkstate.GetLogs(common.Hash{})) {
466+
if !reflect.DeepEqual(state.GetLogs(common.Hash{}, common.Hash{}), checkstate.GetLogs(common.Hash{}, common.Hash{})) {
467467
return fmt.Errorf("got GetLogs(common.Hash{}) == %v, want GetLogs(common.Hash{}) == %v",
468-
state.GetLogs(common.Hash{}), checkstate.GetLogs(common.Hash{}))
468+
state.GetLogs(common.Hash{}, common.Hash{}), checkstate.GetLogs(common.Hash{}, common.Hash{}))
469469
}
470470
return nil
471471
}

core/state_prefetcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (p *statePrefetcher) Prefetch(block *types.Block, statedb *state.StateDB, c
6767
if err != nil {
6868
return // Also invalid block, bail out
6969
}
70-
statedb.Prepare(tx.Hash(), block.Hash(), i)
70+
statedb.Prepare(tx.Hash(), i)
7171
if err := precacheTransaction(msg, p.config, gaspool, statedb, header, evm); err != nil {
7272
return // Ugh, something went horribly wrong, bail out
7373
}

core/state_processor.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package core
1818

1919
import (
2020
"fmt"
21+
"math/big"
2122

2223
"github.com/ethereum/go-ethereum/common"
2324
"github.com/ethereum/go-ethereum/consensus"
@@ -57,11 +58,13 @@ func NewStateProcessor(config *params.ChainConfig, bc *BlockChain, engine consen
5758
// transactions failed to execute due to insufficient gas it will return an error.
5859
func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, error) {
5960
var (
60-
receipts types.Receipts
61-
usedGas = new(uint64)
62-
header = block.Header()
63-
allLogs []*types.Log
64-
gp = new(GasPool).AddGas(block.GasLimit())
61+
receipts types.Receipts
62+
usedGas = new(uint64)
63+
header = block.Header()
64+
blockHash = block.Hash()
65+
blockNumber = block.Number()
66+
allLogs []*types.Log
67+
gp = new(GasPool).AddGas(block.GasLimit())
6568
)
6669
// Mutate the block and state according to any hard-fork specs
6770
if p.config.DAOForkSupport && p.config.DAOForkBlock != nil && p.config.DAOForkBlock.Cmp(block.Number()) == 0 {
@@ -75,8 +78,8 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
7578
if err != nil {
7679
return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err)
7780
}
78-
statedb.Prepare(tx.Hash(), block.Hash(), i)
79-
receipt, err := applyTransaction(msg, p.config, p.bc, nil, gp, statedb, header, tx, usedGas, vmenv)
81+
statedb.Prepare(tx.Hash(), i)
82+
receipt, err := applyTransaction(msg, p.config, p.bc, nil, gp, statedb, blockNumber, blockHash, tx, usedGas, vmenv)
8083
if err != nil {
8184
return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err)
8285
}
@@ -89,7 +92,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
8992
return receipts, allLogs, *usedGas, nil
9093
}
9194

92-
func applyTransaction(msg types.Message, config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, evm *vm.EVM) (*types.Receipt, error) {
95+
func applyTransaction(msg types.Message, config *params.ChainConfig, bc ChainContext, 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) {
9396
// Create a new context to be used in the EVM environment.
9497
txContext := NewEVMTxContext(msg)
9598
evm.Reset(txContext, statedb)
@@ -102,10 +105,10 @@ func applyTransaction(msg types.Message, config *params.ChainConfig, bc ChainCon
102105

103106
// Update the state with pending changes.
104107
var root []byte
105-
if config.IsByzantium(header.Number) {
108+
if config.IsByzantium(blockNumber) {
106109
statedb.Finalise(true)
107110
} else {
108-
root = statedb.IntermediateRoot(config.IsEIP158(header.Number)).Bytes()
111+
root = statedb.IntermediateRoot(config.IsEIP158(blockNumber)).Bytes()
109112
}
110113
*usedGas += result.UsedGas
111114

@@ -126,10 +129,10 @@ func applyTransaction(msg types.Message, config *params.ChainConfig, bc ChainCon
126129
}
127130

128131
// Set the receipt logs and create the bloom filter.
129-
receipt.Logs = statedb.GetLogs(tx.Hash())
132+
receipt.Logs = statedb.GetLogs(tx.Hash(), blockHash)
130133
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
131-
receipt.BlockHash = statedb.BlockHash()
132-
receipt.BlockNumber = header.Number
134+
receipt.BlockHash = blockHash
135+
receipt.BlockNumber = blockNumber
133136
receipt.TransactionIndex = uint(statedb.TxIndex())
134137
return receipt, err
135138
}
@@ -146,5 +149,5 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *commo
146149
// Create a new context to be used in the EVM environment
147150
blockContext := NewEVMBlockContext(header, bc, author)
148151
vmenv := vm.NewEVM(blockContext, vm.TxContext{}, statedb, config, cfg)
149-
return applyTransaction(msg, config, bc, author, gp, statedb, header, tx, usedGas, vmenv)
152+
return applyTransaction(msg, config, bc, author, gp, statedb, header.Number, header.Hash(), tx, usedGas, vmenv)
150153
}

eth/catalyst/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func (api *consensusAPI) AssembleBlock(params assembleBlockParams) (*executableD
178178
from, _ := types.Sender(signer, tx)
179179

180180
// Execute the transaction
181-
env.state.Prepare(tx.Hash(), common.Hash{}, env.tcount)
181+
env.state.Prepare(tx.Hash(), env.tcount)
182182
err = env.commitTransaction(tx, coinbase)
183183
switch err {
184184
case core.ErrGasLimitReached:

eth/state_accessor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func (eth *Ethereum) stateAtTransaction(block *types.Block, txIndex int, reexec
170170
}
171171
// Not yet the searched for transaction, execute on top of the current state
172172
vmenv := vm.NewEVM(context, txContext, statedb, eth.blockchain.Config(), vm.Config{})
173-
statedb.Prepare(tx.Hash(), block.Hash(), idx)
173+
statedb.Prepare(tx.Hash(), idx)
174174
if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.Gas())); err != nil {
175175
return nil, vm.BlockContext{}, nil, fmt.Errorf("transaction %#x failed: %v", tx.Hash(), err)
176176
}

eth/tracers/api.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac
539539

540540
// Generate the next state snapshot fast without tracing
541541
msg, _ := tx.AsMessage(signer, block.BaseFee())
542-
statedb.Prepare(tx.Hash(), block.Hash(), i)
542+
statedb.Prepare(tx.Hash(), i)
543543
vmenv := vm.NewEVM(blockCtx, core.NewEVMTxContext(msg), statedb, api.backend.ChainConfig(), vm.Config{})
544544
if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas())); err != nil {
545545
failed = err
@@ -653,7 +653,7 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block
653653
}
654654
// Execute the transaction and flush any traces to disk
655655
vmenv := vm.NewEVM(vmctx, txContext, statedb, chainConfig, vmConf)
656-
statedb.Prepare(tx.Hash(), block.Hash(), i)
656+
statedb.Prepare(tx.Hash(), i)
657657
_, err = core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.Gas()))
658658
if writer != nil {
659659
writer.Flush()
@@ -816,7 +816,7 @@ func (api *API) traceTx(ctx context.Context, message core.Message, txctx *Contex
816816
vmenv := vm.NewEVM(vmctx, txContext, statedb, api.backend.ChainConfig(), vm.Config{Debug: true, Tracer: tracer, NoBaseFee: true})
817817

818818
// Call Prepare to clear out the statedb access list
819-
statedb.Prepare(txctx.TxHash, txctx.BlockHash, txctx.TxIndex)
819+
statedb.Prepare(txctx.TxHash, txctx.TxIndex)
820820

821821
result, err := core.ApplyMessage(vmenv, message, new(core.GasPool).AddGas(message.Gas()))
822822
if err != nil {

les/state_accessor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (leth *LightEthereum) stateAtTransaction(ctx context.Context, block *types.
5858
msg, _ := tx.AsMessage(signer, block.BaseFee())
5959
txContext := core.NewEVMTxContext(msg)
6060
context := core.NewEVMBlockContext(block.Header(), leth.blockchain, nil)
61-
statedb.Prepare(tx.Hash(), block.Hash(), idx)
61+
statedb.Prepare(tx.Hash(), idx)
6262
if idx == txIndex {
6363
return msg, context, statedb, nil
6464
}

0 commit comments

Comments
 (0)