Skip to content

Commit

Permalink
refactor, bring code into 'changeset' directory (#14948)
Browse files Browse the repository at this point in the history
* refactor, bring code into 'changeset' directory

* go imports
  • Loading branch information
0xAustinWang authored Oct 25, 2024
1 parent 2d28b4e commit 0708332
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 278 deletions.
4 changes: 2 additions & 2 deletions integration-tests/deployment/ccip/add_lane.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func AddLane(e deployment.Environment, state CCIPOnChainState, from, to uint64)
[]fee_quoter.FeeQuoterDestChainConfigArgs{
{
DestChainSelector: to,
DestChainConfig: defaultFeeQuoterDestChainConfig(),
DestChainConfig: DefaultFeeQuoterDestChainConfig(),
},
})
if _, err := deployment.ConfirmIfNoError(e.Chains[from], tx, err); err != nil {
Expand Down Expand Up @@ -98,7 +98,7 @@ func AddLane(e deployment.Environment, state CCIPOnChainState, from, to uint64)
return err
}

func defaultFeeQuoterDestChainConfig() fee_quoter.FeeQuoterDestChainConfig {
func DefaultFeeQuoterDestChainConfig() fee_quoter.FeeQuoterDestChainConfig {
// https://github.com/smartcontractkit/ccip/blob/c4856b64bd766f1ddbaf5d13b42d3c4b12efde3a/contracts/src/v0.8/ccip/libraries/Internal.sol#L337-L337
/*
```Solidity
Expand Down
212 changes: 212 additions & 0 deletions integration-tests/deployment/ccip/changeset/active_candidate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package changeset

import (
"fmt"
"math/big"

"github.com/smartcontractkit/ccip-owner-contracts/pkg/proposal/mcms"
"github.com/smartcontractkit/ccip-owner-contracts/pkg/proposal/timelock"

"github.com/smartcontractkit/chainlink/integration-tests/deployment"
ccdeploy "github.com/smartcontractkit/chainlink/integration-tests/deployment/ccip"
cctypes "github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/types"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/ccip_home"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/keystone/generated/capabilities_registry"
)

// SetCandidateExecPluginOps calls setCandidate on CCIPHome contract through the UpdateDON call on CapReg contract
// This proposes to set up OCR3 config for the provided plugin for the DON
func SetCandidateOnExistingDon(
pluginConfig ccip_home.CCIPHomeOCR3Config,
capReg *capabilities_registry.CapabilitiesRegistry,
ccipHome *ccip_home.CCIPHome,
chainSelector uint64,
nodes deployment.Nodes,
) ([]mcms.Operation, error) {
// fetch DON ID for the chain
donID, err := ccdeploy.DonIDForChain(capReg, ccipHome, chainSelector)
if err != nil {
return nil, fmt.Errorf("fetch don id for chain: %w", err)
}
fmt.Printf("donID: %d", donID)
encodedSetCandidateCall, err := ccdeploy.CCIPHomeABI.Pack(
"setCandidate",
donID,
pluginConfig.PluginType,
pluginConfig,
[32]byte{},
)
if err != nil {
return nil, fmt.Errorf("pack set candidate call: %w", err)
}

// set candidate call
updateDonTx, err := capReg.UpdateDON(
deployment.SimTransactOpts(),
donID,
nodes.PeerIDs(),
[]capabilities_registry.CapabilitiesRegistryCapabilityConfiguration{
{
CapabilityId: ccdeploy.CCIPCapabilityID,
Config: encodedSetCandidateCall,
},
},
false,
nodes.DefaultF(),
)
if err != nil {
return nil, fmt.Errorf("update don w/ exec config: %w", err)
}

return []mcms.Operation{{
To: capReg.Address(),
Data: updateDonTx.Data(),
Value: big.NewInt(0),
}}, nil
}

// PromoteCandidateOp will create the MCMS Operation for `promoteCandidateAndRevokeActive` directed towards the capabilityRegistry
func PromoteCandidateOp(donID uint32, pluginType uint8, capReg *capabilities_registry.CapabilitiesRegistry,
ccipHome *ccip_home.CCIPHome, nodes deployment.Nodes) (mcms.Operation, error) {

allConfigs, err := ccipHome.GetAllConfigs(nil, donID, pluginType)
if err != nil {
return mcms.Operation{}, err
}

if allConfigs.CandidateConfig.ConfigDigest == [32]byte{} {
return mcms.Operation{}, fmt.Errorf("candidate digest is empty, expected nonempty")
}
fmt.Printf("commit candidate digest after setCandidate: %x\n", allConfigs.CandidateConfig.ConfigDigest)

encodedPromotionCall, err := ccdeploy.CCIPHomeABI.Pack(
"promoteCandidateAndRevokeActive",
donID,
pluginType,
allConfigs.CandidateConfig.ConfigDigest,
allConfigs.ActiveConfig.ConfigDigest,
)
if err != nil {
return mcms.Operation{}, fmt.Errorf("pack promotion call: %w", err)
}

updateDonTx, err := capReg.UpdateDON(
deployment.SimTransactOpts(),
donID,
nodes.PeerIDs(),
[]capabilities_registry.CapabilitiesRegistryCapabilityConfiguration{
{
CapabilityId: ccdeploy.CCIPCapabilityID,
Config: encodedPromotionCall,
},
},
false,
nodes.DefaultF(),
)
if err != nil {
return mcms.Operation{}, fmt.Errorf("error creating updateDon op for donID(%d) and plugin type (%d): %w", donID, pluginType, err)
}
return mcms.Operation{
To: capReg.Address(),
Data: updateDonTx.Data(),
Value: big.NewInt(0),
}, nil
}

// PromoteAllCandidatesForChainOps promotes the candidate commit and exec configs to active by calling promoteCandidateAndRevokeActive on CCIPHome through the UpdateDON call on CapReg contract
func PromoteAllCandidatesForChainOps(
capReg *capabilities_registry.CapabilitiesRegistry,
ccipHome *ccip_home.CCIPHome,
chainSelector uint64,
nodes deployment.Nodes,
) ([]mcms.Operation, error) {
// fetch DON ID for the chain
donID, err := ccdeploy.DonIDForChain(capReg, ccipHome, chainSelector)
if err != nil {
return nil, fmt.Errorf("fetch don id for chain: %w", err)
}

var mcmsOps []mcms.Operation
updateCommitOp, err := PromoteCandidateOp(donID, uint8(cctypes.PluginTypeCCIPCommit), capReg, ccipHome, nodes)
if err != nil {
return nil, fmt.Errorf("promote candidate op: %w", err)
}
mcmsOps = append(mcmsOps, updateCommitOp)

updateExecOp, err := PromoteCandidateOp(donID, uint8(cctypes.PluginTypeCCIPExec), capReg, ccipHome, nodes)
if err != nil {
return nil, fmt.Errorf("promote candidate op: %w", err)
}
mcmsOps = append(mcmsOps, updateExecOp)

return mcmsOps, nil
}

// PromoteAllCandidatesProposal generates a proposal to call promoteCandidate on the CCIPHome through CapReg.
// This needs to be called after SetCandidateProposal is executed.
func PromoteAllCandidatesProposal(
state ccdeploy.CCIPOnChainState,
homeChainSel, newChainSel uint64,
nodes deployment.Nodes,
) (*timelock.MCMSWithTimelockProposal, error) {
promoteCandidateOps, err := PromoteAllCandidatesForChainOps(
state.Chains[homeChainSel].CapabilityRegistry,
state.Chains[homeChainSel].CCIPHome,
newChainSel,
nodes.NonBootstraps(),
)
if err != nil {
return nil, err
}

return ccdeploy.BuildProposalFromBatches(state, []timelock.BatchChainOperation{{
ChainIdentifier: mcms.ChainIdentifier(homeChainSel),
Batch: promoteCandidateOps,
}}, "promoteCandidate for commit and execution", 0)
}

// SetCandidateExecPluginProposal calls setCandidate on the CCIPHome for setting up OCR3 exec Plugin config for the new chain.
func SetCandidatePluginProposal(
state ccdeploy.CCIPOnChainState,
e deployment.Environment,
nodes deployment.Nodes,
ocrSecrets deployment.OCRSecrets,
homeChainSel, feedChainSel, newChainSel uint64,
tokenConfig ccdeploy.TokenConfig,
pluginType cctypes.PluginType,
) (*timelock.MCMSWithTimelockProposal, error) {
newDONArgs, err := ccdeploy.BuildOCR3ConfigForCCIPHome(
e.Logger,
ocrSecrets,
state.Chains[newChainSel].OffRamp,
e.Chains[newChainSel],
feedChainSel,
tokenConfig.GetTokenInfo(e.Logger, state.Chains[newChainSel].LinkToken, state.Chains[newChainSel].Weth9),
nodes.NonBootstraps(),
state.Chains[homeChainSel].RMNHome.Address(),
)
if err != nil {
return nil, err
}

execConfig, ok := newDONArgs[pluginType]
if !ok {
return nil, fmt.Errorf("missing exec plugin in ocr3Configs")
}

setCandidateMCMSOps, err := SetCandidateOnExistingDon(
execConfig,
state.Chains[homeChainSel].CapabilityRegistry,
state.Chains[homeChainSel].CCIPHome,
newChainSel,
nodes.NonBootstraps(),
)
if err != nil {
return nil, err
}

return ccdeploy.BuildProposalFromBatches(state, []timelock.BatchChainOperation{{
ChainIdentifier: mcms.ChainIdentifier(homeChainSel),
Batch: setCandidateMCMSOps,
}}, "SetCandidate for execution", 0)
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func TestActiveCandidate(t *testing.T) {
)
require.NoError(t, err)

setCommitCandidateOp, err := ccdeploy.SetCandidateOnExistingDon(
setCommitCandidateOp, err := SetCandidateOnExistingDon(
ocr3ConfigMap[cctypes.PluginTypeCCIPCommit],
state.Chains[homeCS].CapabilityRegistry,
state.Chains[homeCS].CCIPHome,
Expand All @@ -187,7 +187,7 @@ func TestActiveCandidate(t *testing.T) {
ccdeploy.ExecuteProposal(t, e, setCommitCandidateSigned, state, homeCS)

// create the op for the commit plugin as well
setExecCandidateOp, err := ccdeploy.SetCandidateOnExistingDon(
setExecCandidateOp, err := SetCandidateOnExistingDon(
ocr3ConfigMap[cctypes.PluginTypeCCIPExec],
state.Chains[homeCS].CapabilityRegistry,
state.Chains[homeCS].CCIPHome,
Expand Down Expand Up @@ -221,7 +221,7 @@ func TestActiveCandidate(t *testing.T) {
oldCandidateDigest, err := state.Chains[homeCS].CCIPHome.GetCandidateDigest(nil, donID, uint8(cctypes.PluginTypeCCIPExec))
require.NoError(t, err)

promoteOps, err := ccdeploy.PromoteAllCandidatesForChainOps(state.Chains[homeCS].CapabilityRegistry, state.Chains[homeCS].CCIPHome, destCS, nodes.NonBootstraps())
promoteOps, err := PromoteAllCandidatesForChainOps(state.Chains[homeCS].CapabilityRegistry, state.Chains[homeCS].CCIPHome, destCS, nodes.NonBootstraps())
require.NoError(t, err)
promoteProposal, err := ccdeploy.BuildProposalFromBatches(state, []timelock.BatchChainOperation{{
ChainIdentifier: mcms.ChainIdentifier(homeCS),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package ccipdeployment
package changeset

import (
"fmt"
"math/big"

ccipdeployment "github.com/smartcontractkit/chainlink/integration-tests/deployment/ccip"

"github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/types"

"github.com/smartcontractkit/ccip-owner-contracts/pkg/proposal/mcms"
Expand All @@ -18,7 +20,7 @@ import (
// to connect the new chain to the existing chains.
func NewChainInboundProposal(
e deployment.Environment,
state CCIPOnChainState,
state ccipdeployment.CCIPOnChainState,
homeChainSel uint64,
newChainSel uint64,
sources []uint64,
Expand All @@ -40,7 +42,7 @@ func NewChainInboundProposal(
[]fee_quoter.FeeQuoterDestChainConfigArgs{
{
DestChainSelector: newChainSel,
DestChainConfig: defaultFeeQuoterDestChainConfig(),
DestChainConfig: ccipdeployment.DefaultFeeQuoterDestChainConfig(),
},
})
if err != nil {
Expand All @@ -64,7 +66,7 @@ func NewChainInboundProposal(
})
}

addChainOp, err := ApplyChainConfigUpdatesOp(e, state, homeChainSel, []uint64{newChainSel})
addChainOp, err := ccipdeployment.ApplyChainConfigUpdatesOp(e, state, homeChainSel, []uint64{newChainSel})
if err != nil {
return nil, err
}
Expand All @@ -76,21 +78,21 @@ func NewChainInboundProposal(
},
})

return BuildProposalFromBatches(state, batches, "proposal to set new chains", 0)
return ccipdeployment.BuildProposalFromBatches(state, batches, "proposal to set new chains", 0)
}

// AddDonAndSetCandidateProposal adds new DON for destination to home chain
// and sets the commit plugin config as candidateConfig for the don.
func AddDonAndSetCandidateProposal(
state CCIPOnChainState,
state ccipdeployment.CCIPOnChainState,
e deployment.Environment,
nodes deployment.Nodes,
ocrSecrets deployment.OCRSecrets,
homeChainSel, feedChainSel, newChainSel uint64,
tokenConfig TokenConfig,
tokenConfig ccipdeployment.TokenConfig,
pluginType types.PluginType,
) (*timelock.MCMSWithTimelockProposal, error) {
newDONArgs, err := BuildOCR3ConfigForCCIPHome(
newDONArgs, err := ccipdeployment.BuildOCR3ConfigForCCIPHome(
e.Logger,
ocrSecrets,
state.Chains[newChainSel].OffRamp,
Expand All @@ -103,7 +105,7 @@ func AddDonAndSetCandidateProposal(
if err != nil {
return nil, err
}
latestDon, err := LatestCCIPDON(state.Chains[homeChainSel].CapabilityRegistry)
latestDon, err := ccipdeployment.LatestCCIPDON(state.Chains[homeChainSel].CapabilityRegistry)
if err != nil {
return nil, err
}
Expand All @@ -112,7 +114,7 @@ func AddDonAndSetCandidateProposal(
return nil, fmt.Errorf("missing commit plugin in ocr3Configs")
}
donID := latestDon.Id + 1
addDonOp, err := NewDonWithCandidateOp(
addDonOp, err := ccipdeployment.NewDonWithCandidateOp(
donID, commitConfig,
state.Chains[homeChainSel].CapabilityRegistry,
nodes.NonBootstraps(),
Expand All @@ -121,7 +123,7 @@ func AddDonAndSetCandidateProposal(
return nil, err
}

return BuildProposalFromBatches(state, []timelock.BatchChainOperation{{
return ccipdeployment.BuildProposalFromBatches(state, []timelock.BatchChainOperation{{
ChainIdentifier: mcms.ChainIdentifier(homeChainSel),
Batch: []mcms.Operation{addDonOp},
}}, "setCandidate for commit and AddDon on new Chain", 0)
Expand Down
Loading

0 comments on commit 0708332

Please sign in to comment.