Skip to content

Commit

Permalink
Properly define and document our public interface (#656)
Browse files Browse the repository at this point in the history
* some docstrings updated

* Remove dynamic_typing module

* move some utils to tendermint client

* Make `crate::utils` private

* docs

* Remove unused struct

* move events under core

* move timestamp under core

* Move tx_msg under core

* Make naming convention comment not module-level

* Make core::handler private

* Re-export all core::context

* make tx_msg private and re-export Msg

* core module docs

* many docs improvements

* Release v0.39.0 (#657)

* unclog release

* bump version

* Correct issue 621 description

---------

Co-authored-by: Farhad Shabani <[email protected]>

* fix doc test

* `Validation/ExecutionContext` method docs

* more docs improvements

* remove unused function

* client docs improvements

* move trust_threshold to tendermint

* connection

* finish ics-20 docs

* finish docs for the tendermint light client

* core docs

* move `ModuleExtras` out of channel

* Make all `TYPE_URL` private

* Move `Acknowledgement `

* remove superfluous channel message modules

* finish channel docs

* Remove `ics05_port` module

* ValidationError -> IdentifierError

* remove error module

* cleanup ics24_host path

* move host validate module under identifier

* move RouterError

* move msgs under core

* remove tx_msg

* router module

* remove arbitrary restrictions on `ModuleId`

* router

* finish ics23

* mock

* fmt

* Add serialization for various structs and enums (#653)

* Add serialization for various structs and enums

* Create 652-codec-borsh-serde.md

---------

Signed-off-by: Davirain <[email protected]>

* changelog

* remove `ClientTypePath`

---------

Signed-off-by: Davirain <[email protected]>
Co-authored-by: Farhad Shabani <[email protected]>
Co-authored-by: Davirain <[email protected]>
  • Loading branch information
3 people authored May 5, 2023
1 parent aeac915 commit ee16dc2
Show file tree
Hide file tree
Showing 131 changed files with 876 additions and 892 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Reduce and consolidate the amount of public modules exposed
([#235](https://github.com/cosmos/ibc-rs/issues/235))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Make `TYPE_URL`s private ([#597](https://github.com/cosmos/ibc-rs/issues/597))
2 changes: 2 additions & 0 deletions .changelog/unreleased/improvements/376-docstrings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Document every method of `ValidationContext` and `ExecutionContext`
([#376](https://github.com/cosmos/ibc-rs/issues/376))
2 changes: 1 addition & 1 deletion crates/ibc/src/applications/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Various packet encoding semantics which underpin the various types of transactions.
//! Implementation of IBC applications

#[cfg(feature = "serde")]
pub mod transfer;
5 changes: 3 additions & 2 deletions crates/ibc/src/applications/transfer/acknowledgement.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! Defines types used in token transfer acknowledgements

use core::fmt::{Display, Error as FmtError, Formatter};

use super::error::TokenTransferError;
use crate::core::ics04_channel::msgs::acknowledgement::Acknowledgement;
use crate::prelude::*;
use crate::{core::ics04_channel::packet::Acknowledgement, prelude::*};

/// A string constant included in error acknowledgements.
/// NOTE: Changing this const is state machine breaking as acknowledgements are written into state
Expand Down
2 changes: 2 additions & 0 deletions crates/ibc/src/applications/transfer/amount.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Contains the `Amount` type, which represents amounts of tokens transferred.

use core::str::FromStr;
use derive_more::{Display, From, Into};

Expand Down
2 changes: 2 additions & 0 deletions crates/ibc/src/applications/transfer/coin.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Defines coin types; the objects that are being transferred.

use core::fmt::{Display, Error as FmtError, Formatter};
use core::str::{from_utf8, FromStr};
use ibc_proto::cosmos::base::v1beta1::Coin as ProtoCoin;
Expand Down
9 changes: 6 additions & 3 deletions crates/ibc/src/applications/transfer/context.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//! Defines the main context traits and IBC module callbacks

use crate::core::router::ModuleExtras;
use crate::core::ContextError;
use crate::prelude::*;

Expand All @@ -16,13 +19,12 @@ use crate::core::ics04_channel::channel::{Counterparty, Order};
use crate::core::ics04_channel::context::{
SendPacketExecutionContext, SendPacketValidationContext,
};
use crate::core::ics04_channel::handler::ModuleExtras;
use crate::core::ics04_channel::msgs::acknowledgement::Acknowledgement;
use crate::core::ics04_channel::packet::Packet;
use crate::core::ics04_channel::packet::{Acknowledgement, Packet};
use crate::core::ics04_channel::Version;
use crate::core::ics24_host::identifier::{ChannelId, ConnectionId, PortId};
use crate::signer::Signer;

/// Methods required in token transfer validation, to be implemented by the host
pub trait TokenTransferValidationContext: SendPacketValidationContext {
type AccountId: TryFrom<Signer>;

Expand Down Expand Up @@ -71,6 +73,7 @@ pub trait TokenTransferValidationContext: SendPacketValidationContext {
}
}

/// Methods required in token transfer execution, to be implemented by the host
pub trait TokenTransferExecutionContext:
TokenTransferValidationContext + SendPacketExecutionContext
{
Expand Down
12 changes: 11 additions & 1 deletion crates/ibc/src/applications/transfer/denom.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Defines types to represent "denominations" [as defined in ICS-20](https://github.com/cosmos/ibc/blob/main/spec/app/ics-020-fungible-token-transfer/README.md#data-structures)

use core::fmt::{Display, Error as FmtError, Formatter};
use core::str::FromStr;

Expand All @@ -11,7 +13,10 @@ use crate::prelude::*;
#[cfg(feature = "serde")]
use crate::serializers::serde_string;

/// Base denomination type
/// The "base" of a denomination.
///
/// For example, given the token `my_port-1/my_channel-1/my_port-2/my_channel-2/base_denom`,
/// `base_denom` is the "base" of the denomination
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
#[cfg_attr(
Expand Down Expand Up @@ -43,6 +48,11 @@ impl FromStr for BaseDenom {
}
}

/// One hop in a token's trace, which consists of the port and channel IDs of the sender
///
/// For example, given the token `my_port-1/my_channel-1/my_port-2/my_channel-2/base_denom`,
/// `my_port-1/my_channel-1` is a trace prefix, and `my_port-2/my_channel-2` is another one.
/// See [TracePath] which stitches trace prefixes together.
#[cfg_attr(
feature = "parity-scale-codec",
derive(
Expand Down
15 changes: 8 additions & 7 deletions crates/ibc/src/applications/transfer/error.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! Defines the token transfer error type

use core::convert::Infallible;
use core::str::Utf8Error;
use displaydoc::Display;
use ibc_proto::protobuf::Error as TendermintProtoError;
use uint::FromDecStrErr;

use crate::core::ics04_channel::channel::Order;
use crate::core::ics24_host::error::ValidationError;
use crate::core::ics24_host::identifier::{ChannelId, PortId};
use crate::core::ics24_host::identifier::{ChannelId, IdentifierError, PortId};
use crate::core::ContextError;
use crate::prelude::*;

Expand All @@ -15,7 +16,7 @@ pub enum TokenTransferError {
/// context error: `{0}`
ContextError(ContextError),
/// invalid identifier: `{0}`
InvalidIdentifier(ValidationError),
InvalidIdentifier(IdentifierError),
/// destination channel not found in the counterparty of port_id `{port_id}` and channel_id `{channel_id}`
DestinationChannelNotFound {
port_id: PortId,
Expand All @@ -26,12 +27,12 @@ pub enum TokenTransferError {
/// invalid prot id n trace at position: `{pos}`, validation error: `{validation_error}`
InvalidTracePortId {
pos: usize,
validation_error: ValidationError,
validation_error: IdentifierError,
},
/// invalid channel id in trace at position: `{pos}`, validation error: `{validation_error}`
InvalidTraceChannelId {
pos: usize,
validation_error: ValidationError,
validation_error: IdentifierError,
},
/// trace length must be even but got: `{len}`
InvalidTraceLength { len: usize },
Expand Down Expand Up @@ -105,8 +106,8 @@ impl From<ContextError> for TokenTransferError {
}
}

impl From<ValidationError> for TokenTransferError {
fn from(err: ValidationError) -> TokenTransferError {
impl From<IdentifierError> for TokenTransferError {
fn from(err: IdentifierError) -> TokenTransferError {
Self::InvalidIdentifier(err)
}
}
21 changes: 19 additions & 2 deletions crates/ibc/src/applications/transfer/events.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
//! Defines all token transfer event types

use crate::applications::transfer::acknowledgement::TokenTransferAcknowledgement;
use crate::applications::transfer::{Amount, Memo, PrefixedDenom, MODULE_ID_STR};
use crate::events::ModuleEvent;
use crate::applications::transfer::{Amount, PrefixedDenom, MODULE_ID_STR};
use crate::core::events::ModuleEvent;
use crate::prelude::*;
use crate::signer::Signer;

use super::Memo;

const EVENT_TYPE_PACKET: &str = "fungible_token_packet";
const EVENT_TYPE_TIMEOUT: &str = "timeout";
const EVENT_TYPE_DENOM_TRACE: &str = "denomination_trace";
const EVENT_TYPE_TRANSFER: &str = "ibc_transfer";

/// Contains all events variants that can be emitted from the token transfer application
pub enum Event {
Recv(RecvEvent),
Ack(AckEvent),
Expand All @@ -18,6 +23,8 @@ pub enum Event {
Transfer(TransferEvent),
}

/// Event emitted in the [`onRecvPacket`][super::context::on_recv_packet_execute]
/// module callback to indicate the that the `RecvPacket` message was processed
pub struct RecvEvent {
pub sender: Signer,
pub receiver: Signer,
Expand Down Expand Up @@ -52,6 +59,8 @@ impl From<RecvEvent> for ModuleEvent {
}
}

/// Event emitted in the [`onAcknowledgePacket`][super::context::on_acknowledgement_packet_execute]
/// module callback
pub struct AckEvent {
pub sender: Signer,
pub receiver: Signer,
Expand Down Expand Up @@ -86,6 +95,8 @@ impl From<AckEvent> for ModuleEvent {
}
}

/// Event emitted in the [`onAcknowledgePacket`][super::context::on_acknowledgement_packet_execute]
/// module callback to indicate whether the acknowledgement is a success or a failure
pub struct AckStatusEvent {
pub acknowledgement: TokenTransferAcknowledgement,
}
Expand All @@ -105,6 +116,8 @@ impl From<AckStatusEvent> for ModuleEvent {
}
}

/// Event emitted in the [`onTimeoutPacket`][super::context::on_timeout_packet_execute]
/// module callback
pub struct TimeoutEvent {
pub refund_receiver: Signer,
pub refund_denom: PrefixedDenom,
Expand Down Expand Up @@ -133,6 +146,8 @@ impl From<TimeoutEvent> for ModuleEvent {
}
}

/// Event emitted in the [`onRecvPacket`][super::context::on_recv_packet_execute]
/// module callback when new tokens are minted
pub struct DenomTraceEvent {
pub trace_hash: Option<String>,
pub denom: PrefixedDenom,
Expand All @@ -152,6 +167,8 @@ impl From<DenomTraceEvent> for ModuleEvent {
}
}

/// Event emitted in [`sendTransfer`][super::send_transfer] after a successful
/// transfer
pub struct TransferEvent {
pub sender: Signer,
pub receiver: Signer,
Expand Down
4 changes: 4 additions & 0 deletions crates/ibc/src/applications/transfer/memo.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//! Defines the memo type, which represents the string that users can include
//! with a token transfer

use core::convert::Infallible;
use core::fmt::{self, Display};
use core::str::FromStr;

use crate::prelude::*;

/// Represents the token transfer memo
#[cfg_attr(
feature = "parity-scale-codec",
derive(
Expand Down
10 changes: 6 additions & 4 deletions crates/ibc/src/applications/transfer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! ICS 20: Token Transfer implementation allows for multi-chain denomination handling, which
//! constitutes a "fungible token transfer bridge module" between the IBC routing module and an
//! asset tracking module.
//! Implementation of the [fungible token transfer module](https://github.com/cosmos/ibc/blob/main/spec/app/ics-020-fungible-token-transfer/README.md) (ICS-20)

pub mod acknowledgement;
pub mod amount;
pub mod coin;
Expand All @@ -11,7 +10,6 @@ pub mod events;
pub mod memo;
pub mod msgs;
pub mod packet;
pub mod relay;

pub use amount::*;
pub use coin::*;
Expand All @@ -27,3 +25,7 @@ pub const PORT_ID_STR: &str = "transfer";

/// ICS20 application current version.
pub const VERSION: &str = "ics20-1";

mod relay;

pub use relay::send_transfer::{send_transfer, send_transfer_execute, send_transfer_validate};
2 changes: 2 additions & 0 deletions crates/ibc/src/applications/transfer/msgs.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
//! Defines the token transfer message type

pub mod transfer;
13 changes: 6 additions & 7 deletions crates/ibc/src/applications/transfer/msgs/transfer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! This is the definition of a transfer messages that an application submits to a chain.
//! Defines the token transfer message type

use crate::prelude::*;

Expand All @@ -11,11 +11,10 @@ use crate::applications::transfer::packet::PacketData;
use crate::core::ics04_channel::error::PacketError;
use crate::core::ics04_channel::timeout::TimeoutHeight;
use crate::core::ics24_host::identifier::{ChannelId, PortId};
use crate::core::ContextError;
use crate::timestamp::Timestamp;
use crate::tx_msg::Msg;
use crate::core::timestamp::Timestamp;
use crate::core::{ContextError, Msg};

pub const TYPE_URL: &str = "/ibc.applications.transfer.v1.MsgTransfer";
pub(crate) const TYPE_URL: &str = "/ibc.applications.transfer.v1.MsgTransfer";

/// Message used to build an ICS20 token transfer packet.
///
Expand Down Expand Up @@ -119,7 +118,8 @@ impl TryFrom<Any> for MsgTransfer {

#[cfg(test)]
pub mod test_util {
use alloc::borrow::ToOwned;
use super::*;

use core::ops::Add;
use core::time::Duration;
use primitive_types::U256;
Expand All @@ -134,7 +134,6 @@ pub mod test_util {
applications::transfer::BaseCoin,
core::ics24_host::identifier::{ChannelId, PortId},
test_utils::get_dummy_bech32_account,
timestamp::Timestamp,
};

// Returns a dummy ICS20 `MsgTransfer`. If no `timeout_timestamp` is
Expand Down
3 changes: 3 additions & 0 deletions crates/ibc/src/applications/transfer/packet.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Contains the `PacketData` type that defines the structure of token transfers' packet bytes

use alloc::string::ToString;
use core::convert::TryFrom;
use core::str::FromStr;
Expand All @@ -8,6 +10,7 @@ use super::error::TokenTransferError;
use super::{Amount, Memo, PrefixedCoin, PrefixedDenom};
use crate::signer::Signer;

/// Defines the structure of token transfers' packet bytes
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "serde",
Expand Down
3 changes: 2 additions & 1 deletion crates/ibc/src/applications/transfer/relay.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! This module implements the processing logic for ICS20 (token transfer) message.
//! Implements the processing logic for ICS20 (token transfer) message.

use crate::applications::transfer::error::TokenTransferError;
use crate::applications::transfer::is_sender_chain_source;
use crate::applications::transfer::packet::PacketData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::applications::transfer::error::TokenTransferError;
use crate::applications::transfer::events::DenomTraceEvent;
use crate::applications::transfer::packet::PacketData;
use crate::applications::transfer::{is_receiver_chain_source, TracePrefix};
use crate::core::ics04_channel::handler::ModuleExtras;
use crate::core::ics04_channel::packet::Packet;
use crate::core::router::ModuleExtras;
use crate::prelude::*;

/// This function handles the transfer receiving logic.
Expand Down
8 changes: 4 additions & 4 deletions crates/ibc/src/applications/transfer/relay/send_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ use crate::applications::transfer::error::TokenTransferError;
use crate::applications::transfer::events::TransferEvent;
use crate::applications::transfer::msgs::transfer::MsgTransfer;
use crate::applications::transfer::{is_sender_chain_source, MODULE_ID_STR};
use crate::core::events::{MessageEvent, ModuleEvent};
use crate::core::ics04_channel::handler::send_packet::{send_packet_execute, send_packet_validate};
use crate::core::ics04_channel::packet::Packet;
use crate::core::ics24_host::path::{ChannelEndPath, SeqSendPath};
use crate::events::{MessageEvent, ModuleEvent};
use crate::prelude::*;

/// This function handles the transfer sending logic.
/// If this method returns an error, the runtime is expected to rollback all state modifications to
/// the `Ctx` caused by all messages from the transaction that this `msg` is a part of.
/// Initiate a token transfer. Equivalent to calling [`send_transfer_validate`], followed by [`send_transfer_execute`].
pub fn send_transfer<Ctx>(ctx_a: &mut Ctx, msg: MsgTransfer) -> Result<(), TokenTransferError>
where
Ctx: TokenTransferExecutionContext,
Expand All @@ -22,6 +20,7 @@ where
send_transfer_execute(ctx_a, msg)
}

/// Validates the token tranfer. If this succeeds, then it is legal to initiate the transfer with [`send_transfer_execute`].
pub fn send_transfer_validate<Ctx>(ctx_a: &Ctx, msg: MsgTransfer) -> Result<(), TokenTransferError>
where
Ctx: TokenTransferValidationContext,
Expand Down Expand Up @@ -85,6 +84,7 @@ where
Ok(())
}

/// Executes the token transfer. A prior call to [`send_transfer_validate`] MUST have succeeded.
pub fn send_transfer_execute<Ctx>(
ctx_a: &mut Ctx,
msg: MsgTransfer,
Expand Down
Loading

0 comments on commit ee16dc2

Please sign in to comment.