Skip to content

Commit

Permalink
[authority] Refactor AuthorityState creation to have a single creatio…
Browse files Browse the repository at this point in the history
…n point (MystenLabs#504)

* Refactor AuthorityState creation to have a single creation point

* Removed unnecessary already commented out code

* Removed a somewhat redundant AuthorityState constructor used for testing
  • Loading branch information
awelc authored Feb 22, 2022
1 parent eabb8de commit 4e7c20f
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 94 deletions.
4 changes: 3 additions & 1 deletion sui/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use rand::rngs::StdRng;
use rand::Rng;
use std::time::{Duration, Instant};
use structopt::StructOpt;
use sui_adapter::genesis;
use sui_core::{authority::*, authority_server::AuthorityServer};
use sui_network::{network::NetworkClient, transport};
use sui_types::crypto::{get_key_pair, AuthoritySignature};
Expand Down Expand Up @@ -163,11 +164,12 @@ impl ClientServerBenchmark {
let mut account_objects = Vec::new();
let mut gas_objects = Vec::new();
let state = rt.block_on(async {
let state = AuthorityState::new_with_genesis_modules(
let state = AuthorityState::new(
committee.clone(),
public_auth0,
Box::pin(secret_auth0),
store,
genesis::clone_genesis_modules(),
)
.await;
let mut rnd = <StdRng as rand::SeedableRng>::seed_from_u64(0);
Expand Down
27 changes: 19 additions & 8 deletions sui/src/sui_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async fn start_network(config: &NetworkConfig) -> Result<(), anyhow::Error> {
);

for authority in &config.authorities {
let server = make_server(authority, &committee, &[], config.buffer_size).await?;
let server = make_server(authority, &committee, vec![], &[], config.buffer_size).await?;
handles.push(async move {
let spawned_server = match server.spawn().await {
Ok(server) => server,
Expand Down Expand Up @@ -123,6 +123,7 @@ async fn genesis(
}

let mut new_addresses = Vec::new();
let mut preload_modules = Vec::new();
let mut preload_objects = Vec::new();

let new_account_count = genesis_conf
Expand Down Expand Up @@ -162,7 +163,7 @@ async fn genesis(
let sui_lib = sui_framework::get_sui_framework_modules(&genesis_conf.sui_framework_lib_path)?;
let lib_object =
Object::new_package(sui_lib, SuiAddress::default(), TransactionDigest::genesis());
preload_objects.push(lib_object);
preload_modules.push(lib_object);

info!(
"Loading Move framework lib from {:?}",
Expand All @@ -174,7 +175,7 @@ async fn genesis(
SuiAddress::default(),
TransactionDigest::genesis(),
);
preload_objects.push(lib_object);
preload_modules.push(lib_object);

// Build custom move packages
if !genesis_conf.move_packages.is_empty() {
Expand All @@ -197,7 +198,7 @@ async fn genesis(
info!("Loaded package [{}] from {:?}.", object.id(), path);
// Writing package id to network.conf for user to retrieve later.
config.loaded_move_packages.push((path, object.id()));
preload_objects.push(object)
preload_modules.push(object)
}
}

Expand All @@ -209,7 +210,14 @@ async fn genesis(
preload_objects.len()
);
for authority in &config.authorities {
make_server(authority, &committee, &preload_objects, config.buffer_size).await?;
make_server(
authority,
&committee,
preload_modules.clone(),
&preload_objects,
config.buffer_size,
)
.await?;
}

let wallet_path = working_dir.join("wallet.conf");
Expand All @@ -235,7 +243,8 @@ async fn genesis(
async fn make_server(
authority: &AuthorityPrivateInfo,
committee: &Committee,
pre_load_objects: &[Object],
preload_modules: Vec<Object>,
preload_objects: &[Object],
buffer_size: usize,
) -> SuiResult<AuthorityServer> {
let store = Arc::new(AuthorityStore::open(&authority.db_path, None));
Expand All @@ -246,9 +255,11 @@ async fn make_server(
name,
Box::pin(authority.key_pair.copy()),
store,
);
preload_modules,
)
.await;

for object in pre_load_objects {
for object in preload_objects {
state.init_order_lock(object.to_object_reference()).await;
state.insert_object(object.clone()).await;
}
Expand Down
42 changes: 4 additions & 38 deletions sui_core/src/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::{
pin::Pin,
sync::Arc,
};
use sui_adapter::{adapter, genesis};
use sui_adapter::adapter;
use sui_types::{
base_types::*,
committee::Committee,
Expand Down Expand Up @@ -468,32 +468,16 @@ impl AuthorityState {
})
}

pub fn new(
pub async fn new(
committee: Committee,
name: AuthorityName,
secret: StableSyncAuthoritySigner,
store: Arc<AuthorityStore>,
genesis_modules: Vec<Object>,
) -> Self {
let native_functions =
sui_framework::natives::all_natives(MOVE_STDLIB_ADDRESS, SUI_FRAMEWORK_ADDRESS);
AuthorityState {
committee,
name,
secret,
_native_functions: native_functions.clone(),
move_vm: adapter::new_move_vm(native_functions)
.expect("We defined natives to not fail here"),
_database: store,
}
}

pub async fn new_with_genesis_modules(
committee: Committee,
name: AuthorityName,
secret: StableSyncAuthoritySigner,
store: Arc<AuthorityStore>,
) -> Self {
let (genesis_modules, native_functions) = genesis::clone_genesis_data();
// let (genesis_modules, native_functions) = genesis::clone_genesis_data();
let state = AuthorityState {
committee,
name,
Expand All @@ -516,24 +500,6 @@ impl AuthorityState {
state
}

#[cfg(test)]
pub fn new_without_genesis_for_testing(
committee: Committee,
name: AuthorityName,
secret: StableSyncAuthoritySigner,
store: Arc<AuthorityStore>,
) -> Self {
let native_functions = NativeFunctionTable::new();
AuthorityState {
committee,
name,
secret,
_native_functions: native_functions.clone(),
move_vm: adapter::new_move_vm(native_functions).expect("Only fails due to natives."),
_database: store,
}
}

async fn get_object(&self, object_id: &ObjectID) -> Result<Option<Object>, SuiError> {
self._database.get_object(object_id)
}
Expand Down
23 changes: 14 additions & 9 deletions sui_core/src/unit_tests/authority_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ async fn test_publish_dependent_module_ok() {
let gas_payment_object = Object::with_id_owner_for_testing(gas_payment_object_id, sender);
let gas_payment_object_ref = gas_payment_object.to_object_reference();
// create a genesis state that contains the gas object and genesis modules
let (genesis_module_objects, _) = genesis::clone_genesis_data();
let genesis_module_objects = genesis::clone_genesis_modules();
let genesis_module = match &genesis_module_objects[0].data {
Data::Package(m) => CompiledModule::deserialize(m.values().next().unwrap()).unwrap(),
_ => unreachable!(),
Expand Down Expand Up @@ -447,7 +447,7 @@ async fn test_publish_non_existing_dependent_module() {
let gas_payment_object = Object::with_id_owner_for_testing(gas_payment_object_id, sender);
let gas_payment_object_ref = gas_payment_object.to_object_reference();
// create a genesis state that contains the gas object and genesis modules
let (genesis_module_objects, _) = genesis::clone_genesis_data();
let genesis_module_objects = genesis::clone_genesis_modules();
let genesis_module = match &genesis_module_objects[0].data {
Data::Package(m) => CompiledModule::deserialize(m.values().next().unwrap()).unwrap(),
_ => unreachable!(),
Expand Down Expand Up @@ -585,7 +585,7 @@ async fn test_handle_move_order_insufficient_budget() {
let gas_payment_object = Object::with_id_owner_for_testing(gas_payment_object_id, sender);
let gas_payment_object_ref = gas_payment_object.to_object_reference();
// find the function Object::create and call it to create a new object
let (genesis_package_objects, _) = genesis::clone_genesis_data();
let genesis_package_objects = genesis::clone_genesis_modules();
let package_object_ref =
get_genesis_package_by_module(&genesis_package_objects, "ObjectBasics");

Expand Down Expand Up @@ -1257,13 +1257,15 @@ async fn test_authority_persist() {
let mut opts = rocksdb::Options::default();
opts.set_max_open_files(max_files_authority_tests());
let store = Arc::new(AuthorityStore::open(&path, Some(opts)));
let authority = AuthorityState::new_without_genesis_for_testing(
let authority = AuthorityState::new(
committee.clone(),
*authority_key.public_key_bytes(),
// we assume that the node runner is in charge for its key -> it's ok to reopen a copy below.
Box::pin(authority_key.copy()),
store,
);
vec![],
)
.await;

// Create an object
let recipient = dbg_addr(2);
Expand All @@ -1283,12 +1285,14 @@ async fn test_authority_persist() {
let mut opts = rocksdb::Options::default();
opts.set_max_open_files(max_files_authority_tests());
let store = Arc::new(AuthorityStore::open(&path, Some(opts)));
let authority2 = AuthorityState::new_without_genesis_for_testing(
let authority2 = AuthorityState::new(
committee,
*authority_key.public_key_bytes(),
Box::pin(authority_key),
store,
);
vec![],
)
.await;
let obj2 = authority2.get_object(&object_id).await.unwrap().unwrap();

// Check the object is present
Expand Down Expand Up @@ -1667,11 +1671,12 @@ fn init_state_parameters() -> (Committee, SuiAddress, KeyPair, Arc<AuthorityStor
#[cfg(test)]
async fn init_state() -> AuthorityState {
let (committee, _, authority_key, store) = init_state_parameters();
AuthorityState::new_with_genesis_modules(
AuthorityState::new(
committee,
*authority_key.public_key_bytes(),
Box::pin(authority_key),
store,
genesis::clone_genesis_modules(),
)
.await
}
Expand Down Expand Up @@ -1809,7 +1814,7 @@ async fn call_framework_code(
object_arg_ids: Vec<ObjectID>,
pure_args: Vec<Vec<u8>>,
) -> SuiResult<OrderEffects> {
let (genesis_package_objects, _) = genesis::clone_genesis_data();
let genesis_package_objects = genesis::clone_genesis_modules();
let package_object_ref = get_genesis_package_by_module(&genesis_package_objects, module);

call_move(
Expand Down
7 changes: 5 additions & 2 deletions sui_core/src/unit_tests/client_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::{
convert::TryInto,
sync::Arc,
};
use sui_adapter::genesis;
use sui_types::crypto::get_key_pair;
use sui_types::crypto::Signature;
use sui_types::gas_coin::GasCoin;
Expand Down Expand Up @@ -278,11 +279,12 @@ async fn init_local_authorities(
let store = Arc::new(AuthorityStore::open(path, Some(opts)));
let authority_name = *secret.public_key_bytes();

let state = AuthorityState::new_with_genesis_modules(
let state = AuthorityState::new(
committee.clone(),
authority_name,
Box::pin(secret),
store,
genesis::clone_genesis_modules(),
)
.await;
clients.insert(authority_name, LocalAuthorityClient::new(state));
Expand Down Expand Up @@ -324,11 +326,12 @@ async fn init_local_authorities_bad_1(
let mut opts = rocksdb::Options::default();
opts.set_max_open_files(max_files_client_tests());
let store = Arc::new(AuthorityStore::open(path, Some(opts)));
let state = AuthorityState::new_with_genesis_modules(
let state = AuthorityState::new(
committee.clone(),
address,
Box::pin(secret),
store,
genesis::clone_genesis_modules(),
)
.await;
clients.insert(address, LocalAuthorityClient::new(state));
Expand Down
14 changes: 3 additions & 11 deletions sui_programmability/adapter/src/genesis.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use move_vm_runtime::native_functions::NativeFunctionTable;
use once_cell::sync::Lazy;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
Expand All @@ -10,7 +9,6 @@ use sui_types::error::SuiResult;
use sui_types::{
base_types::{SuiAddress, TransactionDigest},
object::Object,
MOVE_STDLIB_ADDRESS, SUI_FRAMEWORK_ADDRESS,
};

static GENESIS: Lazy<Mutex<Genesis>> = Lazy::new(|| {
Expand All @@ -19,28 +17,22 @@ static GENESIS: Lazy<Mutex<Genesis>> = Lazy::new(|| {

struct Genesis {
pub objects: Vec<Object>,
pub native_functions: NativeFunctionTable,
}

pub fn clone_genesis_data() -> (Vec<Object>, NativeFunctionTable) {
pub fn clone_genesis_modules() -> Vec<Object> {
let genesis = GENESIS.lock().unwrap();
(genesis.objects.clone(), genesis.native_functions.clone())
genesis.objects.clone()
}

/// Create and return objects wrapping the genesis modules for fastX
fn create_genesis_module_objects(lib_dir: &Path) -> SuiResult<Genesis> {
let sui_modules = sui_framework::get_sui_framework_modules(lib_dir)?;
let std_modules =
sui_framework::get_move_stdlib_modules(&lib_dir.join("deps").join("move-stdlib"))?;
let native_functions =
sui_framework::natives::all_natives(MOVE_STDLIB_ADDRESS, SUI_FRAMEWORK_ADDRESS);
let owner = SuiAddress::default();
let objects = vec![
Object::new_package(sui_modules, owner, TransactionDigest::genesis()),
Object::new_package(std_modules, owner, TransactionDigest::genesis()),
];
Ok(Genesis {
objects,
native_functions,
})
Ok(Genesis { objects })
}
Loading

0 comments on commit 4e7c20f

Please sign in to comment.