Skip to content

Commit

Permalink
Some renaming, pull migrations closer to code
Browse files Browse the repository at this point in the history
  • Loading branch information
connorwstein committed Aug 7, 2024
1 parent bfec573 commit 4d7eae6
Show file tree
Hide file tree
Showing 16 changed files with 346 additions and 118 deletions.
92 changes: 48 additions & 44 deletions core/capabilities/ccip/deployment/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,36 @@ import (
"github.com/ethereum/go-ethereum/common"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink/v2/core/environment"
"github.com/smartcontractkit/chainlink/v2/core/deployment"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/arm_proxy_contract"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/mock_arm_contract"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/router"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/token_admin_registry"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/weth9"
)

type Proposal struct {
}

func (p Proposal) String() string {
return ""
}

func GenerateAcceptOwnershipProposal(e environment.Environment, state CCIPOnChainState) Proposal {
return Proposal{}
}
var (
ARMProxy_1_1_0 = "ARMProxy 1.0.0"
MockARM_1_0_0 = "MockARM 1.0.0"
LinkToken_1_0_0 = "LinkToken 1.0.0"
TokenAdminRegistry_1_0_0 = "TokenAdminRegistry 1.0.0"
WETH9_1_0_0 = "WETH9 1.0.0"
Router_1_0_0 = "Router 1.0.0"
CapabilitiesRegistry_1_0_0 = "CapabilitiesRegistry 1.0.0"
EVM2EVMMultiOnRamp_1_6_0 = "EVM2EVMMultiOnRamp 1.6.0-dev"
EVM2EVMMultiOffRamp_1_6_0 = "EVM2EVMMultiOffRamp 1.6.0-dev"
PriceRegistry_1_0_0 = "PriceRegistry 1.0.0"
NonceManager_1_0_0 = "NonceManager 1.0.0"
)

