From 71739f891b05372747af9fb6dbbfdcf855cbdc54 Mon Sep 17 00:00:00 2001 From: "zhoujun.ma" Date: Tue, 18 Jun 2024 05:42:22 -0700 Subject: [PATCH] clean up --- api/types/src/convert.rs | 7 +-- api/types/src/transaction.rs | 88 ++++++++++++++++-------------------- 2 files changed, 42 insertions(+), 53 deletions(-) diff --git a/api/types/src/convert.rs b/api/types/src/convert.rs index bcfd522eb6e1a..a38118d0edba4 100644 --- a/api/types/src/convert.rs +++ b/api/types/src/convert.rs @@ -6,8 +6,7 @@ use crate::{ transaction::{ BlockEpilogueTransaction, DecodedTableData, DeleteModule, DeleteResource, DeleteTableItem, DeletedTableData, MultisigPayload, MultisigTransactionPayload, StateCheckpointTransaction, - UserTransactionRequestInner, ValidatorTransaction, WriteModule, WriteResource, - WriteTableItem, + UserTransactionRequestInner, WriteModule, WriteResource, WriteTableItem, }, view::{ViewFunction, ViewRequest}, Address, Bytecode, DirectWriteSet, EntryFunctionId, EntryFunctionPayload, Event, @@ -233,9 +232,7 @@ impl<'a, S: StateView> MoveConverter<'a, S> { }) }, aptos_types::transaction::Transaction::ValidatorTransaction(txn) => { - Transaction::ValidatorTransaction(ValidatorTransaction::from_internal_repr( - txn, info, events, timestamp, - )) + Transaction::ValidatorTransaction((txn, info, events, timestamp).into()) }, }) } diff --git a/api/types/src/transaction.rs b/api/types/src/transaction.rs index 7c499eadf7dce..cd05df936b21e 100755 --- a/api/types/src/transaction.rs +++ b/api/types/src/transaction.rs @@ -579,26 +579,6 @@ pub struct BlockMetadataTransaction { pub timestamp: U64, } -impl BlockMetadataTransaction { - pub fn from_internal_repr( - internal: BlockMetadata, - info: TransactionInfo, - events: Vec, - ) -> 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(), - } - } -} - #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Union)] #[serde(tag = "validator_transaction_type", rename_all = "snake_case")] #[oai( @@ -641,20 +621,31 @@ impl ValidatorTransaction { ValidatorTransaction::DkgResult(t) => t.timestamp, } } +} - pub fn from_internal_repr( - internal: aptos_types::validator_txn::ValidatorTransaction, - info: TransactionInfo, - events: Vec, - timestamp: u64, +impl + From<( + aptos_types::validator_txn::ValidatorTransaction, + TransactionInfo, + Vec, + u64, + )> for ValidatorTransaction +{ + fn from( + (txn, info, events, timestamp): ( + aptos_types::validator_txn::ValidatorTransaction, + TransactionInfo, + Vec, + u64, + ), ) -> Self { - match internal { + match txn { 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), + dkg_transcript: dkg_transcript.into(), }) }, aptos_types::validator_txn::ValidatorTransaction::ObservedJWKUpdate( @@ -663,9 +654,7 @@ impl ValidatorTransaction { info, events, timestamp: U64::from(timestamp), - quorum_certified_update: ExportedQuorumCertifiedUpdate::from_internal_repr( - quorum_certified_update, - ), + quorum_certified_update: quorum_certified_update.into(), }), } } @@ -688,12 +677,12 @@ pub struct ExportedQuorumCertifiedUpdate { pub multi_sig: ExportedAggregateSignature, } -impl ExportedQuorumCertifiedUpdate { - pub fn from_internal_repr(internal: QuorumCertifiedUpdate) -> Self { - let QuorumCertifiedUpdate { update, multi_sig } = internal; +impl From for ExportedQuorumCertifiedUpdate { + fn from(value: QuorumCertifiedUpdate) -> Self { + let QuorumCertifiedUpdate { update, multi_sig } = value; Self { - update: ExportedProviderJWKs::from_internal_repr(update), - multi_sig: ExportedAggregateSignature::from_internal_repr(multi_sig), + update: update.into(), + multi_sig: multi_sig.into(), } } } @@ -703,14 +692,17 @@ impl ExportedQuorumCertifiedUpdate { pub struct ExportedAggregateSignature { signer_indices: Vec, #[serde(skip_serializing_if = "Option::is_none")] - sig: Option>, + sig: Option, } -impl ExportedAggregateSignature { - pub fn from_internal_repr(internal: AggregateSignature) -> Self { +impl From for ExportedAggregateSignature { + fn from(value: AggregateSignature) -> Self { Self { - signer_indices: internal.get_signers_bitvec().iter_ones().collect(), - sig: internal.sig().as_ref().map(|s| s.to_bytes().to_vec()), + signer_indices: value.get_signers_bitvec().iter_ones().collect(), + sig: value + .sig() + .as_ref() + .map(|s| HexEncodedBytes::from(s.to_bytes().to_vec())), } } } @@ -723,13 +715,13 @@ pub struct ExportedProviderJWKs { pub jwks: Vec, } -impl ExportedProviderJWKs { - pub fn from_internal_repr(internal: ProviderJWKs) -> Self { +impl From for ExportedProviderJWKs { + fn from(value: ProviderJWKs) -> Self { let ProviderJWKs { issuer, version, jwks, - } = internal; + } = value; Self { issuer: String::from_utf8(issuer).unwrap_or("non_utf8_issuer".to_string()), version, @@ -754,20 +746,20 @@ pub struct DKGResultTransaction { pub struct ExportedDKGTranscript { epoch: U64, author: Address, - payload: Vec, + payload: HexEncodedBytes, } -impl ExportedDKGTranscript { - pub fn from_internal_repr(internal: DKGTranscript) -> Self { +impl From for ExportedDKGTranscript { + fn from(value: DKGTranscript) -> Self { let DKGTranscript { metadata, transcript_bytes, - } = internal; + } = value; let DKGTranscriptMetadata { epoch, author } = metadata; Self { epoch: epoch.into(), author: author.into(), - payload: transcript_bytes, + payload: HexEncodedBytes::from(transcript_bytes), } } }