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

Validate Requests for a Response in MockBlockchainClientServer #543

Open
utkarshg6 opened this issue Oct 22, 2024 · 1 comment
Open

Validate Requests for a Response in MockBlockchainClientServer #543

utkarshg6 opened this issue Oct 22, 2024 · 1 comment

Comments

@utkarshg6
Copy link
Collaborator

utkarshg6 commented Oct 22, 2024

At first, we need to modify the below function inside MBCSBuilder:

pub fn response<R>(self, result: R) -> Self
where
    R: Serialize,
{
    let result = serde_json::to_string(&result).unwrap();
    let body = format!(
        r#"{{"jsonrpc": "2.0", "result": {}, "id": null}}"#,
        result
    );
    self.store_response_string(body)
}

This function accepts the response as the first argument.

The second argument is only useful in the websocket connections. Since we are not using any and probably won't be using them for a very long period of time, it's advisable to get rid of it.

Now, once you do that, make sure you introduce another argument, expected_request, which should accept a type of web3 call. For example, "eth_getLogs". (Refer to the Quicknode documentation) This argument could be a string as well as an Enum. It's better to create an enum because it'll make it easier to use, as the developer won't need to provide the accurate string everywhere, and adding a new call to the enum will be quick too.

This request should be stored the same way responses are stored and should be validated before sending a response. This validation should exist inside the function thread_guts() of MockBlockchainClientServer.

@utkarshg6 utkarshg6 converted this from a draft issue Oct 22, 2024
@kauri-hero kauri-hero moved this from 🆕 New to 🔖 Ready in MASQ Node v2 Dec 8, 2024
@utkarshg6
Copy link
Collaborator Author

utkarshg6 commented Jan 6, 2025

Here are some examples of legacy code that I'm sharing for historical reference:

  • For the test blockchain_interface_web3_retrieves_transactions

    let requests = test_server.requests_so_far();
    let bodies: Vec<String> = requests
        .into_iter()
        .map(|request| serde_json::from_slice(&request.body()).unwrap())
        .map(|b: Value| serde_json::to_string(&b).unwrap())
        .collect();
    let expected_body_prefix = r#"[{"id":0,"jsonrpc":"2.0","method":"eth_blockNumber","params":[]},{"id":1,"jsonrpc":"2.0","method":"eth_getLogs","params":[{"address":"0x384dec25e03f94931767ce4c3556168468ba24c3","fromBlock":"0x2a","toBlock":"0x400","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",null,"0x000000000000000000000000"#;
    let expected_body_suffix = r#""]}]}]"#;
    let expected_body = format!(
        "{}{}{}",
        expected_body_prefix,
        &to[2..],
        expected_body_suffix
    );
    assert_eq!(bodies, vec!(expected_body));
  • For the test blockchain_interface_web3_handles_no_retrieved_transactions

    let requests = test_server.requests_so_far();
    let bodies: Vec<String> = requests
        .into_iter()
        .map(|request| serde_json::from_slice(&request.body()).unwrap())
        .map(|b: Value| serde_json::to_string(&b).unwrap())
        .collect();
    let expected_body_prefix = r#"[{"id":0,"jsonrpc":"2.0","method":"eth_blockNumber","params":[]},{"id":1,"jsonrpc":"2.0","method":"eth_getLogs","params":[{"address":"0x384dec25e03f94931767ce4c3556168468ba24c3","fromBlock":"0x2a","toBlock":"0x400","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",null,"0x000000000000000000000000"#;
    let expected_body_suffix = r#""]}]}]"#;
    let expected_body = format!(
        "{}{}{}",
        expected_body_prefix,
        &to[2..],
        expected_body_suffix
    );
    assert_eq!(bodies, vec!(expected_body));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🔖 Ready
Development

No branches or pull requests

1 participant