Skip to content

Commit

Permalink
pcli: add un-advertised support for importing Prax registry data
Browse files Browse the repository at this point in the history
This makes a minimal set of changes to allow `pcli` to make use of the Prax
registry. In order to not pre-empt more comprehensive planning about how such
an integration should work, this PR just changes `pcli` so that if the registry
is placed in a `registry.json` file in the `pcli` home directory, it will be
imported into the view database on startup.
  • Loading branch information
hdevalence committed Aug 11, 2024
1 parent c4db70a commit ae51783
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
9 changes: 9 additions & 0 deletions crates/bin/pcli/src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,17 @@ impl Opt {
let path = self.home.join(crate::VIEW_FILE_NAME);
tracing::info!(%path, "using local view service");

let registry_path = self.home.join("registry.json");
// Check if the path exists or set it to nojne
let registry_path = if registry_path.exists() {
Some(registry_path)
} else {
None
};

let svc = ViewServer::load_or_initialize(
Some(path),
registry_path,
&config.full_viewing_key,
config.grpc_url.clone(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ async fn app_can_sweep_a_collection_of_small_notes() -> anyhow::Result<()> {
// Spawn the client-side view server...
let view_server = {
penumbra_view::ViewServer::load_or_initialize(
None::<&camino::Utf8Path>,
None::<&camino::Utf8Path>,
&*test_keys::FULL_VIEWING_KEY,
grpc_url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ async fn view_server_can_be_served_on_localhost() -> anyhow::Result<()> {
// Spawn the client-side view server...
let view_server = {
penumbra_view::ViewServer::load_or_initialize(
None::<&camino::Utf8Path>,
None::<&camino::Utf8Path>,
&*test_keys::FULL_VIEWING_KEY,
grpc_url,
Expand Down
5 changes: 5 additions & 0 deletions crates/view/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ impl ViewServer {
)]
pub async fn load_or_initialize(
storage_path: Option<impl AsRef<Utf8Path>>,
registry_path: Option<impl AsRef<Utf8Path>>,
fvk: &FullViewingKey,
node: Url,
) -> anyhow::Result<Self> {
Expand All @@ -108,6 +109,10 @@ impl ViewServer {
.await?
.tap(|_| tracing::debug!("storage is ready"));

if let Some(registry_path) = registry_path {
storage.load_asset_metadata(registry_path).await?;
}

Self::new(storage, node)
.tap(|_| tracing::trace!("constructing view server"))
.await
Expand Down
33 changes: 33 additions & 0 deletions crates/view/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,36 @@ impl Storage {
.await?
}

/// Loads asset metadata from a JSON file and use to update the database.
pub async fn load_asset_metadata(
&self,
registry_path: impl AsRef<Utf8Path>,
) -> anyhow::Result<()> {
tracing::debug!(registry_path = ?registry_path.as_ref(), "loading asset metadata");
let registry_path = registry_path.as_ref();
// Parse into a serde_json::Value first so we can get the bits we care about
let mut registry_json: serde_json::Value = serde_json::from_str(
std::fs::read_to_string(registry_path)
.context("failed to read file")?
.as_str(),
)
.context("failed to parse JSON")?;

let registry: BTreeMap<String, Metadata> = serde_json::value::from_value(
registry_json
.get_mut("assetById")
.ok_or_else(|| anyhow::anyhow!("missing assetById"))?
.take(),
)
.context("could not parse asset registry")?;

for metadata in registry.into_values() {
self.record_asset(metadata).await?;
}

Ok(())
}

/// Query for account balance by address
pub async fn balances(
&self,
Expand Down Expand Up @@ -1030,7 +1060,10 @@ impl Storage {
}).await?
}

#[tracing::instrument(skip(self))]
pub async fn record_asset(&self, asset: Metadata) -> anyhow::Result<()> {
tracing::debug!(?asset);

let asset_id = asset.id().to_bytes().to_vec();
let denom = asset.base_denom().denom;

Expand Down

0 comments on commit ae51783

Please sign in to comment.