Skip to content

Commit

Permalink
Add block timeout option to MsgMicroTx
Browse files Browse the repository at this point in the history
We need the ability to time out a transaction after a specific number of
blocks in order to avoid double payment cases when making MicroTx
  • Loading branch information
jkilpatr committed Mar 31, 2024
1 parent 8673442 commit 14a886b
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "deep_space"
version = "2.23.6"
version = "2.24.0"
authors = ["Justin Kilpatrick <[email protected]>", "Michał Papierski <[email protected]>"]
repository = "https://github.com/althea-net/deep_space"
description = "A highly portable, batteries included, transaction generation and key management library for CosmosSDK blockchains"
Expand Down
8 changes: 4 additions & 4 deletions src/client/distribution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Contact {
};

let msg = Msg::new(MSG_WITHDRAW_DELEGATOR_REWARD_TYPE_URL, msg);
self.send_message(&[msg], None, &[fee], wait_timeout, private_key)
self.send_message(&[msg], None, &[fee], wait_timeout, None, private_key)
.await
}

Expand Down Expand Up @@ -202,7 +202,7 @@ impl Contact {
msgs.push(msg);
}

self.send_message(&msgs, None, &[fee], wait_timeout, private_key)
self.send_message(&msgs, None, &[fee], wait_timeout, None, private_key)
.await
}

Expand All @@ -219,7 +219,7 @@ impl Contact {
};

let msg = Msg::new(MSG_WITHDRAW_VALIDATOR_COMMISSION_TYPE_URL, msg);
self.send_message(&[msg], None, &[fee], wait_timeout, private_key)
self.send_message(&[msg], None, &[fee], wait_timeout, None, private_key)
.await
}

Expand All @@ -238,7 +238,7 @@ impl Contact {
};

let msg = Msg::new(MSG_FUND_COMMUNITY_POOL_TYPE_URL, msg);
self.send_message(&[msg], None, &[fee], wait_timeout, private_key)
self.send_message(&[msg], None, &[fee], wait_timeout, None, private_key)
.await
}
}
9 changes: 8 additions & 1 deletion src/client/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ use std::time::Duration;
use std::time::Instant;
use tokio::time::{sleep, timeout};

/// This is the default block timeout, it's used when the user doesn't specify a timeout
/// height for a transaction this will be used. It's best to always have a timeout for all transactions
/// to prevent them from becoming stuck or being included at unexpected times
pub const DEFAULT_TRANSACTION_TIMEOUT_BLOCKS: u64 = 100;

