diff --git a/deployment/environment/memory/chain.go b/deployment/environment/memory/chain.go index 020c6df375a..8d0bfd38840 100644 --- a/deployment/environment/memory/chain.go +++ b/deployment/environment/memory/chain.go @@ -40,7 +40,7 @@ type EVMChain struct { Users []*bind.TransactOpts } -type SolanaChain struct { +type SolanaChainConfig struct { Client *solRpc.Client DeployerKey solana.PrivateKey URL string @@ -87,13 +87,10 @@ func getTestSolanaChainSelectors() []uint64 { return result } -func generateSolanaKeypair(t testing.TB) (solana.PrivateKey, string, error) { - // Create a temporary directory that will be cleaned up after the test - tmpDir := t.TempDir() - +func GenerateSolanaKeypair(dir string) (solana.PrivateKey, error) { privateKey, err := solana.NewRandomPrivateKey() if err != nil { - return solana.PrivateKey{}, "", fmt.Errorf("failed to generate private key: %w", err) + return solana.PrivateKey{}, fmt.Errorf("failed to generate private key: %w", err) } // Convert private key bytes to JSON array @@ -107,27 +104,31 @@ func generateSolanaKeypair(t testing.TB) (solana.PrivateKey, string, error) { keypairJSON, err := json.Marshal(intArray) if err != nil { - return solana.PrivateKey{}, "", fmt.Errorf("failed to marshal keypair: %w", err) + return solana.PrivateKey{}, fmt.Errorf("failed to marshal keypair: %w", err) } - // Create the keypair file in the temporary directory - keypairPath := filepath.Join(tmpDir, "solana-keypair.json") + // Create the keypair file in the directory + keypairPath := filepath.Join(dir, "solana-keypair.json") if err := os.WriteFile(keypairPath, keypairJSON, 0600); err != nil { - return solana.PrivateKey{}, "", fmt.Errorf("failed to write keypair to file: %w", err) + return solana.PrivateKey{}, fmt.Errorf("failed to write keypair to file: %w", err) } - return privateKey, keypairPath, nil + return privateKey, nil } -func GenerateChainsSol(t *testing.T, numChains int) map[uint64]SolanaChain { +func GenerateChainsSol(t *testing.T, numChains int) map[uint64]SolanaChainConfig { testSolanaChainSelectors := getTestSolanaChainSelectors() if len(testSolanaChainSelectors) < numChains { t.Fatalf("not enough test solana chain selectors available") } - chains := make(map[uint64]SolanaChain) + + // Create a temporary directory that will be cleaned up after the test + tmpDir := t.TempDir() + + chains := make(map[uint64]SolanaChainConfig) for i := 0; i < numChains; i++ { chainID := testSolanaChainSelectors[i] - admin, keypairPath, err := generateSolanaKeypair(t) + admin, err := GenerateSolanaKeypair(tmpDir) require.NoError(t, err) url, wsURL, err := solChain(t, chainID, &admin) require.NoError(t, err) @@ -135,12 +136,12 @@ func GenerateChainsSol(t *testing.T, numChains int) map[uint64]SolanaChain { balance, err := client.GetBalance(context.Background(), admin.PublicKey(), solRpc.CommitmentConfirmed) require.NoError(t, err) require.NotEqual(t, 0, balance.Value) // auto funded 500000000.000000000 SOL - chains[chainID] = SolanaChain{ + chains[chainID] = SolanaChainConfig{ Client: client, DeployerKey: admin, URL: url, WSURL: wsURL, - KeypairPath: keypairPath, + KeypairPath: tmpDir, } } return chains diff --git a/deployment/environment/memory/environment.go b/deployment/environment/memory/environment.go index 48fa7d71d46..72b84526a82 100644 --- a/deployment/environment/memory/environment.go +++ b/deployment/environment/memory/environment.go @@ -24,7 +24,6 @@ import ( solRpc "github.com/gagliardetto/solana-go/rpc" - solCommonUtil "github.com/smartcontractkit/chainlink-ccip/chains/solana/utils/common" "github.com/smartcontractkit/chainlink-common/pkg/logger" ) @@ -82,7 +81,36 @@ func NewMemoryChains(t *testing.T, numChains int, numUsers int) (map[uint64]depl func NewMemoryChainsSol(t *testing.T, numChains int) map[uint64]deployment.SolChain { mchains := GenerateChainsSol(t, numChains) - return generateMemoryChainSol(mchains) + + chains := make(map[uint64]deployment.SolChain) + + for cid, chainConfig := range mchains { + chains[cid] = deployment.SolChain{ + Selector: cid, + Client: chainConfig.Client, + DeployerKey: &chainConfig.DeployerKey, + URL: chainConfig.URL, + WSURL: chainConfig.WSURL, + KeypairPath: chainConfig.KeypairPath, + ProgramsPath: ProgramsPath, + Confirm: func(instructions []solana.Instruction, opts ...solCommomUtil.TxModifier) error { + _, err := solCommomUtil.SendAndConfirm( + context.Background(), + chainConfig.Client, + instructions, + chainConfig.DeployerKey, + solRpc.CommitmentConfirmed, + opts..., + ) + if err != nil { + return err + } + return nil + }, + } + } + + return chains } func NewMemoryChainsWithChainIDs(t *testing.T, chainIDs []uint64, numUsers int) (map[uint64]deployment.Chain, map[uint64][]*bind.TransactOpts) { @@ -137,29 +165,6 @@ func generateMemoryChain(t *testing.T, inputs map[uint64]EVMChain) map[uint64]de return chains } -func generateMemoryChainSol(inputs map[uint64]SolanaChain) map[uint64]deployment.SolChain { - chains := make(map[uint64]deployment.SolChain) - for cid, chain := range inputs { - chain := chain - chains[cid] = deployment.SolChain{ - Selector: cid, - Client: chain.Client, - DeployerKey: &chain.DeployerKey, - URL: chain.URL, - WSURL: chain.WSURL, - KeypairPath: chain.KeypairPath, - ProgramsPath: ProgramsPath, - Confirm: func(instructions []solana.Instruction, opts ...solCommonUtil.TxModifier) error { - _, err := solCommonUtil.SendAndConfirm( - context.Background(), chain.Client, instructions, chain.DeployerKey, solRpc.CommitmentConfirmed, opts..., - ) - return err - }, - } - } - return chains -} - func NewNodes(t *testing.T, logLevel zapcore.Level, chains map[uint64]deployment.Chain, solChains map[uint64]deployment.SolChain, numNodes, numBootstraps int, registryConfig deployment.CapabilityRegistryConfig) map[string]Node { nodesByPeerID := make(map[string]Node) if numNodes+numBootstraps == 0 {