Skip to content

Commit

Permalink
[Epoch] Create Sui System State object at genesis (MystenLabs#2072)
Browse files Browse the repository at this point in the history
  • Loading branch information
lxfind authored May 19, 2022
1 parent 04d2379 commit 9e06e94
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 18 deletions.
1 change: 0 additions & 1 deletion sui/tests/shared_objects_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ async fn call_shared_object_contract() {
// the handles (or the authorities will stop).
let configs = test_authority_configs();
let _handles = spawn_test_authorities(gas_objects.clone(), &configs).await;

// Publish the move package to all authorities and get the new package ref.
tokio::task::yield_now().await;
tokio::time::sleep(std::time::Duration::from_secs(3)).await;
Expand Down
59 changes: 58 additions & 1 deletion sui_core/src/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use itertools::Itertools;
use move_binary_format::CompiledModule;
use move_bytecode_utils::module_cache::ModuleCache;
use move_core_types::{
account_address::AccountAddress,
ident_str,
language_storage::{ModuleId, StructTag},
resolver::{ModuleResolver, ResourceResolver},
};
Expand Down Expand Up @@ -704,7 +706,7 @@ impl AuthorityState {
sui_framework::natives::all_natives(MOVE_STDLIB_ADDRESS, SUI_FRAMEWORK_ADDRESS);

let mut state = AuthorityState {
committee,
committee: committee.clone(),
name,
secret,
_native_functions: native_functions.clone(),
Expand Down Expand Up @@ -745,6 +747,10 @@ impl AuthorityState {
state
.insert_genesis_objects_bulk_unsafe(&genesis.objects().iter().collect::<Vec<_>>())
.await;
state
.call_genesis_move_functions(&committee, &mut genesis_ctx)
.await
.unwrap();
}

// If a checkpoint store is present, ensure it is up-to-date with the latest
Expand Down Expand Up @@ -808,6 +814,57 @@ impl AuthorityState {
.expect("Cannot bulk insert genesis objects")
}

pub async fn call_genesis_move_functions(
&self,
committee: &Committee,
genesis_ctx: &mut TxContext,
) -> SuiResult {
let genesis_digest = genesis_ctx.digest();
let mut temporary_store =
AuthorityTemporaryStore::new(self.database.clone(), vec![], genesis_digest);
let pubkeys: Vec<Vec<u8>> = committee
.expanded_keys
.values()
.map(|pk| pk.to_bytes().to_vec())
.collect();
// TODO: May use separate sui address than derived from pubkey.
let sui_addresses: Vec<AccountAddress> = committee
.voting_rights
.keys()
.map(|pk| SuiAddress::from(pk).into())
.collect();
// TODO: Allow config to specify human readable validator names.
let names: Vec<Vec<u8>> = (0..sui_addresses.len())
.map(|i| Vec::from(format!("Validator{}", i).as_bytes()))
.collect();
// TODO: Change voting_rights to use u64 instead of usize.
let stakes: Vec<u64> = committee
.voting_rights
.values()
.map(|v| *v as u64)
.collect();
adapter::execute(
&self.move_vm,
&mut temporary_store,
ModuleId::new(SUI_FRAMEWORK_ADDRESS, ident_str!("Genesis").to_owned()),
&ident_str!("create").to_owned(),
vec![],
vec![
CallArg::Pure(bcs::to_bytes(&pubkeys).unwrap()),
CallArg::Pure(bcs::to_bytes(&sui_addresses).unwrap()),
CallArg::Pure(bcs::to_bytes(&names).unwrap()),
// TODO: below is netaddress, for now just use names as we don't yet want to expose them.
CallArg::Pure(bcs::to_bytes(&names).unwrap()),
CallArg::Pure(bcs::to_bytes(&stakes).unwrap()),
],
&mut SuiGasStatus::new_unmetered(),
genesis_ctx,
)?;
self.database
.update_objects_state_for_genesis(temporary_store, genesis_digest)
.await
}

/// Persist the Genesis package to DB along with the side effects for module initialization
async fn store_package_and_init_modules_for_genesis(
&self,
Expand Down
49 changes: 33 additions & 16 deletions sui_programmability/framework/sources/Governance/Genesis.move
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,44 @@ module Sui::Genesis {
/// Initial value of the upper-bound on the number of validators.
const INIT_MAX_VALIDATOR_COUNT: u64 = 100;

/// Basic information of Validator1, as an example, all dummy values.
const VALIDATOR1_SUI_ADDRESS: address = @0x1234;
const VALIDATOR1_PUBKEY_BYTES: vector<u8> = x"FF";
const VALIDATOR1_NAME: vector<u8> = b"Validator1";
const VALIDATOR1_IP_ADDRESS: vector<u8> = x"00FF00FF";
const VALIDATOR1_STAKE: u64 = 100000000000000;

/// This is a module initializer that runs during module publishing.
/// This function will be explicitly called once at genesis.
/// It will create a singleton SuiSystemState object, which contains
/// all the information we need in the system.
fun init(ctx: &mut TxContext) {
fun create(
validator_pubkeys: vector<vector<u8>>,
validator_sui_addresses: vector<address>,
validator_names: vector<vector<u8>>,
validator_net_addresses: vector<vector<u8>>,
validator_stakes: vector<u64>,
ctx: &mut TxContext,
) {
let treasury_cap = SUI::new(ctx);
let storage_fund = Coin::mint_balance(INIT_STORAGE_FUND, &mut treasury_cap);
let validators = Vector::empty();
Vector::push_back(&mut validators, Validator::new(
VALIDATOR1_SUI_ADDRESS,
VALIDATOR1_PUBKEY_BYTES,
VALIDATOR1_NAME,
VALIDATOR1_IP_ADDRESS,
Coin::mint_balance(VALIDATOR1_STAKE, &mut treasury_cap),
));
let count = Vector::length(&validator_pubkeys);
assert!(
Vector::length(&validator_sui_addresses) == count
&& Vector::length(&validator_stakes) == count
&& Vector::length(&validator_names) == count
&& Vector::length(&validator_net_addresses) == count,
1
);
let i = 0;
while (i < count) {
let sui_address = *Vector::borrow(&validator_sui_addresses, i);
let pubkey = *Vector::borrow(&validator_pubkeys, i);
let name = *Vector::borrow(&validator_names, i);
let net_address = *Vector::borrow(&validator_net_addresses, i);
let stake = *Vector::borrow(&validator_stakes, i);
Vector::push_back(&mut validators, Validator::new(
sui_address,
pubkey,
name,
net_address,
Coin::mint_balance(stake, &mut treasury_cap),
));
i = i + 1;
};
SuiSystem::create(
validators,
treasury_cap,
Expand Down

0 comments on commit 9e06e94

Please sign in to comment.