-
Notifications
You must be signed in to change notification settings - Fork 2k
Aptos p2p map to cap reg #21672
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
Merged
Merged
Aptos p2p map to cap reg #21672
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f4d48ad
First draft of sending p2p map to capReg
yashnevatia 46570fb
merging
yashnevatia 4cafdad
move to fix merge
yashnevatia 4d61f2f
Second draft Aptos p2p map to cap reg
yashnevatia 8899c37
Merge branch 'develop' of github.com:smartcontractkit/chainlink into …
yashnevatia 88427e4
add to 2 more changesets
yashnevatia 049310f
Rename to modifier
yashnevatia 8abe949
lint,comment,func
yashnevatia e6ebcea
forgot to remove
yashnevatia 9489136
chain agnostic func
yashnevatia 772c999
Merge branch 'develop' of github.com:smartcontractkit/chainlink into …
yashnevatia 5f7ccea
merging
yashnevatia 7450af2
fixing
yashnevatia 74e9c99
fix test
yashnevatia 2aca673
bump evm cap
yashnevatia 5368ed9
Merge branch 'develop' of github.com:smartcontractkit/chainlink into …
yashnevatia 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
196 changes: 196 additions & 0 deletions
196
deployment/cre/capabilities_registry/v2/changeset/modifier/modifier.go
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,196 @@ | ||
| package modifier | ||
|
|
||
| import ( | ||
| "encoding/hex" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/smartcontractkit/chainlink-common/keystore/corekeys/p2pkey" | ||
|
|
||
| "google.golang.org/protobuf/encoding/protojson" | ||
|
|
||
| cldf "github.com/smartcontractkit/chainlink-deployments-framework/deployment" | ||
| "github.com/smartcontractkit/chainlink-protos/cre/go/values" | ||
| "github.com/smartcontractkit/chainlink/deployment" | ||
| "github.com/smartcontractkit/chainlink/deployment/cre/capabilities_registry/v2/changeset/operations/contracts" | ||
| ) | ||
|
|
||
| // CapabilityConfigModifierParams carries shared inputs for per-DON capability config modification. | ||
| // Extend with new fields when additional modifiers need them; modifiers ignore unused fields. | ||
| type CapabilityConfigModifierParams struct { | ||
| Env *cldf.Environment | ||
| DonName string | ||
| P2PIDs []p2pkey.PeerID | ||
| // Configs is a per-DON clone of the caller's capability configs; modifiers mutate in place. | ||
| Configs []contracts.CapabilityConfig | ||
| } | ||
|
|
||
| // CapabilityConfigModifier applies chain or capability-specific changes to Config (e.g. specConfig). | ||
| type CapabilityConfigModifier interface { | ||
| Modify(params CapabilityConfigModifierParams) error | ||
| } | ||
|
|
||
| func DefaultCapabilityConfigModifiers() []CapabilityConfigModifier { | ||
| return []CapabilityConfigModifier{ | ||
| aptosDonModifier{}, | ||
| } | ||
| } | ||
|
|
||
| // aptos | ||
|
|
||
| type aptosDonModifier struct{} | ||
|
|
||
| func (aptosDonModifier) Modify(params CapabilityConfigModifierParams) error { | ||
| for i := range params.Configs { | ||
| sel, isAptos, parseErr := parseChainSelectorFromCapabilityID(params.Configs[i].Capability.CapabilityID, aptosCapabilityIDPrefix) | ||
| if parseErr != nil { | ||
| return fmt.Errorf("capability %q: %w", params.Configs[i].Capability.CapabilityID, parseErr) | ||
| } | ||
| if !isAptos { | ||
| continue | ||
| } | ||
| if params.Env == nil || params.Env.Offchain == nil { | ||
| return errors.New("AddCapabilities: Aptos capabilities require Env.Offchain (Job Distributor client)") | ||
| } | ||
| if params.Configs[i].Config == nil { | ||
| params.Configs[i].Config = make(map[string]any) | ||
| } | ||
| p2pMap, mapErr := buildP2PToTransmitterMap(params.Env.Offchain, params.P2PIDs, sel) | ||
| if mapErr != nil { | ||
| return fmt.Errorf("capability %q: %w", params.Configs[i].Capability.CapabilityID, mapErr) | ||
| } | ||
| if mergeErr := mergeP2PToTransmitterIntoConfig(params.Configs[i].Config, p2pMap); mergeErr != nil { | ||
| return fmt.Errorf("capability %q: %w", params.Configs[i].Capability.CapabilityID, mergeErr) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // aptosCapabilityIDPrefix is the capability id form used for Aptos chain capabilities | ||
| // (label before optional "@<version>"), e.g. aptos:ChainSelector:12345@1.0.0. | ||
| const aptosCapabilityIDPrefix = "aptos:ChainSelector:" | ||
|
|
||
| // parseChainSelectorFromCapabilityID parses registry capability IDs of the form | ||
| // <prefix><decimal>@<version>. The part after the last "@" is ignored for | ||
| // parsing so only the label matters (e.g. aptos:ChainSelector:12345@1.0.0 → 12345). | ||
| // | ||
| // Returns matched false and no error when the id does not start with prefix | ||
| // (after stripping "@…"). Returns matched true and an error if the prefix is present but the | ||
| // selector is empty or not a base-10 uint64. | ||
| func parseChainSelectorFromCapabilityID(capabilityID, prefix string) (selector uint64, matched bool, err error) { | ||
| capID := capabilityID | ||
| if i := strings.LastIndex(capabilityID, "@"); i >= 0 { | ||
| capID = capabilityID[:i] | ||
| } | ||
| if !strings.HasPrefix(capID, prefix) { | ||
| return 0, false, nil | ||
| } | ||
| raw := strings.TrimPrefix(capID, prefix) | ||
| if raw == "" { | ||
| return 0, true, fmt.Errorf("missing chain selector in capability id %q", capabilityID) | ||
| } | ||
| u, parseErr := strconv.ParseUint(raw, 10, 64) | ||
| if parseErr != nil { | ||
| return 0, true, fmt.Errorf("invalid chain selector in capability id %q: %w", capabilityID, parseErr) | ||
| } | ||
| return u, true, nil | ||
| } | ||
|
|
||
| // buildP2PToTransmitterMap asks Job Distributor for node metadata for donPeerIDs | ||
| // and builds a map used for CapabilityConfig spec: | ||
| // - lowercase hex of the 32-byte P2P id -> transmit account (OCR TransmitAccount) | ||
| // for the given chainSelector. | ||
| // | ||
| // It walks only the nodes returned by NodeInfo. Each must have OCR config for | ||
| // chainSelector and a non-empty transmit account after trim, or this returns an error. | ||
| func buildP2PToTransmitterMap( | ||
| offChainClient deployment.NodeChainConfigsLister, | ||
| donPeerIDs []p2pkey.PeerID, | ||
| chainSelector uint64, | ||
| ) (map[string]string, error) { | ||
| if offChainClient == nil { | ||
| return nil, errors.New("offchain client is nil") | ||
| } | ||
| if len(donPeerIDs) == 0 { | ||
| return nil, errors.New("no DON peer IDs") | ||
| } | ||
| p2pStrs := make([]string, len(donPeerIDs)) | ||
| for i, pid := range donPeerIDs { | ||
| p2pStrs[i] = pid.String() | ||
| } | ||
| nodes, nodeInfoErr := deployment.NodeInfo(p2pStrs, offChainClient) | ||
| if nodeInfoErr != nil { | ||
| return nil, fmt.Errorf("failed to get node info from JD: %w", nodeInfoErr) | ||
| } | ||
| out := make(map[string]string, len(nodes)) | ||
| for _, node := range nodes { | ||
| ocrCfg, ok := node.OCRConfigForChainSelector(chainSelector) | ||
| if !ok { | ||
| return nil, fmt.Errorf("node %s (%s) has no OCR2 config for chain selector %d", | ||
| node.Name, node.PeerID.String(), chainSelector) | ||
| } | ||
| transmitter := strings.TrimSpace(string(ocrCfg.TransmitAccount)) | ||
| if transmitter == "" { | ||
| return nil, fmt.Errorf("empty transmit account for node %s (%s)", node.Name, node.PeerID.String()) | ||
| } | ||
| out[hex.EncodeToString(node.PeerID[:])] = transmitter | ||
| } | ||
| return out, nil | ||
| } | ||
|
|
||
| // mergeP2PToTransmitterIntoConfig sets cfg["specConfig"] to p2pMap (as p2pToTransmitterMap). | ||
| // Caller must omit specConfig or leave it empty; any non-empty specConfig returns an error for now. | ||
| // NOTE: we can make this smarter later if needed. Add overwriting / merging logic etc. | ||
| // | ||
| // specConfig is protobuf values.v1.Map JSON; we build it with values.Wrap so pkg.MarshalProto succeeds. | ||
| func mergeP2PToTransmitterIntoConfig(cfg map[string]any, p2pMap map[string]string) error { | ||
| if cfg == nil { | ||
| return errors.New("nil capability config map") | ||
| } | ||
| if raw, ok := cfg["specConfig"]; ok && raw != nil { | ||
| if !isEmptySpecConfig(raw) { | ||
| return errors.New("specConfig must be empty (omit or {}) for p2pToTransmitterMap injection") | ||
| } | ||
| } | ||
| p2pVal, err := values.Wrap(p2pMap) | ||
| if err != nil { | ||
| return fmt.Errorf("wrap p2pToTransmitterMap: %w", err) | ||
| } | ||
| spec := values.EmptyMap() | ||
| spec.Underlying["p2pToTransmitterMap"] = p2pVal | ||
| out, err := protojson.Marshal(values.ProtoMap(spec)) | ||
| if err != nil { | ||
| return fmt.Errorf("marshal specConfig: %w", err) | ||
| } | ||
| var specAsMap map[string]any | ||
| if err := json.Unmarshal(out, &specAsMap); err != nil { | ||
| return fmt.Errorf("specConfig map: %w", err) | ||
| } | ||
| cfg["specConfig"] = specAsMap | ||
| return nil | ||
| } | ||
|
|
||
| // isEmptySpecConfig reports whether user-provided specConfig is absent-equivalent: | ||
| // nil, {}, or values.v1.Map JSON with no entries ({ "fields": {} }). | ||
| func isEmptySpecConfig(raw any) bool { | ||
| if raw == nil { | ||
| return true | ||
| } | ||
| m, ok := raw.(map[string]any) | ||
| if !ok { | ||
| return false | ||
| } | ||
| if len(m) == 0 { | ||
| return true | ||
| } | ||
| if len(m) == 1 { | ||
| fields, ok := m["fields"].(map[string]any) | ||
| if ok && len(fields) == 0 { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.