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

Voyager packets #3311

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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
23 changes: 14 additions & 9 deletions Cargo.lock

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

11 changes: 10 additions & 1 deletion cosmwasm/union-ibc/app/ucs00-pingpong/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use cosmwasm_schema::cw_serde;
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{wasm_execute, DepsMut, Env, MessageInfo, Response, StdError};
Expand Down Expand Up @@ -59,7 +60,7 @@ pub fn execute(
&config.ibc_host,
&union_ibc_msg::msg::ExecuteMsg::WriteAcknowledgement(
MsgWriteAcknowledgement {
channel_id: packet.source_channel,
channel_id: packet.destination_channel,
packet,
acknowledgement: ack_success().into(),
},
Expand All @@ -81,6 +82,14 @@ pub fn execute(
}
}

#[cw_serde]
pub struct MigrateMsg {}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(_deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
Ok(Response::new())
}

fn enforce_version(version: &str, counterparty_version: Option<&str>) -> Result<(), ContractError> {
if version != PROTOCOL_VERSION {
return Err(ContractError::InvalidIbcVersion {
Expand Down
1 change: 1 addition & 0 deletions cosmwasm/union-ibc/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
sha2 = { workspace = true }
sha3 = { workspace = true }
strum = { version = "0.26.3", features = ["derive"] }
thiserror = { workspace = true }
union-ibc-msg = { workspace = true }
unionlabs = { workspace = true, features = ["ethabi"] }
4 changes: 4 additions & 0 deletions cosmwasm/union-ibc/core/msg/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use unionlabs::hash::H256;

#[derive(serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields, rename_all = "snake_case")]
pub enum QueryMsg {
Expand All @@ -10,4 +12,6 @@ pub enum QueryMsg {
GetConnection { connection_id: u32 },
GetChannel { channel_id: u32 },
GetChannels { contract: String },
GetBatchPackets { channel_id: u32, batch_hash: H256 },
GetBatchReceipts { channel_id: u32, batch_hash: H256 },
}
74 changes: 61 additions & 13 deletions cosmwasm/union-ibc/core/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use union_ibc_msg::{
use unionlabs::{
ethereum::keccak256,
hash::{hash_v2::HexPrefixed, H256},
ics24::ethabi::COMMITMENT_MAGIC,
ics24::ethabi::{BatchPacketsPath, BatchReceiptsPath, COMMITMENT_MAGIC},
};

use crate::{
Expand Down Expand Up @@ -60,11 +60,11 @@ pub mod events {
pub const CLOSE_CONFIRM: &str = "channel_close_confirm";
}
pub mod packet {
pub const SEND: &str = "packet_send";
pub const RECV: &str = "packet_recv";
pub const INTENT_RECV: &str = "packet_intent_recv";
pub const ACK: &str = "packet_ack";
pub const TIMEOUT: &str = "packet_timeout";
pub const SEND: &str = "send_packet";
pub const RECV: &str = "recv_packet";
pub const INTENT_RECV: &str = "intent_recv_packet";
pub const ACK: &str = "ack_packet";
pub const TIMEOUT: &str = "timeout_packet";
pub const BATCH_SEND: &str = "batch_send";
pub const BATCH_ACKS: &str = "batch_acks";
}
Expand Down Expand Up @@ -445,10 +445,15 @@ fn batch_acks(
let commitment = deps.storage.get(commitment_key.as_ref());
match commitment {
Some(acknowledgement) => {
let expected_ack = commit_ack(ack.to_vec());

if acknowledgement == COMMITMENT_MAGIC.as_ref() {
return Err(ContractError::AcknowledgementIsEmpty);
} else if acknowledgement != commit_ack(ack.to_vec()).as_ref() {
return Err(ContractError::AcknowledgementMismatch);
} else if acknowledgement != expected_ack.as_ref() {
return Err(ContractError::AcknowledgementMismatch {
found: acknowledgement.into(),
expected: expected_ack.into(),
});
}
}
None => return Err(ContractError::PacketCommitmentNotFound),
Expand Down Expand Up @@ -620,11 +625,11 @@ fn delete_packet_commitment(
}

fn commit_packet(packet: &Packet) -> H256 {
commit(packet.abi_encode_params())
commit(packet.abi_encode())
}

fn commit_packets(packets: &[Packet]) -> H256 {
commit(packets.abi_encode_params())
commit(packets.abi_encode())
}

fn register_client(
Expand Down Expand Up @@ -1395,7 +1400,11 @@ fn write_acknowledgement(
// make sure the caller owns the channel
let port_id = CHANNEL_OWNER.load(deps.storage, channel_id)?;
if port_id != sender {
return Err(ContractError::Unauthorized);
return Err(ContractError::Unauthorized {
channel_id,
owner: port_id,
caller: sender,
});
}

let commitment_key =
Expand Down Expand Up @@ -1445,7 +1454,11 @@ fn send_packet(

let port_id = CHANNEL_OWNER.load(deps.storage, source_channel)?;
if port_id != sender {
return Err(ContractError::Unauthorized);
return Err(ContractError::Unauthorized {
channel_id: source_channel,
owner: port_id,
caller: sender,
});
}

let channel = ensure_channel_state(deps.as_ref(), source_channel)?;
Expand All @@ -1467,7 +1480,7 @@ fn send_packet(
store_commit(deps.branch(), &commitment_key, &COMMITMENT_MAGIC)?;

Ok(Response::new()
.add_event(Event::new("send_packet").add_attribute(
.add_event(Event::new(events::packet::SEND).add_attribute(
events::attribute::PACKET,
serde_json::to_string(&packet).unwrap(),
))
Expand Down Expand Up @@ -1518,6 +1531,13 @@ fn store_commit(deps: DepsMut, key: &H256, value: &H256) -> Result<(), ContractE
Ok(())
}

fn read_commit(deps: Deps, key: &H256) -> Option<H256> {
deps.storage.get(key.as_ref()).map(|bz| {
bz.try_into()
.expect("H256 is the only value ever written to this storage; qed;")
})
}

fn save_connection(
deps: DepsMut,
connection_id: u32,
Expand Down Expand Up @@ -1664,6 +1684,34 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> Result<Binary, ContractErr
let connection = CONNECTIONS.load(deps.storage, connection_id)?;
Ok(to_json_binary(&connection)?)
}
QueryMsg::GetBatchPackets {
channel_id,
batch_hash,
} => {
let commit = read_commit(
deps,
&BatchPacketsPath {
channel_id,
batch_hash,
}
.key(),
);
Ok(to_json_binary(&commit)?)
}
QueryMsg::GetBatchReceipts {
channel_id,
batch_hash,
} => {
let commit = read_commit(
deps,
&BatchReceiptsPath {
channel_id,
batch_hash,
}
.key(),
);
Ok(to_json_binary(&commit)?)
}
}
}

Expand Down
Loading
Loading