Skip to content

Commit

Permalink
feat: scaffold connection handshake module (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
Farhad-Shabani authored Nov 12, 2024
1 parent c6d149f commit a0af556
Show file tree
Hide file tree
Showing 17 changed files with 400 additions and 8 deletions.
27 changes: 27 additions & 0 deletions cairo-contracts/packages/contracts/src/core.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pub mod IBCCore {
use starknet_ibc_core::channel::ChannelHandlerComponent;
use starknet_ibc_core::client::ClientEventEmitterComponent;
use starknet_ibc_core::client::ClientHandlerComponent;
use starknet_ibc_core::connection::ConnectionEventEmitterComponent;
use starknet_ibc_core::connection::ConnectionHandlerComponent;
use starknet_ibc_core::router::RouterHandlerComponent;

// -----------------------------------------------------------
Expand All @@ -23,6 +25,23 @@ pub mod IBCCore {
ClientHandlerComponent::CoreRegisterClient<ContractState>;
impl ClientInitializerImpl = ClientHandlerComponent::ClientInitializerImpl<ContractState>;

// -----------------------------------------------------------
// Setup Connection Components
// -----------------------------------------------------------

component!(
path: ConnectionEventEmitterComponent,
storage: connection_emitter,
event: ConnectionEventEmitterEvent
);
component!(
path: ConnectionHandlerComponent, storage: connection_handler, event: ConnectionHandlerEvent
);

#[abi(embed_v0)]
impl CoreConnectionHandlerImpl =
ConnectionHandlerComponent::CoreConnectionHandler<ContractState>;

// -----------------------------------------------------------
// Setup Channel Components
// -----------------------------------------------------------
Expand Down Expand Up @@ -60,6 +79,10 @@ pub mod IBCCore {
#[substorage(v0)]
client_handler: ClientHandlerComponent::Storage,
#[substorage(v0)]
connection_emitter: ConnectionEventEmitterComponent::Storage,
#[substorage(v0)]
connection_handler: ConnectionHandlerComponent::Storage,
#[substorage(v0)]
channel_emitter: ChannelEventEmitterComponent::Storage,
#[substorage(v0)]
channel_handler: ChannelHandlerComponent::Storage,
Expand All @@ -75,6 +98,10 @@ pub mod IBCCore {
#[flat]
ClientHandlerEvent: ClientHandlerComponent::Event,
#[flat]
ConnectionEventEmitterEvent: ConnectionEventEmitterComponent::Event,
#[flat]
ConnectionHandlerEvent: ConnectionHandlerComponent::Event,
#[flat]
ChannelEventEmitterEvent: ChannelEventEmitterComponent::Event,
#[flat]
ChannelHandlerEvent: ChannelHandlerComponent::Event,
Expand Down
7 changes: 4 additions & 3 deletions cairo-contracts/packages/contracts/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ pub mod clients {
}
#[cfg(test)]
mod tests {
mod channel;
mod client;
mod transfer;
pub(crate) mod channel;
pub(crate) mod client;
pub(crate) mod connection;
pub(crate) mod transfer;
}
2 changes: 1 addition & 1 deletion cairo-contracts/packages/contracts/src/tests/channel.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use starknet_ibc_testkit::handles::{CoreContract, CoreHandle, AppHandle, ERC20Ha
use starknet_ibc_testkit::setup::SetupImpl;
use starknet_ibc_utils::ComputeKey;

fn setup() -> (
pub fn setup() -> (
CoreContract,
AppContract,
ERC20Contract,
Expand Down
71 changes: 71 additions & 0 deletions cairo-contracts/packages/contracts/src/tests/connection.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use starknet_ibc_contracts::tests::channel::setup;
use starknet_ibc_testkit::configs::CoreConfigTrait;
use starknet_ibc_testkit::handles::CoreHandle;

#[test]
fn test_conn_open_init_ok() {
// -----------------------------------------------------------
// Setup Essentials
// -----------------------------------------------------------

let (core, _, _, core_cfg, _, _, _) = setup();

// -----------------------------------------------------------
// Connection Open Init
// -----------------------------------------------------------

let msg = core_cfg.dummy_msg_conn_open_init();

core.conn_open_init(msg.clone());
}

#[test]
fn test_conn_open_try_ok() {
// -----------------------------------------------------------
// Setup Essentials
// -----------------------------------------------------------

let (core, _, _, core_cfg, _, _, _) = setup();

// -----------------------------------------------------------
// Connection Open Try
// -----------------------------------------------------------

let msg = core_cfg.dummy_msg_conn_open_try();

core.conn_open_try(msg.clone());
}

#[test]
fn test_conn_open_ack_ok() {
// -----------------------------------------------------------
// Setup Essentials
// -----------------------------------------------------------

let (core, _, _, core_cfg, _, _, _) = setup();

// -----------------------------------------------------------
// Connection Open Ack
// -----------------------------------------------------------

let msg = core_cfg.dummy_msg_conn_open_ack();

core.conn_open_ack(msg.clone());
}

#[test]
fn test_conn_open_confirm_ok() {
// -----------------------------------------------------------
// Setup Essentials
// -----------------------------------------------------------

let (core, _, _, core_cfg, _, _, _) = setup();

// -----------------------------------------------------------
// Connection Open Confirm
// -----------------------------------------------------------

let msg = core_cfg.dummy_msg_conn_open_confirm();

core.conn_open_confirm(msg.clone());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#[starknet::component]
pub mod ConnectionEventEmitterComponent {
#[storage]
pub struct Storage {}

#[event]
#[derive(Debug, Drop, starknet::Event)]
pub enum Event {
ConnOpenInitEvent: ConnOpenInitEvent,
ConnOpenTryEvent: ConnOpenTryEvent,
ConnOpenAckEvent: ConnOpenAckEvent,
ConnOpenConfirmEvent: ConnOpenConfirmEvent,
}

#[derive(Debug, Drop, starknet::Event)]
pub struct ConnOpenInitEvent {}

#[derive(Debug, Drop, starknet::Event)]
pub struct ConnOpenTryEvent {}

#[derive(Debug, Drop, starknet::Event)]
pub struct ConnOpenAckEvent {}

#[derive(Debug, Drop, starknet::Event)]
pub struct ConnOpenConfirmEvent {}

#[generate_trait]
pub impl ConnectionEventEmitterImpl<
TContractState, +HasComponent<TContractState>, +Drop<TContractState>
> of ConnectinoEventEmitterTrait<TContractState> {}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#[starknet::component]
pub mod ConnectionHandlerComponent {
use starknet_ibc_core::connection::{
ConnectionEventEmitterComponent, IConnectionHandler, IConnectionQuery, MsgConnOpenAck,
MsgConnOpenConfirm, MsgConnOpenInit, MsgConnOpenTry
};

#[storage]
pub struct Storage {}

#[event]
#[derive(Debug, Drop, starknet::Event)]
pub enum Event {}

// -----------------------------------------------------------
// IConnectionHandler
// -----------------------------------------------------------

#[embeddable_as(CoreConnectionHandler)]
pub impl CoreConnectionHandlerImpl<
TContractState,
+HasComponent<TContractState>,
+Drop<TContractState>,
impl EventEmitter: ConnectionEventEmitterComponent::HasComponent<TContractState>
> of IConnectionHandler<ComponentState<TContractState>> {
fn conn_open_init(ref self: ComponentState<TContractState>, msg: MsgConnOpenInit) {}

fn conn_open_try(ref self: ComponentState<TContractState>, msg: MsgConnOpenTry) {}

fn conn_open_ack(ref self: ComponentState<TContractState>, msg: MsgConnOpenAck) {}

fn conn_open_confirm(ref self: ComponentState<TContractState>, msg: MsgConnOpenConfirm) {}
}

// -----------------------------------------------------------
// IConnectionQuery
// -----------------------------------------------------------

#[embeddable_as(CoreConnectionQuery)]
impl CoreConnectionQueryImpl<
TContractState, +HasComponent<TContractState>, +Drop<TContractState>,
> of IConnectionQuery<ComponentState<TContractState>> {}

// -----------------------------------------------------------
// Connection Internal
// -----------------------------------------------------------

#[generate_trait]
pub(crate) impl ConnectionInternalImpl<
TContractState, +HasComponent<TContractState>, +Drop<TContractState>
> of ConnectionInternalTrait<TContractState> {}

// -----------------------------------------------------------
// Connection Reader/Writer
// -----------------------------------------------------------

#[generate_trait]
pub(crate) impl ConnectionReaderImpl<
TContractState, +HasComponent<TContractState>, +Drop<TContractState>
> of ConnectionReaderTrait<TContractState> {}

#[generate_trait]
pub(crate) impl ConnectionWriterImpl<
TContractState, +HasComponent<TContractState>, +Drop<TContractState>
> of ConnectionWriterTrait<TContractState> {}

// -----------------------------------------------------------
// Connection Event Emitter
// -----------------------------------------------------------

#[generate_trait]
pub(crate) impl EventEmitterImpl<
TContractState,
+HasComponent<TContractState>,
+Drop<TContractState>,
impl EventEmitter: ConnectionEventEmitterComponent::HasComponent<TContractState>
> of EventEmitterTrait<TContractState> {}
}

1 change: 1 addition & 0 deletions cairo-contracts/packages/core/src/connection/errors.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod ConnectionErrors {}
17 changes: 17 additions & 0 deletions cairo-contracts/packages/core/src/connection/interface.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use starknet_ibc_core::connection::{
MsgConnOpenAck, MsgConnOpenConfirm, MsgConnOpenInit, MsgConnOpenTry
};

#[starknet::interface]
pub trait IConnectionHandler<TContractState> {
fn conn_open_init(ref self: TContractState, msg: MsgConnOpenInit);

fn conn_open_try(ref self: TContractState, msg: MsgConnOpenTry);

fn conn_open_ack(ref self: TContractState, msg: MsgConnOpenAck);

fn conn_open_confirm(ref self: TContractState, msg: MsgConnOpenConfirm);
}

#[starknet::interface]
pub trait IConnectionQuery<TContractState> {}
29 changes: 29 additions & 0 deletions cairo-contracts/packages/core/src/connection/msgs.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use starknet_ibc_utils::ValidateBasic;

#[derive(Clone, Debug, Drop, PartialEq, Serde)]
pub struct MsgConnOpenInit {}

impl MsgConnOpenInitValidateBasic of ValidateBasic<MsgConnOpenInit> {
fn validate_basic(self: @MsgConnOpenInit) {}
}

#[derive(Clone, Debug, Drop, PartialEq, Serde)]
pub struct MsgConnOpenTry {}

impl MsgConnOpenTryValidateBasic of ValidateBasic<MsgConnOpenTry> {
fn validate_basic(self: @MsgConnOpenTry) {}
}

#[derive(Clone, Debug, Drop, PartialEq, Serde)]
pub struct MsgConnOpenAck {}

impl MsgConnOpenAckValidateBasic of ValidateBasic<MsgConnOpenAck> {
fn validate_basic(self: @MsgConnOpenAck) {}
}

#[derive(Clone, Debug, Drop, PartialEq, Serde)]
pub struct MsgConnOpenConfirm {}

impl MsgConnOpenConfirmValidateBasic of ValidateBasic<MsgConnOpenConfirm> {
fn validate_basic(self: @MsgConnOpenConfirm) {}
}
14 changes: 14 additions & 0 deletions cairo-contracts/packages/core/src/connection/types.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use starknet_ibc_core::host::{ClientId, ConnectionId, PathPrefix};

#[derive(Clone, Debug, Drop, PartialEq, Serde)]
pub struct Counterparty {
pub client_id: ClientId,
pub connection_id: ConnectionId,
pub prefix: PathPrefix,
}

#[derive(Clone, Debug, Drop, PartialEq, Serde)]
pub struct Version {
pub identifier: ByteArray,
pub features: Array<ByteArray>,
}
5 changes: 5 additions & 0 deletions cairo-contracts/packages/core/src/host/prefixes.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#[derive(Clone, Debug, Drop, PartialEq, Serde)]
pub struct PathPrefix {
pub prefix: ByteArray,
}

pub fn CHANNELS_PREFIX() -> ByteArray {
"channels"
}
Expand Down
Loading

0 comments on commit a0af556

Please sign in to comment.