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

Modify update and update_plugin to move external plugin offsets #112

Merged
merged 3 commits into from
May 8, 2024
Merged
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
66 changes: 66 additions & 0 deletions clients/js/test/externalPlugins/oracle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1854,3 +1854,69 @@ test('it can update with oracle', async (t) => {
asset: asset.publicKey,
});
});

test('it can update oracle to different size external plugin', async (t) => {
const umi = await createUmi();
const oracleSigner = generateSigner(umi);
await fixedAccountInit(umi, {
signer: umi.identity,
account: oracleSigner,
args: {
oracleData: {
__kind: 'V1',
create: ExternalValidationResult.Pass,
transfer: ExternalValidationResult.Pass,
burn: ExternalValidationResult.Pass,
update: ExternalValidationResult.Rejected,
},
},
}).sendAndConfirm(umi);

const asset = generateSigner(umi);
await create(umi, {
asset,
name: 'Test name',
uri: 'https://example.com',
plugins: [
{
type: 'Oracle',
resultsOffset: {
type: 'Anchor',
},
lifecycleChecks: {
create: [CheckResult.CAN_REJECT],
update: [CheckResult.CAN_REJECT],
transfer: [CheckResult.CAN_REJECT],
burn: [CheckResult.CAN_REJECT],
},
baseAddress: oracleSigner.publicKey,
},
],
}).sendAndConfirm(umi);

await updatePlugin(umi, {
asset: asset.publicKey,

plugin: {
key: {
type: 'Oracle',
baseAddress: oracleSigner.publicKey,
},
type: 'Oracle',
resultsOffset: {
type: 'Anchor',
},
lifecycleChecks: {
transfer: [CheckResult.CAN_REJECT],
},
},
}).sendAndConfirm(umi);

// TODO: Validate external plugins.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably check the oracle to see whether the plugin updated correctly too

await assertAsset(t, umi, {
uri: 'https://example.com',
name: 'Test name',
owner: umi.identity.publicKey,
asset: asset.publicKey,
});
});
34 changes: 19 additions & 15 deletions programs/mpl-core/src/processor/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
instruction::accounts::{UpdateCollectionV1Accounts, UpdateV1Accounts},
plugins::{
ExternalPlugin, HookableLifecycleEvent, Plugin, PluginHeaderV1, PluginRegistryV1,
PluginType, RegistryRecord,
PluginType,
},
state::{AssetV1, CollectionV1, DataBlob, Key, SolanaAccount, UpdateAuthority},
utils::{
Expand Down Expand Up @@ -236,20 +236,24 @@ fn process_update<'a, T: DataBlob + SolanaAccount>(
);

plugin_header.save(account, new_core_size as usize)?;
plugin_registry.registry = plugin_registry
.registry
.iter_mut()
.map(|record| {
let new_offset = (record.offset as isize)
.checked_add(size_diff)
.ok_or(MplCoreError::NumericalOverflow)?;
Ok(RegistryRecord {
plugin_type: record.plugin_type,
offset: new_offset as usize,
authority: record.authority,
})
})
.collect::<Result<Vec<_>, MplCoreError>>()?;

// Move offsets for existing registry records.
for record in &mut plugin_registry.external_registry {
let new_offset = (record.offset as isize)
.checked_add(size_diff)
.ok_or(MplCoreError::NumericalOverflow)?;

record.offset = new_offset as usize;
}

for record in &mut plugin_registry.registry {
let new_offset = (record.offset as isize)
.checked_add(size_diff)
.ok_or(MplCoreError::NumericalOverflow)?;

record.offset = new_offset as usize;
}

