@@ -4,13 +4,10 @@ import (
44 "context"
55 "fmt"
66 "log/slog"
7- "os"
87 "strconv"
98 "sync"
10- "time"
119
1210 "github.com/ethereum/go-ethereum/common"
13- "github.com/vechain/hayabusa-e2e/utils"
1411 "github.com/vechain/networkhub/environments/local"
1512 networkhubNetwork "github.com/vechain/networkhub/network"
1613 "github.com/vechain/networkhub/network/node"
@@ -20,80 +17,44 @@ import (
2017)
2118
2219type Network struct {
23- ctx context.Context
24- config * Config
25- network * local.Local
26- genesis * genesis.CustomGenesis
27- nodes []node.Config
28- usedPorts []int // to track used ports for cleanup
20+ ctx context.Context
21+ config * Config
22+ network * local.Local
23+ genesis * genesis.CustomGenesis
24+ nodes []node.Config
2925
3026 mu sync.Mutex
3127}
3228
3329func NewNetwork (config * Config , ctx context.Context ) (* Network , error ) {
34- buildMutex .Lock ()
35- defer buildMutex .Unlock ()
36-
3730 if err := config .Validate (); err != nil {
3831 return nil , err
3932 }
4033
41- repo := "git@github.com:vechain/thor.git"
42-
43- workingDir , ok := os .LookupEnv ("THOR_WORKING_DIR" )
44- var thorBuilder * thorbuilder.Config
45- if ok {
46- thorBuilder = & thorbuilder.Config {
47- BuildConfig : & thorbuilder.BuildConfig {
48- ExistingPath : workingDir ,
49- DebugBuild : config .Debug ,
50- },
51- }
52- } else {
53- slog .Warn ("THOR_WORKING_DIR not set, using default repo/branch" )
54- thorBuilder = & thorbuilder.Config {
55- DownloadConfig : & thorbuilder.DownloadConfig {
56- RepoUrl : repo ,
57- Branch : "release/hayabusa" ,
58- IsReusable : true ,
59- },
60- }
61- builder := thorbuilder .New (thorBuilder )
62- err := builder .Download ()
63- if err != nil {
64- return nil , fmt .Errorf ("failed to download thor: %w" , err )
65- }
66- workingDir = builder .DownloadPath
67- }
68-
6934 nodes := make ([]node.Config , 0 )
7035 genesis := Genesis (config )
71- usedPorts := make ([]int , 0 , config .Nodes * 2 )
7236 for i := range config .Nodes {
73- node , apiPort , p2pPort := makeNode (config , i , ValidatorAccounts [i ], genesis )
37+ node := makeNode (config , i , ValidatorAccounts [i ], genesis )
7438 nodes = append (nodes , node )
75- usedPorts = append (usedPorts , apiPort , p2pPort )
7639 }
7740
7841 network := local .NewEnv ()
7942 netConfig := & networkhubNetwork.Network {
8043 BaseID : config .Name ,
8144 Nodes : nodes ,
82- ThorBuilder : thorBuilder ,
45+ ThorBuilder : thorbuilder . DefaultConfig () ,
8346 }
84- builder := thorbuilder .New (netConfig .ThorBuilder )
85- builder .Download ()
8647 if _ , err := network .LoadConfig (netConfig ); err != nil {
8748 return nil , fmt .Errorf ("failed to load network config: %w" , err )
8849 }
8950
51+ fmt .Println ("The nodes are: " , nodes )
9052 return & Network {
91- ctx : ctx ,
92- config : config ,
93- network : network ,
94- genesis : genesis ,
95- nodes : nodes ,
96- usedPorts : usedPorts ,
53+ ctx : ctx ,
54+ config : config ,
55+ network : network ,
56+ genesis : genesis ,
57+ nodes : nodes ,
9758 }, nil
9859}
9960
@@ -106,13 +67,9 @@ func (n *Network) Genesis() *genesis.CustomGenesis {
10667}
10768
10869func (n * Network ) Stop () {
109- n .mu .Lock ()
110- defer n .mu .Unlock ()
111-
11270 if err := n .network .StopNetwork (); err != nil {
11371 slog .Error ("🛑 failed to stop network" , "error" , err )
11472 }
115- globalPortManager .RemovePorts (n .config .Name )
11673}
11774
11875func (n * Network ) NodeConfigs () []node.Config {
@@ -130,12 +87,6 @@ func (n *Network) NodeLifecycles() map[string]node.Lifecycle {
13087}
13188
13289func (n * Network ) Start () error {
133- n .mu .Lock ()
134- defer n .mu .Unlock ()
135-
136- buildMutex .Lock ()
137- defer buildMutex .Unlock ()
138-
13990 go func () {
14091 <- n .ctx .Done ()
14192 slog .Info ("context done, cleaning up network" )
@@ -146,51 +97,19 @@ func (n *Network) Start() error {
14697 return err
14798 }
14899
149- for _ , nodeConfig := range n .nodes {
150- if err := nodeConfig .HealthCheck (0 , 30 * time .Second ); err != nil {
151- return err
152- }
153- }
154-
155- if err := utils .WaitForPeersConnection (n .nodes , n .ctx ); err != nil {
156- return fmt .Errorf ("failed to connect all nodes: %w" , err )
157- }
158-
159100 return nil
160101}
161102
162103func (n * Network ) AttachNode (buildConfig * thorbuilder.Config , additionalArgs map [string ]string ) error {
163- n .mu .Lock ()
164- defer n .mu .Unlock ()
165- buildMutex .Lock ()
166- defer buildMutex .Unlock ()
167-
168- builder := thorbuilder .New (buildConfig )
169-
170- if err := builder .Download (); err != nil {
171- return fmt .Errorf ("failed to download builder: %w" , err )
172- }
173- path , err := builder .Build ()
174- if err != nil {
175- return fmt .Errorf ("failed to build node: %w" , err )
176- }
177-
178- node , apiPort , p2pPort := makeNode (n .config , len (n .nodes ), ValidatorAccounts [len (n .nodes )], n .genesis )
179- node .SetExecArtifact (path )
180- node .SetAdditionalArgs (additionalArgs )
181- n .nodes = append (n .nodes , node )
182- n .usedPorts = append (n .usedPorts , apiPort , p2pPort )
183- if err := n .network .AttachNode (node ); err != nil {
104+ node := makeNode (n .config , len (n .nodes ), ValidatorAccounts [len (n .nodes )], n .genesis )
105+ if err := n .network .AttachNode (node , buildConfig , additionalArgs ); err != nil {
184106 return fmt .Errorf ("failed to attach node: %w" , err )
185107 }
186- if err := node .HealthCheck (0 , 30 * time .Second ); err != nil {
187- return fmt .Errorf ("failed to health check attached node: %w" , err )
188- }
189108
190109 return nil
191110}
192111
193- func makeNode (config * Config , i int , signer * NodePair , customGenesis * genesis.CustomGenesis ) ( node.Config , int , int ) {
112+ func makeNode (config * Config , i int , signer * NodePair , customGenesis * genesis.CustomGenesis ) node.Config {
194113 verbosity := 3
195114 if config .Verbosity > 0 {
196115 verbosity = config .Verbosity
@@ -207,16 +126,11 @@ func makeNode(config *Config, i int, signer *NodePair, customGenesis *genesis.Cu
207126 nodeID = fmt .Sprintf ("%s-%s" , config .Name , nodeID )
208127 }
209128
210- apiPort := globalPortManager .NewPort (config .Name )
211- p2pPort := globalPortManager .NewPort (config .Name )
212-
213129 return & node.BaseNode {
214130 ID : nodeID ,
215131 Key : common .Bytes2Hex (signer .Node .D .Bytes ()),
216132 Genesis : customGenesis ,
217133 Verbosity : verbosity ,
218134 AdditionalArgs : additionalArgs ,
219- APIAddr : fmt .Sprintf ("0.0.0.0:%d" , apiPort ),
220- P2PListenPort : p2pPort ,
221- }, apiPort , p2pPort
135+ }
222136}
0 commit comments