Skip to content

Commit

Permalink
Add Generator for transaction BCS endpoint for transfers
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewl33 authored and davidiw committed Sep 8, 2022
1 parent a7afb36 commit a4574e4
Show file tree
Hide file tree
Showing 5 changed files with 522 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions testsuite/generate-format/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ serde-reflection = { git = "https://github.com/aptos-labs/serde-reflection", rev
serde_yaml = "0.8.24"
structopt = "0.3.21"

aptos-api-types = { path = "../../api/types", package = "aptos-api-types" }
aptos-config = { path = "../../config" }
aptos-crypto = { path = "../../crates/aptos-crypto", features = ["fuzzing"] }
aptos-crypto-derive = { path = "../../crates/aptos-crypto-derive" }
Expand Down
95 changes: 95 additions & 0 deletions testsuite/generate-format/src/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright (c) Aptos
// SPDX-License-Identifier: Apache-2.0

use aptos_crypto::{
ed25519::{Ed25519PrivateKey, Ed25519PublicKey},
hash::{CryptoHasher as _, TestOnlyHasher},
multi_ed25519::{MultiEd25519PublicKey, MultiEd25519Signature},
traits::{SigningKey, Uniform},
};
use aptos_crypto_derive::{BCSCryptoHash, CryptoHasher};
use aptos_types::{
access_path::{AccessPath, Path},
account_config::{CoinStoreResource, DepositEvent, WithdrawEvent},
contract_event, event,
state_store::state_key::StateKey,
transaction,
transaction::authenticator::{AccountAuthenticator, TransactionAuthenticator},
vm_status::AbortLocation,
write_set,
};
use move_deps::move_core_types::language_storage;
use rand::{rngs::StdRng, SeedableRng};
use serde::{Deserialize, Serialize};
use serde_reflection::{Registry, Result, Samples, Tracer, TracerConfig};

/// Default output file.
pub fn output_file() -> Option<&'static str> {
Some("tests/staged/api.yaml")
}

/// This aims at signing canonically serializable BCS data
#[derive(CryptoHasher, BCSCryptoHash, Serialize, Deserialize)]
struct TestAptosCrypto(String);

/// Record sample values for crypto types used by transactions.
fn trace_crypto_values(tracer: &mut Tracer, samples: &mut Samples) -> Result<()> {
let mut hasher = TestOnlyHasher::default();
hasher.update(b"Test message");
let hashed_message = hasher.finish();

let message = TestAptosCrypto("Hello, World".to_string());

let mut rng: StdRng = SeedableRng::from_seed([0; 32]);
let private_key = Ed25519PrivateKey::generate(&mut rng);
let public_key: Ed25519PublicKey = (&private_key).into();
let signature = private_key.sign(&message);

tracer.trace_value(samples, &hashed_message)?;
tracer.trace_value(samples, &public_key)?;
tracer.trace_value::<MultiEd25519PublicKey>(samples, &public_key.into())?;
tracer.trace_value(samples, &signature)?;
tracer.trace_value::<MultiEd25519Signature>(samples, &signature.into())?;
Ok(())
}

pub fn get_registry() -> Result<Registry> {
let mut tracer =
Tracer::new(TracerConfig::default().is_human_readable(bcs::is_human_readable()));
let mut samples = Samples::new();
// 1. Record samples for types with custom deserializers.
trace_crypto_values(&mut tracer, &mut samples)?;
tracer.trace_value(&mut samples, &event::EventKey::random())?;

// 2. Trace the main entry point(s) + every enum separately.
// stdlib types
tracer.trace_type::<contract_event::ContractEvent>(&samples)?;
tracer.trace_type::<language_storage::TypeTag>(&samples)?;
tracer.trace_type::<transaction::Transaction>(&samples)?;
tracer.trace_type::<transaction::TransactionArgument>(&samples)?;
tracer.trace_type::<transaction::TransactionPayload>(&samples)?;
tracer.trace_type::<transaction::WriteSetPayload>(&samples)?;
tracer.trace_type::<StateKey>(&samples)?;
tracer.trace_type::<transaction::ExecutionStatus>(&samples)?;
tracer.trace_type::<TransactionAuthenticator>(&samples)?;
tracer.trace_type::<write_set::WriteOp>(&samples)?;
tracer.trace_type::<AccountAuthenticator>(&samples)?;
tracer.trace_type::<AbortLocation>(&samples)?;

// events
tracer.trace_type::<WithdrawEvent>(&samples)?;
tracer.trace_type::<DepositEvent>(&samples)?;

// writeset
tracer.trace_type::<AccessPath>(&samples)?;
tracer.trace_type::<Path>(&samples)?;

// api types
tracer.trace_type::<aptos_api_types::TransactionData>(&samples)?;
tracer.trace_type::<aptos_api_types::TransactionOnChainData>(&samples)?;

// output types
tracer.trace_type::<CoinStoreResource>(&samples)?;

tracer.registry()
}
5 changes: 5 additions & 0 deletions testsuite/generate-format/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use serde_reflection::Registry;
use std::str::FromStr;
use structopt::{clap::arg_enum, StructOpt};

/// Rest API types
mod api;
/// Aptos transactions.
mod aptos;
/// Consensus messages.
Expand All @@ -25,6 +27,7 @@ arg_enum! {
#[derive(Debug, StructOpt, Clone, Copy)]
/// A corpus of Rust types to trace, and optionally record on disk.
pub enum Corpus {
API,
Aptos,
Consensus,
Network,
Expand All @@ -44,6 +47,7 @@ impl Corpus {
/// Compute the registry of formats.
pub fn get_registry(self) -> Registry {
let result = match self {
Corpus::API => api::get_registry(),
Corpus::Aptos => aptos::get_registry(),
Corpus::Consensus => consensus::get_registry(),
Corpus::Network => network::get_registry(),
Expand All @@ -60,6 +64,7 @@ impl Corpus {
/// Where to record this corpus on disk.
pub fn output_file(self) -> Option<&'static str> {
match self {
Corpus::API => api::output_file(),
Corpus::Aptos => aptos::output_file(),
Corpus::Consensus => consensus::output_file(),
Corpus::Network => network::output_file(),
Expand Down
Loading

0 comments on commit a4574e4

Please sign in to comment.