Skip to content

Commit

Permalink
initialize modules
Browse files Browse the repository at this point in the history
  • Loading branch information
666lcz committed Mar 1, 2022
1 parent d9b6966 commit 6822e1c
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 82 deletions.
1 change: 1 addition & 0 deletions sui/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ impl ClientServerBenchmark {
Box::pin(secret_auth0),
store,
genesis::clone_genesis_modules(),
&mut genesis::create_genesis_context(),
)
.await;
let mut rnd = <StdRng as rand::SeedableRng>::seed_from_u64(0);
Expand Down
67 changes: 40 additions & 27 deletions sui/src/sui_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ use crate::config::{
};
use anyhow::anyhow;
use futures::future::join_all;
use move_binary_format::CompiledModule;
use move_package::BuildConfig;
use std::collections::BTreeMap;
use std::path::PathBuf;
use std::sync::Arc;
use structopt::StructOpt;
use sui_adapter::genesis;
use sui_core::authority::{AuthorityState, AuthorityStore};
use sui_core::authority_server::AuthorityServer;
use sui_types::base_types::{SequenceNumber, SuiAddress, TransactionDigest, TxContext};
use sui_types::base_types::{SequenceNumber, TxContext};

use sui_adapter::adapter::generate_package_id;
use sui_types::committee::Committee;
Expand Down Expand Up @@ -125,7 +127,7 @@ pub async fn genesis(
}

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

let new_account_count = genesis_conf
Expand Down Expand Up @@ -157,23 +159,22 @@ pub async fn genesis(
}
}

info!(
"Loading Move framework lib from {:?}",
genesis_conf.move_framework_lib_path
);
let move_lib = sui_framework::get_move_stdlib_modules(&genesis_conf.move_framework_lib_path)?;
preload_modules.push(move_lib);

// Load Sui and Move framework lib
info!(
"Loading Sui framework lib from {:?}",
genesis_conf.sui_framework_lib_path
);
let sui_lib = sui_framework::get_sui_framework_modules(&genesis_conf.sui_framework_lib_path)?;
let lib_object = Object::new_package(sui_lib, TransactionDigest::genesis());
preload_modules.push(lib_object);

info!(
"Loading Move framework lib from {:?}",
genesis_conf.move_framework_lib_path
);
let move_lib = sui_framework::get_move_stdlib_modules(&genesis_conf.move_framework_lib_path)?;
let lib_object = Object::new_package(move_lib, TransactionDigest::genesis());
preload_modules.push(lib_object);
preload_modules.push(sui_lib);

