forked from ethereum-optimism/op-geth
/
meta_transaction.go
209 lines (180 loc) · 5.28 KB
/
meta_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
package types
import (
"bytes"
"errors"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rlp"
)
const (
MetaTxPrefixLength = 32
OneHundredPercent = 100
)
var (
// ASCII code of "MantleMetaTxPrefix"
MetaTxPrefix, _ = hexutil.Decode("0x00000000000000000000000000004D616E746C654D6574615478507265666978")
ErrExpiredMetaTx = errors.New("expired meta transaction")
ErrInvalidGasFeeSponsorSig = errors.New("invalid gas fee sponsor signature")
ErrGasFeeSponsorMismatch = errors.New("gas fee sponsor address is mismatch with signature")
ErrInvalidSponsorPercent = errors.New("invalid sponsor percent, expected range [0, 100]")
ErrSponsorBalanceNotEnough = errors.New("sponsor doesn't have enough balance")
)
type MetaTxParams struct {
ExpireHeight uint64
SponsorPercent uint64
Payload []byte
// In tx simulation, Signature will be empty, user can specify GasFeeSponsor to sponsor gas fee
GasFeeSponsor common.Address
// Signature values
V *big.Int
R *big.Int
S *big.Int
}
type MetaTxParamsCache struct {
metaTxParams *MetaTxParams
}
type MetaTxSignData struct {
ChainID *big.Int
Nonce uint64
GasTipCap *big.Int
GasFeeCap *big.Int
Gas uint64
To *common.Address `rlp:"nil"`
Value *big.Int
Data []byte
AccessList AccessList
ExpireHeight uint64
SponsorPercent uint64
}
type MetaTxSignDataV2 struct {
From common.Address
ChainID *big.Int
Nonce uint64
GasTipCap *big.Int
GasFeeCap *big.Int
Gas uint64
To *common.Address `rlp:"nil"`
Value *big.Int
Data []byte
AccessList AccessList
ExpireHeight uint64
SponsorPercent uint64
}
func CalculateSponsorPercentAmount(mxParams *MetaTxParams, amount *big.Int) (*big.Int, *big.Int) {
if mxParams == nil {
return nil, nil
}
sponsorAmount := new(big.Int).Div(
new(big.Int).Mul(amount, big.NewInt(int64(mxParams.SponsorPercent))),
big.NewInt(OneHundredPercent))
selfAmount := new(big.Int).Sub(amount, sponsorAmount)
return sponsorAmount, selfAmount
}
func DecodeMetaTxParams(txData []byte) (*MetaTxParams, error) {
if len(txData) <= len(MetaTxPrefix) {
return nil, nil
}
if !bytes.Equal(txData[:MetaTxPrefixLength], MetaTxPrefix) {
return nil, nil
}
var metaTxParams MetaTxParams
err := rlp.DecodeBytes(txData[MetaTxPrefixLength:], &metaTxParams)
if err != nil {
return nil, err
}
if metaTxParams.SponsorPercent > OneHundredPercent {
return nil, ErrInvalidSponsorPercent
}
return &metaTxParams, nil
}
func DecodeAndVerifyMetaTxParams(tx *Transaction, isMetaTxUpgraded bool) (*MetaTxParams, error) {
if tx.Type() != DynamicFeeTxType {
return nil, nil
}
if mtp := tx.metaTxParams.Load(); mtp != nil {
mtpCache, ok := mtp.(*MetaTxParamsCache)
if ok {
return mtpCache.metaTxParams, nil
}
}
metaTxParams, err := DecodeMetaTxParams(tx.Data())
if err != nil {
return nil, err
}
// Not metaTx
if metaTxParams == nil {
tx.metaTxParams.Store(&MetaTxParamsCache{
metaTxParams: nil,
})
return nil, nil
}
if metaTxParams.SponsorPercent > OneHundredPercent {
return nil, ErrInvalidSponsorPercent
}
if err = checkSponsorSignature(tx, metaTxParams, isMetaTxUpgraded); err != nil {
return nil, err
}
tx.metaTxParams.Store(&MetaTxParamsCache{
metaTxParams: metaTxParams,
})
return metaTxParams, nil
}
func checkSponsorSignature(tx *Transaction, metaTxParams *MetaTxParams, isMetaTxUpgraded bool) error {
var (
txSender, gasFeeSponsorSigner common.Address
err error
)
txSender, err = Sender(LatestSignerForChainID(tx.ChainId()), tx)
if err != nil {
return err
}
if isMetaTxUpgraded {
metaTxSignData := &MetaTxSignDataV2{
From: txSender,
ChainID: tx.ChainId(),
Nonce: tx.Nonce(),
GasTipCap: tx.GasTipCap(),
GasFeeCap: tx.GasFeeCap(),
Gas: tx.Gas(),
To: tx.To(),
Value: tx.Value(),
Data: metaTxParams.Payload,
AccessList: tx.AccessList(),
ExpireHeight: metaTxParams.ExpireHeight,
SponsorPercent: metaTxParams.SponsorPercent,
}
gasFeeSponsorSigner, err = recoverPlain(metaTxSignData.Hash(), metaTxParams.R, metaTxParams.S, metaTxParams.V, true)
if err != nil {
return ErrInvalidGasFeeSponsorSig
}
} else {
metaTxSignData := &MetaTxSignData{
ChainID: tx.ChainId(),
Nonce: tx.Nonce(),
GasTipCap: tx.GasTipCap(),
GasFeeCap: tx.GasFeeCap(),
Gas: tx.Gas(),
To: tx.To(),
Value: tx.Value(),
Data: metaTxParams.Payload,
AccessList: tx.AccessList(),
ExpireHeight: metaTxParams.ExpireHeight,
SponsorPercent: metaTxParams.SponsorPercent,
}
gasFeeSponsorSigner, err = recoverPlain(metaTxSignData.Hash(), metaTxParams.R, metaTxParams.S, metaTxParams.V, true)
if err != nil {
return ErrInvalidGasFeeSponsorSig
}
}
if gasFeeSponsorSigner != metaTxParams.GasFeeSponsor {
return ErrGasFeeSponsorMismatch
}
return nil
}
func (metaTxSignData *MetaTxSignData) Hash() common.Hash {
return rlpHash(metaTxSignData)
}
func (metaTxSignData *MetaTxSignDataV2) Hash() common.Hash {
return rlpHash(metaTxSignData)
}