Skip to content

Commit 6d0e162

Browse files
trantienduchnbuddh0lightclient
committed
Co-authored-by: buddho <[email protected]> Co-authored-by: lightclient <[email protected]>
1 parent c8d76b4 commit 6d0e162

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

core/txpool/legacypool/legacypool.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,7 +1989,6 @@ func (t *lookup) Remove(hash common.Hash) {
19891989
t.lock.Lock()
19901990
defer t.lock.Unlock()
19911991

1992-
t.removeAuthorities(hash)
19931992
tx, ok := t.locals[hash]
19941993
if !ok {
19951994
tx, ok = t.remotes[hash]
@@ -1998,6 +1997,7 @@ func (t *lookup) Remove(hash common.Hash) {
19981997
log.Error("No transaction found to be deleted", "hash", hash)
19991998
return
20001999
}
2000+
t.removeAuthorities(tx)
20012001
t.slots -= numSlots(tx)
20022002
slotsGauge.Update(int64(t.slots))
20032003

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

20612061
// removeAuthorities stops tracking the supplied tx in relation to its
20622062
// authorities.
2063-
func (t *lookup) removeAuthorities(hash common.Hash) {
2064-
for addr := range t.auths {
2063+
func (t *lookup) removeAuthorities(tx *types.Transaction) {
2064+
hash := tx.Hash()
2065+
for _, addr := range tx.SetCodeAuthorities() {
20652066
list := t.auths[addr]
20662067
// Remove tx from tracker.
20672068
if i := slices.Index(list, hash); i >= 0 {

core/txpool/legacypool/legacypool_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"github.com/ethereum/go-ethereum/params"
4242
"github.com/ethereum/go-ethereum/trie"
4343
"github.com/holiman/uint256"
44+
"golang.org/x/exp/slices"
4445
)
4546

4647
var (
@@ -210,6 +211,34 @@ func validatePoolInternals(pool *LegacyPool) error {
210211
return fmt.Errorf("totalcost went negative: %v", txs.totalcost)
211212
}
212213
}
214+
// Ensure all auths in pool are tracked
215+
for _, tx := range pool.all.locals {
216+
for _, addr := range tx.SetCodeAuthorities() {
217+
list := pool.all.auths[addr]
218+
if i := slices.Index(list, tx.Hash()); i < 0 {
219+
return fmt.Errorf("authority not tracked: addr %s, local tx %s", addr, tx.Hash())
220+
}
221+
}
222+
}
223+
for _, tx := range pool.all.remotes {
224+
for _, addr := range tx.SetCodeAuthorities() {
225+
list := pool.all.auths[addr]
226+
if i := slices.Index(list, tx.Hash()); i < 0 {
227+
return fmt.Errorf("authority not tracked: addr %s, remote tx %s", addr, tx.Hash())
228+
}
229+
}
230+
}
231+
232+
// Ensure all auths in pool have an associated tx.
233+
for addr, hashes := range pool.all.auths {
234+
for _, hash := range hashes {
235+
_, okLocal := pool.all.locals[hash]
236+
_, okRemote := pool.all.remotes[hash]
237+
if !okLocal && !okRemote {
238+
return fmt.Errorf("dangling authority, missing originating tx: addr %s, hash %s", addr, hash.Hex())
239+
}
240+
}
241+
}
213242
return nil
214243
}
215244

@@ -2750,6 +2779,32 @@ func TestSetCodeTransactions(t *testing.T) {
27502779
}
27512780
},
27522781
},
2782+
{
2783+
name: "remove-hash-from-authority-tracker",
2784+
pending: 10,
2785+
run: func(name string) {
2786+
var keys []*ecdsa.PrivateKey
2787+
for i := 0; i < 30; i++ {
2788+
key, _ := crypto.GenerateKey()
2789+
keys = append(keys, key)
2790+
addr := crypto.PubkeyToAddress(key.PublicKey)
2791+
testAddBalance(pool, addr, big.NewInt(params.Ether))
2792+
}
2793+
// Create a transactions with 3 unique auths so the lookup's auth map is
2794+
// filled with addresses.
2795+
for i := 0; i < 30; i += 3 {
2796+
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 {
2797+
t.Fatalf("%s: failed to add with remote setcode transaction: %v", name, err)
2798+
}
2799+
}
2800+
// Replace one of the transactions with a normal transaction so that the
2801+
// original hash is removed from the tracker. The hash should be
2802+
// associated with 3 different authorities.
2803+
if err := pool.addRemoteSync(pricedTransaction(0, 100000, big.NewInt(1000), keys[0])); err != nil {
2804+
t.Fatalf("%s: failed to replace with remote transaction: %v", name, err)
2805+
}
2806+
},
2807+
},
27532808
} {
27542809
tt.run(tt.name)
27552810
pending, queued := pool.Stats()

core/types/transaction.go

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

474-
// SetCodeAuthorities returns a list of each authorization's corresponding authority.
474+
// SetCodeAuthorities returns a list of unique authorities from the
475+
// authorization list.
475476
func (tx *Transaction) SetCodeAuthorities() []common.Address {
476477
setcodetx, ok := tx.inner.(*SetCodeTx)
477478
if !ok {
478479
return nil
479480
}
480-
auths := make([]common.Address, 0, len(setcodetx.AuthList))
481+
var (
482+
marks = make(map[common.Address]bool)
483+
auths = make([]common.Address, 0, len(setcodetx.AuthList))
484+
)
481485
for _, auth := range setcodetx.AuthList {
482486
if addr, err := auth.Authority(); err == nil {
487+
if marks[addr] {
488+
continue
489+
}
490+
marks[addr] = true
483491
auths = append(auths, addr)
484492
}
485493
}

0 commit comments

Comments
 (0)