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

test: Add tests and refactor/document structs #871

Merged
merged 1 commit into from
Nov 14, 2024
Merged

Conversation

igamigo
Copy link
Collaborator

@igamigo igamigo commented Sep 12, 2024

Closes #446

@igamigo igamigo force-pushed the igamigo-docs-tests branch 2 times, most recently from 6278299 to 5a04926 Compare September 16, 2024 04:39
@igamigo igamigo marked this pull request as ready for review September 16, 2024 14:57
@igamigo igamigo added the no changelog This PR does not require an entry in the `CHANGELOG.md` file label Sep 16, 2024
@igamigo igamigo requested a review from bobbinth September 22, 2024 23:36
Comment on lines 623 to 624

/// Assert that mock account's `get_item` procedure has index 6
/// Assert that mock account's `get_item` procedure has index 8
#[test]
fn test_check_get_item_index() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I replaced calling foreign procedures by root in favor of using procref. Maybe we don't want this change, but if we do, I'm not sure we need this test anymore.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds reasonable to me. I'm not sure if the intent of the test was to use the MAST root deliberately to test something, but perhaps @Fumuran can clarify.

Copy link
Contributor

Choose a reason for hiding this comment

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

Excuse me for the late reply, I missed the notification.
I think using the procref is much better, for some reason I was so fixated on obtaining the procedure's hash, that I forgot that we can just obtain it with procref. This procedure is necessary only for checking the correct index of the get_item procedure, but since now we use procref this test is not needed anymore. I'll remove it in my test refactoring PR.

Copy link
Contributor

@PhilippGackstatter PhilippGackstatter left a comment

Choose a reason for hiding this comment

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

Nice improvement of replacing quite some test code by using the MockChain.

Not a full review yet, just some preliminary comments.

miden-tx/src/testing/mock_chain/mod.rs Outdated Show resolved Hide resolved
miden-tx/src/testing/mock_chain/mod.rs Outdated Show resolved Hide resolved
miden-tx/src/testing/mock_chain/mod.rs Outdated Show resolved Hide resolved
miden-tx/src/testing/mock_chain/mod.rs Outdated Show resolved Hide resolved
miden-tx/src/testing/tx_context/builder.rs Outdated Show resolved Hide resolved
miden-tx/src/tests/kernel_tests/test_account.rs Outdated Show resolved Hide resolved
///
/// ```
/// let mut mock_chain = MockChain::default();
/// let sender = mock_chain.add_new_wallet(Auth::BasicAuth, vec![asset]); // Add a wallet with assets
Copy link
Contributor

Choose a reason for hiding this comment

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

Just FYI: This is no longer allowed in the new AccountBuilder after #952 since new accounts are not allowed to have assets. This can probably just be changed to be an existing account.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think to be a valid example this still needs to be changed to mock_chain.add_existing_wallet since new wallets are not allowed to have assets.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed the ability of setting assets and changed the examples

Comment on lines 623 to 624

/// Assert that mock account's `get_item` procedure has index 6
/// Assert that mock account's `get_item` procedure has index 8
#[test]
fn test_check_get_item_index() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds reasonable to me. I'm not sure if the intent of the test was to use the MAST root deliberately to test something, but perhaps @Fumuran can clarify.

miden-tx/src/tests/kernel_tests/test_tx.rs Outdated Show resolved Hide resolved
miden-tx/tests/integration/scripts/faucet.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@PhilippGackstatter PhilippGackstatter 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 to me!

I'll wait after the rebase to approve.

Mainly the comment about asserting the error conditions rather than just is_err() would be nice, other than that just small comments.

miden-tx/tests/integration/scripts/p2id.rs Outdated Show resolved Hide resolved
miden-tx/tests/integration/scripts/p2id.rs Outdated Show resolved Hide resolved
miden-tx/tests/integration/scripts/p2id.rs Outdated Show resolved Hide resolved
miden-tx/tests/integration/scripts/p2id.rs Outdated Show resolved Hide resolved
miden-tx/tests/integration/wallet/mod.rs Show resolved Hide resolved
@igamigo
Copy link
Collaborator Author

igamigo commented Nov 12, 2024

Apologies for the delay, was feeling a bit under the weather but should be rebased with all comments addressed now

