@@ -1124,8 +1124,20 @@ func GetFreePort() string {
11241124 return strconv .Itoa (port )
11251125}
11261126
1127+ func (s * Server ) hasAllBlocks (ctx context.Context , m map [int32 ][]* block ) (bool , error ) {
1128+ for _ , k := range m {
1129+ for _ , blk := range k {
1130+ _ , err := s .db .BlockByHash (ctx , * blk .Hash ())
1131+ if err != nil {
1132+ return false , err
1133+ }
1134+ }
1135+ }
1136+ return true , nil
1137+ }
1138+
11271139func TestFork (t * testing.T ) {
1128- ctx , cancel := context .WithCancel (context .Background ())
1140+ ctx , cancel := context .WithTimeout (context .Background (), 7 * time . Second )
11291141 defer func () {
11301142 cancel ()
11311143 }()
@@ -1268,6 +1280,17 @@ func TestFork(t *testing.T) {
12681280 if err != nil {
12691281 t .Fatal (err )
12701282 }
1283+
1284+ // Wait for tbc to insert all blocks
1285+ var hasBlocks bool
1286+ for ! hasBlocks {
1287+ hasBlocks , err = s .hasAllBlocks (ctx , n .blocksAtHeight )
1288+ if err != nil {
1289+ t .Logf ("blocks not yet synced: %v" , err )
1290+ time .Sleep (50 * time .Millisecond )
1291+ }
1292+ }
1293+
12711294 t .Logf ("b11a: %v" , b11a .Hash ())
12721295 t .Logf ("b11b: %v" , b11b .Hash ())
12731296 b11s := n .Best ()
@@ -1382,7 +1405,7 @@ func TestWork(t *testing.T) {
13821405}
13831406
13841407func TestIndexNoFork (t * testing.T ) {
1385- ctx , cancel := context .WithCancel (context .Background ())
1408+ ctx , cancel := context .WithTimeout (context .Background (), 7 * time . Second )
13861409 defer func () {
13871410 cancel ()
13881411 }()
@@ -1460,6 +1483,16 @@ func TestIndexNoFork(t *testing.T) {
14601483 t .Fatal (err )
14611484 }
14621485
1486+ // Wait for tbc to insert all blocks
1487+ var hasBlocks bool
1488+ for ! hasBlocks {
1489+ hasBlocks , err = s .hasAllBlocks (ctx , n .blocksAtHeight )
1490+ if err != nil {
1491+ t .Logf ("blocks not yet synced: %v" , err )
1492+ time .Sleep (50 * time .Millisecond )
1493+ }
1494+ }
1495+
14631496 // genesis -> b3 should work with negative direction (cdiff is less than target)
14641497 direction , err := s .TxIndexIsLinear (ctx , * b3 .Hash ())
14651498 if err != nil {
@@ -1560,7 +1593,7 @@ func TestIndexNoFork(t *testing.T) {
15601593}
15611594
15621595func TestKeystoneIndexNoFork (t * testing.T ) {
1563- ctx , cancel := context .WithCancel (context .Background ())
1596+ ctx , cancel := context .WithTimeout (context .Background (), 7 * time . Second )
15641597 defer func () {
15651598 cancel ()
15661599 }()
@@ -1653,6 +1686,16 @@ func TestKeystoneIndexNoFork(t *testing.T) {
16531686 t .Fatal (err )
16541687 }
16551688
1689+ // Wait for tbc to insert all blocks
1690+ var hasBlocks bool
1691+ for ! hasBlocks {
1692+ hasBlocks , err = s .hasAllBlocks (ctx , n .blocksAtHeight )
1693+ if err != nil {
1694+ t .Logf ("blocks not yet synced: %v" , err )
1695+ time .Sleep (50 * time .Millisecond )
1696+ }
1697+ }
1698+
16561699 // genesis -> b3 should work with negative direction (cdiff is less than target)
16571700 direction , err := s .TxIndexIsLinear (ctx , * b3 .Hash ())
16581701 if err != nil {
@@ -1820,7 +1863,7 @@ func TestKeystoneIndexNoFork(t *testing.T) {
18201863}
18211864
18221865func TestIndexFork (t * testing.T ) {
1823- ctx , cancel := context .WithCancel (context .Background ())
1866+ ctx , cancel := context .WithTimeout (context .Background (), 7 * time . Second )
18241867 defer func () {
18251868 cancel ()
18261869 }()
@@ -1920,6 +1963,16 @@ func TestIndexFork(t *testing.T) {
19201963 t .Fatal (err )
19211964 }
19221965
1966+ // Wait for tbc to insert all blocks
1967+ var hasBlocks bool
1968+ for ! hasBlocks {
1969+ hasBlocks , err = s .hasAllBlocks (ctx , n .blocksAtHeight )
1970+ if err != nil {
1971+ t .Logf ("blocks not yet synced: %v" , err )
1972+ time .Sleep (50 * time .Millisecond )
1973+ }
1974+ }
1975+
19231976 // Verify linear indexing. Current TxIndex is sitting at genesis
19241977
19251978 // genesis -> b3 should work with negative direction (cdiff is less than target)
@@ -2129,7 +2182,7 @@ func TestIndexFork(t *testing.T) {
21292182}
21302183
21312184func TestKeystoneIndexFork (t * testing.T ) {
2132- ctx , cancel := context .WithCancel (context .Background ())
2185+ ctx , cancel := context .WithTimeout (context .Background (), 7 * time . Second )
21332186 defer func () {
21342187 cancel ()
21352188 }()
@@ -2244,6 +2297,16 @@ func TestKeystoneIndexFork(t *testing.T) {
22442297 t .Fatal (err )
22452298 }
22462299
2300+ // Wait for tbc to insert all blocks
2301+ var hasBlocks bool
2302+ for ! hasBlocks {
2303+ hasBlocks , err = s .hasAllBlocks (ctx , n .blocksAtHeight )
2304+ if err != nil {
2305+ t .Logf ("blocks not yet synced: %v" , err )
2306+ time .Sleep (50 * time .Millisecond )
2307+ }
2308+ }
2309+
22472310 // Verify linear indexing. Current TxIndex is sitting at genesis
22482311
22492312 // genesis -> b3 should work with negative direction (cdiff is less than target)
@@ -2630,7 +2693,7 @@ func TestTransactions(t *testing.T) {
26302693}
26312694
26322695func TestForkCanonicity (t * testing.T ) {
2633- ctx , cancel := context .WithCancel (context .Background ())
2696+ ctx , cancel := context .WithTimeout (context .Background (), 7 * time . Second )
26342697 defer func () {
26352698 cancel ()
26362699 }()
@@ -2748,6 +2811,16 @@ func TestForkCanonicity(t *testing.T) {
27482811 t .Fatal (err )
27492812 }
27502813
2814+ // Wait for tbc to insert all blocks
2815+ var hasBlocks bool
2816+ for ! hasBlocks {
2817+ hasBlocks , err = s .hasAllBlocks (ctx , n .blocksAtHeight )
2818+ if err != nil {
2819+ t .Logf ("blocks not yet synced: %v" , err )
2820+ time .Sleep (50 * time .Millisecond )
2821+ }
2822+ }
2823+
27512824 // set checkpoints to genesis, b2 and b4
27522825 s .checkpoints = []checkpoint {
27532826 {4 , * mainChainHashes ["b4" ]},
0 commit comments