Skip to content

Commit

Permalink
Move db and protobuf related code to separate crates
Browse files Browse the repository at this point in the history
Move `crates/p2p/src/db` to `crates/db`, `crates/p2p/src/wire` to
`crates/wire` and then `crates/wire/proto/` out of `wire` to top level
`proto`. Also some minor refactor stuff and fixes to make CI happy
were applied.
  • Loading branch information
Velnbur authored and NikitaMasych committed Jan 8, 2025
1 parent af5a5da commit 476bfda
Show file tree
Hide file tree
Showing 28 changed files with 589 additions and 517 deletions.
33 changes: 31 additions & 2 deletions Cargo.lock

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

36 changes: 35 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
[workspace]
members = ["crates/p2p"]
members = ["crates/db", "crates/p2p", "crates/wire"]

default-members = ["crates/p2p"]

resolver = "2"

[workspace.dependencies]
async-trait = "0.1.83"
bitcoin = { version = "0.32.5", features = ["serde"] }
futures = "0.3.31"
libp2p = { version = "0.54.1", features = [
"noise",
"gossipsub",
"tcp",
"request-response",
"tokio",
"secp256k1",
"macros",
"ping",
"yamux",
"identify",
] }
libp2p-identity = { version = "0.2.10", default-features = false, features = [
"secp256k1",
"peerid",
"rand",
] }
musig2 = { version = "0.1.0", features = ["serde"] }
prost = "0.13.4"
serde = { version = "1.0.217", features = ["derive"] }
snafu = { version = "0.8.5", default-features = false, features = [
"rust_1_81",
"backtrace",
"std",
] }
tokio = { version = "1.42.0", default-features = false, features = [
"macros",
"time",
] }
tokio-util = { version = "0.7.13", default-features = false }
tracing = { version = "0.1.41", default-features = false }
23 changes: 23 additions & 0 deletions crates/db/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "strata-p2p-db"
version = "0.1.0"
edition = "2024"

[dependencies]
async-trait.workspace = true
bitcoin.workspace = true
libp2p-identity.workspace = true
musig2.workspace = true
prost.workspace = true
serde.workspace = true
snafu.workspace = true

# make this implementation optional
sled = "0.34.7"
# temporaty solution for DB serialization. `ciborium` crate is not working, unfortunatly.
serde_json = "1.0.135"

[dev-dependencies]
tokio = { version = "1.42.0", features = ["rt", "macros"] }
rand = "0.8.5"
secp256k1 = { version = "0.29.0", features = ["rand"] }
38 changes: 7 additions & 31 deletions crates/p2p/src/db/mod.rs → crates/db/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
mod sled;

use async_trait::async_trait;
use bitcoin::{hashes::sha256, OutPoint, XOnlyPublicKey};
use libp2p::PeerId;
use bitcoin::{OutPoint, XOnlyPublicKey, hashes::sha256};
use libp2p_identity::PeerId;
use musig2::{PartialSignature, PubNonce};
use serde::{de::DeserializeOwned, Serialize};
use serde::{Serialize, de::DeserializeOwned};
use snafu::{ResultExt, Snafu};

use crate::states::PeerDepositState;

mod prost_serde;
mod sled;
pub mod states;

pub type DBResult<T> = Result<T, RepositoryError>;

#[derive(Debug, Snafu)]
Expand Down Expand Up @@ -176,29 +178,3 @@ pub struct DepositSetupEntry<DSP: prost::Message + Default> {
pub payload: DSP,
pub signature: Vec<u8>,
}

mod prost_serde {
use std::io::Cursor;

use serde::{de::Error, Deserialize, Deserializer, Serializer};

pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
where
T: prost::Message,
S: Serializer,
{
serializer.serialize_bytes(&value.encode_to_vec())
}

pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
D: Deserializer<'de>,
T: prost::Message + Default,
{
let bytes = Vec::<u8>::deserialize(deserializer)?;
let mut curr = Cursor::new(bytes);
let msg = T::decode(&mut curr).map_err(Error::custom)?;

Ok(msg)
}
}
20 changes: 20 additions & 0 deletions crates/db/src/prost_serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use serde::{Deserialize, Deserializer, Serializer, de::Error};

pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
where
T: prost::Message,
S: Serializer,
{
serializer.serialize_bytes(&value.encode_to_vec())
}

pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
D: Deserializer<'de>,
T: prost::Message + Default,
{
let bytes = Vec::<u8>::deserialize(deserializer)?;
let msg = T::decode(bytes.as_slice()).map_err(Error::custom)?;

Ok(msg)
}
8 changes: 4 additions & 4 deletions crates/p2p/src/db/sled.rs → crates/db/src/sled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ impl From<sled::Error> for RepositoryError {
#[cfg(test)]
mod tests {
use bitcoin::{
hashes::{sha256, Hash},
OutPoint, XOnlyPublicKey,
hashes::{Hash, sha256},
};
use libp2p::PeerId;
use musig2::{sign_partial, AggNonce, KeyAggContext, SecNonce};
use libp2p_identity::PeerId;
use musig2::{AggNonce, KeyAggContext, SecNonce, sign_partial};
use rand::thread_rng;
use secp256k1::{All, Keypair, Secp256k1};

use crate::db::{GenesisInfoEntry, NoncesEntry, PartialSignaturesEntry, RepositoryExt};
use crate::{GenesisInfoEntry, NoncesEntry, PartialSignaturesEntry, RepositoryExt};

#[tokio::test]
async fn test_repository() {
Expand Down
File renamed without changes.
57 changes: 23 additions & 34 deletions crates/p2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,36 @@ version = "0.1.0"
edition = "2021"

[dependencies]
async-trait = "0.1.83"
bitcoin = { version = "0.32.5", features = ["serde"] }
futures = "0.3.31"
libp2p = { version = "0.54.1", features = [
"noise",
"gossipsub",
"tcp",
"request-response",
"tokio",
"secp256k1",
"macros",
"ping",
"yamux",
"identify",
"noise",
"gossipsub",
"tcp",
"request-response",
"tokio",
"secp256k1",
"macros",
"ping",
"yamux",
"identify",
] }
musig2 = { version = "0.1.0", features = ["serde"] }
prost = "0.13.4"
serde = { version = "1.0.217", features = ["derive"] }
snafu = { version = "0.8.5", default-features = false, features = [
"rust_1_81",
"backtrace",
"std",
] }
tokio = { version = "1.42.0", default-features = false, features = [
"macros",
"time",
] }
tokio-util = { version = "0.7.13", default-features = false }
tracing = { version = "0.1.41", default-features = false }

# make this implementation optional
sled = "0.34.7"
# temporaty solution for DB serialization. `ciborium` crate is not working, unfortunatly.
serde_json = "1.0.135"
async-trait.workspace = true
bitcoin = { workspace = true, features = ["serde"] }
futures.workspace = true
musig2 = { workspace = true, features = ["serde"] }
prost.workspace = true
serde = { workspace = true, features = ["derive"] }
snafu = { workspace = true, features = ["rust_1_81", "backtrace", "std"] }
strata-p2p-db.path = "../db"
strata-p2p-wire.path = "../wire"
tokio = { workspace = true, features = ["macros", "time"] }
tokio-util.workspace = true
tracing.workspace = true

[dev-dependencies]
sled = "0.34.7"
tokio = { version = "1.42.0", features = ["rt", "macros", "rt-multi-thread"] }
rand = "0.8.5"
secp256k1 = { version = "0.29.0", features = ["rand"] }
tokio-util = { version = "0.7.13", default-features = false, features = ["rt"] }
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }

[build-dependencies]
prost-build = "0.13.4"
12 changes: 0 additions & 12 deletions crates/p2p/build.rs

This file was deleted.

48 changes: 0 additions & 48 deletions crates/p2p/proto/strata/bitvm2/p2p/v1/gossipsub.proto

This file was deleted.

5 changes: 2 additions & 3 deletions crates/p2p/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//! Commands for P2P implementation from operator implemenation.
//! Commands for P2P implementation from operator implementation.
use bitcoin::{hashes::sha256, OutPoint, XOnlyPublicKey};
use musig2::{PartialSignature, PubNonce};
use prost::Message;

use crate::wire::p2p::v1::typed::{
use strata_p2p_wire::p2p::v1::{
DepositNonces, DepositSetup, DepositSigs, GenesisInfo, GossipsubMsgDepositKind,
GossipsubMsgKind,
};
Expand Down
3 changes: 1 addition & 2 deletions crates/p2p/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use bitcoin::hashes::sha256;
use libp2p::PeerId;
use prost::Message;

use crate::wire::p2p::v1::typed::{GossipsubMsg, GossipsubMsgKind};
use strata_p2p_wire::p2p::v1::{GossipsubMsg, GossipsubMsgKind};

/// Events emitted from P2P to handle from operator side.
///
Expand Down
3 changes: 0 additions & 3 deletions crates/p2p/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
pub mod commands;
pub(crate) mod db;
pub mod events;
pub(crate) mod states;
pub mod swarm;
pub(crate) mod timeouts;
pub mod wire;
2 changes: 1 addition & 1 deletion crates/p2p/src/swarm/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use libp2p::{
swarm::NetworkBehaviour,
PeerId, StreamProtocol,
};
use strata_p2p_wire::p2p::v1::proto::{GetMessageRequest, GetMessageResponse};

use super::{codec, hasher::Sha256Hasher, TOPIC};
use crate::wire::p2p::v1::{GetMessageRequest, GetMessageResponse};

pub type RequestResponseProtoBehaviour<Req, Resp> = RequestResponse<codec::Codec<Req, Resp>>;

Expand Down
Loading

0 comments on commit 476bfda

Please sign in to comment.