Skip to content

Commit

Permalink
mock-consensus: 🔀 test node can fast_forward
Browse files Browse the repository at this point in the history
fixes #3933.

this introduces a `fast_forward` method to a test node, allowing tests
using the mock consensus engine (#3588) to fast forward a certain number
of blocks.

the existing `mock_consensus_can_send_a_sequence_of_empty_blocks` test
is rewritten to make use of this method.

this is done in service of #3995.

---

* #3588
* #3933
* #3995
  • Loading branch information
cratelyn committed Mar 12, 2024
1 parent 829dc64 commit a2f5218
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
31 changes: 7 additions & 24 deletions crates/core/app/tests/mock_consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use {
},
rand_core::OsRng,
tap::Tap,
tracing::{error_span, info, Instrument},
tracing::info,
};

/// Exercises that a test node can be instantiated using the consensus service.
Expand Down Expand Up @@ -83,29 +83,12 @@ async fn mock_consensus_can_send_a_sequence_of_empty_blocks() -> anyhow::Result<
let storage = TempStorage::new().await?;
let mut test_node = common::start_test_node(&storage).await?;

// Check that we begin at height 0, before any blocks have been generated.
assert_eq!(
storage.latest_snapshot().get_block_height().await?,
0,
"height should begin at 0"
);

for expected in 1..=8 {
// Generate an empty block.
test_node
.block()
.with_data(vec![])
.execute()
.instrument(error_span!("executing block", %expected))
.await?;

// Check that the latest snapshot has the expected block height.
let height = storage.latest_snapshot().get_block_height().await?;
assert_eq!(
height, expected,
"height should continue to incrementally grow"
);
}
let height = || async { storage.latest_snapshot().get_block_height().await };

// Fast forward eight blocks, and show that the height is 8 after doing so.
assert_eq!(height().await?, 0, "height should begin at 0");
test_node.fast_forward(8).await?;
assert_eq!(height().await?, 8_u64, "height should grow");

// Free our temporary storage.
drop(storage);
Expand Down
39 changes: 39 additions & 0 deletions crates/test/mock-consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,42 @@ impl<C> TestNode<C> {
format!("{:02X?}", self.last_app_hash)
}
}

impl<C> TestNode<C>
where
C: tower::Service<
tendermint::v0_37::abci::ConsensusRequest,
Response = tendermint::v0_37::abci::ConsensusResponse,
Error = tower::BoxError,
> + Send
+ Clone
+ 'static,
C::Future: Send + 'static,
C::Error: Sized,
{
/// Fast forwards a number of blocks.
#[tracing::instrument(
skip(self),
fields(
height = %self.height,
fast_forward.blocks = %blocks,
)
)]
pub async fn fast_forward(&mut self, blocks: usize) -> anyhow::Result<()> {
use {
tap::Tap,
tracing::{info, trace},
};

for i in 0..blocks {
self.block()
.with_data(vec![])
.execute()
.tap(|_| trace!(%i, "executing empty block"))
.await
.tap(|_| trace!(%i, "finished executing empty block"))?;
}

Ok(()).tap(|_| info!("finished fast forward"))
}
}

0 comments on commit a2f5218

Please sign in to comment.