Copy link
Contributor

@PhilippGackstatter PhilippGackstatter left a comment

Choose a reason for hiding this comment

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

Looks great - thanks for the updates! Hope your feeling better soon, too.

Just optional comments, so I'll approve and I think you can merge when you feel it's ready.

///
/// ```
/// let mut mock_chain = MockChain::default();
/// let sender = mock_chain.add_new_wallet(Auth::BasicAuth, vec![asset]); // Add a wallet with assets
Copy link
Contributor

Choose a reason for hiding this comment

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

I think to be a valid example this still needs to be changed to mock_chain.add_existing_wallet since new wallets are not allowed to have assets.

Comment on lines 432 to 443
// Note: data is stored as [a0, a1, a2, a3] but loaded onto the stack as
// [a3, a2, a1, a0, ...]
let metadata = [
Felt::new(max_supply),
Felt::new(10u64),
TokenSymbol::new(token_symbol).unwrap().into(),
Felt::ZERO,
];

let faucet_component = AccountComponent::new(basic_fungible_faucet_library(), vec![StorageSlot::Value(metadata)])
.expect("basic fungible faucet component should satisfy the requirements of a valid account component")
.with_supported_type(AccountType::FungibleFaucet);
Copy link
Contributor

Choose a reason for hiding this comment

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

You should be able to use the BasicFungibleFaucet component here as on next to write this more concisely:

https://github.com/0xPolygonMiden/miden-base/blame/31b1d7a376fed44308606cdafe9cef9aa7994849/miden-tx/src/testing/mock_chain.rs#L356-L367

Comment on lines +451 to +443
let (mut account, seed, authenticator) = match auth_method {
Auth::BasicAuth => {
Copy link
Contributor

Choose a reason for hiding this comment

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

There is some duplication in this function and add_from_account_builder that we could deduplicate I think.
On the other hand, I will refactor add_from_account_builder anyway (#952 (comment)) so we can just leave it as-is and I'll look at it in the follow-up PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I forgot to do a second pass on this function. I mostly copied code because I didn't find a way to re-set the first slot (for the total issuance) in a way that fit the context of the function (ie, from AccountBuilder before passing it on to add_from_account_builder())

miden-tx/tests/integration/main.rs Outdated Show resolved Hide resolved
Comment on lines +132 to +135
assert_transaction_executor_error!(
tx,
ERR_FUNGIBLE_ASSET_DISTRIBUTE_WOULD_CAUSE_MAX_SUPPLY_TO_BE_EXCEEDED
);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is really nice now, thanks for adding this!

objects/src/testing/assets.rs Outdated Show resolved Hide resolved
let non_fungible_asset_details = NonFungibleAssetDetails::new(
AccountId::try_from(account_id).unwrap(),
AccountId::try_from(ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN).unwrap(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
AccountId::try_from(ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN).unwrap(),
Self::mock_issuer(),

Totally optional.

@@ -106,4 +118,9 @@ impl FungibleAsset {
.expect("asset is valid"),
)
}

/// Returns a mocked asset account ID ([ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN]).
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// Returns a mocked asset account ID ([ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN]).
/// Returns the account ID of the issuer of [`FungibleAsset::mock()`] ([ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN]).

Comment on lines 46 to 51
/// Configures storage slots for a wallet account with authentication.
pub fn with_wallet_storage(&mut self, public_key: PublicKey) -> &mut Self {
self.add_slot(StorageSlot::Value(public_key.into()));

self
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The entire AccountStorageBuilder is now unused. Should we remove it?
I think even in cases where we want to build custom storage for tests we should probably construct it with AccountComponents and if that is insufficient modify it on AccountStorage directly like you did when setting total issuance in a faucet's reserved slot - what do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I thought of completely removing this but was not sure anything else from the outside was currently relying on this. I'll remove it.

@igamigo igamigo merged commit ffc0fd9 into next Nov 14, 2024
9 checks passed
@igamigo igamigo deleted the igamigo-docs-tests branch November 14, 2024 20:19
@bobbinth
Copy link
Contributor

@igamigo - since this is merged, we should be able to close a couple of related issues, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no changelog This PR does not require an entry in the `CHANGELOG.md` file
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants