@@ -132,7 +132,7 @@ func TestFullNodeTestSuite(t *testing.T) {
132132func (s * FullNodeTestSuite ) TestBlockProduction () {
133133 s .executor .InjectTx ([]byte ("test transaction" ))
134134 err := waitForAtLeastNBlocks (s .node , 5 , Store )
135- s .NoError (err , "Failed to produce second block " )
135+ s .NoError (err , "Failed to produce more than 5 blocks " )
136136
137137 // Get the current height
138138 height , err := s .node .Store .Height (s .ctx )
@@ -176,53 +176,94 @@ func (s *FullNodeTestSuite) TestSubmitBlocksToDA() {
176176
177177// TestTxGossipingAndAggregation tests that transactions are gossiped and blocks are aggregated and synced across multiple nodes.
178178// It creates 4 nodes (1 aggregator, 3 full nodes), injects a transaction, waits for all nodes to sync, and asserts block equality.
179- func (s * FullNodeTestSuite ) TestTxGossipingAndAggregation () {
180- // First, stop the current node by cancelling its context
181- s .cancel ()
179+ func TestTxGossipingAndAggregation (t * testing.T ) {
180+ config := getTestConfig (t , 1 )
182181
183- // Create a new context for the new node
184- s .ctx , s .cancel = context .WithCancel (context .Background ())
182+ numNodes := 2
183+ nodes , cleanups := createNodesWithCleanup (t , numNodes , config )
184+ for _ , cleanup := range cleanups {
185+ defer cleanup ()
186+ }
185187
186- // Reset error channel
187- s .errCh = make (chan error , 1 )
188+ ctxs := make ([]context.Context , numNodes )
189+ cancelFuncs := make ([]context.CancelFunc , numNodes )
190+ var runningWg sync.WaitGroup
188191
189- require := require .New (s .T ())
190- config := getTestConfig (s .T (), 1 )
192+ // Create a context and cancel function for each node
193+ for i := 0 ; i < numNodes ; i ++ {
194+ ctx , cancel := context .WithCancel (context .Background ())
195+ ctxs [i ] = ctx
196+ cancelFuncs [i ] = cancel
197+ }
191198
192- numNodes := 2
193- nodes , cleanups := createNodesWithCleanup (s .T (), numNodes , config )
194- defer func () {
195- for _ , cleanup := range cleanups {
196- cleanup ()
199+ // Start only nodes[0] (aggregator) first
200+ runningWg .Add (1 )
201+ go func (node * FullNode , ctx context.Context ) {
202+ defer runningWg .Done ()
203+ err := node .Run (ctx )
204+ if err != nil && ! errors .Is (err , context .Canceled ) {
205+ t .Logf ("Error running node 0: %v" , err )
197206 }
198- }()
207+ }(nodes [ 0 ], ctxs [ 0 ] )
199208
200- s .node = nodes [0 ]
209+ // Wait for the first block to be produced by the aggregator
210+ err := waitForFirstBlock (nodes [0 ], Header )
211+ require .NoError (t , err , "Failed to get node height" )
201212
202- // Start all nodes in background
203- for _ , node := range nodes {
204- s .startNodeInBackground (node )
213+ // Verify block manager is properly initialized
214+ require .NotNil (t , nodes [0 ].blockManager , "Block manager should be initialized" )
215+
216+ // Now start the other nodes
217+ for i := 1 ; i < numNodes ; i ++ {
218+ runningWg .Add (1 )
219+ go func (node * FullNode , ctx context.Context , idx int ) {
220+ defer runningWg .Done ()
221+ err := node .Run (ctx )
222+ if err != nil && ! errors .Is (err , context .Canceled ) {
223+ t .Logf ("Error running node %d: %v" , idx , err )
224+ }
225+ }(nodes [i ], ctxs [i ], i )
205226 }
206227
207228 // Inject a transaction into the aggregator's executor
208- executor := nodes [0 ].blockManager .GetExecutor ().(* coreexecutor.DummyExecutor )
209- executor .InjectTx ([]byte ("gossip tx" ))
229+ // executor := nodes[0].blockManager.GetExecutor().(*coreexecutor.DummyExecutor)
230+ // executor.InjectTx([]byte("gossip tx"))
210231
211232 // Wait for all nodes to reach at least 3 blocks
212233 for _ , node := range nodes {
213- require .NoError (waitForAtLeastNBlocks (node , 3 , Store ))
234+ require .NoError (t , waitForAtLeastNBlocks (node , 3 , Store ))
235+ }
236+
237+ // Cancel all node contexts to signal shutdown
238+ for _ , cancel := range cancelFuncs {
239+ cancel ()
240+ }
241+
242+ // Wait for all nodes to stop, with a timeout
243+ waitCh := make (chan struct {})
244+ go func () {
245+ runningWg .Wait ()
246+ close (waitCh )
247+ }()
248+
249+ select {
250+ case <- waitCh :
251+ // Nodes stopped successfully
252+ case <- time .After (5 * time .Second ):
253+ t .Log ("Warning: Not all nodes stopped gracefully within timeout" )
214254 }
215255
216256 // Assert that all nodes have the same block at height 1 and 2
217257 for height := uint64 (1 ); height <= 2 ; height ++ {
218258 var refHash []byte
219259 for i , node := range nodes {
220260 header , _ , err := node .Store .GetBlockData (context .Background (), height )
221- require .NoError (err )
261+ require .NoError (t , err )
222262 if i == 0 {
223263 refHash = header .Hash ()
224264 } else {
225- s .Equal (refHash , header .Hash (), "Block hash mismatch at height %d between node 0 and node %d" , height , i )
265+ headerHash := header .Hash ()
266+ require .EqualValues (t , refHash , headerHash , "Block hash mismatch at height %d between node 0 and node %d" , height , i )
226267 }
227268 }
228269 }
0 commit comments