Skip to content

Commit

Permalink
refactor: general clean-up for forc-test (FuelLabs#3987)
Browse files Browse the repository at this point in the history
## Description

This PR addresses couple of left over comments from previous PRs and
introduces a general clean-up to the `forc-test`. Since lots of new
stuff added rapidly to the codebase some of the comments were left over.
Especially from
FuelLabs#3672 (review)

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
  • Loading branch information
kayagokalp authored Feb 6, 2023
1 parent 685a92b commit bdfe0eb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 64 deletions.
2 changes: 1 addition & 1 deletion forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ impl Built {
}
}

/// Tries to retrieve the `Built` as a `BuiltPackage`, panics otherwise.
/// Tries to retrieve the `Built` as a `BuiltPackage`.
pub fn expect_pkg(self) -> Result<BuiltPackage> {
match self {
Built::Package(built_pkg) => Ok(*built_pkg),
Expand Down
97 changes: 34 additions & 63 deletions forc-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ use fuel_tx as tx;
use fuel_vm::{self as vm, prelude::Opcode};
use pkg::TestPassCondition;
use pkg::{Built, BuiltPackage, CONTRACT_ID_CONSTANT_NAME};
use rand::{distributions::Standard, prelude::Distribution, Rng, SeedableRng};
use rand::{Rng, SeedableRng};
use sway_core::{language::parsed::TreeType, BuildTarget};
use sway_types::{ConfigTimeConstant, Span};
use tx::{AssetId, TxPointer, UtxoId};
use vm::prelude::SecretKey;

/// The result of a `forc test` invocation.
#[derive(Debug)]
Expand Down Expand Up @@ -52,6 +50,8 @@ pub struct TestResult {
pub logs: Vec<fuel_tx::Receipt>,
}

const TEST_METADATA_SEED: u64 = 0x7E57u64;

/// A package or a workspace that has been built, ready for test execution.
pub enum BuiltTests {
Package(PackageTests),
Expand Down Expand Up @@ -108,18 +108,6 @@ pub struct TestPrintOpts {
pub print_logs: bool,
}

/// The required common metadata for building a transaction to deploy a contract or run a test.
#[derive(Debug)]
struct TxMetadata {
secret_key: SecretKey,
utxo_id: UtxoId,
amount: u64,
asset_id: AssetId,
tx_pointer: TxPointer,
maturity: u64,
block_height: tx::Word,
}

/// The storage and the contract id (if a contract is being tested) for a test.
#[derive(Debug)]
struct TestSetup {
Expand Down Expand Up @@ -254,23 +242,6 @@ impl<'a> PackageTests {
}
}

impl Distribution<TxMetadata> for Standard {
/// Samples a random sample for `TxMetadata` which contains both random and constant variables.
/// For random variables a random sampling is done. For constant fields a constant value that
/// can be used directly with `TransactionBuilder` (for test transactions) is set.
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> TxMetadata {
TxMetadata {
secret_key: rng.gen(),
utxo_id: rng.gen(),
amount: 1,
asset_id: rng.gen(),
tx_pointer: rng.gen(),
maturity: 1,
block_height: (u32::MAX >> 1) as u64,
}
}
}

impl Opts {
/// Convert this set of test options into a set of build options.
pub fn into_build_opts(self) -> pkg::BuildOpts {
Expand All @@ -291,12 +262,9 @@ impl Opts {
}

/// Patch this set of test options, so that it will build the package at the given `path`.
pub(crate) fn patch_opts(self, path: &std::path::Path) -> Opts {
let mut opts = self;
let mut pkg_opts = opts.pkg;
pkg_opts.path = path.to_str().map(|path_str| path_str.to_string());
opts.pkg = pkg_opts;
opts
pub(crate) fn with_path(&mut self, path: &std::path::Path) -> Opts {
self.pkg.path = path.to_str().map(ToString::to_string);
self.clone()
}
}

Expand Down Expand Up @@ -377,7 +345,7 @@ fn build_contracts_without_tests(
let pkg_path = pkg_manifest.dir();
let build_opts_without_tests = opts
.clone()
.patch_opts(pkg_path)
.with_path(pkg_path)
.into_build_opts()
.include_tests(false);
let built_pkg =
Expand Down Expand Up @@ -440,21 +408,22 @@ fn deploy_test_contract(built_pkg: BuiltPackage) -> anyhow::Result<TestSetup> {
let mut interpreter = vm::interpreter::Interpreter::with_storage(storage, params);

// Create the deployment transaction.
let mut rng = rand::rngs::StdRng::seed_from_u64(0x7E57u64);
let metadata: TxMetadata = rng.gen();
let mut rng = rand::rngs::StdRng::seed_from_u64(TEST_METADATA_SEED);

// Prepare the transaction metadata.
let secret_key = rng.gen();
let utxo_id = rng.gen();
let amount = 1;
let maturity = 1;
let asset_id = rng.gen();
let tx_pointer = rng.gen();
let block_height = (u32::MAX >> 1) as u64;

let tx = tx::TransactionBuilder::create(bytecode.into(), salt, storage_slots)
.add_unsigned_coin_input(
metadata.secret_key,
metadata.utxo_id,
metadata.amount,
metadata.asset_id,
metadata.tx_pointer,
metadata.maturity,
)
.add_unsigned_coin_input(secret_key, utxo_id, amount, asset_id, tx_pointer, maturity)
.add_output(tx::Output::contract_created(contract_id, state_root))
.maturity(metadata.maturity)
.finalize_checked(metadata.block_height, &params);
.maturity(maturity)
.finalize_checked(block_height, &params);

// Deploy the contract.
interpreter.transact(tx)?;
Expand Down Expand Up @@ -537,20 +506,22 @@ fn exec_test(

// Create a transaction to execute the test function.
let script_input_data = vec![];
let mut rng = rand::rngs::StdRng::seed_from_u64(0x7E57u64);
let metadata: TxMetadata = rng.gen();
let mut rng = rand::rngs::StdRng::seed_from_u64(TEST_METADATA_SEED);

// Prepare the transaction metadata.
let secret_key = rng.gen();
let utxo_id = rng.gen();
let amount = 1;
let maturity = 1;
let asset_id = rng.gen();
let tx_pointer = rng.gen();
let block_height = (u32::MAX >> 1) as u64;

let params = tx::ConsensusParameters::default();
let mut tx = tx::TransactionBuilder::script(bytecode, script_input_data)
.add_unsigned_coin_input(
metadata.secret_key,
metadata.utxo_id,
metadata.amount,
metadata.asset_id,
metadata.tx_pointer,
0,
)
.add_unsigned_coin_input(secret_key, utxo_id, amount, asset_id, tx_pointer, 0)
.gas_limit(tx::ConsensusParameters::DEFAULT.max_gas_per_tx)
.maturity(metadata.maturity)
.maturity(maturity)
.clone();
if let Some(contract_id) = contract_id {
tx.add_input(tx::Input::Contract {
Expand All @@ -566,7 +537,7 @@ fn exec_test(
state_root: tx::Bytes32::zeroed(),
});
}
let tx = tx.finalize_checked(metadata.block_height, &params);
let tx = tx.finalize_checked(block_height, &params);

let mut interpreter = vm::interpreter::Interpreter::with_storage(storage, params);

Expand Down

0 comments on commit bdfe0eb

Please sign in to comment.