// TODO: pull up to environment pkg
// TODO: pull up to general deployment pkg
func deployContract(
lggr logger.Logger,
deploy func() (common.Address, common.Hash, error),
deploy func() (common.Address, string, common.Hash, error),
confirm func(common.Hash) error,
save func(address common.Address) error,
save func(address common.Address, tv string) error,
) (common.Address, error) {
contractAddr, tx, err := deploy()
contractAddr, tvStr, tx, err := deploy()
if err != nil {
lggr.Errorw("Failed to deploy contract", "err", err)
return common.Address{}, err
Expand All @@ -39,7 +43,7 @@ func deployContract(
lggr.Errorw("Failed to confirm deployment", "err", err)
return common.Address{}, err
}
err = save(contractAddr)
err = save(contractAddr, tvStr)
if err != nil {
lggr.Errorw("Failed to save contract address", "err", err)
return common.Address{}, err
Expand All @@ -53,8 +57,8 @@ func (s CCIPSpec) String() string {
return ""
}

func GenerateJobSpecs(capReg common.Address) CCIPSpec {
return CCIPSpec{}
func GenerateJobSpecs(capReg common.Address) (CCIPSpec, error) {
return CCIPSpec{}, nil
}

type DeployCCIPContractConfig struct {
Expand All @@ -66,21 +70,21 @@ type DeployCCIPContractConfig struct {
// For example a list of contracts to skip deploying if they already exist.
// Or mock vs real RMN.
// Deployment produces an address book of everything it deployed.
func DeployCCIPContracts(e environment.Environment, c DeployCCIPContractConfig) (environment.AddressBook, error) {
ab := environment.NewMemoryAddressBook()
func DeployCCIPContracts(e deployment.Environment, c DeployCCIPContractConfig) (deployment.AddressBook, error) {
ab := deployment.NewMemoryAddressBook()
for _, chain := range e.Chains {
saveToChain := func(addr common.Address) error {
return ab.Save(chain.Selector, addr.String())
saveToChain := func(addr common.Address, tv string) error {
return ab.Save(chain.Selector, addr.String(), tv)
}

// TODO: Still waiting for RMNRemote/RMNHome contracts etc.
mockARM, err := deployContract(e.Logger,
func() (common.Address, common.Hash, error) {
func() (common.Address, string, common.Hash, error) {
mockARM, tx, _, err := mock_arm_contract.DeployMockARMContract(
chain.DeployerKey,
chain.Client,
)
return mockARM, tx.Hash(), err
return mockARM, MockARM_1_0_0, tx.Hash(), err
}, chain.Confirm, saveToChain)
if err != nil {
e.Logger.Errorw("Failed to deploy mockARM", "err", err)
Expand All @@ -89,42 +93,42 @@ func DeployCCIPContracts(e environment.Environment, c DeployCCIPContractConfig)
e.Logger.Infow("deployed mockARM", "addr", mockARM)

armProxy, err := deployContract(e.Logger,
func() (common.Address, common.Hash, error) {
mockARM, tx, _, err := arm_proxy_contract.DeployARMProxyContract(
func() (common.Address, string, common.Hash, error) {
armProxy, tx, _, err := arm_proxy_contract.DeployARMProxyContract(
chain.DeployerKey,
chain.Client,
mockARM,
)
return mockARM, tx.Hash(), err
return armProxy, ARMProxy_1_1_0, tx.Hash(), err
}, chain.Confirm, saveToChain)
if err != nil {
e.Logger.Errorw("Failed to deploy armProxy", "err", err)
return ab, err
}
e.Logger.Infow("deployed armProxy", "addr", armProxy)

//weth9, err := deployContract(e.Logger,
// func() (common.Address, common.Hash, error) {
// weth9, tx, _, err := weth9.DeployWETH9(
// chain.DeployerKey,
// chain.Client,
// )
// return weth9, tx.Hash(), err
// }, chain.Confirm, saveToChain)
//if err != nil {
// e.Logger.Errorw("Failed to deploy weth9", "err", err)
// return err
//}
weth9, err := deployContract(e.Logger,
func() (common.Address, string, common.Hash, error) {
weth9, tx, _, err := weth9.DeployWETH9(
chain.DeployerKey,
chain.Client,
)
return weth9, WETH9_1_0_0, tx.Hash(), err
}, chain.Confirm, saveToChain)
if err != nil {
e.Logger.Errorw("Failed to deploy weth9", "err", err)
return ab, err
}

routerAddr, err := deployContract(e.Logger,
func() (common.Address, common.Hash, error) {
func() (common.Address, string, common.Hash, error) {
router, tx, _, err := router.DeployRouter(
chain.DeployerKey,
chain.Client,
common.HexToAddress("0x0"),
weth9,
armProxy,
)
return router, tx.Hash(), err
return router, Router_1_0_0, tx.Hash(), err
}, chain.Confirm, saveToChain)
if err != nil {
e.Logger.Errorw("Failed to deploy router", "err", err)
Expand All @@ -133,11 +137,11 @@ func DeployCCIPContracts(e environment.Environment, c DeployCCIPContractConfig)
e.Logger.Infow("deployed router", "addr", routerAddr)

tokenAdminRegistry, err := deployContract(e.Logger,
func() (common.Address, common.Hash, error) {
func() (common.Address, string, common.Hash, error) {
tokenAdminRegistry, tx, _, err := token_admin_registry.DeployTokenAdminRegistry(
chain.DeployerKey,
chain.Client)
return tokenAdminRegistry, tx.Hash(), err
return tokenAdminRegistry, TokenAdminRegistry_1_0_0, tx.Hash(), err
}, chain.Confirm, saveToChain)
if err != nil {
e.Logger.Errorw("Failed to deploy token admin registry", "err", err)
Expand Down
4 changes: 2 additions & 2 deletions core/capabilities/ccip/deployment/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink/v2/core/environment"
"github.com/smartcontractkit/chainlink/v2/core/deployment"
)

func TestDeployCCIPContracts(t *testing.T) {
e := environment.NewMemoryEnvironment(t, environment.MemoryEnvironmentConfig{
e := deployment.NewMemoryEnvironment(t, deployment.MemoryEnvironmentConfig{
Chains: 1,
Nodes: 1,
})
Expand Down
39 changes: 39 additions & 0 deletions core/capabilities/ccip/deployment/migrations/1_initial_deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package migrations

import (
"github.com/ethereum/go-ethereum/common"

ccipdeployment "github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/deployment"
"github.com/smartcontractkit/chainlink/v2/core/deployment"
)

// We expect the migration input to be unique per migration.
// TODO: Maybe there's a generics approach here?
func Apply0001(env deployment.Environment, c ccipdeployment.DeployCCIPContractConfig) (deployment.MigrationOutput, error) {
ab, err := ccipdeployment.DeployCCIPContracts(env, c)
if err != nil {
// If we fail here, just throw away the addresses.
// TODO: if expensive could consider partial recovery
env.Logger.Errorw("Failed to deploy CCIP contracts", "err", err, "addresses", ab)
return deployment.MigrationOutput{}, err
}
state, err := ccipdeployment.GenerateOnchainState(env, ab)
if err != nil {
return deployment.MigrationOutput{}, err
}
js, err := ccipdeployment.GenerateJobSpecs(common.Address{})
if err != nil {
return deployment.MigrationOutput{}, err
}
proposal, err := ccipdeployment.GenerateAcceptOwnershipProposal(env, env.AllChainSelectors(), state)
if err != nil {
return deployment.MigrationOutput{}, err
}
return deployment.MigrationOutput{
Proposals: []deployment.Proposal{proposal},
AddressBook: ab,
JobSpecs: map[string][]string{
"chain-layer": {js.String()},
},
}, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package migrations

import (
"context"
"testing"

"github.com/stretchr/testify/require"

ccipdeployment "github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/deployment"
"github.com/smartcontractkit/chainlink/v2/core/deployment"
)

func Test0001_InitialDeploy(t *testing.T) {
t.Skip() // WIP
e := deployment.NewMemoryEnvironment(t, deployment.MemoryEnvironmentConfig{
Chains: 1,
Nodes: 1,
})
// Apply migration
output, err := Apply0001(e, ccipdeployment.DeployCCIPContractConfig{})
require.NoError(t, err)

state, err := ccipdeployment.GenerateOnchainState(e, output.AddressBook)
require.NoError(t, err)

// TODO: Validate jobs
// Apply jobs
for nodeIDs, jobs := range output.JobSpecs {
for _, job := range jobs {
_, err := e.Offchain.ProposeJob(context.Background(), nodeIDs, job)
require.NoError(t, err)
}
}
// TODO: Inspect proposal
// Apply proposal
require.NoError(t, ccipdeployment.ApplyProposal(e, output.Proposals[0], state))

// TODO: Inspect onchain state
// TODO: Send traffic

//snap, err := state.Snapshot(e.AllChainSelectors())
//require.NoError(t, err)
//
//// Assert expect every deployed address to be in the address book.
//for name, chain := range snap.Chains {
// addrs, err := ab.Addresses()
// require.NoError(t, err)
// evmChainID, _ := chainsel.ChainIdFromName(name)
// sel, _ := chainsel.SelectorFromChainId(evmChainID)
// assert.Contains(t, addrs[sel], chain.TokenAdminRegistry.String())
//}
}
46 changes: 46 additions & 0 deletions core/capabilities/ccip/deployment/propose.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package deployment

import (
"math/big"
"time"

"github.com/ethereum/go-ethereum/common"
chainsel "github.com/smartcontractkit/chain-selectors"

"github.com/smartcontractkit/chainlink/v2/core/deployment"
)

func GenerateAcceptOwnershipProposal(
e deployment.Environment,
chains []uint64,
state CCIPOnChainState,
) (deployment.Proposal, error) {
// TODO: Just onramp as an example
var ops []deployment.ManyChainMultiSigOp
for _, sel := range chains {
e.Chains[sel].DeployerKey.NoSend = true
txData, err := state.EvmOnRampsV160[sel].AcceptOwnership(e.Chains[sel].DeployerKey)
if err != nil {
return deployment.Proposal{}, err
}
evmID, err := chainsel.ChainIdFromSelector(sel)
if err != nil {
return deployment.Proposal{}, err
}
ops = append(ops, deployment.ManyChainMultiSigOp{
ChainId: big.NewInt(int64(evmID)),
MultiSig: common.Address{},
Nonce: big.NewInt(0),
To: state.EvmOnRampsV160[sel].Address(),
Value: big.NewInt(0),
Data: txData.Data(),
})
}
// TODO: Real valid until.
return deployment.Proposal{ValidUntil: uint32(time.Now().Unix()), Ops: ops}, nil
}

func ApplyProposal(env deployment.Environment, p deployment.Proposal, state CCIPOnChainState) error {
// TODO
return nil
}
Loading

0 comments on commit 4d7eae6

Please sign in to comment.