-
Notifications
You must be signed in to change notification settings - Fork 1
/
pastmediantimemanager.go
executable file
·102 lines (79 loc) · 2.89 KB
/
pastmediantimemanager.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package pastmediantimemanager
import (
"sort"
"github.com/shatll-s/nexelliad/domain/consensus/utils/sorters"
"github.com/shatll-s/nexelliad/domain/consensus/model"
"github.com/shatll-s/nexelliad/domain/consensus/model/externalapi"
"github.com/pkg/errors"
)
// pastMedianTimeManager provides a method to resolve the
// past median time of a block
type pastMedianTimeManager struct {
timestampDeviationTolerance int
databaseContext model.DBReader
dagTraversalManager model.DAGTraversalManager
blockHeaderStore model.BlockHeaderStore
ghostdagDataStore model.GHOSTDAGDataStore
genesisHash *externalapi.DomainHash
virtualPastMedianTimeCache int64
}
// New instantiates a new PastMedianTimeManager
func New(timestampDeviationTolerance int,
databaseContext model.DBReader,
dagTraversalManager model.DAGTraversalManager,
blockHeaderStore model.BlockHeaderStore,
ghostdagDataStore model.GHOSTDAGDataStore,
genesisHash *externalapi.DomainHash) model.PastMedianTimeManager {
return &pastMedianTimeManager{
timestampDeviationTolerance: timestampDeviationTolerance,
databaseContext: databaseContext,
dagTraversalManager: dagTraversalManager,
blockHeaderStore: blockHeaderStore,
ghostdagDataStore: ghostdagDataStore,
genesisHash: genesisHash,
}
}
// PastMedianTime returns the past median time for some block
func (pmtm *pastMedianTimeManager) PastMedianTime(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) (int64, error) {
if blockHash == model.VirtualBlockHash && pmtm.virtualPastMedianTimeCache != 0 {
return pmtm.virtualPastMedianTimeCache, nil
}
window, err := pmtm.dagTraversalManager.BlockWindow(stagingArea, blockHash, 2*pmtm.timestampDeviationTolerance-1)
if err != nil {
return 0, err
}
if len(window) == 0 {
header, err := pmtm.blockHeaderStore.BlockHeader(pmtm.databaseContext, stagingArea, pmtm.genesisHash)
if err != nil {
return 0, err
}
return header.TimeInMilliseconds(), nil
}
pastMedianTime, err := pmtm.windowMedianTimestamp(stagingArea, window)
if err != nil {
return 0, err
}
if blockHash == model.VirtualBlockHash {
pmtm.virtualPastMedianTimeCache = pastMedianTime
}
return pastMedianTime, nil
}
func (pmtm *pastMedianTimeManager) windowMedianTimestamp(
stagingArea *model.StagingArea, window []*externalapi.DomainHash) (int64, error) {
if len(window) == 0 {
return 0, errors.New("Cannot calculate median timestamp for an empty block window")
}
timestamps := make([]int64, len(window))
for i, blockHash := range window {
blockHeader, err := pmtm.blockHeaderStore.BlockHeader(pmtm.databaseContext, stagingArea, blockHash)
if err != nil {
return 0, err
}
timestamps[i] = blockHeader.TimeInMilliseconds()
}
sort.Sort(sorters.Int64Slice(timestamps))
return timestamps[len(timestamps)/2], nil
}
func (pmtm *pastMedianTimeManager) InvalidateVirtualPastMedianTimeCache() {
pmtm.virtualPastMedianTimeCache = 0
}