simulacrum/
lib.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5//! A `Simulacrum` of IOTA.
6//!
7//! The word simulacrum is latin for "likeness, semblance", it is also a spell
8//! in D&D which creates a copy of a creature which then follows the player's
9//! commands and wishes. As such this crate provides the [`Simulacrum`] type
10//! which is a implementation or instantiation of a iota blockchain, one which
11//! doesn't do anything unless acted upon.
12//!
13//! [`Simulacrum`]: crate::Simulacrum
14
15mod epoch_state;
16pub mod store;
17pub mod transaction_executor;
18
19use std::{
20    collections::HashMap,
21    num::NonZeroUsize,
22    path::PathBuf,
23    sync::{Arc, RwLock},
24};
25
26use anyhow::{Result, anyhow};
27use fastcrypto::traits::Signer;
28use iota_config::{
29    genesis, transaction_deny_config::TransactionDenyConfig,
30    verifier_signing_config::VerifierSigningConfig,
31};
32use iota_node_storage::{GrpcIndexes, GrpcStateReader};
33use iota_protocol_config::ProtocolVersion;
34use iota_storage::blob::{Blob, BlobEncoding};
35use iota_swarm_config::{
36    genesis_config::AccountConfig, network_config::NetworkConfig,
37    network_config_builder::ConfigBuilder,
38};
39use iota_types::{
40    base_types::{AuthorityName, IotaAddress, ObjectID, VersionNumber},
41    committee::Committee,
42    crypto::{AuthoritySignature, KeypairTraits},
43    digests::{ConsensusCommitDigest, TransactionDigest},
44    effects::TransactionEffects,
45    error::ExecutionError,
46    gas_coin::{GasCoin, NANOS_PER_IOTA},
47    inner_temporary_store::InnerTemporaryStore,
48    iota_system_state::{
49        IotaSystemState, IotaSystemStateTrait, epoch_start_iota_system_state::EpochStartSystemState,
50    },
51    messages_checkpoint::{
52        CheckpointContents, CheckpointSequenceNumber, EndOfEpochData, VerifiedCheckpoint,
53    },
54    mock_checkpoint_builder::{MockCheckpointBuilder, ValidatorKeypairProvider},
55    object::Object,
56    programmable_transaction_builder::ProgrammableTransactionBuilder,
57    signature::VerifyParams,
58    storage::{EpochInfo, ObjectStore, ReadStore, TransactionInfo},
59    transaction::{
60        EndOfEpochTransactionKind, GasData, Transaction, TransactionData, TransactionKind,
61        VerifiedTransaction,
62    },
63};
64use rand::rngs::OsRng;
65
66pub use self::store::{SimulatorStore, in_mem_store::InMemoryStore};
67use self::{epoch_state::EpochState, store::in_mem_store::KeyStore};
68
69/// A `Simulacrum` of IOTA.
70///
71/// This type represents a simulated instantiation of an IOTA blockchain that
72/// needs to be driven manually, that is time doesn't advance and checkpoints
73/// are not formed unless explicitly requested.
74///
75/// See [module level][mod] documentation for more details.
76///
77/// [mod]: index.html
78pub struct Simulacrum<R = OsRng, Store: SimulatorStore = InMemoryStore> {
79    // Mutable state protected by RwLock for thread-safe interior mutability
80    inner: RwLock<SimulacrumInner<R, Store>>,
81    // Immutable config - can be accessed directly
82    deny_config: TransactionDenyConfig,
83    verifier_signing_config: VerifierSigningConfig,
84}
85
86struct SimulacrumInner<R, Store: SimulatorStore> {
87    rng: R,
88    keystore: KeyStore,
89    #[expect(unused)]
90    genesis: genesis::Genesis,
91    store: Store,
92    checkpoint_builder: MockCheckpointBuilder,
93
94    // Epoch specific data
95    epoch_state: EpochState,
96    data_ingestion_path: Option<PathBuf>,
97}
98
99impl Simulacrum {
100    /// Create a new, random Simulacrum instance using an `OsRng` as the source
101    /// of randomness.
102    #[expect(clippy::new_without_default)]
103    pub fn new() -> Self {
104        Self::new_with_rng(OsRng)
105    }
106}
107
108impl<R> Simulacrum<R>
109where
110    R: rand::RngCore + rand::CryptoRng,
111{
112    /// Create a new Simulacrum instance using the provided `rng`.
113    ///
114    /// This allows you to create a fully deterministic initial chainstate when
115    /// a seeded rng is used.
116    ///
117    /// ```
118    /// use rand::{SeedableRng, rngs::StdRng};
119    /// use simulacrum::Simulacrum;
120    ///
121    /// # fn main() {
122    /// let mut rng = StdRng::seed_from_u64(1);
123    /// let simulacrum = Simulacrum::new_with_rng(rng);
124    /// # }
125    /// ```
126    pub fn new_with_rng(mut rng: R) -> Self {
127        let config = ConfigBuilder::new_with_temp_dir()
128            .rng(&mut rng)
129            .with_chain_start_timestamp_ms(1)
130            .deterministic_committee_size(NonZeroUsize::new(1).unwrap())
131            .build();
132        Self::new_with_network_config_in_mem(&config, rng)
133    }
134
135    pub fn new_with_protocol_version_and_accounts(
136        mut rng: R,
137        chain_start_timestamp_ms: u64,
138        protocol_version: ProtocolVersion,
139        account_configs: Vec<AccountConfig>,
140    ) -> Self {
141        let config = ConfigBuilder::new_with_temp_dir()
142            .rng(&mut rng)
143            .with_chain_start_timestamp_ms(chain_start_timestamp_ms)
144            .deterministic_committee_size(NonZeroUsize::new(1).unwrap())
145            .with_protocol_version(protocol_version)
146            .with_accounts(account_configs)
147            .build();
148        Self::new_with_network_config_in_mem(&config, rng)
149    }
150
151    fn new_with_network_config_in_mem(config: &NetworkConfig, rng: R) -> Self {
152        let store = InMemoryStore::new(&config.genesis);
153        Self::new_with_network_config_store(config, rng, store)
154    }
155}
156
157impl<R, S: store::SimulatorStore> Simulacrum<R, S> {
158    pub fn new_with_network_config_store(config: &NetworkConfig, rng: R, store: S) -> Self {
159        let keystore = KeyStore::from_network_config(config);
160        let checkpoint_builder = MockCheckpointBuilder::new(config.genesis.checkpoint());
161
162        let genesis = &config.genesis;
163        let epoch_state = EpochState::new(genesis.iota_system_object());
164
165        Self {
166            deny_config: TransactionDenyConfig::default(),
167            verifier_signing_config: VerifierSigningConfig::default(),
168            inner: RwLock::new(SimulacrumInner {
169                rng,
170                keystore,
171                genesis: genesis.clone(),
172                store,
173                checkpoint_builder,
174                epoch_state,
175                data_ingestion_path: None,
176            }),
177        }
178    }
179
180    /// Attempts to execute the provided Transaction.
181    ///
182    /// The provided Transaction undergoes the same types of checks that a
183    /// Validator does prior to signing and executing in the production
184    /// system. Some of these checks are as follows:
185    /// - User signature is valid
186    /// - Sender owns all OwnedObject inputs
187    /// - etc
188    ///
189    /// If the above checks are successful then the transaction is immediately
190    /// executed, enqueued to be included in the next checkpoint (the next
191    /// time `create_checkpoint` is called) and the corresponding
192    /// TransactionEffects are returned.
193    pub fn execute_transaction(
194        &self,
195        transaction: Transaction,
196    ) -> anyhow::Result<(TransactionEffects, Option<ExecutionError>)> {
197        let mut inner = self.inner.write().unwrap();
198        let transaction = transaction.try_into_verified_for_testing(&VerifyParams::default())?;
199
200        let (inner_temporary_store, _, effects, execution_error_opt) =
201            inner.epoch_state.execute_transaction(
202                &inner.store,
203                &self.deny_config,
204                &self.verifier_signing_config,
205                &transaction,
206            )?;
207
208        let InnerTemporaryStore {
209            written, events, ..
210        } = inner_temporary_store;
211
212        inner.store.insert_executed_transaction(
213            transaction.clone(),
214            effects.clone(),
215            events,
216            written,
217        );
218
219        // Insert into checkpoint builder
220        inner
221            .checkpoint_builder
222            .push_transaction(transaction, effects.clone());
223        Ok((effects, execution_error_opt.err()))
224    }
225
226    /// Simulate a transaction without committing changes.
227    /// This is useful for testing transaction behavior without modifying state.
228    pub fn simulate_transaction(
229        &self,
230        transaction: TransactionData,
231        checks: iota_types::transaction_executor::VmChecks,
232    ) -> iota_types::error::IotaResult<iota_types::transaction_executor::SimulateTransactionResult>
233    {
234        let inner = self.inner.read().unwrap();
235        inner.epoch_state.simulate_transaction(
236            &inner.store,
237            &self.deny_config,
238            &self.verifier_signing_config,
239            transaction,
240            checks,
241        )
242    }
243
244    /// Creates the next Checkpoint using the Transactions enqueued since the
245    /// last checkpoint was created.
246    pub fn create_checkpoint(&self) -> VerifiedCheckpoint {
247        let (checkpoint, contents) = {
248            let mut inner = self.inner.write().unwrap();
249            let committee = CommitteeWithKeys::new(&inner.keystore, inner.epoch_state.committee());
250            let timestamp_ms = inner.store.get_clock().timestamp_ms();
251            let (checkpoint, contents, _) =
252                inner.checkpoint_builder.build(&committee, timestamp_ms);
253            inner.store.insert_checkpoint(checkpoint.clone());
254            inner.store.insert_checkpoint_contents(contents.clone());
255            (checkpoint, contents)
256        };
257        // Release lock before expensive data ingestion operation
258        self.process_data_ingestion(checkpoint.clone(), contents)
259            .unwrap();
260        checkpoint
261    }
262
263    /// Advances the clock by `duration`.
264    ///
265    /// This creates and executes a ConsensusCommitPrologue transaction which
266    /// advances the chain Clock by the provided duration.
267    pub fn advance_clock(&self, duration: std::time::Duration) -> TransactionEffects {
268        let mut inner = self.inner.write().unwrap();
269        let epoch = inner.epoch_state.epoch();
270        let round = inner.epoch_state.next_consensus_round();
271        let timestamp_ms = inner.store.get_clock().timestamp_ms() + duration.as_millis() as u64;
272        drop(inner);
273
274        let consensus_commit_prologue_transaction =
275            VerifiedTransaction::new_consensus_commit_prologue_v1(
276                epoch,
277                round,
278                timestamp_ms,
279                ConsensusCommitDigest::default(),
280                Vec::new(),
281            );
282
283        self.execute_transaction(consensus_commit_prologue_transaction.into())
284            .expect("advancing the clock cannot fail")
285            .0
286    }
287
288    /// Advances the epoch.
289    ///
290    /// This creates and executes an EndOfEpoch transaction which advances the
291    /// chain into the next epoch. Since it is required to be the final
292    /// transaction in an epoch, the final checkpoint in the epoch is also
293    /// created.
294    ///
295    /// NOTE: This function does not currently support updating the protocol
296    /// version or the system packages
297    pub fn advance_epoch(&self) {
298        let inner = self.inner.read().unwrap();
299        let current_epoch = inner.epoch_state.epoch();
300        let next_epoch = current_epoch + 1;
301        let next_epoch_protocol_version = inner.epoch_state.protocol_version();
302        let gas_cost_summary = inner
303            .checkpoint_builder
304            .epoch_rolling_gas_cost_summary()
305            .clone();
306        let epoch_start_timestamp_ms = inner.store.get_clock().timestamp_ms();
307        drop(inner);
308
309        let next_epoch_system_package_bytes = vec![];
310        let kinds = vec![EndOfEpochTransactionKind::new_change_epoch_v3(
311            next_epoch,
312            next_epoch_protocol_version,
313            gas_cost_summary.storage_cost,
314            gas_cost_summary.computation_cost,
315            gas_cost_summary.computation_cost_burned,
316            gas_cost_summary.storage_rebate,
317            gas_cost_summary.non_refundable_storage_fee,
318            epoch_start_timestamp_ms,
319            next_epoch_system_package_bytes,
320            vec![],
321        )];
322
323        let tx = VerifiedTransaction::new_end_of_epoch_transaction(kinds);
324        self.execute_transaction(tx.into())
325            .expect("advancing the epoch cannot fail");
326
327        let (checkpoint, contents, new_epoch_state) = {
328            let mut inner = self.inner.write().unwrap();
329            let new_epoch_state = EpochState::new(inner.store.get_system_state());
330            let end_of_epoch_data = EndOfEpochData {
331                next_epoch_committee: new_epoch_state.committee().voting_rights.clone(),
332                next_epoch_protocol_version,
333                epoch_commitments: vec![],
334                // Do not simulate supply changes for now.
335                epoch_supply_change: 0,
336            };
337            let (checkpoint, contents, _) = {
338                let committee =
339                    CommitteeWithKeys::new(&inner.keystore, inner.epoch_state.committee());
340                let timestamp_ms = inner.store.get_clock().timestamp_ms();
341                inner.checkpoint_builder.build_end_of_epoch(
342                    &committee,
343                    timestamp_ms,
344                    next_epoch,
345                    end_of_epoch_data,
346                )
347            };
348
349            inner.store.insert_checkpoint(checkpoint.clone());
350            inner.store.insert_checkpoint_contents(contents.clone());
351            inner
352                .store
353                .update_last_checkpoint_of_epoch(current_epoch, *checkpoint.sequence_number());
354            (checkpoint, contents, new_epoch_state)
355        };
356
357        // Process data ingestion without holding the lock
358        self.process_data_ingestion(checkpoint, contents).unwrap();
359
360        // Finally, update the epoch state
361        let mut inner = self.inner.write().unwrap();
362        inner.epoch_state = new_epoch_state;
363    }
364
365    /// Execute a function with read access to the store.
366    ///
367    /// This provides thread-safe access to the underlying store by locking it
368    /// for the duration of the closure execution.
369    pub fn with_store<F, T>(&self, f: F) -> T
370    where
371        F: FnOnce(&S) -> T,
372    {
373        let inner = self.inner.read().unwrap();
374        f(&inner.store)
375    }
376
377    /// Execute a function with read access to the keystore.
378    ///
379    /// This provides thread-safe access to the keystore by locking it
380    /// for the duration of the closure execution.
381    pub fn with_keystore<F, T>(&self, f: F) -> T
382    where
383        F: FnOnce(&KeyStore) -> T,
384    {
385        let inner = self.inner.read().unwrap();
386        f(&inner.keystore)
387    }
388
389    pub fn epoch_start_state(&self) -> EpochStartSystemState {
390        let inner = self.inner.read().unwrap();
391        inner.epoch_state.epoch_start_state()
392    }
393
394    /// Execute a function with mutable access to the internally held RNG.
395    ///
396    /// Provides mutable access to the RNG used to create this Simulacrum for
397    /// use as a source of randomness. Using a seeded RNG to build a
398    /// Simulacrum and then utilizing the stored RNG as a source of
399    /// randomness can lead to a fully deterministic chain evolution.
400    pub fn with_rng<F, T>(&self, f: F) -> T
401    where
402        F: FnOnce(&mut R) -> T,
403    {
404        let mut inner = self.inner.write().unwrap();
405        f(&mut inner.rng)
406    }
407
408    /// Return the reference gas price for the current epoch
409    pub fn reference_gas_price(&self) -> u64 {
410        self.inner.read().unwrap().epoch_state.reference_gas_price()
411    }
412
413    /// Request that `amount` Nanos be sent to `address` from a faucet account.
414    ///
415    /// ```
416    /// use iota_types::{base_types::IotaAddress, gas_coin::NANOS_PER_IOTA};
417    /// use simulacrum::Simulacrum;
418    ///
419    /// # fn main() {
420    /// let mut simulacrum = Simulacrum::new();
421    /// let address = simulacrum.with_rng(|rng| IotaAddress::generate(rng));
422    /// simulacrum.request_gas(address, NANOS_PER_IOTA).unwrap();
423    ///
424    /// // `account` now has a Coin<IOTA> object with single IOTA in it.
425    /// // ...
426    /// # }
427    /// ```
428    pub fn request_gas(&self, address: IotaAddress, amount: u64) -> Result<TransactionEffects> {
429        // For right now we'll just use the first account as the `faucet` account. We
430        // may want to explicitly cordon off the faucet account from the rest of
431        // the accounts though.
432        let (sender, key) = self.with_keystore(|keystore| -> Result<(IotaAddress, _)> {
433            let (s, k) = keystore
434                .accounts()
435                .next()
436                .ok_or_else(|| anyhow!("no accounts available in keystore"))?;
437            Ok((*s, k.copy()))
438        })?;
439
440        let object = self
441            .with_store(|store| {
442                store.owned_objects(sender).find(|object| {
443                    object.is_gas_coin() && object.get_coin_value_unsafe() > amount + NANOS_PER_IOTA
444                })
445            })
446            .ok_or_else(|| {
447                anyhow!("unable to find a coin with enough to satisfy request for {amount} Nanos")
448            })?;
449
450        let gas_data = iota_types::transaction::GasData {
451            payment: vec![object.compute_object_reference()],
452            owner: sender,
453            price: self.reference_gas_price(),
454            budget: NANOS_PER_IOTA,
455        };
456
457        let pt = {
458            let mut builder =
459                iota_types::programmable_transaction_builder::ProgrammableTransactionBuilder::new();
460            builder.transfer_iota(address, Some(amount));
461            builder.finish()
462        };
463
464        let kind = iota_types::transaction::TransactionKind::ProgrammableTransaction(pt);
465        let tx_data =
466            iota_types::transaction::TransactionData::new_with_gas_data(kind, sender, gas_data);
467        let tx = Transaction::from_data_and_signer(tx_data, vec![&key]);
468
469        self.execute_transaction(tx).map(|x| x.0)
470    }
471
472    pub fn set_data_ingestion_path(&self, data_ingestion_path: PathBuf) {
473        let checkpoint = {
474            let mut inner = self.inner.write().unwrap();
475            inner.data_ingestion_path = Some(data_ingestion_path);
476            let checkpoint = inner.store.get_checkpoint_by_sequence_number(0).unwrap();
477            let contents = inner
478                .store
479                .get_checkpoint_contents_by_digest(&checkpoint.content_digest);
480            (checkpoint, contents)
481        };
482        // Release lock before expensive data ingestion operation
483        if let (checkpoint, Some(contents)) = checkpoint {
484            self.process_data_ingestion(checkpoint, contents).unwrap();
485        }
486    }
487
488    /// Overrides the next checkpoint number indirectly by setting the previous
489    /// checkpoint's number to checkpoint_number - 1. This ensures the next
490    /// generated checkpoint has the exact sequence number provided. This
491    /// can be useful to generate checkpoints with specific sequence
492    /// numbers. Monotonicity of checkpoint numbers is enforced strictly.
493    pub fn override_next_checkpoint_number(&self, number: CheckpointSequenceNumber) {
494        let mut inner = self.inner.write().unwrap();
495        let committee = CommitteeWithKeys::new(&inner.keystore, inner.epoch_state.committee());
496        inner
497            .checkpoint_builder
498            .override_next_checkpoint_number(number, &committee);
499    }
500
501    /// Process data ingestion without holding the inner lock.
502    /// This version should be used when you don't already hold the lock.
503    fn process_data_ingestion(
504        &self,
505        checkpoint: VerifiedCheckpoint,
506        checkpoint_contents: CheckpointContents,
507    ) -> anyhow::Result<()> {
508        let path = self.inner.read().unwrap().data_ingestion_path.clone();
509        if let Some(data_path) = path {
510            let file_name = format!("{}.chk", checkpoint.sequence_number);
511            let checkpoint_data = self.try_get_checkpoint_data(checkpoint, checkpoint_contents)?;
512            std::fs::create_dir_all(&data_path)?;
513            let blob = Blob::encode(&checkpoint_data, BlobEncoding::Bcs)?;
514            std::fs::write(data_path.join(file_name), blob.to_bytes())?;
515        }
516        Ok(())
517    }
518}
519
520pub struct CommitteeWithKeys {
521    keystore: KeyStore,
522    committee: Committee,
523}
524
525impl CommitteeWithKeys {
526    fn new(keystore: &KeyStore, committee: &Committee) -> Self {
527        Self {
528            keystore: keystore.clone(),
529            committee: committee.clone(),
530        }
531    }
532
533    pub fn keystore(&self) -> &KeyStore {
534        &self.keystore
535    }
536}
537
538impl ValidatorKeypairProvider for CommitteeWithKeys {
539    fn get_validator_key(&self, name: &AuthorityName) -> &dyn Signer<AuthoritySignature> {
540        self.keystore.validator(name).unwrap()
541    }
542
543    fn get_committee(&self) -> &Committee {
544        &self.committee
545    }
546}
547
548impl<T, V: store::SimulatorStore> ObjectStore for Simulacrum<T, V> {
549    fn try_get_object(
550        &self,
551        object_id: &ObjectID,
552    ) -> Result<Option<Object>, iota_types::storage::error::Error> {
553        self.with_store(|store| store.try_get_object(object_id))
554    }
555
556    fn try_get_object_by_key(
557        &self,
558        object_id: &ObjectID,
559        version: VersionNumber,
560    ) -> Result<Option<Object>, iota_types::storage::error::Error> {
561        self.with_store(|store| store.try_get_object_by_key(object_id, version))
562    }
563}
564
565impl<T, V: store::SimulatorStore> ReadStore for Simulacrum<T, V> {
566    fn try_get_committee(
567        &self,
568        epoch: iota_types::committee::EpochId,
569    ) -> iota_types::storage::error::Result<Option<std::sync::Arc<Committee>>> {
570        self.with_store(|store| store.try_get_committee(epoch))
571    }
572
573    fn try_get_latest_checkpoint(&self) -> iota_types::storage::error::Result<VerifiedCheckpoint> {
574        Ok(self.with_store(|store| store.get_highest_checkpoint().unwrap()))
575    }
576
577    fn try_get_highest_verified_checkpoint(
578        &self,
579    ) -> iota_types::storage::error::Result<VerifiedCheckpoint> {
580        Ok(self.with_store(|store| store.get_highest_checkpoint().unwrap()))
581    }
582
583    fn try_get_highest_synced_checkpoint(
584        &self,
585    ) -> iota_types::storage::error::Result<VerifiedCheckpoint> {
586        Ok(self.with_store(|store| store.get_highest_checkpoint().unwrap()))
587    }
588
589    fn try_get_lowest_available_checkpoint(
590        &self,
591    ) -> iota_types::storage::error::Result<iota_types::messages_checkpoint::CheckpointSequenceNumber>
592    {
593        // TODO wire this up to the underlying sim store, for now this will work since
594        // we never prune the sim store
595        Ok(0)
596    }
597
598    fn try_get_checkpoint_by_digest(
599        &self,
600        digest: &iota_types::messages_checkpoint::CheckpointDigest,
601    ) -> iota_types::storage::error::Result<Option<VerifiedCheckpoint>> {
602        Ok(self.with_store(|store| store.get_checkpoint_by_digest(digest)))
603    }
604
605    fn try_get_checkpoint_by_sequence_number(
606        &self,
607        sequence_number: iota_types::messages_checkpoint::CheckpointSequenceNumber,
608    ) -> iota_types::storage::error::Result<Option<VerifiedCheckpoint>> {
609        Ok(self.with_store(|store| store.get_checkpoint_by_sequence_number(sequence_number)))
610    }
611
612    fn try_get_checkpoint_contents_by_digest(
613        &self,
614        digest: &iota_types::messages_checkpoint::CheckpointContentsDigest,
615    ) -> iota_types::storage::error::Result<
616        Option<iota_types::messages_checkpoint::CheckpointContents>,
617    > {
618        Ok(self.with_store(|store| store.get_checkpoint_contents_by_digest(digest)))
619    }
620
621    fn try_get_checkpoint_contents_by_sequence_number(
622        &self,
623        sequence_number: iota_types::messages_checkpoint::CheckpointSequenceNumber,
624    ) -> iota_types::storage::error::Result<
625        Option<iota_types::messages_checkpoint::CheckpointContents>,
626    > {
627        Ok(self.with_store(|store| {
628            store
629                .get_checkpoint_by_sequence_number(sequence_number)
630                .and_then(|checkpoint| {
631                    store.get_checkpoint_contents_by_digest(&checkpoint.content_digest)
632                })
633        }))
634    }
635
636    fn try_get_transaction(
637        &self,
638        tx_digest: &iota_types::digests::TransactionDigest,
639    ) -> iota_types::storage::error::Result<Option<Arc<VerifiedTransaction>>> {
640        Ok(self.with_store(|store| store.get_transaction(tx_digest)))
641    }
642
643    fn try_get_transaction_effects(
644        &self,
645        tx_digest: &iota_types::digests::TransactionDigest,
646    ) -> iota_types::storage::error::Result<Option<TransactionEffects>> {
647        Ok(self.with_store(|store| store.get_transaction_effects(tx_digest)))
648    }
649
650    fn try_get_events(
651        &self,
652        digest: &iota_types::digests::TransactionDigest,
653    ) -> iota_types::storage::error::Result<Option<iota_types::effects::TransactionEvents>> {
654        Ok(self.with_store(|store| store.get_events(digest)))
655    }
656
657    fn try_get_full_checkpoint_contents_by_sequence_number(
658        &self,
659        sequence_number: iota_types::messages_checkpoint::CheckpointSequenceNumber,
660    ) -> iota_types::storage::error::Result<
661        Option<iota_types::messages_checkpoint::FullCheckpointContents>,
662    > {
663        self.with_store(|store| {
664            store
665                .try_get_checkpoint_by_sequence_number(sequence_number)?
666                .and_then(|chk| store.get_checkpoint_contents_by_digest(&chk.content_digest))
667                .map_or(Ok(None), |contents| {
668                    iota_types::messages_checkpoint::FullCheckpointContents::try_from_checkpoint_contents(
669                        store,
670                        contents,
671                    )
672                })
673        })
674    }
675
676    fn try_get_full_checkpoint_contents(
677        &self,
678        digest: &iota_types::messages_checkpoint::CheckpointContentsDigest,
679    ) -> iota_types::storage::error::Result<
680        Option<iota_types::messages_checkpoint::FullCheckpointContents>,
681    > {
682        self.with_store(|store| {
683            store.get_checkpoint_contents_by_digest(digest)
684            .map_or(Ok(None), |contents| {
685                iota_types::messages_checkpoint::FullCheckpointContents::try_from_checkpoint_contents(
686                    store,
687                    contents,
688                )
689            })
690        })
691    }
692}
693
694impl<T: Send + Sync, V: store::SimulatorStore + Send + Sync> GrpcStateReader for Simulacrum<T, V> {
695    fn get_lowest_available_checkpoint_objects(
696        &self,
697    ) -> iota_types::storage::error::Result<CheckpointSequenceNumber> {
698        Ok(0)
699    }
700
701    fn get_chain_identifier(
702        &self,
703    ) -> iota_types::storage::error::Result<iota_types::digests::ChainIdentifier> {
704        Ok(self
705            .with_store(|store| store.get_checkpoint_by_sequence_number(0))
706            .expect("lowest available checkpoint should exist")
707            .digest()
708            .to_owned()
709            .into())
710    }
711
712    fn get_epoch_last_checkpoint(
713        &self,
714        epoch_id: iota_types::committee::EpochId,
715    ) -> iota_types::storage::error::Result<Option<VerifiedCheckpoint>> {
716        Ok(self.with_store(|store| {
717            store
718                .get_last_checkpoint_of_epoch(epoch_id)
719                .and_then(|seq| store.get_checkpoint_by_sequence_number(seq))
720        }))
721    }
722
723    fn grpc_indexes(&self) -> Option<&dyn iota_node_storage::GrpcIndexes> {
724        Some(self)
725    }
726
727    fn get_struct_layout(
728        &self,
729        _: &move_core_types::language_storage::StructTag,
730    ) -> iota_types::storage::error::Result<Option<move_core_types::annotated_value::MoveTypeLayout>>
731    {
732        Ok(None)
733    }
734}
735
736impl<T: Send + Sync, V: store::SimulatorStore + Send + Sync> Simulacrum<T, V> {
737    fn get_system_state_for_epoch(&self, epoch: u64) -> Option<IotaSystemState> {
738        self.with_store(|store| {
739            if let Some(historical_state) = store.get_system_state_by_epoch(epoch) {
740                return Some(historical_state.clone());
741            }
742            let current_system_state = store.get_system_state();
743            if epoch == current_system_state.epoch() {
744                return Some(current_system_state);
745            }
746            None
747        })
748    }
749}
750
751impl<T: Send + Sync, V: store::SimulatorStore + Send + Sync> GrpcIndexes for Simulacrum<T, V> {
752    fn get_epoch_info(
753        &self,
754        epoch: iota_types::committee::EpochId,
755    ) -> iota_types::storage::error::Result<Option<EpochInfo>> {
756        Ok(self.with_store(|store| {
757            let start_checkpoint_seq = if epoch != 0 {
758                store
759                    .get_last_checkpoint_of_epoch(epoch - 1)
760                    .map(|seq| Some(seq + 1))
761                    .unwrap_or(None)?
762            } else {
763                0
764            };
765
766            let start_checkpoint = store.get_checkpoint_by_sequence_number(start_checkpoint_seq)?;
767
768            let system_state = self.get_system_state_for_epoch(epoch)?;
769
770            let (end_timestamp_ms, end_checkpoint) =
771                if let Some(next_epoch_state) = self.get_system_state_for_epoch(epoch + 1) {
772                    (
773                        Some(next_epoch_state.epoch_start_timestamp_ms()),
774                        Some(
775                            store
776                                .get_last_checkpoint_of_epoch(epoch)
777                                .expect("last checkpoint of completed epoch should exist"),
778                        ),
779                    )
780                } else {
781                    (None, None)
782                };
783
784            Some(EpochInfo {
785                epoch,
786                protocol_version: system_state.protocol_version(),
787                start_timestamp_ms: start_checkpoint.data().timestamp_ms,
788                end_timestamp_ms,
789                start_checkpoint: start_checkpoint_seq,
790                end_checkpoint,
791                reference_gas_price: system_state.reference_gas_price(),
792                system_state,
793            })
794        }))
795    }
796
797    fn get_transaction_info(
798        &self,
799        digest: &TransactionDigest,
800    ) -> iota_types::storage::error::Result<Option<TransactionInfo>> {
801        Ok(self.with_store(|store| {
802            let highest_seq = store
803                .get_highest_checkpoint()
804                .map(|cp| *cp.sequence_number())?;
805
806            for seq in (0..=highest_seq).rev() {
807                if let Some(checkpoint) = store.get_checkpoint_by_sequence_number(seq) {
808                    if let Some(contents) =
809                        store.get_checkpoint_contents_by_digest(&checkpoint.content_digest)
810                    {
811                        if contents
812                            .iter()
813                            .any(|exec_digests| exec_digests.transaction == *digest)
814                        {
815                            // object_types left empty — production GrpcIndexesStore
816                            // populates this from input/output objects but that is
817                            // not needed for the simulacrum test harness.
818                            return Some(TransactionInfo {
819                                checkpoint: *checkpoint.sequence_number(),
820                                object_types: HashMap::new(),
821                            });
822                        }
823                    }
824                }
825            }
826            None
827        }))
828    }
829
830    fn account_owned_objects_info_iter(
831        &self,
832        _owner: iota_types::base_types::IotaAddress,
833        _cursor: Option<&iota_types::storage::OwnedObjectCursor>,
834        _object_type: Option<move_core_types::language_storage::StructTag>,
835    ) -> iota_types::storage::error::Result<
836        Box<dyn Iterator<Item = iota_types::storage::OwnedObjectIteratorItem> + '_>,
837    > {
838        Ok(Box::new(std::iter::empty()))
839    }
840
841    fn dynamic_field_iter(
842        &self,
843        _parent: iota_types::base_types::ObjectID,
844        _cursor: Option<iota_types::base_types::ObjectID>,
845    ) -> iota_types::storage::error::Result<
846        Box<
847            dyn Iterator<
848                    Item = Result<
849                        iota_types::storage::DynamicFieldKey,
850                        typed_store_error::TypedStoreError,
851                    >,
852                > + '_,
853        >,
854    > {
855        Ok(Box::new(std::iter::empty()))
856    }
857
858    fn get_coin_info(
859        &self,
860        _coin_type: &move_core_types::language_storage::StructTag,
861    ) -> iota_types::storage::error::Result<Option<iota_types::storage::CoinInfo>> {
862        Ok(None)
863    }
864
865    fn package_versions_iter(
866        &self,
867        _original_package_id: iota_types::base_types::ObjectID,
868        _cursor: Option<u64>,
869    ) -> iota_types::storage::error::Result<
870        Box<dyn Iterator<Item = iota_types::storage::PackageVersionIteratorItem> + '_>,
871    > {
872        Ok(Box::new(std::iter::empty()))
873    }
874}
875
876impl Simulacrum {
877    /// Generate a random transfer transaction.
878    /// TODO: This is here today to make it easier to write tests. But we should
879    /// utilize all the existing code for generating transactions in
880    /// iota-test-transaction-builder by defining a trait
881    /// that both WalletContext and Simulacrum implement. Then we can remove
882    /// this function.
883    pub fn transfer_txn(&self, recipient: IotaAddress) -> (Transaction, u64) {
884        let (sender, key) = self.with_keystore(|keystore| {
885            let (s, k) = keystore.accounts().next().unwrap();
886            (*s, k.copy())
887        });
888
889        let (object, gas_coin_value) = self.with_store(|store| {
890            let object = store
891                .owned_objects(sender)
892                .find(|object| object.is_gas_coin())
893                .unwrap();
894            let gas_coin = GasCoin::try_from(object).unwrap();
895            (object.clone(), gas_coin.value())
896        });
897        let transfer_amount = gas_coin_value / 2;
898
899        let pt = {
900            let mut builder = ProgrammableTransactionBuilder::new();
901            builder.transfer_iota(recipient, Some(transfer_amount));
902            builder.finish()
903        };
904
905        let kind = TransactionKind::ProgrammableTransaction(pt);
906        let gas_data = GasData {
907            payment: vec![object.compute_object_reference()],
908            owner: sender,
909            price: self.reference_gas_price(),
910            budget: 1_000_000_000,
911        };
912        let tx_data = TransactionData::new_with_gas_data(kind, sender, gas_data);
913        let tx = Transaction::from_data_and_signer(tx_data, vec![&key]);
914        (tx, transfer_amount)
915    }
916}
917
918#[cfg(test)]
919mod tests {
920    use std::time::Duration;
921
922    use iota_types::{
923        base_types::IotaAddress, effects::TransactionEffectsAPI, gas_coin::GasCoin,
924        transaction::TransactionDataAPI,
925    };
926    use rand::{SeedableRng, rngs::StdRng};
927
928    use super::*;
929
930    #[test]
931    fn deterministic_genesis() {
932        let rng = StdRng::from_seed([9; 32]);
933        let chain1 = Simulacrum::new_with_rng(rng);
934        let genesis_checkpoint_digest1 = chain1
935            .with_store(|store| *store.get_checkpoint_by_sequence_number(0).unwrap().digest());
936
937        let rng = StdRng::from_seed([9; 32]);
938        let chain2 = Simulacrum::new_with_rng(rng);
939        let genesis_checkpoint_digest2 = chain2
940            .with_store(|store| *store.get_checkpoint_by_sequence_number(0).unwrap().digest());
941
942        assert_eq!(genesis_checkpoint_digest1, genesis_checkpoint_digest2);
943
944        // Ensure the committees are different when using different seeds
945        let rng = StdRng::from_seed([0; 32]);
946        let chain3 = Simulacrum::new_with_rng(rng);
947
948        let committee1 = chain1.with_store(|store| store.get_committee_by_epoch(0).cloned());
949        let committee3 = chain3.with_store(|store| store.get_committee_by_epoch(0).cloned());
950        assert_ne!(committee1, committee3);
951    }
952
953    #[test]
954    fn simple() {
955        let steps = 10;
956        let sim = Simulacrum::new();
957
958        let start_time_ms = sim.with_store(|store| {
959            let clock = store.get_clock();
960            println!("clock: {clock:#?}");
961            clock.timestamp_ms()
962        });
963
964        for _ in 0..steps {
965            sim.advance_clock(Duration::from_millis(1));
966            sim.create_checkpoint();
967            sim.with_store(|store| {
968                let clock = store.get_clock();
969                println!("clock: {clock:#?}");
970            });
971        }
972        let end_time_ms = sim.with_store(|store| store.get_clock().timestamp_ms());
973        assert_eq!(end_time_ms - start_time_ms, steps);
974        sim.with_store(|store| {
975            dbg!(store.get_highest_checkpoint());
976        });
977    }
978
979    #[test]
980    fn simple_epoch() {
981        let steps = 10;
982        let sim = Simulacrum::new();
983
984        let start_epoch = sim.with_store(|store| store.get_highest_checkpoint().unwrap().epoch);
985        for i in 0..steps {
986            sim.advance_epoch();
987            sim.advance_clock(Duration::from_millis(1));
988            sim.create_checkpoint();
989            println!("{i}");
990        }
991        let end_epoch = sim.with_store(|store| store.get_highest_checkpoint().unwrap().epoch);
992        assert_eq!(end_epoch - start_epoch, steps);
993        sim.with_store(|store| {
994            dbg!(store.get_highest_checkpoint());
995        });
996    }
997
998    #[test]
999    fn transfer() {
1000        let sim = Simulacrum::new();
1001        let recipient = IotaAddress::random();
1002        let (tx, transfer_amount) = sim.transfer_txn(recipient);
1003
1004        let gas_id = tx.data().transaction_data().gas_data().payment[0].0;
1005        let effects = sim.execute_transaction(tx).unwrap().0;
1006        let gas_summary = effects.gas_cost_summary();
1007        let gas_paid = gas_summary.net_gas_usage();
1008
1009        sim.with_store(|store| {
1010            assert_eq!(
1011                (transfer_amount as i64 - gas_paid) as u64,
1012                store::SimulatorStore::get_object(store, &gas_id)
1013                    .and_then(|object| GasCoin::try_from(&object).ok())
1014                    .unwrap()
1015                    .value()
1016            );
1017
1018            assert_eq!(
1019                transfer_amount,
1020                store
1021                    .owned_objects(recipient)
1022                    .next()
1023                    .and_then(|object| GasCoin::try_from(object).ok())
1024                    .unwrap()
1025                    .value()
1026            );
1027        });
1028
1029        let checkpoint = sim.create_checkpoint();
1030
1031        assert_eq!(&checkpoint.epoch_rolling_gas_cost_summary, gas_summary);
1032        assert_eq!(checkpoint.network_total_transactions, 2); // genesis + 1 txn
1033    }
1034}