Skip to content

Commit

Permalink
refactor(mempool)!: remove PreUpdate from Mempool interface and move …
Browse files Browse the repository at this point in the history
…its logic to Lock (tendermint#3361)

This PR partially reverts the backport of tendermint#3314 into the recently
released `v0.38.8` (and `v0.37.7`). With this change the `Mempool`
interface is the same as in previous versions.

The reason is that we do not want to break the public API. We still keep
in the code the feature that tendermint#3314 introduced by moving it inside the
existing `Lock` method. We also keep the `RecheckFull bool` field that
we added to `ErrMempoolIsFull`.

<!--

Please add a reference to the issue that this PR addresses and indicate
which
files are most critical to review. If it fully addresses a particular
issue,
please include "Closes #XXX" (where "XXX" is the issue number).

If this PR is non-trivial/large/complex, please ensure that you have
either
created an issue that the team's had a chance to respond to, or had some
discussion with the team prior to submitting substantial pull requests.
The team
can be reached via GitHub Discussions or the Cosmos Network Discord
server in
the #cometbft channel. GitHub Discussions is preferred over Discord as
it
allows us to keep track of conversations topically.
https://github.com/cometbft/cometbft/discussions

If the work in this PR is not aligned with the team's current
priorities, please
be advised that it may take some time before it is merged - especially
if it has
not yet been discussed with the team.

See the project board for the team's current priorities:
https://github.com/orgs/cometbft/projects/1

-->

---

#### PR checklist

- [ ] Tests written/updated
- [ ] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [ ] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [ ] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec
  • Loading branch information
hvanz authored Jun 28, 2024
1 parent 4027622 commit 1e22f66
Show file tree
Hide file tree
Showing 12 changed files with 9 additions and 34 deletions.
4 changes: 4 additions & 0 deletions .changelog/breaking-changes/3361-mempool-preupdate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- `[mempool]` Revert adding the method `PreUpdate()` to the `Mempool` interface, recently introduced
in the previous patch release. Its logic is now moved into the `Lock` method. With this change,
the `Mempool` interface is the same as before the previous patch.
([\#3361](https://github.com/cometbft/cometbft/pull/3361))
1 change: 0 additions & 1 deletion blocksync/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ func newReactor(
mp := &mpmocks.Mempool{}
mp.On("Lock").Return()
mp.On("Unlock").Return()
mp.On("PreUpdate").Return()
mp.On("FlushAppConn", mock.Anything).Return(nil)
mp.On("Update",
mock.Anything,
Expand Down
1 change: 0 additions & 1 deletion consensus/replay_stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ var _ mempl.Mempool = emptyMempool{}

func (emptyMempool) Lock() {}
func (emptyMempool) Unlock() {}
func (emptyMempool) PreUpdate() {}
func (emptyMempool) Size() int { return 0 }
func (emptyMempool) SizeBytes() int64 { return 0 }
func (emptyMempool) CheckTx(types.Tx, func(*abci.ResponseCheckTx), mempl.TxInfo) error {
Expand Down
10 changes: 3 additions & 7 deletions mempool/clist_mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ func WithMetrics(metrics *Metrics) CListMempoolOption {

// Safe for concurrent use by multiple goroutines.
func (mem *CListMempool) Lock() {
if mem.recheck.setRecheckFull() {
mem.logger.Debug("the state of recheckFull has flipped")
}
mem.updateMtx.Lock()
}

Expand All @@ -160,13 +163,6 @@ func (mem *CListMempool) Unlock() {
mem.updateMtx.Unlock()
}

// Safe for concurrent use by multiple goroutines.
func (mem *CListMempool) PreUpdate() {
if mem.recheck.setRecheckFull() {
mem.logger.Debug("the state of recheckFull has flipped")
}
}

// Safe for concurrent use by multiple goroutines.
func (mem *CListMempool) Size() int {
return mem.txs.Len()
Expand Down
3 changes: 0 additions & 3 deletions mempool/clist_mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,6 @@ func TestMempoolConcurrentUpdateAndReceiveCheckTxResponse(t *testing.T) {
go func(h int) {
defer wg.Done()

mp.PreUpdate()
mp.Lock()
err := mp.FlushAppConn()
require.NoError(t, err)
Expand Down Expand Up @@ -935,7 +934,6 @@ func TestMempoolRecheckRace(t *testing.T) {
}

// Update one transaction to force rechecking the rest.
mp.PreUpdate()
mp.Lock()
err = mp.FlushAppConn()
require.NoError(t, err)
Expand Down Expand Up @@ -975,7 +973,6 @@ func TestMempoolConcurrentCheckTxAndUpdate(t *testing.T) {
break
}
txs := mp.ReapMaxBytesMaxGas(100, -1)
mp.PreUpdate()
mp.Lock()
err := mp.FlushAppConn() // needed to process the pending CheckTx requests and their callbacks
require.NoError(t, err)
Expand Down
6 changes: 2 additions & 4 deletions mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,13 @@ type Mempool interface {

// Lock locks the mempool. The consensus must be able to hold lock to safely
// update.
// Before acquiring the lock, it signals the mempool that a new update is coming.
// If the mempool is still rechecking at this point, it should be considered full.
Lock()

// Unlock unlocks the mempool.
Unlock()

// PreUpdate signals that a new update is coming, before acquiring the mempool lock.
// If the mempool is still rechecking at this point, it should be considered full.
PreUpdate()

// Update informs the mempool that the given txs were committed and can be
// discarded.
//
Expand Down
5 changes: 0 additions & 5 deletions mempool/mocks/mempool.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions mempool/nop_mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ func (*NopMempool) Lock() {}
// Unlock does nothing.
func (*NopMempool) Unlock() {}

func (*NopMempool) PreUpdate() {}

// Update does nothing.
func (*NopMempool) Update(
int64,
Expand Down
2 changes: 0 additions & 2 deletions mempool/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ func TestReactorConcurrency(t *testing.T) {
go func() {
defer wg.Done()

reactors[0].mempool.PreUpdate()
reactors[0].mempool.Lock()
defer reactors[0].mempool.Unlock()

Expand All @@ -111,7 +110,6 @@ func TestReactorConcurrency(t *testing.T) {
go func() {
defer wg.Done()

reactors[1].mempool.PreUpdate()
reactors[1].mempool.Lock()
defer reactors[1].mempool.Unlock()
err := reactors[1].mempool.Update(1, []types.Tx{}, make([]*abci.ExecTxResult, 0), nil, nil)
Expand Down
1 change: 0 additions & 1 deletion state/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,6 @@ func (blockExec *BlockExecutor) Commit(
block *types.Block,
abciResponse *abci.ResponseFinalizeBlock,
) (int64, error) {
blockExec.mempool.PreUpdate()
blockExec.mempool.Lock()
defer blockExec.mempool.Unlock()

Expand Down
5 changes: 0 additions & 5 deletions state/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func TestApplyBlock(t *testing.T) {
mp := &mpmocks.Mempool{}
mp.On("Lock").Return()
mp.On("Unlock").Return()
mp.On("PreUpdate").Return()
mp.On("FlushAppConn", mock.Anything).Return(nil)
mp.On("Update",
mock.Anything,
Expand Down Expand Up @@ -118,7 +117,6 @@ func TestFinalizeBlockDecidedLastCommit(t *testing.T) {
mp := &mpmocks.Mempool{}
mp.On("Lock").Return()
mp.On("Unlock").Return()
mp.On("PreUpdate").Return()
mp.On("FlushAppConn", mock.Anything).Return(nil)
mp.On("Update",
mock.Anything,
Expand Down Expand Up @@ -334,7 +332,6 @@ func TestFinalizeBlockMisbehavior(t *testing.T) {
mp := &mpmocks.Mempool{}
mp.On("Lock").Return()
mp.On("Unlock").Return()
mp.On("PreUpdate").Return()
mp.On("FlushAppConn", mock.Anything).Return(nil)
mp.On("Update",
mock.Anything,
Expand Down Expand Up @@ -593,7 +590,6 @@ func TestFinalizeBlockValidatorUpdates(t *testing.T) {
mp := &mpmocks.Mempool{}
mp.On("Lock").Return()
mp.On("Unlock").Return()
mp.On("PreUpdate").Return()
mp.On("FlushAppConn", mock.Anything).Return(nil)
mp.On("Update",
mock.Anything,
Expand Down Expand Up @@ -726,7 +722,6 @@ func TestEmptyPrepareProposal(t *testing.T) {
mp := &mpmocks.Mempool{}
mp.On("Lock").Return()
mp.On("Unlock").Return()
mp.On("PreUpdate").Return()
mp.On("FlushAppConn", mock.Anything).Return(nil)
mp.On("Update",
mock.Anything,
Expand Down
3 changes: 0 additions & 3 deletions state/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func TestValidateBlockHeader(t *testing.T) {
mp := &mpmocks.Mempool{}
mp.On("Lock").Return()
mp.On("Unlock").Return()
mp.On("PreUpdate").Return()
mp.On("FlushAppConn", mock.Anything).Return(nil)
mp.On("Update",
mock.Anything,
Expand Down Expand Up @@ -136,7 +135,6 @@ func TestValidateBlockCommit(t *testing.T) {
mp := &mpmocks.Mempool{}
mp.On("Lock").Return()
mp.On("Unlock").Return()
mp.On("PreUpdate").Return()
mp.On("FlushAppConn", mock.Anything).Return(nil)
mp.On("Update",
mock.Anything,
Expand Down Expand Up @@ -289,7 +287,6 @@ func TestValidateBlockEvidence(t *testing.T) {
mp := &mpmocks.Mempool{}
mp.On("Lock").Return()
mp.On("Unlock").Return()
mp.On("PreUpdate").Return()
mp.On("FlushAppConn", mock.Anything).Return(nil)
mp.On("Update",
mock.Anything,
Expand Down

0 comments on commit 1e22f66

Please sign in to comment.