plugin_registry.save(account, new_registry_offset as usize)?;
} else {
resize_or_reallocate_account(account, payer, system_program, core.get_size())?;
Expand Down
167 changes: 55 additions & 112 deletions programs/mpl-core/src/processor/update_external_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::{
UpdateCollectionExternalPluginV1Accounts, UpdateExternalPluginV1Accounts,
},
plugins::{
fetch_wrapped_external_plugin, find_external_plugin, ExternalPluginKey,
ExternalPluginUpdateInfo, Plugin, PluginType,
fetch_wrapped_external_plugin, find_external_plugin, ExternalPlugin, ExternalPluginKey,
ExternalPluginUpdateInfo, Plugin, PluginHeaderV1, PluginRegistryV1, PluginType,
},
state::{AssetV1, CollectionV1, DataBlob, Key, SolanaAccount},
utils::{
Expand Down Expand Up @@ -76,92 +76,20 @@ pub(crate) fn update_external_plugin<'a>(
None,
)?;

let mut plugin_registry = plugin_registry.ok_or(MplCoreError::PluginsNotInitialized)?;
let mut plugin_header = plugin_header.ok_or(MplCoreError::PluginsNotInitialized)?;

let plugin_registry_clone = plugin_registry.clone();
let (_, record) = find_external_plugin(&plugin_registry_clone, &args.key, ctx.accounts.asset)?;
let mut registry_record = record.ok_or(MplCoreError::PluginNotFound)?.clone();
registry_record.update(&args.update_info)?;

let mut new_plugin = plugin.clone();
new_plugin.update(&args.update_info);

let plugin_data = plugin.try_to_vec()?;
let new_plugin_data = new_plugin.try_to_vec()?;

// The difference in size between the new and old account which is used to calculate the new size of the account.
let plugin_size = plugin_data.len() as isize;
let size_diff = (new_plugin_data.len() as isize)
.checked_sub(plugin_size)
.ok_or(MplCoreError::NumericalOverflow)?;

// The new size of the account.
let new_size = (ctx.accounts.asset.data_len() as isize)
.checked_add(size_diff)
.ok_or(MplCoreError::NumericalOverflow)?;

// The new offset of the plugin registry is the old offset plus the size difference.
let registry_offset = plugin_header.plugin_registry_offset;
let new_registry_offset = (registry_offset as isize)
.checked_add(size_diff)
.ok_or(MplCoreError::NumericalOverflow)?;
plugin_header.plugin_registry_offset = new_registry_offset as usize;

// The offset of the first plugin is the plugin offset plus the size of the plugin.
let next_plugin_offset = (registry_record.offset as isize)
.checked_add(plugin_size)
.ok_or(MplCoreError::NumericalOverflow)?;

let new_next_plugin_offset = next_plugin_offset
.checked_add(size_diff)
.ok_or(MplCoreError::NumericalOverflow)?;

// //TODO: This is memory intensive, we should use memmove instead probably.
let src =
ctx.accounts.asset.data.borrow()[(next_plugin_offset as usize)..registry_offset].to_vec();
// Increment sequence number and save only if it is `Some(_)`.
asset.increment_seq_and_save(ctx.accounts.asset)?;

resize_or_reallocate_account(
process_update_external_plugin(
asset,
plugin,
args.key,
args.update_info,
plugin_header,
plugin_registry,
ctx.accounts.asset,
ctx.accounts.payer,
ctx.accounts.system_program,
new_size as usize,
)?;

sol_memcpy(
&mut ctx.accounts.asset.data.borrow_mut()[(new_next_plugin_offset as usize)..],
&src,
src.len(),
);

plugin_header.save(ctx.accounts.asset, asset.get_size())?;

// Move offsets for existing registry records.
for record in &mut plugin_registry.external_registry {
if registry_record.offset < record.offset {
record
.offset
.checked_add(size_diff as usize)
.ok_or(MplCoreError::NumericalOverflow)?;
}
}

for record in &mut plugin_registry.registry {
if registry_record.offset < record.offset {
record
.offset
.checked_add(size_diff as usize)
.ok_or(MplCoreError::NumericalOverflow)?;
}
}

plugin_registry.save(ctx.accounts.asset, new_registry_offset as usize)?;
new_plugin.save(ctx.accounts.asset, registry_record.offset)?;

// Increment sequence number and save only if it is `Some(_)`.
asset.increment_seq_and_save(ctx.accounts.asset)?;

process_update_external_plugin()
)
}

#[repr(C)]
Expand Down Expand Up @@ -212,17 +140,41 @@ pub(crate) fn update_collection_external_plugin<'a>(
None,
)?;

