Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reconfig with dkg option A stake.move refactoring ver. A #11741

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 72 additions & 8 deletions aptos-move/e2e-move-tests/src/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use crate::{assert_success, build_package, AptosPackageHooks};
use anyhow::Error;
use aptos_cached_packages::aptos_stdlib;
use aptos_crypto::HashValue;
use aptos_framework::{natives::code::PackageMetadata, BuildOptions, BuiltPackage};
use aptos_gas_profiling::TransactionGasLog;
use aptos_gas_schedule::{
Expand All @@ -18,7 +19,7 @@ use aptos_types::{
account_address::AccountAddress,
account_config::{AccountResource, CoinStoreResource, CORE_CODE_ADDRESS},
contract_event::ContractEvent,
move_utils::MemberId,
move_utils::{as_move_value::AsMoveValue, MemberId},
on_chain_config::{FeatureFlag, GasScheduleV2, OnChainConfig},
state_store::{
state_key::StateKey,
Expand Down Expand Up @@ -607,19 +608,82 @@ impl MoveHarness {
.write_state_value(state_key, bcs::to_bytes(data).unwrap());
}

pub fn change_features_for_next_epoch(
&mut self,
enabled: Vec<FeatureFlag>,
disabled: Vec<FeatureFlag>,
) {
self.change_features_inner("change_feature_flags_for_next_epoch", enabled, disabled)
}

/// Enables features
pub fn enable_features(&mut self, enabled: Vec<FeatureFlag>, disabled: Vec<FeatureFlag>) {
self.change_features_inner("change_feature_flags", enabled, disabled)
}

pub fn change_features_inner(
&mut self,
func_variant: &str,
enabled: Vec<FeatureFlag>,
disabled: Vec<FeatureFlag>,
) {
let acc = self.aptos_framework_account();
let enabled = enabled.into_iter().map(|f| f as u64).collect::<Vec<_>>();
let disabled = disabled.into_iter().map(|f| f as u64).collect::<Vec<_>>();
self.executor.exec("features", func_variant, vec![], vec![
MoveValue::Signer(*acc.address())
.simple_serialize()
.unwrap(),
bcs::to_bytes(&enabled).unwrap(),
bcs::to_bytes(&disabled).unwrap(),
]);
}

pub fn finish_reconfig_with_dkg_result(&mut self, dkg_result: Vec<u8>) {
let args = vec![
MoveValue::Signer(AccountAddress::ONE),
dkg_result.as_move_value(),
];
let args_serialized = args
.into_iter()
.map(|mv| mv.simple_serialize().unwrap())
.collect();
self.executor.exec(
"reconfiguration_with_dkg",
"finish_with_dkg_result",
vec![],
args_serialized,
);
}

pub fn run_block_prologue_ext(
&mut self,
hash: HashValue,
epoch: u64,
round: u64,
proposer: AccountAddress,
failed_proposer_indices: Vec<u64>,
previous_block_votes_bitvec: Vec<u8>,
timestamp_us: u64,
randomness_seed: Option<Vec<u8>>,
) {
let args = vec![
MoveValue::Signer(AccountAddress::ZERO),
MoveValue::Address(AccountAddress::from_bytes(hash.to_vec()).unwrap()),
MoveValue::U64(epoch),
MoveValue::U64(round),
MoveValue::Address(proposer),
failed_proposer_indices.as_move_value(),
previous_block_votes_bitvec.as_move_value(),
timestamp_us.as_move_value(),
randomness_seed.as_move_value(),
];
let args_serialized = args
.into_iter()
.map(|mv| mv.simple_serialize().unwrap())
.collect();
self.executor
.exec("features", "change_feature_flags", vec![], vec![
MoveValue::Signer(*acc.address())
.simple_serialize()
.unwrap(),
bcs::to_bytes(&enabled).unwrap(),
bcs::to_bytes(&disabled).unwrap(),
]);
.exec("block", "block_prologue_ext", vec![], args_serialized);
}

fn override_one_gas_param(&mut self, param: &str, param_value: u64) {
Expand Down
1 change: 1 addition & 0 deletions aptos-move/e2e-move-tests/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod nft_dao;
mod offer_rotation_capability;
mod offer_signer_capability;
mod per_category_gas_limits;
mod reconfig_with_dkg;
mod resource_groups;
mod rotate_auth_key;
mod scripts;
Expand Down
173 changes: 173 additions & 0 deletions aptos-move/e2e-move-tests/src/tests/reconfig_with_dkg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// Copyright © Aptos Foundation

use crate::{assert_abort, assert_success, setup_staking, MoveHarness};
use aptos_crypto::HashValue;
use aptos_types::{
account_config::BlockResource,
dkg::{DKGSessionState, DKGState},
on_chain_config::{
ConfigurationResource, CurrentTimeMicroseconds, FeatureFlag, Features, OnChainConfig,
ValidatorSet,
},
};
use move_core_types::{account_address::AccountAddress, move_resource::MoveStructType};
use std::time::Duration;

struct HarnessWrapper {
cur_epoch: u64,
cur_round: u64,
cur_microseconds: u64,
epoch_interval: Duration,
validator_addrs: Vec<AccountAddress>,
pub inner: MoveHarness,
}

impl HarnessWrapper {
pub fn new() -> Self {
let inner = MoveHarness::new();
let mut ret = Self {
cur_epoch: 0,
cur_round: 0,
cur_microseconds: 0,
epoch_interval: Duration::ZERO,
validator_addrs: vec![],
inner,
};
ret.reload_metadata();
ret
}

fn reload_metadata(&mut self) {
let timestamp_resource = self.read::<CurrentTimeMicroseconds>();
let validator_set = self.read::<ValidatorSet>();
let configuration_resource = self.read::<ConfigurationResource>();
let epoch_interval = Duration::from_micros(
self.inner
.read_resource::<BlockResource>(&AccountAddress::ONE, BlockResource::struct_tag())
.unwrap()
.epoch_interval(),
);
let validator_addrs = validator_set
.active_validators
.iter()
.chain(validator_set.pending_inactive.iter())
.map(|vi| vi.account_address)
.collect();
self.cur_epoch = configuration_resource.epoch();
self.validator_addrs = validator_addrs;
self.cur_round = 0;
self.cur_microseconds = timestamp_resource.microseconds;
self.epoch_interval = epoch_interval;
}

pub fn fast_forward_a_bit(&mut self) {
self.cur_microseconds += 1000;
}

pub fn fast_forward_to_epoch_expiry(&mut self) {
self.cur_microseconds += self.epoch_interval.as_micros() as u64
}

pub fn fast_forward_then_default_block_prologue_ext(&mut self, to_epoch_expiry: bool) {
if to_epoch_expiry {
self.fast_forward_to_epoch_expiry();
} else {
self.fast_forward_a_bit();
}
self.cur_round += 1;
self.inner.run_block_prologue_ext(
HashValue::zero(),
self.cur_epoch,
self.cur_round,
self.validator_addrs[0],
vec![],
vec![],
self.cur_microseconds,
Some(vec![0; 32]),
);
}

pub fn read<T: OnChainConfig>(&mut self) -> T {
self.inner
.read_resource::<T>(&AccountAddress::ONE, T::struct_tag())
.unwrap()
}

#[allow(dead_code)]
pub fn finish_reconfig_with_dkg(&mut self) {
self.inner.finish_reconfig_with_dkg_result(vec![0xFF; 48]);
self.reload_metadata();
}
}

#[test]
fn reconfig_with_dkg_end_to_end() {
let mut harness_wrapper = HarnessWrapper::new();

harness_wrapper.fast_forward_then_default_block_prologue_ext(false);

// Alice asks to join.
let account_alice = harness_wrapper
.inner
.new_account_at(AccountAddress::from_hex_literal("0x1234").unwrap());
assert_success!(setup_staking(
&mut harness_wrapper.inner,
&account_alice,
1_000_000
));

// let account_bob = harness_wrapper.inner.new_account_at(AccountAddress::from_hex_literal("0x5678").unwrap());
// let account_carl = harness_wrapper.inner.new_account_at(AccountAddress::from_hex_literal("0x90ab").unwrap());

harness_wrapper.fast_forward_then_default_block_prologue_ext(false);

// Send a txn to disable a feature. It should not take effect immediately.
assert!(harness_wrapper
.read::<Features>()
.is_enabled(FeatureFlag::BLS12_381_STRUCTURES));
harness_wrapper
.inner
.change_features_for_next_epoch(vec![], vec![FeatureFlag::BLS12_381_STRUCTURES]);
assert!(harness_wrapper
.read::<Features>()
.is_enabled(FeatureFlag::BLS12_381_STRUCTURES));

// This block triggers reconfiguration.
harness_wrapper.fast_forward_then_default_block_prologue_ext(true);

let DKGState {
last_complete,
in_progress,
} = harness_wrapper.read::<DKGState>();
assert!(last_complete.is_none());
let DKGSessionState { metadata, .. } = in_progress.unwrap();
assert_eq!(2, metadata.target_validator_set.len());
assert_eq!(1, metadata.dealer_validator_set.len());
assert!(harness_wrapper
.read::<Features>()
.is_enabled(FeatureFlag::BLS12_381_STRUCTURES));

// DKG may last multiple rounds.
harness_wrapper.fast_forward_then_default_block_prologue_ext(false);

// Join request in the middle of a reconfiguration should be rejected.
let account_bob = harness_wrapper
.inner
.new_account_at(AccountAddress::from_hex_literal("0x5678").unwrap());
let join_status = setup_staking(&mut harness_wrapper.inner, &account_bob, 1_000_000);
assert_abort!(join_status, _);

// It is still possible to make changes to on-chain config buffer.
assert!(harness_wrapper
.read::<Features>()
.is_enabled(FeatureFlag::BN254_STRUCTURES));
harness_wrapper
.inner
.change_features_for_next_epoch(vec![], vec![FeatureFlag::BN254_STRUCTURES]);
assert!(harness_wrapper
.read::<Features>()
.is_enabled(FeatureFlag::BN254_STRUCTURES));

// TODO: fix the aggregator issue and complete it.
// harness_wrapper.finish_reconfig_with_dkg();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ script {
use aptos_framework::timestamp;
use aptos_std::fixed_point64;
use std::features;
use aptos_framework::aptos_governance::reconfigure;

fun main(core_resources: &signer) {
let framework_signer = aptos_governance::get_signer_testnet_only(core_resources, @aptos_framework);
Expand All @@ -18,6 +17,6 @@ script {
);
let feature = features::get_periodical_reward_rate_decrease_feature();
features::change_feature_flags(&framework_signer, vector[feature], vector[]);
reconfigure(&framework_signer);
aptos_governance::force_end_epoch(&framework_signer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ script {
let feature = features::get_collect_and_distribute_gas_fees_feature();

// Trigger reconfiguration first, to also sync all the fees to validators.
aptos_governance::reconfigure(&framework_signer);
aptos_governance::force_end_epoch(&framework_signer);

// Then, disable the feature.
features::change_feature_flags(&framework_signer, vector[], vector[feature]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ script {
features::change_feature_flags(&framework_signer, vector[feature], vector[]);

// Make sure to trigger a reconfiguration!
aptos_governance::reconfigure(&framework_signer);
aptos_governance::force_end_epoch(&framework_signer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ script {
stake::remove_validators(&framework_signer, &vector[addr]);

// Make sure to trigger a reconfiguration!
aptos_governance::reconfigure(&framework_signer);
aptos_governance::force_end_epoch(&framework_signer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ script {
transaction_fee::upgrade_burn_percentage(&framework_signer, burn_percentage);

// Make sure to trigger a reconfiguration!
aptos_governance::reconfigure(&framework_signer);
aptos_governance::force_end_epoch(&framework_signer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module 0xcafe::test {
",
);
builder.add_alias("std", "0x1");
builder.add_alias("vm", "0x0");
builder.add_local_dep("MoveStdlib", &move_stdlib.display().to_string());
let dir = builder.write_to_temp().unwrap();
assert_success!(h.publish_package(&acc, dir.path()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
script {
use aptos_framework::aptos_governance;
use std::features;
use aptos_framework::aptos_governance::reconfigure;

fun main(core_resources: &signer) {
let framework_signer = aptos_governance::get_signer_testnet_only(core_resources, @aptos_framework);
aptos_governance::initialize_partial_voting(&framework_signer);
let feature = features::get_partial_governance_voting();
features::change_feature_flags(&framework_signer, vector[feature], vector[]);
reconfigure(&framework_signer);
aptos_governance::force_end_epoch(&framework_signer);
}
}
Loading
Loading