-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdeploy_factory_and_set_owner_to_mcms.go
More file actions
145 lines (128 loc) · 5.72 KB
/
Copy pathdeploy_factory_and_set_owner_to_mcms.go
File metadata and controls
145 lines (128 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package changesets
import (
"fmt"
"time"
"github.com/smartcontractkit/mcms"
cantonsdk "github.com/smartcontractkit/mcms/sdk/canton"
mcms_types "github.com/smartcontractkit/mcms/types"
"github.com/smartcontractkit/chainlink-deployments-framework/datastore"
cldf "github.com/smartcontractkit/chainlink-deployments-framework/deployment"
"github.com/smartcontractkit/chainlink-deployments-framework/operations"
factorybindings "github.com/smartcontractkit/chainlink-canton/bindings/generated/latest/ccip/factory"
factoryops "github.com/smartcontractkit/chainlink-canton/deployment/operations/ccip/factory"
dsutils "github.com/smartcontractkit/chainlink-canton/deployment/utils/datastore"
cantonmcms "github.com/smartcontractkit/chainlink-canton/deployment/utils/mcms"
opcontract "github.com/smartcontractkit/chainlink-canton/deployment/utils/operations/contract"
)
// DeployFactoryAndSetOwnerToMCMS deploys a CCIPFactory and returns an MCMS proposal to transfer
// ownership via SetOwnerToMCMS (controller = mcmsParty).
type DeployFactoryAndSetOwnerToMCMSConfig struct {
OwnerParty string `json:"ownerParty" yaml:"ownerParty"`
MCMSParty string `json:"mcmsParty" yaml:"mcmsParty"`
InstanceID string `json:"instanceID,omitempty" yaml:"instanceID,omitempty"`
Qualifier string `json:"qualifier,omitempty" yaml:"qualifier,omitempty"`
MinDelay time.Duration `json:"minDelay,omitempty" yaml:"minDelay,omitempty"`
}
type DeployFactoryAndSetOwnerToMCMS struct{}
var _ cldf.ChangeSetV2[CantonCSDeps[DeployFactoryAndSetOwnerToMCMSConfig]] = DeployFactoryAndSetOwnerToMCMS{}
func (d DeployFactoryAndSetOwnerToMCMS) VerifyPreconditions(e cldf.Environment, config CantonCSDeps[DeployFactoryAndSetOwnerToMCMSConfig]) error {
chain, ok := e.BlockChains.CantonChains()[config.ChainSelector]
if !ok {
return fmt.Errorf("canton chain %v not found", config.ChainSelector)
}
if config.Participant < 0 || config.Participant >= len(chain.Participants) {
return fmt.Errorf("participant index %d out of range for canton chain %d with %d participants",
config.Participant, config.ChainSelector, len(chain.Participants))
}
if config.Config.OwnerParty == "" {
return fmt.Errorf("owner party is required")
}
if config.Config.MCMSParty == "" {
return fmt.Errorf("mcms party is required")
}
if config.Config.Qualifier == "" {
return fmt.Errorf("factory qualifier is required")
}
mcmsOwnerQualifier, err := mcmsOwnerQualifierForFactory(config.Config.Qualifier)
if err != nil {
return err
}
if _, err := dsutils.MCMSRawInstanceAddress(e.DataStore, config.ChainSelector, mcmsOwnerQualifier); err != nil {
return fmt.Errorf("MCMS contract not found in datastore (deploy MCMS first): %w", err)
}
if config.Config.MinDelay <= 0 {
return fmt.Errorf("minDelay must be greater than zero")
}
return nil
}
func (d DeployFactoryAndSetOwnerToMCMS) Apply(e cldf.Environment, config CantonCSDeps[DeployFactoryAndSetOwnerToMCMSConfig]) (cldf.ChangesetOutput, error) {
ds := datastore.NewMemoryDataStore()
chain := e.BlockChains.CantonChains()[config.ChainSelector]
cfg := config.Config
factoryRef, err := dsutils.FactoryAddressRef(e.DataStore, config.ChainSelector, cfg.Qualifier)
if err != nil {
factoryRef, err = deployCCIPFactory(e.OperationsBundle, chain, DeployCCIPFactoryParams{
OwnerParty: cfg.OwnerParty,
MCMSParty: cfg.MCMSParty,
InstanceID: cfg.InstanceID,
Qualifier: cfg.Qualifier,
})
if err != nil {
return cldf.ChangesetOutput{}, err
}
if err := ds.AddressRefStore.Add(factoryRef); err != nil {
return cldf.ChangesetOutput{}, fmt.Errorf("store address ref: %w", err)
}
}
factoryRawAddr, err := dsutils.GetRawInstanceAddressFromAddressRef(factoryRef)
if err != nil {
return cldf.ChangesetOutput{}, fmt.Errorf("factory raw instance address: %w", err)
}
// Encode SetOwnerToMCMS for MCMS proposal submission.
exerciseOut, err := operations.ExecuteOperation(e.OperationsBundle, factoryops.SetOwnerToMCMS, chain, opcontract.ChoiceInput[factorybindings.SetOwnerToMCMS]{
InstanceAddress: factoryRawAddr.InstanceAddress(),
RawInstanceAddress: factoryRawAddr.String(),
Args: factorybindings.SetOwnerToMCMS{},
MCMSEnabled: true,
})
if err != nil {
return cldf.ChangesetOutput{}, fmt.Errorf("encode SetOwnerToMCMS: %w", err)
}
batchOp, err := cantonmcms.BuildBatchFromOutputs([]opcontract.ExerciseOutput{exerciseOut.Output})
if err != nil {
return cldf.ChangesetOutput{}, fmt.Errorf("build batch operation: %w", err)
}
mcmsOwnerQualifier, err := mcmsOwnerQualifierForFactory(cfg.Qualifier)
if err != nil {
return cldf.ChangesetOutput{}, err
}
mcmsRawAddr, err := dsutils.MCMSRawInstanceAddress(e.DataStore, config.ChainSelector, mcmsOwnerQualifier)
if err != nil {
return cldf.ChangesetOutput{}, fmt.Errorf("MCMS contract not found: %w", err)
}
participant := chain.Participants[config.Participant]
proposal, err := cantonmcms.GenerateTimelockProposal(
e.GetContext(),
participant.LedgerServices.State,
opcontract.LedgerQueryParties(participant),
cantonmcms.ProposalConfig{
MCMSContract: cantonmcms.MCMSContractInfo{
RawInstanceAddress: mcmsRawAddr,
InstanceAddress: mcmsRawAddr.InstanceAddress(),
},
ChainSelector: mcms_types.ChainSelector(config.ChainSelector),
Description: fmt.Sprintf("Transfer CCIPFactory (%s) ownership to MCMS", cfg.Qualifier),
MinDelay: cfg.MinDelay,
Action: mcms_types.TimelockActionSchedule,
Role: cantonsdk.TimelockRoleProposer,
},
[]mcms_types.BatchOperation{batchOp},
)
if err != nil {
return cldf.ChangesetOutput{}, fmt.Errorf("generate SetOwnerToMCMS proposal: %w", err)
}
return cldf.ChangesetOutput{
DataStore: ds,
MCMSTimelockProposals: []mcms.TimelockProposal{*proposal},
}, nil
}