Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

imp: get CW instantiate entrypoint work for Sovereign client creation #74

Merged
merged 4 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"rust-analyzer.linkedProjects": [
"./clients/sov-celestia-cw/Cargo.toml",
"./ci/risc0-check/methods/guest/Cargo.toml",
"./ci/risc0-check/host/Cargo.toml"
"./ci/risc0-check/host/Cargo.toml",
],
"cSpell.words": [
"jsonrpsee",
Expand Down
99 changes: 50 additions & 49 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ tempfile = "3.5"
thiserror = "1.0.38"

# ibc depedenencies
ibc-core = { git = "https://github.com/cosmos/ibc-rs.git", rev = "b25d7d8868", default-features = false, features = ["borsh","schema","serde"] }
ibc-core-client = { git = "https://github.com/cosmos/ibc-rs.git", rev = "b25d7d8868", default-features = false }
ibc-core-host-cosmos = { git = "https://github.com/cosmos/ibc-rs.git", rev = "b25d7d8868", default-features = false }
ibc-client-tendermint = { git = "https://github.com/cosmos/ibc-rs.git", rev = "b25d7d8868", default-features = false }
ibc-client-wasm-types = { git = "https://github.com/cosmos/ibc-rs.git", rev = "b25d7d8868", default-features = false }
ibc-app-transfer = { git = "https://github.com/cosmos/ibc-rs.git", rev = "b25d7d8868", default-features = false }
ibc-primitives = { git = "https://github.com/cosmos/ibc-rs.git", rev = "b25d7d8868", default-features = false }
ibc-query = { git = "https://github.com/cosmos/ibc-rs.git", rev = "b25d7d8868", default-features = false }
ibc-testkit = { git = "https://github.com/cosmos/ibc-rs.git", rev = "b25d7d8868", default-features = false }
ibc-core = { git = "https://github.com/cosmos/ibc-rs.git", rev = "74c316c36c", default-features = false, features = ["borsh","schema","serde"] }
ibc-core-client = { git = "https://github.com/cosmos/ibc-rs.git", rev = "74c316c36c", default-features = false }
ibc-core-host-cosmos = { git = "https://github.com/cosmos/ibc-rs.git", rev = "74c316c36c", default-features = false }
ibc-client-tendermint = { git = "https://github.com/cosmos/ibc-rs.git", rev = "74c316c36c", default-features = false }
ibc-client-wasm-types = { git = "https://github.com/cosmos/ibc-rs.git", rev = "74c316c36c", default-features = false }
ibc-app-transfer = { git = "https://github.com/cosmos/ibc-rs.git", rev = "74c316c36c", default-features = false }
ibc-primitives = { git = "https://github.com/cosmos/ibc-rs.git", rev = "74c316c36c", default-features = false }
ibc-query = { git = "https://github.com/cosmos/ibc-rs.git", rev = "74c316c36c", default-features = false }
ibc-testkit = { git = "https://github.com/cosmos/ibc-rs.git", rev = "74c316c36c", default-features = false }
ibc-proto = { git = "https://github.com/cosmos/ibc-proto-rs.git", rev = "d1daaab", default-features = false }

# cosmos dependencies
Expand Down
214 changes: 94 additions & 120 deletions clients/sov-celestia-cw/src/contexts/client_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,63 @@ use ibc_core::client::types::error::ClientError;
use ibc_core::client::types::Height;
use ibc_core::handler::types::error::ContextError;
use ibc_core::host::types::identifiers::ClientId;
use ibc_core::host::types::path::{ClientConsensusStatePath, ClientStatePath};
use ibc_core::host::types::path::{
iteration_key, ClientConsensusStatePath, ClientStatePath, ClientUpdateHeightPath,
ClientUpdateTimePath,
};
use ibc_core::host::ValidationContext;
use ibc_core::primitives::proto::Any;
use ibc_core::primitives::Timestamp;
use sov_celestia_client::client_state::ClientState;

use super::definition::StorageMut;
use super::{Context, StorageRef};
use crate::types::{AnyClientState, AnyConsensusState, ProcessedStates, ReadonlyProcessedStates};
use super::Context;
use crate::types::AnyConsensusState;

impl ClientValidationContext for Context<'_> {
fn update_meta(
&self,
client_id: &ClientId,
height: &Height,
) -> Result<(Timestamp, Height), ContextError> {
let timestamp = client_update_time(self, client_id, height)?;
let height = client_update_height(self, client_id, height)?;
let client_update_time_path = ClientUpdateTimePath::new(
client_id.clone(),
height.revision_number(),
height.revision_height(),
);

let path_vec = client_update_time_path.leaf().into_bytes();

let timestamp = self
.retrieve(path_vec)
.map(|timestamp| u64::from_be_bytes(timestamp.try_into().expect("invalid timestamp")));

let timestamp = match timestamp {
Some(time) => {
Timestamp::from_nanoseconds(time).map_err(ClientError::InvalidPacketTimestamp)?
}
None => Err(ClientError::Other {
description: "problem getting processed time".to_string(),
})?,
};

let client_update_height_path = ClientUpdateHeightPath::new(
client_id.clone(),
height.revision_number(),
height.revision_height(),
);

let path_vec = client_update_height_path.leaf().into_bytes();

let revision_height = self
.retrieve(path_vec)
.map(|height| u64::from_be_bytes(height.try_into().expect("invalid height")));

let height = match revision_height {
Some(h) => Height::new(0, h)?,
None => Err(ClientError::Other {
description: "problem getting processed time".to_string(),
})?,
};

Ok((timestamp, height))
}
}
Expand All @@ -31,18 +71,30 @@ impl ClientExecutionContext for Context<'_> {

