Skip to content

Commit

Permalink
more smoke tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zjma committed Feb 29, 2024
1 parent a4aca4a commit b44960f
Show file tree
Hide file tree
Showing 13 changed files with 1,058 additions and 1 deletion.
9 changes: 9 additions & 0 deletions aptos-move/move-examples/on_chain_dice/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "OnChainDice"
version = "0.0.0"

[addresses]
module_owner = "_"

[dependencies]
AptosFramework = { local = "../../framework/aptos-framework" }
21 changes: 21 additions & 0 deletions aptos-move/move-examples/on_chain_dice/sources/dice.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module module_owner::dice {
use std::signer::address_of;
use std::vector;
use aptos_framework::randomness;

struct DiceRollHistory has key {
rolls: vector<u64>,
}

entry fun roll(account: signer) acquires DiceRollHistory {
let addr = address_of(&account);
let roll_history = if (exists<DiceRollHistory>(addr)) {
move_from<DiceRollHistory>(addr)
} else {
DiceRollHistory { rolls: vector[] }
};
let new_roll = randomness::u64_range(0, 6);
vector::push_back(&mut roll_history.rolls, new_roll);
move_to(&account, roll_history);
}
}
13 changes: 12 additions & 1 deletion crates/aptos/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ use std::{
time::Duration,
};
use tempfile::TempDir;
use thiserror::__private::AsDisplay;
use tokio::time::{sleep, Instant};

#[cfg(test)]
Expand Down Expand Up @@ -470,6 +471,16 @@ impl CliTestFramework {
.await
}

pub fn add_file_in_package(&self, rel_path: &str, content: String) {
let source_path = self.move_dir().join(rel_path);
write_to_file(
source_path.as_path(),
&source_path.as_display().to_string(),
content.as_bytes(),
)
.unwrap();
}

