Skip to content

Commit 9303e27

Browse files
Merge #4048
4048: update vm.Run calls to vm.RunV2 (part 1 of many) r=pattyshack a=pattyshack Co-authored-by: Patrick Lee <[email protected]>
2 parents 959c2ba + 444368a commit 9303e27

File tree

5 files changed

+55
-46
lines changed

5 files changed

+55
-46
lines changed

cmd/execution_builder.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ import (
4949
"github.com/onflow/flow-go/engine/execution/rpc"
5050
"github.com/onflow/flow-go/engine/execution/state"
5151
"github.com/onflow/flow-go/engine/execution/state/bootstrap"
52-
"github.com/onflow/flow-go/engine/execution/state/delta"
5352
"github.com/onflow/flow-go/fvm"
5453
fvmState "github.com/onflow/flow-go/fvm/state"
5554
"github.com/onflow/flow-go/fvm/systemcontracts"
@@ -1086,18 +1085,18 @@ func getContractEpochCounter(
10861085
script := fvm.Script(scriptCode)
10871086

10881087
// execute the script
1089-
err = vm.Run(vmCtx, script, delta.NewDeltaView(snapshot))
1088+
_, output, err := vm.RunV2(vmCtx, script, snapshot)
10901089
if err != nil {
10911090
return 0, fmt.Errorf("could not read epoch counter, internal error while executing script: %w", err)
10921091
}
1093-
if script.Err != nil {
1094-
return 0, fmt.Errorf("could not read epoch counter, script error: %w", script.Err)
1092+
if output.Err != nil {
1093+
return 0, fmt.Errorf("could not read epoch counter, script error: %w", output.Err)
10951094
}
1096-
if script.Value == nil {
1095+
if output.Value == nil {
10971096
return 0, fmt.Errorf("could not read epoch counter, script returned no value")
10981097
}
10991098

1100-
epochCounter := script.Value.ToGoValue().(uint64)
1099+
epochCounter := output.Value.ToGoValue().(uint64)
11011100
return epochCounter, nil
11021101
}
11031102

cmd/util/ledger/reporters/account_reporter.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@ func (r *AccountReporter) Report(payload []ledger.Payload, commit ledger.State)
125125
}
126126

127127
type balanceProcessor struct {
128-
vm fvm.VM
129-
ctx fvm.Context
130-
view state.View
131-
env environment.Environment
132-
balanceScript []byte
133-
momentsScript []byte
128+
vm fvm.VM
129+
ctx fvm.Context
130+
storageSnapshot state.StorageSnapshot
131+
env environment.Environment
132+
balanceScript []byte
133+
momentsScript []byte
134134

135135
accounts environment.Accounts
136136

@@ -174,11 +174,11 @@ func NewBalanceReporter(
174174
txnState)
175175

176176
return &balanceProcessor{
177-
vm: vm,
178-
ctx: ctx,
179-
view: view,
180-
accounts: accounts,
181-
env: env,
177+
vm: vm,
178+
ctx: ctx,
179+
storageSnapshot: snapshot,
180+
accounts: accounts,
181+
env: env,
182182
}
183183
}
184184

@@ -343,15 +343,15 @@ func (c *balanceProcessor) balance(address flow.Address) (uint64, bool, error) {
343343
jsoncdc.MustEncode(cadence.NewAddress(address)),
344344
)
345345

346-
err := c.vm.Run(c.ctx, script, c.view)
346+
_, output, err := c.vm.RunV2(c.ctx, script, c.storageSnapshot)
347347
if err != nil {
348348
return 0, false, err
349349
}
350350

351351
var balance uint64
352352
var hasVault bool
353-
if script.Err == nil && script.Value != nil {
354-
balance = script.Value.ToGoValue().(uint64)
353+
if output.Err == nil && output.Value != nil {
354+
balance = output.Value.ToGoValue().(uint64)
355355
hasVault = true
356356
} else {
357357
hasVault = false
@@ -364,14 +364,14 @@ func (c *balanceProcessor) fusdBalance(address flow.Address) (uint64, error) {
364364
jsoncdc.MustEncode(cadence.NewAddress(address)),
365365
)
366366

367-
err := c.vm.Run(c.ctx, script, c.view)
367+
_, output, err := c.vm.RunV2(c.ctx, script, c.storageSnapshot)
368368
if err != nil {
369369
return 0, err
370370
}
371371

372372
var balance uint64
373-
if script.Err == nil && script.Value != nil {
374-
balance = script.Value.ToGoValue().(uint64)
373+
if output.Err == nil && output.Value != nil {
374+
balance = output.Value.ToGoValue().(uint64)
375375
}
376376
return balance, nil
377377
}
@@ -381,14 +381,14 @@ func (c *balanceProcessor) moments(address flow.Address) (int, error) {
381381
jsoncdc.MustEncode(cadence.NewAddress(address)),
382382
)
383383

384-
err := c.vm.Run(c.ctx, script, c.view)
384+
_, output, err := c.vm.RunV2(c.ctx, script, c.storageSnapshot)
385385
if err != nil {
386386
return 0, err
387387
}
388388

389389
var m int
390-
if script.Err == nil && script.Value != nil {
391-
m = script.Value.(cadence.Int).Int()
390+
if output.Err == nil && output.Value != nil {
391+
m = output.Value.(cadence.Int).Int()
392392
}
393393
return m, nil
394394
}

cmd/util/ledger/reporters/fungible_token_tracker_test.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ func TestFungibleTokenTracker(t *testing.T) {
6565
fvm.WithInitialTokenSupply(unittest.GenesisTokenSupply),
6666
}
6767

68-
err := vm.Run(ctx, fvm.Bootstrap(unittest.ServiceAccountPublicKey, bootstrapOptions...), view)
68+
snapshot, _, err := vm.RunV2(ctx, fvm.Bootstrap(unittest.ServiceAccountPublicKey, bootstrapOptions...), view)
69+
require.NoError(t, err)
70+
71+
err = view.Merge(snapshot)
6972
require.NoError(t, err)
7073

7174
// deploy wrapper resource
@@ -101,9 +104,12 @@ func TestFungibleTokenTracker(t *testing.T) {
101104
AddAuthorizer(chain.ServiceAddress())
102105

103106
tx := fvm.Transaction(txBody, derivedBlockData.NextTxIndexForTestingOnly())
104-
err = vm.Run(ctx, tx, view)
107+
snapshot, output, err := vm.RunV2(ctx, tx, view)
108+
require.NoError(t, err)
109+
require.NoError(t, output.Err)
110+
111+
err = view.Merge(snapshot)
105112
require.NoError(t, err)
106-
require.NoError(t, tx.Err)
107113

108114
wrapTokenScript := []byte(fmt.Sprintf(`
109115
import FungibleToken from 0x%s
@@ -127,9 +133,12 @@ func TestFungibleTokenTracker(t *testing.T) {
127133
AddAuthorizer(chain.ServiceAddress())
128134

129135
tx = fvm.Transaction(txBody, derivedBlockData.NextTxIndexForTestingOnly())
130-
err = vm.Run(ctx, tx, view)
136+
snapshot, output, err = vm.RunV2(ctx, tx, view)
137+
require.NoError(t, err)
138+
require.NoError(t, output.Err)
139+
140+
err = view.Merge(snapshot)
131141
require.NoError(t, err)
132-
require.NoError(t, tx.Err)
133142

134143
dir := t.TempDir()
135144
log := zerolog.Nop()

engine/execution/state/bootstrap/bootstrap.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ func (b *Bootstrapper) BootstrapLedger(
3636
chain flow.Chain,
3737
opts ...fvm.BootstrapProcedureOption,
3838
) (flow.StateCommitment, error) {
39-
view := delta.NewDeltaView(
40-
state.NewLedgerStorageSnapshot(
41-
ledger,
42-
flow.StateCommitment(ledger.InitialState())))
39+
storageSnapshot := state.NewLedgerStorageSnapshot(
40+
ledger,
41+
flow.StateCommitment(ledger.InitialState()))
4342

4443
vm := fvm.NewVirtualMachine()
4544

@@ -54,12 +53,15 @@ func (b *Bootstrapper) BootstrapLedger(
5453
opts...,
5554
)
5655

57-
err := vm.Run(ctx, bootstrap, view)
56+
executionSnapshot, _, err := vm.RunV2(ctx, bootstrap, storageSnapshot)
5857
if err != nil {
5958
return flow.DummyStateCommitment, err
6059
}
6160

62-
newStateCommitment, _, err := state.CommitDelta(ledger, view.Delta(), flow.StateCommitment(ledger.InitialState()))
61+
newStateCommitment, _, err := state.CommitDelta(
62+
ledger,
63+
executionSnapshot,
64+
flow.StateCommitment(ledger.InitialState()))
6365
if err != nil {
6466
return flow.DummyStateCommitment, err
6567
}

utils/debug/remoteDebugger.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"github.com/onflow/cadence"
55
"github.com/rs/zerolog"
66

7-
"github.com/onflow/flow-go/engine/execution/state/delta"
87
"github.com/onflow/flow-go/fvm"
98
"github.com/onflow/flow-go/model/flow"
109
)
@@ -51,11 +50,11 @@ func (d *RemoteDebugger) RunTransaction(
5150
d.ctx,
5251
fvm.WithBlockHeader(d.ctx.BlockHeader))
5352
tx := fvm.Transaction(txBody, 0)
54-
err := d.vm.Run(blockCtx, tx, delta.NewDeltaView(snapshot))
53+
_, output, err := d.vm.RunV2(blockCtx, tx, snapshot)
5554
if err != nil {
5655
return nil, err
5756
}
58-
return tx.Err, nil
57+
return output.Err, nil
5958
}
6059

6160
// RunTransaction runs the transaction and tries to collect the registers at
@@ -80,15 +79,15 @@ func (d *RemoteDebugger) RunTransactionAtBlockID(
8079
snapshot.Cache = newFileRegisterCache(regCachePath)
8180
}
8281
tx := fvm.Transaction(txBody, 0)
83-
err := d.vm.Run(blockCtx, tx, delta.NewDeltaView(snapshot))
82+
_, output, err := d.vm.RunV2(blockCtx, tx, snapshot)
8483
if err != nil {
8584
return nil, err
8685
}
8786
err = snapshot.Cache.Persist()
8887
if err != nil {
8988
return nil, err
9089
}
91-
return tx.Err, nil
90+
return output.Err, nil
9291
}
9392

9493
func (d *RemoteDebugger) RunScript(
@@ -106,11 +105,11 @@ func (d *RemoteDebugger) RunScript(
106105
d.ctx,
107106
fvm.WithBlockHeader(d.ctx.BlockHeader))
108107
script := fvm.Script(code).WithArguments(arguments...)
109-
err := d.vm.Run(scriptCtx, script, delta.NewDeltaView(snapshot))
108+
_, output, err := d.vm.RunV2(scriptCtx, script, snapshot)
110109
if err != nil {
111110
return nil, nil, err
112111
}
113-
return script.Value, script.Err, nil
112+
return output.Value, output.Err, nil
114113
}
115114

116115
func (d *RemoteDebugger) RunScriptAtBlockID(
@@ -129,9 +128,9 @@ func (d *RemoteDebugger) RunScriptAtBlockID(
129128
d.ctx,
130129
fvm.WithBlockHeader(d.ctx.BlockHeader))
131130
script := fvm.Script(code).WithArguments(arguments...)
132-
err := d.vm.Run(scriptCtx, script, delta.NewDeltaView(snapshot))
131+
_, output, err := d.vm.RunV2(scriptCtx, script, snapshot)
133132
if err != nil {
134133
return nil, nil, err
135134
}
136-
return script.Value, script.Err, nil
135+
return output.Value, output.Err, nil
137136
}

0 commit comments

Comments
 (0)