|
| 1 | +package transfertomcms |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "slices" |
| 7 | + |
| 8 | + chainselectors "github.com/smartcontractkit/chain-selectors" |
| 9 | + "github.com/smartcontractkit/chainlink-deployments-framework/changeset/sequenceutils" |
| 10 | + cldfdatastore "github.com/smartcontractkit/chainlink-deployments-framework/datastore" |
| 11 | + cldf "github.com/smartcontractkit/chainlink-deployments-framework/deployment" |
| 12 | + |
| 13 | + "github.com/smartcontractkit/cld-changesets/internal/maputil" |
| 14 | +) |
| 15 | + |
| 16 | +var _ cldf.ChangeSetV2[Input] = Changeset{} |
| 17 | + |
| 18 | +// Changeset transfers ownable contract ownership to the MCMS timelock. |
| 19 | +// transferOwnership is sent on-chain by the deployer; acceptOwnership is |
| 20 | +// returned as batch operations in an MCMS timelock proposal. MCMS refs are |
| 21 | +// resolved from the datastore only. |
| 22 | +type Changeset struct{} |
| 23 | + |
| 24 | +func (Changeset) VerifyPreconditions(env cldf.Environment, input Input) error { |
| 25 | + if env.DataStore == nil { |
| 26 | + return errors.New("datastore is required for transfer to MCMS") |
| 27 | + } |
| 28 | + if input.MCMS == nil { |
| 29 | + return errors.New("MCMS timelock proposal input is required") |
| 30 | + } |
| 31 | + if err := input.MCMS.Validate(); err != nil { |
| 32 | + return fmt.Errorf("invalid MCMS timelock proposal input: %w", err) |
| 33 | + } |
| 34 | + if len(input.Cfg.ContractsByChain) == 0 { |
| 35 | + return errors.New("no contracts provided") |
| 36 | + } |
| 37 | + |
| 38 | + byFamily, err := groupByFamily(input) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + families := make([]string, 0, len(byFamily)) |
| 44 | + for family := range byFamily { |
| 45 | + families = append(families, family) |
| 46 | + } |
| 47 | + slices.Sort(families) |
| 48 | + |
| 49 | + for _, family := range families { |
| 50 | + if err := VerifyForFamily(family, env, byFamily[family]); err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return nil |
| 56 | +} |
| 57 | + |
| 58 | +func (Changeset) Apply(env cldf.Environment, input Input) (cldf.ChangesetOutput, error) { |
| 59 | + if input.MCMS == nil { |
| 60 | + return cldf.ChangesetOutput{}, errors.New("MCMS timelock proposal input is required") |
| 61 | + } |
| 62 | + |
| 63 | + deps := Deps{ |
| 64 | + BlockChains: env.BlockChains, |
| 65 | + DataStore: env.DataStore, |
| 66 | + } |
| 67 | + |
| 68 | + var agg sequenceutils.OnChainOutput |
| 69 | + |
| 70 | + for _, chainSelector := range maputil.SortedMapKeys(input.Cfg.ContractsByChain) { |
| 71 | + contracts := input.Cfg.ContractsByChain[chainSelector] |
| 72 | + |
| 73 | + seq, seqErr := SequenceForChainSelector(chainSelector) |
| 74 | + if seqErr != nil { |
| 75 | + return buildOutput(env, input.MCMS, agg, fmt.Errorf("chain selector %d: %w", chainSelector, seqErr)) |
| 76 | + } |
| 77 | + |
| 78 | + var mergeErr error |
| 79 | + agg, mergeErr = sequenceutils.ExecuteOnChainSequenceAndMerge( |
| 80 | + env.OperationsBundle, |
| 81 | + deps, |
| 82 | + seq, |
| 83 | + ChainInput{ |
| 84 | + ChainSelector: chainSelector, |
| 85 | + Contracts: contracts, |
| 86 | + OnlyAcceptOwnership: input.Cfg.OnlyAcceptOwnership, |
| 87 | + MCMS: input.MCMS, |
| 88 | + }, |
| 89 | + agg, |
| 90 | + ) |
| 91 | + if mergeErr != nil { |
| 92 | + return buildOutput(env, input.MCMS, agg, mergeErr) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return buildOutput(env, input.MCMS, agg, nil) |
| 97 | +} |
| 98 | + |
| 99 | +func buildOutput( |
| 100 | + env cldf.Environment, |
| 101 | + mcmsInput *cldf.MCMSTimelockProposalInput, |
| 102 | + agg sequenceutils.OnChainOutput, |
| 103 | + err error, |
| 104 | +) (cldf.ChangesetOutput, error) { |
| 105 | + ds := cldfdatastore.NewMemoryDataStore() |
| 106 | + if metaErr := ds.WriteMetadata(agg.Metadata); metaErr != nil { |
| 107 | + return cldf.ChangesetOutput{DataStore: ds}, |
| 108 | + fmt.Errorf("write metadata to datastore: %w", metaErr) |
| 109 | + } |
| 110 | + |
| 111 | + partialOutput := cldf.ChangesetOutput{DataStore: ds} |
| 112 | + if err != nil { |
| 113 | + return partialOutput, err |
| 114 | + } |
| 115 | + |
| 116 | + builder := cldf.NewOutputBuilder(env, ds). |
| 117 | + WithTimelockProposal(*mcmsInput, agg.BatchOps) |
| 118 | + |
| 119 | + out, buildErr := builder.Build() |
| 120 | + if buildErr != nil { |
| 121 | + return out, fmt.Errorf("build changeset output: %w", buildErr) |
| 122 | + } |
| 123 | + |
| 124 | + if len(out.MCMSTimelockProposals) > 0 { |
| 125 | + env.Logger.Infow("Transfer to MCMS proposal created", "proposalCount", len(out.MCMSTimelockProposals)) |
| 126 | + } |
| 127 | + |
| 128 | + return out, nil |
| 129 | +} |
| 130 | + |
| 131 | +func groupByFamily(input Input) (map[string][]ChainInput, error) { |
| 132 | + byFamily := make(map[string][]ChainInput) |
| 133 | + for chainSelector, contracts := range input.Cfg.ContractsByChain { |
| 134 | + if len(contracts) == 0 { |
| 135 | + return nil, fmt.Errorf("chain %d: no contracts provided", chainSelector) |
| 136 | + } |
| 137 | + family, err := chainselectors.GetSelectorFamily(chainSelector) |
| 138 | + if err != nil { |
| 139 | + return nil, fmt.Errorf("chain selector %d: %w", chainSelector, err) |
| 140 | + } |
| 141 | + byFamily[family] = append(byFamily[family], ChainInput{ |
| 142 | + ChainSelector: chainSelector, |
| 143 | + Contracts: contracts, |
| 144 | + OnlyAcceptOwnership: input.Cfg.OnlyAcceptOwnership, |
| 145 | + MCMS: input.MCMS, |
| 146 | + }) |
| 147 | + } |
| 148 | + |
| 149 | + return byFamily, nil |
| 150 | +} |
0 commit comments