Skip to content

Commit 4a31103

Browse files
committed
configure EVM on each runtime borrow
1 parent d202820 commit 4a31103

File tree

4 files changed

+94
-84
lines changed

4 files changed

+94
-84
lines changed

fvm/environment/runtime.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import (
88

99
type RuntimeParams struct {
1010
runtime.ReusableCadenceRuntimePool
11+
ConfigureCadenceRuntime func(
12+
reusableCadenceRuntime *runtime.ReusableCadenceRuntime,
13+
env Environment,
14+
)
1115
}
1216

1317
func DefaultRuntimeParams() RuntimeParams {
@@ -37,7 +41,16 @@ func (runtime *Runtime) SetEnvironment(env Environment) {
3741
}
3842

3943
func (runtime *Runtime) BorrowCadenceRuntime() *runtime.ReusableCadenceRuntime {
40-
return runtime.ReusableCadenceRuntimePool.Borrow(runtime.env)
44+
env := runtime.env
45+
46+
reusableCadenceRuntime := runtime.ReusableCadenceRuntimePool.Borrow(env)
47+
48+
configure := runtime.ConfigureCadenceRuntime
49+
if configure != nil {
50+
configure(reusableCadenceRuntime, env)
51+
}
52+
53+
return reusableCadenceRuntime
4154
}
4255

4356
func (runtime *Runtime) ReturnCadenceRuntime(

fvm/evm/evm.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func SetupEnvironment(
2626
chainID flow.ChainID,
2727
fvmEnv environment.Environment,
2828
runtimeEnv runtime.Environment,
29-
) error {
29+
) {
3030
sc := systemcontracts.SystemContractsForChain(chainID)
3131
randomBeaconAddress := sc.RandomBeaconHistory.Address
3232
flowTokenAddress := sc.FlowToken.Address
@@ -66,6 +66,4 @@ func SetupEnvironment(
6666
internalEVMFunctions,
6767
evmContractAddress,
6868
)
69-
70-
return nil
7169
}

fvm/script.go

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import (
66

77
"github.com/hashicorp/go-multierror"
88
"github.com/onflow/cadence/common"
9-
"github.com/onflow/cadence/runtime"
9+
cadenceRuntime "github.com/onflow/cadence/runtime"
1010

1111
"github.com/onflow/flow-go/fvm/environment"
1212
"github.com/onflow/flow-go/fvm/errors"
1313
"github.com/onflow/flow-go/fvm/evm"
14+
"github.com/onflow/flow-go/fvm/runtime"
1415
"github.com/onflow/flow-go/fvm/storage"
1516
"github.com/onflow/flow-go/fvm/storage/logical"
1617
"github.com/onflow/flow-go/model/flow"
@@ -126,15 +127,55 @@ func newScriptExecutor(
126127
// creating the executor
127128
scriptInfo := environment.NewScriptInfoParams(proc.Script, proc.Arguments)
128129
ctx.EnvironmentParams.SetScriptInfoParams(scriptInfo)
130+
131+
env := environment.NewScriptEnv(
132+
proc.RequestContext,
133+
ctx.TracerSpan,
134+
ctx.EnvironmentParams,
135+
txnState,
136+
)
137+
138+
if ctx.EVMEnabled {
139+
chainID := ctx.Chain.ChainID()
140+
141+
cadenceVMEnabled := ctx.CadenceVMEnabled
142+
143+
env.Runtime.ConfigureCadenceRuntime = func(
144+
reusableCadenceRuntime *runtime.ReusableCadenceRuntime,
145+
env environment.Environment,
146+
) {
147+
// Setup EVM environment in both script and transaction environments.
148+
// We also need to set up the transaction environment,
149+
// because it is used for nested contract invocations
150+
151+
var scriptRuntimeEnv, txRuntimeEnv cadenceRuntime.Environment
152+
if cadenceVMEnabled {
153+
scriptRuntimeEnv = reusableCadenceRuntime.VMScriptRuntimeEnv
154+
txRuntimeEnv = reusableCadenceRuntime.VMTxRuntimeEnv
155+
} else {
156+
scriptRuntimeEnv = reusableCadenceRuntime.ScriptRuntimeEnv
157+
txRuntimeEnv = reusableCadenceRuntime.TxRuntimeEnv
158+
}
159+
160+
evm.SetupEnvironment(
161+
chainID,
162+
env,
163+
scriptRuntimeEnv,
164+
)
165+
166+
evm.SetupEnvironment(
167+
chainID,
168+
env,
169+
txRuntimeEnv,
170+
)
171+
}
172+
}
173+
129174
return &scriptExecutor{
130175
ctx: ctx,
131176
proc: proc,
132177
txnState: txnState,
133-
env: environment.NewScriptEnv(
134-
proc.RequestContext,
135-
ctx.TracerSpan,
136-
ctx.EnvironmentParams,
137-
txnState),
178+
env: env,
138179
}
139180
}
140181

@@ -200,36 +241,13 @@ func (executor *scriptExecutor) executeScript() error {
200241
rt := executor.env.BorrowCadenceRuntime()
201242
defer executor.env.ReturnCadenceRuntime(rt)
202243

203-
chainID := executor.ctx.Chain.ChainID()
204-
205-
cadenceVMEnabled := executor.ctx.CadenceVMEnabled
206-
207-
if executor.ctx.EVMEnabled {
208-
209-
var scriptRuntimeEnv runtime.Environment
210-
if cadenceVMEnabled {
211-
scriptRuntimeEnv = rt.VMScriptRuntimeEnv
212-
} else {
213-
scriptRuntimeEnv = rt.ScriptRuntimeEnv
214-
}
215-
216-
err := evm.SetupEnvironment(
217-
chainID,
218-
executor.env,
219-
scriptRuntimeEnv,
220-
)
221-
if err != nil {
222-
return err
223-
}
224-
}
225-
226244
value, err := rt.ExecuteScript(
227-
runtime.Script{
245+
cadenceRuntime.Script{
228246
Source: executor.proc.Script,
229247
Arguments: executor.proc.Arguments,
230248
},
231249
common.ScriptLocation(executor.proc.ID),
232-
cadenceVMEnabled,
250+
executor.ctx.CadenceVMEnabled,
233251
)
234252
populateErr := executor.output.PopulateEnvironmentValues(executor.env)
235253
if err != nil {

fvm/transactionInvoker.go

Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import (
55
"strconv"
66

77
"github.com/onflow/cadence/common"
8-
"github.com/onflow/cadence/runtime"
8+
cadenceRuntime "github.com/onflow/cadence/runtime"
99
"github.com/rs/zerolog"
1010
"go.opentelemetry.io/otel/attribute"
1111
otelTrace "go.opentelemetry.io/otel/trace"
1212

1313
"github.com/onflow/flow-go/fvm/environment"
1414
"github.com/onflow/flow-go/fvm/errors"
1515
"github.com/onflow/flow-go/fvm/evm"
16-
reusableRuntime "github.com/onflow/flow-go/fvm/runtime"
16+
"github.com/onflow/flow-go/fvm/runtime"
1717
"github.com/onflow/flow-go/fvm/storage"
1818
"github.com/onflow/flow-go/fvm/storage/derived"
1919
"github.com/onflow/flow-go/fvm/storage/snapshot"
@@ -70,8 +70,8 @@ type transactionExecutor struct {
7070
// writes to any of those registers
7171
executionStateRead *snapshot.ExecutionSnapshot
7272

73-
cadenceRuntime *reusableRuntime.ReusableCadenceRuntime
74-
txnBodyExecutor runtime.Executor
73+
cadenceRuntime *runtime.ReusableCadenceRuntime
74+
txnBodyExecutor cadenceRuntime.Executor
7575

7676
output ProcedureOutput
7777
}
@@ -93,6 +93,30 @@ func newTransactionExecutor(
9393
ctx.EnvironmentParams,
9494
txnState)
9595

96+
if ctx.EVMEnabled {
97+
chainID := ctx.Chain.ChainID()
98+
99+
cadenceVMEnabled := ctx.CadenceVMEnabled
100+
101+
env.Runtime.ConfigureCadenceRuntime = func(
102+
reusableCadenceRuntime *runtime.ReusableCadenceRuntime,
103+
env environment.Environment,
104+
) {
105+
var txRuntimeEnv cadenceRuntime.Environment
106+
if cadenceVMEnabled {
107+
txRuntimeEnv = reusableCadenceRuntime.VMTxRuntimeEnv
108+
} else {
109+
txRuntimeEnv = reusableCadenceRuntime.TxRuntimeEnv
110+
}
111+
112+
evm.SetupEnvironment(
113+
chainID,
114+
env,
115+
txRuntimeEnv,
116+
)
117+
}
118+
}
119+
96120
return &transactionExecutor{
97121
TransactionExecutorParams: ctx.TransactionExecutorParams,
98122
TransactionVerifier: TransactionVerifier{
@@ -186,30 +210,8 @@ func (executor *transactionExecutor) preprocess() error {
186210
// infrequently modified and are expensive to compute. For now this includes
187211
// reading meter parameter overrides and parsing programs.
188212
func (executor *transactionExecutor) preprocessTransactionBody() error {
189-
chainID := executor.ctx.Chain.ChainID()
190213

191214
// setup EVM
192-
cadenceVMEnabled := executor.ctx.CadenceVMEnabled
193-
194-
if executor.ctx.EVMEnabled {
195-
196-
var txRuntimeEnv runtime.Environment
197-
if cadenceVMEnabled {
198-
txRuntimeEnv = executor.cadenceRuntime.VMTxRuntimeEnv
199-
} else {
200-
txRuntimeEnv = executor.cadenceRuntime.TxRuntimeEnv
201-
}
202-
203-
err := evm.SetupEnvironment(
204-
chainID,
205-
executor.env,
206-
txRuntimeEnv,
207-
)
208-
if err != nil {
209-
return err
210-
}
211-
}
212-
213215
// get meter parameters
214216
executionParameters, executionStateRead, err := getExecutionParameters(
215217
executor.env.Logger(),
@@ -238,12 +240,12 @@ func (executor *transactionExecutor) preprocessTransactionBody() error {
238240
executor.nestedTxnId = txnId
239241

240242
executor.txnBodyExecutor = executor.cadenceRuntime.NewTransactionExecutor(
241-
runtime.Script{
243+
cadenceRuntime.Script{
242244
Source: executor.proc.Transaction.Script,
243245
Arguments: executor.proc.Transaction.Arguments,
244246
},
245247
common.TransactionLocation(executor.proc.ID),
246-
cadenceVMEnabled,
248+
executor.ctx.CadenceVMEnabled,
247249
)
248250

249251
// This initializes various cadence variables and parses the programs used
@@ -267,27 +269,6 @@ func (executor *transactionExecutor) execute() error {
267269
}
268270

269271
func (executor *transactionExecutor) ExecuteTransactionBody() error {
270-
chainID := executor.ctx.Chain.ChainID()
271-
272-
// setup EVM
273-
if executor.ctx.EVMEnabled {
274-
275-
var txRuntimeEnv runtime.Environment
276-
if executor.ctx.CadenceVMEnabled {
277-
txRuntimeEnv = executor.cadenceRuntime.VMTxRuntimeEnv
278-
} else {
279-
txRuntimeEnv = executor.cadenceRuntime.TxRuntimeEnv
280-
}
281-
282-
err := evm.SetupEnvironment(
283-
chainID,
284-
executor.env,
285-
txRuntimeEnv,
286-
)
287-
if err != nil {
288-
return err
289-
}
290-
}
291272

292273
var invalidator derived.TransactionInvalidator
293274
if !executor.errs.CollectedError() {

0 commit comments

Comments
 (0)