Skip to content

Commit

Permalink
Change directory back to repo root & Fix Rust v1.81 lints (kaspanet#545)
Browse files Browse the repository at this point in the history
* Change directory back to repodir

Change directory back to repodir after building toolchain

* Clippy

* Update crypto/txscript/src/caches.rs

Co-authored-by: Maxim <59533214+biryukovmaxim@users.noreply.github.com>

* Update crypto/txscript/src/caches.rs

* rename `is_none_or` -> `is_none_or_ex` to avoid conflict with future std

* remove `use std::mem::size_of` wherever possible (added to std prelude recently)

---------

Co-authored-by: Maxim <59533214+biryukovmaxim@users.noreply.github.com>
Co-authored-by: Michael Sutton <msutton@cs.huji.ac.il>
  • Loading branch information
3 people committed Sep 6, 2024
1 parent 06a874f commit afbcf9e
Show file tree
Hide file tree
Showing 26 changed files with 31 additions and 43 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ jobs:
run: |
# Run build script for musl toolchain
source musl-toolchain/build.sh
# Go back to the workspace
cd $GITHUB_WORKSPACE
# Build for musl
cargo --verbose build --bin kaspad --bin rothschild --bin kaspa-wallet --release --target x86_64-unknown-linux-musl
mkdir bin || true
Expand Down
2 changes: 1 addition & 1 deletion consensus/core/src/utxo/utxo_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{
use crate::tx::{TransactionOutpoint, UtxoEntry, VerifiableTransaction};
use kaspa_utils::mem_size::MemSizeEstimator;
use serde::{Deserialize, Serialize};
use std::{collections::hash_map::Entry::Vacant, mem::size_of};
use std::collections::hash_map::Entry::Vacant;

pub trait ImmutableUtxoDiff {
fn added(&self) -> &UtxoCollection;
Expand Down
2 changes: 1 addition & 1 deletion consensus/src/consensus/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use kaspa_consensus_core::{blockstatus::BlockStatus, BlockHashSet};
use kaspa_database::registry::DatabaseStorePrefixes;
use kaspa_hashes::Hash;
use parking_lot::RwLock;
use std::{mem::size_of, ops::DerefMut, sync::Arc};
use std::{ops::DerefMut, sync::Arc};

pub struct ConsensusStorage {
// DB
Expand Down
1 change: 0 additions & 1 deletion consensus/src/model/stores/acceptance_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use kaspa_utils::mem_size::MemSizeEstimator;
use rocksdb::WriteBatch;
use serde::Deserialize;
use serde::Serialize;
use std::mem::size_of;
use std::sync::Arc;

pub trait AcceptanceDataStoreReader {
Expand Down
1 change: 0 additions & 1 deletion consensus/src/model/stores/block_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use kaspa_hashes::Hash;
use kaspa_utils::mem_size::MemSizeEstimator;
use rocksdb::WriteBatch;
use serde::{Deserialize, Serialize};
use std::mem::size_of;
use std::sync::Arc;

pub trait BlockTransactionsStoreReader {
Expand Down
1 change: 0 additions & 1 deletion consensus/src/model/stores/ghostdag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use kaspa_utils::mem_size::MemSizeEstimator;
use rocksdb::WriteBatch;
use serde::{Deserialize, Serialize};
use std::iter::once;
use std::mem::size_of;
use std::{cell::RefCell, sync::Arc};

/// Re-export for convenience
Expand Down
1 change: 0 additions & 1 deletion consensus/src/model/stores/headers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::mem::size_of;
use std::sync::Arc;

use kaspa_consensus_core::{header::Header, BlockHasher, BlockLevel};
Expand Down
7 changes: 3 additions & 4 deletions consensus/src/model/stores/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ pub mod block_transactions;
pub mod block_window_cache;
pub mod children;
pub mod daa;
pub mod selected_chain;
use std::{fmt::Display, mem::size_of};

pub use kaspa_database;
pub mod depth;
pub mod ghostdag;
pub mod headers;
Expand All @@ -16,14 +12,17 @@ pub mod pruning;
pub mod pruning_utxoset;
pub mod reachability;
pub mod relations;
pub mod selected_chain;
pub mod statuses;
pub mod tips;
pub mod utxo_diffs;
pub mod utxo_multisets;
pub mod utxo_set;
pub mod virtual_state;

pub use kaspa_database;
pub use kaspa_database::prelude::DB;
use std::fmt::Display;

#[derive(PartialEq, Eq, Clone, Copy, Hash)]
pub(crate) struct U64Key([u8; size_of::<u64>()]);
Expand Down
5 changes: 2 additions & 3 deletions consensus/src/model/stores/utxo_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub trait UtxoSetStore: UtxoSetStoreReader {
fn write_many(&mut self, utxos: &[(TransactionOutpoint, UtxoEntry)]) -> Result<(), StoreError>;
}

pub const UTXO_KEY_SIZE: usize = kaspa_hashes::HASH_SIZE + std::mem::size_of::<TransactionIndexType>();
pub const UTXO_KEY_SIZE: usize = kaspa_hashes::HASH_SIZE + size_of::<TransactionIndexType>();

#[derive(Eq, Hash, PartialEq, Debug, Copy, Clone)]
struct UtxoKey([u8; UTXO_KEY_SIZE]);
Expand Down Expand Up @@ -81,8 +81,7 @@ impl From<UtxoKey> for TransactionOutpoint {
fn from(k: UtxoKey) -> Self {
let transaction_id = Hash::from_slice(&k.0[..kaspa_hashes::HASH_SIZE]);
let index = TransactionIndexType::from_le_bytes(
<[u8; std::mem::size_of::<TransactionIndexType>()]>::try_from(&k.0[kaspa_hashes::HASH_SIZE..])
.expect("expecting index size"),
<[u8; size_of::<TransactionIndexType>()]>::try_from(&k.0[kaspa_hashes::HASH_SIZE..]).expect("expecting index size"),
);
Self::new(transaction_id, index)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl BlockBodyProcessor {
.copied()
.filter(|parent| {
let status_option = statuses_read_guard.get(*parent).unwrap_option();
status_option.is_none_or(|s| !s.has_block_body())
status_option.is_none_or_ex(|s| !s.has_block_body())
})
.collect();
if !missing.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion consensus/src/processes/coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use kaspa_consensus_core::{
tx::{ScriptPublicKey, ScriptVec, Transaction, TransactionOutput},
BlockHashMap, BlockHashSet,
};
use std::{convert::TryInto, mem::size_of};
use std::convert::TryInto;

use crate::{constants, model::stores::ghostdag::GhostdagData};

Expand Down
2 changes: 1 addition & 1 deletion consensus/src/processes/pruning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl<
let mut expected_pps_queue = VecDeque::new();
for current in self.reachability_service.backward_chain_iterator(hst, pruning_info.pruning_point, false) {
let current_header = self.headers_store.get_header(current).unwrap();
if expected_pps_queue.back().is_none_or(|&&h| h != current_header.pruning_point) {
if expected_pps_queue.back().is_none_or_ex(|&&h| h != current_header.pruning_point) {
expected_pps_queue.push_back(current_header.pruning_point);
}
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/src/processes/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl<
}
}

if highest_with_body.is_none_or(|&h| h == high) {
if highest_with_body.is_none_or_ex(|&h| h == high) {
return Ok(vec![]);
};

Expand Down
1 change: 0 additions & 1 deletion crypto/muhash/fuzz/fuzz_targets/u3072.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use kaspa_muhash::u3072::{self, U3072};
use num_bigint::BigInt;
use num_integer::Integer;
use num_traits::{One, Signed};
use std::mem::size_of;

fuzz_target!(|data: &[u8]| {
if data.len() < muhash::SERIALIZED_MUHASH_SIZE {
Expand Down
4 changes: 2 additions & 2 deletions crypto/muhash/src/u3072.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ pub(crate) type DoubleLimb = u128;
//#[cfg(target_pointer_width = "32")]
//pub(crate) type DoubleLimb = u64;

const LIMB_SIZE_BYTES: usize = std::mem::size_of::<Limb>();
const LIMB_SIZE: usize = std::mem::size_of::<Limb>() * 8;
const LIMB_SIZE_BYTES: usize = size_of::<Limb>();
const LIMB_SIZE: usize = Limb::BITS as usize;
pub const LIMBS: usize = crate::ELEMENT_BYTE_SIZE / LIMB_SIZE_BYTES;

pub const PRIME_DIFF: Limb = 1103717;
Expand Down
3 changes: 1 addition & 2 deletions crypto/txscript/src/caches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ impl<TKey: Clone + std::hash::Hash + Eq + Send + Sync, TData: Clone + Send + Syn
}

pub(crate) fn get(&self, key: &TKey) -> Option<TData> {
self.map.read().get(key).cloned().map(|data| {
self.map.read().get(key).cloned().inspect(|_data| {
self.counters.get_counts.fetch_add(1, Ordering::Relaxed);
data
})
}

Expand Down
1 change: 0 additions & 1 deletion crypto/txscript/src/data_stack.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::TxScriptError;
use core::fmt::Debug;
use core::iter;
use core::mem::size_of;

const DEFAULT_SCRIPT_NUM_LEN: usize = 4;

Expand Down
2 changes: 0 additions & 2 deletions crypto/txscript/src/opcodes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::mem::size_of;

#[macro_use]
mod macros;

Expand Down
4 changes: 2 additions & 2 deletions database/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ mod tests {
let prefix = DatabaseStorePrefixes::AcceptanceData;
assert_eq!(&[prefix as u8], prefix.as_ref());
assert_eq!(
std::mem::size_of::<u8>(),
std::mem::size_of::<DatabaseStorePrefixes>(),
size_of::<u8>(),
size_of::<DatabaseStorePrefixes>(),
"DatabaseStorePrefixes is expected to have the same memory layout of u8"
);
}
Expand Down
4 changes: 1 addition & 3 deletions indexes/utxoindex/src/stores/indexed_utxos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use kaspa_index_core::indexed_utxos::BalanceByScriptPublicKey;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::fmt::Display;
use std::mem::size_of;
use std::sync::Arc;

pub const VERSION_TYPE_SIZE: usize = size_of::<ScriptPublicKeyVersion>(); // Const since we need to re-use this a few times.
Expand Down Expand Up @@ -67,8 +66,7 @@ impl From<TransactionOutpointKey> for TransactionOutpoint {
fn from(key: TransactionOutpointKey) -> Self {
let transaction_id = Hash::from_slice(&key.0[..kaspa_hashes::HASH_SIZE]);
let index = TransactionIndexType::from_le_bytes(
<[u8; std::mem::size_of::<TransactionIndexType>()]>::try_from(&key.0[kaspa_hashes::HASH_SIZE..])
.expect("expected index size"),
<[u8; size_of::<TransactionIndexType>()]>::try_from(&key.0[kaspa_hashes::HASH_SIZE..]).expect("expected index size"),
);
Self::new(transaction_id, index)
}
Expand Down
2 changes: 1 addition & 1 deletion math/src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ macro_rules! construct_uint {
pub const MIN: Self = Self::ZERO;
pub const MAX: Self = $name([u64::MAX; $n_words]);
pub const BITS: u32 = $n_words * u64::BITS;
pub const BYTES: usize = $n_words * core::mem::size_of::<u64>();
pub const BYTES: usize = $n_words * size_of::<u64>();
pub const LIMBS: usize = $n_words;

#[inline]
Expand Down
1 change: 0 additions & 1 deletion mining/src/testutils/coinbase_mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use kaspa_consensus_core::{
subnets::SUBNETWORK_ID_COINBASE,
tx::{Transaction, TransactionOutput},
};
use std::mem::size_of;

const LENGTH_OF_BLUE_SCORE: usize = size_of::<u64>();
const LENGTH_OF_SUBSIDY: usize = size_of::<u64>();
Expand Down
7 changes: 4 additions & 3 deletions protocol/flows/src/flowcontext/orphans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl OrphanBlocksPool {
}
} else {
let status = consensus.async_get_block_status(current).await;
if status.is_none_or(|s| s.is_header_only()) {
if status.is_none_or_ex(|s| s.is_header_only()) {
// Block is not in the orphan pool nor does its body exist consensus-wise, so it is a root
roots.push(current);
}
Expand All @@ -193,7 +193,8 @@ impl OrphanBlocksPool {
if let Occupied(entry) = self.orphans.entry(orphan_hash) {
let mut processable = true;
for p in entry.get().block.header.direct_parents().iter().copied() {
if !processing.contains_key(&p) && consensus.async_get_block_status(p).await.is_none_or(|s| s.is_header_only()) {
if !processing.contains_key(&p) && consensus.async_get_block_status(p).await.is_none_or_ex(|s| s.is_header_only())
{
processable = false;
break;
}
Expand Down Expand Up @@ -249,7 +250,7 @@ impl OrphanBlocksPool {
let mut processable = true;
for parent in block.block.header.direct_parents().iter().copied() {
if self.orphans.contains_key(&parent)
|| consensus.async_get_block_status(parent).await.is_none_or(|status| status.is_header_only())
|| consensus.async_get_block_status(parent).await.is_none_or_ex(|status| status.is_header_only())
{
processable = false;
break;
Expand Down
5 changes: 1 addition & 4 deletions protocol/p2p/src/convert/net_address.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
mem::size_of,
net::{IpAddr, Ipv4Addr, Ipv6Addr},
};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use super::error::ConversionError;
use crate::pb as protowire;
Expand Down
2 changes: 1 addition & 1 deletion utils/src/mem_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! estimate sizes of run-time objects in memory, including deep heap allocations. See
//! struct-level docs for more details.

use std::{collections::HashSet, mem::size_of, sync::Arc};
use std::{collections::HashSet, sync::Arc};

use parking_lot::RwLock;

Expand Down
5 changes: 3 additions & 2 deletions utils/src/option.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
pub trait OptionExtensions<T> {
fn is_none_or(&self, f: impl FnOnce(&T) -> bool) -> bool;
/// Substitute for unstable [Option<T>::is_non_or]
fn is_none_or_ex(&self, f: impl FnOnce(&T) -> bool) -> bool;
}

impl<T> OptionExtensions<T> for Option<T> {
fn is_none_or(&self, f: impl FnOnce(&T) -> bool) -> bool {
fn is_none_or_ex(&self, f: impl FnOnce(&T) -> bool) -> bool {
match self {
Some(v) => f(v),
None => true,
Expand Down

0 comments on commit afbcf9e

Please sign in to comment.