Skip to content

Commit

Permalink
Change to basic compression schema
Browse files Browse the repository at this point in the history
  • Loading branch information
danenbm committed Feb 28, 2024
1 parent 5989864 commit 68e679d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
8 changes: 8 additions & 0 deletions programs/mpl-core/src/plugins/plugin_registry.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use borsh::{BorshDeserialize, BorshSerialize};
use shank::ShankAccount;
use std::cmp::Ordering;

use crate::state::{Authority, DataBlob, Key, SolanaAccount};

Expand Down Expand Up @@ -45,6 +46,13 @@ pub struct RegistryRecord {
pub offset: usize,
}

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

/// A simple type to store the mapping of external Plugin authority to Plugin data.
#[repr(C)]
#[derive(Clone, BorshSerialize, BorshDeserialize, Debug)]
Expand Down
27 changes: 15 additions & 12 deletions programs/mpl-core/src/processor/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use solana_program::{
use crate::{
error::MplCoreError,
instruction::accounts::CompressAccounts,
plugins::{fetch_plugins, Plugin},
plugins::{fetch_plugins, Plugin, RegistryRecord},
state::{
Asset, AuthorityVec, Compressible, DataBlob, HashedAsset, HashedAssetSchema, Key,
PluginHash, SolanaAccount,
Asset, Compressible, DataBlob, HashablePluginSchema, HashedAsset, HashedAssetSchema, Key,
SolanaAccount,
},
utils::load_key,
};
Expand Down Expand Up @@ -45,21 +45,24 @@ pub(crate) fn compress<'a>(accounts: &'a [AccountInfo<'a>], _args: CompressArgs)

let mut plugin_hashes = vec![];
if asset.get_size() != ctx.accounts.asset_address.data_len() {
let registry_records = fetch_plugins(ctx.accounts.asset_address)?;
let mut registry_records = fetch_plugins(ctx.accounts.asset_address)?;

for record in registry_records {
let authorities: AuthorityVec = record.authorities;
let plugin_authorities_hash = authorities.hash()?;
// 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 &(*ctx.accounts.asset_address.data).borrow()[record.offset..],
)?;
let plugin_hash = plugin.hash()?;

plugin_hashes.push(PluginHash {
plugin_authorities_hash,
plugin_hash,
});
let hashable_plugin_schema = HashablePluginSchema {
index: i,
authorities: record.authorities,
plugin,
};

let plugin_hash = hashable_plugin_schema.hash()?;
plugin_hashes.push(plugin_hash);
}
}

Expand Down
25 changes: 17 additions & 8 deletions programs/mpl-core/src/state/hashed_asset_schema.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
use crate::state::Compressible;
use crate::{
plugins::Plugin,
state::{Authority, Compressible},
};
use borsh::{BorshDeserialize, BorshSerialize};

/// The plugin hash is a hash of the plugin authority and the plugin itself.
/// A type that stores a plugin's authorities and deserialized data into a
/// schema that will be later hashed into a hashed asset.
#[derive(Clone, BorshSerialize, BorshDeserialize, Debug, PartialEq, Eq)]
pub struct PluginHash {
/// The hash of the plugin authorities.
pub plugin_authorities_hash: [u8; 32],
/// The hash of the plugin.
pub plugin_hash: [u8; 32],
pub struct HashablePluginSchema {
/// This is the order the plugins are stored in the account, allowing us
/// to keep track of their order in the hashing.
pub index: usize,
/// The authorities who have permission to utilize a plugin.
pub authorities: Vec<Authority>,
/// The deserialized plugin.
pub plugin: Plugin,
}

impl Compressible for HashablePluginSchema {}

/// The hashed asset schema is a schema that contains a hash of the asset and a vec of plugin hashes.
#[derive(Clone, BorshSerialize, BorshDeserialize, Debug, PartialEq, Eq)]
pub struct HashedAssetSchema {
/// The hash of the asset.
pub asset_hash: [u8; 32],
/// A vec of plugin hashes.
pub plugin_hashes: Vec<PluginHash>,
pub plugin_hashes: Vec<[u8; 32]>,
}

impl Compressible for HashedAssetSchema {}
4 changes: 0 additions & 4 deletions programs/mpl-core/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ pub enum Authority {
},
}

/// A vector of authorities.
pub type AuthorityVec = Vec<Authority>;
impl Compressible for AuthorityVec {}

/// Different types of extra accounts that can be passed in for lifecycle hooks.
#[repr(C)]
#[derive(Clone, BorshSerialize, BorshDeserialize, Debug, Eq, PartialEq)]
Expand Down

0 comments on commit 68e679d

Please sign in to comment.