|
| 1 | +package blockchain |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "time" |
| 11 | + |
| 12 | + networkTypes "github.com/docker/docker/api/types/network" |
| 13 | + "github.com/docker/docker/client" |
| 14 | + "github.com/docker/go-connections/nat" |
| 15 | + "github.com/testcontainers/testcontainers-go/modules/compose" |
| 16 | + "github.com/testcontainers/testcontainers-go/wait" |
| 17 | + |
| 18 | + "github.com/smartcontractkit/chainlink-testing-framework/framework" |
| 19 | +) |
| 20 | + |
| 21 | +const ( |
| 22 | + // default ports from mylocalton-docker |
| 23 | + DefaultTonHTTPAPIPort = "8081" |
| 24 | + DefaultTonSimpleServerPort = "8000" |
| 25 | + DefaultTonTONExplorerPort = "8080" |
| 26 | + DefaultTonLiteServerPort = "40004" |
| 27 | + |
| 28 | + // NOTE: Prefunded high-load wallet from MyLocalTon pre-funded wallet, that can send up to 254 messages per 1 external message |
| 29 | + // https://docs.ton.org/v3/documentation/smart-contracts/contracts-specs/highload-wallet#highload-wallet-v2 |
| 30 | + DefaultTonHlWalletAddress = "-1:5ee77ced0b7ae6ef88ab3f4350d8872c64667ffbe76073455215d3cdfab3294b" |
| 31 | + DefaultTonHlWalletMnemonic = "twenty unfair stay entry during please water april fabric morning length lumber style tomorrow melody similar forum width ride render void rather custom coin" |
| 32 | +) |
| 33 | + |
| 34 | +func defaultTon(in *Input) { |
| 35 | + if in.DockerComposeFileURL == "" { |
| 36 | + in.DockerComposeFileURL = "https://raw.githubusercontent.com/neodix42/mylocalton-docker/main/docker-compose.yaml" |
| 37 | + } |
| 38 | + // Note: in local env having all services could be useful(explorer, faucet), in CI we need only core services |
| 39 | + if os.Getenv("CI") == "true" && len(in.TonCoreServices) == 0 { |
| 40 | + // Note: mylocalton-docker's essential services, excluded explorer, restarter, faucet app, |
| 41 | + in.TonCoreServices = []string{ |
| 42 | + "genesis", "tonhttpapi", "event-cache", |
| 43 | + "index-postgres", "index-worker", "index-api", |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func newTon(in *Input) (*Output, error) { |
| 49 | + defaultTon(in) |
| 50 | + containerName := framework.DefaultTCName("blockchain-node") |
| 51 | + |
| 52 | + resp, err := http.Get(in.DockerComposeFileURL) |
| 53 | + if err != nil { |
| 54 | + return nil, fmt.Errorf("failed to download docker-compose file: %v", err) |
| 55 | + } |
| 56 | + defer resp.Body.Close() |
| 57 | + |
| 58 | + tempDir, err := os.MkdirTemp(".", "ton-mylocalton-docker") |
| 59 | + if err != nil { |
| 60 | + return nil, fmt.Errorf("failed to create temp directory: %v", err) |
| 61 | + } |
| 62 | + |
| 63 | + defer func() { |
| 64 | + // delete the folder whether it was successful or not |
| 65 | + _ = os.RemoveAll(tempDir) |
| 66 | + }() |
| 67 | + |
| 68 | + composeFile := filepath.Join(tempDir, "docker-compose.yaml") |
| 69 | + file, err := os.Create(composeFile) |
| 70 | + if err != nil { |
| 71 | + return nil, fmt.Errorf("failed to create compose file: %v", err) |
| 72 | + } |
| 73 | + |
| 74 | + _, err = io.Copy(file, resp.Body) |
| 75 | + if err != nil { |
| 76 | + file.Close() |
| 77 | + return nil, fmt.Errorf("failed to write compose file: %v", err) |
| 78 | + } |
| 79 | + file.Close() |
| 80 | + |
| 81 | + ctx := context.Background() |
| 82 | + |
| 83 | + var stack compose.ComposeStack |
| 84 | + stack, err = compose.NewDockerComposeWith( |
| 85 | + compose.WithStackFiles(composeFile), |
| 86 | + compose.StackIdentifier(containerName), |
| 87 | + ) |
| 88 | + if err != nil { |
| 89 | + return nil, fmt.Errorf("failed to create compose stack: %v", err) |
| 90 | + } |
| 91 | + |
| 92 | + var upOpts []compose.StackUpOption |
| 93 | + upOpts = append(upOpts, compose.Wait(true)) |
| 94 | + |
| 95 | + if len(in.TonCoreServices) > 0 { |
| 96 | + upOpts = append(upOpts, compose.RunServices(in.TonCoreServices...)) |
| 97 | + } |
| 98 | + |
| 99 | + // always wait for healthy |
| 100 | + const genesisBlockID = "E7XwFSQzNkcRepUC23J2nRpASXpnsEKmyyHYV4u/FZY=" |
| 101 | + execStrat := wait.ForExec([]string{ |
| 102 | + "/usr/local/bin/lite-client", |
| 103 | + "-a", "127.0.0.1:" + DefaultTonLiteServerPort, |
| 104 | + "-b", genesisBlockID, |
| 105 | + "-t", "3", |
| 106 | + "-c", "last", |
| 107 | + }). |
| 108 | + WithPollInterval(5 * time.Second). |
| 109 | + WithStartupTimeout(180 * time.Second) |
| 110 | + |
| 111 | + stack = stack. |
| 112 | + WaitForService("genesis", execStrat). |
| 113 | + WaitForService("tonhttpapi", wait.ForListeningPort(DefaultTonHTTPAPIPort+"/tcp")) |
| 114 | + |
| 115 | + if err := stack.Up(ctx, upOpts...); err != nil { |
| 116 | + return nil, fmt.Errorf("failed to start compose stack: %w", err) |
| 117 | + } |
| 118 | + |
| 119 | + // node container is started, now we need to connect it to the network |
| 120 | + genesisCtr, _ := stack.ServiceContainer(ctx, "genesis") |
| 121 | + |
| 122 | + // grab and connect to the network |
| 123 | + cli, _ := client.NewClientWithOpts( |
| 124 | + client.FromEnv, |
| 125 | + client.WithAPIVersionNegotiation(), |
| 126 | + ) |
| 127 | + if err := cli.NetworkConnect( |
| 128 | + ctx, |
| 129 | + framework.DefaultNetworkName, |
| 130 | + genesisCtr.ID, |
| 131 | + &networkTypes.EndpointSettings{ |
| 132 | + Aliases: []string{containerName}, |
| 133 | + }, |
| 134 | + ); err != nil { |
| 135 | + return nil, fmt.Errorf("failed to connect to network: %v", err) |
| 136 | + } |
| 137 | + |
| 138 | + // verify that the container is connected to the network |
| 139 | + inspected, err := cli.ContainerInspect(ctx, genesisCtr.ID) |
| 140 | + if err != nil { |
| 141 | + return nil, fmt.Errorf("inspect error: %w", err) |
| 142 | + } |
| 143 | + |
| 144 | + ns, ok := inspected.NetworkSettings.Networks[framework.DefaultNetworkName] |
| 145 | + if !ok { |
| 146 | + return nil, fmt.Errorf("container %s is NOT on network %s", genesisCtr.ID, framework.DefaultNetworkName) |
| 147 | + } |
| 148 | + |
| 149 | + fmt.Printf("✅ TON genesis '%s' is on network %s with IP %s and Aliases %v\n", |
| 150 | + genesisCtr.ID, framework.DefaultNetworkName, ns.IPAddress, ns.Aliases) |
| 151 | + |
| 152 | + httpHost, _ := genesisCtr.Host(ctx) |
| 153 | + httpPort, _ := genesisCtr.MappedPort(ctx, nat.Port(fmt.Sprintf("%s/tcp", DefaultTonSimpleServerPort))) |
| 154 | + |
| 155 | + return &Output{ |
| 156 | + UseCache: true, |
| 157 | + ChainID: in.ChainID, |
| 158 | + Type: in.Type, |
| 159 | + Family: FamilyTon, |
| 160 | + ContainerName: containerName, |
| 161 | + // Note: in case we need 1+ validators, we need to modify the compose file |
| 162 | + Nodes: []*Node{{ |
| 163 | + // Note: define if we need more access other than the global config(tonutils-go only uses liteclients defined in the config) |
| 164 | + ExternalHTTPUrl: fmt.Sprintf("%s:%s", httpHost, httpPort.Port()), |
| 165 | + InternalHTTPUrl: fmt.Sprintf("%s:%s", containerName, httpPort.Port()), |
| 166 | + }}, |
| 167 | + }, nil |
| 168 | +} |
0 commit comments