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

Add a Powerful Test VM #212

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Add a Powerful Test VM #212

wants to merge 2 commits into from

Conversation

rauljordan
Copy link
Contributor

Description

This PR introduces a powerful test VM that implements the Host trait for the Stylus SDK. It supports mocking every hostio, and allows for mocking of calls, deployments, and checking log emissions. Moreover, it supports Foundry-style storage forking with a simple builder API:

let test_vm = TestVMBuilder::new()
            .contract_address(address!("2460d3db27c4bef88557e8dc9136e6fad189e8c3"))
            .rpc_url("https://sepolia-rollup.arbitrum.io/rpc") // Storage loads will fetch state from the RPC endpoint.
            .build()?

Calls can also be mocked:

#[test]
fn test_mock_calls() {
    let vm = TestVM::new();
    let target = Address::from([2u8; 20]);
    let data = vec![1, 2, 3, 4];
    let expected_return = vec![5, 6, 7, 8];

    // Mock a regular call.
    vm.mock_call(target, data.clone(), Ok(expected_return.clone()));

    let ctx = stylus_core::calls::context::Call::new();
    let result = vm.call(&ctx, target, &data).unwrap();
    assert_eq!(result, expected_return);

    // Mock an error case.
    let error_data = vec![9, 9, 9];
    vm.mock_call(target, data.clone(), Err(error_data.clone()));

    match vm.call(&ctx, target, &data) {
        Err(Error::Revert(returned_data)) => assert_eq!(returned_data, error_data),
        _ => panic!("Expected revert error"),
    }
}

Log emissions can also be inspected:

#[test]
fn test_logs() {
    let vm = TestVM::new();
    let topic1 = B256::from([1u8; 32]);
    let topic2 = B256::from([2u8; 32]);
    let data = vec![3, 4, 5];

    vm.raw_log(&[topic1, topic2], &data).unwrap();

    let logs = vm.get_emitted_logs();
    assert_eq!(logs.len(), 1);
    assert_eq!(logs[0].0, vec![topic1, topic2]);
    assert_eq!(logs[0].1, data);
}

Base automatically changed from add-misc-host-support to main January 29, 2025 20:58
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.

1 participant