From 2f1412ad0f086c0c83a5bb9b38130832af23cbfd Mon Sep 17 00:00:00 2001 From: Andrei Sandu <54316454+sandreim@users.noreply.github.com> Date: Mon, 8 Apr 2024 15:29:35 +0300 Subject: [PATCH] adder-collator: add velocity measurement and make elastic scaling test more robust (#4016) Improves `adder-collator` to also compute the parachain velocity. The velocity is defined as number of parachain blocks progressing per relay chain block. In this test we're asserting that the elastic parachain always progresses by 3 blocks per RCB, while the non-elastic parachain progresses normally - 1 block per RCB. --------- Signed-off-by: Andrei Sandu Co-authored-by: ordian --- .../test-parachains/adder/collator/src/lib.rs | 43 +++++++++++++++++++ .../0001-basic-3cores-6s-blocks.zndsl | 16 ++++--- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/polkadot/parachain/test-parachains/adder/collator/src/lib.rs b/polkadot/parachain/test-parachains/adder/collator/src/lib.rs index 5f62ae34caefa..c2ba93f389b03 100644 --- a/polkadot/parachain/test-parachains/adder/collator/src/lib.rs +++ b/polkadot/parachain/test-parachains/adder/collator/src/lib.rs @@ -102,11 +102,47 @@ impl State { } } +/// Local collator state so we can compute how fast we are advancing +/// per relay parent. +#[derive(Default)] +pub struct LocalCollatorState { + /// First relay block number on which we've built on. + first_relay_parent: Option, + /// Last relay block number on which we've built on. + last_relay_parent: Option, +} + +impl LocalCollatorState { + fn advance(&mut self, new_relay_parent: u32, best_block: u64) { + match (self.first_relay_parent, self.last_relay_parent) { + (Some(first_relay_parent), Some(last_relay_parent)) => { + // Compute the parachain velocity when relay parent changes vs our last + // recorded relay parent. We do this to only print out the velocity + // once per relay parent. + if new_relay_parent > last_relay_parent { + let building_for = (new_relay_parent - first_relay_parent) as f32; + // Round it up, as we don't expect perfect runs in CI. + let velocity = (best_block as f32 / building_for).ceil() as u32; + + log::info!("Parachain velocity: {:}", velocity); + } + }, + _ => {}, + } + + if self.first_relay_parent.is_none() { + self.first_relay_parent = Some(new_relay_parent); + } + self.last_relay_parent = Some(new_relay_parent); + } +} + /// The collator of the adder parachain. pub struct Collator { state: Arc>, key: CollatorPair, seconded_collations: Arc, + collator_state: Arc>, } impl Collator { @@ -116,6 +152,7 @@ impl Collator { state: Arc::new(Mutex::new(State::genesis())), key: CollatorPair::generate().0, seconded_collations: Arc::new(AtomicU32::new(0)), + collator_state: Default::default(), } } @@ -156,12 +193,18 @@ impl Collator { use futures::FutureExt as _; let state = self.state.clone(); + let collator_state = self.collator_state.clone(); let seconded_collations = self.seconded_collations.clone(); Box::new(move |relay_parent, validation_data| { let parent = HeadData::decode(&mut &validation_data.parent_head.0[..]) .expect("Decodes parent head"); + collator_state + .lock() + .unwrap() + .advance(validation_data.relay_parent_number, parent.number); + let (block_data, head_data) = state.lock().unwrap().advance(parent); log::info!( diff --git a/polkadot/zombienet_tests/elastic_scaling/0001-basic-3cores-6s-blocks.zndsl b/polkadot/zombienet_tests/elastic_scaling/0001-basic-3cores-6s-blocks.zndsl index 6ec906e58df8a..b9c0024575497 100644 --- a/polkadot/zombienet_tests/elastic_scaling/0001-basic-3cores-6s-blocks.zndsl +++ b/polkadot/zombienet_tests/elastic_scaling/0001-basic-3cores-6s-blocks.zndsl @@ -14,11 +14,15 @@ elastic-validator-4: reports node_roles is 4 elastic-validator-0: js-script ./assign-core.js with "2000,0" return is 0 within 600 seconds elastic-validator-0: js-script ./assign-core.js with "2000,1" return is 0 within 600 seconds -# Wait for 10 relay chain blocks -elastic-validator-0: reports substrate_block_height{status="best"} is at least 20 within 120 seconds +# Wait for 20 relay chain blocks +elastic-validator-0: reports substrate_block_height{status="best"} is at least 20 within 600 seconds -# Parachain should progress with 3 blocks per relay chain block, so it's reasonable to expect state to be -# at least 50, assuming some tolerance -some-parachain: log line contains "BlockData { state: 50, add: 2 }" within 10 seconds -some-parachain-1: count of log lines containing "BlockData { state: 24, add: 2 }" is 0 within 10 seconds +# Non elastic parachain should progress normally +some-parachain-1: count of log lines containing "Parachain velocity: 1" is at least 9 within 20 seconds +# Sanity +some-parachain-1: count of log lines containing "Parachain velocity: 2" is 0 within 20 seconds + +# Parachain should progress 3 blocks per relay chain block ideally, however this measurement does +# `ceil()` on the actual velocity to account for CI overload. +some-parachain: count of log lines containing "Parachain velocity: 3" is at least 9 within 20 seconds