@@ -362,7 +362,7 @@ func writeChallenges(challenges []Challenges, filename, address string, cnt int)
362362}
363363
364364// read the cache file
365- func readChallenges (filename , address string , expires int64 , cnt int ) ([]Challenges , error ) {
365+ func loadChallenges (filename , address string , expires int64 , cnt int ) ([]Challenges , error ) {
366366 cache := ChallengeCache {}
367367 bytes , err := ioutil .ReadFile (filename )
368368 if err != nil {
@@ -373,12 +373,15 @@ func readChallenges(filename, address string, expires int64, cnt int) ([]Challen
373373 return []Challenges {}, err
374374 }
375375 if cache .Time + expires < time .Now ().Unix () {
376- return []Challenges {}, fmt .Errorf ("Challenge cache is old. Auto-refreshing... " )
376+ return []Challenges {}, fmt .Errorf ("Challenge cache is old." )
377377 }
378378 if cache .Count != cnt {
379- return []Challenges {}, fmt .Errorf ("Challenge cache stored %d instead of %d records. Auto-refreshing... " ,
379+ return []Challenges {}, fmt .Errorf ("Challenge cache stored %d instead of %d records." ,
380380 cache .Count , cnt )
381381 }
382+ if cache .Address != address {
383+ return []Challenges {}, fmt .Errorf ("Challenge cache is for different hotspot." )
384+ }
382385
383386 return cache .Challenges , nil
384387}
@@ -464,14 +467,13 @@ func getDistance(aHost, bHost Hotspot) (float64, float64, error) {
464467func getTimeForHeight (height int64 , challenges []Challenges ) (int64 , error ) {
465468 var t_height int64 = math .MaxInt64
466469 var t int64 = 0
470+ var err error
467471 for _ , c := range challenges {
468472 if c .Height <= t_height && c .Height > height {
469- p := * c . Path
470- if p [ 0 ]. Receipt = = nil {
473+ t , err = c . GetTimestamp ()
474+ if err ! = nil {
471475 continue
472476 }
473- r := * p [0 ].Receipt
474- t = r .Timestamp
475477 t_height = c .Height
476478 }
477479 }
@@ -480,3 +482,25 @@ func getTimeForHeight(height int64, challenges []Challenges) (int64, error) {
480482 }
481483 return 0 , fmt .Errorf ("Unable to find time for height %d" , height )
482484}
485+
486+ // Tries to figure out the Timestamp for the given challenge
487+ func (c * Challenges ) GetTimestamp () (int64 , error ) {
488+ if c .Path == nil {
489+ return 0 , fmt .Errorf ("No paths: unable to determine timestamp for %s@%d" ,
490+ c .Type , c .Time )
491+ }
492+ p := * c .Path
493+
494+ if p [0 ].Receipt != nil {
495+ r := p [0 ].Receipt
496+ return r .Timestamp , nil
497+ }
498+
499+ if p [0 ].Witnesses != nil {
500+ w := * p [0 ].Witnesses
501+ return w [0 ].Timestamp , nil
502+ }
503+
504+ return 0 , fmt .Errorf ("No data: unable to determine timestamp for %s@%d" ,
505+ c .Type , c .Time )
506+ }
0 commit comments