Skip to content

Commit fc4dd18

Browse files
buddh0lightclient
andauthored
core/txpool: fix error logs flood caused by removeAuthorities (#31249)
when remove an non-SetCodeTxType transaction, error logs flood ``` t=2025-02-25T03:11:06+0000 lvl=error msg="Authority with untracked tx" addr=0xD5bf9221fCB1C31Cd1EE477a60c148d40dD63DC1 hash=0x626fdf205a5b1619deb2f9e51fed567353f80acbd522265b455daa0821c571d9 ``` in this PR, only try to removeAuthorities for txs with SetCodeTxType in addition, the performance of removeAuthorities improved a lot, because no need range all `t.auths` now. --------- Co-authored-by: lightclient <[email protected]>
1 parent 1d2043e commit fc4dd18

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

core/txpool/legacypool/legacypool.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,12 +1765,12 @@ func (t *lookup) Remove(hash common.Hash) {
17651765
t.lock.Lock()
17661766
defer t.lock.Unlock()
17671767

1768-
t.removeAuthorities(hash)
17691768
tx, ok := t.txs[hash]
17701769
if !ok {
17711770
log.Error("No transaction found to be deleted", "hash", hash)
17721771
return
17731772
}
1773+
t.removeAuthorities(tx)
17741774
t.slots -= numSlots(tx)
17751775
slotsGauge.Update(int64(t.slots))
17761776

@@ -1808,8 +1808,9 @@ func (t *lookup) addAuthorities(tx *types.Transaction) {
18081808

18091809
// removeAuthorities stops tracking the supplied tx in relation to its
18101810
// authorities.
1811-
func (t *lookup) removeAuthorities(hash common.Hash) {
1812-
for addr := range t.auths {
1811+
func (t *lookup) removeAuthorities(tx *types.Transaction) {
1812+
hash := tx.Hash()
1813+
for _, addr := range tx.SetCodeAuthorities() {
18131814
list := t.auths[addr]
18141815
// Remove tx from tracker.
18151816
if i := slices.Index(list, hash); i >= 0 {

core/txpool/legacypool/legacypool_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"fmt"
2424
"math/big"
2525
"math/rand"
26+
"slices"
2627
"sync"
2728
"sync/atomic"
2829
"testing"
@@ -238,6 +239,23 @@ func validatePoolInternals(pool *LegacyPool) error {
238239
return fmt.Errorf("pending nonce mismatch: have %v, want %v", nonce, last+1)
239240
}
240241
}
242+
// Ensure all auths in pool are tracked
243+
for _, tx := range pool.all.txs {
244+
for _, addr := range tx.SetCodeAuthorities() {
245+
list := pool.all.auths[addr]
246+
if i := slices.Index(list, tx.Hash()); i < 0 {
247+
return fmt.Errorf("authority not tracked: addr %s, tx %s", addr, tx.Hash())
248+
}
249+
}
250+
}
251+
// Ensure all auths in pool have an associated tx.
252+
for addr, hashes := range pool.all.auths {
253+
for _, hash := range hashes {
254+
if _, ok := pool.all.txs[hash]; !ok {
255+
return fmt.Errorf("dangling authority, missing originating tx: addr %s, hash %s", addr, hash.Hex())
256+
}
257+
}
258+
}
241259
return nil
242260
}
243261

@@ -2381,6 +2399,32 @@ func TestSetCodeTransactions(t *testing.T) {
23812399
}
23822400
},
23832401
},
2402+
{
2403+
name: "remove-hash-from-authority-tracker",
2404+
pending: 10,
2405+
run: func(name string) {
2406+
var keys []*ecdsa.PrivateKey
2407+
for i := 0; i < 30; i++ {
2408+
key, _ := crypto.GenerateKey()
2409+
keys = append(keys, key)
2410+
addr := crypto.PubkeyToAddress(key.PublicKey)
2411+
testAddBalance(pool, addr, big.NewInt(params.Ether))
2412+
}
2413+
// Create a transactions with 3 unique auths so the lookup's auth map is
2414+
// filled with addresses.
2415+
for i := 0; i < 30; i += 3 {
2416+
if err := pool.addRemoteSync(pricedSetCodeTx(0, 250000, uint256.NewInt(10), uint256.NewInt(3), keys[i], []unsignedAuth{{0, keys[i]}, {0, keys[i+1]}, {0, keys[i+2]}})); err != nil {
2417+
t.Fatalf("%s: failed to add with remote setcode transaction: %v", name, err)
2418+
}
2419+
}
2420+
// Replace one of the transactions with a normal transaction so that the
2421+
// original hash is removed from the tracker. The hash should be
2422+
// associated with 3 different authorities.
2423+
if err := pool.addRemoteSync(pricedTransaction(0, 100000, big.NewInt(1000), keys[0])); err != nil {
2424+
t.Fatalf("%s: failed to replace with remote transaction: %v", name, err)
2425+
}
2426+
},
2427+
},
23842428
} {
23852429
tt.run(tt.name)
23862430
pending, queued := pool.Stats()

core/types/transaction.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,15 +483,23 @@ func (tx *Transaction) SetCodeAuthorizations() []SetCodeAuthorization {
483483
return setcodetx.AuthList
484484
}
485485

486-
// SetCodeAuthorities returns a list of each authorization's corresponding authority.
486+
// SetCodeAuthorities returns a list of unique authorities from the
487+
// authorization list.
487488
func (tx *Transaction) SetCodeAuthorities() []common.Address {
488489
setcodetx, ok := tx.inner.(*SetCodeTx)
489490
if !ok {
490491
return nil
491492
}
492-
auths := make([]common.Address, 0, len(setcodetx.AuthList))
493+
var (
494+
marks = make(map[common.Address]bool)
495+
auths = make([]common.Address, 0, len(setcodetx.AuthList))
496+
)
493497
for _, auth := range setcodetx.AuthList {
494498
if addr, err := auth.Authority(); err == nil {
499+
if marks[addr] {
500+
continue
501+
}
502+
marks[addr] = true
495503
auths = append(auths, addr)
496504
}
497505
}

0 commit comments

Comments
 (0)