Skip to content

Support expiration policy #6833

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

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Cargo.lock

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

27 changes: 27 additions & 0 deletions sway-lib-std/src/tx.sw
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub const GTF_POLICY_TIP = 0x501;
pub const GTF_POLICY_WITNESS_LIMIT = 0x502;
pub const GTF_POLICY_MATURITY = 0x503;
pub const GTF_POLICY_MAX_FEE = 0x504;
pub const GTF_POLICY_EXPIRATION = 0x505;

/// A transaction type.
pub enum Transaction {
Expand Down Expand Up @@ -133,6 +134,7 @@ const TIP_POLICY: u32 = 1u32 << 0;
const WITNESS_LIMIT_POLICY: u32 = 1u32 << 1;
const MATURITY_POLICY: u32 = 1u32 << 2;
const MAX_FEE_POLICY: u32 = 1u32 << 3;
const EXPIRATION_POLICY: u32 = 1u32 << 4;

/// Returns policies bits. It can be used to identify which policies are set.
fn policies() -> u32 {
Expand Down Expand Up @@ -259,6 +261,31 @@ pub fn tx_max_fee() -> Option<u64> {
}
}

/// Get the expiration for the transaction, if it is set.
///
/// # Returns
///
/// * [Option<u64>] - The expiration for the transaction.
///
/// # Examples
///
/// ```sway
/// use std::tx::tx_expiration;
///
/// fn foo() {
/// let expiration = tx_expiration().unwrap();
/// log(expiration);
/// }
/// ```
pub fn tx_expiration() -> Option<u64> {
let bits = policies();
if bits & EXPIRATION_POLICY > 0 {
Some(__gtf::<u64>(0, GTF_POLICY_EXPIRATION))
} else {
None
}
}

/// Get the length of the script for the transaction.
///
/// # Returns
Expand Down
4 changes: 4 additions & 0 deletions test/src/sdk-harness/test_artifacts/tx_contract/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ abi TxContractTest {
fn get_tx_maturity() -> Option<u32>;
fn get_tx_witness_limit() -> Option<u64>;
fn get_tx_max_fee() -> Option<u64>;
fn get_tx_expiration() -> Option<u64>;
fn get_tx_script_length() -> Option<u64>;
fn get_tx_script_data_length() -> Option<u64>;
fn get_tx_inputs_count() -> u64;
Expand Down Expand Up @@ -66,6 +67,9 @@ impl TxContractTest for Contract {
fn get_tx_max_fee() -> Option<u64> {
tx_max_fee()
}
fn get_tx_expiration() -> Option<u64> {
tx_expiration()
}
fn get_tx_script_length() -> Option<u64> {
tx_script_length()
}
Expand Down
87 changes: 87 additions & 0 deletions test/src/sdk-harness/test_projects/tx_fields/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,93 @@ mod tx {
assert_eq!(no_maturity.value, None);
}

#[tokio::test]
async fn can_get_expiration() {
let (contract_instance, _, wallet, _) = get_contracts(true).await;

let provider = wallet.try_provider().unwrap();

// With the first call, block will be 0, so this should be valid
let result = contract_instance
.methods()
.get_tx_expiration()
.with_tx_policies(TxPolicies::default().with_expiration(0))
.call()
.await
.unwrap();
assert_eq!(result.value, Some(0 as u32));

// Now it is an error as it is now block number 1
let err = contract_instance
.methods()
.get_tx_expiration()
.with_tx_policies(TxPolicies::default().with_expiration(0))
.call()
.await
.expect_err("expiration reached");

assert!(err.to_string().contains("TransactionExpiration"));

// Testing with other numbers
let result = contract_instance
.methods()
.get_tx_expiration()
.with_tx_policies(TxPolicies::default().with_expiration(10))
.call()
.await
.unwrap();
assert_eq!(result.value, Some(10 as u32));


let result = contract_instance
.methods()
.get_tx_expiration()
.with_tx_policies(TxPolicies::default().with_expiration(1234567890))
.call()
.await
.unwrap();
assert_eq!(result.value, Some(1234567890 as u32));

let result = contract_instance
.methods()
.get_tx_expiration()
.with_tx_policies(TxPolicies::default().with_expiration(u32::max()))
.call()
.await
.unwrap();
assert_eq!(result.value, Some(u32::max()));

let result = contract_instance
.methods()
.get_tx_expiration()
.with_tx_policies(TxPolicies::default().with_expiration(u64::max()))
.call()
.await
.unwrap();
assert_eq!(result.value, Some(u32::max())); // Is this a bug?

// Assert none is returned with no expiration
let no_expiration = contract_instance
.methods()
.get_tx_expiration()
.call()
.await
.unwrap();
assert_eq!(no_expiration.value, None);

// Assert tx errors after expiration
provider.produce_blocks(15, None).await?;
let err = contract_instance
.methods()
.get_tx_expiration()
.with_tx_policies(TxPolicies::default().with_expiration(10))
.call()
.await
.expect_err("expiration reached");

assert!(err.to_string().contains("TransactionExpiration"));
}

#[tokio::test]
async fn can_get_script_length() {
let (contract_instance, _, _, _) = get_contracts(true).await;
Expand Down
Loading