impl Contact {
/// Gets the current chain status, returns an enum taking into account the various possible states
/// of the chain and the requesting full node. In the common case this provides the block number
Expand Down Expand Up @@ -215,6 +220,7 @@ impl Contact {
&self,
our_address: Address,
fee: Fee,
timeout_block: Option<u64>,
) -> Result<MessageArgs, CosmosGrpcError> {
let account_info = self.get_account_info(our_address).await?;

Expand All @@ -228,7 +234,8 @@ impl Contact {
account_number: account_info.account_number,
chain_id: header.chain_id,
fee,
timeout_height: header.height as u64 + 100,
timeout_height: header.height as u64
+ timeout_block.unwrap_or(DEFAULT_TRANSACTION_TIMEOUT_BLOCKS),
})
} else {
Err(CosmosGrpcError::BadResponse(
Expand Down
4 changes: 2 additions & 2 deletions src/client/gov/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl Contact {
};

let msg = Msg::new(MSG_VOTE_TYPE_URL, vote);
self.send_message(&[msg], None, &[fee], wait_timeout, private_key)
self.send_message(&[msg], None, &[fee], wait_timeout, None, private_key)
.await
}

Expand All @@ -142,7 +142,7 @@ impl Contact {
};

let msg = Msg::new(MSG_SUBMIT_PROPOSAL_TYPE_URL, proposal);
self.send_message(&[msg], None, &[fee], wait_timeout, private_key)
self.send_message(&[msg], None, &[fee], wait_timeout, None, private_key)
.await
}

Expand Down
1 change: 1 addition & 0 deletions src/client/invariant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ impl Contact {
Some("AAAAAAAHHHHHHH".to_string()),
&[fee_coin.unwrap_or_default()],
Some(wait_timeout),
None,
private_key,
)
.await
Expand Down
12 changes: 10 additions & 2 deletions src/client/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ impl Contact {
/// * `memo` - An optional memo to be included in the transaction, if None the default memo value is set
/// * `fee_coin` - A fee amount and coin type to use, pass an empty array to send a zero fee transaction
/// * `wait_timeout` - An optional amount of time to wait for the transaction to enter the blockchain
/// * `block_timeout` - An optional number of blocks into the future that this transaction should be valid for.
/// If None, DEFAULT_TRANSACTION_TIMEOUT_BLOCKS is used.
/// * `private_key` - A private key used to sign and send the transaction
/// # Examples
/// ```rust
Expand Down Expand Up @@ -143,14 +145,15 @@ impl Contact {
memo: Option<String>,
fee_coin: &[Coin],
wait_timeout: Option<Duration>,
block_timeout: Option<u64>,
private_key: impl PrivateKey,
) -> Result<TxResponse, CosmosGrpcError> {
let our_address = private_key.to_address(&self.chain_prefix).unwrap();

let fee = self
.get_fee_info(messages, fee_coin, private_key.clone())
.await?;
let args = self.get_message_args(our_address, fee).await?;
let args = self.get_message_args(our_address, fee, block_timeout).await?;
trace!("got optional tx info");

self.send_message_with_args(messages, memo, args, wait_timeout, private_key)
Expand Down Expand Up @@ -257,7 +260,7 @@ impl Contact {
payer: None,
};

let args = self.get_message_args(our_address, fee_obj).await?;
let args = self.get_message_args(our_address, fee_obj, None).await?;

let tx_bytes = private_key.sign_std_msg(messages, args, MEMO)?;

Expand Down Expand Up @@ -327,6 +330,7 @@ impl Contact {
None,
&[fee_coin.unwrap_or_default()],
wait_timeout,
None,
private_key,
)
.await
Expand All @@ -342,6 +346,8 @@ impl Contact {
/// * `fee_coin` - A fee amount and coin type to use, pass None to send a zero fee transaction
/// * `destination` - The target destination address
/// * `wait_timeout` - An optional amount of time to wait for the transaction to enter the blockchain
/// * `block_timeout` - A time period in blocks from when this tx is sent that it will be valid for.
/// The default value is DEFAULT_TRANSACTION_TIMEOUT_BLOCKS
/// * `private_key` - A private key used to sign and send the transaction
/// # Examples
/// ```rust
Expand Down Expand Up @@ -371,6 +377,7 @@ impl Contact {
fee_coin: Option<Coin>,
destination: Address,
wait_timeout: Option<Duration>,
block_timeout: Option<u64>,
private_key: impl PrivateKey,
) -> Result<TxResponse, CosmosGrpcError> {
trace!("Creating transaction");
Expand All @@ -387,6 +394,7 @@ impl Contact {
None,
&[fee_coin.unwrap_or_default()],
wait_timeout,
block_timeout,
private_key,
)
.await
Expand Down
6 changes: 3 additions & 3 deletions src/client/staking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl Contact {
};

let msg = Msg::new(MSG_DELEGATE_TYPE_URL, vote);
self.send_message(&[msg], None, &[fee], wait_timeout, private_key)
self.send_message(&[msg], None, &[fee], wait_timeout, None, private_key)
.await
}

Expand All @@ -144,7 +144,7 @@ impl Contact {
};

let msg = Msg::new(MSG_BEGIN_REDELEGATE_TYPE_URL, redelegate);
self.send_message(&[msg], None, &[fee], wait_timeout, private_key)
self.send_message(&[msg], None, &[fee], wait_timeout, None, private_key)
.await
}

Expand All @@ -167,7 +167,7 @@ impl Contact {
};

let msg = Msg::new(MSG_UNDELEGATE_TYPE_URL, undelegate);
self.send_message(&[msg], None, &[fee], wait_timeout, private_key)
self.send_message(&[msg], None, &[fee], wait_timeout, None, private_key)
.await
}
}

0 comments on commit 14a886b

Please sign in to comment.