Skip to content

Commit a8ce878

Browse files
committed
Update computation/testutils to use fvm.RunV2 / snapshot tree
1 parent 7accaea commit a8ce878

File tree

6 files changed

+715
-413
lines changed

6 files changed

+715
-413
lines changed

engine/execution/computation/manager_benchmark_test.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"github.com/onflow/flow-go/fvm"
2222
"github.com/onflow/flow-go/fvm/derived"
2323
reusableRuntime "github.com/onflow/flow-go/fvm/runtime"
24-
"github.com/onflow/flow-go/fvm/state"
24+
"github.com/onflow/flow-go/fvm/storage"
2525
"github.com/onflow/flow-go/model/flow"
2626
"github.com/onflow/flow-go/module/executiondatasync/execution_data"
2727
exedataprovider "github.com/onflow/flow-go/module/executiondatasync/provider"
@@ -44,13 +44,21 @@ type testAccounts struct {
4444
seq uint64
4545
}
4646

47-
func createAccounts(b *testing.B, vm fvm.VM, ledger state.View, num int) *testAccounts {
47+
func createAccounts(
48+
b *testing.B,
49+
vm fvm.VM,
50+
snapshotTree storage.SnapshotTree,
51+
num int,
52+
) (
53+
storage.SnapshotTree,
54+
*testAccounts,
55+
) {
4856
privateKeys, err := testutil.GenerateAccountPrivateKeys(num)
4957
require.NoError(b, err)
5058

51-
addresses, err := testutil.CreateAccounts(
59+
snapshotTree, addresses, err := testutil.CreateAccounts(
5260
vm,
53-
ledger,
61+
snapshotTree,
5462
privateKeys,
5563
chain)
5664
require.NoError(b, err)
@@ -64,16 +72,16 @@ func createAccounts(b *testing.B, vm fvm.VM, ledger state.View, num int) *testAc
6472
privateKey: privateKeys[i],
6573
}
6674
}
67-
return accs
75+
return snapshotTree, accs
6876
}
6977

7078
func mustFundAccounts(
7179
b *testing.B,
7280
vm fvm.VM,
73-
ledger state.View,
81+
snapshotTree storage.SnapshotTree,
7482
execCtx fvm.Context,
7583
accs *testAccounts,
76-
) {
84+
) storage.SnapshotTree {
7785
derivedBlockData := derived.NewEmptyDerivedBlockData()
7886
execCtx = fvm.NewContextFromParent(
7987
execCtx,
@@ -89,10 +97,13 @@ func mustFundAccounts(
8997
tx := fvm.Transaction(
9098
transferTx,
9199
derivedBlockData.NextTxIndexForTestingOnly())
92-
err = vm.Run(execCtx, tx, ledger)
100+
executionSnapshot, output, err := vm.RunV2(execCtx, tx, snapshotTree)
93101
require.NoError(b, err)
94-
require.NoError(b, tx.Err)
102+
require.NoError(b, output.Err)
103+
snapshotTree = snapshotTree.Append(executionSnapshot)
95104
}
105+
106+
return snapshotTree
96107
}
97108

98109
func BenchmarkComputeBlock(b *testing.B) {
@@ -115,16 +126,16 @@ func BenchmarkComputeBlock(b *testing.B) {
115126
runtime.Config{},
116127
)),
117128
)
118-
ledger := testutil.RootBootstrappedLedger(
129+
snapshotTree := testutil.RootBootstrappedLedger(
119130
vm,
120131
execCtx,
121132
fvm.WithAccountCreationFee(fvm.DefaultAccountCreationFee),
122133
fvm.WithMinimumStorageReservation(fvm.DefaultMinimumStorageReservation),
123134
fvm.WithTransactionFee(fvm.DefaultTransactionFees),
124135
fvm.WithStorageMBPerFLOW(fvm.DefaultStorageMBPerFLOW),
125136
)
126-
accs := createAccounts(b, vm, ledger, 1000)
127-
mustFundAccounts(b, vm, ledger, execCtx, accs)
137+
snapshotTree, accs := createAccounts(b, vm, snapshotTree, 1000)
138+
snapshotTree = mustFundAccounts(b, vm, snapshotTree, execCtx, accs)
128139

129140
me := new(module.Local)
130141
me.On("NodeID").Return(flow.ZeroID)
@@ -192,13 +203,12 @@ func BenchmarkComputeBlock(b *testing.B) {
192203
context.Background(),
193204
unittest.IdentifierFixture(),
194205
executableBlock,
195-
ledger)
206+
snapshotTree)
196207
elapsed += time.Since(start)
197208
b.StopTimer()
198209

199210
for _, snapshot := range res.StateSnapshots {
200-
err := ledger.Merge(snapshot)
201-
require.NoError(b, err)
211+
snapshotTree = snapshotTree.Append(snapshot)
202212
}
203213

204214
require.NoError(b, err)

