Skip to content

Commit

Permalink
count everything as spam on rpc nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
williampsmith committed Jun 21, 2024
1 parent aeb24ff commit 72bab95
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 32 deletions.
9 changes: 1 addition & 8 deletions crates/sui-core/src/authority_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -984,14 +984,7 @@ fn normalize(err: SuiError) -> Weight {
macro_rules! handle_with_decoration {
($self:ident, $func_name:ident, $request:ident) => {{
if $self.client_id_source.is_none() {
return match $self.$func_name($request).await {
Ok((result, _)) => {
Ok(result)
}
Err(err) => {
Err(err)
}
}
return $self.$func_name($request).await.map(|(result, _)| result);
}

let client = match $self.client_id_source.as_ref().unwrap() {
Expand Down
12 changes: 2 additions & 10 deletions crates/sui-core/src/traffic_controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ async fn handle_error_tally(
metrics: Arc<TrafficControllerMetrics>,
mem_drainfile_present: bool,
) -> Result<(), reqwest::Error> {
if !tally.error_weight.is_sampled().await {
if !tally.error_weight.is_sampled() {
return Ok(());
}
let resp = policy.handle_tally(tally.clone());
Expand Down Expand Up @@ -330,15 +330,7 @@ async fn handle_spam_tally(
metrics: Arc<TrafficControllerMetrics>,
mem_drainfile_present: bool,
) -> Result<(), reqwest::Error> {
let sampled = futures::future::join_all(vec![
tally.spam_weight.is_sampled(),
policy_config.spam_sample_rate.is_sampled(),
])
.await
.into_iter()
.all(|sampled| sampled);

if !sampled {
if !(tally.spam_weight.is_sampled() && policy_config.spam_sample_rate.is_sampled()) {
return Ok(());
}
let resp = policy.handle_tally(tally.clone());
Expand Down
22 changes: 9 additions & 13 deletions crates/sui-json-rpc/src/axum_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,9 @@ async fn process_raw_request<L: Logger>(
}

// handle response tallying
let method = request.method.to_string();
let response = process_request(request, api_version, service.call_data()).await;
if let Some(traffic_controller) = &service.traffic_controller {
handle_traffic_resp(traffic_controller.clone(), client, &response, method);
handle_traffic_resp(traffic_controller.clone(), client, &response);
}

response
Expand Down Expand Up @@ -199,27 +198,24 @@ async fn handle_traffic_req(
}
}

fn spam_weight_for_method(method: String) -> Weight {
// unless request requires gas payment, count against
// spam tally
match method.as_str() {
"sui_executeTransactionBlock" | "sui_devInspectTransactionBlock" => Weight::zero(),
_ => Weight::one(),
}
}

fn handle_traffic_resp(
traffic_controller: Arc<TrafficController>,
client: Option<IpAddr>,
response: &MethodResponse,
method: String,
) {
let error = response.error_code.map(ErrorCode::from);
traffic_controller.tally(TrafficTally {
direct: client,
through_fullnode: None,
error_weight: error.map(normalize).unwrap_or(Weight::zero()),
spam_weight: spam_weight_for_method(method),
// For now, count everything as spam with equal weight
// on the rpc node side, including gas-charging endpoints
// such as `sui_executeTransactionBlock`, as this can enable
// node operators who wish to rate limit their transcation
// traffic and incentivize high volume clients to choose a
// suitable rpc provider (or run their own). Later we may want
// to provide a weight distribution based on the method being called.
spam_weight: Weight::one(),
timestamp: SystemTime::now(),
});
}
Expand Down
7 changes: 6 additions & 1 deletion crates/sui-types/src/traffic_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Weight {
self.0
}

pub async fn is_sampled(&self) -> bool {
pub fn is_sampled(&self) -> bool {
let mut rng = rand::thread_rng();
let sample = rand::distributions::Uniform::new(0.0, 1.0).sample(&mut rng);
sample <= self.value()
Expand Down Expand Up @@ -201,6 +201,11 @@ pub struct PolicyConfig {
#[serde(default = "default_channel_capacity")]
pub channel_capacity: usize,
#[serde(default = "default_spam_sample_rate")]
/// Note that this sample policy is applied on top of the
/// endpoint-specific sample policy (not configurable) which
/// weighs endpoints by the relative effort required to serve
/// them. Therefore a sample rate of N will yield an actual
/// sample rate <= N.
pub spam_sample_rate: Weight,
#[serde(default = "default_dry_run")]
pub dry_run: bool,
Expand Down

0 comments on commit 72bab95

Please sign in to comment.