Skip to content

Commit

Permalink
refactor(mempool): use tmsync.Waker when txs are available (#761)
Browse files Browse the repository at this point in the history
* refactor(mempool): use tmsync.Waker for available txs notifications

* fix(mempool): panic due to uninitialized txsAvailable

* chore: formatting

* chore(mempool): remove not needed notifiedTxsAvailable

* Revert "chore(mempool): remove not needed notifiedTxsAvailable"

This reverts commit cfcd45c.

* chore: reset notifiedTxsAvailable only when height is changed

* revert: need locking on notifyTxsAvailable
  • Loading branch information
lklimek authored Mar 13, 2024
1 parent 220c648 commit c2c4ecd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
2 changes: 2 additions & 0 deletions internal/consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,8 @@ func (cs *State) handleTimeout(
}

func (cs *State) handleTxsAvailable(ctx context.Context, stateData *StateData) {
// TODO: Change to trace
cs.logger.Debug("new transactions are available", "height", stateData.Height, "round", stateData.Round, "step", stateData.Step)
// We only need to do this for round 0.
if stateData.Round != 0 {
return
Expand Down
4 changes: 3 additions & 1 deletion internal/consensus/vote_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func (s *voteSigner) signAddVote(
}
// If the node not in the validator set, do nothing.
if !stateData.Validators.HasProTxHash(s.privValidator.ProTxHash) {
s.logger.Error("do nothing, node is not a part of validator set")
s.logger.Error("do nothing, node is not a part of validator set",
"protxhash", s.privValidator.ProTxHash.ShortString(),
"validators", stateData.Validators)
return nil
}
keyVals := []any{"height", stateData.Height, "round", stateData.Round, "quorum_hash", stateData.Validators.QuorumHash}
Expand Down
48 changes: 36 additions & 12 deletions internal/mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/dashpay/tenderdash/config"
"github.com/dashpay/tenderdash/internal/libs/clist"
tmstrings "github.com/dashpay/tenderdash/internal/libs/strings"
tmsync "github.com/dashpay/tenderdash/internal/libs/sync"
"github.com/dashpay/tenderdash/libs/log"
"github.com/dashpay/tenderdash/types"
)
Expand Down Expand Up @@ -48,10 +49,12 @@ type TxMempool struct {
// Synchronized fields, protected by mtx.
mtx *sync.RWMutex
notifiedTxsAvailable bool
txsAvailable chan struct{} // one value sent per height when mempool is not empty
preCheck PreCheckFunc
postCheck PostCheckFunc
height int64 // the latest height passed to Update
// txsAvailable is a waker that triggers when transactions are available in the mempool.
// Can be nil if not enabled with EnableTxsAvailable.
txsAvailable *tmsync.Waker
preCheck PreCheckFunc
postCheck PostCheckFunc
height int64 // the latest height passed to Update

txs *clist.CList // valid transactions (passed CheckTx)
txByKey map[types.TxKey]*clist.CElement
Expand Down Expand Up @@ -81,6 +84,7 @@ func NewTxMempool(
txByKey: make(map[types.TxKey]*clist.CElement),
txBySender: make(map[string]*clist.CElement),
}

if cfg.CacheSize > 0 {
txmp.cache = NewLRUTxCache(cfg.CacheSize)
}
Expand Down Expand Up @@ -149,12 +153,26 @@ func (txmp *TxMempool) EnableTxsAvailable() {
txmp.mtx.Lock()
defer txmp.mtx.Unlock()

txmp.txsAvailable = make(chan struct{}, 1)
if txmp.txsAvailable != nil {
if err := txmp.txsAvailable.Close(); err != nil {
txmp.logger.Error("failed to close txsAvailable", "err", err)
}
}
txmp.txsAvailable = tmsync.NewWaker()
}

// TxsAvailable returns a channel which fires once for every height, and only
// when transactions are available in the mempool. It is thread-safe.
func (txmp *TxMempool) TxsAvailable() <-chan struct{} { return txmp.txsAvailable }
//
// Note: returned channel might never close if EnableTxsAvailable() was not called before
// calling this function.
func (txmp *TxMempool) TxsAvailable() <-chan struct{} {
if txmp.txsAvailable == nil {
return make(<-chan struct{})
}

return txmp.txsAvailable.Sleep()
}

// CheckTx adds the given transaction to the mempool if it fits and passes the
// application's ABCI CheckTx method.
Expand Down Expand Up @@ -397,8 +415,10 @@ func (txmp *TxMempool) Update(
len(blockTxs), len(deliverTxResponses)))
}

txmp.height = blockHeight
txmp.notifiedTxsAvailable = false
if txmp.height != blockHeight {
txmp.height = blockHeight
txmp.notifiedTxsAvailable = false
}

if newPreFn != nil {
txmp.preCheck = newPreFn
Expand Down Expand Up @@ -774,8 +794,10 @@ func (txmp *TxMempool) recheckTransactions(ctx context.Context) {

// When recheck is complete, trigger a notification for more transactions.
_ = g.Wait()

txmp.mtx.Lock()
defer txmp.mtx.Unlock()

txmp.notifyTxsAvailable()
}()
}
Expand Down Expand Up @@ -830,6 +852,11 @@ func (txmp *TxMempool) purgeExpiredTxs(blockHeight int64) {
}
}

// notifyTxsAvailable triggers a notification that transactions are available in
// the mempool. It is a no-op if the mempool is empty or if a notification has
// already been sent.
//
// No locking is required to call this method.
func (txmp *TxMempool) notifyTxsAvailable() {
if txmp.Size() == 0 {
return // nothing to do
Expand All @@ -839,9 +866,6 @@ func (txmp *TxMempool) notifyTxsAvailable() {
// channel cap is 1, so this will send once
txmp.notifiedTxsAvailable = true

select {
case txmp.txsAvailable <- struct{}{}:
default:
}
txmp.txsAvailable.Wake()
}
}
2 changes: 1 addition & 1 deletion internal/mempool/reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Reactor struct {

cfg *config.MempoolConfig
mempool *TxMempool
ids *IDs
ids *IDs // Peer IDs assigned for peers

peerEvents p2p.PeerEventSubscriber
p2pClient *client.Client
Expand Down

0 comments on commit c2c4ecd

Please sign in to comment.