-
Notifications
You must be signed in to change notification settings - Fork 255
/
tx.go
871 lines (794 loc) · 21.5 KB
/
tx.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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package types
import (
"bytes"
"encoding/hex"
"encoding/json"
"reflect"
"sort"
"time"
lru "github.com/hashicorp/golang-lru"
"strconv"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/address"
"github.com/33cn/chain33/common/crypto"
)
var (
bCoins = []byte("coins")
bToken = []byte("token")
withdraw = "withdraw"
txCache *lru.Cache
)
func init() {
var err error
txCache, err = lru.New(10240)
if err != nil {
panic(err)
}
}
//TxCacheGet 某些交易的cache 加入缓存中,防止重复进行解析或者计算
func TxCacheGet(tx *Transaction) (*TransactionCache, bool) {
txc, ok := txCache.Get(tx)
if !ok {
return nil, ok
}
return txc.(*TransactionCache), ok
}
//TxCacheSet 设置 cache
func TxCacheSet(tx *Transaction, txc *TransactionCache) {
if txc == nil {
txCache.Remove(tx)
return
}
txCache.Add(tx, txc)
}
// CreateTxGroup 创建组交易, feeRate传入交易费率, 建议通过系统GetProperFee获取
func CreateTxGroup(txs []*Transaction, feeRate int64) (*Transactions, error) {
if len(txs) < 2 {
return nil, ErrTxGroupCountLessThanTwo
}
txgroup := &Transactions{}
txgroup.Txs = txs
totalfee := int64(0)
minfee := int64(0)
header := txs[0].Hash()
for i := len(txs) - 1; i >= 0; i-- {
txs[i].GroupCount = int32(len(txs))
totalfee += txs[i].GetFee()
// Header和Fee设置是为了GetRealFee里面Size的计算,Fee是否为0和不同大小,size也是有差别的,header是否为空差别是common.Sha256Len+2
// 这里直接设置Header兼容性更好, Next不需要,已经设置过了,唯一不同的是,txs[0].fee会跟实际计算有差别,这里设置一个超大值只做计算
txs[i].Header = header
if i == 0 {
//对txs[0].fee设置一个超大值,大于后面实际计算出的fee,也就>=check时候计算出的fee, 对size影响10个字节,在1000临界值时候有差别
txs[i].Fee = 1 << 62
} else {
txs[i].Fee = 0
}
realfee, err := txs[i].GetRealFee(feeRate)
if err != nil {
return nil, err
}
minfee += realfee
if i == 0 {
if totalfee < minfee {
totalfee = minfee
}
txs[0].Fee = totalfee
header = txs[0].Hash()
} else {
txs[i].Fee = 0
txs[i-1].Next = txs[i].Hash()
}
}
for i := 0; i < len(txs); i++ {
txs[i].Header = header
}
return txgroup, nil
}
//Tx 这比用于检查的交易,包含了所有的交易。
//主要是为了兼容原来的设计
func (txgroup *Transactions) Tx() *Transaction {
if len(txgroup.GetTxs()) < 2 {
return nil
}
headtx := txgroup.GetTxs()[0]
//不会影响原来的tx
copytx := *headtx
data := Encode(txgroup)
//放到header中不影响交易的Hash
copytx.Header = data
return ©tx
}
//GetTxGroup 获取交易组
func (txgroup *Transactions) GetTxGroup() *Transactions {
return txgroup
}
//SignN 对交易组的第n笔交易签名
func (txgroup *Transactions) SignN(n int, ty int32, priv crypto.PrivKey) error {
if n >= len(txgroup.GetTxs()) {
return ErrIndex
}
txgroup.GetTxs()[n].Sign(ty, priv)
return nil
}
//CheckSign 检测交易组的签名
func (txgroup *Transactions) CheckSign() bool {
txs := txgroup.Txs
for i := 0; i < len(txs); i++ {
if !txs[i].checkSign() {
return false
}
}
return true
}
//RebuiltGroup 交易内容有变化时需要重新构建交易组
func (txgroup *Transactions) RebuiltGroup() {
header := txgroup.Txs[0].Hash()
for i := len(txgroup.Txs) - 1; i >= 0; i-- {
txgroup.Txs[i].Header = header
if i == 0 {
header = txgroup.Txs[0].Hash()
} else {
txgroup.Txs[i-1].Next = txgroup.Txs[i].Hash()
}
}
for i := 0; i < len(txgroup.Txs); i++ {
txgroup.Txs[i].Header = header
}
}
//SetExpire 设置交易组中交易的过期时间
func (txgroup *Transactions) SetExpire(cfg *Chain33Config, n int, expire time.Duration) {
if n >= len(txgroup.GetTxs()) {
return
}
txgroup.GetTxs()[n].SetExpire(cfg, expire)
}
//IsExpire 交易是否过期
func (txgroup *Transactions) IsExpire(cfg *Chain33Config, height, blocktime int64) bool {
txs := txgroup.Txs
for i := 0; i < len(txs); i++ {
if txs[i].isExpire(cfg, height, blocktime) {
return true
}
}
return false
}
//CheckWithFork 和fork 无关的有个检查函数
func (txgroup *Transactions) CheckWithFork(cfg *Chain33Config, checkFork, paraFork bool, height, minfee, maxFee int64) error {
txs := txgroup.Txs
if len(txs) < 2 {
return ErrTxGroupCountLessThanTwo
}
para := make(map[string]bool)
for i := 0; i < len(txs); i++ {
if txs[i] == nil {
return ErrTxGroupEmpty
}
err := txs[i].check(cfg, height, 0, maxFee)
if err != nil {
return err
}
if title, ok := GetParaExecTitleName(string(txs[i].Execer)); ok {
para[title] = true
}
}
//txgroup 只允许一条平行链的交易, 且平行链txgroup须全部是平行链tx
//如果平行链已经在主链分叉高度前运行了一段时间且有跨链交易,平行链需要自己设置这个fork
if paraFork {
if len(para) > 1 {
tlog.Info("txgroup has multi para transaction")
return ErrTxGroupParaCount
}
if len(para) > 0 {
for _, tx := range txs {
if !IsParaExecName(string(tx.Execer)) {
tlog.Error("para txgroup has main chain transaction")
return ErrTxGroupParaMainMixed
}
}
}
}
for i := 1; i < len(txs); i++ {
if txs[i].Fee != 0 {
return ErrTxGroupFeeNotZero
}
}
//检查txs[0] 的费用是否满足要求
totalfee := int64(0)
for i := 0; i < len(txs); i++ {
fee, err := txs[i].GetRealFee(minfee)
if err != nil {
return err
}
totalfee += fee
}
if txs[0].Fee < totalfee {
return ErrTxFeeTooLow
}
if txs[0].Fee > maxFee && maxFee > 0 && checkFork {
return ErrTxFeeTooHigh
}
//检查hash是否符合要求
for i := 0; i < len(txs); i++ {
//检查头部是否等于头部hash
if i == 0 {
if !bytes.Equal(txs[i].Hash(), txs[i].Header) {
return ErrTxGroupHeader
}
} else {
if !bytes.Equal(txs[0].Header, txs[i].Header) {
return ErrTxGroupHeader
}
}
//检查group count
if txs[i].GroupCount > MaxTxGroupSize {
return ErrTxGroupCountBigThanMaxSize
}
if txs[i].GroupCount != int32(len(txs)) {
return ErrTxGroupCount
}
//检查next
if i < len(txs)-1 {
if !bytes.Equal(txs[i].Next, txs[i+1].Hash()) {
return ErrTxGroupNext
}
} else {
if txs[i].Next != nil {
return ErrTxGroupNext
}
}
}
return nil
}
//Check height == 0 的时候,不做检查
func (txgroup *Transactions) Check(cfg *Chain33Config, height, minfee, maxFee int64) error {
paraFork := cfg.IsFork(height, "ForkTxGroupPara")
checkFork := cfg.IsFork(height, "ForkBlockCheck")
return txgroup.CheckWithFork(cfg, checkFork, paraFork, height, minfee, maxFee)
}
//TransactionCache 交易缓存结构
type TransactionCache struct {
*Transaction
txGroup *Transactions
hash []byte
size int
signok int //init 0, ok 1, err 2
checkok error //init 0, ok 1, err 2
checked bool
payload reflect.Value
plname string
plerr error
}
//NewTransactionCache new交易缓存
func NewTransactionCache(tx *Transaction) *TransactionCache {
return &TransactionCache{Transaction: tx}
}
//Hash 交易hash
func (tx *TransactionCache) Hash() []byte {
if tx.hash == nil {
tx.hash = tx.Transaction.Hash()
}
return tx.hash
}
//SetPayloadValue 设置payload 的cache
func (tx *TransactionCache) SetPayloadValue(plname string, payload reflect.Value, plerr error) {
tx.payload = payload
tx.plerr = plerr
tx.plname = plname
}
//GetPayloadValue 设置payload 的cache
func (tx *TransactionCache) GetPayloadValue() (plname string, payload reflect.Value, plerr error) {
if tx.plerr != nil || tx.plname != "" {
return tx.plname, tx.payload, tx.plerr
}
exec := LoadExecutorType(string(tx.Execer))
if exec == nil {
tx.SetPayloadValue("", reflect.ValueOf(nil), ErrExecNotFound)
return "", reflect.ValueOf(nil), ErrExecNotFound
}
plname, payload, plerr = exec.DecodePayloadValue(tx.Tx())
tx.SetPayloadValue(plname, payload, plerr)
return
}
//Size 交易缓存的大小
func (tx *TransactionCache) Size() int {
if tx.size == 0 {
tx.size = Size(tx.Tx())
}
return tx.size
}
//Tx 交易缓存中tx信息
func (tx *TransactionCache) Tx() *Transaction {
return tx.Transaction
}
//Check 交易缓存中交易组合费用的检测
func (tx *TransactionCache) Check(cfg *Chain33Config, height, minfee, maxFee int64) error {
if !tx.checked {
tx.checked = true
txs, err := tx.GetTxGroup()
if err != nil {
tx.checkok = err
return err
}
if txs == nil {
tx.checkok = tx.check(cfg, height, minfee, maxFee)
} else {
tx.checkok = txs.Check(cfg, height, minfee, maxFee)
}
}
return tx.checkok
}
//GetTotalFee 获取交易真实费用
func (tx *TransactionCache) GetTotalFee(minFee int64) (int64, error) {
txgroup, err := tx.GetTxGroup()
if err != nil {
tx.checkok = err
return 0, err
}
var totalfee int64
if txgroup == nil {
return tx.GetRealFee(minFee)
}
txs := txgroup.Txs
for i := 0; i < len(txs); i++ {
fee, err := txs[i].GetRealFee(minFee)
if err != nil {
return 0, err
}
totalfee += fee
}
return totalfee, nil
}
//GetTxGroup 获取交易组
func (tx *TransactionCache) GetTxGroup() (*Transactions, error) {
var err error
if tx.txGroup == nil {
tx.txGroup, err = tx.Transaction.GetTxGroup()
if err != nil {
return nil, err
}
}
return tx.txGroup, nil
}
//CheckSign 检测签名
func (tx *TransactionCache) CheckSign() bool {
if tx.signok == 0 {
tx.signok = 2
group, err := tx.GetTxGroup()
if err != nil {
return false
}
if group == nil {
//非group,简单校验签名
if ok := tx.checkSign(); ok {
tx.signok = 1
}
} else {
if ok := group.CheckSign(); ok {
tx.signok = 1
}
}
}
return tx.signok == 1
}
//TxsToCache 缓存交易信息
func TxsToCache(txs []*Transaction) (caches []*TransactionCache) {
caches = make([]*TransactionCache, len(txs))
for i := 0; i < len(caches); i++ {
caches[i] = NewTransactionCache(txs[i])
}
return caches
}
//CacheToTxs 从缓存中获取交易信息
func CacheToTxs(caches []*TransactionCache) (txs []*Transaction) {
txs = make([]*Transaction, len(caches))
for i := 0; i < len(caches); i++ {
txs[i] = caches[i].Tx()
}
return txs
}
//HashSign hash 不包含签名,用户通过修改签名无法重新发送交易
func (tx *Transaction) HashSign() []byte {
copytx := *tx
copytx.Signature = nil
data := Encode(©tx)
return common.Sha256(data)
}
//Tx 交易详情
func (tx *Transaction) Tx() *Transaction {
return tx
}
//GetTxGroup 交易组装成交易组格式
func (tx *Transaction) GetTxGroup() (*Transactions, error) {
if tx.GroupCount < 0 || tx.GroupCount == 1 || tx.GroupCount > 20 {
return nil, ErrTxGroupCount
}
if tx.GroupCount > 0 {
var txs Transactions
err := Decode(tx.Header, &txs)
if err != nil {
return nil, err
}
return &txs, nil
}
if tx.Next != nil || tx.Header != nil {
return nil, ErrNomalTx
}
return nil, nil
}
//Size 交易大小
func (tx *Transaction) Size() int {
return Size(tx)
}
//Sign 交易签名
func (tx *Transaction) Sign(ty int32, priv crypto.PrivKey) {
tx.Signature = nil
data := Encode(tx)
pub := priv.PubKey()
sign := priv.Sign(data)
tx.Signature = &Signature{
Ty: ty,
Pubkey: pub.Bytes(),
Signature: sign.Bytes(),
}
}
//CheckSign tx 有些时候是一个交易组
func (tx *Transaction) CheckSign() bool {
return tx.checkSign()
}
//txgroup 的情况
func (tx *Transaction) checkSign() bool {
copytx := *tx
copytx.Signature = nil
data := Encode(©tx)
if tx.GetSignature() == nil {
return false
}
return CheckSign(data, string(tx.Execer), tx.GetSignature())
}
//Check 交易检测
func (tx *Transaction) Check(cfg *Chain33Config, height, minfee, maxFee int64) error {
group, err := tx.GetTxGroup()
if err != nil {
return err
}
if group == nil {
return tx.check(cfg, height, minfee, maxFee)
}
return group.Check(cfg, height, minfee, maxFee)
}
func (tx *Transaction) check(cfg *Chain33Config, height, minfee, maxFee int64) error {
if minfee == 0 {
return nil
}
// 获取当前交易最小交易费
realFee, err := tx.GetRealFee(minfee)
if err != nil {
return err
}
// 检查交易费是否小于最低值
if tx.Fee < realFee {
return ErrTxFeeTooLow
}
if tx.Fee > maxFee && maxFee > 0 && cfg.IsFork(height, "ForkBlockCheck") {
return ErrTxFeeTooHigh
}
//增加交易中chainID的检测,
if tx.ChainID != cfg.GetChainID() {
return ErrTxChainID
}
return nil
}
//SetExpire 设置交易过期时间
func (tx *Transaction) SetExpire(cfg *Chain33Config, expire time.Duration) {
//Txheight处理
if cfg.IsEnable("TxHeight") && int64(expire) > TxHeightFlag {
tx.Expire = int64(expire)
return
}
if int64(expire) > ExpireBound {
if expire < time.Second*120 {
expire = time.Second * 120
}
//用秒数来表示的时间
tx.Expire = Now().Unix() + int64(expire/time.Second)
} else {
tx.Expire = int64(expire)
}
}
//GetRealFee 获取交易真实费用
func (tx *Transaction) GetRealFee(minFee int64) (int64, error) {
txSize := Size(tx)
//如果签名为空,那么加上签名的空间
if tx.Signature == nil {
txSize += 300
}
if txSize > MaxTxSize {
return 0, ErrTxMsgSizeTooBig
}
// 检查交易费是否小于最低值
realFee := int64(txSize/1000+1) * minFee
return realFee, nil
}
//SetRealFee 设置交易真实费用
func (tx *Transaction) SetRealFee(minFee int64) error {
if tx.Fee == 0 {
fee, err := tx.GetRealFee(minFee)
if err != nil {
return err
}
tx.Fee = fee
}
return nil
}
//ExpireBound 交易过期边界值
var ExpireBound int64 = 1000000000 // 交易过期分界线,小于expireBound比较height,大于expireBound比较blockTime
//IsExpire 交易是否过期
func (tx *Transaction) IsExpire(cfg *Chain33Config, height, blocktime int64) bool {
group, _ := tx.GetTxGroup()
if group == nil {
return tx.isExpire(cfg, height, blocktime)
}
return group.IsExpire(cfg, height, blocktime)
}
//GetTxFee 获取交易的费用,区分单笔交易和交易组
func (tx *Transaction) GetTxFee() int64 {
group, _ := tx.GetTxGroup()
if group == nil {
return tx.Fee
}
return group.Txs[0].Fee
}
//From 交易from地址
func (tx *Transaction) From() string {
return address.PubKeyToAddr(tx.GetSignature().GetPubkey())
}
//检查交易是否过期,过期返回true,未过期返回false
func (tx *Transaction) isExpire(cfg *Chain33Config, height, blocktime int64) bool {
valid := tx.Expire
// Expire为0,返回false
if valid == 0 {
return false
}
if valid <= ExpireBound {
//Expire小于1e9,为height valid > height 未过期返回false else true过期
return valid <= height
}
//EnableTxHeight 选项开启, 并且符合条件
if txHeight := GetTxHeight(cfg, valid, height); txHeight > 0 {
if txHeight-LowAllowPackHeight <= height && height <= txHeight+HighAllowPackHeight {
return false
}
return true
}
// Expire大于1e9,为blockTime valid > blocktime返回false 未过期 else true过期
return valid <= blocktime
}
//GetTxHeight 获取交易高度
func GetTxHeight(cfg *Chain33Config, valid int64, height int64) int64 {
if cfg.IsPara() {
return -1
}
if cfg.IsEnableFork(height, "ForkTxHeight", cfg.IsEnable("TxHeight")) && valid > TxHeightFlag {
return valid - TxHeightFlag
}
return -1
}
//JSON Transaction交易信息转成json结构体
func (tx *Transaction) JSON() string {
type transaction struct {
Hash string `json:"hash,omitempty"`
Execer string `json:"execer,omitempty"`
Payload string `json:"payload,omitempty"`
Signature *Signature `json:"signature,omitempty"`
Fee int64 `json:"fee,omitempty"`
Expire int64 `json:"expire,omitempty"`
// 随机ID,可以防止payload 相同的时候,交易重复
Nonce int64 `json:"nonce,omitempty"`
// 对方地址,如果没有对方地址,可以为空
To string `json:"to,omitempty"`
GroupCount int32 `json:"groupCount,omitempty"`
Header string `json:"header,omitempty"`
Next string `json:"next,omitempty"`
ChainID int32 `json:"chainID,omitempty"`
}
newtx := &transaction{}
newtx.Hash = hex.EncodeToString(tx.Hash())
newtx.Execer = string(tx.Execer)
newtx.Payload = hex.EncodeToString(tx.Payload)
newtx.Signature = tx.Signature
newtx.Fee = tx.Fee
newtx.Expire = tx.Expire
newtx.Nonce = tx.Nonce
newtx.To = tx.To
newtx.GroupCount = tx.GroupCount
newtx.Header = hex.EncodeToString(tx.Header)
newtx.Next = hex.EncodeToString(tx.Next)
newtx.ChainID = tx.ChainID
data, err := json.MarshalIndent(newtx, "", "\t")
if err != nil {
return err.Error()
}
return string(data)
}
//Amount 解析tx的payload获取amount值
func (tx *Transaction) Amount() (int64, error) {
// TODO 原来有很多执行器 在这里没有代码, 用默认 0, nil 先
exec := LoadExecutorType(string(tx.Execer))
if exec == nil {
return 0, nil
}
return exec.Amount(tx)
}
//Assets 获取交易中的资产
func (tx *Transaction) Assets() ([]*Asset, error) {
exec := LoadExecutorType(string(tx.Execer))
if exec == nil {
return nil, nil
}
return exec.GetAssets(tx)
}
//GetRealToAddr 解析tx的payload获取real to值
func (tx *Transaction) GetRealToAddr() string {
exec := LoadExecutorType(string(tx.Execer))
if exec == nil {
return tx.To
}
return exec.GetRealToAddr(tx)
}
//GetViewFromToAddr 解析tx的payload获取view from to 值
func (tx *Transaction) GetViewFromToAddr() (string, string) {
exec := LoadExecutorType(string(tx.Execer))
if exec == nil {
return tx.From(), tx.To
}
return exec.GetViewFromToAddr(tx)
}
//ActionName 获取tx交易的Actionname
func (tx *Transaction) ActionName() string {
execName := string(tx.Execer)
exec := LoadExecutorType(execName)
if exec == nil {
//action name 不会影响系统状态,主要是做显示使用
realname := GetRealExecName(tx.Execer)
exec = LoadExecutorType(string(realname))
if exec == nil {
return "unknown"
}
}
return exec.ActionName(tx)
}
//IsWithdraw 判断交易是withdraw交易,需要做from和to地址的swap,方便上层客户理解
func (tx *Transaction) IsWithdraw() bool {
if bytes.Equal(tx.GetExecer(), bCoins) || bytes.Equal(tx.GetExecer(), bToken) {
if tx.ActionName() == withdraw {
return true
}
}
return false
}
// ParseExpire parse expire to int from during or height
func ParseExpire(expire string) (int64, error) {
if len(expire) == 0 {
return 0, ErrInvalidParam
}
if expire[0] == 'H' && expire[1] == ':' {
txHeight, err := strconv.ParseInt(expire[2:], 10, 64)
if err != nil {
return 0, err
}
if txHeight <= 0 {
//fmt.Printf("txHeight should be grate to 0")
return 0, ErrHeightLessZero
}
if txHeight+TxHeightFlag < txHeight {
return 0, ErrHeightOverflow
}
return txHeight + TxHeightFlag, nil
}
blockHeight, err := strconv.ParseInt(expire, 10, 64)
if err == nil {
return blockHeight, nil
}
expireTime, err := time.ParseDuration(expire)
if err == nil {
return int64(expireTime), nil
}
return 0, err
}
//CalcTxShortHash 取txhash的前指定字节,目前默认5
func CalcTxShortHash(hash []byte) string {
if len(hash) >= 5 {
return hex.EncodeToString(hash[0:5])
}
return ""
}
//TransactionSort 对主链以及平行链交易分类
//构造一个map用于临时存储各个子链的交易, 按照title分类,主链交易的title设置成main
//并对map按照title进行排序,不然每次遍历map顺序会不一致
func TransactionSort(rawtxs []*Transaction) []*Transaction {
txMap := make(map[string]*Transactions)
for _, tx := range rawtxs {
title, isPara := GetParaExecTitleName(string(tx.Execer))
if !isPara {
title = MainChainName
}
if txMap[title] != nil {
txMap[title].Txs = append(txMap[title].Txs, tx)
} else {
var temptxs Transactions
temptxs.Txs = append(temptxs.Txs, tx)
txMap[title] = &temptxs
}
}
//需要按照title排序,不然每次遍历的map的顺序会不一致
var newMp = make([]string, 0)
for k := range txMap {
newMp = append(newMp, k)
}
sort.Strings(newMp)
var txs Transactions
for _, v := range newMp {
txs.Txs = append(txs.Txs, txMap[v].GetTxs()...)
}
return txs.GetTxs()
}
//Hash 交易的hash不包含header的值,引入tx group的概念后,做了修改
func (tx *Transaction) Hash() []byte {
copytx := cloneTx(tx)
copytx.Signature = nil
copytx.Header = nil
data := Encode(copytx)
return common.Sha256(data)
}
//FullHash 交易的fullhash包含交易的签名信息
func (tx *Transaction) FullHash() []byte {
copytx := tx.Clone()
data := Encode(copytx)
return common.Sha256(data)
}
//TxGroup 交易组的接口,Transactions 和 Transaction 都符合这个接口
type TxGroup interface {
Tx() *Transaction
GetTxGroup() (*Transactions, error)
CheckSign() bool
}
//这里要避免用 tmp := *tx 这样就会读 可能被 proto 其他线程修改的 size 字段
//proto buffer 字段发生更改之后,一定要修改这里,否则可能引起严重的bug
func cloneTx(tx *Transaction) *Transaction {
copytx := &Transaction{}
copytx.Execer = tx.Execer
copytx.Payload = tx.Payload
copytx.Signature = tx.Signature
copytx.Fee = tx.Fee
copytx.Expire = tx.Expire
copytx.Nonce = tx.Nonce
copytx.To = tx.To
copytx.GroupCount = tx.GroupCount
copytx.Header = tx.Header
copytx.Next = tx.Next
copytx.ChainID = tx.ChainID
return copytx
}
//Clone copytx := proto.Clone(tx).(*Transaction) too slow
func (tx *Transaction) Clone() *Transaction {
if tx == nil {
return nil
}
tmp := cloneTx(tx)
tmp.Signature = tx.Signature.Clone()
return tmp
}
//cloneTxs 拷贝 txs
func cloneTxs(b []*Transaction) []*Transaction {
if b == nil {
return nil
}
txs := make([]*Transaction, len(b))
for i := 0; i < len(b); i++ {
txs[i] = b[i].Clone()
}
return txs
}