Skip to content

Commit

Permalink
Fix AdvanceToBlock blocktime behavior (#648)
Browse files Browse the repository at this point in the history
* fix logic and fix bridge test

* fix stats test

* fixlint
  • Loading branch information
teddyding authored Oct 18, 2023
1 parent c17e215 commit 3e40b0b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
11 changes: 4 additions & 7 deletions protocol/testutil/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,6 @@ func (tApp *TestApp) AdvanceToBlock(
return tApp.App.NewContext(true, tApp.header)
}

// First advance to the prior block using the current block time. This ensures that we only update the time on
// the requested block.
if int64(block)-tApp.header.Height > 1 && options.BlockTime != tApp.header.Time {
tApp.AdvanceToBlock(block-1, options)
}

// Ensure that we grab the lock so that we can read and write passingCheckTxs correctly.
tApp.passingCheckTxsMtx.Lock()
defer tApp.passingCheckTxsMtx.Unlock()
Expand All @@ -413,7 +407,10 @@ func (tApp *TestApp) AdvanceToBlock(
for tApp.App.LastBlockHeight() < int64(block) {
tApp.panicIfChainIsHalted()
tApp.header.Height = tApp.App.LastBlockHeight() + 1
tApp.header.Time = options.BlockTime
// By default, only update block time at the requested block.
if tApp.header.Height == int64(block) {
tApp.header.Time = options.BlockTime
}
tApp.header.LastCommitHash = tApp.App.LastCommitID().Hash
tApp.header.NextValidatorsHash = tApp.App.LastCommitID().Hash

Expand Down
9 changes: 3 additions & 6 deletions protocol/x/bridge/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func TestBridge_Success(t *testing.T) {
genesisState.SafetyParams = tc.safetyParams
},
)
genesis.GenesisTime = tc.blockTime
return genesis
}).Build()
ctx := tApp.InitChain()
Expand Down Expand Up @@ -178,9 +179,7 @@ func TestBridge_Success(t *testing.T) {

// Advance to block right before bridge completion, if necessary.
if blockHeightOfBridgeCompletion-1 > uint32(ctx.BlockHeight()) {
ctx = tApp.AdvanceToBlock(blockHeightOfBridgeCompletion-1, testapp.AdvanceToBlockOptions{
BlockTime: tc.blockTime.Add(-time.Second * 1),
})
ctx = tApp.AdvanceToBlock(blockHeightOfBridgeCompletion-1, testapp.AdvanceToBlockOptions{})
}
// Verify that balances have not changed yet.
for _, event := range tc.bridgeEvents {
Expand All @@ -194,9 +193,7 @@ func TestBridge_Success(t *testing.T) {

// Verify that balances are updated, if bridge events were proposed, at the block of
// bridge completion.
ctx = tApp.AdvanceToBlock(blockHeightOfBridgeCompletion, testapp.AdvanceToBlockOptions{
BlockTime: tc.blockTime,
})
ctx = tApp.AdvanceToBlock(blockHeightOfBridgeCompletion, testapp.AdvanceToBlockOptions{})
for _, event := range tc.bridgeEvents {
balance := tApp.App.BankKeeper.GetBalance(
ctx,
Expand Down
3 changes: 2 additions & 1 deletion protocol/x/stats/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,13 @@ func TestExpireOldStats(t *testing.T) {
})
windowDuration := tApp.App.StatsKeeper.GetWindowDuration(ctx)
// 5 epochs are out of the window
ctx = tApp.AdvanceToBlock(100, testapp.AdvanceToBlockOptions{
tApp.AdvanceToBlock(3, testapp.AdvanceToBlockOptions{
BlockTime: time.Unix(0, 0).
Add(windowDuration).
Add((time.Duration(5*epochstypes.StatsEpochDuration) + 1) * time.Second).
UTC(),
})
ctx = tApp.AdvanceToBlock(100, testapp.AdvanceToBlockOptions{})
k := tApp.App.StatsKeeper

// Create a bunch of EpochStats.
Expand Down

0 comments on commit 3e40b0b

Please sign in to comment.