github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/model/flow/event_test.go (about) 1 package flow_test 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 "github.com/stretchr/testify/require" 8 9 "github.com/onflow/flow-go/model/encoding/rlp" 10 "github.com/onflow/flow-go/model/fingerprint" 11 "github.com/onflow/flow-go/model/flow" 12 "github.com/onflow/flow-go/utils/unittest" 13 ) 14 15 type eventWrapper struct { 16 TxID []byte 17 Index uint32 18 Type string 19 TransactionIndex uint32 20 Payload []byte 21 } 22 23 func wrapEvent(e flow.Event) eventWrapper { 24 return eventWrapper{ 25 TxID: e.TransactionID[:], 26 Index: e.EventIndex, 27 Type: string(e.Type), 28 TransactionIndex: e.TransactionIndex, 29 Payload: e.Payload, 30 } 31 } 32 33 func TestEventFingerprint(t *testing.T) { 34 evt := unittest.EventFixture(flow.EventAccountCreated, 13, 12, unittest.IdentifierFixture(), 32) 35 36 data := fingerprint.Fingerprint(evt) 37 var decoded eventWrapper 38 rlp.NewMarshaler().MustUnmarshal(data, &decoded) 39 assert.Equal(t, wrapEvent(evt), decoded) 40 } 41 42 func TestEventID(t *testing.T) { 43 44 // EventID was historically calculated from just TxID and eventIndex which are enough to uniquely identify it in a system 45 // This test ensures we don't break this promise while introducing proper fingerprinting (which accounts for all the fields) 46 47 txID := unittest.IdentifierFixture() 48 evtA := unittest.EventFixture(flow.EventAccountUpdated, 21, 37, txID, 2) 49 evtB := unittest.EventFixture(flow.EventAccountCreated, 0, 37, txID, 22) 50 51 evtC := unittest.EventFixture(evtA.Type, evtA.TransactionIndex, evtA.EventIndex+1, txID, 2) 52 evtC.Payload = evtA.Payload 53 54 a := evtA.ID() 55 b := evtB.ID() 56 c := evtC.ID() 57 58 assert.Equal(t, a, b) 59 assert.NotEqual(t, a, c) 60 } 61 62 func TestEventsList(t *testing.T) { 63 64 eventA := unittest.EventFixture(flow.EventAccountUpdated, 21, 37, unittest.IdentifierFixture(), 2) 65 eventB := unittest.EventFixture(flow.EventAccountCreated, 0, 37, unittest.IdentifierFixture(), 22) 66 eventC := unittest.EventFixture(flow.EventAccountCreated, 0, 37, unittest.IdentifierFixture(), 22) 67 68 listAB := flow.EventsList{ 69 eventA, 70 eventB, 71 } 72 73 listBA := flow.EventsList{ 74 eventB, 75 eventA, 76 } 77 78 listAC := flow.EventsList{ 79 eventA, 80 eventC, 81 } 82 83 ABHash, err := flow.EventsMerkleRootHash(listAB) 84 assert.NoError(t, err) 85 ACHash, err := flow.EventsMerkleRootHash(listAC) 86 assert.NoError(t, err) 87 BAHash, err := flow.EventsMerkleRootHash(listBA) 88 assert.NoError(t, err) 89 90 t.Run("different events have different hash", func(t *testing.T) { 91 assert.NotEqual(t, ABHash, ACHash) 92 }) 93 94 t.Run("insertion order does not matter", func(t *testing.T) { 95 assert.Equal(t, ABHash, BAHash) 96 }) 97 } 98 99 func TestEventsMerkleRootHash(t *testing.T) { 100 eventA := flow.Event{ 101 Type: "eventTypeString", 102 TransactionIndex: 1, 103 EventIndex: 2, 104 Payload: []byte("cadence-json encoded data"), 105 TransactionID: [flow.IdentifierLen]byte{1, 2, 3}, 106 } 107 108 eventB := flow.Event{ 109 Type: "eventTypeString", 110 TransactionIndex: 1, 111 EventIndex: 3, 112 Payload: []byte("cadence-json encoded data"), 113 TransactionID: [flow.IdentifierLen]byte{1, 2, 3}, 114 } 115 116 expectedRootHashHex := "355446d7b2b9653403abe28ccc405f46c059d2059cb7863f4964c401ee1aa83b" 117 118 ABHash, err := flow.EventsMerkleRootHash([]flow.Event{eventA, eventB}) 119 assert.NoError(t, err) 120 assert.Equal(t, expectedRootHashHex, ABHash.String()) 121 } 122 123 func TestEmptyEventsMerkleRootHash(t *testing.T) { 124 actualHash, err := flow.EventsMerkleRootHash([]flow.Event{}) 125 require.NoError(t, err) 126 require.Equal(t, flow.EmptyEventCollectionID, actualHash) 127 }