Skip to content

Commit

Permalink
fix(API): polish web3 api block-related types (#1994)
Browse files Browse the repository at this point in the history
Follow-up PR for
https://github.com/matter-labs/zksync-era/pull/1946/files
context:
https://github.com/matter-labs/zksync-era/pull/1946/files#r1602029812

## What ❔
+ return correct `gas_limit` and `gas_used`
+ Improve the `ws` unit test to check the full response 

## Why ❔


## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [x] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [x] Tests for the changes have been added / updated.
- [x] Documentation comments have been added / updated.
- [x] Code has been formatted via `zk fmt` and `zk lint`.
- [x] Spellcheck has been run via `zk spellcheck`.
  • Loading branch information
AnastasiiaVashchuk authored May 21, 2024
1 parent d1e1004 commit 6cd3c53
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 72 deletions.

This file was deleted.

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

84 changes: 55 additions & 29 deletions core/lib/dal/src/blocks_web3_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,20 +165,26 @@ impl BlocksWeb3Dal<'_, '_> {
&mut self,
from_block: L2BlockNumber,
) -> DalResult<Vec<BlockHeader>> {
let rows = sqlx::query!(
let blocks_rows: Vec<_> = sqlx::query!(
r#"
SELECT
miniblocks.hash,
miniblocks.number,
miniblocks.hash AS "block_hash",
miniblocks.number AS "block_number",
prev_miniblock.hash AS "parent_hash?",
miniblocks.timestamp
miniblocks.timestamp AS "block_timestamp",
miniblocks.base_fee_per_gas AS "base_fee_per_gas",
miniblocks.gas_limit AS "block_gas_limit?",
transactions.gas_limit AS "transaction_gas_limit?",
transactions.refunded_gas AS "transaction_refunded_gas?"
FROM
miniblocks
LEFT JOIN miniblocks prev_miniblock ON prev_miniblock.number = miniblocks.number - 1
LEFT JOIN transactions ON transactions.miniblock_number = miniblocks.number
WHERE
miniblocks.number > $1
ORDER BY
miniblocks.number ASC
miniblocks.number ASC,
transactions.index_in_block ASC
"#,
i64::from(from_block.0),
)
Expand All @@ -187,30 +193,50 @@ impl BlocksWeb3Dal<'_, '_> {
.fetch_all(self.storage)
.await?;

let blocks = rows.into_iter().map(|row| BlockHeader {
hash: Some(H256::from_slice(&row.hash)),
parent_hash: row
.parent_hash
.as_deref()
.map_or_else(H256::zero, H256::from_slice),
uncles_hash: EMPTY_UNCLES_HASH,
author: H160::zero(),
state_root: H256::zero(),
transactions_root: H256::zero(),
receipts_root: H256::zero(),
number: Some(U64::from(row.number)),
gas_used: U256::zero(),
gas_limit: U256::zero(),
base_fee_per_gas: None,
extra_data: Bytes::default(),
// TODO: include logs
logs_bloom: H2048::default(),
timestamp: U256::from(row.timestamp),
difficulty: U256::zero(),
mix_hash: None,
nonce: None,
});
Ok(blocks.collect())
let mut headers_map = std::collections::HashMap::new();

for row in blocks_rows.iter() {
let entry = headers_map
.entry(row.block_number)
.or_insert_with(|| BlockHeader {
hash: Some(H256::from_slice(&row.block_hash)),
parent_hash: row
.parent_hash
.as_deref()
.map_or_else(H256::zero, H256::from_slice),
uncles_hash: EMPTY_UNCLES_HASH,
author: H160::zero(),
state_root: H256::zero(),
transactions_root: H256::zero(),
receipts_root: H256::zero(),
number: Some(U64::from(row.block_number)),
gas_used: U256::zero(),
gas_limit: (row
.block_gas_limit
.unwrap_or(i64::from(LEGACY_BLOCK_GAS_LIMIT))
as u64)
.into(),
base_fee_per_gas: Some(bigdecimal_to_u256(row.base_fee_per_gas.clone())),
extra_data: Bytes::default(),
logs_bloom: H2048::default(),
timestamp: U256::from(row.block_timestamp),
difficulty: U256::zero(),
mix_hash: None,
nonce: None,
});

if let (Some(gas_limit), Some(refunded_gas)) = (
row.transaction_gas_limit.clone(),
row.transaction_refunded_gas,
) {
entry.gas_used += bigdecimal_to_u256(gas_limit) - U256::from(refunded_gas as u64);
}
}

let mut headers: Vec<BlockHeader> = headers_map.into_values().collect();
headers.sort_by_key(|header| header.number);

Ok(headers)
}

pub async fn resolve_block_id(
Expand Down
1 change: 1 addition & 0 deletions core/lib/eth_client/src/clients/http/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ where
};

latency.observe();

// base_fee_per_gas always exists after London fork
Ok(block.base_fee_per_gas.unwrap())
}
Expand Down
37 changes: 34 additions & 3 deletions core/node/api_server/src/web3/tests/ws.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//! WS-related tests.
use std::collections::HashSet;
use std::{collections::HashSet, str::FromStr};

use assert_matches::assert_matches;
use async_trait::async_trait;
use http::StatusCode;
use tokio::sync::watch;
use zksync_config::configs::chain::NetworkConfig;
use zksync_dal::ConnectionPool;
use zksync_types::{api, Address, L1BatchNumber, H256, U64};
use zksync_types::{api, Address, L1BatchNumber, H160, H2048, H256, U64};
use zksync_web3_decl::{
client::{WsClient, L2},
jsonrpsee::{
Expand All @@ -19,7 +20,7 @@ use zksync_web3_decl::{
rpc_params,
},
namespaces::{EthNamespaceClient, ZksNamespaceClient},
types::{BlockHeader, PubSubFilter},
types::{BlockHeader, Bytes, PubSubFilter},
};

use super::*;
Expand Down Expand Up @@ -290,15 +291,45 @@ impl WsTest for BasicSubscriptionsTest {
.await
.context("Timed out waiting for new block header")?
.context("New blocks subscription terminated")??;

let sha3_uncles_hash =
H256::from_str("0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")
.unwrap();

assert_eq!(
received_block_header.number,
Some(new_l2_block.number.0.into())
);
assert_eq!(received_block_header.hash, Some(new_l2_block.hash));
assert_matches!(received_block_header.parent_hash, H256(_));
assert_eq!(received_block_header.uncles_hash, sha3_uncles_hash);
assert_eq!(received_block_header.author, H160::zero());
assert_eq!(received_block_header.state_root, H256::zero());
assert_eq!(received_block_header.transactions_root, H256::zero());
assert_eq!(received_block_header.receipts_root, H256::zero());
assert_eq!(
received_block_header.number,
Some(U64::from(new_l2_block.number.0))
);
assert_matches!(received_block_header.gas_used, U256(_));
assert_eq!(
received_block_header.gas_limit,
new_l2_block.gas_limit.into()
);
assert_eq!(
received_block_header.base_fee_per_gas,
Some(new_l2_block.base_fee_per_gas.into())
);
assert_eq!(received_block_header.extra_data, Bytes::default());
assert_eq!(received_block_header.logs_bloom, H2048::default());
assert_eq!(
received_block_header.timestamp,
new_l2_block.timestamp.into()
);
assert_eq!(received_block_header.difficulty, U256::zero());
assert_eq!(received_block_header.mix_hash, None);
assert_eq!(received_block_header.nonce, None);

blocks_subscription.unsubscribe().await?;
Ok(())
}
Expand Down

0 comments on commit 6cd3c53

Please sign in to comment.