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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
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
27 changes: 22 additions & 5 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 @@ -139,7 +140,9 @@ impl TransactionBuilder<Script> {
script: script.into(),
script_data,
},
policies: Policies::new().with_max_fee(0),
policies: Policies::new()
.with_max_fee(0)
.with_expiration(u32::MAX.into()),
inputs: Default::default(),
outputs: Default::default(),
witnesses: Default::default(),
Expand All @@ -163,7 +166,9 @@ impl TransactionBuilder<Create> {
salt,
storage_slots,
},
policies: Policies::new().with_max_fee(0),
policies: Policies::new()
.with_max_fee(0)
.with_expiration(u32::MAX.into()),
inputs: Default::default(),
outputs: Default::default(),
witnesses: Default::default(),
Expand Down Expand Up @@ -193,7 +198,9 @@ impl TransactionBuilder<Upgrade> {
pub fn upgrade(purpose: UpgradePurpose) -> Self {
let tx = Upgrade {
body: UpgradeBody { purpose },
policies: Policies::new().with_max_fee(0),
policies: Policies::new()
.with_max_fee(0)
.with_expiration(u32::MAX.into()),
inputs: Default::default(),
outputs: Default::default(),
witnesses: Default::default(),
Expand All @@ -207,7 +214,9 @@ impl TransactionBuilder<Upload> {
pub fn upload(body: UploadBody) -> Self {
let tx = Upload {
body,
policies: Policies::new().with_max_fee(0),
policies: Policies::new()
.with_max_fee(0)
.with_expiration(u32::MAX.into()),
inputs: Default::default(),
outputs: Default::default(),
witnesses: Default::default(),
Expand All @@ -221,7 +230,9 @@ impl TransactionBuilder<Blob> {
pub fn blob(body: BlobBody) -> Self {
let tx = Blob {
body,
policies: Policies::new().with_max_fee(0),
policies: Policies::new()
.with_max_fee(0)
.with_expiration(u32::MAX.into()),
inputs: Default::default(),
outputs: Default::default(),
witnesses: Default::default(),
Expand Down Expand Up @@ -376,6 +387,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 @@ -383,6 +383,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())
AurelienFT marked this conversation as resolved.
Show resolved Hide resolved
.with_witness_limit(Word::MAX >> 4)
.with_max_fee(Word::MAX >> 5),
vec![i.clone()],
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
54 changes: 53 additions & 1 deletion fuel-tx/src/tests/valid_cases/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,56 @@ fn maturity() {
assert_eq!(ValidityError::TransactionMaturity, err);
}

#[test]
fn expiration() {
AurelienFT marked this conversation as resolved.
Show resolved Hide resolved
let rng = &mut StdRng::seed_from_u64(8586);

let block_height = 1000.into();

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

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

let err = Transaction::script(
Default::default(),
vec![],
vec![],
Policies::new().with_expiration(999.into()).with_max_fee(0),
vec![],
vec![],
vec![],
)
.check(block_height, &test_params())
.expect_err("Expected erroneous transaction");

assert_eq!(ValidityError::TransactionExpiration, err);

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

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

#[test]
fn script__check__not_set_witness_limit_success() {
// Given
Expand Down Expand Up @@ -901,7 +951,9 @@ fn create__check_without_signatures__errors_if_wrong_witness_index() {

let err = Transaction::create(
1,
Policies::default().with_max_fee(0),
Policies::default()
.with_max_fee(0)
.with_expiration(u32::MAX.into()),
rng.gen(),
vec![],
vec![Input::coin_signed(
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);
}

#[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 expiration() {
AurelienFT marked this conversation as resolved.
Show resolved Hide resolved
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
Loading
Loading