apply diffs

This commit is contained in:
hinto.janai 2024-10-31 20:30:56 -04:00
parent b57ee2f4cf
commit 2ee3bc34de
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
13 changed files with 35 additions and 54 deletions

2
Cargo.lock generated
View file

@ -712,6 +712,7 @@ dependencies = [
"bytemuck", "bytemuck",
"bytes", "bytes",
"cfg-if", "cfg-if",
"cuprate-helper",
"heed", "heed",
"page_size", "page_size",
"paste", "paste",
@ -884,6 +885,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"borsh", "borsh",
"cuprate-constants", "cuprate-constants",
"cuprate-helper",
"thiserror", "thiserror",
] ]

View file

@ -15,7 +15,7 @@ use tower::{Service, ServiceExt};
use cuprate_consensus::transactions::new_tx_verification_data; use cuprate_consensus::transactions::new_tx_verification_data;
use cuprate_consensus_context::{BlockChainContextRequest, BlockChainContextResponse}; use cuprate_consensus_context::{BlockChainContextRequest, BlockChainContextResponse};
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, usize_to_u64};
use cuprate_types::{VerifiedBlockInformation, VerifiedTransactionInformation}; use cuprate_types::{VerifiedBlockInformation, VerifiedTransactionInformation};
use crate::{hash_of_hashes, BlockId, HashOfHashes}; use crate::{hash_of_hashes, BlockId, HashOfHashes};
@ -38,7 +38,7 @@ const BATCH_SIZE: usize = 4;
#[inline] #[inline]
fn max_height() -> u64 { fn max_height() -> u64 {
(HASHES_OF_HASHES.len() * BATCH_SIZE) as u64 usize_to_u64(HASHES_OF_HASHES.len() * BATCH_SIZE)
} }
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]

View file

