-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support test file creation as a part of test txn generation
- Loading branch information
Showing
6 changed files
with
175 additions
and
110 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
ecosystem/indexer-grpc/indexer-transaction-generator/src/transaction_code_builder.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright (c) Aptos Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use std::fs; | ||
use std::path::Path; | ||
|
||
pub const IMPORTED_MAINNET_TXNS: &str = "imported_mainnet_txns"; | ||
pub const IMPORTED_TESTNET_TXNS: &str = "imported_testnet_txns"; | ||
pub const SCRIPTED_TRANSACTIONS_TXNS: &str = "scripted_transactions"; | ||
|
||
#[derive(Default)] | ||
pub struct TransactionCodeBuilder { | ||
// Holds the generated Rust code for transaction constants | ||
transactions_code: String, | ||
// Holds the match arms for the name generation function for scripted txns (optional) | ||
name_function_code: String, | ||
} | ||
|
||
impl TransactionCodeBuilder { | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
|
||
/** | ||
* Adds a directory of JSON files to the transaction code builder. | ||
* | ||
* @param src_dir: The source directory containing the JSON files | ||
* @param dir_name: The name of the directory to be created in the `json_transactions` directory | ||
* @param module_name: The name of the module to be used in the constant names | ||
* @param generate_name_function: Whether to generate a transaction name lookup function | ||
*/ | ||
pub fn add_directory( | ||
mut self, | ||
json_dir: &Path, | ||
module_name: &str, | ||
generate_name_function: bool, | ||
) -> Self { | ||
let mut all_constants = String::new(); | ||
|
||
// Iterates over all files in the directory | ||
for entry in fs::read_dir(json_dir).expect("Failed to read directory") { | ||
let entry = entry.expect("Failed to get directory entry"); | ||
let path = entry.path(); | ||
|
||
// Checks if the file has a `.json` extension | ||
if path.extension().and_then(|s| s.to_str()) == Some("json") { | ||
let file_name = path.file_stem().unwrap().to_str().unwrap(); | ||
let const_name = format!( | ||
"{}_{}", | ||
module_name.to_uppercase(), | ||
file_name.to_uppercase().replace('-', "_") | ||
); | ||
|
||
// Generates a constant for the JSON file and appends it to the `transactions_code` string | ||
self.transactions_code.push_str(&format!( | ||
r#" | ||
pub const {const_name}: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/json_transactions/{dir_name}/{file_name}.json")); | ||
"#, | ||
const_name = const_name, | ||
dir_name = module_name, | ||
file_name = file_name, | ||
)); | ||
|
||
// Adds the constant to the list of all constants | ||
all_constants.push_str(&format!("{},", const_name)); | ||
|
||
// If name function generation is requested, adds the corresponding match arm | ||
if generate_name_function { | ||
self.name_function_code.push_str(&format!( | ||
" {const_name} => Some(\"{file_name}\"),\n", | ||
const_name = const_name, | ||
file_name = file_name | ||
)); | ||
} | ||
} | ||
} | ||
|
||
// If any constants were created, generate an array holding all of them | ||
if !all_constants.is_empty() { | ||
self.transactions_code.push_str(&format!( | ||
"pub const ALL_{}: &[&[u8]] = &[{}];\n", | ||
module_name.to_uppercase(), | ||
all_constants | ||
)); | ||
} | ||
|
||
self | ||
} | ||
|
||
// Adds the transaction name lookup function if any name match arms were created | ||
pub fn add_transaction_name_function(mut self) -> Self { | ||
if !self.name_function_code.is_empty() { | ||
self.transactions_code.push_str( | ||
r#" | ||
pub fn get_transaction_name(const_data: &[u8]) -> Option<&'static str> { | ||
match const_data { | ||
"#, | ||
); | ||
|
||
self.transactions_code.push_str(&self.name_function_code); | ||
|
||
self.transactions_code.push_str( | ||
r#" | ||
_ => None, | ||
} | ||
} | ||
"#, | ||
); | ||
} | ||
self | ||
} | ||
|
||
pub fn build(self) -> String { | ||
self.transactions_code | ||
} | ||
} |