Skip to content

Commit 32a3f7f

Browse files
committed
[wip]: Fix review comments
1 parent 76bd300 commit 32a3f7f

27 files changed

+419
-433
lines changed

interface/src/instruction.rs

Lines changed: 125 additions & 111 deletions
Large diffs are not rendered by default.

interface/src/state/account.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use pinocchio::pubkey::Pubkey;
22

3-
use super::{account_state::AccountState, COption, Initializable, RawType};
3+
use super::{account_state::AccountState, COption, Initializable, Transmutable};
44

55
/// Incinerator address.
6-
const INCINERATOR_ID: Pubkey =
6+
pub const INCINERATOR_ID: Pubkey =
77
pinocchio_pubkey::pubkey!("1nc1nerator11111111111111111111111111111111");
88

99
/// System program id.
@@ -140,7 +140,7 @@ impl Account {
140140
}
141141
}
142142

143-
impl RawType for Account {
143+
impl Transmutable for Account {
144144
const LEN: usize = core::mem::size_of::<Account>();
145145
}
146146

interface/src/state/mint.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use pinocchio::pubkey::Pubkey;
22

3-
use super::{COption, Initializable, RawType};
3+
use super::{COption, Initializable, Transmutable};
44

55
/// Internal representation of a mint data.
66
#[repr(C)]
@@ -38,8 +38,8 @@ impl Mint {
3838
}
3939

4040
#[inline(always)]
41-
pub fn set_initialized(&mut self, value: bool) {
42-
self.is_initialized = value as u8;
41+
pub fn set_initialized(&mut self) {
42+
self.is_initialized = 1;
4343
}
4444

4545
#[inline(always)]
@@ -83,7 +83,7 @@ impl Mint {
8383
}
8484
}
8585

86-
impl RawType for Mint {
86+
impl Transmutable for Mint {
8787
/// The length of the `Mint` account data.
8888
const LEN: usize = core::mem::size_of::<Mint>();
8989
}

interface/src/state/mod.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ pub mod multisig;
88
/// Type alias for fields represented as `COption`.
99
pub type COption<T> = ([u8; 4], T);
1010

11-
/// Marker trait for types that can cast from a raw pointer.
11+
/// Marker trait for types that can be cast from a raw pointer.
1212
///
1313
/// It is up to the type implementing this trait to guarantee that the cast is safe,
14-
/// i.e., that the fields of the type are well aligned and there are no padding bytes.
15-
pub trait RawType {
14+
/// i.e., the fields of the type are well aligned and there are no padding bytes.
15+
pub trait Transmutable {
1616
/// The length of the type.
1717
///
1818
/// This must be equal to the size of each individual field in the type.
@@ -31,7 +31,7 @@ pub trait Initializable {
3131
///
3232
/// The caller must ensure that `bytes` contains a valid representation of `T`.
3333
#[inline(always)]
34-
pub unsafe fn load<T: Initializable + RawType>(bytes: &[u8]) -> Result<&T, ProgramError> {
34+
pub unsafe fn load<T: Initializable + Transmutable>(bytes: &[u8]) -> Result<&T, ProgramError> {
3535
load_unchecked(bytes).and_then(|t: &T| {
3636
// checks if the data is initialized
3737
if t.is_initialized() {
@@ -50,7 +50,7 @@ pub unsafe fn load<T: Initializable + RawType>(bytes: &[u8]) -> Result<&T, Progr
5050
///
5151
/// The caller must ensure that `bytes` contains a valid representation of `T`.
5252
#[inline(always)]
53-
pub unsafe fn load_unchecked<T: RawType>(bytes: &[u8]) -> Result<&T, ProgramError> {
53+
pub unsafe fn load_unchecked<T: Transmutable>(bytes: &[u8]) -> Result<&T, ProgramError> {
5454
if bytes.len() != T::LEN {
5555
return Err(ProgramError::InvalidAccountData);
5656
}
@@ -63,7 +63,7 @@ pub unsafe fn load_unchecked<T: RawType>(bytes: &[u8]) -> Result<&T, ProgramErro
6363
///
6464
/// The caller must ensure that `bytes` contains a valid representation of `T`.
6565
#[inline(always)]
66-
pub unsafe fn load_mut<T: Initializable + RawType>(
66+
pub unsafe fn load_mut<T: Initializable + Transmutable>(
6767
bytes: &mut [u8],
6868
) -> Result<&mut T, ProgramError> {
6969
load_mut_unchecked(bytes).and_then(|t: &mut T| {
@@ -84,7 +84,9 @@ pub unsafe fn load_mut<T: Initializable + RawType>(
8484
///
8585
/// The caller must ensure that `bytes` contains a valid representation of `T`.
8686
#[inline(always)]
87-
pub unsafe fn load_mut_unchecked<T: RawType>(bytes: &mut [u8]) -> Result<&mut T, ProgramError> {
87+
pub unsafe fn load_mut_unchecked<T: Transmutable>(
88+
bytes: &mut [u8],
89+
) -> Result<&mut T, ProgramError> {
8890
if bytes.len() != T::LEN {
8991
return Err(ProgramError::InvalidAccountData);
9092
}

interface/src/state/multisig.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use pinocchio::pubkey::Pubkey;
22

3-
use super::{Initializable, RawType};
3+
use super::{Initializable, Transmutable};
44

55
/// Minimum number of multisignature signers (min N)
6-
pub const MIN_SIGNERS: usize = 1;
6+
pub const MIN_SIGNERS: u8 = 1;
77

88
/// Maximum number of multisignature signers (max N)
9-
pub const MAX_SIGNERS: usize = 11;
9+
pub const MAX_SIGNERS: u8 = 11;
1010

1111
/// Multisignature data.
1212
#[repr(C)]
@@ -21,12 +21,12 @@ pub struct Multisig {
2121
is_initialized: u8,
2222

2323
/// Signer public keys
24-
pub signers: [Pubkey; MAX_SIGNERS],
24+
pub signers: [Pubkey; MAX_SIGNERS as usize],
2525
}
2626

2727
impl Multisig {
2828
/// Utility function that checks index is between [`MIN_SIGNERS`] and [`MAX_SIGNERS`].
29-
pub fn is_valid_signer_index(index: usize) -> bool {
29+
pub fn is_valid_signer_index(index: u8) -> bool {
3030
(MIN_SIGNERS..=MAX_SIGNERS).contains(&index)
3131
}
3232

@@ -36,7 +36,7 @@ impl Multisig {
3636
}
3737
}
3838

39-
impl RawType for Multisig {
39+
impl Transmutable for Multisig {
4040
/// The length of the `Mint` account data.
4141
const LEN: usize = core::mem::size_of::<Multisig>();
4242
}

p-token/src/entrypoint.rs

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use pinocchio::{
22
account_info::AccountInfo, default_panic_handler, no_allocator, program_entrypoint,
33
program_error::ProgramError, pubkey::Pubkey, ProgramResult,
44
};
5+
use spl_token_interface::instruction::TokenInstruction;
56

67
use crate::processor::*;
78

@@ -36,52 +37,53 @@ pub fn process_instruction(
3637
let (discriminator, instruction_data) = instruction_data
3738
.split_first()
3839
.ok_or(ProgramError::InvalidInstructionData)?;
40+
let instruction = TokenInstruction::try_from(*discriminator)?;
3941

40-
match *discriminator {
42+
match instruction {
4143
// 0 - InitializeMint
42-
0 => {
44+
TokenInstruction::InitializeMint => {
4345
#[cfg(feature = "logging")]
4446
pinocchio::msg!("Instruction: InitializeMint");
4547

46-
process_initialize_mint(accounts, instruction_data, true)
48+
process_initialize_mint(accounts, instruction_data)
4749
}
4850

4951
// 3 - Transfer
50-
3 => {
52+
TokenInstruction::Transfer => {
5153
#[cfg(feature = "logging")]
5254
pinocchio::msg!("Instruction: Transfer");
5355

5456
process_transfer(accounts, instruction_data)
5557
}
5658
// 7 - MintTo
57-
7 => {
59+
TokenInstruction::MintTo => {
5860
#[cfg(feature = "logging")]
5961
pinocchio::msg!("Instruction: MintTo");
6062

6163
process_mint_to(accounts, instruction_data)
6264
}
6365
// 9 - CloseAccount
64-
9 => {
66+
TokenInstruction::CloseAccount => {
6567
#[cfg(feature = "logging")]
6668
pinocchio::msg!("Instruction: CloseAccount");
6769

6870
process_close_account(accounts)
6971
}
7072
// 18 - InitializeAccount3
71-
18 => {
73+
TokenInstruction::InitializeAccount3 => {
7274
#[cfg(feature = "logging")]
7375
pinocchio::msg!("Instruction: InitializeAccount3");
7476

7577
process_initialize_account3(accounts, instruction_data)
7678
}
7779
// 20 - InitializeMint2
78-
20 => {
80+
TokenInstruction::InitializeMint2 => {
7981
#[cfg(feature = "logging")]
8082
pinocchio::msg!("Instruction: InitializeMint2");
8183

8284
process_initialize_mint2(accounts, instruction_data)
8385
}
84-
_ => process_remaining_instruction(accounts, instruction_data, *discriminator),
86+
_ => process_remaining_instruction(accounts, instruction_data, instruction),
8587
}
8688
}
8789

@@ -93,137 +95,137 @@ pub fn process_instruction(
9395
fn process_remaining_instruction(
9496
accounts: &[AccountInfo],
9597
instruction_data: &[u8],
96-
discriminator: u8,
98+
instruction: TokenInstruction,
9799
) -> ProgramResult {
98-
match discriminator {
100+
match instruction {
99101
// 1 - InitializeAccount
100-
1 => {
102+
TokenInstruction::InitializeAccount => {
101103
#[cfg(feature = "logging")]
102104
pinocchio::msg!("Instruction: InitializeAccount");
103105

104106
process_initialize_account(accounts)
105107
}
106108
// 2 - InitializeMultisig
107-
2 => {
109+
TokenInstruction::InitializeMultisig => {
108110
#[cfg(feature = "logging")]
109111
pinocchio::msg!("Instruction: InitializeMultisig");
110112

111113
process_initialize_multisig(accounts, instruction_data)
112114
}
113115
// 4 - Approve
114-
4 => {
116+
TokenInstruction::Approve => {
115117
#[cfg(feature = "logging")]
116118
pinocchio::msg!("Instruction: Approve");
117119

118120
process_approve(accounts, instruction_data)
119121
}
120122
// 5 - Revoke
121-
5 => {
123+
TokenInstruction::Revoke => {
122124
#[cfg(feature = "logging")]
123125
pinocchio::msg!("Instruction: Revoke");
124126

125127
process_revoke(accounts, instruction_data)
126128
}
127129
// 6 - SetAuthority
128-
6 => {
130+
TokenInstruction::SetAuthority => {
129131
#[cfg(feature = "logging")]
130132
pinocchio::msg!("Instruction: SetAuthority");
131133

132134
process_set_authority(accounts, instruction_data)
133135
}
134136
// 8 - Burn
135-
8 => {
137+
TokenInstruction::Burn => {
136138
#[cfg(feature = "logging")]
137139
pinocchio::msg!("Instruction: Burn");
138140

139141
process_burn(accounts, instruction_data)
140142
}
141143
// 10 - FreezeAccount
142-
10 => {
144+
TokenInstruction::FreezeAccount => {
143145
#[cfg(feature = "logging")]
144146
pinocchio::msg!("Instruction: FreezeAccount");
145147

146148
process_freeze_account(accounts)
147149
}
148150
// 11 - ThawAccount
149-
11 => {
151+
TokenInstruction::ThawAccount => {
150152
#[cfg(feature = "logging")]
151153
pinocchio::msg!("Instruction: ThawAccount");
152154

153155
process_thaw_account(accounts)
154156
}
155157
// 12 - TransferChecked
156-
12 => {
158+
TokenInstruction::TransferChecked => {
157159
#[cfg(feature = "logging")]
158160
pinocchio::msg!("Instruction: TransferChecked");
159161

160162
process_transfer_checked(accounts, instruction_data)
161163
}
162164
// 13 - ApproveChecked
163-
13 => {
165+
TokenInstruction::ApproveChecked => {
164166
#[cfg(feature = "logging")]
165167
pinocchio::msg!("Instruction: ApproveChecked");
166168

167169
process_approve_checked(accounts, instruction_data)
168170
}
169171
// 14 - MintToChecked
170-
14 => {
172+
TokenInstruction::MintToChecked => {
171173
#[cfg(feature = "logging")]
172174
pinocchio::msg!("Instruction: MintToChecked");
173175

174176
process_mint_to_checked(accounts, instruction_data)
175177
}
176178
// 15 - BurnChecked
177-
15 => {
179+
TokenInstruction::BurnChecked => {
178180
#[cfg(feature = "logging")]
179181
pinocchio::msg!("Instruction: BurnChecked");
180182

181183
process_burn_checked(accounts, instruction_data)
182184
}
183185
// 16 - InitializeAccount2
184-
16 => {
186+
TokenInstruction::InitializeAccount2 => {
185187
#[cfg(feature = "logging")]
186188
pinocchio::msg!("Instruction: InitializeAccount2");
187189

188190
process_initialize_account2(accounts, instruction_data)
189191
}
190192
// 17 - SyncNative
191-
17 => {
193+
TokenInstruction::SyncNative => {
192194
#[cfg(feature = "logging")]
193195
pinocchio::msg!("Instruction: SyncNative");
194196

195197
process_sync_native(accounts)
196198
}
197199
// 19 - InitializeMultisig2
198-
19 => {
200+
TokenInstruction::InitializeMultisig2 => {
199201
#[cfg(feature = "logging")]
200202
pinocchio::msg!("Instruction: InitializeMultisig2");
201203

202204
process_initialize_multisig2(accounts, instruction_data)
203205
}
204206
// 21 - GetAccountDataSize
205-
21 => {
207+
TokenInstruction::GetAccountDataSize => {
206208
#[cfg(feature = "logging")]
207209
pinocchio::msg!("Instruction: GetAccountDataSize");
208210

209211
process_get_account_data_size(accounts)
210212
}
211213
// 22 - InitializeImmutableOwner
212-
22 => {
214+
TokenInstruction::InitializeImmutableOwner => {
213215
#[cfg(feature = "logging")]
214216
pinocchio::msg!("Instruction: InitializeImmutableOwner");
215217

216218
process_initialize_immutable_owner(accounts)
217219
}
218220
// 23 - AmountToUiAmount
219-
23 => {
221+
TokenInstruction::AmountToUiAmount => {
220222
#[cfg(feature = "logging")]
221223
pinocchio::msg!("Instruction: AmountToUiAmount");
222224

223225
process_amount_to_ui_amount(accounts, instruction_data)
224226
}
225227
// 24 - UiAmountToAmount
226-
24 => {
228+
TokenInstruction::UiAmountToAmount => {
227229
#[cfg(feature = "logging")]
228230
pinocchio::msg!("Instruction: UiAmountToAmount");
229231

p-token/src/processor/approve_checked.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ use super::shared;
44

55
#[inline(always)]
66
pub fn process_approve_checked(accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResult {
7-
let (amount, decimals) = instruction_data.split_at(core::mem::size_of::<u64>());
8-
let amount = u64::from_le_bytes(
9-
amount
10-
.try_into()
11-
.map_err(|_error| ProgramError::InvalidInstructionData)?,
12-
);
7+
// expected u64 (8) + u8 (1)
8+
let (amount, decimals) = if instruction_data.len() == 9 {
9+
let (amount, decimals) = instruction_data.split_at(core::mem::size_of::<u64>());
10+
(
11+
u64::from_le_bytes(amount.try_into().unwrap()),
12+
decimals.first().copied(),
13+
)
14+
} else {
15+
return Err(ProgramError::InvalidInstructionData);
16+
};
1317

14-
shared::approve::process_approve(
15-
accounts,
16-
amount,
17-
Some(*decimals.first().ok_or(ProgramError::InvalidAccountData)?),
18-
)
18+
shared::approve::process_approve(accounts, amount, decimals)
1919
}

0 commit comments

Comments
 (0)