Skip to content

Commit

Permalink
Extract message crate (#3704)
Browse files Browse the repository at this point in the history
* minimize solana_program usage in message module

* move CompiledInstruction to message module

* move AddressLookupTableAccount to message module

* update AddressLookupTableAccount import

* start extracting message crate

* copy ALL_IDS from sysvar module to avoid dependency

* update comment

* use system-interface crate

* re-export message crate in solana-program

* fix tests

* fix doc links

* add doc_auto_cfg

* make serde and bincode optional

* inline const from solana_nonce

* update lock file

* remove thiserror

* frozen-abi support

* missing feature activation

* unused import

* fix frozen-abi support

* missing dep

* sort deps

* update digest

* update system-interface feature activation

* update rev

* fmt

* use published system-interface crate

* add test for inlined ALL_IDS

* missing wasm-bindgen dep

* reduce example_mocks usage

* make blake3 optional

* post-rebase fix
  • Loading branch information
kevinheavey authored Dec 2, 2024
1 parent 52c0382 commit 773e32c
Show file tree
Hide file tree
Showing 22 changed files with 470 additions and 214 deletions.
36 changes: 36 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 @@ -131,6 +131,7 @@ members = [
"sdk/keypair",
"sdk/logger",
"sdk/macro",
"sdk/message",
"sdk/msg",
"sdk/native-token",
"sdk/nonce",
Expand Down Expand Up @@ -488,6 +489,7 @@ solana-log-collector = { path = "log-collector", version = "=2.2.0" }
solana-logger = { path = "sdk/logger", version = "=2.2.0" }
solana-measure = { path = "measure", version = "=2.2.0" }
solana-merkle-tree = { path = "merkle-tree", version = "=2.2.0" }
solana-message = { path = "sdk/message", version = "=2.2.0" }
solana-metrics = { path = "metrics", version = "=2.2.0" }
solana-msg = { path = "sdk/msg", version = "=2.2.0" }
solana-native-token = { path = "sdk/native-token", version = "=2.2.0" }
Expand Down
2 changes: 1 addition & 1 deletion gossip/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub(crate) const PULL_RESPONSE_MIN_SERIALIZED_SIZE: usize = 161;
#[cfg_attr(
feature = "frozen-abi",
derive(AbiExample, AbiEnumVisitor),
frozen_abi(digest = "D8HvpYCkdo6JweUW61WQ9ZQH2AFvzh3G1qthicnvz4E8")
frozen_abi(digest = "ESDND6D3FcRyA6UTUpDVDcS4AkESc5E6UtZWSbT7G8e8")
)]
#[derive(Serialize, Deserialize, Debug)]
#[allow(clippy::large_enum_variant)]
Expand Down
22 changes: 22 additions & 0 deletions programs/sbf/Cargo.lock

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

79 changes: 79 additions & 0 deletions sdk/message/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
[package]
name = "solana-message"
description = "Solana transaction message types."
documentation = "https://docs.rs/solana-message"
version = { workspace = true }
authors = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
edition = { workspace = true }

