Skip to content

Commit c1cb45c

Browse files
RPC456 temporarely removing the json validation and instead adding a counter to see how many json failures do we have
1 parent 8479ef4 commit c1cb45c

File tree

5 files changed

+119
-33
lines changed

5 files changed

+119
-33
lines changed

Cargo.lock

Lines changed: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ cadence = "0.29.0"
1717
cadence-macros = "0.29.0"
1818
dashmap = "5.5.3"
1919
queues = "1.1.0"
20-
jsonrpsee = { version = "0.20.1", features = [
20+
jsonrpsee = { version = "0.22.5", features = [
2121
"server",
2222
"http-client",
2323
"macros",
@@ -38,4 +38,4 @@ yellowstone-grpc-geyser = { git = "https://github.com/helius-labs/yellowstone-gr
3838
rand = "0.8.5"
3939
futures = "0.3.24"
4040
figment = { version = "0.10.6", features = ["env", "test"] }
41-
tower = { version = "0.4.13", features = ["full"] }
41+
tower = { version = "0.4.13", features = ["full"] }

src/main.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@ use cadence::{BufferedUdpMetricSink, QueuingMetricSink, StatsdClient};
55
use cadence_macros::set_global_default;
66
use figment::{providers::Env, Figment};
77
use grpc_geyser::GrpcGeyserImpl;
8-
use jsonrpsee::server::{middleware::ProxyGetRequestLayer, ServerBuilder};
8+
use jsonrpsee::server::{RpcServiceBuilder, ServerBuilder};
9+
use jsonrpsee::server::middleware::http::ProxyGetRequestLayer;
910
use priority_fee::PriorityFeeTracker;
1011
use rpc_server::AtlasPriorityFeeEstimator;
1112
use serde::Deserialize;
1213
use tracing::{error, info};
14+
1315
mod errors;
1416
mod grpc_consumer;
1517
mod grpc_geyser;
1618
mod priority_fee;
1719
mod rpc_server;
1820
mod slot_cache;
1921
mod solana;
22+
mod temp_validator;
2023

2124
#[derive(Debug, Deserialize, Clone)]
2225
struct EstimatorEnv {
@@ -50,7 +53,8 @@ async fn main() {
5053

5154
let port = env.port.unwrap_or(4141);
5255
let server = ServerBuilder::default()
53-
.set_middleware(
56+
.set_rpc_middleware(RpcServiceBuilder::new().layer(temp_validator::RpcValidatorLayer::new()))
57+
.set_http_middleware(
5458
tower::ServiceBuilder::new()
5559
// Proxy `GET /health` requests to internal `health` method.
5660
.layer(

src/rpc_server.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ impl fmt::Debug for AtlasPriorityFeeEstimator {
4444
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
4545
#[serde(
4646
rename_all(serialize = "camelCase", deserialize = "camelCase"),
47-
deny_unknown_fields
4847
)]
48+
// TODO: DKH - add deny_unknown_fields
4949
pub struct GetPriorityFeeEstimateRequest {
5050
pub transaction: Option<String>, // estimate fee for a txn
5151
pub account_keys: Option<Vec<String>>, // estimate fee for a list of accounts
@@ -55,8 +55,8 @@ pub struct GetPriorityFeeEstimateRequest {
5555
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
5656
#[serde(
5757
rename_all(serialize = "camelCase", deserialize = "camelCase"),
58-
deny_unknown_fields
5958
)]
59+
// TODO: DKH - add deny_unknown_fields
6060
pub struct GetPriorityFeeEstimateOptions {
6161
// controls input txn encoding
6262
pub transaction_encoding: Option<UiTransactionEncoding>,
@@ -399,7 +399,6 @@ mod tests {
399399
use cadence::{NopMetricSink, StatsdClient};
400400
use jsonrpsee::core::Cow;
401401
use jsonrpsee::core::__reexports::serde_json;
402-
use jsonrpsee::core::__reexports::serde_json::value::RawValue;
403402
use jsonrpsee::types::{Id, Request, TwoPointZero};
404403
use solana_sdk::clock::Slot;
405404
use solana_sdk::pubkey::Pubkey;
@@ -517,18 +516,21 @@ mod tests {
517516
assert_eq!(resp.priority_fee_estimate, Some(10500.0));
518517
}
519518

520-
#[test]
519+
// #[test]
520+
// TODO: DKH - add the test back after we readd the validation
521521
fn test_parsing_wrong_fields() {
522522
for (param, error) in bad_params() {
523-
let json_val = format!("{{\"jsonrpc\": \"2.0\",\"id\": \"1\", \"method\": \"getPriorityFeeEstimate\", \"params\": {param} }}");
524-
let res = serde_json::from_str::<jsonrpsee::types::Request>(json_val.as_str());
523+
let json_val = format!("{{\"jsonrpc\": \"2.0\",\"id\": \"1\", \"method\": \"getPriorityFeeEstimate\", \"params\": [{param}] }}");
524+
let res = serde_json::from_str::<Request>(json_val.as_str());
525525
let res = res.unwrap();
526526
assert_request(&res, Id::Str(Cow::const_str("1")), "getPriorityFeeEstimate");
527527

528-
let params: serde_json::error::Result<GetPriorityFeeEstimateRequest> =
529-
serde_json::from_str(res.params.map(RawValue::get).unwrap());
530-
assert!(params.is_err());
531-
assert_eq!(params.err().unwrap().to_string(), error, "testing {param}");
528+
if let Some(val) = res.params
529+
{
530+
let params: Result<Vec<GetPriorityFeeEstimateRequest>, _> = serde_json::from_str(val.get());
531+
assert!(params.is_err());
532+
assert_eq!(params.err().unwrap().to_string(), error, "testing {param}");
533+
}
532534
}
533535
}
534536

src/temp_validator.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use cadence_macros::statsd_count;
2+
use crate::priority_fee::PriorityLevel;
3+
use jsonrpsee::core::__reexports::serde_json;
4+
use jsonrpsee::server::middleware::rpc::RpcServiceT;
5+
use jsonrpsee::types::Request;
6+
use serde::{Deserialize, Serialize};
7+
use solana_transaction_status::UiTransactionEncoding;
8+
use tracing::debug;
9+
10+
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
11+
#[serde(
12+
rename_all(serialize = "camelCase", deserialize = "camelCase"),
13+
deny_unknown_fields
14+
)]
15+
struct GetPriorityFeeEstimateOptionsFake {
16+
// controls input txn encoding
17+
pub transaction_encoding: Option<UiTransactionEncoding>,
18+
// controls custom priority fee level response
19+
pub priority_level: Option<PriorityLevel>, // Default to MEDIUM
20+
pub include_all_priority_fee_levels: Option<bool>, // Include all priority level estimates in the response
21+
#[serde()]
22+
pub lookback_slots: Option<u32>, // how many slots to look back on, default 50, min 1, max 300
23+
pub include_vote: Option<bool>, // include vote txns in the estimate
24+
// returns recommended fee, incompatible with custom controls. Currently the recommended fee is the median fee excluding vote txns
25+
pub recommended: Option<bool>, // return the recommended fee (median fee excluding vote txns)
26+
}
27+
28+
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
29+
#[serde(
30+
rename_all(serialize = "camelCase", deserialize = "camelCase"),
31+
deny_unknown_fields
32+
)]
33+
struct GetPriorityFeeEstimateRequestFake {
34+
transaction: Option<String>, // estimate fee for a txn
35+
account_keys: Option<Vec<String>>, // estimate fee for a list of accounts
36+
options: Option<GetPriorityFeeEstimateOptionsFake>,
37+
}
38+
39+
/// RPC logger layer.
40+
#[derive(Copy, Clone, Debug)]
41+
pub struct RpcValidatorLayer;
42+
43+
impl RpcValidatorLayer {
44+
/// Create a new logging layer.
45+
pub fn new() -> Self {
46+
Self
47+
}
48+
}
49+
50+
impl<S> tower::Layer<S> for RpcValidatorLayer {
51+
type Service = RpcValidator<S>;
52+
53+
fn layer(&self, service: S) -> Self::Service {
54+
RpcValidator { service }
55+
}
56+
}
57+
58+
/// A middleware that logs each RPC call and response.
59+
#[derive(Debug)]
60+
pub struct RpcValidator<S> {
61+
service: S,
62+
}
63+
64+
impl<'a, S> RpcServiceT<'a> for RpcValidator<S>
65+
where
66+
S: RpcServiceT<'a> + Send + Sync,
67+
{
68+
type Future = S::Future;
69+
70+
fn call(&self, req: Request<'a>) -> Self::Future {
71+
if let Some(params) = &req.params {
72+
if let Err(err_val) = serde_json::from_str::<Vec<GetPriorityFeeEstimateRequestFake>>(params.get()) {
73+
statsd_count!("rpc_payload_parse_failed", 1);
74+
debug!("RPC parse error: {}, {}", err_val, params);
75+
}
76+
}
77+
78+
self.service.call(req)
79+
}
80+
}

0 commit comments

Comments
 (0)