Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion contrib/codeql/zeroize.ql
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ predicate zeroizeSatisfied(TypeItem t) {
/**
* Holds if `t` reaches the wire through the wiping encoder pair.
*
* `impl_stype!`/`impl_sbyte!` emit `type Encoder = ArrEncoder<N>`; the plain
* `impl_stype!`/`impl_sbytes!` emit `type Encoder = ArrEncoder<N>`; the plain
* `impl_type!`/`impl_bytes!` emit `type Encoder = VecEncoder`.
*/
predicate usesSecretBridge(TypeItem t) {
Expand Down
30 changes: 28 additions & 2 deletions contrib/semgrep/types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,31 @@ rules:
languages: [rust]
paths:
include: [/pkgs/types/src/**/*.rs]
exclude: [/pkgs/types/src/hex.rs, /pkgs/types/src/uint.rs]
pattern-regex: '\b(?:make|impl)_(?:bytes|num|type)!\s*\{'
exclude:
- /pkgs/types/src/entity.rs
- /pkgs/types/src/macros.rs
- /pkgs/types/src/secret.rs
- /pkgs/types/src/uint.rs
pattern-regex: '\b(?:make|impl)_(?:bytes|num|type)!\s*[({]'

- id: types-macro-generics-bracketed
message: "forward macro generics bare: `@parse [$($g)*]`, not `@parse [<$($g)*>]`"
severity: ERROR
languages: [rust]
paths:
include: [/pkgs/types/src/**/*.rs]
pattern-regex: |-
(?x)
@ (?: parse | delegate | codec ) # an internal forwarding arm
\s* \[ \s* < # ... whose payload opens with <

- id: types-macro-impl-unbracketed
message: "bracket generics in a macro impl template: `impl<$($g)*>`, not `impl $($g)*`"
severity: ERROR
languages: [rust]
paths:
include: [/pkgs/types/src/**/*.rs]
pattern-regex: |-
(?xm)
^ [ \t]* impl [ \t]+ # `impl` then a space rather than <
\$ \( # ... then a macro repetition: the generics
32 changes: 32 additions & 0 deletions contrib/semgrep/workspace.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,23 @@ rules:
include: [/pkgs/**/*.rs, /contrib/samples/**/*.rs]
pattern-regex: '\bimpl\b[^{]*\bInto\s*<.*>\s+for\b'

- id: macro-export-no-serde-cfg
message: "use `$crate::cfg_serde!` and `$crate::__private::serde` to root dependency and cfg flag to defining macro."
severity: ERROR
languages: [rust]
paths:
include:
- /pkgs/**/*.rs
exclude:
- /pkgs/types/marker/**
pattern-regex: |-
(?sxm)
^([ \t]*) \#\[macro_export\]
(?: \n\1 \#\[[^\]]*\] )*
\n\1 macro_rules!\s*\w+\s*\{
(?: (?!^\1\}) . )*?
(?: feature \s*=\s* "serde" | (?<![\w$:]) ::?serde:: )

- id: macro-no-bare-foreign-crate
message: >
rooted foreign-crate paths (::crate::) in macros must route
Expand All @@ -116,6 +133,21 @@ rules:
- pattern-not-regex: "::(?:core|alloc|std|serde)::"
- pattern-not-regex: '\$crate::__private::'

- id: macro-no-bare-std-path
message: "root std-prefix paths (::core::, ::alloc::, ::std::) in macros"
severity: ERROR
languages: [rust]
paths:
include:
- /pkgs/**/*.rs
exclude:
- /pkgs/types/marker/**
pattern-regex: |-
(?sxm)
^([ \t]*) macro_rules!\s*\w+\s*\{
(?: (?!^\1\}) . )*?
(?<![:\w$]) (?:core|alloc|std)::

- id: prelude-no-use-alloc
message: "use crate::prelude instead of alloc:: directly"
severity: ERROR
Expand Down
4 changes: 2 additions & 2 deletions pkgs/num/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,14 @@ macro_rules! define_hash {
}

impl Ord for $name {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
fn cmp(&self, other: &Self) -> ::core::cmp::Ordering {
// Lexicographic on raw bytes (consensus ordering).
self.0.cmp(&other.0)
}
}

impl PartialOrd for $name {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
fn partial_cmp(&self, other: &Self) -> Option<::core::cmp::Ordering> {
Some(self.cmp(other))
}
}
Expand Down
2 changes: 2 additions & 0 deletions pkgs/num/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub mod util;
pub mod __private {
pub use bitcoin_consensus_encoding;
pub use dash_types;
#[cfg(feature = "serde")]
pub use serde;
}

pub use arith::ArithInt;
Expand Down
48 changes: 40 additions & 8 deletions pkgs/num/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@

//! Hash newtype macros.

/// dash-num's [`cfg_serde!`](dash_types::cfg_serde), keyed to `dash-num/serde`
/// (this crate) rather than `dash-types/serde`.
#[cfg(feature = "serde")]
#[doc(hidden)]
#[macro_export]
macro_rules! cfg_serde {
($($item:tt)*) => { $($item)* };
}

#[cfg(not(feature = "serde"))]
#[doc(hidden)]
#[macro_export]
macro_rules! cfg_serde {
($($item:tt)*) => {};
}

/// Generates `BaseCodec` + `Encodable` + `Decodable` for hash newtypes.
#[macro_export]
macro_rules! impl_hash {
Expand Down Expand Up @@ -38,10 +54,26 @@ macro_rules! make_hash {
) => {
$(#[$attr])*
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, $crate::__private::dash_types::TypeId)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct $name($base);

$crate::cfg_serde! {
impl $crate::__private::serde::Serialize for $name {
fn serialize<S: $crate::__private::serde::Serializer>(
&self, serializer: S,
) -> Result<S::Ok, S::Error> {
$crate::__private::serde::Serialize::serialize(&self.0, serializer)
}
}

impl<'de> $crate::__private::serde::Deserialize<'de> for $name {
fn deserialize<D: $crate::__private::serde::Deserializer<'de>>(
deserializer: D,
) -> Result<Self, D::Error> {
<$base as $crate::__private::serde::Deserialize>::deserialize(deserializer).map(Self)
}
}
}

impl $name {
/// The all-zeros (null) hash.
pub const ZERO: Self = Self(<$base>::ZERO);
Expand Down Expand Up @@ -88,19 +120,19 @@ macro_rules! make_hash {
fn default() -> Self { Self::ZERO }
}

impl core::fmt::Display for $name {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Display::fmt(&self.0, f)
impl ::core::fmt::Display for $name {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
::core::fmt::Display::fmt(&self.0, f)
}
}

impl core::fmt::Debug for $name {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
impl ::core::fmt::Debug for $name {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
write!(f, "{}({})", stringify!($name), self.0)
}
}

impl core::str::FromStr for $name {
impl ::core::str::FromStr for $name {
type Err = $crate::ParseHexError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand Down
6 changes: 3 additions & 3 deletions pkgs/p2p_core/src/msg/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::primitives::ServiceFlags;

use dash_primitives::{hash_impl, AddrV2, ServiceV1};
use dash_types::codec::{self, BaseCodec, DecodeError, EncodeBuf};
use dash_types::TypeId;
use dash_types::{CompactSize, TypeId};

use core::fmt;

Expand Down Expand Up @@ -47,7 +47,7 @@ pub struct AddrV2Entry {
impl BaseCodec for AddrV2Entry {
fn decode(data: &mut &[u8]) -> Result<Self, DecodeError> {
let time = u32::decode(data)?;
let services = ServiceFlags(codec::read_compact_u64(data)?);
let services = ServiceFlags(CompactSize::decode(data)?.get());
let addr = AddrV2::decode(data)?;
let port = codec::read_u16_be(data)?;
Ok(Self {
Expand All @@ -60,7 +60,7 @@ impl BaseCodec for AddrV2Entry {

fn encode(&self, buf: &mut impl EncodeBuf) {
self.time.encode(buf);
codec::write_compact_u64(self.services.0, buf);
CompactSize::from(self.services.0).encode(buf);
self.addr.encode(buf);
buf.extend_from_slice(&self.port.to_be_bytes());
}
Expand Down
10 changes: 5 additions & 5 deletions pkgs/p2p_core/src/msg/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::prelude::*;
use crate::primitives::ProtocolVersion;

use dash_primitives::{hash_impl, BlockHash, BlockHeader, MerkleRoot};
use dash_types::codec::{self, BaseCodec, DecodeError, EncodeBuf};
use dash_types::TypeId;
use dash_types::codec::{BaseCodec, DecodeError, EncodeBuf};
use dash_types::{CompactSize, TypeId};

/// Maximum headers per message.
const MAX_HEADERS: usize = 2_000;
Expand Down Expand Up @@ -50,7 +50,7 @@ impl_p2p!(Headers);

impl BaseCodec for Headers {
fn decode(data: &mut &[u8]) -> Result<Self, DecodeError> {
let count = codec::read_compact_size(data, MAX_HEADERS)?;
let count = CompactSize::decode(data)?.into_len(MAX_HEADERS)?;
let mut headers = Vec::with_capacity(count);
for _ in 0..count {
headers.push(BlockHeader {
Expand All @@ -62,13 +62,13 @@ impl BaseCodec for Headers {
nonce: u32::decode(data)?,
});
// Consume the trailing tx_count (always 0).
codec::read_compact_size(data, 0)?;
CompactSize::decode(data)?.into_len(0)?;
}
Ok(Self { headers })
}

fn encode(&self, buf: &mut impl EncodeBuf) {
codec::write_compact_size(self.headers.len(), buf);
CompactSize::from(self.headers.len()).encode(buf);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
for h in &self.headers {
h.version.encode(buf);
h.prev_hash.encode(buf);
Expand Down
8 changes: 4 additions & 4 deletions pkgs/p2p_core/src/msg/headers2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::prelude::*;
use crate::primitives::{CompressionState, ProtocolVersion};

use dash_primitives::{hash_impl, BlockHash};
use dash_types::codec::{self, BaseCodec, DecodeError, EncodeBuf};
use dash_types::TypeId;
use dash_types::codec::{BaseCodec, DecodeError, EncodeBuf};
use dash_types::{CompactSize, TypeId};

/// Maximum headers per message.
const MAX_HEADERS: usize = 2_000;
Expand Down Expand Up @@ -47,7 +47,7 @@ impl_p2p!(Headers2);

impl BaseCodec for Headers2 {
fn decode(data: &mut &[u8]) -> Result<Self, DecodeError> {
let count = codec::read_compact_size(data, MAX_HEADERS)?;
let count = CompactSize::decode(data)?.into_len(MAX_HEADERS)?;
let mut state = CompressionState::new();
let mut headers = Vec::with_capacity(count);
for _ in 0..count {
Expand All @@ -57,7 +57,7 @@ impl BaseCodec for Headers2 {
}

fn encode(&self, buf: &mut impl EncodeBuf) {
codec::write_compact_size(self.headers.len(), buf);
CompactSize::from(self.headers.len()).encode(buf);
let mut state = CompressionState::new();
for h in &self.headers {
state.encode_header(h, buf);
Expand Down
2 changes: 1 addition & 1 deletion pkgs/p2p_core/src/primitives/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use core::fmt;
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub struct CommandString([u8; 12]);

impl_bytes!(12, CommandString);
impl_bytes!(CommandString, 12);

hash_impl!(CommandString);

Expand Down
4 changes: 2 additions & 2 deletions pkgs/p2p_core/src/primitives/user_agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::prelude::*;

use dash_primitives::hash_impl;
use dash_types::codec::{self, BaseCodec, DecodeError, EncodeBuf};
use dash_types::{TypeId, Unencodable};
use dash_types::{CompactSize, TypeId, Unencodable};

use core::fmt;

Expand Down Expand Up @@ -39,7 +39,7 @@ impl fmt::Display for UserAgentTooLong {

impl BaseCodec for UserAgent {
fn decode(data: &mut &[u8]) -> Result<Self, DecodeError> {
let len = codec::read_compact_size(data, MAX_USER_AGENT)?;
let len = CompactSize::decode(data)?.into_len(MAX_USER_AGENT)?;
let raw = codec::read_bytes(data, len)?;
Ok(Self(raw.to_vec()))
}
Expand Down
41 changes: 3 additions & 38 deletions pkgs/pkc/src/bls/public_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ use crate::bls::BlsSchemeId;

use bitcoin_hashes::sha256d::Hash as Sha256d;
use dash_num::Hash256;
use dash_types::codec::{take, BaseCodec, DecodeError, EncodeBuf, Hashable, TypeId};
use dash_types::{derive_bytes, impl_type};
use dash_types::codec::{Hashable, TypeId};
use dash_types::{derive_bytes, impl_bytes};

use core::fmt;
use core::marker::PhantomData;

/// Raw BLS public key length (G1 compressed).
Expand All @@ -25,17 +24,7 @@ pub struct BlsPkBytes<S: BlsSchemeId> {
_scheme: PhantomData<S>,
}

impl<S: BlsSchemeId> BaseCodec for BlsPkBytes<S> {
fn decode(data: &mut &[u8]) -> Result<Self, DecodeError> {
take::<BLS_PK_LEN>(data).map(Self::from_bytes)
}

fn encode(&self, buf: &mut impl EncodeBuf) {
buf.extend_from_slice(&self.inner); // nosemgrep: codec-no-raw-extend
}
}

impl_type!(for[S: BlsSchemeId] BlsPkBytes<S>, BLS_PK_LEN);
impl_bytes!(for[S: BlsSchemeId] BlsPkBytes<S>, BLS_PK_LEN);

impl<S: BlsSchemeId> Hashable for BlsPkBytes<S> {
type Hash = Hash256;
Expand Down Expand Up @@ -63,34 +52,10 @@ impl<S: BlsSchemeId> BlsPkBytes<S> {
pub const fn into_bytes(self) -> [u8; BLS_PK_LEN] {
self.inner
}

/// Returns `true` when every byte is zero.
pub fn is_null(&self) -> bool {
self.inner.iter().all(|&b| b == 0)
}
}

impl<S: BlsSchemeId> TypeId for BlsPkBytes<S> {
const TYPE_ID: u32 = S::PK_TYPE_ID;
}

derive_bytes!(for[S: BlsSchemeId] BlsPkBytes<S>, BLS_PK_LEN);

impl<S: BlsSchemeId> fmt::Debug for BlsPkBytes<S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "BlsPkBytes<{}>(", S::LABEL)?;
for byte in &self.inner {
write!(f, "{byte:02x}")?;
}
write!(f, ")")
}
}

impl<S: BlsSchemeId> fmt::Display for BlsPkBytes<S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for byte in &self.inner {
write!(f, "{byte:02x}")?;
}
Ok(())
}
}
Loading
Loading