-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaggregate_functions.go
More file actions
168 lines (143 loc) · 4.64 KB
/
aggregate_functions.go
File metadata and controls
168 lines (143 loc) · 4.64 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package mercury
import (
"fmt"
"math/big"
"sort"
)
var Zero = big.NewInt(0)
// NOTE: All aggregate functions assume at least one element in the passed slice
// The passed slice might be mutated (sorted)
// GetConsensusTimestamp gets the median timestamp
func GetConsensusTimestamp(paos []PAO) uint32 {
sort.Slice(paos, func(i, j int) bool {
return paos[i].GetTimestamp() < paos[j].GetTimestamp()
})
return paos[len(paos)/2].GetTimestamp()
}
// GetConsensusBenchmarkPrice gets the median benchmark price
func GetConsensusBenchmarkPrice(paos []PAO, f int) (*big.Int, error) {
var validBenchmarkPrices []*big.Int
for _, pao := range paos {
bmPrice, valid := pao.GetBenchmarkPrice()
if valid {
validBenchmarkPrices = append(validBenchmarkPrices, bmPrice)
}
}
if len(validBenchmarkPrices) < f+1 {
return nil, fmt.Errorf("fewer than f+1 observations have a valid price (got: %d/%d)", len(validBenchmarkPrices), len(paos))
}
sort.Slice(validBenchmarkPrices, func(i, j int) bool {
return validBenchmarkPrices[i].Cmp(validBenchmarkPrices[j]) < 0
})
return validBenchmarkPrices[len(validBenchmarkPrices)/2], nil
}
type PAOBid interface {
GetBid() (*big.Int, bool)
}
// GetConsensusBid gets the median bid
func GetConsensusBid(paos []PAOBid, f int) (*big.Int, error) {
var validBids []*big.Int
for _, pao := range paos {
bid, valid := pao.GetBid()
if valid {
validBids = append(validBids, bid)
}
}
if len(validBids) < f+1 {
return nil, fmt.Errorf("fewer than f+1 observations have a valid price (got: %d/%d)", len(validBids), len(paos))
}
sort.Slice(validBids, func(i, j int) bool {
return validBids[i].Cmp(validBids[j]) < 0
})
return validBids[len(validBids)/2], nil
}
type PAOAsk interface {
GetAsk() (*big.Int, bool)
}
// GetConsensusAsk gets the median ask
func GetConsensusAsk(paos []PAOAsk, f int) (*big.Int, error) {
var validAsks []*big.Int
for _, pao := range paos {
ask, valid := pao.GetAsk()
if valid {
validAsks = append(validAsks, ask)
}
}
if len(validAsks) < f+1 {
return nil, fmt.Errorf("fewer than f+1 observations have a valid price (got: %d/%d)", len(validAsks), len(paos))
}
sort.Slice(validAsks, func(i, j int) bool {
return validAsks[i].Cmp(validAsks[j]) < 0
})
return validAsks[len(validAsks)/2], nil
}
type PAOMaxFinalizedTimestamp interface {
GetMaxFinalizedTimestamp() (int64, bool)
}
// GetConsensusMaxFinalizedTimestamp returns the highest count with > f observations
func GetConsensusMaxFinalizedTimestamp(paos []PAOMaxFinalizedTimestamp, f int) (int64, error) {
var validTimestampCount int
timestampFrequencyMap := map[int64]int{}
for _, pao := range paos {
ts, valid := pao.GetMaxFinalizedTimestamp()
if valid {
validTimestampCount++
timestampFrequencyMap[ts]++
}
}
// check if we have enough valid timestamps at all
if validTimestampCount < f+1 {
return 0, fmt.Errorf("fewer than f+1 observations have a valid maxFinalizedTimestamp (got: %d/%d)", validTimestampCount, len(paos))
}
var maxTs int64 = -2 // -1 is smallest valid amount
for ts, cnt := range timestampFrequencyMap {
// ignore any timestamps with <= f observations
if cnt > f && ts > maxTs {
maxTs = ts
}
}
if maxTs < -1 {
return 0, fmt.Errorf("no valid maxFinalizedTimestamp with at least f+1 votes (got counts: %v)", timestampFrequencyMap)
}
return maxTs, nil
}
type PAOLinkFee interface {
GetLinkFee() (*big.Int, bool)
}
// GetConsensusLinkFee gets the median link fee
func GetConsensusLinkFee(paos []PAOLinkFee, f int) (*big.Int, error) {
var validLinkFees []*big.Int
for _, pao := range paos {
fee, valid := pao.GetLinkFee()
if valid && fee.Sign() >= 0 {
validLinkFees = append(validLinkFees, fee)
}
}
if len(validLinkFees) < f+1 {
return nil, fmt.Errorf("fewer than f+1 observations have a valid linkFee (got: %d/%d)", len(validLinkFees), len(paos))
}
sort.Slice(validLinkFees, func(i, j int) bool {
return validLinkFees[i].Cmp(validLinkFees[j]) < 0
})
return validLinkFees[len(validLinkFees)/2], nil
}
type PAONativeFee interface {
GetNativeFee() (*big.Int, bool)
}
// GetConsensusNativeFee gets the median native fee
func GetConsensusNativeFee(paos []PAONativeFee, f int) (*big.Int, error) {
var validNativeFees []*big.Int
for _, pao := range paos {
fee, valid := pao.GetNativeFee()
if valid && fee.Sign() >= 0 {
validNativeFees = append(validNativeFees, fee)
}
}
if len(validNativeFees) < f+1 {
return nil, fmt.Errorf("fewer than f+1 observations have a valid nativeFee (got: %d/%d)", len(validNativeFees), len(paos))
}
sort.Slice(validNativeFees, func(i, j int) bool {
return validNativeFees[i].Cmp(validNativeFees[j]) < 0
})
return validNativeFees[len(validNativeFees)/2], nil
}