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)
}
// 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 {
return prefixedRlpHash(
AccessListTxType,
[]any{
chainID,
tx.Nonce,
tx.GasPrice,
tx.Gas,
tx.To,
tx.Value,
tx.Data,
tx.AccessList,
&accessListTxSigHash{
ChainID: chainID,
Nonce: tx.Nonce,
GasPrice: tx.GasPrice,
Gas: tx.Gas,
To: tx.To,
Value: tx.Value,
Data: tx.Data,
AccessList: tx.AccessList,
})
}

View file

@ -421,20 +421,36 @@ func (tx *BlobTx) decode(input []byte) error {
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 {
return prefixedRlpHash(
BlobTxType,
[]any{
chainID,
tx.Nonce,
tx.GasTipCap,
tx.GasFeeCap,
tx.Gas,
tx.To,
tx.Value,
tx.Data,
tx.AccessList,
tx.BlobFeeCap,
tx.BlobHashes,
&blobTxSigHash{
ChainID: chainID,
Nonce: tx.Nonce,
GasTipCap: tx.GasTipCap,
GasFeeCap: tx.GasFeeCap,
Gas: tx.Gas,
To: tx.To,
Value: tx.Value,
Data: tx.Data,
AccessList: tx.AccessList,
BlobFeeCap: tx.BlobFeeCap,
BlobHashes: tx.BlobHashes,
})
}

View file

@ -124,18 +124,32 @@ func (tx *DynamicFeeTx) decode(input []byte) error {
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 {
return prefixedRlpHash(
DynamicFeeTxType,
[]any{
chainID,
tx.Nonce,
tx.GasTipCap,
tx.GasFeeCap,
tx.Gas,
tx.To,
tx.Value,
tx.Data,
tx.AccessList,
&dynamicFeeTxSigHash{
ChainID: chainID,
Nonce: tx.Nonce,
GasTipCap: tx.GasTipCap,
GasFeeCap: tx.GasFeeCap,
Gas: tx.Gas,
To: tx.To,
Value: tx.Value,
Data: tx.Data,
AccessList: tx.AccessList,
})
}

View file

@ -124,15 +124,31 @@ func (tx *LegacyTx) decode([]byte) error {
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.
func (tx *LegacyTx) sigHash(chainID *big.Int) common.Hash {
return rlpHash([]any{
tx.Nonce,
tx.GasPrice,
tx.Gas,
tx.To,
tx.Value,
tx.Data,
chainID, uint(0), uint(0),
return rlpHash(&legacyTxSigHash{
Nonce: tx.Nonce,
GasPrice: tx.GasPrice,
Gas: tx.Gas,
To: tx.To,
Value: tx.Value,
Data: tx.Data,
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)
}
// 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 {
return prefixedRlpHash(
SetCodeTxType,
[]any{
chainID,
tx.Nonce,
tx.GasTipCap,
tx.GasFeeCap,
tx.Gas,
tx.To,
tx.Value,
tx.Data,
tx.AccessList,
tx.AuthList,
&setCodeTxSigHash{
ChainID: chainID,
Nonce: tx.Nonce,
GasTipCap: tx.GasTipCap,
GasFeeCap: tx.GasFeeCap,
Gas: tx.Gas,
To: tx.To,
Value: tx.Value,
Data: tx.Data,
AccessList: tx.AccessList,
AuthList: tx.AuthList,
})
}