@@ -2,8 +2,10 @@ package integtest
22
33import (
44 "fmt"
5+ "maps"
6+ "slices"
7+ gosync "sync"
58 "testing"
6- "time"
79
810 "github.com/NethermindEth/juno/blockchain"
911 "github.com/NethermindEth/juno/consensus"
@@ -16,7 +18,7 @@ import (
1618 "github.com/NethermindEth/juno/sync"
1719 "github.com/NethermindEth/juno/utils"
1820 "github.com/NethermindEth/juno/vm"
19- "github.com/libp2p/go-libp2p/core/host "
21+ "github.com/libp2p/go-libp2p/core/peer "
2022 "github.com/sourcegraph/conc"
2123 "github.com/sourcegraph/conc/iter"
2224 "github.com/stretchr/testify/assert"
@@ -27,7 +29,6 @@ import (
2729const (
2830 commitBufferSize = 1024
2931 maxLag = 10
30- gst = 2 * time .Second // 2 Heartbeats should be enough for the nodes to connect to its peers
3132 hostAddress = "/ip4/0.0.0.0/tcp/0"
3233)
3334
@@ -78,18 +79,15 @@ func loadGenesis(t *testing.T, log *utils.ZapLogger) (core.StateDiff, map[felt.F
7879
7980func initNode (
8081 t * testing.T ,
81- gstChan chan struct {},
8282 index int ,
8383 logger * utils.ZapLogger ,
8484 commits chan commit ,
8585 cfg * testConfig ,
8686 genesisDiff core.StateDiff ,
8787 genesisClasses map [felt.Felt ]core.Class ,
88- ) host.Host {
88+ ready chan struct {},
89+ ) networkNodeConfig {
8990 t .Helper ()
90- defer func () {
91- logger .Debug ("finished initNode" )
92- }()
9391
9492 mockServices := consensus .InitMockServices (0 , 0 , index , cfg .nodeCount )
9593
@@ -98,6 +96,12 @@ func initNode(
9896 bc := getBlockchain (t , genesisDiff , genesisClasses )
9997 vm := vm .New (false , logger )
10098
99+ adjacentNodes := make (map [int ]peer.AddrInfo )
100+ bootstrapPeers := gosync .OnceValue (func () []peer.AddrInfo {
101+ <- ready
102+ return slices .Collect (maps .Values (adjacentNodes ))
103+ })
104+
101105 services , err := consensus .Init (
102106 logger ,
103107 consensusDB ,
@@ -108,6 +112,7 @@ func initNode(
108112 mockServices .TimeoutFn ,
109113 hostAddress ,
110114 mockServices .PrivateKey ,
115+ bootstrapPeers ,
111116 )
112117 require .NoError (t , err )
113118
@@ -119,15 +124,17 @@ func initNode(
119124 require .NoError (t , services .P2P .Run (t .Context ()))
120125 })
121126 wg .Go (func () {
122- <- gstChan
123127 require .NoError (t , services .Driver .Run (t .Context ()))
124128 })
125129 wg .Go (func () {
126130 writeBlock (t , index , bc , services .CommitListener , commits )
127131 })
128132 t .Cleanup (wg .Wait )
129133
130- return services .Host
134+ return networkNodeConfig {
135+ host : services .Host ,
136+ adjacentNodes : adjacentNodes ,
137+ }
131138}
132139
133140func writeBlock (
@@ -233,20 +240,16 @@ func runTest(t *testing.T, cfg testConfig) {
233240
234241 commits := make (chan commit , commitBufferSize )
235242
236- gstChan := make (chan struct {})
243+ ready := make (chan struct {})
237244
238245 hosts := make (networkConfig , honestNodeCount )
239246 iterator := iter.Iterator [networkNodeConfig ]{MaxGoroutines : honestNodeCount }
240247 iterator .ForEachIdx (hosts , func (i int , nodeConfig * networkNodeConfig ) {
241- * nodeConfig = networkNodeConfig {
242- host : initNode (t , gstChan , i , logger , commits , & cfg , genesisDiff , genesisClasses ),
243- adjacentNodes : make (map [int ]struct {}),
244- }
248+ * nodeConfig = initNode (t , i , logger , commits , & cfg , genesisDiff , genesisClasses , ready )
245249 })
246- hosts .setup (t , cfg .networkSetup )
247250
248- time . Sleep ( gst )
249- close (gstChan )
251+ cfg . networkSetup ( t , hosts )
252+ close (ready )
250253
251254 assertCommits (t , commits , cfg , logger )
252255}
0 commit comments