Skip to content

Commit

Permalink
statedb: precompile check before onNewAccount
Browse files Browse the repository at this point in the history
  • Loading branch information
s1na committed Nov 7, 2023
1 parent 023ade6 commit ee58cc7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func initGenesis(ctx *cli.Context) error {
triedb := utils.MakeTrieDatabase(ctx, chaindb, ctx.Bool(utils.CachePreimagesFlag.Name), false)
defer triedb.Close()

_, hash, err := core.SetupGenesisBlockWithOverride(chaindb, triedb, genesis, &overrides)
_, hash, err := core.SetupGenesisBlockWithOverride(chaindb, triedb, genesis, &overrides, nil)
if err != nil {
utils.Fatalf("Failed to write genesis block: %v", err)
}
Expand Down
20 changes: 18 additions & 2 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ type StateDB struct {
// Preimages occurred seen by VM in the scope of block.
preimages map[common.Hash][]byte

// Enabled precompile contracts
precompiles map[common.Address]struct{}

// Per-transaction access list
accessList *accessList

Expand Down Expand Up @@ -179,6 +182,7 @@ func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error)
stateObjectsDestruct: make(map[common.Address]*types.StateAccount),
logs: make(map[common.Hash][]*types.Log),
preimages: make(map[common.Hash][]byte),
precompiles: make(map[common.Address]struct{}),
journal: newJournal(),
accessList: newAccessList(),
transientStorage: newTransientStorage(),
Expand Down Expand Up @@ -656,9 +660,12 @@ func (s *StateDB) createObject(addr common.Address) (newobj, prev *stateObject)
newobj = newObject(s, addr, nil)
if prev == nil {
s.journal.append(createObjectChange{account: &addr})
// TODO: add isPrecompile check
if s.logger != nil {
s.logger.OnNewAccount(addr)
// Precompiled contracts are touched during a call.
// Make sure we avoid emitting a new account event for them.
if _, ok := s.precompiles[addr]; !ok {
s.logger.OnNewAccount(addr)
}
}
} else {
// The original account should be marked as destructed and all cached
Expand Down Expand Up @@ -1372,6 +1379,15 @@ func (s *StateDB) Prepare(rules params.Rules, sender, coinbase common.Address, d
s.transientStorage = newTransientStorage()
}

// PrepareBlock prepares the statedb for execution of a block. It tracks
// the addresses of enabled precompiles for debugging purposes.
func (s *StateDB) PrepareBlock(precompiles []common.Address) {
s.precompiles = make(map[common.Address]struct{})
for _, addr := range precompiles {
s.precompiles[addr] = struct{}{}
}
}

// AddAddressToAccessList adds the given address to the access list
func (s *StateDB) AddAddressToAccessList(addr common.Address) {
if s.accessList.AddAddress(addr) {
Expand Down
2 changes: 2 additions & 0 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
var (
context = NewEVMBlockContext(header, p.bc, nil)
vmenv = vm.NewEVM(context, vm.TxContext{}, statedb, p.config, cfg)
rules = vmenv.ChainConfig().Rules(context.BlockNumber, context.Random != nil, context.Time)
signer = types.MakeSigner(p.config, header.Number, header.Time)
)
if beaconRoot := block.BeaconRoot(); beaconRoot != nil {
ProcessBeaconBlockRoot(*beaconRoot, vmenv, statedb)
}
statedb.PrepareBlock(vm.ActivePrecompiles(rules))
// Iterate over and process the individual transactions
for i, tx := range block.Transactions() {
msg, err := TransactionToMessage(tx, signer, header.BaseFee)
Expand Down

0 comments on commit ee58cc7

Please sign in to comment.