Skip to content

Commit 6665460

Browse files
author
colinlyguo
committed
change usedAndLeftSlots and knownConflicts to util functions
1 parent 2340de4 commit 6665460

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

core/tx_pool.go

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -811,42 +811,15 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
811811
}
812812
list := pool.pending[from]
813813
if list == nil || !list.Overlaps(tx) {
814-
usedAndLeftSlots := func(addr common.Address) (int, int) {
815-
var have int
816-
if list := pool.pending[addr]; list != nil {
817-
have += list.Len()
818-
}
819-
if list := pool.queue[addr]; list != nil {
820-
have += list.Len()
821-
}
822-
if pool.currentState.GetKeccakCodeHash(addr) != codehash.EmptyKeccakCodeHash || len(pool.all.auths[addr]) != 0 {
823-
// Allow at most one in-flight tx for delegated accounts or those with
824-
// a pending authorization.
825-
return have, max(0, 1-have)
826-
}
827-
return have, math.MaxInt
828-
}
829814
// Transaction takes a new nonce value out of the pool. Ensure it doesn't
830815
// overflow the number of permitted transactions from a single account
831816
// (i.e. max cancellable via out-of-bound transaction).
832-
if used, left := usedAndLeftSlots(from); left <= 0 {
817+
if used, left := usedAndLeftSlots(pool, from); left <= 0 {
833818
return fmt.Errorf("%w: pooled %d txs", ErrAccountLimitExceeded, used)
834819
}
835-
knownConflicts := func(auths []common.Address) []common.Address {
836-
var conflicts []common.Address
837-
// Authorities cannot conflict with any pending or queued transactions.
838-
for _, addr := range auths {
839-
if list := pool.pending[addr]; list != nil {
840-
conflicts = append(conflicts, addr)
841-
} else if list := pool.queue[addr]; list != nil {
842-
conflicts = append(conflicts, addr)
843-
}
844-
}
845-
return conflicts
846-
}
847820
// Verify no authorizations will invalidate existing transactions known to
848821
// the pool.
849-
if conflicts := knownConflicts(tx.SetCodeAuthorities()); len(conflicts) > 0 {
822+
if conflicts := knownConflicts(pool, tx.SetCodeAuthorities()); len(conflicts) > 0 {
850823
return fmt.Errorf("%w: authorization conflicts with other known tx", ErrAuthorityReserved)
851824
}
852825
}
@@ -2245,3 +2218,34 @@ func (t *txLookup) removeAuthorities(hash common.Hash) {
22452218
func numSlots(tx *types.Transaction) int {
22462219
return int((tx.Size() + txSlotSize - 1) / txSlotSize)
22472220
}
2221+
2222+
// usedAndLeftSlots returns the number of slots used and left for the given address.
2223+
func usedAndLeftSlots(pool *TxPool, addr common.Address) (int, int) {
2224+
var have int
2225+
if list := pool.pending[addr]; list != nil {
2226+
have += list.Len()
2227+
}
2228+
if list := pool.queue[addr]; list != nil {
2229+
have += list.Len()
2230+
}
2231+
if pool.currentState.GetKeccakCodeHash(addr) != codehash.EmptyKeccakCodeHash || len(pool.all.auths[addr]) != 0 {
2232+
// Allow at most one in-flight tx for delegated accounts or those with
2233+
// a pending authorization.
2234+
return have, max(0, 1-have)
2235+
}
2236+
return have, math.MaxInt
2237+
}
2238+
2239+
// knownConflicts returns a list of addresses that conflict with the given authorities.
2240+
func knownConflicts(pool *TxPool, auths []common.Address) []common.Address {
2241+
var conflicts []common.Address
2242+
// Authorities cannot conflict with any pending or queued transactions.
2243+
for _, addr := range auths {
2244+
if list := pool.pending[addr]; list != nil {
2245+
conflicts = append(conflicts, addr)
2246+
} else if list := pool.queue[addr]; list != nil {
2247+
conflicts = append(conflicts, addr)
2248+
}
2249+
}
2250+
return conflicts
2251+
}

0 commit comments

Comments
 (0)