Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validator txn api fix #13718

Merged
merged 9 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions api/types/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use crate::{
transaction::{
BlockEpilogueTransaction, DecodedTableData, DeleteModule, DeleteResource, DeleteTableItem,
DeletedTableData, MultisigPayload, MultisigTransactionPayload, StateCheckpointTransaction,
UserTransactionRequestInner, WriteModule, WriteResource, WriteTableItem,
UserTransactionRequestInner, ValidatorTransaction, WriteModule, WriteResource,
WriteTableItem,
},
view::{ViewFunction, ViewRequest},
Address, Bytecode, DirectWriteSet, EntryFunctionId, EntryFunctionPayload, Event,
Expand Down Expand Up @@ -176,7 +177,10 @@ impl<'a, S: StateView> MoveConverter<'a, S> {
timestamp: u64,
data: TransactionOnChainData,
) -> Result<Transaction> {
use aptos_types::transaction::Transaction::*;
use aptos_types::transaction::Transaction::{
BlockEpilogue, BlockMetadata, BlockMetadataExt, GenesisTransaction, StateCheckpoint,
UserTransaction,
};
let aux_data = self
.db
.get_transaction_auxiliary_data_by_version(data.version)?;
Expand Down Expand Up @@ -228,7 +232,11 @@ impl<'a, S: StateView> MoveConverter<'a, S> {
},
})
},
ValidatorTransaction(_txn) => (info, events, timestamp).into(),
aptos_types::transaction::Transaction::ValidatorTransaction(txn) => {
Transaction::ValidatorTransaction(ValidatorTransaction::from_internal_repr(
zjma marked this conversation as resolved.
Show resolved Hide resolved
txn, info, events, timestamp,
))
},
})
}

Expand Down
211 changes: 194 additions & 17 deletions api/types/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ use aptos_crypto::{
};
use aptos_types::{
account_address::AccountAddress,
aggregate_signature::AggregateSignature,
block_metadata::BlockMetadata,
block_metadata_ext::BlockMetadataExt,
contract_event::{ContractEvent, EventWithVersion},
dkg::{DKGTranscript, DKGTranscriptMetadata},
jwks::{jwk::JWK, ProviderJWKs, QuorumCertifiedUpdate},
keyless,
transaction::{
authenticator::{
Expand Down Expand Up @@ -190,7 +193,7 @@ impl Transaction {
Transaction::GenesisTransaction(_) => 0,
Transaction::StateCheckpointTransaction(txn) => txn.timestamp.0,
Transaction::BlockEpilogueTransaction(txn) => txn.timestamp.0,
Transaction::ValidatorTransaction(txn) => txn.timestamp.0,
Transaction::ValidatorTransaction(txn) => txn.timestamp().0,
}
}

Expand All @@ -202,7 +205,7 @@ impl Transaction {
Transaction::GenesisTransaction(txn) => Some(txn.info.version.into()),
Transaction::StateCheckpointTransaction(txn) => Some(txn.info.version.into()),
Transaction::BlockEpilogueTransaction(txn) => Some(txn.info.version.into()),
Transaction::ValidatorTransaction(txn) => Some(txn.info.version.into()),
Transaction::ValidatorTransaction(txn) => Some(txn.transaction_info().version.into()),
}
}

Expand All @@ -214,7 +217,7 @@ impl Transaction {
Transaction::GenesisTransaction(txn) => txn.info.success,
Transaction::StateCheckpointTransaction(txn) => txn.info.success,
Transaction::BlockEpilogueTransaction(txn) => txn.info.success,
Transaction::ValidatorTransaction(txn) => txn.info.success,
Transaction::ValidatorTransaction(txn) => txn.transaction_info().success,
}
}

Expand All @@ -230,7 +233,7 @@ impl Transaction {
Transaction::GenesisTransaction(txn) => txn.info.vm_status.clone(),
Transaction::StateCheckpointTransaction(txn) => txn.info.vm_status.clone(),
Transaction::BlockEpilogueTransaction(txn) => txn.info.vm_status.clone(),
Transaction::ValidatorTransaction(txn) => txn.info.vm_status.clone(),
Transaction::ValidatorTransaction(txn) => txn.transaction_info().vm_status.clone(),
}
}

Expand All @@ -242,7 +245,7 @@ impl Transaction {
Transaction::BlockMetadataTransaction(_) => "block_metadata_transaction",
Transaction::StateCheckpointTransaction(_) => "state_checkpoint_transaction",
Transaction::BlockEpilogueTransaction(_) => "block_epilogue_transaction",
Transaction::ValidatorTransaction(_) => "validator_transaction",
Transaction::ValidatorTransaction(vt) => vt.type_str(),
}
}

