|
1 | | -use core::slice::from_raw_parts; |
| 1 | +use core::{mem::MaybeUninit, slice}; |
2 | 2 |
|
3 | 3 | use pinocchio::{ |
4 | 4 | account_info::AccountInfo, |
5 | | - instruction::{AccountMeta, Instruction, Signer}, |
6 | | - program::invoke_signed, |
| 5 | + cpi::invoke_with_bounds, |
| 6 | + instruction::{AccountMeta, Instruction}, |
7 | 7 | program_error::ProgramError, |
8 | 8 | ProgramResult, |
9 | 9 | }; |
10 | 10 |
|
11 | | -extern crate alloc; |
12 | | - |
13 | | -use alloc::vec::Vec; |
14 | | - |
15 | | -use crate::{write_bytes, UNINIT_BYTE}; |
| 11 | +/// Maximum number of multisignature signers. |
| 12 | +pub const MAX_MULTISIG_SIGNERS: usize = 11; |
16 | 13 |
|
17 | 14 | /// Initialize a new Multisig. |
18 | 15 | /// |
19 | 16 | /// ### Accounts: |
20 | 17 | /// 0. `[writable]` The multisig account to initialize. |
21 | 18 | /// 1. `[]` Rent sysvar |
22 | | -/// 2. ..`2+N`. `[]` The signer accounts, must equal to N where `1 <= N <= |
23 | | -/// 11`. |
24 | | -pub struct InitializeMultisig<'a> { |
| 19 | +/// 2. ..`2+N`. `[]` The N signer accounts, where N is between 1 and 11. |
| 20 | +pub struct InitializeMultisig<'a, 'b> |
| 21 | +where |
| 22 | + 'a: 'b, |
| 23 | +{ |
25 | 24 | /// Multisig Account. |
26 | 25 | pub multisig: &'a AccountInfo, |
27 | 26 | /// Rent sysvar Account. |
28 | 27 | pub rent_sysvar: &'a AccountInfo, |
29 | 28 | /// Signer Accounts |
30 | | - pub multisig_signers: Vec<&'a AccountInfo>, |
| 29 | + pub multisig_signers: &'b [&'a AccountInfo], |
31 | 30 | /// The number of signers (M) required to validate this multisignature |
32 | 31 | /// account. |
33 | 32 | pub m: u8, |
34 | 33 | } |
35 | 34 |
|
36 | | -impl InitializeMultisig<'_> { |
| 35 | +impl InitializeMultisig<'_, '_> { |
37 | 36 | #[inline(always)] |
38 | | - pub fn invoke<const ACCOUNTS: usize>(&self) -> ProgramResult { |
39 | | - self.invoke_signed::<ACCOUNTS>(&[]) |
40 | | - } |
41 | | - |
42 | | - pub fn invoke_signed<const ACCOUNTS: usize>(&self, signers: &[Signer]) -> ProgramResult { |
43 | | - if ACCOUNTS != self.multisig_signers.len() + 2 { |
| 37 | + pub fn invoke(&self) -> ProgramResult { |
| 38 | + let &Self { |
| 39 | + multisig, |
| 40 | + rent_sysvar, |
| 41 | + multisig_signers, |
| 42 | + m, |
| 43 | + } = self; |
| 44 | + |
| 45 | + if multisig_signers.len() > MAX_MULTISIG_SIGNERS { |
44 | 46 | return Err(ProgramError::InvalidArgument); |
45 | 47 | } |
46 | 48 |
|
| 49 | + let num_accounts = 2 + multisig_signers.len(); |
| 50 | + |
47 | 51 | // Account metadata |
48 | | - let mut account_metas = Vec::with_capacity(1 + self.multisig_signers.len()); |
49 | | - account_metas.push(AccountMeta::writable(self.multisig.key())); |
| 52 | + const UNINIT_META: MaybeUninit<AccountMeta> = MaybeUninit::<AccountMeta>::uninit(); |
| 53 | + let mut acc_metas = [UNINIT_META; 2 + MAX_MULTISIG_SIGNERS]; |
| 54 | + |
| 55 | + unsafe { |
| 56 | + // SAFETY: |
| 57 | + // - `account_metas` is sized to 2 + MAX_MULTISIG_SIGNERS |
| 58 | + // - Index 0 and 1 are always present |
| 59 | + acc_metas |
| 60 | + .get_unchecked_mut(0) |
| 61 | + .write(AccountMeta::writable(multisig.key())); |
| 62 | + acc_metas |
| 63 | + .get_unchecked_mut(1) |
| 64 | + .write(AccountMeta::readonly(rent_sysvar.key())); |
| 65 | + } |
50 | 66 |
|
51 | | - account_metas.extend( |
52 | | - self.multisig_signers |
53 | | - .iter() |
54 | | - .map(|a| AccountMeta::readonly(a.key())), |
55 | | - ); |
| 67 | + for (account_meta, signer) in acc_metas[2..].iter_mut().zip(multisig_signers.iter()) { |
| 68 | + account_meta.write(AccountMeta::readonly(signer.key())); |
| 69 | + } |
56 | 70 |
|
57 | 71 | // Instruction data layout: |
58 | 72 | // - [0]: instruction discriminator (1 byte, u8) |
59 | 73 | // - [1]: m (1 byte, u8) |
60 | | - let mut instruction_data = [UNINIT_BYTE; 2]; |
61 | | - |
62 | | - // Set discriminator as u8 at offset [0] |
63 | | - write_bytes(&mut instruction_data, &[2]); |
64 | | - // Set number of signers (m) at offset 1 |
65 | | - write_bytes(&mut instruction_data[1..2], &[self.m]); |
| 74 | + let data = &[2, m]; |
66 | 75 |
|
67 | 76 | let instruction = Instruction { |
68 | 77 | program_id: &crate::ID, |
69 | | - accounts: account_metas.as_slice(), |
70 | | - data: unsafe { from_raw_parts(instruction_data.as_ptr() as _, 2) }, |
| 78 | + accounts: unsafe { slice::from_raw_parts(acc_metas.as_ptr() as _, num_accounts) }, |
| 79 | + data, |
71 | 80 | }; |
72 | 81 |
|
73 | | - let mut account_infos = Vec::with_capacity(2 + self.multisig_signers.len()); |
74 | | - |
75 | | - account_infos.push(self.multisig); |
| 82 | + // Account info array |
| 83 | + const UNINIT_INFO: MaybeUninit<&AccountInfo> = MaybeUninit::uninit(); |
| 84 | + let mut acc_infos = [UNINIT_INFO; 2 + MAX_MULTISIG_SIGNERS]; |
76 | 85 |
|
77 | | - account_infos.extend_from_slice(self.multisig_signers.as_slice()); |
| 86 | + unsafe { |
| 87 | + // SAFETY: |
| 88 | + // - `account_infos` is sized to 2 + MAX_MULTISIG_SIGNERS |
| 89 | + // - Index 0 and 1 are always present |
| 90 | + acc_infos.get_unchecked_mut(0).write(multisig); |
| 91 | + acc_infos.get_unchecked_mut(1).write(rent_sysvar); |
| 92 | + } |
78 | 93 |
|
79 | | - let account_infos: [&AccountInfo; ACCOUNTS] = account_infos |
80 | | - .try_into() |
81 | | - .map_err(|_| ProgramError::InvalidArgument)?; |
| 94 | + // Fill signer accounts |
| 95 | + for (account_info, signer) in acc_infos[2..].iter_mut().zip(multisig_signers.iter()) { |
| 96 | + account_info.write(signer); |
| 97 | + } |
82 | 98 |
|
83 | | - invoke_signed(&instruction, &account_infos, signers) |
| 99 | + invoke_with_bounds::<{ 2 + MAX_MULTISIG_SIGNERS }>(&instruction, unsafe { |
| 100 | + slice::from_raw_parts(acc_infos.as_ptr() as _, num_accounts) |
| 101 | + }) |
84 | 102 | } |
85 | 103 | } |
0 commit comments