Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
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
18 changes: 13 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,7 @@
mod tests;

use crate::mock_http_runtime::mock::CanisterHttpRequestMatcher;
use canhttp::http::json::{Id, JsonRpcRequest};
use canhttp::http::json::{ConstantSizeId, Id, JsonRpcRequest};
use pocket_ic::common::rest::{
CanisterHttpHeader, CanisterHttpMethod, CanisterHttpReply, CanisterHttpRequest,
CanisterHttpResponse,
Expand Down Expand Up @@ -35,9 +35,13 @@ impl JsonRpcRequestMatcher {
}
}

pub fn with_id(self, id: impl Into<Id>) -> Self {
pub fn with_id(self, id: u64) -> Self {
self.with_raw_id(Id::from(ConstantSizeId::from(id)))
}

pub fn with_raw_id(self, id: Id) -> Self {
Self {
id: Some(id.into()),
id: Some(id),
..self
}
}
Expand Down Expand Up @@ -164,8 +168,12 @@ 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(self, id: u64) -> JsonRpcResponse {
self.with_raw_id(Id::from(ConstantSizeId::from(id)))
}

pub fn with_raw_id(mut self, id: Id) -> JsonRpcResponse {
self.body["id"] = serde_json::to_value(id).expect("BUG: cannot serialize ID");
self
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/mock_http_runtime/mock/json/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::mock_http_runtime::mock::{json::JsonRpcRequestMatcher, CanisterHttpRequestMatcher};
use candid::Principal;
use canhttp::http::json::Id;
use canhttp::http::json::{ConstantSizeId, Id};
use evm_rpc::constants::{CONTENT_TYPE_HEADER_LOWERCASE, CONTENT_TYPE_VALUE};
use pocket_ic::common::rest::{CanisterHttpHeader, CanisterHttpMethod, CanisterHttpRequest};
use serde_json::{json, Value};
Expand Down Expand Up @@ -32,7 +32,7 @@ mod json_rpc_request_matcher_tests {

#[test]
fn should_not_match_wrong_id() {
assert!(!request_matcher().with_id(Id::Null).matches(&request()));
assert!(!request_matcher().with_raw_id(Id::Null).matches(&request()));
}

#[test]
Expand Down Expand Up @@ -128,7 +128,7 @@ fn request() -> CanisterHttpRequest {
body: serde_json::to_vec(&json!({
"jsonrpc": "2.0",
"method": DEFAULT_RPC_METHOD,
"id": DEFAULT_RPC_ID,
"id": ConstantSizeId::from(DEFAULT_RPC_ID).to_string(),
"params": DEFAULT_RPC_PARAMS,
}))
.unwrap(),
Expand Down
Loading