Skip to content

Commit

Permalink
feat: dummy light client (#25)
Browse files Browse the repository at this point in the history
* add dummy client

* cargo fmt

* format cargo.toml

* rm dummy light client

* rm workspace member cargo lockfile

* update starknet light client

* update cargo lockfile

* rm dummy light client readme

* fix serde feature

* use unknown client and consensus state type error

* use const type url

* build cw-contract on ci

* make type url consts pub

* setup rust toolchain with wasm32 target on ci

* fix re-export error

* use type urls

* update type urls
  • Loading branch information
rnbguy authored Jul 30, 2024
1 parent b2d1e16 commit 9fc4f1b
Show file tree
Hide file tree
Showing 16 changed files with 297 additions and 71 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Rust
on:
pull_request:
paths:
- .github/workflows/rust.yaml
- contracts/**
- light-client/**
- "**/Cargo.toml"
- "**/Cargo.lock"
- justfile
- README.md

push:
tags:
- v[0-9]+.*
branches:
- "release/*"
- main

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ !startsWith(github.ref, 'refs/tags/') && github.ref != 'refs/heads/main' }}

env:
CARGO_INCREMENTAL: 0
CARGO_PROFILE_DEV_DEBUG: 1
CARGO_PROFILE_RELEASE_DEBUG: 1
CARGO_NET_RETRY: 10
RUSTUP_MAX_RETRIES: 10

jobs:
build-cw-contract:
name: Build CosmWasm Light Client Contract
runs-on: ubuntu-latest
timeout-minutes: 60
steps:
- uses: actions/checkout@v4
- name: Install Rust toolchains
uses: actions-rust-lang/setup-rust-toolchain@v1
with:
target: wasm32-unknown-unknown
- name: Install Just
uses: extractions/setup-just@v1
- name: Build CosmWasm light client contract
run: just build-cw-contract
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.DS_Store
corelib/
target/
**/Cargo.lock

# vscode
.vscode/
.vscode/
6 changes: 6 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ repository = "https://github.com/informalsystems/ibc-starknet"
[workspace.lints.rust]
unsafe_code = "forbid"
dead_code = "deny"
rust_2018_idioms = "deny"
rust_2018_idioms = { level = "deny", priority = -1 }
trivial_casts = "deny"
unused_import_braces = "deny"
unused_variables = "allow"
Expand All @@ -34,6 +34,7 @@ uninlined_format_args = "deny"
[workspace.dependencies]
# external dependencies
derive_more = { version = "0.99.11", features = [ "from", "try_into" ] }
serde = { version = "1.0.204", features = [ "derive" ] }

# ibc depedenencies
ibc-core = { version = "0.53.0", default-features = false, features = [ "borsh", "schema" ] }
Expand Down
3 changes: 3 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ install-tools:
rustup component add rustfmt --toolchain nightly
cargo install typos-cli taplo-cli

build-cw-contract:
cd ./light-client/cw-contract && cargo build --target wasm32-unknown-unknown

# Runs formatter and clippy for all the cargo and scarb packages
lint:
@cargo +nightly fmt --all -- --check && \
Expand Down
19 changes: 9 additions & 10 deletions light-client/cw-contract/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
[package]
name = "ibc-client-starknet-cw"
authors = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
repository = { workspace = true }
name = "ibc-client-starknet-cw"
authors = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
repository = { workspace = true }
rust-version = { workspace = true }
version = { workspace = true }
readme = "./../README.md"
keywords = [ "ibc", "light-client", "starknet", "CosmWasm" ]
description = """
"""
version = { workspace = true }
readme = "./../README.md"
keywords = [ "ibc", "light-client", "starknet", "CosmWasm" ]
description = ""

[lib]
crate-type = [ "cdylib", "rlib" ]
Expand Down
14 changes: 0 additions & 14 deletions light-client/impls/Cargo.lock

This file was deleted.

25 changes: 15 additions & 10 deletions light-client/impls/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
[package]
name = "ibc-client-starknet"
authors = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
name = "ibc-client-starknet"
authors = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
rust-version = { workspace = true }
version = { workspace = true }
repository = { workspace = true }
readme = "./../README.md"
keywords = [ "starknet", "ibc", "light-client" ]
description = """
"""
version = { workspace = true }
repository = { workspace = true }
readme = "./../README.md"
keywords = [ "starknet", "ibc", "light-client" ]
description = ""

[lints]
workspace = true

[dependencies]
# external dependencies
derive_more = { workspace = true }
serde = { workspace = true, optional = true }

# ibc dependencies
ibc-core = { workspace = true }
Expand All @@ -28,3 +28,8 @@ std = [
"ibc-core/std",
"ibc-client-starknet-types/std",
]
serde = [
"dep:serde",
"ibc-core/serde",
"ibc-client-starknet-types/serde",
]
18 changes: 9 additions & 9 deletions light-client/impls/src/client_state/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ use super::ClientState;

