Skip to content

Commit a26c920

Browse files
fjlgballet
authored andcommitted
core/types: updates for EIP-7702 API functions (ethereum#30933)
Here I am proposing two small changes to the exported API for EIP-7702: (1) `Authorization` has a very generic name, but it is in fact only used for one niche use case: authorizing code in a `SetCodeTx`. So I propose calling it `SetCodeAuthorization` instead. The signing function is renamed to `SignSetCode` instead of `SignAuth`. (2) The signing function for authorizations should take key as the first parameter, and the authorization second. The key will almost always be in a variable, while the authorization can be given as a literal.
1 parent 996acd3 commit a26c920

File tree

10 files changed

+95
-101
lines changed

10 files changed

+95
-101
lines changed

core/blockchain_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4273,16 +4273,16 @@ func TestEIP7702(t *testing.T) {
42734273
// 1. tx -> addr1 which is delegated to 0xaaaa
42744274
// 2. addr1:0xaaaa calls into addr2:0xbbbb
42754275
// 3. addr2:0xbbbb writes to storage
4276-
auth1, _ := types.SignAuth(types.Authorization{
4276+
auth1, _ := types.SignSetCode(key1, types.SetCodeAuthorization{
42774277
ChainID: gspec.Config.ChainID.Uint64(),
42784278
Address: aa,
42794279
Nonce: 1,
4280-
}, key1)
4281-
auth2, _ := types.SignAuth(types.Authorization{
4280+
})
4281+
auth2, _ := types.SignSetCode(key2, types.SetCodeAuthorization{
42824282
ChainID: 0,
42834283
Address: bb,
42844284
Nonce: 0,
4285-
}, key2)
4285+
})
42864286

42874287
_, blocks, _ := GenerateChainWithGenesis(gspec, engine, 1, func(i int, b *BlockGen) {
42884288
b.SetCoinbase(aa)
@@ -4293,7 +4293,7 @@ func TestEIP7702(t *testing.T) {
42934293
Gas: 500000,
42944294
GasFeeCap: uint256.MustFromBig(newGwei(5)),
42954295
GasTipCap: uint256.NewInt(2),
4296-
AuthList: []types.Authorization{auth1, auth2},
4296+
AuthList: []types.SetCodeAuthorization{auth1, auth2},
42974297
}
42984298
tx := types.MustSignNewTx(key1, signer, txdata)
42994299
b.AddTx(tx)

core/state_processor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func TestStateProcessorErrors(t *testing.T) {
111111
}
112112
return tx
113113
}
114-
var mkSetCodeTx = func(nonce uint64, to common.Address, gasLimit uint64, gasTipCap, gasFeeCap *big.Int, authlist []types.Authorization) *types.Transaction {
114+
var mkSetCodeTx = func(nonce uint64, to common.Address, gasLimit uint64, gasTipCap, gasFeeCap *big.Int, authlist []types.SetCodeAuthorization) *types.Transaction {
115115
tx, err := types.SignTx(types.NewTx(&types.SetCodeTx{
116116
Nonce: nonce,
117117
GasTipCap: uint256.MustFromBig(gasTipCap),

core/state_transition.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (result *ExecutionResult) Revert() []byte {
6767
}
6868

6969
// IntrinsicGas computes the 'intrinsic gas' for a message with the given data.
70-
func IntrinsicGas(data []byte, accessList types.AccessList, authList []types.Authorization, isContractCreation, isHomestead, isEIP2028, isEIP3860 bool) (uint64, error) {
70+
func IntrinsicGas(data []byte, accessList types.AccessList, authList []types.SetCodeAuthorization, isContractCreation, isHomestead, isEIP2028, isEIP3860 bool) (uint64, error) {
7171
// Set the starting gas for the raw transaction
7272
var gas uint64
7373
if isContractCreation && isHomestead {
@@ -143,7 +143,7 @@ type Message struct {
143143
AccessList types.AccessList
144144
BlobGasFeeCap *big.Int
145145
BlobHashes []common.Hash
146-
AuthList []types.Authorization
146+
AuthList []types.SetCodeAuthorization
147147

148148
// When SkipNonceChecks is true, the message nonce is not checked against the
149149
// account nonce in state.
@@ -528,7 +528,7 @@ func (st *stateTransition) execute() (*ExecutionResult, error) {
528528
}
529529

530530
// validateAuthorization validates an EIP-7702 authorization against the state.
531-
func (st *stateTransition) validateAuthorization(auth *types.Authorization) (authority common.Address, err error) {
531+
func (st *stateTransition) validateAuthorization(auth *types.SetCodeAuthorization) (authority common.Address, err error) {
532532
// Verify chain ID is 0 or equal to current chain ID.
533533
if auth.ChainID != 0 && st.evm.ChainConfig().ChainID.Uint64() != auth.ChainID {
534534
return authority, ErrAuthorizationWrongChainID
@@ -559,7 +559,7 @@ func (st *stateTransition) validateAuthorization(auth *types.Authorization) (aut
559559
}
560560

561561
// applyAuthorization applies an EIP-7702 code delegation to the state.
562-
func (st *stateTransition) applyAuthorization(msg *Message, auth *types.Authorization) error {
562+
func (st *stateTransition) applyAuthorization(msg *Message, auth *types.SetCodeAuthorization) error {
563563
authority, err := st.validateAuthorization(auth)
564564
if err != nil {
565565
return err

core/types/gen_authorization.go

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

core/types/transaction.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ func (tx *Transaction) WithBlobTxSidecar(sideCar *BlobTxSidecar) *Transaction {
475475
}
476476

477477
// AuthList returns the authorizations list of the transaction.
478-
func (tx *Transaction) AuthList() []Authorization {
478+
func (tx *Transaction) AuthList() []SetCodeAuthorization {
479479
setcodetx, ok := tx.inner.(*SetCodeTx)
480480
if !ok {
481481
return nil

core/types/transaction_marshalling.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,23 @@ import (
3131
type txJSON struct {
3232
Type hexutil.Uint64 `json:"type"`
3333

34-
ChainID *hexutil.Big `json:"chainId,omitempty"`
35-
Nonce *hexutil.Uint64 `json:"nonce"`
36-
To *common.Address `json:"to"`
37-
Gas *hexutil.Uint64 `json:"gas"`
38-
GasPrice *hexutil.Big `json:"gasPrice"`
39-
MaxPriorityFeePerGas *hexutil.Big `json:"maxPriorityFeePerGas"`
40-
MaxFeePerGas *hexutil.Big `json:"maxFeePerGas"`
41-
MaxFeePerBlobGas *hexutil.Big `json:"maxFeePerBlobGas,omitempty"`
42-
Value *hexutil.Big `json:"value"`
43-
Input *hexutil.Bytes `json:"input"`
44-
AccessList *AccessList `json:"accessList,omitempty"`
45-
BlobVersionedHashes []common.Hash `json:"blobVersionedHashes,omitempty"`
46-
AuthorizationList []Authorization `json:"authorizationList,omitempty"`
47-
V *hexutil.Big `json:"v"`
48-
R *hexutil.Big `json:"r"`
49-
S *hexutil.Big `json:"s"`
50-
YParity *hexutil.Uint64 `json:"yParity,omitempty"`
34+
ChainID *hexutil.Big `json:"chainId,omitempty"`
35+
Nonce *hexutil.Uint64 `json:"nonce"`
36+
To *common.Address `json:"to"`
37+
Gas *hexutil.Uint64 `json:"gas"`
38+
GasPrice *hexutil.Big `json:"gasPrice"`
39+
MaxPriorityFeePerGas *hexutil.Big `json:"maxPriorityFeePerGas"`
40+
MaxFeePerGas *hexutil.Big `json:"maxFeePerGas"`
41+
MaxFeePerBlobGas *hexutil.Big `json:"maxFeePerBlobGas,omitempty"`
42+
Value *hexutil.Big `json:"value"`
43+
Input *hexutil.Bytes `json:"input"`
44+
AccessList *AccessList `json:"accessList,omitempty"`
45+
BlobVersionedHashes []common.Hash `json:"blobVersionedHashes,omitempty"`
46+
AuthorizationList []SetCodeAuthorization `json:"authorizationList,omitempty"`
47+
V *hexutil.Big `json:"v"`
48+
R *hexutil.Big `json:"r"`
49+
S *hexutil.Big `json:"s"`
50+
YParity *hexutil.Uint64 `json:"yParity,omitempty"`
5151

5252
// Blob transaction sidecar encoding:
5353
Blobs []kzg4844.Blob `json:"blobs,omitempty"`

core/types/tx_setcode.go

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,18 @@ type SetCodeTx struct {
5858
Value *uint256.Int
5959
Data []byte
6060
AccessList AccessList
61-
AuthList []Authorization
61+
AuthList []SetCodeAuthorization
6262

6363
// Signature values
6464
V *uint256.Int `json:"v" gencodec:"required"`
6565
R *uint256.Int `json:"r" gencodec:"required"`
6666
S *uint256.Int `json:"s" gencodec:"required"`
6767
}
6868

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

71-
// Authorization is an authorization from an account to deploy code at its address.
72-
type Authorization struct {
71+
// SetCodeAuthorization is an authorization from an account to deploy code at its address.
72+
type SetCodeAuthorization struct {
7373
ChainID uint64 `json:"chainId" gencodec:"required"`
7474
Address common.Address `json:"address" gencodec:"required"`
7575
Nonce uint64 `json:"nonce" gencodec:"required"`
@@ -87,31 +87,25 @@ type authorizationMarshaling struct {
8787
S hexutil.U256
8888
}
8989

90-
// SignAuth signs the provided authorization.
91-
func SignAuth(auth Authorization, prv *ecdsa.PrivateKey) (Authorization, error) {
90+
// SignSetCode creates a signed the SetCode authorization.
91+
func SignSetCode(prv *ecdsa.PrivateKey, auth SetCodeAuthorization) (SetCodeAuthorization, error) {
9292
sighash := auth.sigHash()
9393
sig, err := crypto.Sign(sighash[:], prv)
9494
if err != nil {
95-
return Authorization{}, err
95+
return SetCodeAuthorization{}, err
9696
}
97-
return auth.withSignature(sig), nil
98-
}
99-
100-
// withSignature updates the signature of an Authorization to be equal the
101-
// decoded signature provided in sig.
102-
func (a *Authorization) withSignature(sig []byte) Authorization {
10397
r, s, _ := decodeSignature(sig)
104-
return Authorization{
105-
ChainID: a.ChainID,
106-
Address: a.Address,
107-
Nonce: a.Nonce,
98+
return SetCodeAuthorization{
99+
ChainID: auth.ChainID,
100+
Address: auth.Address,
101+
Nonce: auth.Nonce,
108102
V: sig[64],
109103
R: *uint256.MustFromBig(r),
110104
S: *uint256.MustFromBig(s),
111-
}
105+
}, nil
112106
}
113107

114-
func (a *Authorization) sigHash() common.Hash {
108+
func (a *SetCodeAuthorization) sigHash() common.Hash {
115109
return prefixedRlpHash(0x05, []any{
116110
a.ChainID,
117111
a.Address,
@@ -120,7 +114,7 @@ func (a *Authorization) sigHash() common.Hash {
120114
}
121115

122116
// Authority recovers the the authorizing account of an authorization.
123-
func (a *Authorization) Authority() (common.Address, error) {
117+
func (a *SetCodeAuthorization) Authority() (common.Address, error) {
124118
sighash := a.sigHash()
125119
if !crypto.ValidateSignatureValues(a.V, a.R.ToBig(), a.S.ToBig(), true) {
126120
return common.Address{}, ErrInvalidSig
@@ -152,7 +146,7 @@ func (tx *SetCodeTx) copy() TxData {
152146
Gas: tx.Gas,
153147
// These are copied below.
154148
AccessList: make(AccessList, len(tx.AccessList)),
155-
AuthList: make([]Authorization, len(tx.AuthList)),
149+
AuthList: make([]SetCodeAuthorization, len(tx.AuthList)),
156150
Value: new(uint256.Int),
157151
ChainID: tx.ChainID,
158152
GasTipCap: new(uint256.Int),

internal/ethapi/api.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -937,29 +937,29 @@ func RPCMarshalBlock(block *types.Block, inclTx bool, fullTx bool, config *param
937937

938938
// RPCTransaction represents a transaction that will serialize to the RPC representation of a transaction
939939
type RPCTransaction struct {
940-
BlockHash *common.Hash `json:"blockHash"`
941-
BlockNumber *hexutil.Big `json:"blockNumber"`
942-
From common.Address `json:"from"`
943-
Gas hexutil.Uint64 `json:"gas"`
944-
GasPrice *hexutil.Big `json:"gasPrice"`
945-
GasFeeCap *hexutil.Big `json:"maxFeePerGas,omitempty"`
946-
GasTipCap *hexutil.Big `json:"maxPriorityFeePerGas,omitempty"`
947-
MaxFeePerBlobGas *hexutil.Big `json:"maxFeePerBlobGas,omitempty"`
948-
Hash common.Hash `json:"hash"`
949-
Input hexutil.Bytes `json:"input"`
950-
Nonce hexutil.Uint64 `json:"nonce"`
951-
To *common.Address `json:"to"`
952-
TransactionIndex *hexutil.Uint64 `json:"transactionIndex"`
953-
Value *hexutil.Big `json:"value"`
954-
Type hexutil.Uint64 `json:"type"`
955-
Accesses *types.AccessList `json:"accessList,omitempty"`
956-
ChainID *hexutil.Big `json:"chainId,omitempty"`
957-
BlobVersionedHashes []common.Hash `json:"blobVersionedHashes,omitempty"`
958-
AuthorizationList []types.Authorization `json:"authorizationList,omitempty"`
959-
V *hexutil.Big `json:"v"`
960-
R *hexutil.Big `json:"r"`
961-
S *hexutil.Big `json:"s"`
962-
YParity *hexutil.Uint64 `json:"yParity,omitempty"`
940+
BlockHash *common.Hash `json:"blockHash"`
941+
BlockNumber *hexutil.Big `json:"blockNumber"`
942+
From common.Address `json:"from"`
943+
Gas hexutil.Uint64 `json:"gas"`
944+
GasPrice *hexutil.Big `json:"gasPrice"`
945+
GasFeeCap *hexutil.Big `json:"maxFeePerGas,omitempty"`
946+
GasTipCap *hexutil.Big `json:"maxPriorityFeePerGas,omitempty"`
947+
MaxFeePerBlobGas *hexutil.Big `json:"maxFeePerBlobGas,omitempty"`
948+
Hash common.Hash `json:"hash"`
949+
Input hexutil.Bytes `json:"input"`
950+
Nonce hexutil.Uint64 `json:"nonce"`
951+
To *common.Address `json:"to"`
952+
TransactionIndex *hexutil.Uint64 `json:"transactionIndex"`
953+
Value *hexutil.Big `json:"value"`
954+
Type hexutil.Uint64 `json:"type"`
955+
Accesses *types.AccessList `json:"accessList,omitempty"`
956+
ChainID *hexutil.Big `json:"chainId,omitempty"`
957+
BlobVersionedHashes []common.Hash `json:"blobVersionedHashes,omitempty"`
958+
AuthorizationList []types.SetCodeAuthorization `json:"authorizationList,omitempty"`
959+
V *hexutil.Big `json:"v"`
960+
R *hexutil.Big `json:"r"`
961+
S *hexutil.Big `json:"s"`
962+
YParity *hexutil.Uint64 `json:"yParity,omitempty"`
963963
}
964964

965965
// newRPCTransaction returns a transaction that will serialize to the RPC

internal/ethapi/transaction_args.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ type TransactionArgs struct {
7373
Proofs []kzg4844.Proof `json:"proofs"`
7474

7575
// For SetCodeTxType
76-
AuthorizationList []types.Authorization `json:"authorizationList"`
76+
AuthorizationList []types.SetCodeAuthorization `json:"authorizationList"`
7777

7878
// This configures whether blobs are allowed to be passed.
7979
blobSidecarAllowed bool
@@ -497,7 +497,7 @@ func (args *TransactionArgs) ToTransaction(defaultType int) *types.Transaction {
497497
if args.AccessList != nil {
498498
al = *args.AccessList
499499
}
500-
authList := []types.Authorization{}
500+
authList := []types.SetCodeAuthorization{}
501501
if args.AuthorizationList != nil {
502502
authList = args.AuthorizationList
503503
}

tests/state_test_util.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,11 @@ func (tx *stTransaction) toMessage(ps stPostState, baseFee *big.Int) (*core.Mess
441441
if gasPrice == nil {
442442
return nil, errors.New("no gas price provided")
443443
}
444-
var authList []types.Authorization
444+
var authList []types.SetCodeAuthorization
445445
if tx.AuthorizationList != nil {
446-
authList = make([]types.Authorization, len(tx.AuthorizationList))
446+
authList = make([]types.SetCodeAuthorization, len(tx.AuthorizationList))
447447
for i, auth := range tx.AuthorizationList {
448-
authList[i] = types.Authorization{
448+
authList[i] = types.SetCodeAuthorization{
449449
ChainID: auth.ChainID,
450450
Address: auth.Address,
451451
Nonce: auth.Nonce,

0 commit comments

Comments
 (0)