forked from neo-ngd/neo-go
/
transaction.go
329 lines (297 loc) · 6.09 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
package transaction
import (
"encoding/json"
"errors"
"math"
"math/big"
"sync/atomic"
"github.com/DigitalLabs-web3/neo-go-evm/pkg/io"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
const (
EthTxType = byte(0)
NeoTxType = byte(1)
SignatureLength = 64
MaxScriptLength = math.MaxUint16
MaxTransactionSize = 102400
EthLegacyBaseLength = 100
)
var (
ErrUnsupportType = errors.New("unsupport tx type")
)
type Transaction struct {
Type byte
EthTx *EthTx
NeoTx *NeoTx
Trimmed bool
hash atomic.Value
size atomic.Value
}
func NewTrimmedTX(hash common.Hash) *Transaction {
t := &Transaction{
Trimmed: true,
}
t.hash.Store(hash)
return t
}
func NewTx(t interface{}) *Transaction {
tx := &Transaction{}
switch v := t.(type) {
case *NeoTx:
tx.Type = NeoTxType
tx.NeoTx = v
case *EthTx:
tx.Type = EthTxType
tx.EthTx = v
default:
panic("unsupport tx")
}
return tx
}
func NewTransactionFromBytes(b []byte) (*Transaction, error) {
tx := &Transaction{}
err := io.FromByteArray(tx, b)
if err != nil {
return nil, err
}
return tx, err
}
func (t *Transaction) Nonce() uint64 {
switch t.Type {
case EthTxType:
return t.EthTx.Nonce()
case NeoTxType:
return t.NeoTx.Nonce
default:
panic(ErrUnsupportType)
}
}
func (t *Transaction) To() *common.Address {
switch t.Type {
case EthTxType:
return t.EthTx.To()
case NeoTxType:
return t.NeoTx.To
default:
panic(ErrUnsupportType)
}
}
func (t *Transaction) Gas() uint64 {
switch t.Type {
case EthTxType:
return t.EthTx.Gas()
case NeoTxType:
return t.NeoTx.Gas
default:
panic(ErrUnsupportType)
}
}
func (t *Transaction) GasPrice() *big.Int {
switch t.Type {
case EthTxType:
return t.EthTx.GasPrice()
case NeoTxType:
return t.NeoTx.GasPrice
default:
panic(ErrUnsupportType)
}
}
func (t Transaction) Cost() *big.Int {
cost := big.NewInt(0).Mul(big.NewInt(int64(t.Gas())), t.GasPrice())
return big.NewInt(0).Add(t.Value(), cost)
}
func (t *Transaction) Value() *big.Int {
switch t.Type {
case EthTxType:
return t.EthTx.Value()
case NeoTxType:
return t.NeoTx.Value
default:
panic(ErrUnsupportType)
}
}
func (t *Transaction) Data() []byte {
switch t.Type {
case EthTxType:
return t.EthTx.Data()
case NeoTxType:
return t.NeoTx.Data
default:
panic(ErrUnsupportType)
}
}
func (t *Transaction) Size() int {
if size := t.size.Load(); size != nil {
return size.(int)
}
var size int
switch t.Type {
case EthTxType:
size = int(t.EthTx.Size())
case NeoTxType:
size = t.NeoTx.Size()
default:
panic(ErrUnsupportType)
}
t.size.Store(size)
return size
}
func (t *Transaction) From() common.Address {
switch t.Type {
case EthTxType:
return t.EthTx.Sender
case NeoTxType:
return t.NeoTx.From
default:
panic(ErrUnsupportType)
}
}
func (t *Transaction) AccessList() types.AccessList {
switch t.Type {
case EthTxType:
return t.EthTx.AccessList()
default:
return nil
}
}
func (t *Transaction) Hash() common.Hash {
if hash := t.hash.Load(); hash != nil {
return hash.(common.Hash)
}
var h common.Hash
if t.Type == EthTxType {
h = t.EthTx.Hash()
} else {
h = t.NeoTx.Hash()
}
t.hash.Store(h)
return h
}
func (t Transaction) SignHash(chainId uint64) common.Hash {
if t.Type == EthTxType {
signer := types.NewEIP155Signer(big.NewInt(int64(chainId)))
return signer.Hash(&t.EthTx.Transaction)
} else {
return t.Hash()
}
}
func (t *Transaction) Bytes() ([]byte, error) {
return io.ToByteArray(t)
}
func (t Transaction) FeePerByte() uint64 {
return t.Gas() / uint64(t.Size())
}
func (t *Transaction) EncodeBinary(w *io.BinWriter) {
w.WriteB(t.Type)
switch t.Type {
case EthTxType:
t.EthTx.EncodeBinary(w)
case NeoTxType:
t.NeoTx.EncodeBinary(w)
default:
w.Err = ErrUnsupportType
}
}
func (t *Transaction) DecodeBinary(r *io.BinReader) {
t.Type = r.ReadB()
switch t.Type {
case EthTxType:
inner := new(EthTx)
inner.DecodeBinary(r)
t.EthTx = inner
case NeoTxType:
inner := new(NeoTx)
inner.DecodeBinary(r)
t.NeoTx = inner
default:
r.Err = ErrUnsupportType
}
}
func (t *Transaction) Verify(chainId uint64) error {
switch t.Type {
case EthTxType:
return t.EthTx.Verify(chainId)
case NeoTxType:
if t.NeoTx.From != t.NeoTx.Witness.Address() {
return ErrWitnessUnmatch
}
return t.NeoTx.Witness.VerifyHashable(chainId, t.NeoTx)
default:
return ErrUnsupportType
}
}
func (t *Transaction) WithSignature(chainId uint64, sig []byte) error {
switch t.Type {
case EthTxType:
return t.EthTx.WithSignature(chainId, sig)
default:
return ErrUnsupportType
}
}
func (t *Transaction) WithWitness(witness Witness) error {
if t.Type != NeoTxType {
return ErrUnsupportType
}
t.NeoTx.Witness = witness
return nil
}
func (t *Transaction) UnmarshalJSON(b []byte) error {
tmp := make(map[string]interface{})
err := json.Unmarshal(b, &tmp)
if err != nil {
return err
}
if _, ok := tmp["witness"]; ok {
tx := new(NeoTx)
err = json.Unmarshal(b, tx)
if err != nil {
return err
}
t.Type = NeoTxType
t.NeoTx = tx
return nil
}
if _, ok := tmp["type"]; ok {
tx := new(EthTx)
err = json.Unmarshal(b, tx)
if err != nil {
return err
}
t.Type = EthTxType
t.EthTx = tx
return nil
}
return ErrUnsupportType
}
func (t Transaction) MarshalJSON() ([]byte, error) {
if t.Trimmed {
return json.Marshal(t.Hash())
}
switch t.Type {
case EthTxType:
return json.Marshal(t.EthTx)
case NeoTxType:
return json.Marshal(t.NeoTx)
default:
return nil, ErrUnsupportType
}
}
var (
ErrInvalidTxType = errors.New("invalid tx type")
ErrTipVeryHigh = errors.New("max priority fee per gas higher than 2^256-1")
ErrFeeCapVeryHigh = errors.New("max fee per gas higher than 2^256-1")
ErrTipAboveFeeCap = errors.New("max priority fee per gas higher than max fee per gas")
ErrValueVeryHigh = errors.New("value higher than 2^256-1")
ErrGasPriceVeryHigh = errors.New("gas price higher than 2^256-1")
)
func (t Transaction) IsValid() error {
switch t.Type {
case EthTxType:
return t.EthTx.IsValid()
case NeoTxType:
return t.NeoTx.isValid()
default:
return ErrInvalidTxType
}
}