|
| 1 | +package mcms |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/spf13/cobra" |
| 11 | + |
| 12 | + "github.com/smartcontractkit/mcms/types" |
| 13 | + |
| 14 | + "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/commands/flags" |
| 15 | + "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/commands/text" |
| 16 | + "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/mcms/proposalanalysis" |
| 17 | + proposalanalysisanalyzer "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/mcms/proposalanalysis/analyzer" |
| 18 | + analysisdecoder "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/mcms/proposalanalysis/decoder" |
| 19 | + analysisrenderer "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/mcms/proposalanalysis/renderer" |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + analyzeProposalV2Short = "Analyze proposal using the v2 analysis framework" |
| 24 | + |
| 25 | + analyzeProposalV2Long = text.LongDesc(` |
| 26 | + Analyzes a proposal and renders a human-readable report using the new |
| 27 | + proposal analysis framework. |
| 28 | +
|
| 29 | + This command only supports timelock proposals and is intended to replace |
| 30 | + the legacy analyze-proposal command. |
| 31 | + `) |
| 32 | + |
| 33 | + analyzeProposalV2Example = text.Examples(` |
| 34 | + # Analyze a proposal and print to stdout |
| 35 | + myapp mcms analyze-proposal-v2 -e staging -p ./proposal.json |
| 36 | +
|
| 37 | + # Analyze and save to a file in markdown format |
| 38 | + myapp mcms analyze-proposal-v2 -e staging -p ./proposal.json -o analysis.md |
| 39 | + `) |
| 40 | +) |
| 41 | + |
| 42 | +type analyzeProposalV2Flags struct { |
| 43 | + environment string |
| 44 | + proposalPath string |
| 45 | + chainSelector uint64 |
| 46 | + output string |
| 47 | + format string |
| 48 | +} |
| 49 | + |
| 50 | +func newAnalyzeProposalV2Cmd(cfg Config) *cobra.Command { |
| 51 | + cmd := &cobra.Command{ |
| 52 | + Use: "analyze-proposal-v2", |
| 53 | + Short: analyzeProposalV2Short, |
| 54 | + Long: analyzeProposalV2Long, |
| 55 | + Example: analyzeProposalV2Example, |
| 56 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 57 | + f := analyzeProposalV2Flags{ |
| 58 | + environment: flags.MustString(cmd.Flags().GetString("environment")), |
| 59 | + proposalPath: flags.MustString(cmd.Flags().GetString("proposal")), |
| 60 | + chainSelector: flags.MustUint64(cmd.Flags().GetUint64("selector")), |
| 61 | + output: flags.MustString(cmd.Flags().GetString("output")), |
| 62 | + format: flags.MustString(cmd.Flags().GetString("format")), |
| 63 | + } |
| 64 | + |
| 65 | + return runAnalyzeProposalV2(cmd, cfg, f) |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + flags.Environment(cmd) |
| 70 | + flags.Proposal(cmd) |
| 71 | + flags.ChainSelector(cmd, false) |
| 72 | + |
| 73 | + cmd.Flags().StringP("output", "o", "", "Output file to write analysis result") |
| 74 | + cmd.Flags().String("format", analysisrenderer.IDMarkdown, "Output format: markdown") |
| 75 | + |
| 76 | + return cmd |
| 77 | +} |
| 78 | + |
| 79 | +func runAnalyzeProposalV2(cmd *cobra.Command, cfg Config, f analyzeProposalV2Flags) error { |
| 80 | + ctx := cmd.Context() |
| 81 | + deps := cfg.deps() |
| 82 | + |
| 83 | + proposalCfg, err := LoadProposalConfig(ctx, cfg.Logger, cfg.Domain, deps, cfg.ProposalContextProvider, |
| 84 | + ProposalFlags{ |
| 85 | + ProposalPath: f.proposalPath, |
| 86 | + ProposalKind: string(types.KindTimelockProposal), |
| 87 | + Environment: f.environment, |
| 88 | + ChainSelector: f.chainSelector, |
| 89 | + }, |
| 90 | + acceptExpiredProposal, |
| 91 | + ) |
| 92 | + if err != nil { |
| 93 | + return fmt.Errorf("error creating config: %w", err) |
| 94 | + } |
| 95 | + |
| 96 | + if proposalCfg.TimelockProposal == nil { |
| 97 | + return errors.New("expected proposal be a timelock proposal") |
| 98 | + } |
| 99 | + |
| 100 | + rendererID, err := normalizeRendererFormat(f.format) |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + |
| 105 | + markdownRenderer, err := analysisrenderer.NewMarkdownRenderer() |
| 106 | + if err != nil { |
| 107 | + return fmt.Errorf("create markdown renderer: %w", err) |
| 108 | + } |
| 109 | + |
| 110 | + engine := proposalanalysis.NewAnalyzerEngine() |
| 111 | + if registerErr := engine.RegisterRenderer(markdownRenderer); registerErr != nil { |
| 112 | + return fmt.Errorf("register renderer: %w", registerErr) |
| 113 | + } |
| 114 | + if analyzerErr := registerProposalAnalyzers(engine, cfg.ProposalAnalyzers); analyzerErr != nil { |
| 115 | + return analyzerErr |
| 116 | + } |
| 117 | + |
| 118 | + var evmABIMappings map[string]string |
| 119 | + var solanaDecoders map[string]analysisdecoder.DecodeInstructionFn |
| 120 | + if proposalCfg.ProposalCtx != nil { |
| 121 | + if proposalCfg.ProposalCtx.GetEVMRegistry() != nil { |
| 122 | + evmABIMappings = proposalCfg.ProposalCtx.GetEVMRegistry().GetAllABIs() |
| 123 | + } |
| 124 | + if proposalCfg.ProposalCtx.GetSolanaDecoderRegistry() != nil { |
| 125 | + solanaDecoders = proposalCfg.ProposalCtx.GetSolanaDecoderRegistry().Decoders() |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + analyzedProposal, err := engine.Run(ctx, proposalanalysis.RunRequest{ |
| 130 | + Domain: cfg.Domain, |
| 131 | + Environment: &proposalCfg.Env, |
| 132 | + DecoderConfig: analysisdecoder.Config{ |
| 133 | + EVMABIMappings: evmABIMappings, |
| 134 | + SolanaDecoders: solanaDecoders, |
| 135 | + }, |
| 136 | + }, proposalCfg.TimelockProposal) |
| 137 | + if err != nil { |
| 138 | + return fmt.Errorf("run analysis engine: %w", err) |
| 139 | + } |
| 140 | + |
| 141 | + var out bytes.Buffer |
| 142 | + if err := engine.RenderTo(&out, rendererID, analysisrenderer.RenderRequest{ |
| 143 | + Domain: cfg.Domain.Key(), |
| 144 | + EnvironmentName: proposalCfg.EnvStr, |
| 145 | + }, analyzedProposal); err != nil { |
| 146 | + return fmt.Errorf("render analysis output: %w", err) |
| 147 | + } |
| 148 | + |
| 149 | + if f.output == "" { |
| 150 | + cmd.Println(out.String()) |
| 151 | + return nil |
| 152 | + } |
| 153 | + |
| 154 | + if err := os.WriteFile(f.output, out.Bytes(), 0o600); err != nil { |
| 155 | + return err |
| 156 | + } |
| 157 | + |
| 158 | + return nil |
| 159 | +} |
| 160 | + |
| 161 | +func registerProposalAnalyzers(engine proposalanalysis.AnalyzerEngine, analyzers []proposalanalysisanalyzer.BaseAnalyzer) error { |
| 162 | + for _, analyzer := range analyzers { |
| 163 | + if err := engine.RegisterAnalyzer(analyzer); err != nil { |
| 164 | + return fmt.Errorf("register proposal analyzer %q: %w", analyzer.ID(), err) |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + return nil |
| 169 | +} |
| 170 | + |
| 171 | +func normalizeRendererFormat(format string) (string, error) { |
| 172 | + switch strings.ToLower(strings.TrimSpace(format)) { |
| 173 | + case "", analysisrenderer.IDMarkdown, "md": |
| 174 | + return analysisrenderer.IDMarkdown, nil |
| 175 | + default: |
| 176 | + return "", fmt.Errorf("unknown format %q: only markdown is supported for analyze-proposal-v2", format) |
| 177 | + } |
| 178 | +} |
0 commit comments