Skip to content

Commit

Permalink
refactor(consensus): add test util to more cleanly create validator ids
Browse files Browse the repository at this point in the history
  • Loading branch information
matan-starkware committed Jun 23, 2024
1 parent 3675c53 commit dd51d0a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ use starknet_types_core::felt::Felt;
use tokio;

use super::SingleHeightConsensus;
use crate::test_utils::{MockTestContext, TestBlock};
use crate::types::{ConsensusBlock, ProposalInit, ValidatorId};
use crate::test_utils::{validator_id, MockTestContext, TestBlock};
use crate::types::{ConsensusBlock, ProposalInit};

#[tokio::test]
async fn proposer() {
let mut context = MockTestContext::new();

let node_id: ValidatorId = 1_u32.into();
let node_id = validator_id(1);
let block = TestBlock { content: vec![1, 2, 3], id: BlockHash(Felt::ONE) };
// Set expectations for how the test should run:
context
.expect_validators()
.returning(move |_| vec![node_id, 2_u32.into(), 3_u32.into(), 4_u32.into()]);
.returning(move |_| vec![node_id, validator_id(2), validator_id(3), validator_id(4)]);
context.expect_proposer().returning(move |_, _| node_id);
let block_clone = block.clone();
context.expect_build_proposal().returning(move |_| {
Expand Down Expand Up @@ -52,14 +52,14 @@ async fn proposer() {
async fn validator() {
let mut context = MockTestContext::new();

let node_id: ValidatorId = 1_u32.into();
let proposer: ValidatorId = 2_u32.into();
let node_id = validator_id(1);
let proposer = validator_id(2);
let block = TestBlock { content: vec![1, 2, 3], id: BlockHash(Felt::ONE) };

// Set expectations for how the test should run:
context
.expect_validators()
.returning(move |_| vec![node_id, proposer, 3_u32.into(), 4_u32.into()]);
.returning(move |_| vec![node_id, proposer, validator_id(3), validator_id(4)]);
context.expect_proposer().returning(move |_, _| proposer);
let block_clone = block.clone();
context.expect_validate_proposal().returning(move |_, _| {
Expand Down
7 changes: 7 additions & 0 deletions crates/sequencing/papyrus_consensus/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use async_trait::async_trait;
use futures::channel::{mpsc, oneshot};
use mockall::mock;
use starknet_api::block::{BlockHash, BlockNumber};
use starknet_api::core::ContractAddress;

use crate::types::{ConsensusBlock, ConsensusContext, ConsensusError, ProposalInit, ValidatorId};

Expand Down Expand Up @@ -56,3 +57,9 @@ mock! {
) -> Result<(), ConsensusError>;
}
}

// Convenience function for creating a `ValidatorId` since i32 is the default type for explicit
// numbers.
pub fn validator_id(id: u32) -> ValidatorId {
ContractAddress::from(id)
}

0 comments on commit dd51d0a

Please sign in to comment.