@ -4,6 +4,7 @@ use crypto_bigint::{CheckedMul, U256};
use monero_serai::block::Block; use monero_serai::block::Block;
use cuprate_cryptonight::*; use cuprate_cryptonight::*;
use cuprate_helper::cast::usize_to_u64;
use crate::{ use crate::{
check_block_version_vote, current_unix_timestamp, check_block_version_vote, current_unix_timestamp,
@ -89,8 +90,7 @@ pub fn calculate_pow_hash<R: RandomX>(
} else if hf < &HardFork::V10 { } else if hf < &HardFork::V10 {
cryptonight_hash_v2(buf) cryptonight_hash_v2(buf)
} else if hf < &HardFork::V12 { } else if hf < &HardFork::V12 {
// FIXME: https://github.com/Cuprate/cuprate/issues/167. cryptonight_hash_r(buf, usize_to_u64(height))
cryptonight_hash_r(buf, height as u64)
} else { } else {
randomx_vm randomx_vm
.expect("RandomX VM needed from hf 12") .expect("RandomX VM needed from hf 12")

View file

@ -11,6 +11,7 @@ borsh = ["dep:borsh"]
[dependencies] [dependencies]
cuprate-constants = { workspace = true } cuprate-constants = { workspace = true }
cuprate-helper = { workspace = true, features = ["cast"] }
thiserror = { workspace = true } thiserror = { workspace = true }

View file

@ -21,6 +21,7 @@
use std::cmp::Ordering; use std::cmp::Ordering;
use cuprate_constants::block::MAX_BLOCK_HEIGHT_USIZE; use cuprate_constants::block::MAX_BLOCK_HEIGHT_USIZE;
use cuprate_helper::cast::u32_to_usize;
use thiserror::Error; use thiserror::Error;
@ -368,7 +369,7 @@ impl DecompressedPruningSeed {
// amt_of_cycles * blocks in a cycle + how many blocks through a cycles until the seed starts storing blocks // amt_of_cycles * blocks in a cycle + how many blocks through a cycles until the seed starts storing blocks
let calculated_height = cycles_start * (CRYPTONOTE_PRUNING_STRIPE_SIZE << self.log_stripes) let calculated_height = cycles_start * (CRYPTONOTE_PRUNING_STRIPE_SIZE << self.log_stripes)
+ (self.stripe as usize - 1) * CRYPTONOTE_PRUNING_STRIPE_SIZE; + (u32_to_usize(self.stripe) - 1) * CRYPTONOTE_PRUNING_STRIPE_SIZE;
if calculated_height + CRYPTONOTE_PRUNING_TIP_BLOCKS > blockchain_height { if calculated_height + CRYPTONOTE_PRUNING_TIP_BLOCKS > blockchain_height {
// if our calculated height is greater than the amount of tip blocks then the start of the tip blocks will be the next un-pruned // if our calculated height is greater than the amount of tip blocks then the start of the tip blocks will be the next un-pruned
@ -436,15 +437,12 @@ const fn get_block_pruning_stripe(
if block_height + CRYPTONOTE_PRUNING_TIP_BLOCKS >= blockchain_height { if block_height + CRYPTONOTE_PRUNING_TIP_BLOCKS >= blockchain_height {
None None
} else { } else {
#[expect( let a = block_height / CRYPTONOTE_PRUNING_STRIPE_SIZE;
clippy::cast_possible_truncation, let b = u32_to_usize(1 << log_stripe) - 1;
clippy::cast_sign_loss, let c = (a & b) + 1;
reason = "it's trivial to prove it's ok to us `as` here"
)] #[expect(clippy::cast_possible_truncation)]
Some( Some(c as u32)
(((block_height / CRYPTONOTE_PRUNING_STRIPE_SIZE) & ((1 << log_stripe) as usize - 1))
+ 1) as u32,
)
} }
} }

View file

@ -20,7 +20,7 @@ service = ["dep:thread_local", "dep:rayon", "cuprate-helper/thread"]
[dependencies] [dependencies]
cuprate-database = { workspace = true } cuprate-database = { workspace = true }
cuprate-database-service = { workspace = true } cuprate-database-service = { workspace = true }
cuprate-helper = { workspace = true, features = ["fs", "map", "crypto"] } cuprate-helper = { workspace = true, features = ["fs", "map", "crypto", "cast"] }
cuprate-types = { workspace = true, features = ["blockchain"] } cuprate-types = { workspace = true, features = ["blockchain"] }
cuprate-pruning = { workspace = true } cuprate-pruning = { workspace = true }

View file

@ -4,17 +4,6 @@
clippy::significant_drop_tightening clippy::significant_drop_tightening
)] )]
// Only allow building 64-bit targets.
//
// This allows us to assume 64-bit
// invariants in code, e.g. `usize as u64`.
//
// # Safety
// As of 0d67bfb1bcc431e90c82d577bf36dd1182c807e2 (2024-04-12)
// there are invariants relying on 64-bit pointer sizes.
#[cfg(not(target_pointer_width = "64"))]
compile_error!("Cuprate is only compatible with 64-bit CPUs");
//---------------------------------------------------------------------------------------------------- Public API //---------------------------------------------------------------------------------------------------- Public API
// Import private modules, export public types. // Import private modules, export public types.
// //

View file

