|
| 1 | +package blockchain |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strconv" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/docker/docker/api/types/container" |
| 12 | + "github.com/docker/docker/api/types/mount" |
| 13 | + "github.com/docker/go-connections/nat" |
| 14 | + "github.com/testcontainers/testcontainers-go" |
| 15 | + "github.com/testcontainers/testcontainers-go/wait" |
| 16 | + |
| 17 | + "github.com/smartcontractkit/chainlink-testing-framework/framework" |
| 18 | +) |
| 19 | + |
| 20 | +var configYmlRaw = ` |
| 21 | +json_rpc_url: http://0.0.0.0:%s |
| 22 | +websocket_url: ws://0.0.0.0:%s |
| 23 | +keypair_path: /root/.config/solana/cli/id.json |
| 24 | +address_labels: |
| 25 | + "11111111111111111111111111111111": "" |
| 26 | +commitment: finalized |
| 27 | +` |
| 28 | + |
| 29 | +var idJSONRaw = ` |
| 30 | +[11,2,35,236,230,251,215,68,220,208,166,157,229,181,164,26,150,230,218,229,41,20,235,80,183,97,20,117,191,159,228,243,130,101,145,43,51,163,139,142,11,174,113,54,206,213,188,127,131,147,154,31,176,81,181,147,78,226,25,216,193,243,136,149] |
| 31 | +` |
| 32 | + |
| 33 | +func defaultSolana(in *Input) { |
| 34 | + ci := os.Getenv("CI") == "true" |
| 35 | + if in.Image == "" && !ci { |
| 36 | + in.Image = "f4hrenh9it/solana" |
| 37 | + } |
| 38 | + if in.Image == "" && ci { |
| 39 | + in.Image = "solanalabs/solana:v1.18.26" |
| 40 | + } |
| 41 | + if in.Port == "" { |
| 42 | + in.Port = "8545" |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func newSolana(in *Input) (*Output, error) { |
| 47 | + defaultSolana(in) |
| 48 | + ctx := context.Background() |
| 49 | + containerName := framework.DefaultTCName("blockchain-node") |
| 50 | + // Solana do not allow to set ws port, it just uses --rpc-port=N and sets WS as N+1 automatically |
| 51 | + bindPort := fmt.Sprintf("%s/tcp", in.Port) |
| 52 | + pp, err := strconv.Atoi(in.Port) |
| 53 | + if err != nil { |
| 54 | + return nil, fmt.Errorf("in.Port is not a number") |
| 55 | + } |
| 56 | + in.WSPort = strconv.Itoa(pp + 1) |
| 57 | + wsBindPort := fmt.Sprintf("%s/tcp", in.WSPort) |
| 58 | + |
| 59 | + configYml, err := os.CreateTemp("", "config.yml") |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + configYmlRaw = fmt.Sprintf(configYmlRaw, in.Port, in.WSPort) |
| 64 | + _, err = configYml.WriteString(configYmlRaw) |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + |
| 69 | + idJSON, err := os.CreateTemp("", "id.json") |
| 70 | + if err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + _, err = idJSON.WriteString(idJSONRaw) |
| 74 | + if err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + |
| 78 | + contractsDir, err := filepath.Abs(in.ContractsDir) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + |
| 83 | + req := testcontainers.ContainerRequest{ |
| 84 | + AlwaysPullImage: in.PullImage, |
| 85 | + Image: in.Image, |
| 86 | + Labels: framework.DefaultTCLabels(), |
| 87 | + Name: containerName, |
| 88 | + ExposedPorts: []string{bindPort, wsBindPort}, |
| 89 | + Networks: []string{framework.DefaultNetworkName}, |
| 90 | + NetworkAliases: map[string][]string{ |
| 91 | + framework.DefaultNetworkName: {containerName}, |
| 92 | + }, |
| 93 | + WaitingFor: wait.ForLog("Processed Slot: 1"). |
| 94 | + WithStartupTimeout(30 * time.Second). |
| 95 | + WithPollInterval(100 * time.Millisecond), |
| 96 | + HostConfigModifier: func(h *container.HostConfig) { |
| 97 | + h.PortBindings = nat.PortMap{ |
| 98 | + nat.Port(bindPort): []nat.PortBinding{ |
| 99 | + { |
| 100 | + HostIP: "0.0.0.0", |
| 101 | + HostPort: bindPort, |
| 102 | + }, |
| 103 | + }, |
| 104 | + nat.Port(wsBindPort): []nat.PortBinding{ |
| 105 | + { |
| 106 | + HostIP: "0.0.0.0", |
| 107 | + HostPort: wsBindPort, |
| 108 | + }, |
| 109 | + }, |
| 110 | + } |
| 111 | + h.Mounts = append(h.Mounts, mount.Mount{ |
| 112 | + Type: mount.TypeBind, |
| 113 | + Source: contractsDir, |
| 114 | + Target: "/programs", |
| 115 | + ReadOnly: false, |
| 116 | + }) |
| 117 | + }, |
| 118 | + Files: []testcontainers.ContainerFile{ |
| 119 | + { |
| 120 | + HostFilePath: configYml.Name(), |
| 121 | + ContainerFilePath: "/root/.config/solana/cli/config.yml", |
| 122 | + FileMode: 0644, |
| 123 | + }, |
| 124 | + { |
| 125 | + HostFilePath: idJSON.Name(), |
| 126 | + ContainerFilePath: "/root/.config/solana/cli/id.json", |
| 127 | + FileMode: 0644, |
| 128 | + }, |
| 129 | + }, |
| 130 | + Entrypoint: []string{"sh", "-c", fmt.Sprintf("mkdir -p /root/.config/solana/cli && solana-test-validator --rpc-port %s --mint %s", in.Port, in.PublicKey)}, |
| 131 | + } |
| 132 | + |
| 133 | + c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ |
| 134 | + ContainerRequest: req, |
| 135 | + Started: true, |
| 136 | + }) |
| 137 | + if err != nil { |
| 138 | + return nil, err |
| 139 | + } |
| 140 | + host, err := framework.GetHost(c) |
| 141 | + if err != nil { |
| 142 | + return nil, err |
| 143 | + } |
| 144 | + |
| 145 | + return &Output{ |
| 146 | + UseCache: true, |
| 147 | + Family: "solana", |
| 148 | + ContainerName: containerName, |
| 149 | + Nodes: []*Node{ |
| 150 | + { |
| 151 | + HostWSUrl: fmt.Sprintf("ws://%s:%s", host, in.WSPort), |
| 152 | + HostHTTPUrl: fmt.Sprintf("http://%s:%s", host, in.Port), |
| 153 | + DockerInternalWSUrl: fmt.Sprintf("ws://%s:%s", containerName, in.WSPort), |
| 154 | + DockerInternalHTTPUrl: fmt.Sprintf("http://%s:%s", containerName, in.Port), |
| 155 | + }, |
| 156 | + }, |
| 157 | + }, nil |
| 158 | +} |
0 commit comments