Files
nomos-node/core/src/sdp/mod.rs
T
2026-06-03 17:10:33 +02:00

504 lines
14 KiB
Rust

pub mod blend;
pub mod locked_notes;
use core::{
ops::{Add, Sub},
str::FromStr,
};
use std::{collections::HashMap, hash::Hash};
use blake2::{Blake2b, Digest as _};
use bytes::Bytes;
use lb_cryptarchia_engine::Epoch;
use lb_key_management_system_keys::keys::ZkPublicKey;
use lb_utils::bounded_vec::LowerBoundedVec;
use multiaddr::{Multiaddr, Protocol};
use nom::{IResult, Parser as _, bytes::complete::take};
use serde::{Deserialize, Serialize};
use strum::EnumIter;
use crate::{
block::BlockNumber,
codec::{self, DeserializeOp as _, SerializeOp as _},
mantle::{NoteId, ops::channel::Ed25519PublicKey},
utils::{display_hex_bytes_newtype, serde_bytes_newtype},
};
pub type StakeThreshold = u64;
const ACTIVE_METADATA_BLEND_TYPE: u8 = 0x01;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct MinStake {
pub threshold: StakeThreshold,
pub timestamp: BlockNumber,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ServiceParameters {
/// Minimum epochs during which a declaration cannot be withdrawn
pub lock_period: NumberOfEpochs,
/// Maximum epochs during which an activity message must be sent
pub inactivity_period: NumberOfEpochs,
/// Epochs after which a declaration can be safely deleted by Garbage
/// Collection
pub retention_period: NumberOfEpochs,
// Epoch number at which this parameter was set
pub epoch: Epoch,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct NumberOfEpochs(Epoch);
impl NumberOfEpochs {
#[must_use]
pub const fn new(epoch: Epoch) -> Self {
Self(epoch)
}
}
impl From<u32> for NumberOfEpochs {
fn from(value: u32) -> Self {
Self(value.into())
}
}
impl From<NumberOfEpochs> for u32 {
fn from(this: NumberOfEpochs) -> Self {
this.0.into_inner()
}
}
impl Add for NumberOfEpochs {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl Add<NumberOfEpochs> for Epoch {
type Output = Self;
fn add(self, rhs: NumberOfEpochs) -> Self::Output {
self + rhs.0
}
}
impl Sub<NumberOfEpochs> for Epoch {
type Output = Self;
fn sub(self, rhs: NumberOfEpochs) -> Self::Output {
self - rhs.0
}
}
// TODO: Check spec for max limit once we migrate the SDP Declare op to use the
// `NomEncode` and `NomDecode` traits.
pub type Locators = LowerBoundedVec<Locator, 1>;
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(try_from = "Multiaddr")]
pub struct Locator(Multiaddr);
impl Locator {
#[must_use]
pub const fn new_unchecked(addr: Multiaddr) -> Self {
Self(addr)
}
#[must_use]
pub fn into_inner(self) -> Multiaddr {
self.0
}
}
impl AsRef<Multiaddr> for Locator {
fn as_ref(&self) -> &Multiaddr {
&self.0
}
}
impl TryFrom<Multiaddr> for Locator {
type Error = String;
fn try_from(value: Multiaddr) -> Result<Self, Self::Error> {
for protocol in &value {
match protocol {
Protocol::Ip4(ip) if ip.is_unspecified() => {
return Err(format!(
"Locator multiaddr must not contain an unspecified IPv4 address: {value}"
));
}
Protocol::Ip6(ip) if ip.is_unspecified() => {
return Err(format!(
"Locator multiaddr must not contain an unspecified IPv6 address: {value}"
));
}
Protocol::P2p(_) => {
return Err(format!(
"Locator multiaddr must not contain a peer ID: {value}"
));
}
_ => {}
}
}
Ok(Self(value))
}
}
impl FromStr for Locator {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let multiaddr = s
.parse::<Multiaddr>()
.map_err(|e| format!("Invalid multiaddr: {e}"))?;
println!("Multiaddr: {multiaddr}");
Self::try_from(multiaddr)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize, EnumIter)]
pub enum ServiceType {
#[serde(rename = "BN")]
BlendNetwork,
}
impl AsRef<str> for ServiceType {
fn as_ref(&self) -> &str {
match self {
Self::BlendNetwork => "BN",
}
}
}
impl From<ServiceType> for usize {
fn from(service_type: ServiceType) -> Self {
match service_type {
ServiceType::BlendNetwork => 0,
}
}
}
pub type Nonce = u64;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct ProviderId(pub Ed25519PublicKey);
#[derive(Debug)]
pub struct InvalidKeyBytesError;
impl From<Ed25519PublicKey> for ProviderId {
fn from(pk: Ed25519PublicKey) -> Self {
Self(pk)
}
}
impl TryFrom<[u8; 32]> for ProviderId {
type Error = InvalidKeyBytesError;
fn try_from(bytes: [u8; 32]) -> Result<Self, Self::Error> {
Ed25519PublicKey::from_bytes(&bytes)
.map(ProviderId)
.map_err(|_| InvalidKeyBytesError)
}
}
impl PartialOrd for ProviderId {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for ProviderId {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.as_bytes().cmp(other.0.as_bytes())
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub struct DeclarationId(pub [u8; 32]);
serde_bytes_newtype!(DeclarationId, 32);
display_hex_bytes_newtype!(DeclarationId);
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Declaration {
pub service_type: ServiceType,
pub provider_id: ProviderId,
pub locked_note_id: NoteId,
pub locators: Locators,
pub zk_id: ZkPublicKey,
pub created: Epoch,
pub active: Epoch,
pub withdrawn: Option<Epoch>,
pub nonce: Nonce,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ProviderInfo {
pub locators: Locators,
pub zk_id: ZkPublicKey,
}
const SNAPSHOT_FINALIZATION_DELAY: Epoch = Epoch::new(2);
impl Declaration {
#[must_use]
pub fn new(epoch: Epoch, declaration_msg: &DeclarationMessage) -> Self {
Self {
service_type: declaration_msg.service_type,
provider_id: declaration_msg.provider_id,
locked_note_id: declaration_msg.locked_note_id,
locators: declaration_msg.locators.clone(),
zk_id: declaration_msg.zk_id,
created: epoch,
active: epoch + SNAPSHOT_FINALIZATION_DELAY,
withdrawn: None,
nonce: 0,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct Declarations(HashMap<ServiceType, HashMap<DeclarationId, Declaration>>);
impl Declarations {
pub fn iter(
&self,
) -> impl Iterator<Item = (&ServiceType, &HashMap<DeclarationId, Declaration>)> {
self.0.iter()
}
}
impl From<HashMap<ServiceType, HashMap<DeclarationId, Declaration>>> for Declarations {
fn from(value: HashMap<ServiceType, HashMap<DeclarationId, Declaration>>) -> Self {
Self(value)
}
}
impl FromIterator<(ServiceType, HashMap<DeclarationId, Declaration>)> for Declarations {
fn from_iter<I: IntoIterator<Item = (ServiceType, HashMap<DeclarationId, Declaration>)>>(
iter: I,
) -> Self {
Self(iter.into_iter().collect())
}
}
impl TryFrom<Bytes> for Declarations {
type Error = codec::Error;
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
Self::from_bytes(&bytes)
}
}
impl TryFrom<Declarations> for Bytes {
type Error = codec::Error;
fn try_from(this: Declarations) -> Result<Self, Self::Error> {
this.to_bytes()
}
}
#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct DeclarationMessage {
pub service_type: ServiceType,
pub locators: Locators,
pub provider_id: ProviderId,
pub zk_id: ZkPublicKey,
pub locked_note_id: NoteId,
}
impl DeclarationMessage {
#[must_use]
pub fn id(&self) -> DeclarationId {
let mut hasher = Blake2b::new();
let service = match self.service_type {
ServiceType::BlendNetwork => "BN",
};
// From the
// [spec](https://www.notion.so/nomos-tech/Service-Declaration-Protocol-Specification-1fd261aa09df819ca9f8eb2bdfd4ec1dw):
// declaration_id = Hash(service||provider_id||zk_id||locators)
hasher.update(service.as_bytes());
hasher.update(self.provider_id.0);
for number in self.zk_id.as_fr().0.0 {
hasher.update(number.to_le_bytes());
}
for locator in &self.locators {
hasher.update(locator.0.as_ref());
}
DeclarationId(hasher.finalize().into())
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct WithdrawMessage {
pub declaration_id: DeclarationId,
pub locked_note_id: NoteId,
pub nonce: Nonce,
}
#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct ActiveMessage {
pub declaration_id: DeclarationId,
pub nonce: Nonce,
pub metadata: ActivityMetadata,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum ActivityMetadata {
Blend(Box<blend::ActivityProof>),
}
impl ActivityMetadata {
#[must_use]
pub fn to_metadata_bytes(&self) -> Vec<u8> {
match self {
Self::Blend(proof) => proof.to_metadata_bytes(),
}
}
pub fn from_metadata_bytes(bytes: &[u8]) -> Result<Self, Box<dyn std::error::Error>> {
if bytes.is_empty() {
return Err("empty metadata bytes".to_owned().into());
}
// Read metadata type byte to determine variant
let metadata_type = bytes[0];
match metadata_type {
ACTIVE_METADATA_BLEND_TYPE => {
let proof_opt = blend::ActivityProof::from_metadata_bytes(bytes)?;
Ok(Self::Blend(Box::new(proof_opt)))
}
_ => Err(format!("Unknown metadata type: {metadata_type:#x}").into()),
}
}
}
fn parse_epoch(input: &[u8]) -> IResult<&[u8], Epoch> {
let (input, bytes) = take(size_of::<Epoch>()).parse(input)?;
let epoch_bytes: [u8; 4] = bytes
.try_into()
.map_err(|_| nom::Err::Error(nom::error::Error::new(input, nom::error::ErrorKind::Fail)))?;
Ok((input, u32::from_le_bytes(epoch_bytes).into()))
}
#[cfg(test)]
mod tests {
use lb_groth16::{Field as _, Fr};
use lb_key_management_system_keys::keys::Ed25519Key;
use super::*;
#[test]
fn test_activity_metadata_empty_bytes() {
let result = ActivityMetadata::from_metadata_bytes(&[]);
assert!(result.is_err());
}
#[test]
fn test_activity_metadata_unknown_type() {
let bytes = vec![0xFF]; // Unknown type
let result = ActivityMetadata::from_metadata_bytes(&bytes);
assert!(result.is_err());
assert!(
result
.unwrap_err()
.to_string()
.contains("Unknown metadata type")
);
}
#[test]
fn locator_rejects_multiaddr_with_peer_id() {
assert!("/ip4/65.109.51.37/udp/3000/quic-v1/p2p/12D3KooWL7a8LBbLRYnabptHPFBCmAs49Y7cVMqvzuSdd43tAJk8".parse::<Locator>().unwrap_err().contains("must not contain a peer ID"));
}
#[test]
fn locator_rejects_multiaddr_with_unspecified_ipv4() {
assert!(
"/ip4/0.0.0.0/udp/3000/quic-v1"
.parse::<Locator>()
.unwrap_err()
.contains("must not contain an unspecified IPv4 address")
);
}
#[test]
fn locator_rejects_multiaddr_with_unspecified_ipv6() {
assert!(
"/ip6/::/udp/3000/quic-v1"
.parse::<Locator>()
.unwrap_err()
.contains("must not contain an unspecified IPv6 address")
);
}
#[test]
fn locator_accepts_specific_ip_without_peer_id() {
let addr: Multiaddr = "/ip4/127.0.0.1/udp/3000/quic-v1".parse().unwrap();
let result = Locator::try_from(addr.clone()).unwrap();
assert_eq!(result.into_inner(), addr);
}
#[test]
fn locators_array_serde_equivalence() {
let locator: Locator = "/ip4/127.0.0.1/udp/3001/quic-v1".parse().unwrap();
let locator_vector_serialized = serde_json::to_string(&vec![locator.clone()]).unwrap();
let locators_serialized = serde_json::to_string(&Locators::from(locator.clone())).unwrap();
assert_eq!(locator_vector_serialized, locators_serialized);
let locator_vectors_deserialized_as_locators =
serde_json::from_str::<Locators>(&locator_vector_serialized).unwrap();
assert_eq!(
locator_vectors_deserialized_as_locators,
Locators::from(locator)
);
}
#[test]
fn empty_locators_fail_to_deserialize() {
let empty_locators = Vec::<Locator>::new();
let serialized = serde_json::to_string(&empty_locators).unwrap();
assert_eq!(
serde_json::from_str::<Locators>(&serialized)
.unwrap_err()
.to_string(),
"Input cannot be empty."
);
}
#[test]
fn declaration_initialization() {
let msg = DeclarationMessage {
service_type: ServiceType::BlendNetwork,
locators: vec!["/ip4/127.0.0.1/udp/3001/quic-v1".parse().unwrap()]
.try_into()
.unwrap(),
provider_id: Ed25519Key::from_bytes(&[0; _]).public_key().into(),
zk_id: ZkPublicKey::zero(),
locked_note_id: Fr::ZERO.into(),
};
let declaration = Declaration::new(Epoch::new(10), &msg);
assert_eq!(declaration.service_type, msg.service_type);
assert_eq!(declaration.provider_id, msg.provider_id);
assert_eq!(declaration.locked_note_id, msg.locked_note_id);
assert_eq!(declaration.locators, msg.locators);
assert_eq!(declaration.zk_id, msg.zk_id);
assert_eq!(declaration.created, Epoch::new(10));
assert_eq!(declaration.active, Epoch::new(12)); // created + SNAPSHOT_FINALIZATION_DELAY
assert_eq!(declaration.withdrawn, None);
assert_eq!(declaration.nonce, 0);
}
}