Skip to content

Commit

Permalink
[AptosFramework] integrate staking framework into the system
Browse files Browse the repository at this point in the history
Integrate rust side with the new staking module, mainly the validator set.
  • Loading branch information
zekun000 authored and aptos-bot committed Apr 24, 2022
1 parent 36d6279 commit 05fea23
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl TransactionBenchState {
0,
1,
vec![],
*validator_set.payload()[0].account_address(),
*validator_set.payload().next().unwrap().account_address(),
);

state
Expand Down
2 changes: 1 addition & 1 deletion aptos-move/e2e-tests/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ impl FakeExecutor {
0,
self.block_time,
vec![],
*validator_set.payload()[0].account_address(),
*validator_set.payload().next().unwrap().account_address(),
);
let output = self
.execute_transaction_block(vec![Transaction::BlockMetadata(new_block)])
Expand Down
11 changes: 2 additions & 9 deletions aptos-move/framework/aptos-framework/sources/Block.move
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
module AptosFramework::Block {
use Std::Errors;
use Std::Event;
use AptosFramework::ValidatorSet;
use AptosFramework::Timestamp;
use AptosFramework::SystemAddresses;
use AptosFramework::Reconfiguration;
Expand Down Expand Up @@ -69,7 +68,7 @@ module AptosFramework::Block {

// Authorization
assert!(
proposer == @VMReserved || ValidatorSet::is_validator(proposer),
proposer == @VMReserved || Stake::is_current_validator(proposer),
Errors::requires_address(EVM_OR_VALIDATOR)
);

Expand All @@ -87,16 +86,10 @@ module AptosFramework::Block {
);

if (timestamp - Reconfiguration::last_reconfiguration_time() > block_metadata_ref.epoch_internal) {
on_new_epoch();
Reconfiguration::reconfigure();
}
}

/// Invoke all epoch callbacks.
fun on_new_epoch() {
Stake::on_new_epoch();
Reconfiguration::reconfigure();
}

/// Get the current block height
public fun get_current_block_height(): u64 acquires BlockMetadata {
assert!(is_initialized(), Errors::not_published(EBLOCK_METADATA));
Expand Down
87 changes: 32 additions & 55 deletions aptos-move/framework/aptos-framework/sources/Genesis.move
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ module AptosFramework::Genesis {
use AptosFramework::Stake;
use AptosFramework::TestCoin;
use AptosFramework::Timestamp;
use AptosFramework::ValidatorConfig;
use AptosFramework::ValidatorOperatorConfig;
use AptosFramework::VMConfig;

const EPOCH_INTERVAL: u64 = 86400000000; // one day in microseconds

fun initialize(
core_resource_account: signer,
core_resource_account_auth_key: vector<u8>,
Expand All @@ -30,6 +26,9 @@ module AptosFramework::Genesis {
initial_version: u64,
consensus_config: vector<u8>,
min_price_per_gas_unit: u64,
epoch_interval: u64,
minimum_stake: u64,
maximum_stake: u64,
) {
initialize_internal(
&core_resource_account,
Expand All @@ -42,6 +41,9 @@ module AptosFramework::Genesis {
initial_version,
consensus_config,
min_price_per_gas_unit,
epoch_interval,
minimum_stake,
maximum_stake,
)
}

Expand All @@ -56,6 +58,9 @@ module AptosFramework::Genesis {
initial_version: u64,
consensus_config: vector<u8>,
min_price_per_gas_unit: u64,
epoch_interval: u64,
minimum_stake: u64,
maximum_stake: u64,
) {
// initialize the core resource account
Account::initialize(
Expand All @@ -80,7 +85,7 @@ module AptosFramework::Genesis {
ConsensusConfig::initialize(core_resource_account);
ValidatorSet::initialize_validator_set(core_resource_account);
Version::initialize(core_resource_account, initial_version);
Stake::initialize_validator_set(core_resource_account, 0, 1000000000);
Stake::initialize_validator_set(core_resource_account, minimum_stake, maximum_stake);

VMConfig::initialize(
core_resource_account,
Expand All @@ -105,93 +110,62 @@ module AptosFramework::Genesis {
// this needs to be called at the very end
ChainId::initialize(core_resource_account, chain_id);
Reconfiguration::initialize(core_resource_account);
Block::initialize_block_metadata(core_resource_account, EPOCH_INTERVAL);
Block::initialize_block_metadata(core_resource_account, epoch_interval);
Timestamp::set_time_has_started(core_resource_account);
}

/// Sets up the initial validator set for the network.
/// The validator "owner" accounts, their UTF-8 names, and their authentication
/// keys are encoded in the `owners`, `owner_names`, and `owner_auth_key` vectors.
/// The validator "owner" accounts, and their authentication
/// keys are encoded in the `owners` and `owner_auth_key` vectors.
/// Each validator signs consensus messages with the private key corresponding to the Ed25519
/// public key in `consensus_pubkeys`.
/// Each validator owner has its operation delegated to an "operator" (which may be
/// the owner). The operators, their names, and their authentication keys are encoded
/// in the `operators`, `operator_names`, and `operator_auth_keys` vectors.
/// Finally, each validator must specify the network address
/// (see types/src/network_address/mod.rs) for itself and its full nodes.
fun create_initialize_owners_operators(
fun create_initialize_validators(
core_resource_account: signer,
owners: vector<signer>,
owner_names: vector<vector<u8>>,
owners: vector<address>,
owner_auth_keys: vector<vector<u8>>,
consensus_pubkeys: vector<vector<u8>>,
operators: vector<signer>,
operator_names: vector<vector<u8>>,
operator_auth_keys: vector<vector<u8>>,
validator_network_addresses: vector<vector<u8>>,
full_node_network_addresses: vector<vector<u8>>,
staking_distribution: vector<u64>,
) {
let num_owners = Vector::length(&owners);
let num_owner_names = Vector::length(&owner_names);
assert!(num_owners == num_owner_names, 0);
let num_owner_keys = Vector::length(&owner_auth_keys);
assert!(num_owner_names == num_owner_keys, 0);
let num_operators = Vector::length(&operators);
assert!(num_owner_keys == num_operators, 0);
let num_operator_names = Vector::length(&operator_names);
assert!(num_operators == num_operator_names, 0);
let num_operator_keys = Vector::length(&operator_auth_keys);
assert!(num_operator_names == num_operator_keys, 0);
assert!(num_owners == num_owner_keys, 0);
let num_validator_network_addresses = Vector::length(&validator_network_addresses);
assert!(num_operator_keys == num_validator_network_addresses, 0);
assert!(num_owner_keys == num_validator_network_addresses, 0);
let num_full_node_network_addresses = Vector::length(&full_node_network_addresses);
assert!(num_validator_network_addresses == num_full_node_network_addresses, 0);
let num_staking = Vector::length(&staking_distribution);
assert!(num_full_node_network_addresses == num_staking, 0);

let i = 0;
while (i < num_owners) {
let owner = Vector::borrow(&owners, i);
let owner_address = Signer::address_of(owner);
let owner_name = *Vector::borrow(&owner_names, i);
// create each validator account and rotate its auth key to the correct value
Account::create_validator_account_internal(
&core_resource_account, owner_address, owner_name
);
let (owner_account, _) = Account::create_account_internal(*owner);

let owner_auth_key = *Vector::borrow(&owner_auth_keys, i);
Account::rotate_authentication_key_internal(owner, owner_auth_key);

let operator = Vector::borrow(&operators, i);
let operator_address = Signer::address_of(operator);
let operator_name = *Vector::borrow(&operator_names, i);
// create the operator account + rotate its auth key if it does not already exist
if (!Account::exists_at(operator_address)) {
Account::create_validator_operator_account_internal(
&core_resource_account, operator_address, copy operator_name
);
let operator_auth_key = *Vector::borrow(&operator_auth_keys, i);
Account::rotate_authentication_key_internal(operator, operator_auth_key);
};
// assign the operator to its validator
assert!(ValidatorOperatorConfig::get_human_name(operator_address) == operator_name, 0);
ValidatorConfig::set_operator(owner, operator_address);
Account::rotate_authentication_key_internal(&owner_account, owner_auth_key);

// use the operator account set up the validator config
let validator_network_address = *Vector::borrow(&validator_network_addresses, i);
let full_node_network_address = *Vector::borrow(&full_node_network_addresses, i);
let consensus_pubkey = *Vector::borrow(&consensus_pubkeys, i);
ValidatorConfig::set_config(
operator,
owner_address,
Stake::register_validator_candidate(
&owner_account,
consensus_pubkey,
validator_network_address,
full_node_network_address
);

// finally, add this validator to the validator set
ValidatorSet::add_validator_internal(&core_resource_account, owner_address);
let amount = *Vector::borrow(&staking_distribution, i);
Stake::delegate_stake(&core_resource_account, *owner, amount, 100000);
Stake::join_validator_set(&owner_account);

i = i + 1;
}
};
Stake::on_new_epoch();
}

#[test_only]
Expand All @@ -207,6 +181,9 @@ module AptosFramework::Genesis {
0,
x"",
1,
0,
0,
0
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module AptosFramework::Reconfiguration {
use Std::GUID;
use AptosFramework::SystemAddresses;
use AptosFramework::Timestamp;
use AptosFramework::Stake;

friend AptosFramework::Block;
// TODO: migrate all to callback in block prologue
Expand Down Expand Up @@ -91,6 +92,7 @@ module AptosFramework::Reconfiguration {

/// Signal validators to start using new configuration. Must be called from friend config modules.
public(friend) fun reconfigure() acquires Configuration {
Stake::on_new_epoch();
reconfigure_();
}

Expand Down
Loading

0 comments on commit 05fea23

Please sign in to comment.