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

Cosmos gas price config #3042

Merged
merged 9 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
30 changes: 26 additions & 4 deletions rust/Cargo.lock

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

1 change: 1 addition & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ async-trait = "0.1"
auto_impl = "1.0"
backtrace = "0.3"
base64 = "0.21.2"
bigdecimal = "0.4.2"
bincode = "1.3"
borsh = "0.9"
bs58 = "0.5.0"
Expand Down
1 change: 1 addition & 0 deletions rust/agents/relayer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ hyperlane-base = { path = "../../hyperlane-base" }
hyperlane-ethereum = { path = "../../chains/hyperlane-ethereum" }

[dev-dependencies]
once_cell.workspace = true
tokio-test.workspace = true
hyperlane-test = { path = "../../hyperlane-test" }
hyperlane-base = { path = "../../hyperlane-base", features = ["test-utils"] }
Expand Down
4 changes: 2 additions & 2 deletions rust/agents/relayer/src/msg/gas_payment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use async_trait::async_trait;
use eyre::Result;
use hyperlane_base::db::HyperlaneRocksDB;
use hyperlane_core::{
GasPaymentKey, HyperlaneMessage, InterchainGasExpenditure, InterchainGasPayment,
BigFloat, GasPaymentKey, HyperlaneMessage, InterchainGasExpenditure, InterchainGasPayment,
TxCostEstimate, TxOutcome, U256,
};
use tracing::{debug, error, trace};
Expand Down Expand Up @@ -135,7 +135,7 @@ impl GasPaymentEnforcer {
self.db.process_gas_expenditure(InterchainGasExpenditure {
message_id: message.id(),
gas_used: outcome.gas_used,
tokens_used: outcome.gas_used * outcome.gas_price,
tokens_used: (BigFloat::try_from(outcome.gas_used)? * outcome.gas_price).try_into()?,
})?;
Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions rust/agents/relayer/src/msg/gas_payment/policies/minimum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async fn test_gas_payment_policy_minimum() {
&current_expenditure,
&TxCostEstimate {
gas_limit: U256::from(100000u32),
gas_price: U256::from(100000u32),
gas_price: U256::from(100000u32).try_into().unwrap(),
l2_gas_limit: None,
},
)
Expand All @@ -83,7 +83,7 @@ async fn test_gas_payment_policy_minimum() {
&current_expenditure,
&TxCostEstimate {
gas_limit: U256::from(100000u32),
gas_price: U256::from(100001u32),
gas_price: U256::from(100001u32).try_into().unwrap(),
l2_gas_limit: None,
},
)
Expand All @@ -101,7 +101,7 @@ async fn test_gas_payment_policy_minimum() {
&current_expenditure,
&TxCostEstimate {
gas_limit: U256::from(100000u32),
gas_price: U256::from(100001u32),
gas_price: U256::from(100001u32).try_into().unwrap(),
l2_gas_limit: Some(U256::from(22222u32)),
},
)
Expand Down
4 changes: 2 additions & 2 deletions rust/agents/relayer/src/msg/gas_payment/policies/none.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async fn test_gas_payment_policy_none() {
&current_expenditure,
&TxCostEstimate {
gas_limit: U256::from(100000u32),
gas_price: U256::from(100001u32),
gas_price: U256::from(100001u32).try_into().unwrap(),
l2_gas_limit: None,
},
)
Expand All @@ -70,7 +70,7 @@ async fn test_gas_payment_policy_none() {
&current_expenditure,
&TxCostEstimate {
gas_limit: U256::from(100000u32),
gas_price: U256::from(100001u32),
gas_price: U256::from(100001u32).try_into().unwrap(),
l2_gas_limit: Some(U256::from(22222u32)),
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ impl GasPaymentPolicy for GasPaymentPolicyOnChainFeeQuoting {
#[cfg(test)]
mod test {
use hyperlane_core::H256;
use once_cell::sync::Lazy;

use super::*;

Expand All @@ -85,11 +86,11 @@ mod test {
}

const MIN: U256 = U256([1000, 0, 0, 0]);
const COST_ESTIMATE: TxCostEstimate = TxCostEstimate {
static COST_ESTIMATE: Lazy<TxCostEstimate> = Lazy::new(|| TxCostEstimate {
gas_limit: U256([2000, 0, 0, 0]), // MIN * 2
gas_price: U256([100001, 0, 0, 0]),
gas_price: U256([100001, 0, 0, 0]).try_into().unwrap(),
l2_gas_limit: None,
};
});

#[test]
fn ensure_little_endian() {
Expand Down Expand Up @@ -203,7 +204,7 @@ mod test {

let tx_cost_estimate = TxCostEstimate {
gas_limit: MIN * 100, // Large gas limit
gas_price: COST_ESTIMATE.gas_price,
gas_price: COST_ESTIMATE.gas_price.clone().try_into().unwrap(),
l2_gas_limit: Some(MIN * 2),
};

Expand All @@ -217,7 +218,7 @@ mod test {
&current_expenditure(0),
&TxCostEstimate {
l2_gas_limit: None,
..tx_cost_estimate
..tx_cost_estimate.clone()
}
)
.await
Expand Down
2 changes: 1 addition & 1 deletion rust/agents/relayer/src/msg/pending_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ impl PendingOperation for PendingMessage {
"processing message"
);

op_try!(critical: self.ctx.origin_gas_payment_enforcer.record_tx_outcome(&self.message, tx_outcome), "recording tx outcome");
op_try!(critical: self.ctx.origin_gas_payment_enforcer.record_tx_outcome(&self.message, tx_outcome.clone()), "recording tx outcome");
if tx_outcome.executed {
info!(
txid=?tx_outcome.transaction_id,
Expand Down
3 changes: 3 additions & 0 deletions rust/chains/hyperlane-cosmos/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub enum HyperlaneCosmosError {
/// protobuf error
#[error("{0}")]
Protobuf(#[from] prost::DecodeError),
/// The string is not a valid number
#[error("Failed to parse number from string")]
NumStrParse,
daniel-savu marked this conversation as resolved.
Show resolved Hide resolved
}

impl From<HyperlaneCosmosError> for ChainCommunicationError {
Expand Down
2 changes: 1 addition & 1 deletion rust/chains/hyperlane-cosmos/src/mailbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl Mailbox for CosmosMailbox {

let result = TxCostEstimate {
gas_limit: gas_limit.into(),
gas_price: U256::from(2500),
gas_price: self.provider.grpc().gas_price(),
l2_gas_limit: None,
};

Expand Down
22 changes: 13 additions & 9 deletions rust/chains/hyperlane-cosmos/src/providers/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,16 @@ use cosmrs::{
traits::Message,
},
tx::{self, Fee, MessageExt, SignDoc, SignerInfo},
Amount, Coin,
Coin,
};
use hyperlane_core::{ChainCommunicationError, ChainResult, ContractLocator, U256};
use hyperlane_core::{BigFloat, ChainCommunicationError, ChainResult, ContractLocator, U256};
use serde::Serialize;
use tonic::transport::{Channel, Endpoint};

use crate::address::CosmosAddress;
use crate::HyperlaneCosmosError;
use crate::{address::CosmosAddress, CosmosAmount};
use crate::{signers::Signer, ConnectionConf};

/// The gas price to use for transactions.
/// TODO: is there a nice way to get a suggested price dynamically?
const DEFAULT_GAS_PRICE: f64 = 0.05;
/// A multiplier applied to a simulated transaction's gas usage to
/// calculate the estimated gas.
const GAS_ESTIMATE_MULTIPLIER: f64 = 1.25;
Expand Down Expand Up @@ -91,12 +88,14 @@ pub struct WasmGrpcProvider {
/// GRPC Channel that can be cheaply cloned.
/// See https://docs.rs/tonic/latest/tonic/transport/struct.Channel.html#multiplexing-requests
channel: Channel,
gas_price: CosmosAmount,
}

impl WasmGrpcProvider {
/// Create new CosmWasm GRPC Provider.
pub fn new(
conf: ConnectionConf,
gas_price: CosmosAmount,
locator: Option<ContractLocator>,
signer: Option<Signer>,
) -> ChainResult<Self> {
Expand All @@ -112,6 +111,7 @@ impl WasmGrpcProvider {
contract_address,
signer,
channel,
gas_price,
})
}

Expand All @@ -121,9 +121,12 @@ impl WasmGrpcProvider {
.as_ref()
.ok_or(ChainCommunicationError::SignerUnavailable)
}
}

impl WasmGrpcProvider {
/// Get the gas price
pub fn gas_price(&self) -> BigFloat {
self.gas_price.amount.clone()
}

/// Generates an unsigned SignDoc for a transaction.
async fn generate_unsigned_sign_doc(
&self,
Expand All @@ -147,7 +150,8 @@ impl WasmGrpcProvider {

let auth_info = signer_info.auth_info(Fee::from_amount_and_gas(
Coin::new(
Amount::from((gas_limit as f64 * DEFAULT_GAS_PRICE) as u64),
// The fee to pay is the gas limit * the gas price
(BigFloat::from(gas_limit) * self.gas_price()).try_into()?,
daniel-savu marked this conversation as resolved.
Show resolved Hide resolved
self.conf.get_canonical_asset().as_str(),
)
.map_err(Into::<HyperlaneCosmosError>::into)?,
Expand Down
5 changes: 3 additions & 2 deletions rust/chains/hyperlane-cosmos/src/providers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use hyperlane_core::{
};
use tendermint_rpc::{client::CompatMode, HttpClient};

use crate::{ConnectionConf, HyperlaneCosmosError, Signer};
use crate::{ConnectionConf, CosmosAmount, HyperlaneCosmosError, Signer};

use self::grpc::WasmGrpcProvider;

Expand All @@ -31,7 +31,8 @@ impl CosmosProvider {
locator: Option<ContractLocator>,
signer: Option<Signer>,
) -> ChainResult<Self> {
let grpc_client = WasmGrpcProvider::new(conf.clone(), locator, signer)?;
let gas_price = CosmosAmount::try_from(conf.get_minimum_gas_price().clone())?;
let grpc_client = WasmGrpcProvider::new(conf.clone(), gas_price.clone(), locator, signer)?;
let rpc_client = HttpClient::builder(
conf.get_rpc_url()
.parse()
Expand Down
Loading
Loading