Expand All @@ -256,7 +259,7 @@ impl Transaction {
Transaction::GenesisTransaction(txn) => &txn.info,
Transaction::StateCheckpointTransaction(txn) => &txn.info,
Transaction::BlockEpilogueTransaction(txn) => &txn.info,
Transaction::ValidatorTransaction(txn) => &txn.info,
Transaction::ValidatorTransaction(txn) => txn.transaction_info(),
})
}
}
Expand Down Expand Up @@ -354,16 +357,6 @@ impl From<(&SignedTransaction, TransactionPayload)> for UserTransactionRequest {
}
}

impl From<(TransactionInfo, Vec<Event>, u64)> for Transaction {
fn from((info, events, timestamp): (TransactionInfo, Vec<Event>, u64)) -> Self {
Transaction::ValidatorTransaction(ValidatorTransaction {
info,
events,
timestamp: timestamp.into(),
})
}
}

/// Information related to how a transaction affected the state of the blockchain
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Object)]
pub struct TransactionInfo {
Expand Down Expand Up @@ -586,13 +579,197 @@ pub struct BlockMetadataTransaction {
pub timestamp: U64,
}

impl BlockMetadataTransaction {
pub fn from_internal_repr(
internal: BlockMetadata,
info: TransactionInfo,
events: Vec<Event>,
) -> Self {
Self {
info,
id: internal.id().into(),
epoch: internal.epoch().into(),
round: internal.round().into(),
events,
previous_block_votes_bitvec: internal.previous_block_votes_bitvec().clone(),
proposer: internal.proposer().into(),
failed_proposer_indices: internal.failed_proposer_indices().clone(),
timestamp: internal.timestamp_usecs().into(),
}
}
}
zjma marked this conversation as resolved.
Show resolved Hide resolved

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Union)]
#[serde(tag = "validator_transaction_type", rename_all = "snake_case")]
#[oai(
one_of,
discriminator_name = "validator_transaction_type",
rename_all = "snake_case"
)]
pub enum ValidatorTransaction {
ObservedJwkUpdate(JWKUpdateTransaction),
DkgResult(DKGResultTransaction),
}

impl ValidatorTransaction {
pub fn type_str(&self) -> &'static str {
match self {
ValidatorTransaction::ObservedJwkUpdate(_) => {
"validator_transaction__observed_jwk_update"
},
ValidatorTransaction::DkgResult(_) => "validator_transaction__dkg_result",
}
}

pub fn transaction_info(&self) -> &TransactionInfo {
match self {
ValidatorTransaction::ObservedJwkUpdate(t) => &t.info,
ValidatorTransaction::DkgResult(t) => &t.info,
}
}

pub fn transaction_info_mut(&mut self) -> &mut TransactionInfo {
match self {
ValidatorTransaction::ObservedJwkUpdate(t) => &mut t.info,
ValidatorTransaction::DkgResult(t) => &mut t.info,
}
}

pub fn timestamp(&self) -> U64 {
match self {
ValidatorTransaction::ObservedJwkUpdate(t) => t.timestamp,
ValidatorTransaction::DkgResult(t) => t.timestamp,
}
}

pub fn from_internal_repr(
internal: aptos_types::validator_txn::ValidatorTransaction,
info: TransactionInfo,
events: Vec<Event>,
timestamp: u64,
) -> Self {
zjma marked this conversation as resolved.
Show resolved Hide resolved
match internal {
aptos_types::validator_txn::ValidatorTransaction::DKGResult(dkg_transcript) => {
Self::DkgResult(DKGResultTransaction {
info,
events,
timestamp: U64::from(timestamp),
dkg_transcript: ExportedDKGTranscript::from_internal_repr(dkg_transcript),
})
},
aptos_types::validator_txn::ValidatorTransaction::ObservedJWKUpdate(
quorum_certified_update,
) => Self::ObservedJwkUpdate(JWKUpdateTransaction {
info,
events,
timestamp: U64::from(timestamp),
quorum_certified_update: ExportedQuorumCertifiedUpdate::from_internal_repr(
quorum_certified_update,
),
}),
}
}
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Object)]
pub struct JWKUpdateTransaction {
#[serde(flatten)]
#[oai(flatten)]
pub info: TransactionInfo,
pub events: Vec<Event>,
pub timestamp: U64,
pub quorum_certified_update: ExportedQuorumCertifiedUpdate,
}

/// A more API-friendly representation of the on-chain `aptos_types::jwks::QuorumCertifiedUpdate`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Object)]
pub struct ExportedQuorumCertifiedUpdate {
pub update: ExportedProviderJWKs,
pub multi_sig: ExportedAggregateSignature,
}

impl ExportedQuorumCertifiedUpdate {
pub fn from_internal_repr(internal: QuorumCertifiedUpdate) -> Self {
let QuorumCertifiedUpdate { update, multi_sig } = internal;
Self {
update: ExportedProviderJWKs::from_internal_repr(update),
multi_sig: ExportedAggregateSignature::from_internal_repr(multi_sig),
}
}
}
zjma marked this conversation as resolved.
Show resolved Hide resolved