process_update_external_plugin(
collection,
plugin,
args.key,
args.update_info,
plugin_header,
plugin_registry,
ctx.accounts.collection,
ctx.accounts.payer,
ctx.accounts.system_program,
)
}

#[allow(clippy::too_many_arguments)]
fn process_update_external_plugin<'a, T: DataBlob + SolanaAccount>(
core: T,
plugin: ExternalPlugin,
key: ExternalPluginKey,
update_info: ExternalPluginUpdateInfo,
plugin_header: Option<PluginHeaderV1>,
plugin_registry: Option<PluginRegistryV1>,
account: &AccountInfo<'a>,
payer: &AccountInfo<'a>,
system_program: &AccountInfo<'a>,
) -> ProgramResult {
let mut plugin_registry = plugin_registry.ok_or(MplCoreError::PluginsNotInitialized)?;
let mut plugin_header = plugin_header.ok_or(MplCoreError::PluginsNotInitialized)?;

let plugin_registry_clone = plugin_registry.clone();
let (_, record) =
find_external_plugin(&plugin_registry_clone, &args.key, ctx.accounts.collection)?;
let (_, record) = find_external_plugin(&plugin_registry_clone, &key, account)?;
let mut registry_record = record.ok_or(MplCoreError::PluginNotFound)?.clone();
registry_record.update(&args.update_info)?;
registry_record.update(&update_info)?;

let mut new_plugin = plugin.clone();
new_plugin.update(&args.update_info);
new_plugin.update(&update_info);

let plugin_data = plugin.try_to_vec()?;
let new_plugin_data = new_plugin.try_to_vec()?;
Expand All @@ -234,7 +186,7 @@ pub(crate) fn update_collection_external_plugin<'a>(
.ok_or(MplCoreError::NumericalOverflow)?;

// The new size of the account.
let new_size = (ctx.accounts.collection.data_len() as isize)
let new_size = (account.data_len() as isize)
.checked_add(size_diff)
.ok_or(MplCoreError::NumericalOverflow)?;

Expand All @@ -255,50 +207,41 @@ pub(crate) fn update_collection_external_plugin<'a>(
.ok_or(MplCoreError::NumericalOverflow)?;

// //TODO: This is memory intensive, we should use memmove instead probably.
let src = ctx.accounts.collection.data.borrow()[(next_plugin_offset as usize)..registry_offset]
.to_vec();
let src = account.data.borrow()[(next_plugin_offset as usize)..registry_offset].to_vec();

resize_or_reallocate_account(
ctx.accounts.collection,
ctx.accounts.payer,
ctx.accounts.system_program,
new_size as usize,
)?;
resize_or_reallocate_account(account, payer, system_program, new_size as usize)?;

sol_memcpy(
&mut ctx.accounts.collection.data.borrow_mut()[(new_next_plugin_offset as usize)..],
&mut account.data.borrow_mut()[(new_next_plugin_offset as usize)..],
&src,
src.len(),
);

plugin_header.save(ctx.accounts.collection, collection.get_size())?;
plugin_header.save(account, core.get_size())?;

// Move offsets for existing registry records.
for record in &mut plugin_registry.external_registry {
if registry_record.offset < record.offset {
record
.offset
.checked_add(size_diff as usize)
let new_offset = (record.offset as isize)
.checked_add(size_diff)
.ok_or(MplCoreError::NumericalOverflow)?;

record.offset = new_offset as usize;
}
}

for record in &mut plugin_registry.registry {
if registry_record.offset < record.offset {
record
.offset
.checked_add(size_diff as usize)
let new_offset = (record.offset as isize)
.checked_add(size_diff)
.ok_or(MplCoreError::NumericalOverflow)?;

record.offset = new_offset as usize;
}
}

plugin_registry.save(ctx.accounts.collection, new_registry_offset as usize)?;
new_plugin.save(ctx.accounts.collection, registry_record.offset)?;

process_update_external_plugin()
}
plugin_registry.save(account, new_registry_offset as usize)?;
new_plugin.save(account, registry_record.offset)?;

//TODO
fn process_update_external_plugin() -> ProgramResult {
Ok(())
}
Loading