github.com/koko1123/flow-go-1@v0.29.6/model/flow/event_test.go (about)

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