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

Added BlockHeight type and unified the code to use it everywhere #410

Merged
merged 4 commits into from
Mar 27, 2023
Merged
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
10 changes: 5 additions & 5 deletions fuel-tx/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{Cacheable, Input, Mint, Output, StorageSlot, Transaction, TxPointer,
use crate::Signable;

use fuel_crypto::SecretKey;
use fuel_types::{Nonce, Salt, Word};
use fuel_types::{BlockHeight, Nonce, Salt, Word};

use alloc::vec::Vec;

Expand Down Expand Up @@ -57,7 +57,7 @@ where
}

/// Set the maturity
fn set_maturity(&mut self, maturity: Word) {
fn set_maturity(&mut self, maturity: BlockHeight) {
*self.maturity_mut() = maturity;
}
}
Expand Down Expand Up @@ -143,7 +143,7 @@ impl TransactionBuilder<Create> {
}

impl TransactionBuilder<Mint> {
pub fn mint(block_height: u32, tx_index: u16) -> Self {
pub fn mint(block_height: BlockHeight, tx_index: u16) -> Self {
let tx = Mint {
tx_pointer: TxPointer::new(block_height, tx_index),
outputs: Default::default(),
Expand Down Expand Up @@ -196,7 +196,7 @@ impl<Tx: Buildable> TransactionBuilder<Tx> {
self
}

pub fn maturity(&mut self, maturity: Word) -> &mut Self {
pub fn maturity(&mut self, maturity: BlockHeight) -> &mut Self {
self.tx.set_maturity(maturity);

self
Expand All @@ -210,7 +210,7 @@ impl<Tx: Buildable> TransactionBuilder<Tx> {
amount: Word,
asset_id: fuel_types::AssetId,
tx_pointer: TxPointer,
maturity: Word,
maturity: BlockHeight,
) -> &mut Self {
let pk = secret.public_key();

Expand Down
32 changes: 16 additions & 16 deletions fuel-tx/src/tests/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn input() {
rng.gen(),
rng.gen(),
rng.gen(),
rng.next_u64(),
rng.gen(),
),
Input::coin_predicate(
rng.gen(),
Expand Down Expand Up @@ -494,7 +494,7 @@ fn transaction() {
Transaction::script(
rng.next_u64(),
rng.next_u64(),
rng.next_u64(),
rng.gen(),
rng.gen::<Witness>().into_inner(),
rng.gen::<Witness>().into_inner(),
vec![i.clone()],
Expand All @@ -504,7 +504,7 @@ fn transaction() {
Transaction::script(
rng.next_u64(),
rng.next_u64(),
rng.next_u64(),
rng.gen(),
vec![],
generate_bytes(rng),
vec![i.clone()],
Expand All @@ -514,7 +514,7 @@ fn transaction() {
Transaction::script(
rng.next_u64(),
rng.next_u64(),
rng.next_u64(),
rng.gen(),
rng.gen::<Witness>().into_inner(),
vec![],
vec![i.clone()],
Expand All @@ -524,7 +524,7 @@ fn transaction() {
Transaction::script(
rng.next_u64(),
rng.next_u64(),
rng.next_u64(),
rng.gen(),
vec![],
vec![],
vec![i.clone()],
Expand All @@ -534,7 +534,7 @@ fn transaction() {
Transaction::script(
rng.next_u64(),
rng.next_u64(),
rng.next_u64(),
rng.gen(),
vec![],
vec![],
vec![],
Expand All @@ -544,7 +544,7 @@ fn transaction() {
Transaction::script(
rng.next_u64(),
rng.next_u64(),
rng.next_u64(),
rng.gen(),
vec![],
vec![],
vec![],
Expand All @@ -554,7 +554,7 @@ fn transaction() {
Transaction::script(
rng.next_u64(),
rng.next_u64(),
rng.next_u64(),
rng.gen(),
vec![],
vec![],
vec![],
Expand All @@ -566,7 +566,7 @@ fn transaction() {
Transaction::create(
rng.next_u64(),
ConsensusParameters::DEFAULT.max_gas_per_tx,
rng.next_u64(),
rng.gen(),
rng.gen(),
rng.gen(),
vec![s.clone()],
Expand All @@ -577,7 +577,7 @@ fn transaction() {
Transaction::create(
rng.next_u64(),
ConsensusParameters::DEFAULT.max_gas_per_tx,
rng.next_u64(),
rng.gen(),
rng.gen(),
rng.gen(),
vec![s],
Expand All @@ -588,7 +588,7 @@ fn transaction() {
Transaction::create(
rng.next_u64(),
ConsensusParameters::DEFAULT.max_gas_per_tx,
rng.next_u64(),
rng.gen(),
rng.gen(),
rng.gen(),
vec![],
Expand All @@ -599,7 +599,7 @@ fn transaction() {
Transaction::create(
rng.next_u64(),
ConsensusParameters::DEFAULT.max_gas_per_tx,
rng.next_u64(),
rng.gen(),
rng.gen(),
rng.gen(),
vec![],
Expand All @@ -610,7 +610,7 @@ fn transaction() {
Transaction::create(
rng.next_u64(),
ConsensusParameters::DEFAULT.max_gas_per_tx,
rng.next_u64(),
rng.gen(),
rng.gen(),
rng.gen(),
vec![],
Expand All @@ -621,7 +621,7 @@ fn transaction() {
Transaction::create(
rng.next_u64(),
ConsensusParameters::DEFAULT.max_gas_per_tx,
rng.next_u64(),
rng.gen(),
rng.gen(),
rng.gen(),
vec![],
Expand All @@ -646,7 +646,7 @@ fn create_input_data_offset() {

let gas_price = 100;
let gas_limit = 1000;
let maturity = 10;
let maturity = 10.into();
let bytecode_witness_index = 0x00;
let salt = rng.gen();

Expand Down Expand Up @@ -758,7 +758,7 @@ fn script_input_coin_data_offset() {

let gas_price = 100;
let gas_limit = 1000;
let maturity = 10;
let maturity = 10.into();

let script: Vec<Vec<u8>> = vec![vec![], generate_bytes(rng)];
let script_data: Vec<Vec<u8>> = vec![vec![], generate_bytes(rng)];
Expand Down
8 changes: 4 additions & 4 deletions fuel-tx/src/tests/valid_cases/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ fn transaction_with_duplicate_coin_inputs_is_invalid() {
.add_input(b)
.add_witness(rng.gen())
.finalize()
.check_without_signatures(0, &Default::default())
.check_without_signatures(Default::default(), &Default::default())
.expect_err("Expected checkable failure");

assert_eq!(err, CheckError::DuplicateInputUtxoId { utxo_id });
Expand All @@ -431,7 +431,7 @@ fn transaction_with_duplicate_message_inputs_is_invalid() {
.add_input(message_input)
.add_witness(rng.gen())
.finalize()
.check_without_signatures(0, &Default::default())
.check_without_signatures(Default::default(), &Default::default())
.expect_err("Expected checkable failure");

assert_eq!(err, CheckError::DuplicateMessageInputId { message_id });
Expand All @@ -454,7 +454,7 @@ fn transaction_with_duplicate_contract_inputs_is_invalid() {
.add_output(o)
.add_output(p)
.finalize()
.check_without_signatures(0, &Default::default())
.check_without_signatures(Default::default(), &Default::default())
.expect_err("Expected checkable failure");

assert_eq!(err, CheckError::DuplicateInputContractId { contract_id });
Expand All @@ -477,6 +477,6 @@ fn transaction_with_duplicate_contract_utxo_id_is_valid() {
.add_output(o)
.add_output(p)
.finalize()
.check_without_signatures(0, &Default::default())
.check_without_signatures(Default::default(), &Default::default())
.expect("Duplicated UTXO id is valid for contract input");
}
6 changes: 3 additions & 3 deletions fuel-tx/src/tests/valid_cases/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn contract() {
rng.gen(),
rng.gen(),
rng.next_u32().to_be_bytes()[0],
rng.next_u64(),
rng.gen(),
),
Input::contract(rng.gen(), rng.gen(), rng.gen(), rng.gen(), rng.gen()),
],
Expand All @@ -46,7 +46,7 @@ fn contract() {
rng.gen(),
rng.gen(),
rng.next_u32().to_be_bytes()[0],
rng.next_u64(),
rng.gen(),
),
Input::contract(rng.gen(), rng.gen(), rng.gen(), rng.gen(), rng.gen()),
],
Expand All @@ -67,7 +67,7 @@ fn contract() {
rng.gen(),
rng.gen(),
rng.next_u32().to_be_bytes()[0],
rng.next_u64(),
rng.gen(),
),
Input::contract(rng.gen(), rng.gen(), rng.gen(), rng.gen(), rng.gen()),
],
Expand Down
40 changes: 20 additions & 20 deletions fuel-tx/src/tests/valid_cases/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use std::io::Write;
fn gas_limit() {
let rng = &mut StdRng::seed_from_u64(8586);

let maturity = 100;
let block_height = 1000;
let maturity = 100.into();
let block_height = 1000.into();

Transaction::script(
rng.gen(),
Expand Down Expand Up @@ -78,7 +78,7 @@ fn gas_limit() {
fn maturity() {
let rng = &mut StdRng::seed_from_u64(8586);

let block_height = 1000;
let block_height = 1000.into();

Transaction::script(
rng.gen(),
Expand All @@ -96,7 +96,7 @@ fn maturity() {
Transaction::create(
rng.gen(),
PARAMS.max_gas_per_tx,
1000,
1000.into(),
0,
rng.gen(),
vec![],
Expand All @@ -110,7 +110,7 @@ fn maturity() {
let err = Transaction::script(
rng.gen(),
PARAMS.max_gas_per_tx,
1001,
1001.into(),
vec![],
vec![],
vec![],
Expand All @@ -125,7 +125,7 @@ fn maturity() {
let err = Transaction::create(
rng.gen(),
PARAMS.max_gas_per_tx,
1001,
1001.into(),
0,
rng.gen(),
vec![],
Expand All @@ -143,8 +143,8 @@ fn maturity() {
fn max_iow() {
let rng = &mut StdRng::seed_from_u64(8586);

let maturity = 100;
let block_height = 1000;
let maturity = 100.into();
let block_height = 1000.into();

let secret = SecretKey::random(rng);

Expand Down Expand Up @@ -300,8 +300,8 @@ fn max_iow() {
fn output_change_asset_id() {
let rng = &mut StdRng::seed_from_u64(8586);

let maturity = 100;
let block_height = 1000;
let maturity = 100.into();
let block_height = 1000.into();

let a: AssetId = rng.gen();
let b: AssetId = rng.gen();
Expand Down Expand Up @@ -374,8 +374,8 @@ fn output_change_asset_id() {
fn script() {
let rng = &mut StdRng::seed_from_u64(8586);

let maturity = 100;
let block_height = 1000;
let maturity = 100.into();
let block_height = 1000.into();

let secret = SecretKey::random(rng);
let asset_id: AssetId = rng.gen();
Expand Down Expand Up @@ -443,8 +443,8 @@ fn script() {
fn create() {
let rng = &mut StdRng::seed_from_u64(8586);

let maturity = 100;
let block_height = 1000;
let maturity = 100.into();
let block_height = 1000.into();

let secret = SecretKey::random(rng);
let secret_b = SecretKey::random(rng);
Expand Down Expand Up @@ -659,19 +659,19 @@ fn create() {
fn mint() {
let rng = &mut StdRng::seed_from_u64(8586);

let block_height = 1000;
let block_height = 1000.into();

TransactionBuilder::mint(block_height, rng.gen())
.add_output(Output::coin(rng.gen(), rng.next_u64(), rng.gen()))
.add_output(Output::coin(rng.gen(), rng.next_u64(), rng.gen()))
.finalize()
.check(block_height as Word, &PARAMS)
.check(block_height, &PARAMS)
.expect("Failed to validate tx");

let err = TransactionBuilder::mint(block_height, rng.gen())
.add_output(Output::contract(0, rng.gen(), rng.gen()))
.finalize()
.check(block_height as Word, &PARAMS)
.check(block_height, &PARAMS)
.expect_err("Expected erroneous transaction");

assert_eq!(err, CheckError::TransactionMintOutputIsNotCoin);
Expand All @@ -680,7 +680,7 @@ fn mint() {
.add_output(Output::coin(rng.gen(), rng.next_u64(), AssetId::BASE))
.add_output(Output::coin(rng.gen(), rng.next_u64(), AssetId::BASE))
.finalize()
.check(block_height as Word, &PARAMS)
.check(block_height, &PARAMS)
.expect_err("Expected erroneous transaction");

assert_eq!(err, CheckError::TransactionOutputCoinAssetIdDuplicated(AssetId::BASE));
Expand All @@ -689,7 +689,7 @@ fn mint() {
.add_output(Output::coin(rng.gen(), rng.next_u64(), AssetId::BASE))
.add_output(Output::coin(rng.gen(), rng.next_u64(), AssetId::BASE))
.finalize()
.check(block_height as Word + 1, &PARAMS)
.check(block_height + 1.into(), &PARAMS)
.expect_err("Expected erroneous transaction");

assert_eq!(err, CheckError::TransactionMintIncorrectBlockHeight);
Expand All @@ -699,7 +699,7 @@ fn mint() {
fn tx_id_bytecode_len() {
let rng = &mut StdRng::seed_from_u64(8586);

let maturity = 100;
let maturity = 100.into();
let gas_price = rng.gen();
let salt = rng.gen();

Expand Down
Loading