fn store_client_state(
&mut self,
client_state_path: ClientStatePath,
client_state: AnyClientState,
_client_state_path: ClientStatePath,
client_state: ClientState,
) -> Result<(), ContextError> {
store_client_state(self, client_state_path, client_state)
let key = ClientStatePath::leaf().into_bytes();

let encoded_client_state = self.encode_client_state(client_state)?;

self.insert(key, encoded_client_state);

Ok(())
}

fn store_consensus_state(
&mut self,
consensus_state_path: ClientConsensusStatePath,
consensus_state: AnyConsensusState,
) -> Result<(), ContextError> {
store_consensus_state(self, consensus_state_path, consensus_state)
let key = consensus_state_path.leaf().into_bytes();

let encoded_consensus_state = consensus_state.encode();

self.insert(key, encoded_consensus_state);

Ok(())
}

fn delete_consensus_state(
Expand All @@ -59,8 +111,37 @@ impl ClientExecutionContext for Context<'_> {
host_timestamp: Timestamp,
host_height: Height,
) -> Result<(), ContextError> {
store_update_time(self, client_id.clone(), height, host_timestamp)?;
store_update_height(self, client_id, height, host_height)
let client_update_time_path = ClientUpdateTimePath::new(
client_id.clone(),
height.revision_number(),
height.revision_height(),
);

let path_vec = client_update_time_path.leaf().into_bytes();

let time_vec: [u8; 8] = host_timestamp.nanoseconds().to_be_bytes();

self.insert(path_vec, time_vec);

let client_update_height_path = ClientUpdateHeightPath::new(
client_id,
height.revision_number(),
height.revision_height(),
);

let path_vec = client_update_height_path.leaf().into_bytes();

let revision_height_vec: [u8; 8] = host_height.revision_height().to_be_bytes();

self.insert(path_vec, revision_height_vec);

let iteration_key = iteration_key(height.revision_number(), height.revision_height());

let height_vec = height.to_string().into_bytes();

self.insert(iteration_key, height_vec);

Ok(())
}

fn delete_update_meta(
Expand All @@ -71,110 +152,3 @@ impl ClientExecutionContext for Context<'_> {
todo!()
}
}

fn client_update_time<Ctx>(
ctx: &Ctx,
_client_id: &ClientId,
height: &Height,
) -> Result<Timestamp, ContextError>
where
Ctx: ClientValidationContext + StorageRef,
{
let processed_state = ReadonlyProcessedStates::new(ctx.storage_ref());
let timestamp = match processed_state.get_processed_time(*height, &mut Vec::new()) {
Some(time) => {
Timestamp::from_nanoseconds(time).map_err(ClientError::InvalidPacketTimestamp)?
}
None => Err(ClientError::Other {
description: "problem getting processed time".to_string(),
})?,
};

Ok(timestamp)
}

fn client_update_height<Ctx>(
ctx: &Ctx,
_client_id: &ClientId,
height: &Height,
) -> Result<Height, ContextError>
where
Ctx: ClientValidationContext + StorageRef,
{
let processed_state = ReadonlyProcessedStates::new(ctx.storage_ref());

let height = match processed_state.get_processed_height(*height, &mut Vec::new()) {
Some(h) => Height::new(0, h)?,
None => Err(ClientError::Other {
description: "problem getting processed time".to_string(),
})?,
};

Ok(height)
}