engine/execution/computation/manager_test.go

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ func TestComputeBlockWithStorage(t *testing.T) {
5858
privateKeys, err := testutil.GenerateAccountPrivateKeys(2)
5959
require.NoError(t, err)
6060

61-
ledger := testutil.RootBootstrappedLedger(vm, execCtx)
62-
accounts, err := testutil.CreateAccounts(vm, ledger, privateKeys, chain)
61+
snapshotTree, accounts, err := testutil.CreateAccounts(
62+
vm,
63+
testutil.RootBootstrappedLedger(vm, execCtx),
64+
privateKeys,
65+
chain)
6366
require.NoError(t, err)
6467

6568
tx1 := testutil.DeployCounterContractTransaction(accounts[0], chain)
@@ -150,17 +153,21 @@ func TestComputeBlockWithStorage(t *testing.T) {
150153
derivedChainData: derivedChainData,
151154
}
152155

153-
view := delta.NewDeltaView(ledger)
154-
blockView := view.NewChild()
155-
156156
returnedComputationResult, err := engine.ComputeBlock(
157157
context.Background(),
158158
unittest.IdentifierFixture(),
159159
executableBlock,
160-
blockView)
160+
snapshotTree)
161161
require.NoError(t, err)
162162

163-
require.NotEmpty(t, blockView.(*delta.View).Delta())
163+
hasUpdates := false
164+
for _, snapshot := range returnedComputationResult.StateSnapshots {
165+
if len(snapshot.WriteSet) > 0 {
166+
hasUpdates = true
167+
break
168+
}
169+
}
170+
require.True(t, hasUpdates)
164171
require.Len(t, returnedComputationResult.StateSnapshots, 1+1) // 1 coll + 1 system chunk
165172
assert.NotEmpty(t, returnedComputationResult.StateSnapshots[0].UpdatedRegisters())
166173
}
@@ -703,8 +710,11 @@ func Test_EventEncodingFailsOnlyTxAndCarriesOn(t *testing.T) {
703710

704711
privateKeys, err := testutil.GenerateAccountPrivateKeys(1)
705712
require.NoError(t, err)
706-
ledger := testutil.RootBootstrappedLedger(vm, execCtx)
707-
accounts, err := testutil.CreateAccounts(vm, ledger, privateKeys, chain)
713+
snapshotTree, accounts, err := testutil.CreateAccounts(
714+
vm,
715+
testutil.RootBootstrappedLedger(vm, execCtx),
716+
privateKeys,
717+
chain)
708718
require.NoError(t, err)
709719

710720
// setup transactions
@@ -789,16 +799,13 @@ func Test_EventEncodingFailsOnlyTxAndCarriesOn(t *testing.T) {
789799
derivedChainData: derivedChainData,
790800
}
791801

792-
view := delta.NewDeltaView(ledger)
793-
blockView := view.NewChild()
794-
795802
eventEncoder.enabled = true
796803

797804
returnedComputationResult, err := engine.ComputeBlock(
798805
context.Background(),
799806
unittest.IdentifierFixture(),
800807
executableBlock,
801-
blockView)
808+
snapshotTree)
802809
require.NoError(t, err)
803810

804811
require.Len(t, returnedComputationResult.Events, 2) // 1 collection + 1 system chunk
@@ -858,14 +865,18 @@ func TestScriptStorageMutationsDiscarded(t *testing.T) {
858865
},
859866
)
860867
vm := manager.vm
861-
view := testutil.RootBootstrappedLedger(vm, ctx)
862868

863869
// Create an account private key.
864870
privateKeys, err := testutil.GenerateAccountPrivateKeys(1)
865871
require.NoError(t, err)
866872

867-
// Bootstrap a ledger, creating accounts with the provided private keys and the root account.
868-
accounts, err := testutil.CreateAccounts(vm, view, privateKeys, chain)
873+
// Bootstrap a ledger, creating accounts with the provided private keys
874+
// and the root account.
875+
snapshotTree, accounts, err := testutil.CreateAccounts(
876+
vm,
877+
testutil.RootBootstrappedLedger(vm, ctx),
878+
privateKeys,
879+
chain)
869880
require.NoError(t, err)
870881
account := accounts[0]
871882
address := cadence.NewAddress(account)
@@ -884,13 +895,13 @@ func TestScriptStorageMutationsDiscarded(t *testing.T) {
884895
script,
885896
[][]byte{jsoncdc.MustEncode(address)},
886897
header,
887-
view)
898+
snapshotTree)
888899

889900
require.NoError(t, err)
890901

891902
env := environment.NewScriptEnvironmentFromStorageSnapshot(
892903
ctx.EnvironmentParams,
893-
view)
904+
snapshotTree)
894905

895906
rt := env.BorrowCadenceRuntime()
896907
defer env.ReturnCadenceRuntime(rt)

0 commit comments

Comments
 (0)