handle subscriptions and unimplemented

this also means we handle new methods that haven't been added to our list as only 2 cu
This commit is contained in:
Bryan Stitt 2023-06-26 21:09:33 -07:00
parent 8cb57e2f88
commit 1a7d4c9cbb
2 changed files with 11 additions and 5 deletions

View File

@ -14,7 +14,12 @@ pub struct ComputeUnit(Decimal);
impl ComputeUnit { impl ComputeUnit {
/// costs can vary widely depending on method and chain /// costs can vary widely depending on method and chain
pub fn new(method: &str, chain_id: u64) -> Self { pub fn new(method: &str, chain_id: u64, response_bytes: u64) -> Self {
// TODO: this works, but this is fragile. think of a better way to check the method is a subscription
if method.ends_with(')') {
return Self::subscription_response(response_bytes);
}
let cu = match (chain_id, method) { let cu = match (chain_id, method) {
(_, "debug_traceTransaction") => 309, (_, "debug_traceTransaction") => 309,
(_, "debug_traceCall") => 309, (_, "debug_traceCall") => 309,
@ -96,9 +101,8 @@ impl ComputeUnit {
(_, "eth_getUserOperationReceipt") => 15, (_, "eth_getUserOperationReceipt") => 15,
(_, "eth_supportedEntryPoints") => 5, (_, "eth_supportedEntryPoints") => 5,
(_, method) => { (_, method) => {
// default to 10 CU for methods that aren't included here
warn!("unknown method {}", method); warn!("unknown method {}", method);
10 return Self::unimplemented();
} }
}; };
@ -123,13 +127,15 @@ impl ComputeUnit {
/// All methods cost the same /// All methods cost the same
/// The number of bytes are based on input, and output bytes /// The number of bytes are based on input, and output bytes
pub fn cost(&self, archive_request: bool, cache_hit: bool, usd_per_cu: Decimal) -> Decimal { pub fn cost(&self, archive_request: bool, cache_hit: bool, usd_per_cu: Decimal) -> Decimal {
// TODO: server errors are free. need to split server and user errors
let mut cost = self.0 * usd_per_cu; let mut cost = self.0 * usd_per_cu;
if archive_request { if archive_request {
cost *= Decimal::from_str("2.5").unwrap(); cost *= Decimal::from_str("2.5").unwrap();
} }
// cache hits get a 50% discount // cache hits get a 25% discount
if cache_hit { if cache_hit {
cost *= Decimal::from_str("0.75").unwrap() cost *= Decimal::from_str("0.75").unwrap()
} }

View File

@ -810,7 +810,7 @@ impl TryFrom<RequestMetadata> for RpcQueryStats {
x => x, x => x,
}; };
let cu = ComputeUnit::new(&metadata.method, metadata.chain_id); let cu = ComputeUnit::new(&metadata.method, metadata.chain_id, response_bytes);
// TODO: get from config? a helper function? how should we pick this? // TODO: get from config? a helper function? how should we pick this?
let usd_per_cu = match metadata.chain_id { let usd_per_cu = match metadata.chain_id {