-
Notifications
You must be signed in to change notification settings - Fork 0
feat: grant role timelock - changesets and EVM sequence #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ecPablo
wants to merge
3
commits into
main
Choose a base branch
from
ecpablo/grant-role-timelock
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| // Package all blank-imports built-in MCMS grant-role families and readers. | ||
| package all | ||
|
|
||
| import ( | ||
| _ "github.com/smartcontractkit/cld-changesets/mcms/evm/grant-role" | ||
| _ "github.com/smartcontractkit/cld-changesets/mcms/evm/readers" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| package grantrole | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "slices" | ||
|
|
||
| "github.com/smartcontractkit/chainlink-deployments-framework/changeset/sequenceutils" | ||
| cldfdatastore "github.com/smartcontractkit/chainlink-deployments-framework/datastore" | ||
| cldf "github.com/smartcontractkit/chainlink-deployments-framework/deployment" | ||
|
|
||
| "github.com/smartcontractkit/cld-changesets/internal/maputil" | ||
| ) | ||
|
|
||
| var _ cldf.ChangeSetV2[Input] = Changeset{} | ||
|
|
||
| // Changeset grants RBACTimelock roles across configured chains. | ||
| type Changeset struct{} | ||
|
|
||
| func (Changeset) VerifyPreconditions(env cldf.Environment, input Input) error { | ||
| if env.DataStore == nil { | ||
| return errors.New("datastore is required for grant-role") | ||
| } | ||
| if input.MCMS != nil { | ||
| if err := input.MCMS.Validate(); err != nil { | ||
| return fmt.Errorf("invalid MCMS timelock proposal input: %w", err) | ||
| } | ||
| } | ||
| if len(input.Cfg.GrantsByChain) == 0 { | ||
| return errors.New("no role grants provided") | ||
| } | ||
| if err := validateGrants(input.Cfg.GrantsByChain); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| byFamily, err := groupByFamily(input) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| families := make([]string, 0, len(byFamily)) | ||
| for family := range byFamily { | ||
| families = append(families, family) | ||
| } | ||
| slices.Sort(families) | ||
|
|
||
| for _, family := range families { | ||
| if err := Registry.VerifyForFamily(family, env, byFamily[family]); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (Changeset) Apply(env cldf.Environment, input Input) (cldf.ChangesetOutput, error) { | ||
| deps := Deps{ | ||
| BlockChains: env.BlockChains, | ||
| DataStore: env.DataStore, | ||
| } | ||
|
|
||
| var agg sequenceutils.OnChainOutput | ||
| for _, chainSelector := range maputil.SortedMapKeys(input.Cfg.GrantsByChain) { | ||
| grants := input.Cfg.GrantsByChain[chainSelector] | ||
|
|
||
| seq, seqErr := Registry.SequenceForChainSelector(chainSelector) | ||
| if seqErr != nil { | ||
| return buildOutput(env, input.MCMS, agg, fmt.Errorf("chain selector %d: %w", chainSelector, seqErr)) | ||
| } | ||
|
|
||
| var mergeErr error | ||
| agg, mergeErr = sequenceutils.ExecuteOnChainSequenceAndMerge( | ||
| env.OperationsBundle, | ||
| deps, | ||
| seq, | ||
| SeqInput{ | ||
| ChainSelector: chainSelector, | ||
| Grants: grants, | ||
| MCMS: input.MCMS, | ||
| GasBoostConfig: input.Cfg.GasBoostConfig, | ||
| }, | ||
| agg, | ||
| ) | ||
| if mergeErr != nil { | ||
| return buildOutput(env, input.MCMS, agg, mergeErr) | ||
| } | ||
| } | ||
|
|
||
| return buildOutput(env, input.MCMS, agg, nil) | ||
| } | ||
|
|
||
| func buildOutput( | ||
| env cldf.Environment, | ||
| mcmsInput *cldf.MCMSTimelockProposalInput, | ||
| agg sequenceutils.OnChainOutput, | ||
| err error, | ||
| ) (cldf.ChangesetOutput, error) { | ||
| ds := cldfdatastore.NewMemoryDataStore() | ||
| if metaErr := ds.WriteMetadata(agg.Metadata); metaErr != nil { | ||
| return cldf.ChangesetOutput{DataStore: ds}, | ||
| fmt.Errorf("write metadata to datastore: %w", metaErr) | ||
| } | ||
|
|
||
| partialOutput := cldf.ChangesetOutput{DataStore: ds} | ||
| if err != nil { | ||
| return partialOutput, err | ||
| } | ||
|
|
||
| builder := cldf.NewOutputBuilder(env, ds) | ||
| if mcmsInput != nil { | ||
| builder = builder.WithTimelockProposal(*mcmsInput, agg.BatchOps) | ||
| } | ||
|
|
||
| out, buildErr := builder.Build() | ||
| if buildErr != nil { | ||
| return out, fmt.Errorf("build changeset output: %w", buildErr) | ||
| } | ||
|
|
||
| if mcmsInput != nil && len(out.MCMSTimelockProposals) > 0 { | ||
| env.Logger.Infow("GrantRole proposal created", "proposalCount", len(out.MCMSTimelockProposals)) | ||
| } | ||
|
|
||
| return out, nil | ||
| } | ||
|
|
||
| func validateGrants(grantsByChain map[uint64][]RoleGrant) error { | ||
| for chainSelector, grants := range grantsByChain { | ||
| if len(grants) == 0 { | ||
| return fmt.Errorf("chain %d: no role grants provided", chainSelector) | ||
| } | ||
| seen := make(map[string]struct{}) | ||
| for grantIdx, grant := range grants { | ||
| if !grant.Role.Valid() { | ||
| return fmt.Errorf("chain %d grants[%d]: unsupported timelock role %s", chainSelector, grantIdx, grant.Role.String()) | ||
| } | ||
| if len(grant.Addresses) == 0 { | ||
| return fmt.Errorf("chain %d grants[%d]: no addresses provided", chainSelector, grantIdx) | ||
| } | ||
| for addrIdx, addr := range grant.Addresses { | ||
| if addr == "" { | ||
| return fmt.Errorf("chain %d grants[%d].addresses[%d]: address must not be empty", chainSelector, grantIdx, addrIdx) | ||
| } | ||
| key := grant.Role.String() + ":" + addr | ||
| if _, ok := seen[key]; ok { | ||
| return fmt.Errorf("chain %d grants[%d].addresses[%d]: duplicate grant for role %s and address %s", | ||
| chainSelector, grantIdx, addrIdx, grant.Role.String(), addr) | ||
| } | ||
| seen[key] = struct{}{} | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| package grantrole | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| chainselectors "github.com/smartcontractkit/chain-selectors" | ||
| cldf_chain "github.com/smartcontractkit/chainlink-deployments-framework/chain" | ||
| "github.com/smartcontractkit/chainlink-deployments-framework/changeset/sequenceutils" | ||
| "github.com/smartcontractkit/chainlink-deployments-framework/datastore" | ||
| cldf "github.com/smartcontractkit/chainlink-deployments-framework/deployment" | ||
| "github.com/smartcontractkit/chainlink-deployments-framework/offchain/ocr" | ||
| "github.com/smartcontractkit/chainlink-deployments-framework/pkg/logger" | ||
| mcmssdk "github.com/smartcontractkit/mcms/sdk" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func testEnvironment(t *testing.T, ds datastore.DataStore) cldf.Environment { | ||
| t.Helper() | ||
|
|
||
| return *cldf.NewEnvironment( | ||
| "test", | ||
| logger.Test(t), | ||
| nil, | ||
| ds, | ||
| nil, | ||
| nil, | ||
| func() context.Context { return t.Context() }, | ||
| ocr.OCRSecrets{}, | ||
| cldf_chain.NewBlockChains(nil), | ||
| ) | ||
| } | ||
|
|
||
| func TestChangeset_VerifyPreconditions_NoDatastore(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| input := Input{ | ||
| Cfg: Config{ | ||
| GrantsByChain: map[uint64][]RoleGrant{ | ||
| chainselectors.TEST_90000001.Selector: {{ | ||
| Role: mcmssdk.TimelockRoleProposer, | ||
| Addresses: []string{"0x0000000000000000000000000000000000000001"}, | ||
| }}, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| err := Changeset{}.VerifyPreconditions(testEnvironment(t, nil), input) | ||
| require.EqualError(t, err, "datastore is required for grant-role") | ||
| } | ||
|
|
||
| func TestChangeset_VerifyPreconditions_InvalidInput(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| validAddress := "0x0000000000000000000000000000000000000001" | ||
| tests := []struct { | ||
| name string | ||
| input Input | ||
| wantErr string | ||
| }{ | ||
| { | ||
| name: "no grants", | ||
| input: Input{}, | ||
| wantErr: "no role grants provided", | ||
| }, | ||
| { | ||
| name: "empty grants for chain", | ||
| input: Input{Cfg: Config{GrantsByChain: map[uint64][]RoleGrant{ | ||
| chainselectors.TEST_90000001.Selector: {}, | ||
| }}}, | ||
| wantErr: fmt.Sprintf("chain %d: no role grants provided", chainselectors.TEST_90000001.Selector), | ||
| }, | ||
| { | ||
| name: "unsupported role", | ||
| input: Input{Cfg: Config{GrantsByChain: map[uint64][]RoleGrant{ | ||
| chainselectors.TEST_90000001.Selector: {{Role: mcmssdk.TimelockRole(99), Addresses: []string{validAddress}}}, | ||
| }}}, | ||
| wantErr: fmt.Sprintf("chain %d grants[0]: unsupported timelock role Unknown", chainselectors.TEST_90000001.Selector), | ||
| }, | ||
| { | ||
| name: "no addresses", | ||
| input: Input{Cfg: Config{GrantsByChain: map[uint64][]RoleGrant{ | ||
| chainselectors.TEST_90000001.Selector: {{Role: mcmssdk.TimelockRoleProposer}}, | ||
| }}}, | ||
| wantErr: fmt.Sprintf("chain %d grants[0]: no addresses provided", chainselectors.TEST_90000001.Selector), | ||
| }, | ||
| { | ||
| name: "empty address", | ||
| input: Input{Cfg: Config{GrantsByChain: map[uint64][]RoleGrant{ | ||
| chainselectors.TEST_90000001.Selector: {{ | ||
| Role: mcmssdk.TimelockRoleProposer, | ||
| Addresses: []string{""}, | ||
| }}, | ||
| }}}, | ||
| wantErr: fmt.Sprintf("chain %d grants[0].addresses[0]: address must not be empty", chainselectors.TEST_90000001.Selector), | ||
| }, | ||
| { | ||
| name: "duplicate grant", | ||
| input: Input{Cfg: Config{GrantsByChain: map[uint64][]RoleGrant{ | ||
| chainselectors.TEST_90000001.Selector: {{ | ||
| Role: mcmssdk.TimelockRoleProposer, | ||
| Addresses: []string{validAddress, validAddress}, | ||
| }}, | ||
| }}}, | ||
| wantErr: fmt.Sprintf("chain %d grants[0].addresses[1]: duplicate grant for role Proposer and address 0x0000000000000000000000000000000000000001", chainselectors.TEST_90000001.Selector), | ||
| }, | ||
| { | ||
| name: "invalid MCMS input", | ||
| input: Input{ | ||
| MCMS: &cldf.MCMSTimelockProposalInput{}, | ||
| Cfg: Config{GrantsByChain: map[uint64][]RoleGrant{ | ||
| chainselectors.TEST_90000001.Selector: {{ | ||
| Role: mcmssdk.TimelockRoleProposer, | ||
| Addresses: []string{validAddress}, | ||
| }}, | ||
| }}, | ||
| }, | ||
| wantErr: `invalid MCMS timelock proposal input: invalid MCMS timelock proposal input: invalid timelock action ""`, | ||
| }, | ||
| } | ||
|
|
||
| env := testEnvironment(t, datastore.NewMemoryDataStore().Seal()) | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| err := Changeset{}.VerifyPreconditions(env, tt.input) | ||
| require.EqualError(t, err, tt.wantErr) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestChangeset_VerifyPreconditions_unsupportedFamily(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| err := Changeset{}.VerifyPreconditions( | ||
| testEnvironment(t, datastore.NewMemoryDataStore().Seal()), | ||
| Input{ | ||
| Cfg: Config{ | ||
| GrantsByChain: map[uint64][]RoleGrant{ | ||
| chainselectors.APTOS_MAINNET.Selector: {{ | ||
| Role: mcmssdk.TimelockRoleProposer, | ||
| Addresses: []string{"0x0000000000000000000000000000000000000001"}, | ||
| }}, | ||
| }, | ||
| }, | ||
| }, | ||
| ) | ||
| require.EqualError(t, err, `mcms grant-role: no sequence registered for family "aptos" (none registered)`) | ||
| } | ||
|
|
||
| func TestChangeset_Apply_unsupportedFamily(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| _, err := Changeset{}.Apply(cldf.Environment{}, Input{ | ||
| Cfg: Config{ | ||
| GrantsByChain: map[uint64][]RoleGrant{ | ||
| chainselectors.APTOS_MAINNET.Selector: {{ | ||
| Role: mcmssdk.TimelockRoleProposer, | ||
| Addresses: []string{"0x0000000000000000000000000000000000000001"}, | ||
| }}, | ||
| }, | ||
| }, | ||
| }) | ||
| require.EqualError(t, err, fmt.Sprintf(`chain selector %d: mcms grant-role: no sequence registered for family "aptos" (none registered)`, chainselectors.APTOS_MAINNET.Selector)) | ||
| } | ||
|
|
||
| func TestBuildOutput(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| env := testEnvironment(t, datastore.NewMemoryDataStore().Seal()) | ||
|
|
||
| t.Run("success without MCMS", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| out, err := buildOutput(env, nil, sequenceutils.OnChainOutput{}, nil) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, out.DataStore) | ||
| }) | ||
|
|
||
| t.Run("returns partial output on sequence error", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| out, err := buildOutput(env, nil, sequenceutils.OnChainOutput{}, errors.New("sequence failed")) | ||
| require.EqualError(t, err, "sequence failed") | ||
| require.NotNil(t, out.DataStore) | ||
| }) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could use map.Slice with map.Keys