Skip to content

Commit

Permalink
[sui-indexer] - getEpochs - add flag to return results in descending …
Browse files Browse the repository at this point in the history
…order (MystenLabs#9853)

## Description 

Adds a flag to the `getEpochs` endpoint to allow returning results in
descending order

## Test Plan 

How did you test the new or updated feature?

---
If your changes are not user-facing and not a breaking change, you can
skip the following section. Otherwise, please indicate what changed, and
then add to the Release Notes section as highlighted during the release
process.

### Type of Change (Check all that apply)

- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes
  • Loading branch information
mamos-mysten authored Mar 25, 2023
1 parent 433a1d1 commit a915dea
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 9 deletions.
5 changes: 3 additions & 2 deletions crates/sui-indexer/src/apis/extended_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ impl<S: IndexerStore + Sync + Send + 'static> ExtendedApiServer for ExtendedApi<
&self,
cursor: Option<EpochId>,
limit: Option<usize>,
descending_order: Option<bool>,
) -> RpcResult<EpochPage> {
let limit = validate_limit(limit, QUERY_MAX_RESULT_LIMIT_CHECKPOINTS)?;
let mut epochs = self.state.get_epochs(cursor, limit + 1)?;
let mut epochs = self.state.get_epochs(cursor, limit + 1, descending_order)?;

let has_next_page = epochs.len() > limit;
epochs.truncate(limit);
Expand All @@ -96,7 +97,7 @@ impl<S: IndexerStore + Sync + Send + 'static> ExtendedApiServer for ExtendedApi<
Ok(Page {
data: epochs,
next_cursor,
has_next_page: false,
has_next_page,
})
}

Expand Down
1 change: 1 addition & 0 deletions crates/sui-indexer/src/store/indexer_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ pub trait IndexerStore {
&self,
cursor: Option<EpochId>,
limit: usize,
descending_order: Option<bool>,
) -> Result<Vec<EpochInfo>, IndexerError>;

fn get_current_epoch(&self) -> Result<EpochInfo, IndexerError>;
Expand Down
22 changes: 17 additions & 5 deletions crates/sui-indexer/src/store/pg_indexer_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1247,16 +1247,28 @@ WHERE e1.epoch = e2.epoch
&self,
cursor: Option<EpochId>,
limit: usize,
descending_order: Option<bool>,
) -> Result<Vec<EpochInfo>, IndexerError> {
let id = cursor.map(|id| id as i64).unwrap_or(-1);
let is_descending = descending_order.unwrap_or_default();
let id = cursor
.map(|id| id as i64)
.unwrap_or(if is_descending { i64::MAX } else { -1 });
let mut pg_pool_conn = get_pg_pool_connection(&self.cp)?;
let mut query = epochs_dsl::epochs.into_boxed();
if is_descending {
query = query
.filter(epochs::epoch.lt(id))
.order_by(epochs::epoch.desc());
} else {
query = query
.filter(epochs::epoch.gt(id))
.order_by(epochs::epoch.asc());
}

let epoch_info :Vec<DBEpochInfo> = pg_pool_conn
.build_transaction()
.read_only()
.run(|conn| {
epochs_dsl::epochs.filter(epochs::epoch.gt(id)).order_by(epochs::epoch.asc())
.limit(limit as i64).load(conn)
})
.run(|conn| query.limit(limit as i64).load(conn))
.map_err(|e| {
IndexerError::PostgresReadError(format!(
"Failed reading latest checkpoint sequence number in PostgresDB with error {:?}",
Expand Down
4 changes: 2 additions & 2 deletions crates/sui-indexer/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,14 +789,14 @@ pub mod pg_integration_test {
wait_until_next_checkpoint(&store).await;

let current_epoch = store.get_current_epoch().unwrap();
let epoch_page = store.get_epochs(None, 100).unwrap();
let epoch_page = store.get_epochs(None, 100, None).unwrap();
assert_eq!(0, current_epoch.epoch);
assert!(current_epoch.end_of_epoch_info.is_none());
assert_eq!(1, epoch_page.len());
wait_until_next_epoch(&store).await;

let current_epoch = store.get_current_epoch().unwrap();
let epoch_page = store.get_epochs(None, 100).unwrap();
let epoch_page = store.get_epochs(None, 100, None).unwrap();

assert_eq!(1, current_epoch.epoch);
assert!(current_epoch.end_of_epoch_info.is_none());
Expand Down
2 changes: 2 additions & 0 deletions crates/sui-json-rpc/src/api/extended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub trait ExtendedApi {
cursor: Option<EpochId>,
/// maximum number of items per page
limit: Option<usize>,
/// flag to return results in descending order
descending_order: Option<bool>,
) -> RpcResult<EpochPage>;

/// Return current epoch info
Expand Down
7 changes: 7 additions & 0 deletions crates/sui-open-rpc/spec/openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -1958,6 +1958,13 @@
"format": "uint",
"minimum": 0.0
}
},
{
"name": "descending_order",
"description": "flag to return results in descending order",
"schema": {
"type": "boolean"
}
}
],
"result": {
Expand Down

0 comments on commit a915dea

Please sign in to comment.