mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-22 20:56:42 +00:00
core/types: optimize modernSigner
This commit is contained in:
parent
cfa3b96103
commit
22e5dcea0c
2 changed files with 30 additions and 13 deletions
|
|
@ -52,6 +52,26 @@ const (
|
||||||
SetCodeTxType = 0x04
|
SetCodeTxType = 0x04
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type TxTypeBitVec [(SetCodeTxType + 1 + 7) / 8]byte
|
||||||
|
|
||||||
|
func (v *TxTypeBitVec) Set(txType byte) {
|
||||||
|
if txType >= byte(len(v)*8) {
|
||||||
|
panic("tx type out of range")
|
||||||
|
}
|
||||||
|
v[txType/8] |= 1 << (txType % 8)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *TxTypeBitVec) Has(txType byte) bool {
|
||||||
|
if txType >= byte(len(v)*8) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return v[txType/8]&(1<<(txType%8)) != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *TxTypeBitVec) Equals(o *TxTypeBitVec) bool {
|
||||||
|
return *v == *o
|
||||||
|
}
|
||||||
|
|
||||||
// Transaction is an Ethereum transaction.
|
// Transaction is an Ethereum transaction.
|
||||||
type Transaction struct {
|
type Transaction struct {
|
||||||
inner TxData // Consensus contents of a transaction
|
inner TxData // Consensus contents of a transaction
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,6 @@ import (
|
||||||
"crypto/ecdsa"
|
"crypto/ecdsa"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"maps"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
|
@ -183,9 +182,9 @@ type Signer interface {
|
||||||
// modernSigner is the signer implementation that handles non-legacy transaction types.
|
// modernSigner is the signer implementation that handles non-legacy transaction types.
|
||||||
// For legacy transactions, it defers to one of the legacy signers (frontier, homestead, eip155).
|
// For legacy transactions, it defers to one of the legacy signers (frontier, homestead, eip155).
|
||||||
type modernSigner struct {
|
type modernSigner struct {
|
||||||
txtypes map[byte]struct{}
|
txTypeBitVec TxTypeBitVec
|
||||||
chainID *big.Int
|
chainID *big.Int
|
||||||
legacy Signer
|
legacy Signer
|
||||||
}
|
}
|
||||||
|
|
||||||
func newModernSigner(chainID *big.Int, fork forks.Fork) Signer {
|
func newModernSigner(chainID *big.Int, fork forks.Fork) Signer {
|
||||||
|
|
@ -194,7 +193,6 @@ func newModernSigner(chainID *big.Int, fork forks.Fork) Signer {
|
||||||
}
|
}
|
||||||
s := &modernSigner{
|
s := &modernSigner{
|
||||||
chainID: chainID,
|
chainID: chainID,
|
||||||
txtypes: make(map[byte]struct{}, 4),
|
|
||||||
}
|
}
|
||||||
// configure legacy signer
|
// configure legacy signer
|
||||||
switch {
|
switch {
|
||||||
|
|
@ -205,19 +203,19 @@ func newModernSigner(chainID *big.Int, fork forks.Fork) Signer {
|
||||||
default:
|
default:
|
||||||
s.legacy = FrontierSigner{}
|
s.legacy = FrontierSigner{}
|
||||||
}
|
}
|
||||||
s.txtypes[LegacyTxType] = struct{}{}
|
s.txTypeBitVec.Set(LegacyTxType)
|
||||||
// configure tx types
|
// configure tx types
|
||||||
if fork >= forks.Berlin {
|
if fork >= forks.Berlin {
|
||||||
s.txtypes[AccessListTxType] = struct{}{}
|
s.txTypeBitVec.Set(AccessListTxType)
|
||||||
}
|
}
|
||||||
if fork >= forks.London {
|
if fork >= forks.London {
|
||||||
s.txtypes[DynamicFeeTxType] = struct{}{}
|
s.txTypeBitVec.Set(DynamicFeeTxType)
|
||||||
}
|
}
|
||||||
if fork >= forks.Cancun {
|
if fork >= forks.Cancun {
|
||||||
s.txtypes[BlobTxType] = struct{}{}
|
s.txTypeBitVec.Set(BlobTxType)
|
||||||
}
|
}
|
||||||
if fork >= forks.Prague {
|
if fork >= forks.Prague {
|
||||||
s.txtypes[SetCodeTxType] = struct{}{}
|
s.txTypeBitVec.Set(SetCodeTxType)
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
@ -228,7 +226,7 @@ func (s *modernSigner) ChainID() *big.Int {
|
||||||
|
|
||||||
func (s *modernSigner) Equal(s2 Signer) bool {
|
func (s *modernSigner) Equal(s2 Signer) bool {
|
||||||
other, ok := s2.(*modernSigner)
|
other, ok := s2.(*modernSigner)
|
||||||
return ok && s.chainID.Cmp(other.chainID) == 0 && maps.Equal(s.txtypes, other.txtypes) && s.legacy.Equal(other.legacy)
|
return ok && s.chainID.Cmp(other.chainID) == 0 && s.txTypeBitVec.Equals(&other.txTypeBitVec) && s.legacy.Equal(other.legacy)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *modernSigner) Hash(tx *Transaction) common.Hash {
|
func (s *modernSigner) Hash(tx *Transaction) common.Hash {
|
||||||
|
|
@ -236,8 +234,7 @@ func (s *modernSigner) Hash(tx *Transaction) common.Hash {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *modernSigner) supportsType(txtype byte) bool {
|
func (s *modernSigner) supportsType(txtype byte) bool {
|
||||||
_, ok := s.txtypes[txtype]
|
return s.txTypeBitVec.Has(txtype)
|
||||||
return ok
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *modernSigner) Sender(tx *Transaction) (common.Address, error) {
|
func (s *modernSigner) Sender(tx *Transaction) (common.Address, error) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue