-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker.go
70 lines (57 loc) · 1.24 KB
/
tracker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package timestamps
import "github.com/DJDNS/go-deje/document"
type TimestampTracker struct {
Doc *document.Document
Service TimestampService
// Current iteration range
timestamps []string
tip string
}
func NewTimestampTracker(doc *document.Document, service TimestampService) TimestampTracker {
return TimestampTracker{
Doc: doc,
Service: service,
}
}
// Iterate until tip is found. Returns tip
func (tt *TimestampTracker) FindLatest() (*document.Event, error) {
timestamps, err := tt.Service.GetTimestamps()
if err != nil {
return nil, err
}
tt.timestamps = timestamps
tt.tip = ""
for _, ts := range tt.timestamps {
event, ok := tt.Doc.Events[ts]
if !ok {
continue
}
if !tt.CompatibleWithTip(event) {
continue
}
tt.tip = ts
}
return tt.Doc.Events[tt.tip], nil
}
func (tt *TimestampTracker) CompatibleWithTip(event *document.Event) bool {
if event == nil {
return false
}
hash := event.Hash()
if _, ok := tt.Doc.Events[hash]; !ok {
return false
}
// Always win against no-tip
if tt.tip == "" {
return true
}
tip_event, ok := tt.Doc.Events[tt.tip]
if !ok {
return false
}
ca, err := tip_event.GetCommonAncestor(event)
if err != nil {
return false
}
return ca == tip_event
}