Merge branch 'main' into p2p-request-handler
Some checks are pending
Audit / audit (push) Waiting to run
Deny / audit (push) Waiting to run

This commit is contained in:
Boog900 2024-10-31 15:56:37 +00:00
commit d982a48b78
No known key found for this signature in database
GPG key ID: 42AB1287CB0041C2
71 changed files with 802 additions and 477 deletions

55
Cargo.lock generated
View file

@ -56,6 +56,18 @@ version = "1.0.89"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6" checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6"
[[package]]
name = "arrayref"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76a2e8124351fda1ef8aaaa3bbd7ebbcb486bbcd4225aca0aa0d84bb2db8fecb"
[[package]]
name = "arrayvec"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50"
[[package]] [[package]]
name = "async-stream" name = "async-stream"
version = "0.3.5" version = "0.3.5"
@ -238,6 +250,19 @@ dependencies = [
"digest", "digest",
] ]
[[package]]
name = "blake3"
version = "1.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d82033247fd8e890df8f740e407ad4d038debb9eb1f40533fffb32e7d17dc6f7"
dependencies = [
"arrayref",
"arrayvec",
"cc",
"cfg-if",
"constant_time_eq",
]
[[package]] [[package]]
name = "block-buffer" name = "block-buffer"
version = "0.10.4" version = "0.10.4"
@ -403,6 +428,12 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "constant_time_eq"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c74b8349d32d297c9134b8c88677813a227df8f779daa29bfc29c183fe3dca6"
[[package]] [[package]]
name = "core-foundation" name = "core-foundation"
version = "0.9.4" version = "0.9.4"
@ -576,6 +607,7 @@ name = "cuprate-consensus"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"cuprate-consensus-context",
"cuprate-consensus-rules", "cuprate-consensus-rules",
"cuprate-helper", "cuprate-helper",
"cuprate-test-utils", "cuprate-test-utils",
@ -588,12 +620,30 @@ dependencies = [
"proptest", "proptest",
"proptest-derive", "proptest-derive",
"rand", "rand",
"randomx-rs",
"rayon", "rayon",
"thiserror", "thiserror",
"thread_local", "thread_local",
"tokio", "tokio",
"tokio-test", "tokio-test",
"tower 0.5.1",
"tracing",
]
[[package]]
name = "cuprate-consensus-context"
version = "0.1.0"
dependencies = [
"cuprate-consensus-rules",
"cuprate-helper",
"cuprate-types",
"futures",
"hex",
"monero-serai",
"randomx-rs",
"rayon",
"thiserror",
"thread_local",
"tokio",
"tokio-util", "tokio-util",
"tower 0.5.1", "tower 0.5.1",
"tracing", "tracing",
@ -705,6 +755,7 @@ dependencies = [
"clap", "clap",
"cuprate-blockchain", "cuprate-blockchain",
"cuprate-consensus", "cuprate-consensus",
"cuprate-consensus-context",
"cuprate-consensus-rules", "cuprate-consensus-rules",
"cuprate-helper", "cuprate-helper",
"cuprate-types", "cuprate-types",
@ -900,6 +951,7 @@ name = "cuprate-txpool"
version = "0.0.0" version = "0.0.0"
dependencies = [ dependencies = [
"bitflags 2.6.0", "bitflags 2.6.0",
"blake3",
"bytemuck", "bytemuck",
"cuprate-database", "cuprate-database",
"cuprate-database-service", "cuprate-database-service",
@ -974,6 +1026,7 @@ dependencies = [
"cuprate-async-buffer", "cuprate-async-buffer",
"cuprate-blockchain", "cuprate-blockchain",
"cuprate-consensus", "cuprate-consensus",
"cuprate-consensus-context",
"cuprate-consensus-rules", "cuprate-consensus-rules",
"cuprate-cryptonight", "cuprate-cryptonight",
"cuprate-dandelion-tower", "cuprate-dandelion-tower",

View file

@ -5,6 +5,7 @@ members = [
"binaries/cuprated", "binaries/cuprated",
"constants", "constants",
"consensus", "consensus",
"consensus/context",
"consensus/fast-sync", "consensus/fast-sync",
"consensus/rules", "consensus/rules",
"cryptonight", "cryptonight",
@ -49,9 +50,39 @@ opt-level = 1
opt-level = 3 opt-level = 3
[workspace.dependencies] [workspace.dependencies]
# Cuprate members
cuprate-fast-sync = { path = "consensus/fast-sync" ,default-features = false}
cuprate-consensus-rules = { path = "consensus/rules" ,default-features = false}
cuprate-constants = { path = "constants" ,default-features = false}
cuprate-consensus = { path = "consensus" ,default-features = false}
cuprate-consensus-context = { path = "consensus/context" ,default-features = false}
cuprate-cryptonight = { path = "cryptonight" ,default-features = false}
cuprate-helper = { path = "helper" ,default-features = false}
cuprate-epee-encoding = { path = "net/epee-encoding" ,default-features = false}
cuprate-fixed-bytes = { path = "net/fixed-bytes" ,default-features = false}
cuprate-levin = { path = "net/levin" ,default-features = false}
cuprate-wire = { path = "net/wire" ,default-features = false}
cuprate-p2p = { path = "p2p/p2p" ,default-features = false}
cuprate-p2p-core = { path = "p2p/p2p-core" ,default-features = false}
cuprate-dandelion-tower = { path = "p2p/dandelion-tower" ,default-features = false}
cuprate-async-buffer = { path = "p2p/async-buffer" ,default-features = false}
cuprate-address-book = { path = "p2p/address-book" ,default-features = false}
cuprate-blockchain = { path = "storage/blockchain" ,default-features = false}
cuprate-database = { path = "storage/database" ,default-features = false}
cuprate-database-service = { path = "storage/service" ,default-features = false}
cuprate-txpool = { path = "storage/txpool" ,default-features = false}
cuprate-pruning = { path = "pruning" ,default-features = false}
cuprate-test-utils = { path = "test-utils" ,default-features = false}
cuprate-types = { path = "types" ,default-features = false}
cuprate-json-rpc = { path = "rpc/json-rpc" ,default-features = false}
cuprate-rpc-types = { path = "rpc/types" ,default-features = false}
cuprate-rpc-interface = { path = "rpc/interface" ,default-features = false}
# External dependencies
anyhow = { version = "1.0.89", default-features = false } anyhow = { version = "1.0.89", default-features = false }
async-trait = { version = "0.1.82", default-features = false } async-trait = { version = "0.1.82", default-features = false }
bitflags = { version = "2.6.0", default-features = false } bitflags = { version = "2.6.0", default-features = false }
blake3 = { version = "1", default-features = false }
borsh = { version = "1.5.1", default-features = false } borsh = { version = "1.5.1", default-features = false }
bytemuck = { version = "1.18.0", default-features = false } bytemuck = { version = "1.18.0", default-features = false }
bytes = { version = "1.7.2", default-features = false } bytes = { version = "1.7.2", default-features = false }
@ -323,4 +354,4 @@ non_camel_case_types = "deny"
# unused_results = "deny" # unused_results = "deny"
# non_exhaustive_omitted_patterns = "deny" # non_exhaustive_omitted_patterns = "deny"
# missing_docs = "deny" # missing_docs = "deny"
# missing_copy_implementations = "deny" # missing_copy_implementations = "deny"

View file

@ -9,30 +9,31 @@ repository = "https://github.com/Cuprate/cuprate/tree/main/binaries/cuprated"
[dependencies] [dependencies]
# TODO: after v1.0.0, remove unneeded dependencies. # TODO: after v1.0.0, remove unneeded dependencies.
cuprate-consensus = { path = "../../consensus" } cuprate-consensus = { workspace = true }
cuprate-fast-sync = { path = "../../consensus/fast-sync" } cuprate-fast-sync = { workspace = true }
cuprate-consensus-rules = { path = "../../consensus/rules" } cuprate-consensus-context = { workspace = true }
cuprate-cryptonight = { path = "../../cryptonight" } cuprate-consensus-rules = { workspace = true }
cuprate-helper = { path = "../../helper" } cuprate-cryptonight = { workspace = true }
cuprate-epee-encoding = { path = "../../net/epee-encoding" } cuprate-helper = { workspace = true }
cuprate-fixed-bytes = { path = "../../net/fixed-bytes" } cuprate-epee-encoding = { workspace = true }
cuprate-levin = { path = "../../net/levin" } cuprate-fixed-bytes = { workspace = true }
cuprate-wire = { path = "../../net/wire" } cuprate-levin = { workspace = true }
cuprate-p2p = { path = "../../p2p/p2p" } cuprate-wire = { workspace = true }
cuprate-p2p-core = { path = "../../p2p/p2p-core" } cuprate-p2p = { workspace = true }
cuprate-dandelion-tower = { path = "../../p2p/dandelion-tower", features = ["txpool"] } cuprate-p2p-core = { workspace = true }
cuprate-async-buffer = { path = "../../p2p/async-buffer" } cuprate-dandelion-tower = { workspace = true, features = ["txpool"] }
cuprate-address-book = { path = "../../p2p/address-book" } cuprate-async-buffer = { workspace = true }
cuprate-blockchain = { path = "../../storage/blockchain", features = ["service"] } cuprate-address-book = { workspace = true }
cuprate-database-service = { path = "../../storage/service" } cuprate-blockchain = { workspace = true, features = ["service"] }
cuprate-txpool = { path = "../../storage/txpool" } cuprate-database-service = { workspace = true }
cuprate-database = { path = "../../storage/database" } cuprate-txpool = { workspace = true }
cuprate-pruning = { path = "../../pruning" } cuprate-database = { workspace = true }
cuprate-test-utils = { path = "../../test-utils" } cuprate-pruning = { workspace = true }
cuprate-types = { path = "../../types" } cuprate-test-utils = { workspace = true }
cuprate-json-rpc = { path = "../../rpc/json-rpc" } cuprate-types = { workspace = true }
cuprate-rpc-interface = { path = "../../rpc/interface" } cuprate-json-rpc = { workspace = true }
cuprate-rpc-types = { path = "../../rpc/types" } cuprate-rpc-interface = { workspace = true }
cuprate-rpc-types = { workspace = true }
# TODO: after v1.0.0, remove unneeded dependencies. # TODO: after v1.0.0, remove unneeded dependencies.
anyhow = { workspace = true } anyhow = { workspace = true }
@ -64,7 +65,6 @@ rayon = { workspace = true }
serde_bytes = { workspace = true } serde_bytes = { workspace = true }
serde_json = { workspace = true } serde_json = { workspace = true }
serde = { workspace = true } serde = { workspace = true }
sha3 = { workspace = true, features = ["std"] }
thiserror = { workspace = true } thiserror = { workspace = true }
thread_local = { workspace = true } thread_local = { workspace = true }
tokio-util = { workspace = true } tokio-util = { workspace = true }

View file

@ -8,19 +8,16 @@ use std::{
}; };
use monero_serai::{block::Block, transaction::Transaction}; use monero_serai::{block::Block, transaction::Transaction};
use rayon::prelude::*;
use tokio::sync::{mpsc, oneshot}; use tokio::sync::{mpsc, oneshot};
use tower::{Service, ServiceExt}; use tower::{Service, ServiceExt};
use cuprate_blockchain::service::BlockchainReadHandle; use cuprate_blockchain::service::BlockchainReadHandle;
use cuprate_consensus::transactions::new_tx_verification_data; use cuprate_consensus::transactions::new_tx_verification_data;
use cuprate_helper::cast::usize_to_u64; use cuprate_txpool::service::{
use cuprate_txpool::service::interface::{TxpoolReadRequest, TxpoolReadResponse}; interface::{TxpoolReadRequest, TxpoolReadResponse},
use cuprate_txpool::service::TxpoolReadHandle; TxpoolReadHandle,
use cuprate_types::{
blockchain::{BlockchainReadRequest, BlockchainResponse},
Chain,
}; };
use cuprate_types::blockchain::{BlockchainReadRequest, BlockchainResponse};
use crate::{ use crate::{
blockchain::manager::{BlockchainManagerCommand, IncomingBlockOk}, blockchain::manager::{BlockchainManagerCommand, IncomingBlockOk},
@ -114,6 +111,10 @@ pub async fn handle_incoming_block(
for needed_hash in needed_hashes { for needed_hash in needed_hashes {
let Some(tx) = given_txs.remove(&needed_hash) else { let Some(tx) = given_txs.remove(&needed_hash) else {
// We return back the indexes of all txs missing from our pool, not taking into account the txs
// that were given with the block, as these txs will be dropped. It is not worth it to try to add
// these txs to the pool as this will only happen with a misbehaving peer or if the txpool reaches
// the size limit.
return Err(IncomingBlockError::UnknownTransactions(block_hash, missing)); return Err(IncomingBlockError::UnknownTransactions(block_hash, missing));
}; };
@ -136,7 +137,21 @@ pub async fn handle_incoming_block(
return Ok(IncomingBlockOk::AlreadyHave); return Ok(IncomingBlockOk::AlreadyHave);
} }
// From this point on we MUST not early return without removing the block hash from `BLOCKS_BEING_HANDLED`. // We must remove the block hash from `BLOCKS_BEING_HANDLED`.
let _guard = {
struct RemoveFromBlocksBeingHandled {
block_hash: [u8; 32],
}
impl Drop for RemoveFromBlocksBeingHandled {
fn drop(&mut self) {
BLOCKS_BEING_HANDLED
.lock()
.unwrap()
.remove(&self.block_hash);
}
}
RemoveFromBlocksBeingHandled { block_hash }
};
let (response_tx, response_rx) = oneshot::channel(); let (response_tx, response_rx) = oneshot::channel();
@ -149,15 +164,10 @@ pub async fn handle_incoming_block(
.await .await
.expect("TODO: don't actually panic here, an err means we are shutting down"); .expect("TODO: don't actually panic here, an err means we are shutting down");
let res = response_rx response_rx
.await .await
.expect("The blockchain manager will always respond") .expect("The blockchain manager will always respond")
.map_err(IncomingBlockError::InvalidBlock); .map_err(IncomingBlockError::InvalidBlock)
// Remove the block hash from the blocks being handled.
BLOCKS_BEING_HANDLED.lock().unwrap().remove(&block_hash);
res
} }
/// Check if we have a block with the given hash. /// Check if we have a block with the given hash.

View file

@ -8,15 +8,17 @@ use tracing::error;
use cuprate_blockchain::service::{BlockchainReadHandle, BlockchainWriteHandle}; use cuprate_blockchain::service::{BlockchainReadHandle, BlockchainWriteHandle};
use cuprate_consensus::{ use cuprate_consensus::{
context::RawBlockChainContext, BlockChainContextRequest, BlockChainContextResponse, BlockChainContextRequest, BlockChainContextResponse, BlockChainContextService,
BlockChainContextService, BlockVerifierService, ExtendedConsensusError, TxVerifierService, BlockVerifierService, ExtendedConsensusError, TxVerifierService, VerifyBlockRequest,
VerifyBlockRequest, VerifyBlockResponse, VerifyTxRequest, VerifyTxResponse, VerifyBlockResponse, VerifyTxRequest, VerifyTxResponse,
}; };
use cuprate_consensus_context::RawBlockChainContext;
use cuprate_p2p::{ use cuprate_p2p::{
block_downloader::{BlockBatch, BlockDownloaderConfig}, block_downloader::{BlockBatch, BlockDownloaderConfig},
BroadcastSvc, NetworkInterface, BroadcastSvc, NetworkInterface,
}; };
use cuprate_p2p_core::ClearNet; use cuprate_p2p_core::ClearNet;
use cuprate_txpool::service::TxpoolWriteHandle;
use cuprate_types::{ use cuprate_types::{
blockchain::{BlockchainReadRequest, BlockchainResponse}, blockchain::{BlockchainReadRequest, BlockchainResponse},
Chain, TransactionVerificationData, Chain, TransactionVerificationData,

View file

@ -1,8 +1,10 @@
//! The blockchain manager handler functions. //! The blockchain manager handler functions.
use bytes::Bytes; use bytes::Bytes;
use futures::{TryFutureExt, TryStreamExt}; use futures::{TryFutureExt, TryStreamExt};
use monero_serai::transaction::Input; use monero_serai::{
use monero_serai::{block::Block, transaction::Transaction}; block::Block,
transaction::{Input, Transaction},
};
use rayon::prelude::*; use rayon::prelude::*;
use std::ops::ControlFlow; use std::ops::ControlFlow;
use std::{collections::HashMap, sync::Arc}; use std::{collections::HashMap, sync::Arc};
@ -11,11 +13,11 @@ use tracing::info;
use cuprate_blockchain::service::{BlockchainReadHandle, BlockchainWriteHandle}; use cuprate_blockchain::service::{BlockchainReadHandle, BlockchainWriteHandle};
use cuprate_consensus::{ use cuprate_consensus::{
block::PreparedBlock, context::NewBlockData, transactions::new_tx_verification_data, block::PreparedBlock, transactions::new_tx_verification_data, BlockChainContextRequest,
BlockChainContextRequest, BlockChainContextResponse, BlockVerifierService, BlockChainContextResponse, BlockVerifierService, ExtendedConsensusError, VerifyBlockRequest,
ExtendedConsensusError, VerifyBlockRequest, VerifyBlockResponse, VerifyTxRequest, VerifyBlockResponse, VerifyTxRequest, VerifyTxResponse,
VerifyTxResponse,
}; };
use cuprate_consensus_context::NewBlockData;
use cuprate_helper::cast::usize_to_u64; use cuprate_helper::cast::usize_to_u64;
use cuprate_p2p::{block_downloader::BlockBatch, constants::LONG_BAN, BroadcastRequest}; use cuprate_p2p::{block_downloader::BlockBatch, constants::LONG_BAN, BroadcastRequest};
use cuprate_txpool::service::interface::TxpoolWriteRequest; use cuprate_txpool::service::interface::TxpoolWriteRequest;
@ -24,11 +26,8 @@ use cuprate_types::{
AltBlockInformation, HardFork, TransactionVerificationData, VerifiedBlockInformation, AltBlockInformation, HardFork, TransactionVerificationData, VerifiedBlockInformation,
}; };
use crate::blockchain::manager::commands::IncomingBlockOk;
use crate::{ use crate::{
blockchain::{ blockchain::manager::commands::{BlockchainManagerCommand, IncomingBlockOk},
manager::commands::BlockchainManagerCommand, types::ConsensusBlockchainReadHandle,
},
constants::PANIC_CRITICAL_SERVICE_ERROR, constants::PANIC_CRITICAL_SERVICE_ERROR,
signals::REORG_LOCK, signals::REORG_LOCK,
}; };

View file

@ -1,11 +1,10 @@
// FIXME: This whole module is not great and should be rewritten when the PeerSet is made. // FIXME: This whole module is not great and should be rewritten when the PeerSet is made.
use std::{pin::pin, sync::Arc, time::Duration}; use std::{sync::Arc, time::Duration};
use futures::StreamExt; use futures::StreamExt;
use tokio::time::interval;
use tokio::{ use tokio::{
sync::{mpsc, Notify}, sync::{mpsc, Notify},
time::sleep, time::interval,
}; };
use tower::{Service, ServiceExt}; use tower::{Service, ServiceExt};
use tracing::instrument; use tracing::instrument;

View file

@ -1,13 +1,7 @@
use std::task::{Context, Poll}; use tower::util::MapErr;
use futures::future::BoxFuture;
use futures::{FutureExt, TryFutureExt};
use tower::{util::MapErr, Service};
use cuprate_blockchain::{cuprate_database::RuntimeError, service::BlockchainReadHandle}; use cuprate_blockchain::{cuprate_database::RuntimeError, service::BlockchainReadHandle};
use cuprate_consensus::{BlockChainContextService, BlockVerifierService, TxVerifierService}; use cuprate_consensus::{BlockChainContextService, BlockVerifierService, TxVerifierService};
use cuprate_p2p::block_downloader::{ChainSvcRequest, ChainSvcResponse};
use cuprate_types::blockchain::{BlockchainReadRequest, BlockchainResponse};
/// The [`BlockVerifierService`] with all generic types defined. /// The [`BlockVerifierService`] with all generic types defined.
pub type ConcreteBlockVerifierService = BlockVerifierService< pub type ConcreteBlockVerifierService = BlockVerifierService<

View file

@ -2,48 +2,7 @@
//! //!
//! Will handle initiating the P2P and contains a protocol request handler. //! Will handle initiating the P2P and contains a protocol request handler.
use crate::txpool::IncomingTxHandler; mod network_address;
use cuprate_blockchain::service::BlockchainReadHandle;
use cuprate_consensus::BlockChainContextService;
use cuprate_p2p::{NetworkInterface, P2PConfig};
use cuprate_p2p_core::ClearNet;
use cuprate_txpool::service::TxpoolReadHandle;
use futures::{FutureExt, TryFutureExt};
use tokio::sync::oneshot;
use tower::ServiceExt;
mod core_sync_service;
pub mod request_handler; pub mod request_handler;
pub async fn start_clearnet_p2p( pub use network_address::CrossNetworkInternalPeerId;
blockchain_read_handle: BlockchainReadHandle,
blockchain_context_service: BlockChainContextService,
txpool_read_handle: TxpoolReadHandle,
config: P2PConfig<ClearNet>,
) -> Result<
(
NetworkInterface<ClearNet>,
oneshot::Sender<IncomingTxHandler>,
),
tower::BoxError,
> {
let (incoming_tx_handler_tx, incoming_tx_handler_rx) = oneshot::channel();
let request_handler_maker = request_handler::P2pProtocolRequestHandlerMaker {
blockchain_read_handle,
blockchain_context_service: blockchain_context_service.clone(),
txpool_read_handle,
incoming_tx_handler: None,
incoming_tx_handler_fut: incoming_tx_handler_rx.shared(),
};
Ok((
cuprate_p2p::initialize_network(
request_handler_maker.map_response(|s| s.map_err(Into::into)),
core_sync_service::CoreSyncService(blockchain_context_service),
config,
)
.await?,
incoming_tx_handler_tx,
))
}

View file

@ -0,0 +1,16 @@
use std::net::SocketAddr;
use cuprate_p2p_core::{client::InternalPeerID, ClearNet, NetworkZone};
/// An identifier for a P2P peer on any network.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum CrossNetworkInternalPeerId {
/// A clear-net peer.
ClearNet(InternalPeerID<<ClearNet as NetworkZone>::Addr>),
}
impl From<InternalPeerID<<ClearNet as NetworkZone>::Addr>> for CrossNetworkInternalPeerId {
fn from(addr: InternalPeerID<<ClearNet as NetworkZone>::Addr>) -> Self {
Self::ClearNet(addr)
}
}

View file

@ -5,7 +5,7 @@ use std::convert::Infallible;
use anyhow::Error; use anyhow::Error;
use tower::{Service, ServiceExt}; use tower::{Service, ServiceExt};
use cuprate_consensus::context::{ use cuprate_consensus_context::{
BlockChainContext, BlockChainContextRequest, BlockChainContextResponse, BlockChainContext, BlockChainContextRequest, BlockChainContextResponse,
BlockChainContextService, BlockChainContextService,
}; };

View file

@ -1,7 +1,7 @@
//! Global `static`s used throughout `cuprated`. //! Global `static`s used throughout `cuprated`.
use std::{ use std::{
sync::{atomic::AtomicU64, LazyLock}, sync::LazyLock,
time::{SystemTime, UNIX_EPOCH}, time::{SystemTime, UNIX_EPOCH},
}; };

View file

@ -1,9 +1,15 @@
//! Transaction Pool //! Transaction Pool
//! //!
//! Will handle initiating the tx-pool, providing the preprocessor required for the dandelion pool. //! Handles initiating the tx-pool, providing the preprocessor required for the dandelion pool.
use cuprate_consensus::BlockChainContextService;
use cuprate_p2p::NetworkInterface;
use cuprate_p2p_core::ClearNet;
use cuprate_txpool::service::{TxpoolReadHandle, TxpoolWriteHandle};
use crate::blockchain::ConcreteTxVerifierService;
mod dandelion; mod dandelion;
mod incoming_tx; mod incoming_tx;
mod txs_being_handled; mod txs_being_handled;
pub use incoming_tx::{IncomingTxError, IncomingTxHandler, IncomingTxs}; pub use incoming_tx::IncomingTxHandler;

View file

@ -1,22 +1,24 @@
use std::time::Duration; use std::time::Duration;
use bytes::Bytes; use cuprate_dandelion_tower::{
use cuprate_dandelion_tower::pool::DandelionPoolService; pool::DandelionPoolService, DandelionConfig, DandelionRouter, Graph,
use cuprate_dandelion_tower::{DandelionConfig, DandelionRouter, Graph}; };
use cuprate_p2p::NetworkInterface; use cuprate_p2p::NetworkInterface;
use cuprate_p2p_core::ClearNet; use cuprate_p2p_core::ClearNet;
use cuprate_txpool::service::{TxpoolReadHandle, TxpoolWriteHandle}; use cuprate_txpool::service::{TxpoolReadHandle, TxpoolWriteHandle};
use cuprate_wire::NetworkAddress;
use crate::{
p2p::CrossNetworkInternalPeerId,
txpool::incoming_tx::{DandelionTx, TxId},
};
mod diffuse_service; mod diffuse_service;
mod stem_service; mod stem_service;
mod tx_store; mod tx_store;
#[derive(Clone)] /// The configuration used for [`cuprate_dandelion_tower`].
pub struct DandelionTx(Bytes); ///
/// TODO: should we expose this to users of cuprated? probably not.
type TxId = [u8; 32];
const DANDELION_CONFIG: DandelionConfig = DandelionConfig { const DANDELION_CONFIG: DandelionConfig = DandelionConfig {
time_between_hop: Duration::from_millis(175), time_between_hop: Duration::from_millis(175),
epoch_duration: Duration::from_secs(10 * 60), epoch_duration: Duration::from_secs(10 * 60),
@ -24,21 +26,24 @@ const DANDELION_CONFIG: DandelionConfig = DandelionConfig {
graph: Graph::FourRegular, graph: Graph::FourRegular,
}; };
/// A [`DandelionRouter`] with all generic types defined.
type ConcreteDandelionRouter = DandelionRouter< type ConcreteDandelionRouter = DandelionRouter<
stem_service::OutboundPeerStream, stem_service::OutboundPeerStream,
diffuse_service::DiffuseService, diffuse_service::DiffuseService,
NetworkAddress, CrossNetworkInternalPeerId,
stem_service::StemPeerService<ClearNet>, stem_service::StemPeerService<ClearNet>,
DandelionTx, DandelionTx,
>; >;
/// Starts the dandelion pool manager task and returns a handle to send txs to broadcast.
pub fn start_dandelion_pool_manager( pub fn start_dandelion_pool_manager(
router: ConcreteDandelionRouter, router: ConcreteDandelionRouter,
txpool_read_handle: TxpoolReadHandle, txpool_read_handle: TxpoolReadHandle,
txpool_write_handle: TxpoolWriteHandle, txpool_write_handle: TxpoolWriteHandle,
) -> DandelionPoolService<DandelionTx, TxId, NetworkAddress> { ) -> DandelionPoolService<DandelionTx, TxId, CrossNetworkInternalPeerId> {
cuprate_dandelion_tower::pool::start_dandelion_pool_manager( cuprate_dandelion_tower::pool::start_dandelion_pool_manager(
12, // TODO: make this constant configurable?
32,
router, router,
tx_store::TxStoreService { tx_store::TxStoreService {
txpool_read_handle, txpool_read_handle,
@ -48,6 +53,7 @@ pub fn start_dandelion_pool_manager(
) )
} }
/// Creates a [`DandelionRouter`] from a [`NetworkInterface`].
pub fn dandelion_router(clear_net: NetworkInterface<ClearNet>) -> ConcreteDandelionRouter { pub fn dandelion_router(clear_net: NetworkInterface<ClearNet>) -> ConcreteDandelionRouter {
DandelionRouter::new( DandelionRouter::new(
diffuse_service::DiffuseService { diffuse_service::DiffuseService {

View file

@ -7,10 +7,10 @@ use futures::FutureExt;
use tower::Service; use tower::Service;
use cuprate_dandelion_tower::traits::DiffuseRequest; use cuprate_dandelion_tower::traits::DiffuseRequest;
use cuprate_p2p::{BroadcastRequest, BroadcastSvc, NetworkInterface}; use cuprate_p2p::{BroadcastRequest, BroadcastSvc};
use cuprate_p2p_core::ClearNet; use cuprate_p2p_core::ClearNet;
use super::DandelionTx; use crate::txpool::dandelion::DandelionTx;
/// The dandelion diffusion service. /// The dandelion diffusion service.
pub struct DiffuseService { pub struct DiffuseService {
@ -29,16 +29,15 @@ impl Service<DiffuseRequest<DandelionTx>> for DiffuseService {
} }
fn call(&mut self, req: DiffuseRequest<DandelionTx>) -> Self::Future { fn call(&mut self, req: DiffuseRequest<DandelionTx>) -> Self::Future {
// TODO: Call `into_inner` when 1.82.0 stabilizes // TODO: the dandelion crate should pass along where we got the tx from.
self.clear_net_broadcast_service let Ok(()) = self
.clear_net_broadcast_service
.call(BroadcastRequest::Transaction { .call(BroadcastRequest::Transaction {
tx_bytes: req.0 .0, tx_bytes: req.0 .0,
direction: None, direction: None,
received_from: None, received_from: None,
}) })
.now_or_never() .into_inner();
.unwrap()
.expect("Broadcast service is Infallible");
ready(Ok(())) ready(Ok(()))
} }

View file

@ -3,19 +3,19 @@ use std::{
task::{Context, Poll}, task::{Context, Poll},
}; };
use cuprate_dandelion_tower::{traits::StemRequest, OutboundPeer};
use cuprate_p2p::NetworkInterface;
use cuprate_p2p_core::{
client::{Client, InternalPeerID},
ClearNet, NetworkZone, PeerRequest, ProtocolRequest,
};
use cuprate_wire::{protocol::NewTransactions, NetworkAddress};
use bytes::Bytes; use bytes::Bytes;
use futures::Stream; use futures::Stream;
use tower::Service; use tower::Service;
use super::DandelionTx; use cuprate_dandelion_tower::{traits::StemRequest, OutboundPeer};
use cuprate_p2p::{ClientPoolDropGuard, NetworkInterface};
use cuprate_p2p_core::{
client::{Client, InternalPeerID},
ClearNet, NetworkZone, PeerRequest, ProtocolRequest,
};
use cuprate_wire::protocol::NewTransactions;
use crate::{p2p::CrossNetworkInternalPeerId, txpool::dandelion::DandelionTx};
/// The dandelion outbound peer stream. /// The dandelion outbound peer stream.
pub struct OutboundPeerStream { pub struct OutboundPeerStream {
@ -23,7 +23,10 @@ pub struct OutboundPeerStream {
} }
impl Stream for OutboundPeerStream { impl Stream for OutboundPeerStream {
type Item = Result<OutboundPeer<NetworkAddress, StemPeerService<ClearNet>>, tower::BoxError>; type Item = Result<
OutboundPeer<CrossNetworkInternalPeerId, StemPeerService<ClearNet>>,
tower::BoxError,
>;
fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> { fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> {
// TODO: make the outbound peer choice random. // TODO: make the outbound peer choice random.
@ -32,18 +35,16 @@ impl Stream for OutboundPeerStream {
.client_pool() .client_pool()
.outbound_client() .outbound_client()
.map_or(OutboundPeer::Exhausted, |client| { .map_or(OutboundPeer::Exhausted, |client| {
let addr = match client.info.id { OutboundPeer::Peer(
InternalPeerID::KnownAddr(addr) => addr, CrossNetworkInternalPeerId::ClearNet(client.info.id),
InternalPeerID::Unknown(_) => panic!("Outbound peer had an unknown address"), StemPeerService(client),
}; )
OutboundPeer::Peer(addr.into(), StemPeerService(client))
})))) }))))
} }
} }
/// The stem service, used to send stem txs. /// The stem service, used to send stem txs.
pub struct StemPeerService<N: NetworkZone>(Client<N>); pub struct StemPeerService<N: NetworkZone>(ClientPoolDropGuard<N>);
impl<N: NetworkZone> Service<StemRequest<DandelionTx>> for StemPeerService<N> { impl<N: NetworkZone> Service<StemRequest<DandelionTx>> for StemPeerService<N> {
type Response = <Client<N> as Service<PeerRequest>>::Response; type Response = <Client<N> as Service<PeerRequest>>::Response;

View file

@ -1,11 +1,8 @@
use std::{ use std::task::{Context, Poll};
f32::consts::E,
task::{Context, Poll},
};
use bytes::Bytes; use bytes::Bytes;
use futures::{future::BoxFuture, FutureExt, StreamExt, TryFutureExt}; use futures::{future::BoxFuture, FutureExt};
use tower::{util::Oneshot, Service, ServiceExt}; use tower::{Service, ServiceExt};
use cuprate_dandelion_tower::{ use cuprate_dandelion_tower::{
traits::{TxStoreRequest, TxStoreResponse}, traits::{TxStoreRequest, TxStoreResponse},
@ -13,7 +10,7 @@ use cuprate_dandelion_tower::{
}; };
use cuprate_database::RuntimeError; use cuprate_database::RuntimeError;
use cuprate_txpool::service::{ use cuprate_txpool::service::{
interface::{TxpoolReadRequest, TxpoolReadResponse, TxpoolWriteRequest, TxpoolWriteResponse}, interface::{TxpoolReadRequest, TxpoolReadResponse, TxpoolWriteRequest},
TxpoolReadHandle, TxpoolWriteHandle, TxpoolReadHandle, TxpoolWriteHandle,
}; };

View file

@ -1,85 +1,115 @@
use std::{ use std::{
collections::HashSet, collections::HashSet,
future::ready,
sync::Arc, sync::Arc,
task::{Context, Poll}, task::{Context, Poll},
}; };
use bytes::Bytes; use bytes::Bytes;
use dashmap::DashSet;
use futures::{future::BoxFuture, FutureExt}; use futures::{future::BoxFuture, FutureExt};
use monero_serai::transaction::Transaction; use monero_serai::transaction::Transaction;
use sha3::{Digest, Sha3_256};
use tower::{Service, ServiceExt}; use tower::{Service, ServiceExt};
use cuprate_consensus::{ use cuprate_consensus::{
transactions::new_tx_verification_data, BlockChainContextRequest, BlockChainContextResponse, transactions::new_tx_verification_data, BlockChainContextRequest, BlockChainContextResponse,
BlockChainContextService, ExtendedConsensusError, TxVerifierService, VerifyTxRequest, BlockChainContextService, ExtendedConsensusError, VerifyTxRequest,
VerifyTxResponse,
}; };
use cuprate_dandelion_tower::{ use cuprate_dandelion_tower::{
pool::{DandelionPoolService, IncomingTx, IncomingTxBuilder}, pool::{DandelionPoolService, IncomingTxBuilder},
State, TxState, State, TxState,
}; };
use cuprate_helper::asynch::rayon_spawn_async; use cuprate_helper::asynch::rayon_spawn_async;
use cuprate_p2p::constants::MAX_TRANSACTION_BLOB_SIZE; use cuprate_p2p::NetworkInterface;
use cuprate_txpool::service::{ use cuprate_p2p_core::ClearNet;
interface::{TxpoolReadRequest, TxpoolReadResponse, TxpoolWriteRequest, TxpoolWriteResponse}, use cuprate_txpool::{
TxpoolReadHandle, TxpoolWriteHandle, service::{
interface::{
TxpoolReadRequest, TxpoolReadResponse, TxpoolWriteRequest, TxpoolWriteResponse,
},
TxpoolReadHandle, TxpoolWriteHandle,
},
transaction_blob_hash,
}; };
use cuprate_types::TransactionVerificationData; use cuprate_types::TransactionVerificationData;
use cuprate_wire::NetworkAddress;
use crate::{ use crate::{
blockchain::ConcreteTxVerifierService, blockchain::ConcreteTxVerifierService,
constants::PANIC_CRITICAL_SERVICE_ERROR, constants::PANIC_CRITICAL_SERVICE_ERROR,
p2p::CrossNetworkInternalPeerId,
signals::REORG_LOCK, signals::REORG_LOCK,
txpool::txs_being_handled::{tx_blob_hash, TxBeingHandledLocally, TxsBeingHandled}, txpool::{
dandelion,
txs_being_handled::{TxsBeingHandled, TxsBeingHandledLocally},
},
}; };
/// An error that can happen handling an incoming tx. /// An error that can happen handling an incoming tx.
#[derive(Debug, thiserror::Error)]
pub enum IncomingTxError { pub enum IncomingTxError {
#[error("Peer sent a transaction which is too big")]
TooLarge,
#[error("parse error: {0}")]
Parse(std::io::Error), Parse(std::io::Error),
#[error("consensus error: {0}")]
Consensus(ExtendedConsensusError), Consensus(ExtendedConsensusError),
#[error("Duplicate tx sent in message")]
DuplicateTransaction, DuplicateTransaction,
} }
/// Incoming transactions. /// Incoming transactions.
pub struct IncomingTxs { pub struct IncomingTxs {
/// The raw bytes of the transactions.
pub txs: Vec<Bytes>, pub txs: Vec<Bytes>,
pub state: TxState<NetworkAddress>, /// The routing state of the transactions.
pub state: TxState<CrossNetworkInternalPeerId>,
} }
/// The transaction type used for dandelion++. /// The transaction type used for dandelion++.
#[derive(Clone)] #[derive(Clone)]
pub struct DandelionTx(Bytes); pub struct DandelionTx(pub Bytes);
/// A transaction ID/hash. /// A transaction ID/hash.
type TxId = [u8; 32]; pub(super) type TxId = [u8; 32];
/// The service than handles incoming transaction pool transactions. /// The service than handles incoming transaction pool transactions.
/// ///
/// This service handles everything including verifying the tx, adding it to the pool and routing it to other nodes. /// This service handles everything including verifying the tx, adding it to the pool and routing it to other nodes.
#[derive(Clone)]
pub struct IncomingTxHandler { pub struct IncomingTxHandler {
/// A store of txs currently being handled in incoming tx requests. /// A store of txs currently being handled in incoming tx requests.
pub txs_being_handled: TxsBeingHandled, pub(super) txs_being_handled: TxsBeingHandled,
/// The blockchain context cache. /// The blockchain context cache.
pub blockchain_context_cache: BlockChainContextService, pub(super) blockchain_context_cache: BlockChainContextService,
/// The dandelion txpool manager. /// The dandelion txpool manager.
pub dandelion_pool_manager: DandelionPoolService<DandelionTx, TxId, NetworkAddress>, pub(super) dandelion_pool_manager:
DandelionPoolService<DandelionTx, TxId, CrossNetworkInternalPeerId>,
/// The transaction verifier service. /// The transaction verifier service.
pub tx_verifier_service: ConcreteTxVerifierService, pub(super) tx_verifier_service: ConcreteTxVerifierService,
/// The txpool write handle. /// The txpool write handle.
pub txpool_write_handle: TxpoolWriteHandle, pub(super) txpool_write_handle: TxpoolWriteHandle,
/// The txpool read handle. /// The txpool read handle.
pub txpool_read_handle: TxpoolReadHandle, pub(super) txpool_read_handle: TxpoolReadHandle,
}
impl IncomingTxHandler {
/// Initialize the [`IncomingTxHandler`].
#[expect(clippy::significant_drop_tightening)]
pub fn init(
clear_net: NetworkInterface<ClearNet>,
txpool_write_handle: TxpoolWriteHandle,
txpool_read_handle: TxpoolReadHandle,
blockchain_context_cache: BlockChainContextService,
tx_verifier_service: ConcreteTxVerifierService,
) -> Self {
let dandelion_router = dandelion::dandelion_router(clear_net);
let dandelion_pool_manager = dandelion::start_dandelion_pool_manager(
dandelion_router,
txpool_read_handle.clone(),
txpool_write_handle.clone(),
);
Self {
txs_being_handled: TxsBeingHandled::new(),
blockchain_context_cache,
dandelion_pool_manager,
tx_verifier_service,
txpool_write_handle,
txpool_read_handle,
}
}
} }
impl Service<IncomingTxs> for IncomingTxHandler { impl Service<IncomingTxs> for IncomingTxHandler {
@ -93,8 +123,7 @@ impl Service<IncomingTxs> for IncomingTxHandler {
fn call(&mut self, req: IncomingTxs) -> Self::Future { fn call(&mut self, req: IncomingTxs) -> Self::Future {
handle_incoming_txs( handle_incoming_txs(
req.txs, req,
req.state,
self.txs_being_handled.clone(), self.txs_being_handled.clone(),
self.blockchain_context_cache.clone(), self.blockchain_context_cache.clone(),
self.tx_verifier_service.clone(), self.tx_verifier_service.clone(),
@ -106,18 +135,17 @@ impl Service<IncomingTxs> for IncomingTxHandler {
} }
} }
#[expect(clippy::too_many_arguments)] /// Handles the incoming txs.
async fn handle_incoming_txs( async fn handle_incoming_txs(
txs: Vec<Bytes>, IncomingTxs { txs, state }: IncomingTxs,
state: TxState<NetworkAddress>,
txs_being_handled: TxsBeingHandled, txs_being_handled: TxsBeingHandled,
mut blockchain_context_cache: BlockChainContextService, mut blockchain_context_cache: BlockChainContextService,
mut tx_verifier_service: ConcreteTxVerifierService, mut tx_verifier_service: ConcreteTxVerifierService,
mut txpool_write_handle: TxpoolWriteHandle, mut txpool_write_handle: TxpoolWriteHandle,
mut txpool_read_handle: TxpoolReadHandle, mut txpool_read_handle: TxpoolReadHandle,
mut dandelion_pool_manager: DandelionPoolService<DandelionTx, TxId, NetworkAddress>, mut dandelion_pool_manager: DandelionPoolService<DandelionTx, TxId, CrossNetworkInternalPeerId>,
) -> Result<(), IncomingTxError> { ) -> Result<(), IncomingTxError> {
let reorg_guard = REORG_LOCK.read().await; let _reorg_guard = REORG_LOCK.read().await;
let (txs, stem_pool_txs, txs_being_handled_guard) = let (txs, stem_pool_txs, txs_being_handled_guard) =
prepare_incoming_txs(txs, txs_being_handled, &mut txpool_read_handle).await?; prepare_incoming_txs(txs, txs_being_handled, &mut txpool_read_handle).await?;
@ -159,6 +187,7 @@ async fn handle_incoming_txs(
.await; .await;
} }
// Re-relay any txs we got in the block that were already in our stem pool.
for stem_tx in stem_pool_txs { for stem_tx in stem_pool_txs {
rerelay_stem_tx( rerelay_stem_tx(
&stem_tx, &stem_tx,
@ -175,6 +204,11 @@ async fn handle_incoming_txs(
/// Prepares the incoming transactions for verification. /// Prepares the incoming transactions for verification.
/// ///
/// This will filter out all transactions already in the pool or txs already being handled in another request. /// This will filter out all transactions already in the pool or txs already being handled in another request.
///
/// Returns in order:
/// - The [`TransactionVerificationData`] for all the txs we did not already have
/// - The Ids of the transactions in the incoming message that are in our stem-pool
/// - A [`TxsBeingHandledLocally`] guard that prevents verifying the same tx at the same time across 2 tasks.
async fn prepare_incoming_txs( async fn prepare_incoming_txs(
tx_blobs: Vec<Bytes>, tx_blobs: Vec<Bytes>,
txs_being_handled: TxsBeingHandled, txs_being_handled: TxsBeingHandled,
@ -183,7 +217,7 @@ async fn prepare_incoming_txs(
( (
Vec<Arc<TransactionVerificationData>>, Vec<Arc<TransactionVerificationData>>,
Vec<TxId>, Vec<TxId>,
TxBeingHandledLocally, TxsBeingHandledLocally,
), ),
IncomingTxError, IncomingTxError,
> { > {
@ -194,11 +228,7 @@ async fn prepare_incoming_txs(
let txs = tx_blobs let txs = tx_blobs
.into_iter() .into_iter()
.filter_map(|tx_blob| { .filter_map(|tx_blob| {
if tx_blob.len() > MAX_TRANSACTION_BLOB_SIZE { let tx_blob_hash = transaction_blob_hash(&tx_blob);
return Some(Err(IncomingTxError::TooLarge));
}
let tx_blob_hash = tx_blob_hash(tx_blob.as_ref());
// If a duplicate is in here the incoming tx batch contained the same tx twice. // If a duplicate is in here the incoming tx batch contained the same tx twice.
if !tx_blob_hashes.insert(tx_blob_hash) { if !tx_blob_hashes.insert(tx_blob_hash) {
@ -256,11 +286,18 @@ async fn prepare_incoming_txs(
.await .await
} }
/// Handle a verified tx.
///
/// This will add the tx to the txpool and route it to the network.
async fn handle_valid_tx( async fn handle_valid_tx(
tx: Arc<TransactionVerificationData>, tx: Arc<TransactionVerificationData>,
state: TxState<NetworkAddress>, state: TxState<CrossNetworkInternalPeerId>,
txpool_write_handle: &mut TxpoolWriteHandle, txpool_write_handle: &mut TxpoolWriteHandle,
dandelion_pool_manager: &mut DandelionPoolService<DandelionTx, TxId, NetworkAddress>, dandelion_pool_manager: &mut DandelionPoolService<
DandelionTx,
TxId,
CrossNetworkInternalPeerId,
>,
) { ) {
let incoming_tx = let incoming_tx =
IncomingTxBuilder::new(DandelionTx(Bytes::copy_from_slice(&tx.tx_blob)), tx.tx_hash); IncomingTxBuilder::new(DandelionTx(Bytes::copy_from_slice(&tx.tx_blob)), tx.tx_hash);
@ -271,7 +308,7 @@ async fn handle_valid_tx(
.expect(PANIC_CRITICAL_SERVICE_ERROR) .expect(PANIC_CRITICAL_SERVICE_ERROR)
.call(TxpoolWriteRequest::AddTransaction { .call(TxpoolWriteRequest::AddTransaction {
tx, tx,
state_stem: state.state_stem(), state_stem: state.is_stem_stage(),
}) })
.await .await
.expect("TODO") .expect("TODO")
@ -284,7 +321,7 @@ async fn handle_valid_tx(
return; return;
}; };
// TODO: There is a race condition possible if a tx and block come in at the same time <https://github.com/Cuprate/cuprate/issues/314>. // TODO: There is a race condition possible if a tx and block come in at the same time: <https://github.com/Cuprate/cuprate/issues/314>.
let incoming_tx = incoming_tx let incoming_tx = incoming_tx
.with_routing_state(state) .with_routing_state(state)
@ -301,27 +338,31 @@ async fn handle_valid_tx(
.expect(PANIC_CRITICAL_SERVICE_ERROR); .expect(PANIC_CRITICAL_SERVICE_ERROR);
} }
/// Re-relay a tx that was already in our stem pool.
async fn rerelay_stem_tx( async fn rerelay_stem_tx(
tx_hash: &TxId, tx_hash: &TxId,
state: TxState<NetworkAddress>, state: TxState<CrossNetworkInternalPeerId>,
txpool_read_handle: &mut TxpoolReadHandle, txpool_read_handle: &mut TxpoolReadHandle,
dandelion_pool_manager: &mut DandelionPoolService<DandelionTx, TxId, NetworkAddress>, dandelion_pool_manager: &mut DandelionPoolService<
DandelionTx,
TxId,
CrossNetworkInternalPeerId,
>,
) { ) {
let TxpoolReadResponse::TxBlob { tx_blob, .. } = txpool_read_handle let Ok(TxpoolReadResponse::TxBlob { tx_blob, .. }) = txpool_read_handle
.ready() .ready()
.await .await
.expect(PANIC_CRITICAL_SERVICE_ERROR) .expect(PANIC_CRITICAL_SERVICE_ERROR)
.call(TxpoolReadRequest::TxBlob(*tx_hash)) .call(TxpoolReadRequest::TxBlob(*tx_hash))
.await .await
.expect("TODO")
else { else {
unreachable!() // The tx could have been dropped from the pool.
return;
}; };
let incoming_tx = let incoming_tx =
IncomingTxBuilder::new(DandelionTx(Bytes::copy_from_slice(&tx_blob)), *tx_hash); IncomingTxBuilder::new(DandelionTx(Bytes::copy_from_slice(&tx_blob)), *tx_hash);
// TODO: fill this in properly.
let incoming_tx = incoming_tx let incoming_tx = incoming_tx
.with_routing_state(state) .with_routing_state(state)
.with_state_in_db(Some(State::Stem)) .with_state_in_db(Some(State::Stem))

View file

@ -1,31 +1,39 @@
use dashmap::DashSet;
use sha3::{Digest, Sha3_256};
use std::sync::Arc; use std::sync::Arc;
pub fn tx_blob_hash(tx_bytes: &[u8]) -> [u8; 32] { use dashmap::DashSet;
let mut hasher = Sha3_256::new();
hasher.update(tx_bytes);
hasher.finalize().into()
}
/// A set of txs currently being handled, shared between instances of the incoming tx handler.
#[derive(Clone)] #[derive(Clone)]
pub struct TxsBeingHandled(Arc<DashSet<[u8; 32]>>); pub struct TxsBeingHandled(Arc<DashSet<[u8; 32]>>);
impl TxsBeingHandled { impl TxsBeingHandled {
pub fn local_tracker(&self) -> TxBeingHandledLocally { /// Create a new [`TxsBeingHandled`]
TxBeingHandledLocally { pub fn new() -> Self {
Self(Arc::new(DashSet::new()))
}
/// Create a new [`TxsBeingHandledLocally`] that will keep track of txs being handled in a request.
pub fn local_tracker(&self) -> TxsBeingHandledLocally {
TxsBeingHandledLocally {
txs_being_handled: self.clone(), txs_being_handled: self.clone(),
txs: vec![], txs: vec![],
} }
} }
} }
pub struct TxBeingHandledLocally { /// A tracker of txs being handled in a single request. This will add the txs to the global [`TxsBeingHandled`]
/// tracker as well.
///
/// When this is dropped the txs will be removed from [`TxsBeingHandled`].
pub struct TxsBeingHandledLocally {
txs_being_handled: TxsBeingHandled, txs_being_handled: TxsBeingHandled,
txs: Vec<[u8; 32]>, txs: Vec<[u8; 32]>,
} }
impl TxBeingHandledLocally { impl TxsBeingHandledLocally {
/// Try add a tx to the map from its [`transaction_blob_hash`](cuprate_txpool::transaction_blob_hash).
///
/// Returns `true` if the tx was added and `false` if another task is already handling this tx.
pub fn try_add_tx(&mut self, tx_blob_hash: [u8; 32]) -> bool { pub fn try_add_tx(&mut self, tx_blob_hash: [u8; 32]) -> bool {
if !self.txs_being_handled.0.insert(tx_blob_hash) { if !self.txs_being_handled.0.insert(tx_blob_hash) {
return false; return false;
@ -36,7 +44,7 @@ impl TxBeingHandledLocally {
} }
} }
impl Drop for TxBeingHandledLocally { impl Drop for TxsBeingHandledLocally {
fn drop(&mut self) { fn drop(&mut self) {
for hash in &self.txs { for hash in &self.txs {
self.txs_being_handled.0.remove(hash); self.txs_being_handled.0.remove(hash);

View file

@ -16,7 +16,8 @@ cargo doc --open --package cuprate-blockchain
| Crate | In-tree path | Purpose | | Crate | In-tree path | Purpose |
|-------|--------------|---------| |-------|--------------|---------|
| [`cuprate-consensus`](https://doc.cuprate.org/cuprate_consensus) | [`consensus/`](https://github.com/Cuprate/cuprate/tree/main/consensus) | TODO | [`cuprate-consensus`](https://doc.cuprate.org/cuprate_consensus) | [`consensus/`](https://github.com/Cuprate/cuprate/tree/main/consensus) | TODO
| [`cuprate-consensus-rules`](https://doc.cuprate.org/cuprate_consensus_rules) | [`consensus/rules/`](https://github.com/Cuprate/cuprate/tree/main/consensus-rules) | TODO | [`cuprate-consensus-context`](https://doc.cuprate.org/cuprate_consensus_context) | [`consensus/context/`](https://github.com/Cuprate/cuprate/tree/main/consensus/context) | TODO
| [`cuprate-consensus-rules`](https://doc.cuprate.org/cuprate_consensus_rules) | [`consensus/rules/`](https://github.com/Cuprate/cuprate/tree/main/consensus/rules) | TODO
| [`cuprate-fast-sync`](https://doc.cuprate.org/cuprate_fast_sync) | [`consensus/fast-sync/`](https://github.com/Cuprate/cuprate/tree/main/consensus/fast-sync) | Fast block synchronization | [`cuprate-fast-sync`](https://doc.cuprate.org/cuprate_fast_sync) | [`consensus/fast-sync/`](https://github.com/Cuprate/cuprate/tree/main/consensus/fast-sync) | Fast block synchronization
## Networking ## Networking

View file

@ -8,9 +8,10 @@ authors = ["Boog900"]
repository = "https://github.com/Cuprate/cuprate/tree/main/consensus" repository = "https://github.com/Cuprate/cuprate/tree/main/consensus"
[dependencies] [dependencies]
cuprate-helper = { path = "../helper", default-features = false, features = ["std", "asynch", "num"] } cuprate-helper = { workspace = true, default-features = false, features = ["std", "asynch", "num"] }
cuprate-consensus-rules = { path = "./rules", features = ["rayon"] } cuprate-consensus-rules = { workspace = true, features = ["rayon"] }
cuprate-types = { path = "../types" } cuprate-types = { workspace = true }
cuprate-consensus-context = { workspace = true }
cfg-if = { workspace = true } cfg-if = { workspace = true }
thiserror = { workspace = true } thiserror = { workspace = true }
@ -18,20 +19,17 @@ tower = { workspace = true, features = ["util"] }
tracing = { workspace = true, features = ["std", "attributes"] } tracing = { workspace = true, features = ["std", "attributes"] }
futures = { workspace = true, features = ["std", "async-await"] } futures = { workspace = true, features = ["std", "async-await"] }
randomx-rs = { workspace = true }
monero-serai = { workspace = true, features = ["std"] } monero-serai = { workspace = true, features = ["std"] }
rayon = { workspace = true } rayon = { workspace = true }
thread_local = { workspace = true } thread_local = { workspace = true }
tokio = { workspace = true, features = ["rt"] }
tokio-util = { workspace = true }
hex = { workspace = true } hex = { workspace = true }
rand = { workspace = true } rand = { workspace = true }
[dev-dependencies] [dev-dependencies]
cuprate-test-utils = { path = "../test-utils" } cuprate-test-utils = { workspace = true }
cuprate-consensus-rules = {path = "./rules", features = ["proptest"]} cuprate-consensus-rules = { workspace = true, features = ["proptest"]}
hex-literal = { workspace = true } hex-literal = { workspace = true }
curve25519-dalek = { workspace = true } curve25519-dalek = { workspace = true }
@ -42,4 +40,4 @@ proptest = { workspace = true }
proptest-derive = { workspace = true } proptest-derive = { workspace = true }
[lints] [lints]
workspace = true workspace = true

View file

@ -0,0 +1,27 @@
[package]
name = "cuprate-consensus-context"
version = "0.1.0"
edition = "2021"
license = "MIT"
authors = ["SyntheticBird","Boog900"]
[dependencies]
cuprate-consensus-rules = { workspace = true, features = ["proptest"]}
cuprate-helper = { workspace = true, default-features = false, features = ["std", "cast", "num", "asynch"] }
cuprate-types = { workspace = true, default-features = false, features = ["blockchain"] }
futures = { workspace = true, features = ["std", "async-await"] }
tokio = { workspace = true, features = ["rt-multi-thread", "macros"]}
tokio-util = { workspace = true }
tower = { workspace = true, features = ["util"] }
tracing = { workspace = true, features = ["std", "attributes"] }
thiserror = { workspace = true }
monero-serai = { workspace = true, features = ["std"] }
randomx-rs = { workspace = true }
rayon = { workspace = true }
thread_local = { workspace = true }
hex = { workspace = true }
[lints]
workspace = true

View file

@ -9,9 +9,8 @@ use cuprate_types::{
}; };
use crate::{ use crate::{
ExtendedConsensusError, ContextCacheError, __private::Database, difficulty::DifficultyCache, rx_vms::RandomXVm,
__private::Database, weight::BlockWeightsCache,
context::{difficulty::DifficultyCache, rx_vms::RandomXVm, weight::BlockWeightsCache},
}; };
pub(crate) mod sealed { pub(crate) mod sealed {
@ -38,7 +37,7 @@ pub struct AltChainContextCache {
pub chain_height: usize, pub chain_height: usize,
/// The top hash of the alt chain. /// The top hash of the alt chain.
pub top_hash: [u8; 32], pub top_hash: [u8; 32],
/// The [`ChainID`] of the alt chain. /// The [`ChainId`] of the alt chain.
pub chain_id: Option<ChainId>, pub chain_id: Option<ChainId>,
/// The parent [`Chain`] of this alt chain. /// The parent [`Chain`] of this alt chain.
pub parent_chain: Chain, pub parent_chain: Chain,
@ -98,7 +97,7 @@ impl AltChainMap {
&mut self, &mut self,
prev_id: [u8; 32], prev_id: [u8; 32],
database: D, database: D,
) -> Result<Box<AltChainContextCache>, ExtendedConsensusError> { ) -> Result<Box<AltChainContextCache>, ContextCacheError> {
if let Some(cache) = self.alt_cache_map.remove(&prev_id) { if let Some(cache) = self.alt_cache_map.remove(&prev_id) {
return Ok(cache); return Ok(cache);
} }
@ -133,7 +132,7 @@ pub(crate) async fn get_alt_chain_difficulty_cache<D: Database + Clone>(
prev_id: [u8; 32], prev_id: [u8; 32],
main_chain_difficulty_cache: &DifficultyCache, main_chain_difficulty_cache: &DifficultyCache,
mut database: D, mut database: D,
) -> Result<DifficultyCache, ExtendedConsensusError> { ) -> Result<DifficultyCache, ContextCacheError> {
// find the block with hash == prev_id. // find the block with hash == prev_id.
let BlockchainResponse::FindBlock(res) = database let BlockchainResponse::FindBlock(res) = database
.ready() .ready()
@ -180,7 +179,7 @@ pub(crate) async fn get_alt_chain_weight_cache<D: Database + Clone>(
prev_id: [u8; 32], prev_id: [u8; 32],
main_chain_weight_cache: &BlockWeightsCache, main_chain_weight_cache: &BlockWeightsCache,
mut database: D, mut database: D,
) -> Result<BlockWeightsCache, ExtendedConsensusError> { ) -> Result<BlockWeightsCache, ContextCacheError> {
// find the block with hash == prev_id. // find the block with hash == prev_id.
let BlockchainResponse::FindBlock(res) = database let BlockchainResponse::FindBlock(res) = database
.ready() .ready()

View file

@ -17,7 +17,7 @@ use cuprate_types::{
Chain, Chain,
}; };
use crate::{Database, ExtendedConsensusError, HardFork}; use crate::{ContextCacheError, Database, HardFork};
/// The amount of blocks we account for to calculate difficulty /// The amount of blocks we account for to calculate difficulty
const DIFFICULTY_WINDOW: usize = 720; const DIFFICULTY_WINDOW: usize = 720;
@ -33,9 +33,9 @@ const DIFFICULTY_LAG: usize = 15;
/// ///
#[derive(Debug, Clone, Copy, Eq, PartialEq)] #[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct DifficultyCacheConfig { pub struct DifficultyCacheConfig {
pub(crate) window: usize, pub window: usize,
pub(crate) cut: usize, pub cut: usize,
pub(crate) lag: usize, pub lag: usize,
} }
impl DifficultyCacheConfig { impl DifficultyCacheConfig {
@ -73,14 +73,13 @@ impl DifficultyCacheConfig {
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub struct DifficultyCache { pub struct DifficultyCache {
/// The list of timestamps in the window. /// The list of timestamps in the window.
/// len <= [`DIFFICULTY_BLOCKS_COUNT`] pub timestamps: VecDeque<u64>,
pub(crate) timestamps: VecDeque<u64>,
/// The current cumulative difficulty of the chain. /// The current cumulative difficulty of the chain.
pub(crate) cumulative_difficulties: VecDeque<u128>, pub cumulative_difficulties: VecDeque<u128>,
/// The last height we accounted for. /// The last height we accounted for.
pub(crate) last_accounted_height: usize, pub last_accounted_height: usize,
/// The config /// The config
pub(crate) config: DifficultyCacheConfig, pub config: DifficultyCacheConfig,
} }
impl DifficultyCache { impl DifficultyCache {
@ -91,7 +90,7 @@ impl DifficultyCache {
config: DifficultyCacheConfig, config: DifficultyCacheConfig,
database: D, database: D,
chain: Chain, chain: Chain,
) -> Result<Self, ExtendedConsensusError> { ) -> Result<Self, ContextCacheError> {
tracing::info!("Initializing difficulty cache this may take a while."); tracing::info!("Initializing difficulty cache this may take a while.");
let mut block_start = chain_height.saturating_sub(config.total_block_count()); let mut block_start = chain_height.saturating_sub(config.total_block_count());
@ -134,7 +133,7 @@ impl DifficultyCache {
&mut self, &mut self,
numb_blocks: usize, numb_blocks: usize,
database: D, database: D,
) -> Result<(), ExtendedConsensusError> { ) -> Result<(), ContextCacheError> {
let Some(retained_blocks) = self.timestamps.len().checked_sub(numb_blocks) else { let Some(retained_blocks) = self.timestamps.len().checked_sub(numb_blocks) else {
// More blocks to pop than we have in the cache, so just restart a new cache. // More blocks to pop than we have in the cache, so just restart a new cache.
*self = Self::init_from_chain_height( *self = Self::init_from_chain_height(
@ -361,7 +360,7 @@ async fn get_blocks_in_pow_info<D: Database + Clone>(
database: D, database: D,
block_heights: Range<usize>, block_heights: Range<usize>,
chain: Chain, chain: Chain,
) -> Result<(VecDeque<u64>, VecDeque<u128>), ExtendedConsensusError> { ) -> Result<(VecDeque<u64>, VecDeque<u128>), ContextCacheError> {
tracing::info!("Getting blocks timestamps"); tracing::info!("Getting blocks timestamps");
let BlockchainResponse::BlockExtendedHeaderInRange(ext_header) = database let BlockchainResponse::BlockExtendedHeaderInRange(ext_header) = database

View file

@ -9,7 +9,7 @@ use cuprate_types::{
Chain, Chain,
}; };
use crate::{Database, ExtendedConsensusError}; use crate::{ContextCacheError, Database};
/// The default amount of hard-fork votes to track to decide on activation of a hard-fork. /// The default amount of hard-fork votes to track to decide on activation of a hard-fork.
/// ///
@ -21,9 +21,9 @@ const DEFAULT_WINDOW_SIZE: usize = 10080; // supermajority window check length -
#[derive(Debug, Clone, Copy, Eq, PartialEq)] #[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct HardForkConfig { pub struct HardForkConfig {
/// The network we are on. /// The network we are on.
pub(crate) info: HFsInfo, pub info: HFsInfo,
/// The amount of votes we are taking into account to decide on a fork activation. /// The amount of votes we are taking into account to decide on a fork activation.
pub(crate) window: usize, pub window: usize,
} }
impl HardForkConfig { impl HardForkConfig {
@ -54,17 +54,17 @@ impl HardForkConfig {
/// A struct that keeps track of the current hard-fork and current votes. /// A struct that keeps track of the current hard-fork and current votes.
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub(crate) struct HardForkState { pub struct HardForkState {
/// The current active hard-fork. /// The current active hard-fork.
pub(crate) current_hardfork: HardFork, pub current_hardfork: HardFork,
/// The hard-fork config. /// The hard-fork config.
pub(crate) config: HardForkConfig, pub config: HardForkConfig,
/// The votes in the current window. /// The votes in the current window.
pub(crate) votes: HFVotes, pub votes: HFVotes,
/// The last block height accounted for. /// The last block height accounted for.
pub(crate) last_height: usize, pub last_height: usize,
} }
impl HardForkState { impl HardForkState {
@ -74,7 +74,7 @@ impl HardForkState {
chain_height: usize, chain_height: usize,
config: HardForkConfig, config: HardForkConfig,
mut database: D, mut database: D,
) -> Result<Self, ExtendedConsensusError> { ) -> Result<Self, ContextCacheError> {
tracing::info!("Initializing hard-fork state this may take a while."); tracing::info!("Initializing hard-fork state this may take a while.");
let block_start = chain_height.saturating_sub(config.window); let block_start = chain_height.saturating_sub(config.window);
@ -122,11 +122,11 @@ impl HardForkState {
/// # Invariant /// # Invariant
/// ///
/// This _must_ only be used on a main-chain cache. /// This _must_ only be used on a main-chain cache.
pub(crate) async fn pop_blocks_main_chain<D: Database + Clone>( pub async fn pop_blocks_main_chain<D: Database + Clone>(
&mut self, &mut self,
numb_blocks: usize, numb_blocks: usize,
database: D, database: D,
) -> Result<(), ExtendedConsensusError> { ) -> Result<(), ContextCacheError> {
let Some(retained_blocks) = self.votes.total_votes().checked_sub(self.config.window) else { let Some(retained_blocks) = self.votes.total_votes().checked_sub(self.config.window) else {
*self = Self::init_from_chain_height( *self = Self::init_from_chain_height(
self.last_height + 1 - numb_blocks, self.last_height + 1 - numb_blocks,
@ -159,7 +159,7 @@ impl HardForkState {
} }
/// Add a new block to the cache. /// Add a new block to the cache.
pub(crate) fn new_block(&mut self, vote: HardFork, height: usize) { pub fn new_block(&mut self, vote: HardFork, height: usize) {
// We don't _need_ to take in `height` but it's for safety, so we don't silently loose track // We don't _need_ to take in `height` but it's for safety, so we don't silently loose track
// of blocks. // of blocks.
assert_eq!(self.last_height + 1, height); assert_eq!(self.last_height + 1, height);
@ -194,7 +194,7 @@ impl HardForkState {
} }
/// Returns the current hard-fork. /// Returns the current hard-fork.
pub(crate) const fn current_hardfork(&self) -> HardFork { pub const fn current_hardfork(&self) -> HardFork {
self.current_hardfork self.current_hardfork
} }
} }
@ -205,7 +205,7 @@ async fn get_votes_in_range<D: Database>(
database: D, database: D,
block_heights: Range<usize>, block_heights: Range<usize>,
window_size: usize, window_size: usize,
) -> Result<HFVotes, ExtendedConsensusError> { ) -> Result<HFVotes, ContextCacheError> {
let mut votes = HFVotes::new(window_size); let mut votes = HFVotes::new(window_size);
let BlockchainResponse::BlockExtendedHeaderInRange(vote_list) = database let BlockchainResponse::BlockExtendedHeaderInRange(vote_list) = database

View file

@ -1,9 +1,13 @@
//! # Blockchain Context //! # Blockchain Context
//! //!
//! This module contains a service to get cached context from the blockchain: [`BlockChainContext`]. //! This crate contains a service to get cached context from the blockchain: [`BlockChainContext`].
//! This is used during contextual validation, this does not have all the data for contextual validation //! This is used during contextual validation, this does not have all the data for contextual validation
//! (outputs) for that you will need a [`Database`]. //! (outputs) for that you will need a [`Database`].
//!
// Used in documentation references for [`BlockChainContextRequest`]
// FIXME: should we pull in a dependency just to link docs?
use monero_serai as _;
use std::{ use std::{
cmp::min, cmp::min,
collections::HashMap, collections::HashMap,
@ -18,14 +22,14 @@ use tokio::sync::mpsc;
use tokio_util::sync::PollSender; use tokio_util::sync::PollSender;
use tower::Service; use tower::Service;
use cuprate_consensus_rules::{blocks::ContextToVerifyBlock, current_unix_timestamp, HardFork}; use cuprate_consensus_rules::{
blocks::ContextToVerifyBlock, current_unix_timestamp, ConsensusError, HardFork,
};
use crate::{Database, ExtendedConsensusError}; pub mod difficulty;
pub mod hardforks;
pub(crate) mod difficulty; pub mod rx_vms;
pub(crate) mod hardforks; pub mod weight;
pub(crate) mod rx_vms;
pub(crate) mod weight;
mod alt_chains; mod alt_chains;
mod task; mod task;
@ -36,13 +40,13 @@ use difficulty::DifficultyCache;
use rx_vms::RandomXVm; use rx_vms::RandomXVm;
use weight::BlockWeightsCache; use weight::BlockWeightsCache;
pub(crate) use alt_chains::{sealed::AltChainRequestToken, AltChainContextCache}; pub use alt_chains::{sealed::AltChainRequestToken, AltChainContextCache};
pub use difficulty::DifficultyCacheConfig; pub use difficulty::DifficultyCacheConfig;
pub use hardforks::HardForkConfig; pub use hardforks::HardForkConfig;
pub use tokens::*; pub use tokens::*;
pub use weight::BlockWeightsCacheConfig; pub use weight::BlockWeightsCacheConfig;
pub(crate) const BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW: u64 = 60; pub const BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW: u64 = 60;
/// Config for the context service. /// Config for the context service.
pub struct ContextConfig { pub struct ContextConfig {
@ -91,7 +95,7 @@ impl ContextConfig {
pub async fn initialize_blockchain_context<D>( pub async fn initialize_blockchain_context<D>(
cfg: ContextConfig, cfg: ContextConfig,
database: D, database: D,
) -> Result<BlockChainContextService, ExtendedConsensusError> ) -> Result<BlockChainContextService, ContextCacheError>
where where
D: Database + Clone + Send + Sync + 'static, D: Database + Clone + Send + Sync + 'static,
D::Future: Send + 'static, D::Future: Send + 'static,
@ -414,3 +418,52 @@ impl Service<BlockChainContextRequest> for BlockChainContextService {
.boxed() .boxed()
} }
} }
#[derive(Debug, thiserror::Error)]
pub enum ContextCacheError {
/// A consensus error.
#[error("{0}")]
ConErr(#[from] ConsensusError),
/// A database error.
#[error("Database error: {0}")]
DBErr(#[from] tower::BoxError),
}
use __private::Database;
pub mod __private {
use std::future::Future;
use cuprate_types::blockchain::{BlockchainReadRequest, BlockchainResponse};
/// A type alias trait used to represent a database, so we don't have to write [`tower::Service`] bounds
/// everywhere.
///
/// Automatically implemented for:
/// ```ignore
/// tower::Service<BCReadRequest, Response = BCResponse, Error = tower::BoxError>
/// ```
pub trait Database:
tower::Service<
BlockchainReadRequest,
Response = BlockchainResponse,
Error = tower::BoxError,
Future = Self::Future2,
>
{
type Future2: Future<Output = Result<Self::Response, Self::Error>> + Send + 'static;
}
impl<
T: tower::Service<
BlockchainReadRequest,
Response = BlockchainResponse,
Error = tower::BoxError,
>,
> Database for T
where
T::Future: Future<Output = Result<Self::Response, Self::Error>> + Send + 'static,
{
type Future2 = T::Future;
}
}

View file

@ -26,10 +26,10 @@ use cuprate_types::{
Chain, Chain,
}; };
use crate::{Database, ExtendedConsensusError}; use crate::{ContextCacheError, Database};
/// The amount of randomX VMs to keep in the cache. /// The amount of randomX VMs to keep in the cache.
const RX_SEEDS_CACHED: usize = 2; pub const RX_SEEDS_CACHED: usize = 2;
/// A multithreaded randomX VM. /// A multithreaded randomX VM.
#[derive(Debug)] #[derive(Debug)]
@ -72,14 +72,14 @@ impl RandomX for RandomXVm {
/// The randomX VMs cache, keeps the VM needed to calculate the current block's proof-of-work hash (if a VM is needed) and a /// The randomX VMs cache, keeps the VM needed to calculate the current block's proof-of-work hash (if a VM is needed) and a
/// couple more around this VM. /// couple more around this VM.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub(crate) struct RandomXVmCache { pub struct RandomXVmCache {
/// The top [`RX_SEEDS_CACHED`] RX seeds. /// The top [`RX_SEEDS_CACHED`] RX seeds.
pub(crate) seeds: VecDeque<(usize, [u8; 32])>, pub seeds: VecDeque<(usize, [u8; 32])>,
/// The VMs for `seeds` (if after hf 12, otherwise this will be empty). /// The VMs for `seeds` (if after hf 12, otherwise this will be empty).
pub(crate) vms: HashMap<usize, Arc<RandomXVm>>, pub vms: HashMap<usize, Arc<RandomXVm>>,
/// A single cached VM that was given to us from a part of Cuprate. /// A single cached VM that was given to us from a part of Cuprate.
pub(crate) cached_vm: Option<([u8; 32], Arc<RandomXVm>)>, pub cached_vm: Option<([u8; 32], Arc<RandomXVm>)>,
} }
impl RandomXVmCache { impl RandomXVmCache {
@ -88,7 +88,7 @@ impl RandomXVmCache {
chain_height: usize, chain_height: usize,
hf: &HardFork, hf: &HardFork,
database: D, database: D,
) -> Result<Self, ExtendedConsensusError> { ) -> Result<Self, ContextCacheError> {
let seed_heights = get_last_rx_seed_heights(chain_height - 1, RX_SEEDS_CACHED); let seed_heights = get_last_rx_seed_heights(chain_height - 1, RX_SEEDS_CACHED);
let seed_hashes = get_block_hashes(seed_heights.clone(), database).await?; let seed_hashes = get_block_hashes(seed_heights.clone(), database).await?;
@ -125,18 +125,18 @@ impl RandomXVmCache {
} }
/// Add a randomX VM to the cache, with the seed it was created with. /// Add a randomX VM to the cache, with the seed it was created with.
pub(crate) fn add_vm(&mut self, vm: ([u8; 32], Arc<RandomXVm>)) { pub fn add_vm(&mut self, vm: ([u8; 32], Arc<RandomXVm>)) {
self.cached_vm.replace(vm); self.cached_vm.replace(vm);
} }
/// Creates a RX VM for an alt chain, looking at the main chain RX VMs to see if we can use one /// Creates a RX VM for an alt chain, looking at the main chain RX VMs to see if we can use one
/// of them first. /// of them first.
pub(crate) async fn get_alt_vm<D: Database>( pub async fn get_alt_vm<D: Database>(
&self, &self,
height: usize, height: usize,
chain: Chain, chain: Chain,
database: D, database: D,
) -> Result<Arc<RandomXVm>, ExtendedConsensusError> { ) -> Result<Arc<RandomXVm>, ContextCacheError> {
let seed_height = randomx_seed_height(height); let seed_height = randomx_seed_height(height);
let BlockchainResponse::BlockHash(seed_hash) = database let BlockchainResponse::BlockHash(seed_hash) = database
@ -162,7 +162,7 @@ impl RandomXVmCache {
} }
/// Get the main-chain `RandomX` VMs. /// Get the main-chain `RandomX` VMs.
pub(crate) async fn get_vms(&mut self) -> HashMap<usize, Arc<RandomXVm>> { pub async fn get_vms(&mut self) -> HashMap<usize, Arc<RandomXVm>> {
match self.seeds.len().checked_sub(self.vms.len()) { match self.seeds.len().checked_sub(self.vms.len()) {
// No difference in the amount of seeds to VMs. // No difference in the amount of seeds to VMs.
Some(0) => (), Some(0) => (),
@ -214,7 +214,7 @@ impl RandomXVmCache {
} }
/// Removes all the `RandomX` VMs above the `new_height`. /// Removes all the `RandomX` VMs above the `new_height`.
pub(crate) fn pop_blocks_main_chain(&mut self, new_height: usize) { pub fn pop_blocks_main_chain(&mut self, new_height: usize) {
self.seeds.retain(|(height, _)| *height < new_height); self.seeds.retain(|(height, _)| *height < new_height);
self.vms.retain(|height, _| *height < new_height); self.vms.retain(|height, _| *height < new_height);
} }
@ -222,7 +222,7 @@ impl RandomXVmCache {
/// Add a new block to the VM cache. /// Add a new block to the VM cache.
/// ///
/// hash is the block hash not the blocks proof-of-work hash. /// hash is the block hash not the blocks proof-of-work hash.
pub(crate) fn new_block(&mut self, height: usize, hash: &[u8; 32]) { pub fn new_block(&mut self, height: usize, hash: &[u8; 32]) {
if is_randomx_seed_height(height) { if is_randomx_seed_height(height) {
tracing::debug!("Block {height} is a randomX seed height, adding it to the cache.",); tracing::debug!("Block {height} is a randomX seed height, adding it to the cache.",);
@ -243,7 +243,7 @@ impl RandomXVmCache {
/// Get the last `amount` of RX seeds, the top height returned here will not necessarily be the RX VM for the top block /// Get the last `amount` of RX seeds, the top height returned here will not necessarily be the RX VM for the top block
/// in the chain as VMs include some lag before a seed activates. /// in the chain as VMs include some lag before a seed activates.
pub(crate) fn get_last_rx_seed_heights(mut last_height: usize, mut amount: usize) -> Vec<usize> { pub fn get_last_rx_seed_heights(mut last_height: usize, mut amount: usize) -> Vec<usize> {
let mut seeds = Vec::with_capacity(amount); let mut seeds = Vec::with_capacity(amount);
if is_randomx_seed_height(last_height) { if is_randomx_seed_height(last_height) {
seeds.push(last_height); seeds.push(last_height);
@ -268,7 +268,7 @@ pub(crate) fn get_last_rx_seed_heights(mut last_height: usize, mut amount: usize
async fn get_block_hashes<D: Database + Clone>( async fn get_block_hashes<D: Database + Clone>(
heights: Vec<usize>, heights: Vec<usize>,
database: D, database: D,
) -> Result<Vec<[u8; 32]>, ExtendedConsensusError> { ) -> Result<Vec<[u8; 32]>, ContextCacheError> {
let mut fut = FuturesOrdered::new(); let mut fut = FuturesOrdered::new();
for height in heights { for height in heights {
@ -281,7 +281,7 @@ async fn get_block_hashes<D: Database + Clone>(
else { else {
panic!("Database sent incorrect response!"); panic!("Database sent incorrect response!");
}; };
Result::<_, ExtendedConsensusError>::Ok(hash) Result::<_, ContextCacheError>::Ok(hash)
}); });
} }

View file

@ -16,13 +16,10 @@ use cuprate_types::{
}; };
use crate::{ use crate::{
context::{ alt_chains::{get_alt_chain_difficulty_cache, get_alt_chain_weight_cache, AltChainMap},
alt_chains::{get_alt_chain_difficulty_cache, get_alt_chain_weight_cache, AltChainMap}, difficulty, hardforks, rx_vms, weight, BlockChainContext, BlockChainContextRequest,
difficulty, hardforks, rx_vms, weight, BlockChainContext, BlockChainContextRequest, BlockChainContextResponse, ContextCacheError, ContextConfig, Database, RawBlockChainContext,
BlockChainContextResponse, ContextConfig, RawBlockChainContext, ValidityToken, ValidityToken, BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW,
BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW,
},
Database, ExtendedConsensusError,
}; };
/// A request from the context service to the context task. /// A request from the context service to the context task.
@ -68,7 +65,7 @@ impl<D: Database + Clone + Send + 'static> ContextTask<D> {
pub(crate) async fn init_context( pub(crate) async fn init_context(
cfg: ContextConfig, cfg: ContextConfig,
mut database: D, mut database: D,
) -> Result<Self, ExtendedConsensusError> { ) -> Result<Self, ContextCacheError> {
let ContextConfig { let ContextConfig {
difficulty_cfg, difficulty_cfg,
weights_config, weights_config,

View file

@ -21,12 +21,12 @@ use cuprate_types::{
Chain, Chain,
}; };
use crate::{Database, ExtendedConsensusError, HardFork}; use crate::{ContextCacheError, Database, HardFork};
/// The short term block weight window. /// The short term block weight window.
const SHORT_TERM_WINDOW: usize = 100; pub const SHORT_TERM_WINDOW: usize = 100;
/// The long term block weight window. /// The long term block weight window.
const LONG_TERM_WINDOW: usize = 100000; pub const LONG_TERM_WINDOW: usize = 100000;
/// Configuration for the block weight cache. /// Configuration for the block weight cache.
/// ///
@ -80,7 +80,7 @@ impl BlockWeightsCache {
config: BlockWeightsCacheConfig, config: BlockWeightsCacheConfig,
database: D, database: D,
chain: Chain, chain: Chain,
) -> Result<Self, ExtendedConsensusError> { ) -> Result<Self, ContextCacheError> {
tracing::info!("Initializing weight cache this may take a while."); tracing::info!("Initializing weight cache this may take a while.");
let long_term_weights = get_long_term_weight_in_range( let long_term_weights = get_long_term_weight_in_range(
@ -121,7 +121,7 @@ impl BlockWeightsCache {
&mut self, &mut self,
numb_blocks: usize, numb_blocks: usize,
database: D, database: D,
) -> Result<(), ExtendedConsensusError> { ) -> Result<(), ContextCacheError> {
if self.long_term_weights.window_len() <= numb_blocks { if self.long_term_weights.window_len() <= numb_blocks {
// More blocks to pop than we have in the cache, so just restart a new cache. // More blocks to pop than we have in the cache, so just restart a new cache.
*self = Self::init_from_chain_height( *self = Self::init_from_chain_height(
@ -258,7 +258,7 @@ fn calculate_effective_median_block_weight(
} }
/// Calculates a blocks long term weight. /// Calculates a blocks long term weight.
pub(crate) fn calculate_block_long_term_weight( pub fn calculate_block_long_term_weight(
hf: HardFork, hf: HardFork,
block_weight: usize, block_weight: usize,
long_term_median: usize, long_term_median: usize,
@ -287,7 +287,7 @@ async fn get_blocks_weight_in_range<D: Database + Clone>(
range: Range<usize>, range: Range<usize>,
database: D, database: D,
chain: Chain, chain: Chain,
) -> Result<Vec<usize>, ExtendedConsensusError> { ) -> Result<Vec<usize>, ContextCacheError> {
tracing::info!("getting block weights."); tracing::info!("getting block weights.");
let BlockchainResponse::BlockExtendedHeaderInRange(ext_headers) = database let BlockchainResponse::BlockExtendedHeaderInRange(ext_headers) = database
@ -311,7 +311,7 @@ async fn get_long_term_weight_in_range<D: Database + Clone>(
range: Range<usize>, range: Range<usize>,
database: D, database: D,
chain: Chain, chain: Chain,
) -> Result<Vec<usize>, ExtendedConsensusError> { ) -> Result<Vec<usize>, ContextCacheError> {
tracing::info!("getting block long term weights."); tracing::info!("getting block long term weights.");
let BlockchainResponse::BlockExtendedHeaderInRange(ext_headers) = database let BlockchainResponse::BlockExtendedHeaderInRange(ext_headers) = database

View file

@ -9,11 +9,12 @@ name = "cuprate-fast-sync-create-hashes"
path = "src/create.rs" path = "src/create.rs"
[dependencies] [dependencies]
cuprate-blockchain = { path = "../../storage/blockchain" } cuprate-blockchain = { workspace = true }
cuprate-consensus = { path = ".." } cuprate-consensus = { workspace = true }
cuprate-consensus-rules = { path = "../rules" } cuprate-consensus-rules = { workspace = true }
cuprate-types = { path = "../../types" } cuprate-consensus-context = { workspace = true }
cuprate-helper = { path = "../../helper", features = ["cast"] } cuprate-types = { workspace = true }
cuprate-helper = { workspace = true, features = ["cast"] }
clap = { workspace = true, features = ["derive", "std"] } clap = { workspace = true, features = ["derive", "std"] }
hex = { workspace = true } hex = { workspace = true }
@ -27,4 +28,4 @@ tower = { workspace = true }
[dev-dependencies] [dev-dependencies]
[lints] [lints]
workspace = true workspace = true

View file

@ -12,10 +12,8 @@ use monero_serai::{
}; };
use tower::{Service, ServiceExt}; use tower::{Service, ServiceExt};
use cuprate_consensus::{ use cuprate_consensus::transactions::new_tx_verification_data;
context::{BlockChainContextRequest, BlockChainContextResponse}, use cuprate_consensus_context::{BlockChainContextRequest, BlockChainContextResponse};
transactions::new_tx_verification_data,
};
use cuprate_consensus_rules::{miner_tx::MinerTxError, ConsensusError}; use cuprate_consensus_rules::{miner_tx::MinerTxError, ConsensusError};
use cuprate_helper::cast::u64_to_usize; use cuprate_helper::cast::u64_to_usize;
use cuprate_types::{VerifiedBlockInformation, VerifiedTransactionInformation}; use cuprate_types::{VerifiedBlockInformation, VerifiedTransactionInformation};

View file

@ -11,10 +11,10 @@ proptest = ["cuprate-types/proptest"]
rayon = ["dep:rayon"] rayon = ["dep:rayon"]
[dependencies] [dependencies]
cuprate-constants = { path = "../../constants", default-features = false } cuprate-constants = { workspace = true, default-features = false, features = ["block"] }
cuprate-helper = { path = "../../helper", default-features = false, features = ["std", "cast"] } cuprate-helper = { workspace = true, default-features = false, features = ["std", "cast"] }
cuprate-types = { path = "../../types", default-features = false } cuprate-types = { workspace = true, default-features = false }
cuprate-cryptonight = {path = "../../cryptonight"} cuprate-cryptonight = { workspace = true }
monero-serai = { workspace = true, features = ["std"] } monero-serai = { workspace = true, features = ["std"] }
curve25519-dalek = { workspace = true, features = ["alloc", "zeroize", "precomputed-tables"] } curve25519-dalek = { workspace = true, features = ["alloc", "zeroize", "precomputed-tables"] }

View file

@ -63,9 +63,9 @@ where
/// An internal function that returns an iterator or a parallel iterator if the /// An internal function that returns an iterator or a parallel iterator if the
/// `rayon` feature is enabled. /// `rayon` feature is enabled.
#[cfg(not(feature = "rayon"))] #[cfg(not(feature = "rayon"))]
fn try_par_iter<T>(t: T) -> impl std::iter::Iterator<Item = T::Item> fn try_par_iter<T>(t: T) -> impl Iterator<Item = T::Item>
where where
T: std::iter::IntoIterator, T: IntoIterator,
{ {
t.into_iter() t.into_iter()
} }

View file

@ -14,6 +14,9 @@ use monero_serai::{
}; };
use tower::{Service, ServiceExt}; use tower::{Service, ServiceExt};
use cuprate_consensus_context::{
BlockChainContextRequest, BlockChainContextResponse, RawBlockChainContext,
};
use cuprate_helper::asynch::rayon_spawn_async; use cuprate_helper::asynch::rayon_spawn_async;
use cuprate_types::{ use cuprate_types::{
AltBlockInformation, TransactionVerificationData, VerifiedBlockInformation, AltBlockInformation, TransactionVerificationData, VerifiedBlockInformation,
@ -30,7 +33,6 @@ use cuprate_consensus_rules::{
}; };
use crate::{ use crate::{
context::{BlockChainContextRequest, BlockChainContextResponse, RawBlockChainContext},
transactions::{VerifyTxRequest, VerifyTxResponse}, transactions::{VerifyTxRequest, VerifyTxResponse},
Database, ExtendedConsensusError, Database, ExtendedConsensusError,
}; };

View file

@ -7,6 +7,12 @@ use std::{collections::HashMap, sync::Arc};
use monero_serai::{block::Block, transaction::Input}; use monero_serai::{block::Block, transaction::Input};
use tower::{Service, ServiceExt}; use tower::{Service, ServiceExt};
use cuprate_consensus_context::{
difficulty::DifficultyCache,
rx_vms::RandomXVm,
weight::{self, BlockWeightsCache},
AltChainContextCache, AltChainRequestToken, BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW,
};
use cuprate_consensus_rules::{ use cuprate_consensus_rules::{
blocks::{ blocks::{
check_block_pow, check_block_weight, check_timestamp, randomx_seed_height, BlockError, check_block_pow, check_block_weight, check_timestamp, randomx_seed_height, BlockError,
@ -22,12 +28,6 @@ use cuprate_types::{
use crate::{ use crate::{
block::{free::pull_ordered_transactions, PreparedBlock}, block::{free::pull_ordered_transactions, PreparedBlock},
context::{
difficulty::DifficultyCache,
rx_vms::RandomXVm,
weight::{self, BlockWeightsCache},
AltChainContextCache, AltChainRequestToken, BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW,
},
BlockChainContextRequest, BlockChainContextResponse, ExtendedConsensusError, BlockChainContextRequest, BlockChainContextResponse, ExtendedConsensusError,
VerifyBlockResponse, VerifyBlockResponse,
}; };

View file

@ -5,6 +5,7 @@ use rayon::prelude::*;
use tower::{Service, ServiceExt}; use tower::{Service, ServiceExt};
use tracing::instrument; use tracing::instrument;
use cuprate_consensus_context::rx_vms::RandomXVm;
use cuprate_consensus_rules::{ use cuprate_consensus_rules::{
blocks::{check_block_pow, is_randomx_seed_height, randomx_seed_height, BlockError}, blocks::{check_block_pow, is_randomx_seed_height, randomx_seed_height, BlockError},
hard_forks::HardForkError, hard_forks::HardForkError,
@ -15,7 +16,6 @@ use cuprate_helper::asynch::rayon_spawn_async;
use crate::{ use crate::{
block::{free::pull_ordered_transactions, PreparedBlock, PreparedBlockExPow}, block::{free::pull_ordered_transactions, PreparedBlock, PreparedBlockExPow},
context::rx_vms::RandomXVm,
transactions::new_tx_verification_data, transactions::new_tx_verification_data,
BlockChainContextRequest, BlockChainContextResponse, ExtendedConsensusError, BlockChainContextRequest, BlockChainContextResponse, ExtendedConsensusError,
VerifyBlockResponse, VerifyBlockResponse,

View file

@ -24,13 +24,12 @@ use cuprate_consensus_rules::ConsensusError;
mod batch_verifier; mod batch_verifier;
pub mod block; pub mod block;
pub mod context;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
pub mod transactions; pub mod transactions;
pub use block::{BlockVerifierService, VerifyBlockRequest, VerifyBlockResponse}; pub use block::{BlockVerifierService, VerifyBlockRequest, VerifyBlockResponse};
pub use context::{ pub use cuprate_consensus_context::{
initialize_blockchain_context, BlockChainContext, BlockChainContextRequest, initialize_blockchain_context, BlockChainContext, BlockChainContextRequest,
BlockChainContextResponse, BlockChainContextService, ContextConfig, BlockChainContextResponse, BlockChainContextService, ContextConfig,
}; };

View file

@ -2,15 +2,13 @@ use proptest::strategy::ValueTree;
use proptest::{strategy::Strategy, test_runner::TestRunner}; use proptest::{strategy::Strategy, test_runner::TestRunner};
use tower::ServiceExt; use tower::ServiceExt;
use crate::{ use cuprate_consensus_context::{
context::{ initialize_blockchain_context, BlockChainContextRequest, BlockChainContextResponse,
initialize_blockchain_context, BlockChainContextRequest, BlockChainContextResponse, ContextConfig, NewBlockData,
ContextConfig, NewBlockData,
},
tests::mock_db::*,
HardFork,
}; };
use crate::{tests::mock_db::*, HardFork};
pub(crate) mod data; pub(crate) mod data;
mod difficulty; mod difficulty;
mod hardforks; mod hardforks;

View file

@ -4,10 +4,10 @@ use proptest::collection::{size_range, vec};
use proptest::{prelude::*, prop_assert_eq, prop_compose, proptest}; use proptest::{prelude::*, prop_assert_eq, prop_compose, proptest};
use crate::{ use crate::{
context::difficulty::*,
tests::{context::data::DIF_3000000_3002000, mock_db::*}, tests::{context::data::DIF_3000000_3002000, mock_db::*},
HardFork, HardFork,
}; };
use cuprate_consensus_context::difficulty::*;
use cuprate_helper::num::median; use cuprate_helper::num::median;
use cuprate_types::Chain; use cuprate_types::Chain;

View file

@ -1,13 +1,11 @@
use proptest::{collection::vec, prelude::*}; use proptest::{collection::vec, prelude::*};
use cuprate_consensus_context::{hardforks::HardForkState, HardForkConfig};
use cuprate_consensus_rules::hard_forks::{HFInfo, HFsInfo, HardFork, NUMB_OF_HARD_FORKS}; use cuprate_consensus_rules::hard_forks::{HFInfo, HFsInfo, HardFork, NUMB_OF_HARD_FORKS};
use crate::{ use crate::tests::{
context::{hardforks::HardForkState, HardForkConfig}, context::data::{HFS_2678808_2688888, HFS_2688888_2689608},
tests::{ mock_db::*,
context::data::{HFS_2678808_2688888, HFS_2688888_2689608},
mock_db::*,
},
}; };
const TEST_WINDOW_SIZE: usize = 25; const TEST_WINDOW_SIZE: usize = 25;

View file

@ -3,15 +3,13 @@ use std::collections::VecDeque;
use proptest::prelude::*; use proptest::prelude::*;
use tokio::runtime::Builder; use tokio::runtime::Builder;
use cuprate_consensus_context::rx_vms::{get_last_rx_seed_heights, RandomXVmCache};
use cuprate_consensus_rules::{ use cuprate_consensus_rules::{
blocks::{is_randomx_seed_height, randomx_seed_height}, blocks::{is_randomx_seed_height, randomx_seed_height},
HardFork, HardFork,
}; };
use crate::{ use crate::tests::mock_db::*;
context::rx_vms::{get_last_rx_seed_heights, RandomXVmCache},
tests::mock_db::*,
};
#[test] #[test]
fn rx_heights_consistent() { fn rx_heights_consistent() {

View file

@ -1,11 +1,11 @@
use crate::{ use crate::{
context::{
weight::{calculate_block_long_term_weight, BlockWeightsCache},
BlockWeightsCacheConfig,
},
tests::{context::data::BW_2850000_3050000, mock_db::*}, tests::{context::data::BW_2850000_3050000, mock_db::*},
HardFork, HardFork,
}; };
use cuprate_consensus_context::{
weight::{calculate_block_long_term_weight, BlockWeightsCache},
BlockWeightsCacheConfig,
};
use cuprate_types::Chain; use cuprate_types::Chain;
pub(crate) const TEST_WEIGHT_CONFIG: BlockWeightsCacheConfig = pub(crate) const TEST_WEIGHT_CONFIG: BlockWeightsCacheConfig =

View file

@ -19,4 +19,4 @@ rpc = []
[dev-dependencies] [dev-dependencies]
[lints] [lints]
workspace = true workspace = true

View file

@ -16,6 +16,7 @@ atomic = ["dep:crossbeam"]
asynch = ["dep:futures", "dep:rayon"] asynch = ["dep:futures", "dep:rayon"]
cast = [] cast = []
constants = [] constants = []
crypto = ["dep:curve25519-dalek", "dep:monero-serai", "std"]
fs = ["dep:dirs"] fs = ["dep:dirs"]
num = [] num = []
map = ["cast", "dep:monero-serai", "dep:cuprate-constants"] map = ["cast", "dep:monero-serai", "dep:cuprate-constants"]
@ -24,14 +25,15 @@ thread = ["std", "dep:target_os_lib"]
tx = ["dep:monero-serai"] tx = ["dep:monero-serai"]
[dependencies] [dependencies]
cuprate-constants = { path = "../constants", optional = true, features = ["block"] } cuprate-constants = { workspace = true, optional = true, features = ["block"] }
crossbeam = { workspace = true, optional = true } chrono = { workspace = true, optional = true, features = ["std", "clock"] }
chrono = { workspace = true, optional = true, features = ["std", "clock"] } crossbeam = { workspace = true, optional = true }
dirs = { workspace = true, optional = true } curve25519-dalek = { workspace = true, optional = true }
futures = { workspace = true, optional = true, features = ["std"] } dirs = { workspace = true, optional = true }
monero-serai = { workspace = true, optional = true } futures = { workspace = true, optional = true, features = ["std"] }
rayon = { workspace = true, optional = true } monero-serai = { workspace = true, optional = true }
rayon = { workspace = true, optional = true }
# This is kinda a stupid work around. # This is kinda a stupid work around.
# [thread] needs to activate one of these libs (windows|libc) # [thread] needs to activate one of these libs (windows|libc)

122
helper/src/crypto.rs Normal file
View file

@ -0,0 +1,122 @@
//! Crypto related functions and runtime initialized constants
//---------------------------------------------------------------------------------------------------- Use
use std::sync::LazyLock;
use curve25519_dalek::{
constants::ED25519_BASEPOINT_POINT, edwards::VartimeEdwardsPrecomputation,
traits::VartimePrecomputedMultiscalarMul, EdwardsPoint, Scalar,
};
use monero_serai::generators::H;
//---------------------------------------------------------------------------------------------------- Pre-computation
/// This is the decomposed amount table containing the mandatory Pre-RCT amounts. It is used to pre-compute
/// zero commitments at runtime.
///
/// Defined at:
/// - <https://github.com/monero-project/monero/blob/893916ad091a92e765ce3241b94e706ad012b62a/src/ringct/rctOps.cpp#L44>
#[rustfmt::skip]
pub const ZERO_COMMITMENT_DECOMPOSED_AMOUNT: [u64; 172] = [
1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 20, 30, 40, 50, 60, 70, 80, 90,
100, 200, 300, 400, 500, 600, 700, 800, 900,
1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000,
10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000,
100000, 200000, 300000, 400000, 500000, 600000, 700000, 800000, 900000,
1000000, 2000000, 3000000, 4000000, 5000000, 6000000, 7000000, 8000000, 9000000,
10000000, 20000000, 30000000, 40000000, 50000000, 60000000, 70000000, 80000000, 90000000,
100000000, 200000000, 300000000, 400000000, 500000000, 600000000, 700000000, 800000000, 900000000,
1000000000, 2000000000, 3000000000, 4000000000, 5000000000, 6000000000, 7000000000, 8000000000, 9000000000,
10000000000, 20000000000, 30000000000, 40000000000, 50000000000, 60000000000, 70000000000, 80000000000, 90000000000,
100000000000, 200000000000, 300000000000, 400000000000, 500000000000, 600000000000, 700000000000, 800000000000, 900000000000,
1000000000000, 2000000000000, 3000000000000, 4000000000000, 5000000000000, 6000000000000, 7000000000000, 8000000000000, 9000000000000,
10000000000000, 20000000000000, 30000000000000, 40000000000000, 50000000000000, 60000000000000, 70000000000000, 80000000000000, 90000000000000,
100000000000000, 200000000000000, 300000000000000, 400000000000000, 500000000000000, 600000000000000, 700000000000000, 800000000000000, 900000000000000,
1000000000000000, 2000000000000000, 3000000000000000, 4000000000000000, 5000000000000000, 6000000000000000, 7000000000000000, 8000000000000000, 9000000000000000,
10000000000000000, 20000000000000000, 30000000000000000, 40000000000000000, 50000000000000000, 60000000000000000, 70000000000000000, 80000000000000000, 90000000000000000,
100000000000000000, 200000000000000000, 300000000000000000, 400000000000000000, 500000000000000000, 600000000000000000, 700000000000000000, 800000000000000000, 900000000000000000,
1000000000000000000, 2000000000000000000, 3000000000000000000, 4000000000000000000, 5000000000000000000, 6000000000000000000, 7000000000000000000, 8000000000000000000, 9000000000000000000,
10000000000000000000
];
/// Runtime initialized [`H`] generator.
static H_PRECOMP: LazyLock<VartimeEdwardsPrecomputation> =
LazyLock::new(|| VartimeEdwardsPrecomputation::new([*H, ED25519_BASEPOINT_POINT]));
/// Runtime initialized zero commitment lookup table
///
/// # Invariant
/// This function assumes that the [`ZERO_COMMITMENT_DECOMPOSED_AMOUNT`]
/// table is sorted.
pub static ZERO_COMMITMENT_LOOKUP_TABLE: LazyLock<[EdwardsPoint; 172]> = LazyLock::new(|| {
let mut lookup_table: [EdwardsPoint; 172] = [ED25519_BASEPOINT_POINT; 172];
for (i, amount) in ZERO_COMMITMENT_DECOMPOSED_AMOUNT.into_iter().enumerate() {
lookup_table[i] = ED25519_BASEPOINT_POINT + *H * Scalar::from(amount);
}
lookup_table
});
//---------------------------------------------------------------------------------------------------- Free functions
/// This function computes the zero commitment given a specific amount.
///
/// It will first attempt to lookup into the table of known Pre-RCT value.
/// Compute it otherwise.
#[expect(clippy::cast_possible_truncation)]
pub fn compute_zero_commitment(amount: u64) -> EdwardsPoint {
// OPTIMIZATION: Unlike monerod which execute a linear search across its lookup
// table (O(n)). Cuprate is making use of an arithmetic based constant time
// version (O(1)). It has been benchmarked in both hit and miss scenarios against
// a binary search lookup (O(log2(n))). To understand the following algorithm it
// is important to observe the pattern that follows the values of
// [`ZERO_COMMITMENT_DECOMPOSED_AMOUNT`].
// First obtain the logarithm base 10 of the amount. and extend it back to obtain
// the amount without its most significant digit.
let Some(log) = amount.checked_ilog10() else {
// amount = 0 so H component is 0.
return ED25519_BASEPOINT_POINT;
};
let div = 10_u64.pow(log);
// Extract the most significant digit.
let most_significant_digit = amount / div;
// If the *rounded* version is different than the exact amount. Then
// there aren't only trailing zeroes behind the most significant digit.
// The amount is not part of the table and can calculated apart.
if most_significant_digit * div != amount {
return H_PRECOMP.vartime_multiscalar_mul([Scalar::from(amount), Scalar::ONE]);
}
// Calculating the index back by progressing within the powers of 10.
// The index of the first value in the cached amount's row.
let row_start = u64::from(log) * 9;
// The index of the cached amount
let index = (most_significant_digit - 1 + row_start) as usize;
ZERO_COMMITMENT_LOOKUP_TABLE[index]
}
//---------------------------------------------------------------------------------------------------- Tests
#[cfg(test)]
mod test {
use curve25519_dalek::{traits::VartimePrecomputedMultiscalarMul, Scalar};
use crate::crypto::{compute_zero_commitment, H_PRECOMP, ZERO_COMMITMENT_DECOMPOSED_AMOUNT};
#[test]
/// Compare the output of `compute_zero_commitment` for all
/// preRCT decomposed amounts against their actual computation.
///
/// Assert that the lookup table returns the correct commitments
fn compare_lookup_with_computation() {
for amount in ZERO_COMMITMENT_DECOMPOSED_AMOUNT {
let commitment = H_PRECOMP.vartime_multiscalar_mul([Scalar::from(amount), Scalar::ONE]);
assert!(commitment == compute_zero_commitment(amount));
}
}
}

View file

@ -30,6 +30,9 @@ pub mod time;
#[cfg(feature = "tx")] #[cfg(feature = "tx")]
pub mod tx; pub mod tx;
#[cfg(feature = "crypto")]
pub mod crypto;
//---------------------------------------------------------------------------------------------------- Private Usage //---------------------------------------------------------------------------------------------------- Private Usage
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------

View file

@ -15,8 +15,8 @@ default = ["std"]
std = ["dep:thiserror", "bytes/std", "cuprate-fixed-bytes/std"] std = ["dep:thiserror", "bytes/std", "cuprate-fixed-bytes/std"]
[dependencies] [dependencies]
cuprate-helper = { path = "../../helper", default-features = false, features = ["cast"] } cuprate-helper = { workspace = true, default-features = false, features = ["cast"] }
cuprate-fixed-bytes = { path = "../fixed-bytes", default-features = false } cuprate-fixed-bytes = { workspace = true, default-features = false }
paste = "1.0.15" paste = "1.0.15"
ref-cast = "1.0.23" ref-cast = "1.0.23"
@ -27,4 +27,4 @@ thiserror = { workspace = true, optional = true}
hex = { workspace = true, features = ["default"] } hex = { workspace = true, features = ["default"] }
[lints] [lints]
workspace = true workspace = true

View file

@ -12,7 +12,7 @@ default = []
tracing = ["dep:tracing", "tokio-util/tracing"] tracing = ["dep:tracing", "tokio-util/tracing"]
[dependencies] [dependencies]
cuprate-helper = { path = "../../helper", default-features = false, features = ["cast"] } cuprate-helper = { workspace = true, default-features = false, features = ["cast"] }
cfg-if = { workspace = true } cfg-if = { workspace = true }
thiserror = { workspace = true } thiserror = { workspace = true }
@ -30,4 +30,4 @@ tokio = { workspace = true, features = ["full"] }
futures = { workspace = true, features = ["std"] } futures = { workspace = true, features = ["std"] }
[lints] [lints]
workspace = true workspace = true

View file

@ -11,11 +11,11 @@ default = []
tracing = ["cuprate-levin/tracing"] tracing = ["cuprate-levin/tracing"]
[dependencies] [dependencies]
cuprate-levin = { path = "../levin" } cuprate-levin = { workspace = true }
cuprate-epee-encoding = { path = "../epee-encoding" } cuprate-epee-encoding = { workspace = true }
cuprate-fixed-bytes = { path = "../fixed-bytes" } cuprate-fixed-bytes = { workspace = true }
cuprate-types = { path = "../../types", default-features = false, features = ["epee"] } cuprate-types = { workspace = true, default-features = false, features = ["epee"] }
cuprate-helper = { path = "../../helper", default-features = false, features = ["map"] } cuprate-helper = { workspace = true, default-features = false, features = ["map"] }
bitflags = { workspace = true, features = ["std"] } bitflags = { workspace = true, features = ["std"] }
bytes = { workspace = true, features = ["std"] } bytes = { workspace = true, features = ["std"] }

View file

@ -7,9 +7,9 @@ authors = ["Boog900"]
[dependencies] [dependencies]
cuprate-constants = { path = "../../constants" } cuprate-constants = { workspace = true }
cuprate-pruning = { path = "../../pruning" } cuprate-pruning = { workspace = true }
cuprate-p2p-core = { path = "../p2p-core" } cuprate-p2p-core = { workspace = true }
tower = { workspace = true, features = ["util"] } tower = { workspace = true, features = ["util"] }
tokio = { workspace = true, features = ["time", "fs", "rt"]} tokio = { workspace = true, features = ["time", "fs", "rt"]}
@ -26,9 +26,9 @@ rand = { workspace = true, features = ["std", "std_rng"] }
borsh = { workspace = true, features = ["derive", "std"]} borsh = { workspace = true, features = ["derive", "std"]}
[dev-dependencies] [dev-dependencies]
cuprate-test-utils = {path = "../../test-utils"} cuprate-test-utils = { workspace = true }
tokio = { workspace = true, features = ["rt-multi-thread", "macros"]} tokio = { workspace = true, features = ["rt-multi-thread", "macros"]}
[lints] [lints]
workspace = true workspace = true

View file

@ -74,7 +74,10 @@ pub enum TxState<Id> {
} }
impl<Id> TxState<Id> { impl<Id> TxState<Id> {
pub const fn state_stem(&self) -> bool { /// Returns `true` if the tx is in the stem stage.
///
/// [`TxState::Local`] & [`TxState::Stem`] are the 2 stem stage states.
pub const fn is_stem_stage(&self) -> bool {
matches!(self, Self::Local | Self::Stem { .. }) matches!(self, Self::Local | Self::Stem { .. })
} }
} }

View file

@ -10,9 +10,9 @@ default = ["borsh"]
borsh = ["dep:borsh", "cuprate-pruning/borsh"] borsh = ["dep:borsh", "cuprate-pruning/borsh"]
[dependencies] [dependencies]
cuprate-helper = { path = "../../helper", features = ["asynch"], default-features = false } cuprate-helper = { workspace = true, features = ["asynch"], default-features = false }
cuprate-wire = { path = "../../net/wire", features = ["tracing"] } cuprate-wire = { workspace = true, features = ["tracing"] }
cuprate-pruning = { path = "../../pruning" } cuprate-pruning = { workspace = true }
tokio = { workspace = true, features = ["net", "sync", "macros", "time", "rt", "rt-multi-thread"]} tokio = { workspace = true, features = ["net", "sync", "macros", "time", "rt", "rt-multi-thread"]}
tokio-util = { workspace = true, features = ["codec"] } tokio-util = { workspace = true, features = ["codec"] }
@ -29,10 +29,10 @@ hex-literal = { workspace = true }
borsh = { workspace = true, features = ["derive", "std"], optional = true } borsh = { workspace = true, features = ["derive", "std"], optional = true }
[dev-dependencies] [dev-dependencies]
cuprate-test-utils = { path = "../../test-utils" } cuprate-test-utils = { workspace = true }
hex = { workspace = true, features = ["std"] } hex = { workspace = true, features = ["std"] }
tokio-test = { workspace = true } tokio-test = { workspace = true }
[lints] [lints]
workspace = true workspace = true

View file

@ -6,15 +6,15 @@ license = "MIT"
authors = ["Boog900"] authors = ["Boog900"]
[dependencies] [dependencies]
cuprate-constants = { path = "../../constants" } cuprate-constants = { workspace = true }
cuprate-fixed-bytes = { path = "../../net/fixed-bytes" } cuprate-fixed-bytes = { workspace = true }
cuprate-wire = { path = "../../net/wire" } cuprate-wire = { workspace = true }
cuprate-p2p-core = { path = "../p2p-core", features = ["borsh"] } cuprate-p2p-core = { workspace = true, features = ["borsh"] }
cuprate-address-book = { path = "../address-book" } cuprate-address-book = { workspace = true }
cuprate-pruning = { path = "../../pruning" } cuprate-pruning = { workspace = true }
cuprate-helper = { path = "../../helper", features = ["asynch"], default-features = false } cuprate-helper = { workspace = true, features = ["asynch"], default-features = false }
cuprate-async-buffer = { path = "../async-buffer" } cuprate-async-buffer = { workspace = true }
cuprate-types = { path = "../../types", default-features = false } cuprate-types = { workspace = true, default-features = false }
monero-serai = { workspace = true, features = ["std"] } monero-serai = { workspace = true, features = ["std"] }
@ -35,10 +35,10 @@ tracing = { workspace = true, features = ["std", "attributes"] }
borsh = { workspace = true, features = ["derive", "std"] } borsh = { workspace = true, features = ["derive", "std"] }
[dev-dependencies] [dev-dependencies]
cuprate-test-utils = { path = "../../test-utils" } cuprate-test-utils = { workspace = true }
indexmap = { workspace = true } indexmap = { workspace = true }
proptest = { workspace = true } proptest = { workspace = true }
tokio-test = { workspace = true } tokio-test = { workspace = true }
[lints] [lints]
workspace = true workspace = true

View file

@ -24,7 +24,7 @@ use cuprate_p2p_core::{
pub(crate) mod disconnect_monitor; pub(crate) mod disconnect_monitor;
mod drop_guard_client; mod drop_guard_client;
pub(crate) use drop_guard_client::ClientPoolDropGuard; pub use drop_guard_client::ClientPoolDropGuard;
/// The client pool, which holds currently connected free peers. /// The client pool, which holds currently connected free peers.
/// ///
@ -166,14 +166,15 @@ impl<N: NetworkZone> ClientPool<N> {
}) })
} }
pub fn outbound_client(&self) -> Option<Client<N>> { /// Returns the first outbound peer when iterating over the peers.
pub fn outbound_client(self: &Arc<Self>) -> Option<ClientPoolDropGuard<N>> {
let client = self let client = self
.clients .clients
.iter() .iter()
.find(|element| element.value().info.direction == ConnectionDirection::Outbound)?; .find(|element| element.value().info.direction == ConnectionDirection::Outbound)?;
let id = *client.key(); let id = *client.key();
Some(self.clients.remove(&id).unwrap().1) Some(self.borrow_client(&id).unwrap())
} }
} }

View file

@ -18,7 +18,7 @@ use cuprate_p2p_core::{
pub mod block_downloader; pub mod block_downloader;
mod broadcast; mod broadcast;
mod client_pool; pub mod client_pool;
pub mod config; pub mod config;
pub mod connection_maintainer; pub mod connection_maintainer;
pub mod constants; pub mod constants;
@ -26,6 +26,7 @@ mod inbound_server;
use block_downloader::{BlockBatch, BlockDownloaderConfig, ChainSvcRequest, ChainSvcResponse}; use block_downloader::{BlockBatch, BlockDownloaderConfig, ChainSvcRequest, ChainSvcResponse};
pub use broadcast::{BroadcastRequest, BroadcastSvc}; pub use broadcast::{BroadcastRequest, BroadcastSvc};
pub use client_pool::{ClientPool, ClientPoolDropGuard};
pub use config::{AddressBookConfig, P2PConfig}; pub use config::{AddressBookConfig, P2PConfig};
use connection_maintainer::MakeConnectionRequest; use connection_maintainer::MakeConnectionRequest;
@ -82,7 +83,7 @@ where
let outbound_handshaker = outbound_handshaker_builder.build(); let outbound_handshaker = outbound_handshaker_builder.build();
let client_pool = client_pool::ClientPool::new(); let client_pool = ClientPool::new();
let (make_connection_tx, make_connection_rx) = mpsc::channel(3); let (make_connection_tx, make_connection_rx) = mpsc::channel(3);
@ -132,7 +133,7 @@ where
#[derive(Clone)] #[derive(Clone)]
pub struct NetworkInterface<N: NetworkZone> { pub struct NetworkInterface<N: NetworkZone> {
/// A pool of free connected peers. /// A pool of free connected peers.
pool: Arc<client_pool::ClientPool<N>>, pool: Arc<ClientPool<N>>,
/// A [`Service`] that allows broadcasting to all connected peers. /// A [`Service`] that allows broadcasting to all connected peers.
broadcast_svc: BroadcastSvc<N>, broadcast_svc: BroadcastSvc<N>,
/// A channel to request extra connections. /// A channel to request extra connections.
@ -173,7 +174,7 @@ impl<N: NetworkZone> NetworkInterface<N> {
} }
/// Borrows the `ClientPool`, for access to connected peers. /// Borrows the `ClientPool`, for access to connected peers.
pub const fn client_pool(&self) -> &Arc<client_pool::ClientPool<N>> { pub const fn client_pool(&self) -> &Arc<ClientPool<N>> {
&self.pool &self.pool
} }
} }

View file

@ -10,11 +10,11 @@ default = []
borsh = ["dep:borsh"] borsh = ["dep:borsh"]
[dependencies] [dependencies]
cuprate-constants = { path = "../constants" } cuprate-constants = { workspace = true }
thiserror = { workspace = true } thiserror = { workspace = true }
borsh = { workspace = true, features = ["derive", "std"], optional = true } borsh = { workspace = true, features = ["derive", "std"], optional = true }
[lints] [lints]
workspace = true workspace = true

View file

@ -13,10 +13,10 @@ default = ["dummy", "serde"]
dummy = [] dummy = []
[dependencies] [dependencies]
cuprate-epee-encoding = { path = "../../net/epee-encoding", default-features = false } cuprate-epee-encoding = { workspace = true, default-features = false }
cuprate-json-rpc = { path = "../json-rpc", default-features = false } cuprate-json-rpc = { workspace = true, default-features = false }
cuprate-rpc-types = { path = "../types", features = ["serde", "epee"], default-features = false } cuprate-rpc-types = { workspace = true, features = ["serde", "epee"], default-features = false }
cuprate-helper = { path = "../../helper", features = ["asynch"], default-features = false } cuprate-helper = { workspace = true, features = ["asynch"], default-features = false }
anyhow = { workspace = true } anyhow = { workspace = true }
axum = { version = "0.7.5", features = ["json"], default-features = false } axum = { version = "0.7.5", features = ["json"], default-features = false }
@ -26,7 +26,7 @@ paste = { workspace = true }
futures = { workspace = true } futures = { workspace = true }
[dev-dependencies] [dev-dependencies]
cuprate-test-utils = { path = "../../test-utils" } cuprate-test-utils = { workspace = true }
axum = { version = "0.7.5", features = ["json", "tokio", "http2"] } axum = { version = "0.7.5", features = ["json", "tokio", "http2"] }
serde_json = { workspace = true, features = ["std"] } serde_json = { workspace = true, features = ["std"] }

View file

@ -14,18 +14,18 @@ serde = ["dep:serde", "cuprate-fixed-bytes/serde"]
epee = ["dep:cuprate-epee-encoding"] epee = ["dep:cuprate-epee-encoding"]
[dependencies] [dependencies]
cuprate-epee-encoding = { path = "../../net/epee-encoding", optional = true } cuprate-epee-encoding = { workspace = true, optional = true }
cuprate-fixed-bytes = { path = "../../net/fixed-bytes" } cuprate-fixed-bytes = { workspace = true }
cuprate-types = { path = "../../types", default-features = false, features = ["epee", "serde"] } cuprate-types = { workspace = true, default-features = false, features = ["epee", "serde"] }
paste = { workspace = true } paste = { workspace = true }
serde = { workspace = true, optional = true } serde = { workspace = true, optional = true }
[dev-dependencies] [dev-dependencies]
cuprate-test-utils = { path = "../../test-utils" } cuprate-test-utils = { workspace = true }
serde = { workspace = true } serde = { workspace = true }
serde_json = { workspace = true } serde_json = { workspace = true }
[lints] [lints]
workspace = true workspace = true

View file

@ -18,11 +18,11 @@ redb-memory = ["cuprate-database/redb-memory"]
service = ["dep:thread_local", "dep:rayon", "cuprate-helper/thread"] service = ["dep:thread_local", "dep:rayon", "cuprate-helper/thread"]
[dependencies] [dependencies]
cuprate-database = { path = "../database" } cuprate-database = { workspace = true }
cuprate-database-service = { path = "../service" } cuprate-database-service = { workspace = true }
cuprate-helper = { path = "../../helper", features = ["fs", "map"] } cuprate-helper = { workspace = true, features = ["fs", "map", "crypto"] }
cuprate-types = { path = "../../types", features = ["blockchain"] } cuprate-types = { workspace = true, features = ["blockchain"] }
cuprate-pruning = { path = "../../pruning" } cuprate-pruning = { workspace = true }
bitflags = { workspace = true, features = ["std", "serde", "bytemuck"] } bitflags = { workspace = true, features = ["std", "serde", "bytemuck"] }
bytemuck = { workspace = true, features = ["must_cast", "derive", "min_const_generics", "extern_crate_alloc"] } bytemuck = { workspace = true, features = ["must_cast", "derive", "min_const_generics", "extern_crate_alloc"] }
@ -38,9 +38,9 @@ rayon = { workspace = true, optional = true }
bytes = "1.7.2" bytes = "1.7.2"
[dev-dependencies] [dev-dependencies]
cuprate-constants = { path = "../../constants" } cuprate-constants = { workspace = true }
cuprate-helper = { path = "../../helper", features = ["thread", "cast"] } cuprate-helper = { workspace = true, features = ["thread", "cast"] }
cuprate-test-utils = { path = "../../test-utils" } cuprate-test-utils = { workspace = true }
tokio = { workspace = true, features = ["full"] } tokio = { workspace = true, features = ["full"] }
tempfile = { workspace = true } tempfile = { workspace = true }

View file

@ -1,12 +1,13 @@
//! Output functions. //! Output functions.
//---------------------------------------------------------------------------------------------------- Import //---------------------------------------------------------------------------------------------------- Import
use curve25519_dalek::{constants::ED25519_BASEPOINT_POINT, edwards::CompressedEdwardsY, Scalar}; use curve25519_dalek::edwards::CompressedEdwardsY;
use monero_serai::{generators::H, transaction::Timelock}; use monero_serai::transaction::Timelock;
use cuprate_database::{ use cuprate_database::{
RuntimeError, {DatabaseRo, DatabaseRw}, RuntimeError, {DatabaseRo, DatabaseRw},
}; };
use cuprate_helper::crypto::compute_zero_commitment;
use cuprate_helper::map::u64_to_timelock; use cuprate_helper::map::u64_to_timelock;
use cuprate_types::OutputOnChain; use cuprate_types::OutputOnChain;
@ -155,9 +156,7 @@ pub fn output_to_output_on_chain(
amount: Amount, amount: Amount,
table_tx_unlock_time: &impl DatabaseRo<TxUnlockTime>, table_tx_unlock_time: &impl DatabaseRo<TxUnlockTime>,
) -> Result<OutputOnChain, RuntimeError> { ) -> Result<OutputOnChain, RuntimeError> {
// FIXME: implement lookup table for common values: let commitment = compute_zero_commitment(amount);
// <https://github.com/monero-project/monero/blob/c8214782fb2a769c57382a999eaf099691c836e7/src/ringct/rctOps.cpp#L322>
let commitment = ED25519_BASEPOINT_POINT + *H * Scalar::from(amount);
let time_lock = if output let time_lock = if output
.output_flags .output_flags

View file

@ -2,10 +2,10 @@
//---------------------------------------------------------------------------------------------------- Import //---------------------------------------------------------------------------------------------------- Import
use bytemuck::TransparentWrapper; use bytemuck::TransparentWrapper;
use curve25519_dalek::{constants::ED25519_BASEPOINT_POINT, Scalar};
use monero_serai::transaction::{Input, Timelock, Transaction}; use monero_serai::transaction::{Input, Timelock, Transaction};
use cuprate_database::{DatabaseRo, DatabaseRw, RuntimeError, StorableVec}; use cuprate_database::{DatabaseRo, DatabaseRw, RuntimeError, StorableVec};
use cuprate_helper::crypto::compute_zero_commitment;
use crate::{ use crate::{
ops::{ ops::{
@ -136,12 +136,9 @@ pub fn add_tx(
.enumerate() .enumerate()
.map(|(i, output)| { .map(|(i, output)| {
// Create commitment. // Create commitment.
// <https://github.com/Cuprate/cuprate/pull/102#discussion_r1559489302>
// FIXME: implement lookup table for common values:
// <https://github.com/monero-project/monero/blob/c8214782fb2a769c57382a999eaf099691c836e7/src/ringct/rctOps.cpp#L322>
let commitment = if miner_tx { let commitment = if miner_tx {
ED25519_BASEPOINT_POINT compute_zero_commitment(output.amount.unwrap_or(0))
+ *monero_serai::generators::H * Scalar::from(output.amount.unwrap_or(0))
} else { } else {
proofs proofs
.as_ref() .as_ref()

View file

@ -9,8 +9,8 @@ repository = "https://github.com/Cuprate/cuprate/tree/main/storage/service"
keywords = ["cuprate", "service", "database"] keywords = ["cuprate", "service", "database"]
[dependencies] [dependencies]
cuprate-database = { path = "../database" } cuprate-database = { workspace = true }
cuprate-helper = { path = "../../helper", features = ["fs", "thread", "map"] } cuprate-helper = { workspace = true, features = ["fs", "thread", "map"] }
serde = { workspace = true, optional = true } serde = { workspace = true, optional = true }
rayon = { workspace = true } rayon = { workspace = true }

View file

@ -19,17 +19,17 @@ service = ["dep:tower", "dep:rayon", "dep:cuprate-database-service"]
serde = ["dep:serde", "cuprate-database/serde", "cuprate-database-service/serde"] serde = ["dep:serde", "cuprate-database/serde", "cuprate-database-service/serde"]
[dependencies] [dependencies]
cuprate-database = { path = "../database", features = ["heed"] } cuprate-database = { workspace = true, features = ["heed"] }
cuprate-database-service = { path = "../service", optional = true } cuprate-database-service = { workspace = true, optional = true }
cuprate-types = { path = "../../types" } cuprate-types = { workspace = true }
cuprate-helper = { path = "../../helper", default-features = false, features = ["constants"] } cuprate-helper = { workspace = true, default-features = false, features = ["constants"] }
monero-serai = { workspace = true, features = ["std"] } monero-serai = { workspace = true, features = ["std"] }
bytemuck = { workspace = true, features = ["must_cast", "derive", "min_const_generics", "extern_crate_alloc"] } bytemuck = { workspace = true, features = ["must_cast", "derive", "min_const_generics", "extern_crate_alloc"] }
bitflags = { workspace = true, features = ["std", "serde", "bytemuck"] } bitflags = { workspace = true, features = ["std", "serde", "bytemuck"] }
thiserror = { workspace = true } thiserror = { workspace = true }
hex = { workspace = true } hex = { workspace = true }
sha3 = { workspace = true, features = ["std"] } blake3 = { workspace = true, features = ["std"] }
tower = { workspace = true, optional = true } tower = { workspace = true, optional = true }
rayon = { workspace = true, optional = true } rayon = { workspace = true, optional = true }
@ -37,11 +37,11 @@ rayon = { workspace = true, optional = true }
serde = { workspace = true, optional = true } serde = { workspace = true, optional = true }
[dev-dependencies] [dev-dependencies]
cuprate-test-utils = { path = "../../test-utils" } cuprate-test-utils = { workspace = true }
tokio = { workspace = true } tokio = { workspace = true }
tempfile = { workspace = true } tempfile = { workspace = true }
hex-literal = { workspace = true } hex-literal = { workspace = true }
[lints] [lints]
workspace = true workspace = true

View file

@ -1,8 +1,6 @@
//! General free functions (related to the tx-pool database). //! General free functions (related to the tx-pool database).
//---------------------------------------------------------------------------------------------------- Import //---------------------------------------------------------------------------------------------------- Import
use sha3::{Digest, Sha3_256};
use cuprate_database::{ConcreteEnv, Env, EnvInner, InitError, RuntimeError, TxRw}; use cuprate_database::{ConcreteEnv, Env, EnvInner, InitError, RuntimeError, TxRw};
use crate::{config::Config, tables::OpenTables, types::TransactionBlobHash}; use crate::{config::Config, tables::OpenTables, types::TransactionBlobHash};
@ -70,7 +68,5 @@ pub fn open(config: Config) -> Result<ConcreteEnv, InitError> {
/// The exact way the hash is calculated is not stable and is subject to change, as such it should not be exposed /// The exact way the hash is calculated is not stable and is subject to change, as such it should not be exposed
/// as a way to interact with Cuprate externally. /// as a way to interact with Cuprate externally.
pub fn transaction_blob_hash(tx_blob: &[u8]) -> TransactionBlobHash { pub fn transaction_blob_hash(tx_blob: &[u8]) -> TransactionBlobHash {
let mut hasher = Sha3_256::new(); blake3::hash(tx_blob).into()
hasher.update(tx_blob);
hasher.finalize().into()
} }

View file

@ -8,9 +8,10 @@ use monero_serai::transaction::Transaction;
use cuprate_database::{DatabaseRo, RuntimeError}; use cuprate_database::{DatabaseRo, RuntimeError};
use cuprate_types::{TransactionVerificationData, TxVersion}; use cuprate_types::{TransactionVerificationData, TxVersion};
use crate::tables::TransactionInfos; use crate::{
use crate::types::TxStateFlags; tables::{Tables, TransactionInfos},
use crate::{tables::Tables, types::TransactionHash}; types::{TransactionHash, TxStateFlags},
};
/// Gets the [`TransactionVerificationData`] of a transaction in the tx-pool, leaving the tx in the pool. /// Gets the [`TransactionVerificationData`] of a transaction in the tx-pool, leaving the tx in the pool.
pub fn get_transaction_verification_data( pub fn get_transaction_verification_data(
@ -37,6 +38,10 @@ pub fn get_transaction_verification_data(
}) })
} }
/// Returns `true` if the transaction with the given hash is in the stem pool.
///
/// # Errors
/// This will return an [`Err`] if the transaction is not in the pool.
pub fn in_stem_pool( pub fn in_stem_pool(
tx_hash: &TransactionHash, tx_hash: &TransactionHash,
tx_infos: &impl DatabaseRo<TransactionInfos>, tx_infos: &impl DatabaseRo<TransactionInfos>,

View file

@ -17,18 +17,18 @@ use crate::{
/// The transaction pool [`tower::Service`] read request type. /// The transaction pool [`tower::Service`] read request type.
#[derive(Clone)] #[derive(Clone)]
pub enum TxpoolReadRequest { pub enum TxpoolReadRequest {
/// A request for the blob (raw bytes) of a transaction with the given hash. /// Get the blob (raw bytes) of a transaction with the given hash.
TxBlob(TransactionHash), TxBlob(TransactionHash),
/// A request for the [`TransactionVerificationData`] of a transaction in the tx pool. /// Get the [`TransactionVerificationData`] of a transaction in the tx pool.
TxVerificationData(TransactionHash), TxVerificationData(TransactionHash),
/// A request to filter (remove) all **known** transactions from the set. /// Filter (remove) all **known** transactions from the set.
/// ///
/// The hash is **not** the transaction hash, it is the hash of the serialized tx-blob. /// The hash is **not** the transaction hash, it is the hash of the serialized tx-blob.
FilterKnownTxBlobHashes(HashSet<TransactionBlobHash>), FilterKnownTxBlobHashes(HashSet<TransactionBlobHash>),
/// A request to pull some transactions for an incoming block. /// Get some transactions for an incoming block.
TxsForBlock(Vec<TransactionHash>), TxsForBlock(Vec<TransactionHash>),
/// Get information on all transactions in the pool. /// Get information on all transactions in the pool.
@ -42,10 +42,10 @@ pub enum TxpoolReadRequest {
/// The transaction pool [`tower::Service`] read response type. /// The transaction pool [`tower::Service`] read response type.
#[expect(clippy::large_enum_variant)] #[expect(clippy::large_enum_variant)]
pub enum TxpoolReadResponse { pub enum TxpoolReadResponse {
/// A response containing the raw bytes of a transaction. /// The response for [`TxpoolReadRequest::TxBlob`].
TxBlob { tx_blob: Vec<u8>, state_stem: bool }, TxBlob { tx_blob: Vec<u8>, state_stem: bool },
/// A response of [`TransactionVerificationData`]. /// The response for [`TxpoolReadRequest::TxVerificationData`].
TxVerificationData(TransactionVerificationData), TxVerificationData(TransactionVerificationData),
/// The response for [`TxpoolReadRequest::FilterKnownTxBlobHashes`]. /// The response for [`TxpoolReadRequest::FilterKnownTxBlobHashes`].
@ -94,14 +94,10 @@ pub enum TxpoolWriteRequest {
}, },
/// Remove a transaction with the given hash from the pool. /// Remove a transaction with the given hash from the pool.
///
/// Returns [`TxpoolWriteResponse::Ok`].
RemoveTransaction(TransactionHash), RemoveTransaction(TransactionHash),
/// Promote a transaction from the stem pool to the fluff pool. /// Promote a transaction from the stem pool to the fluff pool.
/// If the tx is already in the fluff pool this does nothing. /// If the tx is already in the fluff pool this does nothing.
///
/// Returns [`TxpoolWriteResponse::Ok`].
Promote(TransactionHash), Promote(TransactionHash),
/// Tell the tx-pool about a new block. /// Tell the tx-pool about a new block.
@ -117,6 +113,8 @@ pub enum TxpoolWriteRequest {
pub enum TxpoolWriteResponse { pub enum TxpoolWriteResponse {
/// Response to: /// Response to:
/// - [`TxpoolWriteRequest::RemoveTransaction`] /// - [`TxpoolWriteRequest::RemoveTransaction`]
/// - [`TxpoolWriteRequest::Promote`]
/// - [`TxpoolWriteRequest::NewBlock`]
Ok, Ok,
/// Response to [`TxpoolWriteRequest::AddTransaction`]. /// Response to [`TxpoolWriteRequest::AddTransaction`].

View file

@ -21,7 +21,7 @@ use crate::{
types::{ReadResponseResult, TxpoolReadHandle}, types::{ReadResponseResult, TxpoolReadHandle},
}, },
tables::{KnownBlobHashes, OpenTables, TransactionBlobs, TransactionInfos}, tables::{KnownBlobHashes, OpenTables, TransactionBlobs, TransactionInfos},
types::{TransactionBlobHash, TransactionHash, TxStateFlags}, types::{TransactionBlobHash, TransactionHash},
}; };
// TODO: update the docs here // TODO: update the docs here
@ -102,11 +102,10 @@ fn tx_blob(env: &ConcreteEnv, tx_hash: &TransactionHash) -> ReadResponseResult {
let tx_infos_table = inner_env.open_db_ro::<TransactionInfos>(&tx_ro)?; let tx_infos_table = inner_env.open_db_ro::<TransactionInfos>(&tx_ro)?;
let tx_blob = tx_blobs_table.get(tx_hash)?.0; let tx_blob = tx_blobs_table.get(tx_hash)?.0;
let tx_info = tx_infos_table.get(tx_hash)?;
Ok(TxpoolReadResponse::TxBlob { Ok(TxpoolReadResponse::TxBlob {
tx_blob, tx_blob,
state_stem: tx_info.flags.contains(TxStateFlags::STATE_STEM), state_stem: in_stem_pool(tx_hash, &tx_infos_table)?,
}) })
} }
@ -134,7 +133,7 @@ fn filter_known_tx_blob_hashes(
let mut stem_pool_hashes = Vec::new(); let mut stem_pool_hashes = Vec::new();
// A closure that returns if a tx with a certain blob hash is unknown. // A closure that returns `true` if a tx with a certain blob hash is unknown.
// This also fills in `stem_tx_hashes`. // This also fills in `stem_tx_hashes`.
let mut tx_unknown = |blob_hash| -> Result<bool, RuntimeError> { let mut tx_unknown = |blob_hash| -> Result<bool, RuntimeError> {
match tx_blob_hashes.get(&blob_hash) { match tx_blob_hashes.get(&blob_hash) {

View file

@ -112,19 +112,28 @@ fn promote(
let env_inner = env.env_inner(); let env_inner = env.env_inner();
let tx_rw = env_inner.tx_rw()?; let tx_rw = env_inner.tx_rw()?;
let mut tx_infos = env_inner.open_db_rw::<TransactionInfos>(&tx_rw)?; let res = || {
let mut tx_infos = env_inner.open_db_rw::<TransactionInfos>(&tx_rw)?;
tx_infos.update(tx_hash, |mut info| { tx_infos.update(tx_hash, |mut info| {
info.flags.remove(TxStateFlags::STATE_STEM); info.flags.remove(TxStateFlags::STATE_STEM);
Some(info) Some(info)
})?; })
};
drop(tx_infos); if let Err(e) = res() {
// error promoting the tx, abort the DB transaction.
TxRw::abort(tx_rw)
.expect("could not maintain database atomicity by aborting write transaction");
return Err(e);
}
TxRw::commit(tx_rw)?; TxRw::commit(tx_rw)?;
Ok(TxpoolWriteResponse::Ok) Ok(TxpoolWriteResponse::Ok)
} }
/// [`TxpoolWriteRequest::NewBlock`]
fn new_block( fn new_block(
env: &ConcreteEnv, env: &ConcreteEnv,
spent_key_images: &[KeyImage], spent_key_images: &[KeyImage],
@ -136,6 +145,7 @@ fn new_block(
let result = || { let result = || {
let mut tables_mut = env_inner.open_tables_mut(&tx_rw)?; let mut tables_mut = env_inner.open_tables_mut(&tx_rw)?;
// Remove all txs which spend key images that were spent in the new block.
for key_image in spent_key_images { for key_image in spent_key_images {
match tables_mut match tables_mut
.spent_key_images() .spent_key_images()

View file

@ -6,10 +6,10 @@ license = "MIT"
authors = ["Boog900", "hinto-janai"] authors = ["Boog900", "hinto-janai"]
[dependencies] [dependencies]
cuprate-types = { path = "../types" } cuprate-types = { workspace = true }
cuprate-helper = { path = "../helper", features = ["map", "tx"] } cuprate-helper = { workspace = true, features = ["map", "tx"] }
cuprate-wire = { path = "../net/wire" } cuprate-wire = { workspace = true }
cuprate-p2p-core = { path = "../p2p/p2p-core", features = ["borsh"] } cuprate-p2p-core = { workspace = true, features = ["borsh"] }
hex = { workspace = true } hex = { workspace = true }
hex-literal = { workspace = true } hex-literal = { workspace = true }
@ -31,4 +31,4 @@ hex = { workspace = true }
pretty_assertions = { workspace = true } pretty_assertions = { workspace = true }
[lints] [lints]
workspace = true workspace = true

View file

@ -18,9 +18,9 @@ json = ["hex", "dep:cuprate-helper"]
hex = ["dep:hex"] hex = ["dep:hex"]
[dependencies] [dependencies]
cuprate-epee-encoding = { path = "../net/epee-encoding", optional = true } cuprate-epee-encoding = { workspace = true, optional = true, features = ["std"] }
cuprate-helper = { path = "../helper", optional = true, features = ["cast"] } cuprate-helper = { workspace = true, optional = true, features = ["cast"] }
cuprate-fixed-bytes = { path = "../net/fixed-bytes" } cuprate-fixed-bytes = { workspace = true }
bytes = { workspace = true } bytes = { workspace = true }
curve25519-dalek = { workspace = true } curve25519-dalek = { workspace = true }
@ -39,4 +39,4 @@ pretty_assertions = { workspace = true }
serde_json = { workspace = true, features = ["std"] } serde_json = { workspace = true, features = ["std"] }
[lints] [lints]
workspace = true workspace = true