[txpool] Best function to accept onTopOf argument and indicate whenever it is too early to gather tx for a block (#685)

Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local>
This commit is contained in:
ledgerwatch 2022-10-14 23:11:37 +01:00 committed by GitHub
parent 7e68f20838
commit 51b120abdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 5 deletions

View File

@ -605,7 +605,11 @@ func (p *TxPool) Started() bool { return p.started.Load() }
// Best - returns top `n` elements of pending queue
// id doesn't perform full copy of txs, hovewer underlying elements are immutable
func (p *TxPool) Best(n uint16, txs *types.TxsRlp, tx kv.Tx) error {
func (p *TxPool) Best(n uint16, txs *types.TxsRlp, tx kv.Tx, onTopOf uint64) (bool, error) {
// First wait for the corresponding block to arrive
if p.lastSeenBlock.Load() < onTopOf {
return false, nil // Too early
}
p.lock.RLock()
defer p.lock.RUnlock()
@ -621,7 +625,7 @@ func (p *TxPool) Best(n uint16, txs *types.TxsRlp, tx kv.Tx) error {
}
rlpTx, sender, isLocal, err := p.getRlpLocked(tx, mt.Tx.IDHash[:])
if err != nil {
return err
return false, err
}
if len(rlpTx) == 0 {
p.pending.Remove(mt)
@ -633,7 +637,7 @@ func (p *TxPool) Best(n uint16, txs *types.TxsRlp, tx kv.Tx) error {
j++
}
txs.Resize(uint(j))
return nil
return true, nil
}
func (p *TxPool) CountContent() (int, int, int) {

View File

@ -50,7 +50,7 @@ var TxPoolAPIVersion = &types2.VersionReply{Major: 1, Minor: 0, Patch: 0}
type txPool interface {
ValidateSerializedTxn(serializedTxn []byte) error
Best(n uint16, txs *types.TxsRlp, tx kv.Tx) error
Best(n uint16, txs *types.TxsRlp, tx kv.Tx, onTopOf uint64) (bool, error)
GetRlp(tx kv.Tx, hash []byte) ([]byte, error)
AddLocalTxs(ctx context.Context, newTxs types.TxSlots, tx kv.Tx) ([]DiscardReason, error)
deprecatedForEach(_ context.Context, f func(rlp, sender []byte, t SubPoolType), tx kv.Tx)
@ -154,7 +154,7 @@ func (s *GrpcServer) Pending(ctx context.Context, _ *emptypb.Empty) (*txpool_pro
reply := &txpool_proto.PendingReply{}
reply.Txs = make([]*txpool_proto.PendingReply_Tx, 0, 32)
txSlots := types.TxsRlp{}
if err := s.txPool.Best(math.MaxInt16, &txSlots, tx); err != nil {
if _, err := s.txPool.Best(math.MaxInt16, &txSlots, tx, 0 /* onTopOf */); err != nil {
return nil, err
}
var senderArr [20]byte