-
Notifications
You must be signed in to change notification settings - Fork 24
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
base: main
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for contracts-stylus ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
There was a problem hiding this 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>>, |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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())); |
There was a problem hiding this comment.
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.
Resolves #307
PR Checklist