Skip to content

Commit

Permalink
fix: use eth_getTransactionCount(pending) to get nonce (#1194)
Browse files Browse the repository at this point in the history
  • Loading branch information
karlem authored Nov 7, 2024
1 parent c93fb75 commit 1388cd2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 26 deletions.
25 changes: 25 additions & 0 deletions Cargo.lock

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

55 changes: 29 additions & 26 deletions ipc/provider/src/manager/evm/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,9 @@ impl SubnetManager for EthSubnetManager {
signer.clone(),
);

let call = call_with_premium_estimation(signer, registry_contract.new_subnet_actor(params))
.await?;
let call =
call_with_premium_and_pending_block(signer, registry_contract.new_subnet_actor(params))
.await?;
// TODO: Edit call to get estimate premium
let pending_tx = call.send().await?;
// We need the retry to parse the deployment event. At the time of this writing, it's a bug
Expand Down Expand Up @@ -348,7 +349,7 @@ impl SubnetManager for EthSubnetManager {
let mut txn = contract.join(ethers::types::Bytes::from(pub_key), U256::from(collateral));
txn = self.handle_txn_token(&subnet, txn, collateral, 0).await?;

let txn = call_with_premium_estimation(signer, txn).await?;
let txn = call_with_premium_and_pending_block(signer, txn).await?;

// Use the pending state to get the nonce because there could have been a pre-fund. Best would be to use this for everything.
let txn = txn.block(BlockId::Number(ethers::types::BlockNumber::Pending));
Expand All @@ -374,7 +375,7 @@ impl SubnetManager for EthSubnetManager {
let mut txn = contract.pre_fund(U256::from(balance));
txn = self.handle_txn_token(&subnet, txn, 0, balance).await?;

let txn = call_with_premium_estimation(signer, txn).await?;
let txn = call_with_premium_and_pending_block(signer, txn).await?;

txn.send().await?;
Ok(())
Expand All @@ -398,7 +399,7 @@ impl SubnetManager for EthSubnetManager {
let contract =
subnet_actor_manager_facet::SubnetActorManagerFacet::new(address, signer.clone());

call_with_premium_estimation(signer, contract.pre_release(amount.into()))
call_with_premium_and_pending_block(signer, contract.pre_release(amount.into()))
.await?
.send()
.await?
Expand All @@ -425,7 +426,7 @@ impl SubnetManager for EthSubnetManager {
let mut txn = contract.stake(U256::from(collateral));
txn = self.handle_txn_token(&subnet, txn, collateral, 0).await?;

let txn = call_with_premium_estimation(signer, txn).await?;
let txn = call_with_premium_and_pending_block(signer, txn).await?;

txn.send().await?.await?;

Expand All @@ -452,7 +453,8 @@ impl SubnetManager for EthSubnetManager {
let contract =
subnet_actor_manager_facet::SubnetActorManagerFacet::new(address, signer.clone());

let txn = call_with_premium_estimation(signer, contract.unstake(collateral.into())).await?;
let txn = call_with_premium_and_pending_block(signer, contract.unstake(collateral.into()))
.await?;
txn.send().await?.await?;

Ok(())
Expand All @@ -466,7 +468,7 @@ impl SubnetManager for EthSubnetManager {
let contract =
subnet_actor_manager_facet::SubnetActorManagerFacet::new(address, signer.clone());

call_with_premium_estimation(signer, contract.leave())
call_with_premium_and_pending_block(signer, contract.leave())
.await?
.send()
.await?
Expand All @@ -483,7 +485,7 @@ impl SubnetManager for EthSubnetManager {
let contract =
subnet_actor_manager_facet::SubnetActorManagerFacet::new(address, signer.clone());

call_with_premium_estimation(signer, contract.kill())
call_with_premium_and_pending_block(signer, contract.kill())
.await?
.send()
.await?
Expand Down Expand Up @@ -524,7 +526,7 @@ impl SubnetManager for EthSubnetManager {
let contract =
subnet_actor_reward_facet::SubnetActorRewardFacet::new(address, signer.clone());

call_with_premium_estimation(signer, contract.claim())
call_with_premium_and_pending_block(signer, contract.claim())
.await?
.send()
.await?
Expand Down Expand Up @@ -564,7 +566,7 @@ impl SubnetManager for EthSubnetManager {
gateway_manager_facet::FvmAddress::try_from(to)?,
);
txn.tx.set_value(value);
let txn = call_with_premium_estimation(signer, txn).await?;
let txn = call_with_premium_and_pending_block(signer, txn).await?;

let pending_tx = txn.send().await?;
let receipt = pending_tx.retries(TRANSACTION_RECEIPT_RETRIES).await?;
Expand Down Expand Up @@ -598,7 +600,7 @@ impl SubnetManager for EthSubnetManager {
let token_contract = IERC20::new(token_address, signer.clone());

let txn = token_contract.approve(self.ipc_contract_info.gateway_addr, value);
let txn = call_with_premium_estimation(signer, txn).await?;
let txn = call_with_premium_and_pending_block(signer, txn).await?;

let pending_tx = txn.send().await?;
let receipt = pending_tx.retries(TRANSACTION_RECEIPT_RETRIES).await?;
Expand Down Expand Up @@ -630,7 +632,7 @@ impl SubnetManager for EthSubnetManager {
gateway_manager_facet::FvmAddress::try_from(to)?,
value,
);
let txn = call_with_premium_estimation(signer, txn).await?;
let txn = call_with_premium_and_pending_block(signer, txn).await?;

let pending_tx = txn.send().await?;
let receipt = pending_tx.retries(TRANSACTION_RECEIPT_RETRIES).await?;
Expand Down Expand Up @@ -660,7 +662,7 @@ impl SubnetManager for EthSubnetManager {
);
let mut txn = gateway_contract.release(gateway_manager_facet::FvmAddress::try_from(to)?);
txn.tx.set_value(value);
let txn = call_with_premium_estimation(signer, txn).await?;
let txn = call_with_premium_and_pending_block(signer, txn).await?;

let pending_tx = txn.send().await?;
let receipt = pending_tx.retries(TRANSACTION_RECEIPT_RETRIES).await?;
Expand Down Expand Up @@ -695,7 +697,7 @@ impl SubnetManager for EthSubnetManager {
let mut key = [0u8; 32];
key.copy_from_slice(&postbox_msg_key);

call_with_premium_estimation(signer, gateway_contract.propagate(key))
call_with_premium_and_pending_block(signer, gateway_contract.propagate(key))
.await?
.send()
.await?;
Expand Down Expand Up @@ -834,7 +836,7 @@ impl SubnetManager for EthSubnetManager {
let contract =
subnet_actor_manager_facet::SubnetActorManagerFacet::new(address, signer.clone());

call_with_premium_estimation(signer, contract.add_bootstrap_node(endpoint))
call_with_premium_and_pending_block(signer, contract.add_bootstrap_node(endpoint))
.await?
.send()
.await?
Expand Down Expand Up @@ -911,7 +913,7 @@ impl SubnetManager for EthSubnetManager {
tracing::debug!("from address: {:?}", from);

let call = contract.set_federated_power(addresses, pubkeys, power_u256);
let txn = call_with_premium_estimation(signer, call).await?;
let txn = call_with_premium_and_pending_block(signer, call).await?;
let pending_tx = txn.send().await?;
let receipt = pending_tx.retries(TRANSACTION_RECEIPT_RETRIES).await?;
block_number_from_receipt(receipt)
Expand Down Expand Up @@ -1092,10 +1094,8 @@ impl EthSubnetManager {
let wallet = LocalWallet::from_bytes(private_key.private_key())?
.with_chain_id(self.ipc_contract_info.chain_id);

Ok(SignerMiddleware::new(
self.ipc_contract_info.provider.clone(),
wallet,
))
let signer = SignerMiddleware::new(self.ipc_contract_info.provider.clone(), wallet);
Ok(signer)
}

pub fn from_subnet_with_wallet_store(
Expand Down Expand Up @@ -1178,7 +1178,7 @@ impl BottomUpCheckpointRelayer for EthSubnetManager {
signer.clone(),
);
let call = contract.submit_checkpoint(checkpoint, signatories, signatures);
let call = call_with_premium_estimation(signer, call).await?;
let call = call_with_premium_and_pending_block(signer, call).await?;

let pending_tx = call.send().await?;
let receipt = pending_tx.retries(TRANSACTION_RECEIPT_RETRIES).await?;
Expand Down Expand Up @@ -1278,9 +1278,10 @@ impl BottomUpCheckpointRelayer for EthSubnetManager {
}
}

/// Receives an input `FunctionCall` and returns a new instance
/// after estimating an optimal `gas_premium` for the transaction
pub(crate) async fn call_with_premium_estimation<B, D, M>(
/// Takes a `FunctionCall` input and returns a new instance with an estimated optimal `gas_premium`.
/// The function also uses the pending block number to help retrieve the latest nonce
/// via `get_transaction_count` with the `pending` parameter.
pub(crate) async fn call_with_premium_and_pending_block<B, D, M>(
signer: Arc<DefaultSignerMiddleware>,
call: ethers_contract::FunctionCall<B, D, M>,
) -> Result<ethers_contract::FunctionCall<B, D, M>>
Expand All @@ -1289,7 +1290,9 @@ where
M: ethers::abi::Detokenize,
{
let (max_priority_fee_per_gas, _) = premium_estimation(signer).await?;
Ok(call.gas_price(max_priority_fee_per_gas))
Ok(call
.gas_price(max_priority_fee_per_gas)
.block(ethers::types::BlockNumber::Pending))
}

/// Returns an estimation of an optimal `gas_premium` and `gas_fee_cap`
Expand Down

0 comments on commit 1388cd2

Please sign in to comment.