Skip to content

Commit

Permalink
Optimisation + review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
StanislavBreadless committed Dec 7, 2021
1 parent 03f8579 commit eead858
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use jsonrpc_http_server::{RequestMiddleware, RequestMiddlewareAction};
use super::types::RequestMetadata;

const CLOUDFLARE_CONNECTING_IP_HEADER: &str = "CF-Connecting-IP";
const METADATA_PARAM_NAME: &str = "meta";
const METADATA_PARAM_NAME: &str = "request_metadata";

/// Unfortunately, the JSON-RPC library does not natively support retrieving any information about the HTTP request,
///
Expand Down Expand Up @@ -42,7 +42,7 @@ impl MethodWithIpDescription {
/// If the method should have information about the IP appended to its parameters, it returns the new call
/// which is identical to the supplied one, but with the IP appended.
fn get_call_with_ip_if_needed(
call: jsonrpc_core::MethodCall,
mut call: jsonrpc_core::MethodCall,
ip: Option<String>,
) -> jsonrpc_core::MethodCall {
// Methods, which should have the information about the ip appended to them
Expand All @@ -68,15 +68,13 @@ fn get_call_with_ip_if_needed(
serde_json::to_value(metadata).unwrap()
});

let mut new_call = call.clone();

match call.params {
Params::Array(mut params) => {
Params::Array(ref mut params) => {
// The query is definitely wrong. We may proceed and the server will handle it normally
if params.len() > description.maximum_params
|| params.len() < description.minimum_params
{
return new_call;
return call;
}

// If the length is equal to the maximum amount of the
Expand All @@ -92,21 +90,19 @@ fn get_call_with_ip_if_needed(

if let Some(metadata) = metadata {
params.push(metadata);
new_call.params = Params::Array(params);
}

new_call
call
}
Params::Map(mut params_map) => {
Params::Map(ref mut params_map) => {
if let Some(metadata) = metadata {
params_map.insert(METADATA_PARAM_NAME.to_owned(), metadata);
} else {
// Just in case the user tried to override the value in the map
params_map.remove(METADATA_PARAM_NAME);
}
new_call.params = Params::Map(params_map);

new_call
call
}
_ => call,
}
Expand All @@ -123,18 +119,16 @@ async fn insert_ip_if_needed(body: hyper::Body, ip: Option<String>) -> hyper::Re
body_bytes.extend(bytes?.into_iter());
}

let body_str = String::from_utf8(body_bytes.clone());
let call: std::result::Result<jsonrpc_core::MethodCall, _> =
serde_json::from_slice(&body_bytes);

if let Ok(s) = body_str {
let call: std::result::Result<jsonrpc_core::MethodCall, _> = serde_json::from_str(&s);
if let Ok(call) = call {
let new_call = get_call_with_ip_if_needed(call, ip);
let new_body_bytes = serde_json::to_string(&new_call);
if let Ok(s) = new_body_bytes {
body_bytes = s.as_bytes().to_owned();
}
};
}
if let Ok(call) = call {
let new_call = get_call_with_ip_if_needed(call, ip);
let new_body_bytes = serde_json::to_vec(&new_call);
if let Ok(s) = new_body_bytes {
body_bytes = s;
}
};

Ok(body_bytes)
}
Expand Down
16 changes: 8 additions & 8 deletions core/bin/zksync_api/src/api_server/rpc_server/rpc_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ impl RpcApp {
tx: Box<ZkSyncTx>,
signature: Box<TxEthSignatureVariant>,
fast_processing: Option<bool>,
meta: Option<RequestMetadata>,
request_metadata: Option<RequestMetadata>,
) -> Result<TxHash> {
let start = Instant::now();

let result = self
.tx_sender
.submit_tx_with_separate_fp(*tx, *signature, fast_processing, meta)
.submit_tx_with_separate_fp(*tx, *signature, fast_processing, request_metadata)
.await
.map_err(Error::from);
metrics::histogram!("api.rpc.tx_submit", start.elapsed());
Expand All @@ -142,13 +142,13 @@ impl RpcApp {
self,
txs: Vec<TxWithSignature>,
eth_signatures: Option<EthBatchSignatures>,
meta: Option<RequestMetadata>,
request_metadata: Option<RequestMetadata>,
) -> Result<Vec<TxHash>> {
let start = Instant::now();

let result: Result<Vec<TxHash>> = self
.tx_sender
.submit_txs_batch(txs, eth_signatures, meta)
.submit_txs_batch(txs, eth_signatures, request_metadata)
.await
.map_err(Error::from)
.map(|response| {
Expand Down Expand Up @@ -236,7 +236,7 @@ impl RpcApp {
tx_type: ApiTxFeeTypes,
address: Address,
token: TokenLike,
meta: Option<RequestMetadata>,
request_metadata: Option<RequestMetadata>,
) -> Result<Fee> {
let start = Instant::now();
let ticker = self.tx_sender.ticker_requests.clone();
Expand All @@ -254,7 +254,7 @@ impl RpcApp {
&result.normal_fee.total_fee,
&result.subsidized_fee.total_fee,
&result.subsidy_size_usd,
meta,
request_metadata,
)
.await?;

Expand All @@ -273,7 +273,7 @@ impl RpcApp {
tx_types: Vec<ApiTxFeeTypes>,
addresses: Vec<Address>,
token: TokenLike,
meta: Option<RequestMetadata>,
request_metadata: Option<RequestMetadata>,
) -> Result<TotalFee> {
let start = Instant::now();
if tx_types.len() != addresses.len() {
Expand Down Expand Up @@ -305,7 +305,7 @@ impl RpcApp {
&result.normal_fee.total_fee,
&result.subsidized_fee.total_fee,
&result.subsidy_size_usd,
meta,
request_metadata,
)
.await?;

Expand Down
8 changes: 4 additions & 4 deletions core/bin/zksync_api/src/api_server/rpc_server/rpc_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ pub trait Rpc {
tx: Box<ZkSyncTx>,
signature: Box<TxEthSignatureVariant>,
fast_processing: Option<bool>,
meta: Option<RequestMetadata>,
request_metadata: Option<RequestMetadata>,
) -> BoxFutureResult<TxHash>;

#[rpc(name = "submit_txs_batch", returns = "Vec<TxHash>")]
fn submit_txs_batch(
&self,
txs: Vec<TxWithSignature>,
eth_signatures: Option<EthBatchSignatures>,
meta: Option<RequestMetadata>,
request_metadata: Option<RequestMetadata>,
) -> BoxFutureResult<Vec<TxHash>>;

#[rpc(name = "contract_address", returns = "ContractAddressResp")]
Expand All @@ -73,7 +73,7 @@ pub trait Rpc {
tx_type: ApiTxFeeTypes,
_address: Address,
token_like: TokenLike,
meta: Option<RequestMetadata>,
request_metadata: Option<RequestMetadata>,
) -> BoxFutureResult<Fee>;

// _addresses argument is left for the backward compatibility.
Expand All @@ -83,7 +83,7 @@ pub trait Rpc {
tx_types: Vec<ApiTxFeeTypes>,
_addresses: Vec<Address>,
token_like: TokenLike,
meta: Option<RequestMetadata>,
request_metadata: Option<RequestMetadata>,
) -> BoxFutureResult<TotalFee>;

#[rpc(name = "get_token_price", returns = "BigDecimal")]
Expand Down
18 changes: 9 additions & 9 deletions core/bin/zksync_api/src/api_server/tx_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ impl TxSender {
mut tx: ZkSyncTx,
signature: TxEthSignatureVariant,
fast_processing: Option<bool>,
meta: Option<RequestMetadata>,
request_metadata: Option<RequestMetadata>,
) -> Result<TxHash, SubmitError> {
let fast_processing = fast_processing.unwrap_or(false);
if fast_processing && !tx.is_withdraw() {
Expand All @@ -387,7 +387,7 @@ impl TxSender {
withdraw.fast = fast_processing;
}

self.submit_tx(tx, signature, meta).await
self.submit_tx(tx, signature, request_metadata).await
}

pub async fn can_subsidize(
Expand All @@ -399,12 +399,12 @@ impl TxSender {
.access_storage()
.await?
.misc_schema()
.get_total_used_subsidy_for_type(self.current_subsidy_type.clone())
.get_total_used_subsidy_for_type(&self.current_subsidy_type)
.await?;
let subsidized_already_usd = scaled_big_decimal_to_ratio(subsidized_already)?;

let result = if self.max_subsidy_usd > subsidized_already_usd {
&self.max_subsidy_usd - subsidized_already_usd >= new_subsidy_usd
&self.max_subsidy_usd - &subsidized_already_usd >= new_subsidy_usd
} else {
false
};
Expand All @@ -417,9 +417,9 @@ impl TxSender {
normal_fee: &BigUint,
subsidized_fee: &BigUint,
subsidy_size_usd: &Ratio<BigUint>,
meta: Option<RequestMetadata>,
request_metadata: Option<RequestMetadata>,
) -> Result<bool, SubmitError> {
let should_subsidize_ip = if let Some(meta) = meta {
let should_subsidize_ip = if let Some(meta) = request_metadata {
self.subsidized_ips.contains(&meta.ip)
} else {
false
Expand Down Expand Up @@ -621,7 +621,7 @@ impl TxSender {
&self,
txs: Vec<TxWithSignature>,
eth_signatures: Option<EthBatchSignatures>,
meta: Option<RequestMetadata>,
request_metadata: Option<RequestMetadata>,
) -> Result<SubmitBatchResponse, SubmitError> {
// Bring the received signatures into a vector for simplified work.
let eth_signatures = EthBatchSignatures::api_arg_to_vec(eth_signatures);
Expand Down Expand Up @@ -719,7 +719,7 @@ impl TxSender {
&batch_token_fee.normal_fee.total_fee,
&batch_token_fee.subsidized_fee.total_fee,
&batch_token_fee.subsidy_size_usd,
meta,
request_metadata,
)
.await?
{
Expand Down Expand Up @@ -756,7 +756,7 @@ impl TxSender {
&required_eth_fee.normal_fee.total_fee,
&required_eth_fee.subsidized_fee.total_fee,
&required_eth_fee.subsidy_size_usd,
meta,
request_metadata,
)
.await?
{
Expand Down
12 changes: 7 additions & 5 deletions core/bin/zksync_api/src/fee_ticker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,15 +484,18 @@ impl<API: FeeTickerAPI, INFO: FeeTickerInfo, WATCHER: TokenWatcher> FeeTicker<AP
);

if fee_type == CPK_CREATE2_FEE_TYPE {
let subsidized_fee_usd = self.config.subsidy_cpk_price_usd.clone();
let token_price = self
.get_token_price(TokenLike::Id(token.id), TokenPriceRequestType::USDForOneWei)
.await?;

// It is safe to do unwrap in the next two lines, because token being acceptable for fees
// assumes that the token's price is > 0
let token_price = big_decimal_to_ratio(&token_price).unwrap();
let full_amount = subsidized_fee_usd.checked_div(&token_price).unwrap();
let full_amount = self
.config
.subsidy_cpk_price_usd
.checked_div(&token_price)
.unwrap();

let subsidized_fee = Fee::new(
fee_type,
Expand Down Expand Up @@ -606,15 +609,14 @@ impl<API: FeeTickerAPI, INFO: FeeTickerInfo, WATCHER: TokenWatcher> FeeTicker<AP
}

let normal_fee = {
let total_zkp_fee = (&zkp_cost_chunk * total_op_chunks) * token_usd_risk.clone();
let total_zkp_fee = (&zkp_cost_chunk * total_op_chunks) * &token_usd_risk;
let total_gas_fee =
(&wei_price_usd * total_normal_gas_tx_amount * &scale_gas_price) * &token_usd_risk;
BatchFee::new(total_zkp_fee, total_gas_fee)
};

let subsidized_fee = {
let total_zkp_fee =
(zkp_cost_chunk * total_subsidized_op_chunks) * token_usd_risk.clone();
let total_zkp_fee = (zkp_cost_chunk * total_subsidized_op_chunks) * &token_usd_risk;
let total_gas_fee =
(&wei_price_usd * total_subsidized_gas_tx_amount * &scale_gas_price)
* &token_usd_risk;
Expand Down
8 changes: 4 additions & 4 deletions core/bin/zksync_api/src/fee_ticker/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ fn get_subsidy_token_fee_in_usd(
* fee_in_token
}

fn convert_to_usd(amount: Ratio<BigUint>, token: TokenLike) -> Ratio<BigUint> {
fn convert_to_usd(amount: &Ratio<BigUint>, token: TokenLike) -> Ratio<BigUint> {
let token_precision = block_on(MockApiProvider.get_token(token.clone()))
.unwrap()
.decimals;
Expand Down Expand Up @@ -410,7 +410,7 @@ fn test_ticker_subsidy() {
None,
);
let create2_subsidy_price_usd =
convert_to_usd(create2_subsidy_price.clone(), TokenLike::Id(TokenId(0)));
convert_to_usd(&create2_subsidy_price, TokenLike::Id(TokenId(0)));

// Due to precision-rounding, the price might differ, but it shouldn't by more than 1 cent
assert!(
Expand Down Expand Up @@ -453,7 +453,7 @@ fn test_ticker_subsidy() {
);
assert_eq!(normal_transfer_price, subsidy_transfer_price);
let normal_transfer_price_usd =
convert_to_usd(normal_transfer_price, TokenLike::Id(TokenId(0)));
convert_to_usd(&normal_transfer_price, TokenLike::Id(TokenId(0)));

// Subsidy also works for batches
let batch_price_token = block_on(ticker.get_batch_from_ticker_in_wei(
Expand All @@ -466,7 +466,7 @@ fn test_ticker_subsidy() {
))
.unwrap();
let subsidy_batch_price_usd = convert_to_usd(
Ratio::from(batch_price_token.subsidized_fee.total_fee),
&Ratio::from(batch_price_token.subsidized_fee.total_fee),
TokenLike::Id(TokenId(0)),
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TABLE subsidies (
CREATE TABLE IF NOT EXISTS subsidies (
id SERIAL PRIMARY KEY,
tx_hash bytea NOT NULL,
--- USD amounts are stored scaled by 10^6
Expand Down
2 changes: 1 addition & 1 deletion core/lib/storage/src/misc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<'a, 'c> MiscSchema<'a, 'c> {
/// Loads tokens from the database starting from the given id with the given limit in the ascending order.
pub async fn get_total_used_subsidy_for_type(
&mut self,
subsidy_type: String,
subsidy_type: &str,
) -> QueryResult<BigDecimal> {
let start = Instant::now();
let sum = sqlx::query!(
Expand Down
8 changes: 4 additions & 4 deletions core/lib/storage/src/tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@ async fn stored_subsidy(mut storage: StorageProcessor<'_>) -> QueryResult<()> {
let subsidy_3 = get_subsidy(subsidy_name.clone(), 15);

let get_total_subsidy = MiscSchema(&mut storage)
.get_total_used_subsidy_for_type(subsidy_name.clone())
.get_total_used_subsidy_for_type(&subsidy_name)
.await?;
assert_eq!(get_total_subsidy, BigDecimal::from(0));

MiscSchema(&mut storage).store_subsidy(subsidy_1).await?;
let get_total_subsidy = MiscSchema(&mut storage)
.get_total_used_subsidy_for_type(subsidy_name.clone())
.get_total_used_subsidy_for_type(&subsidy_name)
.await?;
assert_eq!(get_total_subsidy, BigDecimal::from(10));

MiscSchema(&mut storage).store_subsidy(subsidy_2).await?;
let get_total_subsidy = MiscSchema(&mut storage)
.get_total_used_subsidy_for_type(subsidy_name.clone())
.get_total_used_subsidy_for_type(&subsidy_name)
.await?;
assert_eq!(get_total_subsidy, BigDecimal::from(10));

MiscSchema(&mut storage).store_subsidy(subsidy_3).await?;
let get_total_subsidy = MiscSchema(&mut storage)
.get_total_used_subsidy_for_type(subsidy_name.clone())
.get_total_used_subsidy_for_type(&subsidy_name)
.await?;
assert_eq!(get_total_subsidy, BigDecimal::from(25));

Expand Down
2 changes: 1 addition & 1 deletion docker/prover/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# syntax=docker/dockerfile:experimental
FROM rust:1.55 as builder
FROM rust:1.57 as builder
WORKDIR /usr/src/zksync
COPY . .
RUN --mount=type=cache,target=/usr/local/cargo/registry \
Expand Down
2 changes: 1 addition & 1 deletion docker/server/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# syntax=docker/dockerfile:experimental
FROM rust:1.55 as builder
FROM rust:1.57 as builder
WORKDIR /usr/src/zksync
COPY . .
RUN --mount=type=cache,target=/usr/local/cargo/registry \
Expand Down

0 comments on commit eead858

Please sign in to comment.