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

Tests cleanup 2 #771

Merged
merged 17 commits into from
Jul 3, 2024
Merged

Tests cleanup 2 #771

merged 17 commits into from
Jul 3, 2024

Conversation

hackaugusto
Copy link
Contributor

builds on #764

@hackaugusto hackaugusto requested review from igamigo and bobbinth June 25, 2024 19:01

// TEST HELPERS
// ================================================================================================

pub fn consumed_note_data_ptr(note_idx: u32) -> memory::MemoryAddress {
memory::CONSUMED_NOTE_DATA_SECTION_OFFSET + note_idx * memory::NOTE_MEM_SIZE
}

pub fn mock_executed_tx(asset_preservation: AssetPreservationStatus) -> ExecutedTransaction {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was a very weird implementation, the dummy program push.0 drop had nothing to do with the actual tests, which always had custom code. also most tests didn't need the final account state (only one used it).

so in order to make the test setup and the actual test consistent with one another, I removed this in favor of just using the context. It is less code, less indirection, less inconsistencies, and the same results.

/// Loads epilogue file and returns the complete code formatted as
/// "{imports}{epilogue_code}{code}"`
#[cfg(feature = "std")]
fn insert_epilogue(imports: &str, code: &str) -> String {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure what this was trying to accomplish. Replaced it with a simpler use.miden... instead.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Originally I replaced the function with something very similar as I didn't fully understand why it was there. I think only a few tests made use of it so this is much better.

/// Loads epilogue file and returns the complete code formatted as
/// "{prologue_code}{code}"`
#[cfg(feature = "std")]
fn insert_prologue(code: &str) -> String {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This too wasn't really necessary, and using use statements was sufficient for the tests.

@@ -308,6 +313,23 @@ impl From<AccountId> for LeafIndex<ACCOUNT_TREE_DEPTH> {
// CONVERSIONS TO ACCOUNT ID
// ================================================================================================

pub const fn account_id_from_felt(value: Felt) -> Result<AccountId, AccountError> {
Copy link
Contributor Author

@hackaugusto hackaugusto Jun 25, 2024

Choose a reason for hiding this comment

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

I added a few const functions. My goal was to remove the duplication in the test context, when setting up account ids and assets. The issue is that unwrap is no const yet, so I ended up leaving the code changes behind, once unwrap const is declared stable this can be used.

.unwrap()
}

pub fn with_mock_notes_too_few_input(mut self) -> Self {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just like the accounts, I broke up the scenarios into different methods. This makes it easier to modify the scenarios, since the function body contains only the code relevant to the scenario at hand. I'm going to stop here, since at this stage it is easy to add a input_note_transfer_delayed_authentication of sorts, which is what I need for testing, but I think these methods need better names (they are completely cryptic at the moment), some documentation, and some could even be removed because they are not used more than once.

Comment on lines +385 to +399
// ACCOUNT IDS
// --------------------------------------------------------------------------------------------
let sender = AccountId::try_from(ACCOUNT_ID_SENDER).unwrap();
let faucet_id_1 = AccountId::try_from(ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_1).unwrap();
let faucet_id_2 = AccountId::try_from(ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_2).unwrap();
let faucet_id_3 = AccountId::try_from(ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_3).unwrap();

// ASSETS
// --------------------------------------------------------------------------------------------
let fungible_asset_1: Asset =
FungibleAsset::new(faucet_id_1, CONSUMED_ASSET_1_AMOUNT).unwrap().into();
let fungible_asset_2: Asset =
FungibleAsset::new(faucet_id_2, CONSUMED_ASSET_2_AMOUNT).unwrap().into();
let fungible_asset_3: Asset =
FungibleAsset::new(faucet_id_3, CONSUMED_ASSET_3_AMOUNT).unwrap().into();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are the constants I was trying to define using const functions. Unfortunately because of the validation/unwrap it is not possible yet.

@hackaugusto hackaugusto force-pushed the hacka-tests-cleanup-2 branch 2 times, most recently from d3ce3ec to 7b630cd Compare June 25, 2024 19:40
@bobbinth
Copy link
Contributor

@hackaugusto - I merged #764 first and it screwed up commits in this PR. Could you rebase this PR from next?

@hackaugusto hackaugusto force-pushed the hacka-tests-cleanup-2 branch from 7b630cd to e3dada6 Compare June 26, 2024 12:32
Comment on lines +236 to +248
fn input_note_simple(
&mut self,
sender: AccountId,
assets: impl IntoIterator<Item = Asset>,
inputs: impl IntoIterator<Item = Felt>,
) -> Note {
NoteBuilder::new(sender, ChaCha20Rng::from_seed(self.rng.gen()))
.note_inputs(inputs)
.unwrap()
.add_assets(assets)
.build(&self.assembler)
.unwrap()
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

NOTE: I'm not sure any of these notes are valid. The default code of a note is empty, literally begin end, meaning the assets are not added to the account running the tx kernel nor added to a new output note. These should fail on the epilogue check, when the asset vault from the inputs+account is compared against the outputs+account. I believe tests are not failing because we are not testing the main.masm.

With that said, I didn't dig through further. I would need to read all the tests, and figure out how they are using the notes, to know what the fix is (that is, if my guess above is even correct). Figuring out what these functions are actually used for needs more work.

Comment on lines +250 to +287
fn input_note_with_one_output_note(
&mut self,
sender: AccountId,
assets: impl IntoIterator<Item = Asset>,
inputs: impl IntoIterator<Item = Felt>,
output: &Note,
) -> Note {
let code = format!(
"
begin
# NOTE
# ---------------------------------------------------------------------------------
push.{recipient}
push.{PUBLIC_NOTE}
push.{aux}
push.{tag}
call.{ACCOUNT_CREATE_NOTE_MAST_ROOT}

push.{asset} movup.4
call.{ACCOUNT_ADD_ASSET_TO_NOTE_MAST_ROOT}
dropw dropw dropw
end
",
PUBLIC_NOTE = NoteType::Public as u8,
recipient = prepare_word(&output.recipient().digest()),
aux = output.metadata().aux(),
tag = output.metadata().tag(),
asset = prepare_assets(output.assets())[0],
);

NoteBuilder::new(sender, ChaCha20Rng::from_seed(self.rng.gen()))
.note_inputs(inputs)
.unwrap()
.add_assets(assets)
.code(code)
.build(&self.assembler)
.unwrap()
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

NOTE: I'm not sure what is the point of these notes. It looks like it should be a tx_script instead? There are quite a bit of weirdness to these:

  • the inputs are never used
  • the input assets are never consumed
  • the output could have more than one asset, which is not handled here

These are largely also issues with output_notes_data_procedure.

Here too more work is needed to figure out how to handle these.

Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you! I didn't do a super in-depth review - so, it would be good to get another review from @igamigo.

@@ -40,24 +40,29 @@ pub enum AccountType {
RegularAccountUpdatableCode = REGULAR_ACCOUNT_UPDATABLE_CODE,
}

pub const fn account_type_from_u64(value: u64) -> AccountType {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add doc comments to this function. Specifically, we should mention that account type is expected to be in the two most significant bits of the passed-in value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

@@ -308,6 +313,23 @@ impl From<AccountId> for LeafIndex<ACCOUNT_TREE_DEPTH> {
// CONVERSIONS TO ACCOUNT ID
// ================================================================================================

pub const fn account_id_from_felt(value: Felt) -> Result<AccountId, AccountError> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add a doc comment to this function (could be the same as on lines 336 - 342 below).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

@hackaugusto hackaugusto force-pushed the hacka-tests-cleanup-2 branch from e3dada6 to 273d0c0 Compare July 2, 2024 14:10
Copy link
Collaborator

@igamigo igamigo left a comment

Choose a reason for hiding this comment

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

LGTM, all changes make sense :)

/// Loads epilogue file and returns the complete code formatted as
/// "{imports}{epilogue_code}{code}"`
#[cfg(feature = "std")]
fn insert_epilogue(imports: &str, code: &str) -> String {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Originally I replaced the function with something very similar as I didn't fully understand why it was there. I think only a few tests made use of it so this is much better.

@hackaugusto hackaugusto force-pushed the hacka-tests-cleanup-2 branch from 273d0c0 to 04e42d2 Compare July 3, 2024 14:10
@hackaugusto hackaugusto merged commit f25bf32 into next Jul 3, 2024
12 checks passed
@hackaugusto hackaugusto deleted the hacka-tests-cleanup-2 branch July 3, 2024 14:26
@hackaugusto
Copy link
Contributor Author

merged the PR since it had two separate approvals, and it was starting to get conflicts

bobbinth pushed a commit that referenced this pull request Jul 4, 2024
* tests: output_notes_data_procedure takes &[Note]

* tests: removed mock_executed_tx

* tests: removed insert_epilogue

* tests: removed insert_prologue

* tests: split mock_notes utility into two

* chore: removed unecessary constructors

* chore: removed unecessary constructors

* tests: added const fungible asset constructor

* tests: builder accepting multiple assets

* tests: builder accepting intoiterator

* tests: method to create individual output notes

* tests: method to create input note wich creates two output notes

* tests: method to create input note wich creates one output note

* tests: method to create simple transfer note

* tests: method to create simple transfer note

* tests: split notes scenarios into many methods

* docs: added docs to account id utils
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants