forked from gitter-badger/tao2
/
order_transaction.go
349 lines (304 loc) · 10.4 KB
/
order_transaction.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package types
import (
"container/heap"
"errors"
"io"
"math/big"
"sync/atomic"
"github.com/Tao-Network/tao2/common"
"github.com/Tao-Network/tao2/rlp"
)
//go:generate gencodec -type txdata -field-override txdataMarshaling -out gen_tx_json.go
var (
// ErrInvalidOrderSig invalidate signer
ErrInvalidOrderSig = errors.New("invalid transaction v, r, s values")
errNoSignerOrder = errors.New("missing signing methods")
)
const (
OrderStatusNew = "NEW"
OrderStatusOpen = "OPEN"
OrderStatusPartialFilled = "PARTIAL_FILLED"
OrderStatusFilled = "FILLED"
OrderStatusCancelled = "CANCELLED"
OrderTypeMo = "MO"
OrderTypeLo = "LO"
)
// OrderTransaction order transaction
type OrderTransaction struct {
data ordertxdata
// caches
hash atomic.Value
size atomic.Value
from atomic.Value
}
type ordertxdata struct {
AccountNonce uint64 `json:"nonce" gencodec:"required"`
Quantity *big.Int `json:"quantity,omitempty"`
Price *big.Int `json:"price,omitempty"`
ExchangeAddress common.Address `json:"exchangeAddress,omitempty"`
UserAddress common.Address `json:"userAddress,omitempty"`
BaseToken common.Address `json:"baseToken,omitempty"`
QuoteToken common.Address `json:"quoteToken,omitempty"`
Status string `json:"status,omitempty"`
Side string `json:"side,omitempty"`
Type string `json:"type,omitempty"`
OrderID uint64 `json:"orderid,omitempty"`
// Signature values
V *big.Int `json:"v" gencodec:"required"`
R *big.Int `json:"r" gencodec:"required"`
S *big.Int `json:"s" gencodec:"required"`
// This is only used when marshaling to JSON.
Hash common.Hash `json:"hash"`
}
// IsCancelledOrder check if tx is cancelled transaction
func (tx *OrderTransaction) IsCancelledOrder() bool {
if tx.Status() == OrderStatusCancelled {
return true
}
return false
}
// IsMoTypeOrder check if tx type is MO Order
func (tx *OrderTransaction) IsMoTypeOrder() bool {
if tx.Type() == OrderTypeMo {
return true
}
return false
}
// IsLoTypeOrder check if tx type is LO Order
func (tx *OrderTransaction) IsLoTypeOrder() bool {
if tx.Type() == OrderTypeLo {
return true
}
return false
}
// EncodeRLP implements rlp.Encoder
func (tx *OrderTransaction) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, &tx.data)
}
// DecodeRLP implements rlp.Decoder
func (tx *OrderTransaction) DecodeRLP(s *rlp.Stream) error {
_, size, _ := s.Kind()
err := s.Decode(&tx.data)
if err == nil {
tx.size.Store(common.StorageSize(rlp.ListSize(size)))
}
return err
}
// Nonce return nonce of account
func (tx *OrderTransaction) Nonce() uint64 { return tx.data.AccountNonce }
func (tx *OrderTransaction) Quantity() *big.Int { return tx.data.Quantity }
func (tx *OrderTransaction) Price() *big.Int { return tx.data.Price }
func (tx *OrderTransaction) ExchangeAddress() common.Address { return tx.data.ExchangeAddress }
func (tx *OrderTransaction) UserAddress() common.Address { return tx.data.UserAddress }
func (tx *OrderTransaction) BaseToken() common.Address { return tx.data.BaseToken }
func (tx *OrderTransaction) QuoteToken() common.Address { return tx.data.QuoteToken }
func (tx *OrderTransaction) Status() string { return tx.data.Status }
func (tx *OrderTransaction) Side() string { return tx.data.Side }
func (tx *OrderTransaction) Type() string { return tx.data.Type }
func (tx *OrderTransaction) Signature() (V, R, S *big.Int) { return tx.data.V, tx.data.R, tx.data.S }
func (tx *OrderTransaction) OrderHash() common.Hash { return tx.data.Hash }
func (tx *OrderTransaction) OrderID() uint64 { return tx.data.OrderID }
func (tx *OrderTransaction) EncodedSide() *big.Int {
if tx.Side() == "BUY" {
return big.NewInt(0)
} else {
return big.NewInt(1)
}
}
func (tx *OrderTransaction) SetOrderHash(h common.Hash) { tx.data.Hash = h }
// From get transaction from
func (tx *OrderTransaction) From() *common.Address {
if tx.data.V != nil {
signer := OrderTxSigner{}
if f, err := OrderSender(signer, tx); err != nil {
return nil
} else {
return &f
}
} else {
return nil
}
}
// WithSignature returns a new transaction with the given signature.
// This signature needs to be formatted as described in the yellow paper (v+27).
func (tx *OrderTransaction) WithSignature(signer OrderSigner, sig []byte) (*OrderTransaction, error) {
r, s, v, err := signer.SignatureValues(tx, sig)
if err != nil {
return nil, err
}
cpy := &OrderTransaction{data: tx.data}
cpy.data.R, cpy.data.S, cpy.data.V = r, s, v
return cpy, nil
}
// ImportSignature make order tx with specific signature
func (tx *OrderTransaction) ImportSignature(V, R, S *big.Int) *OrderTransaction {
if V != nil {
tx.data.V = V
}
if R != nil {
tx.data.R = R
}
if S != nil {
tx.data.S = S
}
return tx
}
// Hash hashes the RLP encoding of tx.
// It uniquely identifies the transaction.
func (tx *OrderTransaction) Hash() common.Hash {
if hash := tx.hash.Load(); hash != nil {
return hash.(common.Hash)
}
v := rlpHash(tx)
tx.hash.Store(v)
return v
}
// CacheHash cache hash
func (tx *OrderTransaction) CacheHash() {
v := rlpHash(tx)
tx.hash.Store(v)
}
// Size returns the true RLP encoded storage size of the transaction, either by
// encoding and returning it, or returning a previsouly cached value.
func (tx *OrderTransaction) Size() common.StorageSize {
if size := tx.size.Load(); size != nil {
return size.(common.StorageSize)
}
c := writeCounter(0)
rlp.Encode(&c, &tx.data)
tx.size.Store(common.StorageSize(c))
return common.StorageSize(c)
}
// NewOrderTransaction init order from value
func NewOrderTransaction(nonce uint64, quantity, price *big.Int, ex, ua, b, q common.Address, status, side, t string, hash common.Hash, id uint64) *OrderTransaction {
return newOrderTransaction(nonce, quantity, price, ex, ua, b, q, status, side, t, hash, id)
}
func newOrderTransaction(nonce uint64, quantity, price *big.Int, ex, ua, b, q common.Address, status, side, t string, hash common.Hash, id uint64) *OrderTransaction {
d := ordertxdata{
AccountNonce: nonce,
Quantity: new(big.Int),
Price: new(big.Int),
ExchangeAddress: ex,
UserAddress: ua,
BaseToken: b,
QuoteToken: q,
Status: status,
Side: side,
Type: t,
Hash: hash,
OrderID: id,
V: new(big.Int),
R: new(big.Int),
S: new(big.Int),
}
if quantity != nil {
d.Quantity.Set(quantity)
}
if price != nil {
d.Price.Set(price)
}
return &OrderTransaction{data: d}
}
// OrderTransactions is a Transaction slice type for basic sorting.
type OrderTransactions []*OrderTransaction
// Len returns the length of s.
func (s OrderTransactions) Len() int { return len(s) }
// Swap swaps the i'th and the j'th element in s.
func (s OrderTransactions) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// GetRlp implements Rlpable and returns the i'th element of s in rlp.
func (s OrderTransactions) GetRlp(i int) []byte {
enc, _ := rlp.EncodeToBytes(s[i])
return enc
}
// OrderTxDifference returns a new set t which is the difference between a to b.
func OrderTxDifference(a, b OrderTransactions) (keep OrderTransactions) {
keep = make(OrderTransactions, 0, len(a))
remove := make(map[common.Hash]struct{})
for _, tx := range b {
remove[tx.Hash()] = struct{}{}
}
for _, tx := range a {
if _, ok := remove[tx.Hash()]; !ok {
keep = append(keep, tx)
}
}
return keep
}
// OrderTxByNonce sorted order by nonce defined
type OrderTxByNonce OrderTransactions
func (s OrderTxByNonce) Len() int { return len(s) }
func (s OrderTxByNonce) Less(i, j int) bool { return s[i].data.AccountNonce < s[j].data.AccountNonce }
func (s OrderTxByNonce) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s *OrderTxByNonce) Push(x interface{}) {
*s = append(*s, x.(*OrderTransaction))
}
func (s *OrderTxByNonce) Pop() interface{} {
old := *s
n := len(old)
x := old[n-1]
*s = old[0 : n-1]
return x
}
type OrderTransactionByNonce struct {
txs map[common.Address]OrderTransactions
heads OrderTxByNonce
signer OrderSigner
}
func NewOrderTransactionByNonce(signer OrderSigner, txs map[common.Address]OrderTransactions) *OrderTransactionByNonce {
// Initialize a price based heap with the head transactions
heads := make(OrderTxByNonce, 0, len(txs))
for from, accTxs := range txs {
heads = append(heads, accTxs[0])
// Ensure the sender address is from the signer
acc, _ := OrderSender(signer, accTxs[0])
txs[acc] = accTxs[1:]
if from != acc {
delete(txs, from)
}
}
heap.Init(&heads)
// Assemble and return the transaction set
return &OrderTransactionByNonce{
txs: txs,
heads: heads,
signer: signer,
}
}
// Peek returns the next transaction by price.
func (t *OrderTransactionByNonce) Peek() *OrderTransaction {
if len(t.heads) == 0 {
return nil
}
return t.heads[0]
}
// Shift replaces the current best head with the next one from the same account.
func (t *OrderTransactionByNonce) Shift() {
acc, _ := OrderSender(t.signer, t.heads[0])
if txs, ok := t.txs[acc]; ok && len(txs) > 0 {
t.heads[0], t.txs[acc] = txs[0], txs[1:]
heap.Fix(&t.heads, 0)
} else {
heap.Pop(&t.heads)
}
}
// Pop removes the best transaction, *not* replacing it with the next one from
// the same account. This should be used when a transaction cannot be executed
// and hence all subsequent ones should be discarded from the same account.
func (t *OrderTransactionByNonce) Pop() {
heap.Pop(&t.heads)
}