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

rust runtime integration tests #339

Merged
merged 17 commits into from
Apr 21, 2021
Merged

rust runtime integration tests #339

merged 17 commits into from
Apr 21, 2021

Conversation

4meta5
Copy link
Contributor

@4meta5 4meta5 commented Apr 8, 2021

What does it do?

Adds integration tests for complete Moonbeam runtime.

This will be useful for testing #311 from the runtime context.

What important points reviewers should know?

Is there something left for follow-up PRs?

What alternative implementations were considered?

Are there relevant PRs or issues in other repositories (Substrate, Polkadot, Frontier, Cumulus)?

What value does it bring to the blockchain users?

Checklist

  • Does it require a purge of the network?
  • You bumped the runtime version if there are breaking changes in the runtime ?
  • Does it require changes in documentation/tutorials ?

@4meta5
Copy link
Contributor Author

4meta5 commented Apr 12, 2021

  • test single block author -> rewards

@JoshOrndorff
Copy link
Contributor

I see now, the runtime is imported from use moonbeam_runtime::Runtime. This is great 👍

@JoshOrndorff
Copy link
Contributor

It might be useful to add a test similar to #337 at this level. That would help us know whether the block gas limit problem is in the runtime or RPC layer (I'm guessing runtime, but would be good to confirm).

@4meta5
Copy link
Contributor Author

4meta5 commented Apr 13, 2021

cc @JoshOrndorff I'm having trouble simulating the author-filter logic (necessary for block authoring):

  • the set_author_inherent runtime method is private so can't call it from the integration test
  • when I make the above method public (which shouldn't be done) and call it, it throws the parachain validation data not set error in author-filter so we need to set that validation data from the cumulus::parachain_system pallet but the set_validation_data method is also private in this pallet (and we can't make this method public)

I guess both problems reduce to figuring out how to call private runtime methods from this integration test context.

@JoshOrndorff
Copy link
Contributor

Hmm, that's an interesting point. Here are a few thoughts on how we might proceed. I don't know which if any of them is right.

  1. Should those methods just be public? Is the timestamp pallet's extrinsic public? If that's the solution, we can PR cumulus to make it public.

  2. Maybe we shouldn't bother to actually call the extrinsics. What if the test is like:

  • Alice stakes as collator.
  • Bob nominates her.
  • Simulate alice authoring a block by calling note_author directly (skipping the inherent entirely)
  • Run chain to end of round
  • Assert that both Alice and Bob received rewards.

Although, I admit option #2 is pretty focused on the staking solution and not so much on the authoring...

  1. If we used a different filter (for example height filter) it wouldn't require the parachain inherent. Although that would have negative consequences on the live network. I guess that's more of a thought to share than a solution...

  2. If it turns out authoring is too hard to interface with because of the inherents, another multi-pallet test would be:
    Alice sends funds to bob with balances::transfer
    Bob sends them to charlie through the evm
    Charlie stakes them.
    Assert charlie is staked.

Even if authoring can be made to work, it might not be the most rewarding place to start.

@JoshOrndorff
Copy link
Contributor

@4meta5 4meta5 marked this pull request as ready for review April 16, 2021 19:23
@4meta5 4meta5 added A0-pleasereview Pull request needs code review. B0-silent Changes should not be mentioned in any release notes labels Apr 16, 2021
@4meta5 4meta5 requested review from girazoki and JoshOrndorff April 16, 2021 19:28
Copy link
Contributor

@joelamouche joelamouche left a comment

Choose a reason for hiding this comment

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

Great progress towards security! Got a few comments

runtime/tests/integration_test.rs Show resolved Hide resolved
runtime/tests/integration_test.rs Show resolved Hide resolved
runtime/tests/integration_test.rs Show resolved Hide resolved
runtime/tests/integration_test.rs Show resolved Hide resolved
);
}
for x in 2..1201 {
set_alice_as_author();
Copy link
Contributor

Choose a reason for hiding this comment

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

why do you need to set alice as author if she is the only collator? Maybe set up a test with more than one collator and somehow count how many time each collator was selected as author and test that the balance reward is correct in a dynamic way

Copy link
Contributor Author

Choose a reason for hiding this comment

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

An author must be set every block or on_finalize in author-inherent panics.

Having multiple collators to test correct reward distribution is already done in parachain-staking unit tests. I think it's reasonable to add the same test here as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

@joelamouche This is a good chance to learn about Substrate inherents. When a collator authors a block, they have the opportunity to include inherent extrinsics. In moonbeam there are three of them (timestamp, validation_data, and author) and all three are required. It is behind-the-scenes from a user perspective, but every time a collator authors a moonbeam block, they insert these three inherents. Since Amar is mocking here, and there is no collator process to do any actual block authoring, we have to call these inherents manually. So setting the author to Alice here is mocking Alice authoring the block.

Copy link
Contributor Author

@4meta5 4meta5 Apr 21, 2021

Choose a reason for hiding this comment

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

Maybe set up a test with more than one collator and somehow count how many time each collator was selected as author and test that the balance reward is correct in a dynamic way

I'd rather do this at the pallet level (and have done it there) where I can mock the set_author command more easily.

I added Charlie, Dave, and Eve to the set of collators in this test and none of them were included in the set of valid block authors so it kept returning the following error:

---- reward_block_authors stdout ----
thread 'reward_block_authors' panicked at 'Expected Ok(_). Got Err(
    DispatchErrorWithPostInfo {
        post_info: PostDispatchInfo {
            actual_weight: None,
            pays_fee: Pays::Yes,
        },
        error: DispatchError::Module {
            index: 17,
            error: 1,
            message: Some(
                "CannotBeAuthor",
            ),
        },
    },
)', runtime/tests/integration_test.rs:337:17

Testing multiple block authors in a runtime integration test is worthwhile, but I'd prefer for it to be pursued in a follow up...

@4meta5
Copy link
Contributor Author

4meta5 commented Apr 21, 2021

This feels ready to merge. I'll do it once someone approves :)

Copy link
Contributor

@JoshOrndorff JoshOrndorff 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 to me. Every time I read it, it inspires more ideas of tests to add. I think they should be added in followups though. This PR shows that the method works and adds 3 good tests already.

@JoshOrndorff JoshOrndorff merged commit 113837e into master Apr 21, 2021
@JoshOrndorff JoshOrndorff deleted the amar-int-tests branch April 21, 2021 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A0-pleasereview Pull request needs code review. B0-silent Changes should not be mentioned in any release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants