Skip to content

Commit

Permalink
Extract solana-nonce crate (#3082)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* add doc_auto_cfg

* reorganise modules

---------

Co-authored-by: Jon C <[email protected]>
  • Loading branch information
kevinheavey and joncinque authored Oct 29, 2024
1 parent a17693d commit 1cde3d5
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 18 deletions.
15 changes: 15 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 @@ -122,6 +122,7 @@ members = [
"sdk/macro",
"sdk/msg",
"sdk/native-token",
"sdk/nonce",
"sdk/package-metadata",
"sdk/package-metadata-macro",
"sdk/packet",
Expand Down Expand Up @@ -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" }
Expand Down
13 changes: 13 additions & 0 deletions programs/sbf/Cargo.lock

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

37 changes: 37 additions & 0 deletions sdk/nonce/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"]
3 changes: 2 additions & 1 deletion sdk/program/src/nonce/mod.rs → sdk/nonce/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
21 changes: 11 additions & 10 deletions sdk/program/src/nonce/state/current.rs → sdk/nonce/src/state.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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() {
Expand Down
16 changes: 9 additions & 7 deletions sdk/program/src/nonce/state/mod.rs → sdk/nonce/src/versions.rs
Original file line number Diff line number Diff line change
@@ -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<State>),
/// Current variants have durable nonce and blockhash domains separated.
Expand Down Expand Up @@ -114,8 +117,7 @@ impl From<Versions> 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,
};

Expand Down
1 change: 1 addition & 0 deletions sdk/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
7 changes: 7 additions & 0 deletions sdk/program/src/nonce.rs
Original file line number Diff line number Diff line change
@@ -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},
};
}

0 comments on commit 1cde3d5

Please sign in to comment.