|
| 1 | +// Copyright (c) Aptos Foundation |
| 2 | +// Licensed pursuant to the Innovation-Enabling Source Code License, available at https://github.com/aptos-labs/aptos-core/blob/main/LICENSE |
| 3 | + |
| 4 | +use crate::smoke_test_environment::SwarmBuilder; |
| 5 | +use aptos_cached_packages::aptos_stdlib; |
| 6 | +use aptos_crypto::{ |
| 7 | + slh_dsa_sha2_128s::{PrivateKey, PublicKey}, |
| 8 | + traits::Uniform, |
| 9 | +}; |
| 10 | +use aptos_forge::{AptosPublicInfo, LocalSwarm, Swarm}; |
| 11 | +use aptos_logger::{debug, info}; |
| 12 | +use aptos_types::{ |
| 13 | + on_chain_config::{FeatureFlag, Features}, |
| 14 | + transaction::authenticator::{AnyPublicKey, AuthenticationKey}, |
| 15 | +}; |
| 16 | +use rand::rngs::OsRng; |
| 17 | +use std::sync::Arc; |
| 18 | + |
| 19 | +#[tokio::test] |
| 20 | +async fn test_slh_dsa_feature_disabled() { |
| 21 | + slh_dsa_scenario(false).await |
| 22 | +} |
| 23 | + |
| 24 | +#[tokio::test] |
| 25 | +async fn test_slh_dsa_feature_enabled() { |
| 26 | + slh_dsa_scenario(true).await |
| 27 | +} |
| 28 | + |
| 29 | +/// Config the chain, run an SLH-DSA txn, and assert txn result. |
| 30 | +async fn slh_dsa_scenario(enable_feature: bool) { |
| 31 | + let (swarm, mut info) = setup_local_net(enable_feature).await; |
| 32 | + |
| 33 | + // Generate SLH-DSA keypair |
| 34 | + let mut rng = OsRng; |
| 35 | + let private_key = PrivateKey::generate(&mut rng); |
| 36 | + let public_key: PublicKey = (&private_key).into(); |
| 37 | + |
| 38 | + // Create account address from public key |
| 39 | + let auth_key = AuthenticationKey::any_key(AnyPublicKey::slh_dsa_sha2_128s(public_key.clone())); |
| 40 | + let account_address = auth_key.account_address(); |
| 41 | + |
| 42 | + info!( |
| 43 | + "SLH-DSA account address: {}", |
| 44 | + account_address.to_hex_literal() |
| 45 | + ); |
| 46 | + |
| 47 | + // Create the account on-chain |
| 48 | + info.sync_root_account_sequence_number().await; |
| 49 | + info.create_user_account_with_any_key(&AnyPublicKey::slh_dsa_sha2_128s(public_key.clone())) |
| 50 | + .await |
| 51 | + .unwrap(); |
| 52 | + info.sync_root_account_sequence_number().await; |
| 53 | + |
| 54 | + // Fund the account |
| 55 | + info.mint(account_address, 10_000_000_000).await.unwrap(); |
| 56 | + info.sync_root_account_sequence_number().await; |
| 57 | + |
| 58 | + // Create a recipient account |
| 59 | + let recipient = info |
| 60 | + .create_and_fund_user_account(20_000_000_000) |
| 61 | + .await |
| 62 | + .unwrap(); |
| 63 | + |
| 64 | + // Create and sign a transaction |
| 65 | + let raw_txn = info |
| 66 | + .transaction_factory() |
| 67 | + .payload(aptos_stdlib::aptos_coin_transfer( |
| 68 | + recipient.address(), |
| 69 | + 1_000_000, |
| 70 | + )) |
| 71 | + .sender(account_address) |
| 72 | + .sequence_number(0) |
| 73 | + .build(); |
| 74 | + |
| 75 | + let signed_txn = raw_txn |
| 76 | + .sign_slh_dsa_sha2_128s(&private_key, public_key) |
| 77 | + .unwrap() |
| 78 | + .into_inner(); |
| 79 | + |
| 80 | + // Submit the transaction |
| 81 | + info!( |
| 82 | + "Submitting SLH-DSA transaction (feature enabled: {})", |
| 83 | + enable_feature |
| 84 | + ); |
| 85 | + let result = swarm |
| 86 | + .aptos_public_info() |
| 87 | + .client() |
| 88 | + .submit_without_deserializing_response(&signed_txn) |
| 89 | + .await; |
| 90 | + |
| 91 | + debug!("result={:?}", result); |
| 92 | + |
| 93 | + if enable_feature { |
| 94 | + if let Err(e) = result { |
| 95 | + panic!( |
| 96 | + "SLH-DSA transaction should have succeeded when feature is enabled, but got error: {:?}", |
| 97 | + e |
| 98 | + ); |
| 99 | + } |
| 100 | + } else { |
| 101 | + if result.is_ok() { |
| 102 | + panic!("SLH-DSA transaction should have failed with FEATURE_UNDER_GATING when feature is disabled"); |
| 103 | + } |
| 104 | + |
| 105 | + // Verify the error is FEATURE_UNDER_GATING |
| 106 | + let error = result.unwrap_err(); |
| 107 | + let error_msg = format!("{:?}", error); |
| 108 | + assert!( |
| 109 | + error_msg.contains("FEATURE_UNDER_GATING"), |
| 110 | + "Expected FEATURE_UNDER_GATING error, but got: {:?}", |
| 111 | + error |
| 112 | + ); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +async fn setup_local_net(enable_slh_dsa: bool) -> (LocalSwarm, AptosPublicInfo) { |
| 117 | + let (swarm, mut _cli, _faucet) = SwarmBuilder::new_local(1) |
| 118 | + .with_init_genesis_config(Arc::new(move |conf| { |
| 119 | + let mut features = Features::default(); |
| 120 | + if enable_slh_dsa { |
| 121 | + features.enable(FeatureFlag::SLH_DSA_SHA2_128S_SIGNATURE); |
| 122 | + } else { |
| 123 | + features.disable(FeatureFlag::SLH_DSA_SHA2_128S_SIGNATURE); |
| 124 | + } |
| 125 | + conf.initial_features_override = Some(features); |
| 126 | + })) |
| 127 | + .with_aptos() |
| 128 | + .build_with_cli(0) |
| 129 | + .await; |
| 130 | + |
| 131 | + let info = swarm.aptos_public_info(); |
| 132 | + (swarm, info) |
| 133 | +} |
0 commit comments