Skip to content

Commit

Permalink
Make tx timeout configurable from the config file
Browse files Browse the repository at this point in the history
  • Loading branch information
shekohex committed Dec 1, 2023
1 parent 9495ec2 commit eaa1731
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
8 changes: 5 additions & 3 deletions Rocket.example.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
[default]
db = "faucet"
mnemonic="<mnemonic>"
time_to_wait_between_claims = { secs = 0, nanos = 0 }
native_token_amount = 0.5
token_amount = 10
time_to_wait_between_claims = { secs = 30, nanos = 0 }
token_amount = 20
native_token_amount = 3
verify_following_webb = false
tx_timeout = 30000

[global.oauth.twitter]
provider = "twitter"
Expand Down
3 changes: 3 additions & 0 deletions src/faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ pub async fn handle_token_transfer(
.as_u128(),
asset_id: None,
signer: signer_pair.inner().clone(),
timeout: std::time::Duration::from_millis(
app_config.tx_timeout,
),
result_sender,
})
.expect("Failed to send transaction to processor");
Expand Down
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ const fn default_verify_following_webb() -> bool {
true
}

const fn default_tx_timeout_ms() -> u64 {
18_000 // 3 blocks
}

#[derive(Deserialize)]
pub struct AppConfig {
/// The database to use for the auth and claims
Expand All @@ -93,6 +97,10 @@ pub struct AppConfig {
/// Whether to verify that the user is following the webb twitter account
#[serde(default = "default_verify_following_webb")]
pub verify_following_webb: bool,
/// The timeout for transactions in milliseconds
/// This is to prevent the faucet from hanging
#[serde(default = "default_tx_timeout_ms")]
pub tx_timeout: u64,
}

fn auth_db_firing() -> impl Fairing {
Expand Down
13 changes: 9 additions & 4 deletions src/txes/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl TransactionProcessingSystem {
asset_id,
signer,
result_sender,
timeout,
} => {
let res = handle_substrate_tx(
api,
Expand All @@ -68,6 +69,7 @@ impl TransactionProcessingSystem {
native_token_amount,
asset_id,
signer,
timeout,
result_sender,
)
.await;
Expand Down Expand Up @@ -199,13 +201,15 @@ async fn handle_evm_token_tx<M: Middleware>(
}
}

#[allow(clippy::too_many_arguments)]
async fn handle_substrate_tx(
api: OnlineClient<PolkadotConfig>,
to: AccountId32,
_amount: u128,
native_token_amount: u128,
asset_id: Option<u32>,
signer: subxt_signer::sr25519::Keypair,
timeout: std::time::Duration,
result_sender: oneshot::Sender<Result<TxResult, Error>>,
) -> Result<(), Error> {
let res = match asset_id {
Expand All @@ -219,6 +223,7 @@ async fn handle_substrate_tx(
to,
native_token_amount,
signer,
timeout,
)
.await
}
Expand All @@ -236,8 +241,8 @@ async fn handle_substrate_native_tx(
to: AccountId32,
amount: u128,
signer: subxt_signer::sr25519::Keypair,
timeout: std::time::Duration,
) -> Result<TxResult, Error> {
const BLOCK_TIME: u64 = 6000; // 6 seconds
let to_address = MultiAddress::Id(to.clone());
let balance_transfer_tx =
RuntimeApi::tx().balances().transfer(to_address, amount);
Expand All @@ -253,12 +258,12 @@ async fn handle_substrate_native_tx(
let tx_result_fut = tx_api
.sign_and_submit_then_watch_default(&balance_transfer_tx, &signer)
.map_err(|e| Error::Custom(e.to_string()));
let timeout_fut =
tokio::time::sleep(std::time::Duration::from_millis(2 * BLOCK_TIME));

let timeout_fut = tokio::time::sleep(timeout);

let tx_result = tokio::select! {
res = tx_result_fut => res,
_ = timeout_fut => Err(Error::Custom("Timed out waiting for tx to be included in block".to_string())),
_ = timeout_fut => Err(Error::Custom("Timed out waiting for tx to be sent to the network, please try again".to_string())),
}?;

let tx_hash = tx_result.extrinsic_hash();
Expand Down
2 changes: 2 additions & 0 deletions src/txes/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;

use ethers::types::U256;
use ethers::{
Expand Down Expand Up @@ -62,6 +63,7 @@ pub enum Transaction {
native_token_amount: u128,
asset_id: Option<u32>,
signer: subxt_signer::sr25519::Keypair,
timeout: Duration,
result_sender: oneshot::Sender<Result<TxResult, Error>>,
},
}
Expand Down

0 comments on commit eaa1731

Please sign in to comment.