Skip to content

Commit

Permalink
Extract precompile-error crate (solana-labs#2300)
Browse files Browse the repository at this point in the history
* extract precompile-error crate

* update PrecompileError usage

* remove thiserror from precompile-error crate

* fmt

* remove num-derive

* fix imports after rebase

* sort deps

* sort deps

* fmt
  • Loading branch information
kevinheavey authored Oct 16, 2024
1 parent a458839 commit a8aef04
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 29 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ members = [
"sdk/native-token",
"sdk/package-metadata",
"sdk/package-metadata-macro",
"sdk/precompile-error",
"sdk/program",
"sdk/program-entrypoint",
"sdk/program-error",
Expand Down Expand Up @@ -444,6 +445,7 @@ solana-package-metadata-macro = { path = "sdk/package-metadata-macro", version =
solana-perf = { path = "perf", version = "=2.1.0" }
solana-poh = { path = "poh", version = "=2.1.0" }
solana-poseidon = { path = "poseidon", version = "=2.1.0" }
solana-precompile-error = { path = "sdk/precompile-error", version = "=2.1.0" }
solana-program = { path = "sdk/program", version = "=2.1.0", default-features = false }
solana-program-error = { path = "sdk/program-error", version = "=2.1.0" }
solana-program-memory = { path = "sdk/program-memory", version = "=2.1.0" }
Expand Down
10 changes: 10 additions & 0 deletions programs/sbf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ full = [
"sha3",
"digest",
"solana-pubkey/rand",
"dep:solana-precompile-error"
]
borsh = ["dep:borsh", "solana-program/borsh", "solana-secp256k1-recover/borsh"]
dev-context-only-utils = ["qualifier_attr", "solana-account/dev-context-only-utils"]
Expand Down Expand Up @@ -96,7 +97,9 @@ solana-frozen-abi = { workspace = true, optional = true, features = [
solana-frozen-abi-macro = { workspace = true, optional = true, features = [
"frozen-abi",
] }
solana-instruction = { workspace = true }
solana-native-token = { workspace = true }
solana-precompile-error = { workspace = true, optional = true }
solana-program = { workspace = true }
solana-program-memory = { workspace = true }
solana-pubkey = { workspace = true, default-features = false, features = ["std"] }
Expand Down
17 changes: 17 additions & 0 deletions sdk/precompile-error/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "solana-precompile-error"
description = "Solana PrecompileError type"
documentation = "https://docs.rs/solana-precompile-error"
version = { workspace = true }
authors = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
edition = { workspace = true }

[dependencies]
num-traits = { workspace = true }
solana-decode-error = { workspace = true }

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
76 changes: 76 additions & 0 deletions sdk/precompile-error/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/// Precompile errors
use {core::fmt, solana_decode_error::DecodeError};

/// Precompile errors
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PrecompileError {
InvalidPublicKey,
InvalidRecoveryId,
InvalidSignature,
InvalidDataOffsets,
InvalidInstructionDataSize,
}

impl num_traits::FromPrimitive for PrecompileError {
#[inline]
fn from_i64(n: i64) -> Option<Self> {
if n == PrecompileError::InvalidPublicKey as i64 {
Some(PrecompileError::InvalidPublicKey)
} else if n == PrecompileError::InvalidRecoveryId as i64 {
Some(PrecompileError::InvalidRecoveryId)
} else if n == PrecompileError::InvalidSignature as i64 {
Some(PrecompileError::InvalidSignature)
} else if n == PrecompileError::InvalidDataOffsets as i64 {
Some(PrecompileError::InvalidDataOffsets)
} else if n == PrecompileError::InvalidInstructionDataSize as i64 {
Some(PrecompileError::InvalidInstructionDataSize)
} else {
None
}
}
#[inline]
fn from_u64(n: u64) -> Option<Self> {
Self::from_i64(n as i64)
}
}

impl num_traits::ToPrimitive for PrecompileError {
#[inline]
fn to_i64(&self) -> Option<i64> {
Some(match *self {
PrecompileError::InvalidPublicKey => PrecompileError::InvalidPublicKey as i64,
PrecompileError::InvalidRecoveryId => PrecompileError::InvalidRecoveryId as i64,
PrecompileError::InvalidSignature => PrecompileError::InvalidSignature as i64,
PrecompileError::InvalidDataOffsets => PrecompileError::InvalidDataOffsets as i64,
PrecompileError::InvalidInstructionDataSize => {
PrecompileError::InvalidInstructionDataSize as i64
}
})
}
#[inline]
fn to_u64(&self) -> Option<u64> {
self.to_i64().map(|x| x as u64)
}
}

impl std::error::Error for PrecompileError {}

impl fmt::Display for PrecompileError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
PrecompileError::InvalidPublicKey => f.write_str("public key is not valid"),
PrecompileError::InvalidRecoveryId => f.write_str("id is not valid"),
PrecompileError::InvalidSignature => f.write_str("signature is not valid"),
PrecompileError::InvalidDataOffsets => f.write_str("offset not valid"),
PrecompileError::InvalidInstructionDataSize => {
f.write_str("instruction is incorrect size")
}
}
}
}

impl<T> DecodeError<T> for PrecompileError {
fn type_of() -> &'static str {
"PrecompileError"
}
}
3 changes: 2 additions & 1 deletion sdk/src/ed25519_instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
#![cfg(feature = "full")]

use {
crate::{instruction::Instruction, precompiles::PrecompileError},
bytemuck::bytes_of,
bytemuck_derive::{Pod, Zeroable},
ed25519_dalek::{ed25519::signature::Signature, Signer, Verifier},
solana_feature_set::{ed25519_precompile_verify_strict, FeatureSet},
solana_instruction::Instruction,
solana_precompile_error::PrecompileError,
};

pub const PUBKEY_SERIALIZED_SIZE: usize = 32;
Expand Down
31 changes: 4 additions & 27 deletions sdk/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,13 @@
#![cfg(feature = "full")]

#[deprecated(since = "2.1.0", note = "Use `solana-precompile-error` crate instead.")]
pub use solana_precompile_error::PrecompileError;
use {
lazy_static::lazy_static,
num_derive::{FromPrimitive, ToPrimitive},
solana_decode_error::DecodeError,
solana_feature_set::FeatureSet,
solana_program::{instruction::CompiledInstruction, pubkey::Pubkey},
thiserror::Error,
lazy_static::lazy_static, solana_feature_set::FeatureSet,
solana_program::instruction::CompiledInstruction, solana_pubkey::Pubkey,
};

/// Precompile errors
#[derive(Error, Debug, Clone, PartialEq, Eq, FromPrimitive, ToPrimitive)]
pub enum PrecompileError {
#[error("public key is not valid")]
InvalidPublicKey,
#[error("id is not valid")]
InvalidRecoveryId,
#[error("signature is not valid")]
InvalidSignature,
#[error("offset not valid")]
InvalidDataOffsets,
#[error("instruction is incorrect size")]
InvalidInstructionDataSize,
}

impl<T> DecodeError<T> for PrecompileError {
fn type_of() -> &'static str {
"PrecompileError"
}
}

/// All precompiled programs must implement the `Verify` function
pub type Verify = fn(&[u8], &[&[u8]], &FeatureSet) -> std::result::Result<(), PrecompileError>;

Expand Down
3 changes: 2 additions & 1 deletion sdk/src/secp256k1_instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,10 +788,11 @@
#![cfg(feature = "full")]

use {
crate::{instruction::Instruction, precompiles::PrecompileError},
digest::Digest,
serde_derive::{Deserialize, Serialize},
solana_feature_set::FeatureSet,
solana_instruction::Instruction,
solana_precompile_error::PrecompileError,
};

pub const HASHED_PUBKEY_SERIALIZED_SIZE: usize = 20;
Expand Down

0 comments on commit a8aef04

Please sign in to comment.