Skip to content

Commit

Permalink
Add randomness to pallet-collator-assignment (#267)
Browse files Browse the repository at this point in the history
* Add randomness to pallet-collator-assignment

* Mock randomness inherent in tests

* Do not shuffle collators if seed is 0

* Merge the functions to add and fill new container chains into one

* Rotate collators according to config param

* Use per-block randomness instead of per-epoch randomness

* Dont check collator2002-01 dbpath

Because a different collator may be assigned to container 2002

* Add zombie_tanssi_rotation test suite

* Unit test collator assignment rotation

* Fix integration test by killing storage value

* Rename para-rotation suite to avoid moonwall bug

* Increase zombienet test timeout

* Mix subject with relay randomness, and fix bug all seeds starting with 1

There was a bug because we tried to read an Option<Hash> as a Hash, and
this results in the first byte of the [u8; 32] being always 1, which is
the marker for "Some"

* Fix collators rotating 1 block too early

* Add event CollatorAssignment::NewPendingAssignment

* Fix tests after 1.1.0 upgrade

* Make full_rotation_period configurable, defined in pallet_configuration

* Use fast_or_prod macro to set full_rotation_period = 5 for tests

* Store randomness from previous relay block in pallet storage

Because since #288 collator rotation is handled in on_initialize, and we
can only read the relay randomness after the set_validation_data
inherent, which happens after on_initialize.

The solution is to read the randomness on the on_finalize hook of the
previous block, store it in pallet_collator_assignment, and delete it
when reading it.

* Change para-rotation test to rotate every 5 sessions instead of every 1

Rotating every session does not leave enough time for collators to
detect their assignment, and some tests result in timeout

* Rewrite migration using get_raw and put_raw

* Rotation period of 0 disables rotation
  • Loading branch information
tmpolaczyk authored Oct 19, 2023
1 parent 93cfc2f commit 850faaf
Show file tree
Hide file tree
Showing 31 changed files with 2,159 additions and 678 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,61 @@ jobs:
name: logs
path: logs

zombienet-tests-rotation:
runs-on: self-hosted
needs: ["set-tags", "build"]
steps:
- name: Checkout
uses: actions/checkout@v3
with:
ref: ${{ needs.set-tags.outputs.git_ref }}

- name: Pnpm
uses: pnpm/action-setup@v2
with:
version: 8

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 20.x
cache: "pnpm"

- name: "Download binaries"
uses: actions/[email protected]
with:
name: binaries
path: target/release

- name: "Run zombie test"
run: |
chmod uog+x target/release/tanssi-node
chmod uog+x target/release/container-chain-template-simple-node
chmod uog+x target/release/container-chain-template-frontier-node
cd test
pnpm install
## Run tests
pnpm moonwall test zombie_tanssi_rotation
- name: "Gather zombie logs"
if: failure()
run: |
ls -ltr /tmp
latest_zombie_dir=$(find /tmp -type d -iname "*zombie*" -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" ")
logs_dir="logs"
mkdir -p "$logs_dir"
find "$latest_zombie_dir" -type f -name "*.log" -exec cp {} "$logs_dir" \;
- name: "Upload zombie logs"
if: failure()
uses: actions/[email protected]
with:
name: logs-rotation
path: logs

zombienet-tests-warp-sync:
runs-on: ubuntu-latest
needs: ["set-tags", "build"]
Expand Down
3 changes: 3 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ cumulus-client-pov-recovery = { git = "https://github.com/moondance-labs/polkado
cumulus-client-service = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.1.0", default-features = false }
cumulus-primitives-parachain-inherent = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.1.0", default-features = false }
cumulus-relay-chain-interface = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.1.0", default-features = false }
cumulus-test-relay-sproof-builder = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.1.0", default-features = false }
xcm-emulator = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.1.0", default-features = false }

# Frontier (wasm)
Expand Down Expand Up @@ -220,6 +221,7 @@ hex-literal = { version = "0.3.4" }
log = { version = "0.4.17", default-features = false }
serde = { version = "1.0.152", default-features = false }
smallvec = "1.10.0"
rand_chacha = { version = "0.3.1", default-features = false }

# General (client)
async-io = "1.3"
Expand Down
6 changes: 4 additions & 2 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
use {
cumulus_primitives_core::ParaId,
dancebox_runtime::{
AccountId, MaintenanceModeConfig, MigrationsConfig, PolkadotXcmConfig, RegistrarConfig,
Signature, SudoConfig,
prod_or_fast, AccountId, MaintenanceModeConfig, MigrationsConfig, PolkadotXcmConfig,
RegistrarConfig, Signature, SudoConfig,
},
nimbus_primitives::NimbusId,
pallet_configuration::HostConfiguration,
Expand Down Expand Up @@ -195,6 +195,7 @@ pub fn development_config(
min_orchestrator_collators: 1u32,
max_orchestrator_collators: 1u32,
collators_per_container: 2u32,
full_rotation_period: prod_or_fast!(24u32, 5u32),
},
..Default::default()
},
Expand Down Expand Up @@ -259,6 +260,7 @@ pub fn local_dancebox_config(
min_orchestrator_collators: 2u32,
max_orchestrator_collators: 5u32,
collators_per_container: 2u32,
full_rotation_period: prod_or_fast!(24u32, 5u32),
},
..Default::default()
},
Expand Down
1 change: 1 addition & 0 deletions pallets/authority-assignment/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ pub fn run_to_block(n: u64) {
let old_block_number = System::block_number();

for x in (old_block_number + 1)..=n {
System::reset_events();
System::set_block_number(x);

if x % SESSION_LEN == 1 {
Expand Down
4 changes: 2 additions & 2 deletions pallets/collator-assignment/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ sp-std = { workspace = true }
tp-collator-assignment = { workspace = true }
tp-traits = { workspace = true }
frame-benchmarking = { workspace = true }
rand = { workspace = true, optional = true }
rand = { workspace = true }
rand_chacha = { workspace = true }

[dev-dependencies]
sp-io = { workspace = true }
Expand All @@ -44,7 +45,6 @@ runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"rand",
"tp-traits/runtime-benchmarks",
]
try-runtime = [ "frame-support/try-runtime" ]
21 changes: 21 additions & 0 deletions pallets/collator-assignment/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ fn invulnerables<T: Config + frame_system::Config>(count: u32, seed: u32) -> Vec
invulnerables
}

fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
let events = frame_system::Pallet::<T>::events();
let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
// compare to the last event record
let EventRecord { event, .. } = &events[events.len() - 1];
assert_eq!(event, &system_event);
}

#[benchmarks]
mod benchmarks {
use super::*;
Expand Down Expand Up @@ -75,6 +83,9 @@ mod benchmarks {
container_chains: BTreeMap::from_iter(old_container_chains),
};
<CollatorContainerChain<T>>::put(&old_assigned);
// Do not use [0; 32] because that seed will not shuffle the list of collators
let random_seed = [1; 32];
<Randomness<T>>::put(random_seed);

#[block]
{
Expand All @@ -89,6 +100,16 @@ mod benchmarks {
0
);

// Worst case is `full_rotation: false` because it needs to check the previous assignment
assert_last_event::<T>(
Event::NewPendingAssignment {
random_seed,
full_rotation: false,
target_session: T::SessionIndex::from(1u32),
}
.into(),
);

Ok(())
}

Expand Down
Loading

0 comments on commit 850faaf

Please sign in to comment.