-
Notifications
You must be signed in to change notification settings - Fork 2
docs(mcms): add proposal analysis package usage docs #799
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
graham-chainlink
wants to merge
1
commit into
main
Choose a base branch
from
ggoh/OPT-409/add-proposalanalysis-docs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| Package analyzer defines analyzer interfaces used by the proposal analysis engine. | ||
|
|
||
| # Implementing an Analyzer | ||
|
|
||
| Every analyzer must implement `BaseAnalyzer`: | ||
|
|
||
| - ID() string: unique identifier. | ||
| - Dependencies() []string: analyzer IDs that must run first. | ||
|
|
||
| Then implement one of the scope-specific analyzer interfaces: | ||
|
|
||
| - ProposalAnalyzer | ||
| - BatchOperationAnalyzer | ||
| - CallAnalyzer | ||
| - ParameterAnalyzer | ||
|
|
||
| Example proposal-level analyzer: | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/mcms/proposalanalysis" | ||
| "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/mcms/proposalanalysis/analyzer" | ||
| "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/mcms/proposalanalysis/analyzer/annotation" | ||
| "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/mcms/proposalanalysis/decoder" | ||
| ) | ||
|
|
||
| type RiskAnalyzer struct{} | ||
|
|
||
| func (RiskAnalyzer) ID() string { | ||
| return "risk-analyzer" | ||
| } | ||
|
|
||
| func (RiskAnalyzer) Dependencies() []string { | ||
| return []string{"dependency-analyzer"} | ||
| } | ||
|
|
||
| func (RiskAnalyzer) CanAnalyze( | ||
| ctx context.Context, | ||
| req analyzer.ProposalAnalyzeRequest, | ||
| proposal decoder.DecodedTimelockProposal, | ||
| ) bool { | ||
| _ = ctx | ||
| _ = req | ||
| _ = proposal | ||
| return true | ||
| } | ||
|
|
||
| func (RiskAnalyzer) Analyze( | ||
| ctx context.Context, | ||
| req analyzer.ProposalAnalyzeRequest, | ||
| proposal decoder.DecodedTimelockProposal, | ||
| ) (annotation.Annotations, error) { | ||
| _ = ctx | ||
| _ = req | ||
| _ = proposal | ||
|
|
||
| return annotation.Annotations{ | ||
| annotation.New("risk-level", "string", "medium"), | ||
| }, nil | ||
| } | ||
|
|
||
| # Registering with the engine | ||
|
|
||
| eng := proposalanalysis.NewAnalyzerEngine() | ||
| if err := eng.RegisterAnalyzer(RiskAnalyzer{}); err != nil { | ||
| return err | ||
| } | ||
graham-chainlink marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| The engine resolves analyzer execution order using `Dependencies()` and passes | ||
| dependency-scoped annotations through the request's `DependencyAnnotationStore`. | ||
| */ | ||
| package analyzer | ||
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,78 @@ | ||
| /* | ||
| Package proposalanalysis provides an engine for analyzing MCMS timelock proposals | ||
| and rendering the analyzed output. | ||
|
|
||
| # Overview | ||
|
|
||
| The engine workflow is: | ||
|
|
||
| 1. Create an engine with optional configuration. | ||
| 2. Register one or more analyzers. | ||
| 3. Register a renderer. | ||
| 4. Run analysis on a proposal. | ||
| 5. Render the analyzed proposal with RenderTo. | ||
|
|
||
| # Usage | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "time" | ||
|
|
||
| "github.com/smartcontractkit/mcms" | ||
|
|
||
| "github.com/smartcontractkit/chainlink-deployments-framework/deployment" | ||
| cldfdomain "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/domain" | ||
graham-chainlink marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/mcms/proposalanalysis" | ||
| "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/mcms/proposalanalysis/decoder" | ||
| "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/mcms/proposalanalysis/renderer" | ||
| ) | ||
|
|
||
| func Example() error { | ||
| ctx := context.Background() | ||
|
|
||
| engine := proposalanalysis.NewAnalyzerEngine( | ||
| proposalanalysis.WithAnalyzerTimeout(90*time.Second), | ||
| ) | ||
|
|
||
| // Register your analyzers (implementations omitted for brevity). | ||
| if err := engine.RegisterAnalyzer(newDependencyAnalyzer()); err != nil { | ||
| return err | ||
| } | ||
| if err := engine.RegisterAnalyzer(newRiskAnalyzer()); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| mdRenderer, err := renderer.NewMarkdownRenderer() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if err := engine.RegisterRenderer(mdRenderer); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| domain := cldfdomain.NewDomain("/path/to/domains", "ccip") | ||
| env := ... | ||
| proposal := ... | ||
|
|
||
| analyzed, err := engine.Run(ctx, proposalanalysis.RunRequest{ | ||
| Domain: domain, | ||
| Environment: env, | ||
| DecoderConfig: decoder.Config{}, | ||
| }, proposal) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return engine.RenderTo( | ||
| os.Stdout, | ||
| renderer.IDMarkdown, | ||
| renderer.RenderRequest{ | ||
| Domain: domain.Key(), | ||
| EnvironmentName: env.Name, | ||
| }, | ||
| analyzed, | ||
| ) | ||
| } | ||
| */ | ||
| package proposalanalysis | ||
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,67 @@ | ||
| /* | ||
| Package renderer defines renderer interfaces used to format analyzed proposals. | ||
|
|
||
| # Implementing a Renderer | ||
|
|
||
| A renderer must implement: | ||
|
|
||
| - ID() string: unique renderer identifier. | ||
| - RenderTo(io.Writer, RenderRequest, analyzer.AnalyzedProposal) error | ||
|
|
||
| Example custom plain-text renderer: | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
|
|
||
| "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/mcms/proposalanalysis" | ||
| "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/mcms/proposalanalysis/analyzer" | ||
| "github.com/smartcontractkit/chainlink-deployments-framework/engine/cld/mcms/proposalanalysis/renderer" | ||
| ) | ||
|
|
||
| type PlainRenderer struct{} | ||
|
|
||
| func (PlainRenderer) ID() string { | ||
| return "plain" | ||
| } | ||
|
|
||
| func (PlainRenderer) RenderTo( | ||
| w io.Writer, | ||
| req renderer.RenderRequest, | ||
| proposal analyzer.AnalyzedProposal, | ||
| ) error { | ||
| if _, err := fmt.Fprintf( | ||
| w, | ||
| "domain=%s env=%s batches=%d\n", | ||
| req.Domain, | ||
| req.EnvironmentName, | ||
| len(proposal.BatchOperations()), | ||
| ); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| for _, batch := range proposal.BatchOperations() { | ||
| if _, err := fmt.Fprintf( | ||
| w, | ||
| "- chain=%d calls=%d\n", | ||
| batch.ChainSelector(), | ||
| len(batch.Calls()), | ||
| ); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| # Registering with the engine | ||
|
|
||
| eng := proposalanalysis.NewAnalyzerEngine() | ||
| if err := eng.RegisterRenderer(PlainRenderer{}); err != nil { | ||
| return err | ||
| } | ||
graham-chainlink marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| To render output, call `eng.RenderTo(...)` with the renderer ID and a | ||
| `renderer.RenderRequest`. | ||
| */ | ||
| package renderer | ||
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.
It would be nice to also add a page on the
docs/folder with similar information and examples.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.
yeah deciding on this, will see how we go, maybe we will add a more official page on the docs.cld too