Skip to content
Merged
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
146 changes: 146 additions & 0 deletions crates/networking/p2p/src/gossipsub/lean/topics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,149 @@ impl std::fmt::Display for LeanGossipTopicKind {
}
}
}

#[cfg(test)]
mod tests {
use libp2p::gossipsub::TopicHash;

use super::{
ENCODING_POSTFIX, LEAN_BLOCK_TOPIC, LeanGossipTopic, LeanGossipTopicKind, TOPIC_PREFIX,
};
use crate::gossipsub::error::GossipsubError;

#[test]
fn test_gossip_topic_creation() {
let topic = LeanGossipTopic {
kind: LeanGossipTopicKind::Block,
fork: "0x12345678".to_string(),
};

assert_eq!(
topic,
LeanGossipTopic {
kind: LeanGossipTopicKind::Block,
fork: "0x12345678".to_string(),
}
);
assert_eq!(
topic.to_string(),
"/leanconsensus/0x12345678/block/ssz_snappy"
);
}

#[test]
fn test_gossip_topic_from_string() {
let topic_str = "/leanconsensus/0x12345678/block/ssz_snappy";
let topic = TopicHash::from_raw(topic_str);

assert_eq!(
LeanGossipTopic::from_topic_hash(&topic).unwrap(),
LeanGossipTopic {
kind: LeanGossipTopicKind::Block,
fork: "0x12345678".to_string(),
}
);
}

#[test]
fn test_parse_topic_string() {
let topic = TopicHash::from_raw(format!(
"/{TOPIC_PREFIX}/0x12345678/{LEAN_BLOCK_TOPIC}/{ENCODING_POSTFIX}",
));

let parsed = LeanGossipTopic::from_topic_hash(&topic).unwrap();

assert_eq!(parsed.fork, "0x12345678");
assert_eq!(parsed.kind, LeanGossipTopicKind::Block);
}

#[test]
fn test_invalid_topic_string() {
let invalid_topic = TopicHash::from_raw("/invalid/topic");
let wrong_prefix = TopicHash::from_raw("/wrongprefix/0x123/block/ssz_snappy");

let invalid_topic_err = LeanGossipTopic::from_topic_hash(&invalid_topic).unwrap_err();
assert!(matches!(invalid_topic_err, GossipsubError::InvalidTopic(_)));
assert!(
invalid_topic_err
.to_string()
.contains("Invalid topic format")
);

let wrong_prefix_err = LeanGossipTopic::from_topic_hash(&wrong_prefix).unwrap_err();
assert!(matches!(wrong_prefix_err, GossipsubError::InvalidTopic(_)));
assert!(
wrong_prefix_err
.to_string()
.contains("Invalid topic format")
);
}

#[test]
fn test_topic_kind_enum() {
assert_eq!(LeanGossipTopicKind::Block.to_string(), "block");
assert_eq!(
LeanGossipTopicKind::AttestationSubnet(0).to_string(),
"attestation_0"
);
}

#[test]
fn test_validate_fork_success() {
let topic = LeanGossipTopic {
kind: LeanGossipTopicKind::Block,
fork: "0x12345678".to_string(),
};

let expected_fork = "0x12345678";

assert_eq!(topic.fork, expected_fork);
}

#[test]
fn test_validate_fork_raises_on_mismatch() {
let topic = LeanGossipTopic {
kind: LeanGossipTopicKind::Block,
fork: "0x12345678".to_string(),
};

let expected_fork = "0xtesttopic";

assert_ne!(topic.fork, expected_fork);
}

#[test]
fn test_from_string_validated_success() {
let topic = TopicHash::from_raw("/leanconsensus/0x12345678/block/ssz_snappy");

let parsed = LeanGossipTopic::from_topic_hash(&topic).unwrap();

assert_eq!(
parsed,
LeanGossipTopic {
kind: LeanGossipTopicKind::Block,
fork: "0x12345678".to_string(),
}
);
assert_eq!(parsed.fork, "0x12345678");
}

#[test]
fn test_from_string_validated_raises_on_mismatch() {
let topic = TopicHash::from_raw("/leanconsensus/0x12345678/block/ssz_snappy");

let parsed = LeanGossipTopic::from_topic_hash(&topic).unwrap();

assert_ne!(parsed.fork, "0xtesttopic");
}

#[test]
fn test_from_string_validated_raises_on_invalid_topic() {
let topic = TopicHash::from_raw("/invalid/topic");

let err = LeanGossipTopic::from_topic_hash(&topic).unwrap_err();

assert!(matches!(err, GossipsubError::InvalidTopic(_)));
assert!(err.to_string().contains("Invalid topic format"));
}
}
Loading