Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vms/avm: Add exists bool to mempool Peek #2465

Merged
merged 3 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions vms/avm/block/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ func (b *builder) BuildBlock(context.Context) (snowman.Block, error) {
remainingSize = targetBlockSize
)
for {
tx := b.mempool.Peek()
tx, exists := b.mempool.Peek()
// Invariant: [mempool.MaxTxSize] < [targetBlockSize]. This guarantees
// that we will only stop building a block once there are no
// transactions in the mempool or the block is at least
// [targetBlockSize - mempool.MaxTxSize] bytes full.
if tx == nil || len(tx.Bytes()) > remainingSize {
if !exists || len(tx.Bytes()) > remainingSize {
break
}
b.mempool.Remove([]*txs.Tx{tx})
Expand Down
26 changes: 13 additions & 13 deletions vms/avm/block/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ func TestBuilderBuildBlock(t *testing.T) {
tx := &txs.Tx{Unsigned: unsignedTx}

mempool := mempool.NewMockMempool(ctrl)
mempool.EXPECT().Peek().Return(tx)
mempool.EXPECT().Peek().Return(tx, true)
mempool.EXPECT().Remove([]*txs.Tx{tx})
mempool.EXPECT().MarkDropped(tx.ID(), errTest)
// Second loop iteration
mempool.EXPECT().Peek().Return(nil)
mempool.EXPECT().Peek().Return(nil, false)
mempool.EXPECT().RequestBuildBlock()

return New(
Expand Down Expand Up @@ -179,11 +179,11 @@ func TestBuilderBuildBlock(t *testing.T) {
tx := &txs.Tx{Unsigned: unsignedTx}

mempool := mempool.NewMockMempool(ctrl)
mempool.EXPECT().Peek().Return(tx)
mempool.EXPECT().Peek().Return(tx, true)
mempool.EXPECT().Remove([]*txs.Tx{tx})
mempool.EXPECT().MarkDropped(tx.ID(), errTest)
// Second loop iteration
mempool.EXPECT().Peek().Return(nil)
mempool.EXPECT().Peek().Return(nil, false)
mempool.EXPECT().RequestBuildBlock()

return New(
Expand Down Expand Up @@ -225,11 +225,11 @@ func TestBuilderBuildBlock(t *testing.T) {
tx := &txs.Tx{Unsigned: unsignedTx}

mempool := mempool.NewMockMempool(ctrl)
mempool.EXPECT().Peek().Return(tx)
mempool.EXPECT().Peek().Return(tx, true)
mempool.EXPECT().Remove([]*txs.Tx{tx})
mempool.EXPECT().MarkDropped(tx.ID(), errTest)
// Second loop iteration
mempool.EXPECT().Peek().Return(nil)
mempool.EXPECT().Peek().Return(nil, false)
mempool.EXPECT().RequestBuildBlock()

return New(
Expand Down Expand Up @@ -309,14 +309,14 @@ func TestBuilderBuildBlock(t *testing.T) {
)

mempool := mempool.NewMockMempool(ctrl)
mempool.EXPECT().Peek().Return(tx1)
mempool.EXPECT().Peek().Return(tx1, true)
mempool.EXPECT().Remove([]*txs.Tx{tx1})
// Second loop iteration
mempool.EXPECT().Peek().Return(tx2)
mempool.EXPECT().Peek().Return(tx2, true)
mempool.EXPECT().Remove([]*txs.Tx{tx2})
mempool.EXPECT().MarkDropped(tx2.ID(), blkexecutor.ErrConflictingBlockTxs)
// Third loop iteration
mempool.EXPECT().Peek().Return(nil)
mempool.EXPECT().Peek().Return(nil, false)
mempool.EXPECT().RequestBuildBlock()

// To marshal the tx/block
Expand Down Expand Up @@ -385,10 +385,10 @@ func TestBuilderBuildBlock(t *testing.T) {
tx := &txs.Tx{Unsigned: unsignedTx}

mempool := mempool.NewMockMempool(ctrl)
mempool.EXPECT().Peek().Return(tx)
mempool.EXPECT().Peek().Return(tx, true)
mempool.EXPECT().Remove([]*txs.Tx{tx})
// Second loop iteration
mempool.EXPECT().Peek().Return(nil)
mempool.EXPECT().Peek().Return(nil, false)
mempool.EXPECT().RequestBuildBlock()

// To marshal the tx/block
Expand Down Expand Up @@ -459,10 +459,10 @@ func TestBuilderBuildBlock(t *testing.T) {
tx := &txs.Tx{Unsigned: unsignedTx}

mempool := mempool.NewMockMempool(ctrl)
mempool.EXPECT().Peek().Return(tx)
mempool.EXPECT().Peek().Return(tx, true)
mempool.EXPECT().Remove([]*txs.Tx{tx})
// Second loop iteration
mempool.EXPECT().Peek().Return(nil)
mempool.EXPECT().Peek().Return(nil, false)
mempool.EXPECT().RequestBuildBlock()

// To marshal the tx/block
Expand Down
8 changes: 4 additions & 4 deletions vms/avm/txs/mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type Mempool interface {
Remove(txs []*txs.Tx)

// Peek returns the oldest tx in the mempool.
Peek() *txs.Tx
Peek() (tx *txs.Tx, exists bool)

// RequestBuildBlock notifies the consensus engine that a block should be
// built if there is at least one transaction in the mempool.
Expand Down Expand Up @@ -182,9 +182,9 @@ func (m *mempool) Remove(txsToRemove []*txs.Tx) {
}
}

func (m *mempool) Peek() *txs.Tx {
_, tx, _ := m.unissuedTxs.Oldest()
return tx
func (m *mempool) Peek() (*txs.Tx, bool) {
_, tx, exists := m.unissuedTxs.Oldest()
return tx, exists
}

func (m *mempool) RequestBuildBlock() {
Expand Down
20 changes: 14 additions & 6 deletions vms/avm/txs/mempool/mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,20 +181,28 @@ func TestPeekTxs(t *testing.T) {

testTxs := createTestTxs(2)

require.Nil(mempool.Peek())
tx, exists := mempool.Peek()
require.False(exists)
require.Nil(tx)

require.NoError(mempool.Add(testTxs[0]))
require.NoError(mempool.Add(testTxs[1]))

require.Equal(mempool.Peek(), testTxs[0])
require.NotEqual(mempool.Peek(), testTxs[1])
tx, exists = mempool.Peek()
require.True(exists)
require.Equal(tx, testTxs[0])
require.NotEqual(tx, testTxs[1])

mempool.Remove([]*txs.Tx{testTxs[0]})

require.NotEqual(mempool.Peek(), testTxs[0])
require.Equal(mempool.Peek(), testTxs[1])
tx, exists = mempool.Peek()
require.True(exists)
require.NotEqual(tx, testTxs[0])
require.Equal(tx, testTxs[1])

mempool.Remove([]*txs.Tx{testTxs[1]})

require.Nil(mempool.Peek())
tx, exists = mempool.Peek()
require.False(exists)
require.Nil(tx)
}
5 changes: 3 additions & 2 deletions vms/avm/txs/mempool/mock_mempool.go

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

Loading