Skip to content

Commit

Permalink
Use types in update_plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
danenbm committed Mar 8, 2024
1 parent fc9d3a6 commit fc9e0ae
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
12 changes: 6 additions & 6 deletions programs/mpl-core/src/plugins/freeze.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use borsh::{BorshDeserialize, BorshSerialize};
use solana_program::{account_info::AccountInfo, program_error::ProgramError};

use crate::state::{Asset, Authority, DataBlob};
use crate::state::{Authority, CoreAsset, DataBlob};

use super::{PluginValidation, ValidationResult};

Expand Down Expand Up @@ -54,21 +54,21 @@ impl PluginValidation for Freeze {
Ok(ValidationResult::Pass)
}

fn validate_update_plugin(
fn validate_update_plugin<T: CoreAsset>(
&self,
asset: &Asset,
core_asset: &T,
authority_info: &AccountInfo,
authority: &Authority,
) -> Result<ValidationResult, ProgramError> {
// The owner can't update the freeze status.
if (authority_info.key != &asset.owner
&& (authority_info.key == &asset.update_authority.key()
if (authority_info.key != core_asset.owner()
&& (authority_info.key == &core_asset.update_authority().key()
&& authority == (&Authority::UpdateAuthority))
|| authority == (&Authority::Pubkey {
address: *authority_info.key,
}))
// Unless the owner is the only authority.
|| (authority_info.key == &asset.owner
|| (authority_info.key == core_asset.owner()
&& authority == (&Authority::Owner))
{
Ok(ValidationResult::Approved)
Expand Down
26 changes: 14 additions & 12 deletions programs/mpl-core/src/plugins/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use solana_program::{account_info::AccountInfo, program_error::ProgramError};

use crate::{
error::MplCoreError,
state::{Asset, Authority, Key},
state::{Authority, CoreAsset, Key},
};

use super::{Plugin, PluginType, RegistryRecord};
Expand Down Expand Up @@ -116,25 +116,27 @@ impl Plugin {

/// Route the validation of the update_plugin action to the appropriate plugin.
/// There is no check for updating a plugin because the plugin itself MUST validate the change.
pub(crate) fn validate_update_plugin(
pub(crate) fn validate_update_plugin<T: CoreAsset>(
plugin: &Plugin,
asset: &Asset,
core_asset: &T,
authority: &AccountInfo,
_unused: Option<&AccountInfo>,
authorities: &Authority,
) -> Result<ValidationResult, ProgramError> {
match plugin {
Plugin::Reserved => Err(MplCoreError::InvalidPlugin.into()),
Plugin::Royalties(royalties) => {
royalties.validate_update_plugin(asset, authority, authorities)
royalties.validate_update_plugin(core_asset, authority, authorities)
}
Plugin::Freeze(freeze) => freeze.validate_update_plugin(asset, authority, authorities),
Plugin::Burn(burn) => burn.validate_update_plugin(asset, authority, authorities),
Plugin::Freeze(freeze) => {
freeze.validate_update_plugin(core_asset, authority, authorities)
}
Plugin::Burn(burn) => burn.validate_update_plugin(core_asset, authority, authorities),
Plugin::Transfer(transfer) => {
transfer.validate_update_plugin(asset, authority, authorities)
transfer.validate_update_plugin(core_asset, authority, authorities)
}
Plugin::UpdateDelegate(update_delegate) => {
update_delegate.validate_update_plugin(asset, authority, authorities)
update_delegate.validate_update_plugin(core_asset, authority, authorities)
}
}
}
Expand Down Expand Up @@ -296,14 +298,14 @@ pub(crate) trait PluginValidation {
) -> Result<ValidationResult, ProgramError>;

/// Validate the update_plugin lifecycle action.
fn validate_update_plugin(
fn validate_update_plugin<T: CoreAsset>(
&self,
asset: &Asset,
core_asset: &T,
authority_info: &AccountInfo,
authority: &Authority,
) -> Result<ValidationResult, ProgramError> {
if (authority_info.key == &asset.owner && authority == &Authority::Owner)
|| (authority_info.key == &asset.update_authority.key()
if (authority_info.key == core_asset.owner() && authority == &Authority::Owner)
|| (authority_info.key == &core_asset.update_authority().key()
&& authority == &Authority::UpdateAuthority)
|| authority
== (&Authority::Pubkey {
Expand Down
8 changes: 4 additions & 4 deletions programs/mpl-core/src/processor/update_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
error::MplCoreError,
instruction::accounts::{UpdateCollectionPluginAccounts, UpdatePluginAccounts},
plugins::{Plugin, PluginType, ValidationResult},
state::Key,
state::{Asset, Collection, Key},
utils::{fetch_core_data, load_key},
};

Expand Down Expand Up @@ -34,7 +34,7 @@ pub(crate) fn update_plugin<'a>(
return Err(MplCoreError::NotAvailable.into());
}

let (asset, _, plugin_registry) = fetch_core_data(ctx.accounts.asset)?;
let (asset, _, plugin_registry) = fetch_core_data::<Asset>(ctx.accounts.asset)?;
let plugin_registry = plugin_registry.ok_or(MplCoreError::PluginsNotInitialized)?;

let plugin_type: PluginType = (&args.plugin).into();
Expand Down Expand Up @@ -83,7 +83,7 @@ pub(crate) fn update_collection_plugin<'a>(
assert_signer(payer)?;
}

let (asset, _, plugin_registry) = fetch_core_data(ctx.accounts.collection)?;
let (collection, _, plugin_registry) = fetch_core_data::<Collection>(ctx.accounts.collection)?;
let plugin_registry = plugin_registry.ok_or(MplCoreError::PluginsNotInitialized)?;

let plugin_type: PluginType = (&args.plugin).into();
Expand All @@ -95,7 +95,7 @@ pub(crate) fn update_collection_plugin<'a>(

let result = Plugin::validate_update_plugin(
&Plugin::load(ctx.accounts.collection, registry_record.offset)?,
&asset,
&collection,
ctx.accounts.authority,
None,
&registry_record.authority,
Expand Down

0 comments on commit fc9e0ae

Please sign in to comment.