Skip to content

Commit

Permalink
mock-consensus: 💹 generated blocks' height increases (#3840)
Browse files Browse the repository at this point in the history
this makes changes to the mock consensus engine so that the generated
`Block`s have monotonically increasing height.

see also:
* #3588
* #3788

an example, running the
`mock_consensus_can_send_a_sequence_of_empty_blocks` test:

```
; RUST_LOG="penumbra_mock_consensus=info" cargo test --package penumbra-app sequence_of_empty_blocks -- --nocapture
   Compiling penumbra-mock-consensus v0.67.1
   Compiling penumbra-app v0.67.1
    Finished test [unoptimized + debuginfo] target(s) in 9.10s
     Running unittests src/lib.rs (target/debug/deps/penumbra_app-1e86f1775ec8d9a7)

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 3 filtered out; finished in 0.00s

     Running tests/mock_consensus.rs (target/debug/deps/mock_consensus-caa27518a071c6af)

running 1 test
  2024-02-16T21:18:58.850646Z  INFO penumbra_mock_consensus::block: sending block
    at crates/test/mock-consensus/src/block.rs:97
    in penumbra_mock_consensus::block::execute with height: 1, time: 1708118338

  2024-02-16T21:18:58.860604Z  INFO penumbra_mock_consensus::block: finished sending block
    at crates/test/mock-consensus/src/block.rs:105
    in penumbra_mock_consensus::block::execute with height: 1, time: 1708118338

  2024-02-16T21:18:58.860725Z  INFO penumbra_mock_consensus::block: sending block
    at crates/test/mock-consensus/src/block.rs:97
    in penumbra_mock_consensus::block::execute with height: 2, time: 1708118338

  2024-02-16T21:18:58.869760Z  INFO penumbra_mock_consensus::block: finished sending block
    at crates/test/mock-consensus/src/block.rs:105
    in penumbra_mock_consensus::block::execute with height: 2, time: 1708118338

  2024-02-16T21:18:58.869883Z  INFO penumbra_mock_consensus::block: sending block
    at crates/test/mock-consensus/src/block.rs:97
    in penumbra_mock_consensus::block::execute with height: 3, time: 1708118338

  2024-02-16T21:18:58.879037Z  INFO penumbra_mock_consensus::block: finished sending block
    at crates/test/mock-consensus/src/block.rs:105
    in penumbra_mock_consensus::block::execute with height: 3, time: 1708118338

  2024-02-16T21:18:58.879143Z  INFO penumbra_mock_consensus::block: sending block
    at crates/test/mock-consensus/src/block.rs:97
    in penumbra_mock_consensus::block::execute with height: 4, time: 1708118338

  2024-02-16T21:18:58.888524Z  INFO penumbra_mock_consensus::block: finished sending block
    at crates/test/mock-consensus/src/block.rs:105
    in penumbra_mock_consensus::block::execute with height: 4, time: 1708118338

  2024-02-16T21:18:58.888659Z  INFO penumbra_mock_consensus::block: sending block
    at crates/test/mock-consensus/src/block.rs:97
    in penumbra_mock_consensus::block::execute with height: 5, time: 1708118338

  2024-02-16T21:18:58.898337Z  INFO penumbra_mock_consensus::block: finished sending block
    at crates/test/mock-consensus/src/block.rs:105
    in penumbra_mock_consensus::block::execute with height: 5, time: 1708118338

  2024-02-16T21:18:58.898486Z  INFO penumbra_mock_consensus::block: sending block
    at crates/test/mock-consensus/src/block.rs:97
    in penumbra_mock_consensus::block::execute with height: 6, time: 1708118338

  2024-02-16T21:18:58.908190Z  INFO penumbra_mock_consensus::block: finished sending block
    at crates/test/mock-consensus/src/block.rs:105
    in penumbra_mock_consensus::block::execute with height: 6, time: 1708118338

  2024-02-16T21:18:58.908325Z  INFO penumbra_mock_consensus::block: sending block
    at crates/test/mock-consensus/src/block.rs:97
    in penumbra_mock_consensus::block::execute with height: 7, time: 1708118338

  2024-02-16T21:18:58.917988Z  INFO penumbra_mock_consensus::block: finished sending block
    at crates/test/mock-consensus/src/block.rs:105
    in penumbra_mock_consensus::block::execute with height: 7, time: 1708118338

  2024-02-16T21:18:58.918116Z  INFO penumbra_mock_consensus::block: sending block
    at crates/test/mock-consensus/src/block.rs:97
    in penumbra_mock_consensus::block::execute with height: 8, time: 1708118338

  2024-02-16T21:18:58.927903Z  INFO penumbra_mock_consensus::block: finished sending block
    at crates/test/mock-consensus/src/block.rs:105
    in penumbra_mock_consensus::block::execute with height: 8, time: 1708118338

test mock_consensus_can_send_a_sequence_of_empty_blocks ... ok
```
  • Loading branch information
cratelyn authored Feb 16, 2024
1 parent 66db41d commit d08d8f2
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 31 deletions.
49 changes: 37 additions & 12 deletions crates/core/app/tests/mock_consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@

mod common;

