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

Sync fork #25

Merged
merged 2 commits into from
May 9, 2024
Merged
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
107 changes: 77 additions & 30 deletions src/middlewares/methods/inject_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ impl Middleware<CallRequest, CallResult> for InjectParamsMiddleware {
if param.ty == "BlockNumber" {
if let Some(number) = request.params.get(idx).and_then(|x| x.as_u64()) {
let (_, finalized) = self.finalized.read().await;
// avoid cache unfinalized data
if number > finalized {
context.insert(BypassCache(true));
}
Expand All @@ -119,43 +120,51 @@ impl Middleware<CallRequest, CallResult> for InjectParamsMiddleware {
};

let idx = self.get_index();
match request.params.len() {
len if len == idx + 1 => {
// full params with current block
let min_len = idx + 1;
let len = request.params.len();

match len.cmp(&min_len) {
std::cmp::Ordering::Greater => {
// too many params, no injection needed
return handle_request(request).await;
}
len if len <= idx => {
async move {
// without current block
let to_inject = self.get_parameter().await;
tracing::trace!("Injected param {} to method {}", &to_inject, request.method);
let params_passed = request.params.len();
while request.params.len() < idx {
let current = request.params.len();
if self.params[current].optional {
request.params.push(JsonValue::Null);
} else {
let (required, optional) = self.params_count();
return Err(errors::invalid_params(format!(
"Expected {:?} parameters ({:?} optional), {:?} found instead",
required + optional,
optional,
params_passed
)));
}
std::cmp::Ordering::Less => {
// too few params

// ensure missing params are optional and push null
for i in len..(min_len - 1) {
if self.params[i].optional {
request.params.push(JsonValue::Null);
} else {
let (required, optional) = self.params_count();
return Err(errors::invalid_params(format!(
"Expected {:?} parameters ({:?} optional), {:?} found instead",
required + optional,
optional,
len
)));
}
request.params.push(to_inject);

handle_request(request).await
}
.with_context(TRACER.context("inject_params"))
.await

// Set param to null, it will be replaced later
request.params.push(JsonValue::Null);
}
_ => {
// unexpected number of params
handle_request(request).await
std::cmp::Ordering::Equal => {
// same number of params, no need to push params
}
};

// Here we are sure we have full params in the request, but it still might be set to null
async move {
if request.params[idx].is_null() {
let to_inject = self.get_parameter().await;
tracing::trace!("Injected param {} to method {}", &to_inject, request.method);
request.params[idx] = to_inject;
}
handle_request(request).await
}
.with_context(TRACER.context("inject_params"))
.await
}
}

Expand Down Expand Up @@ -284,6 +293,44 @@ mod tests {
assert_eq!(result, json!("0x1111"));
}

#[tokio::test]
async fn inject_if_param_is_null() {
let params = vec![json!("0x1234"), json!(None::<()>)];
let (middleware, _) = create_inject_middleware(
InjectType::BlockHashAt(1),
vec![
MethodParam {
name: "key".to_string(),
ty: "StorageKey".to_string(),
optional: false,
inject: false,
},
MethodParam {
name: "at".to_string(),
ty: "BlockHash".to_string(),
optional: true,
inject: true,
},
],
)
.await;
let result = middleware
.call(
CallRequest::new("state_getStorage", params.clone()),
Default::default(),
Box::new(move |req: CallRequest, _| {
async move {
assert_eq!(req.params, vec![json!("0x1234"), json!("0xabcd")]);
Ok(json!("0x1111"))
}
.boxed()
}),
)
.await
.unwrap();
assert_eq!(result, json!("0x1111"));
}

#[tokio::test]
async fn inject_if_without_current_block_hash() {
let (middleware, _context) = create_inject_middleware(
Expand Down
Loading