Skip to content

Commit

Permalink
Merge pull request #35 from ourzora/erik/dat-112-improve-hashing-func…
Browse files Browse the repository at this point in the history
…tion-for-the-gossipsub-message-hash

Update hashing function
  • Loading branch information
erikreppel authored Apr 18, 2024
2 parents 25afc36 + a940b5f commit 6ad73e2
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 15 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ alloy-primitives = { git = "https://github.com/alloy-rs/core", rev = "7574bfc",
alloy-sol-macro = { git = "https://github.com/alloy-rs/core", rev = "7574bfc", features = ["json"] }
url = "2.5.0"
futures = "0.3.30"
sha256 = "1.5.0"


[patch.crates-io]
Expand Down
29 changes: 21 additions & 8 deletions src/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use libp2p::kad::Addresses;
use libp2p::multiaddr::Protocol;
use libp2p::swarm::{ConnectionId, NetworkBehaviour, NetworkInfo, SwarmEvent};
use libp2p::{gossipsub, kad, noise, tcp, yamux, Multiaddr, PeerId};
use sha256::digest;
use std::hash::{DefaultHasher, Hash, Hasher};
use std::time::Duration;
use tokio::select;
Expand Down Expand Up @@ -85,20 +86,14 @@ impl SwarmController {
)?
.with_dns()?
.with_behaviour(|key| {
let message_id_fn = |message: &gossipsub::Message| {
let mut s = DefaultHasher::new();
message.data.hash(&mut s);
gossipsub::MessageId::from(s.finish().to_string())
};

let mut b =
kad::Behaviour::new(peer_id, MemoryStore::new(key.public().to_peer_id()));
b.set_mode(Some(kad::Mode::Server));
let gossipsub_config = gossipsub::ConfigBuilder::default()
.heartbeat_interval(Duration::from_secs(10))
.validation_mode(gossipsub::ValidationMode::Strict)
.protocol_id("/mintpool/0.1.0", Version::V1_1)
.message_id_fn(message_id_fn)
.message_id_fn(gossipsub_message_id)
.build()
.expect("valid config");

Expand Down Expand Up @@ -156,7 +151,7 @@ impl SwarmController {
}

async fn handle_command(&mut self, command: SwarmCommand) {
tracing::info!("Received command: {:?}", command);
tracing::debug!("Received command: {:?}", command);
match command {
SwarmCommand::ConnectToPeer { address } => match address.parse() {
Ok(addr) => {
Expand Down Expand Up @@ -432,6 +427,24 @@ impl SwarmController {
}
}

fn gossipsub_message_id(message: &gossipsub::Message) -> gossipsub::MessageId {
if message.topic == announce_topic().hash() {
let s = String::from_utf8_lossy(&message.data);
let hash = digest(s.to_string());
gossipsub::MessageId::from(hash)
} else {
let s = String::from_utf8_lossy(&message.data);
match PremintTypes::from_json(s.to_string()) {
Ok(premint) => {
let metadata = premint.metadata();
let hash = digest(metadata.id);
gossipsub::MessageId::from(hash)
}
Err(_) => gossipsub::MessageId::from("likely_spam".to_string()),
}
}
}

#[derive(Debug)]
pub struct NetworkState {
pub local_peer_id: PeerId,
Expand Down
7 changes: 4 additions & 3 deletions src/premints/zora_premint_v2/rules.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloy::hex;
use std::str::FromStr;

use alloy_primitives::Signature;
Expand Down Expand Up @@ -89,7 +90,6 @@ pub async fn is_valid_signature<T: Reader>(
) -> eyre::Result<Evaluation> {
// * if contract exists, check if the signer is the contract admin
// * if contract does not exist, check if the signer is the proposed contract admin

let signature = Signature::from_str(premint.signature.as_str())?;

let domain = premint.eip712_domain();
Expand All @@ -98,8 +98,9 @@ pub async fn is_valid_signature<T: Reader>(

if signer != premint.collection.contractAdmin {
reject!(
"Invalid signature for contract admin {}",
premint.collection.contractAdmin
"Invalid signature for contract admin {} vs recovered {}",
premint.collection.contractAdmin,
signer
)
} else {
Ok(Accept)
Expand Down
1 change: 1 addition & 0 deletions src/stdin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Supported commands:
/peers - list all connected peers
/node - shows node info
/announce - announce self to the network to connect to all available peers
/list - list all premints in the database
<premint json> - send a premint to the network
"#;

Expand Down
2 changes: 1 addition & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct PremintName(pub String);

impl PremintName {
pub fn msg_topic(&self) -> gossipsub::IdentTopic {
gossipsub::IdentTopic::new(format!("mintpool::{:?}", self))
gossipsub::IdentTopic::new(format!("mintpool::premint::{:?}", self))
}
}

Expand Down
3 changes: 0 additions & 3 deletions tests/e2e_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ use alloy_sol_types::{SolCall, SolValue};
use alloy_transport::TransportErrorKind;
use mintpool::config::{ChainInclusionMode, Config};
use mintpool::controller::{ControllerCommands, DBQuery};
use mintpool::premints::zora_premint_v2::rules::is_valid_signature;
use mintpool::premints::zora_premint_v2::types::IZoraPremintV2::MintArguments;
use mintpool::premints::zora_premint_v2::types::{
IZoraPremintV2, ZoraPremintV2, PREMINT_FACTORY_ADDR,
};
use mintpool::rules::Evaluation::Accept;
use mintpool::rules::RuleContext;
use mintpool::run;
use mintpool::types::PremintTypes;
use std::env;
Expand Down

0 comments on commit 6ad73e2

Please sign in to comment.