Skip to content

Commit

Permalink
Merge #2183
Browse files Browse the repository at this point in the history
2183: Implement get tx by block and block_index 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 3, 2022
2 parents 8a06713 + 62df0a5 commit 39c2391
Show file tree
Hide file tree
Showing 14 changed files with 613 additions and 318 deletions.
51 changes: 49 additions & 2 deletions core/bin/zksync_api/src/api_server/rest/v02/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use actix_web::{web, Scope};
use zksync_api_types::v02::{
block::{BlockInfo, BlockStatus},
pagination::{parse_query, ApiEither, BlockAndTxHash, Paginated, PaginationQuery},
transaction::{Transaction, TxHashSerializeWrapper},
transaction::{Transaction, TxData, TxHashSerializeWrapper},
};
use zksync_crypto::{convert::FeConvert, Fr};
use zksync_storage::{chain::block::records::StorageBlockDetails, ConnectionPool, QueryResult};
Expand Down Expand Up @@ -126,6 +126,20 @@ impl ApiBlockData {
storage.paginate_checked(&new_query).await
}

async fn tx_data(
&self,
block_number: BlockNumber,
block_index: u64,
) -> Result<Option<TxData>, Error> {
let mut storage = self.pool.access_storage().await.map_err(Error::storage)?;
Ok(storage
.chain()
.operations_ext_schema()
.tx_data_by_block_and_index_api_v02(block_number, block_index)
.await
.map_err(Error::storage)?)
}

async fn get_last_committed_block_number(&self) -> QueryResult<BlockNumber> {
let mut storage = self.pool.access_storage().await?;
storage
Expand Down Expand Up @@ -175,6 +189,15 @@ async fn block_transactions(
data.transaction_page(block_number, query).await.into()
}

async fn transaction_in_block(
data: web::Data<ApiBlockData>,
path: web::Path<(BlockNumber, u64)>,
) -> ApiResult<Option<TxData>> {
let (block_number, block_index) = *path;
let res = api_try!(data.tx_data(block_number, block_index).await);
ApiResult::Ok(res)
}

pub fn api_scope(pool: ConnectionPool, cache: BlockDetailsCache) -> Scope {
let data = ApiBlockData::new(pool, cache);

Expand All @@ -186,6 +209,10 @@ pub fn api_scope(pool: ConnectionPool, cache: BlockDetailsCache) -> Scope {
"{block_position}/transactions",
web::get().to(block_transactions),
)
.route(
"{block_position}/transactions/{block_index}",
web::get().to(transaction_in_block),
)
}

#[cfg(test)]
Expand Down Expand Up @@ -267,7 +294,7 @@ mod tests {
assert_eq!(paginated.pagination.direction, PaginationDirection::Older);
assert_eq!(paginated.pagination.from, tx_hash);

for (tx, expected_tx) in paginated.list.into_iter().zip(expected_txs) {
for (tx, expected_tx) in paginated.list.into_iter().zip(expected_txs.clone()) {
assert_eq!(
tx.tx_hash.to_string().replace("sync-tx:", "0x"),
expected_tx.tx_hash
Expand All @@ -280,6 +307,26 @@ mod tests {
}
}

for expected_tx in expected_txs {
if !expected_tx.success {
continue;
}
let response = client
.transaction_in_block(
expected_tx.block_number as u32,
expected_tx.block_index.unwrap() as u32,
)
.await?;
let tx: Option<TxData> = deserialize_response_result(response)?;
let tx = tx.unwrap().tx;
assert_eq!(tx.created_at, Some(expected_tx.created_at));
assert_eq!(*tx.block_number.unwrap(), expected_tx.block_number as u32);
assert_eq!(tx.fail_reason, expected_tx.fail_reason);
if matches!(tx.op, TransactionData::L2(_)) {
assert_eq!(serde_json::to_value(tx.op).unwrap(), expected_tx.op);
}
}

server.stop().await;
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ impl Paginate<PendingOpsRequest> for StorageProcessor<'_> {
);
Transaction {
tx_hash,
block_index: None,
block_number: None,
op: TransactionData::L1(tx),
status: TxInBlockStatus::Queued,
Expand Down
1 change: 1 addition & 0 deletions core/bin/zksync_api/src/api_server/rest/v02/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ impl ApiTransactionData {
let tx_hash = op.tx_hash();
let tx = Transaction {
tx_hash,
block_index: None,
block_number: None,
op: TransactionData::L1(L1Transaction::from_pending_op(
op.data,
Expand Down
9 changes: 9 additions & 0 deletions core/lib/api_client/src/rest/v02/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ impl Client {
.await
}

pub async fn transaction_in_block(&self, block_number: u32, tx_index: u32) -> Result<Response> {
self.get_with_scope(
super::API_V02_SCOPE,
&format!("blocks/{}/transactions/{}", block_number, tx_index),
)
.send()
.await
}

pub async fn block_transactions(
&self,
pagination_query: &PaginationQuery<ApiEither<TxHash>>,
Expand Down
1 change: 1 addition & 0 deletions core/lib/api_types/src/v02/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub enum Receipt {
pub struct Transaction {
#[serde(serialize_with = "ZeroPrefixHexSerde::serialize")]
pub tx_hash: TxHash,
pub block_index: Option<u32>,
pub block_number: Option<BlockNumber>,
pub op: TransactionData,
pub status: TxInBlockStatus,
Expand Down
Loading

0 comments on commit 39c2391

Please sign in to comment.