Pool: started atomic (#61)

* save

* save
This commit is contained in:
Alex Sharov 2021-09-08 12:30:37 +07:00 committed by GitHub
parent b4933b94d2
commit c4efc0ea3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -505,6 +505,7 @@ func (b *ByNonce) replaceOrInsert(mt *metaTx) *metaTx {
type TxPool struct {
lock *sync.RWMutex
stared atomic.Bool
protocolBaseFee atomic.Uint64
currentBaseFee atomic.Uint64
@ -669,7 +670,7 @@ func (p *TxPool) IsLocal(idHash []byte) bool {
return ok
}
func (p *TxPool) AddNewGoodPeer(peerID PeerID) { p.recentlyConnectedPeers.AddPeer(peerID) }
func (p *TxPool) Started() bool { return p.protocolBaseFee.Load() > 0 }
func (p *TxPool) Started() bool { return p.stared.Load() }
// Best - returns top `n` elements of pending queue
// id doesn't perform full copy of txs, hovewer underlying elements are immutable
@ -810,11 +811,10 @@ func (p *TxPool) AddLocals(ctx context.Context, newTxs TxSlots, tx kv.Tx) ([]Dis
return nil, err
}
protocolBaseFee, currentBaseFee := p.protocolBaseFee.Load(), p.currentBaseFee.Load()
if protocolBaseFee == 0 || currentBaseFee == 0 {
return nil, fmt.Errorf("non-zero base fee: %d,%d", protocolBaseFee, currentBaseFee)
if !p.Started() {
return nil, fmt.Errorf("pool not started yet")
}
if err := onNewTxs(tx, p.senders, newTxs, protocolBaseFee, currentBaseFee, p.pending, p.baseFee, p.queued, p.byNonce, p.byHash, p.discardLocked); err != nil {
if err := onNewTxs(tx, p.senders, newTxs, p.protocolBaseFee.Load(), p.currentBaseFee.Load(), p.pending, p.baseFee, p.queued, p.byNonce, p.byHash, p.discardLocked); err != nil {
return nil, err
}
@ -867,11 +867,11 @@ func (p *TxPool) processRemoteTxs(ctx context.Context, tx kv.Tx) error {
return err
}
protocolBaseFee, currentBaseFee := p.protocolBaseFee.Load(), p.currentBaseFee.Load()
if protocolBaseFee == 0 || currentBaseFee == 0 {
return fmt.Errorf("non-zero base fee: %d,%d", protocolBaseFee, currentBaseFee)
if !p.stared.Load() {
return fmt.Errorf("txpool not started yet")
}
if err := onNewTxs(tx, p.senders, newTxs, protocolBaseFee, currentBaseFee, p.pending, p.baseFee, p.queued, p.byNonce, p.byHash, p.discardLocked); err != nil {
if err := onNewTxs(tx, p.senders, newTxs, p.protocolBaseFee.Load(), p.currentBaseFee.Load(), p.pending, p.baseFee, p.queued, p.byNonce, p.byHash, p.discardLocked); err != nil {
return err
}
@ -930,6 +930,7 @@ func (p *TxPool) setBaseFee(baseFee uint64) (uint64, uint64) {
p.protocolBaseFee.Store(calcProtocolBaseFee(baseFee))
p.currentBaseFee.Store(baseFee)
}
p.stared.Store(true)
return p.protocolBaseFee.Load(), p.currentBaseFee.Load()
}