|
| 1 | +package blueprints_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + |
| 9 | + "github.com/onflow/cadence" |
| 10 | + cadenceCommon "github.com/onflow/cadence/common" |
| 11 | + "github.com/onflow/cadence/encoding/ccf" |
| 12 | + |
| 13 | + "github.com/onflow/flow-go/fvm/blueprints" |
| 14 | + "github.com/onflow/flow-go/model/flow" |
| 15 | + "github.com/onflow/flow-go/utils/unittest" |
| 16 | +) |
| 17 | + |
| 18 | +func TestProcessCallbacksTransaction(t *testing.T) { |
| 19 | + t.Parallel() |
| 20 | + |
| 21 | + chain := flow.Mainnet.Chain() |
| 22 | + const effortLeft = 100 |
| 23 | + tx, err := blueprints.ProcessCallbacksTransaction(chain, effortLeft) |
| 24 | + require.NoError(t, err) |
| 25 | + |
| 26 | + assert.NotNil(t, tx) |
| 27 | + assert.NotEmpty(t, tx.Script) |
| 28 | + assert.Equal(t, uint64(flow.DefaultMaxTransactionGasLimit), tx.GasLimit) |
| 29 | + encodedEffort, err := ccf.Encode(cadence.UInt64(effortLeft)) |
| 30 | + require.NoError(t, err) |
| 31 | + assert.Equal(t, encodedEffort, tx.Arguments[0]) |
| 32 | +} |
| 33 | + |
| 34 | +func TestExecuteCallbacksTransactions(t *testing.T) { |
| 35 | + t.Parallel() |
| 36 | + |
| 37 | + chain := flow.Mainnet.Chain() |
| 38 | + |
| 39 | + tests := []struct { |
| 40 | + name string |
| 41 | + events []flow.Event |
| 42 | + expectedTxs int |
| 43 | + expectError bool |
| 44 | + errorMessage string |
| 45 | + }{ |
| 46 | + { |
| 47 | + name: "no events", |
| 48 | + events: []flow.Event{}, |
| 49 | + expectedTxs: 0, |
| 50 | + expectError: false, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "single valid event", |
| 54 | + events: []flow.Event{createValidCallbackEvent(t, 1, 100)}, |
| 55 | + expectedTxs: 1, |
| 56 | + expectError: false, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "multiple valid events", |
| 60 | + events: []flow.Event{ |
| 61 | + createValidCallbackEvent(t, 1, 100), |
| 62 | + createValidCallbackEvent(t, 2, 200), |
| 63 | + createValidCallbackEvent(t, 3, 300), |
| 64 | + }, |
| 65 | + expectedTxs: 3, |
| 66 | + expectError: false, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "invalid event type", |
| 70 | + events: []flow.Event{createInvalidTypeEvent()}, |
| 71 | + expectedTxs: 0, |
| 72 | + expectError: true, |
| 73 | + errorMessage: "failed to get callback args from event", |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "invalid event payload", |
| 77 | + events: []flow.Event{createInvalidPayloadEvent()}, |
| 78 | + expectedTxs: 0, |
| 79 | + expectError: true, |
| 80 | + errorMessage: "failed to get callback args from event", |
| 81 | + }, |
| 82 | + } |
| 83 | + |
| 84 | + for _, tt := range tests { |
| 85 | + t.Run(tt.name, func(t *testing.T) { |
| 86 | + txs, err := blueprints.ExecuteCallbacksTransactions(chain, tt.events) |
| 87 | + |
| 88 | + if tt.expectError { |
| 89 | + assert.Error(t, err) |
| 90 | + assert.Contains(t, err.Error(), tt.errorMessage) |
| 91 | + assert.Nil(t, txs) |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + assert.NoError(t, err) |
| 96 | + assert.Len(t, txs, tt.expectedTxs) |
| 97 | + |
| 98 | + for i, tx := range txs { |
| 99 | + assert.NotNil(t, tx) |
| 100 | + assert.NotEmpty(t, tx.Script) |
| 101 | + expectedEffort := uint64(100 * (i + 1)) // Events created with efforts 100, 200, 300 |
| 102 | + assert.Equal(t, expectedEffort, tx.GasLimit) |
| 103 | + assert.Len(t, tx.Arguments, 1) |
| 104 | + assert.NotEmpty(t, tx.Arguments[0]) |
| 105 | + |
| 106 | + t.Logf("Transaction %d: ID arg length: %d, GasLimit: %d", |
| 107 | + i, len(tx.Arguments[0]), tx.GasLimit) |
| 108 | + } |
| 109 | + }) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func TestExecuteCallbackTransaction(t *testing.T) { |
| 114 | + t.Parallel() |
| 115 | + |
| 116 | + chain := flow.Mainnet.Chain() |
| 117 | + |
| 118 | + const id = 123 |
| 119 | + const effort = 456 |
| 120 | + event := createValidCallbackEvent(t, id, effort) |
| 121 | + txs, err := blueprints.ExecuteCallbacksTransactions(chain, []flow.Event{event}) |
| 122 | + |
| 123 | + require.NoError(t, err) |
| 124 | + require.Len(t, txs, 1) |
| 125 | + |
| 126 | + tx := txs[0] |
| 127 | + assert.NotNil(t, tx) |
| 128 | + assert.NotEmpty(t, tx.Script) |
| 129 | + assert.Equal(t, uint64(effort), tx.GasLimit) |
| 130 | + assert.Len(t, tx.Arguments, 1) |
| 131 | + |
| 132 | + expectedEncodedID, err := ccf.Encode(cadence.NewUInt64(id)) |
| 133 | + require.NoError(t, err) |
| 134 | + assert.Equal(t, tx.Arguments[0], expectedEncodedID) |
| 135 | + |
| 136 | + assert.Equal(t, tx.GasLimit, uint64(effort)) |
| 137 | +} |
| 138 | + |
| 139 | +func createValidCallbackEvent(t *testing.T, id uint64, effort uint64) flow.Event { |
| 140 | + // todo use proper location |
| 141 | + contractAddress := flow.HexToAddress("0x0000000000000000") |
| 142 | + location := cadenceCommon.NewAddressLocation(nil, cadenceCommon.Address(contractAddress), "CallbackScheduler") |
| 143 | + |
| 144 | + eventType := cadence.NewEventType( |
| 145 | + location, |
| 146 | + "CallbackProcessed", |
| 147 | + []cadence.Field{ |
| 148 | + {Identifier: "ID", Type: cadence.UInt64Type}, |
| 149 | + {Identifier: "executionEffort", Type: cadence.UInt64Type}, |
| 150 | + }, |
| 151 | + nil, |
| 152 | + ) |
| 153 | + |
| 154 | + event := cadence.NewEvent( |
| 155 | + []cadence.Value{ |
| 156 | + cadence.NewUInt64(id), |
| 157 | + cadence.NewUInt64(effort), |
| 158 | + }, |
| 159 | + ).WithType(eventType) |
| 160 | + |
| 161 | + payload, err := ccf.Encode(event) |
| 162 | + require.NoError(t, err) |
| 163 | + |
| 164 | + return flow.Event{ |
| 165 | + Type: flow.EventType("A.0x0000000000000000.CallbackScheduler.CallbackProcessed"), |
| 166 | + TransactionID: unittest.IdentifierFixture(), |
| 167 | + TransactionIndex: 0, |
| 168 | + EventIndex: 0, |
| 169 | + Payload: payload, |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +func createInvalidTypeEvent() flow.Event { |
| 174 | + return flow.Event{ |
| 175 | + Type: flow.EventType("A.0x0000000000000000.SomeContract.WrongEvent"), |
| 176 | + TransactionID: unittest.IdentifierFixture(), |
| 177 | + TransactionIndex: 0, |
| 178 | + EventIndex: 0, |
| 179 | + Payload: []byte("invalid"), |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +func createInvalidPayloadEvent() flow.Event { |
| 184 | + return flow.Event{ |
| 185 | + Type: flow.EventType("A.0x0000000000000000.CallbackScheduler.CallbackProcessed"), |
| 186 | + TransactionID: unittest.IdentifierFixture(), |
| 187 | + TransactionIndex: 0, |
| 188 | + EventIndex: 0, |
| 189 | + Payload: []byte("not valid ccf"), |
| 190 | + } |
| 191 | +} |
0 commit comments