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

feat: add event checks to motsu #441

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft

Conversation

bidzyyys
Copy link
Collaborator

Resolves #307

PR Checklist

  • Tests
  • Documentation
  • Changelog

@bidzyyys bidzyyys self-assigned this Dec 10, 2024
@bidzyyys bidzyyys linked an issue Dec 10, 2024 that may be closed by this pull request
1 task
Copy link

netlify bot commented Dec 10, 2024

Deploy Preview for contracts-stylus ready!

Name Link
🔨 Latest commit f1aa080
🔍 Latest deploy log https://app.netlify.com/sites/contracts-stylus/deploys/6757eaebdcc674000894a4ba
😎 Deploy Preview https://deploy-preview-441--contracts-stylus.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Member

@qalisander qalisander left a comment

Choose a reason for hiding this comment

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

Awesome @bidzyyys!
Few improvements I have:

block_timestamp: u64,
chain_id: u64,
contract_address: [u8; 42],
events: Vec<Vec<u8>>,
Copy link
Member

@qalisander qalisander Dec 10, 2024

Choose a reason for hiding this comment

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

I would better create wrapper type for event, to store them like Vec<Event> (or Vec<EncodedEvent>)

/// Dummy msg sender set for tests.
const MSG_SENDER: &[u8; 42] = b"0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF";

pub(crate) struct Environment {
Copy link
Member

Choose a reason for hiding this comment

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

I remember, that the whole reason of MockStorage was to store not just storage bytes (contract_storage), but also fields that you've put into Environment struct

Copy link
Member

Choose a reason for hiding this comment

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

Because these data are also "mocked" values for our test cases

//! Module with unit test EVM environment for Stylus contracts.

/// Block Timestamp - Epoch timestamp: 1st January 2025 `00::00::00`.
const BLOCK_TIMESTAMP: u64 = 1_735_689_600;
Copy link
Member

Choose a reason for hiding this comment

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

Good! Seems rational for me to move these constants to the other module

// https://github.com/OffchainLabs/stylus-sdk-rs/blob/v0.6.0/stylus-sdk/src/evm.rs#L38-L52
let buffer = read_bytes(data, len);
let encoded_event: Vec<u8> =
buffer.clone().into_iter().skip(topics * WORD_BYTES).collect();
Copy link
Member

@qalisander qalisander Dec 10, 2024

Choose a reason for hiding this comment

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

If you would have Event struct (or EncodedEvent), you could use here conversion to it from buffer

///
/// The key is the name of the test thread,
/// and the value is the context of the test case.
static EVM: Lazy<DashMap<ThreadName, TestCase>> = Lazy::new(DashMap::new);
Copy link
Member

Choose a reason for hiding this comment

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

We can call it MOCK_EVM. But yeah, mentioning EVM in the name of the test storage seems quite reasonable to me!

Copy link
Member

Choose a reason for hiding this comment

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

Since we are trying to mimic its behavior for test cases

@@ -204,6 +206,10 @@ mod tests {
let res = contract.pause();
assert!(res.is_ok());
assert_eq!(contract.paused(), true);
let expected_event = Paused { account: msg::sender() };
assert!(motsu::emits_event(expected_event.clone()));
Copy link
Member

Choose a reason for hiding this comment

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

How about changing logic for motsu::emits_event, that it would take an event from collection in case of match. No need in calling motsu::clear_events() after.

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.

[Feature]: Allow checking for events in unit tests
2 participants