Skip to content

Commit

Permalink
Merge #2205
Browse files Browse the repository at this point in the history
2205: Fix txs volume metric r=Deniallugo a=Deniallugo

Signed-off-by: deniallugo <[email protected]>

Co-authored-by: deniallugo <[email protected]>
  • Loading branch information
bors-matterlabs-dev[bot] and Deniallugo authored Mar 22, 2022
2 parents d34f9ef + af202d9 commit 99cee3b
Show file tree
Hide file tree
Showing 30 changed files with 189 additions and 73 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ members = [
"core/bin/zksync_forced_exit_requests",

# Libraries
"core/lib/token_db_cache",
"core/lib/circuit",
"core/lib/eth_client",
"core/lib/eth_signer",
Expand Down
1 change: 1 addition & 0 deletions core/bin/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ async fn run_server(components: &ComponentsToRun) {
tasks.push(zksync_api::api_server::web3::start_rpc_server(
connection_pool.clone(),
&Web3Config::from_env(),
&CommonApiConfig::from_env(),
));
}

Expand Down
1 change: 1 addition & 0 deletions core/bin/zksync_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ api_test = []
[dependencies]
zksync_types = { path = "../../lib/types", version = "1.0" }
zksync_storage = { path = "../../lib/storage", version = "1.0" }
zksync_token_db_cache = { path = "../../lib/token_db_cache", version = "1.0" }

zksync_crypto = { path = "../../lib/crypto", version = "1.0" }
zksync_config = { path = "../../lib/config", version = "1.0" }
Expand Down
8 changes: 7 additions & 1 deletion core/bin/zksync_api/src/api_server/event_notify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use jsonrpc_pubsub::{
SubscriptionId,
};
use std::time::Duration;
use zksync_config::configs::api::CommonApiConfig;
use zksync_storage::ConnectionPool;
use zksync_types::tx::TxHash;
use zksync_types::BlockNumber;
Expand Down Expand Up @@ -59,11 +60,16 @@ pub fn start_sub_notifier(
mut subscription_stream: mpsc::Receiver<EventNotifierRequest>,
api_requests_caches_size: usize,
miniblock_interval: Duration,
common_api_config: &CommonApiConfig,
) -> tokio::task::JoinHandle<()> {
let (new_block_sender, mut new_block_receiver) = mpsc::channel(NOTIFIER_CHANNEL_CAPACITY);
let (new_txs_sender, mut new_txs_receiver) = mpsc::channel(NOTIFIER_CHANNEL_CAPACITY);

let mut notifier = OperationNotifier::new(api_requests_caches_size, db_pool.clone());
let mut notifier = OperationNotifier::new(
api_requests_caches_size,
db_pool.clone(),
common_api_config.invalidate_token_cache_period(),
);

tokio::spawn(async move {
let fetcher = EventFetcher::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::api_server::rpc_server::types::{
BlockInfo, ETHOpInfoResp, ResponseAccountState, TransactionInfoResp,
};
use jsonrpc_pubsub::{typed::Subscriber, SubscriptionId};
use std::time::Instant;
use std::time::{Duration, Instant};
use zksync_storage::ConnectionPool;
use zksync_types::aggregated_operations::AggregatedOperation;
use zksync_types::tx::TxHash;
Expand All @@ -23,9 +23,13 @@ pub struct OperationNotifier {
}

impl OperationNotifier {
pub fn new(cache_capacity: usize, db_pool: ConnectionPool) -> Self {
pub fn new(
cache_capacity: usize,
db_pool: ConnectionPool,
token_cache_invalidate_period: Duration,
) -> Self {
Self {
state: NotifierState::new(cache_capacity, db_pool),
state: NotifierState::new(cache_capacity, db_pool, token_cache_invalidate_period),
tx_subs: SubStorage::new(),
prior_op_subs: SubStorage::new(),
account_subs: SubStorage::new(),
Expand Down
13 changes: 8 additions & 5 deletions core/bin/zksync_api/src/api_server/event_notify/state.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::api_server::rpc_server::types::{BlockInfo, ResponseAccountState};
use crate::utils::token_db_cache::TokenDBCache;
use lru_cache::LruCache;
use std::time::Instant;
use std::time::{Duration, Instant};
use zksync_storage::chain::operations::records::StoredExecutedPriorityOperation;
use zksync_storage::chain::operations_ext::records::TxReceiptResponse;
use zksync_storage::ConnectionPool;
use zksync_token_db_cache::TokenDBCache;
use zksync_types::aggregated_operations::AggregatedActionType;
use zksync_types::tx::TxHash;
use zksync_types::BlockNumber;
Expand All @@ -16,17 +16,20 @@ pub struct NotifierState {
pub(super) cache_of_transaction_receipts: LruCache<Vec<u8>, TxReceiptResponse>,
pub(super) cache_of_blocks_info: LruCache<BlockNumber, BlockInfo>,
pub(super) tokens_cache: TokenDBCache,

pub(super) db_pool: ConnectionPool,
}

impl NotifierState {
pub fn new(cache_capacity: usize, db_pool: ConnectionPool) -> Self {
pub fn new(
cache_capacity: usize,
db_pool: ConnectionPool,
token_cache_invalidate_period: Duration,
) -> Self {
Self {
cache_of_executed_priority_operations: LruCache::new(cache_capacity),
cache_of_transaction_receipts: LruCache::new(cache_capacity),
cache_of_blocks_info: LruCache::new(cache_capacity),
tokens_cache: TokenDBCache::new(),
tokens_cache: TokenDBCache::new(token_cache_invalidate_period),
db_pool,
}
}
Expand Down
3 changes: 2 additions & 1 deletion core/bin/zksync_api/src/api_server/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ use num::BigUint;
// Workspace uses
use zksync_api_types::v02::account::{DepositingAccountBalances, DepositingFunds, OngoingDeposit};
use zksync_storage::StorageProcessor;
use zksync_token_db_cache::TokenDBCache;
use zksync_types::{Address, H256};
use zksync_utils::remove_prefix;

// Local uses
use super::rest::v02::error::Error;
use crate::{fee_ticker::PriceError, utils::token_db_cache::TokenDBCache};
use crate::fee_ticker::PriceError;

pub fn try_parse_hash(query: &str) -> Result<H256, hex::FromHexError> {
const HASH_SIZE: usize = 32; // 32 bytes
Expand Down
8 changes: 3 additions & 5 deletions core/bin/zksync_api/src/api_server/rest/v02/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use zksync_api_types::v02::{
};
use zksync_crypto::params::{MIN_NFT_TOKEN_ID, NFT_TOKEN_ID_VAL};
use zksync_storage::{ConnectionPool, StorageProcessor};
use zksync_token_db_cache::TokenDBCache;
use zksync_types::{tx::TxHash, AccountId, Address, BlockNumber, SerialId, TokenLike};

// Local uses
Expand All @@ -26,10 +27,7 @@ use super::{
paginate_trait::Paginate,
response::ApiResult,
};
use crate::{
api_server::helpers::get_depositing, api_try, fee_ticker::PriceError,
utils::token_db_cache::TokenDBCache,
};
use crate::{api_server::helpers::get_depositing, api_try, fee_ticker::PriceError};

/// Shared data between `api/v02/accounts` endpoints.
#[derive(Clone)]
Expand Down Expand Up @@ -534,7 +532,7 @@ mod tests {
move |cfg: &TestServerConfig| {
api_scope(
cfg.pool.clone(),
TokenDBCache::new(),
TokenDBCache::new(cfg.config.api.common.invalidate_token_cache_period()),
cfg.config.eth_watch.confirmations_for_eth_event,
)
},
Expand Down
4 changes: 2 additions & 2 deletions core/bin/zksync_api/src/api_server/rest/v02/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use zksync_api_types::v02::{
use zksync_config::ZkSyncConfig;
use zksync_crypto::params::MIN_NFT_TOKEN_ID;
use zksync_storage::{ConnectionPool, StorageProcessor};
use zksync_token_db_cache::TokenDBCache;
use zksync_types::{tx::TxHash, AccountId, Token, TokenId, TokenLike};

// Local uses
Expand All @@ -31,7 +32,6 @@ use super::{
use crate::{
api_try,
fee_ticker::{FeeTicker, PriceError, TokenPriceRequestType},
utils::token_db_cache::TokenDBCache,
};

/// Shared data between `api/v0.2/tokens` endpoints.
Expand Down Expand Up @@ -365,7 +365,7 @@ mod tests {
api_scope(
&cfg.config,
cfg.pool.clone(),
TokenDBCache::new(),
TokenDBCache::new(cfg.config.api.common.invalidate_token_cache_period()),
fee_ticker.clone(),
)
},
Expand Down
4 changes: 1 addition & 3 deletions core/bin/zksync_api/src/api_server/rpc_server/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ use zksync_api_types::v02::{
};
use zksync_crypto::params::{MIN_NFT_TOKEN_ID, NFT_TOKEN_ID_VAL};
use zksync_storage::StorageProcessor;
use zksync_token_db_cache::TokenDBCache;
use zksync_types::{Account, AccountId, Address, Nonce, PubKeyHash, TokenId};
use zksync_utils::BigUintSerdeWrapper;

// Local uses
use crate::utils::token_db_cache::TokenDBCache;

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct ResponseAccountState {
Expand Down
1 change: 1 addition & 0 deletions core/bin/zksync_api/src/api_server/rpc_subscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ pub fn start_ws_server(
event_sub_receiver,
common_config.caches_size,
miniblock_iteration_interval,
common_config,
);

let req_rpc_app = super::rpc_server::RpcApp::new(
Expand Down
5 changes: 3 additions & 2 deletions core/bin/zksync_api/src/api_server/tx_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use zksync_api_types::{
};
use zksync_storage::misc::records::Subsidy;
use zksync_storage::{chain::account::records::EthAccountType, ConnectionPool};
use zksync_token_db_cache::TokenDBCache;
use zksync_types::{
tx::{
EthBatchSignData, EthBatchSignatures, EthSignData, Order, SignedZkSyncTx, TxEthSignature,
Expand All @@ -48,7 +49,7 @@ use crate::{
VerifySignatureRequest,
},
tx_error::{Toggle2FAError, TxAddError},
utils::{block_details_cache::BlockDetailsCache, token_db_cache::TokenDBCache},
utils::block_details_cache::BlockDetailsCache,
};
use zksync_config::configs::api::CommonApiConfig;

Expand Down Expand Up @@ -174,7 +175,7 @@ impl TxSender {
pool: connection_pool,
sign_verify_requests: sign_verify_request_sender,
ticker,
tokens: TokenDBCache::new(),
tokens: TokenDBCache::new(config.invalidate_token_cache_period()),
forced_exit_checker: ForcedExitChecker::new(
config.forced_exit_minimum_account_age_secs,
),
Expand Down
8 changes: 5 additions & 3 deletions core/bin/zksync_api/src/api_server/web3/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ use std::collections::HashMap;
use std::convert::TryInto;
use std::path::PathBuf;
use std::str::FromStr;
use std::time::Duration;
// External uses
use ethabi::{encode, Contract, Function, Token as AbiToken};
use jsonrpc_core::{Error, ErrorCode, Result};
use tiny_keccak::keccak256;
// Workspace uses
use zksync_storage::StorageProcessor;
use zksync_token_db_cache::TokenDBCache;
use zksync_types::{TokenId, TokenKind, NFT};

// Local uses
use super::{
converter::u256_from_biguint,
types::{H160, U256},
NFT_FACTORY_ADDRESS, ZKSYNC_PROXY_ADDRESS,
};
use crate::utils::token_db_cache::TokenDBCache;

type Selector = [u8; 4];

Expand Down Expand Up @@ -58,7 +60,7 @@ impl CallsHelper {
.collect()
}

pub fn new() -> Self {
pub fn new(invalidate_token_cache_period: Duration) -> Self {
let mut path = PathBuf::new();
path.push(std::env::var("ZKSYNC_HOME").unwrap_or_else(|_| "/".to_string()));
path.push("etc/web3-abi");
Expand All @@ -85,7 +87,7 @@ impl CallsHelper {
Self {
erc20: erc20_function_by_selector,
nft_factory: nft_factory_function_by_selector,
tokens: TokenDBCache::new(),
tokens: TokenDBCache::new(invalidate_token_cache_period),
zksync_proxy_address: H160::from_str(ZKSYNC_PROXY_ADDRESS).unwrap(),
nft_factory_address: H160::from_str(NFT_FACTORY_ADDRESS).unwrap(),
}
Expand Down
7 changes: 4 additions & 3 deletions core/bin/zksync_api/src/api_server/web3/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
use std::collections::HashMap;
use std::path::PathBuf;
use std::str::FromStr;
use std::time::Duration;
// External uses
use ethabi::{encode, Contract, Token as AbiToken};
use jsonrpc_core::{Error, Result};
use num::{BigUint, Zero};
// Workspace uses
use zksync_storage::StorageProcessor;
use zksync_token_db_cache::TokenDBCache;
use zksync_types::{Nonce, Token, TokenId, TokenKind, ZkSyncOp, NFT};
// Local uses
use super::{
converter::{log, u256_from_biguint},
types::{Bytes, CommonLogData, Event, Log, H160, H256, U256},
NFT_FACTORY_ADDRESS, ZKSYNC_PROXY_ADDRESS,
};
use crate::utils::token_db_cache::TokenDBCache;

#[derive(Debug, Clone)]
pub struct LogsHelper {
Expand All @@ -26,7 +27,7 @@ pub struct LogsHelper {
}

impl LogsHelper {
pub fn new() -> Self {
pub fn new(invalidate_token_cache_period: Duration) -> Self {
let mut path = PathBuf::new();
path.push(std::env::var("ZKSYNC_HOME").unwrap_or_else(|_| "/".to_string()));
path.push("etc/web3-abi");
Expand Down Expand Up @@ -93,7 +94,7 @@ impl LogsHelper {

Self {
topic_by_event,
tokens: TokenDBCache::new(),
tokens: TokenDBCache::new(invalidate_token_cache_period),
zksync_proxy_address: H160::from_str(ZKSYNC_PROXY_ADDRESS).unwrap(),
nft_factory_address: H160::from_str(NFT_FACTORY_ADDRESS).unwrap(),
}
Expand Down
15 changes: 10 additions & 5 deletions core/bin/zksync_api/src/api_server/web3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use zksync_utils::panic_notify::{spawn_panic_handler, ThreadPanicNotify};
use self::{calls::CallsHelper, logs::LogsHelper, rpc_trait::Web3Rpc};

use tokio::task::JoinHandle;
use zksync_config::configs::api::Web3Config;
use zksync_config::configs::api::{CommonApiConfig, Web3Config};

mod calls;
mod converter;
Expand All @@ -35,11 +35,15 @@ pub struct Web3RpcApp {
}

impl Web3RpcApp {
pub fn new(connection_pool: ConnectionPool, config: &Web3Config) -> Self {
pub fn new(
connection_pool: ConnectionPool,
config: &Web3Config,
common_config: &CommonApiConfig,
) -> Self {
Web3RpcApp {
connection_pool,
logs_helper: LogsHelper::new(),
calls_helper: CallsHelper::new(),
logs_helper: LogsHelper::new(common_config.invalidate_token_cache_period()),
calls_helper: CallsHelper::new(common_config.invalidate_token_cache_period()),
max_block_range: config.max_block_range,
chain_id: config.chain_id,
}
Expand All @@ -60,10 +64,11 @@ impl Web3RpcApp {
pub fn start_rpc_server(
connection_pool: ConnectionPool,
web3_config: &Web3Config,
common_config: &CommonApiConfig,
) -> JoinHandle<()> {
let addr = web3_config.bind_addr();

let rpc_app = Web3RpcApp::new(connection_pool, web3_config);
let rpc_app = Web3RpcApp::new(connection_pool, web3_config, common_config);
let (handler, panic_sender) = spawn_panic_handler();

std::thread::spawn(move || {
Expand Down
Loading

0 comments on commit 99cee3b

Please sign in to comment.