Skip to content

Commit

Permalink
extracted_request_metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
StanislavBreadless committed Dec 7, 2021
1 parent 462d8b0 commit 1e24856
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 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 = "request_metadata";
const METADATA_PARAM_NAME: &str = "extracted_request_metadata";

/// Unfortunately, the JSON-RPC library does not natively support retrieving any information about the HTTP request,
///
Expand Down
21 changes: 13 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,18 @@ impl RpcApp {
tx: Box<ZkSyncTx>,
signature: Box<TxEthSignatureVariant>,
fast_processing: Option<bool>,
request_metadata: Option<RequestMetadata>,
extracted_request_metadata: Option<RequestMetadata>,
) -> Result<TxHash> {
let start = Instant::now();

let result = self
.tx_sender
.submit_tx_with_separate_fp(*tx, *signature, fast_processing, request_metadata)
.submit_tx_with_separate_fp(
*tx,
*signature,
fast_processing,
extracted_request_metadata,
)
.await
.map_err(Error::from);
metrics::histogram!("api.rpc.tx_submit", start.elapsed());
Expand All @@ -142,13 +147,13 @@ impl RpcApp {
self,
txs: Vec<TxWithSignature>,
eth_signatures: Option<EthBatchSignatures>,
request_metadata: Option<RequestMetadata>,
extracted_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, request_metadata)
.submit_txs_batch(txs, eth_signatures, extracted_request_metadata)
.await
.map_err(Error::from)
.map(|response| {
Expand Down Expand Up @@ -236,7 +241,7 @@ impl RpcApp {
tx_type: ApiTxFeeTypes,
address: Address,
token: TokenLike,
request_metadata: Option<RequestMetadata>,
extracted_request_metadata: Option<RequestMetadata>,
) -> Result<Fee> {
let start = Instant::now();
let ticker = self.tx_sender.ticker_requests.clone();
Expand All @@ -254,7 +259,7 @@ impl RpcApp {
&result.normal_fee.total_fee,
&result.subsidized_fee.total_fee,
&result.subsidy_size_usd,
request_metadata,
extracted_request_metadata,
)
.await?;

Expand All @@ -273,7 +278,7 @@ impl RpcApp {
tx_types: Vec<ApiTxFeeTypes>,
addresses: Vec<Address>,
token: TokenLike,
request_metadata: Option<RequestMetadata>,
extracted_request_metadata: Option<RequestMetadata>,
) -> Result<TotalFee> {
let start = Instant::now();
if tx_types.len() != addresses.len() {
Expand Down Expand Up @@ -305,7 +310,7 @@ impl RpcApp {
&result.normal_fee.total_fee,
&result.subsidized_fee.total_fee,
&result.subsidy_size_usd,
request_metadata,
extracted_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>,
request_metadata: Option<RequestMetadata>,
extracted_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>,
request_metadata: Option<RequestMetadata>,
extracted_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,
request_metadata: Option<RequestMetadata>,
extracted_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,
request_metadata: Option<RequestMetadata>,
extracted_request_metadata: Option<RequestMetadata>,
) -> BoxFutureResult<TotalFee>;

#[rpc(name = "get_token_price", returns = "BigDecimal")]
Expand Down
19 changes: 10 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>,
request_metadata: Option<RequestMetadata>,
extracted_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,8 @@ impl TxSender {
withdraw.fast = fast_processing;
}

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

pub async fn can_subsidize(
Expand Down Expand Up @@ -417,9 +418,9 @@ impl TxSender {
normal_fee: &BigUint,
subsidized_fee: &BigUint,
subsidy_size_usd: &Ratio<BigUint>,
request_metadata: Option<RequestMetadata>,
extracted_request_metadata: Option<RequestMetadata>,
) -> Result<bool, SubmitError> {
let should_subsidize_ip = if let Some(meta) = request_metadata {
let should_subsidize_ip = if let Some(meta) = extracted_request_metadata {
self.subsidized_ips.contains(&meta.ip)
} else {
false
Expand Down Expand Up @@ -482,7 +483,7 @@ impl TxSender {
&self,
tx: ZkSyncTx,
signature: TxEthSignatureVariant,
meta: Option<RequestMetadata>,
extracted_request_metadata: Option<RequestMetadata>,
) -> Result<TxHash, SubmitError> {
if tx.is_close() {
return Err(SubmitError::AccountCloseDisabled);
Expand Down Expand Up @@ -534,7 +535,7 @@ impl TxSender {
&required_fee_data.normal_fee.total_fee,
&required_fee_data.subsidized_fee.total_fee,
&required_fee_data.subsidy_size_usd,
meta,
extracted_request_metadata,
)
.await?
{
Expand Down Expand Up @@ -621,7 +622,7 @@ impl TxSender {
&self,
txs: Vec<TxWithSignature>,
eth_signatures: Option<EthBatchSignatures>,
request_metadata: Option<RequestMetadata>,
extracted_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 +720,7 @@ impl TxSender {
&batch_token_fee.normal_fee.total_fee,
&batch_token_fee.subsidized_fee.total_fee,
&batch_token_fee.subsidy_size_usd,
request_metadata,
extracted_request_metadata,
)
.await?
{
Expand Down Expand Up @@ -756,7 +757,7 @@ impl TxSender {
&required_eth_fee.normal_fee.total_fee,
&required_eth_fee.subsidized_fee.total_fee,
&required_eth_fee.subsidy_size_usd,
request_metadata,
extracted_request_metadata,
)
.await?
{
Expand Down

0 comments on commit 1e24856

Please sign in to comment.