Skip to content

Commit

Permalink
changing to ctf
Browse files Browse the repository at this point in the history
  • Loading branch information
yashnevatia committed Jan 9, 2025
1 parent b325e8b commit 31e8133
Show file tree
Hide file tree
Showing 11 changed files with 277 additions and 120 deletions.
12 changes: 6 additions & 6 deletions core/scripts/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ require (
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/bytecodealliance/wasmtime-go/v23 v23.0.0 // indirect
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/bytedance/sonic v1.12.3 // indirect
github.com/bytedance/sonic/loader v0.2.0 // indirect
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
Expand Down Expand Up @@ -135,7 +135,7 @@ require (
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gabriel-vasile/mimetype v1.4.6 // indirect
github.com/gagliardetto/binary v0.8.0 // indirect
github.com/gagliardetto/solana-go v1.12.0 // indirect
github.com/gagliardetto/treeout v0.1.4 // indirect
Expand Down Expand Up @@ -164,7 +164,7 @@ require (
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.22.0 // indirect
github.com/go-playground/validator/v10 v10.22.1 // indirect
github.com/go-viper/mapstructure/v2 v2.1.0 // indirect
github.com/go-webauthn/webauthn v0.9.4 // indirect
github.com/go-webauthn/x v0.1.5 // indirect
Expand Down Expand Up @@ -251,7 +251,7 @@ require (
github.com/maruel/natural v1.1.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mfridman/interpolate v0.0.2 // indirect
github.com/mimoo/StrobeGo v0.0.0-20210601165009-122bf33a46e0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
Expand Down Expand Up @@ -284,7 +284,7 @@ require (
github.com/prometheus/procfs v0.15.1 // indirect
github.com/prometheus/prometheus v0.54.1 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/rogpeppe/go-internal v1.13.1 // indirect
github.com/rs/cors v1.10.1 // indirect
Expand Down
73 changes: 60 additions & 13 deletions core/scripts/go.sum

Large diffs are not rendered by default.

117 changes: 110 additions & 7 deletions deployment/environment/memory/chain.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package memory

import (
"context"
"encoding/json"
"fmt"
"math/big"
"os"
"strconv"
"testing"
"time"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -11,15 +17,18 @@ import (
"github.com/ethereum/go-ethereum/ethclient/simulated"
"github.com/gagliardetto/solana-go"
solRpc "github.com/gagliardetto/solana-go/rpc"
"github.com/hashicorp/consul/sdk/freeport"
"github.com/mr-tron/base58"

"github.com/stretchr/testify/require"

solTestUtil "github.com/smartcontractkit/chainlink-ccip/chains/solana/contracts/tests/testutils"

chainsel "github.com/smartcontractkit/chain-selectors"

"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"

"github.com/smartcontractkit/chainlink-testing-framework/framework"
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/blockchain"
)

type EVMChain struct {
Expand All @@ -31,6 +40,9 @@ type EVMChain struct {
type SolanaChain struct {
Client *solRpc.Client
DeployerKey *solana.PrivateKey
URL string
WSURL string
KeypairPath string
}

func fundAddress(t *testing.T, from *bind.TransactOpts, to common.Address, amount *big.Int, backend *simulated.Backend) {
Expand All @@ -53,6 +65,45 @@ func fundAddress(t *testing.T, from *bind.TransactOpts, to common.Address, amoun
backend.Commit()
}

func generateAndStoreKeypair() (solana.PrivateKey, string, error) {
// Generate a random private key
privateKey, err := solana.NewRandomPrivateKey()
if err != nil {
return solana.PrivateKey{}, "", fmt.Errorf("failed to generate private key: %w", err)
}

privateKeyBytes, err := base58.Decode(privateKey.String())
if err != nil {
return solana.PrivateKey{}, "", fmt.Errorf("failed to decode Base58 private key: %w", err)
}

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 {
return solana.PrivateKey{}, "", fmt.Errorf("failed to marshal keypair to JSON: %w", err)
}

// Create a temporary file
tempFile, err := os.CreateTemp("", "solana-keypair-*.json")
if err != nil {
return solana.PrivateKey{}, "", fmt.Errorf("failed to create temporary file: %w", err)
}
defer tempFile.Close()

// Write the keypair data to the file
if err := os.WriteFile(tempFile.Name(), keypairJSON, 0600); err != nil {
return solana.PrivateKey{}, "", fmt.Errorf("failed to write keypair to file: %w", err)
}

// Return the path to the temporary file
return privateKey, tempFile.Name(), nil
}

func GenerateChains(t *testing.T, numChains int, numUsers int) map[uint64]EVMChain {
chains := make(map[uint64]EVMChain)
for i := 0; i < numChains; i++ {
Expand Down Expand Up @@ -80,13 +131,20 @@ func GenerateChainsSol(t *testing.T, numChains int) map[uint64]SolanaChain {
chains := make(map[uint64]SolanaChain)
for i := 0; i < numChains; i++ {
chainID := testSolanaChainSelectors[i]
url, _ := solTestUtil.SetupLocalSolNodeWithFlags(t)
admin, gerr := solana.NewRandomPrivateKey()
solTestUtil.FundTestAccounts(t, []solana.PublicKey{admin.PublicKey()}, url)
require.NoError(t, gerr)
admin, keypairPath, err := generateAndStoreKeypair()
require.NoError(t, err)
url, wsURL, err := solChain(t, chainID, &admin)
require.NoError(t, err)
client := solRpc.New(url)
balance, err := client.GetBalance(context.Background(), admin.PublicKey(), solRpc.CommitmentConfirmed)
require.NoError(t, err)
require.NotEqual(t, balance.Value, 0) // auto funded 500000000.000000000 SOL

Check failure on line 141 in deployment/environment/memory/chain.go

View workflow job for this annotation

GitHub Actions / GolangCI Lint (deployment)

expected-actual: need to reverse actual and expected values (testifylint)
chains[chainID] = SolanaChain{
Client: solRpc.New(url),
Client: client,
DeployerKey: &admin,
URL: url,
WSURL: wsURL,
KeypairPath: keypairPath,
}
}
return chains
Expand Down Expand Up @@ -126,3 +184,48 @@ func evmChain(t *testing.T, numUsers int) EVMChain {
Users: users,
}
}

func solChain(t *testing.T, chainID uint64, adminKey *solana.PrivateKey) (string, string, error) {
t.Helper()

// initialize the docker network used by CTF
// TODO: framework.DefaultNetwork(once) is broken for me, use a static name for now
framework.DefaultNetworkName = "chainlink"

port := freeport.GetOne(t)

bcInput := &blockchain.Input{
Type: "solana",
ChainID: strconv.FormatUint(chainID, 10),
PublicKey: adminKey.PublicKey().String(),
Port: strconv.Itoa(port),
// TODO: ContractsDir & SolanaPrograms via env vars
}
output, err := blockchain.NewBlockchainNetwork(bcInput)
require.NoError(t, err)
// TODO:cleanup the container

url := output.Nodes[0].HostHTTPUrl
wsURL := output.Nodes[0].HostWSUrl

// Wait for api server to boot
client := solRpc.New(url)
var ready bool
for i := 0; i < 30; i++ {
time.Sleep(time.Second)
out, err := client.GetHealth(tests.Context(t))
if err != nil || out != solRpc.HealthOk {
t.Logf("API server not ready yet (attempt %d)\n", i+1)
continue
}
ready = true
break
}
if !ready {
t.Logf("solana-test-validator is not ready after 30 attempts")
}
require.True(t, ready)
t.Logf("solana-test-validator is ready at %s", url)

return url, wsURL, nil
}
15 changes: 8 additions & 7 deletions deployment/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/google/uuid v1.6.0
github.com/hashicorp/consul/sdk v0.16.1
github.com/hashicorp/go-multierror v1.1.1
github.com/mr-tron/base58 v1.2.0
github.com/pelletier/go-toml/v2 v2.2.3
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.33.0
Expand All @@ -33,6 +34,7 @@ require (
github.com/smartcontractkit/chainlink-ccip/chains/solana v0.0.0-20250103152858-8973fd0c912b
github.com/smartcontractkit/chainlink-common v0.4.1-0.20241223143929-db7919d60550
github.com/smartcontractkit/chainlink-protos/job-distributor v0.6.0
github.com/smartcontractkit/chainlink-testing-framework/framework v0.4.1
github.com/smartcontractkit/chainlink-testing-framework/lib v1.50.13
github.com/smartcontractkit/libocr v0.0.0-20241223215956-e5b78d8e3919
github.com/stretchr/testify v1.10.0
Expand Down Expand Up @@ -117,8 +119,8 @@ require (
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/bytecodealliance/wasmtime-go/v23 v23.0.0 // indirect
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/bytedance/sonic v1.12.3 // indirect
github.com/bytedance/sonic/loader v0.2.0 // indirect
github.com/c2h5oh/datasize v0.0.0-20220606134207-859f65c6625b // indirect
github.com/c9s/goprocinfo v0.0.0-20210130143923-c95fcf8c64a8 // indirect
github.com/cdk8s-team/cdk8s-core-go/cdk8s/v2 v2.7.5 // indirect
Expand Down Expand Up @@ -188,7 +190,7 @@ require (
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gabriel-vasile/mimetype v1.4.6 // indirect
github.com/gagliardetto/binary v0.8.0 // indirect
github.com/gagliardetto/treeout v0.1.4 // indirect
github.com/gballet/go-libpcsclite v0.0.0-20191108122812-4678299bea08 // indirect
Expand Down Expand Up @@ -217,7 +219,7 @@ require (
github.com/go-openapi/validate v0.23.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.22.0 // indirect
github.com/go-playground/validator/v10 v10.22.1 // indirect
github.com/go-redis/redis/v8 v8.11.5 // indirect
github.com/go-viper/mapstructure/v2 v2.1.0 // indirect
github.com/go-webauthn/webauthn v0.9.4 // indirect
Expand Down Expand Up @@ -331,7 +333,7 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/miekg/dns v1.1.61 // indirect
github.com/mimoo/StrobeGo v0.0.0-20210601165009-122bf33a46e0 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
Expand All @@ -355,7 +357,6 @@ require (
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/mostynb/zstdpool-freelist v0.0.0-20201229113212-927304c0c3b1 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/mtibben/percent v0.2.1 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
Expand Down Expand Up @@ -387,7 +388,7 @@ require (
github.com/prometheus/procfs v0.15.1 // indirect
github.com/prometheus/prometheus v0.54.1 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/rogpeppe/go-internal v1.13.1 // indirect
github.com/rs/cors v1.10.1 // indirect
Expand Down
Loading

0 comments on commit 31e8133

Please sign in to comment.