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

feat: improve IBC packet structure #110

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
58 changes: 27 additions & 31 deletions contracts/babylon/src/ibc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::error::ContractError;
use babylon_bindings::BabylonMsg;
use babylon_proto::babylon::zoneconcierge::v1::{
zoneconcierge_packet_data::Packet, BtcTimestamp, ZoneconciergePacketData,
outbound_packet::Packet as OutboundPacketType, BtcTimestamp, OutboundPacket,
};

use crate::state::config::CONFIG;
Expand Down Expand Up @@ -123,33 +123,26 @@ pub fn ibc_packet_receive(
_env: Env,
msg: IbcPacketReceiveMsg,
) -> Result<IbcReceiveResponse<BabylonMsg>, Never> {
// put this in a closure so we can convert all error responses into acknowledgements
(|| {
let packet = msg.packet;
// which local channel did this packet come on
let caller = packet.dest.channel_id;
let zc_packet_data =
ZoneconciergePacketData::decode(packet.data.as_slice()).map_err(|e| {
StdError::generic_err(format!("failed to decode ZoneconciergePacketData: {e}"))
})?;
let zc_packet = zc_packet_data
let packet_data = OutboundPacket::decode(packet.data.as_slice())
.map_err(|e| StdError::generic_err(format!("failed to decode OutboundPacket: {e}")))?;
let outbound_packet = packet_data
.packet
.ok_or(StdError::generic_err("empty IBC packet"))?;
match zc_packet {
Packet::BtcTimestamp(btc_ts) => ibc_packet::handle_btc_timestamp(deps, caller, &btc_ts),
Packet::BtcStaking(btc_staking) => {
match outbound_packet {
OutboundPacketType::BtcTimestamp(btc_ts) => {
ibc_packet::handle_btc_timestamp(deps, caller, &btc_ts)
}
OutboundPacketType::BtcStaking(btc_staking) => {
ibc_packet::handle_btc_staking(deps, caller, &btc_staking)
}
Packet::ConsumerSlashing(_) => Err(StdError::generic_err(
"ConsumerSlashing packet should not be received",
)),
}
})()
.or_else(|e| {
// we try to capture all app-level errors and convert them into
// acknowledgement packets that contain an error code.
Ok(
IbcReceiveResponse::new(StdAck::error(format!("invalid packet: {e}"))) // TODO: design error ack format
IbcReceiveResponse::new(StdAck::error(format!("invalid packet: {e}")))
.add_event(Event::new("ibc").add_attribute("packet", "receive")),
)
})
Expand All @@ -165,8 +158,9 @@ pub(crate) mod ibc_packet {
};
use babylon_apis::finality_api::Evidence;
use babylon_proto::babylon::btcstaking::v1::BtcStakingIbcPacket;
use babylon_proto::babylon::zoneconcierge::v1::zoneconcierge_packet_data::Packet::ConsumerSlashing;
use babylon_proto::babylon::zoneconcierge::v1::ConsumerSlashingIbcPacket;
use babylon_proto::babylon::zoneconcierge::v1::{
inbound_packet::Packet as InboundPacketType, ConsumerSlashingIbcPacket, InboundPacket,
};
use cosmwasm_std::{to_json_binary, IbcChannel, IbcMsg, WasmMsg};

pub fn handle_btc_timestamp(
Expand Down Expand Up @@ -263,18 +257,20 @@ pub(crate) mod ibc_packet {
channel: &IbcChannel,
evidence: &Evidence,
) -> Result<IbcMsg, ContractError> {
let packet = ZoneconciergePacketData {
packet: Some(ConsumerSlashing(ConsumerSlashingIbcPacket {
evidence: Some(babylon_proto::babylon::finality::v1::Evidence {
fp_btc_pk: evidence.fp_btc_pk.to_vec().into(),
block_height: evidence.block_height,
pub_rand: evidence.pub_rand.to_vec().into(),
canonical_app_hash: evidence.canonical_app_hash.to_vec().into(),
fork_app_hash: evidence.fork_app_hash.to_vec().into(),
canonical_finality_sig: evidence.canonical_finality_sig.to_vec().into(),
fork_finality_sig: evidence.fork_finality_sig.to_vec().into(),
}),
})),
let packet = InboundPacket {
packet: Some(InboundPacketType::ConsumerSlashing(
ConsumerSlashingIbcPacket {
evidence: Some(babylon_proto::babylon::finality::v1::Evidence {
fp_btc_pk: evidence.fp_btc_pk.to_vec().into(),
block_height: evidence.block_height,
pub_rand: evidence.pub_rand.to_vec().into(),
canonical_app_hash: evidence.canonical_app_hash.to_vec().into(),
fork_app_hash: evidence.fork_app_hash.to_vec().into(),
canonical_finality_sig: evidence.canonical_finality_sig.to_vec().into(),
fork_finality_sig: evidence.fork_finality_sig.to_vec().into(),
}),
},
)),
};
let msg = IbcMsg::SendPacket {
channel_id: channel.endpoint.channel_id.clone(),
Expand Down
2 changes: 1 addition & 1 deletion packages/proto/babylon
gusin13 marked this conversation as resolved.
Show resolved Hide resolved
Submodule babylon updated 143 files
3 changes: 2 additions & 1 deletion packages/proto/buf.gen.rust.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ types:
- babylon.zoneconcierge.v1.ProofFinalizedChainInfo
- babylon.zoneconcierge.v1.FinalizedChainInfo
- babylon.zoneconcierge.v1.QueryFinalizedChainsInfoResponse
- babylon.zoneconcierge.v1.ZoneconciergePacketData
- babylon.zoneconcierge.v1.OutboundPacket
- babylon.zoneconcierge.v1.InboundPacket
- babylon.zoneconcierge.v1.BTCTimestamp
- babylon.btcstaking.v1.FinalityProvider
- babylon.btcstaking.v1.BTCDelegation
Expand Down
31 changes: 23 additions & 8 deletions packages/proto/src/gen/babylon.zoneconcierge.v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,16 @@ pub struct ProofFinalizedChainInfo {
#[prost(message, repeated, tag="3")]
pub proof_epoch_submitted: ::prost::alloc::vec::Vec<super::super::btccheckpoint::v1::TransactionInfo>,
}
/// ZoneconciergePacketData is the message that defines the IBC packets of
/// ZoneConcierge
/// OutboundPacket represents packets sent from Babylon to other chains
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct ZoneconciergePacketData {
pub struct OutboundPacket {
/// packet is the actual message carried in the IBC packet
#[prost(oneof="zoneconcierge_packet_data::Packet", tags="1, 2, 3")]
pub packet: ::core::option::Option<zoneconcierge_packet_data::Packet>,
#[prost(oneof="outbound_packet::Packet", tags="1, 2")]
pub packet: ::core::option::Option<outbound_packet::Packet>,
}
/// Nested message and enum types in `ZoneconciergePacketData`.
pub mod zoneconcierge_packet_data {
/// Nested message and enum types in `OutboundPacket`.
pub mod outbound_packet {
/// packet is the actual message carried in the IBC packet
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Oneof)]
Expand All @@ -167,7 +166,23 @@ pub mod zoneconcierge_packet_data {
BtcTimestamp(super::BtcTimestamp),
#[prost(message, tag="2")]
BtcStaking(super::super::super::btcstaking::v1::BtcStakingIbcPacket),
#[prost(message, tag="3")]
}
}
/// InboundPacket represents packets received by Babylon from other chains
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct InboundPacket {
/// packet is the actual message carried in the IBC packet
#[prost(oneof="inbound_packet::Packet", tags="1")]
pub packet: ::core::option::Option<inbound_packet::Packet>,
}
/// Nested message and enum types in `InboundPacket`.
pub mod inbound_packet {
/// packet is the actual message carried in the IBC packet
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Oneof)]
pub enum Packet {
#[prost(message, tag="1")]
ConsumerSlashing(super::ConsumerSlashingIbcPacket),
}
}
Expand Down