Skip to content

Commit 78914fc

Browse files
committed
Use Either
1 parent e9e7603 commit 78914fc

File tree

21 files changed

+194
-143
lines changed

21 files changed

+194
-143
lines changed

Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/bin/zksync_api/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ metrics = "=0.13.0-alpha.8"
7272
lru-cache = "0.1.2"
7373
once_cell = "1.4"
7474
regex = "1"
75+
either = "1.6.1"
7576

7677
[dev-dependencies]
7778
zksync_test_account = { path = "../../tests/test_account" }

core/bin/zksync_api/src/api_server/rest/v02/account.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ use actix_web::{web, Scope};
1111
use zksync_api_types::v02::{
1212
account::{Account, AccountAddressOrId, AccountStateType},
1313
pagination::{
14-
parse_query, AccountTxsRequestWithLatest, IdOrLatest, Paginated, PaginationQuery,
15-
PendingOpsRequest,
14+
parse_query, AccountTxsRequest, ApiEither, Paginated, PaginationQuery, PendingOpsRequest,
1615
},
1716
transaction::{Transaction, TxHashSerializeWrapper},
1817
};
@@ -169,12 +168,12 @@ impl ApiAccountData {
169168

170169
async fn account_txs(
171170
&self,
172-
query: PaginationQuery<IdOrLatest<TxHash>>,
171+
query: PaginationQuery<ApiEither<TxHash>>,
173172
address: Address,
174173
) -> Result<Paginated<Transaction, TxHashSerializeWrapper>, Error> {
175174
let mut storage = self.pool.access_storage().await.map_err(Error::storage)?;
176175
let new_query = PaginationQuery {
177-
from: AccountTxsRequestWithLatest {
176+
from: AccountTxsRequest {
178177
address,
179178
tx_hash: query.from,
180179
},
@@ -190,7 +189,7 @@ impl ApiAccountData {
190189
/// but we can still find pending deposits for its address that is why account_id is Option.
191190
async fn account_pending_txs(
192191
&self,
193-
query: PaginationQuery<IdOrLatest<SerialId>>,
192+
query: PaginationQuery<ApiEither<SerialId>>,
194193
address: Address,
195194
account_id: Option<AccountId>,
196195
) -> Result<Paginated<Transaction, SerialId>, Error> {
@@ -243,9 +242,7 @@ async fn account_txs(
243242
web::Path(account_id_or_address): web::Path<String>,
244243
web::Query(query): web::Query<PaginationQuery<String>>,
245244
) -> ApiResult<Paginated<Transaction, TxHashSerializeWrapper>> {
246-
let query =
247-
api_try!(parse_query(query)
248-
.ok_or_else(|| Error::from(InvalidDataError::QueryDeserializationError)));
245+
let query = api_try!(parse_query(query).map_err(Error::from));
249246
let address_or_id = api_try!(data.parse_account_id_or_address(&account_id_or_address));
250247
let address = api_try!(data.get_address_by_address_or_id(address_or_id).await);
251248
data.account_txs(query, address).await.into()
@@ -256,9 +253,7 @@ async fn account_pending_txs(
256253
web::Path(account_id_or_address): web::Path<String>,
257254
web::Query(query): web::Query<PaginationQuery<String>>,
258255
) -> ApiResult<Paginated<Transaction, SerialId>> {
259-
let query =
260-
api_try!(parse_query(query)
261-
.ok_or_else(|| Error::from(InvalidDataError::QueryDeserializationError)));
256+
let query = api_try!(parse_query(query).map_err(Error::from));
262257
let address_or_id = api_try!(data.parse_account_id_or_address(&account_id_or_address));
263258
let address = api_try!(
264259
data.get_address_by_address_or_id(address_or_id.clone())
@@ -438,7 +433,7 @@ mod tests {
438433
from: PendingOpsRequest {
439434
address: Address::default(),
440435
account_id: Some(AccountId::default()),
441-
serial_id: IdOrLatest::Id(0),
436+
serial_id: ApiEither::from(0),
442437
},
443438
limit: 0,
444439
direction: PaginationDirection::Newer,
@@ -477,7 +472,7 @@ mod tests {
477472
assert_eq!(account_info_by_id, account_info_by_address);
478473

479474
let query = PaginationQuery {
480-
from: IdOrLatest::Id(tx_hash),
475+
from: ApiEither::from(tx_hash),
481476
limit: 1,
482477
direction: PaginationDirection::Newer,
483478
};
@@ -515,7 +510,7 @@ mod tests {
515510
});
516511

517512
let query = PaginationQuery {
518-
from: IdOrLatest::Id(1),
513+
from: ApiEither::from(1),
519514
limit: 1,
520515
direction: PaginationDirection::Newer,
521516
};

core/bin/zksync_api/src/api_server/rest/v02/block.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use actix_web::{web, Scope};
99
// Workspace uses
1010
use zksync_api_types::v02::{
1111
block::{BlockInfo, BlockStatus},
12-
pagination::{parse_query, BlockAndTxHashOrLatest, IdOrLatest, Paginated, PaginationQuery},
12+
pagination::{parse_query, ApiEither, BlockAndTxHash, Paginated, PaginationQuery},
1313
transaction::{Transaction, TxHashSerializeWrapper},
1414
};
1515
use zksync_crypto::{convert::FeConvert, Fr};
@@ -25,7 +25,7 @@ use super::{
2525
use crate::{api_try, utils::block_details_cache::BlockDetailsCache};
2626

2727
pub fn block_info_from_details(details: StorageBlockDetails) -> BlockInfo {
28-
let status = if details.verified_at.is_some() {
28+
let status = if details.is_verified() {
2929
BlockStatus::Finalized
3030
} else {
3131
BlockStatus::Committed
@@ -101,7 +101,7 @@ impl ApiBlockData {
101101

102102
async fn block_page(
103103
&self,
104-
query: PaginationQuery<IdOrLatest<BlockNumber>>,
104+
query: PaginationQuery<ApiEither<BlockNumber>>,
105105
) -> Result<Paginated<BlockInfo, BlockNumber>, Error> {
106106
let mut storage = self.pool.access_storage().await.map_err(Error::storage)?;
107107
storage.paginate_checked(&query).await
@@ -110,12 +110,12 @@ impl ApiBlockData {
110110
async fn transaction_page(
111111
&self,
112112
block_number: BlockNumber,
113-
query: PaginationQuery<IdOrLatest<TxHash>>,
113+
query: PaginationQuery<ApiEither<TxHash>>,
114114
) -> Result<Paginated<Transaction, TxHashSerializeWrapper>, Error> {
115115
let mut storage = self.pool.access_storage().await.map_err(Error::storage)?;
116116

117117
let new_query = PaginationQuery {
118-
from: BlockAndTxHashOrLatest {
118+
from: BlockAndTxHash {
119119
block_number,
120120
tx_hash: query.from,
121121
},
@@ -151,9 +151,7 @@ async fn block_pagination(
151151
data: web::Data<ApiBlockData>,
152152
web::Query(query): web::Query<PaginationQuery<String>>,
153153
) -> ApiResult<Paginated<BlockInfo, BlockNumber>> {
154-
let query =
155-
api_try!(parse_query(query)
156-
.ok_or_else(|| Error::from(InvalidDataError::QueryDeserializationError)));
154+
let query = api_try!(parse_query(query).map_err(Error::from));
157155
data.block_page(query).await.into()
158156
}
159157

@@ -174,9 +172,7 @@ async fn block_transactions(
174172
web::Query(query): web::Query<PaginationQuery<String>>,
175173
) -> ApiResult<Paginated<Transaction, TxHashSerializeWrapper>> {
176174
let block_number = api_try!(data.get_block_number_by_position(&block_position).await);
177-
let query =
178-
api_try!(parse_query(query)
179-
.ok_or_else(|| Error::from(InvalidDataError::QueryDeserializationError)));
175+
let query = api_try!(parse_query(query).map_err(Error::from));
180176

181177
data.transaction_page(block_number, query).await.into()
182178
}
@@ -224,7 +220,7 @@ mod tests {
224220
);
225221

226222
let query = PaginationQuery {
227-
from: IdOrLatest::Id(BlockNumber(1)),
223+
from: ApiEither::from(BlockNumber(1)),
228224
limit: 3,
229225
direction: PaginationDirection::Newer,
230226
};
@@ -258,7 +254,7 @@ mod tests {
258254
let tx_hash = TxHash::from_str(tx_hash_str).unwrap();
259255

260256
let query = PaginationQuery {
261-
from: IdOrLatest::Id(tx_hash),
257+
from: ApiEither::from(tx_hash),
262258
limit: 2,
263259
direction: PaginationDirection::Older,
264260
};

core/bin/zksync_api/src/api_server/rest/v02/error.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use serde_repr::Serialize_repr;
77
use thiserror::Error;
88

99
// Workspace uses
10-
use zksync_api_types::v02::pagination::MAX_LIMIT;
10+
use zksync_api_types::v02::pagination::{UnknownFromParamater, MAX_LIMIT};
1111

1212
// Local uses
1313
use crate::{api_server::tx_sender::SubmitError, fee_ticker::PriceError};
@@ -96,10 +96,8 @@ pub enum InvalidDataError {
9696
InvalidCurrency,
9797
#[error("Transaction is not found")]
9898
TransactionNotFound,
99-
#[error("Limit for pagination should be less or equal than {}", MAX_LIMIT)]
99+
#[error("Limit for pagination should be less than or equal to {}", MAX_LIMIT)]
100100
PaginationLimitTooBig,
101-
#[error("Cannot parse query parameters")]
102-
QueryDeserializationError,
103101
}
104102

105103
impl ApiError for InvalidDataError {
@@ -116,7 +114,6 @@ impl ApiError for InvalidDataError {
116114
Self::InvalidCurrency => ErrorCode::InvalidCurrency,
117115
Self::TransactionNotFound => ErrorCode::TransactionNotFound,
118116
Self::PaginationLimitTooBig => ErrorCode::PaginationLimitTooBig,
119-
Self::QueryDeserializationError => ErrorCode::QueryDeserializationError,
120117
}
121118
}
122119
}
@@ -204,3 +201,13 @@ impl ApiError for PriceError {
204201
}
205202
}
206203
}
204+
205+
impl ApiError for UnknownFromParamater {
206+
fn error_type(&self) -> String {
207+
String::from("invalidDataError")
208+
}
209+
210+
fn code(&self) -> ErrorCode {
211+
ErrorCode::QueryDeserializationError
212+
}
213+
}

0 commit comments

Comments
 (0)