Skip to content

Commit

Permalink
Merge branch 'dev' into perekopskiy-zks-431-create-a-centralized-plac…
Browse files Browse the repository at this point in the history
…e-for-reading
  • Loading branch information
perekopskiy committed Feb 12, 2021
2 parents e9b92e7 + 4c69271 commit 8eea55e
Show file tree
Hide file tree
Showing 55 changed files with 1,558 additions and 670 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ jobs:
run: |
ci_run zk
ci_run zk fmt --check
ci_run zk lint --check
ci_run zk lint rust --check
ci_run zk lint js --check
ci_run zk lint ts --check
ci_run zk lint md --check
unit-tests:
runs-on: [self-hosted, CI-worker]
Expand All @@ -59,7 +62,7 @@ jobs:
ci_run ln -s /usr/src/keys/setup keys/setup
ci_run zk
ci_run zk run verify-keys unpack
ci_run zk contract build-dev
ci_run zk contract build
ci_run zk run deploy-erc20 dev
ci_run zk run deploy-eip1271
ci_run zk db basic-setup
Expand Down Expand Up @@ -148,7 +151,7 @@ jobs:
ci_run ln -s /usr/src/keys/setup keys/setup
ci_run zk
ci_run zk run verify-keys unpack
ci_run zk contract build-dev
ci_run zk contract build
- name: integration-testkit
run: ci_run zk test integration testkit --offline
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions changelog/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ All notable changes to the core components will be documented in this file.

### Changed

- Rejected transactions are now stored in the database for 2 weeks only.

### Added

### Fixed
Expand Down
4 changes: 4 additions & 0 deletions changelog/js-sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ All notable changes to `zksync.js` will be documented in this file.

### Changed

### Deprecated

- WebSocket provider.

### Fixed

## Version 0.8.4
Expand Down
4 changes: 4 additions & 0 deletions changelog/rust-sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to `zksync_rs` will be documented in this file.

## Unreleased

### Added

- Constructor of RpcProvider from address and network.

**Version 0.2.2** is being developed.

