Skip to content
Closed
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
24 changes: 6 additions & 18 deletions vms/platformvm/block/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/utils/timer"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/utils/units"
Expand All @@ -40,7 +39,11 @@ var (

type Builder interface {
mempool.Mempool
mempool.BlockTimer

// ResetBlockTimer schedules a timer to notify the consensus engine once
// there is a block ready to be built. If a block is ready to be built when
// this function is called, the engine will be notified directly.
ResetBlockTimer()

// BuildBlock is called on timer clock to attempt to create
// next block
Expand All @@ -58,9 +61,6 @@ type builder struct {
txExecutorBackend *txexecutor.Backend
blkManager blockexecutor.Manager

// channel to send messages to the consensus engine
toEngine chan<- common.Message

// This timer goes off when it is time for the next validator to add/leave
// the validator set. When it goes off ResetTimer() is called, potentially
// triggering creation of a new block.
Expand All @@ -72,14 +72,12 @@ func New(
txBuilder txbuilder.Builder,
txExecutorBackend *txexecutor.Backend,
blkManager blockexecutor.Manager,
toEngine chan<- common.Message,
) Builder {
builder := &builder{
Mempool: mempool,
txBuilder: txBuilder,
txExecutorBackend: txExecutorBackend,
blkManager: blkManager,
toEngine: toEngine,
}

builder.timer = timer.NewTimer(builder.setNextBuildBlockTime)
Expand Down Expand Up @@ -192,7 +190,7 @@ func (b *builder) setNextBuildBlockTime() {

if _, err := b.buildBlock(); err == nil {
// We can build a block now
b.notifyBlockReady()
b.Mempool.RequestBuildBlock()
return
}

Expand Down Expand Up @@ -229,16 +227,6 @@ func (b *builder) setNextBuildBlockTime() {
b.timer.SetTimeoutIn(waitTime)
}

// notifyBlockReady tells the consensus engine that a new block is ready to be
// created
func (b *builder) notifyBlockReady() {
select {
case b.toEngine <- common.PendingTxs:
default:
b.txExecutorBackend.Ctx.Log.Debug("dropping message to consensus engine")
}
}

// [timestamp] is min(max(now, parent timestamp), next staker change time)
func buildBlock(
builder *builder,
Expand Down
4 changes: 2 additions & 2 deletions vms/platformvm/block/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestPreviouslyDroppedTxsCanBeReAddedToMempool(t *testing.T) {
txID := tx.ID()

// A tx simply added to mempool is obviously not marked as dropped
require.NoError(env.mempool.Add(tx))
require.NoError(env.mempool.Add([]*txs.Tx{tx}))
require.True(env.mempool.Has(txID))
reason := env.mempool.GetDropReason(txID)
require.NoError(reason)
Expand All @@ -94,7 +94,7 @@ func TestPreviouslyDroppedTxsCanBeReAddedToMempool(t *testing.T) {
// A previously dropped tx, popped then re-added to mempool,
// is not dropped anymore
env.mempool.Remove([]*txs.Tx{tx})
require.NoError(env.mempool.Add(tx))
require.NoError(env.mempool.Add([]*txs.Tx{tx}))

require.True(env.mempool.Has(txID))
reason = env.mempool.GetDropReason(txID)
Expand Down
3 changes: 1 addition & 2 deletions vms/platformvm/block/builder/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func newEnvironment(t *testing.T) *environment {
metrics, err := metrics.New("", registerer)
require.NoError(err)

res.mempool, err = mempool.New("mempool", registerer, res)
res.mempool, err = mempool.New(res.ctx, "mempool", registerer, nil)
require.NoError(err)

res.blkManager = blockexecutor.NewManager(
Expand All @@ -193,7 +193,6 @@ func newEnvironment(t *testing.T) *environment {
res.txBuilder,
&res.backend,
res.blkManager,
nil, // toEngine,
)

res.blkManager.SetPreference(genesisID)
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/block/builder/standard_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestAtomicTxImports(t *testing.T) {
)
require.NoError(err)

require.NoError(env.Builder.Add(tx))
require.NoError(env.Builder.Add([]*txs.Tx{tx}))
b, err := env.Builder.BuildBlock(context.Background())
require.NoError(err)
// Test multiple verify calls work
Expand Down
8 changes: 1 addition & 7 deletions vms/platformvm/block/executor/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ const (
)

var (
_ mempool.BlockTimer = (*environment)(nil)

defaultMinStakingDuration = 24 * time.Hour
defaultMaxStakingDuration = 365 * 24 * time.Hour
defaultGenesisTime = time.Date(1997, 1, 1, 0, 0, 0, 0, time.UTC)
Expand Down Expand Up @@ -131,10 +129,6 @@ type environment struct {
backend *executor.Backend
}

func (*environment) ResetBlockTimer() {
// dummy call, do nothing for now
}

func newEnvironment(t *testing.T, ctrl *gomock.Controller) *environment {
res := &environment{
isBootstrapped: &utils.Atomic[bool]{},
Expand Down Expand Up @@ -199,7 +193,7 @@ func newEnvironment(t *testing.T, ctrl *gomock.Controller) *environment {
metrics := metrics.Noop

var err error
res.mempool, err = mempool.New("mempool", registerer, res)
res.mempool, err = mempool.New(res.ctx, "mempool", registerer, nil)
if err != nil {
panic(fmt.Errorf("failed to create mempool: %w", err))
}
Expand Down
14 changes: 5 additions & 9 deletions vms/platformvm/block/executor/rejector.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,11 @@ func (r *rejector) rejectBlock(b block.Block, blockType string) error {
return nil
}

for _, tx := range b.Txs() {
if err := r.Mempool.Add(tx); err != nil {
r.ctx.Log.Debug(
"failed to reissue tx",
zap.Stringer("txID", tx.ID()),
zap.Stringer("blkID", blkID),
zap.Error(err),
)
}
if err := r.Mempool.Add(b.Txs()); err != nil {
r.ctx.Log.Debug(
"failed to reissue txs from rejected block",
zap.Stringer("blkID", blkID),
)
}

return nil
Expand Down
4 changes: 1 addition & 3 deletions vms/platformvm/block/executor/rejector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ func TestRejectBlock(t *testing.T) {
}

// Set expected calls on dependencies.
for _, tx := range blk.Txs() {
mempool.EXPECT().Add(tx).Return(nil).Times(1)
}
mempool.EXPECT().Add(blk.Txs()).Return(nil).Times(1)

require.NoError(tt.rejectFunc(rejector, blk))
// Make sure block and its parent are removed from the state map.
Expand Down
7 changes: 1 addition & 6 deletions vms/platformvm/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,7 @@ func (n *network) issueTx(tx *txs.Tx) error {
return nil
}

if err := n.mempool.Add(tx); err != nil {
n.ctx.Log.Debug("tx failed to be added to the mempool",
zap.Stringer("txID", txID),
zap.Error(err),
)

if err := n.mempool.Add([]*txs.Tx{tx}); err != nil {
n.mempool.MarkDropped(txID, err)
return err
}
Expand Down
Loading