Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement bytemuck for plugin header loading #213

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions programs/mpl-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ pub enum MplCoreError {
/// 48 - Cannot move asset to collection with permanent delegates
#[error("Cannot move asset to collection with permanent delegates")]
PermanentDelegatesPreventMove,

/// 49 - Invalid plugin header data
#[error("Invalid plugin header data")]
InvalidPluginHeader,
}

impl PrintProgramError for MplCoreError {
Expand Down
2 changes: 2 additions & 0 deletions programs/mpl-core/src/plugins/plugin_header.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::state::{DataBlob, Key, SolanaAccount};
use borsh::{BorshDeserialize, BorshSerialize};
use shank::ShankAccount;
use bytemuck;

/// The plugin header is the first part of the plugin metadata.
/// This field stores the Key
/// And a pointer to the Plugin Registry stored at the end of the account.
#[repr(C)]
#[derive(Clone, BorshSerialize, BorshDeserialize, Debug, ShankAccount)]
#[derive(bytemuck::Pod, bytemuck::Zeroable)]
pub struct PluginHeaderV1 {
/// The Discriminator of the header which doubles as a Plugin metadata version.
pub key: Key, // 1
Expand Down
13 changes: 6 additions & 7 deletions programs/mpl-core/src/plugins/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use solana_program::{
pubkey::Pubkey,
};
use std::collections::HashSet;
use bytemuck;

use crate::{
error::MplCoreError,
Expand Down Expand Up @@ -578,8 +579,8 @@ pub fn delete_plugin<'a, T: DataBlob>(
return Err(MplCoreError::PluginNotFound.into());
}

//TODO: Bytemuck this.
let mut header = PluginHeaderV1::load(account, asset.len())?;
let header = bytemuck::try_from_bytes::<PluginHeaderV1>(&account.data[..std::mem::size_of::<PluginHeaderV1>()])
.map_err(|_| MplCoreError::InvalidPluginHeader)?;
let mut plugin_registry = PluginRegistryV1::load(account, header.plugin_registry_offset)?;

if let Some(index) = plugin_registry
Expand Down Expand Up @@ -659,13 +660,11 @@ pub fn delete_external_plugin_adapter<'a, T: DataBlob>(
return Err(MplCoreError::ExternalPluginAdapterNotFound.into());
}

//TODO: Bytemuck this.
let mut header = PluginHeaderV1::load(account, asset.len())?;
let header = bytemuck::try_from_bytes::<PluginHeaderV1>(&account.data[..std::mem::size_of::<PluginHeaderV1>()])
.map_err(|_| MplCoreError::InvalidPluginHeader)?;
let mut plugin_registry = PluginRegistryV1::load(account, header.plugin_registry_offset)?;

let result = find_external_plugin_adapter(&plugin_registry, plugin_key, account)?;

if let (Some(index), _) = result {
if let Some(index) = plugin_registry.find_external_plugin_adapter_index(plugin_key) {
let registry_record = plugin_registry.external_registry.remove(index);
let serialized_registry_record = registry_record.try_to_vec()?;

Expand Down
2 changes: 2 additions & 0 deletions programs/mpl-core/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ impl DataBlob for Authority {
FromPrimitive,
EnumIter,
)]
#[derive(bytemuck::Pod, bytemuck::Zeroable)]
#[repr(u8)]
pub enum Key {
/// Uninitialized or invalid account.
Uninitialized,
Expand Down
Loading