diff --git a/Cargo.lock b/Cargo.lock index 59b444c6..da50b677 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1084,7 +1084,7 @@ dependencies = [ [[package]] name = "ibc" version = "0.43.1" -source = "git+https://github.com/cosmos/ibc-rs.git?rev=12317a0#12317a08bc46c7ee9fb65127c70351c70bb445b4" +source = "git+https://github.com/cosmos/ibc-rs.git?rev=4a69b49#4a69b49411957c4e1f6432d0bf32d4eac45e9b15" dependencies = [ "bytes", "derive_more", @@ -1112,8 +1112,7 @@ dependencies = [ [[package]] name = "ibc-derive" version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2628857829b03dc5d434d116a0ed865e6634bad684368f466bd1a0431f49d0a6" +source = "git+https://github.com/cosmos/ibc-rs.git?rev=4a69b49#4a69b49411957c4e1f6432d0bf32d4eac45e9b15" dependencies = [ "darling", "proc-macro2", @@ -3108,8 +3107,3 @@ dependencies = [ "quote", "syn 2.0.28", ] - -[[patch.unused]] -name = "ibc-proto" -version = "0.32.0" -source = "git+https://github.com/cosmos/ibc-proto-rs?rev=e8b6d0e9#e8b6d0e9a6669a01fca2b15106bd054adcc9f2bd" diff --git a/Cargo.toml b/Cargo.toml index a6e2458e..396f7256 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ cosmrs = "0.14.0" displaydoc = { version = "0.2", default-features = false } derive_more = { version = "0.99.17", default-features = false, features = ["from", "into", "display"] } ed25519 = { version = "2.1.0", default-features = false } -ibc = { git = "https://github.com/cosmos/ibc-rs.git", rev= "12317a0" } +ibc = { git = "https://github.com/cosmos/ibc-rs.git", rev= "4a69b49" } ibc-proto = { version = "0.32.1", default-features = false, features = ["server", "proto-descriptor"] } ics23 = { version = "0.10.1", default-features = false, features = ["host-functions"] } primitive-types = { version = "0.12.0", default-features = false, features = ["serde_no_std"] } @@ -54,6 +54,3 @@ bytes = "1.0.1" lazy_static = "1.4.0" proptest = "*" rand = "*" - -[patch.crates-io] -ibc-proto = { git = "https://github.com/cosmos/ibc-proto-rs", rev = "e8b6d0e9" } diff --git a/src/modules/ibc/impls.rs b/src/modules/ibc/impls.rs index b8a72dd3..d42431ce 100644 --- a/src/modules/ibc/impls.rs +++ b/src/modules/ibc/impls.rs @@ -98,7 +98,7 @@ where S: 'static + ProvableStore + Default + Debug, { pub fn new(store: SharedStore, bank_keeper: BankBalanceKeeper) -> Self { - let transfer_module = IbcTransferModule::new(store.clone(), bank_keeper); + let transfer_module = IbcTransferModule::new(bank_keeper); let router = IbcRouter::new(transfer_module); Self { @@ -120,90 +120,6 @@ where } } -/// The IBC module -/// -/// Implements all IBC-rs validation and execution contexts and gRPC endpoints -/// required by `hermes` as well. -#[derive(Clone)] -pub struct IbcContext -where - S: Store + Send + Sync + Debug, -{ - /// Handle to store instance. - /// The module is guaranteed exclusive access to all paths in the store key-space. - pub store: SharedStore, - /// Counter for clients - client_counter: u64, - /// Counter for connections - conn_counter: u64, - /// Counter for channels - channel_counter: u64, - /// Tracks the processed time for client updates - client_processed_times: HashMap<(ClientId, IbcHeight), Timestamp>, - /// Tracks the processed height for client updates - client_processed_heights: HashMap<(ClientId, IbcHeight), IbcHeight>, - /// Map of host consensus states - consensus_states: HashMap, - /// A typed-store for AnyClientState - pub client_state_store: ProtobufStore, ClientStatePath, TmClientState, Any>, - /// A typed-store for AnyConsensusState - pub consensus_state_store: - ProtobufStore, ClientConsensusStatePath, TmConsensusState, Any>, - /// A typed-store for ConnectionEnd - connection_end_store: - ProtobufStore, ConnectionPath, ConnectionEnd, RawConnectionEnd>, - /// A typed-store for ConnectionIds - connection_ids_store: JsonStore, ClientConnectionPath, Vec>, - /// A typed-store for ChannelEnd - channel_end_store: ProtobufStore, ChannelEndPath, ChannelEnd, RawChannelEnd>, - /// A typed-store for send sequences - send_sequence_store: JsonStore, SeqSendPath, Sequence>, - /// A typed-store for receive sequences - recv_sequence_store: JsonStore, SeqRecvPath, Sequence>, - /// A typed-store for ack sequences - ack_sequence_store: JsonStore, SeqAckPath, Sequence>, - /// A typed-store for packet commitments - packet_commitment_store: BinStore, CommitmentPath, PacketCommitment>, - /// A typed-store for packet receipts - packet_receipt_store: TypedSet, ReceiptPath>, - /// A typed-store for packet ack - packet_ack_store: BinStore, AckPath, AcknowledgementCommitment>, - /// IBC Events - pub(crate) events: Vec, - /// message logs - logs: Vec, -} - -impl IbcContext -where - S: 'static + ProvableStore + Default + Debug, -{ - pub fn new(store: SharedStore) -> Self { - Self { - client_counter: 0, - conn_counter: 0, - channel_counter: 0, - client_processed_times: Default::default(), - client_processed_heights: Default::default(), - consensus_states: Default::default(), - client_state_store: TypedStore::new(store.clone()), - consensus_state_store: TypedStore::new(store.clone()), - connection_end_store: TypedStore::new(store.clone()), - connection_ids_store: TypedStore::new(store.clone()), - channel_end_store: TypedStore::new(store.clone()), - send_sequence_store: TypedStore::new(store.clone()), - recv_sequence_store: TypedStore::new(store.clone()), - ack_sequence_store: TypedStore::new(store.clone()), - packet_commitment_store: TypedStore::new(store.clone()), - packet_receipt_store: TypedStore::new(store.clone()), - packet_ack_store: TypedStore::new(store.clone()), - store, - events: Vec::new(), - logs: Vec::new(), - } - } -} - impl Ibc where S: ProvableStore + Debug, @@ -246,8 +162,10 @@ where .get_transfer_module_mut() .expect("Failed to get the transfer module"); - send_transfer(transfer_module, transfer_msg).map_err(|e| AppError::Custom { - reason: e.to_string(), + send_transfer(&mut self.ctx, transfer_module, transfer_msg).map_err(|e| { + AppError::Custom { + reason: e.to_string(), + } })?; Ok(transfer_module @@ -328,6 +246,90 @@ where } } +/// The IBC module +/// +/// Implements all IBC-rs validation and execution contexts and gRPC endpoints +/// required by `hermes` as well. +#[derive(Clone)] +pub struct IbcContext +where + S: Store + Send + Sync + Debug, +{ + /// Handle to store instance. + /// The module is guaranteed exclusive access to all paths in the store key-space. + pub store: SharedStore, + /// Counter for clients + client_counter: u64, + /// Counter for connections + conn_counter: u64, + /// Counter for channels + channel_counter: u64, + /// Tracks the processed time for client updates + client_processed_times: HashMap<(ClientId, IbcHeight), Timestamp>, + /// Tracks the processed height for client updates + client_processed_heights: HashMap<(ClientId, IbcHeight), IbcHeight>, + /// Map of host consensus states + consensus_states: HashMap, + /// A typed-store for AnyClientState + pub client_state_store: ProtobufStore, ClientStatePath, TmClientState, Any>, + /// A typed-store for AnyConsensusState + pub consensus_state_store: + ProtobufStore, ClientConsensusStatePath, TmConsensusState, Any>, + /// A typed-store for ConnectionEnd + connection_end_store: + ProtobufStore, ConnectionPath, ConnectionEnd, RawConnectionEnd>, + /// A typed-store for ConnectionIds + connection_ids_store: JsonStore, ClientConnectionPath, Vec>, + /// A typed-store for ChannelEnd + channel_end_store: ProtobufStore, ChannelEndPath, ChannelEnd, RawChannelEnd>, + /// A typed-store for send sequences + send_sequence_store: JsonStore, SeqSendPath, Sequence>, + /// A typed-store for receive sequences + recv_sequence_store: JsonStore, SeqRecvPath, Sequence>, + /// A typed-store for ack sequences + ack_sequence_store: JsonStore, SeqAckPath, Sequence>, + /// A typed-store for packet commitments + packet_commitment_store: BinStore, CommitmentPath, PacketCommitment>, + /// A typed-store for packet receipts + packet_receipt_store: TypedSet, ReceiptPath>, + /// A typed-store for packet ack + packet_ack_store: BinStore, AckPath, AcknowledgementCommitment>, + /// IBC Events + pub(crate) events: Vec, + /// message logs + logs: Vec, +} + +impl IbcContext +where + S: 'static + ProvableStore + Default + Debug, +{ + pub fn new(store: SharedStore) -> Self { + Self { + client_counter: 0, + conn_counter: 0, + channel_counter: 0, + client_processed_times: Default::default(), + client_processed_heights: Default::default(), + consensus_states: Default::default(), + client_state_store: TypedStore::new(store.clone()), + consensus_state_store: TypedStore::new(store.clone()), + connection_end_store: TypedStore::new(store.clone()), + connection_ids_store: TypedStore::new(store.clone()), + channel_end_store: TypedStore::new(store.clone()), + send_sequence_store: TypedStore::new(store.clone()), + recv_sequence_store: TypedStore::new(store.clone()), + ack_sequence_store: TypedStore::new(store.clone()), + packet_commitment_store: TypedStore::new(store.clone()), + packet_receipt_store: TypedStore::new(store.clone()), + packet_ack_store: TypedStore::new(store.clone()), + store, + events: Vec::new(), + logs: Vec::new(), + } + } +} + pub(crate) struct TmEvent(pub TendermintEvent); impl From for Event { diff --git a/src/modules/ibc/router.rs b/src/modules/ibc/router.rs index c379aa3e..ddb336b6 100644 --- a/src/modules/ibc/router.rs +++ b/src/modules/ibc/router.rs @@ -19,7 +19,7 @@ pub struct IbcRouter where S: Store + Send + Sync + Debug, { - transfer: Arc>>, + transfer: Arc>>, /// Mapping of which IBC modules own which port port_to_module_map: BTreeMap, @@ -29,7 +29,7 @@ impl IbcRouter where S: 'static + Store + Send + Sync + Debug, { - pub fn new(transfer: IbcTransferModule>) -> Self { + pub fn new(transfer: IbcTransferModule>) -> Self { let mut port_to_module_map = BTreeMap::default(); let transfer_module_id: ModuleId = ModuleId::new(IBC_TRANSFER_MODULE_ID.to_string()); port_to_module_map.insert(PortId::transfer(), transfer_module_id); @@ -42,7 +42,7 @@ where pub fn get_transfer_module_mut( &mut self, - ) -> Option<&mut IbcTransferModule>> { + ) -> Option<&mut IbcTransferModule>> { match Arc::get_mut(&mut self.transfer) { Some(m) => Some(m), None => None, diff --git a/src/modules/ibc/transfer.rs b/src/modules/ibc/transfer.rs index 1aeee067..d7a05705 100644 --- a/src/modules/ibc/transfer.rs +++ b/src/modules/ibc/transfer.rs @@ -1,17 +1,11 @@ -use crate::{ - helper::Height, - modules::{ - auth::account::ACCOUNT_PREFIX, - bank::context::BankKeeper, - bank::util::{Coin, Denom}, - }, - store::{ - SharedStore, Store, {BinStore, JsonStore, ProtobufStore, TypedStore}, - }, +use crate::modules::{ + auth::account::ACCOUNT_PREFIX, + bank::context::BankKeeper, + bank::util::{Coin, Denom}, }; use core::fmt::Debug; use cosmrs::AccountId; -use ibc::{applications::transfer::VERSION, core::ics24_host::path::SeqSendPath}; +use ibc::applications::transfer::VERSION; use ibc::{ applications::transfer::{ context::{ @@ -21,32 +15,16 @@ use ibc::{ error::TokenTransferError, PrefixedCoin, }, - clients::ics07_tendermint::{ - client_state::ClientState as TmClientState, - consensus_state::ConsensusState as TmConsensusState, - }, core::{ - ics02_client::error::ClientError, - ics03_connection::{connection::ConnectionEnd, error::ConnectionError}, ics04_channel::{ - channel::{ChannelEnd, Counterparty, Order}, - commitment::PacketCommitment, - context::{SendPacketExecutionContext, SendPacketValidationContext}, + channel::{Counterparty, Order}, error::{ChannelError, PacketError}, - packet::{Packet, Sequence}, + packet::Packet, Version as ChannelVersion, }, - ics24_host::{ - identifier::{ChannelId, ClientId, ConnectionId}, - path::{ - ChannelEndPath, ClientConsensusStatePath, ClientStatePath, CommitmentPath, - ConnectionPath, - }, - }, + ics24_host::identifier::{ChannelId, ConnectionId}, router::{Module as IbcModule, ModuleExtras}, - ContextError, }, - Height as IbcHeight, }; use ibc::{ core::{ @@ -55,12 +33,6 @@ use ibc::{ }, Signer, }; -use ibc_proto::{ - google::protobuf::Any, - ibc::core::{ - channel::v1::Channel as RawChannelEnd, connection::v1::ConnectionEnd as RawConnectionEnd, - }, -}; use ibc::applications::transfer::context::{ on_acknowledgement_packet_validate, on_chan_open_ack_validate, on_chan_open_confirm_validate, @@ -69,59 +41,31 @@ use ibc::applications::transfer::context::{ on_timeout_packet_validate, }; -use super::impls::{AnyConsensusState, IbcContext}; - #[derive(Clone, Debug)] -pub struct IbcTransferModule +pub struct IbcTransferModule where - S: Send + Sync, BK: Send + Sync, { /// A bank keeper to enable sending, minting and burning of tokens bank_keeper: BK, - /// A typed-store for AnyClientState - client_state_store: ProtobufStore, ClientStatePath, TmClientState, Any>, - /// A typed-store for AnyConsensusState - consensus_state_store: - ProtobufStore, ClientConsensusStatePath, TmConsensusState, Any>, - /// A typed-store for ConnectionEnd - connection_end_store: - ProtobufStore, ConnectionPath, ConnectionEnd, RawConnectionEnd>, - /// A typed-store for ChannelEnd - channel_end_store: ProtobufStore, ChannelEndPath, ChannelEnd, RawChannelEnd>, - /// A typed-store for send sequences - send_sequence_store: JsonStore, SeqSendPath, Sequence>, - /// A typed-store for packet commitments - packet_commitment_store: BinStore, CommitmentPath, PacketCommitment>, pub events: Vec, - - log: Vec, } -impl IbcTransferModule +impl IbcTransferModule where - S: 'static + Store, BK: 'static + Send + Sync + BankKeeper, { - pub fn new(store: SharedStore, bank_keeper: BK) -> Self { + pub fn new(bank_keeper: BK) -> Self { Self { bank_keeper, - client_state_store: TypedStore::new(store.clone()), - consensus_state_store: TypedStore::new(store.clone()), - connection_end_store: TypedStore::new(store.clone()), - channel_end_store: TypedStore::new(store.clone()), - send_sequence_store: TypedStore::new(store.clone()), - packet_commitment_store: TypedStore::new(store), events: Vec::new(), - log: Vec::new(), } } } -impl IbcModule for IbcTransferModule +impl IbcModule for IbcTransferModule where - S: Store + Debug + 'static, BK: 'static + Send + Sync + Debug + BankKeeper, Self: Send + Sync, { @@ -353,9 +297,8 @@ where } } -impl TokenTransferExecutionContext for IbcTransferModule +impl TokenTransferExecutionContext for IbcTransferModule where - S: Store + Send + Sync + Debug + 'static, BK: BankKeeper + Send + Sync, { fn send_coins_execute( @@ -415,9 +358,8 @@ where } } -impl TokenTransferValidationContext for IbcTransferModule +impl TokenTransferValidationContext for IbcTransferModule where - S: Store + Send + Sync + Debug + 'static, BK: BankKeeper + Send + Sync, { type AccountId = Signer; @@ -485,108 +427,3 @@ where Ok(()) } } - -impl SendPacketValidationContext for IbcTransferModule -where - S: Store + Send + Sync + Debug + 'static, - BK: Send + Sync, -{ - type ClientValidationContext = IbcContext; - type E = IbcContext; - type AnyConsensusState = AnyConsensusState; - type AnyClientState = TmClientState; - - fn channel_end(&self, channel_end_path: &ChannelEndPath) -> Result { - self.channel_end_store - .get(Height::Pending, channel_end_path) - .ok_or(ContextError::ChannelError(ChannelError::ChannelNotFound { - port_id: channel_end_path.0.clone(), - channel_id: channel_end_path.1.clone(), - })) - } - - fn connection_end(&self, connection_id: &ConnectionId) -> Result { - self.connection_end_store - .get(Height::Pending, &ConnectionPath::new(connection_id)) - .ok_or(ContextError::ConnectionError( - ConnectionError::ConnectionNotFound { - connection_id: connection_id.clone(), - }, - )) - } - - fn client_state(&self, client_id: &ClientId) -> Result { - self.client_state_store - .get(Height::Pending, &ClientStatePath::new(client_id)) - .ok_or(ContextError::ClientError( - ClientError::ClientStateNotFound { - client_id: client_id.clone(), - }, - )) - } - - fn client_consensus_state( - &self, - client_cons_state_path: &ClientConsensusStatePath, - ) -> Result { - let height = IbcHeight::new(client_cons_state_path.epoch, client_cons_state_path.height) - .map_err(|_| ContextError::ClientError(ClientError::InvalidHeight))?; - self.consensus_state_store - .get(Height::Pending, client_cons_state_path) - .ok_or(ContextError::ClientError( - ClientError::ConsensusStateNotFound { - client_id: client_cons_state_path.client_id.clone(), - height, - }, - )) - .map(|cs| cs.into()) - } - - fn get_next_sequence_send( - &self, - seq_send_path: &SeqSendPath, - ) -> Result { - self.send_sequence_store - .get(Height::Pending, seq_send_path) - .ok_or(ContextError::PacketError(PacketError::MissingNextSendSeq { - port_id: seq_send_path.0.clone(), - channel_id: seq_send_path.1.clone(), - })) - } -} - -impl SendPacketExecutionContext for IbcTransferModule -where - S: Store + Send + Sync + Debug + 'static, - BK: BankKeeper + Send + Sync, -{ - fn store_packet_commitment( - &mut self, - commitment_path: &CommitmentPath, - commitment: PacketCommitment, - ) -> Result<(), ContextError> { - self.packet_commitment_store - .set(commitment_path.clone(), commitment) - .map_err(|_| PacketError::ImplementationSpecific)?; - Ok(()) - } - - fn store_next_sequence_send( - &mut self, - seq_send_path: &SeqSendPath, - seq: Sequence, - ) -> Result<(), ContextError> { - self.send_sequence_store - .set(seq_send_path.clone(), seq) - .map_err(|_| PacketError::ImplementationSpecific)?; - Ok(()) - } - - fn emit_ibc_event(&mut self, event: IbcEvent) { - self.events.push(event) - } - - fn log_message(&mut self, message: String) { - self.log.push(message) - } -}