Skip to content

Commit 261bbdd

Browse files
authored
p-token: Add format and lint scripts (#40)
* Fix formatting * Fix clippy * Add format and lint scripts * Tweak jobs order
1 parent da8d9bd commit 261bbdd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+687
-521
lines changed

.github/workflows/main.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,44 @@ jobs:
6161
- name: Lint
6262
run: pnpm programs:lint
6363

64+
format_and_lint_interface:
65+
name: Format & Lint Interface
66+
runs-on: ubuntu-latest
67+
steps:
68+
- name: Git Checkout
69+
uses: actions/checkout@v4
70+
71+
- name: Setup Environment
72+
uses: ./.github/actions/setup
73+
with:
74+
clippy: true
75+
rustfmt: true
76+
77+
- name: Format
78+
run: pnpm interface:format
79+
80+
- name: Lint
81+
run: pnpm interface:lint
82+
83+
format_and_lint_ptoken:
84+
name: Format & Lint p-token
85+
runs-on: ubuntu-latest
86+
steps:
87+
- name: Git Checkout
88+
uses: actions/checkout@v4
89+
90+
- name: Setup Environment
91+
uses: ./.github/actions/setup
92+
with:
93+
clippy: true
94+
rustfmt: true
95+
96+
- name: Format
97+
run: pnpm p-token:format
98+
99+
- name: Lint
100+
run: pnpm p-token:lint
101+
64102
audit_rust:
65103
name: Audit Rust
66104
runs-on: ubuntu-latest
@@ -154,6 +192,7 @@ jobs:
154192
build_ptoken:
155193
name: Build p-token
156194
runs-on: ubuntu-latest
195+
needs: [format_and_lint_interface, format_and_lint_ptoken]
157196
steps:
158197
- name: Git Checkout
159198
uses: actions/checkout@v4

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ edition = "2021"
1212
level = "warn"
1313
check-cfg = [
1414
'cfg(target_os, values("solana"))',
15-
'cfg(feature, values("frozen-abi", "no-entrypoint"))',
15+
'cfg(feature, values("custom-alloc", "custom-panic", "frozen-abi", "no-entrypoint"))',
1616
]
1717

1818
[workspace.metadata.cli]

interface/src/instruction.rs

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,13 @@ pub enum TokenInstruction {
6565
///
6666
/// 0. `[writable]` The multisignature account to initialize.
6767
/// 1. `[]` Rent sysvar.
68-
/// 2. `..+N` `[signer]` The signer accounts, must equal to N where `1 <= N <= 11`.
68+
/// 2. `..+N` `[signer]` The signer accounts, must equal to N where `1 <=
69+
/// N <= 11`.
6970
///
7071
/// Data expected by this instruction:
7172
///
72-
/// - `u8` The number of signers (M) required to validate this multisignature account.
73+
/// - `u8` The number of signers (M) required to validate this
74+
/// multisignature account.
7375
InitializeMultisig,
7476

7577
/// Transfers tokens from one account to another either directly or via a
@@ -268,7 +270,8 @@ pub enum TokenInstruction {
268270
/// Data expected by this instruction:
269271
///
270272
/// - `u64` The amount of tokens to transfer.
271-
/// - `u8` Expected number of base 10 digits to the right of the decimal place.
273+
/// - `u8` Expected number of base 10 digits to the right of the decimal
274+
/// place.
272275
TransferChecked,
273276

274277
/// Approves a delegate. A delegate is given the authority over tokens on
@@ -296,7 +299,8 @@ pub enum TokenInstruction {
296299
/// Data expected by this instruction:
297300
///
298301
/// - `u64` The amount of tokens the delegate is approved for.
299-
/// - `u8` Expected number of base 10 digits to the right of the decimal place.
302+
/// - `u8` Expected number of base 10 digits to the right of the decimal
303+
/// place.
300304
ApproveChecked,
301305

302306
/// Mints new tokens to an account. The native mint does not support
@@ -322,7 +326,8 @@ pub enum TokenInstruction {
322326
/// Data expected by this instruction:
323327
///
324328
/// - `u64` The amount of new tokens to mint.
325-
/// - `u8` Expected number of base 10 digits to the right of the decimal place.
329+
/// - `u8` Expected number of base 10 digits to the right of the decimal
330+
/// place.
326331
MintToChecked,
327332

328333
/// Burns tokens by removing them from an account. [`BurnChecked`] does not
@@ -349,13 +354,14 @@ pub enum TokenInstruction {
349354
/// Data expected by this instruction:
350355
///
351356
/// - `u64` The amount of tokens to burn.
352-
/// - `u8` Expected number of base 10 digits to the right of the decimal place.
357+
/// - `u8` Expected number of base 10 digits to the right of the decimal
358+
/// place.
353359
BurnChecked,
354360

355-
/// Like [`InitializeAccount`], but the owner pubkey is passed via instruction
356-
/// data rather than the accounts list. This variant may be preferable
357-
/// when using Cross Program Invocation from an instruction that does
358-
/// not need the owner's `AccountInfo` otherwise.
361+
/// Like [`InitializeAccount`], but the owner pubkey is passed via
362+
/// instruction data rather than the accounts list. This variant may be
363+
/// preferable when using Cross Program Invocation from an instruction
364+
/// that does not need the owner's `AccountInfo` otherwise.
359365
///
360366
/// Accounts expected by this instruction:
361367
///
@@ -399,11 +405,13 @@ pub enum TokenInstruction {
399405
/// Accounts expected by this instruction:
400406
///
401407
/// 0. `[writable]` The multisignature account to initialize.
402-
/// 1. `..+N` `[signer]` The signer accounts, must equal to N where `1 <= N <= 11`.
408+
/// 1. `..+N` `[signer]` The signer accounts, must equal to N where `1 <=
409+
/// N <= 11`.
403410
///
404411
/// Data expected by this instruction:
405412
///
406-
/// - `u8` The number of signers (M) required to validate this multisignature account.
413+
/// - `u8` The number of signers (M) required to validate this
414+
/// multisignature account.
407415
InitializeMultisig2,
408416

409417
/// Like [`InitializeMint`], but does not require the Rent sysvar to be
@@ -462,9 +470,9 @@ pub enum TokenInstruction {
462470
/// - `u64` The amount of tokens to reformat.
463471
AmountToUiAmount,
464472

465-
/// Convert a `UiAmount` of tokens to a little-endian `u64` raw Amount, using
466-
/// the given mint. In this version of the program, the mint can only
467-
/// specify the number of decimals.
473+
/// Convert a `UiAmount` of tokens to a little-endian `u64` raw Amount,
474+
/// using the given mint. In this version of the program, the mint can
475+
/// only specify the number of decimals.
468476
///
469477
/// Return data can be fetched using `sol_get_return_data` and deserializing
470478
/// the return data as a little-endian `u64`.
@@ -490,22 +498,26 @@ pub enum TokenInstruction {
490498
/// 3. `..+M` `[signer]` M signer accounts.
491499
WithdrawExcessLamports = 38,
492500

493-
/// Executes a batch of instructions. The instructions to be executed are specified
494-
/// in sequence on the instruction data. Each instruction provides:
501+
/// Executes a batch of instructions. The instructions to be executed are
502+
/// specified in sequence on the instruction data. Each instruction
503+
/// provides:
495504
/// - `u8`: number of accounts
496505
/// - `u8`: instruction data length (includes the discriminator)
497506
/// - `u8`: instruction discriminator
498507
/// - `[u8]`: instruction data
499508
///
500-
/// Accounts follow a similar pattern, where accounts for each instruction are
501-
/// specified in sequence. Therefore, the number of accounts expected by this
502-
/// instruction is variable, i.e., it depends on the instructions provided.
509+
/// Accounts follow a similar pattern, where accounts for each instruction
510+
/// are specified in sequence. Therefore, the number of accounts
511+
/// expected by this instruction is variable, i.e., it depends on the
512+
/// instructions provided.
503513
///
504-
/// Both the number of accounts and instruction data length are used to identify
505-
/// the slice of accounts and instruction data for each instruction.
514+
/// Both the number of accounts and instruction data length are used to
515+
/// identify the slice of accounts and instruction data for each
516+
/// instruction.
506517
///
507-
/// Note that it is not sound to have a `batch` instruction that contains other
508-
/// `batch` instruction; an error will be raised when this is detected.
518+
/// Note that it is not sound to have a `batch` instruction that contains
519+
/// other `batch` instruction; an error will be raised when this is
520+
/// detected.
509521
Batch = 255,
510522
// Any new variants also need to be added to program-2022 `TokenInstruction`, so that the
511523
// latter remains a superset of this instruction set. New variants also need to be added to
@@ -554,8 +566,10 @@ impl TryFrom<u8> for AuthorityType {
554566

555567
#[cfg(test)]
556568
mod tests {
557-
use super::{AuthorityType, TokenInstruction};
558-
use strum::IntoEnumIterator;
569+
use {
570+
super::{AuthorityType, TokenInstruction},
571+
strum::IntoEnumIterator,
572+
};
559573

560574
#[test]
561575
fn test_token_instruction_from_u8_exhaustive() {

interface/src/state/account.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use pinocchio::pubkey::Pubkey;
2-
3-
use super::{account_state::AccountState, COption, Initializable, Transmutable};
1+
use {
2+
super::{account_state::AccountState, COption, Initializable, Transmutable},
3+
pinocchio::pubkey::Pubkey,
4+
};
45

56
/// Incinerator address.
67
pub const INCINERATOR_ID: Pubkey =

interface/src/state/mint.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use pinocchio::pubkey::Pubkey;
2-
3-
use super::{COption, Initializable, Transmutable};
1+
use {
2+
super::{COption, Initializable, Transmutable},
3+
pinocchio::pubkey::Pubkey,
4+
};
45

56
/// Internal representation of a mint data.
67
#[repr(C)]

interface/src/state/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ pub type COption<T> = ([u8; 4], T);
1010

1111
/// Marker trait for types that can be cast from a raw pointer.
1212
///
13-
/// It is up to the type implementing this trait to guarantee that the cast is safe,
14-
/// i.e., the fields of the type are well aligned and there are no padding bytes.
13+
/// It is up to the type implementing this trait to guarantee that the cast is
14+
/// safe, i.e., the fields of the type are well aligned and there are no padding
15+
/// bytes.
1516
pub trait Transmutable {
1617
/// The length of the type.
1718
///

interface/src/state/multisig.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use pinocchio::pubkey::Pubkey;
2-
3-
use super::{Initializable, Transmutable};
1+
use {
2+
super::{Initializable, Transmutable},
3+
pinocchio::pubkey::Pubkey,
4+
};
45

56
/// Minimum number of multisignature signers (min N)
67
pub const MIN_SIGNERS: u8 = 1;
@@ -25,7 +26,8 @@ pub struct Multisig {
2526
}
2627

2728
impl Multisig {
28-
/// Utility function that checks index is between [`MIN_SIGNERS`] and [`MAX_SIGNERS`].
29+
/// Utility function that checks index is between [`MIN_SIGNERS`] and
30+
/// [`MAX_SIGNERS`].
2931
pub fn is_valid_signer_index(index: u8) -> bool {
3032
(MIN_SIGNERS..=MAX_SIGNERS).contains(&index)
3133
}

p-token/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ solana-sdk = "2.1"
2727
spl-token = { version="^4", features=["no-entrypoint"] }
2828
spl-token-2022 = { version="^7", features=["no-entrypoint"] }
2929
test-case = "3.3.1"
30+
31+
[lints]
32+
workspace = true

p-token/src/entrypoint.rs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use pinocchio::{
2-
account_info::AccountInfo, default_panic_handler, no_allocator, program_entrypoint,
3-
program_error::ProgramError, pubkey::Pubkey, ProgramResult,
1+
use {
2+
crate::processor::*,
3+
pinocchio::{
4+
account_info::AccountInfo, default_panic_handler, no_allocator, program_entrypoint,
5+
program_error::ProgramError, pubkey::Pubkey, ProgramResult,
6+
},
47
};
58

6-
use crate::processor::*;
7-
89
program_entrypoint!(process_instruction);
910
// Do not allocate memory.
1011
no_allocator!();
@@ -13,10 +14,11 @@ default_panic_handler!();
1314

1415
/// Process an instruction.
1516
///
16-
/// In the first stage, the entrypoint checks the discriminator of the instruction data
17-
/// to determine whether the instruction is a "batch" instruction or a "regular" instruction.
18-
/// This avoids nesting of "batch" instructions, since it is not sound to have a "batch"
19-
/// instruction inside another "batch" instruction.
17+
/// In the first stage, the entrypoint checks the discriminator of the
18+
/// instruction data to determine whether the instruction is a "batch"
19+
/// instruction or a "regular" instruction. This avoids nesting of "batch"
20+
/// instructions, since it is not sound to have a "batch" instruction inside
21+
/// another "batch" instruction.
2022
#[inline(always)]
2123
pub fn process_instruction(
2224
_program_id: &Pubkey,
@@ -40,20 +42,21 @@ pub fn process_instruction(
4042

4143
/// Process a "regular" instruction.
4244
///
43-
/// The processor of the token program is divided into two parts to reduce the overhead
44-
/// of having a large `match` statement. The first part of the processor handles the
45-
/// most common instructions, while the second part handles the remaining instructions.
45+
/// The processor of the token program is divided into two parts to reduce the
46+
/// overhead of having a large `match` statement. The first part of the
47+
/// processor handles the most common instructions, while the second part
48+
/// handles the remaining instructions.
4649
///
47-
/// The rationale is to reduce the overhead of making multiple comparisons for popular
48-
/// instructions.
50+
/// The rationale is to reduce the overhead of making multiple comparisons for
51+
/// popular instructions.
4952
///
5053
/// Instructions on the first part of the inner processor:
5154
///
52-
/// - `0`: `InitializeMint`
53-
/// - `1`: `InitializeAccount`
54-
/// - `3`: `Transfer`
55-
/// - `7`: `MintTo`
56-
/// - `9`: `CloseAccount`
55+
/// - `0`: `InitializeMint`
56+
/// - `1`: `InitializeAccount`
57+
/// - `3`: `Transfer`
58+
/// - `7`: `MintTo`
59+
/// - `9`: `CloseAccount`
5760
/// - `16`: `InitializeAccount2`
5861
/// - `18`: `InitializeAccount3`
5962
/// - `20`: `InitializeMint2`
@@ -129,9 +132,10 @@ pub(crate) fn inner_process_instruction(
129132

130133
/// Process a remaining "regular" instruction.
131134
///
132-
/// This function is called by the [`inner_process_instruction`] function if the discriminator
133-
/// does not match any of the common instructions. This function is used to reduce the
134-
/// overhead of having a large `match` statement in the [`inner_process_instruction`] function.
135+
/// This function is called by the [`inner_process_instruction`] function if the
136+
/// discriminator does not match any of the common instructions. This function
137+
/// is used to reduce the overhead of having a large `match` statement in the
138+
/// [`inner_process_instruction`] function.
135139
fn inner_process_remaining_instruction(
136140
accounts: &[AccountInfo],
137141
instruction_data: &[u8],

p-token/src/processor/amount_to_ui_amount.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
use core::str::from_utf8_unchecked;
2-
use pinocchio::{
3-
account_info::AccountInfo, program::set_return_data, program_error::ProgramError, ProgramResult,
1+
use {
2+
super::{check_account_owner, MAX_FORMATTED_DIGITS},
3+
core::str::from_utf8_unchecked,
4+
pinocchio::{
5+
account_info::AccountInfo, program::set_return_data, program_error::ProgramError,
6+
ProgramResult,
7+
},
8+
pinocchio_log::logger::{Argument, Logger},
9+
spl_token_interface::{
10+
error::TokenError,
11+
state::{load, mint::Mint},
12+
},
413
};
5-
use pinocchio_log::logger::{Argument, Logger};
6-
use spl_token_interface::{
7-
error::TokenError,
8-
state::{load, mint::Mint},
9-
};
10-
11-
use super::{check_account_owner, MAX_FORMATTED_DIGITS};
1214

1315
#[inline(always)]
1416
pub fn process_amount_to_ui_amount(

p-token/src/processor/approve.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use pinocchio::{account_info::AccountInfo, program_error::ProgramError, ProgramResult};
2-
3-
use super::shared;
1+
use {
2+
super::shared,
3+
pinocchio::{account_info::AccountInfo, program_error::ProgramError, ProgramResult},
4+
};
45

56
#[inline(always)]
67
pub fn process_approve(accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResult {

p-token/src/processor/approve_checked.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use pinocchio::{account_info::AccountInfo, program_error::ProgramError, ProgramResult};
2-
3-
use super::shared;
1+
use {
2+
super::shared,
3+
pinocchio::{account_info::AccountInfo, program_error::ProgramError, ProgramResult},
4+
};
45

56
#[inline(always)]
67
pub fn process_approve_checked(accounts: &[AccountInfo], instruction_data: &[u8]) -> ProgramResult {

0 commit comments

Comments
 (0)