Skip to content

Commit

Permalink
adder-collator: add velocity measurement and make elastic scaling tes…
Browse files Browse the repository at this point in the history
…t more robust (paritytech#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 <andrei-mihail@parity.io>
Co-authored-by: ordian <write@reusable.software>
  • Loading branch information
sandreim and ordian authored Apr 8, 2024
1 parent c1063a5 commit 039d183
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
43 changes: 43 additions & 0 deletions polkadot/parachain/test-parachains/adder/collator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32>,
/// Last relay block number on which we've built on.
last_relay_parent: Option<u32>,
}

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<Mutex<State>>,
key: CollatorPair,
seconded_collations: Arc<AtomicU32>,
collator_state: Arc<Mutex<LocalCollatorState>>,
}

impl Collator {
Expand All @@ -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(),
}
}

Expand Down Expand Up @@ -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!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 039d183

Please sign in to comment.