Skip to content

Commit 2d82a5a

Browse files
Merge #4109 #4119
4109: Update computation/testutils to use fvm.RunV2 / snapshot tree r=pattyshack a=pattyshack 4119: Remove unnecessary delta view usage in tests r=pattyshack a=pattyshack Co-authored-by: Patrick Lee <[email protected]>
3 parents 31fff71 + a8ce878 + b6394e3 commit 2d82a5a

File tree

10 files changed

+750
-468
lines changed

10 files changed

+750
-468
lines changed

engine/execution/computation/committer/committer_test.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/stretchr/testify/require"
88

99
"github.com/onflow/flow-go/engine/execution/computation/committer"
10-
"github.com/onflow/flow-go/engine/execution/state/delta"
10+
"github.com/onflow/flow-go/fvm/state"
1111
led "github.com/onflow/flow-go/ledger"
1212
ledgermock "github.com/onflow/flow-go/ledger/mock"
1313
"github.com/onflow/flow-go/model/flow"
@@ -33,16 +33,12 @@ func TestLedgerViewCommitter(t *testing.T) {
3333
Return(expectedProof, nil).
3434
Once()
3535

36-
view := delta.NewDeltaView(nil)
37-
38-
err := view.Set(
39-
flow.NewRegisterID("owner", "key"),
40-
[]byte{1},
41-
)
42-
require.NoError(t, err)
43-
4436
newState, proof, _, err := com.CommitView(
45-
view.Finalize(),
37+
&state.ExecutionSnapshot{
38+
WriteSet: map[flow.RegisterID]flow.RegisterValue{
39+
flow.NewRegisterID("owner", "key"): []byte{1},
40+
},
41+
},
4642
utils.StateCommitmentFixture())
4743
require.NoError(t, err)
4844
require.Equal(t, flow.StateCommitment(expectedStateCommitment), newState)

engine/execution/computation/computer/computer_test.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
fvmmock "github.com/onflow/flow-go/fvm/mock"
3636
reusableRuntime "github.com/onflow/flow-go/fvm/runtime"
3737
"github.com/onflow/flow-go/fvm/state"
38+
"github.com/onflow/flow-go/fvm/storage"
3839
"github.com/onflow/flow-go/fvm/storage/testutils"
3940
"github.com/onflow/flow-go/fvm/systemcontracts"
4041
"github.com/onflow/flow-go/ledger"
@@ -362,15 +363,20 @@ func TestBlockExecutor_ExecuteBlock(t *testing.T) {
362363

363364
opts := append(baseOpts, contextOptions...)
364365
ctx := fvm.NewContext(opts...)
365-
view := delta.NewDeltaView(nil)
366+
snapshotTree := storage.NewSnapshotTree(nil)
366367

367368
baseBootstrapOpts := []fvm.BootstrapProcedureOption{
368369
fvm.WithInitialTokenSupply(unittest.GenesisTokenSupply),
369370
}
370371
bootstrapOpts := append(baseBootstrapOpts, bootstrapOptions...)
371-
err := vm.Run(ctx, fvm.Bootstrap(unittest.ServiceAccountPublicKey, bootstrapOpts...), view)
372+
executionSnapshot, _, err := vm.RunV2(
373+
ctx,
374+
fvm.Bootstrap(unittest.ServiceAccountPublicKey, bootstrapOpts...),
375+
snapshotTree)
372376
require.NoError(t, err)
373377

378+
snapshotTree = snapshotTree.Append(executionSnapshot)
379+
374380
comm := new(computermock.ViewCommitter)
375381

376382
bservice := requesterunit.MockBlobService(blockstore.NewBlockstore(dssync.MutexWrap(datastore.NewMapDatastore())))
@@ -407,7 +413,7 @@ func TestBlockExecutor_ExecuteBlock(t *testing.T) {
407413
context.Background(),
408414
unittest.IdentifierFixture(),
409415
block,
410-
view,
416+
snapshotTree,
411417
derivedBlockData)
412418
assert.NoError(t, err)
413419
assert.Len(t, result.StateSnapshots, 1)
@@ -718,18 +724,15 @@ func TestBlockExecutor_ExecuteBlock(t *testing.T) {
718724
const transactionCount = 2
719725
block := generateBlock(collectionCount, transactionCount, rag)
720726

721-
view := delta.NewDeltaView(nil)
722-
723-
err = view.Set(
724-
flow.AccountStatusRegisterID(flow.BytesToAddress(address.Bytes())),
725-
environment.NewAccountStatus().ToBytes())
726-
require.NoError(t, err)
727+
key := flow.AccountStatusRegisterID(
728+
flow.BytesToAddress(address.Bytes()))
729+
value := environment.NewAccountStatus().ToBytes()
727730

728731
result, err := exe.ExecuteBlock(
729732
context.Background(),
730733
unittest.IdentifierFixture(),
731734
block,
732-
view,
735+
state.MapStorageSnapshot{key: value},
733736
derived.NewEmptyDerivedBlockData())
734737
assert.NoError(t, err)
735738
assert.Len(t, result.StateSnapshots, collectionCount+1) // +1 system chunk
@@ -818,18 +821,15 @@ func TestBlockExecutor_ExecuteBlock(t *testing.T) {
818821

819822
block := generateBlock(collectionCount, transactionCount, rag)
820823

821-
view := delta.NewDeltaView(nil)
822-
823-
err = view.Set(
824-
flow.AccountStatusRegisterID(flow.BytesToAddress(address.Bytes())),
825-
environment.NewAccountStatus().ToBytes())
826-
require.NoError(t, err)
824+
key := flow.AccountStatusRegisterID(
825+
flow.BytesToAddress(address.Bytes()))
826+
value := environment.NewAccountStatus().ToBytes()
827827

828828
result, err := exe.ExecuteBlock(
829829
context.Background(),
830830
unittest.IdentifierFixture(),
831831
block,
832-
view,
832+
state.MapStorageSnapshot{key: value},
833833
derived.NewEmptyDerivedBlockData())
834834
require.NoError(t, err)
835835
assert.Len(t, result.StateSnapshots, collectionCount+1) // +1 system chunk
@@ -1154,13 +1154,11 @@ func Test_ExecutingSystemCollection(t *testing.T) {
11541154
// create empty block, it will have system collection attached while executing
11551155
block := generateBlock(0, 0, rag)
11561156

1157-
view := delta.NewDeltaView(ledger)
1158-
11591157
result, err := exe.ExecuteBlock(
11601158
context.Background(),
11611159
unittest.IdentifierFixture(),
11621160
block,
1163-
view,
1161+
ledger,
11641162
derived.NewEmptyDerivedBlockData())
11651163
assert.NoError(t, err)
11661164
assert.Len(t, result.StateSnapshots, 1) // +1 system chunk

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)