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

refactor: convert payload_size_bytes from usize to u64 #117

Merged
merged 1 commit into from
Jan 5, 2024
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
19 changes: 10 additions & 9 deletions src/accounting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::*;
/// Returns the cycles cost of a JSON-RPC request.
pub fn get_json_rpc_cost(
source: &ResolvedJsonRpcSource,
payload_size_bytes: usize,
payload_size_bytes: u64,
max_response_bytes: u64,
) -> u128 {
match source {
Expand All @@ -22,7 +22,7 @@ pub fn get_json_rpc_cost(
/// Returns the cycles cost of a Candid-RPC request.
pub fn get_candid_rpc_cost(
provider: &Provider,
payload_size_bytes: usize,
payload_size_bytes: u64,
effective_response_size_estimate: u64,
) -> u128 {
let base_cost = 400_000_000u128 + 100_000u128 * (2 * effective_response_size_estimate as u128);
Expand All @@ -35,11 +35,11 @@ pub fn get_candid_rpc_cost(
/// Calculates the baseline cost of sending a JSON-RPC request using HTTP outcalls.
pub fn get_http_request_cost(
api: &RpcApi,
payload_size_bytes: usize,
payload_size_bytes: u64,
max_response_bytes: u64,
) -> u128 {
let nodes_in_subnet = UNSTABLE_SUBNET_SIZE.with(|n| *n.borrow());
let ingress_bytes = (payload_size_bytes + api.url.len()) as u128 + INGRESS_OVERHEAD_BYTES;
let ingress_bytes = payload_size_bytes as u128 + api.url.len() as u128 + INGRESS_OVERHEAD_BYTES;
let base_cost = INGRESS_MESSAGE_RECEIVED_COST
+ INGRESS_MESSAGE_BYTE_RECEIVED_COST * ingress_bytes
+ HTTP_OUTCALL_REQUEST_COST
Expand All @@ -48,7 +48,7 @@ pub fn get_http_request_cost(
}

/// Calculate the additional cost for calling a registered JSON-RPC provider.
pub fn get_provider_cost(provider: &Provider, payload_size_bytes: usize) -> u128 {
pub fn get_provider_cost(provider: &Provider, payload_size_bytes: u64) -> u128 {
let nodes_in_subnet = UNSTABLE_SUBNET_SIZE.with(|m| *m.borrow());
let cost_per_node = provider.cycles_per_call as u128
+ provider.cycles_per_message_byte as u128 * payload_size_bytes as u128;
Expand All @@ -69,15 +69,15 @@ fn test_request_cost() {
url: url.to_string(),
headers: vec![],
}),
payload.len(),
payload.len() as u64,
1000,
);
let base_cost_10_extra_bytes = get_json_rpc_cost(
&ResolvedJsonRpcSource::Api(RpcApi {
url: url.to_string(),
headers: vec![],
}),
payload.len() + 10,
payload.len() as u64 + 10,
1000,
);
let estimated_cost_10_extra_bytes = base_cost
Expand Down Expand Up @@ -113,7 +113,7 @@ fn test_provider_cost() {
};
let base_cost = get_provider_cost(
&provider,
"{\"jsonrpc\":\"2.0\",\"method\":\"eth_gasPrice\",\"params\":[],\"id\":1}".len(),
"{\"jsonrpc\":\"2.0\",\"method\":\"eth_gasPrice\",\"params\":[],\"id\":1}".len() as u64,
);

let provider_10_extra_bytes = Provider {
Expand All @@ -130,7 +130,8 @@ fn test_provider_cost() {
};
let base_cost_10_extra_bytes = get_provider_cost(
&provider_10_extra_bytes,
"{\"jsonrpc\":\"2.0\",\"method\":\"eth_gasPrice\",\"params\":[],\"id\":1}".len() + 10,
"{\"jsonrpc\":\"2.0\",\"method\":\"eth_gasPrice\",\"params\":[],\"id\":1}".len() as u64
+ 10,
);
assert_eq!(
base_cost + (10 * 2 + 1000) * nodes_in_subnet as u128,
Expand Down
2 changes: 1 addition & 1 deletion src/candid_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl RpcTransport for CanisterTransport {
request
.body
.as_ref()
.map(|bytes| bytes.len())
.map(|bytes| bytes.len() as u64)
.unwrap_or_default(),
effective_response_size_estimate,
);
Expand Down
4 changes: 2 additions & 2 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub async fn do_json_rpc_request(
add_metric!(err_no_permission, 1);
return Err(ProviderError::NoPermission.into());
}
let cycles_cost = get_json_rpc_cost(&source, json_rpc_payload.len(), max_response_bytes);
let cycles_cost = get_json_rpc_cost(&source, json_rpc_payload.len() as u64, max_response_bytes);
let (api, provider) = match source {
ResolvedJsonRpcSource::Api(api) => (api, None),
ResolvedJsonRpcSource::Provider(provider) => (provider.api(), Some(provider)),
Expand Down Expand Up @@ -81,7 +81,7 @@ pub async fn do_http_request_with_metrics(
request
.body
.as_ref()
.map(|bytes| bytes.len())
.map(|bytes| bytes.len() as u64)
.unwrap_or_default(),
);
PROVIDERS.with(|p| {
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ fn request_cost(
) -> Result<u128, RpcError> {
Ok(get_json_rpc_cost(
&source.resolve().unwrap(),
json_rpc_payload.len(),
json_rpc_payload.len() as u64,
max_response_bytes,
))
}
Expand Down