Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ alloy-rpc-types = { workspace = true }
assert_matches = { workspace = true }
async-trait = { workspace = true }
candid_parser = { workspace = true }
derive_more = { workspace = true, features = ["from", "into"] }
evm_rpc_client = { path = "evm_rpc_client", features = ["alloy"] }
ic-crypto-test-utils-reproducible-rng = { git = "https://github.com/dfinity/ic", rev = "release-2024-09-26_01-31-base" }
ic-error-types = { workspace = true }
Expand Down
9 changes: 3 additions & 6 deletions src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
types::{ApiKey, Metrics, OverrideProvider, ProviderId, StorableLogFilter},
};
use candid::Principal;
use canhttp::http::json::Id;
use canhttp::http::json::{ConstantSizeId, Id};
use canhttp::multi::Timestamp;
use canlog::LogFilter;
use ic_stable_structures::memory_manager::VirtualMemory;
Expand All @@ -27,7 +27,7 @@ type StableMemory = VirtualMemory<DefaultMemoryImpl>;
thread_local! {
// Unstable static data: these are reset when the canister is upgraded.
pub static UNSTABLE_METRICS: RefCell<Metrics> = RefCell::new(Metrics::default());
static UNSTABLE_HTTP_REQUEST_COUNTER: RefCell<u64> = const {RefCell::new(0)};
static UNSTABLE_HTTP_REQUEST_COUNTER: RefCell<ConstantSizeId> = const {RefCell::new(ConstantSizeId::ZERO)};
static UNSTABLE_RPC_SERVICE_OK_RESULTS_TIMESTAMPS: RefCell<SupportedRpcServiceUsage> = RefCell::new(SupportedRpcServiceUsage::default());

// Stable static data: these are preserved when the canister is upgraded.
Expand Down Expand Up @@ -113,10 +113,7 @@ pub fn set_override_provider(provider: OverrideProvider) {

pub fn next_request_id() -> Id {
UNSTABLE_HTTP_REQUEST_COUNTER.with_borrow_mut(|counter| {
let current_request_id = *counter;
// overflow is not an issue here because we only use `next_request_id` to correlate
// requests and responses in logs.
*counter = counter.wrapping_add(1);
let current_request_id = counter.get_and_increment();
Id::from(current_request_id)
})
}
Expand Down
21 changes: 16 additions & 5 deletions tests/mock_http_runtime/mock/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
mod tests;

use crate::mock_http_runtime::mock::CanisterHttpRequestMatcher;
use canhttp::http::json::{Id, JsonRpcRequest};
use canhttp::http::json::{ConstantSizeId, Id, JsonRpcRequest};
use derive_more::{From, Into};
use pocket_ic::common::rest::{
CanisterHttpHeader, CanisterHttpMethod, CanisterHttpReply, CanisterHttpRequest,
CanisterHttpResponse,
Expand All @@ -11,6 +12,15 @@ use serde_json::{json, Value};
use std::{collections::BTreeSet, str::FromStr};
use url::{Host, Url};

#[derive(From, Into)]
pub struct JsonRpcId(Id);
Comment thread
gregorydemay marked this conversation as resolved.
Outdated

impl From<u64> for JsonRpcId {
fn from(id: u64) -> Self {
Self(Id::from(ConstantSizeId::from(id)))
}
}

#[derive(Clone, Debug)]
pub struct JsonRpcRequestMatcher {
pub method: String,
Expand All @@ -35,9 +45,9 @@ impl JsonRpcRequestMatcher {
}
}

pub fn with_id(self, id: impl Into<Id>) -> Self {
pub fn with_id(self, id: impl Into<JsonRpcId>) -> Self {
Self {
id: Some(id.into()),
id: Some(Id::from(id.into())),
..self
}
}
Expand Down Expand Up @@ -164,8 +174,9 @@ impl From<Value> for JsonRpcResponse {
}

impl JsonRpcResponse {
pub fn with_id(mut self, id: impl Into<Id>) -> JsonRpcResponse {
self.body["id"] = serde_json::to_value(id.into()).expect("BUG: cannot serialize ID");
pub fn with_id(mut self, id: impl Into<JsonRpcId>) -> JsonRpcResponse {
self.body["id"] =
serde_json::to_value(Id::from(id.into())).expect("BUG: cannot serialize ID");
self
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ async fn should_not_modify_json_rpc_request_from_request_endpoint() {
let mock_request = r#"{"id":123,"jsonrpc":"2.0","method":"eth_gasPrice"}"#;
let mock_response = r#"{"jsonrpc":"2.0","id":123,"result":"0x00112233"}"#;
let mocks = MockHttpOutcallsBuilder::new()
.given(JsonRpcRequestMatcher::with_method("eth_gasPrice").with_id(123_u64))
.given(JsonRpcRequestMatcher::with_method("eth_gasPrice").with_id(Id::Number(123)))
.respond_with(JsonRpcResponse::from(mock_response));

let setup = EvmRpcSetup::new().await.mock_api_keys().await;
Expand Down
Loading