/// A more API-friendly representation of the on-chain `aptos_types::aggregate_signature::AggregateSignature`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Object)]
pub struct ExportedAggregateSignature {
signer_indices: Vec<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
sig: Option<Vec<u8>>,
gregnazario marked this conversation as resolved.
Show resolved Hide resolved
zjma marked this conversation as resolved.
Show resolved Hide resolved
}

impl ExportedAggregateSignature {
pub fn from_internal_repr(internal: AggregateSignature) -> Self {
Self {
signer_indices: internal.get_signers_bitvec().iter_ones().collect(),
sig: internal.sig().as_ref().map(|s| s.to_bytes().to_vec()),
}
}
}

/// A more API-friendly representation of the on-chain `aptos_types::jwks::ProviderJWKs`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Object)]
pub struct ValidatorTransaction {
pub struct ExportedProviderJWKs {
pub issuer: String,
pub version: u64,
pub jwks: Vec<JWK>,
}

impl ExportedProviderJWKs {
pub fn from_internal_repr(internal: ProviderJWKs) -> Self {
let ProviderJWKs {
issuer,
version,
jwks,
} = internal;
Self {
issuer: String::from_utf8(issuer).unwrap_or("non_utf8_issuer".to_string()),
version,
jwks: jwks.iter().map(|on_chain_jwk|{
JWK::try_from(on_chain_jwk).expect("conversion from on-chain representation to human-friendly representation should work")
}).collect(),
}
}
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Object)]
pub struct DKGResultTransaction {
#[serde(flatten)]
#[oai(flatten)]
pub info: TransactionInfo,
pub events: Vec<Event>,
pub timestamp: U64,
pub dkg_transcript: ExportedDKGTranscript,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Object)]
pub struct ExportedDKGTranscript {
epoch: U64,
author: Address,
payload: Vec<u8>,
zjma marked this conversation as resolved.
Show resolved Hide resolved
}

impl ExportedDKGTranscript {
pub fn from_internal_repr(internal: DKGTranscript) -> Self {
let DKGTranscript {
metadata,
transcript_bytes,
} = internal;
let DKGTranscriptMetadata { epoch, author } = metadata;
Self {
epoch: epoch.into(),
author: author.into(),
payload: transcript_bytes,
}
}
}

/// An event from a transaction
Expand Down
10 changes: 5 additions & 5 deletions crates/aptos/src/common/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1365,16 +1365,16 @@ impl From<&Transaction> for TransactionSummary {
sequence_number: None,
},
Transaction::ValidatorTransaction(txn) => TransactionSummary {
transaction_hash: txn.info.hash,
transaction_hash: txn.transaction_info().hash,
gas_used: None,
gas_unit_price: None,
pending: None,
sender: None,
sequence_number: None,
success: Some(txn.info.success),
timestamp_us: Some(txn.timestamp.0),
version: Some(txn.info.version.0),
vm_status: Some(txn.info.vm_status.clone()),
success: Some(txn.transaction_info().success),
timestamp_us: Some(txn.timestamp().0),
version: Some(txn.transaction_info().version.0),
vm_status: Some(txn.transaction_info().vm_status.clone()),
},
}
}
Expand Down
5 changes: 3 additions & 2 deletions crates/indexer/src/indexer/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,9 @@ async fn fetch_nexts(
bet.info.epoch = Some(epoch_bcs);
},
Transaction::ValidatorTransaction(ref mut st) => {
st.info.block_height = Some(block_height_bcs);
st.info.epoch = Some(epoch_bcs);
let info = st.transaction_info_mut();
info.block_height = Some(block_height_bcs);
info.epoch = Some(epoch_bcs);
},
};
txn
Expand Down
2 changes: 1 addition & 1 deletion crates/indexer/src/models/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ impl Transaction {
},
APITransaction::ValidatorTransaction(validator_txn) => (
Self::from_transaction_info(
&validator_txn.info,
validator_txn.transaction_info(),
None,
transaction.type_str().to_string(),
0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,9 @@ impl IndexerStreamCoordinator {
bet.info.epoch = Some(epoch_bcs);
},
APITransaction::ValidatorTransaction(ref mut vt) => {
vt.info.block_height = Some(block_height_bcs);
vt.info.epoch = Some(epoch_bcs);
let info = vt.transaction_info_mut();
info.block_height = Some(block_height_bcs);
info.epoch = Some(epoch_bcs);
},
};
txn
Expand Down
5 changes: 5 additions & 0 deletions types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ num-derive = { workspace = true }
num-traits = { workspace = true }
once_cell = { workspace = true }
passkey-types = { workspace = true }
poem-openapi = { workspace = true }
poem-openapi-derive = { workspace = true }
proptest = { workspace = true, optional = true }
proptest-derive = { workspace = true, optional = true }
quick_cache = { workspace = true }
Expand Down Expand Up @@ -95,3 +97,6 @@ harness = false
[[bench]]
name = "state_key"
harness = false

[package.metadata.cargo-machete]
ignored = ['poem-openapi']
Loading
Loading