|
| 1 | +package firedrill |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "slices" |
| 7 | + |
| 8 | + chainselectors "github.com/smartcontractkit/chain-selectors" |
| 9 | + cldf_chain "github.com/smartcontractkit/chainlink-deployments-framework/chain" |
| 10 | + "github.com/smartcontractkit/chainlink-deployments-framework/changeset/sequenceutils" |
| 11 | + cldfdatastore "github.com/smartcontractkit/chainlink-deployments-framework/datastore" |
| 12 | + cldf "github.com/smartcontractkit/chainlink-deployments-framework/deployment" |
| 13 | +) |
| 14 | + |
| 15 | +var _ cldf.ChangeSetV2[Input] = Changeset{} |
| 16 | + |
| 17 | +// Changeset creates an MCMS signing fire-drill proposal with noop operations per chain. |
| 18 | +// It exercises signing and execution pipelines without mutating on-chain configuration. |
| 19 | +type Changeset struct{} |
| 20 | + |
| 21 | +// ResolvedSelectors returns the chain selectors VerifyPreconditions and Apply will use. |
| 22 | +// When cfg.Selectors is empty, it defaults to every Solana chain in the environment followed by every EVM chain. |
| 23 | +func (cfg Config) ResolvedSelectors(e cldf.Environment) []uint64 { |
| 24 | + return resolvedSelectors(e, cfg.Selectors) |
| 25 | +} |
| 26 | + |
| 27 | +func (Changeset) VerifyPreconditions(e cldf.Environment, input Input) error { |
| 28 | + if e.DataStore == nil { |
| 29 | + return errors.New("datastore is required for MCMS fire drill") |
| 30 | + } |
| 31 | + if input.MCMS == nil { |
| 32 | + return errors.New("MCMS timelock proposal input is required") |
| 33 | + } |
| 34 | + if err := input.MCMS.Validate(); err != nil { |
| 35 | + return fmt.Errorf("invalid MCMS timelock proposal input: %w", err) |
| 36 | + } |
| 37 | + |
| 38 | + selectors := resolvedSelectors(e, input.Cfg.Selectors) |
| 39 | + if len(selectors) == 0 { |
| 40 | + return errors.New("no chain selectors resolved for MCMS fire drill") |
| 41 | + } |
| 42 | + |
| 43 | + byFamily := make(map[string][]ChainInput) |
| 44 | + for _, chainSelector := range selectors { |
| 45 | + family, err := chainselectors.GetSelectorFamily(chainSelector) |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + byFamily[family] = append(byFamily[family], ChainInput{ |
| 50 | + ChainSelector: chainSelector, |
| 51 | + MCMS: *input.MCMS, |
| 52 | + }) |
| 53 | + } |
| 54 | + |
| 55 | + families := make([]string, 0, len(byFamily)) |
| 56 | + for family := range byFamily { |
| 57 | + families = append(families, family) |
| 58 | + } |
| 59 | + slices.Sort(families) |
| 60 | + |
| 61 | + for _, family := range families { |
| 62 | + if err := verifyForFamily(family, e, byFamily[family]); err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +func (Changeset) Apply(e cldf.Environment, input Input) (cldf.ChangesetOutput, error) { |
| 71 | + if input.MCMS == nil { |
| 72 | + return cldf.ChangesetOutput{}, errors.New("MCMS timelock proposal input is required") |
| 73 | + } |
| 74 | + |
| 75 | + selectors := resolvedSelectors(e, input.Cfg.Selectors) |
| 76 | + if len(selectors) == 0 { |
| 77 | + return cldf.ChangesetOutput{}, errors.New("no chain selectors resolved for MCMS fire drill") |
| 78 | + } |
| 79 | + |
| 80 | + deps := Deps{ |
| 81 | + BlockChains: e.BlockChains, |
| 82 | + DataStore: e.DataStore, |
| 83 | + } |
| 84 | + |
| 85 | + var agg sequenceutils.OnChainOutput |
| 86 | + |
| 87 | + for _, chainSelector := range selectors { |
| 88 | + seq, seqErr := SequenceForChainSelector(chainSelector) |
| 89 | + if seqErr != nil { |
| 90 | + return buildOutput(e, input.MCMS, agg, fmt.Errorf("chain selector %d: %w", chainSelector, seqErr)) |
| 91 | + } |
| 92 | + |
| 93 | + var mergeErr error |
| 94 | + agg, mergeErr = sequenceutils.ExecuteOnChainSequenceAndMerge( |
| 95 | + e.OperationsBundle, |
| 96 | + deps, |
| 97 | + seq, |
| 98 | + ChainInput{ |
| 99 | + ChainSelector: chainSelector, |
| 100 | + MCMS: *input.MCMS, |
| 101 | + }, |
| 102 | + agg, |
| 103 | + ) |
| 104 | + if mergeErr != nil { |
| 105 | + return buildOutput(e, input.MCMS, agg, mergeErr) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return buildOutput(e, input.MCMS, agg, nil) |
| 110 | +} |
| 111 | + |
| 112 | +func buildOutput( |
| 113 | + e cldf.Environment, |
| 114 | + mcmsInput *cldf.MCMSTimelockProposalInput, |
| 115 | + agg sequenceutils.OnChainOutput, |
| 116 | + err error, |
| 117 | +) (cldf.ChangesetOutput, error) { |
| 118 | + ds := cldfdatastore.NewMemoryDataStore() |
| 119 | + if e.DataStore != nil { |
| 120 | + if mergeErr := ds.Merge(e.DataStore); mergeErr != nil { |
| 121 | + return cldf.ChangesetOutput{}, fmt.Errorf("merge environment datastore: %w", mergeErr) |
| 122 | + } |
| 123 | + } |
| 124 | + if metaErr := ds.WriteMetadata(agg.Metadata); metaErr != nil { |
| 125 | + return cldf.ChangesetOutput{DataStore: ds}, |
| 126 | + fmt.Errorf("write metadata to datastore: %w", metaErr) |
| 127 | + } |
| 128 | + |
| 129 | + partialOutput := cldf.ChangesetOutput{DataStore: ds} |
| 130 | + if err != nil { |
| 131 | + return partialOutput, err |
| 132 | + } |
| 133 | + |
| 134 | + builder := cldf.NewOutputBuilder(e, ds). |
| 135 | + WithTimelockProposal(*mcmsInput, agg.BatchOps) |
| 136 | + |
| 137 | + out, buildErr := builder.Build() |
| 138 | + if buildErr != nil { |
| 139 | + return out, fmt.Errorf("build changeset output: %w", buildErr) |
| 140 | + } |
| 141 | + |
| 142 | + if len(out.MCMSTimelockProposals) > 0 { |
| 143 | + e.Logger.Infow("MCMS fire drill proposal created", "proposalCount", len(out.MCMSTimelockProposals)) |
| 144 | + } |
| 145 | + |
| 146 | + return out, nil |
| 147 | +} |
| 148 | + |
| 149 | +func resolvedSelectors(e cldf.Environment, selectors []uint64) []uint64 { |
| 150 | + if len(selectors) > 0 { |
| 151 | + return selectors |
| 152 | + } |
| 153 | + |
| 154 | + solSelectors := e.BlockChains.ListChainSelectors(cldf_chain.WithFamily(chainselectors.FamilySolana)) |
| 155 | + evmSelectors := e.BlockChains.ListChainSelectors(cldf_chain.WithFamily(chainselectors.FamilyEVM)) |
| 156 | + out := make([]uint64, 0, len(solSelectors)+len(evmSelectors)) |
| 157 | + out = append(out, solSelectors...) |
| 158 | + out = append(out, evmSelectors...) |
| 159 | + |
| 160 | + return out |
| 161 | +} |
0 commit comments