Skip to content

Commit

Permalink
Data sim 6/x + Graphql testing: add simulacrum store persistence (Mys…
Browse files Browse the repository at this point in the history
…tenLabs#14362)

## Description 

Puts Simulacrum store behind a trait which allows implementing other
backing stores, particularly persistent store using Rocksdb. This is
needed for Graphql testing which wants more control over the storage
layer.

My biggest issue with this change is returning clones over references.
This is because we cannot return ref to objects in DB.
I doubt this will be a big issue as most items are small and perf is not
priority in simulator now.

## Test Plan 

Existing

---
If your changes are not user-facing and not a breaking change, you can
skip the following section. Otherwise, please indicate what changed, and
then add to the Release Notes section as highlighted during the release
process.

### Type of Change (Check all that apply)

- [ ] protocol change
- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes
  • Loading branch information
oxade authored Nov 13, 2023
1 parent e6fe3ce commit 69cec6b
Show file tree
Hide file tree
Showing 11 changed files with 757 additions and 215 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions crates/simulacrum/src/epoch_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use sui_types::{
transaction::{TransactionDataAPI, VerifiedTransaction},
};

use crate::store::InMemoryStore;
use crate::SimulatorStore;

pub struct EpochState {
epoch_start_state: EpochStartSystemState,
Expand Down Expand Up @@ -84,7 +84,7 @@ impl EpochState {

pub fn execute_transaction(
&self,
store: &InMemoryStore,
store: &dyn SimulatorStore,
deny_config: &TransactionDenyConfig,
transaction: &VerifiedTransaction,
) -> Result<(
Expand All @@ -103,7 +103,7 @@ impl EpochState {
&input_object_kinds,
&receiving_object_refs,
deny_config,
store,
&store,
)?;

let (input_objects, receiving_objects) = store.read_objects_for_synchronous_execution(
Expand All @@ -126,7 +126,7 @@ impl EpochState {
let transaction_data = transaction.data().transaction_data();
let (kind, signer, gas) = transaction_data.execution_parts();
Ok(self.executor.execute_transaction_to_effects(
store,
store.backing_store(),
&self.protocol_config,
self.limits_metrics.clone(),
false, // enable_expensive_checks
Expand Down
38 changes: 21 additions & 17 deletions crates/simulacrum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ use sui_types::{
};

use self::epoch_state::EpochState;
use self::store::KeyStore;
pub use self::store::{InMemoryStore, SimulatorStore};
pub use self::store::in_mem_store::InMemoryStore;
use self::store::in_mem_store::KeyStore;
pub use self::store::SimulatorStore;
use sui_types::mock_checkpoint_builder::{MockCheckpointBuilder, ValidatorKeypairProvider};

mod epoch_state;
mod store;
pub mod store;

/// A `Simulacrum` of Sui.
///
Expand Down Expand Up @@ -101,7 +102,7 @@ where
.with_chain_start_timestamp_ms(1)
.deterministic_committee_size(NonZeroUsize::new(1).unwrap())
.build();
Self::new_with_network_config(&config, rng)
Self::new_with_network_config_in_mem(&config, rng)
}

pub fn new_with_protocol_version_and_accounts(
Expand All @@ -117,12 +118,18 @@ where
.with_protocol_version(protocol_version)
.with_accounts(account_configs)
.build();
Self::new_with_network_config(&config, rng)
Self::new_with_network_config_in_mem(&config, rng)
}

fn new_with_network_config(config: &NetworkConfig, rng: R) -> Self {
let keystore = KeyStore::from_network_config(config);
fn new_with_network_config_in_mem(config: &NetworkConfig, rng: R) -> Self {
let store = InMemoryStore::new(&config.genesis);
Self::new_with_network_config_store(config, rng, store)
}
}

impl<R, S: store::SimulatorStore> Simulacrum<R, S> {
pub fn new_with_network_config_store(config: &NetworkConfig, rng: R, store: S) -> Self {
let keystore = KeyStore::from_network_config(config);
let checkpoint_builder = MockCheckpointBuilder::new(config.genesis.checkpoint());

let genesis = &config.genesis;
Expand All @@ -138,9 +145,7 @@ where
deny_config: TransactionDenyConfig::default(),
}
}
}

impl<R> Simulacrum<R> {
/// Attempts to execute the provided Transaction.
///
/// The provided Transaction undergoes the same types of checks that a Validator does prior to
Expand Down Expand Up @@ -254,7 +259,7 @@ impl<R> Simulacrum<R> {
self.epoch_state = new_epoch_state;
}

pub fn store(&self) -> &InMemoryStore {
pub fn store(&self) -> &dyn SimulatorStore {
&self.store
}

Expand Down Expand Up @@ -395,15 +400,15 @@ mod tests {
fn deterministic_genesis() {
let rng = StdRng::from_seed([9; 32]);
let chain1 = Simulacrum::new_with_rng(rng);
let genesis_checkpoint_digest1 = chain1
let genesis_checkpoint_digest1 = *chain1
.store()
.get_checkpoint_by_sequence_number(0)
.unwrap()
.digest();

let rng = StdRng::from_seed([9; 32]);
let chain2 = Simulacrum::new_with_rng(rng);
let genesis_checkpoint_digest2 = chain2
let genesis_checkpoint_digest2 = *chain2
.store()
.get_checkpoint_by_sequence_number(0)
.unwrap()
Expand Down Expand Up @@ -469,7 +474,7 @@ mod tests {
.owned_objects(sender)
.find(|object| object.is_gas_coin())
.unwrap();
let gas_coin = GasCoin::try_from(object).unwrap();
let gas_coin = GasCoin::try_from(&object).unwrap();
let gas_id = object.id();
let transfer_amount = gas_coin.value() / 2;

Expand All @@ -496,9 +501,8 @@ mod tests {

assert_eq!(
(transfer_amount as i64 - gas_paid) as u64,
sim.store()
.get_object(&gas_id)
.and_then(|object| GasCoin::try_from(object).ok())
store::SimulatorStore::get_object(sim.store(), &gas_id)
.and_then(|object| GasCoin::try_from(&object).ok())
.unwrap()
.value()
);
Expand All @@ -508,7 +512,7 @@ mod tests {
sim.store()
.owned_objects(recipient)
.next()
.and_then(|object| GasCoin::try_from(object).ok())
.and_then(|object| GasCoin::try_from(&object).ok())
.unwrap()
.value()
);
Expand Down
Loading

0 comments on commit 69cec6b

Please sign in to comment.