Skip to content

Commit c79fc0f

Browse files
committed
config: support extra precompiles
1 parent cd8f5f4 commit c79fc0f

File tree

8 files changed

+26
-11
lines changed

8 files changed

+26
-11
lines changed

core/state_transition.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
308308

309309
// Set up the initial access list.
310310
if rules := st.evm.ChainConfig().Rules(st.evm.Context.BlockNumber); rules.IsBerlin {
311-
st.state.PrepareAccessList(msg.From(), msg.To(), vm.ActivePrecompiles(rules), msg.AccessList())
311+
st.state.PrepareAccessList(msg.From(), msg.To(), vm.ActivePrecompiles(rules, st.evm.ChainConfig()), msg.AccessList())
312312
}
313313
var (
314314
ret []byte

core/vm/contracts.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ func init() {
128128
}
129129
}
130130

131-
// ActivePrecompiles returns the precompiles enabled with the current configuration.
132-
func ActivePrecompiles(rules params.Rules) []common.Address {
131+
func ethereumPrecompiles(rules params.Rules) []common.Address {
133132
switch {
134133
case rules.IsBerlin:
135134
return PrecompiledAddressesBerlin
@@ -142,6 +141,15 @@ func ActivePrecompiles(rules params.Rules) []common.Address {
142141
}
143142
}
144143

144+
// ActivePrecompiles returns the precompiles enabled with the current configuration.
145+
func ActivePrecompiles(rules params.Rules, chainConfig *params.ChainConfig) []common.Address {
146+
res := ethereumPrecompiles(rules)
147+
for _, precompile := range chainConfig.ExtraPrecompiles {
148+
res = append(res, common.BigToAddress(&precompile))
149+
}
150+
return res
151+
}
152+
145153
// RunPrecompiledContract runs and evaluates the output of a precompiled contract.
146154
// It returns
147155
// - the returned bytes,

core/vm/evm.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ func (evm *EVM) precompile(addr common.Address) (PrecompiledContract, bool) {
5353
default:
5454
precompiles = PrecompiledContractsHomestead
5555
}
56+
for k, v := range evm.Config.ExtraPrecompiles {
57+
precompiles[k] = v
58+
}
5659
p, ok := precompiles[addr]
5760
return p, ok
5861
}

core/vm/interpreter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ type Config struct {
3636
JumpTable [256]*operation // EVM instruction table, automatically populated if unset
3737

3838
ExtraEips []int // Additional EIPS that are to be enabled
39+
40+
ExtraPrecompiles map[common.Address]PrecompiledContract
3941
}
4042

4143
// ScopeContext contains the things that are per-call, such as stack and memory,

core/vm/runtime/runtime.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) {
119119
sender = vm.AccountRef(cfg.Origin)
120120
)
121121
if rules := cfg.ChainConfig.Rules(vmenv.Context.BlockNumber); rules.IsBerlin {
122-
cfg.State.PrepareAccessList(cfg.Origin, &address, vm.ActivePrecompiles(rules), nil)
122+
cfg.State.PrepareAccessList(cfg.Origin, &address, vm.ActivePrecompiles(rules, cfg.ChainConfig), nil)
123123
}
124124
cfg.State.CreateAccount(address)
125125
// set the receiver's (the executing contract) code for execution.
@@ -151,7 +151,7 @@ func Create(input []byte, cfg *Config) ([]byte, common.Address, uint64, error) {
151151
sender = vm.AccountRef(cfg.Origin)
152152
)
153153
if rules := cfg.ChainConfig.Rules(vmenv.Context.BlockNumber); rules.IsBerlin {
154-
cfg.State.PrepareAccessList(cfg.Origin, nil, vm.ActivePrecompiles(rules), nil)
154+
cfg.State.PrepareAccessList(cfg.Origin, nil, vm.ActivePrecompiles(rules, cfg.ChainConfig), nil)
155155
}
156156
// Call the code with the given configuration.
157157
code, address, leftOverGas, err := vmenv.Create(
@@ -177,7 +177,7 @@ func Call(address common.Address, input []byte, cfg *Config) ([]byte, uint64, er
177177
statedb := cfg.State
178178

179179
if rules := cfg.ChainConfig.Rules(vmenv.Context.BlockNumber); rules.IsBerlin {
180-
statedb.PrepareAccessList(cfg.Origin, &address, vm.ActivePrecompiles(rules), nil)
180+
statedb.PrepareAccessList(cfg.Origin, &address, vm.ActivePrecompiles(rules, cfg.ChainConfig), nil)
181181
}
182182
// Call the code with the given configuration.
183183
ret, leftOverGas, err := vmenv.Call(

eth/tracers/tracer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ func (jst *Tracer) CaptureStart(env *vm.EVM, from common.Address, to common.Addr
687687
jst.dbWrapper.db = env.StateDB
688688
// Update list of precompiles based on current block
689689
rules := env.ChainConfig().Rules(env.Context.BlockNumber)
690-
jst.activePrecompiles = vm.ActivePrecompiles(rules)
690+
jst.activePrecompiles = vm.ActivePrecompiles(rules, env.ChainConfig())
691691

692692
// Compute intrinsic gas
693693
isHomestead := env.ChainConfig().IsHomestead(env.Context.BlockNumber)

internal/ethapi/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,7 @@ func AccessList(ctx context.Context, b Backend, blockNrOrHash rpc.BlockNumberOrH
14291429
to = crypto.CreateAddress(args.from(), uint64(*args.Nonce))
14301430
}
14311431
// Retrieve the precompiles since they don't need to be added to the access list
1432-
precompiles := vm.ActivePrecompiles(b.ChainConfig().Rules(header.Number))
1432+
precompiles := vm.ActivePrecompiles(b.ChainConfig().Rules(header.Number), b.ChainConfig())
14331433

14341434
// Create an initial tracer
14351435
prevTracer := vm.NewAccessListTracer(nil, args.from(), to, precompiles)

params/config.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,16 +225,16 @@ var (
225225
//
226226
// This configuration is intentionally not using keyed fields to force anyone
227227
// adding flags to the config to also have to set these fields.
228-
AllEthashProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil}
228+
AllEthashProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil, nil}
229229

230230
// AllCliqueProtocolChanges contains every protocol change (EIPs) introduced
231231
// and accepted by the Ethereum core developers into the Clique consensus.
232232
//
233233
// This configuration is intentionally not using keyed fields to force anyone
234234
// adding flags to the config to also have to set these fields.
235-
AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}}
235+
AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}, nil}
236236

237-
TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil}
237+
TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil, nil}
238238
TestRules = TestChainConfig.Rules(new(big.Int))
239239
)
240240

@@ -319,6 +319,8 @@ type ChainConfig struct {
319319
// Various consensus engines
320320
Ethash *EthashConfig `json:"ethash,omitempty"`
321321
Clique *CliqueConfig `json:"clique,omitempty"`
322+
323+
ExtraPrecompiles []big.Int `json:"extraPrecompiles,omitempty"`
322324
}
323325

324326
// EthashConfig is the consensus engine configs for proof-of-work based sealing.

0 commit comments

Comments
 (0)