Skip to content

Commit

Permalink
Merge branch 'main' into rano/store/imp-inmemory-store
Browse files Browse the repository at this point in the history
  • Loading branch information
rnbguy committed Apr 24, 2024
2 parents 93b68df + 5b4d3a2 commit 59d2158
Show file tree
Hide file tree
Showing 21 changed files with 561 additions and 430 deletions.
553 changes: 284 additions & 269 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ tracing = "0.1.26"
tracing-subscriber = "0.3.16"

# ibc dependencies
ibc = { git = "https://github.com/cosmos/ibc-rs", rev = "4769bd7", default-features = false, features = ["serde"] }
ibc-query = { git = "https://github.com/cosmos/ibc-rs", rev = "4769bd7", default-features = false }
ibc-proto = { version = "0.41.0", default-features = false }
ibc = { version = "0.51.0", default-features = false, features = ["serde"] }
ibc-query = { version = "0.51.0", default-features = false }
ibc-proto = { version = "0.42.2", default-features = false }
ics23 = { version = "0.11", default-features = false }

# tendermint dependencies
Expand All @@ -46,4 +46,7 @@ tendermint-abci = { version = "0.34", default-features = false }
tendermint-proto = { version = "0.34", default-features = false }
tendermint-rpc = { version = "0.34", default-features = false }

[patch.crates-io]
ibc = { git = "https://github.com/cosmos/ibc-rs.git", rev = "c659e210d2" }
ibc-query = { git = "https://github.com/cosmos/ibc-rs.git", rev = "c659e210d2" }

1 change: 0 additions & 1 deletion basecoin/app/src/abci/v0_38/tendermint.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::convert::TryInto;
use std::fmt::Debug;

use basecoin_modules::error::Error;
Expand Down
2 changes: 0 additions & 2 deletions basecoin/app/src/service.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::convert::TryInto;

use basecoin_store::context::ProvableStore;
use cosmrs::Tx;
use ibc_proto::cosmos::base::tendermint::v1beta1::service_server::Service as HealthService;
Expand Down
1 change: 0 additions & 1 deletion basecoin/modules/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,3 @@ tendermint-rpc = { workspace = true , features = ["http-client"] }
# internal dependencies
basecoin-store = { path = "../store" }


2 changes: 0 additions & 2 deletions basecoin/modules/src/auth/account.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::convert::{TryFrom, TryInto};

use basecoin_store::types::Path;
use cosmrs::AccountId;
use ibc_proto::cosmos::auth::v1beta1::BaseAccount;
Expand Down
1 change: 0 additions & 1 deletion basecoin/modules/src/bank/impls.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::HashMap;
use std::convert::TryInto;
use std::fmt::Debug;
use std::str::FromStr;

Expand Down
4 changes: 2 additions & 2 deletions basecoin/modules/src/gov/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use basecoin_store::impls::SharedStore;
use basecoin_store::types::{Height, Path, ProtobufStore, TypedStore};
use basecoin_store::utils::{SharedRw, SharedRwExt};
use cosmrs::AccountId;
use ibc::cosmos_host::upgrade_proposal::{upgrade_client_proposal_handler, UpgradeProposal};
use ibc::cosmos_host::upgrade_proposal::{execute_upgrade_client_proposal, UpgradeProposal};
use ibc_proto::cosmos::gov::v1beta1::query_server::QueryServer;
use ibc_proto::google::protobuf::Any;
use ibc_proto::Protobuf;
Expand Down Expand Up @@ -71,7 +71,7 @@ where

let mut upgrade_ctx = self.upgrade_ctx.write_access();