[dependencies]
bincode = { workspace = true, optional = true }
blake3 = { workspace = true, features = ["traits-preview"], optional = true }
lazy_static = { workspace = true }
serde = { workspace = true, optional = true }
serde_derive = { workspace = true, optional = true }
solana-bincode = { workspace = true, optional = true }
solana-frozen-abi = { workspace = true, optional = true }
solana-frozen-abi-macro = { workspace = true, optional = true }
solana-hash = { workspace = true }
solana-instruction = { workspace = true, features = ["std"] }
solana-logger = { workspace = true, optional = true }
solana-pubkey = { workspace = true }
solana-sanitize = { workspace = true }
solana-sdk-ids = { workspace = true }
solana-short-vec = { workspace = true, optional = true }
solana-system-interface = { workspace = true, optional = true, features = [
"bincode",
] }
solana-transaction-error = { workspace = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = { workspace = true }

[dev-dependencies]
anyhow = { workspace = true }
bitflags = { workspace = true }
borsh = { workspace = true }
itertools = { workspace = true }
serde_json = { workspace = true }
solana-message = { path = ".", features = ["dev-context-only-utils"] }
solana-nonce = { workspace = true }
solana-program = { path = "../program" }
solana-sha256-hasher = { workspace = true }
solana-sysvar = { workspace = true }
static_assertions = { workspace = true }

[features]
bincode = [
"dep:bincode",
"dep:solana-bincode",
"dep:solana-system-interface",
"serde",
]
blake3 = ["dep:blake3"]
dev-context-only-utils = ["bincode", "blake3"]
frozen-abi = [
"dep:solana-frozen-abi",
"dep:solana-frozen-abi-macro",
"dep:solana-logger",
"solana-hash/frozen-abi",
"solana-pubkey/frozen-abi",
]
serde = [
"dep:serde",
"dep:serde_derive",
"dep:solana-short-vec",
"solana-hash/serde",
"solana-pubkey/serde",
]

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
all-features = true
rustdoc-args = ["--cfg=docsrs"]

[lints]
workspace = true
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use {
crate::{
instruction::{CompiledInstruction, Instruction},
message::{v0::LoadedAddresses, CompileError},
pubkey::Pubkey,
},
crate::{compiled_instruction::CompiledInstruction, v0::LoadedAddresses, CompileError},
solana_instruction::Instruction,
solana_pubkey::Pubkey,
std::{collections::BTreeMap, iter::zip, ops::Index},
};

Expand Down Expand Up @@ -152,7 +150,7 @@ impl PartialEq for AccountKeys<'_> {

#[cfg(test)]
mod tests {
use {super::*, crate::instruction::AccountMeta};
use {super::*, solana_instruction::AccountMeta};

fn test_account_keys() -> [Pubkey; 6] {
let key0 = Pubkey::new_unique();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::v0::{LoadedAddresses, MessageAddressTableLookup};
use crate::v0::{LoadedAddresses, MessageAddressTableLookup};
#[deprecated(
since = "2.1.0",
note = "Use solana_transaction_error::AddressLoaderError instead"
Expand Down
56 changes: 56 additions & 0 deletions sdk/message/src/compiled_instruction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#[cfg(feature = "serde")]
use serde_derive::{Deserialize, Serialize};
#[cfg(feature = "frozen-abi")]
use solana_frozen_abi_macro::AbiExample;
use {solana_pubkey::Pubkey, solana_sanitize::Sanitize};

/// A compact encoding of an instruction.
///
/// A `CompiledInstruction` is a component of a multi-instruction [`Message`],
/// which is the core of a Solana transaction. It is created during the
/// construction of `Message`. Most users will not interact with it directly.
///
/// [`Message`]: crate::Message
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
#[cfg_attr(
feature = "serde",
derive(Deserialize, Serialize),
serde(rename_all = "camelCase")
)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct CompiledInstruction {
/// Index into the transaction keys array indicating the program account that executes this instruction.
pub program_id_index: u8,
/// Ordered indices into the transaction keys array indicating which accounts to pass to the program.
#[cfg_attr(feature = "serde", serde(with = "solana_short_vec"))]
pub accounts: Vec<u8>,
/// The program input data.
#[cfg_attr(feature = "serde", serde(with = "solana_short_vec"))]
pub data: Vec<u8>,
}

impl Sanitize for CompiledInstruction {}

impl CompiledInstruction {
#[cfg(feature = "bincode")]
pub fn new<T: serde::Serialize>(program_ids_index: u8, data: &T, accounts: Vec<u8>) -> Self {
let data = bincode::serialize(data).unwrap();
Self {
program_id_index: program_ids_index,
accounts,
data,
}
}

pub fn new_from_raw_parts(program_id_index: u8, data: Vec<u8>, accounts: Vec<u8>) -> Self {
Self {
program_id_index,
accounts,
data,
}
}

pub fn program_id<'a>(&self, program_ids: &'a [Pubkey]) -> &'a Pubkey {
&program_ids[self.program_id_index as usize]
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#[cfg(not(target_os = "solana"))]
use crate::{
address_lookup_table::AddressLookupTableAccount,
message::v0::{LoadedAddresses, MessageAddressTableLookup},
v0::{LoadedAddresses, MessageAddressTableLookup},
AddressLookupTableAccount,
};
use {
crate::{instruction::Instruction, message::MessageHeader, pubkey::Pubkey},
crate::MessageHeader, core::fmt, solana_instruction::Instruction, solana_pubkey::Pubkey,
std::collections::BTreeMap,
thiserror::Error,
};

/// A helper struct to collect pubkeys compiled for a set of instructions
Expand All @@ -17,16 +16,32 @@ pub(crate) struct CompiledKeys {
}

#[cfg_attr(target_os = "solana", allow(dead_code))]
#[derive(PartialEq, Debug, Error, Eq, Clone)]
#[derive(PartialEq, Debug, Eq, Clone)]
pub enum CompileError {
#[error("account index overflowed during compilation")]
AccountIndexOverflow,
#[error("address lookup table index overflowed during compilation")]
AddressTableLookupIndexOverflow,
#[error("encountered unknown account key `{0}` during instruction compilation")]
UnknownInstructionKey(Pubkey),
}

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

impl fmt::Display for CompileError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
CompileError::AccountIndexOverflow => {
f.write_str("account index overflowed during compilation")
}
CompileError::AddressTableLookupIndexOverflow => {
f.write_str("address lookup table index overflowed during compilation")
}
CompileError::UnknownInstructionKey(key) => f.write_fmt(format_args!(
"encountered unknown account key `{0}` during instruction compilation",
key,
)),
}
}
}

#[derive(Default, Debug, Clone, PartialEq, Eq)]
struct CompiledKeyMeta {
is_signer: bool,
Expand Down Expand Up @@ -184,7 +199,7 @@ impl CompiledKeys {

#[cfg(test)]
mod tests {
use {super::*, crate::instruction::AccountMeta, bitflags::bitflags};
use {super::*, bitflags::bitflags, solana_instruction::AccountMeta};

bitflags! {
#[derive(Clone, Copy)]
Expand Down
Loading

0 comments on commit 773e32c

Please sign in to comment.