Skip to content

Commit

Permalink
Add test test_multiple_consensus_blocks_derive_same_domain_block
Browse files Browse the repository at this point in the history
Signed-off-by: linning <[email protected]>
  • Loading branch information
NingLin-P committed Aug 24, 2023
1 parent a8443f3 commit 4ee14fe
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions domains/client/domain-operator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ domain-client-message-relayer = { version = "0.1.0", path = "../relayer" }
domain-test-service = { version = "0.1.0", path = "../../test/service" }
domain-test-primitives = { version = "0.1.0", path = "../../test/primitives" }
evm-domain-test-runtime = { version = "0.1.0", path = "../../test/runtime/evm" }
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/substrate", rev = "55c157cff49b638a59d81a9f971f0f9a66829c71" }
num-traits = "0.2.15"
pallet-balances = { version = "4.0.0-dev", git = "https://github.com/subspace/substrate", rev = "55c157cff49b638a59d81a9f971f0f9a66829c71" }
pallet-domains = { version = "0.1.0", path = "../../../crates/pallet-domains" }
Expand Down
96 changes: 95 additions & 1 deletion domains/client/domain-operator/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ use sp_domains::fraud_proof::{ExecutionPhase, FraudProof, InvalidStateTransition
use sp_domains::transaction::InvalidTransactionCode;
use sp_domains::{Bundle, DomainId, DomainsApi};
use sp_runtime::generic::{BlockId, Digest, DigestItem};
use sp_runtime::traits::{BlakeTwo256, Header as HeaderT};
use sp_runtime::traits::{BlakeTwo256, Block as BlockT, Header as HeaderT};
use sp_runtime::OpaqueExtrinsic;
use subspace_fraud_proof::invalid_state_transition_proof::ExecutionProver;
use subspace_runtime_primitives::opaque::Block as CBlock;
use subspace_runtime_primitives::Balance;
use subspace_test_service::{
produce_block_with, produce_blocks, produce_blocks_until, MockConsensusNode,
Expand Down Expand Up @@ -1816,3 +1817,96 @@ async fn test_restart_domain_operator() {
assert_eq!(ferdie.client.info().best_number, 11);
assert_eq!(alice.client.info().best_number, 10);
}

#[substrate_test_utils::test(flavor = "multi_thread")]
async fn test_multiple_consensus_blocks_derive_same_domain_block() {
let directory = TempDir::new().expect("Must be able to create temporary directory");

let mut builder = sc_cli::LoggerBuilder::new("");
builder.with_colors(false);
let _ = builder.init();

let tokio_handle = tokio::runtime::Handle::current();

// Start Ferdie
let mut ferdie = MockConsensusNode::run(
tokio_handle.clone(),
Ferdie,
BasePath::new(directory.path().join("ferdie")),
);

// Produce 1 consensus block to initialize genesis domain
ferdie.produce_block_with_slot(1.into()).await.unwrap();

// Run Alice (a evm domain authority node)
let mut alice = domain_test_service::DomainNodeBuilder::new(
tokio_handle.clone(),
Alice,
BasePath::new(directory.path().join("alice")),
)
.build_evm_node(Role::Authority, GENESIS_DOMAIN_ID, &mut ferdie)
.await;

produce_blocks!(ferdie, alice, 3).await.unwrap();
let common_block_hash = ferdie.client.info().best_hash;
let bundle_to_tx = |opaque_bundle| {
subspace_test_runtime::UncheckedExtrinsic::new_unsigned(
pallet_domains::Call::submit_bundle { opaque_bundle }.into(),
)
.into()
};

// Fork A
let (slot, bundle) = ferdie.produce_slot_and_wait_for_bundle_submission().await;
// Include one more extrinsic in fork A such that we can have a different consensus block
let remark_tx = subspace_test_runtime::UncheckedExtrinsic::new_unsigned(
frame_system::Call::remark { remark: vec![0; 8] }.into(),
)
.into();
let consensus_block_hash_fork_a = ferdie
.produce_block_with_slot_at(
slot,
common_block_hash,
Some(vec![bundle_to_tx(bundle.clone().unwrap()), remark_tx]),
)
.await
.unwrap();

// Fork B
let consensus_block_hash_fork_b = ferdie
.produce_block_with_slot_at(
slot,
common_block_hash,
Some(vec![bundle_to_tx(bundle.unwrap())]),
)
.await
.unwrap();

// The same domain block mapped to 2 different consensus blocks
let consensus_best_hashes = crate::aux_schema::consensus_block_hash_for::<
_,
_,
<CBlock as BlockT>::Hash,
>(&*alice.client, alice.client.info().best_hash)
.unwrap();
assert_eq!(
consensus_best_hashes,
vec![consensus_block_hash_fork_a, consensus_block_hash_fork_b]
);
assert_ne!(consensus_block_hash_fork_a, consensus_block_hash_fork_b);

// Produce one more block at fork A to make it the canonical chain and the operator
// should submit the ER of fork A
let (slot, bundle) = ferdie.produce_slot_and_wait_for_bundle_submission().await;
ferdie
.produce_block_with_slot_at(slot, consensus_block_hash_fork_a, None)
.await
.unwrap();
assert_eq!(
bundle.unwrap().into_receipt().consensus_block_hash,
consensus_block_hash_fork_a
);

// Simply produce more block
produce_blocks!(ferdie, alice, 3).await.unwrap();
}

0 comments on commit 4ee14fe

Please sign in to comment.