Skip to content

Commit

Permalink
ignoring keypair if not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
yashnevatia committed Jan 10, 2025
1 parent c97c9aa commit dc0c1a7
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 87 deletions.
98 changes: 62 additions & 36 deletions deployment/ccip/changeset/cs_deploy_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,17 @@ func solRouterProgramData(e deployment.Environment, chain deployment.SolChain, c
return programData, nil
}

func checkRouterInitialized(e deployment.Environment, chain deployment.SolChain, ccipRouterProgram solana.PublicKey) (bool, error) {
routerConfigPDA := GetRouterConfigPDA(ccipRouterProgram)
routerConfigInfo, err := chain.Client.GetAccountInfoWithOpts(e.GetContext(), routerConfigPDA, &solRpc.GetAccountInfoOpts{
Commitment: solRpc.CommitmentConfirmed,
})
if err != nil {
return false, nil
}
return routerConfigInfo != nil && len(routerConfigInfo.Value.Data.GetBinary()) > 0, nil
}

func deployChainContractsSolana(
e deployment.Environment,
chain deployment.SolChain,
Expand All @@ -490,8 +501,9 @@ func deployChainContractsSolana(
linkTokenContract := chainState.LinkToken
e.Logger.Infow("link token", "addr", linkTokenContract.String())

var ccipRouterProgram solana.PublicKey
if chainState.SolCcipRouter.IsZero() {
// deploy and initialize router
//deploy router
programID, err := chain.DeployProgram(e.Logger, "ccip_router")
if err != nil {
return fmt.Errorf("failed to deploy program: %w", err)
Expand All @@ -500,47 +512,61 @@ func deployChainContractsSolana(
tv := deployment.NewTypeAndVersion("SolCcipRouter", deployment.Version1_0_0)
e.Logger.Infow("Deployed contract", "Contract", tv.String(), "addr", programID, "chain", chain.String())

ccipRouterProgram := solana.MustPublicKeyFromBase58(programID)
programData, err := solRouterProgramData(e, chain, ccipRouterProgram)
ccipRouterProgram = solana.MustPublicKeyFromBase58(programID)
err = ab.Save(chain.Selector, programID, tv)
if err != nil {
return fmt.Errorf("failed to get solana router program data: %w", err)
return fmt.Errorf("failed to save address: %w", err)
}
} else {
e.Logger.Infow("Using existing router", "addr", chainState.SolCcipRouter.String())
ccipRouterProgram = chainState.SolCcipRouter
}
ccip_router.SetProgramID(ccipRouterProgram)

ccip_router.SetProgramID(ccipRouterProgram)

defaultGasLimit := solBinary.Uint128{Lo: 3000, Hi: 0, Endianness: nil}

instruction, err := ccip_router.NewInitializeInstruction(
chain.Selector, // chain selector
defaultGasLimit, // default gas limit
true, // allow out of order execution
EnableExecutionAfter, // period to wait before allowing manual execution
solana.PublicKey{},
GetRouterConfigPDA(ccipRouterProgram),
GetRouterStatePDA(ccipRouterProgram),
chain.DeployerKey.PublicKey(),
solana.SystemProgramID,
ccipRouterProgram,
programData.Address,
GetExternalExecutionConfigPDA(ccipRouterProgram),
GetExternalTokenPoolsSignerPDA(ccipRouterProgram),
).ValidateAndBuild()
// check if solana router is initalised
initialized, err := checkRouterInitialized(e, chain, ccipRouterProgram)
if err != nil {
return err
}
if initialized {
e.Logger.Infow("Router already initialized, skipping initialization", "chain", chain.String())
return nil
}

if err != nil {
return fmt.Errorf("failed to build instruction: %w", err)
}
err = chain.Confirm([]solana.Instruction{instruction})
programData, err := solRouterProgramData(e, chain, ccipRouterProgram)
if err != nil {
return fmt.Errorf("failed to get solana router program data: %w", err)
}

defaultGasLimit := solBinary.Uint128{Lo: 3000, Hi: 0, Endianness: nil}

instruction, err := ccip_router.NewInitializeInstruction(
chain.Selector, // chain selector
defaultGasLimit, // default gas limit
true, // allow out of order execution
EnableExecutionAfter, // period to wait before allowing manual execution
solana.PublicKey{},
GetRouterConfigPDA(ccipRouterProgram),
GetRouterStatePDA(ccipRouterProgram),
chain.DeployerKey.PublicKey(),
solana.SystemProgramID,
ccipRouterProgram,
programData.Address,
GetExternalExecutionConfigPDA(ccipRouterProgram),
GetExternalTokenPoolsSignerPDA(ccipRouterProgram),
).ValidateAndBuild()

if err != nil {
return fmt.Errorf("failed to confirm instructions: %w", err)
}
if err != nil {
return fmt.Errorf("failed to build instruction: %w", err)
}
err = chain.Confirm([]solana.Instruction{instruction})

err = ab.Save(chain.Selector, programID, tv)
if err != nil {
return fmt.Errorf("failed to save address: %w", err)
}
//TODO: deploy token pool contract
//TODO: log errors
if err != nil {
return fmt.Errorf("failed to confirm instructions: %w", err)
}

//TODO: deploy token pool contract
//TODO: log errors

return nil
}
42 changes: 2 additions & 40 deletions deployment/ccip/changeset/cs_deploy_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ package changeset
import (
"encoding/json"
"fmt"
"os"
"testing"

"github.com/mr-tron/base58"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"

"github.com/gagliardetto/solana-go"

"github.com/smartcontractkit/chainlink/deployment"
commonchangeset "github.com/smartcontractkit/chainlink/deployment/common/changeset"
"github.com/smartcontractkit/chainlink/deployment/common/proposalutils"
Expand Down Expand Up @@ -48,6 +44,8 @@ func TestDeployChainContractsChangeset(t *testing.T) {
ChainSelector: chain,
})
}

SavePreloadedSolAddresses(e, solChainSelectors[0])
e, err = commonchangeset.ApplyChangesets(t, e, nil, []commonchangeset.ChangesetApplication{
{
Changeset: commonchangeset.WrapChangeSet(DeployHomeChain),
Expand Down Expand Up @@ -131,39 +129,3 @@ func TestDeployCCIPContracts(t *testing.T) {
require.NoError(t, err)
fmt.Println(string(b))
}

// IGNORE
func TestSolanaKeygen(t *testing.T) {
privateKey, _ := solana.NewRandomPrivateKey()
fmt.Println(privateKey.String())

// Decode the Base58 private key
privateKeyBytes, err := base58.Decode(privateKey.String())
if err != nil {
fmt.Printf("Error decoding Base58 private key: %v\n", err)
return
}
fmt.Printf("Bytes after decode: %v\n", privateKeyBytes)

// Convert bytes to array of integers
intArray := make([]int, len(privateKeyBytes))
for i, b := range privateKeyBytes {
intArray[i] = int(b)
}

// Marshal the integer array to JSON
keypairJSON, err := json.Marshal(intArray)
if err != nil {
fmt.Printf("Error marshaling to JSON: %v\n", err)
return
}
outputFilePath := "/Users/yashvardhan/.config/solana/myid.json"
if err := os.WriteFile(outputFilePath, keypairJSON, 0600); err != nil {
fmt.Printf("Error writing keypair to file: %v\n", err)
return
}

pk, err := solana.PrivateKeyFromSolanaKeygenFile(outputFilePath)
require.NoError(t, err)
require.Equal(t, pk.String(), privateKey.String())
}

This file was deleted.

6 changes: 6 additions & 0 deletions deployment/ccip/changeset/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1257,3 +1257,9 @@ func DefaultRouterMessage(receiverAddress common.Address) router.ClientEVM2AnyMe
ExtraArgs: nil,
}
}

func SavePreloadedSolAddresses(e deployment.Environment, solChainSelector uint64) {
tv := deployment.NewTypeAndVersion("SolCcipRouter", deployment.Version1_0_0)
// TODO: this should be solTestConfig.CCIPRouterProgram
e.ExistingAddresses.Save(solChainSelector, "AmTB9SpwRjjKd3dHjFJiQoVt2bSzbzFnzBHCSpX4k9MW", tv)
}
18 changes: 13 additions & 5 deletions deployment/environment/memory/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"

solTestConfig "github.com/smartcontractkit/chainlink-ccip/chains/solana/contracts/tests/config"
"github.com/smartcontractkit/chainlink-testing-framework/framework"
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/blockchain"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
Expand Down Expand Up @@ -195,12 +196,19 @@ func solChain(t *testing.T, chainID uint64, adminKey *solana.PrivateKey) (string

port := freeport.GetOne(t)

fmt.Println(solTestConfig.CcipRouterProgram.String())

bcInput := &blockchain.Input{
Type: "solana",
ChainID: strconv.FormatUint(chainID, 10),
PublicKey: adminKey.PublicKey().String(),
Port: strconv.Itoa(port),
// TODO: ContractsDir & SolanaPrograms via env vars
Type: "solana",
ChainID: strconv.FormatUint(chainID, 10),
PublicKey: adminKey.PublicKey().String(),
Port: strconv.Itoa(port),
ContractsDir: ProgramsPath,
// TODO: this should be solTestConfig.CCIPRouterProgram
// TODO: make this a function
SolanaPrograms: map[string]string{
"ccip_router": "AmTB9SpwRjjKd3dHjFJiQoVt2bSzbzFnzBHCSpX4k9MW",
},
}
output, err := blockchain.NewBlockchainNetwork(bcInput)
require.NoError(t, err)
Expand Down
30 changes: 25 additions & 5 deletions deployment/solana_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package deployment
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -55,12 +57,30 @@ func (c SolChain) Name() string {
}

func (c SolChain) DeployProgram(logger logger.Logger, programName string) (string, error) {
programFile := fmt.Sprintf("%s/%s.so", c.ProgramsPath, programName)
programKeyPair := fmt.Sprintf("%s/%s-keypair.json", c.ProgramsPath, programName)
programFile := filepath.Join(c.ProgramsPath, programName+".so")
programKeyPair := filepath.Join(c.ProgramsPath, programName+"-keypair.json")

logger.Infow("Deploying program", "programFile", programFile, "programKeyPair", programKeyPair, "keypairPath", c.KeypairPath, "url", c.URL)
// Construct the CLI command: solana program deploy
cmd := exec.Command("solana", "program", "deploy", programFile, "--program-id", programKeyPair, "--keypair", c.KeypairPath, "--url", c.URL)
// Base command with required args
baseArgs := []string{
"program", "deploy",
programFile, //.so file
"--keypair", c.KeypairPath, //admin, upgradeAuthority
"--url", c.URL, //rpc url
}

var cmd *exec.Cmd
if _, err := os.Stat(programKeyPair); err == nil {
// Keypair exists, include program-id
logger.Infow("Deploying program with existing keypair",
"programFile", programFile,
"programKeyPair", programKeyPair)
cmd = exec.Command("solana", append(baseArgs, "--program-id", programKeyPair)...)
} else {
// Keypairs wont be created for devenvs
logger.Infow("Deploying new program",
"programFile", programFile)
cmd = exec.Command("solana", baseArgs...)
}

// Capture the command output
var stdout, stderr bytes.Buffer
Expand Down

0 comments on commit dc0c1a7

Please sign in to comment.