let mut genesis_ctx = genesis::create_genesis_context();
// Build custom move packages
if !genesis_conf.move_packages.is_empty() {
info!(
Expand All @@ -185,33 +186,24 @@ pub async fn genesis(
for path in genesis_conf.move_packages {
let mut modules =
sui_framework::build_move_package(&path, BuildConfig::default(), false)?;
let package_id = generate_package_id(
&mut modules,
&mut TxContext::new(&SuiAddress::default(), TransactionDigest::genesis()),
)?;
let package_id = generate_package_id(&mut modules, &mut genesis_ctx)?;

let object = Object::new_package(modules, TransactionDigest::genesis());
info!("Loaded package [{}] from {:?}.", object.id(), path);
info!("Loaded package [{}] from {:?}.", package_id, path);
// Writing package id to network.conf for user to retrieve later.
config.loaded_move_packages.push((path, package_id));
preload_modules.push(object)
preload_modules.push(modules)
}
}

let committee = Committee::new(voting_right);

// Make server state to persist the objects and modules.
info!(
"Preloading {} objects to authorities.",
preload_objects.len()
);
for authority in &config.authorities {
make_server(
make_server_with_genesis_ctx(
authority,
&committee,
preload_modules.clone(),
&preload_objects,
config.buffer_size,
&mut genesis_ctx,
)
.await?;
}
Expand All @@ -235,9 +227,29 @@ pub async fn genesis(
pub async fn make_server(
authority: &AuthorityPrivateInfo,
committee: &Committee,
preload_modules: Vec<Object>,
preload_modules: Vec<Vec<CompiledModule>>,
preload_objects: &[Object],
buffer_size: usize,
) -> SuiResult<AuthorityServer> {
let mut genesis_ctx = genesis::create_genesis_context();
make_server_with_genesis_ctx(
authority,
committee,
preload_modules,
preload_objects,
buffer_size,
&mut genesis_ctx,
)
.await
}

async fn make_server_with_genesis_ctx(
authority: &AuthorityPrivateInfo,
committee: &Committee,
preload_modules: Vec<Vec<CompiledModule>>,
preload_objects: &[Object],
buffer_size: usize,
genesis_ctx: &mut TxContext,
) -> SuiResult<AuthorityServer> {
let store = Arc::new(AuthorityStore::open(&authority.db_path, None));
let name = *authority.key_pair.public_key_bytes();
Expand All @@ -248,6 +260,7 @@ pub async fn make_server(
Box::pin(authority.key_pair.copy()),
store,
preload_modules,
genesis_ctx,
)
.await;

Expand Down
4 changes: 2 additions & 2 deletions sui/src/unit_tests/sui_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use move_binary_format::normalized::Type;
use move_core_types::{account_address::AccountAddress, identifier::Identifier};
use serde_json::{json, Value};
use sui_adapter::{self, genesis::clone_genesis_modules};
use sui_adapter::{self, genesis::clone_genesis_objects};
use sui_types::{
base_types::{ObjectID, SuiAddress},
SUI_FRAMEWORK_ADDRESS,
Expand Down Expand Up @@ -223,7 +223,7 @@ fn test_basic_args_linter_pure_args() {

#[test]
fn test_basic_args_linter_top_level() {
let genesis_objs = clone_genesis_modules();
let genesis_objs = clone_genesis_objects();
let framework_pkg = genesis_objs
.iter()
.find(|q| q.id() == ObjectID::from(SUI_FRAMEWORK_ADDRESS))
Expand Down
98 changes: 76 additions & 22 deletions sui_core/src/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use move_binary_format::CompiledModule;
use move_bytecode_utils::module_cache::ModuleCache;
use move_core_types::{
language_storage::{ModuleId, StructTag},
Expand All @@ -13,7 +14,7 @@ use std::{
pin::Pin,
sync::Arc,
};
use sui_adapter::adapter;
use sui_adapter::adapter::{self};
use sui_types::{
base_types::*,
committee::Committee,
Expand All @@ -36,6 +37,9 @@ use temporary_store::AuthorityTemporaryStore;
mod authority_store;
pub use authority_store::AuthorityStore;

// based on https://github.com/diem/move/blob/62d48ce0d8f439faa83d05a4f5cd568d4bfcb325/language/tools/move-cli/src/sandbox/utils/mod.rs#L50
const MAX_GAS_BUDGET: u64 = 18446744073709551615 / 1000 - 1;

/// a Trait object for `signature::Signer` that is:
/// - Pin, i.e. confined to one place in memory (we don't want to copy private keys).
/// - Sync, i.e. can be safely shared between threads.
Expand Down Expand Up @@ -163,20 +167,7 @@ impl AuthorityState {
let input_objects = transaction.input_objects();
let mut all_objects = Vec::with_capacity(input_objects.len());

// There is at least one input
fp_ensure!(
!input_objects.is_empty(),
SuiError::ObjectInputArityViolation
);
// Ensure that there are no duplicate inputs
let mut used = HashSet::new();
if !input_objects.iter().all(|o| used.insert(o.object_id())) {
return Err(SuiError::DuplicateObjectRefInput);
}

let ids: Vec<_> = input_objects.iter().map(|kind| kind.object_id()).collect();

let objects = self.get_objects(&ids[..]).await?;
let objects = self.fetch_objects(&input_objects).await?;
let mutable_object_addresses: HashSet<_> = objects
.iter()
.flat_map(|opt_obj| match opt_obj {
Expand Down Expand Up @@ -214,6 +205,26 @@ impl AuthorityState {
Ok(all_objects)
}

async fn fetch_objects(
&self,
input_objects: &[InputObjectKind],
) -> Result<Vec<Option<Object>>, SuiError> {
// There is at least one input
fp_ensure!(
!input_objects.is_empty(),
SuiError::ObjectInputArityViolation
);
// Ensure that there are no duplicate inputs
let mut used = HashSet::new();
if !input_objects.iter().all(|o| used.insert(o.object_id())) {
return Err(SuiError::DuplicateObjectRefInput);
}

let ids: Vec<_> = input_objects.iter().map(|kind| kind.object_id()).collect();

self.get_objects(&ids[..]).await
}

/// Initiate a new transaction.
pub async fn handle_transaction(
&self,
Expand Down Expand Up @@ -497,7 +508,8 @@ impl AuthorityState {
name: AuthorityName,
secret: StableSyncAuthoritySigner,
store: Arc<AuthorityStore>,
genesis_modules: Vec<Object>,
genesis_packages: Vec<Vec<CompiledModule>>,
genesis_ctx: &mut TxContext,
) -> Self {
let native_functions =
sui_framework::natives::all_natives(MOVE_STDLIB_ADDRESS, SUI_FRAMEWORK_ADDRESS);
Expand All @@ -511,14 +523,10 @@ impl AuthorityState {
_database: store,
};

for genesis_module in genesis_modules {
#[cfg(debug_assertions)]
genesis_module.data.try_as_package().unwrap();

for genesis_modules in genesis_packages {
state
.init_transaction_lock(genesis_module.to_object_reference())
.store_package_and_init_modules(genesis_ctx, genesis_modules)
.await;
state.insert_object(genesis_module).await;
}
state
}
Expand All @@ -533,6 +541,43 @@ impl AuthorityState {
.expect("TODO: propagate the error")
}

async fn store_package_and_init_modules(
&self,
ctx: &mut TxContext,
modules: Vec<CompiledModule>,
) {
let inputs = Transaction::input_objects_in_compiled_modules(&modules);
let input_objects = self
.fetch_objects(&inputs)
.await
.unwrap()
.into_iter()
.flatten()
.collect::<Vec<_>>();

let mut temporary_store = AuthorityTemporaryStore::new(self, &input_objects, ctx.digest());
let package_id = ObjectID::from(*modules[0].self_id().address());
let natives = self._native_functions.clone();
let vm = adapter::verify_and_link(&temporary_store, &modules, package_id, natives).unwrap();

adapter::store_package_and_init_modules(
&mut temporary_store,
&vm,
modules,
ctx,
MAX_GAS_BUDGET,
)
.unwrap();
temporary_store.ensure_active_inputs_mutated();
let unwrapped_object_ids = self
.get_unwrapped_object_ids(temporary_store.written())
.unwrap();
temporary_store.patch_unwrapped_object_version(unwrapped_object_ids);
self.update_objects_state(temporary_store, ctx.digest())
.await
.unwrap();
}

/// Make an information response for a transaction
async fn make_transaction_info(
&self,
Expand Down Expand Up @@ -579,6 +624,15 @@ impl AuthorityState {
.update_state(temporary_store, certificate, signed_effects)
}

async fn update_objects_state(
&self,
temporary_store: AuthorityTemporaryStore,
transaction_digest: TransactionDigest,
) -> Result<(), SuiError> {
self._database
.update_objects_state(temporary_store, transaction_digest)
}

/// Get a read reference to an object/seq lock
pub async fn get_transaction_lock(
&self,
Expand Down
14 changes: 12 additions & 2 deletions sui_core/src/authority/authority_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,18 @@ impl AuthorityStore {
})
}

/// Updates the objects in the state
pub fn batch_update_objects(
/// Only update the objects state
pub fn update_objects_state(
&self,
temporary_store: AuthorityTemporaryStore,
transaction_digest: TransactionDigest,
) -> Result<(), SuiError> {
let write_batch = self.transaction_lock.batch();
self.batch_update_objects(write_batch, temporary_store, transaction_digest)
}

/// Helper function for updating the objects in the state
fn batch_update_objects(
&self,
mut write_batch: DBBatch,
temporary_store: AuthorityTemporaryStore,
Expand Down
Loading

0 comments on commit 6822e1c

Please sign in to comment.