mpl-core 0.12.1

A flexible digital asset standard for Solana
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
#![cfg(feature = "test-sbf")]
pub mod setup;

use std::borrow::BorrowMut;

use mpl_core::{
    accounts::BaseAssetV1,
    fetch_external_plugin_adapter_data_info,
    instructions::{
        AddExternalPluginAdapterV1Builder, UpdatePluginV1Builder,
        WriteExternalPluginAdapterDataV1Builder,
    },
    types::{
        AppDataInitInfo, Attribute, Attributes, ExternalPluginAdapterInitInfo,
        ExternalPluginAdapterKey, ExternalPluginAdapterSchema, FreezeDelegate, Plugin,
        PluginAuthority, PluginAuthorityPair,
    },
    Asset,
};
pub use setup::*;

use solana_program::account_info::AccountInfo;
use solana_program_test::tokio;
use solana_sdk::{signature::Keypair, signer::Signer, transaction::Transaction};

// ============================================================================
// Test 1: WriteExternalPluginAdapterDataV1 — regression test for shrinking the
// first of two AppData plugins.
//
// Previously, update_external_plugin_adapter_data() in plugins/utils.rs called
// resize_or_reallocate_account() before sol_memmove, so shrinking caused
// data_len() to return the new (smaller) size and saturating_sub yielded 0,
// moving nothing and corrupting trailing plugin data and/or the registry.
//
// The fix reorders: memmove first (while the buffer is full-size), then realloc.
// This test verifies the shrunk plugin, the trailing plugin, and the registry
// all survive intact.
// ============================================================================
#[tokio::test]
async fn test_write_external_plugin_adapter_data_shrink_preserves_second_plugin() {
    let mut context = program_test().start_with_context().await;

    // Step 1: Create an asset with TWO AppData plugins (different data authorities).
    let owner = Keypair::new();
    airdrop(&mut context, &owner.pubkey(), 10_000_000_000)
        .await
        .unwrap();

    let asset = Keypair::new();
    create_asset(
        &mut context,
        CreateAssetHelperArgs {
            owner: Some(owner.pubkey()),
            payer: None,
            asset: &asset,
            data_state: None,
            name: None,
            uri: None,
            authority: None,
            update_authority: None,
            collection: None,
            plugins: vec![],
            external_plugin_adapters: vec![ExternalPluginAdapterInitInfo::AppData(
                AppDataInitInfo {
                    init_plugin_authority: Some(PluginAuthority::UpdateAuthority),
                    data_authority: PluginAuthority::UpdateAuthority,
                    schema: Some(ExternalPluginAdapterSchema::Binary),
                },
            )],
        },
    )
    .await
    .unwrap();

    // Add a second AppData plugin keyed by Owner authority.
    let ix = AddExternalPluginAdapterV1Builder::new()
        .asset(asset.pubkey())
        .payer(context.payer.pubkey())
        .init_info(ExternalPluginAdapterInitInfo::AppData(AppDataInitInfo {
            init_plugin_authority: Some(PluginAuthority::UpdateAuthority),
            data_authority: PluginAuthority::Owner,
            schema: Some(ExternalPluginAdapterSchema::Binary),
        }))
        .instruction();

    let tx = Transaction::new_signed_with_payer(
        &[ix],
        Some(&context.payer.pubkey()),
        &[&context.payer],
        context.last_blockhash,
    );
    context.banks_client.process_transaction(tx).await.unwrap();

    // Step 2: Write LARGE data (500 bytes) to the FIRST AppData plugin.
    let large_data: Vec<u8> = (0..500).map(|i| (i % 256) as u8).collect();
    let ix = WriteExternalPluginAdapterDataV1Builder::new()
        .asset(asset.pubkey())
        .payer(context.payer.pubkey())
        .key(ExternalPluginAdapterKey::AppData(
            PluginAuthority::UpdateAuthority,
        ))
        .data(large_data.clone())
        .instruction();

    let tx = Transaction::new_signed_with_payer(
        &[ix],
        Some(&context.payer.pubkey()),
        &[&context.payer],
        context.last_blockhash,
    );
    context.banks_client.process_transaction(tx).await.unwrap();

    // Step 3: Write a known pattern to the SECOND AppData plugin.
    let second_plugin_data: Vec<u8> = vec![0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF];
    let ix = WriteExternalPluginAdapterDataV1Builder::new()
        .asset(asset.pubkey())
        .payer(context.payer.pubkey())
        .authority(Some(owner.pubkey()))
        .key(ExternalPluginAdapterKey::AppData(PluginAuthority::Owner))
        .data(second_plugin_data.clone())
        .instruction();

    let tx = Transaction::new_signed_with_payer(
        &[ix],
        Some(&context.payer.pubkey()),
        &[&context.payer, &owner],
        context.last_blockhash,
    );
    context.banks_client.process_transaction(tx).await.unwrap();

    // Verify both plugins are readable before shrink.
    let account_before = context
        .banks_client
        .get_account(asset.pubkey())
        .await
        .unwrap()
        .unwrap();
    let size_before = account_before.data.len();
    println!("Account size before shrink: {}", size_before);

    let asset_before = Asset::from_bytes(&account_before.data).unwrap();
    assert_eq!(asset_before.external_plugin_adapter_list.app_data.len(), 2);

    // Verify second plugin data is intact.
    {
        let mut account_copy = account_before.clone();
        let binding = asset.pubkey();
        let account_info = AccountInfo::new(
            &binding,
            false,
            false,
            &mut account_copy.lamports,
            account_copy.data.borrow_mut(),
            &account_copy.owner,
            false,
        );

        let (data_offset, data_len) = fetch_external_plugin_adapter_data_info::<BaseAssetV1>(
            &account_info,
            None,
            &ExternalPluginAdapterKey::AppData(PluginAuthority::Owner),
        )
        .unwrap();

        let data_slice = &account_copy.data[data_offset..data_offset + data_len];
        assert_eq!(
            data_slice, &second_plugin_data,
            "Second plugin data should be intact before shrink"
        );
    }

    // Step 4: SHRINK the first AppData from 500 bytes to 5 bytes.
    // This exercises the memmove-before-realloc path for a large shrink (495
    // bytes). The trailing plugin data and registry must be shifted left before
    // the account is truncated.
    let small_data: Vec<u8> = vec![0x01, 0x02, 0x03, 0x04, 0x05];
    let ix = WriteExternalPluginAdapterDataV1Builder::new()
        .asset(asset.pubkey())
        .payer(context.payer.pubkey())
        .key(ExternalPluginAdapterKey::AppData(
            PluginAuthority::UpdateAuthority,
        ))
        .data(small_data.clone())
        .instruction();

    let tx = Transaction::new_signed_with_payer(
        &[ix],
        Some(&context.payer.pubkey()),
        &[&context.payer],
        context.last_blockhash,
    );

    context.banks_client.process_transaction(tx).await.unwrap();

    // Step 5: Verify the asset is still intact after shrink.
    let account_after = context
        .banks_client
        .get_account(asset.pubkey())
        .await
        .unwrap()
        .unwrap();
    let size_after = account_after.data.len();
    assert!(
        size_after < size_before,
        "Expected account to shrink from {} to {}, but it did not",
        size_before,
        size_after
    );

    // Deserialize the asset — should not fail.
    let asset_after = Asset::from_bytes(&account_after.data)
        .expect("Asset deserialization should succeed after shrink — registry must remain intact");

    // Both AppData plugins should still be present.
    assert_eq!(
        asset_after.external_plugin_adapter_list.app_data.len(),
        2,
        "Both AppData plugins should survive the shrink"
    );

    // Second plugin's data should be readable and unchanged.
    {
        let mut account_copy = account_after.clone();
        let binding = asset.pubkey();
        let account_info = AccountInfo::new(
            &binding,
            false,
            false,
            &mut account_copy.lamports,
            account_copy.data.borrow_mut(),
            &account_copy.owner,
            false,
        );

        let (data_offset, data_len) = fetch_external_plugin_adapter_data_info::<BaseAssetV1>(
            &account_info,
            None,
            &ExternalPluginAdapterKey::AppData(PluginAuthority::Owner),
        )
        .expect("Should be able to fetch second plugin data after shrink");

        assert!(
            data_offset + data_len <= account_after.data.len(),
            "Second plugin data out of bounds: offset={} len={} account_size={}",
            data_offset,
            data_len,
            account_after.data.len()
        );

        let actual_data = &account_after.data[data_offset..data_offset + data_len];
        assert_eq!(
            actual_data, &second_plugin_data,
            "Second plugin data must be unchanged after shrinking the first plugin"
        );
    }

    // First plugin's data should now equal the shrunk payload.
    {
        let mut account_copy = account_after.clone();
        let binding = asset.pubkey();
        let account_info = AccountInfo::new(
            &binding,
            false,
            false,
            &mut account_copy.lamports,
            account_copy.data.borrow_mut(),
            &account_copy.owner,
            false,
        );

        let (data_offset, data_len) = fetch_external_plugin_adapter_data_info::<BaseAssetV1>(
            &account_info,
            None,
            &ExternalPluginAdapterKey::AppData(PluginAuthority::UpdateAuthority),
        )
        .expect("Should be able to fetch first plugin data after shrink");

        assert!(
            data_offset + data_len <= account_after.data.len(),
            "First plugin data out of bounds: offset={} len={} account_size={}",
            data_offset,
            data_len,
            account_after.data.len()
        );

        let actual_data = &account_after.data[data_offset..data_offset + data_len];
        assert_eq!(
            actual_data, &small_data,
            "First plugin data must equal the shrunk payload"
        );
    }
}

// ============================================================================
// Test 2: UpdatePluginV1 — regression test for shrinking an Attributes plugin
// when a FreezeDelegate plugin follows it.
//
// Previously, process_update_plugin() in processor/update_plugin.rs called
// resize_or_reallocate_account() before sol_memmove, so on shrink the memmove
// source region could extend beyond the new (truncated) account boundary.
//
// The fix reorders: memmove first (reads from the full-size buffer), then
// realloc. This test verifies the Attributes content, the trailing
// FreezeDelegate, and the registry all survive intact.
// ============================================================================
#[tokio::test]
async fn test_update_plugin_shrink_attributes_preserves_trailing_plugins() {
    let mut context = program_test().start_with_context().await;

    // Step 1: Create an asset with a LARGE Attributes plugin and a FreezeDelegate.
    // Attributes is variable-size (Vec<Attribute>), so we can shrink it.
    let asset = Keypair::new();

    // Create with many attributes to make it large.
    let large_attributes: Vec<Attribute> = (0..30)
        .map(|i| Attribute {
            key: format!("key_{:03}", i),
            value: format!(
                "value_{:03}_padding_to_make_this_larger_{}",
                i,
                "x".repeat(20)
            ),
        })
        .collect();

    create_asset(
        &mut context,
        CreateAssetHelperArgs {
            owner: None,
            payer: None,
            asset: &asset,
            data_state: None,
            name: None,
            uri: None,
            authority: None,
            update_authority: None,
            collection: None,
            plugins: vec![
                PluginAuthorityPair {
                    plugin: Plugin::Attributes(Attributes {
                        attribute_list: large_attributes.clone(),
                    }),
                    authority: None,
                },
                PluginAuthorityPair {
                    plugin: Plugin::FreezeDelegate(FreezeDelegate { frozen: false }),
                    authority: None,
                },
            ],
            external_plugin_adapters: vec![],
        },
    )
    .await
    .unwrap();

    // Verify initial state.
    let account_before = context
        .banks_client
        .get_account(asset.pubkey())
        .await
        .unwrap()
        .unwrap();
    let size_before = account_before.data.len();
    println!("Account size before shrink: {}", size_before);

    let asset_before = Asset::from_bytes(&account_before.data).unwrap();
    assert!(asset_before.plugin_list.freeze_delegate.is_some());
    let attrs = asset_before.plugin_list.attributes.as_ref().unwrap();
    assert_eq!(attrs.attributes.attribute_list.len(), 30);

    // Step 2: Update Attributes to have very few attributes (massive shrink).
    let small_attributes = vec![Attribute {
        key: "x".to_string(),
        value: "y".to_string(),
    }];

    let ix = UpdatePluginV1Builder::new()
        .asset(asset.pubkey())
        .payer(context.payer.pubkey())
        .plugin(Plugin::Attributes(Attributes {
            attribute_list: small_attributes.clone(),
        }))
        .instruction();

    let tx = Transaction::new_signed_with_payer(
        &[ix],
        Some(&context.payer.pubkey()),
        &[&context.payer],
        context.last_blockhash,
    );

    context
        .banks_client
        .process_transaction(tx)
        .await
        .expect("Attributes shrink transaction should succeed");

    // Step 3: Verify the asset is still fully readable and FreezeDelegate intact.
    let account_after = context
        .banks_client
        .get_account(asset.pubkey())
        .await
        .unwrap()
        .unwrap();
    let size_after = account_after.data.len();
    assert!(
        size_after < size_before,
        "Expected account to shrink from {} to {}, but it did not",
        size_before,
        size_after
    );

    let asset_after = Asset::from_bytes(&account_after.data)
        .expect("Asset deserialization should succeed after Attributes shrink");

    // Check Attributes has the exact expected content.
    let attrs_after = asset_after
        .plugin_list
        .attributes
        .as_ref()
        .expect("Attributes plugin must still be present after shrink");
    assert_eq!(
        attrs_after.attributes.attribute_list.len(),
        1,
        "Attributes should have exactly 1 entry after update"
    );
    assert_eq!(attrs_after.attributes.attribute_list[0].key, "x");
    assert_eq!(attrs_after.attributes.attribute_list[0].value, "y");

    // Check FreezeDelegate is still intact.
    let fd = asset_after
        .plugin_list
        .freeze_delegate
        .as_ref()
        .expect("FreezeDelegate must still be present after Attributes shrink");
    assert_eq!(
        fd.freeze_delegate,
        FreezeDelegate { frozen: false },
        "FreezeDelegate should be unchanged"
    );
}

