github.com/koko1123/flow-go-1@v0.29.6/fvm/systemcontracts/system_contracts_test.go (about)

     1  package systemcontracts
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/koko1123/flow-go-1/model/flow"
    11  )
    12  
    13  // TestSystemContract_Address tests that we can retrieve a canonical address
    14  // for all accepted chains and contracts.
    15  func TestSystemContracts(t *testing.T) {
    16  	chains := []flow.ChainID{flow.Mainnet, flow.Testnet, flow.Sandboxnet, flow.Benchnet, flow.Localnet, flow.Emulator}
    17  
    18  	for _, chain := range chains {
    19  		_, err := SystemContractsForChain(chain)
    20  		require.NoError(t, err)
    21  		checkSystemContracts(t, chain)
    22  	}
    23  }
    24  
    25  // TestSystemContract_InvalidChainID tests that we get an error if querying by an
    26  // invalid chain ID.
    27  func TestSystemContract_InvalidChainID(t *testing.T) {
    28  	invalidChain := flow.ChainID("invalid-chain")
    29  
    30  	_, err := SystemContractsForChain(invalidChain)
    31  	assert.Error(t, err)
    32  }
    33  
    34  // TestServiceEvents tests that we can retrieve service events for all accepted
    35  // chains and contracts.
    36  func TestServiceEvents(t *testing.T) {
    37  	chains := []flow.ChainID{flow.Mainnet, flow.Testnet, flow.Sandboxnet, flow.Benchnet, flow.Localnet, flow.Emulator}
    38  
    39  	for _, chain := range chains {
    40  		_, err := ServiceEventsForChain(chain)
    41  		checkServiceEvents(t, chain)
    42  		require.NoError(t, err)
    43  	}
    44  }
    45  
    46  // TestServiceEventLookup_Consistency sanity checks consistency of the lookup
    47  // method, in case an update to ServiceEvents forgets to update the lookup.
    48  func TestServiceEventAll_Consistency(t *testing.T) {
    49  	chains := []flow.ChainID{flow.Mainnet, flow.Testnet, flow.Sandboxnet, flow.Benchnet, flow.Localnet, flow.Emulator}
    50  
    51  	fields := reflect.TypeOf(ServiceEvents{}).NumField()
    52  	for _, chain := range chains {
    53  		events, err := ServiceEventsForChain(chain)
    54  		require.NoError(t, err)
    55  
    56  		// ensure all events are returns
    57  		all := events.All()
    58  		assert.Equal(t, fields, len(all))
    59  	}
    60  }
    61  
    62  // TestServiceEvents_InvalidChainID tests that we get an error if querying by an
    63  // invalid chain ID.
    64  func TestServiceEvents_InvalidChainID(t *testing.T) {
    65  	invalidChain := flow.ChainID("invalid-chain")
    66  
    67  	_, err := ServiceEventsForChain(invalidChain)
    68  	assert.Error(t, err)
    69  }
    70  
    71  func checkSystemContracts(t *testing.T, chainID flow.ChainID) {
    72  	contracts, err := SystemContractsForChain(chainID)
    73  	require.NoError(t, err)
    74  
    75  	addresses, ok := contractAddressesByChainID[chainID]
    76  	require.True(t, ok, "missing chain %s", chainID.String())
    77  
    78  	// entries may not be empty
    79  	assert.NotEqual(t, flow.EmptyAddress, addresses[ContractNameEpoch])
    80  	assert.NotEqual(t, flow.EmptyAddress, addresses[ContractNameClusterQC])
    81  	assert.NotEqual(t, flow.EmptyAddress, addresses[ContractNameDKG])
    82  
    83  	// entries must match internal mapping
    84  	assert.Equal(t, addresses[ContractNameEpoch], contracts.Epoch.Address)
    85  	assert.Equal(t, addresses[ContractNameClusterQC], contracts.ClusterQC.Address)
    86  	assert.Equal(t, addresses[ContractNameDKG], contracts.DKG.Address)
    87  }
    88  
    89  func checkServiceEvents(t *testing.T, chainID flow.ChainID) {
    90  	events, err := ServiceEventsForChain(chainID)
    91  	require.NoError(t, err)
    92  
    93  	addresses, ok := contractAddressesByChainID[chainID]
    94  	require.True(t, ok, "missing chain %w", chainID.String())
    95  
    96  	epochContractAddr := addresses[ContractNameEpoch]
    97  	// entries may not be empty
    98  	assert.NotEqual(t, flow.EmptyAddress, epochContractAddr)
    99  
   100  	// entries must match internal mapping
   101  	assert.Equal(t, epochContractAddr, events.EpochSetup.Address)
   102  	assert.Equal(t, epochContractAddr, events.EpochCommit.Address)
   103  }