|
| 1 | +package grantrole |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "testing" |
| 8 | + |
| 9 | + chainselectors "github.com/smartcontractkit/chain-selectors" |
| 10 | + cldf_chain "github.com/smartcontractkit/chainlink-deployments-framework/chain" |
| 11 | + "github.com/smartcontractkit/chainlink-deployments-framework/changeset/sequenceutils" |
| 12 | + "github.com/smartcontractkit/chainlink-deployments-framework/datastore" |
| 13 | + cldf "github.com/smartcontractkit/chainlink-deployments-framework/deployment" |
| 14 | + "github.com/smartcontractkit/chainlink-deployments-framework/offchain/ocr" |
| 15 | + "github.com/smartcontractkit/chainlink-deployments-framework/pkg/logger" |
| 16 | + mcmssdk "github.com/smartcontractkit/mcms/sdk" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | +) |
| 19 | + |
| 20 | +func testEnvironment(t *testing.T, ds datastore.DataStore) cldf.Environment { |
| 21 | + t.Helper() |
| 22 | + |
| 23 | + return *cldf.NewEnvironment( |
| 24 | + "test", |
| 25 | + logger.Test(t), |
| 26 | + nil, |
| 27 | + ds, |
| 28 | + nil, |
| 29 | + nil, |
| 30 | + func() context.Context { return t.Context() }, |
| 31 | + ocr.OCRSecrets{}, |
| 32 | + cldf_chain.NewBlockChains(nil), |
| 33 | + ) |
| 34 | +} |
| 35 | + |
| 36 | +func TestChangeset_VerifyPreconditions_NoDatastore(t *testing.T) { |
| 37 | + t.Parallel() |
| 38 | + |
| 39 | + input := Input{ |
| 40 | + Cfg: Config{ |
| 41 | + GrantsByChain: map[uint64][]RoleGrant{ |
| 42 | + chainselectors.TEST_90000001.Selector: {{ |
| 43 | + Role: mcmssdk.TimelockRoleProposer, |
| 44 | + Addresses: []string{"0x0000000000000000000000000000000000000001"}, |
| 45 | + }}, |
| 46 | + }, |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + err := Changeset{}.VerifyPreconditions(testEnvironment(t, nil), input) |
| 51 | + require.EqualError(t, err, "datastore is required for grant-role") |
| 52 | +} |
| 53 | + |
| 54 | +func TestChangeset_VerifyPreconditions_InvalidInput(t *testing.T) { |
| 55 | + t.Parallel() |
| 56 | + |
| 57 | + validAddress := "0x0000000000000000000000000000000000000001" |
| 58 | + tests := []struct { |
| 59 | + name string |
| 60 | + input Input |
| 61 | + wantErr string |
| 62 | + }{ |
| 63 | + { |
| 64 | + name: "no grants", |
| 65 | + input: Input{}, |
| 66 | + wantErr: "no role grants provided", |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "empty grants for chain", |
| 70 | + input: Input{Cfg: Config{GrantsByChain: map[uint64][]RoleGrant{ |
| 71 | + chainselectors.TEST_90000001.Selector: {}, |
| 72 | + }}}, |
| 73 | + wantErr: fmt.Sprintf("chain %d: no role grants provided", chainselectors.TEST_90000001.Selector), |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "unsupported role", |
| 77 | + input: Input{Cfg: Config{GrantsByChain: map[uint64][]RoleGrant{ |
| 78 | + chainselectors.TEST_90000001.Selector: {{Role: mcmssdk.TimelockRole(99), Addresses: []string{validAddress}}}, |
| 79 | + }}}, |
| 80 | + wantErr: fmt.Sprintf("chain %d grants[0]: unsupported timelock role Unknown", chainselectors.TEST_90000001.Selector), |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "no addresses", |
| 84 | + input: Input{Cfg: Config{GrantsByChain: map[uint64][]RoleGrant{ |
| 85 | + chainselectors.TEST_90000001.Selector: {{Role: mcmssdk.TimelockRoleProposer}}, |
| 86 | + }}}, |
| 87 | + wantErr: fmt.Sprintf("chain %d grants[0]: no addresses provided", chainselectors.TEST_90000001.Selector), |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "empty address", |
| 91 | + input: Input{Cfg: Config{GrantsByChain: map[uint64][]RoleGrant{ |
| 92 | + chainselectors.TEST_90000001.Selector: {{ |
| 93 | + Role: mcmssdk.TimelockRoleProposer, |
| 94 | + Addresses: []string{""}, |
| 95 | + }}, |
| 96 | + }}}, |
| 97 | + wantErr: fmt.Sprintf("chain %d grants[0].addresses[0]: address must not be empty", chainselectors.TEST_90000001.Selector), |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "duplicate grant", |
| 101 | + input: Input{Cfg: Config{GrantsByChain: map[uint64][]RoleGrant{ |
| 102 | + chainselectors.TEST_90000001.Selector: {{ |
| 103 | + Role: mcmssdk.TimelockRoleProposer, |
| 104 | + Addresses: []string{validAddress, validAddress}, |
| 105 | + }}, |
| 106 | + }}}, |
| 107 | + wantErr: fmt.Sprintf("chain %d grants[0].addresses[1]: duplicate grant for role Proposer and address 0x0000000000000000000000000000000000000001", chainselectors.TEST_90000001.Selector), |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "invalid MCMS input", |
| 111 | + input: Input{ |
| 112 | + MCMS: &cldf.MCMSTimelockProposalInput{}, |
| 113 | + Cfg: Config{GrantsByChain: map[uint64][]RoleGrant{ |
| 114 | + chainselectors.TEST_90000001.Selector: {{ |
| 115 | + Role: mcmssdk.TimelockRoleProposer, |
| 116 | + Addresses: []string{validAddress}, |
| 117 | + }}, |
| 118 | + }}, |
| 119 | + }, |
| 120 | + wantErr: `invalid MCMS timelock proposal input: invalid MCMS timelock proposal input: invalid timelock action ""`, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + env := testEnvironment(t, datastore.NewMemoryDataStore().Seal()) |
| 125 | + for _, tt := range tests { |
| 126 | + t.Run(tt.name, func(t *testing.T) { |
| 127 | + t.Parallel() |
| 128 | + |
| 129 | + err := Changeset{}.VerifyPreconditions(env, tt.input) |
| 130 | + require.EqualError(t, err, tt.wantErr) |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func TestChangeset_VerifyPreconditions_unsupportedFamily(t *testing.T) { |
| 136 | + t.Parallel() |
| 137 | + |
| 138 | + err := Changeset{}.VerifyPreconditions( |
| 139 | + testEnvironment(t, datastore.NewMemoryDataStore().Seal()), |
| 140 | + Input{ |
| 141 | + Cfg: Config{ |
| 142 | + GrantsByChain: map[uint64][]RoleGrant{ |
| 143 | + chainselectors.APTOS_MAINNET.Selector: {{ |
| 144 | + Role: mcmssdk.TimelockRoleProposer, |
| 145 | + Addresses: []string{"0x0000000000000000000000000000000000000001"}, |
| 146 | + }}, |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, |
| 150 | + ) |
| 151 | + require.EqualError(t, err, `mcms grant-role: no sequence registered for family "aptos" (none registered)`) |
| 152 | +} |
| 153 | + |
| 154 | +func TestChangeset_Apply_unsupportedFamily(t *testing.T) { |
| 155 | + t.Parallel() |
| 156 | + |
| 157 | + _, err := Changeset{}.Apply(cldf.Environment{}, Input{ |
| 158 | + Cfg: Config{ |
| 159 | + GrantsByChain: map[uint64][]RoleGrant{ |
| 160 | + chainselectors.APTOS_MAINNET.Selector: {{ |
| 161 | + Role: mcmssdk.TimelockRoleProposer, |
| 162 | + Addresses: []string{"0x0000000000000000000000000000000000000001"}, |
| 163 | + }}, |
| 164 | + }, |
| 165 | + }, |
| 166 | + }) |
| 167 | + require.EqualError(t, err, fmt.Sprintf(`chain selector %d: mcms grant-role: no sequence registered for family "aptos" (none registered)`, chainselectors.APTOS_MAINNET.Selector)) |
| 168 | +} |
| 169 | + |
| 170 | +func TestBuildOutput(t *testing.T) { |
| 171 | + t.Parallel() |
| 172 | + |
| 173 | + env := testEnvironment(t, datastore.NewMemoryDataStore().Seal()) |
| 174 | + |
| 175 | + t.Run("success without MCMS", func(t *testing.T) { |
| 176 | + t.Parallel() |
| 177 | + |
| 178 | + out, err := buildOutput(env, nil, sequenceutils.OnChainOutput{}, nil) |
| 179 | + require.NoError(t, err) |
| 180 | + require.NotNil(t, out.DataStore) |
| 181 | + }) |
| 182 | + |
| 183 | + t.Run("returns partial output on sequence error", func(t *testing.T) { |
| 184 | + t.Parallel() |
| 185 | + |
| 186 | + out, err := buildOutput(env, nil, sequenceutils.OnChainOutput{}, errors.New("sequence failed")) |
| 187 | + require.EqualError(t, err, "sequence failed") |
| 188 | + require.NotNil(t, out.DataStore) |
| 189 | + }) |
| 190 | +} |
0 commit comments