fn store_client_state<Ctx>(
ctx: &mut Ctx,
client_state_path: ClientStatePath,
client_state: AnyClientState,
) -> Result<(), ContextError>
where
Ctx: ClientExecutionContext + StorageMut,
{
let client_state_value = Any::from(client_state).value;

ctx.storage_mut().set(
client_state_path.to_string().as_bytes(),
client_state_value.as_slice(),
);

Ok(())
}

fn store_consensus_state<Ctx>(
ctx: &mut Ctx,
consensus_state_path: ClientConsensusStatePath,
consensus_state: AnyConsensusState,
) -> Result<(), ContextError>
where
Ctx: ClientExecutionContext + StorageMut,
{
let consensus_state_value = Any::from(consensus_state).value;

ctx.storage_mut().set(
consensus_state_path.to_string().as_bytes(),
consensus_state_value.as_slice(),
);

Ok(())
}

fn store_update_time<Ctx>(
ctx: &mut Ctx,
_client_id: ClientId,
height: Height,
timestamp: Timestamp,
) -> Result<(), ContextError>
where
Ctx: ClientExecutionContext + StorageMut,
{
let mut processed_state = ProcessedStates::new(ctx.storage_mut());
processed_state.set_processed_time(height, timestamp.nanoseconds(), &mut Vec::new());

Ok(())
}

fn store_update_height<Ctx>(
ctx: &mut Ctx,
_client_id: ClientId,
height: Height,
host_height: Height,
) -> Result<(), ContextError>
where
Ctx: ClientExecutionContext + StorageMut,
{
let mut processed_state = ProcessedStates::new(ctx.storage_mut());
processed_state.set_processed_height(height, host_height.revision_height(), &mut Vec::new());
processed_state.set_iteration_key(height, &mut Vec::new());
Ok(())
}
15 changes: 8 additions & 7 deletions clients/sov-celestia-cw/src/contexts/core_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ use ibc_core::host::types::path::{
use ibc_core::host::{ExecutionContext, ValidationContext};
use ibc_core::primitives::proto::{Any, Protobuf};
use ibc_core::primitives::{Signer, Timestamp};
use sov_celestia_client::client_state::ClientState;
use sov_celestia_client::types::client_state::SOV_TENDERMINT_CLIENT_STATE_TYPE_URL;
use sov_celestia_client::types::proto::v1::{
ClientState as RawClientState, ConsensusState as RawConsensusState,
};

use super::{Context, StorageRef};
use crate::types::{AnyClientState, AnyConsensusState};
use crate::types::AnyConsensusState;

impl ValidationContext for Context<'_> {
type V = Self;
type E = Self;
type AnyConsensusState = AnyConsensusState;
type AnyClientState = AnyClientState;
type AnyClientState = ClientState;

fn get_client_validation_context(&self) -> &Self::V {
self
Expand Down Expand Up @@ -256,11 +257,11 @@ impl ExecutionContext for Context<'_> {
}
}

pub fn client_state<Ctx>(ctx: &Ctx, client_id: &ClientId) -> Result<AnyClientState, ContextError>
pub fn client_state<Ctx>(ctx: &Ctx, client_id: &ClientId) -> Result<ClientState, ContextError>
where
Ctx: ValidationContext + StorageRef,
{
let client_state_path = ClientStatePath::new(client_id).to_string();
let client_state_path = ClientStatePath::new(client_id.clone()).to_string();

let client_state_value =
ctx.storage_ref()
Expand All @@ -274,10 +275,10 @@ where
description: e.to_string(),
})?;

Ok(AnyClientState::Sovereign(sov_client_state))
Ok(sov_client_state)
}

fn decode_client_state<Ctx>(_ctx: &Ctx, client_state: Any) -> Result<AnyClientState, ContextError>
fn decode_client_state<Ctx>(_ctx: &Ctx, client_state: Any) -> Result<ClientState, ContextError>
where
Ctx: ValidationContext + StorageRef,
{
Expand All @@ -290,7 +291,7 @@ where
}
})?;

Ok(AnyClientState::Sovereign(sov_client_state))
Ok(sov_client_state)
}
_ => Err(ClientError::Other {
description: "Client state type not supported".to_string(),
Expand Down
Loading
Loading