github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/state_synchronization/indexer/util_test.go (about)

     1  package indexer
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/onflow/cadence"
     7  	"github.com/onflow/cadence/encoding/ccf"
     8  	"github.com/onflow/cadence/runtime/common"
     9  	"github.com/onflow/cadence/runtime/stdlib"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/onflow/flow/protobuf/go/flow/entities"
    14  
    15  	"github.com/onflow/flow-go/model/flow"
    16  	"github.com/onflow/flow-go/utils/unittest"
    17  	"github.com/onflow/flow-go/utils/unittest/generator"
    18  )
    19  
    20  // TestFindContractUpdates tests the findContractUpdates function returns all contract updates
    21  func TestFindContractUpdates(t *testing.T) {
    22  	t.Parallel()
    23  
    24  	g := generator.EventGenerator(generator.WithEncoding(entities.EventEncodingVersion_CCF_V0))
    25  
    26  	expectedAddress1 := unittest.RandomAddressFixture()
    27  	expected1 := common.NewAddressLocation(nil, common.Address(expectedAddress1), "TestContract1")
    28  
    29  	expectedAddress2 := unittest.RandomAddressFixture()
    30  	expected2 := common.NewAddressLocation(nil, common.Address(expectedAddress2), "TestContract2")
    31  
    32  	events := []flow.Event{
    33  		g.New(), // random event
    34  		contractUpdatedFixture(
    35  			t,
    36  			expected1.Address,
    37  			expected1.Name,
    38  		),
    39  		g.New(), // random event
    40  		g.New(), // random event
    41  		contractUpdatedFixture(
    42  			t,
    43  			expected2.Address,
    44  			expected2.Name,
    45  		),
    46  	}
    47  
    48  	updates, err := findContractUpdates(events)
    49  	require.NoError(t, err)
    50  
    51  	assert.Len(t, updates, 2)
    52  
    53  	_, ok := updates[expected1]
    54  	assert.Truef(t, ok, "could not find %s", expected1.ID())
    55  
    56  	_, ok = updates[expected2]
    57  	assert.Truef(t, ok, "could not find %s", expected2.ID())
    58  }
    59  
    60  func contractUpdatedFixture(t *testing.T, address common.Address, contractName string) flow.Event {
    61  	contractUpdateEventType := &cadence.EventType{
    62  		Location:            stdlib.AccountContractAddedEventType.Location,
    63  		QualifiedIdentifier: stdlib.AccountContractAddedEventType.QualifiedIdentifier(),
    64  		Fields: []cadence.Field{
    65  			{
    66  				Identifier: "address",
    67  				Type:       cadence.AddressType,
    68  			},
    69  			{
    70  				Identifier: "codeHash",
    71  				Type:       cadence.AddressType, // actually a byte slice, but we're ignoring it anyway
    72  			},
    73  			{
    74  				Identifier: "contract",
    75  				Type:       cadence.StringType,
    76  			},
    77  		},
    78  	}
    79  
    80  	contractString, err := cadence.NewString(contractName)
    81  	require.NoError(t, err)
    82  
    83  	testEvent := cadence.NewEvent(
    84  		[]cadence.Value{
    85  			cadence.NewAddress(address),
    86  			cadence.NewAddress(flow.EmptyAddress),
    87  			contractString,
    88  		}).WithType(contractUpdateEventType)
    89  
    90  	payload, err := ccf.Encode(testEvent)
    91  	require.NoError(t, err)
    92  
    93  	return flow.Event{
    94  		Type:             accountContractUpdated,
    95  		TransactionID:    unittest.IdentifierFixture(),
    96  		TransactionIndex: 0,
    97  		EventIndex:       0,
    98  		Payload:          payload,
    99  	}
   100  }