Skip to content

Commit

Permalink
Add helpers used in DAS
Browse files Browse the repository at this point in the history
  • Loading branch information
danenbm committed Mar 13, 2024
1 parent 088d42f commit d46e979
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
53 changes: 53 additions & 0 deletions clients/rust/src/hooked/compression_proof.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use borsh::BorshDeserialize;

use crate::{
accounts::{BaseAsset, PluginHeader, PluginRegistry},
types::{CompressionProof, HashablePluginSchema, Plugin, RegistryRecord},
DataBlob,
};

/// Fetch asset and the plugin registry.
pub fn fetch_all(account: &[u8]) -> Result<CompressionProof, std::io::Error> {
let asset = BaseAsset::from_bytes(account)?;
let asset_size = asset.get_size();
let mut compression_proof = CompressionProof::new(asset.clone(), 0, vec![]);

if asset_size != account.len() {
let header = PluginHeader::from_bytes(&account[asset.get_size()..])?;
let plugin_registry =
PluginRegistry::from_bytes(&account[(header.plugin_registry_offset as usize)..])?;

let mut registry_records = plugin_registry.registry;

// It should already be sorted but we just want to make sure.
registry_records.sort_by(RegistryRecord::compare_offsets);

for (i, record) in registry_records.into_iter().enumerate() {
let plugin = Plugin::deserialize(&mut &account[record.offset as usize..])?;

let hashable_plugin_schema = HashablePluginSchema {
index: i as u64,
authority: record.authority,
plugin,
};

compression_proof.plugins.push(hashable_plugin_schema);
}
}
Ok(compression_proof)
}

impl CompressionProof {
/// Create a new `CompressionProof`. Note this uses a passed-in `seq` rather than
/// the one contained in `asset` to avoid errors.
pub fn new(asset: BaseAsset, seq: u64, plugins: Vec<HashablePluginSchema>) -> Self {
Self {
owner: asset.owner,
update_authority: asset.update_authority,
name: asset.name,
uri: asset.uri,
seq,
plugins,
}
}
}
17 changes: 14 additions & 3 deletions clients/rust/src/hooked/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
pub mod plugins;
pub use plugins::*;

pub mod compression_proof;
pub use compression_proof::*;

use borsh::{BorshDeserialize, BorshSerialize};
pub use plugins::*;
use std::mem::size_of;

use std::{cmp::Ordering, mem::size_of};

use crate::{
accounts::{BaseAsset, BaseCollection, PluginHeader, PluginRegistry},
errors::MplCoreError,
types::{Key, Plugin, PluginType},
types::{Key, Plugin, PluginType, RegistryRecord},
};
use solana_program::account_info::AccountInfo;

Expand Down Expand Up @@ -140,3 +144,10 @@ pub trait SolanaAccount: BorshSerialize + BorshDeserialize {
borsh::to_writer(&mut account.data.borrow_mut()[offset..], self)
}
}

impl RegistryRecord {
/// Associated function for sorting `RegistryRecords` by offset.
pub fn compare_offsets(a: &RegistryRecord, b: &RegistryRecord) -> Ordering {
a.offset.cmp(&b.offset)
}
}

0 comments on commit d46e979

Please sign in to comment.