### Added
Expand Down
3 changes: 1 addition & 2 deletions contracts/contracts/Verifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ contract Verifier is KeysWithPlonkVerifier, KeysWithPlonkVerifierOld, Config {
uint128 _amount,
uint256[] calldata _proof
) external view returns (bool) {
bytes32 commitment =
sha256(abi.encodePacked(uint256(_rootHash) & INPUT_MASK, _accountId, _owner, _tokenId, _amount));
bytes32 commitment = sha256(abi.encodePacked(_rootHash, _accountId, _owner, _tokenId, _amount));

uint256[] memory inputs = new uint256[](1);
inputs[0] = uint256(commitment) & INPUT_MASK;
Expand Down
2 changes: 1 addition & 1 deletion contracts/contracts/ZkSync.sol
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ contract ZkSync is UpgradeableMaster, Storage, Config, Events, ReentrancyGuard {
bool trigger =
block.number >= priorityRequests[firstPriorityRequestId].expirationBlock &&
priorityRequests[firstPriorityRequestId].expirationBlock != 0;
if (trigger) {
if ($$(EASY_EXODUS) || trigger) {
if (!exodusMode) {
exodusMode = true;
emit ExodusMode();
Expand Down
2 changes: 2 additions & 0 deletions contracts/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const localConfig = Object.assign({}, prodConfig);
// @ts-ignore
localConfig.UPGRADE_NOTICE_PERIOD = 0;
localConfig.DUMMY_VERIFIER = process.env.CONTRACTS_TEST_DUMMY_VERIFIER === 'true';
// @ts-ignore
localConfig.EASY_EXODUS = process.env.CONTRACTS_TEST_EASY_EXODUS === 'true';

const contractDefs = {
rinkeby: testnetConfig,
Expand Down
2 changes: 1 addition & 1 deletion core/bin/key_generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ fn main() {
create_verifier_contract(config);
}
Command::CircuitSize => {
count_gates_recursive_verification_keys();
calculate_and_print_max_zksync_main_circuit_size();
count_gates_recursive_verification_keys();
}
}
}
22 changes: 13 additions & 9 deletions core/bin/zksync_api/src/api_server/rpc_server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use zksync_storage::{
},
ConnectionPool, StorageProcessor,
};
use zksync_types::{tx::TxHash, Address, BatchFee, Fee, TokenLike, TxFeeTypes};
use zksync_types::{tx::TxHash, Address, BatchFee, BlockNumber, Fee, TokenLike, TxFeeTypes};

// Local uses
use crate::{
Expand Down Expand Up @@ -184,20 +184,24 @@ impl RpcApp {
Some(block)
} else {
let mut storage = self.access_storage().await?;
let block = storage
let blocks = storage
.chain()
.block_schema()
.find_block_by_height_or_hash(block_number.to_string())
.await;
.load_block_range(BlockNumber(block_number as u32), 1)
.await
.unwrap_or_default();

if let Some(block) = block.clone() {
if !blocks.is_empty() && blocks[0].block_number == block_number {
// Unverified blocks can still change, so we can't cache them.
if block.verified_at.is_some() && block.block_number == block_number {
self.cache_of_blocks_info.insert(block_number, block);
}
self.cache_of_blocks_info
.insert(block_number, blocks[0].clone());
}

block
if !blocks.is_empty() {
Some(blocks[0].clone())
} else {
None
}
};

metrics::histogram!("api.rpc.get_block_info", start.elapsed());
Expand Down
11 changes: 4 additions & 7 deletions core/bin/zksync_api/src/fee_ticker/ticker_api/coingecko.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,13 @@ impl TokenPriceAPI for CoinGeckoAPI {

// If we use 2 day interval we will get hourly prices and not minute by minute which makes
// response faster and smaller
let request = self
let market_chart = self
.client
.get(market_chart_url)
.query(&[("vs_currency", "usd"), ("days", "2")]);

let api_request_future = tokio::time::timeout(REQUEST_TIMEOUT, request.send());

let market_chart = api_request_future
.timeout(REQUEST_TIMEOUT)
.query(&[("vs_currency", "usd"), ("days", "2")])
.send()
.await
.map_err(|_| anyhow::format_err!("CoinGecko API request timeout"))?
.map_err(|err| anyhow::format_err!("CoinGecko API request failed: {}", err))?
.json::<CoinGeckoMarketChart>()
.await?;
Expand Down
11 changes: 6 additions & 5 deletions core/bin/zksync_api/src/fee_ticker/ticker_api/coinmarkercap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ impl TokenPriceAPI for CoinMarketCapAPI {
))
.expect("failed to join url path");

let api_request_future =
tokio::time::timeout(REQUEST_TIMEOUT, self.client.get(request_url).send());

let mut api_response = api_request_future
let mut api_response = self
.client
.get(request_url)
.timeout(REQUEST_TIMEOUT)
.send()
.await
.map_err(|_| anyhow::format_err!("Coinmarketcap API request timeout"))?
.map_err(|err| anyhow::format_err!("Coinmarketcap API request failed: {}", err))?
.json::<CoinmarketCapResponse>()
.await?;

let mut token_info = api_response
.data
.remove(&TokenLike::Symbol(token_symbol.to_string()))
Expand Down
15 changes: 8 additions & 7 deletions core/bin/zksync_api/src/fee_ticker/validator/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ impl UniswapTokenWatcher {

let query = format!("{{token(id: \"{:#x}\"){{tradeVolumeUSD}}}}", address);

let request = self.client.post(&self.addr).json(&serde_json::json!({
"query": query.clone(),
}));
let api_request_future = tokio::time::timeout(REQUEST_TIMEOUT, request.send());

let response: GraphqlResponse = api_request_future
let response = self
.client
.post(&self.addr)
.json(&serde_json::json!({
"query": query.clone(),
}))
.timeout(REQUEST_TIMEOUT)
.send()
.await
.map_err(|_| anyhow::format_err!("Uniswap API request timeout"))?
.map_err(|err| anyhow::format_err!("Uniswap API request failed: {}", err))?
.json::<GraphqlResponse>()
.await?;
Expand Down
6 changes: 6 additions & 0 deletions core/bin/zksync_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
eth_watch::start_eth_watch,
mempool::run_mempool_tasks,
private_api::start_private_core_api,
rejected_tx_cleaner::run_rejected_tx_cleaner,
state_keeper::{start_state_keeper, ZkSyncStateKeeper},
};
use futures::{channel::mpsc, future};
Expand All @@ -22,6 +23,7 @@ pub mod committer;
pub mod eth_watch;
pub mod mempool;
pub mod private_api;
pub mod rejected_tx_cleaner;
pub mod state_keeper;

/// Waits for *any* of the tokio tasks to be finished.
Expand Down Expand Up @@ -150,6 +152,9 @@ pub async fn run_core(
DEFAULT_CHANNEL_CAPACITY,
);

// Start rejected transactions cleaner task.
let rejected_tx_cleaner_task = run_rejected_tx_cleaner(&config, connection_pool.clone());

// Start block proposer.
let proposer_task = run_block_proposer_task(
&config,
Expand All @@ -171,6 +176,7 @@ pub async fn run_core(
committer_task,
mempool_task,
proposer_task,
rejected_tx_cleaner_task,
];

Ok(task_futures)
Expand Down
37 changes: 37 additions & 0 deletions core/bin/zksync_core/src/rejected_tx_cleaner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//! The cleaner is responsible for removing rejected transactions from the database
//! that were stored 2 or more weeks ago (this value is configurable as well as the actor's sleep time).
//!
//! The purpose is not to store the information about the failed transaction execution
//! which is useful only for a short period of time. Since such transactions are not actually
//! included in the block and don't affect the state hash, there is no much sense to keep
//! them forever.
// External uses
use tokio::{task::JoinHandle, time};

// Workspace deps
use zksync_config::ZkSyncConfig;
use zksync_storage::ConnectionPool;

#[must_use]
pub fn run_rejected_tx_cleaner(config: &ZkSyncConfig, db_pool: ConnectionPool) -> JoinHandle<()> {
let max_age = config.db.rejected_transactions_max_age();
let interval = config.db.rejected_transactions_cleaner_interval();
let mut timer = time::interval(interval);

tokio::spawn(async move {
loop {
let mut storage = db_pool
.access_storage()
.await
.expect("transactions cleaner couldn't access the database");
storage
.chain()
.operations_schema()
.remove_rejected_transactions(max_age)
.await
.expect("failed to delete rejected transactions from the database");
timer.tick().await;
}
})
}
22 changes: 14 additions & 8 deletions core/bin/zksync_core/src/state_keeper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ impl PendingBlock {
}
}

pub fn system_time_timestamp() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("failed to get system time")
.as_secs()
}

/// Responsible for tx processing and block forming.
pub struct ZkSyncStateKeeper {
/// Current plasma state
Expand Down Expand Up @@ -389,10 +396,7 @@ impl ZkSyncStateKeeper {
initial_state.unprocessed_priority_op,
max_block_size,
previous_root_hash,
SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("failed to get system time")
.as_secs(),
system_time_timestamp(),
),
available_block_chunk_sizes,
max_miniblock_iterations,
Expand Down Expand Up @@ -546,6 +550,11 @@ impl ZkSyncStateKeeper {
let start = Instant::now();
let mut executed_ops = Vec::new();

// If pending block is empty we update timestamp
if self.pending_block.success_operations.is_empty() {
self.pending_block.timestamp = system_time_timestamp();
}

// We want to store this variable before moving anything from the pending block.
let empty_proposed_block = proposed_block.is_empty();

Expand Down Expand Up @@ -928,10 +937,7 @@ impl ZkSyncStateKeeper {
.last()
.expect("failed to get max block size"),
H256::default(),
SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("failed to get system time")
.as_secs(),
system_time_timestamp(),
),
);

Expand Down
24 changes: 18 additions & 6 deletions core/bin/zksync_core/src/state_keeper/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ mod apply_priority_op {

mod apply_tx {
use super::*;
use zksync_types::gas_counter::{VerifyCost, TX_GAS_LIMIT};

/// Checks if withdrawal is processed correctly by the state_keeper
#[test]
Expand Down Expand Up @@ -385,12 +386,13 @@ mod apply_tx {
}

/// Checks if processing withdrawal fails because the gas limit is reached.
/// This sends 46 withdrawals (very ineficcient, but all constants in
/// This sends 46 withdrawals (very inefficient, but all constants in
/// GasCounter are hardcoded, so I see no way out)
#[test]
fn gas_limit_reached() {
let withdrawals_number = 46;
let mut tester = StateKeeperTester::new(6 * withdrawals_number, 1, 1);
let withdrawals_number = (TX_GAS_LIMIT - VerifyCost::base_cost().as_u64() * 130 / 100)
/ (VerifyCost::WITHDRAW_COST * 130 / 100);
let mut tester = StateKeeperTester::new(6 * withdrawals_number as usize, 1, 1);
for i in 1..=withdrawals_number {
let withdrawal = create_account_and_withdrawal(
&mut tester,
Expand All @@ -401,10 +403,20 @@ mod apply_tx {
Default::default(),
);
let result = tester.state_keeper.apply_tx(&withdrawal);
if i < withdrawals_number {
assert!(result.is_ok())
if i <= withdrawals_number {
assert!(
result.is_ok(),
"i: {}, withdrawals: {}",
i,
withdrawals_number
)
} else {
assert!(result.is_err())
assert!(
result.is_err(),
"i: {}, withdrawals: {}",
i,
withdrawals_number
)
}
}
}
Expand Down
Loading

0 comments on commit 8eea55e

Please sign in to comment.