Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract solana-nonce crate #3082

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,13 @@ members = [
"sdk/derivation-path",
"sdk/epoch-schedule",
"sdk/feature-set",
"sdk/fee-calculator",
"sdk/gen-headers",
"sdk/hash",
"sdk/instruction",
"sdk/macro",
"sdk/msg",
"sdk/nonce",
"sdk/package-metadata-macro",
"sdk/program",
"sdk/program-error",
Expand Down Expand Up @@ -407,6 +409,7 @@ solana-entry = { path = "entry", version = "=2.1.0" }
solana-epoch-schedule = { path = "sdk/epoch-schedule", version = "=2.1.0" }
solana-faucet = { path = "faucet", version = "=2.1.0" }
solana-feature-set = { path = "sdk/feature-set", version = "=2.1.0" }
solana-fee-calculator = { path = "sdk/fee-calculator", version = "=2.1.0" }
solana-fee = { path = "fee", version = "=2.1.0" }
solana-frozen-abi = { path = "frozen-abi", version = "=2.1.0" }
solana-frozen-abi-macro = { path = "frozen-abi/macro", version = "=2.1.0" }
Expand All @@ -431,6 +434,7 @@ solana-metrics = { path = "metrics", version = "=2.1.0" }
solana-msg = { path = "sdk/msg", version = "=2.1.0" }
solana-net-utils = { path = "net-utils", version = "=2.1.0" }
solana-nohash-hasher = "0.2.1"
solana-nonce = { path = "sdk/nonce", version = "=2.1.0" }
solana-notifier = { path = "notifier", version = "=2.1.0" }
solana-package-metadata-macro = { path = "sdk/package-metadata-macro", version = "=2.1.0" }
solana-perf = { path = "perf", version = "=2.1.0" }
Expand Down
2 changes: 1 addition & 1 deletion accounts-db/src/blockhash_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl HashInfo {
#[cfg_attr(
feature = "frozen-abi",
derive(AbiExample),
frozen_abi(digest = "BxykY65dC2NCcDm17rHQPjEY8wK55sKAhfhKVFGc5T1u")
frozen_abi(digest = "2GFWjonjAdte2KsJthPzFdSvVKJ4viKYTPzUHB8dzjtE")
)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct BlockhashQueue {
Expand Down
23 changes: 23 additions & 0 deletions programs/sbf/Cargo.lock

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

2 changes: 1 addition & 1 deletion runtime/src/bank/serde_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ mod tests {
#[cfg_attr(
feature = "frozen-abi",
derive(AbiExample),
frozen_abi(digest = "D7zx9HfzJa1cqhNariHYufgyLUVLR64iPoMFzRYqs8rZ")
frozen_abi(digest = "2G3gi9LAN7w45KNu4GffLfUUCTQLcChzrvSp7ah3Awbv")
)]
#[derive(Serialize)]
pub struct BankAbiTestWrapper {
Expand Down
29 changes: 29 additions & 0 deletions sdk/fee-calculator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "solana-fee-calculator"
description = "Solana transaction fee calculation"
documentation = "https://docs.rs/solana-fee-calculator"
version = { workspace = true }
authors = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
edition = { workspace = true }

[dependencies]
log = { workspace = true }
serde = { workspace = true, optional = true }
serde_derive = { workspace = true, optional = true }
solana-frozen-abi = { workspace = true, optional = true }
solana-frozen-abi-macro = { workspace = true, optional = true }

[dev-dependencies]
solana-clock = { workspace = true }
solana-logger = { workspace = true }
static_assertions = { workspace = true }

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

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
39 changes: 27 additions & 12 deletions sdk/program/src/fee_calculator.rs → sdk/fee-calculator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
//! Calculation of transaction fees.

#![cfg_attr(feature = "frozen-abi", feature(min_specialization))]
#![allow(clippy::arithmetic_side_effects)]
use {log::*, solana_clock::DEFAULT_MS_PER_SLOT};
#![no_std]

use log::*;
#[cfg(feature = "frozen-abi")]
extern crate std;

#[repr(C)]
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
#[derive(Serialize, Deserialize, Default, PartialEq, Eq, Clone, Copy, Debug)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "frozen-abi", derive(solana_frozen_abi_macro::AbiExample))]
#[cfg_attr(
feature = "serde",
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[derive(Default, PartialEq, Eq, Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct FeeCalculator {
/// The current cost of a signature.
///
Expand All @@ -23,13 +31,17 @@ impl FeeCalculator {
}
}

#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "frozen-abi", derive(solana_frozen_abi_macro::AbiExample))]
#[cfg_attr(
feature = "serde",
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[derive(PartialEq, Eq, Clone, Debug)]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct FeeRateGovernor {
// The current cost of a signature This amount may increase/decrease over time based on
// cluster processing load.
#[serde(skip)]
#[cfg_attr(feature = "serde", serde(skip))]
pub lamports_per_signature: u64,

// The target cost of a signature when the cluster is operating around target_signatures_per_slot
Expand All @@ -49,6 +61,9 @@ pub struct FeeRateGovernor {
}

pub const DEFAULT_TARGET_LAMPORTS_PER_SIGNATURE: u64 = 10_000;
const DEFAULT_MS_PER_SLOT: u64 = 400;
#[cfg(test)]
static_assertions::const_assert_eq!(DEFAULT_MS_PER_SLOT, solana_clock::DEFAULT_MS_PER_SLOT);
pub const DEFAULT_TARGET_SIGNATURES_PER_SLOT: u64 = 50 * DEFAULT_MS_PER_SLOT;

// Percentage of tx fees to burn
Expand Down Expand Up @@ -88,15 +103,15 @@ impl FeeRateGovernor {
if me.target_signatures_per_slot > 0 {
// lamports_per_signature can range from 50% to 1000% of
// target_lamports_per_signature
me.min_lamports_per_signature = std::cmp::max(1, me.target_lamports_per_signature / 2);
me.min_lamports_per_signature = core::cmp::max(1, me.target_lamports_per_signature / 2);
me.max_lamports_per_signature = me.target_lamports_per_signature * 10;

// What the cluster should charge at `latest_signatures_per_slot`
let desired_lamports_per_signature =
me.max_lamports_per_signature
.min(me.min_lamports_per_signature.max(
me.target_lamports_per_signature
* std::cmp::min(latest_signatures_per_slot, u32::MAX as u64)
* core::cmp::min(latest_signatures_per_slot, u32::MAX as u64)
/ me.target_signatures_per_slot,
));

Expand All @@ -114,7 +129,7 @@ impl FeeRateGovernor {
// Adjust fee by 5% of target_lamports_per_signature to produce a smooth
// increase/decrease in fees over time.
let gap_adjust =
std::cmp::max(1, me.target_lamports_per_signature / 20) as i64 * gap.signum();
core::cmp::max(1, me.target_lamports_per_signature / 20) as i64 * gap.signum();

trace!(
"lamports_per_signature gap is {}, adjusting by {}",
Expand Down
35 changes: 35 additions & 0 deletions sdk/nonce/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[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"]
File renamed without changes.
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::state::Versions};

#[test]
fn default_is_uninitialized() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

mod current;
pub use current::{Data, DurableNonce, State};
use {
crate::{hash::Hash, pubkey::Pubkey},
serde_derive::{Deserialize, Serialize},
std::collections::HashSet,
};
use {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 +114,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
Loading
Loading