Skip to content
This repository was archived by the owner on Feb 28, 2023. It is now read-only.

Commit 3c9a38d

Browse files
authored
Merge pull request #16 from synfinatic/timestamps
Determine timestamps better & fix cache
2 parents da2998e + 836b6fa commit 3c9a38d

4 files changed

Lines changed: 45 additions & 16 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ endif
99
BUILDINFOSDET ?=
1010
PROGRAM_ARGS ?=
1111

12-
PROJECT_VERSION := 0.5.3
12+
PROJECT_VERSION := 0.5.4
1313
DOCKER_REPO := synfinatic
1414
PROJECT_NAME := helium-analysis
1515
PROJECT_TAG := $(shell git describe --tags 2>/dev/null $(git rev-list --tags --max-count=1))

cmd/challenges.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {
464467
func 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+
}

cmd/graph.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,18 @@ func generatePeerGraphs(address string, challenges []Challenges, min int, zoom b
293293
x_min := 0.0
294294
x_max := 0.0
295295
if !zoom {
296-
c := *challenges[0].Path
297-
p := *c[0].Witnesses
298-
x_max = float64(p[0].Timestamp)
299-
last := len(challenges) - 1
300-
c = *challenges[last].Path
301-
p = *c[0].Witnesses
302-
x_min = float64(p[0].Timestamp)
296+
for i := 0; x_max == 0; i++ {
297+
max, err := challenges[i].GetTimestamp()
298+
if err == nil {
299+
x_max = float64(max)
300+
}
301+
}
302+
for i := len(challenges) - 1; x_min == 0; i-- {
303+
min, err := challenges[i].GetTimestamp()
304+
if err == nil {
305+
x_min = float64(min)
306+
}
307+
}
303308
}
304309

305310
cnt := 0

cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func main() {
116116
log.Fatalf("%s", err)
117117
}
118118
} else {
119-
c, err = readChallenges(CHALLENGES_CACHE_FILE, address, challengesExpires*3600, challengesCnt)
119+
c, err = loadChallenges(CHALLENGES_CACHE_FILE, address, challengesExpires*3600, challengesCnt)
120120
if err != nil {
121121
log.WithError(err).Warnf("Unable to load challenges file. Refreshing...")
122122
c, err = fetchChallenges(address, challengesCnt)

0 commit comments

Comments
 (0)