Skip to content

Commit

Permalink
feat: split voyager ibc state query rpc and use Bytes more (#3194)
Browse files Browse the repository at this point in the history
  • Loading branch information
benluelo authored Nov 1, 2024
2 parents 9b00a12 + 48944db commit 09f2b02
Show file tree
Hide file tree
Showing 41 changed files with 1,384 additions and 976 deletions.
12 changes: 1 addition & 11 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion hubble/src/bera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use beacon_api_types::{
execution_payload_header::ExecutionPayloadHeader, ExecutionPayloadHeaderSsz, Mainnet,
};
use color_eyre::{eyre::eyre, Result};
use cometbft_rpc::{types::AbciQueryResponse, Client};
use cometbft_rpc::{rpc_types::AbciQueryResponse, Client};
use tracing::info;
use unionlabs::encoding::DecodeAs;

Expand Down
9 changes: 5 additions & 4 deletions lib/cometbft-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ use unionlabs::{
option_unwrap, result_unwrap,
};

use crate::types::{
use crate::rpc_types::{
AbciQueryResponse, AllValidatorsResponse, BlockResponse, BroadcastTxSyncResponse,
CommitResponse, Order, StatusResponse, TxResponse, TxSearchResponse, ValidatorsResponse,
};

#[cfg(test)]
mod tests;

pub mod rpc_types;
pub mod serde;
pub mod types;
pub use cometbft_types as types;

pub type JsonRpcError = jsonrpsee::core::client::Error;

Expand Down Expand Up @@ -81,7 +82,7 @@ impl Client {
pub async fn validators(
&self,
height: Option<NonZeroU64>,
pagination: Option<types::ValidatorsPagination>,
pagination: Option<rpc_types::ValidatorsPagination>,
) -> Result<ValidatorsResponse, JsonRpcError> {
self.inner
.request(
Expand Down Expand Up @@ -116,7 +117,7 @@ impl Client {
} = self
.validators(
height,
Some(types::ValidatorsPagination {
Some(rpc_types::ValidatorsPagination {
page,
per_page: Some(PER_PAGE),
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ use unionlabs::{
},
};

use crate::serde::serde_as;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "snake_case")]
pub enum Order {
Expand Down Expand Up @@ -102,7 +100,6 @@ pub struct ValidatorsPagination {
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AbciQueryResponse {
#[serde(deserialize_with = "serde_as::<_, protos::cometbft::abci::v1::QueryResponse, _>")]
pub response: QueryResponse,
}

Expand Down
2 changes: 1 addition & 1 deletion lib/cometbft-rpc/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ mod validators {
fn validators() {
ensure_json(
"testdata/validators/bartio-6760022.json",
crate::types::ValidatorsResponse {
crate::rpc_types::ValidatorsResponse {
block_height: NonZeroU64::new(6760022).unwrap(),
validators: vec![
Validator {
Expand Down
23 changes: 10 additions & 13 deletions lib/ibc-events/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use core::{num::NonZeroU64, str::FromStr};

use cometbft_types::abci::event::Event;
use unionlabs::{
bytes::Bytes,
hash::hash_v2::HexUnprefixed,
ibc::core::{channel::order::Order, client::height::Height},
id::{ChannelId, ClientId, ConnectionId, PortId},
};
Expand Down Expand Up @@ -302,9 +304,8 @@ event! {
deprecated("packet_data", "packet_ack", "packet_connection")
)]
WriteAcknowledgement {
#[parse(hex::decode)]
#[serde(with = "::serde_utils::hex_string")]
packet_data_hex: Vec<u8>,
#[parse(|s: &str| s.parse::<Bytes<HexUnprefixed>>().map(|b| b.into_encoding()))]
packet_data_hex: Bytes,
#[parse(Height::from_str)]
packet_timeout_height: Height,
#[parse(u64::from_str)]
Expand All @@ -319,18 +320,16 @@ event! {
packet_dst_port: PortId,
#[parse(ChannelId::from_str_prefixed)]
packet_dst_channel: ChannelId,
#[parse(hex::decode)]
#[serde(with = "::serde_utils::hex_string")]
packet_ack_hex: Vec<u8>,
#[parse(|s: &str| s.parse::<Bytes<HexUnprefixed>>().map(|b| b.into_encoding()))]
packet_ack_hex: Bytes,
#[parse(ConnectionId::from_str_prefixed)]
connection_id: ConnectionId,
},

#[event(tag = "recv_packet", deprecated("packet_data", "packet_connection"))]
RecvPacket {
#[parse(hex::decode)]
#[serde(with = "::serde_utils::hex_string")]
packet_data_hex: Vec<u8>,
#[parse(|s: &str| s.parse::<Bytes<HexUnprefixed>>().map(|b| b.into_encoding()))]
packet_data_hex: Bytes,
#[parse(Height::from_str)]
packet_timeout_height: Height,
#[parse(u64::from_str)]
Expand All @@ -353,11 +352,9 @@ event! {

#[event(tag = "send_packet", deprecated("packet_data", "packet_connection"))]
SendPacket {
#[parse(hex::decode)]
#[serde(with = "::serde_utils::hex_string")]
packet_data_hex: Vec<u8>,
#[parse(|s: &str| s.parse::<Bytes<HexUnprefixed>>().map(|b| b.into_encoding()))]
packet_data_hex: Bytes,
#[parse(Height::from_str)]
// TODO: Make this generic height instead of concrete
packet_timeout_height: Height,
#[parse(u64::from_str)]
packet_timeout_timestamp: u64,
Expand Down
6 changes: 3 additions & 3 deletions lib/ibc-vm-rs/src/states/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ pub fn write_acknowledgement<T: IbcHost>(
packet_src_channel: packet.source_channel,
packet_dst_port: packet.destination_port,
packet_dst_channel: packet.destination_channel,
packet_ack_hex: hex::encode(ack).into_bytes(),
packet_ack_hex: hex::encode(ack).into_bytes().into(),
connection_id: channel.connection_hops[0].clone(),
})
}
Expand Down Expand Up @@ -472,7 +472,7 @@ impl<T: IbcHost> Runnable<T> for SendPacket {
source_channel,
destination_port,
destination_channel,
data,
data: data.into(),
timeout_height,
timeout_timestamp,
};
Expand Down Expand Up @@ -521,7 +521,7 @@ fn packet_commitment<T: IbcHost>(host: &mut T, packet: &Packet) -> Vec<u8> {
packet_commitment.extend_from_slice(packet.timeout_timestamp.to_be_bytes().as_slice());
packet_commitment.extend_from_slice(packet.timeout_height.revision().to_be_bytes().as_slice());
packet_commitment.extend_from_slice(packet.timeout_height.height().to_be_bytes().as_slice());
packet_commitment.extend_from_slice(host.sha256(packet.data.clone()).as_slice());
packet_commitment.extend_from_slice(host.sha256(packet.data.clone().into_vec()).as_slice());
packet_commitment
}

Expand Down
24 changes: 18 additions & 6 deletions lib/unionlabs/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,24 @@ impl<E: Encoding> From<Vec<u8>> for Bytes<E> {
}
}

impl<E: Encoding> From<Bytes<E>> for Vec<u8> {
fn from(value: Bytes<E>) -> Self {
value.deref().into()
}
}

impl<E: Encoding> From<Cow<'static, [u8]>> for Bytes<E> {
fn from(value: Cow<'static, [u8]>) -> Self {
Self::new(value)
}
}

impl<E: Encoding> From<Bytes<E>> for Cow<'static, [u8]> {
fn from(value: Bytes<E>) -> Self {
value.bytes
}
}

impl<E: Encoding> From<&Vec<u8>> for Bytes<E> {
fn from(value: &Vec<u8>) -> Self {
Self::new(value.to_owned())
Expand All @@ -216,12 +234,6 @@ impl<E: Encoding> From<&[u8]> for Bytes<E> {
}
}

impl<E: Encoding> From<Bytes<E>> for Vec<u8> {
fn from(value: Bytes<E>) -> Self {
value.deref().into()
}
}

// TODO: Feature gate rlp across the crate
// #[cfg(feature = "rlp")]
impl<E: Encoding> rlp::Decodable for Bytes<E> {
Expand Down
13 changes: 6 additions & 7 deletions lib/unionlabs/src/ibc/core/channel/msg_acknowledgement.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use macros::model;

use crate::ibc::core::{channel::packet::Packet, client::height::Height};
use crate::{
bytes::Bytes,
ibc::core::{channel::packet::Packet, client::height::Height},
};

#[model(proto(raw(protos::ibc::core::channel::v1::MsgAcknowledgement)))]
pub struct MsgAcknowledgement {
pub packet: Packet,
#[serde(with = "::serde_utils::hex_string")]
#[debug(wrap = ::serde_utils::fmt::DebugAsHex)]
pub acknowledgement: Vec<u8>,
#[serde(with = "::serde_utils::hex_string")]
#[debug(wrap = ::serde_utils::fmt::DebugAsHex)]
pub proof_acked: Vec<u8>,
pub acknowledgement: Bytes,
pub proof_acked: Bytes,
pub proof_height: Height,
}
5 changes: 2 additions & 3 deletions lib/unionlabs/src/ibc/core/channel/msg_channel_open_ack.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use macros::model;

use crate::{
bytes::Bytes,
ibc::core::client::height::Height,
id::{ChannelId, PortId},
};
Expand All @@ -12,8 +13,6 @@ pub struct MsgChannelOpenAck {
pub counterparty_channel_id: ChannelId,
// yes, this is actually just an unbounded string
pub counterparty_version: String,
#[serde(with = "::serde_utils::hex_string")]
#[debug(wrap = ::serde_utils::fmt::DebugAsHex)]
pub proof_try: Vec<u8>,
pub proof_try: Bytes,
pub proof_height: Height,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use macros::model;

use crate::{
bytes::Bytes,
ibc::core::client::height::Height,
id::{ChannelId, PortId},
};
Expand All @@ -9,8 +10,6 @@ use crate::{
pub struct MsgChannelOpenConfirm {
pub port_id: PortId,
pub channel_id: ChannelId,
#[serde(with = "::serde_utils::hex_string")]
#[debug(wrap = ::serde_utils::fmt::DebugAsHex)]
pub proof_ack: Vec<u8>,
pub proof_ack: Bytes,
pub proof_height: Height,
}
5 changes: 2 additions & 3 deletions lib/unionlabs/src/ibc/core/channel/msg_channel_open_try.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use macros::model;

use crate::{
bytes::Bytes,
ibc::core::{channel::channel::Channel, client::height::Height},
id::PortId,
};
Expand All @@ -10,8 +11,6 @@ pub struct MsgChannelOpenTry {
pub port_id: PortId,
pub channel: Channel,
pub counterparty_version: String,
#[serde(with = "::serde_utils::hex_string")]
#[debug(wrap = ::serde_utils::fmt::DebugAsHex)]
pub proof_init: Vec<u8>,
pub proof_init: Bytes,
pub proof_height: Height,
}
9 changes: 5 additions & 4 deletions lib/unionlabs/src/ibc/core/channel/msg_recv_packet.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use macros::model;

use crate::ibc::core::{channel::packet::Packet, client::height::Height};
use crate::{
bytes::Bytes,
ibc::core::{channel::packet::Packet, client::height::Height},
};

#[model(proto(raw(protos::ibc::core::channel::v1::MsgRecvPacket)))]
pub struct MsgRecvPacket {
pub packet: Packet,
#[serde(with = "::serde_utils::hex_string")]
#[debug(wrap = ::serde_utils::fmt::DebugAsHex)]
pub proof_commitment: Vec<u8>,
pub proof_commitment: Bytes,
pub proof_height: Height,
}
11 changes: 5 additions & 6 deletions lib/unionlabs/src/ibc/core/channel/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use core::num::{NonZeroU64, TryFromIntError};
use macros::model;

use crate::{
bytes::Bytes,
errors::{required, MissingField},
ibc::core::client::height::Height,
id::{ChannelId, Ics24IdParseError, ParsePrefixedIdError, PortId},
Expand All @@ -15,9 +16,7 @@ pub struct Packet {
pub source_channel: ChannelId,
pub destination_port: PortId,
pub destination_channel: ChannelId,
#[serde(with = "::serde_utils::hex_string")]
#[debug(wrap = ::serde_utils::fmt::DebugAsHex)]
pub data: Vec<u8>,
pub data: Bytes,
pub timeout_height: Height,
pub timeout_timestamp: u64,
}
Expand All @@ -30,7 +29,7 @@ impl From<Packet> for protos::ibc::core::channel::v1::Packet {
source_channel: value.source_channel.to_string_prefixed(),
destination_port: value.destination_port.to_string(),
destination_channel: value.destination_channel.to_string_prefixed(),
data: value.data,
data: value.data.into(),
timeout_height: Some(value.timeout_height.into()),
timeout_timestamp: value.timeout_timestamp,
}
Expand Down Expand Up @@ -74,7 +73,7 @@ impl TryFrom<protos::ibc::core::channel::v1::Packet> for Packet {
.map_err(TryFromPacketError::DestinationPort)?,
destination_channel: ChannelId::from_str_prefixed(&proto.destination_channel)
.map_err(TryFromPacketError::DestinationChannel)?,
data: proto.data,
data: proto.data.into(),
timeout_height: required!(proto.timeout_height)?.into(),
timeout_timestamp: proto.timeout_timestamp,
})
Expand All @@ -97,7 +96,7 @@ impl From<Packet> for contracts::ibc_handler::IbcCoreChannelV1PacketData {
source_channel: value.source_channel.to_string_prefixed(),
destination_port: value.destination_port.to_string(),
destination_channel: value.destination_channel.to_string_prefixed(),
data: value.data.into(),
data: value.data.into_vec().into(),
timeout_height: contracts::ibc_handler::IbcCoreClientV1HeightData {
revision_number: value.timeout_height.revision(),
revision_height: value.timeout_height.height(),
Expand Down
10 changes: 4 additions & 6 deletions lib/unionlabs/src/ibc/core/client/msg_create_client.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use macros::model;

use crate::bytes::Bytes;

#[model(proto(raw(protos::ibc::core::client::v1::MsgCreateClient)))]
pub struct MsgCreateClient {
#[serde(with = "::serde_utils::hex_string")]
#[debug(wrap = ::serde_utils::fmt::DebugAsHex)]
pub client_state: Vec<u8>,
#[serde(with = "::serde_utils::hex_string")]
#[debug(wrap = ::serde_utils::fmt::DebugAsHex)]
pub consensus_state: Vec<u8>,
pub client_state: Bytes,
pub consensus_state: Bytes,
}
Loading

0 comments on commit 09f2b02

Please sign in to comment.