let event = upgrade_client_proposal_handler(upgrade_ctx.deref_mut(), upgrade_proposal)
let event = execute_upgrade_client_proposal(upgrade_ctx.deref_mut(), upgrade_proposal)
.map_err(|e| AppError::Custom {
reason: format!("Error handling upgrade proposal: {:?}", e),
})?;
Expand Down
127 changes: 90 additions & 37 deletions basecoin/modules/src/ibc/client_contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ use basecoin_store::context::Store;
use basecoin_store::types::Height;
use ibc::clients::tendermint::client_state::ClientState as TmClientState;
use ibc::clients::tendermint::consensus_state::ConsensusState as TmConsensusState;
use ibc::clients::tendermint::context::{CommonContext, ValidationContext as TmValidationContext};
use ibc::clients::tendermint::context::{
ConsensusStateConverter, ValidationContext as TmValidationContext,
};
use ibc::core::client::context::{ClientExecutionContext, ClientValidationContext};
use ibc::core::client::types::error::ClientError;
use ibc::core::client::types::Height as IbcHeight;
use ibc::core::handler::types::error::ContextError;
use ibc::core::host::types::identifiers::ClientId;
use ibc::core::host::types::path::{ClientConsensusStatePath, ClientStatePath, Path};
use ibc::core::host::types::path::{
ClientConsensusStatePath, ClientStatePath, ClientUpdateHeightPath, ClientUpdateTimePath, Path,
};
use ibc::core::host::ValidationContext;
use ibc::primitives::Timestamp;

