-
Notifications
You must be signed in to change notification settings - Fork 180
/
tx_data.go
177 lines (144 loc) · 3.82 KB
/
tx_data.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
package types
import (
"fmt"
"math/big"
"github.com/okex/exchain/app/utils"
ethcmn "github.com/ethereum/go-ethereum/common"
)
// TxData implements the Ethereum transaction data structure. It is used
// solely as intended in Ethereum abiding by the protocol.
type TxData struct {
AccountNonce uint64 `json:"nonce"`
Price *big.Int `json:"gasPrice"`
GasLimit uint64 `json:"gas"`
Recipient *ethcmn.Address `json:"to" rlp:"nil"` // nil means contract creation
Amount *big.Int `json:"value"`
Payload []byte `json:"input"`
// signature values
V *big.Int `json:"v"`
R *big.Int `json:"r"`
S *big.Int `json:"s"`
// hash is only used when marshaling to JSON
Hash *ethcmn.Hash `json:"hash" rlp:"-"`
}
// encodableTxData implements the Ethereum transaction data structure. It is used
// solely as intended in Ethereum abiding by the protocol.
type encodableTxData struct {
AccountNonce uint64 `json:"nonce"`
Price string `json:"gasPrice"`
GasLimit uint64 `json:"gas"`
Recipient *ethcmn.Address `json:"to" rlp:"nil"` // nil means contract creation
Amount string `json:"value"`
Payload []byte `json:"input"`
// signature values
V string `json:"v"`
R string `json:"r"`
S string `json:"s"`
// hash is only used when marshaling to JSON
Hash *ethcmn.Hash `json:"hash" rlp:"-"`
}
func (td TxData) String() string {
if td.Recipient != nil {
return fmt.Sprintf("nonce=%d price=%s gasLimit=%d recipient=%s amount=%s data=0x%x v=%s r=%s s=%s",
td.AccountNonce, td.Price, td.GasLimit, td.Recipient.Hex(), td.Amount, td.Payload, td.V, td.R, td.S)
}
return fmt.Sprintf("nonce=%d price=%s gasLimit=%d recipient=nil amount=%s data=0x%x v=%s r=%s s=%s",
td.AccountNonce, td.Price, td.GasLimit, td.Amount, td.Payload, td.V, td.R, td.S)
}
// MarshalAmino defines custom encoding scheme for TxData
func (td TxData) MarshalAmino() ([]byte, error) {
gasPrice, err := utils.MarshalBigInt(td.Price)
if err != nil {
return nil, err
}
amount, err := utils.MarshalBigInt(td.Amount)
if err != nil {
return nil, err
}
v, err := utils.MarshalBigInt(td.V)
if err != nil {
return nil, err
}
r, err := utils.MarshalBigInt(td.R)
if err != nil {
return nil, err
}
s, err := utils.MarshalBigInt(td.S)
if err != nil {
return nil, err
}
e := encodableTxData{
AccountNonce: td.AccountNonce,
Price: gasPrice,
GasLimit: td.GasLimit,
Recipient: td.Recipient,
Amount: amount,
Payload: td.Payload,
V: v,
R: r,
S: s,
Hash: td.Hash,
}
return ModuleCdc.MarshalBinaryBare(e)
}
// UnmarshalAmino defines custom decoding scheme for TxData
func (td *TxData) UnmarshalAmino(data []byte) error {
var e encodableTxData
err := ModuleCdc.UnmarshalBinaryBare(data, &e)
if err != nil {
return err
}
td.AccountNonce = e.AccountNonce
td.GasLimit = e.GasLimit
td.Recipient = e.Recipient
td.Payload = e.Payload
td.Hash = e.Hash
price, err := utils.UnmarshalBigInt(e.Price)
if err != nil {
return err
}
if td.Price != nil {
td.Price.Set(price)
} else {
td.Price = price
}
amt, err := utils.UnmarshalBigInt(e.Amount)
if err != nil {
return err
}
if td.Amount != nil {
td.Amount.Set(amt)
} else {
td.Amount = amt
}
v, err := utils.UnmarshalBigInt(e.V)
if err != nil {
return err
}
if td.V != nil {
td.V.Set(v)
} else {
td.V = v
}
r, err := utils.UnmarshalBigInt(e.R)
if err != nil {
return err
}
if td.R != nil {
td.R.Set(r)
} else {
td.R = r
}
s, err := utils.UnmarshalBigInt(e.S)
if err != nil {
return err
}
if td.S != nil {
td.S.Set(s)
} else {
td.S = s
}
return nil
}
// TODO: Implement JSON marshaling/ unmarshaling for this type
// TODO: Implement YAML marshaling/ unmarshaling for this type