Skip to content

Commit

Permalink
Merge pull request #727 from blockscout/kf/feat/user-ops-pagination
Browse files Browse the repository at this point in the history
[USER_OPS_INDEXER] blockscout-like pagination
  • Loading branch information
k1rill-fedoseev authored Jan 9, 2024
2 parents ede86f9 + 92057c5 commit e3adf9d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ service UserOpsService {
rpc ListFactories(ListFactoriesRequest) returns (ListFactoriesResponse);
}

message Pagination {
string page_token = 1;
uint32 page_size = 2;
}

message GetAccountRequest {
string address = 1;
}
Expand Down Expand Up @@ -47,7 +52,7 @@ message ListAccountsRequest {

message ListAccountsResponse {
repeated Account items = 1;
optional string next_page_token = 2;
Pagination next_page_params = 2;
}

message ListBundlesRequest {
Expand All @@ -59,7 +64,7 @@ message ListBundlesRequest {

message ListBundlesResponse {
repeated Bundle items = 1;
optional string next_page_token = 2;
Pagination next_page_params = 2;
}

message ListUserOpsRequest {
Expand All @@ -77,7 +82,7 @@ message ListUserOpsRequest {

message ListUserOpsResponse {
repeated ListUserOp items = 1;
optional string next_page_token = 2;
Pagination next_page_params = 2;
}

message ListBundlersRequest {
Expand All @@ -87,7 +92,7 @@ message ListBundlersRequest {

message ListBundlersResponse {
repeated Bundler items = 1;
optional string next_page_token = 2;
Pagination next_page_params = 2;
}

message ListPaymastersRequest {
Expand All @@ -97,7 +102,7 @@ message ListPaymastersRequest {

message ListPaymastersResponse {
repeated Paymaster items = 1;
optional string next_page_token = 2;
Pagination next_page_params = 2;
}

message ListFactoriesRequest {
Expand All @@ -107,7 +112,7 @@ message ListFactoriesRequest {

message ListFactoriesResponse {
repeated Factory items = 1;
optional string next_page_token = 2;
Pagination next_page_params = 2;
}

message Account {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,44 +412,44 @@ definitions:
type: array
items:
$ref: '#/definitions/v1Account'
next_page_token:
type: string
next_page_params:
$ref: '#/definitions/v1Pagination'
v1ListBundlersResponse:
type: object
properties:
items:
type: array
items:
$ref: '#/definitions/v1Bundler'
next_page_token:
type: string
next_page_params:
$ref: '#/definitions/v1Pagination'
v1ListBundlesResponse:
type: object
properties:
items:
type: array
items:
$ref: '#/definitions/v1Bundle'
next_page_token:
type: string
next_page_params:
$ref: '#/definitions/v1Pagination'
v1ListFactoriesResponse:
type: object
properties:
items:
type: array
items:
$ref: '#/definitions/v1Factory'
next_page_token:
type: string
next_page_params:
$ref: '#/definitions/v1Pagination'
v1ListPaymastersResponse:
type: object
properties:
items:
type: array
items:
$ref: '#/definitions/v1Paymaster'
next_page_token:
type: string
next_page_params:
$ref: '#/definitions/v1Pagination'
v1ListUserOp:
type: object
properties:
Expand All @@ -476,8 +476,16 @@ definitions:
type: array
items:
$ref: '#/definitions/v1ListUserOp'
next_page_token:
next_page_params:
$ref: '#/definitions/v1Pagination'
v1Pagination:
type: object
properties:
page_token:
type: string
page_size:
type: integer
format: int64
v1Paymaster:
type: object
properties:
Expand Down
70 changes: 44 additions & 26 deletions user-ops-indexer/user-ops-indexer-server/src/services/user_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use user_ops_indexer_proto::blockscout::user_ops_indexer::v1::{
GetPaymasterRequest, GetUserOpRequest, ListAccountsRequest, ListAccountsResponse,
ListBundlersRequest, ListBundlersResponse, ListBundlesRequest, ListBundlesResponse,
ListFactoriesRequest, ListFactoriesResponse, ListPaymastersRequest, ListPaymastersResponse,
ListUserOpsRequest, ListUserOpsResponse, Paymaster, UserOp,
ListUserOpsRequest, ListUserOpsResponse, Pagination, Paymaster, UserOp,
};

const DEFAULT_PAGE_SIZE: u32 = 10;
const DEFAULT_PAGE_SIZE: u32 = 50;

pub struct UserOpsService {
db: DatabaseConnection,
Expand All @@ -29,9 +29,9 @@ impl UserOpsService {
Self { db, settings }
}

fn normalize_page_size(&self, size: Option<u32>) -> u64 {
fn normalize_page_size(&self, size: Option<u32>) -> u32 {
size.unwrap_or(DEFAULT_PAGE_SIZE)
.clamp(1, self.settings.max_page_size) as u64
.clamp(1, self.settings.max_page_size)
}
}

Expand Down Expand Up @@ -142,17 +142,24 @@ impl UserOps for UserOpsService {
let page_token = inner.page_token.map(parse_filter).transpose()?;
let page_size = self.normalize_page_size(inner.page_size);

let (accounts, next_page_token) =
repository::account::list_accounts(&self.db, factory_filter, page_token, page_size)
.await
.map_err(|err| {
tracing::error!(error = ?err, "failed to query accounts");
Status::internal("failed to query accounts")
})?;
let (accounts, next_page_token) = repository::account::list_accounts(
&self.db,
factory_filter,
page_token,
page_size as u64,
)
.await
.map_err(|err| {
tracing::error!(error = ?err, "failed to query accounts");
Status::internal("failed to query accounts")
})?;

let res = ListAccountsResponse {
items: accounts.into_iter().map(|acc| acc.into()).collect(),
next_page_token: next_page_token.map(|a| to_checksum(&a, None)),
next_page_params: next_page_token.map(|a| Pagination {
page_token: to_checksum(&a, None),
page_size,
}),
};

Ok(Response::new(res))
Expand All @@ -176,7 +183,7 @@ impl UserOps for UserOpsService {
bundler_filter,
entry_point_filter,
page_token,
page_size,
page_size as u64,
)
.await
.map_err(|err| {
Expand All @@ -186,8 +193,10 @@ impl UserOps for UserOpsService {

let res = ListBundlesResponse {
items: bundles.into_iter().map(|b| b.into()).collect(),
next_page_token: next_page_token
.map(|(b, t, i)| format!("{},{},{}", b, t.encode_hex(), i)),
next_page_params: next_page_token.map(|(b, t, i)| Pagination {
page_token: format!("{},{},{}", b, t.encode_hex(), i),
page_size,
}),
};

Ok(Response::new(res))
Expand Down Expand Up @@ -222,7 +231,7 @@ impl UserOps for UserOpsService {
bundle_index_filter,
block_number_filter,
page_token,
page_size,
page_size as u64,
)
.await
.map_err(|err| {
Expand All @@ -232,7 +241,10 @@ impl UserOps for UserOpsService {

let res = ListUserOpsResponse {
items: ops.into_iter().map(|acc| acc.into()).collect(),
next_page_token: next_page_token.map(|(b, o)| format!("{},{}", b, o.encode_hex())),
next_page_params: next_page_token.map(|(b, o)| Pagination {
page_token: format!("{},{}", b, o.encode_hex()),
page_size,
}),
};

Ok(Response::new(res))
Expand All @@ -249,7 +261,7 @@ impl UserOps for UserOpsService {
let page_size = self.normalize_page_size(inner.page_size);

let (bundlers, next_page_token) =
repository::bundler::list_bundlers(&self.db, page_token, page_size)
repository::bundler::list_bundlers(&self.db, page_token, page_size as u64)
.await
.map_err(|err| {
tracing::error!(error = ?err, "failed to query bundlers");
Expand All @@ -258,8 +270,10 @@ impl UserOps for UserOpsService {

let res = ListBundlersResponse {
items: bundlers.into_iter().map(|b| b.into()).collect(),
next_page_token: next_page_token
.map(|(t, f)| format!("{},{}", t, to_checksum(&f, None))),
next_page_params: next_page_token.map(|(t, f)| Pagination {
page_token: format!("{},{}", t, to_checksum(&f, None)),
page_size,
}),
};

Ok(Response::new(res))
Expand All @@ -276,7 +290,7 @@ impl UserOps for UserOpsService {
let page_size = self.normalize_page_size(inner.page_size);

let (paymasters, next_page_token) =
repository::paymaster::list_paymasters(&self.db, page_token, page_size)
repository::paymaster::list_paymasters(&self.db, page_token, page_size as u64)
.await
.map_err(|err| {
tracing::error!(error = ?err, "failed to query paymasters");
Expand All @@ -285,8 +299,10 @@ impl UserOps for UserOpsService {

let res = ListPaymastersResponse {
items: paymasters.into_iter().map(|b| b.into()).collect(),
next_page_token: next_page_token
.map(|(t, f)| format!("{},{}", t, to_checksum(&f, None))),
next_page_params: next_page_token.map(|(t, f)| Pagination {
page_token: format!("{},{}", t, to_checksum(&f, None)),
page_size,
}),
};

Ok(Response::new(res))
Expand All @@ -303,7 +319,7 @@ impl UserOps for UserOpsService {
let page_size = self.normalize_page_size(inner.page_size);

let (factories, next_page_token) =
repository::factory::list_factories(&self.db, page_token, page_size)
repository::factory::list_factories(&self.db, page_token, page_size as u64)
.await
.map_err(|err| {
tracing::error!(error = ?err, "failed to query factories");
Expand All @@ -312,8 +328,10 @@ impl UserOps for UserOpsService {

let res = ListFactoriesResponse {
items: factories.into_iter().map(|b| b.into()).collect(),
next_page_token: next_page_token
.map(|(t, f)| format!("{},{}", t, to_checksum(&f, None))),
next_page_params: next_page_token.map(|(t, f)| Pagination {
page_token: format!("{},{}", t, to_checksum(&f, None)),
page_size,
}),
};

Ok(Response::new(res))
Expand Down

0 comments on commit e3adf9d

Please sign in to comment.