Skip to content

Commit

Permalink
Contract updates addressing the ibc-go wasm light client refactoring (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
misko9 authored Dec 15, 2023
1 parent 470fc17 commit 5fd0719
Show file tree
Hide file tree
Showing 47 changed files with 1,114 additions and 1,000 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions contracts/pallet-ibc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct HostConsensusProof {
pub header: Vec<u8>,
pub extrinsic: Vec<u8>,
pub extrinsic_proof: Vec<Vec<u8>>,
pub code_id: Option<Vec<u8>>,
pub checksum: Option<Vec<u8>>,
}

impl<T: Config + Send + Sync> ClientReader for Context<T>
Expand Down Expand Up @@ -284,7 +284,7 @@ where
AnyConsensusState::wasm(cs).map_err(ICS02Error::encode)?
},
_ =>
if connection_proof.code_id.is_some() {
if connection_proof.checksum.is_some() {
log::trace!(target: "pallet_ibc", "in client : [host_consensus_state] >> using wasm code id");
AnyConsensusState::wasm(cs).map_err(ICS02Error::encode)?
} else {
Expand Down
12 changes: 6 additions & 6 deletions contracts/pallet-ibc/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ibc::{
core::{
ics02_client::{
events as ClientEvents,
events::{CodeId, NewBlock},
events::{Checksum, NewBlock},
},
ics03_connection::events as ConnectionEvents,
ics04_channel::{events as ChannelEvents, packet::Packet},
Expand Down Expand Up @@ -218,7 +218,7 @@ pub enum IbcEvent {
/// App module
AppModule { kind: Vec<u8>, module_id: Vec<u8> },
/// Push WASM Code
PushWasmCode { wasm_code_id: CodeId },
PushWasmCode { wasm_checksum: Checksum },
}

impl From<RawIbcEvent> for IbcEvent {
Expand Down Expand Up @@ -439,8 +439,8 @@ impl From<RawIbcEvent> for IbcEvent {
module_id: ev.module_name.to_string().as_bytes().to_vec(),
},
RawIbcEvent::PushWasmCode(ev) => {
let wasm_code_id = ev.0;
IbcEvent::PushWasmCode { wasm_code_id }
let wasm_checksum = ev.0;
IbcEvent::PushWasmCode { wasm_checksum }
},
}
}
Expand Down Expand Up @@ -545,8 +545,8 @@ impl TryFrom<IbcEvent> for RawIbcEvent {
consensus_height: Height::new(consensus_revision_number, consensus_height),
},
))),
IbcEvent::PushWasmCode { wasm_code_id } =>
Ok(RawIbcEvent::PushWasmCode(ClientEvents::PushWasmCode(wasm_code_id))),
IbcEvent::PushWasmCode { wasm_checksum } =>
Ok(RawIbcEvent::PushWasmCode(ClientEvents::PushWasmCode(wasm_checksum))),
IbcEvent::OpenInitConnection {
revision_height,
revision_number,
Expand Down
110 changes: 16 additions & 94 deletions contracts/pallet-ibc/src/light_clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ use frame_support::{
pallet_prelude::{StorageValue, ValueQuery},
traits::StorageInstance,
};
use ibc::{
core::{
ics02_client,
ics02_client::{
client_consensus::ConsensusState, client_message::ClientMessage,
client_state::ClientState,
},
use ibc::core::{
ics02_client,
ics02_client::{
client_consensus::ConsensusState, client_message::ClientMessage, client_state::ClientState,
},
Height,
};
use ibc_derive::{ClientDef, ClientMessage, ClientState, ConsensusState, Protobuf};
use ibc_primitives::runtime_interface;
Expand All @@ -25,12 +21,8 @@ use ics07_tendermint::{
consensus_state::TENDERMINT_CONSENSUS_STATE_TYPE_URL,
};
use ics08_wasm::{
client_message::{
WASM_CLIENT_MESSAGE_TYPE_URL, WASM_HEADER_TYPE_URL, WASM_MISBEHAVIOUR_TYPE_URL,
},
client_state::WASM_CLIENT_STATE_TYPE_URL,
consensus_state::WASM_CONSENSUS_STATE_TYPE_URL,
Bytes,
client_message::WASM_CLIENT_MESSAGE_TYPE_URL, client_state::WASM_CLIENT_STATE_TYPE_URL,
consensus_state::WASM_CONSENSUS_STATE_TYPE_URL, Bytes,
};
use ics10_grandpa::{
client_message::{
Expand Down Expand Up @@ -279,13 +271,13 @@ impl AnyClientState {
}

impl AnyClientState {
pub fn wasm(inner: Self, code_id: Bytes) -> Result<Self, tendermint_proto::Error> {
pub fn wasm(inner: Self, checksum: Bytes) -> Result<Self, tendermint_proto::Error> {
Ok(Self::Wasm(
ics08_wasm::client_state::ClientState::<AnyClient, Self, AnyConsensusState> {
data: inner.encode_to_vec()?,
latest_height: inner.latest_height(),
inner: Box::new(inner),
code_id,
checksum,
_phantom: Default::default(),
},
))
Expand All @@ -310,7 +302,6 @@ pub enum AnyConsensusState {
impl AnyConsensusState {
pub fn wasm(inner: Self) -> Result<Self, tendermint_proto::Error> {
Ok(Self::Wasm(ics08_wasm::consensus_state::ConsensusState {
timestamp: inner.timestamp().nanoseconds(),
data: inner.encode_to_vec()?,
inner: Box::new(inner),
}))
Expand All @@ -334,69 +325,16 @@ pub enum AnyClientMessage {
}

impl AnyClientMessage {
pub fn maybe_header_height(&self) -> Option<Height> {
match self {
Self::Tendermint(inner) => match inner {
ics07_tendermint::client_message::ClientMessage::Header(h) => Some(h.height()),
ics07_tendermint::client_message::ClientMessage::Misbehaviour(_) => None,
},
Self::Beefy(inner) => match inner {
ics11_beefy::client_message::ClientMessage::Header(_) =>
unimplemented!("beefy header height"),
ics11_beefy::client_message::ClientMessage::Misbehaviour(_) => None,
},
Self::Grandpa(inner) => match inner {
ics10_grandpa::client_message::ClientMessage::Header(h) => Some(h.height()),
ics10_grandpa::client_message::ClientMessage::Misbehaviour(_) => None,
},
Self::Wasm(inner) => match inner {
ics08_wasm::client_message::ClientMessage::Header(h) =>
h.inner.maybe_header_height(),
ics08_wasm::client_message::ClientMessage::Misbehaviour(_) => None,
},
#[cfg(test)]
Self::Mock(inner) => match inner {
ibc::mock::header::MockClientMessage::Header(h) => Some(h.height()),
ibc::mock::header::MockClientMessage::Misbehaviour(_) => None,
},
}
}

pub fn wasm(inner: Self) -> Result<Self, tendermint_proto::Error> {
let maybe_height = inner.maybe_header_height();
Ok(match maybe_height {
Some(height) => Self::Wasm(ics08_wasm::client_message::ClientMessage::Header(
ics08_wasm::client_message::Header {
data: inner.encode_to_vec()?,
height,
inner: Box::new(inner),
},
)),
None => Self::Wasm(ics08_wasm::client_message::ClientMessage::Misbehaviour(
ics08_wasm::client_message::Misbehaviour {
data: inner.encode_to_vec()?,
inner: Box::new(inner),
},
)),
})
}

pub fn unpack_recursive(&self) -> &Self {
match self {
Self::Wasm(ics08_wasm::client_message::ClientMessage::Header(h)) =>
h.inner.unpack_recursive(),
Self::Wasm(ics08_wasm::client_message::ClientMessage::Misbehaviour(m)) =>
m.inner.unpack_recursive(),
_ => self,
}
Ok(Self::Wasm(ics08_wasm::client_message::ClientMessage {
data: inner.encode_to_vec()?,
inner: Box::new(inner),
}))
}

pub fn unpack_recursive_into(self) -> Self {
match self {
Self::Wasm(ics08_wasm::client_message::ClientMessage::Header(h)) =>
h.inner.unpack_recursive_into(),
Self::Wasm(ics08_wasm::client_message::ClientMessage::Misbehaviour(m)) =>
m.inner.unpack_recursive_into(),
Self::Wasm(ics08_wasm::client_message::ClientMessage { inner, data }) => *inner,
_ => self,
}
}
Expand Down Expand Up @@ -446,16 +384,6 @@ impl TryFrom<Any> for AnyClientMessage {
ics08_wasm::client_message::ClientMessage::decode_vec(&value.value)
.map_err(ics02_client::error::Error::decode_raw_header)?,
)),
WASM_HEADER_TYPE_URL =>
Ok(Self::Wasm(ics08_wasm::client_message::ClientMessage::Header(
ics08_wasm::client_message::Header::decode_vec(&value.value)
.map_err(ics02_client::error::Error::decode_raw_header)?,
))),
WASM_MISBEHAVIOUR_TYPE_URL =>
Ok(Self::Wasm(ics08_wasm::client_message::ClientMessage::Misbehaviour(
ics08_wasm::client_message::Misbehaviour::decode_vec(&value.value)
.map_err(ics02_client::error::Error::decode_raw_header)?,
))),
_ => Err(ics02_client::error::Error::unknown_consensus_state_type(value.type_url)),
}
}
Expand All @@ -464,15 +392,9 @@ impl TryFrom<Any> for AnyClientMessage {
impl From<AnyClientMessage> for Any {
fn from(client_msg: AnyClientMessage) -> Self {
match client_msg {
AnyClientMessage::Wasm(msg) => match msg {
ics08_wasm::client_message::ClientMessage::Header(h) => Any {
type_url: WASM_HEADER_TYPE_URL.to_string(),
value: h.encode_vec().expect("encode_vec failed"),
},
ics08_wasm::client_message::ClientMessage::Misbehaviour(m) => Any {
type_url: WASM_MISBEHAVIOUR_TYPE_URL.to_string(),
value: m.encode_vec().expect("encode_vec failed"),
},
AnyClientMessage::Wasm(msg) => Any {
type_url: WASM_CLIENT_MESSAGE_TYPE_URL.to_string(),
value: msg.encode_vec().expect("encode_vec failed"),
},
AnyClientMessage::Grandpa(msg) => match msg {
ics10_grandpa::client_message::ClientMessage::Header(h) => Any {
Expand Down
8 changes: 4 additions & 4 deletions hyperspace/core/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use ibc::{
core::{
ics02_client::{
client_state::ClientType,
events::{CodeId, UpdateClient},
events::{Checksum, UpdateClient},
msgs::{create_client::MsgCreateAnyClient, update_client::MsgUpdateAnyClient},
},
ics03_connection::msgs::{
Expand Down Expand Up @@ -101,7 +101,7 @@ chains! {
Cosmos(CosmosClientConfig, CosmosClient<DefaultConfig>),
}

fn wrap_any_msg_into_wasm(msg: Any, code_id: Bytes) -> Result<Any, anyhow::Error> {
fn wrap_any_msg_into_wasm(msg: Any, checksum: Bytes) -> Result<Any, anyhow::Error> {
// TODO: consider rewriting with Ics26Envelope
use ibc::core::{
ics02_client::msgs::{
Expand All @@ -119,7 +119,7 @@ fn wrap_any_msg_into_wasm(msg: Any, code_id: Bytes) -> Result<Any, anyhow::Error
let mut msg_decoded =
MsgCreateAnyClient::<LocalClientTypes>::decode_vec(&msg.value).unwrap();
msg_decoded.consensus_state = AnyConsensusState::wasm(msg_decoded.consensus_state)?;
msg_decoded.client_state = AnyClientState::wasm(msg_decoded.client_state, code_id)?;
msg_decoded.client_state = AnyClientState::wasm(msg_decoded.client_state, checksum)?;
msg_decoded.to_any()
},
CONN_OPEN_TRY_TYPE_URL => {
Expand Down Expand Up @@ -147,5 +147,5 @@ fn wrap_any_msg_into_wasm(msg: Any, code_id: Bytes) -> Result<Any, anyhow::Error
#[derive(Clone)]
pub struct WasmChain {
pub inner: Box<AnyChain>,
pub code_id: Bytes,
pub checksum: Bytes,
}
8 changes: 4 additions & 4 deletions hyperspace/core/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ impl UploadWasmCmd {
let mut config: AnyConfig = toml::from_str(&file_content)?;
let client = config.clone().into_client().await?;
let wasm = tokio::fs::read(&self.wasm_path).await?;
let code_id = client.upload_wasm(wasm).await?;
let code_id_str = hex::encode(code_id);
println!("{code_id_str}");
config.set_wasm_code_id(code_id_str);
let checksum = client.upload_wasm(wasm).await?;
let checksum_str = hex::encode(checksum);
println!("{checksum_str}");
config.set_wasm_checksum(checksum_str);
Ok(config)
}

Expand Down
24 changes: 12 additions & 12 deletions hyperspace/core/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ macro_rules! chains {
Self::Wasm(chain) => {
let messages = messages
.into_iter()
.map(|msg| wrap_any_msg_into_wasm(msg, chain.code_id.clone()))
.map(|msg| wrap_any_msg_into_wasm(msg, chain.checksum.clone()))
.collect::<Result<Vec<_>, _>>()?;
chain.inner.submit(messages).await.map_err(AnyError::into)
},
Expand Down Expand Up @@ -989,15 +989,15 @@ macro_rules! chains {

impl AnyConfig {
pub async fn into_client(self) -> anyhow::Result<AnyChain> {
let maybe_wasm_code_id = self.wasm_code_id();
let maybe_wasm_checksum = self.wasm_checksum();
let chain = match self {
$(
$(#[$($meta)*])*
AnyConfig::$name(config) => AnyChain::$name(<$client>::new(config).await?),
)*
};
if let Some(code_id) = maybe_wasm_code_id {
Ok(AnyChain::Wasm(WasmChain { inner: Box::new(chain), code_id }))
if let Some(checksum) = maybe_wasm_checksum {
Ok(AnyChain::Wasm(WasmChain { inner: Box::new(chain), checksum }))
} else {
Ok(chain)
}
Expand Down Expand Up @@ -1036,25 +1036,25 @@ macro_rules! chains {
}
}

pub fn wasm_code_id(&self) -> Option<CodeId> {
let maybe_code_id = match self {
pub fn wasm_checksum(&self) -> Option<Checksum> {
let maybe_checksum = match self {
$(
$(#[$($meta)*])*
Self::$name(chain) => chain.wasm_code_id.as_ref(),
Self::$name(chain) => chain.wasm_checksum.as_ref(),
)*
};
let maybe_code_id =
maybe_code_id.map(|s| hex::decode(s).expect("Wasm code id is hex-encoded"));
let maybe_checksum =
maybe_checksum.map(|s| hex::decode(s).expect("Wasm checksum is hex-encoded"));

maybe_code_id
maybe_checksum
}

pub fn set_wasm_code_id(&mut self, code_id: String) {
pub fn set_wasm_checksum(&mut self, checksum: String) {
match self {
$(
$(#[$($meta)*])*
Self::$name(chain) => {
chain.wasm_code_id = Some(code_id);
chain.wasm_checksum = Some(checksum);
},
)*
}
Expand Down
4 changes: 2 additions & 2 deletions hyperspace/core/src/substrate/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,8 @@ macro_rules! define_ibc_event_wrapper {
RawIbcEvent::AppModule { kind, module_id },
MetadataIbcEvent::Empty => RawIbcEvent::Empty,
MetadataIbcEvent::ChainError => RawIbcEvent::ChainError,
MetadataIbcEvent::PushWasmCode{ wasm_code_id } => RawIbcEvent::PushWasmCode {
wasm_code_id
MetadataIbcEvent::PushWasmCode{ wasm_checksum } => RawIbcEvent::PushWasmCode {
wasm_checksum
},
$($additional)*
}
Expand Down
2 changes: 1 addition & 1 deletion hyperspace/cosmos/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ pub struct CosmosClientConfig {
pub max_tx_size: usize,
/// All the client states and headers will be wrapped in WASM ones using the WASM code ID.
#[serde(default)]
pub wasm_code_id: Option<String>,
pub wasm_checksum: Option<String>,
/*
Here is a list of dropped configuration parameters from Hermes Config.toml
that could be set to default values or removed for the MVP phase:
Expand Down
10 changes: 5 additions & 5 deletions hyperspace/cosmos/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,17 @@ pub fn client_misbehaviour_try_from_abci_event(
pub fn push_wasm_code_try_from_abci_event(
abci_event: &AbciEvent,
) -> Result<client_events::PushWasmCode, IbcEventError> {
let mut code_id = None;
let mut checksum = None;
for tag in &abci_event.attributes {
let key = tag.key.as_str();
let value = tag.value.as_str();
if let client_events::WASM_CODE_ID_ATTRIBUTE_KEY = key {
code_id = Some(hex::decode(value).map_err(IbcEventError::from_hex_error)?)
if let client_events::WASM_CHECKSUM_ATTRIBUTE_KEY = key {
checksum = Some(hex::decode(value).map_err(IbcEventError::from_hex_error)?)
}
}

Ok(client_events::PushWasmCode(code_id.ok_or_else(|| {
IbcEventError::missing_key(client_events::WASM_CODE_ID_ATTRIBUTE_KEY.to_owned())
Ok(client_events::PushWasmCode(checksum.ok_or_else(|| {
IbcEventError::missing_key(client_events::WASM_CHECKSUM_ATTRIBUTE_KEY.to_owned())
})?))
}

Expand Down
Loading

0 comments on commit 5fd0719

Please sign in to comment.