Skip to content

Commit

Permalink
Integrate with develop
Browse files Browse the repository at this point in the history
  • Loading branch information
carte7000 committed Dec 6, 2024
1 parent fb5c90c commit f884d44
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 101 deletions.
141 changes: 121 additions & 20 deletions deployment/ccip/changeset/cs_update_rmn_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package changeset
import (
"fmt"
"math/big"
"reflect"

"github.com/ethereum/go-ethereum/common"
"github.com/smartcontractkit/ccip-owner-contracts/pkg/gethwrappers"
Expand All @@ -23,42 +24,78 @@ type SetRMNHomeCandidateConfig struct {
}

func (c SetRMNHomeCandidateConfig) Validate() error {
if c.HomeChainSelector == 0 {
return fmt.Errorf("HomeChainSelector must be set")
err := deployment.IsValidChainSelector(c.HomeChainSelector)
if err != nil {
return err
}

if len(c.RMNDynamicConfig.OffchainConfig) != 0 {
return fmt.Errorf("RMNDynamicConfig.OffchainConfig must be empty")
}
if len(c.RMNStaticConfig.OffchainConfig) != 0 {
return fmt.Errorf("RMNStaticConfig.OffchainConfig must be empty")
}

if len(c.RMNStaticConfig.Nodes) > 256 {
return fmt.Errorf("RMNStaticConfig.Nodes must be less than 256")
}

peerIds := make(map[[32]byte]struct{})
for _, node := range c.RMNStaticConfig.Nodes {
if _, exists := peerIds[node.PeerId]; exists {
return fmt.Errorf("peerId %x is duplicated", node.PeerId)
}
peerIds[node.PeerId] = struct{}{}
}

offchainPublicKeys := make(map[[32]byte]struct{})
for _, node := range c.RMNStaticConfig.Nodes {
if _, exists := offchainPublicKeys[node.OffchainPublicKey]; exists {
return fmt.Errorf("offchainPublicKey %x is duplicated", node.OffchainPublicKey)
}
offchainPublicKeys[node.OffchainPublicKey] = struct{}{}
}

return nil
}

type PromoteRMNHomeCandidateConfig struct {
HomeChainSelector uint64
DigestToPromote [32]byte
}

func (c PromoteRMNHomeCandidateConfig) Validate() error {
if c.HomeChainSelector == 0 {
return fmt.Errorf("HomeChainSelector must be set")
err := deployment.IsValidChainSelector(c.HomeChainSelector)
if err != nil {
return err
}

return nil
}

func NewSetRMNHomeCandidateConfigChangeset(e deployment.Environment, config SetRMNHomeCandidateConfig) (deployment.ChangesetOutput, error) {
state, err := LoadOnchainState(e)
lggr := e.Logger
if err != nil {
return deployment.ChangesetOutput{}, fmt.Errorf("failed to load onchain state: %w", err)
}

lggr := e.Logger

err = config.Validate()
if err != nil {
return deployment.ChangesetOutput{}, err
}

homeChain := e.Chains[config.HomeChainSelector]

rmnHome := state.Chains[config.HomeChainSelector].RMNHome
if rmnHome == nil {
return deployment.ChangesetOutput{}, fmt.Errorf("RMNHome not found for chain %s", homeChain.String())
}

setCandidateTx, err := rmnHome.SetCandidate(deployment.SimTransactOpts(), config.RMNStaticConfig, config.RMNDynamicConfig, config.DigestToOverride)
if err != nil {
lggr.Errorw("Failed to build call data to set RMNHome candidate digest", "err", err)
lggr.Errorw("Failed to build call data to set RMNHome candidate digest", "err", err, "chain", homeChain.String())
return deployment.ChangesetOutput{}, err
}

Expand All @@ -77,27 +114,43 @@ func NewSetRMNHomeCandidateConfigChangeset(e deployment.Environment, config SetR

func NewPromoteCandidateConfigChangeset(e deployment.Environment, config PromoteRMNHomeCandidateConfig) (deployment.ChangesetOutput, error) {
state, err := LoadOnchainState(e)
lggr := e.Logger

if err != nil {
return deployment.ChangesetOutput{}, fmt.Errorf("failed to load onchain state: %w", err)
}

lggr := e.Logger

err = config.Validate()
if err != nil {
return deployment.ChangesetOutput{}, err
}

homeChain := e.Chains[config.HomeChainSelector]
rmnHome := state.Chains[config.HomeChainSelector].RMNHome
if rmnHome == nil {
return deployment.ChangesetOutput{}, fmt.Errorf("RMNHome not found for chain %s", homeChain.String())
}

currentCandidateDigest, err := rmnHome.GetCandidateDigest(nil)
if err != nil {
lggr.Errorw("Failed to get RMNHome candidate digest", "err", err)
lggr.Errorw("Failed to get RMNHome candidate digest", "err", err, "chain", homeChain.String())
return deployment.ChangesetOutput{}, err
}

currentActiveDigest, err := rmnHome.GetActiveDigest(nil)
if err != nil {
lggr.Errorw("Failed to get RMNHome active digest", "err", err)
lggr.Errorw("Failed to get RMNHome active digest", "err", err, "chain", homeChain.String())
return deployment.ChangesetOutput{}, err
}

if currentCandidateDigest != config.DigestToPromote {
return deployment.ChangesetOutput{}, fmt.Errorf("candidate digest does not match digest to promote for chain %s", homeChain.String())
}

promoteCandidateTx, err := rmnHome.PromoteCandidateAndRevokeActive(deployment.SimTransactOpts(), currentCandidateDigest, currentActiveDigest)
if err != nil {
lggr.Errorw("Failed to get call data to promote RMNHome candidate digest", "err", err)
lggr.Errorw("Failed to get call data to promote RMNHome candidate digest", "err", err, "chain", homeChain.String())
return deployment.ChangesetOutput{}, err
}

Expand All @@ -110,7 +163,7 @@ func NewPromoteCandidateConfigChangeset(e deployment.Environment, config Promote
prop, err := buildProposal(e, op, state, config.HomeChainSelector)

if err != nil {
lggr.Errorw("Failed to build proposal", "err", err)
lggr.Errorw("Failed to build proposal", "err", err, "chain", homeChain.String())
return deployment.ChangesetOutput{}, err
}

Expand Down Expand Up @@ -144,14 +197,26 @@ func buildProposerPerChain(e deployment.Environment, state CCIPOnChainState) map
return proposerPerChain
}

func buildRemoteRemotePerChain(e deployment.Environment, state CCIPOnChainState) map[uint64]*rmn_remote.RMNRemote {
func buildRMNRemotePerChain(e deployment.Environment, state CCIPOnChainState) map[uint64]*rmn_remote.RMNRemote {
timelocksPerChain := make(map[uint64]*rmn_remote.RMNRemote)
for _, chain := range e.Chains {
timelocksPerChain[chain.Selector] = state.Chains[chain.Selector].RMNRemote
}
return timelocksPerChain
}

func buildRMNRemoteAddressPerChain(e deployment.Environment, state CCIPOnChainState) map[uint64]common.Address {
rmnRemotePerChain := buildRMNRemotePerChain(e, state)
rmnRemoteAddressPerChain := make(map[uint64]common.Address)
for chain, remote := range rmnRemotePerChain {
if remote == nil {
continue
}
rmnRemoteAddressPerChain[chain] = remote.Address()
}
return rmnRemoteAddressPerChain
}

func buildProposal(e deployment.Environment, op mcms.Operation, state CCIPOnChainState, homeChainSelector uint64) (*timelock.MCMSWithTimelockProposal, error) {
batches := []timelock.BatchChainOperation{
{
Expand Down Expand Up @@ -180,42 +245,78 @@ type SetRMNRemoteConfig struct {
}

func (c SetRMNRemoteConfig) Validate() error {
if c.HomeChainSelector == 0 {
return fmt.Errorf("HomeChainSelector must be set")
err := deployment.IsValidChainSelector(c.HomeChainSelector)
if err != nil {
return err
}

for i := 0; i < len(c.Signers)-1; i++ {
if c.Signers[i].NodeIndex >= c.Signers[i+1].NodeIndex {
return fmt.Errorf("signers must be in ascending order of nodeIndex")
}
}

if len(c.Signers) < 2*int(c.F)+1 {
return fmt.Errorf("signers count must great or equal to %d", 2*c.F+1)
}

return nil
}

func NewSetRMNRemoteConfigChangeset(e deployment.Environment, config SetRMNRemoteConfig) (deployment.ChangesetOutput, error) {
state, err := LoadOnchainState(e)
lggr := e.Logger

if err != nil {
return deployment.ChangesetOutput{}, fmt.Errorf("failed to load onchain state: %w", err)
}

lggr := e.Logger

err = config.Validate()
if err != nil {
return deployment.ChangesetOutput{}, err
}

homeChain := e.Chains[config.HomeChainSelector]
rmnHome := state.Chains[config.HomeChainSelector].RMNHome
if rmnHome == nil {
return deployment.ChangesetOutput{}, fmt.Errorf("RMNHome not found for chain %s", homeChain.String())
}

activeConfig, err := rmnHome.GetActiveDigest(nil)
if err != nil {
lggr.Errorw("Failed to get RMNHome active digest", "err", err)
lggr.Errorw("Failed to get RMNHome active digest", "err", err, "chain", homeChain.String())
return deployment.ChangesetOutput{}, err
}

rmnRemotePerChain := buildRemoteRemotePerChain(e, state)
rmnRemotePerChain := buildRMNRemotePerChain(e, state)
batches := make([]timelock.BatchChainOperation, 0)
for chain, remote := range rmnRemotePerChain {
if remote == nil {
continue
}

tx, err := remote.SetConfig(deployment.SimTransactOpts(), rmn_remote.RMNRemoteConfig{
currentVersionConfig, err := remote.GetVersionedConfig(nil)
if err != nil {
lggr.Errorw("Failed to get RMNRemote config", "err", err, "chain", e.Chains[chain].String())
return deployment.ChangesetOutput{}, err
}

newConfig := rmn_remote.RMNRemoteConfig{
RmnHomeContractConfigDigest: activeConfig,
Signers: config.Signers,
F: config.F,
})
}

if reflect.DeepEqual(currentVersionConfig.Config, newConfig) {
lggr.Infow("RMNRemote config already up to date", "chain", e.Chains[chain].String())
continue
}

tx, err := remote.SetConfig(deployment.SimTransactOpts(), newConfig)

if err != nil {
lggr.Errorw("Failed to build call data to set RMNRemote config", "err", err)
lggr.Errorw("Failed to build call data to set RMNRemote config", "err", err, "chain", e.Chains[chain].String())
return deployment.ChangesetOutput{}, err
}

Expand Down
Loading

0 comments on commit f884d44

Please sign in to comment.