Skip to content

Commit e3f1e40

Browse files
committed
core/types: optimize modernSigner
1 parent b1809d1 commit e3f1e40

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

core/types/transaction.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,26 @@ const (
5353
SetCodeTxType = 0x04
5454
)
5555

56+
type TxTypeBitVec [(SetCodeTxType + 1 + 7) / 8]byte
57+
58+
func (v *TxTypeBitVec) Set(txType byte) {
59+
if txType >= byte(len(v)*8) {
60+
panic("tx type out of range")
61+
}
62+
v[txType/8] |= 1 << (txType % 8)
63+
}
64+
65+
func (v *TxTypeBitVec) Has(txType byte) bool {
66+
if txType >= byte(len(v)*8) {
67+
return false
68+
}
69+
return v[txType/8]&(1<<(txType%8)) != 0
70+
}
71+
72+
func (v *TxTypeBitVec) Equals(o *TxTypeBitVec) bool {
73+
return *v == *o
74+
}
75+
5676
// Transaction is an Ethereum transaction.
5777
type Transaction struct {
5878
inner TxData // Consensus contents of a transaction

core/types/transaction_signing.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"crypto/ecdsa"
2121
"errors"
2222
"fmt"
23-
"maps"
2423
"math/big"
2524

2625
"github.com/ethereum/go-ethereum/common"
@@ -183,9 +182,9 @@ type Signer interface {
183182
// modernSigner is the signer implementation that handles non-legacy transaction types.
184183
// For legacy transactions, it defers to one of the legacy signers (frontier, homestead, eip155).
185184
type modernSigner struct {
186-
txtypes map[byte]struct{}
187-
chainID *big.Int
188-
legacy Signer
185+
txTypeBitVec TxTypeBitVec
186+
chainID *big.Int
187+
legacy Signer
189188
}
190189

191190
func newModernSigner(chainID *big.Int, fork forks.Fork) Signer {
@@ -194,7 +193,6 @@ func newModernSigner(chainID *big.Int, fork forks.Fork) Signer {
194193
}
195194
s := &modernSigner{
196195
chainID: chainID,
197-
txtypes: make(map[byte]struct{}, 4),
198196
}
199197
// configure legacy signer
200198
switch {
@@ -205,19 +203,19 @@ func newModernSigner(chainID *big.Int, fork forks.Fork) Signer {
205203
default:
206204
s.legacy = FrontierSigner{}
207205
}
208-
s.txtypes[LegacyTxType] = struct{}{}
206+
s.txTypeBitVec.Set(LegacyTxType)
209207
// configure tx types
210208
if fork >= forks.Berlin {
211-
s.txtypes[AccessListTxType] = struct{}{}
209+
s.txTypeBitVec.Set(AccessListTxType)
212210
}
213211
if fork >= forks.London {
214-
s.txtypes[DynamicFeeTxType] = struct{}{}
212+
s.txTypeBitVec.Set(DynamicFeeTxType)
215213
}
216214
if fork >= forks.Cancun {
217-
s.txtypes[BlobTxType] = struct{}{}
215+
s.txTypeBitVec.Set(BlobTxType)
218216
}
219217
if fork >= forks.Prague {
220-
s.txtypes[SetCodeTxType] = struct{}{}
218+
s.txTypeBitVec.Set(SetCodeTxType)
221219
}
222220
return s
223221
}
@@ -228,16 +226,15 @@ func (s *modernSigner) ChainID() *big.Int {
228226

229227
func (s *modernSigner) Equal(s2 Signer) bool {
230228
other, ok := s2.(*modernSigner)
231-
return ok && s.chainID.Cmp(other.chainID) == 0 && maps.Equal(s.txtypes, other.txtypes) && s.legacy.Equal(other.legacy)
229+
return ok && s.chainID.Cmp(other.chainID) == 0 && s.txTypeBitVec.Equals(&other.txTypeBitVec) && s.legacy.Equal(other.legacy)
232230
}
233231

234232
func (s *modernSigner) Hash(tx *Transaction) common.Hash {
235233
return tx.inner.sigHash(s.chainID)
236234
}
237235

238236
func (s *modernSigner) supportsType(txtype byte) bool {
239-
_, ok := s.txtypes[txtype]
240-
return ok
237+
return s.txTypeBitVec.Has(txtype)
241238
}
242239

243240
func (s *modernSigner) Sender(tx *Transaction) (common.Address, error) {

0 commit comments

Comments
 (0)