Skip to content

Commit

Permalink
Merge pull request #1777 from matter-labs/perekopskiy-zks-753-impleme…
Browse files Browse the repository at this point in the history
…nt-web3-methods-with-logs

Implement web3 methods with logs
  • Loading branch information
perekopskiy authored Aug 13, 2021
2 parents f68a00b + 47664bb commit af92b45
Show file tree
Hide file tree
Showing 42 changed files with 4,325 additions and 438 deletions.
190 changes: 132 additions & 58 deletions Cargo.lock

Large diffs are not rendered by default.

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

# Server micro-services
"core/bin/zksync_api",
Expand Down
4 changes: 2 additions & 2 deletions core/bin/data_restore/src/inmemory_storage_interactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ impl InMemoryStorageInteractor {
account.nonce = max(account.nonce, *new_nonce);
account.pub_key_hash = *new_pub_key_hash;
}
AccountUpdate::MintNFT { ref token } => {
AccountUpdate::MintNFT { ref token, .. } => {
self.tokens.insert(
token.id,
Token {
Expand All @@ -280,7 +280,7 @@ impl InMemoryStorageInteractor {
},
);
}
AccountUpdate::RemoveNFT { ref token } => {
AccountUpdate::RemoveNFT { ref token, .. } => {
self.tokens.remove(&token.id);
}
}
Expand Down
19 changes: 19 additions & 0 deletions core/bin/mint_nft_nonce_migration/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "mint_nft_nonce_migration"
version = "1.0.0"
edition = "2018"
authors = ["The Matter Labs Team <[email protected]>"]
homepage = "https://zksync.io/"
repository = "https://github.com/matter-labs/zksync"
license = "Apache-2.0"
keywords = ["blockchain", "zksync"]
categories = ["cryptography"]
publish = false # We don't want to publish our binaries.

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
zksync_storage = { path = "../../lib/storage", version = "1.0" }
zksync_config = { path = "../../lib/config", version = "1.0" }
tokio = { version = "0.2", features = ["full"] }
anyhow = "1.0"
13 changes: 13 additions & 0 deletions core/bin/mint_nft_nonce_migration/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use zksync_storage::StorageProcessor;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let mut storage = StorageProcessor::establish_connection().await?;
storage
.chain()
.state_schema()
.mint_nft_updates_set_nonces()
.await?;

Ok(())
}
42 changes: 36 additions & 6 deletions core/bin/zksync_api/src/api_server/rest/v02/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use tokio::sync::Mutex;
use zksync_api_client::rest::client::Client;
use zksync_api_types::v02::Response;
use zksync_config::ZkSyncConfig;
use zksync_crypto::rand::{SeedableRng, XorShiftRng};
use zksync_crypto::rand::{Rng, SeedableRng, XorShiftRng};
use zksync_storage::{
chain::operations::records::NewExecutedPriorityOperation,
chain::operations::OperationsSchema,
Expand All @@ -37,9 +37,10 @@ use zksync_types::{
operations::{ChangePubKeyOp, TransferToNewOp},
prover::ProverJobType,
tx::ChangePubKeyType,
AccountId, AccountMap, Address, BatchFee, BlockNumber, Deposit, DepositOp, ExecutedOperations,
ExecutedPriorityOp, ExecutedTx, Fee, FullExit, FullExitOp, MintNFTOp, Nonce, OutputFeeType,
PriorityOp, Token, TokenId, TokenLike, Transfer, TransferOp, ZkSyncOp, ZkSyncTx, H256,
AccountId, AccountMap, AccountUpdate, Address, BatchFee, BlockNumber, Deposit, DepositOp,
ExecutedOperations, ExecutedPriorityOp, ExecutedTx, Fee, FullExit, FullExitOp, MintNFTOp,
Nonce, OutputFeeType, PriorityOp, Token, TokenId, TokenLike, Transfer, TransferOp, ZkSyncOp,
ZkSyncTx, H256, NFT,
};

// Local uses
Expand Down Expand Up @@ -395,6 +396,7 @@ impl TestServerConfig {
*account_id,
account,
block_number.0 * accounts.len() as u32 + id as u32,
&mut rng,
));
});
apply_updates(&mut accounts, updates.clone());
Expand All @@ -412,6 +414,34 @@ impl TestServerConfig {
vec![]
};

