Skip to content

Commit 28af9f0

Browse files
trantienduchnfjlislishude
authored
core/types, test: correct chainId 7702 (#47)
* pick up ethereum/go-ethereum#30982 Co-authored-by: Felix Lange <[email protected]> * tests: synchronize with go-ethereum, run code gen Co-authored-by: Felix Lange <[email protected]> * core/types: correct chainId check for pragueSigner (#31032) Use zero value check for the pragueSigner This aligns with cancunSigner and londonSigner as well. * core/types: initialize ChainID in SetCodeTx copy method (#31054) --------- Co-authored-by: Felix Lange <[email protected]> Co-authored-by: Shude Li <[email protected]>
1 parent c4cb036 commit 28af9f0

11 files changed

+67
-51
lines changed

core/blockchain_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4781,20 +4781,19 @@ func TestEIP7702(t *testing.T) {
47814781
// 2. addr1:0xaaaa calls into addr2:0xbbbb
47824782
// 3. addr2:0xbbbb writes to storage
47834783
auth1, _ := types.SignSetCode(key1, types.SetCodeAuthorization{
4784-
ChainID: gspec.Config.ChainID.Uint64(),
4784+
ChainID: *uint256.MustFromBig(gspec.Config.ChainID),
47854785
Address: aa,
47864786
Nonce: 1,
47874787
})
47884788
auth2, _ := types.SignSetCode(key2, types.SetCodeAuthorization{
4789-
ChainID: 0,
47904789
Address: bb,
47914790
Nonce: 0,
47924791
})
47934792

47944793
_, blocks, _ := GenerateChainWithGenesis(gspec, engine, 1, func(i int, b *BlockGen) {
47954794
b.SetCoinbase(aa)
47964795
txdata := &types.SetCodeTx{
4797-
ChainID: gspec.Config.ChainID.Uint64(),
4796+
ChainID: uint256.MustFromBig(gspec.Config.ChainID),
47984797
Nonce: 0,
47994798
To: addr1,
48004799
Gas: 500000,

core/state_transition.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,8 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
557557

558558
// validateAuthorization validates an EIP-7702 authorization against the state.
559559
func (st *StateTransition) validateAuthorization(auth *types.SetCodeAuthorization) (authority common.Address, err error) {
560-
// Verify chain ID is 0 or equal to current chain ID.
561-
if auth.ChainID != 0 && st.evm.ChainConfig().ChainID.Uint64() != auth.ChainID {
560+
// Verify chain ID is null or equal to current chain ID.
561+
if !auth.ChainID.IsZero() && auth.ChainID.CmpBig(st.evm.ChainConfig().ChainID) != 0 {
562562
return authority, ErrAuthorizationWrongChainID
563563
}
564564
// Limit nonce to 2^64-1 per EIP-2681.

core/types/blob_tx.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ type BlobTx struct {
4747
Sidecar *BlobTxSidecar `rlp:"-"`
4848

4949
// Signature values
50-
V *uint256.Int `json:"v" gencodec:"required"`
51-
R *uint256.Int `json:"r" gencodec:"required"`
52-
S *uint256.Int `json:"s" gencodec:"required"`
50+
V *uint256.Int
51+
R *uint256.Int
52+
S *uint256.Int
5353
}
5454

5555
// BlobTxSidecar contains the blobs of a blob transaction.

core/types/dynamic_fee_tx.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ type DynamicFeeTx struct {
3737
AccessList AccessList
3838

3939
// Signature values
40-
V *big.Int `json:"v" gencodec:"required"`
41-
R *big.Int `json:"r" gencodec:"required"`
42-
S *big.Int `json:"s" gencodec:"required"`
40+
V *big.Int
41+
R *big.Int
42+
S *big.Int
4343
}
4444

4545
// copy creates a deep copy of the transaction data and initializes all fields.

core/types/gen_authorization.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/types/setcode_tx.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func AddressToDelegation(addr common.Address) []byte {
4949
// SetCodeTx implements the EIP-7702 transaction type which temporarily installs
5050
// the code at the signer's address.
5151
type SetCodeTx struct {
52-
ChainID uint64
52+
ChainID *uint256.Int
5353
Nonce uint64
5454
GasTipCap *uint256.Int // a.k.a. maxPriorityFeePerGas
5555
GasFeeCap *uint256.Int // a.k.a. maxFeePerGas
@@ -61,16 +61,16 @@ type SetCodeTx struct {
6161
AuthList []SetCodeAuthorization
6262

6363
// Signature values
64-
V *uint256.Int `json:"v" gencodec:"required"`
65-
R *uint256.Int `json:"r" gencodec:"required"`
66-
S *uint256.Int `json:"s" gencodec:"required"`
64+
V *uint256.Int
65+
R *uint256.Int
66+
S *uint256.Int
6767
}
6868

6969
//go:generate go run github.com/fjl/gencodec -type SetCodeAuthorization -field-override authorizationMarshaling -out gen_authorization.go
7070

7171
// SetCodeAuthorization is an authorization from an account to deploy code at its address.
7272
type SetCodeAuthorization struct {
73-
ChainID uint64 `json:"chainId" gencodec:"required"`
73+
ChainID uint256.Int `json:"chainId" gencodec:"required"`
7474
Address common.Address `json:"address" gencodec:"required"`
7575
Nonce uint64 `json:"nonce" gencodec:"required"`
7676
V uint8 `json:"yParity" gencodec:"required"`
@@ -80,7 +80,7 @@ type SetCodeAuthorization struct {
8080

8181
// field type overrides for gencodec
8282
type authorizationMarshaling struct {
83-
ChainID hexutil.Uint64
83+
ChainID hexutil.U256
8484
Nonce hexutil.Uint64
8585
V hexutil.Uint64
8686
R hexutil.U256
@@ -148,7 +148,7 @@ func (tx *SetCodeTx) copy() TxData {
148148
AccessList: make(AccessList, len(tx.AccessList)),
149149
AuthList: make([]SetCodeAuthorization, len(tx.AuthList)),
150150
Value: new(uint256.Int),
151-
ChainID: tx.ChainID,
151+
ChainID: new(uint256.Int),
152152
GasTipCap: new(uint256.Int),
153153
GasFeeCap: new(uint256.Int),
154154
V: new(uint256.Int),
@@ -160,6 +160,9 @@ func (tx *SetCodeTx) copy() TxData {
160160
if tx.Value != nil {
161161
cpy.Value.Set(tx.Value)
162162
}
163+
if tx.ChainID != nil {
164+
cpy.ChainID.Set(tx.ChainID)
165+
}
163166
if tx.GasTipCap != nil {
164167
cpy.GasTipCap.Set(tx.GasTipCap)
165168
}
@@ -180,7 +183,7 @@ func (tx *SetCodeTx) copy() TxData {
180183

181184
// accessors for innerTx.
182185
func (tx *SetCodeTx) txType() byte { return SetCodeTxType }
183-
func (tx *SetCodeTx) chainID() *big.Int { return big.NewInt(int64(tx.ChainID)) }
186+
func (tx *SetCodeTx) chainID() *big.Int { return tx.ChainID.ToBig() }
184187
func (tx *SetCodeTx) accessList() AccessList { return tx.AccessList }
185188
func (tx *SetCodeTx) data() []byte { return tx.Data }
186189
func (tx *SetCodeTx) gas() uint64 { return tx.Gas }
@@ -212,7 +215,7 @@ func (tx *SetCodeTx) rawSignatureValues() (v, r, s *big.Int) {
212215
}
213216

214217
func (tx *SetCodeTx) setSignatureValues(chainID, v, r, s *big.Int) {
215-
tx.ChainID = chainID.Uint64()
218+
tx.ChainID = uint256.MustFromBig(chainID)
216219
tx.V.SetFromBig(v)
217220
tx.R.SetFromBig(r)
218221
tx.S.SetFromBig(s)

core/types/transaction_marshalling.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (t *Transaction) MarshalJSON() ([]byte, error) {
140140
enc.Proofs = tx.Sidecar.Proofs
141141
}
142142
case *SetCodeTx:
143-
enc.ChainID = (*hexutil.Big)(new(big.Int).SetUint64(tx.ChainID))
143+
enc.ChainID = (*hexutil.Big)(tx.ChainID.ToBig())
144144
enc.MaxFeePerGas = (*hexutil.Big)(tx.GasFeeCap.ToBig())
145145
enc.MaxPriorityFeePerGas = (*hexutil.Big)(tx.GasTipCap.ToBig())
146146
enc.AccessList = &tx.AccessList
@@ -334,7 +334,11 @@ func (t *Transaction) UnmarshalJSON(input []byte) error {
334334
if dec.ChainID == nil {
335335
return errors.New("missing required field 'chainId' in transaction")
336336
}
337-
itx.ChainID = uint256.MustFromBig((*big.Int)(dec.ChainID))
337+
var overflow bool
338+
itx.ChainID, overflow = uint256.FromBig(dec.ChainID.ToInt())
339+
if overflow {
340+
return errors.New("'chainId' value overflows uint256")
341+
}
338342
if dec.MaxPriorityFeePerGas == nil {
339343
return errors.New("missing required field 'maxPriorityFeePerGas' for txdata")
340344
}
@@ -360,7 +364,6 @@ func (t *Transaction) UnmarshalJSON(input []byte) error {
360364
itx.AccessList = *dec.AccessList
361365
}
362366
// signature R
363-
var overflow bool
364367
itx.R, overflow = uint256.FromBig(r)
365368
if overflow {
366369
return errors.New("'r' value overflows uint256")
@@ -385,7 +388,11 @@ func (t *Transaction) UnmarshalJSON(input []byte) error {
385388
if dec.ChainID == nil {
386389
return errors.New("missing required field 'chainId' in transaction")
387390
}
388-
itx.ChainID = dec.ChainID.ToInt().Uint64()
391+
var overflow bool
392+
itx.ChainID, overflow = uint256.FromBig(dec.ChainID.ToInt())
393+
if overflow {
394+
return errors.New("'chainId' value overflows uint256")
395+
}
389396
if dec.To == nil {
390397
return errors.New("missing required field 'to' in transaction")
391398
}
@@ -407,7 +414,6 @@ func (t *Transaction) UnmarshalJSON(input []byte) error {
407414
itx.AuthList = dec.AuthorizationList
408415

409416
// signature R
410-
var overflow bool
411417
itx.R, overflow = uint256.FromBig(r)
412418
if overflow {
413419
return errors.New("'r' value overflows uint256")

core/types/transaction_signing.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func (s pragueSigner) SignatureValues(tx *Transaction, sig []byte) (R, S, V *big
285285
}
286286
// Check that chain ID of tx matches the signer. We also accept ID zero here,
287287
// because it indicates that the chain ID was not specified in the tx.
288-
if txdata.ChainID != 0 && new(big.Int).SetUint64(txdata.ChainID).Cmp(s.chainId) != 0 {
288+
if txdata.ChainID.Sign() != 0 && txdata.ChainID.CmpBig(s.chainId) != 0 {
289289
return nil, nil, nil, fmt.Errorf("%w: have %d want %d", ErrInvalidChainId, txdata.ChainID, s.chainId)
290290
}
291291
R, S, _ = decodeSignature(sig)
@@ -358,7 +358,7 @@ func (s cancunSigner) SignatureValues(tx *Transaction, sig []byte) (R, S, V *big
358358
}
359359
// Check that chain ID of tx matches the signer. We also accept ID zero here,
360360
// because it indicates that the chain ID was not specified in the tx.
361-
if txdata.ChainID.Sign() != 0 && txdata.ChainID.ToBig().Cmp(s.chainId) != 0 {
361+
if txdata.ChainID.Sign() != 0 && txdata.ChainID.CmpBig(s.chainId) != 0 {
362362
return nil, nil, nil, fmt.Errorf("%w: have %d want %d", ErrInvalidChainId, txdata.ChainID, s.chainId)
363363
}
364364
R, S, _ = decodeSignature(sig)

internal/ethapi/transaction_args.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ func (args *TransactionArgs) toTransaction() *types.Transaction {
316316
}
317317
data = &types.SetCodeTx{
318318
To: *args.To,
319-
ChainID: args.ChainID.ToInt().Uint64(),
319+
ChainID: uint256.MustFromBig(args.ChainID.ToInt()),
320320
Nonce: uint64(*args.Nonce),
321321
Gas: uint64(*args.Gas),
322322
GasFeeCap: uint256.MustFromBig((*big.Int)(args.MaxFeePerGas)),

tests/gen_stenv.go

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)