github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/common/rpc/convert/events_test.go (about) 1 package convert_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 "github.com/onflow/cadence/encoding/ccf" 11 jsoncdc "github.com/onflow/cadence/encoding/json" 12 "github.com/onflow/flow/protobuf/go/flow/entities" 13 14 "github.com/onflow/flow-go/engine/common/rpc/convert" 15 "github.com/onflow/flow-go/model/flow" 16 "github.com/onflow/flow-go/utils/unittest" 17 ) 18 19 // TestConvertEventWithoutPayloadConversion tests converting events to and from protobuf messages 20 // with no payload modification 21 func TestConvertEventWithoutPayloadConversion(t *testing.T) { 22 t.Parallel() 23 24 txID := unittest.IdentifierFixture() 25 cadenceValue := cadence.NewInt(2) 26 27 t.Run("convert empty event", func(t *testing.T) { 28 event := unittest.EventFixture(flow.EventAccountCreated, 2, 3, txID, 0) 29 30 msg := convert.EventToMessage(event) 31 converted := convert.MessageToEvent(msg) 32 33 assert.Equal(t, event, converted) 34 }) 35 36 t.Run("convert json cdc encoded event", func(t *testing.T) { 37 ccfPayload, err := ccf.Encode(cadenceValue) 38 require.NoError(t, err) 39 40 event := unittest.EventFixture(flow.EventAccountCreated, 2, 3, txID, 0) 41 event.Payload = ccfPayload 42 43 msg := convert.EventToMessage(event) 44 converted := convert.MessageToEvent(msg) 45 46 assert.Equal(t, event, converted) 47 }) 48 49 t.Run("convert json cdc encoded event", func(t *testing.T) { 50 jsonPayload, err := jsoncdc.Encode(cadenceValue) 51 require.NoError(t, err) 52 53 event := unittest.EventFixture(flow.EventAccountCreated, 2, 3, txID, 0) 54 event.Payload = jsonPayload 55 56 msg := convert.EventToMessage(event) 57 converted := convert.MessageToEvent(msg) 58 59 assert.Equal(t, event.Type, converted.Type) 60 }) 61 } 62 63 // TestConvertEventWithPayloadConversion tests converting events to and from protobuf messages 64 // with payload modification 65 func TestConvertEventWithPayloadConversion(t *testing.T) { 66 t.Parallel() 67 68 txID := unittest.IdentifierFixture() 69 cadenceValue := cadence.NewInt(2) 70 71 var err error 72 ccfEvent := unittest.EventFixture(flow.EventAccountCreated, 2, 3, txID, 0) 73 ccfEvent.Payload, err = ccf.Encode(cadenceValue) 74 require.NoError(t, err) 75 76 jsonEvent := unittest.EventFixture(flow.EventAccountCreated, 2, 3, txID, 0) 77 jsonEvent.Payload, err = jsoncdc.Encode(cadenceValue) 78 require.NoError(t, err) 79 80 t.Run("convert payload from ccf to jsoncdc", func(t *testing.T) { 81 message := convert.EventToMessage(ccfEvent) 82 convertedEvent, err := convert.MessageToEventFromVersion(message, entities.EventEncodingVersion_CCF_V0) 83 assert.NoError(t, err) 84 85 assert.Equal(t, jsonEvent, *convertedEvent) 86 }) 87 88 t.Run("convert payload from jsoncdc to jsoncdc", func(t *testing.T) { 89 message := convert.EventToMessage(jsonEvent) 90 convertedEvent, err := convert.MessageToEventFromVersion(message, entities.EventEncodingVersion_JSON_CDC_V0) 91 assert.NoError(t, err) 92 93 assert.Equal(t, jsonEvent, *convertedEvent) 94 }) 95 } 96 97 func TestConvertEvents(t *testing.T) { 98 t.Parallel() 99 100 eventCount := 3 101 txID := unittest.IdentifierFixture() 102 103 events := make([]flow.Event, eventCount) 104 ccfEvents := make([]flow.Event, eventCount) 105 jsonEvents := make([]flow.Event, eventCount) 106 for i := 0; i < eventCount; i++ { 107 cadenceValue := cadence.NewInt(i) 108 109 eventIndex := 3 + uint32(i) 110 111 event := unittest.EventFixture(flow.EventAccountCreated, 2, eventIndex, txID, 0) 112 ccfEvent := unittest.EventFixture(flow.EventAccountCreated, 2, eventIndex, txID, 0) 113 jsonEvent := unittest.EventFixture(flow.EventAccountCreated, 2, eventIndex, txID, 0) 114 115 var err error 116 ccfEvent.Payload, err = ccf.Encode(cadenceValue) 117 require.NoError(t, err) 118 119 jsonEvent.Payload, err = jsoncdc.Encode(cadenceValue) 120 require.NoError(t, err) 121 122 events[i] = event 123 ccfEvents[i] = ccfEvent 124 jsonEvents[i] = jsonEvent 125 } 126 127 t.Run("empty", func(t *testing.T) { 128 messages := convert.EventsToMessages(nil) 129 assert.Len(t, messages, 0) 130 }) 131 132 t.Run("convert with passthrough payload conversion", func(t *testing.T) { 133 messages := convert.EventsToMessages(events) 134 require.Len(t, messages, len(events)) 135 136 for i, message := range messages { 137 event := events[i] 138 require.Equal(t, event.EventIndex, message.EventIndex) 139 require.Equal(t, event.TransactionIndex, message.TransactionIndex) 140 require.Equal(t, event.Payload, message.Payload) 141 require.Equal(t, event.TransactionID[:], message.TransactionId) 142 require.Equal(t, string(event.Type), message.Type) 143 } 144 145 converted := convert.MessagesToEvents(messages) 146 assert.Equal(t, events, converted) 147 }) 148 149 t.Run("convert event from ccf to jsoncdc", func(t *testing.T) { 150 messages := convert.EventsToMessages(ccfEvents) 151 converted, err := convert.MessagesToEventsWithEncodingConversion(messages, entities.EventEncodingVersion_CCF_V0, entities.EventEncodingVersion_JSON_CDC_V0) 152 assert.NoError(t, err) 153 154 assert.Equal(t, jsonEvents, converted) 155 }) 156 157 t.Run("convert event from jsoncdc", func(t *testing.T) { 158 messages := convert.EventsToMessages(jsonEvents) 159 converted, err := convert.MessagesToEventsWithEncodingConversion(messages, entities.EventEncodingVersion_JSON_CDC_V0, entities.EventEncodingVersion_JSON_CDC_V0) 160 assert.NoError(t, err) 161 162 assert.Equal(t, jsonEvents, converted) 163 }) 164 165 t.Run("convert event from ccf", func(t *testing.T) { 166 messages := convert.EventsToMessages(jsonEvents) 167 converted, err := convert.MessagesToEventsWithEncodingConversion(messages, entities.EventEncodingVersion_CCF_V0, entities.EventEncodingVersion_CCF_V0) 168 assert.NoError(t, err) 169 170 assert.Equal(t, jsonEvents, converted) 171 }) 172 173 t.Run("convert event from jsoncdc to ccf", func(t *testing.T) { 174 messages := convert.EventsToMessages(jsonEvents) 175 converted, err := convert.MessagesToEventsWithEncodingConversion(messages, entities.EventEncodingVersion_JSON_CDC_V0, entities.EventEncodingVersion_CCF_V0) 176 assert.Error(t, err) 177 assert.Nil(t, converted) 178 }) 179 } 180 181 func TestConvertServiceEvent(t *testing.T) { 182 t.Parallel() 183 184 serviceEvents := unittest.ServiceEventsFixture(1) 185 require.Len(t, serviceEvents, 1) 186 187 msg, err := convert.ServiceEventToMessage(serviceEvents[0]) 188 require.NoError(t, err) 189 190 converted, err := convert.MessageToServiceEvent(msg) 191 require.NoError(t, err) 192 193 assert.Equal(t, serviceEvents[0], *converted) 194 } 195 196 func TestConvertServiceEventList(t *testing.T) { 197 t.Parallel() 198 199 serviceEvents := unittest.ServiceEventsFixture(5) 200 require.Len(t, serviceEvents, 5) 201 202 msg, err := convert.ServiceEventListToMessages(serviceEvents) 203 require.NoError(t, err) 204 205 converted, err := convert.MessagesToServiceEventList(msg) 206 require.NoError(t, err) 207 208 assert.Equal(t, serviceEvents, converted) 209 } 210 211 // TestConvertMessagesToBlockEvents tests that converting a protobuf EventsResponse_Result message to and from block events in the same 212 // block 213 func TestConvertMessagesToBlockEvents(t *testing.T) { 214 t.Parallel() 215 216 count := 2 217 blockEvents := make([]flow.BlockEvents, count) 218 for i := 0; i < count; i++ { 219 header := unittest.BlockHeaderFixture(unittest.WithHeaderHeight(uint64(i))) 220 blockEvents[i] = unittest.BlockEventsFixture(header, 2) 221 } 222 223 msg, err := convert.BlockEventsToMessages(blockEvents) 224 require.NoError(t, err) 225 226 converted := convert.MessagesToBlockEvents(msg) 227 require.NoError(t, err) 228 229 assert.Equal(t, blockEvents, converted) 230 }