|
7 | 7 | "math/big"
|
8 | 8 | "testing"
|
9 | 9 |
|
10 |
| - "github.com/onflow/cadence/runtime/common" |
11 |
| - |
12 | 10 | "github.com/onflow/cadence/encoding/ccf"
|
13 | 11 | gethTypes "github.com/onflow/go-ethereum/core/types"
|
14 | 12 | gethParams "github.com/onflow/go-ethereum/params"
|
@@ -103,49 +101,25 @@ func TestEVMRun(t *testing.T) {
|
103 | 101 | require.NoError(t, err)
|
104 | 102 | require.NoError(t, output.Err)
|
105 | 103 | require.NotEmpty(t, state.WriteSet)
|
| 104 | + snapshot = snapshot.Append(state) |
106 | 105 |
|
107 |
| - // assert event fiedls are correct |
108 |
| - require.Len(t, output.Events, 2) |
109 |
| - |
110 |
| - blockEvent := output.Events[1] |
111 |
| - |
112 |
| - assert.Equal( |
113 |
| - t, |
114 |
| - common.NewAddressLocation( |
115 |
| - nil, |
116 |
| - common.Address(sc.EVMContract.Address), |
117 |
| - string(types.EventTypeBlockExecuted), |
118 |
| - ).ID(), |
119 |
| - string(blockEvent.Type), |
120 |
| - ) |
121 |
| - |
122 |
| - ev, err := ccf.Decode(nil, blockEvent.Payload) |
123 |
| - require.NoError(t, err) |
124 |
| - cadenceEvent, ok := ev.(cadence.Event) |
125 |
| - require.True(t, ok) |
126 |
| - |
127 |
| - blockEventPayload, err := types.DecodeBlockEventPayload(cadenceEvent) |
128 |
| - require.NoError(t, err) |
129 |
| - require.NotEmpty(t, blockEventPayload.Hash) |
130 |
| - |
| 106 | + // assert event fields are correct |
| 107 | + require.Len(t, output.Events, 1) |
131 | 108 | txEvent := output.Events[0]
|
132 | 109 |
|
133 |
| - assert.Equal( |
134 |
| - t, |
135 |
| - common.NewAddressLocation( |
136 |
| - nil, |
137 |
| - common.Address(sc.EVMContract.Address), |
138 |
| - string(types.EventTypeTransactionExecuted), |
139 |
| - ).ID(), |
140 |
| - string(txEvent.Type), |
141 |
| - ) |
| 110 | + // commit block |
| 111 | + blockEventPayload, snapshot := callEVMHeartBeat(t, |
| 112 | + ctx, |
| 113 | + vm, |
| 114 | + snapshot) |
142 | 115 |
|
143 |
| - ev, err = ccf.Decode(nil, txEvent.Payload) |
144 |
| - require.NoError(t, err) |
145 |
| - cadenceEvent, ok = ev.(cadence.Event) |
146 |
| - require.True(t, ok) |
| 116 | + require.NotEmpty(t, blockEventPayload.Hash) |
| 117 | + require.Equal(t, uint64(43785), blockEventPayload.TotalGasUsed) |
| 118 | + require.NotEmpty(t, blockEventPayload.Hash) |
| 119 | + require.Len(t, blockEventPayload.TransactionHashes, 1) |
| 120 | + require.NotEmpty(t, blockEventPayload.ReceiptRoot) |
147 | 121 |
|
148 |
| - txEventPayload, err := types.DecodeTransactionEventPayload(cadenceEvent) |
| 122 | + txEventPayload := testutils.TxEventToPayload(t, txEvent, sc.EVMContract.Address) |
149 | 123 | require.NoError(t, err)
|
150 | 124 |
|
151 | 125 | txPayload, err := types.CadenceUInt8ArrayValueToBytes(txEventPayload.Payload)
|
@@ -388,16 +362,11 @@ func TestEVMRun(t *testing.T) {
|
388 | 362 | require.NotEmpty(t, state.WriteSet)
|
389 | 363 |
|
390 | 364 | txEvent := output.Events[0]
|
391 |
| - ev, err := ccf.Decode(nil, txEvent.Payload) |
392 |
| - require.NoError(t, err) |
393 |
| - cadenceEvent, ok := ev.(cadence.Event) |
394 |
| - require.True(t, ok) |
| 365 | + txEventPayload := testutils.TxEventToPayload(t, txEvent, sc.EVMContract.Address) |
395 | 366 |
|
396 |
| - event, err := types.DecodeTransactionEventPayload(cadenceEvent) |
397 |
| - require.NoError(t, err) |
398 |
| - require.NotEmpty(t, event.Hash) |
| 367 | + require.NotEmpty(t, txEventPayload.Hash) |
399 | 368 |
|
400 |
| - encodedLogs, err := types.CadenceUInt8ArrayValueToBytes(event.Logs) |
| 369 | + encodedLogs, err := types.CadenceUInt8ArrayValueToBytes(txEventPayload.Logs) |
401 | 370 | require.NoError(t, err)
|
402 | 371 |
|
403 | 372 | var logs []*gethTypes.Log
|
@@ -491,7 +460,10 @@ func TestEVMBatchRun(t *testing.T) {
|
491 | 460 | require.NoError(t, output.Err)
|
492 | 461 | require.NotEmpty(t, state.WriteSet)
|
493 | 462 |
|
494 |
| - require.Len(t, output.Events, batchCount+1) // +1 block executed |
| 463 | + // append the state |
| 464 | + snapshot = snapshot.Append(state) |
| 465 | + |
| 466 | + require.Len(t, output.Events, batchCount) |
495 | 467 | for i, event := range output.Events {
|
496 | 468 | if i == batchCount { // last one is block executed
|
497 | 469 | continue
|
@@ -519,31 +491,15 @@ func TestEVMBatchRun(t *testing.T) {
|
519 | 491 | assert.Equal(t, storedValues[i], last.Big().Int64())
|
520 | 492 | }
|
521 | 493 |
|
522 |
| - // last one is block executed, make sure TotalGasUsed is non-zero |
523 |
| - blockEvent := output.Events[batchCount] |
524 |
| - |
525 |
| - assert.Equal( |
526 |
| - t, |
527 |
| - common.NewAddressLocation( |
528 |
| - nil, |
529 |
| - common.Address(sc.EVMContract.Address), |
530 |
| - string(types.EventTypeBlockExecuted), |
531 |
| - ).ID(), |
532 |
| - string(blockEvent.Type), |
533 |
| - ) |
534 |
| - |
535 |
| - ev, err := ccf.Decode(nil, blockEvent.Payload) |
536 |
| - require.NoError(t, err) |
537 |
| - cadenceEvent, ok := ev.(cadence.Event) |
538 |
| - require.True(t, ok) |
| 494 | + // commit block |
| 495 | + blockEventPayload, snapshot := callEVMHeartBeat(t, |
| 496 | + ctx, |
| 497 | + vm, |
| 498 | + snapshot) |
539 | 499 |
|
540 |
| - blockEventPayload, err := types.DecodeBlockEventPayload(cadenceEvent) |
541 |
| - require.NoError(t, err) |
542 | 500 | require.NotEmpty(t, blockEventPayload.Hash)
|
543 | 501 | require.Equal(t, uint64(155513), blockEventPayload.TotalGasUsed)
|
544 |
| - |
545 |
| - // append the state |
546 |
| - snapshot = snapshot.Append(state) |
| 502 | + require.Len(t, blockEventPayload.TransactionHashes, 5) |
547 | 503 |
|
548 | 504 | // retrieve the values
|
549 | 505 | retrieveCode := []byte(fmt.Sprintf(
|
@@ -1003,38 +959,31 @@ func TestEVMAddressDeposit(t *testing.T) {
|
1003 | 959 | bal := getEVMAccountBalance(t, ctx, vm, snapshot, addr)
|
1004 | 960 | require.Equal(t, expectedBalance, bal)
|
1005 | 961 |
|
1006 |
| - // block executed event, make sure TotalGasUsed is non-zero |
1007 |
| - blockEvent := output.Events[3] |
1008 |
| - |
1009 |
| - assert.Equal( |
1010 |
| - t, |
1011 |
| - common.NewAddressLocation( |
1012 |
| - nil, |
1013 |
| - common.Address(sc.EVMContract.Address), |
1014 |
| - string(types.EventTypeBlockExecuted), |
1015 |
| - ).ID(), |
1016 |
| - string(blockEvent.Type), |
1017 |
| - ) |
1018 |
| - |
1019 |
| - ev, err := ccf.Decode(nil, blockEvent.Payload) |
| 962 | + // tx executed event |
| 963 | + txEvent := output.Events[2] |
| 964 | + txEventPayload := testutils.TxEventToPayload(t, txEvent, sc.EVMContract.Address) |
1020 | 965 | require.NoError(t, err)
|
1021 |
| - cadenceEvent, ok := ev.(cadence.Event) |
1022 |
| - require.True(t, ok) |
1023 |
| - |
1024 |
| - blockEventPayload, err := types.DecodeBlockEventPayload(cadenceEvent) |
1025 |
| - require.NoError(t, err) |
1026 |
| - require.NotEmpty(t, blockEventPayload.Hash) |
1027 |
| - require.Equal(t, uint64(21000), blockEventPayload.TotalGasUsed) |
1028 | 966 |
|
1029 | 967 | // deposit event
|
1030 |
| - depositEvent := output.Events[4] |
| 968 | + depositEvent := output.Events[3] |
1031 | 969 | depEv, err := types.FlowEventToCadenceEvent(depositEvent)
|
1032 | 970 | require.NoError(t, err)
|
1033 | 971 |
|
1034 | 972 | depEvPayload, err := types.DecodeFLOWTokensDepositedEventPayload(depEv)
|
1035 | 973 | require.NoError(t, err)
|
1036 | 974 |
|
1037 | 975 | require.Equal(t, types.OneFlow, depEvPayload.BalanceAfterInAttoFlow.Value)
|
| 976 | + |
| 977 | + // commit block |
| 978 | + blockEventPayload, _ := callEVMHeartBeat(t, |
| 979 | + ctx, |
| 980 | + vm, |
| 981 | + snapshot) |
| 982 | + |
| 983 | + require.NotEmpty(t, blockEventPayload.Hash) |
| 984 | + require.Equal(t, uint64(21000), blockEventPayload.TotalGasUsed) |
| 985 | + require.Len(t, blockEventPayload.TransactionHashes, 1) |
| 986 | + require.Equal(t, txEventPayload.Hash, string(blockEventPayload.TransactionHashes[0])) |
1038 | 987 | })
|
1039 | 988 | }
|
1040 | 989 |
|
@@ -1197,7 +1146,7 @@ func TestCadenceOwnedAccountFunctionalities(t *testing.T) {
|
1197 | 1146 | require.NoError(t, err)
|
1198 | 1147 | require.NoError(t, output.Err)
|
1199 | 1148 |
|
1200 |
| - withdrawEvent := output.Events[10] |
| 1149 | + withdrawEvent := output.Events[7] |
1201 | 1150 |
|
1202 | 1151 | ev, err := types.FlowEventToCadenceEvent(withdrawEvent)
|
1203 | 1152 | require.NoError(t, err)
|
@@ -2557,12 +2506,52 @@ func setupCOA(
|
2557 | 2506 | snap = snap.Append(es)
|
2558 | 2507 |
|
2559 | 2508 | // 3rd event is the cadence owned account created event
|
2560 |
| - coaAddress, err := types.COAAddressFromFlowCOACreatedEvent(sc.EVMContract.Address, output.Events[2]) |
| 2509 | + coaAddress, err := types.COAAddressFromFlowCOACreatedEvent(sc.EVMContract.Address, output.Events[1]) |
2561 | 2510 | require.NoError(t, err)
|
2562 | 2511 |
|
2563 | 2512 | return coaAddress, snap
|
2564 | 2513 | }
|
2565 | 2514 |
|
| 2515 | +func callEVMHeartBeat( |
| 2516 | + t *testing.T, |
| 2517 | + ctx fvm.Context, |
| 2518 | + vm fvm.VM, |
| 2519 | + snap snapshot.SnapshotTree, |
| 2520 | +) (*types.BlockEventPayload, snapshot.SnapshotTree) { |
| 2521 | + sc := systemcontracts.SystemContractsForChain(ctx.Chain.ChainID()) |
| 2522 | + |
| 2523 | + heartBeatCode := []byte(fmt.Sprintf( |
| 2524 | + ` |
| 2525 | + import EVM from %s |
| 2526 | + transaction { |
| 2527 | + prepare(serviceAccount: auth(BorrowValue) &Account) { |
| 2528 | + let evmHeartbeat = serviceAccount.storage |
| 2529 | + .borrow<&EVM.Heartbeat>(from: /storage/EVMHeartbeat) |
| 2530 | + ?? panic("Couldn't borrow EVM.Heartbeat Resource") |
| 2531 | + evmHeartbeat.heartbeat() |
| 2532 | + } |
| 2533 | + } |
| 2534 | + `, |
| 2535 | + sc.EVMContract.Address.HexWithPrefix(), |
| 2536 | + )) |
| 2537 | + tx := fvm.Transaction( |
| 2538 | + flow.NewTransactionBody(). |
| 2539 | + SetScript(heartBeatCode). |
| 2540 | + AddAuthorizer(sc.FlowServiceAccount.Address), |
| 2541 | + 0) |
| 2542 | + |
| 2543 | + state, output, err := vm.Run(ctx, tx, snap) |
| 2544 | + require.NoError(t, err) |
| 2545 | + require.NoError(t, output.Err) |
| 2546 | + require.NotEmpty(t, state.WriteSet) |
| 2547 | + snap = snap.Append(state) |
| 2548 | + |
| 2549 | + // validate block event |
| 2550 | + require.Len(t, output.Events, 1) |
| 2551 | + blockEvent := output.Events[0] |
| 2552 | + return BlockEventToPayload(t, blockEvent, sc.EVMContract.Address), snap |
| 2553 | +} |
| 2554 | + |
2566 | 2555 | func getFlowAccountBalance(
|
2567 | 2556 | t *testing.T,
|
2568 | 2557 | ctx fvm.Context,
|
|
0 commit comments