Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move most of program into program-core #2432

Closed
wants to merge 37 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
d989c68
extract solana-program-core from solana-program
kevinheavey Aug 3, 2024
edf438e
make curve25519-dalek an optional feature of program-core
kevinheavey Aug 3, 2024
8c553d3
fmt
kevinheavey Aug 3, 2024
7b99d7b
no default features in program-core
kevinheavey Aug 3, 2024
a8f8b12
unused solana-program dep
kevinheavey Aug 3, 2024
5a5ab2d
make bytemuck optional in program-core
kevinheavey Aug 3, 2024
b2387b0
make bincode optional in program-core
kevinheavey Aug 3, 2024
ac51eec
make serde optional in program-core
kevinheavey Aug 3, 2024
d8b09ac
make sha2 optional in program-core
kevinheavey Aug 3, 2024
d8064b3
fix sbf imports
kevinheavey Aug 3, 2024
4a61429
fix wasm issues
kevinheavey Aug 3, 2024
2b294e3
make sha3 optional in program-core
kevinheavey Aug 3, 2024
bec1419
remove num_derive from program-core
kevinheavey Aug 3, 2024
56b869a
remove thiserror from program-core
kevinheavey Aug 3, 2024
a41c7a9
update lock file
kevinheavey Aug 3, 2024
57c07e9
remove CloneZeroed from program-core
kevinheavey Aug 3, 2024
3d73891
fmt
kevinheavey Aug 3, 2024
d70653d
add const pubkey support and make declare_id a declarative macro
kevinheavey Aug 3, 2024
9410352
remove unused dep
kevinheavey Aug 3, 2024
1a1ff24
deps cleanup
kevinheavey Aug 3, 2024
2fa31a1
make base64 dep optional
kevinheavey Aug 3, 2024
854fa96
fix doc test
kevinheavey Aug 3, 2024
1ca260e
make bv dep optional
kevinheavey Aug 3, 2024
da10f7b
clean up cfg clause
kevinheavey Aug 3, 2024
05b5096
activate bv feature in solana-program
kevinheavey Aug 3, 2024
da4397b
make blake3 optional in program-core
kevinheavey Aug 3, 2024
b43b5d8
remove unnecessary num-traits feature
kevinheavey Aug 3, 2024
4ebf335
make num-traits optional in program-core
kevinheavey Aug 3, 2024
2f87c53
make log optional in program-core
kevinheavey Aug 3, 2024
6b369a0
clippy
kevinheavey Aug 3, 2024
cbc3ca8
fix num-traits feature
kevinheavey Aug 4, 2024
9ccc12b
clippy
kevinheavey Aug 4, 2024
e5e08a4
update nits.sh
kevinheavey Aug 4, 2024
416ba55
conditional test fix
kevinheavey Aug 4, 2024
bd7d4f9
sort Cargo.toml
kevinheavey Aug 4, 2024
17d5889
fmt
kevinheavey Aug 4, 2024
cc6e753
fix for sha2+curve25519 feature activation
kevinheavey Aug 5, 2024
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
Prev Previous commit
Next Next commit
make sha2 optional in program-core
kevinheavey committed Aug 3, 2024
commit d8b09ac6ade102eb4e9a134771e505ed385c7b4f
4 changes: 3 additions & 1 deletion sdk/program-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ qualifier_attr = { workspace = true, optional = true }
serde = { workspace = true, optional = true }
serde_bytes = { workspace = true, optional = true }
serde_derive = { workspace = true, optional = true }
sha2 = { workspace = true }
sha2 = { workspace = true, optional = true }
sha3 = { workspace = true }
solana-atomic-u64 = { workspace = true }
solana-decode-error = { workspace = true }
@@ -99,6 +99,7 @@ dev-context-only-utils = [
"bytemuck",
"curve25519",
"serde",
"sha2"
]
frozen-abi = [
"dep:solana-frozen-abi",
@@ -112,3 +113,4 @@ serde = [
"dep:solana-short-vec",
"bv/serde"
]
sha2 = ["dep:sha2"]
4 changes: 2 additions & 2 deletions sdk/program-core/src/address_lookup_table/instruction.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(feature = "bincode")]
use crate::instruction::AccountMeta;
#[cfg(any(feature = "bincode", feature = "curve25519", target_os = "solana"))]
#[cfg(any(feature = "bincode", target_os = "solana"))]
use crate::instruction::Instruction;
use crate::{clock::Slot, pubkey::Pubkey};

