forked from diem/diem
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[decoupled-execution] Integration Part 4 - More tests
Add unit tests for ordering_state_computer and execution_phase. Closes: diem#8784
- Loading branch information
1 parent
9b36530
commit 516b1d9
Showing
7 changed files
with
235 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,73 @@ | ||
// Copyright (c) The Diem Core Contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Placeholder | ||
use crate::experimental::{ | ||
execution_phase::ExecutionPhase, tests::ordering_state_computer_tests::random_empty_block, | ||
}; | ||
use consensus_types::{block::Block, executed_block::ExecutedBlock}; | ||
use diem_types::ledger_info::{LedgerInfo, LedgerInfoWithSignatures}; | ||
|
||
use crate::test_utils::{consensus_runtime, timed_block_on, RandomComputeResultStateComputer}; | ||
use consensus_types::block::block_test_utils::certificate_for_genesis; | ||
use diem_crypto::{ed25519::Ed25519Signature, hash::ACCUMULATOR_PLACEHOLDER_HASH}; | ||
use diem_types::{account_address::AccountAddress, validator_verifier::random_validator_verifier}; | ||
use executor_types::StateComputeResult; | ||
use futures::{SinkExt, StreamExt}; | ||
use std::{collections::BTreeMap, sync::Arc}; | ||
|
||
#[test] | ||
fn test_execution_phase() { | ||
let num_nodes = 1; | ||
let channel_size = 30; | ||
let mut runtime = consensus_runtime(); | ||
|
||
let (mut execution_phase_tx, execution_phase_rx) = | ||
channel::new_test::<(Vec<Block>, LedgerInfoWithSignatures)>(channel_size); | ||
|
||
let (commit_phase_tx, mut commit_phase_rx) = | ||
channel::new_test::<(Vec<ExecutedBlock>, LedgerInfoWithSignatures)>(channel_size); | ||
|
||
let random_state_computer = RandomComputeResultStateComputer::new(); | ||
let random_execute_result_root_hash = random_state_computer.get_root_hash(); | ||
|
||
let execution_phase = ExecutionPhase::new( | ||
execution_phase_rx, | ||
Arc::new(random_state_computer), | ||
commit_phase_tx, | ||
); | ||
|
||
runtime.spawn(execution_phase.start()); | ||
|
||
let (signers, _) = random_validator_verifier(num_nodes, None, false); | ||
let signer = &signers[0]; | ||
let genesis_qc = certificate_for_genesis(); | ||
let block = random_empty_block(signer, genesis_qc); | ||
|
||
let dummy_state_compute_result = StateComputeResult::new_dummy(); | ||
|
||
let li = LedgerInfo::new( | ||
block.gen_block_info( | ||
dummy_state_compute_result.root_hash(), | ||
dummy_state_compute_result.version(), | ||
dummy_state_compute_result.epoch_state().clone(), | ||
), | ||
*ACCUMULATOR_PLACEHOLDER_HASH, | ||
); | ||
|
||
let li_sig = | ||
LedgerInfoWithSignatures::new(li, BTreeMap::<AccountAddress, Ed25519Signature>::new()); | ||
|
||
let blocks = vec![block.clone()]; | ||
|
||
timed_block_on(&mut runtime, async move { | ||
execution_phase_tx.send((blocks, li_sig.clone())).await.ok(); | ||
let (executed_blocks, executed_finality_proof) = commit_phase_rx.next().await.unwrap(); | ||
assert_eq!(executed_blocks.len(), 1); | ||
assert_eq!( | ||
executed_blocks[0].compute_result(), | ||
&StateComputeResult::new_dummy_with_root_hash(random_execute_result_root_hash) | ||
); | ||
assert_eq!(executed_blocks[0].block(), &block); | ||
assert_eq!(executed_finality_proof, li_sig); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 86 additions & 1 deletion
87
consensus/src/experimental/tests/ordering_state_computer_tests.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,89 @@ | ||
// Copyright (c) The Diem Core Contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Placeholder | ||
use crate::{ | ||
experimental::ordering_state_computer::OrderingStateComputer, state_replication::StateComputer, | ||
}; | ||
use channel::Receiver; | ||
use consensus_types::block::{block_test_utils::certificate_for_genesis, Block}; | ||
use diem_crypto::hash::ACCUMULATOR_PLACEHOLDER_HASH; | ||
use diem_types::{ | ||
ledger_info::{LedgerInfo, LedgerInfoWithSignatures}, | ||
validator_verifier::random_validator_verifier, | ||
}; | ||
use executor_types::StateComputeResult; | ||
use std::sync::Arc; | ||
|
||
use crate::test_utils::{consensus_runtime, timed_block_on}; | ||
use consensus_types::{executed_block::ExecutedBlock, quorum_cert::QuorumCert}; | ||
use diem_crypto::ed25519::Ed25519Signature; | ||
use diem_types::{account_address::AccountAddress, validator_signer::ValidatorSigner}; | ||
use futures::StreamExt; | ||
use rand::Rng; | ||
use std::collections::BTreeMap; | ||
|
||
pub fn prepare_ordering_state_computer( | ||
channel_size: usize, | ||
) -> ( | ||
Arc<OrderingStateComputer>, | ||
Receiver<(Vec<Block>, LedgerInfoWithSignatures)>, | ||
) { | ||
let (commit_result_tx, commit_result_rx) = | ||
channel::new_test::<(Vec<Block>, LedgerInfoWithSignatures)>(channel_size); | ||
let state_computer = Arc::new(OrderingStateComputer::new(commit_result_tx)); | ||
|
||
(state_computer, commit_result_rx) | ||
} | ||
|
||
pub fn random_empty_block(signer: &ValidatorSigner, qc: QuorumCert) -> Block { | ||
let mut rng = rand::thread_rng(); | ||
Block::new_proposal(vec![], rng.gen::<u64>(), rng.gen::<u64>(), qc, signer) | ||
} | ||
|
||
#[test] | ||
fn test_ordering_state_computer() { | ||
let num_nodes = 1; | ||
let channel_size = 30; | ||
let mut runtime = consensus_runtime(); | ||
|
||
let (state_computer, mut commit_result_rx) = prepare_ordering_state_computer(channel_size); | ||
|
||
let (signers, _) = random_validator_verifier(num_nodes, None, false); | ||
let signer = &signers[0]; | ||
let genesis_qc = certificate_for_genesis(); | ||
let block = random_empty_block(signer, genesis_qc); | ||
|
||
// test compute | ||
let dummy_state_compute_result = state_computer | ||
.compute(&block, *ACCUMULATOR_PLACEHOLDER_HASH) | ||
.unwrap(); | ||
assert_eq!(dummy_state_compute_result, StateComputeResult::new_dummy()); | ||
|
||
// test commit | ||
let li = LedgerInfo::new( | ||
block.gen_block_info( | ||
dummy_state_compute_result.root_hash(), | ||
dummy_state_compute_result.version(), | ||
dummy_state_compute_result.epoch_state().clone(), | ||
), | ||
*ACCUMULATOR_PLACEHOLDER_HASH, | ||
); | ||
|
||
let blocks = vec![Arc::new(ExecutedBlock::new( | ||
block.clone(), | ||
dummy_state_compute_result, | ||
))]; | ||
|
||
let li_sig = | ||
LedgerInfoWithSignatures::new(li, BTreeMap::<AccountAddress, Ed25519Signature>::new()); | ||
|
||
// ordering_state_computer should send the same block and finality proof to the channel | ||
timed_block_on(&mut runtime, async move { | ||
state_computer.commit(&blocks, li_sig.clone()).await.ok(); | ||
|
||
let (ordered_block, finality_proof) = commit_result_rx.next().await.unwrap(); | ||
assert_eq!(ordered_block.len(), 1); | ||
assert_eq!(ordered_block[0], block); | ||
assert_eq!(finality_proof, li_sig); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters