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!: adjust Candid-RPC methods for state machine tests #95

Merged
merged 37 commits into from
Dec 12, 2023

Conversation

rvanasa
Copy link
Collaborator

@rvanasa rvanasa commented Dec 7, 2023

This PR adds initial state machine tests for the Candid-RPC endpoints.

Progress:

  • Adjust EvmRpcSetup implementation based on feedback in previous code reviews
  • Test unexpected fields in JSON-RPC responses
  • Add CandidType trait implementations in ckETH codebase
  • Implement "success cases" for each Candid-RPC method
  • Implement "fail cases" for Candid-RPC methods

Note that this PR also switches the canister to use the original JSON-RPC camelCase naming conventions, which ended up being necessary due to limitations with the Candid Rust implementation. This has the side benefit of being more consistent with the large amount of existing documentation on JSON-RPC methods across the Internet.

Resolves #91.

Future work:

@rvanasa rvanasa marked this pull request as ready for review December 12, 2023 01:12
#[ic_cdk_macros::update]
#[candid_method]
#[update(name = "eth_getLogs")]
#[candid_method(rename = "eth_getLogs")]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Question for anyone more familiar with the Rust CDK: is there a way to avoid all these duplicate attributes (aside from renaming the function itself)?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know the answer. What happens if you only use rename?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Only using one or the other results in the generated Candid interface having a different name from the canister method.

@rvanasa rvanasa changed the title test(state-machine): Candid-RPC method success cases feat!: adjust Candid-RPC methods for state machine tests Dec 12, 2023
@@ -199,11 +200,15 @@ impl BoundedStorable for PrincipalStorable {

#[derive(Clone, Debug, Eq, PartialEq, CandidType, Deserialize)]
pub struct ProviderView {
#[serde(rename = "providerId")]
Copy link
Collaborator Author

@rvanasa rvanasa Dec 12, 2023

Choose a reason for hiding this comment

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

Candid unfortunately doesn't support #[serde(rename_all = ...)], so these individual attributes are necessary. Leaving out the attributes results in an encoding / decoding mismatch, so this is required for supporting both JSON and Candid (de)serialization from the same data structure.

)
cycles: u128,
) -> RpcResult<HttpResponse> {
if !is_authorized(&ic_cdk::caller(), Auth::FreeRpc) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The cycles payment logic was originally in the forked ckETH codebase; I moved it here to support the FreeRpc authorization for Candid-RPC methods. Everything else works the same as before.

}
.into());
}
ic_cdk::api::call::msg_cycles_accept128(cycles);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit confused about the logic here: Shouldn't the attached cycles match a predetermined amount required to make the outcall? What is the point of passing along a cycles parameter that may or may not be sufficient (nor necessarily the same as the number of attached cycles)?

Why is any amount greater than cycles allowed?

Copy link
Collaborator Author

@rvanasa rvanasa Dec 12, 2023

Choose a reason for hiding this comment

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

cycles (now renamed to cycles_cost for clarity) is the cost calculated within the ckETH codebase based on the HTTPS outcall. msg_cycles_accept128() only consumes this cost, so you can send more cycles than necessary without being overcharged.

This will eventually be replaced with the get_request_cost() function in this repository, but it's quite a large refactor so I'm making incremental steps towards that.

}

#[test]
fn eth_send_raw_transaction_should_succeed() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it make sense to add a few tests for the unhappy path? For example, sending an invalid transaction should result in an error, right?

Copy link
Collaborator Author

@rvanasa rvanasa Dec 12, 2023

Choose a reason for hiding this comment

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

I was planning to add these separately, but I'll include them here for efficiency given the review time zone offset.

Update: added several unhappy path / "fail case" tests.

@rvanasa rvanasa merged commit 9f86dbe into main Dec 12, 2023
3 checks passed
@rvanasa rvanasa deleted the test-unexpected-response-fields branch December 12, 2023 19:30
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.

Test unexpected JSON fields in Candid-RPC methods
2 participants