@ -2,6 +2,7 @@
//---------------------------------------------------------------------------------------------------- Import //---------------------------------------------------------------------------------------------------- Import
use cuprate_database::{DatabaseRo, RuntimeError}; use cuprate_database::{DatabaseRo, RuntimeError};
use cuprate_helper::cast::u64_to_usize;
use crate::{ use crate::{
ops::macros::doc_error, ops::macros::doc_error,
@ -25,8 +26,7 @@ use crate::{
pub fn chain_height( pub fn chain_height(
table_block_heights: &impl DatabaseRo<BlockHeights>, table_block_heights: &impl DatabaseRo<BlockHeights>,
) -> Result<BlockHeight, RuntimeError> { ) -> Result<BlockHeight, RuntimeError> {
#[expect(clippy::cast_possible_truncation, reason = "we enforce 64-bit")] table_block_heights.len().map(u64_to_usize)
table_block_heights.len().map(|height| height as usize)
} }
/// Retrieve the height of the top block. /// Retrieve the height of the top block.
@ -48,8 +48,7 @@ pub fn top_block_height(
) -> Result<BlockHeight, RuntimeError> { ) -> Result<BlockHeight, RuntimeError> {
match table_block_heights.len()? { match table_block_heights.len()? {
0 => Err(RuntimeError::KeyNotFound), 0 => Err(RuntimeError::KeyNotFound),
#[expect(clippy::cast_possible_truncation, reason = "we enforce 64-bit")] height => Ok(u64_to_usize(height) - 1),
height => Ok(height as usize - 1),
} }
} }

View file

@ -7,8 +7,7 @@ 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::{cast::u32_to_usize, crypto::compute_zero_commitment, map::u64_to_timelock};
use cuprate_helper::map::u64_to_timelock;
use cuprate_types::OutputOnChain; use cuprate_types::OutputOnChain;
use crate::{ use crate::{
@ -172,7 +171,7 @@ pub fn output_to_output_on_chain(
.unwrap_or(None); .unwrap_or(None);
Ok(OutputOnChain { Ok(OutputOnChain {
height: output.height as usize, height: u32_to_usize(output.height),
time_lock, time_lock,
key, key,
commitment, commitment,
@ -212,7 +211,7 @@ pub fn rct_output_to_output_on_chain(
.unwrap_or(None); .unwrap_or(None);
Ok(OutputOnChain { Ok(OutputOnChain {
height: rct_output.height as usize, height: u32_to_usize(rct_output.height),
time_lock, time_lock,
key, key,
commitment, commitment,

View file

@ -23,7 +23,7 @@ use thread_local::ThreadLocal;
use cuprate_database::{ConcreteEnv, DatabaseRo, Env, EnvInner, RuntimeError}; use cuprate_database::{ConcreteEnv, DatabaseRo, Env, EnvInner, RuntimeError};
use cuprate_database_service::{init_thread_pool, DatabaseReadService, ReaderThreads}; use cuprate_database_service::{init_thread_pool, DatabaseReadService, ReaderThreads};
use cuprate_helper::map::combine_low_high_bits_to_u128; use cuprate_helper::{cast::u64_to_usize, map::combine_low_high_bits_to_u128};
use cuprate_types::{ use cuprate_types::{
blockchain::{BlockchainReadRequest, BlockchainResponse}, blockchain::{BlockchainReadRequest, BlockchainResponse},
Chain, ChainId, ExtendedBlockHeader, OutputHistogramInput, OutputOnChain, Chain, ChainId, ExtendedBlockHeader, OutputHistogramInput, OutputOnChain,
@ -419,14 +419,10 @@ fn number_outputs_with_amount(env: &ConcreteEnv, amounts: Vec<Amount>) -> Respon
let tables = thread_local(env); let tables = thread_local(env);
// Cache the amount of RCT outputs once. // Cache the amount of RCT outputs once.
#[expect(
clippy::cast_possible_truncation,
reason = "INVARIANT: #[cfg] @ lib.rs asserts `usize == u64`"
)]
let num_rct_outputs = { let num_rct_outputs = {
let tx_ro = env_inner.tx_ro()?; let tx_ro = env_inner.tx_ro()?;
let tables = env_inner.open_tables(&tx_ro)?; let tables = env_inner.open_tables(&tx_ro)?;
tables.rct_outputs().len()? as usize u64_to_usize(tables.rct_outputs().len()?)
}; };
// Collect results using `rayon`. // Collect results using `rayon`.
@ -442,11 +438,7 @@ fn number_outputs_with_amount(env: &ConcreteEnv, amounts: Vec<Amount>) -> Respon
} else { } else {
// v1 transactions. // v1 transactions.
match tables.num_outputs().get(&amount) { match tables.num_outputs().get(&amount) {
#[expect( Ok(count) => Ok((amount, u64_to_usize(count))),
clippy::cast_possible_truncation,
reason = "INVARIANT: #[cfg] @ lib.rs asserts `usize == u64`"
)]
Ok(count) => Ok((amount, count as usize)),
// If we get a request for an `amount` that doesn't exist, // If we get a request for an `amount` that doesn't exist,
// we return `0` instead of an error. // we return `0` instead of an error.
Err(RuntimeError::KeyNotFound) => Ok((amount, 0)), Err(RuntimeError::KeyNotFound) => Ok((amount, 0)),

View file

@ -17,6 +17,7 @@ use rand::Rng;
use tower::{Service, ServiceExt}; use tower::{Service, ServiceExt};
use cuprate_database::{ConcreteEnv, DatabaseIter, DatabaseRo, Env, EnvInner, RuntimeError}; use cuprate_database::{ConcreteEnv, DatabaseIter, DatabaseRo, Env, EnvInner, RuntimeError};
use cuprate_helper::cast::{u64_to_usize, usize_to_u64};
use cuprate_test_utils::data::{BLOCK_V16_TX0, BLOCK_V1_TX2, BLOCK_V9_TX3}; use cuprate_test_utils::data::{BLOCK_V16_TX0, BLOCK_V1_TX2, BLOCK_V9_TX3};
use cuprate_types::{ use cuprate_types::{
blockchain::{BlockchainReadRequest, BlockchainResponse, BlockchainWriteRequest}, blockchain::{BlockchainReadRequest, BlockchainResponse, BlockchainWriteRequest},
@ -168,11 +169,7 @@ async fn test_template(
num_req num_req
.iter() .iter()
.map(|amount| match tables.num_outputs().get(amount) { .map(|amount| match tables.num_outputs().get(amount) {
#[expect( Ok(count) => (*amount, u64_to_usize(count)),
clippy::cast_possible_truncation,
reason = "INVARIANT: #[cfg] @ lib.rs asserts `usize == u64`"
)]
Ok(count) => (*amount, count as usize),
Err(RuntimeError::KeyNotFound) => (*amount, 0), Err(RuntimeError::KeyNotFound) => (*amount, 0),
Err(e) => panic!("{e:?}"), Err(e) => panic!("{e:?}"),
}) })
@ -322,7 +319,7 @@ async fn test_template(
// Assert the amount of `Output`'s returned is as expected. // Assert the amount of `Output`'s returned is as expected.
let table_output_len = tables.outputs().len().unwrap() + tables.rct_outputs().len().unwrap(); let table_output_len = tables.outputs().len().unwrap() + tables.rct_outputs().len().unwrap();
assert_eq!(output_count as u64, table_output_len); assert_eq!(usize_to_u64(output_count), table_output_len);
assert_eq!(output_count, response_output_count); assert_eq!(output_count, response_output_count);
} }

View file

@ -12,11 +12,14 @@ keywords = ["cuprate", "database"]
# default = ["heed"] # default = ["heed"]
# default = ["redb"] # default = ["redb"]
# default = ["redb-memory"] # default = ["redb-memory"]
heed = ["dep:heed"] heed = ["dep:heed", "dep:cuprate-helper"]
redb = ["dep:redb"] redb = ["dep:redb"]
redb-memory = ["redb"] redb-memory = ["redb"]
[dependencies] [dependencies]
# `usize_to_u64` needed in `heed`
cuprate-helper = { workspace = true, features = ["cast"], optional = true }
bytemuck = { version = "1.18.0", features = ["must_cast", "derive", "min_const_generics", "extern_crate_alloc"] } bytemuck = { version = "1.18.0", features = ["must_cast", "derive", "min_const_generics", "extern_crate_alloc"] }
bytes = { workspace = true } bytes = { workspace = true }
cfg-if = { workspace = true } cfg-if = { workspace = true }

View file

@ -9,6 +9,8 @@ use std::{
use heed::{DatabaseFlags, EnvFlags, EnvOpenOptions}; use heed::{DatabaseFlags, EnvFlags, EnvOpenOptions};
use cuprate_helper::cast::u64_to_usize;
use crate::{ use crate::{
backend::heed::{ backend::heed::{
database::{HeedTableRo, HeedTableRw}, database::{HeedTableRo, HeedTableRw},
@ -144,9 +146,8 @@ impl Env for ConcreteEnv {
// (current disk size) + (a bit of leeway) // (current disk size) + (a bit of leeway)
// to account for empty databases where we // to account for empty databases where we
// need to write same tables. // need to write same tables.
#[expect(clippy::cast_possible_truncation, reason = "only 64-bit targets")]
let disk_size_bytes = match std::fs::File::open(&config.db_file) { let disk_size_bytes = match std::fs::File::open(&config.db_file) {
Ok(file) => file.metadata()?.len() as usize, Ok(file) => u64_to_usize(file.metadata()?.len()),
// The database file doesn't exist, 0 bytes. // The database file doesn't exist, 0 bytes.
Err(io_err) if io_err.kind() == std::io::ErrorKind::NotFound => 0, Err(io_err) if io_err.kind() == std::io::ErrorKind::NotFound => 0,
Err(io_err) => return Err(io_err.into()), Err(io_err) => return Err(io_err.into()),