Skip to content

Commit

Permalink
use storage ledgerinfo for txn APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
areshand committed Sep 20, 2024
1 parent 1c65af5 commit efe0365
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 13 deletions.
3 changes: 2 additions & 1 deletion api/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,9 +863,10 @@ impl Context {
pub fn get_transaction_by_hash(
&self,
hash: HashValue,
ledger_version: u64,
) -> Result<Option<TransactionOnChainData>> {
self.db
.get_transaction_by_hash(hash, true)?
.get_transaction_by_hash(hash, ledger_version, true)?
.map(|t| self.convert_into_transaction_on_chain_data(t))
.transpose()
}
Expand Down
18 changes: 11 additions & 7 deletions api/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,10 +793,11 @@ impl TransactionsApi {
let context = self.context.clone();
let accept_type = accept_type.clone();

let ledger_info = api_spawn_blocking(move || context.get_latest_ledger_info()).await?;
let ledger_info =
api_spawn_blocking(move || context.get_latest_storage_ledger_info()).await?;

let txn_data = self
.get_by_hash(hash.into())
.get_by_hash(hash.into(), &ledger_info)
.await
.context(format!("Failed to get transaction by hash {}", hash))
.map_err(|err| {
Expand Down Expand Up @@ -836,7 +837,7 @@ impl TransactionsApi {
api_spawn_blocking(move || context.get_latest_storage_ledger_info()).await?;

let txn_data = self
.get_by_hash(hash.into())
.get_by_hash(hash.into(), &ledger_info)
.await
.context(format!("Failed to get transaction by hash {}", hash))
.map_err(|err| {
Expand Down Expand Up @@ -960,12 +961,15 @@ impl TransactionsApi {
async fn get_by_hash(
&self,
hash: aptos_crypto::HashValue,
ledger_info: &LedgerInfo,
) -> anyhow::Result<Option<TransactionData>> {
let context = self.context.clone();
let from_db = tokio::task::spawn_blocking(move || context.get_transaction_by_hash(hash))
.await
.context("Failed to join task to read transaction by hash")?
.context("Failed to read transaction by hash from DB")?;
let version = ledger_info.version();
let from_db =
tokio::task::spawn_blocking(move || context.get_transaction_by_hash(hash, version))
.await
.context("Failed to join task to read transaction by hash")?
.context("Failed to read transaction by hash from DB")?;
Ok(match from_db {
None => self
.context
Expand Down
1 change: 1 addition & 0 deletions peer-monitoring-service/server/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ mod database_mock {
fn get_transaction_by_hash(
&self,
hash: HashValue,
ledger_version: Version,
fetch_events: bool,
) -> Result<Option<TransactionWithProof>>;

Expand Down
1 change: 1 addition & 0 deletions state-sync/state-sync-driver/src/tests/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ mock! {
fn get_transaction_by_hash(
&self,
hash: HashValue,
ledger_version: Version,
fetch_events: bool,
) -> Result<Option<TransactionWithProof>>;

Expand Down
1 change: 1 addition & 0 deletions state-sync/storage-service/server/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ mock! {
fn get_transaction_by_hash(
&self,
hash: HashValue,
ledger_version: Version,
fetch_events: bool,
) -> aptos_storage_interface::Result<Option<TransactionWithProof>>;

Expand Down
3 changes: 2 additions & 1 deletion storage/aptosdb/src/db/include/aptosdb_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,14 @@ impl DbReader for AptosDB {
fn get_transaction_by_hash(
&self,
hash: HashValue,
ledger_version: Version,
fetch_events: bool,
) -> Result<Option<TransactionWithProof>> {
gauged_api("get_transaction_by_hash", || {
self.ledger_db
.transaction_db()
.get_transaction_version_by_hash(&hash)?
.map(|v| self.get_transaction_with_proof(v, v, fetch_events))
.map(|v| self.get_transaction_with_proof(v, ledger_version, fetch_events))
.transpose()
})
}
Expand Down
2 changes: 1 addition & 1 deletion storage/aptosdb/src/db/test_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ pub fn verify_committed_transactions(
.try_as_signed_user_txn()
.unwrap();
let txn_with_proof = db
.get_transaction_by_hash(txn_to_commit.transaction().hash(), true)
.get_transaction_by_hash(txn_to_commit.transaction().hash(), ledger_version, true)
.unwrap()
.unwrap();
assert_eq!(
Expand Down
3 changes: 0 additions & 3 deletions storage/aptosdb/src/ledger_db/transaction_db_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ proptest! {
let hash = txn.hash();
prop_assert_eq!(transaction_db.get_transaction(version as Version).unwrap(), txn);
prop_assert_eq!(transaction_db.get_transaction_version_by_hash(&hash).unwrap(), Some(version as Version));
if version > 0 {
prop_assert_eq!(transaction_db.get_transaction_version_by_hash(&hash).unwrap(), None);
}
}

prop_assert!(transaction_db.get_transaction(num_txns as Version).is_err());
Expand Down
1 change: 1 addition & 0 deletions storage/storage-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ pub trait DbReader: Send + Sync {
fn get_transaction_by_hash(
&self,
hash: HashValue,
ledger_version: Version,
fetch_events: bool,
) -> Result<Option<TransactionWithProof>>;

Expand Down

0 comments on commit efe0365

Please sign in to comment.