-
Notifications
You must be signed in to change notification settings - Fork 89
/
coinlocker.go
312 lines (262 loc) · 8.93 KB
/
coinlocker.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// 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 coinlock
import (
"fmt"
"sync"
"decred.org/dcrdex/dex/order"
)
type CoinID = order.CoinID
// CoinLockChecker provides the ability to check if a coin or an order's backing
// coins are locked.
type CoinLockChecker interface {
// CoinLocked indicates if a coin is locked.
CoinLocked(coin CoinID) bool
// OrderCoinsLocked returns all coins locked by an order.
OrderCoinsLocked(oid order.OrderID) []CoinID
}
// CoinLocker provides the ability to lock, unlock and check lock status of
// coins.
type CoinLocker interface {
CoinLockChecker
// UnlockAll releases all locked coins.
UnlockAll()
// UnlockOrderCoins unlocks all locked coins associated with an order.
UnlockOrderCoins(oid order.OrderID)
// UnlockOrdersCoins is like UnlockOrderCoins for multiple orders.
UnlockOrdersCoins(oids []order.OrderID)
// LockOrdersCoins locks all of the coins associated with multiple orders.
LockOrdersCoins(orders []order.Order) (failed []order.Order)
// LockCoins locks coins associated with certain orders. The input is
// defined as a map of OrderIDs to a CoinID slice since it is likely easiest
// for the caller to construct the input in this way.
LockCoins(orderCoins map[order.OrderID][]CoinID) (failed map[order.OrderID][]CoinID)
}
// MasterCoinLocker coordinates a book and swap coin locker. The lock status of
// a coin may be checked, and the locker for the book and swapper may be
// obtained via the Book and Swap methods.
type MasterCoinLocker struct {
bookLock *AssetCoinLocker
swapLock *AssetCoinLocker
}
// NewMasterCoinLocker creates a new NewMasterCoinLocker.
func NewMasterCoinLocker() *MasterCoinLocker {
return &MasterCoinLocker{
bookLock: NewAssetCoinLocker(),
swapLock: NewAssetCoinLocker(),
}
}
// CoinLocked indicates if a coin is locked by either the swap or book lock.
func (cl *MasterCoinLocker) CoinLocked(coin CoinID) bool {
lockedBySwap := cl.swapLock.CoinLocked(coin)
if lockedBySwap {
return true
}
return cl.bookLock.CoinLocked(coin)
}
// OrderCoinsLocked lists all coins locked by a given order are locked by either
// the swap or book lock.
func (cl *MasterCoinLocker) OrderCoinsLocked(oid order.OrderID) []CoinID {
coins := cl.swapLock.OrderCoinsLocked(oid)
if len(coins) > 0 {
return coins
}
// TODO: Figure out how we will handle the evolving coinIDs for an order
// with partial fills and decide how to merge the results of both swap and
// book locks.
return cl.bookLock.OrderCoinsLocked(oid)
}
// Book provides the market-level CoinLocker.
func (cl *MasterCoinLocker) Book() CoinLocker {
return &bookLocker{cl}
}
// Swap provides the swap-level CoinLocker.
func (cl *MasterCoinLocker) Swap() CoinLocker {
return &swapLocker{cl}
}
type bookLocker struct {
*MasterCoinLocker
}
// LockOrdersCoins locks all coins for the given orders.
func (bl *bookLocker) LockOrdersCoins(orders []order.Order) []order.Order {
return bl.bookLock.LockOrdersCoins(orders)
}
// LockOrdersCoins locks coins associated with certain orders.
func (bl *bookLocker) LockCoins(orderCoins map[order.OrderID][]CoinID) map[order.OrderID][]CoinID {
return bl.bookLock.LockCoins(orderCoins)
}
// UnlockAll releases all locked coins.
func (bl *bookLocker) UnlockAll() {
bl.bookLock.UnlockAll()
}
// UnlockOrdersCoins unlocks all locked coins associated with an order.
func (bl *bookLocker) UnlockOrdersCoins(oids []order.OrderID) {
bl.bookLock.UnlockOrdersCoins(oids)
}
// UnlockOrderCoins unlocks all locked coins associated with an order.
func (bl *bookLocker) UnlockOrderCoins(oid order.OrderID) {
bl.bookLock.UnlockOrderCoins(oid)
}
var _ (CoinLocker) = (*bookLocker)(nil)
type swapLocker struct {
*MasterCoinLocker
}
// LockOrdersCoins locks all coins for the given orders.
func (sl *swapLocker) LockOrdersCoins(orders []order.Order) []order.Order {
return sl.swapLock.LockOrdersCoins(orders)
}
// LockOrdersCoins locks coins associated with certain orders.
func (sl *swapLocker) LockCoins(orderCoins map[order.OrderID][]CoinID) map[order.OrderID][]CoinID {
return sl.swapLock.LockCoins(orderCoins)
}
// UnlockAll releases all locked coins.
func (sl *swapLocker) UnlockAll() {
sl.swapLock.UnlockAll()
}
// UnlockOrderCoins unlocks all locked coins associated with an order.
func (sl *swapLocker) UnlockOrderCoins(oid order.OrderID) {
sl.swapLock.UnlockOrderCoins(oid)
}
// UnlockOrdersCoins unlocks all locked coins associated with an order.
func (sl *swapLocker) UnlockOrdersCoins(oids []order.OrderID) {
sl.swapLock.UnlockOrdersCoins(oids)
}
var _ (CoinLocker) = (*swapLocker)(nil)
type coinIDKey string
// AssetCoinLocker is a coin locker for a single asset. Do not use this for more
// than one asset.
type AssetCoinLocker struct {
coinMtx sync.RWMutex
lockedCoins map[coinIDKey]struct{}
lockedCoinsByOrder map[order.OrderID][]CoinID
}
// NewAssetCoinLocker constructs a new AssetCoinLocker.
func NewAssetCoinLocker() *AssetCoinLocker {
return &AssetCoinLocker{
lockedCoins: make(map[coinIDKey]struct{}),
lockedCoinsByOrder: make(map[order.OrderID][]CoinID),
}
}
// UnlockAll releases all locked coins.
func (ac *AssetCoinLocker) UnlockAll() {
ac.coinMtx.Lock()
ac.lockedCoins = make(map[coinIDKey]struct{})
ac.lockedCoinsByOrder = make(map[order.OrderID][]CoinID)
ac.coinMtx.Unlock()
}
// CoinLocked indicates if a coin identifier (e.g. UTXO) is locked.
func (ac *AssetCoinLocker) CoinLocked(coin CoinID) bool {
ac.coinMtx.RLock()
_, locked := ac.lockedCoins[coinIDKey(coin)]
ac.coinMtx.RUnlock()
return locked
}
// OrderCoinsLocked lists the coin IDs (e.g. UTXOs) locked by an order.
func (ac *AssetCoinLocker) OrderCoinsLocked(oid order.OrderID) []CoinID {
ac.coinMtx.RLock()
defer ac.coinMtx.RUnlock()
return ac.lockedCoinsByOrder[oid]
}
// unlockOrderCoins should be called with the coinMtx locked.
func (ac *AssetCoinLocker) unlockOrderCoins(oid order.OrderID) {
coins := ac.lockedCoinsByOrder[oid]
for i := range coins {
delete(ac.lockedCoins, coinIDKey(coins[i]))
}
}
// UnlockOrderCoins unlocks any coins backing the order.
func (ac *AssetCoinLocker) UnlockOrderCoins(oid order.OrderID) {
ac.coinMtx.Lock()
ac.unlockOrderCoins(oid)
ac.coinMtx.Unlock()
}
// UnlockOrdersCoins unlocks any coins backing the orders.
func (ac *AssetCoinLocker) UnlockOrdersCoins(oids []order.OrderID) {
ac.coinMtx.Lock()
for i := range oids {
ac.unlockOrderCoins(oids[i])
}
ac.coinMtx.Unlock()
}
// LockCoins locks all coins (e.g. UTXOS) connected with certain orders.
func (ac *AssetCoinLocker) LockCoins(orderCoins map[order.OrderID][]CoinID) (failed map[order.OrderID][]CoinID) {
failed = make(map[order.OrderID][]CoinID)
ac.coinMtx.Lock()
for oid, coins := range orderCoins {
var fail bool
for i := range coins {
_, locked := ac.lockedCoins[coinIDKey(coins[i])]
if locked {
failed[oid] = append(failed[oid], coins[i])
fail = true
}
}
if fail {
// Do not lock any of this order's coins.
continue
}
ac.lockedCoinsByOrder[oid] = coins
for i := range coins {
ac.lockedCoins[coinIDKey(coins[i])] = struct{}{}
}
}
ac.coinMtx.Unlock()
return
}
// LockOrdersCoins locks all coins associated with certain orders.
func (ac *AssetCoinLocker) LockOrdersCoins(orders []order.Order) (failed []order.Order) {
ac.coinMtx.Lock()
ordersLoop:
for _, ord := range orders {
coinIDs := ord.Trade().Coins
if len(coinIDs) == 0 {
continue // e.g. CancelOrder
}
for i := range coinIDs {
_, locked := ac.lockedCoins[coinIDKey(coinIDs[i])]
if locked {
failed = append(failed, ord)
continue ordersLoop
}
}
ac.lockedCoinsByOrder[ord.ID()] = coinIDs
for i := range coinIDs {
ac.lockedCoins[coinIDKey(coinIDs[i])] = struct{}{}
}
}
ac.coinMtx.Unlock()
return
}
// DEXCoinLocker manages multiple MasterCoinLocker, one for each asset used by
// the DEX.
type DEXCoinLocker struct {
masterLocks map[uint32]*MasterCoinLocker
}
// NewDEXCoinLocker creates a new DEXCoinLocker for the given assets.
func NewDEXCoinLocker(assets []uint32) *DEXCoinLocker {
masterLocks := make(map[uint32]*MasterCoinLocker, len(assets))
for _, asset := range assets {
masterLocks[asset] = NewMasterCoinLocker()
}
return &DEXCoinLocker{masterLocks}
}
// AssetLocker retrieves the MasterCoinLocker for an asset.
func (c *DEXCoinLocker) AssetLocker(asset uint32) *MasterCoinLocker {
return c.masterLocks[asset]
}
// CoinLocked checks if a coin belonging to an asset is locked.
func (c *DEXCoinLocker) CoinLocked(asset uint32, coin string) bool {
locker := c.masterLocks[asset]
if locker == nil {
panic(fmt.Sprintf("unknown asset %d", asset))
}
return locker.CoinLocked(CoinID(coin))
}
// OrderCoinsLocked retrieves all locked coins for a given asset and user.
func (c *DEXCoinLocker) OrderCoinsLocked(asset uint32, oid order.OrderID) []CoinID {
locker := c.masterLocks[asset]
if locker == nil {
panic(fmt.Sprintf("unknown asset %d", asset))
}
return locker.OrderCoinsLocked(oid)
}