Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Channel impl #5

Merged
merged 20 commits into from
Oct 30, 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
21 changes: 21 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ members = [
"client-impls/mock/meta",
"client-impls/qbft",
"client-impls/qbft/meta",
"channel",
"channel/meta",
"connection",
"connection/meta",
"host",
Expand Down
30 changes: 30 additions & 0 deletions channel/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "channel"
version = "0.0.0"
publish = false
edition = "2021"
authors = ["you"]

[lib]
path = "src/lib.rs"

[dependencies.multiversx-sc]
version = "=0.53.0"

[dependencies.common-types]
path = "../common/common-types"

[dependencies.common-modules]
path = "../common/common-modules"

[dependencies.client-common]
path = "../client-impls/client-common"

[dependencies.host]
path = "../host"

[dev-dependencies]
num-bigint = "0.4"

[dev-dependencies.multiversx-sc-scenario]
version = "=0.53.0"
12 changes: 12 additions & 0 deletions channel/meta/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "channel-meta"
version = "0.0.0"
edition = "2021"
publish = false

[dependencies.channel]
path = ".."

[dependencies.multiversx-sc-meta-lib]
version = "=0.53.0"
default-features = false
3 changes: 3 additions & 0 deletions channel/meta/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
multiversx_sc_meta_lib::cli_main::<channel::AbiProvider>();
}
3 changes: 3 additions & 0 deletions channel/multiversx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"language": "rust"
}
43 changes: 43 additions & 0 deletions channel/src/channel_libs/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use common_types::{channel_types::height, ChannelId, PortId, Sequence, Timestamp};

use super::packet_types::Packet;

multiversx_sc::imports!();
multiversx_sc::derive_imports!();

#[derive(TypeAbi, TopEncode)]
pub struct SendPacketEventData<'a, M: ManagedTypeApi> {
pub seq: Sequence,
pub source_port: &'a PortId<M>,
pub source_channel: &'a ChannelId<M>,
pub timeout_height: height::Data,
pub timeout_timestamp: Timestamp,
pub data: &'a ManagedBuffer<M>,
}