// ============================================================================
// Test 3: WriteExternalPluginAdapterDataV1 — shrink with a single AppData
// plugin (regression/coverage guard).
//
// With only one AppData plugin, sol_memmove has no trailing plugin data to
// shift — the tail length is just the registry, which is re-serialized from
// the in-memory PluginRegistryV1 after the move anyway. So this case does
// not exercise the specific realloc-before-memmove corruption path that
// multi-plugin layouts hit. It still guards against shrink-related regressions
// (e.g. incorrect new_size, data_offset math, or registry save errors).
// ============================================================================
#[tokio::test]
async fn test_write_external_plugin_adapter_data_single_plugin_shrink() {
    let mut context = program_test().start_with_context().await;

    let asset = Keypair::new();
    create_asset(
        &mut context,
        CreateAssetHelperArgs {
            owner: None,
            payer: None,
            asset: &asset,
            data_state: None,
            name: None,
            uri: None,
            authority: None,
            update_authority: None,
            collection: None,
            plugins: vec![],
            external_plugin_adapters: vec![ExternalPluginAdapterInitInfo::AppData(
                AppDataInitInfo {
                    init_plugin_authority: Some(PluginAuthority::UpdateAuthority),
                    data_authority: PluginAuthority::UpdateAuthority,
                    schema: Some(ExternalPluginAdapterSchema::Binary),
                },
            )],
        },
    )
    .await
    .unwrap();

    // Write large data.
    let large_data: Vec<u8> = vec![0xAB; 800];
    let ix = WriteExternalPluginAdapterDataV1Builder::new()
        .asset(asset.pubkey())
        .payer(context.payer.pubkey())
        .key(ExternalPluginAdapterKey::AppData(
            PluginAuthority::UpdateAuthority,
        ))
        .data(large_data.clone())
        .instruction();

    let tx = Transaction::new_signed_with_payer(
        &[ix],
        Some(&context.payer.pubkey()),
        &[&context.payer],
        context.last_blockhash,
    );
    context.banks_client.process_transaction(tx).await.unwrap();

    // Verify pre-shrink.
    let account_before = context
        .banks_client
        .get_account(asset.pubkey())
        .await
        .unwrap()
        .unwrap();
    let size_before = account_before.data.len();
    println!("Account size before shrink: {}", size_before);

    let asset_before = Asset::from_bytes(&account_before.data).unwrap();
    assert_eq!(asset_before.external_plugin_adapter_list.app_data.len(), 1);

    // Shrink to tiny data.
    let small_data: Vec<u8> = vec![0x01];
    let ix = WriteExternalPluginAdapterDataV1Builder::new()
        .asset(asset.pubkey())
        .payer(context.payer.pubkey())
        .key(ExternalPluginAdapterKey::AppData(
            PluginAuthority::UpdateAuthority,
        ))
        .data(small_data.clone())
        .instruction();

    let tx = Transaction::new_signed_with_payer(
        &[ix],
        Some(&context.payer.pubkey()),
        &[&context.payer],
        context.last_blockhash,
    );

    context
        .banks_client
        .process_transaction(tx)
        .await
        .expect("Shrink transaction should succeed — program must handle shrinking gracefully");

    // Verify post-shrink: can we still deserialize and read the data?
    let account_after = context
        .banks_client
        .get_account(asset.pubkey())
        .await
        .unwrap()
        .unwrap();
    let size_after = account_after.data.len();
    assert!(
        size_after < size_before,
        "Expected account to shrink from {} to {}, but it did not",
        size_before,
        size_after
    );

    let asset_after = Asset::from_bytes(&account_after.data)
        .expect("Asset deserialization should succeed after single-plugin shrink");

    assert_eq!(
        asset_after.external_plugin_adapter_list.app_data.len(),
        1,
        "AppData plugin must still be present after shrink"
    );

    // Verify the data content matches what we wrote.
    {
        let mut account_copy = account_after.clone();
        let binding = asset.pubkey();
        let account_info = AccountInfo::new(
            &binding,
            false,
            false,
            &mut account_copy.lamports,
            account_copy.data.borrow_mut(),
            &account_copy.owner,
            false,
        );

        let (data_offset, data_len) = fetch_external_plugin_adapter_data_info::<BaseAssetV1>(
            &account_info,
            None,
            &ExternalPluginAdapterKey::AppData(PluginAuthority::UpdateAuthority),
        )
        .expect("Should be able to fetch AppData after single-plugin shrink");

        assert!(
            data_offset + data_len <= account_after.data.len(),
            "AppData region out of bounds: offset={} len={} account_size={}",
            data_offset,
            data_len,
            account_after.data.len()
        );

        let actual = &account_after.data[data_offset..data_offset + data_len];
        assert_eq!(
            actual, &small_data,
            "AppData content must match the shrunk payload"
        );
    }
}

