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

refactor: replace fungible token example sim tests with workspaces #699

Merged
merged 12 commits into from
Jan 27, 2022

Conversation

itegulov
Copy link
Contributor

I had to cut a few asserts from some of the tests since workspaces does not expose some of the stuff they needed, but at the very least all of the tests have survived the migration without losing meaningfulness. I left TODOs in appropriate places for us to revisit later.

Also, I took the liberty to delete examples/heavy.rs since it was originally added in #307 to showcase sim tests performance. But lmk if you would prefer for me to keep and refactor it too.

Related to #563 #662

let none: Option<bool> = None;
let res = contract
.call(&worker, "storage_deposit")
.args_json((account_id, none))?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@austinabell is there a way to instantiate None with an explicit generic type in one line? I have tried None: Option<bool>, but apparently type ascriptions are experimental.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, can just do () or I think Option::<bool>::None works if you want it to be clear the type (serializes no different though)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem with () is that it does not match the function's signature. Even if None and () do serialize the same way, as an external user reading these examples I would be confused why we are passing () instead of Option<bool>.

Option::<bool>::None works though, thanks!

Copy link
Contributor

@austinabell austinabell left a comment

Choose a reason for hiding this comment

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

oops forgot to finalize this review, sorry if any are stale comments. Was just a quick scan until the CI passes, which seemed more workspaces/sandbox utils related

Comment on lines 10 to 12
const TEN_THOUSAND_NEAR: u128 = 100_000_000_000_000_000_000_000_000_000;
const ONE_HUNDRED_NEAR: u128 = 100_000_000_000_000_000_000_000_000;
const FIFTY_NEAR: u128 = 50_000_000_000_000_000_000_000_000;
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we have the ONE_NEAR constant in the SDK we can use it here to be more readable? Mult can be used with consts with no added cost. Or can just avoid using these constants and do that where it is used

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, at first I have refactored these values to look like 10000 * ONE_NEAR, but later I found out about parse_near! so ended up with in-place parse_near!("10000 N") instead. lmk which way you prefer.

.deposit(ONE_YOCTO)
.transact()
.await?;
assert!(matches!(res.status, FinalExecutionStatus::SuccessValue(_)));
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe worth adding an is_success method or something equivalent to the result to abstract this away in workspaces? cc @ChaoticTempest

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 agree, that would simplify my life as a workspaces user

Copy link
Member

@ChaoticTempest ChaoticTempest Jan 19, 2022

Choose a reason for hiding this comment

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

Interesting. Normally the pattern for these was to do an into_result()? to forward the error, but I see that in this case we want to do assertions to be more explicit. Good idea overall, I'll add a ticket here's a PR near/near-workspaces-rs#58 for it since it's pretty small addition anyways

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, CallExecutionDetails does not have into_result() anyway. Thanks for the PR, I will take a look shortly

examples/fungible-token/tests/workspaces.rs Outdated Show resolved Hide resolved
let none: Option<bool> = None;
let res = contract
.call(&worker, "storage_deposit")
.args_json((account_id, none))?
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, can just do () or I think Option::<bool>::None works if you want it to be clear the type (serializes no different though)

@itegulov
Copy link
Contributor Author

oops forgot to finalize this review, sorry if any are stale comments.

No worries. Indeed, I have already refactored most of the weird stuff away.

Was just a quick scan until the CI passes, which seemed more workspaces/sandbox utils related

Yeah, I am still trying to figure out why Github Actions don't want to run the downloaded sandbox binaries... There is some weirdness going on in Buildkite too, but I have not looked too deeply into that yet.

examples/fungible-token/tests/workspaces.rs Outdated Show resolved Hide resolved
.deposit(ONE_YOCTO)
.transact()
.await?;
assert!(matches!(res.status, FinalExecutionStatus::SuccessValue(_)));
Copy link
Member

@ChaoticTempest ChaoticTempest Jan 19, 2022

Choose a reason for hiding this comment

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

Interesting. Normally the pattern for these was to do an into_result()? to forward the error, but I see that in this case we want to do assertions to be more explicit. Good idea overall, I'll add a ticket here's a PR near/near-workspaces-rs#58 for it since it's pretty small addition anyways

examples/fungible-token/tests/workspaces.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@austinabell austinabell left a comment

Choose a reason for hiding this comment

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

LGTM, I'll wait for @ChaoticTempest approval before pulling in because he might have some tricks :)

register_user(&worker, &contract, defi_contract.id()).await?;

// root invests in defi by calling `ft_transfer_call`
// TODO: Pack into one transaction
Copy link
Contributor

Choose a reason for hiding this comment

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

can you explain what you mean by this? Not completely intuitive

Copy link
Contributor Author

@itegulov itegulov Jan 25, 2022

Choose a reason for hiding this comment

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

yeah sorry for using incorrect terminology. What I meant to say is that these two actions (fn call ft_transfer_call and fn call storage_unregister) are supposed to be batched into a single transaction according to the old simtest-powered tests. Since workspaces do not support batched transactions yet, I had to split these two actions into two separate transactions. But to be fair I do not really see why you would want to batch these two specific action other than to (maybe?) save some gas... Atomicity does not matter here I think.

I will re-phrase the wording

examples/fungible-token/tests/workspaces.rs Outdated Show resolved Hide resolved
Copy link
Member

@ChaoticTempest ChaoticTempest 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 as well. Will be adding the promise results soon, so hopefully we can add in those commented out promise_error checks

examples/fungible-token/tests/workspaces.rs Outdated Show resolved Hide resolved
@@ -5,8 +5,13 @@ authors = ["Near Inc <hello@nearprotocol.com>"]
edition = "2018"

[dev-dependencies]
anyhow = "1.0"
near-primitives = "0.5.0"
Copy link
Contributor

Choose a reason for hiding this comment

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

yeah an extension of what Phuong mentioned, this shouldn't be included

Suggested change
near-primitives = "0.5.0"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As @ChaoticTempest mentioned in the other thread I actually need this to pull FinalExecutionStatus

Copy link
Contributor

Choose a reason for hiding this comment

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

This seems like something that shouldn't be exposed through workspaces

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, do you mean that workspaces should have its own type for expressing the call execution status?

CC @ChaoticTempest

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, this is not an ideal pattern for workspace users to have to import a nearcore internal crate. Not a big deal, but something that should be changed at some point

Copy link
Member

Choose a reason for hiding this comment

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

ooof, this was a pattern I wasn't expecting to cover with asserts! with FinalExecutionStatus. Think we can defer this later until a new minor version of workspaces is released and we can convert these asserts! over to using is_success calls

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Think we can defer this later until a new minor version of workspaces is released and we can convert these asserts! over to using is_success calls

Yeah, we can defer for sure and is_success will help in this case, but CallExecutionDetails::status (which is a part of the public API) will still be a type from near-primitives so I think eventually we would want to replace it with something else.

# Conflicts:
#	examples/fungible-token/Cargo.lock
#	examples/fungible-token/res/defi.wasm
#	examples/fungible-token/res/fungible_token.wasm
@itegulov itegulov force-pushed the daniyar/fungible-token-example-workspaces branch from 3a913d8 to 1af263a Compare January 27, 2022 05:19
@itegulov itegulov merged commit 5dee86c into master Jan 27, 2022
@itegulov itegulov deleted the daniyar/fungible-token-example-workspaces branch January 27, 2022 05:52
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