Skip to content
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

Add expiration policy #871

Merged
merged 12 commits into from
Dec 2, 2024
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added
- [871](https://github.com/FuelLabs/fuel-vm/pull/871): Add `expiration` policy that prevent a transaction to be inserted after a given block height.

### Fixed
- [860](https://github.com/FuelLabs/fuel-vm/pull/860): Fixed missing fuzzing coverage report in CI.

Expand Down
4 changes: 4 additions & 0 deletions fuel-asm/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ crate::enum_try_from! {

/// Set `$rA` to `tx.policies[count_ones(0b1111 & tx.policyTypes) - 1].maxFee`
PolicyMaxFee = 0x504,

/// Set `$rA` to `tx.policies[count_ones(0b11111 & tx.policyTypes) - 1].expiration`
PolicyExpiration = 0x505,
},
Immediate12
}
Expand Down Expand Up @@ -340,6 +343,7 @@ fn encode_gtf_args() {
GTFArgs::PolicyTip,
GTFArgs::PolicyWitnessLimit,
GTFArgs::PolicyMaturity,
GTFArgs::PolicyExpiration,
GTFArgs::PolicyMaxFee,
];

Expand Down
7 changes: 7 additions & 0 deletions fuel-tx/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
field::{
self,
BytecodeWitnessIndex,
Expiration,
Maturity,
Tip,
Witnesses,
Expand Down Expand Up @@ -376,6 +377,12 @@ impl<Tx: Buildable> TransactionBuilder<Tx> {
self
}

pub fn expiration(&mut self, expiration: BlockHeight) -> &mut Self {
self.tx.set_expiration(expiration);

self
}

pub fn witness_limit(&mut self, witness_limit: Word) -> &mut Self {
self.tx.set_witness_limit(witness_limit);

Expand Down
16 changes: 16 additions & 0 deletions fuel-tx/src/tests/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ fn transaction_serde_serialization_deserialization() {
},
Policies::new()
.with_tip(Word::MAX >> 1)
.with_expiration((u32::MAX >> 2).into())
.with_maturity((u32::MAX >> 3).into())
.with_witness_limit(Word::MAX >> 4)
.with_max_fee(Word::MAX >> 5),
Expand All @@ -397,6 +398,7 @@ fn transaction_serde_serialization_deserialization() {
Policies::new()
.with_tip(Word::MAX >> 1)
.with_maturity((u32::MAX >> 3).into())
.with_expiration((u32::MAX >> 2).into())
.with_witness_limit(Word::MAX >> 4)
.with_max_fee(Word::MAX >> 5),
vec![],
Expand All @@ -411,6 +413,7 @@ fn transaction_serde_serialization_deserialization() {
Policies::new()
.with_tip(Word::MAX >> 1)
.with_maturity((u32::MAX >> 3).into())
.with_expiration((u32::MAX >> 2).into())
.with_witness_limit(Word::MAX >> 4)
.with_max_fee(Word::MAX >> 5),
vec![],
Expand All @@ -425,6 +428,7 @@ fn transaction_serde_serialization_deserialization() {
Policies::new()
.with_tip(Word::MAX >> 1)
.with_maturity((u32::MAX >> 3).into())
.with_expiration((u32::MAX >> 2).into())
.with_witness_limit(Word::MAX >> 4)
.with_max_fee(Word::MAX >> 5),
vec![],
Expand All @@ -438,6 +442,7 @@ fn transaction_serde_serialization_deserialization() {
Policies::new()
.with_tip(Word::MAX >> 1)
.with_maturity((u32::MAX >> 3).into())
.with_expiration((u32::MAX >> 2).into())
.with_witness_limit(Word::MAX >> 4)
.with_max_fee(Word::MAX >> 5),
vec![i.clone()],
Expand All @@ -451,6 +456,7 @@ fn transaction_serde_serialization_deserialization() {
Policies::new()
.with_tip(Word::MAX >> 1)
.with_maturity((u32::MAX >> 3).into())
.with_expiration((u32::MAX >> 2).into())
.with_witness_limit(Word::MAX >> 4)
.with_max_fee(Word::MAX >> 5),
vec![],
Expand All @@ -464,6 +470,7 @@ fn transaction_serde_serialization_deserialization() {
Policies::new()
.with_tip(Word::MAX >> 1)
.with_maturity((u32::MAX >> 3).into())
.with_expiration((u32::MAX >> 2).into())
.with_witness_limit(Word::MAX >> 4)
.with_max_fee(Word::MAX >> 5),
vec![],
Expand All @@ -477,6 +484,7 @@ fn transaction_serde_serialization_deserialization() {
Policies::new()
.with_tip(Word::MAX >> 1)
.with_maturity((u32::MAX >> 3).into())
.with_expiration((u32::MAX >> 2).into())
.with_witness_limit(Word::MAX >> 4)
.with_max_fee(Word::MAX >> 5),
vec![],
Expand All @@ -496,6 +504,7 @@ fn transaction_serde_serialization_deserialization() {
Policies::new()
.with_tip(Word::MAX >> 1)
.with_maturity((u32::MAX >> 3).into())
.with_expiration((u32::MAX >> 2).into())
.with_witness_limit(Word::MAX >> 4)
.with_max_fee(Word::MAX >> 5),
vec![i.clone()],
Expand All @@ -513,6 +522,7 @@ fn transaction_serde_serialization_deserialization() {
Policies::new()
.with_tip(Word::MAX >> 1)
.with_maturity((u32::MAX >> 3).into())
.with_expiration((u32::MAX >> 2).into())
.with_witness_limit(Word::MAX >> 4)
.with_max_fee(Word::MAX >> 5),
vec![],
Expand All @@ -530,6 +540,7 @@ fn transaction_serde_serialization_deserialization() {
Policies::new()
.with_tip(Word::MAX >> 1)
.with_maturity((u32::MAX >> 3).into())
.with_expiration((u32::MAX >> 2).into())
.with_witness_limit(Word::MAX >> 4)
.with_max_fee(Word::MAX >> 5),
vec![],
Expand All @@ -547,6 +558,7 @@ fn transaction_serde_serialization_deserialization() {
Policies::new()
.with_tip(Word::MAX >> 1)
.with_maturity((u32::MAX >> 3).into())
.with_expiration((u32::MAX >> 2).into())
.with_witness_limit(Word::MAX >> 4)
.with_max_fee(Word::MAX >> 5),
vec![],
Expand All @@ -566,6 +578,7 @@ fn transaction_serde_serialization_deserialization() {
Policies::new()
.with_tip(Word::MAX >> 1)
.with_maturity((u32::MAX >> 3).into())
.with_expiration((u32::MAX >> 2).into())
.with_witness_limit(Word::MAX >> 4)
.with_max_fee(Word::MAX >> 5),
vec![i.clone()],
Expand All @@ -583,6 +596,7 @@ fn transaction_serde_serialization_deserialization() {
Policies::new()
.with_tip(Word::MAX >> 1)
.with_maturity((u32::MAX >> 3).into())
.with_expiration((u32::MAX >> 2).into())
.with_witness_limit(Word::MAX >> 4)
.with_max_fee(Word::MAX >> 5),
vec![],
Expand All @@ -600,6 +614,7 @@ fn transaction_serde_serialization_deserialization() {
Policies::new()
.with_tip(Word::MAX >> 1)
.with_maturity((u32::MAX >> 3).into())
.with_expiration((u32::MAX >> 2).into())
.with_witness_limit(Word::MAX >> 4)
.with_max_fee(Word::MAX >> 5),
vec![],
Expand All @@ -617,6 +632,7 @@ fn transaction_serde_serialization_deserialization() {
Policies::new()
.with_tip(Word::MAX >> 1)
.with_maturity((u32::MAX >> 3).into())
.with_expiration((u32::MAX >> 2).into())
.with_witness_limit(Word::MAX >> 4)
.with_max_fee(Word::MAX >> 5),
vec![],
Expand Down
81 changes: 81 additions & 0 deletions fuel-tx/src/tests/valid_cases/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,87 @@ fn maturity() {
assert_eq!(ValidityError::TransactionMaturity, err);
}

#[test]
fn script__check__valid_expiration_policy() {
let rng = &mut StdRng::seed_from_u64(8586);

let block_height = 1000.into();

TransactionBuilder::script(generate_bytes(rng), generate_bytes(rng))
// Given
.expiration(block_height)
.add_fee_input()
.finalize()
// When
.check(block_height, &test_params())
// Then
.expect("Failed to validate script");
}

#[test]
fn script__check__invalid_expiration_policy() {
AurelienFT marked this conversation as resolved.
Show resolved Hide resolved
let block_height = 1000.into();

// Given
let err = Transaction::script(
Default::default(),
vec![],
vec![],
Policies::new().with_expiration(999.into()).with_max_fee(0),
vec![],
vec![],
vec![],
)
// When
.check(block_height, &test_params())
.expect_err("Expected erroneous transaction");
AurelienFT marked this conversation as resolved.
Show resolved Hide resolved

// Then
assert_eq!(ValidityError::TransactionExpiration, err);
}

#[test]
fn create__check__valid_expiration_policy() {
let rng = &mut StdRng::seed_from_u64(8586);

let block_height = 1000.into();

TransactionBuilder::create(rng.gen(), rng.gen(), vec![])
// Given
.expiration(block_height)
.add_fee_input()
.add_contract_created()
.finalize()
// When
.check(block_height, &test_params())
// Then
.expect("Failed to validate tx create");
}

#[test]
fn create__check__invalid_expiration_policy() {
let rng = &mut StdRng::seed_from_u64(8586);

let block_height = 1000.into();

// Given
let err = Transaction::create(
0,
Policies::new().with_expiration(999.into()).with_max_fee(0),
rng.gen(),
vec![],
vec![],
vec![],
vec![rng.gen()],
)
// When
.check(block_height, &test_params())
.expect_err("Expected erroneous transaction");

// Then
assert_eq!(ValidityError::TransactionExpiration, err);
}

#[test]
fn script__check__not_set_witness_limit_success() {
// Given
Expand Down
17 changes: 17 additions & 0 deletions fuel-tx/src/tests/valid_cases/transaction/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ fn valid_blob_transaction() -> TransactionBuilder<Blob> {
predicate(),
vec![],
));
builder.expiration(u32::MAX.into());

builder
}
Expand Down Expand Up @@ -69,6 +70,22 @@ fn check__fails_if_maturity_not_met() {
assert_eq!(Err(ValidityError::TransactionMaturity), result);
}

#[test]
fn check__fails_if_expiration_not_met() {
// Given
let block_height: BlockHeight = 1000.into();
let failing_block_height = block_height.pred().unwrap();
let tx = valid_blob_transaction()
.expiration(failing_block_height)
.finalize_as_transaction();

// When
let result = tx.check(block_height, &test_params());

// Then
assert_eq!(Err(ValidityError::TransactionExpiration), result);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we check the happy and sad paths for script and create, but not for blob, upgrade, and upload?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It follows the same pattern as the others policies, the correct cas is only tested in valid_cases.rs file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should take the existing coverage as cannon. I think we should cover happy and sad for all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okeyyyy, added :)

}

#[test]
fn check__fails_if_blob_id_doesnt_match_payload() {
// Given
Expand Down
19 changes: 19 additions & 0 deletions fuel-tx/src/tests/valid_cases/transaction/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fn valid_upgrade_transaction() -> TransactionBuilder<Upgrade> {
vec![],
));
builder.with_params(test_params());
builder.expiration(u32::MAX.into());

builder
}
Expand All @@ -52,6 +53,7 @@ fn valid_upgrade_transaction_with_message() -> TransactionBuilder<Upgrade> {
vec![],
));
builder.with_params(test_params());
builder.expiration(u32::MAX.into());

builder
}
Expand Down Expand Up @@ -91,6 +93,23 @@ fn maturity() {
assert_eq!(Err(ValidityError::TransactionMaturity), result);
}

#[test]
fn upgrade__check__valid_expiration_policy() {
let block_height: BlockHeight = 1000.into();
let failing_block_height = block_height.pred().unwrap();

// Given
let tx = valid_upgrade_transaction()
.expiration(failing_block_height)
.finalize_as_transaction();

// When
let result = tx.check(block_height, &test_params());

// Then
assert_eq!(Err(ValidityError::TransactionExpiration), result);
}

#[test]
fn check__not_set_witness_limit_success() {
// Given
Expand Down
18 changes: 18 additions & 0 deletions fuel-tx/src/tests/valid_cases/transaction/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ fn valid_upload_transaction() -> TransactionBuilder<Upload> {
predicate(),
vec![],
));
builder.expiration(u32::MAX.into());

builder
}
Expand Down Expand Up @@ -168,6 +169,23 @@ fn maturity() {
assert_eq!(Err(ValidityError::TransactionMaturity), result);
}

#[test]
fn upload__check__valid_expiration_policy() {
let block_height: BlockHeight = 1000.into();
let failing_block_height = block_height.pred().unwrap();

// Given
let tx = valid_upload_transaction()
.expiration(failing_block_height)
.finalize_as_transaction();

// When
let result = tx.check(block_height, &test_params());

// Then
assert_eq!(Err(ValidityError::TransactionExpiration), result);
}

#[test]
fn check__not_set_witness_limit_success() {
// Given
Expand Down
22 changes: 22 additions & 0 deletions fuel-tx/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,28 @@ pub mod field {
}
}

pub trait Expiration {
fn expiration(&self) -> BlockHeight;
fn set_expiration(&mut self, value: BlockHeight);
}

impl<T: Policies + ?Sized> Expiration for T {
#[inline(always)]
fn expiration(&self) -> BlockHeight {
self.policies()
.get(PolicyType::Expiration)
.and_then(|value| u32::try_from(value).ok())
.unwrap_or(u32::MAX)
.into()
}

#[inline(always)]
fn set_expiration(&mut self, block_height: BlockHeight) {
self.policies_mut()
.set(PolicyType::Expiration, Some(*block_height.deref() as u64))
}
}

pub trait MaxFeeLimit {
fn max_fee_limit(&self) -> Word;
fn set_max_fee_limit(&mut self, value: Word);
Expand Down
Loading
Loading