-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathton.go
More file actions
134 lines (113 loc) · 4.21 KB
/
ton.go
File metadata and controls
134 lines (113 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package blockchain
import (
"context"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"time"
"github.com/docker/go-connections/nat"
"github.com/testcontainers/testcontainers-go/modules/compose"
"github.com/testcontainers/testcontainers-go/wait"
"github.com/smartcontractkit/chainlink-testing-framework/framework"
)
const (
// default ports from mylocalton-docker
DefaultTonHTTPAPIPort = "8081"
DefaultTonSimpleServerPort = "8000"
DefaultTonTONExplorerPort = "8080"
DefaultTonLiteServerPort = "40004"
// NOTE: Prefunded high-load wallet from MyLocalTon pre-funded wallet, that can send up to 254 messages per 1 external message
// https://docs.ton.org/v3/documentation/smart-contracts/contracts-specs/highload-wallet#highload-wallet-v2
DefaultTonHlWalletAddress = "-1:5ee77ced0b7ae6ef88ab3f4350d8872c64667ffbe76073455215d3cdfab3294b"
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"
)
func defaultTon(in *Input) {
if in.Image == "" {
// Note: mylocalton uses a compose file, not a single image. Reusing common image field
in.Image = "https://raw.githubusercontent.com/neodix42/mylocalton-docker/main/docker-compose.yaml"
}
// Note: in local env having all services could be useful(explorer, faucet), in CI we need only core services
if os.Getenv("CI") == "true" && len(in.TonCoreServices) == 0 {
// Note: mylocalton-docker's essential services, excluded explorer, restarter, faucet app,
in.TonCoreServices = []string{
"genesis", "tonhttpapi", "event-cache",
"index-postgres", "index-worker", "index-api",
}
}
}
func newTon(in *Input) (*Output, error) {
defaultTon(in)
containerName := framework.DefaultTCName("blockchain-node")
resp, err := http.Get(in.Image)
if err != nil {
return nil, fmt.Errorf("failed to download docker-compose file: %v", err)
}
defer resp.Body.Close()
tempDir, err := os.MkdirTemp(".", "ton-mylocalton-docker")
if err != nil {
return nil, fmt.Errorf("failed to create temp directory: %v", err)
}
defer func() {
// delete the folder whether it was successful or not
_ = os.RemoveAll(tempDir)
}()
composeFile := filepath.Join(tempDir, "docker-compose.yaml")
file, err := os.Create(composeFile)
if err != nil {
return nil, fmt.Errorf("failed to create compose file: %v", err)
}
_, err = io.Copy(file, resp.Body)
if err != nil {
file.Close()
return nil, fmt.Errorf("failed to write compose file: %v", err)
}
file.Close()
ctx := context.Background()
var stack compose.ComposeStack
stack, err = compose.NewDockerComposeWith(
compose.WithStackFiles(composeFile),
compose.StackIdentifier(containerName),
)
if err != nil {
return nil, fmt.Errorf("failed to create compose stack: %v", err)
}
var upOpts []compose.StackUpOption
upOpts = append(upOpts, compose.Wait(true))
if len(in.TonCoreServices) > 0 {
upOpts = append(upOpts, compose.RunServices(in.TonCoreServices...))
}
// always wait for healthy
const genesisBlockID = "E7XwFSQzNkcRepUC23J2nRpASXpnsEKmyyHYV4u/FZY="
execStrat := wait.ForExec([]string{
"/usr/local/bin/lite-client",
"-a", "127.0.0.1:" + DefaultTonLiteServerPort,
"-b", genesisBlockID,
"-t", "3",
"-c", "last",
}).
WithPollInterval(5 * time.Second).
WithStartupTimeout(180 * time.Second)
stack = stack.
WaitForService("genesis", execStrat).
WaitForService("tonhttpapi", wait.ForListeningPort(DefaultTonHTTPAPIPort+"/tcp"))
if err := stack.Up(ctx, upOpts...); err != nil {
return nil, fmt.Errorf("failed to start compose stack: %w", err)
}
httpCtr, _ := stack.ServiceContainer(ctx, "genesis")
httpHost, _ := httpCtr.Host(ctx)
httpPort, _ := httpCtr.MappedPort(ctx, nat.Port(fmt.Sprintf("%s/tcp", DefaultTonSimpleServerPort)))
return &Output{
UseCache: true,
ChainID: in.ChainID,
Type: in.Type,
Family: FamilyTon,
ContainerName: containerName,
// Note: in case we need 1+ validators, we need to modify the compose file
Nodes: []*Node{{
// Note: define if we need more access other than the global config(tonutils-go only uses liteclients defined in the config)
ExternalHTTPUrl: fmt.Sprintf("%s:%s", httpHost, httpPort.Port()),
}},
}, nil
}