Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions program/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ use pinocchio::program_error::ProgramError;
pub enum ProgramMetadataError {
/// 0 - The program account is not executable.
NotExecutableAccount,

/// 1 - The program state is invalid.
InvalidProgramState,

/// 2 - The program data account is invalid.
InvalidProgramDataAccount,

/// 3 - The metadata account is immutable.
ImmutableMetadataAccount,

/// 4 - The account data length is invalid.
InvalidDataLength,
}

impl From<ProgramMetadataError> for ProgramError {
Expand Down
17 changes: 8 additions & 9 deletions program/src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use pinocchio::{
pubkey::{Pubkey, PUBKEY_BYTES},
};

use crate::state::{header::Header, Account, AccountDiscriminator};
use crate::{
error::ProgramMetadataError,
state::{header::Header, Account, AccountDiscriminator},
};

pub mod allocate;
pub mod close;
Expand Down Expand Up @@ -71,16 +74,14 @@ fn is_program_authority(
.map_err(|_| ProgramError::InvalidAccountData)?
}
_ => {
// TODO: use custom error (invalid program state)
return Err(ProgramError::InvalidAccountData);
return Err(ProgramMetadataError::InvalidProgramState.into());
}
}
};

// Program <-> Program Data check.
if expected_program_data != *program_data.key() {
// TODO: use custom error (invalid program data account)
return Err(ProgramError::InvalidAccountData);
return Err(ProgramMetadataError::InvalidProgramDataAccount.into());
}

// Program Data checks.
Expand All @@ -101,8 +102,7 @@ fn is_program_authority(
}
}
_ => {
// TODO: use custom error (invalid program state)
return Err(ProgramError::InvalidAccountData);
return Err(ProgramMetadataError::InvalidProgramState.into());
}
}
};
Expand All @@ -124,8 +124,7 @@ fn validate_metadata(metadata: &AccountInfo) -> Result<&Header, ProgramError> {
return Err(ProgramError::UninitializedAccount);
}
if !header.mutable() {
// TODO: use custom error (immutable metadata account)
return Err(ProgramError::InvalidAccountData);
return Err(ProgramMetadataError::ImmutableMetadataAccount.into());
}
Ok(header)
}
Expand Down
5 changes: 2 additions & 3 deletions program/src/processor/set_immutable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use pinocchio::{account_info::AccountInfo, program_error::ProgramError, ProgramResult};

use crate::state::header::Header;
use crate::{error::ProgramMetadataError, state::header::Header};

use super::{validate_authority, validate_metadata};

Expand Down Expand Up @@ -40,8 +40,7 @@ pub fn set_immutable(accounts: &[AccountInfo]) -> ProgramResult {
if header.mutable() {
header.mutable = 0;
} else {
// TODO: use custom error (metadata already immutable)
return Err(ProgramError::InvalidAccountData);
return Err(ProgramMetadataError::ImmutableMetadataAccount.into());
}

Ok(())
Expand Down
7 changes: 3 additions & 4 deletions program/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use pinocchio::{
use data::{Data, ExternalData};
use header::Header;

use crate::error::ProgramMetadataError;

/// The length of the seed used to derive the metadata account address.
pub const SEED_LEN: usize = 16;

Expand Down Expand Up @@ -176,10 +178,7 @@ impl DataSource {
match (self, length) {
(DataSource::Direct | DataSource::Url, l) if l > 0 => Ok(()),
(DataSource::External, ExternalData::LEN) => Ok(()),
_ => {
// TODO: use custom error (invalid data length)
Err(ProgramError::InvalidAccountData)
}
_ => Err(ProgramMetadataError::InvalidDataLength.into()),
}
}
}
Expand Down
Loading