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