-
Notifications
You must be signed in to change notification settings - Fork 86
/
latest.go
179 lines (155 loc) · 4.77 KB
/
latest.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
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
169
170
171
172
173
174
175
176
177
178
179
// This code is available on the terms of the project LICENSE.md file,
// also available online at https://blueoakcouncil.org/license/1.0.0.
package auth
import (
"bytes"
"sort"
"sync"
"decred.org/dcrdex/dex/order"
)
type matchOutcome struct {
// sorting is done by time and match ID
time int64
mid order.MatchID
// match outcome and value
outcome Violation
base, quote uint32 // market
value uint64
}
func lessByTimeThenMID(ti, tj *matchOutcome) bool {
if ti.time == tj.time {
return bytes.Compare(ti.mid[:], tj.mid[:]) < 0 // ascending (smaller ID first)
}
return ti.time < tj.time // ascending (newest last in slice)
}
type latestMatchOutcomes struct {
mtx sync.Mutex
cap int16
outcomes []*matchOutcome
}
func newLatestMatchOutcomes(cap int16) *latestMatchOutcomes {
return &latestMatchOutcomes{
cap: cap,
outcomes: make([]*matchOutcome, 0, cap+1), // cap+1 since an old item is popped *after* a new one is pushed
}
}
func (la *latestMatchOutcomes) add(mo *matchOutcome) {
la.mtx.Lock()
defer la.mtx.Unlock()
// Use sort.Search and insert it at the right spot.
n := len(la.outcomes)
i := sort.Search(n, func(i int) bool {
return lessByTimeThenMID(la.outcomes[n-1-i], mo)
})
if i == int(la.cap) /* i == n && n == int(la.cap) */ {
// The new one is the oldest/smallest, but already at capacity.
return
}
// Insert at proper location.
i = n - i // i-1 is first location that stays
la.outcomes = append(la.outcomes[:i], append([]*matchOutcome{mo}, la.outcomes[i:]...)...)
// Pop one stamped if the slice was at capacity prior to pushing the new one.
if len(la.outcomes) > int(la.cap) {
// pop front, the oldest stamped
la.outcomes[0] = nil // avoid memory leak
la.outcomes = la.outcomes[1:]
}
}
func (la *latestMatchOutcomes) binViolations() map[Violation]int64 {
bins := make(map[Violation]int64)
for _, mo := range la.outcomes {
bins[mo.outcome]++
}
return bins
}
// SwapAmounts breaks down the quantities of completed swaps in four rough
// categories: successfully swapped (Swapped), failed with counterparty funds
// locked for the long/maker lock time (StuckLong), failed with counterparty
// funds locked for the short/taker lock time (StuckShort), and failed to
// initiate swap following match with no funds locked in contracts (Spoofed).
type SwapAmounts struct {
Swapped int64
StuckLong int64
StuckShort int64
Spoofed int64
}
func (sa *SwapAmounts) addAmt(v Violation, value int64) {
switch v {
case ViolationSwapSuccess:
sa.Swapped += value
case ViolationNoSwapAsTaker:
sa.StuckLong += value
case ViolationNoRedeemAsMaker:
sa.StuckShort += value
case ViolationNoSwapAsMaker, ViolationPreimageMiss: // ! preimage misses are presently in preimageOutcome
sa.Spoofed += value
}
}
// func (la *latestMatchOutcomes) swapAmounts() *SwapAmounts {
// sa := new(SwapAmounts)
// for _, mo := range la.outcomes {
// sa.addAmt(mo.outcome, int64(mo.value)) // must be same units (e.g. lots)!
// }
// return sa
// }
func (la *latestMatchOutcomes) mktSwapAmounts(base, quote uint32) *SwapAmounts {
sa := new(SwapAmounts)
for _, mo := range la.outcomes {
if mo.base == base && mo.quote == quote {
sa.addAmt(mo.outcome, int64(mo.value))
}
}
return sa
}
type preimageOutcome struct {
time int64
oid order.OrderID
miss bool
}
func lessByTimeThenOID(ti, tj *preimageOutcome) bool {
if ti.time == tj.time {
return bytes.Compare(ti.oid[:], tj.oid[:]) < 0 // ascending (smaller ID first)
}
return ti.time < tj.time // ascending (newest last in slice)
}
type latestPreimageOutcomes struct {
mtx sync.Mutex
cap int16
outcomes []*preimageOutcome
}
func newLatestPreimageOutcomes(cap int16) *latestPreimageOutcomes {
return &latestPreimageOutcomes{
cap: cap,
outcomes: make([]*preimageOutcome, 0, cap+1), // cap+1 since an old item is popped *after* a new one is pushed
}
}
func (la *latestPreimageOutcomes) add(po *preimageOutcome) {
la.mtx.Lock()
defer la.mtx.Unlock()
// Use sort.Search and insert it at the right spot.
n := len(la.outcomes)
i := sort.Search(n, func(i int) bool {
return lessByTimeThenOID(la.outcomes[n-1-i], po)
})
if i == int(la.cap) /* i == n && n == int(la.cap) */ {
// The new one is the oldest/smallest, but already at capacity.
return
}
// Insert at proper location.
i = n - i // i-1 is first location that stays
la.outcomes = append(la.outcomes[:i], append([]*preimageOutcome{po}, la.outcomes[i:]...)...)
// Pop one stamped if the slice was at capacity prior to pushing the new one.
if len(la.outcomes) > int(la.cap) {
// pop front, the oldest stamped
la.outcomes[0] = nil // avoid memory leak
la.outcomes = la.outcomes[1:]
}
}
func (la *latestPreimageOutcomes) misses() (misses int32) {
for _, th := range la.outcomes {
if th.miss {
misses++
}
}
return
}