#[multiversx_sc::module]
pub trait EventsModule {
#[event("generatedChannelIdEvent")]
fn generated_channel_id_event(&self, #[indexed] channel_id: &ChannelId<Self::Api>);

#[event("writeAckEvent")]
fn write_ack_event(
&self,
#[indexed] dest_port_id: &PortId<Self::Api>,
#[indexed] dest_channel: &ChannelId<Self::Api>,
#[indexed] seq: Sequence,
ack: &ManagedBuffer,
);

#[event("sendPacketEvent")]
fn send_packet_event(&self, data: SendPacketEventData<Self::Api>);

#[event("receivePacketEvent")]
fn receive_packet_event(&self, #[indexed] packet: &Packet<Self::Api>);

#[event("ackPacketEvent")]
fn ack_packet_event(&self, #[indexed] packet: &Packet<Self::Api>, ack: &ManagedBuffer);

#[event("timeoutPacketEvent")]
fn timeout_packet_event(&self, #[indexed] packet: &Packet<Self::Api>);
}
54 changes: 54 additions & 0 deletions channel/src/channel_libs/handshake_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use common_types::{
channel_types::{channel, height},
ChannelId, Hash, PortId, Version,
};

multiversx_sc::imports!();
multiversx_sc::derive_imports!();

#[derive(TypeAbi, TopEncode, TopDecode)]
pub struct MsgChannelOpenInit<M: ManagedTypeApi> {
pub port_id: PortId<M>,
pub channel: channel::Data<M>,
}

#[derive(TypeAbi, TopEncode, TopDecode)]
pub struct MsgChannelOpenTry<M: ManagedTypeApi> {
pub port_id: PortId<M>,
pub channel: channel::Data<M>,
pub counterparty_version: Version<M>,
pub proof_init: Hash<M>,
pub proof_height: height::Data,
}

#[derive(TypeAbi, TopEncode, TopDecode)]
pub struct MsgChannelOpenAck<M: ManagedTypeApi> {
pub port_id: PortId<M>,
pub channel_id: ChannelId<M>,
pub counterparty_version: Version<M>,
pub counterparty_channel_id: ChannelId<M>,
pub proof_try: Hash<M>,
pub proof_height: height::Data,
}

#[derive(TypeAbi, TopEncode, TopDecode)]
pub struct MsgChannelOpenConfirm<M: ManagedTypeApi> {
pub port_id: PortId<M>,
pub channel_id: ChannelId<M>,
pub proof_ack: Hash<M>,
pub proof_height: height::Data,
}

#[derive(TypeAbi, TopEncode, TopDecode)]
pub struct MsgChannelCloseInit<M: ManagedTypeApi> {
pub port_id: PortId<M>,
pub channel_id: ChannelId<M>,
}

#[derive(TypeAbi, TopEncode, TopDecode)]
pub struct MsgChannelCloseConfirm<M: ManagedTypeApi> {
pub port_id: PortId<M>,
pub channel_id: ChannelId<M>,
pub proof_init: Hash<M>,
pub proof_height: height::Data,
}
23 changes: 23 additions & 0 deletions channel/src/channel_libs/ibc_channel_lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use common_types::Hash;

use super::packet_types::PacketReceipt;

multiversx_sc::imports!();
multiversx_sc::derive_imports!();

#[multiversx_sc::module]
pub trait IbcChannelLibModule: common_modules::utils::UtilsModule {
fn receipt_commitment_to_receipt(&self, commitment: &Hash<Self::Api>) -> PacketReceipt {
if self.is_empty_hash(commitment) {
return PacketReceipt::None;
}

let encoded_success = self.encode_to_buffer(&PacketReceipt::Successful);
let successful_hash = self.crypto().keccak256(&encoded_success);
if commitment == &successful_hash {
return PacketReceipt::Successful;
}

sc_panic!("Unknown channel packet receipt commitment");
}
}
4 changes: 4 additions & 0 deletions channel/src/channel_libs/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod events;
pub mod handshake_types;
pub mod ibc_channel_lib;
pub mod packet_types;
126 changes: 126 additions & 0 deletions channel/src/channel_libs/packet_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
use common_types::{channel_types::height, ChannelId, Hash, PortId, Sequence, Timestamp};

multiversx_sc::imports!();
multiversx_sc::derive_imports!();

#[derive(TopEncode)]
pub enum PacketReceipt {
None,
Successful,
}

/// `Packet` defines a type that carries data across different chains through IBC.
///
/// `seq` corresponds to the order of sends and receives, where a packet with an earlier sequence number must be sent and received before a packet with a later sequence number
///
/// `src_port` identifies the port on the sending chain
///
/// `src_channel` identifies the channel end on the sending chain
///
/// `dest_port` identifies the port on the receiving chain
///
/// `dest_channel` identifies the channel end on the receiving chain
///
/// `data` is an opaque value which can be defined by the application logic of the associated modules
///
/// `timeout_height` indicates a consensus height on the destination chain after which the packet will no longer be processed, and will instead count as having timed-out
///
/// `timeout_timestamp` indicates a timestamp on the destination chain after which the packet will no longer be processed, and will instead count as having timed-out
#[derive(TypeAbi, TopEncode, TopDecode, NestedEncode, NestedDecode, Clone)]
pub struct Packet<M: ManagedTypeApi> {
pub seq: Sequence,
pub src_port: PortId<M>,
pub src_channel: ChannelId<M>,
pub dest_port: PortId<M>,
pub dest_channel: ChannelId<M>,
pub data: ManagedBuffer<M>,
pub timeout_height: height::Data,
pub timeout_timestamp: Timestamp,
}

#[derive(TypeAbi, TopEncode, TopDecode)]
pub struct MsgPacketRecv<M: ManagedTypeApi> {
pub packet: Packet<M>,
pub proof: Hash<M>,
pub proof_height: height::Data,
}

#[derive(TypeAbi, TopEncode, TopDecode)]
pub struct MsgPacketAcknowledgement<M: ManagedTypeApi> {
pub packet: Packet<M>,
pub ack: ManagedBuffer<M>, // TODO: Or is it Hash<M>?
pub proof: Hash<M>,
pub proof_height: height::Data,
}

#[derive(TypeAbi, TopEncode, TopDecode)]
pub struct MsgTimeoutPacket<M: ManagedTypeApi> {
pub packet: Packet<M>,
pub proof: Hash<M>,
pub proof_height: height::Data,
pub next_seq_recv: Sequence,
}

#[derive(TypeAbi, TopEncode, TopDecode)]
pub struct MsgTimeoutOnClose<M: ManagedTypeApi> {
pub packet: Packet<M>,
pub proof_unreceived: Hash<M>,
pub proof_close: Hash<M>,
pub proof_height: height::Data,
pub next_seq_recv: Sequence,
pub counterparty_upgrade_seq: Sequence,
}

pub trait TimeoutArgs<M: ManagedTypeApi> {
fn get_packet(&self) -> &Packet<M>;

fn get_proof(&self) -> &Hash<M>;

fn get_proof_height(&self) -> height::Data;

fn get_next_seq_recv(&self) -> Sequence;
}

impl<M: ManagedTypeApi> TimeoutArgs<M> for MsgTimeoutPacket<M> {
#[inline(always)]
fn get_packet(&self) -> &Packet<M> {
&self.packet
}

#[inline(always)]
fn get_proof(&self) -> &Hash<M> {
&self.proof
}

#[inline(always)]
fn get_proof_height(&self) -> height::Data {
self.proof_height
}

#[inline(always)]
fn get_next_seq_recv(&self) -> Sequence {
self.next_seq_recv
}
}

impl<M: ManagedTypeApi> TimeoutArgs<M> for MsgTimeoutOnClose<M> {
#[inline(always)]
fn get_packet(&self) -> &Packet<M> {
&self.packet
}

#[inline(always)]
fn get_proof(&self) -> &Hash<M> {
&self.proof_unreceived
}

#[inline(always)]
fn get_proof_height(&self) -> height::Data {
self.proof_height
}

#[inline(always)]
fn get_next_seq_recv(&self) -> Sequence {
self.next_seq_recv
}
}
25 changes: 25 additions & 0 deletions channel/src/interfaces/client_interface.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pub mod generic_client_proxy {
use client_common::{GetLatestInfoResultType, VerifyMembershipArgs, VerifyNonMembershipArgs};
use common_types::{channel_types::height, ClientId, Timestamp};

multiversx_sc::imports!();

#[multiversx_sc::proxy]
pub trait GenericClientProxy {
#[view(getTimestampAtHeight)]
fn get_timestamp_at_height(
&self,
client_id: &ClientId<Self::Api>,
height: &height::Data,
) -> Timestamp;

#[view(getLatestInfo)]
fn get_latest_info(&self, client_id: ClientId<Self::Api>) -> GetLatestInfoResultType;

#[view(verifyMembership)]
fn verify_membership(&self, args: VerifyMembershipArgs<Self::Api>) -> bool;

#[view(verifyNonMembership)]
fn verify_non_membership(&self, args: VerifyNonMembershipArgs<Self::Api>) -> bool;
}
}
Loading
Loading