@@ -2,15 +2,18 @@ package integtest
22
33import (
44 "fmt"
5+ goitre "iter"
56 "maps"
67 "slices"
78 gosync "sync"
89 "testing"
10+ "time"
911
1012 "github.com/NethermindEth/juno/blockchain"
1113 "github.com/NethermindEth/juno/consensus"
1214 "github.com/NethermindEth/juno/consensus/driver"
1315 "github.com/NethermindEth/juno/consensus/starknet"
16+ "github.com/NethermindEth/juno/consensus/types"
1417 "github.com/NethermindEth/juno/core"
1518 "github.com/NethermindEth/juno/core/felt"
1619 "github.com/NethermindEth/juno/db/memory"
@@ -28,8 +31,8 @@ import (
2831
2932const (
3033 commitBufferSize = 1024
31- maxLag = 10
3234 hostAddress = "/ip4/0.0.0.0/tcp/0"
35+ maxRoundWait = types .Round (5 )
3336)
3437
3538var (
@@ -169,6 +172,26 @@ func writeBlock(
169172 }
170173}
171174
175+ func commitStream (t * testing.T , nodeCount int , commits chan commit ) goitre.Seq [commit ] {
176+ timeoutFn := consensus .MockTimeoutFn (nodeCount )
177+ var maxCommitWait time.Duration
178+ for round := range maxRoundWait {
179+ maxCommitWait += timeoutFn (types .StepPropose , round ) + timeoutFn (types .StepPrevote , round ) + timeoutFn (types .StepPrecommit , round )
180+ }
181+ return func (yield func (commit ) bool ) {
182+ for {
183+ select {
184+ case commit := <- commits :
185+ if ! yield (commit ) {
186+ return
187+ }
188+ case <- time .After (maxCommitWait ):
189+ require .FailNow (t , "timed out waiting for commit" )
190+ }
191+ }
192+ }
193+ }
194+
172195func assertCommits (t * testing.T , commits chan commit , cfg testConfig , logger * utils.ZapLogger ) {
173196 t .Helper ()
174197
@@ -182,22 +205,23 @@ func assertCommits(t *testing.T, commits chan commit, cfg testConfig, logger *ut
182205 commitCount := make (map [uint64 ]int )
183206
184207 nextHeight := uint64 (1 )
185- for commit := range commits {
208+ for commit := range commitStream ( t , cfg . nodeCount , commits ) {
186209 blockNumber := commit .committedBlock .Block .Number
187210 blockHash := commit .committedBlock .Block .Hash
188- // Ignore commits after the target height
189- if blockNumber > cfg .targetHeight {
190- continue
191- }
192211
193212 require .Falsef (
194213 t ,
195- blockNumber > nextHeight + maxLag ,
214+ blockNumber > nextHeight + cfg . targetHeight ,
196215 "finished height %d is too far behind committed height %d" ,
197216 nextHeight ,
198217 blockNumber ,
199218 )
200219
220+ // Ignore commits after the target height
221+ if blockNumber > cfg .targetHeight {
222+ continue
223+ }
224+
201225 // Ignore faulty nodes
202226 if commit .nodeIndex >= honestNodeCount {
203227 continue
@@ -218,12 +242,12 @@ func assertCommits(t *testing.T, commits chan commit, cfg testConfig, logger *ut
218242 commitCount [blockNumber ]++
219243
220244 // If all honest nodes committed at this height, increment the finished counter
221- for commitCount [nextHeight ] == honestNodeCount && nextHeight < cfg .targetHeight {
245+ for commitCount [nextHeight ] == honestNodeCount && nextHeight <= cfg .targetHeight {
222246 logger .Infow ("all honest nodes committed" , "height" , nextHeight )
223247 nextHeight ++
224248 }
225249
226- if nextHeight == cfg .targetHeight {
250+ if nextHeight > cfg .targetHeight {
227251 break
228252 }
229253 }
0 commit comments