use cnidarium::TempStorage;
use common::BuilderExt;
use penumbra_app::server::consensus::Consensus;
use penumbra_genesis::AppState;
use tendermint::evidence::List;
use {
self::common::BuilderExt,
cnidarium::TempStorage,
penumbra_app::server::consensus::Consensus,
penumbra_genesis::AppState,
penumbra_sct::component::clock::EpochRead,
tendermint::evidence::List,
tracing::{error_span, Instrument},
};

/// Exercises that a test node can be instantiated using the consensus service.
#[tokio::test]
async fn mock_consensus_can_send_an_init_chain_request() -> anyhow::Result<()> {
// Install a test logger, and acquire some temporary storage.
Expand Down Expand Up @@ -38,8 +43,10 @@ async fn mock_consensus_can_send_an_init_chain_request() -> anyhow::Result<()> {
Ok(())
}

/// Exercises that a series of empty blocks, with no validator set present, can be successfully
/// executed by the consensus service.
#[tokio::test]
async fn mock_consensus_can_send_a_single_empty_block() -> anyhow::Result<()> {
async fn mock_consensus_can_send_a_sequence_of_empty_blocks() -> anyhow::Result<()> {
// Install a test logger, and acquire some temporary storage.
let guard = common::set_tracing_subscriber();
let storage = TempStorage::new().await?;
Expand All @@ -56,12 +63,30 @@ async fn mock_consensus_can_send_a_single_empty_block() -> anyhow::Result<()> {
.await?
};

engine
.block()
.with_data(vec![])
.with_evidence(List::new(Vec::new()))
.execute()
.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.
engine
.block()
.with_data(vec![])
.with_evidence(List::new(Vec::new()))
.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"
);
}

// Free our temporary storage.
drop(storage);
Expand Down
53 changes: 34 additions & 19 deletions crates/test/mock-consensus/src/block.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//! [`Builder`] facilities for constructing [`Block`]s.
//!
/// Builders are acquired by calling [`TestNode::block()`].
//! Builders are acquired by calling [`TestNode::block()`].
use {
crate::TestNode,
anyhow::bail,
tap::Tap,
tendermint::{
account,
block::{header::Version, Block, Commit, Header, Height},
block::{self, header::Version, Block, Commit, Header, Round},
chain, evidence,
v0_37::abci::{ConsensusRequest, ConsensusResponse},
AppHash, Hash,
Expand All @@ -21,19 +22,13 @@ use {
/// These are acquired by calling [`TestNode::block()`].
pub struct Builder<'e, C> {
/// A unique reference to the test node.
//
// NB: this is currently unused, but will eventually be used to fill in header fields, etc.
#[allow(dead_code)]
test_node: &'e mut TestNode<C>,

/// Transaction data.
data: Option<Vec<Vec<u8>>>,

/// Evidence of malfeasance.
evidence: Option<evidence::List>,

/// Last commit.
last_commit: Option<Commit>,
}

impl<C> TestNode<C> {
Expand All @@ -43,11 +38,12 @@ impl<C> TestNode<C> {
test_node: self,
data: Default::default(),
evidence: Default::default(),
last_commit: Default::default(),
}
}
}

// === impl Builder ===

impl<'e, C> Builder<'e, C> {
/// Sets the data for this block.
pub fn with_data(self, data: Vec<Vec<u8>>) -> Self {
Expand All @@ -65,14 +61,6 @@ impl<'e, C> Builder<'e, C> {
}
}

/// Sets the last [`Commit`] for this block.
pub fn with_last_commit(self, last_commit: Commit) -> Self {
Self {
last_commit: Some(last_commit),
..self
}
}

// TODO(kate): add more `with_` setters for fields in the header.
// TODO(kate): set some fields using state in the test node.
}
Expand Down Expand Up @@ -119,21 +107,48 @@ where
}

/// Consumes this builder, returning its [`TestNode`] reference and a [`Block`].
#[instrument(
level = "info"
skip(self),
fields(height),
)]
fn finish(self) -> Result<(&'e mut TestNode<C>, Block), anyhow::Error> {
tracing::trace!("building block");
let Self {
data: Some(data),
evidence: Some(evidence),
last_commit,
test_node,
} = self
else {
bail!("builder was not fully initialized")
};

let height = {
let height = test_node.height.increment();
test_node.height = height.clone();
tracing::Span::current().record("height", height.value());
height
};

let last_commit = if height.value() != 1 {
let block_id = block::Id {
hash: Hash::None,
part_set_header: block::parts::Header::new(0, Hash::None)?,
};
Some(Commit {
height,
round: Round::default(),
block_id,
signatures: Vec::default(),
})
} else {
None // The first block has no previous commit to speak of.
};

let header = Header {
version: Version { block: 1, app: 1 },
chain_id: chain::Id::try_from("test".to_owned())?,
height: Height::try_from(1_u8)?,
height,
time: tendermint::Time::now(),
last_block_id: None,
last_commit_hash: None,
Expand Down
1 change: 1 addition & 0 deletions crates/test/mock-consensus/src/builder/init_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl Builder {

Ok(TestNode {
consensus,
height: block::Height::from(0_u8),
last_app_hash: app_hash.as_bytes().to_owned(),
})
}
Expand Down
1 change: 1 addition & 0 deletions crates/test/mock-consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod block;
pub struct TestNode<C> {
consensus: C,
last_app_hash: Vec<u8>,
height: tendermint::block::Height,
}

impl<C> TestNode<C> {
Expand Down

0 comments on commit d08d8f2

Please sign in to comment.