@@ -1258,7 +1258,7 @@ func (s *Service) createUpgradeVersionBlock(scID skipchain.SkipBlockID, version
1258
1258
// and recreating it on the remote side.
1259
1259
// sb is a block in the byzcoin instance that we want
1260
1260
// to download.
1261
- func (s * Service ) downloadDB (sb * skipchain.SkipBlock ) error {
1261
+ func (s * Service ) downloadDB (sb * skipchain.SkipBlock , node int ) error {
1262
1262
log .Lvlf2 ("%s: downloading DB" , s .ServerIdentity ())
1263
1263
idStr := fmt .Sprintf ("%x" , sb .SkipChainID ())
1264
1264
@@ -1284,6 +1284,9 @@ func (s *Service) downloadDB(sb *skipchain.SkipBlock) error {
1284
1284
// Then start downloading the stateTrie over the network.
1285
1285
cl := NewClient (sb .SkipChainID (), * sb .Roster )
1286
1286
cl .DontContact (s .ServerIdentity ())
1287
+ if err := cl .UseNode (node ); err != nil {
1288
+ return xerrors .Errorf ("While setting node: %v" , err )
1289
+ }
1287
1290
var db * bbolt.DB
1288
1291
var bucketName []byte
1289
1292
var nonce uint64
@@ -1395,37 +1398,53 @@ func (s *Service) catchupAll() error {
1395
1398
1396
1399
log .Lvlf2 ("Our latest block: %d / %x" , sb .Index , sb .Hash )
1397
1400
1398
- cl := skipchain .NewClient ()
1399
- // Get the latest block known by the Cothority.
1400
- var reply * skipchain.GetUpdateChainReply
1401
- for i , node := range sb .Roster .List {
1402
- cl .UseNode (i )
1403
- replyTmp , err := cl .GetUpdateChain (sb .Roster , sb .Hash )
1404
- if err != nil {
1405
- log .Warn ("couldn't get update from" , node )
1406
- continue
1407
- }
1408
- if reply == nil || len (replyTmp .Update ) > len (reply .Update ) {
1409
- reply = replyTmp
1410
- }
1401
+ if err := s .catchupID (scID , sb .Roster ); err != nil {
1402
+ return xerrors .Errorf ("couldn't catchup: %v" , err )
1411
1403
}
1404
+ }
1405
+ return nil
1406
+ }
1412
1407
1413
- if reply == nil {
1414
- // Might be that the other nodes are not yet up,
1415
- // so just continue with the other chains.
1416
- // Call to s.catchup will probably also fail, so skip it.
1417
- log .Error ("couldn't get a new latest block" )
1408
+ // catchupID forces the download of the given ID and tries its best to use
1409
+ // the given roster to download all necessary info.
1410
+ func (s * Service ) catchupID (scID skipchain.SkipBlockID , roster * onet.Roster ) error {
1411
+ cl := skipchain .NewClient ()
1412
+ // Get the latest block known by the Cothority.
1413
+ var reply * skipchain.GetUpdateChainReply
1414
+ for i , node := range roster .List {
1415
+ log .Lvlf2 ("Asking node %s for latest block" , node )
1416
+ cl .UseNode (i )
1417
+ replyTmp , err := cl .GetUpdateChain (roster , scID )
1418
+ if err != nil {
1419
+ log .Warn ("couldn't get update from" , node )
1418
1420
continue
1419
1421
}
1420
-
1421
- if len (reply .Update ) == 0 {
1422
- return xerrors .New ("no block found in chain update" )
1422
+ if reply == nil {
1423
+ reply = replyTmp
1424
+ } else {
1425
+ latestTmp := replyTmp .Update [len (replyTmp .Update )- 1 ]
1426
+ latest := reply .Update [len (reply .Update )- 1 ]
1427
+ if latestTmp .Index > latest .Index {
1428
+ reply = replyTmp
1429
+ }
1423
1430
}
1431
+ }
1424
1432
1425
- sb = reply .Update [len (reply .Update )- 1 ]
1426
- log .Lvl2 ("Catching up with latest block:" , sb .Index )
1427
- s .catchUp (sb )
1433
+ if reply == nil {
1434
+ // Might be that the other nodes are not yet up,
1435
+ // so just continue with the other chains.
1436
+ // Call to s.catchup will probably also fail, so skip it.
1437
+ log .Error ("couldn't get a new latest block" )
1438
+ return nil
1428
1439
}
1440
+
1441
+ if len (reply .Update ) == 0 {
1442
+ return xerrors .New ("no block found in chain update" )
1443
+ }
1444
+
1445
+ sb := reply .Update [len (reply .Update )- 1 ]
1446
+ log .Lvl2 ("Catching up with latest block:" , sb .Index )
1447
+ s .catchUp (sb )
1429
1448
return nil
1430
1449
}
1431
1450
@@ -1498,13 +1517,13 @@ func (s *Service) catchUp(sb *skipchain.SkipBlock) {
1498
1517
1499
1518
// Load the trie.
1500
1519
download := false
1501
- st , err := s .getStateTrie (sb .SkipChainID ())
1502
1520
cl := skipchain .NewClient ()
1503
1521
cl .DontContact (s .ServerIdentity ())
1522
+ st , err := s .getStateTrie (sb .SkipChainID ())
1504
1523
if err != nil {
1505
1524
if sb .Index < catchupDownloadAll {
1506
1525
// Asked to catch up on an unknown chain, but don't want to download, instead only replay
1507
- // the blocks. This is mostly useful for testing, in a real deployement the catchupDownloadAll
1526
+ // the blocks. This is mostly useful for testing, in a real deployment the catchupDownloadAll
1508
1527
// will be smaller than any chain that is online for more than a day.
1509
1528
log .Warnf ("%s: problem with trie, " +
1510
1529
"will create a new one: %+v" , s .ServerIdentity (), err )
@@ -1541,10 +1560,14 @@ func (s *Service) catchUp(sb *skipchain.SkipBlock) {
1541
1560
1542
1561
// Check if we are updating the right index.
1543
1562
if download {
1544
- log .Lvl2 (s .ServerIdentity (), "Downloading whole DB for catching up" )
1545
- err := s .downloadDB (sb )
1546
- if err != nil {
1547
- log .Error ("Error while downloading trie:" , err )
1563
+ for i := range sb .Roster .List {
1564
+ log .Lvl2 (s .ServerIdentity (), "Downloading whole DB for catching up" )
1565
+ err := s .downloadDB (sb , i )
1566
+ if err != nil {
1567
+ log .Error ("Error while downloading trie:" , err )
1568
+ } else {
1569
+ return
1570
+ }
1548
1571
}
1549
1572
1550
1573
// Note: in that case we don't get the previous blocks and therefore we can't
@@ -3126,13 +3149,6 @@ func newService(c *onet.Context) (onet.Service, error) {
3126
3149
return nil , xerrors .Errorf ("unknown db version number %v" , ver )
3127
3150
}
3128
3151
3129
- go func () {
3130
- // initialize the stats of the storage
3131
- if err := s .stateChangeStorage .calculateSize (); err != nil {
3132
- log .Errorf ("couldn't calculate size: %v" , err )
3133
- }
3134
- }()
3135
-
3136
3152
if _ , err := s .startAllChains (); err != nil {
3137
3153
return nil , xerrors .Errorf ("starting chains: %v" , err )
3138
3154
}
0 commit comments