-
Notifications
You must be signed in to change notification settings - Fork 24
/
types.go
executable file
·521 lines (484 loc) · 13.9 KB
/
types.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
*
* This file is part of go-palletone.
* go-palletone is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* go-palletone 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 General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with go-palletone. If not, see <http://www.gnu.org/licenses/>.
* /
*
* * @author PalletOne core developer <dev@pallet.one>
* * @date 2018-2019
*
*/
package txspool
import (
"fmt"
"io"
"math/big"
"strconv"
"sync"
"time"
"github.com/ethereum/go-ethereum/rlp"
"github.com/palletone/go-palletone/common"
"github.com/palletone/go-palletone/dag/modules"
)
type TxPoolTxStatus byte
type ProcessorFunc func(tx *TxPoolTransaction) (getNext bool, err error)
const (
//未打包
TxPoolTxStatus_Unpacked TxPoolTxStatus = iota
//已打包,未稳定
TxPoolTxStatus_Packed
//已丢弃
TxPoolTxStatus_Discarded
//孤儿交易
TxPoolTxStatus_Orphan
)
//对于孤儿Tx来说,现在有一个新的Tx到来,那么本Tx是否可以判断为普通交易
func (tx *TxPoolTransaction) IsFineToNormal(hash common.Hash) bool {
for h, b := range tx.DependOnTxs {
if h == hash {
continue
}
if b == false {
return false
}
}
return true
}
func (tx *TxPoolTransaction) IsDependOnTx(hash common.Hash) bool {
for h := range tx.DependOnTxs {
if h == hash {
return true
}
}
return false
}
func (tx *TxPoolTransaction) IsFrom(addr common.Address) bool {
for _, a := range tx.FromAddr {
if a == addr {
return true
}
}
return false
}
func (tx *TxPoolTransaction) IsTo(addr common.Address) bool {
for _, a := range tx.ToAddr {
if a == addr {
return true
}
}
return false
}
func (tx *TxPoolTransaction) GetTxFee2() uint64 {
total := uint64(0)
for _, aa := range tx.TxFee {
total += aa.Amount
}
return total
}
type TxPoolTransaction struct {
Tx *modules.Transaction
From []*modules.OutPoint
CreationDate time.Time `json:"creation_date"`
Priority_lvl string `json:"priority_lvl"` // 打包的优先级
UnitHash common.Hash
UnitIndex uint64
Pending bool //in unit
Confirmed bool //in unit and stable
IsOrphan bool
Discarded bool // will remove
TxFee []*modules.Addition `json:"tx_fee"`
Index uint64 `json:"index"` // index 是该Unit位置。
Extra []byte
Tag uint64
Expiration time.Time
TxHash common.Hash
ReqHash common.Hash
FromAddr []common.Address
ToAddr []common.Address
IsSysContractRequest bool
IsUserContractFullTx bool
Status TxPoolTxStatus
//该Tx依赖于哪些TxId作为先决条件,value是表示该依赖的Tx是否已经在Dag或者交易池找到
DependOnTxs map[common.Hash]bool
}
func (tx *TxPoolTransaction) Less(otherTx interface{}) bool {
ap, _ := strconv.ParseFloat(tx.Priority_lvl, 64)
bp, _ := strconv.ParseFloat(otherTx.(*TxPoolTransaction).Priority_lvl, 64)
return ap < bp
}
func (tx *TxPoolTransaction) GetPriorityLvl() string {
if tx.Priority_lvl != "" && tx.Priority_lvl > "0" {
return tx.Priority_lvl
}
var priority_lvl float64
if txfee := tx.GetTxFee(); txfee.Int64() > 0 {
if tx.CreationDate.Unix() <= 0 {
tx.CreationDate = time.Now()
}
priority_lvl, _ = strconv.ParseFloat(fmt.Sprintf("%f", float64(txfee.Int64())/
tx.Tx.Size().Float64()*(1+float64(time.Now().Second()-tx.CreationDate.Second())/(24*3600))), 64)
}
tx.Priority_lvl = strconv.FormatFloat(priority_lvl, 'f', -1, 64)
return tx.Priority_lvl
}
func (tx *TxPoolTransaction) GetPriorityfloat64() float64 {
level, _ := strconv.ParseFloat(tx.Priority_lvl, 64)
if level > 0 {
return level
}
var priority_lvl float64
if txfee := tx.GetTxFee(); txfee.Int64() > 0 {
if tx.CreationDate.Unix() <= 0 {
tx.CreationDate = time.Now()
}
priority_lvl, _ = strconv.ParseFloat(fmt.Sprintf("%f", float64(txfee.Int64())/
tx.Tx.Size().Float64()*(1+float64(time.Now().Second()-tx.CreationDate.Second())/(24*3600))), 64)
}
return priority_lvl
}
func (tx *TxPoolTransaction) SetPriorityLvl(priority float64) {
tx.Priority_lvl = strconv.FormatFloat(priority, 'f', -1, 64)
}
func (tx *TxPoolTransaction) GetTxFee() *big.Int {
var fee uint64
if tx.TxFee != nil {
for _, ad := range tx.TxFee {
fee += ad.Amount
}
} else {
fee = 20 // 20dao
}
return big.NewInt(int64(fee))
}
//type Transactions []*Transaction
type TxPoolTxs []*TxPoolTransaction
// TxByPrice implements both the sort and the heap interface, making it useful
// for all at once sorting as well as individually adding and removing elements.
type TxByPrice TxPoolTxs
func (s TxByPrice) Len() int { return len(s) }
func (s TxByPrice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s *TxByPrice) Push(x interface{}) {
*s = append(*s, x.(*TxPoolTransaction))
}
func (s *TxByPrice) Pop() interface{} {
old := *s
n := len(old)
x := old[n-1]
*s = old[0 : n-1]
return x
}
type TxByPriority []*TxPoolTransaction
func (s TxByPriority) Len() int { return len(s) }
func (s TxByPriority) Less(i, j int) bool { return s[i].Priority_lvl > s[j].Priority_lvl }
func (s TxByPriority) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s *TxByPriority) Push(x interface{}) {
*s = append(*s, x.(*TxPoolTransaction))
}
func (s *TxByPriority) Pop() interface{} {
old := *s
n := len(old)
x := old[n-1]
*s = old[0 : n-1]
return x
}
type TxByCreationDate []*TxPoolTransaction
func (tc TxByCreationDate) Len() int { return len(tc) }
func (tc TxByCreationDate) Less(i, j int) bool { return tc[i].Priority_lvl > tc[j].Priority_lvl }
func (tc TxByCreationDate) Swap(i, j int) { tc[i], tc[j] = tc[j], tc[i] }
type SequeueTxPoolTxs struct {
seqtxs []*TxPoolTransaction
mu sync.RWMutex
}
// add
func (seqTxs *SequeueTxPoolTxs) Len() int {
seqTxs.mu.RLock()
defer seqTxs.mu.RUnlock()
return len((*seqTxs).seqtxs)
}
func (seqTxs *SequeueTxPoolTxs) Add(newPoolTx *TxPoolTransaction) {
seqTxs.mu.Lock()
defer seqTxs.mu.Unlock()
(*seqTxs).seqtxs = append((*seqTxs).seqtxs, newPoolTx)
}
// add priority
func (seqTxs *SequeueTxPoolTxs) AddPriority(newPoolTx *TxPoolTransaction) {
seqTxs.mu.Lock()
defer seqTxs.mu.Unlock()
if seqTxs.Len() == 0 {
(*seqTxs).seqtxs = append((*seqTxs).seqtxs, newPoolTx)
} else {
added := false
for i, item := range (*seqTxs).seqtxs {
if newPoolTx.GetPriorityfloat64() > item.GetPriorityfloat64() {
(*seqTxs).seqtxs = append((*seqTxs).seqtxs[:i], append([]*TxPoolTransaction{newPoolTx}, (*seqTxs).seqtxs[i:]...)...)
added = true
break
}
}
if !added {
(*seqTxs).seqtxs = append((*seqTxs).seqtxs, newPoolTx)
}
}
}
// get
func (seqTxs *SequeueTxPoolTxs) Get() *TxPoolTransaction {
seqTxs.mu.Lock()
defer seqTxs.mu.Unlock()
if seqTxs.Len() <= 0 {
return nil
}
if seqTxs.Len() == 1 {
first := (*seqTxs).seqtxs[0]
(*seqTxs).seqtxs = make([]*TxPoolTransaction, 0)
return first
}
first, rest := (*seqTxs).seqtxs[0], (*seqTxs).seqtxs[1:]
(*seqTxs).seqtxs = rest
return first
}
// get all
func (seqTxs *SequeueTxPoolTxs) All() []*TxPoolTransaction {
seqTxs.mu.Lock()
defer seqTxs.mu.Unlock()
items := (*seqTxs).seqtxs[:]
(*seqTxs).seqtxs = make([]*TxPoolTransaction, 0)
return items
}
type txpoolTransactionTemp struct {
Msgs []messageTemp
CertId []byte
Illegal bool
Version uint32
Nonce uint64
From []modules.OutPoint
CreationDate time.Time `json:"creation_date"`
Priority_lvl string `json:"priority_lvl"`
UnitHash common.Hash
UnitIndex uint64
Pending bool
Confirmed bool
IsOrphan bool
Discarded bool // will remove
TxFee []modules.Addition `json:"tx_fee"`
Index uint64 `json:"index"`
Extra []byte
Tag uint64
Expiration time.Time
DependOnTxsTrue []common.Hash
DependOnTxsFalse []common.Hash
}
type messageTemp struct {
App modules.MessageType
Data []byte
}
func (pooltx *TxPoolTransaction) EncodeRLP(w io.Writer) error {
temp := &txpoolTransactionTemp{}
msgs := pooltx.Tx.Messages()
for _, m := range msgs {
m1 := messageTemp{
App: m.App,
}
d, err := rlp.EncodeToBytes(m.Payload)
if err != nil {
return err
}
m1.Data = d
temp.Msgs = append(temp.Msgs, m1)
}
temp.CertId = common.CopyBytes(pooltx.Tx.CertId())
temp.Illegal = pooltx.Tx.Illegal()
temp.Version = pooltx.Tx.Version()
temp.Nonce = pooltx.Tx.Nonce()
for _, from := range pooltx.From {
temp.From = append(temp.From, *from)
}
temp.CreationDate = pooltx.CreationDate
temp.Priority_lvl = pooltx.Priority_lvl
temp.UnitHash = pooltx.UnitHash
temp.UnitIndex = pooltx.UnitIndex
temp.Pending = pooltx.Pending
temp.Confirmed = pooltx.Confirmed
temp.IsOrphan = pooltx.IsOrphan
temp.Discarded = pooltx.Discarded
for _, addition := range pooltx.TxFee {
temp.TxFee = append(temp.TxFee, *addition)
}
temp.Index = pooltx.Index
temp.Extra = pooltx.Extra
temp.Tag = pooltx.Tag
temp.Expiration = pooltx.Expiration
if len(pooltx.DependOnTxs) > 0 {
//temp.DependOnTxs = append(temp.DependOnTxs, pooltx.DependOnTxs...)
for tx, pass := range pooltx.DependOnTxs {
if pass {
temp.DependOnTxsTrue = append(temp.DependOnTxsTrue, tx)
} else {
temp.DependOnTxsFalse = append(temp.DependOnTxsFalse, tx)
}
}
}
return rlp.Encode(w, temp)
}
func (pooltx *TxPoolTransaction) DecodeRLP(s *rlp.Stream) error {
raw, err := s.Raw()
if err != nil {
return err
}
temp := &txpoolTransactionTemp{}
err = rlp.DecodeBytes(raw, temp)
if err != nil {
return err
}
msgs := make([]*modules.Message, 0)
for _, m := range temp.Msgs {
m1 := &modules.Message{
App: m.App,
}
if m.App == modules.APP_PAYMENT {
var pay modules.PaymentPayload
err := rlp.DecodeBytes(m.Data, &pay)
if err != nil {
return err
}
m1.Payload = &pay
} else if m.App == modules.APP_DATA {
var text modules.DataPayload
err := rlp.DecodeBytes(m.Data, &text)
if err != nil {
return err
}
m1.Payload = &text
} else if m.App == modules.APP_CONTRACT_TPL_REQUEST {
var payload modules.ContractInstallRequestPayload
err := rlp.DecodeBytes(m.Data, &payload)
if err != nil {
return err
}
m1.Payload = &payload
} else if m.App == modules.APP_CONTRACT_TPL {
var payload modules.ContractTplPayload
err := rlp.DecodeBytes(m.Data, &payload)
if err != nil {
return err
}
m1.Payload = &payload
} else if m.App == modules.APP_CONTRACT_DEPLOY_REQUEST {
var payload modules.ContractDeployRequestPayload
err := rlp.DecodeBytes(m.Data, &payload)
if err != nil {
return err
}
m1.Payload = &payload
} else if m.App == modules.APP_CONTRACT_DEPLOY {
var payload modules.ContractDeployPayload
err := rlp.DecodeBytes(m.Data, &payload)
if err != nil {
temp := &modules.ContractDeployPayloadV1{}
err = rlp.DecodeBytes(m.Data, temp)
if err != nil {
return err
}
payload.TemplateId = temp.TemplateId
payload.ContractId = temp.ContractId
payload.Name = temp.Name
payload.Args = temp.Args
payload.EleNode.EleList = temp.EleList
payload.ReadSet = temp.ReadSet
payload.WriteSet = temp.WriteSet
payload.ErrMsg = temp.ErrMsg
}
m1.Payload = &payload
} else if m.App == modules.APP_CONTRACT_INVOKE_REQUEST {
var payload modules.ContractInvokeRequestPayload
err := rlp.DecodeBytes(m.Data, &payload)
if err != nil {
return err
}
m1.Payload = &payload
} else if m.App == modules.APP_CONTRACT_INVOKE {
var payload modules.ContractInvokePayload
err := rlp.DecodeBytes(m.Data, &payload)
if err != nil {
return err
}
m1.Payload = &payload
} else if m.App == modules.APP_CONTRACT_STOP_REQUEST {
var payload modules.ContractStopRequestPayload
err := rlp.DecodeBytes(m.Data, &payload)
if err != nil {
return err
}
m1.Payload = &payload
} else if m.App == modules.APP_CONTRACT_STOP {
var payload modules.ContractStopPayload
err := rlp.DecodeBytes(m.Data, &payload)
if err != nil {
return err
}
m1.Payload = &payload
} else if m.App == modules.APP_SIGNATURE {
var sigPayload modules.SignaturePayload
err := rlp.DecodeBytes(m.Data, &sigPayload)
if err != nil {
return err
}
m1.Payload = &sigPayload
} else if m.App == modules.APP_ACCOUNT_UPDATE {
var accountUpdateOp modules.AccountStateUpdatePayload
err := rlp.DecodeBytes(m.Data, &accountUpdateOp)
if err != nil {
return err
}
m1.Payload = &accountUpdateOp
} else {
fmt.Println("Unknown message app type:", m.App)
}
msgs = append(msgs, m1)
}
pooltx.Tx = modules.NewTransaction(msgs)
pooltx.Tx.SetIllegal(temp.Illegal)
pooltx.Tx.SetVersion(temp.Version)
pooltx.Tx.SetNonce(temp.Nonce)
pooltx.Tx.SetCertId(common.CopyBytes(temp.CertId))
pooltx.From = make([]*modules.OutPoint, 0)
for _, from := range temp.From {
f := from
pooltx.From = append(pooltx.From, &f)
}
pooltx.CreationDate = temp.CreationDate
pooltx.Priority_lvl = temp.Priority_lvl
pooltx.UnitHash = temp.UnitHash
pooltx.UnitIndex = temp.UnitIndex
pooltx.Pending = temp.Pending
pooltx.Confirmed = temp.Confirmed
pooltx.IsOrphan = temp.IsOrphan
pooltx.Discarded = temp.Discarded
pooltx.TxFee = make([]*modules.Addition, 0)
for _, addition := range temp.TxFee {
a := addition
pooltx.TxFee = append(pooltx.TxFee, &a)
}
pooltx.Index = temp.Index
pooltx.Extra = common.CopyBytes(temp.Extra)
pooltx.Tag = temp.Tag
pooltx.Expiration = temp.Expiration
for _, tx := range temp.DependOnTxsTrue {
pooltx.DependOnTxs[tx] = true
}
for _, tx := range temp.DependOnTxsFalse {
pooltx.DependOnTxs[tx] = false
}
//if len(temp.DependOnTxs) > 0 {
// pooltx.DependOnTxs = append(temp.DependOnTxs, temp.DependOnTxs...)
//}
return nil
}