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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
//! State transition types

use {
    crate::{account::ExtraAccountMeta, error::AccountResolutionError},
    solana_program::{
        account_info::AccountInfo,
        instruction::{AccountMeta, Instruction},
        program_error::ProgramError,
        pubkey::Pubkey,
    },
    spl_discriminator::SplDiscriminate,
    spl_pod::slice::{PodSlice, PodSliceMut},
    spl_type_length_value::state::{TlvState, TlvStateBorrowed, TlvStateMut},
    std::future::Future,
};

/// Type representing the output of an account fetching function, for easy
/// chaining between APIs
pub type AccountDataResult = Result<Option<Vec<u8>>, AccountFetchError>;
/// Generic error type that can come out of any client while fetching account
/// data
pub type AccountFetchError = Box<dyn std::error::Error + Send + Sync>;

/// Helper to convert an `AccountInfo` to an `AccountMeta`
fn account_info_to_meta(account_info: &AccountInfo) -> AccountMeta {
    AccountMeta {
        pubkey: *account_info.key,
        is_signer: account_info.is_signer,
        is_writable: account_info.is_writable,
    }
}

/// De-escalate an account meta if necessary
fn de_escalate_account_meta(account_meta: &mut AccountMeta, account_metas: &[AccountMeta]) {
    // This is a little tricky to read, but the idea is to see if
    // this account is marked as writable or signer anywhere in
    // the instruction at the start. If so, DON'T escalate it to
    // be a writer or signer in the CPI
    let maybe_highest_privileges = account_metas
        .iter()
        .filter(|&x| x.pubkey == account_meta.pubkey)
        .map(|x| (x.is_signer, x.is_writable))
        .reduce(|acc, x| (acc.0 || x.0, acc.1 || x.1));
    // If `Some`, then the account was found somewhere in the instruction
    if let Some((is_signer, is_writable)) = maybe_highest_privileges {
        if !is_signer && is_signer != account_meta.is_signer {
            // Existing account is *NOT* a signer already, but the CPI
            // wants it to be, so de-escalate to not be a signer
            account_meta.is_signer = false;
        }
        if !is_writable && is_writable != account_meta.is_writable {
            // Existing account is *NOT* writable already, but the CPI
            // wants it to be, so de-escalate to not be writable
            account_meta.is_writable = false;
        }
    }
}

/// Stateless helper for storing additional accounts required for an
/// instruction.
///
/// This struct works with any `SplDiscriminate`, and stores the extra accounts
/// needed for that specific instruction, using the given `ArrayDiscriminator`
/// as the type-length-value `ArrayDiscriminator`, and then storing all of the
/// given `AccountMeta`s as a zero-copy slice.
///
/// Sample usage:
///
/// ```rust
/// use {
///     futures_util::TryFutureExt,
///     solana_client::nonblocking::rpc_client::RpcClient,
///     solana_program::{
///         account_info::AccountInfo, instruction::{AccountMeta, Instruction},
///         pubkey::Pubkey
///     },
///     spl_discriminator::{ArrayDiscriminator, SplDiscriminate},
///     spl_tlv_account_resolution::{
///         account::ExtraAccountMeta,
///         seeds::Seed,
///         state::{AccountDataResult, AccountFetchError, ExtraAccountMetaList}
///     },
/// };
///
/// struct MyInstruction;
/// impl SplDiscriminate for MyInstruction {
///     // Give it a unique discriminator, can also be generated using a hash function
///     const SPL_DISCRIMINATOR: ArrayDiscriminator = ArrayDiscriminator::new([1; ArrayDiscriminator::LENGTH]);
/// }
///
/// // actually put it in the additional required account keys and signer / writable
/// let extra_metas = [
///     AccountMeta::new(Pubkey::new_unique(), false).into(),
///     AccountMeta::new_readonly(Pubkey::new_unique(), false).into(),
///     ExtraAccountMeta::new_with_seeds(
///         &[
///             Seed::Literal {
///                 bytes: b"some_string".to_vec(),
///             },
///             Seed::InstructionData {
///                 index: 1,
///                 length: 1, // u8
///             },
///             Seed::AccountKey { index: 1 },
///         ],
///         false,
///         true,
///     ).unwrap(),
///     ExtraAccountMeta::new_external_pda_with_seeds(
///         0,
///         &[Seed::AccountKey { index: 2 }],
///         false,
///         false,
///     ).unwrap(),
/// ];
///
/// // assume that this buffer is actually account data, already allocated to `account_size`
/// let account_size = ExtraAccountMetaList::size_of(extra_metas.len()).unwrap();
/// let mut buffer = vec![0; account_size];
///
/// // Initialize the structure for your instruction
/// ExtraAccountMetaList::init::<MyInstruction>(&mut buffer, &extra_metas).unwrap();
///
/// // Off-chain, you can add the additional accounts directly from the account data
/// // You need to provide the resolver a way to fetch account data off-chain
/// struct MyClient {
///     client: RpcClient,
/// }
/// impl MyClient {
///     pub fn new() -> Self {
///         Self {
///             client: RpcClient::new_mock("succeeds".to_string()),
///         }
///     }
///     pub async fn get_account_data(&self, address: Pubkey) -> AccountDataResult {
///         self.client.get_account(&address)
///             .await
///             .map(|acct| Some(acct.data))
///             .map_err(|e| Box::new(e) as AccountFetchError)
///     }
/// }
///
/// let client = MyClient::new();
/// let program_id = Pubkey::new_unique();
/// let mut instruction = Instruction::new_with_bytes(program_id, &[0, 1, 2], vec![]);
/// # futures::executor::block_on(async {
///     // Now use the resolver to add the additional accounts off-chain
///     ExtraAccountMetaList::add_to_instruction::<MyInstruction, _, _>(
///         &mut instruction,
///         |address: Pubkey| client.get_account_data(address),
///         &buffer,
///     )
///     .await;
/// # });
///
/// // On-chain, you can add the additional accounts *and* account infos
/// let mut cpi_instruction = Instruction::new_with_bytes(program_id, &[0, 1, 2], vec![]);
/// let mut cpi_account_infos = vec![]; // assume the other required account infos are already included
/// let remaining_account_infos: &[AccountInfo<'_>] = &[]; // these are the account infos provided to the instruction that are *not* part of any other known interface
/// ExtraAccountMetaList::add_to_cpi_instruction::<MyInstruction>(
///     &mut cpi_instruction,
///     &mut cpi_account_infos,
///     &buffer,
///     &remaining_account_infos,
/// );
/// ```
pub struct ExtraAccountMetaList;
impl ExtraAccountMetaList {
    /// Initialize pod slice data for the given instruction and its required
    /// list of `ExtraAccountMeta`s
    pub fn init<T: SplDiscriminate>(
        data: &mut [u8],
        extra_account_metas: &[ExtraAccountMeta],
    ) -> Result<(), ProgramError> {
        let mut state = TlvStateMut::unpack(data).unwrap();
        let tlv_size = PodSlice::<ExtraAccountMeta>::size_of(extra_account_metas.len())?;
        let (bytes, _) = state.alloc::<T>(tlv_size, false)?;
        let mut validation_data = PodSliceMut::init(bytes)?;
        for meta in extra_account_metas {
            validation_data.push(*meta)?;
        }
        Ok(())
    }

