Skip to content

Commit

Permalink
Support custom extension options when building tx
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Aug 17, 2022
1 parent 0a3db64 commit f073cd4
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 14 deletions.
16 changes: 5 additions & 11 deletions Cargo.lock

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

11 changes: 8 additions & 3 deletions relayer/src/chain/cosmos/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ pub fn sign_tx(

let signer = encode_signer_info(&config.address_type, account.sequence, key_bytes)?;

let (body, body_bytes) = tx_body_and_bytes(messages, tx_memo)?;
let (body, body_bytes) =
tx_body_and_bytes(messages, tx_memo, config.extension_options.clone())?;

let (auth_info, auth_info_bytes) = auth_info_and_bytes(signer, fee.clone())?;

Expand Down Expand Up @@ -159,13 +160,17 @@ fn auth_info_and_bytes(signer_info: SignerInfo, fee: Fee) -> Result<(AuthInfo, V
Ok((auth_info, auth_buf))
}

fn tx_body_and_bytes(proto_msgs: Vec<Any>, memo: &Memo) -> Result<(TxBody, Vec<u8>), Error> {
fn tx_body_and_bytes(
proto_msgs: Vec<Any>,
memo: &Memo,
extension_options: Vec<Any>,
) -> Result<(TxBody, Vec<u8>), Error> {
// Create TxBody
let body = TxBody {
messages: proto_msgs.to_vec(),
memo: memo.to_string(),
timeout_height: 0_u64,
extension_options: Vec::<Any>::new(),
extension_options,
non_critical_extension_options: Vec::<Any>::new(),
};

Expand Down
7 changes: 7 additions & 0 deletions relayer/src/chain/cosmos/types/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use core::str::FromStr;
use core::time::Duration;
use http::Uri;
use ibc::core::ics24_host::identifier::ChainId;
use ibc_proto::google::protobuf::Any;
use tendermint_rpc::{HttpClient, Url};

use crate::chain::cosmos::types::gas::GasConfig;
Expand All @@ -17,6 +18,7 @@ pub struct TxConfig {
pub grpc_address: Uri,
pub rpc_timeout: Duration,
pub address_type: AddressType,
pub extension_options: Vec<Any>,
}

impl<'a> TryFrom<&'a ChainConfig> for TxConfig {
Expand All @@ -39,6 +41,11 @@ impl<'a> TryFrom<&'a ChainConfig> for TxConfig {
grpc_address,
rpc_timeout: config.rpc_timeout,
address_type: config.address_type.clone(),
extension_options: config
.extension_options
.iter()
.map(|opt| opt.to_any())
.collect::<Result<_, _>>()?,
})
}
}
32 changes: 32 additions & 0 deletions relayer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use alloc::collections::BTreeMap;
use core::{fmt, time::Duration};
use std::{fs, fs::File, io::Write, path::Path};

use ibc_proto::google::protobuf::Any;
use serde_derive::{Deserialize, Serialize};
use tendermint_light_client_verifier::types::TrustThreshold;

Expand All @@ -18,6 +19,8 @@ use ibc::timestamp::ZERO_DURATION;

use crate::chain::ChainType;
use crate::config::types::{MaxMsgNum, MaxTxSize, Memo};
use crate::error::Error as RelayerError;
use crate::extension_options::ExtensionOptionDynamicFeeTx;
use crate::keyring::Store;

pub use error::Error;
Expand All @@ -42,6 +45,33 @@ impl fmt::Display for GasPrice {
}
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum ExtensionOption {
EthermintDynamicFee(ExtensionOptionDynamicFeeTx),
}

impl ExtensionOption {
pub fn to_any(&self) -> Result<Any, RelayerError> {
match self {
Self::EthermintDynamicFee(ext) => ext.to_any(),
}
}
}

impl fmt::Display for ExtensionOption {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::EthermintDynamicFee(ext) => {
write!(
f,
"EthermintDynamicFee(max_priority_price: {})",
ext.max_priority_price
)
}
}
}
}

/// Defaults for various fields
pub mod default {
use super::*;
Expand Down Expand Up @@ -375,6 +405,8 @@ pub struct ChainConfig {
pub packet_filter: PacketFilter,
#[serde(default)]
pub address_type: AddressType,
#[serde(default = "Vec::new", skip_serializing_if = "Vec::is_empty")]
pub extension_options: Vec<ExtensionOption>,
}

/// Attempt to load and parse the TOML config file as a `Config`.
Expand Down
26 changes: 26 additions & 0 deletions relayer/src/extension_options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use ibc_proto::google::protobuf::Any;
use prost::Message;
use serde_derive::{Deserialize, Serialize};

use crate::error::Error;

// ExtensionOptionDynamicFeeTx is an extension option used with ethermint dynamic fee tx.
// protobuf message: https://github.com/evmos/ethermint/blob/main/proto/ethermint/types/v1/dynamic_fee.proto
#[derive(Clone, PartialEq, Message, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ExtensionOptionDynamicFeeTx {
#[prost(string, tag = "1")]
pub max_priority_price: ::prost::alloc::string::String,
}

impl ExtensionOptionDynamicFeeTx {
pub fn to_any(&self) -> Result<Any, Error> {
let mut buf = Vec::new();
Message::encode(self, &mut buf)
.map_err(|e| Error::protobuf_encode("ExtensionOptionDynamicFeeTx".into(), e))?;
Ok(Any {
type_url: "/ethermint.types.v1.ExtensionOptionDynamicFeeTx".to_string(),
value: buf,
})
}
}
1 change: 1 addition & 0 deletions relayer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub mod connection;
pub mod denom;
pub mod error;
pub mod event;
pub mod extension_options;
pub mod foreign_client;
pub mod keyring;
pub mod light_client;
Expand Down
3 changes: 3 additions & 0 deletions tools/test-framework/src/relayer/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ pub fn new_tx_config_for_test(

let address_type = Default::default();

let extension_options = Default::default();

Ok(TxConfig {
chain_id,
gas_config,
Expand All @@ -69,6 +71,7 @@ pub fn new_tx_config_for_test(
grpc_address,
rpc_timeout,
address_type,
extension_options,
})
}

Expand Down
1 change: 1 addition & 0 deletions tools/test-framework/src/types/single/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ impl FullNode {
address_type: Default::default(),
memo_prefix: Default::default(),
proof_specs: Default::default(),
extension_options: Default::default(),
})
}

Expand Down

0 comments on commit f073cd4

Please sign in to comment.