Skip to content

Commit 035e89e

Browse files
fjljakub-freebit
authored andcommitted
core/types: change SetCodeTx.ChainID to uint256 (ethereum#30982)
We still need to decide how to handle non-specfic `chainId` in the JSON encoding of authorizations. With `chainId` being a uint64, the previous implementation just used value zero. However, it might actually be more correct to use the value `null` for this case.
1 parent 27e74f0 commit 035e89e

11 files changed

+44
-38
lines changed

core/blockchain_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4265,20 +4265,19 @@ func TestEIP7702(t *testing.T) {
42654265
// 2. addr1:0xaaaa calls into addr2:0xbbbb
42664266
// 3. addr2:0xbbbb writes to storage
42674267
auth1, _ := types.SignSetCode(key1, types.SetCodeAuthorization{
4268-
ChainID: gspec.Config.ChainID.Uint64(),
4268+
ChainID: *uint256.MustFromBig(gspec.Config.ChainID),
42694269
Address: aa,
42704270
Nonce: 1,
42714271
})
42724272
auth2, _ := types.SignSetCode(key2, types.SetCodeAuthorization{
4273-
ChainID: 0,
42744273
Address: bb,
42754274
Nonce: 0,
42764275
})
42774276

42784277
_, blocks, _ := GenerateChainWithGenesis(gspec, engine, 1, func(i int, b *BlockGen) {
42794278
b.SetCoinbase(aa)
42804279
txdata := &types.SetCodeTx{
4281-
ChainID: gspec.Config.ChainID.Uint64(),
4280+
ChainID: uint256.MustFromBig(gspec.Config.ChainID),
42824281
Nonce: 0,
42834282
To: addr1,
42844283
Gas: 500000,

core/state_transition.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,8 +529,8 @@ func (st *stateTransition) execute() (*ExecutionResult, error) {
529529

530530
// validateAuthorization validates an EIP-7702 authorization against the state.
531531
func (st *stateTransition) validateAuthorization(auth *types.SetCodeAuthorization) (authority common.Address, err error) {
532-
// Verify chain ID is 0 or equal to current chain ID.
533-
if auth.ChainID != 0 && st.evm.ChainConfig().ChainID.Uint64() != auth.ChainID {
532+
// Verify chain ID is null or equal to current chain ID.
533+
if !auth.ChainID.IsZero() && auth.ChainID.CmpBig(st.evm.ChainConfig().ChainID) != 0 {
534534
return authority, ErrAuthorizationWrongChainID
535535
}
536536
// Limit nonce to 2^64-1 per EIP-2681.

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/transaction_marshalling.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func (tx *Transaction) MarshalJSON() ([]byte, error) {
155155
enc.Proofs = itx.Sidecar.Proofs
156156
}
157157
case *SetCodeTx:
158-
enc.ChainID = (*hexutil.Big)(new(big.Int).SetUint64(itx.ChainID))
158+
enc.ChainID = (*hexutil.Big)(itx.ChainID.ToBig())
159159
enc.Nonce = (*hexutil.Uint64)(&itx.Nonce)
160160
enc.To = tx.To()
161161
enc.Gas = (*hexutil.Uint64)(&itx.Gas)
@@ -353,7 +353,11 @@ func (tx *Transaction) UnmarshalJSON(input []byte) error {
353353
if dec.ChainID == nil {
354354
return errors.New("missing required field 'chainId' in transaction")
355355
}
356-
itx.ChainID = uint256.MustFromBig((*big.Int)(dec.ChainID))
356+
var overflow bool
357+
itx.ChainID, overflow = uint256.FromBig(dec.ChainID.ToInt())
358+
if overflow {
359+
return errors.New("'chainId' value overflows uint256")
360+
}
357361
if dec.Nonce == nil {
358362
return errors.New("missing required field 'nonce' in transaction")
359363
}
@@ -395,7 +399,6 @@ func (tx *Transaction) UnmarshalJSON(input []byte) error {
395399
itx.BlobHashes = dec.BlobVersionedHashes
396400

397401
// signature R
398-
var overflow bool
399402
if dec.R == nil {
400403
return errors.New("missing required field 'r' in transaction")
401404
}
@@ -432,7 +435,11 @@ func (tx *Transaction) UnmarshalJSON(input []byte) error {
432435
if dec.ChainID == nil {
433436
return errors.New("missing required field 'chainId' in transaction")
434437
}
435-
itx.ChainID = dec.ChainID.ToInt().Uint64()
438+
var overflow bool
439+
itx.ChainID, overflow = uint256.FromBig(dec.ChainID.ToInt())
440+
if overflow {
441+
return errors.New("'chainId' value overflows uint256")
442+
}
436443
if dec.Nonce == nil {
437444
return errors.New("missing required field 'nonce' in transaction")
438445
}
@@ -470,7 +477,6 @@ func (tx *Transaction) UnmarshalJSON(input []byte) error {
470477
itx.AuthList = dec.AuthorizationList
471478

472479
// signature R
473-
var overflow bool
474480
if dec.R == nil {
475481
return errors.New("missing required field 'r' in transaction")
476482
}

core/types/transaction_signing.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func (s pragueSigner) SignatureValues(tx *Transaction, sig []byte) (R, S, V *big
219219
}
220220
// Check that chain ID of tx matches the signer. We also accept ID zero here,
221221
// because it indicates that the chain ID was not specified in the tx.
222-
if txdata.ChainID != 0 && new(big.Int).SetUint64(txdata.ChainID).Cmp(s.chainId) != 0 {
222+
if txdata.ChainID != nil && txdata.ChainID.CmpBig(s.chainId) != 0 {
223223
return nil, nil, nil, fmt.Errorf("%w: have %d want %d", ErrInvalidChainId, txdata.ChainID, s.chainId)
224224
}
225225
R, S, _ = decodeSignature(sig)

core/types/tx_blob.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/tx_dynamic_fee.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/tx_setcode.go

Lines changed: 8 additions & 8 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
@@ -180,7 +180,7 @@ func (tx *SetCodeTx) copy() TxData {
180180

181181
// accessors for innerTx.
182182
func (tx *SetCodeTx) txType() byte { return SetCodeTxType }
183-
func (tx *SetCodeTx) chainID() *big.Int { return big.NewInt(int64(tx.ChainID)) }
183+
func (tx *SetCodeTx) chainID() *big.Int { return tx.ChainID.ToBig() }
184184
func (tx *SetCodeTx) accessList() AccessList { return tx.AccessList }
185185
func (tx *SetCodeTx) data() []byte { return tx.Data }
186186
func (tx *SetCodeTx) gas() uint64 { return tx.Gas }
@@ -207,7 +207,7 @@ func (tx *SetCodeTx) rawSignatureValues() (v, r, s *big.Int) {
207207
}
208208

209209
func (tx *SetCodeTx) setSignatureValues(chainID, v, r, s *big.Int) {
210-
tx.ChainID = chainID.Uint64()
210+
tx.ChainID = uint256.MustFromBig(chainID)
211211
tx.V.SetFromBig(v)
212212
tx.R.SetFromBig(r)
213213
tx.S.SetFromBig(s)

internal/ethapi/transaction_args.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ func (args *TransactionArgs) ToTransaction(defaultType int) *types.Transaction {
503503
}
504504
data = &types.SetCodeTx{
505505
To: *args.To,
506-
ChainID: args.ChainID.ToInt().Uint64(),
506+
ChainID: uint256.MustFromBig(args.ChainID.ToInt()),
507507
Nonce: uint64(*args.Nonce),
508508
Gas: uint64(*args.Gas),
509509
GasFeeCap: uint256.MustFromBig((*big.Int)(args.MaxFeePerGas)),

tests/gen_stauthorization.go

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

0 commit comments

Comments
 (0)