Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
use bytes for grandpa state machine representation
Browse files Browse the repository at this point in the history
  • Loading branch information
Wizdave97 committed Jul 5, 2023
1 parent eddf840 commit 47baba8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 31 deletions.
11 changes: 6 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.

1 change: 1 addition & 0 deletions ismp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ derive_more = { version = "0.99.17", default-features = false, features = ["from
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
serde = { version = "1.0.136", features = ["derive"], optional = true }
primitive-types = { version = "0.12.1", default-features = false, features = ["scale-info", "serde_no_std"] }
serde_json = { version = "1.0.99", default-features = false, features = ["alloc"] }

[features]
default = ["std"]
Expand Down
39 changes: 32 additions & 7 deletions ismp/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,10 @@ pub enum StateMachine {
Kusama(u32),
/// State machines running on grandpa consensus client
#[codec(index = 5)]
Grandpa(u32),
Grandpa([u8; 8]),
/// State machines chains running on beefy consensus client
#[codec(index = 6)]
Beefy(u32),
Beefy([u8; 8]),
}

impl ToString for StateMachine {
Expand All @@ -206,8 +206,14 @@ impl ToString for StateMachine {
StateMachine::Base => "BASE".to_string(),
StateMachine::Polkadot(id) => format!("POLKADOT-{id}"),
StateMachine::Kusama(id) => format!("KUSAMA-{id}"),
StateMachine::Grandpa(id) => format!("GRANDPA-{id}"),
StateMachine::Beefy(id) => format!("BEEFY-{id}"),
StateMachine::Grandpa(id) => format!(
"GRANDPA-{}",
serde_json::to_string(id).expect("Array to string is infallible")
),
StateMachine::Beefy(id) => format!(
"BEEFY-{}",
serde_json::to_string(id).expect("Array to string is infallible")
),
}
}
}
Expand Down Expand Up @@ -241,21 +247,40 @@ impl FromStr for StateMachine {
let id = name
.split('-')
.last()
.and_then(|id| u32::from_str(id).ok())
.and_then(|id| serde_json::from_str(id).ok())
.ok_or_else(|| format!("invalid state machine: {name}"))?;
StateMachine::Grandpa(id)
}
name if name.starts_with("BEEFY-") => {
let id = name
.split('-')
.last()
.and_then(|id| u32::from_str(id).ok())
.and_then(|id| serde_json::from_str(id).ok())
.ok_or_else(|| format!("invalid state machine: {name}"))?;
StateMachine::Beefy(id)
}
name => Err(format!("Unkown state machine: {name}"))?,
name => Err(format!("Unknown state machine: {name}"))?,
};

Ok(s)
}
}

#[cfg(test)]
mod tests {
use crate::host::StateMachine;
use alloc::string::ToString;
use core::str::FromStr;

#[test]
fn state_machine_conversions() {
let grandpa = StateMachine::Grandpa(*b"standard");
let beefy = StateMachine::Beefy(*b"standard");

let grandpa_string = grandpa.to_string();
let beefy_string = beefy.to_string();

assert_eq!(grandpa, StateMachine::from_str(&grandpa_string).unwrap());
assert_eq!(beefy, StateMachine::from_str(&beefy_string).unwrap());
}
}

0 comments on commit 47baba8

Please sign in to comment.