Skip to content

Commit

Permalink
genesis: use checkpoint from genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill committed Jan 11, 2023
1 parent 2c70d36 commit 2a6ed91
Show file tree
Hide file tree
Showing 20 changed files with 229 additions and 103 deletions.
24 changes: 13 additions & 11 deletions crates/sui-config/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use sui_types::messages::{CallArg, TransactionEffects};
use sui_types::messages::{CertifiedTransaction, Transaction};
use sui_types::messages::{InputObjects, SignedTransaction};
use sui_types::messages_checkpoint::{
CertifiedCheckpointSummary, CheckpointContents, CheckpointSummary,
CertifiedCheckpointSummary, CheckpointContents, CheckpointSummary, VerifiedCheckpoint,
};
use sui_types::object::Owner;
use sui_types::sui_system_state::SuiSystemState;
Expand Down Expand Up @@ -83,6 +83,10 @@ impl Genesis {
&self.objects
}

pub fn object(&self, id: ObjectID) -> Option<Object> {
self.objects.iter().find(|o| o.id() == id).cloned()
}

pub fn transaction(&self) -> &CertifiedTransaction {
&self.transaction
}
Expand All @@ -91,6 +95,14 @@ impl Genesis {
&self.effects
}

pub fn checkpoint(&self) -> VerifiedCheckpoint {
VerifiedCheckpoint::new(self.checkpoint.clone(), &self.committee().unwrap()).unwrap()
}

pub fn checkpoint_contents(&self) -> &CheckpointContents {
&self.checkpoint_contents
}

