Skip to content

Commit 09c4ad4

Browse files
committed
Add comments from Jon
#### Problem P-token is getting ready to be used everywhere, but it hasn't been reviewed! #### Summary of changes Nothing show-stopping, mostly nits and cleanup ideas. Great work!
1 parent 08aa3cc commit 09c4ad4

25 files changed

+129
-18
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[workspace]
22
resolver = "2"
33
members = ["program"]
4+
exclude = ["p-token"]
45

56
[workspace.lints.rust.unexpected_cfgs]
67
level = "warn"

interface/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
[package]
22
name = "token-interface"
33
version = "0.0.0"
4-
edition = { workspace = true }
4+
edition = "2021"
55
readme = "./README.md"
6-
license = { workspace = true }
7-
repository = { workspace = true }
6+
license = "Apache-2.0"
7+
#repository = { workspace = true }
88
publish = false
99

1010
[lib]
1111
crate-type = ["rlib"]
1212

1313
[dependencies]
14-
pinocchio = { workspace = true }
15-
pinocchio-pubkey = { workspace = true }
14+
pinocchio = { path = "../../../pinocchio/sdk/pinocchio" }
15+
pinocchio-pubkey = { path = "../../../pinocchio/sdk/pubkey" }

interface/src/instruction.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ pub enum TokenInstruction<'a> {
360360
///
361361
/// 0. `[writable]` The account to initialize.
362362
/// 1. `[]` The mint this account will be associated with.
363+
/// JC nit: numbering here
363364
/// 3. `[]` Rent sysvar
364365
InitializeAccount2 {
365366
/// The new account's owner/multisignature.

interface/src/state/account.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ impl Account {
5656
}
5757

5858
#[inline(always)]
59+
// JC: I can't remember if we discussed this, but is this the current token
60+
// program behavior? ie it only sets whether the option is set, without
61+
// setting the value to all zeroes?
62+
// To be clear, this behavior is totally fine to me, but if there's a
63+
// difference we need to point it out. People may be relying on the previous
64+
// behavior for things like `get_program_accounts`
5965
pub fn clear_delegate(&mut self) {
6066
self.delegate.0[0] = 0;
6167
}
@@ -110,6 +116,9 @@ impl Account {
110116
}
111117

112118
#[inline(always)]
119+
// JC: This is mentioned elsewhere with the delegate, but is this the current
120+
// behavior? Or does it zero out the whole thing if the close authority is
121+
// cleared?
113122
pub fn clear_close_authority(&mut self) {
114123
self.close_authority.0[0] = 0;
115124
}

interface/src/state/mint.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,17 @@ impl Mint {
3838
}
3939

4040
#[inline(always)]
41+
// JC nit: since there's never a case of `uninitializing` a mint, maybe this
42+
// could not take the value and just set `is_initialized` to `1`
4143
pub fn set_initialized(&mut self, value: bool) {
4244
self.is_initialized = value as u8;
4345
}
4446

4547
#[inline(always)]
48+
// JC: this one worries me in particular, for people who might be doing
49+
// `get_program_accounts` with just their mint authority -- if we don't
50+
// clear the whole 36 bytes, they might get false positives. But either way,
51+
// this might be the behavior now, in which case, no problem!
4652
pub fn clear_mint_authority(&mut self) {
4753
self.mint_authority.0[0] = 0;
4854
}
@@ -63,6 +69,8 @@ impl Mint {
6369
}
6470

6571
#[inline(always)]
72+
// JC: same as with the other COption<Pubkey> fields, check that this is the
73+
// current behavior
6674
pub fn clear_freeze_authority(&mut self) {
6775
self.freeze_authority.0[0] = 0;
6876
}

interface/src/state/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ pub type COption<T> = ([u8; 4], T);
1212
///
1313
/// It is up to the type implementing this trait to guarantee that the cast is safe,
1414
/// i.e., that the fields of the type are well aligned and there are no padding bytes.
15+
/// JC nit: typically, these are called `Pod`s, but that term is a little bit
16+
/// loaded with bytemuck unfortunately! We can probably come up with another
17+
/// name, maybe `PackedType` since the bits are packed, or `Transmutable` to
18+
/// indicate that it's safe to transmute.
1519
pub trait RawType {
1620
/// The length of the type.
1721
///

p-token/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[package]
22
name = "token-program"
33
version = "0.0.0"
4-
edition = { workspace = true }
4+
edition = "2021"
55
readme = "./README.md"
6-
license = { workspace = true }
7-
repository = { workspace = true }
6+
license = "Apache-2.0"
7+
#repository = { workspace = true }
88
publish = false
99

1010
[package.metadata.solana]
@@ -18,8 +18,8 @@ logging = []
1818
test-sbf = []
1919

2020
[dependencies]
21-
pinocchio = { workspace = true }
22-
pinocchio-log = { workspace = true }
21+
pinocchio = { path = "../../../pinocchio/sdk/pinocchio" }
22+
pinocchio-log = { path = "../../../pinocchio/sdk/log/crate" }
2323
token-interface = { version = "^0", path = "../interface" }
2424

2525
[dev-dependencies]

p-token/src/entrypoint.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ pub fn process_instruction(
3737
.split_first()
3838
.ok_or(ProgramError::InvalidInstructionData)?;
3939

40+
// JC: for readability, is it possible to have a u8 enum with the instruction
41+
// type? I don't think it should add any compute usage
4042
match *discriminator {
4143
// 0 - InitializeMint
4244
0 => {

p-token/src/processor/approve_checked.rs

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

55
#[inline(always)]
66
pub fn process_approve_checked(accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResult {
7+
// JC: this will panic if the instruction data is too short -- let's avoid
8+
// panicking
79
let (amount, decimals) = instruction_data.split_at(core::mem::size_of::<u64>());
810
let amount = u64::from_le_bytes(
11+
// JC: if the length is checked earlier, then we should be able to unwrap this
912
amount
1013
.try_into()
1114
.map_err(|_error| ProgramError::InvalidInstructionData)?,

p-token/src/processor/burn_checked.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ pub fn process_burn_checked(accounts: &[AccountInfo], instruction_data: &[u8]) -
99
let (amount, decimals) = instruction_data.split_at(core::mem::size_of::<u64>());
1010
(
1111
u64::from_le_bytes(
12+
// JC: this should be unwrappabable since the length was just checked
13+
// and the split amount is valid -- if it gives CU gains, go for it!
1214
amount
1315
.try_into()
1416
.map_err(|_error| ProgramError::InvalidInstructionData)?,

0 commit comments

Comments
 (0)