Skip to content

Commit

Permalink
Merge pull request #1904 from oasisprotocol/kostko/fix/rofl-client-ga…
Browse files Browse the repository at this point in the history
…s-estimation

runtime-sdk/modules/rofl: Fix gas estimation in client
  • Loading branch information
kostko authored Jul 23, 2024
2 parents dc8edda + 5b1dc87 commit 20e5298
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions runtime-sdk/src/modules/rofl/app/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ where
let round = self.latest_round().await?;

// Resolve account nonces.
let mut first_signer_address = Default::default();
for (idx, signer) in signers.iter().enumerate() {
let sigspec = SignatureAddressSpec::try_from_pk(&signer.public_key())
.ok_or(anyhow!("signature scheme not supported"))?;
Expand All @@ -171,25 +172,32 @@ where

tx.append_auth_signature(sigspec, nonce);

// If gas is not set, perform estimation.
if idx == 0 && tx.fee_gas() == 0 {
let gas = self
.estimate_gas(EstimateGasQuery {
caller: if let PublicKey::Secp256k1(pk) = signer.public_key() {
Some(CallerAddress::EthAddress(
pk.to_eth_address().try_into().unwrap(),
))
} else {
Some(CallerAddress::Address(address))
},
tx: tx.clone(),
propagate_failures: false,
})
.await?;
tx.set_fee_gas(gas);
// Store first signer address for gas estimation to avoid rederivation.
if idx == 0 {
first_signer_address = address;
}
}

// Perform gas estimation after all signer infos have been added as otherwise we may
// underestimate the amount of gas needed.
if tx.fee_gas() == 0 {
let signer = &signers[0]; // Checked to have at least one signer above.
let gas = self
.estimate_gas(EstimateGasQuery {
caller: if let PublicKey::Secp256k1(pk) = signer.public_key() {
Some(CallerAddress::EthAddress(
pk.to_eth_address().try_into().unwrap(),
))
} else {
Some(CallerAddress::Address(first_signer_address))
},
tx: tx.clone(),
propagate_failures: false,
})
.await?;
tx.set_fee_gas(gas);
}

// Determine gas price. Currently we always use the native denomination.
let mgp = self.gas_price(round, &token::Denomination::NATIVE).await?;
let fee = mgp.saturating_mul(tx.fee_gas().into());
Expand Down

0 comments on commit 20e5298

Please sign in to comment.