pub async fn update_validator_network_addresses(
&self,
operator_index: usize,
Expand Down Expand Up @@ -1026,7 +1037,7 @@ impl CliTestFramework {
.await
}

fn aptos_framework_dir() -> PathBuf {
pub fn aptos_framework_dir() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("..")
.join("..")
Expand Down
101 changes: 101 additions & 0 deletions testsuite/smoke-test/src/randomness/disable_feature_0.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright © Aptos Foundation

use crate::{
randomness::{decrypt_key_map, get_on_chain_resource, verify_dkg_transcript},
smoke_test_environment::SwarmBuilder,
};
use aptos_forge::{Node, Swarm, SwarmExt};
use aptos_logger::{debug, info};
use aptos_types::{
dkg::DKGState,
on_chain_config::{FeatureFlag, Features},
randomness::PerBlockRandomness,
};
use std::{sync::Arc, time::Duration};

/// Disable on-chain randomness by only disabling feature `RECONFIGURE_WITH_DKG`.
#[tokio::test]
async fn disable_feature_0() {
let epoch_duration_secs = 20;

let (mut swarm, mut cli, _faucet) = SwarmBuilder::new_local(4)
.with_num_fullnodes(1)
.with_aptos()
.with_init_genesis_config(Arc::new(move |conf| {
conf.epoch_duration_secs = epoch_duration_secs;
conf.allow_new_validators = true;

// Ensure vtxn is enabled.
conf.consensus_config.enable_validator_txns();

// Ensure randomness flag is set.
let mut features = Features::default();
features.enable(FeatureFlag::RECONFIGURE_WITH_DKG);
conf.initial_features_override = Some(features);
}))
.build_with_cli(0)
.await;

let root_addr = swarm.chain_info().root_account().address();
let root_idx = cli.add_account_with_address_to_cli(swarm.root_key(), root_addr);

let decrypt_key_map = decrypt_key_map(&swarm);

let client_endpoint = swarm.validators().nth(1).unwrap().rest_api_endpoint();
let client = aptos_rest_client::Client::new(client_endpoint.clone());

swarm
.wait_for_all_nodes_to_catchup_to_epoch(3, Duration::from_secs(epoch_duration_secs * 2))
.await
.expect("Waited too long for epoch 3.");

info!("Now in epoch 3. Disabling feature RECONFIGURE_WITH_DKG.");
let disable_dkg_script = r#"
script {
use aptos_framework::aptos_governance;
fun main(core_resources: &signer) {
let framework_signer = aptos_governance::get_signer_testnet_only(core_resources, @0000000000000000000000000000000000000000000000000000000000000001);
let dkg_feature_id: u64 = std::features::get_reconfigure_with_dkg_feature();
aptos_governance::toggle_features(&framework_signer, vector[], vector[dkg_feature_id]);
}
}
"#;

let txn_summary = cli
.run_script(root_idx, disable_dkg_script)
.await
.expect("Txn execution error.");
debug!("disabling_dkg_summary={:?}", txn_summary);

swarm
.wait_for_all_nodes_to_catchup_to_epoch(4, Duration::from_secs(epoch_duration_secs * 2))
.await
.expect("Waited too long for epoch 4.");

info!("Now in epoch 4. DKG transcript should still be available. Randomness seed should be unavailable.");
let dkg_session = get_on_chain_resource::<DKGState>(&client)
.await
.last_completed
.expect("dkg result for epoch 4 should be present");
assert_eq!(4, dkg_session.target_epoch());
assert!(verify_dkg_transcript(&dkg_session, &decrypt_key_map).is_ok());

let randomness_seed = get_on_chain_resource::<PerBlockRandomness>(&client).await;
assert!(randomness_seed.seed.is_none());

swarm
.wait_for_all_nodes_to_catchup_to_epoch(5, Duration::from_secs(epoch_duration_secs * 2))
.await
.expect("Waited too long for epoch 5.");

info!("Now in epoch 5. DKG transcript should be unavailable. Randomness seed should be unavailable.");
let maybe_last_complete = get_on_chain_resource::<DKGState>(&client)
.await
.last_completed;
assert!(
maybe_last_complete.is_none() || maybe_last_complete.as_ref().unwrap().target_epoch() != 5
);

let randomness_seed = get_on_chain_resource::<PerBlockRandomness>(&client).await;
assert!(randomness_seed.seed.is_none());
}
111 changes: 111 additions & 0 deletions testsuite/smoke-test/src/randomness/disable_feature_1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright © Aptos Foundation

use crate::{
randomness::{decrypt_key_map, get_on_chain_resource, verify_dkg_transcript},
smoke_test_environment::SwarmBuilder,
utils::get_current_consensus_config,
};
use aptos_forge::{Node, Swarm, SwarmExt};
use aptos_logger::{debug, info};
use aptos_types::{
dkg::DKGState,
on_chain_config::{FeatureFlag, Features},
randomness::PerBlockRandomness,
};
use std::{sync::Arc, time::Duration};

/// Disable on-chain randomness by only disabling validator transactions.
#[tokio::test]
async fn disable_feature_1() {
let epoch_duration_secs = 20;

let (mut swarm, mut cli, _faucet) = SwarmBuilder::new_local(4)
.with_num_fullnodes(1)
.with_aptos()
.with_init_genesis_config(Arc::new(move |conf| {
conf.epoch_duration_secs = epoch_duration_secs;
conf.allow_new_validators = true;

// Ensure vtxn is enabled.
conf.consensus_config.enable_validator_txns();

// Ensure randomness flag is set.
let mut features = Features::default();
features.enable(FeatureFlag::RECONFIGURE_WITH_DKG);
conf.initial_features_override = Some(features);
}))
.build_with_cli(0)
.await;

let root_addr = swarm.chain_info().root_account().address();
let root_idx = cli.add_account_with_address_to_cli(swarm.root_key(), root_addr);

let decrypt_key_map = decrypt_key_map(&swarm);

let client_endpoint = swarm.validators().nth(1).unwrap().rest_api_endpoint();
let client = aptos_rest_client::Client::new(client_endpoint.clone());

swarm
.wait_for_all_nodes_to_catchup_to_epoch(3, Duration::from_secs(epoch_duration_secs * 2))
.await
.expect("Waited too long for epoch 3.");

info!("Now in epoch 3. Disabling validator transactions.");
let mut config = get_current_consensus_config(&client).await;
assert!(config.is_vtxn_enabled());
config.disable_validator_txns();
let config_bytes = bcs::to_bytes(&config).unwrap();
let disable_vtxn_script = format!(
r#"
script {{
use aptos_framework::aptos_governance;
use aptos_framework::consensus_config;
fun main(core_resources: &signer) {{
let framework_signer = aptos_governance::get_signer_testnet_only(core_resources, @0000000000000000000000000000000000000000000000000000000000000001);
let config_bytes = vector{:?};
consensus_config::set_for_next_epoch(&framework_signer, config_bytes);
aptos_governance::reconfigure(&framework_signer);
}}
}}
"#,
config_bytes
);
debug!("disable_vtxn_script={}", disable_vtxn_script);
let txn_summary = cli
.run_script(root_idx, disable_vtxn_script.as_str())
.await
.expect("Txn execution error.");
debug!("disabling_vtxn_summary={:?}", txn_summary);

swarm
.wait_for_all_nodes_to_catchup_to_epoch(4, Duration::from_secs(epoch_duration_secs * 2))
.await
.expect("Waited too long for epoch 4.");

info!("Now in epoch 4. DKG transcript should still be available. Randomness seed should be unavailable.");
let dkg_session = get_on_chain_resource::<DKGState>(&client)
.await
.last_completed
.expect("dkg result for epoch 4 should be present");
assert_eq!(4, dkg_session.target_epoch());
assert!(verify_dkg_transcript(&dkg_session, &decrypt_key_map).is_ok());

let randomness_seed = get_on_chain_resource::<PerBlockRandomness>(&client).await;
assert!(randomness_seed.seed.is_none());

swarm
.wait_for_all_nodes_to_catchup_to_epoch(5, Duration::from_secs(epoch_duration_secs * 2))
.await
.expect("Waited too long for epoch 5.");

info!("Now in epoch 5. DKG transcript should be unavailable. Randomness seed should be unavailable.");
let maybe_last_complete = get_on_chain_resource::<DKGState>(&client)
.await
.last_completed;
assert!(
maybe_last_complete.is_none() || maybe_last_complete.as_ref().unwrap().target_epoch() != 5
);

let randomness_seed = get_on_chain_resource::<PerBlockRandomness>(&client).await;
assert!(randomness_seed.seed.is_none());
}
59 changes: 59 additions & 0 deletions testsuite/smoke-test/src/randomness/dkg_with_validator_down.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright © Aptos Foundation

use crate::{
randomness::{decrypt_key_map, verify_dkg_transcript, wait_for_dkg_finish},
smoke_test_environment::SwarmBuilder,
};
use aptos_forge::NodeExt;
use aptos_types::on_chain_config::{FeatureFlag, Features};
use std::sync::Arc;

#[tokio::test]
async fn dkg_with_validator_down() {
let epoch_duration_secs = 10;
let estimated_dkg_latency_secs = 20;
let time_limit_secs = epoch_duration_secs + estimated_dkg_latency_secs;

let mut swarm = SwarmBuilder::new_local(4)
.with_num_fullnodes(1)
.with_aptos()
.with_init_genesis_config(Arc::new(|conf| {
conf.epoch_duration_secs = 10;

// Ensure vtxn is enabled.
conf.consensus_config.enable_validator_txns();

// Ensure randomness flag is set.
let mut features = Features::default();
features.enable(FeatureFlag::RECONFIGURE_WITH_DKG);
conf.initial_features_override = Some(features);
}))
.build()
.await;
let decrypt_key_map = decrypt_key_map(&swarm);

let client = swarm.validators().last().unwrap().rest_client();
println!("Wait for an epoch start.");
let dkg_session_1 = wait_for_dkg_finish(&client, None, time_limit_secs).await;

println!("Current epoch is {}.", dkg_session_1.target_epoch());

println!("Take one validator down.");
swarm.validators_mut().take(1).for_each(|v| {
v.stop();
});

println!(
"Wait until we fully entered epoch {}.",
dkg_session_1.target_epoch() + 1
);

let dkg_session_2 = wait_for_dkg_finish(
&client,
Some(dkg_session_1.target_epoch() + 1),
time_limit_secs,
)
.await;

assert!(verify_dkg_transcript(&dkg_session_2, &decrypt_key_map).is_ok());
}
Loading

0 comments on commit b44960f

Please sign in to comment.