Skip to content

Commit

Permalink
doc: document changes in code
Browse files Browse the repository at this point in the history
Signed-off-by: Francisco de Borja Aranda Castillejo <[email protected]>
  • Loading branch information
fbac committed Jul 22, 2024
1 parent 7b0656a commit a5a3c14
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
5 changes: 4 additions & 1 deletion x/evm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
// CustomContractFn defines a custom precompiled contract generator with ctx, rules and returns a precompiled contract.
type CustomContractFn func(sdk.Context, params.Rules) vm.PrecompiledContract

// EventConverter type represents a function that parses a list of EventAttributes to a list of Ethereum Log objects.
type EventConverter = func([]abci.EventAttribute) []*ethtypes.Log

// Keeper grants access to the EVM module state and implements the go-ethereum StateDB interface.
Expand Down Expand Up @@ -78,7 +79,9 @@ type Keeper struct {
hooks types.EvmHooks

// Legacy subspace
ss paramstypes.Subspace
ss paramstypes.Subspace

// customContractFns is the list of precompiled stateful contract functions.
customContractFns []CustomContractFn

ck consensusparamkeeper.Keeper
Expand Down
16 changes: 16 additions & 0 deletions x/evm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,41 @@ func (k *Keeper) NewEVM(
if tracer == nil {
tracer = k.Tracer(ctx, msg, cfg.ChainConfig)
}

vmConfig := k.VMConfig(ctx, msg, cfg, tracer)
rules := cfg.ChainConfig.Rules(big.NewInt(ctx.BlockHeight()), cfg.ChainConfig.MergeNetsplitBlock != nil)
contracts := make(map[common.Address]vm.PrecompiledContract)
active := make([]common.Address, 0)

// Creates the list of **default** precompiled contracts (not stateful) for this set of rules.
// contracts hold the list of all contracts, while active holds the list of all active addresses.
// i.e create the list of contracts for Berlin.
for addr, c := range vm.DefaultPrecompiles(rules) {
contracts[addr] = c
active = append(active, addr)
}

Check failure

Code scanning / gosec

the value in the range statement should be _ unless copying a map: want: for key := range m Error

expected exactly 1 statement (either append, delete, or copying to another map) in a range with a map, got 2

// Add the custom stateful precompiled contracts and their addresses to the list.
// Then, mark them as active.
for _, fn := range k.customContractFns {
c := fn(ctx, rules)
addr := c.Address()
contracts[addr] = c
active = append(active, addr)
}

// Sort the active slice in address ascending order.
sort.SliceStable(active, func(i, j int) bool {
return bytes.Compare(active[i].Bytes(), active[j].Bytes()) < 0
})

evm := vm.NewEVM(blockCtx, txCtx, stateDB, cfg.ChainConfig, vmConfig)

// Set the precompiled contracts:
// - contracts contains all the contracts.
// - active contains the addresses of the active contracts.
evm.WithPrecompiles(contracts, active)

return evm
}

Expand Down
5 changes: 3 additions & 2 deletions x/evm/statedb/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (

const StateDBContextKey = "statedb"

// EventConverter represents a function able to parse a sdk.Event into an ethtypes.Log.
type EventConverter = func(sdk.Event) (*ethtypes.Log, error)

// revision is the identifier of a version of state.
Expand All @@ -43,7 +44,7 @@ type revision struct {
journalIndex int
}

var _ vm.StateDB = &StateDB{}
var _ vm.StateDB = (*StateDB)(nil)

// StateDB structs within the ethereum protocol are used to store anything
// within the merkle trie. StateDBs take care of caching and storing
Expand Down Expand Up @@ -85,7 +86,7 @@ type StateDB struct {
//nolint
evmDenom string
//nolint
err error
err error
}

// New creates a new state from a given trie.
Expand Down

0 comments on commit a5a3c14

Please sign in to comment.