let mut mint_nft_updates = Vec::new();
for (i, tx) in txs.iter().enumerate() {
if let Some(tx) = tx.get_executed_tx() {
if let ZkSyncTx::MintNFT(tx) = &tx.signed_tx.tx {
let nft_address: Address = rng.gen::<[u8; 20]>().into();
let content_hash: H256 = rng.gen::<[u8; 32]>().into();
let token = NFT::new(
TokenId(80000 + block_number.0 * 100 + i as u32),
0,
tx.creator_id,
tx.creator_address,
nft_address,
None,
content_hash,
);
let update = (
tx.creator_id,
AccountUpdate::MintNFT {
token,
nonce: Nonce(0),
},
);
mint_nft_updates.push(update);
}
}
}
updates.extend(mint_nft_updates);

storage
.chain()
.block_schema()
Expand Down Expand Up @@ -608,7 +638,7 @@ impl TestServerConfig {
block_number: 2,
block_index: 2,
operation: serde_json::to_value(
dummy_deposit_op(Address::default(), AccountId(1), VERIFIED_OP_SERIAL_ID, 2).op,
dummy_deposit_op(Address::default(), AccountId(3), VERIFIED_OP_SERIAL_ID, 2).op,
)
.unwrap(),
from_account: Default::default(),
Expand All @@ -630,7 +660,7 @@ impl TestServerConfig {
block_number: EXECUTED_BLOCKS_COUNT as i64 + 1,
block_index: 1,
operation: serde_json::to_value(
dummy_full_exit_op(AccountId(1), Address::default(), COMMITTED_OP_SERIAL_ID, 3)
dummy_full_exit_op(AccountId(3), Address::default(), COMMITTED_OP_SERIAL_ID, 3)
.op,
)
.unwrap(),
Expand Down
102 changes: 102 additions & 0 deletions core/bin/zksync_api/src/api_server/web3/converter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Built-in uses
use std::convert::TryInto;
// External uses
use jsonrpc_core::{Error, Result};
use num::BigUint;
// Workspace uses
use zksync_storage::StorageProcessor;
// Local uses
use super::types::{BlockNumber, Bytes, CommonLogData, Log, Transaction, TxData, H160, H256, U256};

pub fn u256_from_biguint(number: BigUint) -> U256 {
U256::from_dec_str(&number.to_string()).unwrap()
}

pub async fn resolve_block_number(
storage: &mut StorageProcessor<'_>,
number: Option<BlockNumber>,
) -> Result<Option<zksync_types::BlockNumber>> {
let last_saved_block = storage
.chain()
.block_schema()
.get_last_saved_block()
.await
.map_err(|_| Error::internal_error())?;

let number = match number {
Some(number) => number,
None => {
return Ok(Some(last_saved_block));
}
};

let number = match number {
BlockNumber::Earliest => zksync_types::BlockNumber(0),
BlockNumber::Committed => storage
.chain()
.block_schema()
.get_last_committed_confirmed_block()
.await
.map_err(|_| Error::internal_error())?,
BlockNumber::Finalized => storage
.chain()
.block_schema()
.get_last_verified_confirmed_block()
.await
.map_err(|_| Error::internal_error())?,
BlockNumber::Latest | BlockNumber::Pending => last_saved_block,
BlockNumber::Number(number) => {
if number.as_u64() > last_saved_block.0 as u64 {
return Ok(None);
}
// Unwrap can be safely used because `number` is not greater than `last_saved_block`
// which is `u32` variable.
zksync_types::BlockNumber(number.as_u64().try_into().unwrap())
}
};
Ok(Some(number))
}

pub fn transaction_from_tx_data(tx: TxData) -> Transaction {
Transaction {
hash: tx.tx_hash,
nonce: tx.nonce.into(),
block_hash: Some(tx.block_hash),
block_number: Some(tx.block_number.into()),
transaction_index: tx.block_index.map(Into::into),
from: Some(tx.from),
to: tx.to,
value: 0.into(),
gas_price: 0.into(),
gas: 0.into(),
input: Vec::new().into(),
v: None,
r: None,
s: None,
raw: None,
transaction_type: None,
access_list: None,
}
}

pub fn log(
address: H160,
topic: H256,
data: Bytes,
common_data: CommonLogData,
transaction_log_index: U256,
) -> Log {
Log {
address,
topics: vec![topic],
data,
block_hash: common_data.block_hash,
block_number: common_data.block_number,
transaction_hash: Some(common_data.transaction_hash),
transaction_index: common_data.transaction_index,
log_index: None,
transaction_log_index: Some(transaction_log_index),
log_type: None,
removed: Some(false),
}
}
Loading

0 comments on commit af92b45

Please sign in to comment.