impl ClientStateCommon for ClientState {
fn verify_consensus_state(&self, consensus_state: Any) -> Result<(), ClientError> {
todo!()
Ok(())
}

fn client_type(&self) -> ClientType {
todo!()
"blind-001".parse().unwrap()
}

fn latest_height(&self) -> ibc_core::client::types::Height {
todo!()
fn latest_height(&self) -> Height {
self.0.latest_height
}

fn validate_proof_height(&self, proof_height: Height) -> Result<(), ClientError> {
todo!()
Ok(())
}

fn verify_upgrade_client(
Expand All @@ -35,11 +35,11 @@ impl ClientStateCommon for ClientState {
proof_upgrade_consensus_state: CommitmentProofBytes,
root: &CommitmentRoot,
) -> Result<(), ClientError> {
todo!()
Ok(())
}

fn serialize_path(&self, path: Path) -> Result<PathBytes, ClientError> {
todo!()
Ok(path.to_string().as_bytes().to_vec().into())
}

fn verify_membership_raw(
Expand All @@ -50,7 +50,7 @@ impl ClientStateCommon for ClientState {
path: PathBytes,
value: Vec<u8>,
) -> Result<(), ClientError> {
todo!()
Ok(())
}

fn verify_non_membership_raw(
Expand All @@ -60,6 +60,6 @@ impl ClientStateCommon for ClientState {
root: &CommitmentRoot,
path: PathBytes,
) -> Result<(), ClientError> {
todo!()
Ok(())
}
}
88 changes: 82 additions & 6 deletions light-client/impls/src/client_state/execution.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
use ibc_client_starknet_types::ClientState as ClientStateType;
use ibc_core::client::context::client_state::ClientStateExecution;
use ibc_core::client::context::prelude::ClientStateCommon;
use ibc_core::client::context::ClientExecutionContext;
use ibc_core::client::types::error::ClientError;
use ibc_core::client::types::Height;
use ibc_core::host::types::identifiers::ClientId;
use ibc_core::host::types::path::{ClientConsensusStatePath, ClientStatePath};
use ibc_core::primitives::proto::Any;
use ibc_core::primitives::Timestamp;

use super::ClientState;
use crate::ConsensusState;

impl<E> ClientStateExecution<E> for ClientState
where
E: ClientExecutionContext,
E: ClientExecutionContext<ClientStateMut = ClientState, ConsensusStateRef = ConsensusState>,
{
fn initialise(
&self,
ctx: &mut E,
client_id: &ClientId,
consensus_state: Any,
) -> Result<(), ClientError> {
todo!()
let latest_height = Height::min(0);

update_client_and_consensus_state(
ctx,
latest_height,
client_id,
self.clone(),
consensus_state.try_into()?,
)?;

Ok(())
}

fn update_state(
Expand All @@ -26,7 +41,19 @@ where
client_id: &ClientId,
header: Any,
) -> Result<Vec<Height>, ClientError> {
todo!()
let latest_height = ctx.client_state(client_id)?.latest_height().increment();

let new_client_state = ClientStateType { latest_height }.into();

update_client_and_consensus_state(
ctx,
latest_height,
client_id,
new_client_state,
ConsensusState::default(),
)?;

Ok(vec![latest_height])
}

fn update_state_on_misbehaviour(
Expand All @@ -35,7 +62,7 @@ where
client_id: &ClientId,
client_message: Any,
) -> Result<(), ClientError> {
todo!()
Ok(())
}

fn update_state_on_upgrade(
Expand All @@ -45,7 +72,17 @@ where
upgraded_client_state: Any,
upgraded_consensus_state: Any,
) -> Result<Height, ClientError> {
todo!()
let latest_height = ctx.client_state(client_id)?.latest_height().increment();

update_client_and_consensus_state(
ctx,
latest_height,
client_id,
upgraded_client_state.try_into()?,
upgraded_consensus_state.try_into()?,
)?;

Ok(latest_height)
}

fn update_on_recovery(
Expand All @@ -55,6 +92,45 @@ where
substitute_client_state: Any,
substitute_consensus_state: Any,
) -> Result<(), ClientError> {
todo!()
let latest_height = ctx
.client_state(subject_client_id)?
.latest_height()
.increment();

update_client_and_consensus_state(
ctx,
latest_height,
subject_client_id,
substitute_client_state.try_into()?,
substitute_consensus_state.try_into()?,
)?;

Ok(())
}
}

fn update_client_and_consensus_state<E: ClientExecutionContext>(
ctx: &mut E,
client_height: Height,
client_id: &ClientId,
client_state: E::ClientStateRef,
consensus_state: E::ConsensusStateRef,
) -> Result<(), ClientError> {
ctx.store_consensus_state(
ClientConsensusStatePath::new(
client_id.clone(),
client_height.revision_number(),
client_height.revision_height(),
),
consensus_state,
)?;
ctx.store_client_state(ClientStatePath::new(client_id.clone()), client_state)?;
ctx.store_update_meta(
client_id.clone(),
client_height,
Timestamp::none(),
Height::min(0),
)?;

Ok(())
}
Loading

0 comments on commit 9fc4f1b

Please sign in to comment.