-
Notifications
You must be signed in to change notification settings - Fork 1
/
transaction.go
105 lines (81 loc) · 1.86 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
package types
import (
"math/big"
"sync/atomic"
"github.com/JResearchLabs/Flutechain/helper/keccak"
)
type Transaction struct {
Nonce uint64
GasPrice *big.Int
Gas uint64
To *Address
Value *big.Int
Input []byte
V *big.Int
R *big.Int
S *big.Int
Hash Hash
From Address
// Cache
size atomic.Value
}
func (t *Transaction) IsContractCreation() bool {
return t.To == nil
}
// ComputeHash computes the hash of the transaction
func (t *Transaction) ComputeHash() *Transaction {
ar := marshalArenaPool.Get()
hash := keccak.DefaultKeccakPool.Get()
v := t.MarshalRLPWith(ar)
hash.WriteRlp(t.Hash[:0], v)
marshalArenaPool.Put(ar)
keccak.DefaultKeccakPool.Put(hash)
return t
}
func (t *Transaction) Copy() *Transaction {
tt := new(Transaction)
*tt = *t
tt.GasPrice = new(big.Int)
if t.GasPrice != nil {
tt.GasPrice.Set(t.GasPrice)
}
tt.Value = new(big.Int)
if t.Value != nil {
tt.Value.Set(t.Value)
}
if t.R != nil {
tt.R = new(big.Int)
tt.R = big.NewInt(0).SetBits(t.R.Bits())
}
if t.S != nil {
tt.S = new(big.Int)
tt.S = big.NewInt(0).SetBits(t.S.Bits())
}
tt.Input = make([]byte, len(t.Input))
copy(tt.Input[:], t.Input[:])
return tt
}
// Cost returns gas * gasPrice + value
func (t *Transaction) Cost() *big.Int {
total := new(big.Int).Mul(t.GasPrice, new(big.Int).SetUint64(t.Gas))
total.Add(total, t.Value)
return total
}
func (t *Transaction) Size() uint64 {
if size := t.size.Load(); size != nil {
sizeVal, ok := size.(uint64)
if !ok {
return 0
}
return sizeVal
}
size := uint64(len(t.MarshalRLP()))
t.size.Store(size)
return size
}
func (t *Transaction) ExceedsBlockGasLimit(blockGasLimit uint64) bool {
return t.Gas > blockGasLimit
}
func (t *Transaction) IsUnderpriced(priceLimit uint64) bool {
return t.GasPrice.Cmp(big.NewInt(0).SetUint64(priceLimit)) < 0
}