Skip to content

Commit

Permalink
[Ecosystem][API][Indexer] Updates to API/Indexer WriteSet support per…
Browse files Browse the repository at this point in the history
… Review changes

Closes: aptos-labs#550
  • Loading branch information
CapCap authored and aptos-bot committed Apr 21, 2022
1 parent 6092e50 commit 18edbd1
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 105 deletions.
41 changes: 16 additions & 25 deletions api/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,10 @@ impl Context {
) -> Result<Vec<TransactionOnChainData>> {
let data = self
.db
.get_transactions(start_version, limit as u64, ledger_version, true)?;
let tx_outputs =
self.db
.get_transaction_outputs(start_version, limit as u64, ledger_version)?;
.get_transaction_outputs(start_version, limit as u64, ledger_version)?;

let txn_start_version = data
.first_transaction_version
.first_transaction_output_version
.ok_or_else(|| format_err!("no start version from database"))?;
ensure!(
txn_start_version == start_version,
Expand All @@ -148,30 +145,23 @@ impl Context {
start_version
);

let txns = data.transactions;
let infos = data.proof.transaction_infos;
let events = data.events.unwrap_or_default();
let changes: Vec<aptos_types::write_set::WriteSet> = tx_outputs
.transactions_and_outputs
.into_iter()
.map(|txo| txo.1.unpack().0)
.collect();
let transactions_and_outputs = data.transactions_and_outputs;

ensure!(
txns.len() == infos.len() && txns.len() == events.len() && txns.len() == changes.len(),
"invalid data size from database: {}, {}, {}, {}",
txns.len(),
transactions_and_outputs.len() == infos.len(),
"invalid data size from database: {}, {}",
transactions_and_outputs.len(),
infos.len(),
events.len(),
changes.len(),
);

txns.into_iter()
transactions_and_outputs
.into_iter()
.zip(infos.into_iter())
.zip(events.into_iter())
.zip(changes.into_iter())
.enumerate()
.map(|(i, (((txn, info), events), write_set))| {
.map(|(i, ((txn, txn_output), info))| {
let version = start_version + i as u64;
let (write_set, events, _, _) = txn_output.unpack();
self.get_accumulator_root_hash(version)
.map(|h| (version, txn, info, events, h, write_set).into())
})
Expand Down Expand Up @@ -244,12 +234,13 @@ impl Context {
&self,
txn: TransactionWithProof,
) -> Result<TransactionOnChainData> {
let tx_outputs = self
// the type is Vec<(Transaction, TransactionOutput)> - given we have one transaction here, there should only ever be one value in this array
let (_, txn_output) = &self
.db
.get_transaction_outputs(txn.version, 1, txn.version)?;

.get_transaction_outputs(txn.version, 1, txn.version)?
.transactions_and_outputs[0];
self.get_accumulator_root_hash(txn.version)
.map(|h| (txn, h, tx_outputs).into())
.map(|h| (txn, h, txn_output).into())
}

pub fn get_events(
Expand Down
5 changes: 2 additions & 3 deletions api/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,11 @@ impl Transactions {
let first_version = data[0].version;
let mut timestamp = self.context.get_block_timestamp(first_version)?;
let resolver = self.context.move_resolver()?;
let converter = resolver.as_converter();
let txns: Vec<Transaction> = data
.into_iter()
.map(|t| {
let txn = resolver
.as_converter()
.try_into_onchain_transaction(timestamp, t)?;
let txn = converter.try_into_onchain_transaction(timestamp, t)?;
// update timestamp, when txn is metadata block transaction
// new timestamp is used for the following transactions
timestamp = txn.timestamp();
Expand Down
15 changes: 7 additions & 8 deletions api/types/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ impl<'a, R: MoveResolver + ?Sized> MoveConverter<'a, R> {
success: info.status().is_success(),
vm_status: self.explain_vm_status(info.status()),
accumulator_root_hash: accumulator_root_hash.into(),
// TODO: the resource value is interpreted by the type definition at the version of the converter, not the version of the tx: must be fixed before we allow module updates
changes: write_set
.into_iter()
.filter_map(|(sk, wo)| self.try_into_write_set_change(sk, wo).ok())
Expand Down Expand Up @@ -186,6 +187,7 @@ impl<'a, R: MoveResolver + ?Sized> MoveConverter<'a, R> {
let (write_set, events) = d.into_inner();
WriteSetPayload {
write_set: WriteSet::DirectWriteSet(DirectWriteSet {
// TODO: the resource value is interpreted by the type definition at the version of the converter, not the version of the tx: must be fixed before we allow module updates
changes: write_set
.into_iter()
.map(|(state_key, op)| self.try_into_write_set_change(state_key, op))
Expand Down Expand Up @@ -224,12 +226,9 @@ impl<'a, R: MoveResolver + ?Sized> MoveConverter<'a, R> {
access_path: AccessPath,
op: WriteOp,
) -> Result<WriteSetChange> {
let sk = StateKey::AccessPath(access_path);
let state_key_hash = sk.hash().to_hex_literal();
let access_path = match sk {
StateKey::AccessPath(access_path) => access_path,
_ => unreachable!(),
};
let state_key_hash = StateKey::AccessPath(access_path.clone())
.hash()
.to_hex_literal();
let ret = match op {
WriteOp::Deletion => match access_path.get_path() {
Path::Code(module_id) => WriteSetChange::DeleteModule {
Expand Down Expand Up @@ -303,7 +302,7 @@ impl<'a, R: MoveResolver + ?Sized> MoveConverter<'a, R> {
Ok(RawTransaction::new(
sender.into(),
sequence_number.into(),
self.try_into_diem_core_transaction_payload(payload)?,
self.try_into_aptos_core_transaction_payload(payload)?,
max_gas_amount.into(),
gas_unit_price.into(),
gas_currency_code,
Expand All @@ -312,7 +311,7 @@ impl<'a, R: MoveResolver + ?Sized> MoveConverter<'a, R> {
))
}

pub fn try_into_diem_core_transaction_payload(
pub fn try_into_aptos_core_transaction_payload(
&self,
payload: TransactionPayload,
) -> Result<aptos_types::transaction::TransactionPayload> {
Expand Down
9 changes: 4 additions & 5 deletions api/types/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use aptos_types::{
contract_event::ContractEvent,
transaction::{
authenticator::{AccountAuthenticator, TransactionAuthenticator},
Script, SignedTransaction, TransactionOutputListWithProof, TransactionWithProof,
Script, SignedTransaction, TransactionOutput, TransactionWithProof,
},
};

Expand Down Expand Up @@ -75,17 +75,16 @@ impl
From<(
TransactionWithProof,
aptos_crypto::HashValue,
TransactionOutputListWithProof,
&TransactionOutput,
)> for TransactionOnChainData
{
fn from(
(txn, accumulator_root_hash, txn_output_list): (
(txn, accumulator_root_hash, txn_output): (
TransactionWithProof,
aptos_crypto::HashValue,
TransactionOutputListWithProof,
&TransactionOutput,
),
) -> Self {
let (_, txn_output) = &txn_output_list.transactions_and_outputs[0];
Self {
version: txn.version,
transaction: txn.transaction,
Expand Down
Loading

0 comments on commit 18edbd1

Please sign in to comment.