-
-
Save aliX40/9213159922b9793760abdff3bcb817fa to your computer and use it in GitHub Desktop.
PoC 4
This file contains hidden or 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
| use anyhow::{Context, Result}; | |
| use aptos_crypto::{ed25519::Ed25519PrivateKey, PrivateKey, SigningKey}; | |
| use aptos_sdk::move_types::{ | |
| identifier::Identifier, | |
| language_storage::{ModuleId, TypeTag}, | |
| }; | |
| use aptos_sdk::{ | |
| coin_client::CoinClient, | |
| rest_client::{Client, FaucetClient}, | |
| // transaction_builder::TransactionBuilder, | |
| types::LocalAccount, | |
| }; | |
| use aptos_types::{ | |
| account_address::AccountAddress, | |
| account_config::account, | |
| chain_id::ChainId, | |
| transaction::{ | |
| authenticator::AuthenticationKey, EntryFunction, RawTransaction, SignedTransaction, | |
| TransactionPayload, | |
| }, | |
| }; | |
| use bcs; | |
| use maptos_dof_execution::SignedTransaction as MaptosSignedTransaction; | |
| use movement_da_light_node_client::MovementDaLightNodeClient; | |
| use movement_da_light_node_proto::{BatchWriteRequest, BlobWrite}; | |
| use movement_types::transaction::Transaction as MovementTransaction; | |
| use prost::Message; | |
| use reqwest::Url; | |
| use std::str::FromStr; | |
| use std::{ | |
| result, | |
| time::{Duration, Instant,SystemTime, UNIX_EPOCH}, | |
| vec, | |
| }; | |
| use tokio::time::sleep; | |
| use tracing::{info, Level}; | |
| use tracing_subscriber::FmtSubscriber; | |
| async fn setup_account( | |
| faucet_url: &str, | |
| node_url: &str, | |
| ) -> Result<(AccountAddress, Ed25519PrivateKey, LocalAccount)> { | |
| // Generate a keypair | |
| // let private_key = Ed25519PrivateKey::generate_for_testing(); | |
| // let public_key = private_key.public_key(); | |
| // let auth_key = AuthenticationKey::ed25519(&public_key); | |
| // let account_address = AccountAddress::from(auth_key); | |
| let alice = LocalAccount::generate(&mut rand::rngs::OsRng); | |
| // Initialize the Faucet client as in the reference sample. | |
| let private_key = alice.private_key().clone(); | |
| let public_key = alice.public_key().clone(); | |
| let auth_key = alice.authentication_key(); | |
| let account_address = alice.address(); | |
| let faucet_client = FaucetClient::new(Url::parse(faucet_url)?, Url::parse(node_url)?); | |
| info!("Account address: {}", account_address); | |
| info!("Auth key: {}", auth_key); | |
| info!("Public key: {}", public_key); | |
| // Fund the account with error context. | |
| faucet_client | |
| .fund(account_address, 10000700) | |
| .await | |
| .context("Failed to fund the account")?; | |
| Ok((account_address, private_key, alice)) | |
| } | |
| // Simplified function to create a mock transaction | |
| fn create_mock_transaction( | |
| sender: AccountAddress, | |
| sequence_number: u64, | |
| private_key: &Ed25519PrivateKey, | |
| ) -> Result<MaptosSignedTransaction> { | |
| // Create a simple script transaction | |
| let payload = TransactionPayload::Script(aptos_types::transaction::Script::new( | |
| vec![1, 2, 3], // Simple dummy bytecode | |
| vec![], // No type arguments | |
| vec![], // No arguments | |
| )); | |
| // Create a raw transaction | |
| let raw_transaction = RawTransaction::new( | |
| sender, | |
| sequence_number, | |
| payload, | |
| 100000, // Max gas amount | |
| 1000, // Higher gas unit price to satisfy the minimum bound | |
| u64::MAX, // Expiration timestamp (far future) | |
| ChainId::new(27), | |
| ); | |
| // Directly sign the raw transaction | |
| let signature = private_key.sign(&raw_transaction).unwrap(); | |
| let signed_txn = SignedTransaction::new(raw_transaction, private_key.public_key(), signature); | |
| // Convert to Movement's SignedTransaction type | |
| let maptos_txn = MaptosSignedTransaction::try_from(signed_txn)?; | |
| Ok(maptos_txn) | |
| } | |
| fn create_send_coin_transaction( | |
| sender: AccountAddress, | |
| to_account: AccountAddress, | |
| amount: u64, | |
| sequence_number: u64, | |
| time_increase:u64, | |
| private_key: &Ed25519PrivateKey, | |
| ) -> Result<MaptosSignedTransaction> { | |
| let payload = TransactionPayload::EntryFunction(EntryFunction::new( | |
| ModuleId::new(AccountAddress::ONE, Identifier::new("coin").unwrap()), | |
| Identifier::new("transfer").unwrap(), | |
| vec![TypeTag::from_str("0x1::aptos_coin::AptosCoin").unwrap()], | |
| vec![bcs::to_bytes(&to_account).unwrap(), bcs::to_bytes(&amount).unwrap()], | |
| )); | |
| let now = SystemTime::now().duration_since(UNIX_EPOCH)?; | |
| let raw_transaction = RawTransaction::new( | |
| sender, | |
| sequence_number, | |
| payload, | |
| 10000, // Max gas amount | |
| 100, // Higher gas unit price to satisfy the minimum bound | |
| now.as_secs()+time_increase, // Expiration timestamp (far future) | |
| ChainId::new(27), | |
| ); | |
| // Directly sign the raw transaction | |
| let signature = private_key.sign(&raw_transaction).unwrap(); | |
| let signed_txn = SignedTransaction::new(raw_transaction, private_key.public_key(), signature); | |
| // Convert to Movement's SignedTransaction type | |
| let maptos_txn = MaptosSignedTransaction::try_from(signed_txn)?; | |
| Ok(maptos_txn) | |
| } | |
| async fn test_transaction_sending() -> Result<()> { | |
| // Initialize tracing for logging | |
| let subscriber = FmtSubscriber::builder().with_max_level(Level::INFO).finish(); | |
| tracing::subscriber::set_global_default(subscriber)?; | |
| info!("Initializing transaction test"); | |
| // Instead of generating a random account, use a real funded account as in the reference. | |
| let (sender, private_key, mut account1) = | |
| setup_account("http://localhost:30732", "http://localhost:30731").await?; | |
| let (sender2, private_key2, account2) = | |
| setup_account("http://localhost:30732", "http://localhost:30731").await?; | |
| // Sleep for 20 seconds to allow funding to process. | |
| sleep(Duration::from_secs(23)).await; | |
| // Initialize Node Client | |
| let rest_client = Client::new(Url::parse("http://localhost:30731")?); | |
| let coin_client = CoinClient::new(&rest_client); | |
| println!( | |
| "Test Account: {:?}", | |
| coin_client | |
| .get_account_balance(&sender) | |
| .await | |
| .context("Failed to get Bob's account balance")? | |
| ); | |
| info!("Creating test transactions"); | |
| let mut txns = Vec::with_capacity(32); | |
| // Create 32 transactions with incrementing sequence numbers | |
| for i in 0..10 { | |
| let tx = create_send_coin_transaction( | |
| sender, | |
| sender2, | |
| 100, | |
| i, | |
| 1, // Incrementing time now with 1 second | |
| &private_key | |
| )?; | |
| txns.push(tx); | |
| } | |
| let response = rest_client.submit_batch(&txns).await?; | |
| info!("Batch of 10 transactions submitted successfully"); | |
| println!("Response: {:?}", response); | |
| sleep(Duration::from_secs(15)).await; | |
| println!( | |
| "Test Account: {:?}", | |
| coin_client | |
| .get_account_balance(&sender) | |
| .await | |
| .context("Failed to get Bob's account balance")? | |
| ); | |
| Ok(()) | |
| } | |
| #[tokio::main] | |
| async fn main() -> Result<()> { | |
| match test_transaction_sending().await { | |
| Ok(_) => { | |
| info!("Transaction test completed successfully"); | |
| Ok(()) | |
| } | |
| Err(e) => { | |
| eprintln!("Transaction test failed: {}", e); | |
| Err(e) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment