@@ -2,17 +2,15 @@ package blockchain
22
33import (
44 "context"
5- "encoding/json"
65 "fmt"
7- "net/http"
8- "net/url"
96 "strconv"
107 "time"
118
129 "github.com/docker/docker/api/types/container"
1310 "github.com/testcontainers/testcontainers-go"
1411 "github.com/testcontainers/testcontainers-go/network"
1512 "github.com/testcontainers/testcontainers-go/wait"
13+ "github.com/xssnick/tonutils-go/liteclient"
1614
1715 "github.com/smartcontractkit/chainlink-testing-framework/framework"
1816)
@@ -27,60 +25,13 @@ const (
2725 liteServerPortOffset = 100 // internal, arbitrary offset for lite server port
2826)
2927
30- // TON config structures (e.g.: ton-blockchain.github.io/testnet-global.config.json)
31- type tonLiteServer struct {
32- IP int64 `json:"ip"`
33- Port int `json:"port"`
34- ID struct {
35- Key string `json:"key"`
36- Type string `json:"@type"`
37- } `json:"id"`
38- }
39-
40- type tonConfig struct {
41- LiteServers []tonLiteServer `json:"liteservers"`
42- }
43-
44- // convert int64 IP to string format (matches https://github.com/xssnick/tonutils-go/liteclient/connection.go/intToIP4)
45- func intToIP4 (ip int64 ) string {
46- uip := uint32 (ip ) //nolint:gosec // IP conversion is safe for TON format
47- return fmt .Sprintf ("%d.%d.%d.%d" ,
48- (uip >> 24 )& 0xFF ,
49- (uip >> 16 )& 0xFF ,
50- (uip >> 8 )& 0xFF ,
51- uip & 0xFF )
52- }
53-
54- // fetch and parse TON config to generate liteserver URLs
55- func fetchTonConfig (configURL string ) ([]string , error ) {
56- parsedURL , err := url .Parse (configURL )
57- if err != nil {
58- return nil , fmt .Errorf ("invalid config URL: %w" , err )
59- }
60- if parsedURL .Scheme != "http" && parsedURL .Scheme != "https" {
61- return nil , fmt .Errorf ("invalid URL scheme: %s" , parsedURL .Scheme )
62- }
63-
64- resp , err := http .Get (configURL ) //nolint:gosec // URL is validated above
65- if err != nil {
66- return nil , fmt .Errorf ("failed to fetch config: %w" , err )
67- }
68- defer resp .Body .Close ()
69- defer resp .Body .Close ()
70-
71- var config tonConfig
72- if err := json .NewDecoder (resp .Body ).Decode (& config ); err != nil {
73- return nil , fmt .Errorf ("failed to decode config: %w" , err )
74- }
75-
76- var liteServerURLs []string
77- for _ , ls := range config .LiteServers {
78- ipStr := intToIP4 (ls .IP )
79- url := fmt .Sprintf ("liteserver://%s@%s:%d" , ls .ID .Key , ipStr , ls .Port )
80- liteServerURLs = append (liteServerURLs , url )
81- }
82-
83- return liteServerURLs , nil
28+ // intToIP4 converts int64 IP to string format (matches tonutils-go implementation)
29+ func intToIP4 (ipInt int64 ) string {
30+ b0 := strconv .FormatInt ((ipInt >> 24 )& 0xff , 10 )
31+ b1 := strconv .FormatInt ((ipInt >> 16 )& 0xff , 10 )
32+ b2 := strconv .FormatInt ((ipInt >> 8 )& 0xff , 10 )
33+ b3 := strconv .FormatInt ((ipInt & 0xff ), 10 )
34+ return b0 + "." + b1 + "." + b2 + "." + b3
8435}
8536
8637type portMapping struct {
@@ -163,11 +114,11 @@ func newTon(in *Input) (*Output, error) {
163114 "-t" , "3" , "-c" , "last" ,
164115 }).WithStartupTimeout (2 * time .Minute ),
165116 Mounts : testcontainers.ContainerMounts {
166- {
117+ testcontainers. ContainerMount {
167118 Source : testcontainers.GenericVolumeMountSource {Name : fmt .Sprintf ("shared-data-%s" , networkName )},
168119 Target : "/usr/share/data" ,
169120 },
170- {
121+ testcontainers. ContainerMount {
171122 Source : testcontainers.GenericVolumeMountSource {Name : fmt .Sprintf ("ton-db-%s" , networkName )},
172123 Target : "/var/ton-work/db" ,
173124 },
@@ -190,18 +141,28 @@ func newTon(in *Input) (*Output, error) {
190141 return nil , err
191142 }
192143
193- // fetch config and generate liteserver URLs from actual config
144+ // Fetch config using tonutils-go
194145 configURL := fmt .Sprintf ("http://localhost:%s/localhost.global.config.json" , ports .SimpleServer )
195146
196- liteServerURLs , err := fetchTonConfig ( configURL )
147+ config , err := liteclient . GetConfigFromUrl ( ctx , configURL )
197148 if err != nil {
198- return nil , err
149+ return nil , fmt . Errorf ( "failed to fetch TON config: %w" , err )
199150 }
200151
201- if len (liteServerURLs ) == 0 {
152+ if len (config . Liteservers ) == 0 {
202153 return nil , fmt .Errorf ("no liteservers found in config" )
203154 }
204155
156+ // Use the first liteserver to create URLs
157+ ls := config .Liteservers [0 ]
158+ ipStr := intToIP4 (ls .IP )
159+ publicKey := ls .ID .Key
160+ port := ls .Port
161+
162+ // Create external and internal URLs
163+ externalURL := fmt .Sprintf ("liteserver://%s@%s:%d" , publicKey , ipStr , port )
164+ internalURL := fmt .Sprintf ("liteserver://%s@%s:%d" , publicKey , name , port )
165+
205166 return & Output {
206167 UseCache : true ,
207168 ChainID : in .ChainID ,
@@ -210,9 +171,9 @@ func newTon(in *Input) (*Output, error) {
210171 ContainerName : name ,
211172 Container : c ,
212173 Nodes : []* Node {{
213- // URLs now contain liteserver://publickey@host:port
214- ExternalHTTPUrl : liteServerURLs [ 0 ] ,
215- InternalHTTPUrl : liteServerURLs [ 0 ] ,
174+ // URLs now contain liteserver://publickey@host:port connections
175+ ExternalHTTPUrl : externalURL ,
176+ InternalHTTPUrl : internalURL ,
216177 }},
217178 }, nil
218179}
0 commit comments