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/platformvm: Add TestBuildBlockShouldAdvanceTime test #2471

Merged
merged 4 commits into from
Dec 12, 2023
Merged
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
90 changes: 36 additions & 54 deletions vms/platformvm/block/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,42 @@ func TestBuildBlockShouldReward(t *testing.T) {
require.NotEmpty(rewardUTXOs)
}

func TestBuildBlockAdvanceTime(t *testing.T) {
require := require.New(t)

env := newEnvironment(t)
env.ctx.Lock.Lock()
defer func() {
require.NoError(shutdownEnvironment(env))
env.ctx.Lock.Unlock()
}()

var (
now = env.backend.Clk.Time()
nextTime = now.Add(2 * txexecutor.SyncBound)
)

// Add a staker to [env.state]
env.state.PutCurrentValidator(&state.Staker{
NextTime: nextTime,
Priority: txs.PrimaryNetworkValidatorCurrentPriority,
})

// Advance wall clock to [nextTime]
env.backend.Clk.Set(nextTime)

// [BuildBlock] should build a block advancing the time to [NextTime]
blkIntf, err := env.Builder.BuildBlock(context.Background())
require.NoError(err)

require.IsType(&blockexecutor.Block{}, blkIntf)
blk := blkIntf.(*blockexecutor.Block)
require.Empty(blk.Txs())
require.IsType(&block.BanffStandardBlock{}, blk.Block)
standardBlk := blk.Block.(*block.BanffStandardBlock)
require.Equal(nextTime.Unix(), standardBlk.Timestamp().Unix())
}

func TestPreviouslyDroppedTxsCanBeReAddedToMempool(t *testing.T) {
require := require.New(t)

Expand Down Expand Up @@ -489,60 +525,6 @@ func TestBuildBlock(t *testing.T) {
}

tests := []test{
{
name: "should advance time",
builderF: func(ctrl *gomock.Controller) *builder {
mempool := mempool.NewMockMempool(ctrl)

// There are no txs.
mempool.EXPECT().DropExpiredStakerTxs(gomock.Any()).Return([]ids.ID{})
mempool.EXPECT().Peek().Return(nil, false)

clk := &mockable.Clock{}
clk.Set(now)
return &builder{
Mempool: mempool,
txExecutorBackend: &txexecutor.Backend{
Clk: clk,
},
}
},
timestamp: now.Add(-1 * time.Second),
forceAdvanceTime: true,
parentStateF: func(ctrl *gomock.Controller) state.Chain {
s := state.NewMockChain(ctrl)

// add current validator that ends at [now] - 1 second.
// That is, it ends in the past but after the current chain time.
// Handle calls in [getNextStakerToReward]
// and [GetNextStakerChangeTime]
// when determining whether to issue a reward tx.
currentStakerIter := state.NewMockStakerIterator(ctrl)
gomock.InOrder(
// expect calls from [getNextStakerToReward]
currentStakerIter.EXPECT().Next().Return(true),
currentStakerIter.EXPECT().Value().Return(&state.Staker{
NextTime: now.Add(-1 * time.Second),
Priority: txs.PrimaryNetworkDelegatorCurrentPriority,
}),
currentStakerIter.EXPECT().Release(),
)

s.EXPECT().GetCurrentStakerIterator().Return(currentStakerIter, nil).Times(1)
return s
},
expectedBlkF: func(require *require.Assertions) block.Block {
expectedBlk, err := block.NewBanffStandardBlock(
now.Add(-1*time.Second), // note the advanced time
parentID,
height,
nil, // empty block to advance time
)
require.NoError(err)
return expectedBlk
},
expectedErr: nil,
},
{
name: "has a staker tx no force",
builderF: func(ctrl *gomock.Controller) *builder {
Expand Down
Loading