Skip to content

Commit

Permalink
RPC456 temporarely removing the json validation and instead adding a …
Browse files Browse the repository at this point in the history
…counter to see how many json failures do we have
  • Loading branch information
dmitriy-helius committed Aug 26, 2024
1 parent 8479ef4 commit c1cb45c
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 33 deletions.
38 changes: 19 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ cadence = "0.29.0"
cadence-macros = "0.29.0"
dashmap = "5.5.3"
queues = "1.1.0"
jsonrpsee = { version = "0.20.1", features = [
jsonrpsee = { version = "0.22.5", features = [
"server",
"http-client",
"macros",
Expand All @@ -38,4 +38,4 @@ yellowstone-grpc-geyser = { git = "https://github.com/helius-labs/yellowstone-gr
rand = "0.8.5"
futures = "0.3.24"
figment = { version = "0.10.6", features = ["env", "test"] }
tower = { version = "0.4.13", features = ["full"] }
tower = { version = "0.4.13", features = ["full"] }
8 changes: 6 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ use cadence::{BufferedUdpMetricSink, QueuingMetricSink, StatsdClient};
use cadence_macros::set_global_default;
use figment::{providers::Env, Figment};
use grpc_geyser::GrpcGeyserImpl;
use jsonrpsee::server::{middleware::ProxyGetRequestLayer, ServerBuilder};
use jsonrpsee::server::{RpcServiceBuilder, ServerBuilder};
use jsonrpsee::server::middleware::http::ProxyGetRequestLayer;
use priority_fee::PriorityFeeTracker;
use rpc_server::AtlasPriorityFeeEstimator;
use serde::Deserialize;
use tracing::{error, info};

mod errors;
mod grpc_consumer;
mod grpc_geyser;
mod priority_fee;
mod rpc_server;
mod slot_cache;
mod solana;
mod temp_validator;

#[derive(Debug, Deserialize, Clone)]
struct EstimatorEnv {
Expand Down Expand Up @@ -50,7 +53,8 @@ async fn main() {

let port = env.port.unwrap_or(4141);
let server = ServerBuilder::default()
.set_middleware(
.set_rpc_middleware(RpcServiceBuilder::new().layer(temp_validator::RpcValidatorLayer::new()))
.set_http_middleware(
tower::ServiceBuilder::new()
// Proxy `GET /health` requests to internal `health` method.
.layer(
Expand Down
22 changes: 12 additions & 10 deletions src/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ impl fmt::Debug for AtlasPriorityFeeEstimator {
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
#[serde(
rename_all(serialize = "camelCase", deserialize = "camelCase"),
deny_unknown_fields
)]
// TODO: DKH - add deny_unknown_fields
pub struct GetPriorityFeeEstimateRequest {
pub transaction: Option<String>, // estimate fee for a txn
pub account_keys: Option<Vec<String>>, // estimate fee for a list of accounts
Expand All @@ -55,8 +55,8 @@ pub struct GetPriorityFeeEstimateRequest {
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
#[serde(
rename_all(serialize = "camelCase", deserialize = "camelCase"),
deny_unknown_fields
)]
// TODO: DKH - add deny_unknown_fields
pub struct GetPriorityFeeEstimateOptions {
// controls input txn encoding
pub transaction_encoding: Option<UiTransactionEncoding>,
Expand Down Expand Up @@ -399,7 +399,6 @@ mod tests {
use cadence::{NopMetricSink, StatsdClient};
use jsonrpsee::core::Cow;
use jsonrpsee::core::__reexports::serde_json;
use jsonrpsee::core::__reexports::serde_json::value::RawValue;
use jsonrpsee::types::{Id, Request, TwoPointZero};
use solana_sdk::clock::Slot;
use solana_sdk::pubkey::Pubkey;
Expand Down Expand Up @@ -517,18 +516,21 @@ mod tests {
assert_eq!(resp.priority_fee_estimate, Some(10500.0));
}

#[test]
// #[test]
// TODO: DKH - add the test back after we readd the validation
fn test_parsing_wrong_fields() {

Check warning on line 521 in src/rpc_server.rs

View workflow job for this annotation

GitHub Actions / fee-estimator-tests

function `test_parsing_wrong_fields` is never used
for (param, error) in bad_params() {
let json_val = format!("{{\"jsonrpc\": \"2.0\",\"id\": \"1\", \"method\": \"getPriorityFeeEstimate\", \"params\": {param} }}");
let res = serde_json::from_str::<jsonrpsee::types::Request>(json_val.as_str());
let json_val = format!("{{\"jsonrpc\": \"2.0\",\"id\": \"1\", \"method\": \"getPriorityFeeEstimate\", \"params\": [{param}] }}");
let res = serde_json::from_str::<Request>(json_val.as_str());
let res = res.unwrap();
assert_request(&res, Id::Str(Cow::const_str("1")), "getPriorityFeeEstimate");

let params: serde_json::error::Result<GetPriorityFeeEstimateRequest> =
serde_json::from_str(res.params.map(RawValue::get).unwrap());
assert!(params.is_err());
assert_eq!(params.err().unwrap().to_string(), error, "testing {param}");
if let Some(val) = res.params
{
let params: Result<Vec<GetPriorityFeeEstimateRequest>, _> = serde_json::from_str(val.get());
assert!(params.is_err());
assert_eq!(params.err().unwrap().to_string(), error, "testing {param}");
}
}
}

Expand Down
80 changes: 80 additions & 0 deletions src/temp_validator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use cadence_macros::statsd_count;
use crate::priority_fee::PriorityLevel;
use jsonrpsee::core::__reexports::serde_json;
use jsonrpsee::server::middleware::rpc::RpcServiceT;
use jsonrpsee::types::Request;
use serde::{Deserialize, Serialize};
use solana_transaction_status::UiTransactionEncoding;
use tracing::debug;

#[derive(Serialize, Deserialize, Clone, Debug, Default)]
#[serde(
rename_all(serialize = "camelCase", deserialize = "camelCase"),
deny_unknown_fields
)]
struct GetPriorityFeeEstimateOptionsFake {
// controls input txn encoding
pub transaction_encoding: Option<UiTransactionEncoding>,
// controls custom priority fee level response
pub priority_level: Option<PriorityLevel>, // Default to MEDIUM
pub include_all_priority_fee_levels: Option<bool>, // Include all priority level estimates in the response
#[serde()]
pub lookback_slots: Option<u32>, // how many slots to look back on, default 50, min 1, max 300
pub include_vote: Option<bool>, // include vote txns in the estimate
// returns recommended fee, incompatible with custom controls. Currently the recommended fee is the median fee excluding vote txns
pub recommended: Option<bool>, // return the recommended fee (median fee excluding vote txns)
}

#[derive(Serialize, Deserialize, Clone, Debug, Default)]
#[serde(
rename_all(serialize = "camelCase", deserialize = "camelCase"),
deny_unknown_fields
)]
struct GetPriorityFeeEstimateRequestFake {
transaction: Option<String>, // estimate fee for a txn
account_keys: Option<Vec<String>>, // estimate fee for a list of accounts
options: Option<GetPriorityFeeEstimateOptionsFake>,
}

/// RPC logger layer.
#[derive(Copy, Clone, Debug)]
pub struct RpcValidatorLayer;

impl RpcValidatorLayer {
/// Create a new logging layer.
pub fn new() -> Self {
Self
}
}

impl<S> tower::Layer<S> for RpcValidatorLayer {
type Service = RpcValidator<S>;

fn layer(&self, service: S) -> Self::Service {
RpcValidator { service }
}
}

/// A middleware that logs each RPC call and response.
#[derive(Debug)]
pub struct RpcValidator<S> {
service: S,
}

impl<'a, S> RpcServiceT<'a> for RpcValidator<S>
where
S: RpcServiceT<'a> + Send + Sync,
{
type Future = S::Future;

fn call(&self, req: Request<'a>) -> Self::Future {
if let Some(params) = &req.params {
if let Err(err_val) = serde_json::from_str::<Vec<GetPriorityFeeEstimateRequestFake>>(params.get()) {
statsd_count!("rpc_payload_parse_failed", 1);
debug!("RPC parse error: {}, {}", err_val, params);
}
}

self.service.call(req)
}
}

0 comments on commit c1cb45c

Please sign in to comment.