core/types: using defined struct to optimize sigHash

This commit is contained in:
cuiweixie 2026-01-08 19:52:54 +08:00
parent eaaa5b716d
commit edd08d1383
5 changed files with 124 additions and 50 deletions

View file

@ -128,17 +128,30 @@ func (tx *AccessListTx) decode(input []byte) error {
return rlp.DecodeBytes(input, tx) return rlp.DecodeBytes(input, tx)
} }
// accessListTxSigHash represents the fields used for computing the signature hash
// of an AccessListTx transaction.
type accessListTxSigHash struct {
ChainID *big.Int
Nonce uint64
GasPrice *big.Int
Gas uint64
To *common.Address `rlp:"nil"`
Value *big.Int
Data []byte
AccessList AccessList
}
func (tx *AccessListTx) sigHash(chainID *big.Int) common.Hash { func (tx *AccessListTx) sigHash(chainID *big.Int) common.Hash {
return prefixedRlpHash( return prefixedRlpHash(
AccessListTxType, AccessListTxType,
[]any{ &accessListTxSigHash{
chainID, ChainID: chainID,
tx.Nonce, Nonce: tx.Nonce,
tx.GasPrice, GasPrice: tx.GasPrice,
tx.Gas, Gas: tx.Gas,
tx.To, To: tx.To,
tx.Value, Value: tx.Value,
tx.Data, Data: tx.Data,
tx.AccessList, AccessList: tx.AccessList,
}) })
} }

View file

@ -421,20 +421,36 @@ func (tx *BlobTx) decode(input []byte) error {
return nil return nil
} }
// blobTxSigHash represents the fields used for computing the signature hash
// of a BlobTx transaction.
type blobTxSigHash struct {
ChainID *big.Int
Nonce uint64
GasTipCap *uint256.Int
GasFeeCap *uint256.Int
Gas uint64
To common.Address
Value *uint256.Int
Data []byte
AccessList AccessList
BlobFeeCap *uint256.Int
BlobHashes []common.Hash
}
func (tx *BlobTx) sigHash(chainID *big.Int) common.Hash { func (tx *BlobTx) sigHash(chainID *big.Int) common.Hash {
return prefixedRlpHash( return prefixedRlpHash(
BlobTxType, BlobTxType,
[]any{ &blobTxSigHash{
chainID, ChainID: chainID,
tx.Nonce, Nonce: tx.Nonce,
tx.GasTipCap, GasTipCap: tx.GasTipCap,
tx.GasFeeCap, GasFeeCap: tx.GasFeeCap,
tx.Gas, Gas: tx.Gas,
tx.To, To: tx.To,
tx.Value, Value: tx.Value,
tx.Data, Data: tx.Data,
tx.AccessList, AccessList: tx.AccessList,
tx.BlobFeeCap, BlobFeeCap: tx.BlobFeeCap,
tx.BlobHashes, BlobHashes: tx.BlobHashes,
}) })
} }

View file

@ -124,18 +124,32 @@ func (tx *DynamicFeeTx) decode(input []byte) error {
return rlp.DecodeBytes(input, tx) return rlp.DecodeBytes(input, tx)
} }
// dynamicFeeTxSigHash represents the fields used for computing the signature hash
// of a DynamicFeeTx transaction.
type dynamicFeeTxSigHash 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
}
func (tx *DynamicFeeTx) sigHash(chainID *big.Int) common.Hash { func (tx *DynamicFeeTx) sigHash(chainID *big.Int) common.Hash {
return prefixedRlpHash( return prefixedRlpHash(
DynamicFeeTxType, DynamicFeeTxType,
[]any{ &dynamicFeeTxSigHash{
chainID, ChainID: chainID,
tx.Nonce, Nonce: tx.Nonce,
tx.GasTipCap, GasTipCap: tx.GasTipCap,
tx.GasFeeCap, GasFeeCap: tx.GasFeeCap,
tx.Gas, Gas: tx.Gas,
tx.To, To: tx.To,
tx.Value, Value: tx.Value,
tx.Data, Data: tx.Data,
tx.AccessList, AccessList: tx.AccessList,
}) })
} }

View file

@ -124,15 +124,31 @@ func (tx *LegacyTx) decode([]byte) error {
panic("decode called on LegacyTx)") panic("decode called on LegacyTx)")
} }
// legacyTxSigHash represents the fields used for computing the signature hash
// of a LegacyTx transaction (post-EIP155).
type legacyTxSigHash struct {
Nonce uint64
GasPrice *big.Int
Gas uint64
To *common.Address `rlp:"nil"`
Value *big.Int
Data []byte
ChainID *big.Int
V uint
R uint
}
// OBS: This is the post-EIP155 hash, the pre-EIP155 does not contain a chainID. // OBS: This is the post-EIP155 hash, the pre-EIP155 does not contain a chainID.
func (tx *LegacyTx) sigHash(chainID *big.Int) common.Hash { func (tx *LegacyTx) sigHash(chainID *big.Int) common.Hash {
return rlpHash([]any{ return rlpHash(&legacyTxSigHash{
tx.Nonce, Nonce: tx.Nonce,
tx.GasPrice, GasPrice: tx.GasPrice,
tx.Gas, Gas: tx.Gas,
tx.To, To: tx.To,
tx.Value, Value: tx.Value,
tx.Data, Data: tx.Data,
chainID, uint(0), uint(0), ChainID: chainID,
V: uint(0),
R: uint(0),
}) })
} }

View file

@ -225,19 +225,34 @@ func (tx *SetCodeTx) decode(input []byte) error {
return rlp.DecodeBytes(input, tx) return rlp.DecodeBytes(input, tx)
} }
// setCodeTxSigHash represents the fields used for computing the signature hash
// of a SetCodeTx transaction.
type setCodeTxSigHash struct {
ChainID *big.Int
Nonce uint64
GasTipCap *uint256.Int
GasFeeCap *uint256.Int
Gas uint64
To common.Address
Value *uint256.Int
Data []byte
AccessList AccessList
AuthList []SetCodeAuthorization
}
func (tx *SetCodeTx) sigHash(chainID *big.Int) common.Hash { func (tx *SetCodeTx) sigHash(chainID *big.Int) common.Hash {
return prefixedRlpHash( return prefixedRlpHash(
SetCodeTxType, SetCodeTxType,
[]any{ &setCodeTxSigHash{
chainID, ChainID: chainID,
tx.Nonce, Nonce: tx.Nonce,
tx.GasTipCap, GasTipCap: tx.GasTipCap,
tx.GasFeeCap, GasFeeCap: tx.GasFeeCap,
tx.Gas, Gas: tx.Gas,
tx.To, To: tx.To,
tx.Value, Value: tx.Value,
tx.Data, Data: tx.Data,
tx.AccessList, AccessList: tx.AccessList,
tx.AuthList, AuthList: tx.AuthList,
}) })
} }