Skip to content

Commit

Permalink
sim data source 2/x: impl node state getter for simulacrum (#14187)
Browse files Browse the repository at this point in the history
## Description 

Continuation of #14164
This will allow the simulator feed data into indexer etc

## Test Plan 

N/A

---
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 Oct 11, 2023
1 parent fe37f45 commit 42de67f
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ mysten-util-mem = { path = "crates/mysten-util-mem" }
mysten-util-mem-derive = { path = "crates/mysten-util-mem-derive" }
prometheus-closure-metric = { path = "crates/prometheus-closure-metric" }
shared-crypto = { path = "crates/shared-crypto" }
simulacrum = { path = "crates/simulacrum" }
sui = { path = "crates/sui" }
sui-adapter-transactional-tests = { path = "crates/sui-adapter-transactional-tests" }
sui-analytics-indexer = { path = "crates/sui-analytics-indexer" }
Expand Down
2 changes: 2 additions & 0 deletions crates/sui-rest-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ serde.workspace = true
bcs.workspace = true
reqwest.workspace = true
axum.workspace = true
rand.workspace = true
simulacrum.workspace = true
sui-types.workspace = true
sui-core.workspace = true
workspace-hack.workspace = true
Expand Down
90 changes: 90 additions & 0 deletions crates/sui-rest-api/src/node_state_getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use sui_core::authority::AuthorityState;
use sui_types::error::UserInputError;
use sui_types::{
base_types::{ObjectID, VersionNumber},
digests::{TransactionDigest, TransactionEventsDigest},
Expand Down Expand Up @@ -118,3 +119,92 @@ impl NodeStateGetter for AuthorityState {
self.database.get_object(object_id)
}
}

impl<T: Sync + Send> NodeStateGetter for simulacrum::Simulacrum<T> {
fn get_verified_checkpoint_by_sequence_number(
&self,
sequence_number: CheckpointSequenceNumber,
) -> SuiResult<VerifiedCheckpoint> {
self.store()
.get_checkpoint_by_sequence_number(sequence_number)
.cloned()
.ok_or(SuiError::UserInputError {
error: UserInputError::VerifiedCheckpointNotFound(sequence_number),
})
}

fn get_latest_checkpoint_sequence_number(&self) -> SuiResult<CheckpointSequenceNumber> {
Ok(self
.store()
.get_highest_checkpint()
.map(|checkpoint| *checkpoint.sequence_number())
.unwrap_or(0))
}

fn get_checkpoint_contents(
&self,
content_digest: CheckpointContentsDigest,
) -> SuiResult<CheckpointContents> {
self.store()
.get_checkpoint_contents(&content_digest)
.cloned()
.ok_or(SuiError::UserInputError {
error: UserInputError::CheckpointContentsNotFound(content_digest),
})
}

fn multi_get_transaction_blocks(
&self,
tx_digests: &[TransactionDigest],
) -> SuiResult<Vec<Option<VerifiedTransaction>>> {
Ok(tx_digests
.iter()
.map(|digest| self.store().get_transaction(digest).cloned())
.collect())
}

fn multi_get_executed_effects(
&self,
digests: &[TransactionDigest],
) -> SuiResult<Vec<Option<TransactionEffects>>> {
Ok(digests
.iter()
.map(|digest| self.store().get_transaction_effects(digest).cloned())
.collect())
}

fn multi_get_events(
&self,
event_digests: &[TransactionEventsDigest],
) -> SuiResult<Vec<Option<TransactionEvents>>> {
Ok(event_digests
.iter()
.map(|digest| self.store().get_transaction_events(digest).cloned())
.collect())
}

fn multi_get_object_by_key(
&self,
object_keys: &[ObjectKey],
) -> Result<Vec<Option<Object>>, SuiError> {
object_keys
.iter()
.map(|key| ObjectStore::get_object_by_key(&self.store(), &key.0, key.1))
.collect::<Result<Vec<_>, SuiError>>()
}

fn get_object_by_key(
&self,
object_id: &ObjectID,
version: VersionNumber,
) -> Result<Option<Object>, SuiError> {
Ok(self
.store()
.get_object_at_version(object_id, version)
.cloned())
}

fn get_object(&self, object_id: &ObjectID) -> Result<Option<Object>, SuiError> {
ObjectStore::get_object(&self.store(), object_id)
}
}

1 comment on commit 42de67f

@vercel
Copy link

@vercel vercel bot commented on 42de67f Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.