Skip to content

Commit

Permalink
sdk: Extract solana-sysvar-id crate (anza-xyz#3309)
Browse files Browse the repository at this point in the history
* sdk: Extract `solana-sysvar` crate

#### Problem

The sysvars in `solana-program` are tightly coupled with types that
exist in `solana-program`. For example, all of the special sysvar
getters like `Rent::get()` are implemented through a macro that falls
back to using `program_stubs`.

Because of this tight coupling, it's difficult to pull out bits for the
sysvars.

#### Summary of changes

After numerous attempts, I've decided to keep it simple and only extract
`SysvarId`, its helper macros, and `get_sysvar`.

To go along with that, all of the separated sysvar crates now implement
the sysvar ids themselves under a new `sysvar` feature. This new feature
might be overkill, so let me know if we should just include the sysvar
ids by default. I went with a feature to include an implementation using
the `sol_get_sysvar` syscall in the future.

It was really messy to include the `Sysvar` trait from `solana-program`
because it falls back to using `bincode`, which we know performs poorly
for on-chain programs. So the future idea is to create a new `Sysvar`
trait in `solana-sysvar` which will require fewer bits to deserialize
sysvars.

Let me know what you think about this PR and the future idea! Note that
I'll need to rebase this on top of anza-xyz#3249 and anza-xyz#3272 when they land.

* Add solana-define-syscall for solana builds

* Rename solana-sysvar -> solana-sysvar-id

* Move back `get_sysvar` to solana-program

* Update lockfile for v2.2
  • Loading branch information
joncinque authored and ray-kast committed Nov 27, 2024
1 parent e7ce07f commit d4bcee7
Show file tree
Hide file tree
Showing 38 changed files with 251 additions and 100 deletions.
16 changes: 16 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 @@ -142,6 +142,7 @@ members = [
"sdk/slot-hashes",
"sdk/slot-history",
"sdk/stable-layout",
"sdk/sysvar-id",
"sdk/transaction-error",
"send-transaction-service",
"short-vec",
Expand Down Expand Up @@ -505,6 +506,7 @@ solana-svm-example-paytube = { path = "svm/examples/paytube", version = "=2.2.0"
solana-svm-rent-collector = { path = "svm-rent-collector", version = "=2.2.0" }
solana-svm-transaction = { path = "svm-transaction", version = "=2.2.0" }
solana-system-program = { path = "programs/system", version = "=2.2.0" }
solana-sysvar-id = { path = "sdk/sysvar-id", version = "=2.2.0" }
solana-test-validator = { path = "test-validator", version = "=2.2.0" }
solana-thin-client = { path = "thin-client", version = "=2.2.0" }
solana-transaction-error = { path = "sdk/transaction-error", version = "=2.2.0" }
Expand Down
14 changes: 14 additions & 0 deletions programs/sbf/Cargo.lock

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

5 changes: 5 additions & 0 deletions sdk/clock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ edition = { workspace = true }
serde = { workspace = true, optional = true }
serde_derive = { workspace = true, optional = true }
solana-sdk-macro = { workspace = true }
solana-sysvar-id = { workspace = true, optional = true }

[dev-dependencies]
solana-clock = { path = ".", features = ["sysvar"] }
static_assertions = { workspace = true }

[features]
serde = ["dep:serde", "dep:serde_derive"]
sysvar = ["dep:solana-sysvar-id"]

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
all-features = true
rustdoc-args = ["--cfg=docsrs"]
4 changes: 4 additions & 0 deletions sdk/clock/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
//!
//! [oracle]: https://docs.solanalabs.com/implemented-proposals/validator-timestamp-oracle
#![no_std]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]

#[cfg(feature = "sysvar")]
pub mod sysvar;

#[cfg(feature = "serde")]
use serde_derive::{Deserialize, Serialize};
Expand Down
3 changes: 3 additions & 0 deletions sdk/clock/src/sysvar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use {crate::Clock, solana_sysvar_id::declare_sysvar_id};

declare_sysvar_id!("SysvarC1ock11111111111111111111111111111111", Clock);
3 changes: 3 additions & 0 deletions sdk/epoch-schedule/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ serde_derive = { workspace = true, optional = true }
solana-frozen-abi = { workspace = true, optional = true }
solana-frozen-abi-macro = { workspace = true, optional = true }
solana-sdk-macro = { workspace = true }
solana-sysvar-id = { workspace = true, optional = true }

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dev-dependencies]
solana-clock = { workspace = true }
solana-epoch-schedule = { path = ".", features = ["sysvar"] }
static_assertions = { workspace = true }

[features]
frozen-abi = ["dep:solana-frozen-abi", "dep:solana-frozen-abi-macro"]
serde = ["dep:serde", "dep:serde_derive"]
sysvar = ["dep:solana-sysvar-id"]

[lints]
workspace = true
3 changes: 3 additions & 0 deletions sdk/epoch-schedule/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#[cfg(feature = "frozen-abi")]
extern crate std;

#[cfg(feature = "sysvar")]
pub mod sysvar;

#[cfg(feature = "serde")]
use serde_derive::{Deserialize, Serialize};
use solana_sdk_macro::CloneZeroed;
Expand Down
3 changes: 3 additions & 0 deletions sdk/epoch-schedule/src/sysvar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use {crate::EpochSchedule, solana_sysvar_id::declare_sysvar_id};

declare_sysvar_id!("SysvarEpochSchedu1e111111111111111111111111", EpochSchedule);
2 changes: 2 additions & 0 deletions sdk/last-restart-slot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ edition = { workspace = true }
serde = { workspace = true, optional = true }
serde_derive = { workspace = true, optional = true }
solana-sdk-macro = { workspace = true }
solana-sysvar-id = { workspace = true, optional = true }

[features]
serde = ["dep:serde", "dep:serde_derive"]
sysvar = ["dep:solana-sysvar-id"]

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
Expand Down
3 changes: 3 additions & 0 deletions sdk/last-restart-slot/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Information about the last restart slot (hard fork).
#![cfg_attr(docsrs, feature(doc_auto_cfg))]

#[cfg(feature = "sysvar")]
pub mod sysvar;

use solana_sdk_macro::CloneZeroed;

#[repr(C)]
Expand Down
6 changes: 6 additions & 0 deletions sdk/last-restart-slot/src/sysvar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use {crate::LastRestartSlot, solana_sysvar_id::declare_sysvar_id};

declare_sysvar_id!(
"SysvarLastRestartS1ot1111111111111111111111",
LastRestartSlot
);
13 changes: 7 additions & 6 deletions sdk/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ solana-account-info = { workspace = true, features = ["bincode"] }
solana-atomic-u64 = { workspace = true }
solana-bincode = { workspace = true }
solana-borsh = { workspace = true, optional = true }
solana-clock = { workspace = true, features = ["serde"] }
solana-clock = { workspace = true, features = ["serde", "sysvar"] }
solana-cpi = { workspace = true }
solana-decode-error = { workspace = true }
solana-epoch-schedule = { workspace = true, features = ["serde"] }
solana-epoch-schedule = { workspace = true, features = ["serde", "sysvar"] }
solana-fee-calculator = { workspace = true, features = ["serde"] }
solana-frozen-abi = { workspace = true, optional = true, features = ["frozen-abi"] }
solana-frozen-abi-macro = { workspace = true, optional = true, features = ["frozen-abi"] }
Expand All @@ -53,7 +53,7 @@ solana-instruction = { workspace = true, default-features = false, features = [
"serde",
"std",
] }
solana-last-restart-slot = { workspace = true, features = ["serde"] }
solana-last-restart-slot = { workspace = true, features = ["serde", "sysvar"] }
solana-msg = { workspace = true }
solana-native-token = { workspace = true }
solana-program-entrypoint = { workspace = true }
Expand All @@ -62,17 +62,18 @@ solana-program-memory = { workspace = true }
solana-program-option = { workspace = true }
solana-program-pack = { workspace = true }
solana-pubkey = { workspace = true, features = ["bytemuck", "curve25519", "serde", "std"] }
solana-rent = { workspace = true, features = ["serde"] }
solana-rent = { workspace = true, features = ["serde", "sysvar"] }
solana-sanitize = { workspace = true }
solana-sdk-macro = { workspace = true }
solana-secp256k1-recover = { workspace = true }
solana-serde-varint = { workspace = true }
solana-serialize-utils = { workspace = true }
solana-sha256-hasher = { workspace = true, features = ["sha2"] }
solana-short-vec = { workspace = true }
solana-slot-hashes = { workspace = true, features = ["serde"] }
solana-slot-history = { workspace = true, features = ["serde"] }
solana-slot-hashes = { workspace = true, features = ["serde", "sysvar"] }
solana-slot-history = { workspace = true, features = ["serde", "sysvar"] }
solana-stable-layout = { workspace = true }
solana-sysvar-id = { workspace = true }
thiserror = { workspace = true }

# This is currently needed to build on-chain programs reliably.
Expand Down
2 changes: 2 additions & 0 deletions sdk/program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,8 @@ pub mod sdk_ids {
#[deprecated(since = "2.1.0", note = "Use `solana-decode-error` crate instead")]
pub use solana_decode_error as decode_error;
pub use solana_pubkey::{declare_deprecated_id, declare_id, pubkey};
#[deprecated(since = "2.1.0", note = "Use `solana-sysvar-id` crate instead")]
pub use solana_sysvar_id::{declare_deprecated_sysvar_id, declare_sysvar_id};

#[macro_use]
extern crate serde_derive;
Expand Down
11 changes: 6 additions & 5 deletions sdk/program/src/sysvar/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
//! Ok(())
//! }
//! #
//! # use solana_program::sysvar::SysvarId;
//! # use solana_sysvar_id::SysvarId;
//! # let p = Clock::id();
//! # let l = &mut 1169280;
//! # let d = &mut vec![240, 153, 233, 7, 0, 0, 0, 0, 11, 115, 118, 98, 0, 0, 0, 0, 51, 1, 0, 0, 0, 0, 0, 0, 52, 1, 0, 0, 0, 0, 0, 0, 121, 50, 119, 98, 0, 0, 0, 0];
Expand Down Expand Up @@ -81,7 +81,7 @@
//! Ok(())
//! }
//! #
//! # use solana_program::sysvar::SysvarId;
//! # use solana_sysvar_id::SysvarId;
//! # let p = Clock::id();
//! # let l = &mut 1169280;
//! # let d = &mut vec![240, 153, 233, 7, 0, 0, 0, 0, 11, 115, 118, 98, 0, 0, 0, 0, 51, 1, 0, 0, 0, 0, 0, 0, 52, 1, 0, 0, 0, 0, 0, 0, 121, 50, 119, 98, 0, 0, 0, 0];
Expand Down Expand Up @@ -127,9 +127,10 @@
//! ```
use crate::{impl_sysvar_get, program_error::ProgramError, sysvar::Sysvar};
pub use solana_clock::Clock;

crate::declare_sysvar_id!("SysvarC1ock11111111111111111111111111111111", Clock);
pub use solana_clock::{
sysvar::{check_id, id, ID},
Clock,
};

impl Sysvar for Clock {
impl_sysvar_get!(sol_get_clock_sysvar);
Expand Down
11 changes: 7 additions & 4 deletions sdk/program/src/sysvar/epoch_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
//! Ok(())
//! }
//! #
//! # use solana_program::sysvar::SysvarId;
//! # use solana_sysvar_id::SysvarId;
//! # let p = EpochRewards::id();
//! # let l = &mut 1559040;
//! # let epoch_rewards = EpochRewards {
Expand Down Expand Up @@ -99,7 +99,7 @@
//! Ok(())
//! }
//! #
//! # use solana_program::sysvar::SysvarId;
//! # use solana_sysvar_id::SysvarId;
//! # let p = EpochRewards::id();
//! # let l = &mut 1559040;
//! # let epoch_rewards = EpochRewards {
Expand Down Expand Up @@ -160,9 +160,12 @@
//! ```
pub use crate::epoch_rewards::EpochRewards;
use crate::{impl_sysvar_get, program_error::ProgramError, sysvar::Sysvar};
use {
crate::{impl_sysvar_get, program_error::ProgramError, sysvar::Sysvar},
solana_sysvar_id::declare_sysvar_id,
};

crate::declare_sysvar_id!("SysvarEpochRewards1111111111111111111111111", EpochRewards);
declare_sysvar_id!("SysvarEpochRewards1111111111111111111111111", EpochRewards);

impl Sysvar for EpochRewards {
impl_sysvar_get!(sol_get_epoch_rewards_sysvar);
Expand Down
Loading

0 comments on commit d4bcee7

Please sign in to comment.