Skip to content

Commit

Permalink
Merge #1697
Browse files Browse the repository at this point in the history
1697: Add information about factory to `getNFT` r=slumber a=perekopskiy



Co-authored-by: perekopskiy <[email protected]>
  • Loading branch information
bors-matterlabs-dev[bot] and perekopskiy authored Jun 21, 2021
2 parents 5169366 + f0c11b4 commit 8ab2150
Show file tree
Hide file tree
Showing 17 changed files with 614 additions and 51 deletions.
16 changes: 10 additions & 6 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 @@ -4,7 +4,7 @@ use std::time::Instant;
use bigdecimal::BigDecimal;
use jsonrpc_core::{Error, Result};
// Workspace uses
use zksync_api_client::rest::v1::accounts::NFT;
use zksync_api_client::rest::v1::accounts::ApiNFT;
use zksync_types::{
tx::{EthBatchSignatures, TxEthSignatureVariant, TxHash},
Address, BatchFee, Fee, Token, TokenId, TokenLike, TxFeeTypes, ZkSyncTx,
Expand Down Expand Up @@ -154,13 +154,17 @@ impl RpcApp {
})
}

pub async fn _impl_get_nft(self, id: TokenId) -> Result<Option<NFT>> {
pub async fn _impl_get_nft(self, id: TokenId) -> Result<Option<ApiNFT>> {
let start = Instant::now();
let mut storage = self.access_storage().await?;
let result = storage.tokens_schema().get_nft(id).await.map_err(|err| {
vlog::warn!("Internal Server Error: '{}'; input: N/A", err);
Error::internal_error()
})?;
let result = storage
.tokens_schema()
.get_nft_with_factories(id)
.await
.map_err(|err| {
vlog::warn!("Internal Server Error: '{}'; input: N/A", err);
Error::internal_error()
})?;

metrics::histogram!("api.rpc.get_nft", start.elapsed());
Ok(result.map(|nft| nft.into()))
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 @@ -6,7 +6,7 @@ use jsonrpc_core::Error;
use jsonrpc_derive::rpc;

// Workspace uses
use zksync_api_client::rest::v1::accounts::NFT;
use zksync_api_client::rest::v1::accounts::ApiNFT;
use zksync_crypto::params::ZKSYNC_VERSION;
use zksync_types::{
tx::{EthBatchSignatures, TxEthSignatureVariant, TxHash},
Expand Down Expand Up @@ -81,8 +81,8 @@ pub trait Rpc {
#[rpc(name = "get_zksync_version", returns = "String")]
fn get_zksync_version(&self) -> Result<String, Error>;

#[rpc(name = "get_nft", returns = "Option<NFT>")]
fn get_nft(&self, id: TokenId) -> FutureResp<Option<NFT>>;
#[rpc(name = "get_nft", returns = "Option<ApiNFT>")]
fn get_nft(&self, id: TokenId) -> FutureResp<Option<ApiNFT>>;
}

impl Rpc for RpcApp {
Expand Down Expand Up @@ -233,7 +233,7 @@ impl Rpc for RpcApp {
Ok(String::from(ZKSYNC_VERSION))
}

fn get_nft(&self, id: TokenId) -> FutureResp<Option<NFT>> {
fn get_nft(&self, id: TokenId) -> FutureResp<Option<ApiNFT>> {
let handle = self.runtime_handle.clone();
let self_ = self.clone();
let resp = async move { handle.spawn(self_._impl_get_nft(id)).await.unwrap() };
Expand Down
24 changes: 14 additions & 10 deletions core/bin/zksync_core/src/committer/aggregated_committer.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
use chrono::{DateTime, Utc};
use std::cmp::max;
use std::time::Duration;
use std::{cmp::max, time::Duration};
use zksync_config::ZkSyncConfig;
use zksync_crypto::proof::AggregatedProof;
use zksync_storage::chain::block::BlockSchema;
use zksync_storage::chain::operations::OperationsSchema;
use zksync_storage::prover::ProverSchema;
use zksync_storage::StorageProcessor;
use zksync_types::aggregated_operations::{
AggregatedActionType, AggregatedOperation, BlocksCommitOperation, BlocksCreateProofOperation,
BlocksExecuteOperation, BlocksProofOperation,
use zksync_storage::{
chain::{block::BlockSchema, operations::OperationsSchema},
prover::ProverSchema,
StorageProcessor,
};
use zksync_types::{
aggregated_operations::{
AggregatedActionType, AggregatedOperation, BlocksCommitOperation,
BlocksCreateProofOperation, BlocksExecuteOperation, BlocksProofOperation,
},
block::Block,
gas_counter::GasCounter,
BlockNumber, U256,
};
use zksync_types::{block::Block, gas_counter::GasCounter, BlockNumber, U256};

fn create_new_commit_operation(
last_committed_block: &Block,
Expand Down
31 changes: 31 additions & 0 deletions core/lib/api_client/src/rest/v1/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,37 @@ impl From<zksync_types::NFT> for NFT {
}
}
}

#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
pub struct ApiNFT {
id: TokenId,
content_hash: H256,
creator_id: AccountId,
creator_address: Address,
serial_id: u32,
address: Address,
symbol: String,
current_factory: Address,
withdrawn_factory: Option<Address>,
}

impl From<zksync_types::tokens::ApiNFT> for ApiNFT {
fn from(val: zksync_types::tokens::ApiNFT) -> Self {
Self {
id: val.id,
content_hash: val.content_hash,
creator_id: val.creator_id,
creator_address: val.creator_address,
serial_id: val.serial_id,
address: val.address,
symbol: val.symbol,
current_factory: val.current_factory,
withdrawn_factory: val.withdrawn_factory,
}
}
}

/// Account state at the time of the zkSync block commit or verification.
/// This means that each account has various states.
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Default)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS withdrawn_nfts_factories;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE withdrawn_nfts_factories
(
token_id INT PRIMARY KEY,
factory_address TEXT NOT NULL
);
193 changes: 193 additions & 0 deletions core/lib/storage/sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -2387,6 +2387,20 @@
]
}
},
"6e99e599875e2567cc2f280fda3dbb0ae6a13fcc065dca1f6e96204bd14c27d1": {
"query": "INSERT INTO server_config (contract_addr, gov_contract_addr, nft_factory_addr) VALUES ($1, $2, $3)",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Text",
"Text",
"Text"
]
},
"nullable": []
}
},
"714d10cb76076a8c10d147a14bfda609e7d809186b602406b671d4dd79a0ca8e": {
"query": "SELECT * FROM accounts",
"describe": {
Expand Down Expand Up @@ -3464,6 +3478,18 @@
"nullable": []
}
},
"a35474b8ed25c6265defe4e7621f11eae0deedc08a9cbc9780773c5be5697fcc": {
"query": "\n INSERT INTO withdrawn_nfts_factories (token_id, factory_address)\n SELECT token_id, \n COALESCE(nft_factory.factory_address, server_config.nft_factory_addr) as factory_address\n FROM nft\n INNER JOIN server_config ON server_config.id = true\n LEFT JOIN nft_factory ON nft_factory.creator_id = nft.creator_account_id\n WHERE nft.token_id = ANY($1)\n ",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Int4Array"
]
},
"nullable": []
}
},
"a4969ac155106f1d8dd9b305e71ce36b3ee39adf75574d40e123a617a502ffe4": {
"query": "INSERT INTO executed_transactions (block_number, block_index, tx, operation, tx_hash, from_account, to_account, success, fail_reason, primary_account_address, nonce, created_at, eth_sign_data, batch_id)\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)\n ON CONFLICT (tx_hash)\n DO NOTHING",
"describe": {
Expand Down Expand Up @@ -3528,6 +3554,74 @@
"nullable": []
}
},
"aafe4eaa64fd1b3ab1205f64329460b9a5f354e41c4ddc8a1f39f4661e7f9040": {
"query": "\n SELECT nft.*, tokens.symbol, withdrawn_nfts_factories.factory_address as \"withdrawn_factory?\",\n COALESCE(nft_factory.factory_address, server_config.nft_factory_addr) as \"current_factory!\"\n FROM nft\n INNER JOIN server_config\n ON server_config.id = true\n INNER JOIN tokens\n ON tokens.id = nft.token_id\n LEFT JOIN nft_factory\n ON nft_factory.creator_id = nft.creator_account_id\n LEFT JOIN withdrawn_nfts_factories\n ON withdrawn_nfts_factories.token_id = nft.token_id\n WHERE nft.token_id = $1\n LIMIT 1\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "token_id",
"type_info": "Int4"
},
{
"ordinal": 1,
"name": "creator_account_id",
"type_info": "Int4"
},
{
"ordinal": 2,
"name": "creator_address",
"type_info": "Bytea"
},
{
"ordinal": 3,
"name": "serial_id",
"type_info": "Int4"
},
{
"ordinal": 4,
"name": "address",
"type_info": "Bytea"
},
{
"ordinal": 5,
"name": "content_hash",
"type_info": "Bytea"
},
{
"ordinal": 6,
"name": "symbol",
"type_info": "Text"
},
{
"ordinal": 7,
"name": "withdrawn_factory?",
"type_info": "Text"
},
{
"ordinal": 8,
"name": "current_factory!",
"type_info": "Text"
}
],
"parameters": {
"Left": [
"Int4"
]
},
"nullable": [
false,
false,
false,
false,
false,
false,
false,
false,
null
]
}
},
"b1c528c67d3c2ecea86e3ba1b2407cb4ee72149d66be0498be1c1162917c065d": {
"query": "INSERT INTO block_witness (block, witness)\n VALUES ($1, $2)\n ON CONFLICT (block)\n DO NOTHING",
"describe": {
Expand Down Expand Up @@ -5080,6 +5174,105 @@
"nullable": []
}
},
"e56b7f4f240fe2ad368efb9cd845b0e7bca4a3c8f2f91b00a120a3e6dfc91a6e": {
"query": "SELECT * FROM executed_transactions WHERE block_number BETWEEN $1 AND $2 AND success = true",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "block_number",
"type_info": "Int8"
},
{
"ordinal": 1,
"name": "block_index",
"type_info": "Int4"
},
{
"ordinal": 2,
"name": "tx",
"type_info": "Jsonb"
},
{
"ordinal": 3,
"name": "operation",
"type_info": "Jsonb"
},
{
"ordinal": 4,
"name": "tx_hash",
"type_info": "Bytea"
},
{
"ordinal": 5,
"name": "from_account",
"type_info": "Bytea"
},
{
"ordinal": 6,
"name": "to_account",
"type_info": "Bytea"
},
{
"ordinal": 7,
"name": "success",
"type_info": "Bool"
},
{
"ordinal": 8,
"name": "fail_reason",
"type_info": "Text"
},
{
"ordinal": 9,
"name": "primary_account_address",
"type_info": "Bytea"
},
{
"ordinal": 10,
"name": "nonce",
"type_info": "Int8"
},
{
"ordinal": 11,
"name": "created_at",
"type_info": "Timestamptz"
},
{
"ordinal": 12,
"name": "eth_sign_data",
"type_info": "Jsonb"
},
{
"ordinal": 13,
"name": "batch_id",
"type_info": "Int8"
}
],
"parameters": {
"Left": [
"Int8",
"Int8"
]
},
"nullable": [
false,
true,
false,
false,
false,
false,
true,
false,
true,
false,
false,
false,
true,
true
]
}
},
"e7b1a3e830945cfe5c876255bbaa97dae409e1f642539ec898fd5dc3bb991bfc": {
"query": "\n WITH aggr_comm AS (\n SELECT \n aggregate_operations.created_at, \n eth_operations.final_hash, \n commit_aggregated_blocks_binding.block_number \n FROM aggregate_operations\n INNER JOIN commit_aggregated_blocks_binding ON aggregate_operations.id = commit_aggregated_blocks_binding.op_id\n INNER JOIN eth_aggregated_ops_binding ON aggregate_operations.id = eth_aggregated_ops_binding.op_id\n INNER JOIN eth_operations ON eth_operations.id = eth_aggregated_ops_binding.eth_op_id\n WHERE aggregate_operations.confirmed = true \n )\n ,aggr_exec as (\n SELECT \n aggregate_operations.created_at, \n eth_operations.final_hash, \n execute_aggregated_blocks_binding.block_number \n FROM aggregate_operations\n INNER JOIN execute_aggregated_blocks_binding ON aggregate_operations.id = execute_aggregated_blocks_binding.op_id\n INNER JOIN eth_aggregated_ops_binding ON aggregate_operations.id = eth_aggregated_ops_binding.op_id\n INNER JOIN eth_operations ON eth_operations.id = eth_aggregated_ops_binding.eth_op_id\n WHERE aggregate_operations.confirmed = true \n )\n SELECT\n blocks.number AS \"block_number!\",\n blocks.root_hash AS \"new_state_root!\",\n blocks.block_size AS \"block_size!\",\n committed.final_hash AS \"commit_tx_hash?\",\n verified.final_hash AS \"verify_tx_hash?\",\n committed.created_at AS \"committed_at!\",\n verified.created_at AS \"verified_at?\"\n FROM blocks\n INNER JOIN aggr_comm committed ON blocks.number = committed.block_number\n LEFT JOIN aggr_exec verified ON blocks.number = verified.block_number\n WHERE false\n OR committed.final_hash = $1\n OR verified.final_hash = $1\n OR blocks.root_hash = $1\n OR blocks.number = $2\n ORDER BY blocks.number DESC\n LIMIT 1;\n ",
"describe": {
Expand Down
Loading

0 comments on commit 8ab2150

Please sign in to comment.