Skip to content

Commit a851e39

Browse files
lmittmannfjl
andauthored
core/types: use new atomic types in caches (#29411)
* use generic atomic types in tx caches * use generic atomic types in block caches * eth/catalyst: avoid copying tx in test --------- Co-authored-by: lmittmann <[email protected]> Co-authored-by: Felix Lange <[email protected]>
1 parent 9cb8de8 commit a851e39

File tree

4 files changed

+19
-20
lines changed

4 files changed

+19
-20
lines changed

core/types/block.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ type Block struct {
197197
withdrawals Withdrawals
198198

199199
// caches
200-
hash atomic.Value
201-
size atomic.Value
200+
hash atomic.Pointer[common.Hash]
201+
size atomic.Uint64
202202

203203
// These fields are used by package eth to track
204204
// inter-peer block relay.
@@ -406,8 +406,8 @@ func (b *Block) BlobGasUsed() *uint64 {
406406
// Size returns the true RLP encoded storage size of the block, either by encoding
407407
// and returning it, or returning a previously cached value.
408408
func (b *Block) Size() uint64 {
409-
if size := b.size.Load(); size != nil {
410-
return size.(uint64)
409+
if size := b.size.Load(); size > 0 {
410+
return size
411411
}
412412
c := writeCounter(0)
413413
rlp.Encode(&c, b)
@@ -486,11 +486,11 @@ func (b *Block) WithWithdrawals(withdrawals []*Withdrawal) *Block {
486486
// The hash is computed on the first call and cached thereafter.
487487
func (b *Block) Hash() common.Hash {
488488
if hash := b.hash.Load(); hash != nil {
489-
return hash.(common.Hash)
489+
return *hash
490490
}
491-
v := b.header.Hash()
492-
b.hash.Store(v)
493-
return v
491+
h := b.header.Hash()
492+
b.hash.Store(&h)
493+
return h
494494
}
495495

496496
type Blocks []*Block

core/types/transaction.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ type Transaction struct {
5757
time time.Time // Time first seen locally (spam avoidance)
5858

5959
// caches
60-
hash atomic.Value
61-
size atomic.Value
62-
from atomic.Value
60+
hash atomic.Pointer[common.Hash]
61+
size atomic.Uint64
62+
from atomic.Pointer[sigCache]
6363
}
6464

6565
// NewTx creates a new transaction.
@@ -462,7 +462,7 @@ func (tx *Transaction) Time() time.Time {
462462
// Hash returns the transaction hash.
463463
func (tx *Transaction) Hash() common.Hash {
464464
if hash := tx.hash.Load(); hash != nil {
465-
return hash.(common.Hash)
465+
return *hash
466466
}
467467

468468
var h common.Hash
@@ -471,15 +471,15 @@ func (tx *Transaction) Hash() common.Hash {
471471
} else {
472472
h = prefixedRlpHash(tx.Type(), tx.inner)
473473
}
474-
tx.hash.Store(h)
474+
tx.hash.Store(&h)
475475
return h
476476
}
477477

478478
// Size returns the true encoded storage size of the transaction, either by encoding
479479
// and returning it, or returning a previously cached value.
480480
func (tx *Transaction) Size() uint64 {
481-
if size := tx.size.Load(); size != nil {
482-
return size.(uint64)
481+
if size := tx.size.Load(); size > 0 {
482+
return size
483483
}
484484

485485
// Cache miss, encode and cache.

core/types/transaction_signing.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ func MustSignNewTx(prv *ecdsa.PrivateKey, s Signer, txdata TxData) *Transaction
128128
// signing method. The cache is invalidated if the cached signer does
129129
// not match the signer used in the current call.
130130
func Sender(signer Signer, tx *Transaction) (common.Address, error) {
131-
if sc := tx.from.Load(); sc != nil {
132-
sigCache := sc.(sigCache)
131+
if sigCache := tx.from.Load(); sigCache != nil {
133132
// If the signer used to derive from in a previous
134133
// call is not the same as used current, invalidate
135134
// the cache.
@@ -142,7 +141,7 @@ func Sender(signer Signer, tx *Transaction) (common.Address, error) {
142141
if err != nil {
143142
return common.Address{}, err
144143
}
145-
tx.from.Store(sigCache{signer: signer, from: addr})
144+
tx.from.Store(&sigCache{signer: signer, from: addr})
146145
return addr, nil
147146
}
148147

eth/catalyst/simulated_beacon_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func startSimulatedBeaconEthService(t *testing.T, genesis *core.Genesis) (*node.
7474
// send enough transactions to fill multiple blocks
7575
func TestSimulatedBeaconSendWithdrawals(t *testing.T) {
7676
var withdrawals []types.Withdrawal
77-
txs := make(map[common.Hash]types.Transaction)
77+
txs := make(map[common.Hash]*types.Transaction)
7878

7979
var (
8080
// testKey is a private key to use for funding a tester account.
@@ -110,7 +110,7 @@ func TestSimulatedBeaconSendWithdrawals(t *testing.T) {
110110
if err != nil {
111111
t.Fatalf("error signing transaction, err=%v", err)
112112
}
113-
txs[tx.Hash()] = *tx
113+
txs[tx.Hash()] = tx
114114

115115
if err := ethService.APIBackend.SendTx(context.Background(), tx); err != nil {
116116
t.Fatal("SendTx failed", err)

0 commit comments

Comments
 (0)