Skip to content

Commit

Permalink
sui-core: properly create genesis in a number of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill committed Jul 8, 2022
1 parent 6769ca4 commit 2df7c77
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 122 deletions.
2 changes: 1 addition & 1 deletion crates/sui-config/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl<R: ::rand::RngCore + ::rand::CryptoRng> ConfigBuilder<R> {
.unwrap();

let genesis = {
let mut builder = genesis::Builder::new(genesis_ctx)
let mut builder = genesis::Builder::new_with_context(genesis_ctx)
.add_move_modules(custom_modules)
.add_objects(objects);

Expand Down
18 changes: 15 additions & 3 deletions crates/sui-config/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Genesis {
}

pub fn get_default_genesis() -> Self {
Builder::new(sui_adapter::genesis::get_genesis_context()).build()
Builder::new_with_context(sui_adapter::genesis::get_genesis_context()).build()
}

pub fn load<P: AsRef<Path>>(path: P) -> Result<Self, anyhow::Error> {
Expand Down Expand Up @@ -149,8 +149,19 @@ pub struct Builder {
validators: Vec<ValidatorInfo>,
}

impl Default for Builder {
fn default() -> Self {
Self::new()
}
}

impl Builder {
pub fn new(genesis_ctx: TxContext) -> Self {
pub fn new() -> Self {
let genesis_ctx = sui_adapter::genesis::get_genesis_context();
Self::new_with_context(genesis_ctx)
}

pub fn new_with_context(genesis_ctx: TxContext) -> Self {
Self {
sui_framework: None,
move_framework: None,
Expand Down Expand Up @@ -377,7 +388,8 @@ mod test {

#[test]
fn roundtrip() {
let genesis = Builder::new(sui_adapter::genesis::get_genesis_context()).build();
let genesis =
Builder::new_with_context(sui_adapter::genesis::get_genesis_context()).build();

let s = serde_yaml::to_string(&genesis).unwrap();
let from_s = serde_yaml::from_str(&s).unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,12 @@ pub async fn init_configurable_authorities(
for _i in 0..authority_action.len() {
gas_objects.push(Object::with_owner_for_testing(addr1));
}
let genesis_objects = authority_genesis_objects(authority_count, gas_objects.clone());
let genesis_objects = vec![
gas_objects.clone(),
gas_objects.clone(),
gas_objects.clone(),
gas_objects.clone(),
];

// Create committee.
let mut key_pairs = Vec::new();
Expand Down
16 changes: 2 additions & 14 deletions crates/sui-core/src/epoch/tests/reconfiguration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,7 @@ async fn test_start_epoch_change() {
let gas_object = Object::with_id_owner_for_testing(ObjectID::random(), sender);
let genesis_objects = vec![object.clone(), gas_object.clone()];
// Create authority_aggregator and authority states.
let (net, states) = init_local_authorities(vec![
genesis_objects.clone(),
genesis_objects.clone(),
genesis_objects.clone(),
genesis_objects.clone(),
])
.await;
let (net, states) = init_local_authorities(4, genesis_objects.clone()).await;
let state = states[0].clone();
// Set the checkpoint number to be near the end of epoch.
state
Expand Down Expand Up @@ -166,13 +160,7 @@ async fn test_start_epoch_change() {
async fn test_finish_epoch_change() {
// Create authority_aggregator and authority states.
let genesis_objects = vec![];
let (net, states) = init_local_authorities(vec![
genesis_objects.clone(),
genesis_objects.clone(),
genesis_objects.clone(),
genesis_objects.clone(),
])
.await;
let (net, states) = init_local_authorities(4, genesis_objects.clone()).await;
let actives: Vec<_> = states
.iter()
.map(|state| {
Expand Down
83 changes: 34 additions & 49 deletions crates/sui-core/src/unit_tests/authority_aggregator_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use signature::Signer;

use sui_adapter::genesis;
use sui_config::genesis::Genesis;
use sui_types::crypto::Signature;
use sui_config::ValidatorInfo;
use sui_types::crypto::{get_key_pair, PublicKeyBytes};
use sui_types::crypto::{KeyPair, Signature};

use sui_types::messages::Transaction;
use sui_types::object::{Object, GAS_VALUE_FOR_TESTING};
Expand All @@ -19,53 +20,49 @@ use crate::authority::AuthorityState;
use crate::authority_client::LocalAuthorityClient;
use crate::authority_client::LocalAuthorityClientFaultConfig;

pub fn authority_genesis_objects(
authority_count: usize,
objects_per_authority: Vec<Object>,
) -> Vec<Vec<Object>> {
let mut objects = vec![];
for _ in 0..authority_count {
objects.push(objects_per_authority.clone());
}
objects
}

pub async fn init_local_authorities(
genesis_objects: Vec<Vec<Object>>,
committee_size: usize,
genesis_objects: Vec<Object>,
) -> (
AuthorityAggregator<LocalAuthorityClient>,
Vec<Arc<AuthorityState>>,
) {
let genesis = sui_config::genesis::Genesis::get_default_genesis();
init_local_authorities_with_genesis(&genesis, genesis_objects).await
let mut builder = sui_config::genesis::Builder::new().add_objects(genesis_objects);
let mut key_pairs = Vec::new();
for _ in 0..committee_size {
let (_, key_pair) = get_key_pair();
let authority_name = *key_pair.public_key_bytes();
let validator_info = ValidatorInfo {
public_key: authority_name,
stake: 1,
delegation: 0,
network_address: sui_config::utils::new_network_address(),
};
builder = builder.add_validator(validator_info);
key_pairs.push((authority_name, key_pair));
}
let genesis = builder.build();
init_local_authorities_with_genesis(&genesis, key_pairs).await
}

pub async fn init_local_authorities_with_genesis(
genesis: &Genesis,
genesis_objects: Vec<Vec<Object>>,
key_pairs: Vec<(PublicKeyBytes, KeyPair)>,
) -> (
AuthorityAggregator<LocalAuthorityClient>,
Vec<Arc<AuthorityState>>,
) {
telemetry_subscribers::init_for_testing();
let mut key_pairs = Vec::new();
let mut voting_rights = BTreeMap::new();
for _ in 0..genesis_objects.len() {
let (_, key_pair) = get_key_pair();
let authority_name = *key_pair.public_key_bytes();
voting_rights.insert(authority_name, 1);
key_pairs.push((authority_name, key_pair));
}
let committee = Committee::new(0, voting_rights).unwrap();
let committee = genesis.committee().unwrap();

let mut clients = BTreeMap::new();
let mut states = Vec::new();
for ((authority_name, secret), objects) in key_pairs.into_iter().zip(genesis_objects) {
for (authority_name, secret) in key_pairs {
let client = LocalAuthorityClient::new_with_objects(
committee.clone(),
authority_name,
secret,
objects,
genesis.objects().to_owned(),
genesis,
)
.await;
Expand Down Expand Up @@ -330,17 +327,14 @@ where
}

async fn execute_transaction_with_fault_configs(
genesis: &Genesis,
configs_before_process_transaction: &[(usize, LocalAuthorityClientFaultConfig)],
configs_before_process_certificate: &[(usize, LocalAuthorityClientFaultConfig)],
) -> SuiResult {
let (addr1, key1) = get_key_pair();
let (addr2, _) = get_key_pair();
let gas_object1 = Object::with_owner_for_testing(addr1);
let gas_object2 = Object::with_owner_for_testing(addr1);
let genesis_objects =
authority_genesis_objects(4, vec![gas_object1.clone(), gas_object2.clone()]);
let mut authorities = init_local_authorities_with_genesis(genesis, genesis_objects)
let mut authorities = init_local_authorities(4, vec![gas_object1.clone(), gas_object2.clone()])
.await
.0;

Expand Down Expand Up @@ -370,7 +364,7 @@ async fn execute_transaction_with_fault_configs(

#[tokio::test]
async fn test_map_reducer() {
let (authorities, _) = init_local_authorities(authority_genesis_objects(4, vec![])).await;
let (authorities, _) = init_local_authorities(4, vec![]).await;

// Test: reducer errors get propagated up
let res = authorities
Expand Down Expand Up @@ -499,9 +493,8 @@ async fn test_get_all_owned_objects() {
let gas_object1 = Object::with_owner_for_testing(addr1);
let gas_ref_1 = gas_object1.compute_object_reference();
let gas_object2 = Object::with_owner_for_testing(addr2);
let genesis_objects =
authority_genesis_objects(4, vec![gas_object1.clone(), gas_object2.clone()]);
let (authorities, _) = init_local_authorities(genesis_objects).await;
let (authorities, _) =
init_local_authorities(4, vec![gas_object1.clone(), gas_object2.clone()]).await;
let authority_clients: Vec<_> = authorities.authority_clients.values().collect();

// Make a schedule of transactions
Expand Down Expand Up @@ -587,9 +580,8 @@ async fn test_sync_all_owned_objects() {
let (addr2, _) = get_key_pair();
let gas_object1 = Object::with_owner_for_testing(addr1);
let gas_object2 = Object::with_owner_for_testing(addr1);
let genesis_objects =
authority_genesis_objects(4, vec![gas_object1.clone(), gas_object2.clone()]);
let (authorities, _) = init_local_authorities(genesis_objects).await;
let (authorities, _) =
init_local_authorities(4, vec![gas_object1.clone(), gas_object2.clone()]).await;
let authority_clients: Vec<_> = authorities.authority_clients.values().collect();

let framework_obj_ref = genesis::get_framework_object_ref();
Expand Down Expand Up @@ -698,9 +690,8 @@ async fn test_process_transaction1() {
let (addr1, key1) = get_key_pair();
let gas_object1 = Object::with_owner_for_testing(addr1);
let gas_object2 = Object::with_owner_for_testing(addr1);
let genesis_objects =
authority_genesis_objects(4, vec![gas_object1.clone(), gas_object2.clone()]);
let (authorities, _) = init_local_authorities(genesis_objects).await;
let (authorities, _) =
init_local_authorities(4, vec![gas_object1.clone(), gas_object2.clone()]).await;
let authority_clients: Vec<_> = authorities.authority_clients.values().collect();

let framework_obj_ref = genesis::get_framework_object_ref();
Expand Down Expand Up @@ -759,9 +750,8 @@ async fn test_process_certificate() {
let (addr1, key1) = get_key_pair();
let gas_object1 = Object::with_owner_for_testing(addr1);
let gas_object2 = Object::with_owner_for_testing(addr1);
let genesis_objects =
authority_genesis_objects(4, vec![gas_object1.clone(), gas_object2.clone()]);
let (authorities, _) = init_local_authorities(genesis_objects).await;
let (authorities, _) =
init_local_authorities(4, vec![gas_object1.clone(), gas_object2.clone()]).await;
let authority_clients: Vec<_> = authorities.authority_clients.values().collect();

let framework_obj_ref = genesis::get_framework_object_ref();
Expand Down Expand Up @@ -819,7 +809,6 @@ async fn test_process_transaction_fault_success() {
// A transaction is sent to all authories, however one of them will error out either before or after processing the transaction.
// A cert should still be created, and sent out to all authorities again. This time
// a different authority errors out either before or after processing the cert.
let genesis = sui_config::genesis::Genesis::get_default_genesis();
for i in 0..4 {
let mut config_before_process_transaction = LocalAuthorityClientFaultConfig::default();
if i % 2 == 0 {
Expand All @@ -834,7 +823,6 @@ async fn test_process_transaction_fault_success() {
config_before_process_certificate.fail_after_handle_confirmation = true;
}
execute_transaction_with_fault_configs(
&genesis,
&[(0, config_before_process_transaction)],
&[(1, config_before_process_certificate)],
)
Expand All @@ -852,9 +840,7 @@ async fn test_process_transaction_fault_fail() {
fail_before_handle_transaction: true,
..Default::default()
};
let genesis = sui_config::genesis::Genesis::get_default_genesis();
assert!(execute_transaction_with_fault_configs(
&genesis,
&[
(0, fail_before_process_transaction_config),
(1, fail_before_process_transaction_config),
Expand All @@ -869,7 +855,6 @@ async fn test_process_transaction_fault_fail() {
..Default::default()
};
assert!(execute_transaction_with_fault_configs(
&genesis,
&[],
&[
(0, fail_before_process_certificate_config),
Expand Down
Loading

0 comments on commit 2df7c77

Please sign in to comment.