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

fix: wasm32-unknown-unknown target compilation #2951

Merged
merged 7 commits into from
Nov 22, 2023
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
824 changes: 439 additions & 385 deletions rust/Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions rust/chains/hyperlane-cosmos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ serde_json = { workspace = true }
sha2 = { workspace = true }
sha256 = { workspace = true }
tendermint = { workspace = true, features = ["rust-crypto", "secp256k1"] }
tendermint-rpc = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tonic = { workspace = true }
Expand Down
16 changes: 16 additions & 0 deletions rust/chains/hyperlane-cosmos/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use cosmrs::proto::prost;
use hyperlane_core::ChainCommunicationError;

/// Errors from the crates specific to the hyperlane-cosmos
Expand All @@ -6,6 +7,9 @@ use hyperlane_core::ChainCommunicationError;
/// in hyperlane-core using the `From` trait impl
#[derive(Debug, thiserror::Error)]
pub enum HyperlaneCosmosError {
/// base64 error
#[error("{0}")]
Base64(#[from] base64::DecodeError),
/// bech32 error
#[error("{0}")]
Bech32(#[from] bech32::Error),
Expand All @@ -18,6 +22,18 @@ pub enum HyperlaneCosmosError {
/// Cosmos error report
#[error("{0}")]
CosmosErrorReport(#[from] cosmrs::ErrorReport),
#[error("{0}")]
/// Cosmrs Tendermint Error
CosmrsTendermintError(#[from] cosmrs::tendermint::Error),
/// Tonic error
#[error("{0}")]
Tonic(#[from] tonic::transport::Error),
/// Tendermint RPC Error
#[error(transparent)]
TendermintError(#[from] tendermint_rpc::error::Error),
/// protobuf error
#[error("{0}")]
Protobuf(#[from] prost::DecodeError),
}

impl From<HyperlaneCosmosError> for ChainCommunicationError {
Expand Down
37 changes: 30 additions & 7 deletions rust/chains/hyperlane-cosmos/src/interchain_gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
rpc::{CosmosWasmIndexer, ParsedEvent, WasmIndexer},
signers::Signer,
utils::{CONTRACT_ADDRESS_ATTRIBUTE_KEY, CONTRACT_ADDRESS_ATTRIBUTE_KEY_BASE64},
ConnectionConf, CosmosProvider,
ConnectionConf, CosmosProvider, HyperlaneCosmosError,
};

/// A reference to a InterchainGasPaymaster contract on some Cosmos chain
Expand Down Expand Up @@ -119,23 +119,36 @@ impl CosmosInterchainGasPaymasterIndexer {
contract_address = Some(value.to_string());
}
v if *CONTRACT_ADDRESS_ATTRIBUTE_KEY_BASE64 == v => {
contract_address = Some(String::from_utf8(BASE64.decode(value)?)?);
contract_address = Some(String::from_utf8(
BASE64
.decode(value)
.map_err(Into::<HyperlaneCosmosError>::into)?,
)?);
}

MESSAGE_ID_ATTRIBUTE_KEY => {
gas_payment.message_id = Some(H256::from_slice(hex::decode(value)?.as_slice()));
}
v if *MESSAGE_ID_ATTRIBUTE_KEY_BASE64 == v => {
gas_payment.message_id = Some(H256::from_slice(
hex::decode(String::from_utf8(BASE64.decode(value)?)?)?.as_slice(),
hex::decode(String::from_utf8(
BASE64
.decode(value)
.map_err(Into::<HyperlaneCosmosError>::into)?,
)?)?
.as_slice(),
));
}

PAYMENT_ATTRIBUTE_KEY => {
gas_payment.payment = Some(U256::from_dec_str(value)?);
}
v if *PAYMENT_ATTRIBUTE_KEY_BASE64 == v => {
let dec_str = String::from_utf8(BASE64.decode(value)?)?;
let dec_str = String::from_utf8(
BASE64
.decode(value)
.map_err(Into::<HyperlaneCosmosError>::into)?,
)?;
// U256's from_str assumes a radix of 16, so we explicitly use from_dec_str.
gas_payment.payment = Some(U256::from_dec_str(dec_str.as_str())?);
}
Expand All @@ -144,7 +157,11 @@ impl CosmosInterchainGasPaymasterIndexer {
gas_payment.gas_amount = Some(U256::from_dec_str(value)?);
}
v if *GAS_AMOUNT_ATTRIBUTE_KEY_BASE64 == v => {
let dec_str = String::from_utf8(BASE64.decode(value)?)?;
let dec_str = String::from_utf8(
BASE64
.decode(value)
.map_err(Into::<HyperlaneCosmosError>::into)?,
)?;
// U256's from_str assumes a radix of 16, so we explicitly use from_dec_str.
gas_payment.gas_amount = Some(U256::from_dec_str(dec_str.as_str())?);
}
Expand All @@ -153,8 +170,14 @@ impl CosmosInterchainGasPaymasterIndexer {
gas_payment.destination = Some(value.parse::<u32>()?);
}
v if *DESTINATION_ATTRIBUTE_KEY_BASE64 == v => {
gas_payment.destination =
Some(String::from_utf8(BASE64.decode(value)?)?.parse()?);
gas_payment.destination = Some(
String::from_utf8(
BASE64
.decode(value)
.map_err(Into::<HyperlaneCosmosError>::into)?,
)?
.parse()?,
);
}

_ => {}
Expand Down
14 changes: 10 additions & 4 deletions rust/chains/hyperlane-cosmos/src/libs/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use hyperlane_core::{ChainCommunicationError, ChainResult, Error::Overflow, H256
use tendermint::account::Id as TendermintAccountId;
use tendermint::public_key::PublicKey as TendermintPublicKey;

use crate::HyperlaneCosmosError;

/// Wrapper around the cosmrs AccountId type that abstracts bech32 encoding
#[derive(new, Debug)]
pub struct CosmosAddress {
Expand All @@ -27,15 +29,18 @@ impl CosmosAddress {
// Get the RIPEMD160(SHA256(pubkey))
let tendermint_id = TendermintAccountId::from(tendermint_pubkey);
// Bech32 encoding
let account_id = AccountId::new(prefix, tendermint_id.as_bytes())?;
let account_id = AccountId::new(prefix, tendermint_id.as_bytes())
.map_err(Into::<HyperlaneCosmosError>::into)?;
// Hex digest
let digest = Self::bech32_decode(account_id.clone())?;
Ok(CosmosAddress::new(account_id, digest))
}

/// Creates a wrapper arround a cosmrs AccountId from a private key byte array
pub fn from_privkey(priv_key: &[u8], prefix: &str) -> ChainResult<Self> {
let pubkey = SigningKey::from_slice(priv_key)?.public_key();
let pubkey = SigningKey::from_slice(priv_key)
.map_err(Into::<HyperlaneCosmosError>::into)?
.public_key();
Self::from_pubkey(pubkey, prefix)
}

Expand All @@ -47,7 +52,8 @@ impl CosmosAddress {
// This is the hex-encoded version of the address
let bytes = digest.as_bytes();
// Bech32 encode it
let account_id = AccountId::new(prefix, bytes)?;
let account_id =
AccountId::new(prefix, bytes).map_err(Into::<HyperlaneCosmosError>::into)?;
Ok(CosmosAddress::new(account_id, digest))
}

Expand Down Expand Up @@ -92,7 +98,7 @@ impl FromStr for CosmosAddress {
type Err = ChainCommunicationError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let account_id = AccountId::from_str(s)?;
let account_id = AccountId::from_str(s).map_err(Into::<HyperlaneCosmosError>::into)?;
let digest = Self::bech32_decode(account_id.clone())?;
Ok(Self::new(account_id, digest))
}
Expand Down
22 changes: 16 additions & 6 deletions rust/chains/hyperlane-cosmos/src/mailbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ use std::{
str::FromStr,
};

use crate::address::CosmosAddress;
use crate::grpc::{WasmGrpcProvider, WasmProvider};
use crate::payloads::mailbox::{
GeneralMailboxQuery, ProcessMessageRequest, ProcessMessageRequestInner,
};
use crate::payloads::{general, mailbox};
use crate::rpc::{CosmosWasmIndexer, ParsedEvent, WasmIndexer};
use crate::CosmosProvider;
use crate::{address::CosmosAddress, types::tx_response_to_outcome};
use crate::{
grpc::{WasmGrpcProvider, WasmProvider},
HyperlaneCosmosError,
};
use crate::{signers::Signer, utils::get_block_height_for_lag, ConnectionConf};
use async_trait::async_trait;
use cosmrs::proto::cosmos::base::abci::v1beta1::TxResponse;
Expand Down Expand Up @@ -182,7 +185,7 @@ impl Mailbox for CosmosMailbox {
.wasm_send(process_message, tx_gas_limit)
.await?;

Ok(response.try_into()?)
Ok(tx_response_to_outcome(response)?)
}

#[instrument(err, ret, skip(self), fields(msg=%message, metadata=%fmt_bytes(metadata)))]
Expand Down Expand Up @@ -286,7 +289,11 @@ impl CosmosMailboxIndexer {
contract_address = Some(value.to_string());
}
v if *CONTRACT_ADDRESS_ATTRIBUTE_KEY_BASE64 == v => {
contract_address = Some(String::from_utf8(BASE64.decode(value)?)?);
contract_address = Some(String::from_utf8(
BASE64
.decode(value)
.map_err(Into::<HyperlaneCosmosError>::into)?,
)?);
}

MESSAGE_ATTRIBUTE_KEY => {
Expand All @@ -298,8 +305,11 @@ impl CosmosMailboxIndexer {
v if *MESSAGE_ATTRIBUTE_KEY_BASE64 == v => {
// Intentionally using read_from to get a Result::Err if there's
// an issue with the message.
let mut reader =
Cursor::new(hex::decode(String::from_utf8(BASE64.decode(value)?)?)?);
let mut reader = Cursor::new(hex::decode(String::from_utf8(
BASE64
.decode(value)
.map_err(Into::<HyperlaneCosmosError>::into)?,
)?)?);
message = Some(HyperlaneMessage::read_from(&mut reader)?);
}

Expand Down
24 changes: 20 additions & 4 deletions rust/chains/hyperlane-cosmos/src/merkle_tree_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
get_block_height_for_lag, CONTRACT_ADDRESS_ATTRIBUTE_KEY,
CONTRACT_ADDRESS_ATTRIBUTE_KEY_BASE64,
},
ConnectionConf, CosmosProvider, Signer,
ConnectionConf, CosmosProvider, HyperlaneCosmosError, Signer,
};

#[derive(Debug)]
Expand Down Expand Up @@ -224,23 +224,39 @@ impl CosmosMerkleTreeHookIndexer {
contract_address = Some(value.to_string());
}
v if *CONTRACT_ADDRESS_ATTRIBUTE_KEY_BASE64 == v => {
contract_address = Some(String::from_utf8(BASE64.decode(value)?)?);
contract_address = Some(String::from_utf8(
BASE64
.decode(value)
.map_err(Into::<HyperlaneCosmosError>::into)?,
)?);
}

MESSAGE_ID_ATTRIBUTE_KEY => {
insertion.message_id = Some(H256::from_slice(hex::decode(value)?.as_slice()));
}
v if *MESSAGE_ID_ATTRIBUTE_KEY_BASE64 == v => {
insertion.message_id = Some(H256::from_slice(
hex::decode(String::from_utf8(BASE64.decode(value)?)?)?.as_slice(),
hex::decode(String::from_utf8(
BASE64
.decode(value)
.map_err(Into::<HyperlaneCosmosError>::into)?,
)?)?
.as_slice(),
));
}

INDEX_ATTRIBUTE_KEY => {
insertion.leaf_index = Some(value.parse::<u32>()?);
}
v if *INDEX_ATTRIBUTE_KEY_BASE64 == v => {
insertion.leaf_index = Some(String::from_utf8(BASE64.decode(value)?)?.parse()?);
insertion.leaf_index = Some(
String::from_utf8(
BASE64
.decode(value)
.map_err(Into::<HyperlaneCosmosError>::into)?,
)?
.parse()?,
);
}

_ => {}
Expand Down
33 changes: 22 additions & 11 deletions rust/chains/hyperlane-cosmos/src/providers/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ impl WasmGrpcProvider {
locator: ContractLocator,
signer: Option<Signer>,
) -> ChainResult<Self> {
let channel = Endpoint::new(conf.get_grpc_url())?.connect_lazy();
let endpoint =
Endpoint::new(conf.get_grpc_url()).map_err(Into::<HyperlaneCosmosError>::into)?;
let channel = endpoint.connect_lazy();
let contract_address = CosmosAddress::from_h256(locator.address, &conf.get_prefix())?;

Ok(Self {
Expand Down Expand Up @@ -144,17 +146,21 @@ impl WasmGrpcProvider {
Coin::new(
Amount::from((gas_limit as f64 * DEFAULT_GAS_PRICE) as u64),
self.conf.get_canonical_asset().as_str(),
)?,
)
.map_err(Into::<HyperlaneCosmosError>::into)?,
gas_limit,
));

SignDoc::new(
&tx_body,
&auth_info,
&self.conf.get_chain_id().parse()?,
account_info.account_number,
let chain_id = self
.conf
.get_chain_id()
.parse()
.map_err(Into::<HyperlaneCosmosError>::into)?;

Ok(
SignDoc::new(&tx_body, &auth_info, &chain_id, account_info.account_number)
.map_err(Into::<HyperlaneCosmosError>::into)?,
)
.map_err(Into::into)
}

/// Generates a raw signed transaction including `msgs`, estimating gas if a limit is not provided.
Expand All @@ -172,8 +178,12 @@ impl WasmGrpcProvider {
let sign_doc = self.generate_unsigned_sign_doc(msgs, gas_limit).await?;

let signer = self.get_signer()?;
let tx_signed = sign_doc.sign(&signer.signing_key()?)?;
Ok(tx_signed.to_bytes()?)
let tx_signed = sign_doc
.sign(&signer.signing_key()?)
.map_err(Into::<HyperlaneCosmosError>::into)?;
Ok(tx_signed
.to_bytes()
.map_err(Into::<HyperlaneCosmosError>::into)?)
}

/// Estimates gas for a transaction containing `msgs`.
Expand Down Expand Up @@ -227,7 +237,8 @@ impl WasmGrpcProvider {
.ok_or_else(|| ChainCommunicationError::from_other_str("account not present"))?
.value
.as_slice(),
)?;
)
.map_err(Into::<HyperlaneCosmosError>::into)?;
Ok(account)
}
}
Expand Down
21 changes: 14 additions & 7 deletions rust/chains/hyperlane-cosmos/src/providers/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use hyperlane_core::{ChainCommunicationError, ChainResult, ContractLocator, LogM
use tracing::{instrument, trace};

use crate::address::CosmosAddress;
use crate::ConnectionConf;
use crate::{ConnectionConf, HyperlaneCosmosError};

const PAGINATION_LIMIT: u8 = 100;

Expand Down Expand Up @@ -66,10 +66,15 @@ impl CosmosWasmIndexer {
event_type: String,
reorg_period: u32,
) -> ChainResult<Self> {
let client = HttpClient::builder(conf.get_rpc_url().parse()?)
// Consider supporting different compatibility modes.
.compat_mode(CompatMode::latest())
.build()?;
let client = HttpClient::builder(
conf.get_rpc_url()
.parse()
.map_err(Into::<HyperlaneCosmosError>::into)?,
)
// Consider supporting different compatibility modes.
.compat_mode(CompatMode::latest())
.build()
.map_err(Into::<HyperlaneCosmosError>::into)?;
Ok(Self {
client,
contract_address: CosmosAddress::from_h256(
Expand All @@ -88,7 +93,8 @@ impl CosmosWasmIndexer {
Ok(self
.client
.tx_search(query, false, page, PAGINATION_LIMIT, Order::Ascending)
.await?)
.await
.map_err(Into::<HyperlaneCosmosError>::into)?)
}

// Iterate through all txs, filter out failed txs, find target events
Expand Down Expand Up @@ -172,7 +178,8 @@ impl WasmIndexer for CosmosWasmIndexer {
let latest_height: u32 = self
.client
.latest_block()
.await?
.await
.map_err(Into::<HyperlaneCosmosError>::into)?
.block
.header
.height
Expand Down
Loading
Loading