/
transaction.go
999 lines (907 loc) · 29.4 KB
/
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
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
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
// Copyright (c) 2018 The MATRIX Authors
// Distributed under the MIT software license, see the accompanying
// file COPYING or or http://www.opensource.org/licenses/mit-license.php
package types
import (
"container/heap"
"errors"
"github.com/MatrixAINetwork/go-matrix/base58"
"github.com/MatrixAINetwork/go-matrix/common"
"github.com/MatrixAINetwork/go-matrix/common/hexutil"
"github.com/MatrixAINetwork/go-matrix/crypto"
"github.com/MatrixAINetwork/go-matrix/log"
"github.com/MatrixAINetwork/go-matrix/params"
"github.com/MatrixAINetwork/go-matrix/rlp"
"io"
"math/big"
"sync/atomic"
"time"
)
//go:generate gencodec -type txdata -field-override txdataMarshaling -out gen_tx_json.go
var (
ErrInvalidSig = errors.New("invalid transaction v, r, s values")
)
func init() {
rlp.InterfaceConstructorMap[uint16(NormalTxIndex)] = func() interface{} {
return &Transaction{}
}
rlp.InterfaceConstructorMap[uint16(BroadCastTxIndex)] = func() interface{} {
return &TransactionBroad{}
}
}
// deriveSigner makes a *best* guess about which signer to use.
//func deriveSigner(V *big.Int) Signer {
// if V.Sign() != 0 && isProtectedV(V) {
// return NewEIP155Signer(deriveChainId(V))
// } else {
// return HomesteadSigner{}
// }
//}
type Transaction struct {
data txdata
// caches
hash atomic.Value
size atomic.Value
from atomic.Value
entrustfrom atomic.Value
//Mtype bool
Currency string //币种
// by
N []uint32
IsEntrustGas bool
IsEntrustByTime bool //是否是按时间委托(不是按时间就是按高度,二选一)
}
type TransactionCall struct {
*Transaction
}
func (tc *TransactionCall) CheckNonce() bool { return false }
//
type Transaction_Mx struct {
Data txdata
//Mtype bool //
Currency string
TxType_Mx byte
LockHeight uint64 `json:"lockHeight" gencodec:"required"`
ExtraTo []Tx_to `json:"extra_to" gencodec:"required"`
}
//
type ExtraTo_tr struct {
To_tr *common.Address `json:"to"`
Value_tr *hexutil.Big `json:"value"`
Input_tr *hexutil.Bytes `json:"input"`
}
//
type Tx_to struct {
Recipient *common.Address `json:"to" rlp:"nil"` // nil means contract creation
Amount *big.Int `json:"value" gencodec:"required"`
Payload []byte `json:"input" gencodec:"required"`
}
//
type Matrix_Extra struct {
TxType byte `json:"txType" gencodec:"required"`
LockHeight uint64 `json:"lockHeight" gencodec:"required"`
//ExtraTo []Tx_to `json:"extra_to" gencodec:"required"`
ExtraTo []Tx_to ` rlp:"tail"` //
}
// 用于洪泛(传输)
type Floodtxdata struct {
AccountNonce uint64 `json:"nonce" gencodec:"required"`
Price *big.Int `json:"gasPrice" gencodec:"required"`
GasLimit uint64 `json:"gas" gencodec:"required"`
Recipient *common.Address `json:"to" rlp:"nil"` // nil means contract creation
Amount *big.Int `json:"value" gencodec:"required"`
Payload []byte `json:"input" gencodec:"required"`
//Mtype bool //
Currency string
// Signature values
V *big.Int `json:"v" gencodec:"required"`
R *big.Int `json:"r" gencodec:"required"`
TxEnterType byte `json:"TxEnterType" gencodec:"required"` //是否是委托
IsEntrustTx byte `json:"IsEntrustTx" gencodec:"required"` //是否是委托
CommitTime uint64 `json:"CommitTime" gencodec:"required"` //创建交易时间
Extra []Matrix_Extra ` rlp:"tail"`
}
type txdata struct {
AccountNonce uint64 `json:"nonce" gencodec:"required"`
Price *big.Int `json:"gasPrice" gencodec:"required"`
GasLimit uint64 `json:"gas" gencodec:"required"`
Recipient *common.Address `json:"to" rlp:"nil"` // nil means contract creation
Amount *big.Int `json:"value" gencodec:"required"`
Payload []byte `json:"input" gencodec:"required"`
// 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" rlp:"-"`
TxEnterType byte `json:"TxEnterType" gencodec:"required"` //入池类型
IsEntrustTx byte `json:"IsEntrustTx" gencodec:"required"` //是否是委托
CommitTime uint64 `json:"CommitTime" gencodec:"required"` //创建交易时间
Extra []Matrix_Extra ` rlp:"tail"` //
}
//==================================zhenghe==========================================//
func TxdataAddresToString(currency string, data *txdata, data1 *txdata1) {
data1.AccountNonce = data.AccountNonce
data1.Price = data.Price
data1.GasLimit = data.GasLimit
data1.Amount = data.Amount
data1.Payload = data.Payload
data1.V = data.V
data1.R = data.R
data1.S = data.S
data1.Hash = data.Hash
data1.TxEnterType = data.TxEnterType
data1.IsEntrustTx = data.IsEntrustTx
data1.CommitTime = data.CommitTime
if data.Recipient != nil {
data1.Recipient = new(string)
to := *data.Recipient
*data1.Recipient = base58.Base58EncodeToString(currency, to)
}
if len(data.Extra) > 0 {
tmpEx1 := make([]Matrix_Extra1, 0)
for _, er := range data.Extra {
tmpEr1 := new(Matrix_Extra1)
tmpEr1.TxType = er.TxType
tmpEr1.LockHeight = er.LockHeight
exto := make([]Tx_to1, 0)
if len(er.ExtraTo) > 0 {
for _, tto := range er.ExtraTo {
tmTo := new(Tx_to1)
if tto.Recipient != nil {
tmTo.Recipient = new(string)
*tmTo.Recipient = base58.Base58EncodeToString(currency, *tto.Recipient)
}
tmTo.Payload = tto.Payload
tmTo.Amount = tto.Amount
exto = append(exto, *tmTo)
}
}
tmpEr1.ExtraTo = exto
tmpEx1 = append(tmpEx1, *tmpEr1)
}
data1.Extra = tmpEx1
}
}
func TxdataStringToAddres(data1 *txdata1, data *txdata) {
data.AccountNonce = data1.AccountNonce
data.Price = data1.Price
data.GasLimit = data1.GasLimit
data.Amount = data1.Amount
data.Payload = data1.Payload
data.V = data1.V
data.R = data1.R
data.S = data1.S
data.Hash = data1.Hash
data.TxEnterType = data1.TxEnterType
data.IsEntrustTx = data1.IsEntrustTx
data.CommitTime = data1.CommitTime
if data1.Recipient != nil {
data.Recipient = new(common.Address)
*data.Recipient ,_ = base58.Base58DecodeToAddress(*data1.Recipient)
}
if len(data1.Extra) > 0 {
tmpEx1 := make([]Matrix_Extra, 0)
for _, er := range data1.Extra {
tmpEr1 := new(Matrix_Extra)
tmpEr1.TxType = er.TxType
tmpEr1.LockHeight = er.LockHeight
exto := make([]Tx_to, 0)
if len(er.ExtraTo) > 0 {
for _, tto := range er.ExtraTo {
tmTo := new(Tx_to)
if tto.Recipient != nil {
tmTo.Recipient = new(common.Address)
*tmTo.Recipient,_ = base58.Base58DecodeToAddress(*tto.Recipient)
}
tmTo.Payload = tto.Payload
tmTo.Amount = tto.Amount
exto = append(exto, *tmTo)
}
}
tmpEr1.ExtraTo = exto
tmpEx1 = append(tmpEx1, *tmpEr1)
}
data.Extra = tmpEx1
}
}
type Tx_to1 struct {
Recipient *string `json:"to" rlp:"nil"` // nil means contract creation
Amount *big.Int `json:"value" gencodec:"required"`
Payload []byte `json:"input" gencodec:"required"`
}
type Matrix_Extra1 struct {
TxType byte `json:"txType" gencodec:"required"`
LockHeight uint64 `json:"lockHeight" gencodec:"required"`
ExtraTo []Tx_to1 `json:"extra_to" gencodec:"required"`
//ExtraTo []Tx_to1 ` rlp:"tail"` //
}
//to地址为string类型
type txdata1 struct {
AccountNonce uint64 `json:"nonce" gencodec:"required"`
Price *big.Int `json:"gasPrice" gencodec:"required"`
GasLimit uint64 `json:"gas" gencodec:"required"`
Recipient *string `json:"to" rlp:"nil"` // nil means contract creation
Amount *big.Int `json:"value" gencodec:"required"`
Payload []byte `json:"input" gencodec:"required"`
// 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" rlp:"-"`
TxEnterType byte `json:"TxEnterType" gencodec:"required"` //入池类型
IsEntrustTx byte `json:"IsEntrustTx" gencodec:"required"` //是否是委托
CommitTime uint64 `json:"CommitTime" gencodec:"required"` //创建交易时间
Extra []Matrix_Extra1 ` rlp:"tail"` //
}
//============================================================================//
type txdataMarshaling struct {
AccountNonce hexutil.Uint64
Price *hexutil.Big
GasLimit hexutil.Uint64
Amount *hexutil.Big
Payload hexutil.Bytes
V *hexutil.Big
R *hexutil.Big
S *hexutil.Big
}
func NewTransaction(nonce uint64, to common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte, V *big.Int,R *big.Int,S *big.Int,typ byte, isEntrustTx byte,currency string,committime uint64) *Transaction {
return newTransaction(nonce, &to, amount, gasLimit, gasPrice, data, V,R,S,typ, isEntrustTx,currency,committime)
}
func NewContractCreation(nonce uint64, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte,V *big.Int,R *big.Int,S *big.Int, typ byte, isEntrustTx byte,currency string,committime uint64) *Transaction {
return newTransaction(nonce, nil, amount, gasLimit, gasPrice, data, V,R,S,typ, isEntrustTx,currency,committime)
}
//
func NewTransactions(nonce uint64, to common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte,V *big.Int,R *big.Int,S *big.Int, ex []*ExtraTo_tr, localtime uint64, txType byte, isEntrustTx byte,currency string,committime uint64) *Transaction {
return newTransactions(nonce, &to, amount, gasLimit, gasPrice, data,V,R,S, ex, localtime, txType, isEntrustTx,currency,committime)
}
//
func newTransactions(nonce uint64, to *common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte,V *big.Int,R *big.Int,S *big.Int, ex []*ExtraTo_tr, localtime uint64, txType byte, isEntrustTx byte,currency string,committime uint64) *Transaction {
if len(data) > 0 {
data = common.CopyBytes(data)
}
d := txdata{
AccountNonce: nonce,
Recipient: to,
Payload: data,
Amount: new(big.Int),
GasLimit: gasLimit,
Price: new(big.Int),
V: new(big.Int),
R: new(big.Int),
S: new(big.Int),
TxEnterType: NormalTxIndex,
IsEntrustTx: isEntrustTx,
CommitTime: committime,
Extra: make([]Matrix_Extra, 0),
}
if amount != nil {
d.Amount.Set(amount)
}
if gasPrice != nil {
d.Price.Set(gasPrice)
}
if V != nil{
d.V.Set(V)
}
if R != nil{
d.R.Set(R)
}
if S != nil{
d.S.Set(S)
}
//
matrixEx := new(Matrix_Extra)
arrayTx := make([]Tx_to, 0)
if len(ex) > 0 {
for _, extro := range ex {
var input []byte
if extro.Input_tr == nil {
input = make([]byte, 0)
} else {
input = *extro.Input_tr
}
txto := new(Tx_to)
txto.Amount = (*big.Int)(extro.Value_tr)
txto.Recipient = extro.To_tr
txto.Payload = input
arrayTx = append(arrayTx, *txto)
}
}
//if txType == common.ExtraRevocable {
// d.CommitTime = uint64(time.Now().Unix()) + uint64(300)
//} else if txType == common.ExtraTimeTxType {
// d.CommitTime = uint64(time.Now().Unix()) + uint64(600)
//} else {
// d.CommitTime = committime
//}
d.CommitTime = committime
matrixEx.TxType = txType
matrixEx.LockHeight = localtime
matrixEx.ExtraTo = arrayTx
d.Extra = append(d.Extra, *matrixEx)
tx := &Transaction{Currency:currency,data: d}
return tx
}
func newTransaction(nonce uint64, to *common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte,V *big.Int,R *big.Int,S *big.Int, typ byte, isEntrustTx byte,currency string,committime uint64) *Transaction {
if len(data) > 0 {
data = common.CopyBytes(data)
}
d := txdata{
AccountNonce: nonce,
Recipient: to,
Payload: data,
Amount: new(big.Int),
GasLimit: gasLimit,
Price: new(big.Int),
V: new(big.Int),
R: new(big.Int),
S: new(big.Int),
TxEnterType: NormalTxIndex,
IsEntrustTx: isEntrustTx,
CommitTime: committime,
}
/*
if typ > 0 {
mx := new(Matrix_Extra)
mx.TxType = typ
d.Extra = append(d.Extra, *mx)
}
*/
mx := new(Matrix_Extra)
mx.TxType = typ
d.Extra = append(d.Extra, *mx)
if amount != nil {
d.Amount.Set(amount)
}
if gasPrice != nil {
d.Price.Set(gasPrice)
}
if V != nil{
d.V.Set(V)
}
if R != nil{
d.R.Set(R)
}
if S != nil{
d.S.Set(S)
}
tx := &Transaction{Currency:currency,data: d}
return tx
}
// ChainId returns which chain id this transaction was signed for (if at all)
func (tx *Transaction) ChainId() *big.Int {
return deriveChainId(tx.data.V)
}
// Protected returns whether the transaction is protected from replay protection.
//func (tx *Transaction) Protected() bool {
// return isProtectedV(tx.data.V)
//}
func isProtectedV(V *big.Int) bool {
if V.BitLen() <= 8 {
v := V.Uint64()
return v != 27 && v != 28
}
// anything not 27 or 28 are considered unprotected
return true
}
type extTransaction struct {
Data txdata
Currency string
//Mtype bool
From common.Address
}
// EncodeRLP implements rlp.Encoder
func (tx *Transaction) EncodeRLP(w io.Writer) error {
etx := &extTransaction{Data: tx.data, Currency: tx.Currency}
if tx.GetMatrixType() == common.ExtraUnGasMinerTxType || tx.GetMatrixType() == common.ExtraUnGasValidatorTxType ||
tx.GetMatrixType() == common.ExtraUnGasInterestTxType || tx.GetMatrixType() == common.ExtraUnGasTxsType || tx.GetMatrixType() == common.ExtraUnGasLotteryTxType {
etx.From = tx.From()
}
return rlp.Encode(w, etx)
}
// DecodeRLP implements rlp.Decoder
func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
var err error
_, size, _ := s.Kind()
var extData extTransaction
err = s.Decode(&extData)
tx.data = extData.Data
tx.Currency = extData.Currency
if tx.GetMatrixType() == common.ExtraUnGasMinerTxType || tx.GetMatrixType() == common.ExtraUnGasValidatorTxType ||
tx.GetMatrixType() == common.ExtraUnGasInterestTxType || tx.GetMatrixType() == common.ExtraUnGasTxsType || tx.GetMatrixType() == common.ExtraUnGasLotteryTxType {
tx.SetFromLoad(extData.From)
}
if err == nil {
tx.size.Store(common.StorageSize(rlp.ListSize(size)))
}
return err
}
// MarshalJSON encodes the web3 RPC transaction format.
func (tx *Transaction) MarshalJSON() ([]byte, error) {
hash := tx.Hash()
data := tx.data
data.Hash = &hash
return data.MarshalJSON()
}
// UnmarshalJSON decodes the web3 RPC transaction format.
func (tx *Transaction) UnmarshalJSON(input []byte) error {
var dec txdata
if err := dec.UnmarshalJSON(input); err != nil {
return err
}
var V byte
if isProtectedV(dec.V) {
chainID := deriveChainId(dec.V).Uint64()
V = byte(dec.V.Uint64() - 35 - 2*chainID)
} else {
V = byte(dec.V.Uint64() - 27)
}
if !crypto.ValidateSignatureValues(V, dec.R, dec.S, false) {
return ErrInvalidSig
}
*tx = Transaction{data: dec}
return nil
}
type man_txdata struct {
currency string
data txdata1
}
func (tx *Transaction) ManTx_UnmarshalJSON(input []byte) error {
var dec man_txdata
if err := dec.UnmarshalJSON(input); err != nil {
return err
}
var V byte
if isProtectedV(dec.data.V) {
chainID := deriveChainId(dec.data.V).Uint64()
V = byte(dec.data.V.Uint64() - 35 - 2*chainID)
} else {
V = byte(dec.data.V.Uint64() - 27)
}
if !crypto.ValidateSignatureValues(V, dec.data.R, dec.data.S, false) {
return ErrInvalidSig
}
TxdataStringToAddres(&dec.data, &tx.data)
*tx = Transaction{Currency:dec.currency,data: tx.data}
return nil
}
func (tx *Transaction) GetConstructorType() uint16 {
return uint16(NormalTxIndex)
}
func (tx *Transaction) Data() []byte { return common.CopyBytes(tx.data.Payload) }
func (tx *Transaction) Gas() uint64 { return tx.data.GasLimit }
func (tx *Transaction) GasPrice() *big.Int { return new(big.Int).Set(tx.data.Price) }
func (tx *Transaction) Value() *big.Int { return new(big.Int).Set(tx.data.Amount) }
func (tx *Transaction) Nonce() uint64 { return tx.data.AccountNonce }
func (tx *Transaction) CheckNonce() bool { return true }
func (tx *Transaction) GetTxHashStruct() {
}
func (tx *Transaction) GetCreateTime() uint32 {
return uint32(tx.data.CommitTime)
}
func (tx *Transaction) Call() error {
return nil
}
func (tx *Transaction) GetLocalHeight() uint32 {
if tx.data.Extra != nil && len(tx.data.Extra) > 0 {
return uint32(tx.data.Extra[0].LockHeight)
}
return uint32(time.Now().Unix())
}
func (tx *Transaction) TxType() byte { return tx.data.TxEnterType }
func (tx *Transaction) IsEntrustTx() bool { return tx.data.IsEntrustTx == 1 }
//
func (tx *Transaction) GetMatrix_EX() []Matrix_Extra { return tx.data.Extra }
func (tx *Transaction) GetMatrixType() byte {
if tx.data.Extra != nil && len(tx.data.Extra) > 0 {
return tx.data.Extra[0].TxType
}
return common.ExtraNormalTxType
}
//Y 为了兼容去掉的Message结构体
func (tx *Transaction) From() common.Address {
addr, err := tx.GetTxFrom()
if err != nil {
return common.Address{}
}
return addr
}
func (tx *Transaction) GetFromLoad() interface{} {
return tx.from.Load()
}
func (tx *Transaction) SetFromLoad(x interface{}) {
from, ok := x.(common.Address)
if ok {
tx.from.Store(sigCache{signer: NewEIP155Signer(tx.ChainId()), from: from})
} else {
tx.from.Store(x)
}
}
func (tx *Transaction) Setentrustfrom(x interface{}) {
tx.entrustfrom.Store(x)
}
func (tx *Transaction) GasFrom() (from common.Address) {
tmp, ok := tx.from.Load().(sigCache)
if !ok {
tmpfrom, isok := tx.from.Load().(common.Address)
if !isok {
return common.Address{}
}
from = tmpfrom
} else {
from = tmp.from
}
return
}
func (tx *Transaction) AmontFrom() (from common.Address) {
//TODO from 要改为entrustfrom
tmp, ok := tx.entrustfrom.Load().(sigCache)
if !ok {
tmpfrom, isok := tx.entrustfrom.Load().(common.Address)
if !isok {
return tx.From()
}
from = tmpfrom
} else {
from = tmp.from
}
return
}
//
func (tx *Transaction) GetTxFrom() (from common.Address, err error) {
if tx.from.Load() == nil {
//如果交易没有做过验签则err不为空。
return common.Address{}, errors.New("Address is Nil")
}
var tf common.Address
//如果交易做过验签则err为空。
tmp, ok := tx.from.Load().(sigCache)
if !ok {
tmpfrom, isok := tx.from.Load().(common.Address)
if !isok {
return common.Address{}, errors.New("load Address is Nil")
}
if tmpfrom != tf {
from = tmpfrom
} else {
return common.Address{}, errors.New("load Address is Nil")
}
} else {
from = tmp.from
}
return
}
func (tx *Transaction) TotalAmount() *big.Int {
total := tx.data.Amount
for _, extra := range tx.data.Extra[0].ExtraTo {
total.Add(total, extra.Amount)
}
return total
}
//// Cost returns amount + gasprice * gaslimit.
func (tx *Transaction) CostALL() *big.Int {
total := new(big.Int).Mul(tx.data.Price, new(big.Int).SetUint64(tx.data.GasLimit))
total.Add(total, tx.data.Amount)
for _, extra := range tx.data.Extra[0].ExtraTo {
total.Add(total, extra.Amount)
}
return total
}
func (tx *Transaction) GetTxNLen() int {
return len(tx.N)
}
func (tx *Transaction) GetIsEntrustGas() bool {
return tx.IsEntrustGas
}
func (tx *Transaction) GetIsEntrustByTime() bool {
return tx.IsEntrustByTime
}
func (tx *Transaction) SetIsEntrustGas(b bool) {
tx.IsEntrustGas = b
}
func (tx *Transaction) SetIsEntrustByTime(b bool) {
tx.IsEntrustByTime = b
}
//
func (tx *Transaction) GetTxV() *big.Int { return tx.data.V }
func (tx *Transaction) SetTxV(v *big.Int) { tx.data.V = v }
func (tx *Transaction) SetTxR(r *big.Int) { tx.data.R = r }
//
func (tx *Transaction) GetTxS() *big.Int { return tx.data.S }
//
func (tx *Transaction) GetTxR() *big.Int { return tx.data.R }
func (tx *Transaction) GetTxN(index int) uint32 {
return tx.N[index]
}
// 在传递交易时用来操作Nonce
func (tx *Transaction) SetNonce(nc uint64) {
tx.data.AccountNonce = nc
}
//
func GetFloodData(tx *Transaction) *Floodtxdata {
floodtx := &Floodtxdata{
AccountNonce: tx.data.AccountNonce & params.NonceSubOne,
Price: tx.data.Price,
GasLimit: tx.data.GasLimit,
Recipient: tx.data.Recipient,
Amount: tx.data.Amount,
Payload: tx.data.Payload,
//Mtype: tx.Mtype, //
Currency: tx.Currency,
// Signature values
V: tx.data.V,
R: tx.data.R,
TxEnterType: tx.data.TxEnterType,
IsEntrustTx: tx.data.IsEntrustTx,
CommitTime: tx.data.CommitTime,
Extra: tx.data.Extra,
}
return floodtx
}
//
func SetFloodData(floodtx *Floodtxdata) *Transaction {
tx := &Transaction{}
tx.data.AccountNonce = floodtx.AccountNonce | params.NonceAddOne
tx.data.Price = floodtx.Price
tx.data.GasLimit = floodtx.GasLimit
tx.data.Recipient = floodtx.Recipient
tx.data.Amount = floodtx.Amount
tx.data.Payload = floodtx.Payload
// Signature values
tx.data.V = floodtx.V
tx.data.R = floodtx.R
tx.data.TxEnterType = floodtx.TxEnterType
tx.data.IsEntrustTx = floodtx.IsEntrustTx
tx.data.CommitTime = floodtx.CommitTime
tx.data.Extra = floodtx.Extra
//tx.Mtype = floodtx.Mtype //
tx.Currency = floodtx.Currency
return tx
}
func ConvTxtoMxtx(txer SelfTransaction) *Transaction_Mx {
tx, ok := txer.(*Transaction)
if !ok {
return nil
}
tx_Mx := &Transaction_Mx{}
tx_Mx.Data.AccountNonce = tx.data.AccountNonce & params.NonceSubOne
tx_Mx.Data.Price = tx.data.Price
tx_Mx.Data.GasLimit = tx.data.GasLimit
tx_Mx.Data.Recipient = tx.data.Recipient
tx_Mx.Data.Amount = tx.data.Amount
tx_Mx.Data.Payload = tx.data.Payload
// Signature values
tx_Mx.Data.V = tx.data.V
tx_Mx.Data.R = tx.data.R
tx_Mx.Data.S = tx.data.S
tx_Mx.Data.TxEnterType = tx.data.TxEnterType
tx_Mx.Data.IsEntrustTx = tx.data.IsEntrustTx
tx_Mx.Data.CommitTime = tx.data.CommitTime
tx_Mx.Data.Extra = tx.data.Extra
//tx_Mx.Mtype = tx.Mtype //
tx_Mx.Currency = tx.Currency
//tx_Mx.Data.Extra = append(tx_Mx.Data.Extra,tx.data.Extra[])
if len(tx.data.Extra) > 0 {
tx_Mx.TxType_Mx = tx.data.Extra[0].TxType
tx_Mx.LockHeight = tx.data.Extra[0].LockHeight
tx_Mx.ExtraTo = tx.data.Extra[0].ExtraTo
} else {
log.Error("tx.data.Extra is nil")
return nil
}
return tx_Mx
}
func ConvMxtotx(tx_Mx *Transaction_Mx) *Transaction {
txd := txdata{
AccountNonce: tx_Mx.Data.AccountNonce | params.NonceAddOne,
Price: tx_Mx.Data.Price,
GasLimit: tx_Mx.Data.GasLimit,
Recipient: tx_Mx.Data.Recipient,
Amount: tx_Mx.Data.Amount,
Payload: tx_Mx.Data.Payload,
// Signature values
V: tx_Mx.Data.V,
R: tx_Mx.Data.R,
S: tx_Mx.Data.S,
TxEnterType: tx_Mx.Data.TxEnterType,
IsEntrustTx: tx_Mx.Data.IsEntrustTx,
CommitTime: tx_Mx.Data.CommitTime,
Extra: tx_Mx.Data.Extra,
}
//mx := Matrix_Extra{
// TxType: tx_Mx.TxType_Mx,
// LockHeight: tx_Mx.LockHeight,
//}
//if len(tx_Mx.ExtraTo) > 0 {
// mx.ExtraTo = tx_Mx.ExtraTo
//}
//txd.Extra = append(txd.Extra, mx)
tx := &Transaction{Currency: tx_Mx.Currency, data: txd}
return tx
}
//
func (tx *Transaction) SetTxS(S *big.Int) { tx.data.S = S }
func (tx *Transaction) SetTxCurrency(currency string) {
tx.Currency = currency
}
func (tx *Transaction) GetTxCurrency() string {
return tx.Currency
}
//func (tx *Transaction) SetTxN(N uint32) {tx.data.N = N}
//func (tx *Transaction) GetTxN() uint32{return tx.data.N}
//func (tx *Transaction) GetTxIsFlood() bool{return tx.data.IsFlood}
// To returns the recipient address of the transaction.
// It returns nil if the transaction is a contract creation.
func (tx *Transaction) To() *common.Address {
//return tx.data.Recipient
if tx.data.Recipient == nil {
return nil
}
to := *tx.data.Recipient
return &to
}
// Hash hashes the RLP encoding of tx.
// It uniquely identifies the transaction.
func (tx *Transaction) Hash() common.Hash {
if hash := tx.hash.Load(); hash != nil {
return hash.(common.Hash)
}
if len(tx.data.Extra) != 1 {
panic("Transaction data Extra length must be 1")
}
v := rlpHash(tx)
tx.hash.Store(v)
return 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 *Transaction) 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)
}
// 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 *Transaction) WithSignature(signer Signer, sig []byte) (SelfTransaction, error) {
r, s, v, err := signer.SignatureValues(tx, sig)
if err != nil {
return nil, err
}
cpy := &Transaction{Currency:tx.Currency,data: tx.data}
cpy.data.R, cpy.data.S, cpy.data.V = r, s, v
////
//if len(cpy.data.Extra) > 0 {
// cpy.data.V.Add(cpy.data.V, big.NewInt(128))
//}
return cpy, nil
}
// Cost returns amount + gasprice * gaslimit.
func (tx *Transaction) Cost() *big.Int {
total := new(big.Int).Mul(tx.data.Price, new(big.Int).SetUint64(tx.data.GasLimit))
total.Add(total, tx.data.Amount)
return total
}
func (tx *Transaction) RawSignatureValues() (*big.Int, *big.Int, *big.Int) {
return tx.data.V, tx.data.R, tx.data.S
}
// Transactions is a Transaction slice type for basic sorting.
type SelfTransactions []SelfTransaction
// Len returns the length of s.
func (s SelfTransactions) Len() int { return len(s) }
// Swap swaps the i'th and the j'th element in s.
func (s SelfTransactions) 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 SelfTransactions) GetRlp(i int) []byte {
enc, _ := rlp.EncodeToBytes(s[i])
return enc
}
// TxDifference returns a new set t which is the difference between a to b.
func TxDifference(a, b SelfTransactions) (keep SelfTransactions) {
keep = make(SelfTransactions, 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
}
// TxByNonce implements the sort interface to allow sorting a list of transactions
// by their nonces. This is usually only useful for sorting transactions from a
// single account, otherwise a nonce comparison doesn't make much sense.
type TxByNonce []SelfTransaction
func (s TxByNonce) Len() int { return len(s) }
func (s TxByNonce) Less(i, j int) bool { return s[i].Nonce() < s[j].Nonce() }
func (s TxByNonce) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s *TxByNonce) Push(x interface{}) {
//*s = append(*s, x.(*Transaction))
}
func (s *TxByNonce) Pop() interface{} {
old := *s
n := len(old)
x := old[n-1]
*s = old[0 : n-1]
return x
}
// 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 SelfTransactions
func (s TxByPrice) Len() int { return len(s) }
func (s TxByPrice) Less(i, j int) bool { return s[i].GasPrice().Cmp(s[j].GasPrice()) > 0 }
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.(*Transaction))
}
func (s *TxByPrice) Pop() interface{} {
old := *s
n := len(old)
x := old[n-1]
*s = old[0 : n-1]
return x
}
// TransactionsByPriceAndNonce represents a set of transactions that can return
// transactions in a profit-maximizing sorted order, while supporting removing
// entire batches of transactions for non-executable accounts.
type TransactionsByPriceAndNonce struct {
txs map[common.Address]SelfTransactions // Per account nonce-sorted list of transactions
//heads TxByPrice // Next transaction for each unique account (price heap)
heads TxByNonce
signer Signer // Signer for the set of transactions
}
// NewTransactionsByPriceAndNonce creates a transaction set that can retrieve
// price sorted transactions in a nonce-honouring way.
//
// Note, the input map is reowned so the caller should not interact any more with
// if after providing it to the constructor.
func NewTransactionsByPriceAndNonce(signer Signer, txs map[common.Address]SelfTransactions) *TransactionsByPriceAndNonce {
// Initialize a price based heap with the head transactions
heads := make(TxByNonce, 0, len(txs))
for from, accTxs := range txs {
heads = append(heads, accTxs[0])
// Ensure the sender address is from the signer
acc, _ := Sender(signer, accTxs[0])
txs[acc] = accTxs[1:]
if from != acc {
delete(txs, from)
}
}
//heap.Init(&heads)
// Assemble and return the transaction set
return &TransactionsByPriceAndNonce{
txs: txs,
heads: heads,
signer: signer,
}
}
// Peek returns the next transaction by price.
func (t *TransactionsByPriceAndNonce) Peek() SelfTransaction {
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 *TransactionsByPriceAndNonce) Shift() {
acc, _ := Sender(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 *TransactionsByPriceAndNonce) Pop() {
heap.Pop(&t.heads)
}