solana_program/
lib.rs

1//! The base library for all Solana on-chain Rust programs.
2//!
3//! All Solana Rust programs that run on-chain will link to this crate, which
4//! acts as a standard library for Solana programs. Solana programs also link to
5//! the [Rust standard library][std], though it is [modified][sstd] for the
6//! Solana runtime environment. While off-chain programs that interact with the
7//! Solana network _can_ link to this crate, they typically instead use the
8//! [`solana-sdk`] crate, which reexports all modules from `solana-program`.
9//!
10//! [std]: https://doc.rust-lang.org/stable/std/
11//! [sstd]: https://solana.com/docs/programs/limitations#rust-libraries
12//! [`solana-sdk`]: https://docs.rs/solana-sdk/latest/solana_sdk/
13//!
14//! This library defines
15//!
16//! - macros for declaring the [program entrypoint][pe],
17//! - [core data types][cdt],
18//! - [logging] macros,
19//! - [serialization] methods,
20//! - methods for [cross-program instruction execution][cpi],
21//! - program IDs and instruction constructors for the system program and other
22//!   [native programs][np],
23//! - [sysvar] accessors.
24//!
25//! [pe]: #defining-a-solana-program
26//! [cdt]: #core-data-types
27//! [logging]: crate::log
28//! [serialization]: #serialization
29//! [np]: #native-programs
30//! [cpi]: #cross-program-instruction-execution
31//! [sysvar]: crate::sysvar
32//!
33//! Idiomatic examples of `solana-program` usage can be found in
34//! [the Solana Program Library][spl].
35//!
36//! [spl]: https://github.com/solana-labs/solana-program-library
37//!
38//! # Defining a solana program
39//!
40//! Solana program crates have some unique properties compared to typical Rust
41//! programs:
42//!
43//! - They are often compiled for both on-chain use and off-chain use. This is
44//!   primarily because off-chain clients may need access to data types
45//!   defined by the on-chain program.
46//! - They do not define a `main` function, but instead define their entrypoint
47//!   with the [`entrypoint!`] macro.
48//! - They are compiled as the ["cdylib"] crate type for dynamic loading
49//!   by the Solana runtime.
50//! - They run in a constrained VM environment, and while they do have access to
51//!   the [Rust standard library][std], many features of the standard library,
52//!   particularly related to OS services, will fail at runtime, will silently
53//!   do nothing, or are not defined. See the [restrictions to the Rust standard
54//!   library][sstd] in the Solana documentation for more.
55//!
56//! [std]: https://doc.rust-lang.org/std/index.html
57//! ["cdylib"]: https://doc.rust-lang.org/reference/linkage.html
58//!
59//! Because multiple crates that are linked together cannot all define
60//! program entrypoints (see the [`entrypoint!`] documentation) a common
61//! convention is to use a [Cargo feature] called `no-entrypoint` to allow
62//! the program entrypoint to be disabled.
63//!
64//! [Cargo feature]: https://doc.rust-lang.org/cargo/reference/features.html
65//!
66//! The skeleton of a Solana program typically looks like:
67//!
68//! ```
69//! #[cfg(not(feature = "no-entrypoint"))]
70//! pub mod entrypoint {
71//!     use solana_program::{
72//!         account_info::AccountInfo,
73//!         entrypoint,
74//!         entrypoint::ProgramResult,
75//!         pubkey::Pubkey,
76//!     };
77//!
78//!     entrypoint!(process_instruction);
79//!
80//!     pub fn process_instruction(
81//!         program_id: &Pubkey,
82//!         accounts: &[AccountInfo],
83//!         instruction_data: &[u8],
84//!     ) -> ProgramResult {
85//!         // Decode and dispatch instructions here.
86//!         todo!()
87//!     }
88//! }
89//!
90//! // Additional code goes here.
91//! ```
92//!
93//! With a `Cargo.toml` file that contains
94//!
95//! ```toml
96//! [lib]
97//! crate-type = ["cdylib"]
98//!
99//! [features]
100//! no-entrypoint = []
101//! ```
102//!
103//! Note that a Solana program must specify its crate-type as "cdylib", to
104//! be discovered and built by the `cargo-build-sbf` command as a deployable program.
105//! Solana programs also often have crate-type "rlib" so they can be linked to other Rust crates.
106//! Avoid using "rlib" and "cdylib" crates together, since their combined usage precludes
107//! compiler optimizations that may decrease program size and CU usage.
108//!
109//! Prefer writing a separate package if it is supposed to be used as a library for other Solana
110//! programs (i.e. a "rlib" only crate). This would be normally the case for defining account
111//! types and helpers that are used by both clients and program. When creating a Rust project
112//! intended to be a program ready for deployment, use only the "cdylib" crate type.
113//!
114//! # On-chain vs. off-chain compilation targets
115//!
116//! Solana programs run on the [rbpf] VM, which implements a variant of the
117//! [eBPF] instruction set. Because this crate can be compiled for both on-chain
118//! and off-chain execution, the environments of which are significantly
119//! different, it extensively uses [conditional compilation][cc] to tailor its
120//! implementation to the environment. The `cfg` predicate used for identifying
121//! compilation for on-chain programs is `target_os = "solana"`, as in this
122//! example from the `solana-program` codebase that logs a message via a
123//! syscall when run on-chain, and via a library call when offchain:
124//!
125//! [rbpf]: https://github.com/solana-labs/rbpf
126//! [eBPF]: https://ebpf.io/
127//! [cc]: https://doc.rust-lang.org/reference/conditional-compilation.html
128//!
129//! ```
130//! pub fn sol_log(message: &str) {
131//!     #[cfg(target_os = "solana")]
132//!     unsafe {
133//!         sol_log_(message.as_ptr(), message.len() as u64);
134//!     }
135//!
136//!     #[cfg(not(target_os = "solana"))]
137//!     program_stubs::sol_log(message);
138//! }
139//! # mod program_stubs {
140//! #     pub(crate) fn sol_log(message: &str) { }
141//! # }
142//! ```
143//!
144//! This `cfg` pattern is suitable as well for user code that needs to work both
145//! on-chain and off-chain.
146//!
147//! `solana-program` and `solana-sdk` were previously a single crate. Because of
148//! this history, and because of the dual-usage of `solana-program` for two
149//! different environments, it contains some features that are not available to
150//! on-chain programs at compile-time. It also contains some on-chain features
151//! that will fail in off-chain scenarios at runtime. This distinction is not
152//! well-reflected in the documentation.
153//!
154//! For a more complete description of Solana's implementation of eBPF and its
155//! limitations, see the main Solana documentation for [on-chain programs][ocp].
156//!
157//! [ocp]: https://solana.com/docs/programs
158//!
159//! # Core data types
160//!
161//! - [`Pubkey`] — The address of a [Solana account][acc]. Some account
162//!   addresses are [ed25519] public keys, with corresponding secret keys that
163//!   are managed off-chain. Often, though, account addresses do not have
164//!   corresponding secret keys — as with [_program derived
165//!   addresses_][pdas] — or the secret key is not relevant to the
166//!   operation of a program, and may have even been disposed of. As running
167//!   Solana programs can not safely create or manage secret keys, the full
168//!   [`Keypair`] is not defined in `solana-program` but in `solana-sdk`.
169//! - [`Hash`] — A cryptographic hash. Used to uniquely identify blocks,
170//!   and also for general purpose hashing.
171//! - [`AccountInfo`] — A description of a single Solana account. All accounts
172//!   that might be accessed by a program invocation are provided to the program
173//!   entrypoint as `AccountInfo`.
174//! - [`Instruction`] — A directive telling the runtime to execute a program,
175//!   passing it a set of accounts and program-specific data.
176//! - [`ProgramError`] and [`ProgramResult`] — The error type that all programs
177//!   must return, reported to the runtime as a `u64`.
178//! - [`Sol`] — The Solana native token type, with conversions to and from
179//!   [_lamports_], the smallest fractional unit of SOL, in the [`native_token`]
180//!   module.
181//!
182//! [acc]: https://solana.com/docs/core/accounts
183//! [`Pubkey`]: pubkey::Pubkey
184//! [`Hash`]: hash::Hash
185//! [`Instruction`]: instruction::Instruction
186//! [`AccountInfo`]: account_info::AccountInfo
187//! [`ProgramError`]: program_error::ProgramError
188//! [`ProgramResult`]: entrypoint::ProgramResult
189//! [ed25519]: https://ed25519.cr.yp.to/
190//! [`Keypair`]: https://docs.rs/solana-sdk/latest/solana_sdk/signer/keypair/struct.Keypair.html
191//! [SHA-256]: https://en.wikipedia.org/wiki/SHA-2
192//! [`Sol`]: native_token::Sol
193//! [_lamports_]: https://solana.com/docs/intro#what-are-sols
194//!
195//! # Serialization
196//!
197//! Within the Solana runtime, programs, and network, at least three different
198//! serialization formats are used, and `solana-program` provides access to
199//! those needed by programs.
200//!
201//! In user-written Solana program code, serialization is primarily used for
202//! accessing [`AccountInfo`] data and [`Instruction`] data, both of which are
203//! program-specific binary data. Every program is free to decide their own
204//! serialization format, but data received from other sources —
205//! [sysvars][sysvar] for example — must be deserialized using the methods
206//! indicated by the documentation for that data or data type.
207//!
208//! [`AccountInfo`]: account_info::AccountInfo
209//! [`Instruction`]: instruction::Instruction
210//!
211//! The three serialization formats in use in Solana are:
212//!
213//! - __[Borsh]__, a compact and well-specified format developed by the [NEAR]
214//!   project, suitable for use in protocol definitions and for archival storage.
215//!   It has a [Rust implementation][brust] and a [JavaScript implementation][bjs]
216//!   and is recommended for all purposes.
217//!
218//!   Users need to import the [`borsh`] crate themselves — it is not
219//!   re-exported by `solana-program`, though this crate provides several useful
220//!   utilities in its [`borsh1` module][borshmod] that are not available in the
221//!   `borsh` library.
222//!
223//!   The [`Instruction::new_with_borsh`] function creates an `Instruction` by
224//!   serializing a value with borsh.
225//!
226//!   [Borsh]: https://borsh.io/
227//!   [NEAR]: https://near.org/
228//!   [brust]: https://docs.rs/borsh
229//!   [bjs]: https://github.com/near/borsh-js
230//!   [`borsh`]: https://docs.rs/borsh
231//!   [borshmod]: crate::borsh1
232//!   [`Instruction::new_with_borsh`]: instruction::Instruction::new_with_borsh
233//!
234//! - __[Bincode]__, a compact serialization format that implements the [Serde]
235//!   Rust APIs. As it does not have a specification nor a JavaScript
236//!   implementation, and uses more CPU than borsh, it is not recommend for new
237//!   code.
238//!
239//!   Many system program and native program instructions are serialized with
240//!   bincode, and it is used for other purposes in the runtime. In these cases
241//!   Rust programmers are generally not directly exposed to the encoding format
242//!   as it is hidden behind APIs.
243//!
244//!   The [`Instruction::new_with_bincode`] function creates an `Instruction` by
245//!   serializing a value with bincode.
246//!
247//!   [Bincode]: https://docs.rs/bincode
248//!   [Serde]: https://serde.rs/
249//!   [`Instruction::new_with_bincode`]: instruction::Instruction::new_with_bincode
250//!
251//! - __[`Pack`]__, a Solana-specific serialization API that is used by many
252//!   older programs in the [Solana Program Library][spl] to define their
253//!   account format. It is difficult to implement and does not define a
254//!   language-independent serialization format. It is not generally recommended
255//!   for new code.
256//!
257//!   [`Pack`]: https://docs.rs/solana-program-pack/latest/trait.Pack.html
258//!
259//! Developers should carefully consider the CPU cost of serialization, balanced
260//! against the need for correctness and ease of use: off-the-shelf
261//! serialization formats tend to be more expensive than carefully hand-written
262//! application-specific formats; but application-specific formats are more
263//! difficult to ensure the correctness of, and to provide multi-language
264//! implementations for. It is not uncommon for programs to pack and unpack
265//! their data with hand-written code.
266//!
267//! # Cross-program instruction execution
268//!
269//! Solana programs may call other programs, termed [_cross-program
270//! invocation_][cpi] (CPI), with the [`invoke`] and [`invoke_signed`]
271//! functions. When calling another program the caller must provide the
272//! [`Instruction`] to be invoked, as well as the [`AccountInfo`] for every
273//! account required by the instruction. Because the only way for a program to
274//! acquire `AccountInfo` values is by receiving them from the runtime at the
275//! [program entrypoint][entrypoint!], any account required by the callee
276//! program must transitively be required by the caller program, and provided by
277//! _its_ caller.
278//!
279//! [`invoke`]: program::invoke
280//! [`invoke_signed`]: program::invoke_signed
281//! [cpi]: https://solana.com/docs/core/cpi
282//!
283//! A simple example of transferring lamports via CPI:
284//!
285//! ```
286//! use solana_account_info::{next_account_info, AccountInfo};
287//! use solana_program_entrypoint::entrypoint;
288//! use solana_program_error::ProgramResult;
289//! use solana_cpi::invoke;
290//! use solana_pubkey::Pubkey;
291//! use solana_system_interface::instruction::transfer;
292//!
293//! entrypoint!(process_instruction);
294//!
295//! fn process_instruction(
296//!     program_id: &Pubkey,
297//!     accounts: &[AccountInfo],
298//!     instruction_data: &[u8],
299//! ) -> ProgramResult {
300//!     let account_info_iter = &mut accounts.iter();
301//!
302//!     let payer = next_account_info(account_info_iter)?;
303//!     let recipient = next_account_info(account_info_iter)?;
304//!
305//!     assert!(payer.is_writable);
306//!     assert!(payer.is_signer);
307//!     assert!(recipient.is_writable);
308//!
309//!     let lamports = 1000000;
310//!
311//!     invoke(
312//!         &transfer(payer.key, recipient.key, lamports),
313//!         &[payer.clone(), recipient.clone()],
314//!     )
315//! }
316//! ```
317//!
318//! Solana also includes a mechanism to let programs control and sign for
319//! accounts without needing to protect a corresponding secret key, called
320//! [_program derived addresses_][pdas]. PDAs are derived with the
321//! [`Pubkey::find_program_address`] function. With a PDA, a program can call
322//! `invoke_signed` to call another program while virtually "signing" for the
323//! PDA.
324//!
325//! [pdas]: https://solana.com/docs/core/cpi#program-derived-addresses
326//! [`Pubkey::find_program_address`]: pubkey::Pubkey::find_program_address
327//!
328//! A simple example of creating an account for a PDA:
329//!
330//! ```
331//! use solana_account_info::{next_account_info, AccountInfo};
332//! use solana_program_entrypoint::entrypoint;
333//! use solana_program_error::ProgramResult;
334//! use solana_cpi::invoke_signed;
335//! use solana_pubkey::Pubkey;
336//! use solana_system_interface::instruction::create_account;
337//!
338//! entrypoint!(process_instruction);
339//!
340//! fn process_instruction(
341//!     program_id: &Pubkey,
342//!     accounts: &[AccountInfo],
343//!     instruction_data: &[u8],
344//! ) -> ProgramResult {
345//!     let account_info_iter = &mut accounts.iter();
346//!     let payer = next_account_info(account_info_iter)?;
347//!     let vault_pda = next_account_info(account_info_iter)?;
348//!     let system_program = next_account_info(account_info_iter)?;
349//!
350//!     assert!(payer.is_writable);
351//!     assert!(payer.is_signer);
352//!     assert!(vault_pda.is_writable);
353//!     assert_eq!(vault_pda.owner, &solana_system_interface::program::ID);
354//!     assert!(solana_system_interface::program::check_id(system_program.key));
355//!
356//!     let vault_bump_seed = instruction_data[0];
357//!     let vault_seeds = &[b"vault", payer.key.as_ref(), &[vault_bump_seed]];
358//!     let expected_vault_pda = Pubkey::create_program_address(vault_seeds, program_id)?;
359//!
360//!     assert_eq!(vault_pda.key, &expected_vault_pda);
361//!
362//!     let lamports = 10000000;
363//!     let vault_size = 16;
364//!
365//!     invoke_signed(
366//!         &create_account(
367//!             &payer.key,
368//!             &vault_pda.key,
369//!             lamports,
370//!             vault_size,
371//!             &program_id,
372//!         ),
373//!         &[
374//!             payer.clone(),
375//!             vault_pda.clone(),
376//!         ],
377//!         &[
378//!             &[
379//!                 b"vault",
380//!                 payer.key.as_ref(),
381//!                 &[vault_bump_seed],
382//!             ],
383//!         ]
384//!     )?;
385//!     Ok(())
386//! }
387//! ```
388//!
389//! # Native programs
390//!
391//! Some solana programs are [_native programs_][np2], running native machine
392//! code that is distributed with the runtime, with well-known program IDs.
393//!
394//! [np2]: https://docs.solanalabs.com/runtime/programs
395//!
396//! Some native programs can be [invoked][cpi] by other programs, but some can
397//! only be executed as "top-level" instructions included by off-chain clients
398//! in a [`Transaction`].
399//!
400//! [`Transaction`]: https://docs.rs/solana-sdk/latest/solana_sdk/transaction/struct.Transaction.html
401//!
402//! This crate defines the program IDs for most native programs. Even though
403//! some native programs cannot be invoked by other programs, a Solana program
404//! may need access to their program IDs. For example, a program may need to
405//! verify that an ed25519 signature verification instruction was included in
406//! the same transaction as its own instruction. For many native programs, this
407//! crate also defines enums that represent the instructions they process, and
408//! constructors for building the instructions.
409//!
410//! Locations of program IDs and instruction constructors are noted in the list
411//! below, as well as whether they are invokable by other programs.
412//!
413//! While some native programs have been active since the genesis block, others
414//! are activated dynamically after a specific [slot], and some are not yet
415//! active. This documentation does not distinguish which native programs are
416//! active on any particular network. The `solana feature status` CLI command
417//! can help in determining active features.
418//!
419//! [slot]: https://solana.com/docs/terminology#slot
420//!
421//! Native programs important to Solana program authors include:
422//!
423//! - __System Program__: Creates new accounts, allocates account data, assigns
424//!   accounts to owning programs, transfers lamports from System Program owned
425//!   accounts and pays transaction fees.
426//!   - ID: [`solana_system_interface::program::ID`](https://docs.rs/solana-system-interface/latest/solana_system_interface/program/constant.ID.html)
427//!   - Instruction: [`solana_system_interface::instruction`](https://docs.rs/solana-system-interface/latest/solana_system_interface/instruction/index.html)
428//!   - Invokable by programs? yes
429//!
430//! - __Compute Budget Program__: Requests additional CPU or memory resources
431//!   for a transaction. This program does nothing when called from another
432//!   program.
433//!   - ID: [`solana_compute_budget_interface::ID`](https://docs.rs/solana-compute-budget-interface/latest/solana_compute_budget_interface/constant.ID.html)
434//!   - Instruction: [`solana_compute_budget_interface::ComputeBudgetInstruction`](https://docs.rs/solana-compute-budget-interface/latest/solana_compute_budget_interface/enum.ComputeBudgetInstruction.html)
435//!   - Invokable by programs? no
436//!
437//! - __ed25519 Program__: Verifies an ed25519 signature.
438//!   - ID: [`solana_sdk_ids::ed25519_program::ID`](https://docs.rs/solana-sdk-ids/latest/solana_sdk_ids/ed25519_program/constant.ID.html)
439//!   - Instruction: [`solana_ed25519_program::new_ed25519_instruction_with_signature`](https://docs.rs/solana-ed25519-program/latest/solana_ed25519_program/fn.new_ed25519_instruction_with_signature.html)
440//!   - Invokable by programs? no
441//!
442//! - __secp256k1 Program__: Verifies secp256k1 public key recovery operations.
443//!   - ID: [`solana_sdk_ids::secp256k1_program::ID`](https://docs.rs/solana-sdk-ids/latest/solana_sdk_ids/secp256k1_program/constant.ID.html)
444//!   - Instruction: [`solana_secp256k1_program::new_secp256k1_instruction_with_signature`](https://docs.rs/solana-secp256k1-program/latest/solana_secp256k1_program/fn.new_secp256k1_instruction_with_signature.html)
445//!   - Invokable by programs? no
446//!
447//! - __BPF Loader__: Deploys, and executes immutable programs on the chain.
448//!   - ID: [`solana_sdk_ids::bpf_loader::ID`](https://docs.rs/solana-sdk-ids/latest/solana_sdk_ids/bpf_loader/constant.ID.html)
449//!   - Instruction: [`solana_loader_v2_interface::instruction`](https://docs.rs/solana-loader-v2-interface/latest/solana_loader_v2_interface/instruction/index.html)
450//!   - Invokable by programs? yes
451//!
452//! - __Upgradable BPF Loader__: Deploys, upgrades, and executes upgradable
453//!   programs on the chain.
454//!   - ID: [`solana_sdk_ids::bpf_loader_upgradeable::ID`](https://docs.rs/solana-sdk-ids/latest/solana_sdk_ids/bpf_loader_upgradeable/constant.ID.html)
455//!   - Instruction: [`solana_loader_v3_interface::instruction`](https://docs.rs/solana-loader-v3-interface/latest/solana_loader_v3_interface/instruction/index.html)
456//!   - Invokable by programs? yes
457//!
458//! - __Deprecated BPF Loader__: Deploys, and executes immutable programs on the
459//!   chain.
460//!   - ID: [`solana_sdk_ids::bpf_loader_deprecated::ID`](https://docs.rs/solana-sdk-ids/latest/solana_sdk_ids/bpf_loader_deprecated/constant.ID.html)
461//!   - Instruction: [`solana_loader_v2_interface::instruction`](https://docs.rs/solana-loader-v2-interface/latest/solana_loader_v2_interface/instruction/index.html)
462//!   - Invokable by programs? yes
463//!
464//! [lut]: https://docs.solanalabs.com/proposals/versioned-transactions
465
466#![allow(incomplete_features)]
467#![cfg_attr(feature = "frozen-abi", feature(specialization))]
468#![cfg_attr(docsrs, feature(doc_auto_cfg))]
469
470// Allows macro expansion of `use ::solana_program::*` to work within this crate
471extern crate self as solana_program;
472
473pub mod bpf_loader;
474pub mod bpf_loader_deprecated;
475pub mod compute_units;
476pub mod ed25519_program;
477pub mod entrypoint_deprecated;
478pub mod epoch_schedule;
479pub use solana_epoch_stake as epoch_stake;
480pub mod hash;
481pub mod incinerator;
482pub mod instruction;
483pub mod lamports;
484pub mod log;
485pub mod program;
486pub mod program_error;
487pub mod secp256k1_program;
488pub mod slot_hashes;
489pub mod slot_history;
490pub mod syscalls;
491pub mod sysvar;
492
493#[deprecated(since = "2.2.0", note = "Use `solana-big-mod-exp` crate instead")]
494pub use solana_big_mod_exp as big_mod_exp;
495#[deprecated(since = "2.2.0", note = "Use `solana-blake3-hasher` crate instead")]
496pub use solana_blake3_hasher as blake3;
497#[cfg(feature = "borsh")]
498#[deprecated(since = "2.1.0", note = "Use `solana-borsh` crate instead")]
499pub use solana_borsh::v1 as borsh1;
500#[deprecated(since = "2.1.0", note = "Use `solana-epoch-rewards` crate instead")]
501pub use solana_epoch_rewards as epoch_rewards;
502#[deprecated(since = "2.1.0", note = "Use `solana-fee-calculator` crate instead")]
503pub use solana_fee_calculator as fee_calculator;
504#[deprecated(since = "2.2.0", note = "Use `solana-keccak-hasher` crate instead")]
505pub use solana_keccak_hasher as keccak;
506#[deprecated(since = "2.1.0", note = "Use `solana-last-restart-slot` crate instead")]
507pub use solana_last_restart_slot as last_restart_slot;
508#[deprecated(since = "2.1.0", note = "Use `solana-program-memory` crate instead")]
509pub use solana_program_memory as program_memory;
510#[deprecated(since = "2.1.0", note = "Use `solana-program-pack` crate instead")]
511pub use solana_program_pack as program_pack;
512#[deprecated(since = "2.1.0", note = "Use `solana-secp256k1-recover` crate instead")]
513pub use solana_secp256k1_recover as secp256k1_recover;
514#[deprecated(since = "2.1.0", note = "Use `solana-serde-varint` crate instead")]
515pub use solana_serde_varint as serde_varint;
516#[deprecated(since = "2.1.0", note = "Use `solana-serialize-utils` crate instead")]
517pub use solana_serialize_utils as serialize_utils;
518#[deprecated(since = "2.1.0", note = "Use `solana-short-vec` crate instead")]
519pub use solana_short_vec as short_vec;
520#[deprecated(since = "2.1.0", note = "Use `solana-stable-layout` crate instead")]
521pub use solana_stable_layout as stable_layout;
522#[cfg(not(target_os = "solana"))]
523pub use solana_sysvar::program_stubs;
524pub use {
525    solana_account_info::{self as account_info, debug_account_data},
526    solana_clock as clock,
527    solana_msg::msg,
528    solana_native_token as native_token,
529    solana_program_entrypoint::{
530        self as entrypoint, custom_heap_default, custom_panic_default, entrypoint,
531        entrypoint_no_alloc,
532    },
533    solana_program_option as program_option, solana_pubkey as pubkey, solana_rent as rent,
534    solana_sysvar::impl_sysvar_get,
535};
536/// The [config native program][np].
537///
538/// [np]: https://docs.solanalabs.com/runtime/programs#config-program
539pub mod config {
540    pub mod program {
541        pub use solana_sdk_ids::config::{check_id, id, ID};
542    }
543}
544
545pub use solana_pubkey::{declare_deprecated_id, declare_id, pubkey};
546#[deprecated(since = "2.1.0", note = "Use `solana-sysvar-id` crate instead")]
547pub use solana_sysvar_id::{declare_deprecated_sysvar_id, declare_sysvar_id};
548
549/// Convenience macro for doing integer division where the operation's safety
550/// can be checked at compile-time.
551///
552/// Since `unchecked_div_by_const!()` is supposed to fail at compile-time, abuse
553/// doctests to cover failure modes
554///
555/// # Examples
556///
557/// Literal denominator div-by-zero fails:
558///
559/// ```compile_fail
560/// # use solana_program::unchecked_div_by_const;
561/// # fn main() {
562/// let _ = unchecked_div_by_const!(10, 0);
563/// # }
564/// ```
565///
566/// Const denominator div-by-zero fails:
567///
568/// ```compile_fail
569/// # use solana_program::unchecked_div_by_const;
570/// # fn main() {
571/// const D: u64 = 0;
572/// let _ = unchecked_div_by_const!(10, D);
573/// # }
574/// ```
575///
576/// Non-const denominator fails:
577///
578/// ```compile_fail
579/// # use solana_program::unchecked_div_by_const;
580/// # fn main() {
581/// let d = 0;
582/// let _ = unchecked_div_by_const!(10, d);
583/// # }
584/// ```
585///
586/// Literal denominator div-by-zero fails:
587///
588/// ```compile_fail
589/// # use solana_program::unchecked_div_by_const;
590/// # fn main() {
591/// const N: u64 = 10;
592/// let _ = unchecked_div_by_const!(N, 0);
593/// # }
594/// ```
595///
596/// Const denominator div-by-zero fails:
597///
598/// ```compile_fail
599/// # use solana_program::unchecked_div_by_const;
600/// # fn main() {
601/// const N: u64 = 10;
602/// const D: u64 = 0;
603/// let _ = unchecked_div_by_const!(N, D);
604/// # }
605/// ```
606///
607/// Non-const denominator fails:
608///
609/// ```compile_fail
610/// # use solana_program::unchecked_div_by_const;
611/// # fn main() {
612/// # const N: u64 = 10;
613/// let d = 0;
614/// let _ = unchecked_div_by_const!(N, d);
615/// # }
616/// ```
617///
618/// Literal denominator div-by-zero fails:
619///
620/// ```compile_fail
621/// # use solana_program::unchecked_div_by_const;
622/// # fn main() {
623/// let n = 10;
624/// let _ = unchecked_div_by_const!(n, 0);
625/// # }
626/// ```
627///
628/// Const denominator div-by-zero fails:
629///
630/// ```compile_fail
631/// # use solana_program::unchecked_div_by_const;
632/// # fn main() {
633/// let n = 10;
634/// const D: u64 = 0;
635/// let _ = unchecked_div_by_const!(n, D);
636/// # }
637/// ```
638///
639/// Non-const denominator fails:
640///
641/// ```compile_fail
642/// # use solana_program::unchecked_div_by_const;
643/// # fn main() {
644/// let n = 10;
645/// let d = 0;
646/// let _ = unchecked_div_by_const!(n, d);
647/// # }
648/// ```
649#[macro_export]
650macro_rules! unchecked_div_by_const {
651    ($num:expr, $den:expr) => {{
652        // Ensure the denominator is compile-time constant
653        let _ = [(); ($den - $den) as usize];
654        // Compile-time constant integer div-by-zero passes for some reason
655        // when invoked from a compilation unit other than that where this
656        // macro is defined. Do an explicit zero-check for now. Sorry about the
657        // ugly error messages!
658        // https://users.rust-lang.org/t/unexpected-behavior-of-compile-time-integer-div-by-zero-check-in-declarative-macro/56718
659        let _ = [(); ($den as usize) - 1];
660        #[allow(clippy::arithmetic_side_effects)]
661        let quotient = $num / $den;
662        quotient
663    }};
664}
665
666// This re-export is purposefully listed after all other exports: because of an
667// interaction within rustdoc between the reexports inside this module of
668// `solana_program`'s top-level modules, and `solana_sdk`'s glob re-export of
669// `solana_program`'s top-level modules, if this re-export is not lexically last
670// rustdoc fails to generate documentation for the re-exports within
671// `solana_sdk`.
672#[deprecated(since = "2.2.0", note = "Use solana-example-mocks instead")]
673#[cfg(not(target_os = "solana"))]
674pub use solana_example_mocks as example_mocks;
675
676#[cfg(test)]
677mod tests {
678    use super::unchecked_div_by_const;
679
680    #[test]
681    fn test_unchecked_div_by_const() {
682        const D: u64 = 2;
683        const N: u64 = 10;
684        let n = 10;
685        assert_eq!(unchecked_div_by_const!(10, 2), 5);
686        assert_eq!(unchecked_div_by_const!(N, 2), 5);
687        assert_eq!(unchecked_div_by_const!(n, 2), 5);
688        assert_eq!(unchecked_div_by_const!(10, D), 5);
689        assert_eq!(unchecked_div_by_const!(N, D), 5);
690        assert_eq!(unchecked_div_by_const!(n, D), 5);
691    }
692}