Skip to content

Commit

Permalink
Rename to Request metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
StanislavBreadless committed Dec 6, 2021
1 parent 3e3dd25 commit afbe557
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use futures::{FutureExt, StreamExt};
use jsonrpc_core::Params;
use jsonrpc_http_server::{RequestMiddleware, RequestMiddlewareAction};

use super::types::RequestMetadata;

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

///
Expand Down Expand Up @@ -84,7 +86,10 @@ fn get_call_with_ip_if_needed(
params.push(serde_json::Value::Null);
}

params.push(serde_json::Value::String(ip));
let metadata = RequestMetadata { ip };
let metadata = serde_json::to_value(metadata).unwrap();

params.push(metadata);

new_call.params = Params::Array(params);
new_call
Expand Down
26 changes: 13 additions & 13 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 @@ -29,9 +29,9 @@ use crate::{
use super::{types::*, RpcApp};

impl RpcApp {
fn should_subsidie_ip(&self, ip: Option<String>) -> bool {
if let Some(ip_str) = ip {
self.subsidized_ips.contains(&ip_str)
fn should_subsidie_ip(&self, meta: Option<RequestMetadata>) -> bool {
if let Some(meta) = meta {
self.subsidized_ips.contains(&meta.ip)
} else {
false
}
Expand Down Expand Up @@ -134,10 +134,10 @@ impl RpcApp {
tx: Box<ZkSyncTx>,
signature: Box<TxEthSignatureVariant>,
fast_processing: Option<bool>,
ip: Option<String>,
meta: Option<RequestMetadata>,
) -> Result<TxHash> {
let start = Instant::now();
let should_subsidie_cpk = self.should_subsidie_ip(ip);
let should_subsidie_cpk = self.should_subsidie_ip(meta);

let result = self
.tx_sender
Expand All @@ -152,11 +152,11 @@ impl RpcApp {
self,
txs: Vec<TxWithSignature>,
eth_signatures: Option<EthBatchSignatures>,
ip: Option<String>,
meta: Option<RequestMetadata>,
) -> Result<Vec<TxHash>> {
let start = Instant::now();

let should_subsidize_ip = self.should_subsidie_ip(ip);
let should_subsidize_ip = self.should_subsidie_ip(meta);

let result: Result<Vec<TxHash>> = self
.tx_sender
Expand Down Expand Up @@ -248,7 +248,7 @@ impl RpcApp {
tx_type: ApiTxFeeTypes,
address: Address,
token: TokenLike,
ip: Option<String>,
meta: Option<RequestMetadata>,
) -> Result<Fee> {
let start = Instant::now();
let ticker = self.tx_sender.ticker_requests.clone();
Expand All @@ -265,7 +265,7 @@ impl RpcApp {
&result.normal_fee.total_fee,
&result.subsidized_fee.total_fee,
&result.subsidy_size_usd,
ip,
meta,
)
.await?;

Expand All @@ -284,9 +284,9 @@ impl RpcApp {
normal_fee: &BigUint,
subsidized_fee: &BigUint,
subsidy_size_usd: &Ratio<BigUint>,
ip: Option<String>,
meta: Option<RequestMetadata>,
) -> Result<bool> {
let should_subsidize_ip = self.should_subsidie_ip(ip);
let should_subsidize_ip = self.should_subsidie_ip(meta);

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

Expand Down
24 changes: 12 additions & 12 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>,
ip: Option<String>,
ip: Option<RequestMetadata>,
) -> BoxFutureResult<TxHash>;

#[rpc(name = "submit_txs_batch", returns = "Vec<TxHash>")]
fn submit_txs_batch(
&self,
txs: Vec<TxWithSignature>,
eth_signatures: Option<EthBatchSignatures>,
ip: Option<String>,
meta: 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,
ip: Option<String>,
meta: 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,
ip: Option<String>,
meta: Option<RequestMetadata>,
) -> BoxFutureResult<TotalFee>;

#[rpc(name = "get_token_price", returns = "BigDecimal")]
Expand Down Expand Up @@ -130,18 +130,18 @@ impl Rpc for RpcApp {
tx: Box<ZkSyncTx>,
signature: Box<TxEthSignatureVariant>,
fast_processing: Option<bool>,
ip: Option<String>,
meta: Option<RequestMetadata>,
) -> BoxFutureResult<TxHash> {
spawn!(self._impl_tx_submit(tx, signature, fast_processing, ip))
spawn!(self._impl_tx_submit(tx, signature, fast_processing, meta))
}

fn submit_txs_batch(
&self,
txs: Vec<TxWithSignature>,
eth_signatures: Option<EthBatchSignatures>,
ip: Option<String>,
meta: Option<RequestMetadata>,
) -> BoxFutureResult<Vec<TxHash>> {
spawn!(self._impl_submit_txs_batch(txs, eth_signatures, ip))
spawn!(self._impl_submit_txs_batch(txs, eth_signatures, meta))
}

fn contract_address(&self) -> BoxFutureResult<ContractAddressResp> {
Expand All @@ -157,19 +157,19 @@ impl Rpc for RpcApp {
tx_type: ApiTxFeeTypes,
address: Address,
token_like: TokenLike,
ip: Option<String>,
meta: Option<RequestMetadata>,
) -> BoxFutureResult<Fee> {
spawn!(self._impl_get_tx_fee(tx_type, address, token_like, ip))
spawn!(self._impl_get_tx_fee(tx_type, address, token_like, meta))
}

fn get_txs_batch_fee_in_wei(
&self,
tx_types: Vec<ApiTxFeeTypes>,
addresses: Vec<Address>,
token_like: TokenLike,
ip: Option<String>,
meta: Option<RequestMetadata>,
) -> BoxFutureResult<TotalFee> {
spawn!(self._impl_get_txs_batch_fee_in_wei(tx_types, addresses, token_like, ip))
spawn!(self._impl_get_txs_batch_fee_in_wei(tx_types, addresses, token_like, meta))
}

fn get_token_price(&self, token_like: TokenLike) -> BoxFutureResult<BigDecimal> {
Expand Down

0 comments on commit afbe557

Please sign in to comment.