@@ -63,7 +63,7 @@ pub enum ProgramInstruction {
CloseLookupTable,
}

#[cfg(any(feature = "curve25519", target_os = "solana"))]
#[cfg(any(all(feature = "curve25519", feature = "sha2"), target_os = "solana"))]
/// Derives the address of an address table account from a wallet address and a recent block's slot.
pub fn derive_lookup_table_address(
authority_address: &Pubkey,
6 changes: 3 additions & 3 deletions sdk/program-core/src/bpf_loader_upgradeable.rs
Original file line number Diff line number Diff line change
@@ -16,9 +16,9 @@
//! [`loader_upgradeable_instruction`]: crate::loader_upgradeable_instruction

use crate::pubkey::Pubkey;
#[cfg(any(feature = "curve25519", target_os = "solana"))]
#[cfg(all(feature = "bincode", any(feature = "curve25519", target_os = "solana")))]
use crate::sysvar;
#[cfg(any(feature = "bincode", feature = "curve25519", target_os = "solana"))]
#[cfg(feature = "bincode")]
use crate::{
instruction::{AccountMeta, Instruction, InstructionError},
loader_upgradeable_instruction::UpgradeableLoaderInstruction,
@@ -88,7 +88,7 @@ impl UpgradeableLoaderState {
}
}

#[cfg(any(feature = "curve25519", target_os = "solana"))]
#[cfg(any(all(feature = "curve25519", feature = "sha2"), target_os = "solana"))]
/// Returns the program data address for a program ID
pub fn get_program_data_address(program_address: &Pubkey) -> Pubkey {
Pubkey::find_program_address(&[program_address.as_ref()], &id()).0
8 changes: 7 additions & 1 deletion sdk/program-core/src/hash.rs
Original file line number Diff line number Diff line change
@@ -7,10 +7,11 @@
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
#[cfg(feature = "bytemuck")]
use bytemuck_derive::{Pod, Zeroable};
#[cfg(feature = "sha2")]
use sha2::{Digest, Sha256};
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::wasm_bindgen;
use {
sha2::{Digest, Sha256},
solana_sanitize::Sanitize,
std::{convert::TryFrom, fmt, mem, str::FromStr},
thiserror::Error,
@@ -44,11 +45,13 @@ const MAX_BASE58_LEN: usize = 44;
#[repr(transparent)]
pub struct Hash(pub(crate) [u8; HASH_BYTES]);

#[cfg(feature = "sha2")]
#[derive(Clone, Default)]
pub struct Hasher {
hasher: Sha256,
}

#[cfg(feature = "sha2")]
impl Hasher {
pub fn hash(&mut self, val: &[u8]) {
self.hasher.update(val);
@@ -140,6 +143,7 @@ impl Hash {
}
}

#[cfg(any(feature = "sha2", target_os = "solana"))]
/// Return a Sha256 hash for the given data.
pub fn hashv(vals: &[&[u8]]) -> Hash {
// Perform the calculation inline, calling this from within a program is
@@ -165,11 +169,13 @@ pub fn hashv(vals: &[&[u8]]) -> Hash {
}
}

#[cfg(any(feature = "sha2", target_os = "solana"))]
/// Return a Sha256 hash for the given data.
pub fn hash(val: &[u8]) -> Hash {
hashv(&[val])
}

#[cfg(any(feature = "sha2", target_os = "solana"))]
/// Return the hash of the given hash extended with the given value.
pub fn extend_and_hash(id: &Hash, val: &[u8]) -> Hash {
let mut hash_data = id.as_ref().to_vec();
13 changes: 7 additions & 6 deletions sdk/program-core/src/nonce/state/current.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use crate::{
fee_calculator::FeeCalculator,
hash::{hashv, Hash},
pubkey::Pubkey,
};
use crate::{fee_calculator::FeeCalculator, hash::Hash, pubkey::Pubkey};
#[cfg(feature = "serde")]
use serde_derive::{Deserialize, Serialize};

#[cfg(any(feature = "sha2", target_os = "solana"))]
const DURABLE_NONCE_HASH_PREFIX: &[u8] = "DURABLE_NONCE".as_bytes();

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
@@ -54,8 +51,12 @@ impl Data {
}

impl DurableNonce {
#[cfg(any(feature = "sha2", target_os = "solana"))]
pub fn from_blockhash(blockhash: &Hash) -> Self {
Self(hashv(&[DURABLE_NONCE_HASH_PREFIX, blockhash.as_ref()]))
Self(crate::hash::hashv(&[
DURABLE_NONCE_HASH_PREFIX,
blockhash.as_ref(),
]))
}

/// Hash value used as recent_blockhash field in Transactions.
1 change: 1 addition & 0 deletions sdk/program-core/src/nonce/state/mod.rs
Original file line number Diff line number Diff line change
@@ -52,6 +52,7 @@ impl Versions {
}
}

#[cfg(any(feature = "sha2", target_os = "solana"))]
/// Upgrades legacy nonces out of chain blockhash domains.
pub fn upgrade(self) -> Option<Self> {
match self {
11 changes: 6 additions & 5 deletions sdk/program-core/src/pubkey.rs
Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@ use bytemuck_derive::{Pod, Zeroable};
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::wasm_bindgen;
use {
crate::hash::hashv,
num_derive::{FromPrimitive, ToPrimitive},
solana_decode_error::DecodeError,
std::{
@@ -31,6 +30,7 @@ pub const MAX_SEEDS: usize = 16;
/// Maximum string length of a base58 encoded pubkey
const MAX_BASE58_LEN: usize = 44;

#[cfg(any(feature = "sha2", target_os = "solana"))]
const PDA_MARKER: &[u8; 21] = b"ProgramDerivedAddress";

#[cfg_attr(feature = "serde", derive(Serialize))]
@@ -191,6 +191,7 @@ impl Pubkey {
Self::from(b)
}

#[cfg(any(feature = "sha2", target_os = "solana"))]
pub fn create_with_seed(
base: &Pubkey,
seed: &str,
@@ -207,7 +208,7 @@ impl Pubkey {
return Err(PubkeyError::IllegalOwner);
}
}
let hash = hashv(&[base.as_ref(), seed.as_ref(), owner]);
let hash = crate::hash::hashv(&[base.as_ref(), seed.as_ref(), owner]);
Ok(Pubkey::from(hash.to_bytes()))
}

@@ -464,7 +465,7 @@ impl Pubkey {
/// #
/// # Ok::<(), anyhow::Error>(())
/// ```
#[cfg(any(feature = "curve25519", target_os = "solana"))]
#[cfg(any(all(feature = "curve25519", feature = "sha2"), target_os = "solana"))]
pub fn find_program_address(seeds: &[&[u8]], program_id: &Pubkey) -> (Pubkey, u8) {
Self::try_find_program_address(seeds, program_id)
.unwrap_or_else(|| panic!("Unable to find a viable program address bump seed"))
@@ -482,7 +483,7 @@ impl Pubkey {
/// See the documentation for [`find_program_address`] for a full description.
///
/// [`find_program_address`]: Pubkey::find_program_address
#[cfg(any(feature = "curve25519", target_os = "solana"))]
#[cfg(any(all(feature = "curve25519", feature = "sha2"), target_os = "solana"))]
#[allow(clippy::same_item_push)]
pub fn try_find_program_address(seeds: &[&[u8]], program_id: &Pubkey) -> Option<(Pubkey, u8)> {
// Perform the calculation inline, calling this from within a program is
@@ -567,7 +568,7 @@ impl Pubkey {
/// assert_eq!(expected_pda, actual_pda);
/// # Ok::<(), anyhow::Error>(())
/// ```
#[cfg(any(feature = "curve25519", target_os = "solana"))]
#[cfg(any(all(feature = "curve25519", feature = "sha2"), target_os = "solana"))]
pub fn create_program_address(
seeds: &[&[u8]],
program_id: &Pubkey,
1 change: 1 addition & 0 deletions sdk/program/Cargo.toml
Original file line number Diff line number Diff line change
@@ -37,6 +37,7 @@ solana-program-core = { workspace = true, features = [
"bytemuck",
"curve25519",
"serde",
"sha2"
] }
solana-program-memory = { workspace = true }
solana-sanitize = { workspace = true }