pub fn epoch(&self) -> EpochId {
0
}
Expand Down Expand Up @@ -470,16 +482,6 @@ impl Builder {
// Ensure we have signatures from all validators
assert_eq!(checkpoint.auth_signature.len(), validators.len() as u64);

// TODO(bmwill) remove this and don't override previous txn digest once we actually use the
// checkpoint created from genesis.
let objects = objects
.into_iter()
.map(|object| Object {
previous_transaction: TransactionDigest::genesis(),
..object
})
.collect::<Vec<_>>();

let genesis = Genesis {
checkpoint,
checkpoint_contents,
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-core/src/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ impl AuthorityState {

/// Executes a certificate for its effects.
#[instrument(level = "trace", skip_all)]
pub(crate) async fn execute_certificate(
pub async fn execute_certificate(
&self,
certificate: &VerifiedCertificate,
epoch_store: &Arc<AuthorityPerEpochStore>,
Expand Down
20 changes: 20 additions & 0 deletions crates/sui-core/src/authority/authority_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::path::Path;
use std::sync::Arc;
use sui_config::node::AuthorityStorePruningConfig;
use sui_storage::mutex_table::{LockGuard, MutexTable};
use sui_types::message_envelope::Message;
use sui_types::object::Owner;
use sui_types::object::PACKAGE_VERSION;
use sui_types::storage::{ChildObjectResolver, ObjectKey};
Expand Down Expand Up @@ -111,6 +112,25 @@ impl AuthorityStore {
.bulk_object_insert(&genesis.objects().iter().collect::<Vec<_>>())
.await
.expect("Cannot bulk insert genesis objects");

// insert txn and effects of genesis
let transaction = genesis
.transaction()
.clone()
.verify(&genesis.committee().unwrap())
.unwrap();

store
.perpetual_tables
.certificates
.insert(transaction.digest(), transaction.serializable_ref())
.unwrap();

store
.perpetual_tables
.effects
.insert(&genesis.effects().digest(), genesis.effects())
.unwrap();
}

Ok(store)
Expand Down
13 changes: 13 additions & 0 deletions crates/sui-core/src/checkpoints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ impl CheckpointStore {
Arc::new(Self::open_tables_read_write(path.to_path_buf(), None, None))
}

pub fn insert_genesis_checkpoint(
&self,
checkpoint: VerifiedCheckpoint,
contents: CheckpointContents,
) {
self.insert_verified_checkpoint(checkpoint.clone()).unwrap();
self.insert_checkpoint_contents(contents).unwrap();
self.update_highest_synced_checkpoint(&checkpoint).unwrap();
self.checkpoint_summary
.insert(&checkpoint.sequence_number(), checkpoint.summary())
.unwrap();
}

pub fn get_checkpoint_by_digest(
&self,
digest: &CheckpointDigest,
Expand Down
27 changes: 23 additions & 4 deletions crates/sui-core/src/quorum_driver/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ async fn setup() -> (
) {
let (sender, keypair): (_, AccountKeyPair) = get_key_pair();
let gas_object = Object::with_owner_for_testing(sender);
let (aggregator, _, _) = init_local_authorities(4, vec![gas_object.clone()]).await;
let (aggregator, _, genesis, _) = init_local_authorities(4, vec![gas_object.clone()]).await;

let tx = make_tx(&gas_object, sender, &keypair);
let gas_object = genesis
.objects()
.iter()
.find(|o| o.id() == gas_object.id())
.unwrap();

let tx = make_tx(gas_object, sender, &keypair);
(aggregator, tx)
}

Expand Down Expand Up @@ -194,10 +200,23 @@ async fn test_quorum_driver_update_validators_and_max_retry_times() {

#[tokio::test]
async fn test_quorum_driver_retry_on_object_locked() -> Result<(), anyhow::Error> {
let mut gas_objects = generate_test_gas_objects();
let gas_objects = generate_test_gas_objects();
let (sender, keypair): (SuiAddress, AccountKeyPair) = deterministic_random_account_key();

let (aggregator, _, _) = init_local_authorities(4, gas_objects.clone()).await;
let (aggregator, _, genesis, _) = init_local_authorities(4, gas_objects.clone()).await;

let mut gas_objects = gas_objects
.into_iter()
.map(|o| {
genesis
.objects()
.iter()
.find(|go| go.id() == o.id())
.unwrap()
.to_owned()
})
.collect::<Vec<_>>();

let aggregator = Arc::new(aggregator);

let quorum_driver_handler = Arc::new(
Expand Down
16 changes: 0 additions & 16 deletions crates/sui-core/src/test_authority_clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use sui_types::{
TransactionInfoRequest, TransactionInfoResponse,
},
messages_checkpoint::{CheckpointRequest, CheckpointResponse},
object::Object,
};
use sui_types::{error::SuiResult, messages::HandleCertificateResponse};

Expand Down Expand Up @@ -135,21 +134,6 @@ impl LocalAuthorityClient {
}
}

pub async fn new_with_objects(
committee: Committee,
secret: AuthorityKeyPair,
objects: Vec<Object>,
genesis: &Genesis,
) -> Self {
let client = Self::new(committee, secret, genesis).await;

for object in objects {
client.state.insert_genesis_object(object).await;
}

client
}

pub fn new_from_authority(state: Arc<AuthorityState>) -> Self {
Self {
state,
Expand Down
19 changes: 10 additions & 9 deletions crates/sui-core/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ async fn init_genesis(
.cloned()
.collect();
let pkg = Object::new_package(modules, TransactionDigest::genesis()).unwrap();
let pkg_ref = pkg.compute_object_reference();
let pkg_id = pkg.id();
genesis_objects.push(pkg);

let mut builder = sui_config::genesis::Builder::new().add_objects(genesis_objects);
Expand Down Expand Up @@ -201,6 +201,12 @@ async fn init_genesis(
builder = builder.add_validator_signature(key);
}
let genesis = builder.build();
let pkg_ref = genesis
.objects()
.iter()
.find(|o| o.id() == pkg_id)
.unwrap()
.compute_object_reference();
(genesis, key_pairs, pkg_ref)
}

Expand All @@ -210,11 +216,12 @@ pub async fn init_local_authorities(
) -> (
AuthorityAggregator<LocalAuthorityClient>,
Vec<Arc<AuthorityState>>,
Genesis,
ObjectRef,
) {
let (genesis, key_pairs, pkg_ref) = init_genesis(committee_size, genesis_objects).await;
let (aggregator, authorities) = init_local_authorities_with_genesis(&genesis, key_pairs).await;
(aggregator, authorities, pkg_ref)
(aggregator, authorities, genesis, pkg_ref)
}

pub async fn init_local_authorities_with_genesis(
Expand All @@ -230,13 +237,7 @@ pub async fn init_local_authorities_with_genesis(
let mut clients = BTreeMap::new();
let mut states = Vec::new();
for (authority_name, secret) in key_pairs {
let client = LocalAuthorityClient::new_with_objects(
committee.clone(),
secret,
genesis.objects().to_owned(),
genesis,
)
.await;
let client = LocalAuthorityClient::new(committee.clone(), secret, genesis).await;
states.push(client.state.clone());
clients.insert(authority_name, client);
}
Expand Down
4 changes: 3 additions & 1 deletion crates/sui-core/src/transaction_input_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,9 @@ async fn check_objects(
if !errors.is_empty() {
return Err(SuiError::TransactionInputObjectsErrors { errors });
}
fp_ensure!(!all_objects.is_empty(), SuiError::ObjectInputArityViolation);
if !transaction.kind.is_genesis_tx() && all_objects.is_empty() {
return Err(SuiError::ObjectInputArityViolation);
}

Ok(InputObjects::new(all_objects))
}
Expand Down
19 changes: 11 additions & 8 deletions crates/sui-core/src/unit_tests/authority_aggregator_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,10 @@ async fn execute_transaction_with_fault_configs(
let (addr2, _): (_, AccountKeyPair) = get_key_pair();
let gas_object1 = Object::with_owner_for_testing(addr1);
let gas_object2 = Object::with_owner_for_testing(addr1);
let mut authorities = init_local_authorities(4, vec![gas_object1.clone(), gas_object2.clone()])
.await
.0;
let (mut authorities, _, genesis, _) =
init_local_authorities(4, vec![gas_object1.clone(), gas_object2.clone()]).await;
let gas_object1 = genesis.object(gas_object1.id()).unwrap();
let gas_object2 = genesis.object(gas_object2.id()).unwrap();

for (index, config) in configs_before_process_transaction {
get_local_client(&mut authorities, *index).fault_config = *config;
Expand Down Expand Up @@ -313,12 +314,14 @@ async fn test_quorum_map_and_reduce_timeout() {
.cloned()
.collect();
let pkg = Object::new_package(modules, TransactionDigest::genesis()).unwrap();
let pkg_ref = pkg.compute_object_reference();
let (addr1, key1): (_, AccountKeyPair) = get_key_pair();
let gas_object1 = Object::with_owner_for_testing(addr1);
let genesis_objects = vec![pkg.clone(), gas_object1.clone()];
let (mut authorities, _, genesis, _) = init_local_authorities(4, genesis_objects).await;
let pkg = genesis.object(pkg.id()).unwrap();
let pkg_ref = pkg.compute_object_reference();
let gas_object1 = genesis.object(gas_object1.id()).unwrap();
let gas_ref_1 = gas_object1.compute_object_reference();
let genesis_objects = vec![pkg, gas_object1];
let (mut authorities, _, _) = init_local_authorities(4, genesis_objects).await;
let tx = create_object_move_transaction(addr1, &key1, addr1, 100, pkg_ref, gas_ref_1);
let certified_tx = authorities.process_transaction(tx.clone()).await;
assert!(certified_tx.is_ok());
Expand Down Expand Up @@ -352,7 +355,7 @@ async fn test_quorum_map_and_reduce_timeout() {

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

// Test: reducer errors get propagated up
let res = authorities
Expand Down Expand Up @@ -487,7 +490,7 @@ async fn test_execute_cert_to_true_effects() {
let (addr1, key1): (_, AccountKeyPair) = get_key_pair();
let gas_object1 = Object::with_owner_for_testing(addr1);
let gas_object2 = Object::with_owner_for_testing(addr1);
let (authorities, _, pkg_ref) =
let (authorities, _, _, pkg_ref) =
init_local_authorities(4, vec![gas_object1.clone(), gas_object2.clone()]).await;
let authority_clients: Vec<_> = authorities.authority_clients.values().collect();

Expand Down
2 changes: 1 addition & 1 deletion crates/sui-core/src/unit_tests/execution_driver_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ async fn test_transaction_manager() {
.collect_vec();
let all_gas_objects = gas_objects.clone().into_iter().flatten().collect_vec();

let (aggregator, authorities, framework_obj_ref) =
let (aggregator, authorities, _genesis, framework_obj_ref) =
init_local_authorities(4, all_gas_objects.clone()).await;
let authority_clients: Vec<_> = authorities
.iter()
Expand Down
3 changes: 1 addition & 2 deletions crates/sui-core/src/unit_tests/pay_sui_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,7 @@ async fn execute_pay_all_sui(
input_coin_objects
.clone()
.into_iter()
.map(ToOwned::to_owned)
.collect(),
.map(ToOwned::to_owned),
)
.build();
let genesis = network_config.genesis;
Expand Down
12 changes: 6 additions & 6 deletions crates/sui-json-rpc/src/unit_tests/rpc_server_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ async fn test_get_transaction() -> Result<(), anyhow::Error> {
}
// test get_transactions_in_range
let tx: Vec<TransactionDigest> = http_client.get_transactions_in_range(0, 10).await?;
assert_eq!(4, tx.len());
assert_eq!(5, tx.len());

// test get_transactions_in_range with smaller range
let tx: Vec<TransactionDigest> = http_client.get_transactions_in_range(1, 3).await?;
Expand Down Expand Up @@ -538,7 +538,7 @@ async fn test_get_fullnode_transaction() -> Result<(), anyhow::Error> {
.get_transactions(TransactionQuery::All, first_page.next_cursor, None, false)
.await
.unwrap();
assert_eq!(15, second_page.data.len());
assert_eq!(16, second_page.data.len());
assert!(second_page.next_cursor.is_none());

let mut all_txs_rev = first_page.data.clone();
Expand Down Expand Up @@ -667,7 +667,7 @@ async fn test_get_fullnode_events() -> Result<(), anyhow::Error> {
.get_events(EventQuery::All, Some((5, 0).into()), Some(20), false)
.await
.unwrap();
assert_eq!(15, page2.data.len());
assert_eq!(16, page2.data.len());
assert_eq!(None, page2.next_cursor);

// test get all events descending
Expand All @@ -677,7 +677,7 @@ async fn test_get_fullnode_events() -> Result<(), anyhow::Error> {
.await
.unwrap();
assert_eq!(3, page1.data.len());
assert_eq!(Some((16, 0).into()), page1.next_cursor);
assert_eq!(Some((17, 0).into()), page1.next_cursor);
let page2 = client
.event_api()
.get_events(EventQuery::All, Some((16, 0).into()), None, true)
Expand Down Expand Up @@ -710,7 +710,7 @@ async fn test_get_fullnode_events() -> Result<(), anyhow::Error> {
)
.await
.unwrap();
assert_eq!(4, page.data.len());
assert_eq!(9, page.data.len());

let object = client
.read_api()
Expand All @@ -727,7 +727,7 @@ async fn test_get_fullnode_events() -> Result<(), anyhow::Error> {
.get_events(EventQuery::Object(object), None, Some(10), false)
.await
.unwrap();
assert_eq!(4, page.data.len());
assert_eq!(5, page.data.len());

Ok(())
}
Expand Down
6 changes: 6 additions & 0 deletions crates/sui-network/src/state_sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,12 @@ where

self.spawn_notify_peers_of_checkpoint(checkpoint);
} else {
// Ensure that if consensus sends us a checkpoint that we expect to be the next one,
// that it isn't on a fork
if checkpoint.sequence_number() == next_sequence_number {
assert_eq!(checkpoint.previous_digest(), previous_digest);
}

// Otherwise stick it with the other unprocessed checkpoints and we can try to sync the missing
// ones
self.peer_heights
Expand Down
Loading

0 comments on commit 2a6ed91

Please sign in to comment.