|
| 1 | +package blockchain |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/docker/go-connections/nat" |
| 13 | + "github.com/smartcontractkit/chainlink-testing-framework/framework" |
| 14 | + |
| 15 | + "github.com/testcontainers/testcontainers-go/modules/compose" |
| 16 | + "github.com/testcontainers/testcontainers-go/wait" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + // default ports from mylocalton-docker |
| 21 | + DefaultTonHTTPAPIPort = "8081" |
| 22 | + DefaultTonSimpleServerPort = "8000" |
| 23 | + DefaultTonTONExplorerPort = "8080" |
| 24 | + DefaultTonLiteServerPort = "40004" |
| 25 | +) |
| 26 | + |
| 27 | +func defaultTon(in *Input) { |
| 28 | + if in.Image == "" { |
| 29 | + in.Image = "neodix42/mylocalton-docker:latest" |
| 30 | + } |
| 31 | + if in.Port != "" { |
| 32 | + framework.L.Warn().Msgf("'port' field is set but only default port can be used: %s", DefaultTonHTTPAPIPort) |
| 33 | + } |
| 34 | + in.Port = DefaultTonHTTPAPIPort |
| 35 | +} |
| 36 | + |
| 37 | +func newTon(in *Input) (*Output, error) { |
| 38 | + defaultTon(in) |
| 39 | + containerName := framework.DefaultTCName("blockchain-node") |
| 40 | + |
| 41 | + resp, err := http.Get("https://raw.githubusercontent.com/neodix42/mylocalton-docker/main/docker-compose.yaml") |
| 42 | + if err != nil { |
| 43 | + return nil, fmt.Errorf("failed to download docker-compose file: %v", err) |
| 44 | + } |
| 45 | + defer resp.Body.Close() |
| 46 | + |
| 47 | + tempDir, err := os.MkdirTemp(".", "ton-mylocalton-docker") |
| 48 | + if err != nil { |
| 49 | + return nil, fmt.Errorf("failed to create temp directory: %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + defer func() { |
| 53 | + // delete the folder whether it was successful or not |
| 54 | + _ = os.RemoveAll(tempDir) |
| 55 | + }() |
| 56 | + |
| 57 | + composeFile := filepath.Join(tempDir, "docker-compose.yaml") |
| 58 | + file, err := os.Create(composeFile) |
| 59 | + if err != nil { |
| 60 | + return nil, fmt.Errorf("failed to create compose file: %v", err) |
| 61 | + } |
| 62 | + |
| 63 | + _, err = io.Copy(file, resp.Body) |
| 64 | + if err != nil { |
| 65 | + file.Close() |
| 66 | + return nil, fmt.Errorf("failed to write compose file: %v", err) |
| 67 | + } |
| 68 | + file.Close() |
| 69 | + |
| 70 | + ctx := context.Background() |
| 71 | + |
| 72 | + var stack compose.ComposeStack |
| 73 | + stack, err = compose.NewDockerComposeWith( |
| 74 | + compose.WithStackFiles(composeFile), |
| 75 | + compose.StackIdentifier(containerName), |
| 76 | + ) |
| 77 | + if err != nil { |
| 78 | + return nil, fmt.Errorf("failed to create compose stack: %v", err) |
| 79 | + } |
| 80 | + |
| 81 | + var upOpts []compose.StackUpOption |
| 82 | + |
| 83 | + // always wait for healthy |
| 84 | + upOpts = append(upOpts, compose.Wait(true)) |
| 85 | + services := in.CoreServices |
| 86 | + if os.Getenv("CI") == "true" && len(services) == 0 { |
| 87 | + services = []string{ |
| 88 | + "genesis", "tonhttpapi", "event-cache", |
| 89 | + "index-postgres", "index-worker", "index-api", |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if len(services) > 0 { |
| 94 | + upOpts = append(upOpts, compose.RunServices(services...)) |
| 95 | + } |
| 96 | + |
| 97 | + const genesisBlockID = "E7XwFSQzNkcRepUC23J2nRpASXpnsEKmyyHYV4u/FZY=" |
| 98 | + execStrat := wait.ForExec([]string{ |
| 99 | + "/usr/local/bin/lite-client", |
| 100 | + "-a", "127.0.0.1:" + DefaultTonLiteServerPort, |
| 101 | + "-b", genesisBlockID, |
| 102 | + "-t", "3", |
| 103 | + "-c", "last", |
| 104 | + }). |
| 105 | + WithPollInterval(5 * time.Second). |
| 106 | + WithStartupTimeout(180 * time.Second) |
| 107 | + |
| 108 | + stack = stack. |
| 109 | + WaitForService("genesis", execStrat). |
| 110 | + WaitForService("tonhttpapi", wait.ForListeningPort(DefaultTonHTTPAPIPort+"/tcp")) |
| 111 | + |
| 112 | + if err := stack.Up(ctx, upOpts...); err != nil { |
| 113 | + return nil, fmt.Errorf("failed to start compose stack: %w", err) |
| 114 | + } |
| 115 | + cfgCtr, _ := stack.ServiceContainer(ctx, "genesis") |
| 116 | + cfgHost, _ := cfgCtr.Host(ctx) |
| 117 | + cfgPort, _ := cfgCtr.MappedPort(ctx, nat.Port("8000/tcp")) |
| 118 | + globalCfgURL := fmt.Sprintf("http://%s:%s/localhost.global.config.json", cfgHost, cfgPort.Port()) |
| 119 | + |
| 120 | + // discover lite‐server addr |
| 121 | + liteCtr, _ := stack.ServiceContainer(ctx, "genesis") |
| 122 | + liteHost, _ := liteCtr.Host(ctx) |
| 123 | + litePort, _ := liteCtr.MappedPort(ctx, nat.Port("40004/tcp")) |
| 124 | + |
| 125 | + return &Output{ |
| 126 | + UseCache: true, |
| 127 | + ChainID: in.ChainID, |
| 128 | + Type: in.Type, |
| 129 | + Family: FamilyTon, |
| 130 | + ContainerName: containerName, |
| 131 | + Nodes: []*Node{{ |
| 132 | + // todo: do we need more access? |
| 133 | + ExternalHTTPUrl: fmt.Sprintf("%s:%s", liteHost, litePort.Port()), |
| 134 | + }}, |
| 135 | + NetworkSpecificData: &NetworkSpecificData{ |
| 136 | + TonGlobalConfigURL: globalCfgURL, |
| 137 | + }, |
| 138 | + }, nil |
| 139 | +} |
0 commit comments