-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add mcms/utils package with buildInspector and buildConverters #578
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
Draft
gustavogama-cll
wants to merge
1
commit into
main
Choose a base branch
from
ggama/wip-feat-utils-package
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.
Draft
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
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,35 @@ | ||
| package utils | ||
|
|
||
| import ( | ||
| "errors" | ||
|
|
||
| "github.com/smartcontractkit/mcms" | ||
| "github.com/smartcontractkit/mcms/sdk/aptos" | ||
| "github.com/smartcontractkit/mcms/types" | ||
| ) | ||
|
|
||
| func AptosRoleFromProposal(proposal *mcms.TimelockProposal) (aptos.TimelockRole, error) { | ||
| if proposal == nil { | ||
| return 0, errors.New("aptos timelock proposal is needed") | ||
| } | ||
|
|
||
| role, err := AptosRoleFromAction(proposal.Action) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
|
|
||
| return role, nil | ||
| } | ||
|
|
||
| func AptosRoleFromAction(action types.TimelockAction) (aptos.TimelockRole, error) { | ||
| switch action { | ||
| case types.TimelockActionBypass: | ||
| return aptos.TimelockRoleBypasser, nil | ||
| case types.TimelockActionSchedule: | ||
| return aptos.TimelockRoleProposer, nil | ||
| case types.TimelockActionCancel: | ||
| return aptos.TimelockRoleCanceller, nil | ||
| default: | ||
| return 0, errors.New("unknown timelock action") | ||
| } | ||
| } | ||
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,46 @@ | ||
| package utils | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| chainsel "github.com/smartcontractkit/chain-selectors" | ||
|
|
||
| "github.com/smartcontractkit/mcms" | ||
| "github.com/smartcontractkit/mcms/sdk" | ||
| "github.com/smartcontractkit/mcms/sdk/aptos" | ||
| "github.com/smartcontractkit/mcms/sdk/evm" | ||
| "github.com/smartcontractkit/mcms/sdk/solana" | ||
| "github.com/smartcontractkit/mcms/sdk/sui" | ||
| "github.com/smartcontractkit/mcms/types" | ||
| ) | ||
|
|
||
| // BuildConvertersForTimelockProposal constructs a map of chain selectors to their respective timelock converters based on the provided timelock proposal. | ||
| func BuildConvertersForTimelockProposal( | ||
| proposal mcms.TimelockProposal, | ||
| ) (map[types.ChainSelector]sdk.TimelockConverter, error) { | ||
| converters := make(map[types.ChainSelector]sdk.TimelockConverter) | ||
| for chainSelector := range proposal.ChainMetadata { | ||
| fam, err := types.GetChainSelectorFamily(chainSelector) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error getting chain family: %w", err) | ||
| } | ||
|
|
||
| var converter sdk.TimelockConverter | ||
| switch fam { | ||
| case chainsel.FamilyEVM: | ||
| converter = &evm.TimelockConverter{} | ||
| case chainsel.FamilySolana: | ||
| converter = solana.TimelockConverter{} | ||
| case chainsel.FamilyAptos: | ||
| converter = aptos.NewTimelockConverter() | ||
| case chainsel.FamilySui: | ||
| converter, _ = sui.NewTimelockConverter() | ||
| default: | ||
| return nil, fmt.Errorf("unsupported chain family %s", fam) | ||
| } | ||
|
|
||
| converters[chainSelector] = converter | ||
| } | ||
|
|
||
| return converters, 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,160 @@ | ||
| package utils | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| chainsel "github.com/smartcontractkit/chain-selectors" | ||
|
|
||
| "github.com/smartcontractkit/mcms" | ||
| "github.com/smartcontractkit/mcms/sdk" | ||
| "github.com/smartcontractkit/mcms/sdk/aptos" | ||
| "github.com/smartcontractkit/mcms/sdk/evm" | ||
| "github.com/smartcontractkit/mcms/sdk/solana" | ||
| "github.com/smartcontractkit/mcms/sdk/sui" | ||
| "github.com/smartcontractkit/mcms/types" | ||
| ) | ||
|
|
||
| // BuildInspectorsForTimelockProposal() gets a map of inspectors for the given proposal | ||
| func BuildInspectorsForTimelockProposal( | ||
| proposal mcms.TimelockProposal, | ||
| chainClientProvider BlockChainClientProvider, | ||
| ) (map[types.ChainSelector]sdk.Inspector, error) { | ||
| inspectors := map[types.ChainSelector]sdk.Inspector{} | ||
| for chainSelector := range proposal.ChainMetadata { | ||
| inspector, err := buildInspectorForChainSelector(proposal, chainSelector, chainClientProvider) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to build inspector for chain selector %d: %w", chainSelector, err) | ||
| } | ||
| inspectors[chainSelector] = inspector | ||
| } | ||
|
|
||
| return inspectors, nil | ||
| } | ||
|
|
||
| func buildInspectorForChainSelector( | ||
| proposal mcms.TimelockProposal, | ||
| chainSelector types.ChainSelector, | ||
| chainClientProvider BlockChainClientProvider, | ||
| ) (sdk.Inspector, error) { | ||
| client, err := chainClientProvider.GetClient(uint64(chainSelector)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get blockchain client for chain selector %d: %w", chainSelector, err) | ||
| } | ||
| inspector, err := getInspectorFromChainSelector(client, chainSelector, proposal.Action) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get inspector for chain selector %d: %w", chainSelector, err) | ||
| } | ||
|
|
||
| return inspector, nil | ||
| } | ||
|
|
||
| // BuildInspectorsForChainSelectors allows us to implement convenience wrappers for | ||
| // the features we currently have to hide behind the "inspector" interface. | ||
| // For instance, GetOpCount. | ||
| // Ideally, we'd separate the mcms.TimelockProposal interface from the implementation, | ||
| // which could allow use to make GetOpCount a method of the TimelockProposal type itself (right now | ||
| // we can't because of import cycles) | ||
| func GetOpCount( | ||
| ctx context.Context, | ||
| proposal mcms.TimelockProposal, | ||
| chainSelector types.ChainSelector, | ||
| chainClientProvider BlockChainClientProvider, | ||
| ) (uint64, error) { | ||
| inspector, err := buildInspectorForChainSelector(proposal, chainSelector, chainClientProvider) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("failed to build inspector for chain selector %d: %w", chainSelector, err) | ||
| } | ||
|
|
||
| chainMetadata, ok := proposal.ChainMetadata[chainSelector] | ||
| if !ok { | ||
| return 0, fmt.Errorf("failed to find chain metadata for chain selector %d: %w", chainSelector, err) | ||
| } | ||
|
|
||
| return inspector.GetOpCount(ctx, chainMetadata.MCMAddress) | ||
| } | ||
|
|
||
| // getInspectorFromChainSelector returns an inspector for the given chain selector and chain clients | ||
| func getInspectorFromChainSelector( | ||
| chainClient any, selector types.ChainSelector, action types.TimelockAction, | ||
| ) (sdk.Inspector, error) { | ||
| fam, err := types.GetChainSelectorFamily(selector) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error getting chainClient family: %w", err) | ||
| } | ||
|
|
||
| var inspector sdk.Inspector | ||
| switch fam { | ||
| case chainsel.FamilyEVM: | ||
| evmClient, ok := chainClient.(evm.ContractDeployBackend) | ||
| if !ok { | ||
| return nil, fmt.Errorf("invalid EVM client type for selector %d", selector) | ||
| } | ||
| inspector = evm.NewInspector(evmClient) | ||
|
|
||
| case chainsel.FamilySolana: | ||
| solanaClient, ok := chainClient.(solana.RPCClient) | ||
| if !ok { | ||
| return nil, fmt.Errorf("invalid Solana client type for selector %d", selector) | ||
| } | ||
| inspector = solana.NewInspector(solanaClient) | ||
|
|
||
| case chainsel.FamilyAptos: | ||
| role, rerr := AptosRoleFromAction(action) | ||
| if rerr != nil { | ||
| return nil, fmt.Errorf("error getting aptos role from proposal: %w", rerr) | ||
| } | ||
| aptosClient, ok := chainClient.(aptos.RPCClient) | ||
| if !ok { | ||
| return nil, fmt.Errorf("invalid Aptos client type for selector %d", selector) | ||
| } | ||
| inspector = aptos.NewInspector(aptosClient, role) | ||
|
|
||
| case chainsel.FamilySui: | ||
| suiClient, ok := chainClient.(sui.RPCClient) | ||
| if !ok { | ||
| return nil, fmt.Errorf("invalid Sui client type for selector %d", selector) | ||
| } | ||
| // FIXME: where do we get the bindSigner and mcmsPackageID set as nil and "" below? | ||
| inspector, err = sui.NewInspector(suiClient, nil, "", sui.TimelockRoleFromAction(action)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create Sui inspector for selector %d", selector) | ||
| } | ||
|
|
||
| default: | ||
| return nil, fmt.Errorf("unsupported chain family %s", fam) | ||
| } | ||
|
|
||
| return inspector, nil | ||
| } | ||
|
|
||
| // This requires an addition to CLDf which we'd need to negotiate with the CLD team: | ||
| // diff --git i/chain/blockchain.go w/chain/blockchain.go | ||
| // index e80aac9..9a3b8b6 100644 | ||
| // --- i/chain/blockchain.go | ||
| // +++ w/chain/blockchain.go | ||
| // | ||
| // @@ -82,6 +82,18 @@ func (b BlockChains) GetBySelector(selector uint64) (BlockChain, error) { | ||
| // return nil, ErrBlockChainNotFound | ||
| // } | ||
| // | ||
| // +// GetClient returns the rpc client for the given selector as an opaque type | ||
| // +func (b BlockChains) GetClient(selector uint64) (any, error) { | ||
| // + chain, ok := b.chains[selector] | ||
| // + if !ok { | ||
| // + return nil, ErrBlockChainNotFound | ||
| // + } | ||
| // + | ||
| // + // FIXME: needs to be tested; | ||
| // + // we could also avoid reflection by adding an "RPCClient()" method to the BlockChain interface | ||
| // + // or we could do a "switch (chain.Type()" | ||
| // + return reflect.ValueOf(chain).Elem().FieldByName("Client").Interface(), nil | ||
| // +} | ||
| // + | ||
| // | ||
| // // Exists checks if a chain with the given selector exists. | ||
| // func (b BlockChains) Exists(selector uint64) bool { | ||
| // _, ok := b.chains[selector] | ||
| type BlockChainClientProvider interface { | ||
| GetClient(selector uint64) (any, error) | ||
| } |
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,30 @@ | ||
| package utils | ||
|
Check failure on line 1 in utils/sui.go
|
||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/smartcontractkit/mcms" | ||
| "github.com/smartcontractkit/mcms/sdk/sui" | ||
| "github.com/smartcontractkit/mcms/types" | ||
| ) | ||
|
|
||
| func SuiMetadataFromProposal(selector types.ChainSelector, proposal *mcms.TimelockProposal) (sui.AdditionalFieldsMetadata, error) { | ||
| if proposal == nil { | ||
| return sui.AdditionalFieldsMetadata{}, errors.New("sui timelock proposal is needed") | ||
| } | ||
|
|
||
| var metadata sui.AdditionalFieldsMetadata | ||
| err := json.Unmarshal([]byte(proposal.ChainMetadata[selector].AdditionalFields), &metadata) | ||
| if err != nil { | ||
| return sui.AdditionalFieldsMetadata{}, fmt.Errorf("error unmarshaling sui chain metadata: %w", err) | ||
| } | ||
|
|
||
| err = metadata.Validate() | ||
| if err != nil { | ||
| return sui.AdditionalFieldsMetadata{}, fmt.Errorf("error validating sui chain metadata: %w", err) | ||
| } | ||
|
|
||
| return metadata, nil | ||
| } | ||
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.
I'm not a fan of
utilspackages but I couldn't come up with anything better and this seems to be common enough at CLL that nobody will care.