Expand All @@ -20,29 +24,70 @@ impl<S> ClientValidationContext for IbcContext<S>
where
S: Store + Debug,
{
type ClientStateRef = TmClientState;
type ConsensusStateRef = AnyConsensusState;

fn client_state(&self, client_id: &ClientId) -> Result<Self::ClientStateRef, ContextError> {
Ok(self
.client_state_store
.get(Height::Pending, &ClientStatePath(client_id.clone()))
.ok_or(ClientError::ClientStateNotFound {
client_id: client_id.clone(),
})?)
}

fn consensus_state(
&self,
client_cons_state_path: &ClientConsensusStatePath,
) -> Result<Self::ConsensusStateRef, ContextError> {
let height = IbcHeight::new(
client_cons_state_path.revision_number,
client_cons_state_path.revision_height,
)
.map_err(|_| ClientError::InvalidHeight)?;
let consensus_state = self
.consensus_state_store
.get(Height::Pending, client_cons_state_path)
.ok_or(ClientError::ConsensusStateNotFound {
client_id: client_cons_state_path.client_id.clone(),
height,
})?;

Ok(consensus_state.into())
}

/// Returns the time and height when the client state for the given
/// [`ClientId`] was updated with a header for the given [`IbcHeight`]
fn update_meta(
fn client_update_meta(
&self,
client_id: &ClientId,
height: &IbcHeight,
) -> Result<(Timestamp, IbcHeight), ContextError> {
let client_update_time_path = ClientUpdateTimePath::new(
client_id.clone(),
height.revision_number(),
height.revision_height(),
);
let processed_timestamp = self
.client_processed_times
.get(&(client_id.clone(), *height))
.cloned()
.get(Height::Pending, &client_update_time_path)
.ok_or(ClientError::UpdateMetaDataNotFound {
client_id: client_id.clone(),
height: *height,
})?;
let client_update_height_path = ClientUpdateHeightPath::new(
client_id.clone(),
height.revision_number(),
height.revision_height(),
);
let processed_height = self
.client_processed_heights
.get(&(client_id.clone(), *height))
.cloned()
.get(Height::Pending, &client_update_height_path)
.ok_or(ClientError::UpdateMetaDataNotFound {
client_id: client_id.clone(),
height: *height,
})?;

Ok((processed_timestamp, processed_height))
}
}
Expand All @@ -51,17 +96,13 @@ impl<S> ClientExecutionContext for IbcContext<S>
where
S: Store + Debug,
{
type V = Self;

type AnyClientState = TmClientState;

type AnyConsensusState = AnyConsensusState;
type ClientStateMut = TmClientState;

/// Called upon successful client creation and update
fn store_client_state(
&mut self,
client_state_path: ClientStatePath,
client_state: Self::AnyClientState,
client_state: Self::ClientStateMut,
) -> Result<(), ContextError> {
self.client_state_store
.set(client_state_path, client_state)
Expand All @@ -76,7 +117,7 @@ where
fn store_consensus_state(
&mut self,
consensus_state_path: ClientConsensusStatePath,
consensus_state: Self::AnyConsensusState,
consensus_state: Self::ConsensusStateRef,
) -> Result<(), ContextError> {
let tm_consensus_state: TmConsensusState =
consensus_state.try_into().map_err(|_| ClientError::Other {
Expand Down Expand Up @@ -108,10 +149,26 @@ where
host_timestamp: Timestamp,
host_height: IbcHeight,
) -> Result<(), ContextError> {
let client_update_time_path = ClientUpdateTimePath::new(
client_id.clone(),
height.revision_number(),
height.revision_height(),
);
self.client_processed_times
.insert((client_id.clone(), height), host_timestamp);
.set(client_update_time_path, host_timestamp)
.map_err(|_| ClientError::Other {
description: "store update error".into(),
})?;
let client_update_height_path = ClientUpdateHeightPath::new(
client_id.clone(),
height.revision_number(),
height.revision_height(),
);
self.client_processed_heights
.insert((client_id, height), host_height);
.set(client_update_height_path, host_height)
.map_err(|_| ClientError::Other {
description: "store update error".into(),
})?;
Ok(())
}

Expand All @@ -122,20 +179,28 @@ where
client_id: ClientId,
height: IbcHeight,
) -> Result<(), ContextError> {
self.client_processed_times
.remove(&(client_id.clone(), height));
self.client_processed_heights.remove(&(client_id, height));
let client_update_time_path = ClientUpdateTimePath::new(
client_id.clone(),
height.revision_number(),
height.revision_height(),
);
self.client_processed_times.delete(client_update_time_path);
let client_update_height_path = ClientUpdateHeightPath::new(
client_id.clone(),
height.revision_number(),
height.revision_height(),
);
self.client_processed_heights
.delete(client_update_height_path);
Ok(())
}
}

impl<S> CommonContext for IbcContext<S>
impl<S> TmValidationContext for IbcContext<S>
where
S: Store + Debug,
Self::ConsensusStateRef: ConsensusStateConverter,
{
type ConversionError = &'static str;
type AnyConsensusState = AnyConsensusState;

fn host_timestamp(&self) -> Result<Timestamp, ContextError> {
ValidationContext::host_timestamp(self)
}
Expand All @@ -144,13 +209,6 @@ where
ValidationContext::host_height(self)
}

fn consensus_state(
&self,
client_cons_state_path: &ClientConsensusStatePath,
) -> Result<Self::AnyConsensusState, ContextError> {
ValidationContext::consensus_state(self, client_cons_state_path)
}

fn consensus_state_heights(
&self,
client_id: &ClientId,
Expand Down Expand Up @@ -180,17 +238,12 @@ where
})
.collect()
}
}

impl<S> TmValidationContext for IbcContext<S>
where
S: Store + Debug,
{
fn next_consensus_state(
&self,
client_id: &ClientId,
height: &IbcHeight,
) -> Result<Option<Self::AnyConsensusState>, ContextError> {
) -> Result<Option<Self::ConsensusStateRef>, ContextError> {
let path = format!("clients/{client_id}/consensusStates")
.try_into()
.unwrap(); // safety - path must be valid since ClientId and height are valid Identifiers
Expand Down Expand Up @@ -224,7 +277,7 @@ where
&self,
client_id: &ClientId,
height: &IbcHeight,
) -> Result<Option<Self::AnyConsensusState>, ContextError> {
) -> Result<Option<Self::ConsensusStateRef>, ContextError> {
let path = format!("clients/{client_id}/consensusStates")
.try_into()
.unwrap(); // safety - path must be valid since ClientId and height are valid Identifiers
Expand Down
Loading

0 comments on commit 59d2158

Please sign in to comment.