Skip to content

Commit

Permalink
refactor to improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc committed May 9, 2024
1 parent 4255da9 commit c1d6889
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/middlewares/methods/inject_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,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 @@ -121,37 +122,43 @@ impl Middleware<CallRequest, CallResult> for InjectParamsMiddleware {
};

let idx = self.get_index();
match request.params.len() {
len if len > idx + 1 => {
// unexpected number of params
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 => {
// without current block
let params_passed = request.params.len();
while request.params.len() < idx {
let current = request.params.len();
if self.params[current].optional {
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,
params_passed
len
)));
}
}

// Set param to null, it will be replaced later
request.params.push(JsonValue::Null);
}
_ => {} // full params, block potentially might be null
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] == JsonValue::Null {
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;
Expand Down
53 changes: 53 additions & 0 deletions validate_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
extensions:
client:
endpoints:
- wss://acala-rpc.dwellir.com
- wss://acala-rpc.aca-api.network
- wss://acala-rpc.aca-staging.network
- wss://acala-rpc-node-2.aca-api.network
- wss://acala-internal-rpc.aca-api.network:8443
health_check:
interval_sec: 30 # check interval, default is 10s
healthy_response_time_ms: 500 # max response time to be considered healthy, default is 500ms
health_method: system_health
response: # response contains { isSyncing: false }
!contains
- - isSyncing
- !eq false
event_bus:
substrate_api:
stale_timeout_seconds: 180 # rotate endpoint if no new blocks for 3 minutes
telemetry:
provider: none
cache:
default_ttl_seconds: 60
default_size: 500
merge_subscription:
keep_alive_seconds: 60
server:
port: 9944
listen_address: '0.0.0.0'
max_connections: 2000
http_methods:
- path: /health
method: system_health
- path: /liveness
method: chain_getBlockHash
cors: all
validator:
ignore_methods:
- system_health
- system_name
- system_version
- author_pendingExtrinsics

middlewares:
methods:
- inject_params
- validate
- upstream
subscriptions:
- merge_subscription
- upstream

rpcs: substrate

0 comments on commit c1d6889

Please sign in to comment.