// ============================================================================
// Test 4: UpdatePluginV1 — regression test for shrinking an Attributes plugin
// when an AppData external plugin is also present.
//
// Previously, process_update_plugin() in processor/update_plugin.rs called
// resize_or_reallocate_account() before sol_memmove, so on shrink the
// memmove's source region could extend beyond the truncated account boundary,
// corrupting the external plugin data stored after Attributes.
//
// The fix reorders: memmove first, then realloc. This test verifies the
// Attributes content, the trailing AppData plugin, and the registry all
// survive intact.
// ============================================================================
#[tokio::test]
async fn test_update_plugin_shrink_attributes_preserves_external_plugin() {
    let mut context = program_test().start_with_context().await;

    let asset = Keypair::new();

    // Create with large Attributes + an AppData external plugin.
    let large_attributes: Vec<Attribute> = (0..25)
        .map(|i| Attribute {
            key: format!("attr_{:03}", i),
            value: format!("val_{:03}_{}", i, "abcdefghijklmnopqrstuvwxyz".repeat(2)),
        })
        .collect();

    create_asset(
        &mut context,
        CreateAssetHelperArgs {
            owner: None,
            payer: None,
            asset: &asset,
            data_state: None,
            name: None,
            uri: None,
            authority: None,
            update_authority: None,
            collection: None,
            plugins: vec![PluginAuthorityPair {
                plugin: Plugin::Attributes(Attributes {
                    attribute_list: large_attributes.clone(),
                }),
                authority: None,
            }],
            external_plugin_adapters: vec![ExternalPluginAdapterInitInfo::AppData(
                AppDataInitInfo {
                    init_plugin_authority: Some(PluginAuthority::UpdateAuthority),
                    data_authority: PluginAuthority::UpdateAuthority,
                    schema: Some(ExternalPluginAdapterSchema::Binary),
                },
            )],
        },
    )
    .await
    .unwrap();

    // Write data to AppData.
    let app_data_content: Vec<u8> = vec![0xDE, 0xAD, 0xBE, 0xEF, 0xCA, 0xFE];
    let ix = WriteExternalPluginAdapterDataV1Builder::new()
        .asset(asset.pubkey())
        .payer(context.payer.pubkey())
        .key(ExternalPluginAdapterKey::AppData(
            PluginAuthority::UpdateAuthority,
        ))
        .data(app_data_content.clone())
        .instruction();

    let tx = Transaction::new_signed_with_payer(
        &[ix],
        Some(&context.payer.pubkey()),
        &[&context.payer],
        context.last_blockhash,
    );
    context.banks_client.process_transaction(tx).await.unwrap();

    // Verify pre-shrink state.
    let account_before = context
        .banks_client
        .get_account(asset.pubkey())
        .await
        .unwrap()
        .unwrap();
    let size_before = account_before.data.len();

    let asset_before = Asset::from_bytes(&account_before.data).unwrap();
    assert!(asset_before.plugin_list.attributes.is_some());
    assert_eq!(asset_before.external_plugin_adapter_list.app_data.len(), 1);
    println!("Account size before shrink: {}", size_before);

    // Shrink Attributes drastically.
    let small_attributes = vec![Attribute {
        key: "a".to_string(),
        value: "b".to_string(),
    }];

    let ix = UpdatePluginV1Builder::new()
        .asset(asset.pubkey())
        .payer(context.payer.pubkey())
        .plugin(Plugin::Attributes(Attributes {
            attribute_list: small_attributes,
        }))
        .instruction();

    let tx = Transaction::new_signed_with_payer(
        &[ix],
        Some(&context.payer.pubkey()),
        &[&context.payer],
        context.last_blockhash,
    );

    context
        .banks_client
        .process_transaction(tx)
        .await
        .expect("Attributes shrink transaction should succeed");

    // Verify post-shrink.
    let account_after = context
        .banks_client
        .get_account(asset.pubkey())
        .await
        .unwrap()
        .unwrap();
    let size_after = account_after.data.len();
    assert!(
        size_after < size_before,
        "Expected account to shrink from {} to {}, but it did not",
        size_before,
        size_after
    );

    let asset_after = Asset::from_bytes(&account_after.data)
        .expect("Asset deserialization should succeed after Attributes shrink");

    // Check Attributes has the exact expected content.
    let attrs_after = asset_after
        .plugin_list
        .attributes
        .as_ref()
        .expect("Attributes plugin must still be present after shrink");
    assert_eq!(
        attrs_after.attributes.attribute_list.len(),
        1,
        "Attributes should have exactly 1 entry after update"
    );
    assert_eq!(attrs_after.attributes.attribute_list[0].key, "a");
    assert_eq!(attrs_after.attributes.attribute_list[0].value, "b");

    // Verify AppData external plugin is still present.
    assert_eq!(
        asset_after.external_plugin_adapter_list.app_data.len(),
        1,
        "AppData plugin must still be present after Attributes shrink"
    );

    // Verify the AppData content is unchanged.
    {
        let mut account_copy = account_after.clone();
        let binding = asset.pubkey();
        let account_info = AccountInfo::new(
            &binding,
            false,
            false,
            &mut account_copy.lamports,
            account_copy.data.borrow_mut(),
            &account_copy.owner,
            false,
        );

        let (data_offset, data_len) = fetch_external_plugin_adapter_data_info::<BaseAssetV1>(
            &account_info,
            None,
            &ExternalPluginAdapterKey::AppData(PluginAuthority::UpdateAuthority),
        )
        .expect("Should be able to fetch AppData after Attributes shrink");

        assert!(
            data_offset + data_len <= account_after.data.len(),
            "AppData region out of bounds: offset={} len={} account_size={}",
            data_offset,
            data_len,
            account_after.data.len()
        );

        let actual = &account_after.data[data_offset..data_offset + data_len];
        assert_eq!(
            actual, &app_data_content,
            "AppData content must be unchanged after Attributes shrink"
        );
    }
}