    /// Update pod slice data for the given instruction and its required
    /// list of `ExtraAccountMeta`s
    pub fn update<T: SplDiscriminate>(
        data: &mut [u8],
        extra_account_metas: &[ExtraAccountMeta],
    ) -> Result<(), ProgramError> {
        let mut state = TlvStateMut::unpack(data).unwrap();
        let tlv_size = PodSlice::<ExtraAccountMeta>::size_of(extra_account_metas.len())?;
        let bytes = state.realloc_first::<T>(tlv_size)?;
        let mut validation_data = PodSliceMut::init(bytes)?;
        for meta in extra_account_metas {
            validation_data.push(*meta)?;
        }
        Ok(())
    }

    /// Get the underlying `PodSlice<ExtraAccountMeta>` from an unpacked TLV
    ///
    /// Due to lifetime annoyances, this function can't just take in the bytes,
    /// since then we would be returning a reference to a locally created
    /// `TlvStateBorrowed`. I hope there's a better way to do this!
    pub fn unpack_with_tlv_state<'a, T: SplDiscriminate>(
        tlv_state: &'a TlvStateBorrowed,
    ) -> Result<PodSlice<'a, ExtraAccountMeta>, ProgramError> {
        let bytes = tlv_state.get_first_bytes::<T>()?;
        PodSlice::<ExtraAccountMeta>::unpack(bytes)
    }

    /// Get the byte size required to hold `num_items` items
    pub fn size_of(num_items: usize) -> Result<usize, ProgramError> {
        Ok(TlvStateBorrowed::get_base_len()
            .saturating_add(PodSlice::<ExtraAccountMeta>::size_of(num_items)?))
    }

    /// Checks provided account infos against validation data, using
    /// instruction data and program ID to resolve any dynamic PDAs
    /// if necessary.
    ///
    /// Note: this function will also verify all extra required accounts
    /// have been provided in the correct order
    pub fn check_account_infos<T: SplDiscriminate>(
        account_infos: &[AccountInfo],
        instruction_data: &[u8],
        program_id: &Pubkey,
        data: &[u8],
    ) -> Result<(), ProgramError> {
        let state = TlvStateBorrowed::unpack(data).unwrap();
        let extra_meta_list = ExtraAccountMetaList::unpack_with_tlv_state::<T>(&state)?;
        let extra_account_metas = extra_meta_list.data();

        let initial_accounts_len = account_infos.len() - extra_account_metas.len();

        // Convert to `AccountMeta` to check resolved metas
        let provided_metas = account_infos
            .iter()
            .map(account_info_to_meta)
            .collect::<Vec<_>>();

        for (i, config) in extra_account_metas.iter().enumerate() {
            let meta = {
                // Create a list of `Ref`s so we can reference account data in the
                // resolution step
                let account_key_data_refs = account_infos
                    .iter()
                    .map(|info| {
                        let key = *info.key;
                        let data = info.try_borrow_data()?;
                        Ok((key, data))
                    })
                    .collect::<Result<Vec<_>, ProgramError>>()?;

                config.resolve(instruction_data, program_id, |usize| {
                    account_key_data_refs
                        .get(usize)
                        .map(|(pubkey, opt_data)| (pubkey, Some(opt_data.as_ref())))
                })?
            };

            // Ensure the account is in the correct position
            let expected_index = i
                .checked_add(initial_accounts_len)
                .ok_or::<ProgramError>(AccountResolutionError::CalculationFailure.into())?;
            if provided_metas.get(expected_index) != Some(&meta) {
                return Err(AccountResolutionError::IncorrectAccount.into());
            }
        }

        Ok(())
    }

    /// Add the additional account metas to an existing instruction
    pub async fn add_to_instruction<T: SplDiscriminate, F, Fut>(
        instruction: &mut Instruction,
        fetch_account_data_fn: F,
        data: &[u8],
    ) -> Result<(), ProgramError>
    where
        F: Fn(Pubkey) -> Fut,
        Fut: Future<Output = AccountDataResult>,
    {
        let state = TlvStateBorrowed::unpack(data)?;
        let bytes = state.get_first_bytes::<T>()?;
        let extra_account_metas = PodSlice::<ExtraAccountMeta>::unpack(bytes)?;

        // Fetch account data for each of the instruction accounts
        let mut account_key_datas = vec![];
        for meta in instruction.accounts.iter() {
            let account_data = fetch_account_data_fn(meta.pubkey)
                .await
                .map_err::<ProgramError, _>(|_| {
                    AccountResolutionError::AccountFetchFailed.into()
                })?;
            account_key_datas.push((meta.pubkey, account_data));
        }

        for extra_meta in extra_account_metas.data().iter() {
            let mut meta =
                extra_meta.resolve(&instruction.data, &instruction.program_id, |usize| {
                    account_key_datas
                        .get(usize)
                        .map(|(pubkey, opt_data)| (pubkey, opt_data.as_ref().map(|x| x.as_slice())))
                })?;
            de_escalate_account_meta(&mut meta, &instruction.accounts);

            // Fetch account data for the new account
            account_key_datas.push((
                meta.pubkey,
                fetch_account_data_fn(meta.pubkey)
                    .await
                    .map_err::<ProgramError, _>(|_| {
                        AccountResolutionError::AccountFetchFailed.into()
                    })?,
            ));
            instruction.accounts.push(meta);
        }
        Ok(())
    }

    /// Add the additional account metas and account infos for a CPI
    pub fn add_to_cpi_instruction<'a, T: SplDiscriminate>(
        cpi_instruction: &mut Instruction,
        cpi_account_infos: &mut Vec<AccountInfo<'a>>,
        data: &[u8],
        account_infos: &[AccountInfo<'a>],
    ) -> Result<(), ProgramError> {
        let state = TlvStateBorrowed::unpack(data)?;
        let bytes = state.get_first_bytes::<T>()?;
        let extra_account_metas = PodSlice::<ExtraAccountMeta>::unpack(bytes)?;

        for extra_meta in extra_account_metas.data().iter() {
            let mut meta = {
                // Create a list of `Ref`s so we can reference account data in the
                // resolution step
                let account_key_data_refs = cpi_account_infos
                    .iter()
                    .map(|info| {
                        let key = *info.key;
                        let data = info.try_borrow_data()?;
                        Ok((key, data))
                    })
                    .collect::<Result<Vec<_>, ProgramError>>()?;

                extra_meta.resolve(
                    &cpi_instruction.data,
                    &cpi_instruction.program_id,
                    |usize| {
                        account_key_data_refs
                            .get(usize)
                            .map(|(pubkey, opt_data)| (pubkey, Some(opt_data.as_ref())))
                    },
                )?
            };
            de_escalate_account_meta(&mut meta, &cpi_instruction.accounts);

            let account_info = account_infos
                .iter()
                .find(|&x| *x.key == meta.pubkey)
                .ok_or(AccountResolutionError::IncorrectAccount)?
                .clone();

            cpi_instruction.accounts.push(meta);
            cpi_account_infos.push(account_info);
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use {
        super::*,
        crate::seeds::Seed,
        solana_program::{clock::Epoch, instruction::AccountMeta, pubkey::Pubkey},
        solana_program_test::tokio,
        spl_discriminator::{ArrayDiscriminator, SplDiscriminate},
        std::collections::HashMap,
    };

    pub struct TestInstruction;
    impl SplDiscriminate for TestInstruction {
        const SPL_DISCRIMINATOR: ArrayDiscriminator =
            ArrayDiscriminator::new([1; ArrayDiscriminator::LENGTH]);
    }

    pub struct TestOtherInstruction;
    impl SplDiscriminate for TestOtherInstruction {
        const SPL_DISCRIMINATOR: ArrayDiscriminator =
            ArrayDiscriminator::new([2; ArrayDiscriminator::LENGTH]);
    }

    pub struct MockRpc<'a> {
        cache: HashMap<Pubkey, &'a AccountInfo<'a>>,
    }
    impl<'a> MockRpc<'a> {
        pub fn setup(account_infos: &'a [AccountInfo<'a>]) -> Self {
            let mut cache = HashMap::new();
            for info in account_infos {
                cache.insert(*info.key, info);
            }
            Self { cache }
        }

        pub async fn get_account_data(&self, pubkey: Pubkey) -> AccountDataResult {
            Ok(self
                .cache
                .get(&pubkey)
                .map(|account| account.try_borrow_data().unwrap().to_vec()))
        }
    }

    #[tokio::test]
    async fn init_with_metas() {
        let metas = [
            AccountMeta::new(Pubkey::new_unique(), false).into(),
            AccountMeta::new(Pubkey::new_unique(), true).into(),
            AccountMeta::new_readonly(Pubkey::new_unique(), true).into(),
            AccountMeta::new_readonly(Pubkey::new_unique(), false).into(),
        ];
        let account_size = ExtraAccountMetaList::size_of(metas.len()).unwrap();
        let mut buffer = vec![0; account_size];

        ExtraAccountMetaList::init::<TestInstruction>(&mut buffer, &metas).unwrap();

        let mock_rpc = MockRpc::setup(&[]);

        let mut instruction = Instruction::new_with_bytes(Pubkey::new_unique(), &[], vec![]);
        ExtraAccountMetaList::add_to_instruction::<TestInstruction, _, _>(
            &mut instruction,
            |pubkey| mock_rpc.get_account_data(pubkey),
            &buffer,
        )
        .await
        .unwrap();

        let check_metas = metas
            .iter()
            .map(|e| AccountMeta::try_from(e).unwrap())
            .collect::<Vec<_>>();

        assert_eq!(instruction.accounts, check_metas,);
    }

    #[tokio::test]
    async fn init_with_infos() {
        let program_id = Pubkey::new_unique();

        let pubkey1 = Pubkey::new_unique();
        let mut lamports1 = 0;
        let mut data1 = [];
        let pubkey2 = Pubkey::new_unique();
        let mut lamports2 = 0;
        let mut data2 = [4, 4, 4, 6, 6, 6, 8, 8];
        let pubkey3 = Pubkey::new_unique();
        let mut lamports3 = 0;
        let mut data3 = [];
        let owner = Pubkey::new_unique();
        let account_infos = [
            AccountInfo::new(
                &pubkey1,
                false,
                true,
                &mut lamports1,
                &mut data1,
                &owner,
                false,
                Epoch::default(),
            ),
            AccountInfo::new(
                &pubkey2,
                true,
                false,
                &mut lamports2,
                &mut data2,
                &owner,
                false,
                Epoch::default(),
            ),
            AccountInfo::new(
                &pubkey3,
                false,
                false,
                &mut lamports3,
                &mut data3,
                &owner,
                false,
                Epoch::default(),
            ),
        ];

        let required_pda = ExtraAccountMeta::new_with_seeds(
            &[
                Seed::AccountKey { index: 0 },
                Seed::AccountData {
                    account_index: 1,
                    data_index: 2,
                    length: 4,
                },
            ],
            false,
            true,
        )
        .unwrap();

        // Convert to `ExtraAccountMeta`
        let required_extra_accounts = [
            ExtraAccountMeta::from(&account_infos[0]),
            ExtraAccountMeta::from(&account_infos[1]),
            ExtraAccountMeta::from(&account_infos[2]),
            required_pda,
        ];

        let account_size = ExtraAccountMetaList::size_of(required_extra_accounts.len()).unwrap();
        let mut buffer = vec![0; account_size];

        ExtraAccountMetaList::init::<TestInstruction>(&mut buffer, &required_extra_accounts)
            .unwrap();

        let mock_rpc = MockRpc::setup(&account_infos);

        let mut instruction = Instruction::new_with_bytes(program_id, &[], vec![]);
        ExtraAccountMetaList::add_to_instruction::<TestInstruction, _, _>(
            &mut instruction,
            |pubkey| mock_rpc.get_account_data(pubkey),
            &buffer,
        )
        .await
        .unwrap();

        let (check_required_pda, _) = Pubkey::find_program_address(
            &[
                account_infos[0].key.as_ref(),                      // Account key
                &account_infos[1].try_borrow_data().unwrap()[2..6], // Account data
            ],
            &program_id,
        );

        // Convert to `AccountMeta` to check instruction
        let check_metas = [
            account_info_to_meta(&account_infos[0]),
            account_info_to_meta(&account_infos[1]),
            account_info_to_meta(&account_infos[2]),
            AccountMeta::new(check_required_pda, false),
        ];

        assert_eq!(instruction.accounts, check_metas,);

        assert_eq!(
            instruction.accounts.get(3).unwrap().pubkey,
            check_required_pda
        );
    }

    #[tokio::test]
    async fn init_with_extra_account_metas() {
        let program_id = Pubkey::new_unique();

        let extra_meta3_literal_str = "seed_prefix";

        let ix_account1 = AccountMeta::new(Pubkey::new_unique(), false);
        let ix_account2 = AccountMeta::new(Pubkey::new_unique(), true);

        let extra_meta1 = AccountMeta::new(Pubkey::new_unique(), false);
        let extra_meta2 = AccountMeta::new(Pubkey::new_unique(), true);
        let extra_meta3 = ExtraAccountMeta::new_with_seeds(
            &[
                Seed::Literal {
                    bytes: extra_meta3_literal_str.as_bytes().to_vec(),
                },
                Seed::InstructionData {
                    index: 1,
                    length: 1, // u8
                },
                Seed::AccountKey { index: 0 },
                Seed::AccountKey { index: 2 },
            ],
            false,
            true,
        )
        .unwrap();

        let metas = [
            ExtraAccountMeta::from(&extra_meta1),
            ExtraAccountMeta::from(&extra_meta2),
            extra_meta3,
        ];

        let ix_data = vec![1, 2, 3, 4];
        let ix_accounts = vec![ix_account1.clone(), ix_account2.clone()];
        let mut instruction = Instruction::new_with_bytes(program_id, &ix_data, ix_accounts);

        let account_size = ExtraAccountMetaList::size_of(metas.len()).unwrap();
        let mut buffer = vec![0; account_size];

        ExtraAccountMetaList::init::<TestInstruction>(&mut buffer, &metas).unwrap();

        let mock_rpc = MockRpc::setup(&[]);

        ExtraAccountMetaList::add_to_instruction::<TestInstruction, _, _>(
            &mut instruction,
            |pubkey| mock_rpc.get_account_data(pubkey),
            &buffer,
        )
        .await
        .unwrap();

        let check_extra_meta3_u8_arg = ix_data[1];
        let check_extra_meta3_pubkey = Pubkey::find_program_address(
            &[
                extra_meta3_literal_str.as_bytes(),
                &[check_extra_meta3_u8_arg],
                ix_account1.pubkey.as_ref(),
                extra_meta1.pubkey.as_ref(),
            ],
            &program_id,
        )
        .0;
        let check_metas = [
            ix_account1,
            ix_account2,
            extra_meta1,
            extra_meta2,
            AccountMeta::new(check_extra_meta3_pubkey, false),
        ];

        assert_eq!(
            instruction.accounts.get(4).unwrap().pubkey,
            check_extra_meta3_pubkey,
        );
        assert_eq!(instruction.accounts, check_metas,);
    }

    #[tokio::test]
    async fn init_multiple() {
        let extra_meta5_literal_str = "seed_prefix";
        let extra_meta5_literal_u32 = 4u32;
        let other_meta2_literal_str = "other_seed_prefix";

        let extra_meta1 = AccountMeta::new(Pubkey::new_unique(), false);
        let extra_meta2 = AccountMeta::new(Pubkey::new_unique(), true);
        let extra_meta3 = AccountMeta::new_readonly(Pubkey::new_unique(), true);
        let extra_meta4 = AccountMeta::new_readonly(Pubkey::new_unique(), false);
        let extra_meta5 = ExtraAccountMeta::new_with_seeds(
            &[
                Seed::Literal {
                    bytes: extra_meta5_literal_str.as_bytes().to_vec(),
                },
                Seed::Literal {
                    bytes: extra_meta5_literal_u32.to_le_bytes().to_vec(),
                },
                Seed::InstructionData {
                    index: 5,
                    length: 1, // u8
                },
                Seed::AccountKey { index: 2 },
            ],
            false,
            true,
        )
        .unwrap();

        let other_meta1 = AccountMeta::new(Pubkey::new_unique(), false);
        let other_meta2 = ExtraAccountMeta::new_with_seeds(
            &[
                Seed::Literal {
                    bytes: other_meta2_literal_str.as_bytes().to_vec(),
                },
                Seed::InstructionData {
                    index: 1,
                    length: 4, // u32
                },
                Seed::AccountKey { index: 0 },
            ],
            false,
            true,
        )
        .unwrap();

        let metas = [
            ExtraAccountMeta::from(&extra_meta1),
            ExtraAccountMeta::from(&extra_meta2),
            ExtraAccountMeta::from(&extra_meta3),
            ExtraAccountMeta::from(&extra_meta4),
            extra_meta5,
        ];
        let other_metas = [ExtraAccountMeta::from(&other_meta1), other_meta2];

        let account_size = ExtraAccountMetaList::size_of(metas.len()).unwrap()
            + ExtraAccountMetaList::size_of(other_metas.len()).unwrap();
        let mut buffer = vec![0; account_size];

        ExtraAccountMetaList::init::<TestInstruction>(&mut buffer, &metas).unwrap();
        ExtraAccountMetaList::init::<TestOtherInstruction>(&mut buffer, &other_metas).unwrap();

        let mock_rpc = MockRpc::setup(&[]);

        let program_id = Pubkey::new_unique();
        let ix_data = vec![0, 0, 0, 0, 0, 7, 0, 0];
        let ix_accounts = vec![];
        let mut instruction = Instruction::new_with_bytes(program_id, &ix_data, ix_accounts);
        ExtraAccountMetaList::add_to_instruction::<TestInstruction, _, _>(
            &mut instruction,
            |pubkey| mock_rpc.get_account_data(pubkey),
            &buffer,
        )
        .await
        .unwrap();

        let check_extra_meta5_u8_arg = ix_data[5];
        let check_extra_meta5_pubkey = Pubkey::find_program_address(
            &[
                extra_meta5_literal_str.as_bytes(),
                extra_meta5_literal_u32.to_le_bytes().as_ref(),
                &[check_extra_meta5_u8_arg],
                extra_meta3.pubkey.as_ref(),
            ],
            &program_id,
        )
        .0;
        let check_metas = [
            extra_meta1,
            extra_meta2,
            extra_meta3,
            extra_meta4,
            AccountMeta::new(check_extra_meta5_pubkey, false),
        ];

        assert_eq!(
            instruction.accounts.get(4).unwrap().pubkey,
            check_extra_meta5_pubkey,
        );
        assert_eq!(instruction.accounts, check_metas,);

        let program_id = Pubkey::new_unique();
        let ix_account1 = AccountMeta::new(Pubkey::new_unique(), false);
        let ix_account2 = AccountMeta::new(Pubkey::new_unique(), true);
        let ix_accounts = vec![ix_account1.clone(), ix_account2.clone()];
        let ix_data = vec![0, 26, 0, 0, 0, 0, 0];
        let mut instruction = Instruction::new_with_bytes(program_id, &ix_data, ix_accounts);
        ExtraAccountMetaList::add_to_instruction::<TestOtherInstruction, _, _>(
            &mut instruction,
            |pubkey| mock_rpc.get_account_data(pubkey),
            &buffer,
        )
        .await
        .unwrap();

        let check_other_meta2_u32_arg = u32::from_le_bytes(ix_data[1..5].try_into().unwrap());
        let check_other_meta2_pubkey = Pubkey::find_program_address(
            &[
                other_meta2_literal_str.as_bytes(),
                check_other_meta2_u32_arg.to_le_bytes().as_ref(),
                ix_account1.pubkey.as_ref(),
            ],
            &program_id,
        )
        .0;
        let check_other_metas = [
            ix_account1,
            ix_account2,
            other_meta1,
            AccountMeta::new(check_other_meta2_pubkey, false),
        ];

        assert_eq!(
            instruction.accounts.get(3).unwrap().pubkey,
            check_other_meta2_pubkey,
        );
        assert_eq!(instruction.accounts, check_other_metas,);
    }

    #[tokio::test]
    async fn init_mixed() {
        let extra_meta5_literal_str = "seed_prefix";
        let extra_meta6_literal_u64 = 28u64;

        let pubkey1 = Pubkey::new_unique();
        let mut lamports1 = 0;
        let mut data1 = [];
        let pubkey2 = Pubkey::new_unique();
        let mut lamports2 = 0;
        let mut data2 = [];
        let pubkey3 = Pubkey::new_unique();
        let mut lamports3 = 0;
        let mut data3 = [];
        let owner = Pubkey::new_unique();
        let account_infos = [
            AccountInfo::new(
                &pubkey1,
                false,
                true,
                &mut lamports1,
                &mut data1,
                &owner,
                false,
                Epoch::default(),
            ),
            AccountInfo::new(
                &pubkey2,
                true,
                false,
                &mut lamports2,
                &mut data2,
                &owner,
                false,
                Epoch::default(),
            ),
            AccountInfo::new(
                &pubkey3,
                false,
                false,
                &mut lamports3,
                &mut data3,
                &owner,
                false,
                Epoch::default(),
            ),
        ];

        let extra_meta1 = AccountMeta::new(Pubkey::new_unique(), false);
        let extra_meta2 = AccountMeta::new(Pubkey::new_unique(), true);
        let extra_meta3 = AccountMeta::new_readonly(Pubkey::new_unique(), true);
        let extra_meta4 = AccountMeta::new_readonly(Pubkey::new_unique(), false);
        let extra_meta5 = ExtraAccountMeta::new_with_seeds(
            &[
                Seed::Literal {
                    bytes: extra_meta5_literal_str.as_bytes().to_vec(),
                },
                Seed::InstructionData {
                    index: 1,
                    length: 8, // [u8; 8]
                },
                Seed::InstructionData {
                    index: 9,
                    length: 32, // Pubkey
                },
                Seed::AccountKey { index: 2 },
            ],
            false,
            true,
        )
        .unwrap();
        let extra_meta6 = ExtraAccountMeta::new_with_seeds(
            &[
                Seed::Literal {
                    bytes: extra_meta6_literal_u64.to_le_bytes().to_vec(),
                },
                Seed::AccountKey { index: 1 },
                Seed::AccountKey { index: 4 },
            ],
            false,
            true,
        )
        .unwrap();

        let test_ix_required_extra_accounts = account_infos
            .iter()
            .map(ExtraAccountMeta::from)
            .collect::<Vec<_>>();
        let test_other_ix_required_extra_accounts = [
            ExtraAccountMeta::from(&extra_meta1),
            ExtraAccountMeta::from(&extra_meta2),
            ExtraAccountMeta::from(&extra_meta3),
            ExtraAccountMeta::from(&extra_meta4),
            extra_meta5,
            extra_meta6,
        ];

        let account_size = ExtraAccountMetaList::size_of(test_ix_required_extra_accounts.len())
            .unwrap()
            + ExtraAccountMetaList::size_of(test_other_ix_required_extra_accounts.len()).unwrap();
        let mut buffer = vec![0; account_size];

        ExtraAccountMetaList::init::<TestInstruction>(
            &mut buffer,
            &test_ix_required_extra_accounts,
        )
        .unwrap();
        ExtraAccountMetaList::init::<TestOtherInstruction>(
            &mut buffer,
            &test_other_ix_required_extra_accounts,
        )
        .unwrap();

        let mock_rpc = MockRpc::setup(&account_infos);

        let program_id = Pubkey::new_unique();
        let mut instruction = Instruction::new_with_bytes(program_id, &[], vec![]);
        ExtraAccountMetaList::add_to_instruction::<TestInstruction, _, _>(
            &mut instruction,
            |pubkey| mock_rpc.get_account_data(pubkey),
            &buffer,
        )
        .await
        .unwrap();

        let test_ix_check_metas = account_infos
            .iter()
            .map(account_info_to_meta)
            .collect::<Vec<_>>();
        assert_eq!(instruction.accounts, test_ix_check_metas,);

        let program_id = Pubkey::new_unique();
        let instruction_u8array_arg = [1, 2, 3, 4, 5, 6, 7, 8];
        let instruction_pubkey_arg = Pubkey::new_unique();
        let mut instruction_data = vec![0];
        instruction_data.extend_from_slice(&instruction_u8array_arg);
        instruction_data.extend_from_slice(instruction_pubkey_arg.as_ref());
        let mut instruction = Instruction::new_with_bytes(program_id, &instruction_data, vec![]);
        ExtraAccountMetaList::add_to_instruction::<TestOtherInstruction, _, _>(
            &mut instruction,
            |pubkey| mock_rpc.get_account_data(pubkey),
            &buffer,
        )
        .await
        .unwrap();

        let check_extra_meta5_pubkey = Pubkey::find_program_address(
            &[
                extra_meta5_literal_str.as_bytes(),
                &instruction_u8array_arg,
                instruction_pubkey_arg.as_ref(),
                extra_meta3.pubkey.as_ref(),
            ],
            &program_id,
        )
        .0;

        let check_extra_meta6_pubkey = Pubkey::find_program_address(
            &[
                extra_meta6_literal_u64.to_le_bytes().as_ref(),
                extra_meta2.pubkey.as_ref(),
                check_extra_meta5_pubkey.as_ref(), // The first PDA should be at index 4
            ],
            &program_id,
        )
        .0;

        let test_other_ix_check_metas = vec![
            extra_meta1,
            extra_meta2,
            extra_meta3,
            extra_meta4,
            AccountMeta::new(check_extra_meta5_pubkey, false),
            AccountMeta::new(check_extra_meta6_pubkey, false),
        ];

        assert_eq!(
            instruction.accounts.get(4).unwrap().pubkey,
            check_extra_meta5_pubkey,
        );
        assert_eq!(
            instruction.accounts.get(5).unwrap().pubkey,
            check_extra_meta6_pubkey,
        );
        assert_eq!(instruction.accounts, test_other_ix_check_metas,);
    }

    #[tokio::test]
    async fn cpi_instruction() {
        // Say we have a program that CPIs to another program.
        //
        // Say that _other_ program will need extra account infos.

        // This will be our program
        let program_id = Pubkey::new_unique();
        let owner = Pubkey::new_unique();

        // Some seeds used by the program for PDAs
        let required_pda1_literal_string = "required_pda1";
        let required_pda2_literal_u32 = 4u32;

        // Define instruction data
        //  - 0: u8
        //  - 1-8: [u8; 8]
        //  - 9-16: u64
        let instruction_u8array_arg = [1, 2, 3, 4, 5, 6, 7, 8];
        let instruction_u64_arg = 208u64;
        let mut instruction_data = vec![0];
        instruction_data.extend_from_slice(&instruction_u8array_arg);
        instruction_data.extend_from_slice(instruction_u64_arg.to_le_bytes().as_ref());

        // Define known instruction accounts
        let ix_accounts = vec![
            AccountMeta::new(Pubkey::new_unique(), false),
            AccountMeta::new(Pubkey::new_unique(), false),
        ];

        // Define extra account metas required by the program we will CPI to
        let extra_meta1 = AccountMeta::new(Pubkey::new_unique(), false);
        let extra_meta2 = AccountMeta::new(Pubkey::new_unique(), true);
        let extra_meta3 = AccountMeta::new_readonly(Pubkey::new_unique(), false);
        let required_accounts = [
            ExtraAccountMeta::from(&extra_meta1),
            ExtraAccountMeta::from(&extra_meta2),
            ExtraAccountMeta::from(&extra_meta3),
            ExtraAccountMeta::new_with_seeds(
                &[
                    Seed::Literal {
                        bytes: required_pda1_literal_string.as_bytes().to_vec(),
                    },
                    Seed::InstructionData {
                        index: 1,
                        length: 8, // [u8; 8]
                    },
                    Seed::AccountKey { index: 1 },
                ],
                false,
                true,
            )
            .unwrap(),
            ExtraAccountMeta::new_with_seeds(
                &[
                    Seed::Literal {
                        bytes: required_pda2_literal_u32.to_le_bytes().to_vec(),
                    },
                    Seed::InstructionData {
                        index: 9,
                        length: 8, // u64
                    },
                    Seed::AccountKey { index: 5 },
                ],
                false,
                true,
            )
            .unwrap(),
            ExtraAccountMeta::new_with_seeds(
                &[
                    Seed::InstructionData {
                        index: 0,
                        length: 1, // u8
                    },
                    Seed::AccountData {
                        account_index: 2,
                        data_index: 0,
                        length: 8,
                    },
                ],
                false,
                true,
            )
            .unwrap(),
            ExtraAccountMeta::new_with_seeds(
                &[
                    Seed::AccountData {
                        account_index: 5,
                        data_index: 4,
                        length: 4,
                    }, // This one is a PDA!
                ],
                false,
                true,
            )
            .unwrap(),
        ];

        // Now here we're going to build the list of account infos
        // We'll need to include:
        //  - The instruction account infos for the program to CPI to
        //  - The extra account infos for the program to CPI to
        //  - Some other arbitrary account infos our program may use

        // First we need to manually derive each PDA
        let check_required_pda1_pubkey = Pubkey::find_program_address(
            &[
                required_pda1_literal_string.as_bytes(),
                &instruction_u8array_arg,
                ix_accounts.get(1).unwrap().pubkey.as_ref(), // The second account
            ],
            &program_id,
        )
        .0;
        let check_required_pda2_pubkey = Pubkey::find_program_address(
            &[
                required_pda2_literal_u32.to_le_bytes().as_ref(),
                instruction_u64_arg.to_le_bytes().as_ref(),
                check_required_pda1_pubkey.as_ref(), // The first PDA should be at index 5
            ],
            &program_id,
        )
        .0;
        let check_required_pda3_pubkey = Pubkey::find_program_address(
            &[
                &[0],    // Instruction "discriminator" (u8)
                &[8; 8], // The first 8 bytes of the data for account at index 2 (extra account 1)
            ],
            &program_id,
        )
        .0;
        let check_required_pda4_pubkey = Pubkey::find_program_address(
            &[
                &[7; 4], /* 4 bytes starting at index 4 of the data for account at index 5 (extra
                         * pda 1) */
            ],
            &program_id,
        )
        .0;

        // The instruction account infos for the program to CPI to
        let pubkey_ix_1 = ix_accounts.get(0).unwrap().pubkey;
        let mut lamports_ix_1 = 0;
        let mut data_ix_1 = [];
        let pubkey_ix_2 = ix_accounts.get(1).unwrap().pubkey;
        let mut lamports_ix_2 = 0;
        let mut data_ix_2 = [];

        // The extra account infos for the program to CPI to
        let mut lamports1 = 0;
        let mut data1 = [8; 12];
        let mut lamports2 = 0;
        let mut data2 = [];
        let mut lamports3 = 0;
        let mut data3 = [];
        let mut lamports_pda1 = 0;
        let mut data_pda1 = [7; 12];
        let mut lamports_pda2 = 0;
        let mut data_pda2 = [];
        let mut lamports_pda3 = 0;
        let mut data_pda3 = [];
        let mut lamports_pda4 = 0;
        let mut data_pda4 = [];

        // Some other arbitrary account infos our program may use
        let pubkey_arb_1 = Pubkey::new_unique();
        let mut lamports_arb_1 = 0;
        let mut data_arb_1 = [];
        let pubkey_arb_2 = Pubkey::new_unique();
        let mut lamports_arb_2 = 0;
        let mut data_arb_2 = [];

        let all_account_infos = [
            AccountInfo::new(
                &pubkey_ix_1,
                ix_accounts.get(0).unwrap().is_signer,
                ix_accounts.get(0).unwrap().is_writable,
                &mut lamports_ix_1,
                &mut data_ix_1,
                &owner,
                false,
                Epoch::default(),
            ),
            AccountInfo::new(
                &pubkey_ix_2,
                ix_accounts.get(1).unwrap().is_signer,
                ix_accounts.get(1).unwrap().is_writable,
                &mut lamports_ix_2,
                &mut data_ix_2,
                &owner,
                false,
                Epoch::default(),
            ),
            AccountInfo::new(
                &extra_meta1.pubkey,
                required_accounts.get(0).unwrap().is_signer.into(),
                required_accounts.get(0).unwrap().is_writable.into(),
                &mut lamports1,
                &mut data1,
                &owner,
                false,
                Epoch::default(),
            ),
            AccountInfo::new(
                &extra_meta2.pubkey,
                required_accounts.get(1).unwrap().is_signer.into(),
                required_accounts.get(1).unwrap().is_writable.into(),
                &mut lamports2,
                &mut data2,
                &owner,
                false,
                Epoch::default(),
            ),
            AccountInfo::new(
                &extra_meta3.pubkey,
                required_accounts.get(2).unwrap().is_signer.into(),
                required_accounts.get(2).unwrap().is_writable.into(),
                &mut lamports3,
                &mut data3,
                &owner,
                false,
                Epoch::default(),
            ),
            AccountInfo::new(
                &check_required_pda1_pubkey,
                required_accounts.get(3).unwrap().is_signer.into(),
                required_accounts.get(3).unwrap().is_writable.into(),
                &mut lamports_pda1,
                &mut data_pda1,
                &owner,
                false,
                Epoch::default(),
            ),
            AccountInfo::new(
                &check_required_pda2_pubkey,
                required_accounts.get(4).unwrap().is_signer.into(),
                required_accounts.get(4).unwrap().is_writable.into(),
                &mut lamports_pda2,
                &mut data_pda2,
                &owner,
                false,
                Epoch::default(),
            ),
            AccountInfo::new(
                &check_required_pda3_pubkey,
                required_accounts.get(5).unwrap().is_signer.into(),
                required_accounts.get(5).unwrap().is_writable.into(),
                &mut lamports_pda3,
                &mut data_pda3,
                &owner,
                false,
                Epoch::default(),
            ),
            AccountInfo::new(
                &check_required_pda4_pubkey,
                required_accounts.get(6).unwrap().is_signer.into(),
                required_accounts.get(6).unwrap().is_writable.into(),
                &mut lamports_pda4,
                &mut data_pda4,
                &owner,
                false,
                Epoch::default(),
            ),
            AccountInfo::new(
                &pubkey_arb_1,
                false,
                true,
                &mut lamports_arb_1,
                &mut data_arb_1,
                &owner,
                false,
                Epoch::default(),
            ),
            AccountInfo::new(
                &pubkey_arb_2,
                false,
                true,
                &mut lamports_arb_2,
                &mut data_arb_2,
                &owner,
                false,
                Epoch::default(),
            ),
        ];

        // Let's use a mock RPC and set up a test instruction to check the CPI
        // instruction against later
        let rpc_account_infos = all_account_infos.clone();
        let mock_rpc = MockRpc::setup(&rpc_account_infos);

        let account_size = ExtraAccountMetaList::size_of(required_accounts.len()).unwrap();
        let mut buffer = vec![0; account_size];
        ExtraAccountMetaList::init::<TestInstruction>(&mut buffer, &required_accounts).unwrap();

        let mut instruction =
            Instruction::new_with_bytes(program_id, &instruction_data, ix_accounts.clone());
        ExtraAccountMetaList::add_to_instruction::<TestInstruction, _, _>(
            &mut instruction,
            |pubkey| mock_rpc.get_account_data(pubkey),
            &buffer,
        )
        .await
        .unwrap();

        // Perform the account resolution for the CPI instruction

        // Create the instruction itself
        let mut cpi_instruction =
            Instruction::new_with_bytes(program_id, &instruction_data, ix_accounts);

        // Start with the known account infos
        let mut cpi_account_infos =
            vec![all_account_infos[0].clone(), all_account_infos[1].clone()];

        // Mess up the ordering of the account infos to make it harder!
        let mut messed_account_infos = all_account_infos.clone();
        messed_account_infos.swap(0, 4);
        messed_account_infos.swap(1, 2);
        messed_account_infos.swap(3, 4);
        messed_account_infos.swap(5, 6);
        messed_account_infos.swap(8, 7);

        // Resolve the rest!
        ExtraAccountMetaList::add_to_cpi_instruction::<TestInstruction>(
            &mut cpi_instruction,
            &mut cpi_account_infos,
            &buffer,
            &messed_account_infos,
        )
        .unwrap();

        // Our CPI instruction should match the check instruction.
        assert_eq!(cpi_instruction, instruction);

        // CPI account infos should have the instruction account infos
        // and the extra required account infos from the validation account,
        // and they should be in the correct order.
        // Note: The two additional arbitrary account infos for the currently
        // executing program won't be present in the CPI instruction's account
        // infos, so we will omit them (hence the `..9`).
        let check_account_infos = &all_account_infos[..9];
        assert_eq!(cpi_account_infos.len(), check_account_infos.len());
        for (a, b) in std::iter::zip(cpi_account_infos, check_account_infos) {
            assert_eq!(a.key, b.key);
            assert_eq!(a.is_signer, b.is_signer);
            assert_eq!(a.is_writable, b.is_writable);
        }
    }

    async fn update_and_assert_metas(
        program_id: Pubkey,
        buffer: &mut Vec<u8>,
        updated_metas: &[ExtraAccountMeta],
        check_metas: &[AccountMeta],
    ) {
        // resize buffer if necessary
        let account_size = ExtraAccountMetaList::size_of(updated_metas.len()).unwrap();
        if account_size > buffer.len() {
            buffer.resize(account_size, 0);
        }

        // update
        ExtraAccountMetaList::update::<TestInstruction>(buffer, updated_metas).unwrap();

        // retrieve metas and assert
        let state = TlvStateBorrowed::unpack(buffer).unwrap();
        let unpacked_metas_pod =
            ExtraAccountMetaList::unpack_with_tlv_state::<TestInstruction>(&state).unwrap();
        let unpacked_metas = unpacked_metas_pod.data();
        assert_eq!(
            unpacked_metas, updated_metas,
            "The ExtraAccountMetas in the buffer should match the expected ones."
        );

        let mock_rpc = MockRpc::setup(&[]);

        let mut instruction = Instruction::new_with_bytes(program_id, &[], vec![]);
        ExtraAccountMetaList::add_to_instruction::<TestInstruction, _, _>(
            &mut instruction,
            |pubkey| mock_rpc.get_account_data(pubkey),
            buffer,
        )
        .await
        .unwrap();

        assert_eq!(instruction.accounts, check_metas,);
    }

    #[tokio::test]
    async fn update_extra_account_meta_list() {
        let program_id = Pubkey::new_unique();

        // Create list of initial metas
        let initial_metas = [
            ExtraAccountMeta::new_with_pubkey(&Pubkey::new_unique(), false, true).unwrap(),
            ExtraAccountMeta::new_with_pubkey(&Pubkey::new_unique(), true, false).unwrap(),
        ];

        // initialize
        let initial_account_size = ExtraAccountMetaList::size_of(initial_metas.len()).unwrap();
        let mut buffer = vec![0; initial_account_size];
        ExtraAccountMetaList::init::<TestInstruction>(&mut buffer, &initial_metas).unwrap();

        // Create updated metas list of the same size
        let updated_metas_1 = [
            ExtraAccountMeta::new_with_pubkey(&Pubkey::new_unique(), true, true).unwrap(),
            ExtraAccountMeta::new_with_pubkey(&Pubkey::new_unique(), false, false).unwrap(),
        ];
        let check_metas_1 = updated_metas_1
            .iter()
            .map(|e| AccountMeta::try_from(e).unwrap())
            .collect::<Vec<_>>();
        update_and_assert_metas(program_id, &mut buffer, &updated_metas_1, &check_metas_1).await;

        // Create updated and larger list of metas
        let updated_metas_2 = [
            ExtraAccountMeta::new_with_pubkey(&Pubkey::new_unique(), true, true).unwrap(),
            ExtraAccountMeta::new_with_pubkey(&Pubkey::new_unique(), false, false).unwrap(),
            ExtraAccountMeta::new_with_pubkey(&Pubkey::new_unique(), false, true).unwrap(),
        ];
        let check_metas_2 = updated_metas_2
            .iter()
            .map(|e| AccountMeta::try_from(e).unwrap())
            .collect::<Vec<_>>();
        update_and_assert_metas(program_id, &mut buffer, &updated_metas_2, &check_metas_2).await;

        // Create updated and smaller list of metas
        let updated_metas_3 =
            [ExtraAccountMeta::new_with_pubkey(&Pubkey::new_unique(), true, true).unwrap()];
        let check_metas_3 = updated_metas_3
            .iter()
            .map(|e| AccountMeta::try_from(e).unwrap())
            .collect::<Vec<_>>();
        update_and_assert_metas(program_id, &mut buffer, &updated_metas_3, &check_metas_3).await;

        // Create updated list of metas with a simple PDA
        let seed_pubkey = Pubkey::new_unique();
        let updated_metas_4 = [
            ExtraAccountMeta::new_with_pubkey(&seed_pubkey, true, true).unwrap(),
            ExtraAccountMeta::new_with_seeds(
                &[
                    Seed::Literal {
                        bytes: b"seed-prefix".to_vec(),
                    },
                    Seed::AccountKey { index: 0 },
                ],
                false,
                true,
            )
            .unwrap(),
        ];
        let simple_pda = Pubkey::find_program_address(
            &[
                b"seed-prefix",       // Literal prefix
                seed_pubkey.as_ref(), // Account at index 0
            ],
            &program_id,
        )
        .0;
        let check_metas_4 = [
            AccountMeta::new(seed_pubkey, true),
            AccountMeta::new(simple_pda, false),
        ];

        update_and_assert_metas(program_id, &mut buffer, &updated_metas_4, &check_metas_4).await;
    }

    #[test]
    fn check_account_infos_test() {
        let program_id = Pubkey::new_unique();
        let owner = Pubkey::new_unique();

        // Create a list of required account metas
        let pubkey1 = Pubkey::new_unique();
        let pubkey2 = Pubkey::new_unique();
        let required_accounts = [
            ExtraAccountMeta::new_with_pubkey(&pubkey1, false, true).unwrap(),
            ExtraAccountMeta::new_with_pubkey(&pubkey2, false, false).unwrap(),
            ExtraAccountMeta::new_with_seeds(
                &[
                    Seed::Literal {
                        bytes: b"lit_seed".to_vec(),
                    },
                    Seed::InstructionData {
                        index: 0,
                        length: 4,
                    },
                    Seed::AccountKey { index: 0 },
                ],
                false,
                true,
            )
            .unwrap(),
        ];

        // Create the validation data
        let account_size = ExtraAccountMetaList::size_of(required_accounts.len()).unwrap();
        let mut buffer = vec![0; account_size];
        ExtraAccountMetaList::init::<TestInstruction>(&mut buffer, &required_accounts).unwrap();

        // Create the instruction data
        let instruction_data = vec![0, 1, 2, 3, 4, 5, 6, 7];

        // Set up a list of the required accounts as account infos,
        // with two instruction accounts
        let pubkey_ix_1 = Pubkey::new_unique();
        let mut lamports_ix_1 = 0;
        let mut data_ix_1 = [];
        let pubkey_ix_2 = Pubkey::new_unique();
        let mut lamports_ix_2 = 0;
        let mut data_ix_2 = [];
        let mut lamports1 = 0;
        let mut data1 = [];
        let mut lamports2 = 0;
        let mut data2 = [];
        let mut lamports3 = 0;
        let mut data3 = [];
        let pda = Pubkey::find_program_address(
            &[b"lit_seed", &instruction_data[..4], pubkey_ix_1.as_ref()],
            &program_id,
        )
        .0;
        let account_infos = [
            // Instruction account 1
            AccountInfo::new(
                &pubkey_ix_1,
                false,
                true,
                &mut lamports_ix_1,
                &mut data_ix_1,
                &owner,
                false,
                Epoch::default(),
            ),
            // Instruction account 2
            AccountInfo::new(
                &pubkey_ix_2,
                false,
                true,
                &mut lamports_ix_2,
                &mut data_ix_2,
                &owner,
                false,
                Epoch::default(),
            ),
            // Required account 1
            AccountInfo::new(
                &pubkey1,
                false,
                true,
                &mut lamports1,
                &mut data1,
                &owner,
                false,
                Epoch::default(),
            ),
            // Required account 2
            AccountInfo::new(
                &pubkey2,
                false,
                false,
                &mut lamports2,
                &mut data2,
                &owner,
                false,
                Epoch::default(),
            ),
            // Required account 3 (PDA)
            AccountInfo::new(
                &pda,
                false,
                true,
                &mut lamports3,
                &mut data3,
                &owner,
                false,
                Epoch::default(),
            ),
        ];

        // Create another list of account infos to intentionally mess up
        let mut messed_account_infos = account_infos.clone().to_vec();
        messed_account_infos.swap(0, 2);
        messed_account_infos.swap(1, 4);
        messed_account_infos.swap(3, 2);

        // Account info check should fail for the messed list
        assert_eq!(
            ExtraAccountMetaList::check_account_infos::<TestInstruction>(
                &messed_account_infos,
                &instruction_data,
                &program_id,
                &buffer,
            )
            .unwrap_err(),
            AccountResolutionError::IncorrectAccount.into(),
        );

        // Account info check should pass for the correct list
        assert_eq!(
            ExtraAccountMetaList::check_account_infos::<TestInstruction>(
                &account_infos,
                &instruction_data,
                &program_id,
                &buffer,
            ),
            Ok(()),
        );
    }
}