Skip to content

Commit

Permalink
Dynamic consensus state ids (#60)
Browse files Browse the repository at this point in the history
* add dynamic consensus state id

* clippy fix

* configurable challenge and unbonding periods

* lock toolchain

* use bytes for grandpa state machine representation

* prevent creation of duplicate consensus state ids

* static consensus state id

* refactor

* cargo fmt

---------

Co-authored-by: Seun Lanlege <seunlanlege@gmail.com>
  • Loading branch information
Wizdave97 and seunlanlege authored Jul 6, 2023
1 parent 3e2880d commit 90344e8
Show file tree
Hide file tree
Showing 11 changed files with 324 additions and 149 deletions.
13 changes: 7 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Install toolchain
uses: dtolnay/rust-toolchain@nightly
with:
toolchain: nightly
toolchain: nightly-2022-10-28
targets: wasm32-unknown-unknown

- name: Rust cache
Expand All @@ -32,13 +32,13 @@ jobs:
cache-on-failure: true

- name: Build
run: cargo +nightly check --all --benches --workspace
run: cargo +nightly-2022-10-28 check --all --benches --workspace

- name: Build `no-std`
run: cargo +nightly build -p ismp --no-default-features --verbose --target=wasm32-unknown-unknown
run: cargo +nightly-2022-10-28 build -p ismp --no-default-features --verbose --target=wasm32-unknown-unknown

- name: Run tests
run: cargo +nightly test --all-features --workspace --verbose
run: cargo +nightly-2022-10-28 test --all-features --workspace --verbose

lint:
runs-on: ubuntu-latest
Expand All @@ -52,6 +52,7 @@ jobs:
- name: Install toolchain
uses: dtolnay/rust-toolchain@nightly
with:
toolchain: nightly-2022-10-28
components: rustfmt, clippy

- name: Rust cache
Expand All @@ -60,7 +61,7 @@ jobs:
cache-on-failure: true

- name: Check format
run: cargo +nightly fmt --all --check
run: cargo +nightly-2022-10-28 fmt --all --check

- name: Check clippy
run: cargo +nightly clippy --all-targets --workspace --all-features --verbose -- -D warnings
run: cargo +nightly-2022-10-28 clippy --all-targets --workspace --all-features --verbose -- -D warnings
39 changes: 20 additions & 19 deletions Cargo.lock

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

38 changes: 22 additions & 16 deletions ismp-testsuite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ mod tests;

use crate::mocks::MOCK_CONSENSUS_CLIENT_ID;
use ismp::{
consensus::{IntermediateState, StateCommitment, StateMachineHeight, StateMachineId},
consensus::{
ConsensusStateId, IntermediateState, StateCommitment, StateMachineHeight, StateMachineId,
},
handlers::handle_incoming_message,
host::{IsmpHost, StateMachine},
host::{Ethereum, IsmpHost, StateMachine},
messaging::{
ConsensusMessage, Message, Proof, RequestMessage, ResponseMessage, TimeoutMessage,
},
Expand All @@ -33,12 +35,16 @@ use ismp::{
util::hash_request,
};

fn mock_consensus_state_id() -> ConsensusStateId {
*b"mock"
}

fn setup_mock_client<H: IsmpHost>(host: &H) -> IntermediateState {
let intermediate_state = IntermediateState {
height: StateMachineHeight {
id: StateMachineId {
state_id: StateMachine::Ethereum,
consensus_client: MOCK_CONSENSUS_CLIENT_ID,
state_id: StateMachine::Ethereum(Ethereum::ExecutionLayer),
consensus_state_id: mock_consensus_state_id(),
},
height: 1,
},
Expand All @@ -49,7 +55,8 @@ fn setup_mock_client<H: IsmpHost>(host: &H) -> IntermediateState {
},
};

host.store_consensus_state(MOCK_CONSENSUS_CLIENT_ID, vec![]).unwrap();
host.store_consensus_state(mock_consensus_state_id(), vec![]).unwrap();
host.store_consensus_state_id(mock_consensus_state_id(), MOCK_CONSENSUS_CLIENT_ID).unwrap();
host.store_state_machine_commitment(intermediate_state.height, intermediate_state.commitment)
.unwrap();

Expand All @@ -63,13 +70,13 @@ fn setup_mock_client<H: IsmpHost>(host: &H) -> IntermediateState {
pub fn check_challenge_period<H: IsmpHost>(host: &H) -> Result<(), &'static str> {
let consensus_message = Message::Consensus(ConsensusMessage {
consensus_proof: vec![],
consensus_client_id: MOCK_CONSENSUS_CLIENT_ID,
consensus_state_id: mock_consensus_state_id(),
});
let intermediate_state = setup_mock_client(host);
// Set the previous update time
let challenge_period = host.challenge_period(MOCK_CONSENSUS_CLIENT_ID);
let challenge_period = host.challenge_period(mock_consensus_state_id()).unwrap();
let previous_update_time = host.timestamp() - (challenge_period / 2);
host.store_consensus_update_time(MOCK_CONSENSUS_CLIENT_ID, previous_update_time).unwrap();
host.store_consensus_update_time(mock_consensus_state_id(), previous_update_time).unwrap();

let res = handle_incoming_message::<H>(host, consensus_message);
assert!(matches!(res, Err(ismp::error::Error::ChallengePeriodNotElapsed { .. })));
Expand Down Expand Up @@ -118,14 +125,13 @@ pub fn check_challenge_period<H: IsmpHost>(host: &H) -> Result<(), &'static str>
pub fn check_client_expiry<H: IsmpHost>(host: &H) -> Result<(), &'static str> {
let consensus_message = Message::Consensus(ConsensusMessage {
consensus_proof: vec![],
consensus_client_id: MOCK_CONSENSUS_CLIENT_ID,
consensus_state_id: mock_consensus_state_id(),
});
setup_mock_client(host);
// Set the previous update time
let client = host.consensus_client(MOCK_CONSENSUS_CLIENT_ID).unwrap();
let unbonding_period = client.unbonding_period();
let unbonding_period = host.unbonding_period(mock_consensus_state_id()).unwrap();
let previous_update_time = host.timestamp() - unbonding_period;
host.store_consensus_update_time(MOCK_CONSENSUS_CLIENT_ID, previous_update_time).unwrap();
host.store_consensus_update_time(mock_consensus_state_id(), previous_update_time).unwrap();

let res = handle_incoming_message::<H>(host, consensus_message);
assert!(matches!(res, Err(ismp::error::Error::UnbondingPeriodElapsed { .. })));
Expand All @@ -137,9 +143,9 @@ pub fn check_client_expiry<H: IsmpHost>(host: &H) -> Result<(), &'static str> {
pub fn frozen_check<H: IsmpHost>(host: &H) -> Result<(), &'static str> {
let intermediate_state = setup_mock_client(host);
// Set the previous update time
let challenge_period = host.challenge_period(MOCK_CONSENSUS_CLIENT_ID);
let challenge_period = host.challenge_period(mock_consensus_state_id()).unwrap();
let previous_update_time = host.timestamp() - (challenge_period * 2);
host.store_consensus_update_time(MOCK_CONSENSUS_CLIENT_ID, previous_update_time).unwrap();
host.store_consensus_update_time(mock_consensus_state_id(), previous_update_time).unwrap();

let frozen_height = StateMachineHeight {
id: intermediate_state.height.id,
Expand Down Expand Up @@ -194,9 +200,9 @@ pub fn timeout_post_processing_check<H: IsmpHost>(
dispatcher: &dyn IsmpDispatcher,
) -> Result<(), &'static str> {
let intermediate_state = setup_mock_client(host);
let challenge_period = host.challenge_period(MOCK_CONSENSUS_CLIENT_ID);
let challenge_period = host.challenge_period(mock_consensus_state_id()).unwrap();
let previous_update_time = host.timestamp() - (challenge_period * 2);
host.store_consensus_update_time(MOCK_CONSENSUS_CLIENT_ID, previous_update_time).unwrap();
host.store_consensus_update_time(mock_consensus_state_id(), previous_update_time).unwrap();
let dispatch_post = DispatchPost {
dest_chain: StateMachine::Kusama(2000),
from: vec![0u8; 32],
Expand Down
Loading

0 comments on commit 90344e8

Please sign in to comment.