From 1cde3d5a8c939c2133db7beda110537445a5504e Mon Sep 17 00:00:00 2001 From: Kevin Heavey Date: Wed, 30 Oct 2024 02:00:43 +0400 Subject: [PATCH] Extract solana-nonce crate (#3082) * extract solana-nonce crate * activate solana-nonce/serde in solana-program * update lock file * post-rebase fixes * add docsrs metadata Co-authored-by: Jon C * add doc_auto_cfg * reorganise modules --------- Co-authored-by: Jon C --- Cargo.lock | 15 ++++++++ Cargo.toml | 2 + programs/sbf/Cargo.lock | 13 +++++++ sdk/nonce/Cargo.toml | 37 +++++++++++++++++++ .../src/nonce/mod.rs => nonce/src/lib.rs} | 3 +- .../state/current.rs => nonce/src/state.rs} | 21 ++++++----- .../state/mod.rs => nonce/src/versions.rs} | 16 ++++---- sdk/program/Cargo.toml | 1 + sdk/program/src/nonce.rs | 7 ++++ 9 files changed, 97 insertions(+), 18 deletions(-) create mode 100644 sdk/nonce/Cargo.toml rename sdk/{program/src/nonce/mod.rs => nonce/src/lib.rs} (60%) rename sdk/{program/src/nonce/state/current.rs => nonce/src/state.rs} (82%) rename sdk/{program/src/nonce/state/mod.rs => nonce/src/versions.rs} (96%) create mode 100644 sdk/program/src/nonce.rs diff --git a/Cargo.lock b/Cargo.lock index c1bd8072bbb30c..91a9f007a306ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7253,6 +7253,20 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b8a731ed60e89177c8a7ab05fe0f1511cedd3e70e773f288f9de33a9cfdc21e" +[[package]] +name = "solana-nonce" +version = "2.2.0" +dependencies = [ + "bincode", + "serde", + "serde_derive", + "solana-fee-calculator", + "solana-hash", + "solana-nonce", + "solana-pubkey", + "solana-sha256-hasher", +] + [[package]] name = "solana-notifier" version = "2.2.0" @@ -7444,6 +7458,7 @@ dependencies = [ "solana-logger", "solana-msg", "solana-native-token", + "solana-nonce", "solana-program-entrypoint", "solana-program-error", "solana-program-memory", diff --git a/Cargo.toml b/Cargo.toml index e49bbcb83f1473..af053d9745d203 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -122,6 +122,7 @@ members = [ "sdk/macro", "sdk/msg", "sdk/native-token", + "sdk/nonce", "sdk/package-metadata", "sdk/package-metadata-macro", "sdk/packet", @@ -453,6 +454,7 @@ solana-msg = { path = "sdk/msg", version = "=2.2.0" } solana-native-token = { path = "sdk/native-token", version = "=2.2.0" } solana-net-utils = { path = "net-utils", version = "=2.2.0" } solana-nohash-hasher = "0.2.1" +solana-nonce = { path = "sdk/nonce", version = "=2.2.0" } solana-notifier = { path = "notifier", version = "=2.2.0" } solana-package-metadata = { path = "sdk/package-metadata", version = "=2.2.0" } solana-package-metadata-macro = { path = "sdk/package-metadata-macro", version = "=2.2.0" } diff --git a/programs/sbf/Cargo.lock b/programs/sbf/Cargo.lock index f05690877ca2d3..ad5da64434aabd 100644 --- a/programs/sbf/Cargo.lock +++ b/programs/sbf/Cargo.lock @@ -5690,6 +5690,18 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b8a731ed60e89177c8a7ab05fe0f1511cedd3e70e773f288f9de33a9cfdc21e" +[[package]] +name = "solana-nonce" +version = "2.2.0" +dependencies = [ + "serde", + "serde_derive", + "solana-fee-calculator", + "solana-hash", + "solana-pubkey", + "solana-sha256-hasher", +] + [[package]] name = "solana-packet" version = "2.2.0" @@ -5809,6 +5821,7 @@ dependencies = [ "solana-last-restart-slot", "solana-msg", "solana-native-token", + "solana-nonce", "solana-program-entrypoint", "solana-program-error", "solana-program-memory", diff --git a/sdk/nonce/Cargo.toml b/sdk/nonce/Cargo.toml new file mode 100644 index 00000000000000..9b884176266037 --- /dev/null +++ b/sdk/nonce/Cargo.toml @@ -0,0 +1,37 @@ +[package] +name = "solana-nonce" +description = "Solana durable transaction nonces." +documentation = "https://docs.rs/solana-nonce" +version = { workspace = true } +authors = { workspace = true } +repository = { workspace = true } +homepage = { workspace = true } +license = { workspace = true } +edition = { workspace = true } + +[dependencies] +serde = { workspace = true, optional = true } +serde_derive = { workspace = true, optional = true } +solana-fee-calculator = { workspace = true } +solana-hash = { workspace = true, default-features = false } +solana-pubkey = { workspace = true, default-features = false } +solana-sha256-hasher = { workspace = true } + +[dev-dependencies] +bincode = { workspace = true } +solana-nonce = { path = ".", features = ["dev-context-only-utils"] } + +[features] +dev-context-only-utils = ["serde"] +serde = [ + "dep:serde", + "dep:serde_derive", + "solana-fee-calculator/serde", + "solana-hash/serde", + "solana-pubkey/serde", +] + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] +all-features = true +rustdoc-args = ["--cfg=docsrs"] diff --git a/sdk/program/src/nonce/mod.rs b/sdk/nonce/src/lib.rs similarity index 60% rename from sdk/program/src/nonce/mod.rs rename to sdk/nonce/src/lib.rs index 61bbefa5a9edb6..024b0b717b1e3c 100644 --- a/sdk/program/src/nonce/mod.rs +++ b/sdk/nonce/src/lib.rs @@ -1,6 +1,7 @@ +#![cfg_attr(docsrs, feature(doc_auto_cfg))] //! Durable transaction nonces. pub mod state; -pub use state::State; +pub mod versions; pub const NONCED_TX_MARKER_IX_INDEX: u8 = 0; diff --git a/sdk/program/src/nonce/state/current.rs b/sdk/nonce/src/state.rs similarity index 82% rename from sdk/program/src/nonce/state/current.rs rename to sdk/nonce/src/state.rs index 447cdff97f50bf..32450a39025eb0 100644 --- a/sdk/program/src/nonce/state/current.rs +++ b/sdk/nonce/src/state.rs @@ -1,21 +1,21 @@ +#[cfg(feature = "serde")] +use serde_derive::{Deserialize, Serialize}; use { - crate::{ - fee_calculator::FeeCalculator, - hash::{hashv, Hash}, - pubkey::Pubkey, - }, - serde_derive::{Deserialize, Serialize}, + solana_fee_calculator::FeeCalculator, solana_hash::Hash, solana_pubkey::Pubkey, + solana_sha256_hasher::hashv, }; const DURABLE_NONCE_HASH_PREFIX: &[u8] = "DURABLE_NONCE".as_bytes(); -#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Deserialize, Serialize)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] pub struct DurableNonce(Hash); /// Initialized data of a durable transaction nonce account. /// /// This is stored within [`State`] for initialized nonce accounts. -#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq, Clone)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +#[derive(Debug, Default, PartialEq, Eq, Clone)] pub struct Data { /// Address of the account that signs transactions using the nonce account. pub authority: Pubkey, @@ -67,7 +67,8 @@ impl DurableNonce { /// /// When created in memory with [`State::default`] or when deserialized from an /// uninitialized account, a nonce account will be [`State::Uninitialized`]. -#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq, Clone)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +#[derive(Debug, Default, PartialEq, Eq, Clone)] pub enum State { #[default] Uninitialized, @@ -92,7 +93,7 @@ impl State { #[cfg(test)] mod test { - use {super::*, crate::nonce::state::Versions}; + use {super::*, crate::versions::Versions}; #[test] fn default_is_uninitialized() { diff --git a/sdk/program/src/nonce/state/mod.rs b/sdk/nonce/src/versions.rs similarity index 96% rename from sdk/program/src/nonce/state/mod.rs rename to sdk/nonce/src/versions.rs index d55bc9063afcff..6b6dfa78cc7aea 100644 --- a/sdk/program/src/nonce/state/mod.rs +++ b/sdk/nonce/src/versions.rs @@ -1,14 +1,17 @@ //! State for durable transaction nonces. -mod current; -pub use current::{Data, DurableNonce, State}; use { - crate::{hash::Hash, pubkey::Pubkey}, - serde_derive::{Deserialize, Serialize}, + crate::state::{Data, DurableNonce, State}, + solana_hash::Hash, + solana_pubkey::Pubkey, std::collections::HashSet, }; -#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] +#[cfg_attr( + feature = "serde", + derive(serde_derive::Deserialize, serde_derive::Serialize) +)] +#[derive(Debug, PartialEq, Eq, Clone)] pub enum Versions { Legacy(Box), /// Current variants have durable nonce and blockhash domains separated. @@ -114,8 +117,7 @@ impl From for State { #[cfg(test)] mod tests { use { - super::*, - crate::{fee_calculator::FeeCalculator, pubkey::Pubkey}, + super::*, solana_fee_calculator::FeeCalculator, solana_pubkey::Pubkey, std::iter::repeat_with, }; diff --git a/sdk/program/Cargo.toml b/sdk/program/Cargo.toml index 37e88e9d5629f1..4252838b59c10f 100644 --- a/sdk/program/Cargo.toml +++ b/sdk/program/Cargo.toml @@ -56,6 +56,7 @@ solana-instruction = { workspace = true, default-features = false, features = [ solana-last-restart-slot = { workspace = true, features = ["serde", "sysvar"] } solana-msg = { workspace = true } solana-native-token = { workspace = true } +solana-nonce = { workspace = true, features = ["serde"] } solana-program-entrypoint = { workspace = true } solana-program-error = { workspace = true, features = ["serde"] } solana-program-memory = { workspace = true } diff --git a/sdk/program/src/nonce.rs b/sdk/program/src/nonce.rs new file mode 100644 index 00000000000000..5dc9f571f1468a --- /dev/null +++ b/sdk/program/src/nonce.rs @@ -0,0 +1,7 @@ +pub use solana_nonce::{state::State, NONCED_TX_MARKER_IX_INDEX}; +pub mod state { + pub use solana_nonce::{ + state::{Data, DurableNonce, State}, + versions::{AuthorizeNonceError, Versions}, + }; +}