Skip to content

Commit

Permalink
[sharded] use internal indexer ledger version in API context
Browse files Browse the repository at this point in the history
  • Loading branch information
areshand committed Jul 2, 2024
1 parent 075f189 commit 8b7dd2d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 4 deletions.
8 changes: 7 additions & 1 deletion api/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ pub struct Account {
limit: Option<u16>,
/// Current ledger info
pub latest_ledger_info: LedgerInfo,
/// Latest ledger version in internal indexer db that could be older than the ledger version
pub latest_internal_indexer_ledger_version: u64,
/// Latest ledger info in internal indexer db that could be older than the ledger info
pub latest_internal_indexer_ledger_info: LedgerInfo,
}

impl Account {
Expand All @@ -207,14 +211,16 @@ impl Account {
.get_latest_ledger_info_and_verify_lookup_version(
requested_ledger_version.map(|inner| inner.0),
)?;

let (latest_internal_indexer_ledger_info, latest_internal_indexer_ledger_version) = context.get_latest_internal_indexer_ledger_version_and_info()?;
Ok(Self {
context,
address,
ledger_version: requested_ledger_version,
start,
limit,
latest_ledger_info,
latest_internal_indexer_ledger_version,
latest_internal_indexer_ledger_info,
})
}

Expand Down
9 changes: 9 additions & 0 deletions api/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ impl Context {
))
}


pub fn get_latest_ledger_info_and_verify_lookup_version<E: StdApiError>(
&self,
requested_ledger_version: Option<Version>,
Expand All @@ -277,6 +278,14 @@ impl Context {
Ok((latest_ledger_info, requested_ledger_version))
}

pub fn get_latest_internal_indexer_ledger_version_and_info(&self) -> Result<(LedgerInfo, Version)> {
let latest_version = self.indexer_reader
.as_ref().map(|e| e.get_latest_internal_indexer_ledger_version()?).unwrap_or(0);
let mut latest_ledger_info = self.get_latest_ledger_info()?;
latest_ledger_info.ledger_version = latest_version;
Ok((latest_ledger_info, latest_version))
}

pub fn get_latest_ledger_info_with_signatures(&self) -> Result<LedgerInfoWithSignatures> {
Ok(self.db.get_latest_ledger_info()?)
}
Expand Down
4 changes: 2 additions & 2 deletions api/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl EventsApi {
let account = Account::new(api.context.clone(), address.0, None, None, None)?;
account.verify_account_or_object_resource()?;
api.list(
account.latest_ledger_info,
account.latest_internal_indexer_ledger_info,
accept_type,
page,
EventKey::new(creation_number.0 .0, address.0.into()),
Expand Down Expand Up @@ -146,7 +146,7 @@ impl EventsApi {
api_spawn_blocking(move || {
let account = Account::new(api.context.clone(), address.0, None, None, None)?;
let key = account.find_event_key(event_handle.0, field_name.0.into())?;
api.list(account.latest_ledger_info, accept_type, page, key)
api.list(account.latest_internal_indexer_ledger_info, accept_type, page, key)
})
.await
}
Expand Down
2 changes: 1 addition & 1 deletion api/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ impl TransactionsApi {
let account = Account::new(self.context.clone(), address, None, None, None)?;
account.get_account_resource()?;

let latest_ledger_info = account.latest_ledger_info;
let latest_ledger_info = account.latest_internal_indexer_ledger_info
// TODO: Return more specific errors from within this function.
let data = self.context.get_account_transactions(
address.into(),
Expand Down
7 changes: 7 additions & 0 deletions storage/indexer/src/indexer_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ impl IndexerReader for IndexerReaders {
anyhow::bail!("Table info reader is not available")
}

fn get_latest_internal_indexer_ledger_version(&self) -> anyhow::Result<Version> {
if let Some(db_indexer) = &self.db_indexer_reader {
db_indexer.get_persisted_version()
}
anyhow::bail!("DB indexer reader is not available")
}

fn get_events(
&self,
event_key: &EventKey,
Expand Down
2 changes: 2 additions & 0 deletions types/src/indexer/indexer_db_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ pub trait IndexerReader: Send + Sync {
cursor: Option<&StateKey>,
version: Version,
) -> Result<Box<dyn Iterator<Item = Result<(StateKey, StateValue)>> + '_>>;

fn get_latest_internal_indexer_ledger_version(&self) -> Result<Version>;
}

0 comments on commit 8b7dd2d

Please sign in to comment.