-
Notifications
You must be signed in to change notification settings - Fork 10
/
transaction.go
299 lines (251 loc) · 7.63 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
package models
import (
"bytes"
"errors"
"fmt"
"math/big"
"github.com/onflow/cadence"
"github.com/onflow/flow-go/fvm/evm/events"
"github.com/onflow/flow-go/fvm/evm/types"
"github.com/onflow/go-ethereum/common"
"github.com/onflow/go-ethereum/core/txpool"
gethTypes "github.com/onflow/go-ethereum/core/types"
"github.com/onflow/go-ethereum/rlp"
)
const (
// TxSlotSize is used to calculate how many data slots a single transaction
// takes up based on its size. The slots are used as DoS protection, ensuring
// that validating a new transaction remains a constant operation (in reality
// O(maxslots), where max slots are 4 currently).
TxSlotSize = 32 * 1024
// TxMaxSize is the maximum size a single transaction can have. This field has
// non-trivial consequences: larger transactions are significantly harder and
// more expensive to propagate; larger transactions also take more resources
// to validate whether they fit into the pool or not.
TxMaxSize = 4 * TxSlotSize // 128KB
)
type Transaction interface {
Hash() common.Hash
RawSignatureValues() (v *big.Int, r *big.Int, s *big.Int)
From() (common.Address, error)
To() *common.Address
Data() []byte
Nonce() uint64
Value() *big.Int
Type() uint8
Gas() uint64
GasFeeCap() *big.Int
GasTipCap() *big.Int
GasPrice() *big.Int
BlobGas() uint64
BlobGasFeeCap() *big.Int
BlobHashes() []common.Hash
Size() uint64
AccessList() gethTypes.AccessList
MarshalBinary() ([]byte, error)
}
var _ Transaction = &DirectCall{}
type DirectCall struct {
*types.DirectCall
}
func (dc DirectCall) RawSignatureValues() (
v *big.Int,
r *big.Int,
s *big.Int,
) {
return dc.Transaction().RawSignatureValues()
}
func (dc DirectCall) From() (common.Address, error) {
return dc.DirectCall.From.ToCommon(), nil
}
func (dc DirectCall) To() *common.Address {
// for contract deployments, `to` should always be `nil`
if dc.SubType == types.DeployCallSubType {
return nil
}
var to *common.Address
if !dc.DirectCall.EmptyToField() {
ct := dc.DirectCall.To.ToCommon()
to = &ct
}
return to
}
func (dc DirectCall) Data() []byte {
return dc.DirectCall.Data
}
func (dc DirectCall) Nonce() uint64 {
return dc.DirectCall.Nonce
}
func (dc DirectCall) Value() *big.Int {
return dc.DirectCall.Value
}
func (dc DirectCall) Type() uint8 {
return dc.DirectCall.Transaction().Type()
}
func (dc DirectCall) Gas() uint64 {
return dc.DirectCall.GasLimit
}
func (dc DirectCall) GasFeeCap() *big.Int {
return big.NewInt(0)
}
func (dc DirectCall) GasTipCap() *big.Int {
return big.NewInt(0)
}
func (dc DirectCall) GasPrice() *big.Int {
return big.NewInt(0)
}
func (dc DirectCall) BlobGas() uint64 {
return 0
}
func (dc DirectCall) BlobGasFeeCap() *big.Int {
return big.NewInt(0)
}
func (dc DirectCall) BlobHashes() []common.Hash {
return []common.Hash{}
}
func (dc DirectCall) Size() uint64 {
encoded, err := dc.MarshalBinary()
if err != nil {
return 0
}
return rlp.ListSize(uint64(len(encoded)))
}
func (dc DirectCall) AccessList() gethTypes.AccessList {
return gethTypes.AccessList{}
}
func (dc DirectCall) MarshalBinary() ([]byte, error) {
return dc.DirectCall.Encode()
}
var _ Transaction = &TransactionCall{}
type TransactionCall struct {
*gethTypes.Transaction
}
func (tc TransactionCall) Hash() common.Hash {
return tc.Transaction.Hash()
}
func (tc TransactionCall) From() (common.Address, error) {
return gethTypes.Sender(
gethTypes.LatestSignerForChainID(tc.ChainId()),
tc.Transaction,
)
}
func (tc TransactionCall) MarshalBinary() ([]byte, error) {
encoded, err := tc.Transaction.MarshalBinary()
return append([]byte{tc.Type()}, encoded...), err
}
// decodeTransactionEvent takes a cadence event for transaction executed
// and decodes its payload into a Transaction interface and a Receipt.
// The concrete type will be either a TransactionCall or a DirectCall.
func decodeTransactionEvent(event cadence.Event) (
Transaction,
*Receipt,
*events.TransactionEventPayload,
error,
) {
txEvent, err := events.DecodeTransactionEventPayload(event)
if err != nil {
return nil, nil, nil, fmt.Errorf(
"failed to Cadence decode transaction event [%s]: %w",
event.String(),
err,
)
}
gethReceipt := &gethTypes.Receipt{
BlockNumber: big.NewInt(int64(txEvent.BlockHeight)),
Type: txEvent.TransactionType,
TxHash: txEvent.Hash,
ContractAddress: common.HexToAddress(txEvent.ContractAddress),
GasUsed: txEvent.GasConsumed,
TransactionIndex: uint(txEvent.Index),
EffectiveGasPrice: big.NewInt(0),
}
if len(txEvent.Logs) > 0 {
err = rlp.Decode(bytes.NewReader(txEvent.Logs), &gethReceipt.Logs)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to RLP-decode logs: %w", err)
}
}
if txEvent.ErrorCode == uint16(types.ErrCodeNoError) {
gethReceipt.Status = gethTypes.ReceiptStatusSuccessful
} else {
gethReceipt.Status = gethTypes.ReceiptStatusFailed
}
gethReceipt.Bloom = gethTypes.CreateBloom([]*gethTypes.Receipt{gethReceipt})
var revertReason []byte
if txEvent.ErrorCode == uint16(types.ExecutionErrCodeExecutionReverted) {
revertReason = txEvent.ReturnedData
}
receipt := NewReceipt(gethReceipt, revertReason, txEvent.PrecompiledCalls)
var tx Transaction
// check if the transaction payload is actually from a direct call,
// which is a special state transition in Flow EVM.
if txEvent.TransactionType == types.DirectCallTxType {
directCall, err := types.DirectCallFromEncoded(txEvent.Payload)
if err != nil {
return nil, nil, nil, fmt.Errorf(
"failed to RLP-decode direct call [%x]: %w",
txEvent.Payload,
err,
)
}
tx = DirectCall{DirectCall: directCall}
} else {
gethTx := &gethTypes.Transaction{}
if err := gethTx.UnmarshalBinary(txEvent.Payload); err != nil {
return nil, nil, nil, fmt.Errorf(
"failed to RLP-decode transaction [%x]: %w",
txEvent.Payload,
err,
)
}
receipt.EffectiveGasPrice = gethTx.EffectiveGasTipValue(nil)
tx = TransactionCall{Transaction: gethTx}
}
return tx, receipt, txEvent, nil
}
func UnmarshalTransaction(value []byte) (Transaction, error) {
if value[0] == types.DirectCallTxType {
directCall, err := types.DirectCallFromEncoded(value)
if err != nil {
return nil, fmt.Errorf("failed to RLP-decode direct call [%x]: %w", value, err)
}
return DirectCall{DirectCall: directCall}, nil
}
tx := &gethTypes.Transaction{}
if err := tx.UnmarshalBinary(value[1:]); err != nil {
return nil, fmt.Errorf("failed to RLP-decode transaction [%x]: %w", value, err)
}
return TransactionCall{Transaction: tx}, nil
}
func ValidateTransaction(
tx *gethTypes.Transaction,
head *gethTypes.Header,
signer gethTypes.Signer,
opts *txpool.ValidationOptions,
) error {
txDataLen := len(tx.Data())
// Contract creation doesn't validate call data, handle first
if tx.To() == nil {
// Contract creation should contain sufficient data to deploy a contract. A
// typical error is omitting sender due to some quirk in the javascript call
// e.g. https://github.com/onflow/go-ethereum/issues/16106.
if txDataLen == 0 {
// Prevent sending ether into black hole (show stopper)
if tx.Value().Cmp(big.NewInt(0)) > 0 {
return errors.New("transaction will create a contract with value but empty code")
}
// No value submitted at least, critically Warn, but don't blow up
return errors.New("transaction will create a contract with empty code")
}
}
// Not a contract creation, validate as a plain transaction
if tx.To() != nil {
if bytes.Equal(tx.To().Bytes(), common.Address{}.Bytes()) {
return errors.New("transaction recipient is the zero address")
}
}
if err := txpool.ValidateTransaction(tx, head, signer, opts); err != nil {
return err
}
return nil
}