Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SigmaAggregation: Integration #43

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM rust:slim AS builder

RUN apt update && apt-get install -y clang
RUN update-ca-certificates

WORKDIR /usr/src/app

# copy entire workspace
COPY . .

RUN cargo build --release


FROM debian:bullseye-slim
COPY --from=builder /usr/src/app/target/release/spectrum-node ./
CMD [ "./spectrum-node" ]
17 changes: 15 additions & 2 deletions spectrum-crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@ pub mod digest;
mod hash;
pub mod pubkey;

/// Result of partial verification.
#[derive(Debug)]
pub enum PVResult<T> {
Invalid,
Valid {
contribution: T,
/// `true` if contribution is only partially valid.
partially: bool,
},
}

/// Some statement which can be verified against public data `P`.
pub trait VerifiableAgainst<P> {
fn verify(&self, public_data: &P) -> bool;
pub trait VerifiableAgainst<P>: Sized {
/// Verifies the statement and returns valid part in case partial verification succeded.
/// Returns `PVResult::Invalid` otherwise.
fn verify(self, public_data: &P) -> PVResult<Self>;
}
2 changes: 1 addition & 1 deletion spectrum-network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ rand = "0.8.5"
rand_chacha = "0.3.1"
wasm-timer = "0.2.5"
serde = { version = "1.0.147", features = ["derive"] }
ciborium = "0.2.0"
ciborium = "0.2.1"
smallvec = "1.10.0"
derive_more = "0.99.17"
either = { version = "1.8.1", features = ["serde"] }
Expand Down
66 changes: 32 additions & 34 deletions spectrum-network/src/protocol_handler.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use ::void::Void;
use std::collections::{BTreeMap, HashMap};
use std::fmt::Debug;
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};

use ::void::Void;
use either::Either;
use futures::channel::mpsc;
use futures::channel::mpsc::Receiver;
Expand Down Expand Up @@ -114,15 +114,15 @@ pub enum ProtocolHandlerError {
MalformedMessage(RawMessage),
}

pub trait ProtocolSpec<'de> {
type THandshake: serde::Serialize + serde::Deserialize<'de> + Versioned + Send;
type TMessage: serde::Serialize + serde::Deserialize<'de> + Versioned + Debug + Send + Clone;
pub trait ProtocolSpec {
type THandshake: serde::Serialize + for<'de> serde::Deserialize<'de> + Versioned + Send;
type TMessage: serde::Serialize + for<'de> serde::Deserialize<'de> + Versioned + Debug + Send + Clone;
}

impl<'de, L, R> ProtocolSpec<'de> for Either<L, R>
impl<L, R> ProtocolSpec for Either<L, R>
where
L: ProtocolSpec<'de>,
R: ProtocolSpec<'de>,
L: ProtocolSpec,
R: ProtocolSpec,
{
type THandshake = Either<L::THandshake, R::THandshake>;
type TMessage = Either<L::TMessage, R::TMessage>;
Expand Down Expand Up @@ -157,21 +157,21 @@ pub trait TemporalProtocolStage<THandshake, TMessage, TOut> {
) -> Poll<Either<ProtocolBehaviourOut<THandshake, TMessage>, TOut>>;
}

pub trait ProtocolBehaviour<'de> {
pub trait ProtocolBehaviour {
/// Protocol specification.
type TProto: ProtocolSpec<'de>;
type TProto: ProtocolSpec;

/// Inject an event that we have established a conn with a peer.
fn inject_peer_connected(&mut self, peer_id: PeerId) {}

/// Inject a new message coming from a peer.
fn inject_message(&mut self, peer_id: PeerId, content: <Self::TProto as ProtocolSpec<'de>>::TMessage) {}
fn inject_message(&mut self, peer_id: PeerId, content: <Self::TProto as ProtocolSpec>::TMessage) {}

/// Inject protocol request coming from a peer.
fn inject_protocol_requested(
&mut self,
peer_id: PeerId,
handshake: Option<<Self::TProto as ProtocolSpec<'de>>::THandshake>,
handshake: Option<<Self::TProto as ProtocolSpec>::THandshake>,
) {
}

Expand All @@ -182,7 +182,7 @@ pub trait ProtocolBehaviour<'de> {
fn inject_protocol_enabled(
&mut self,
peer_id: PeerId,
handshake: Option<<Self::TProto as ProtocolSpec<'de>>::THandshake>,
handshake: Option<<Self::TProto as ProtocolSpec>::THandshake>,
) {
}

Expand All @@ -196,63 +196,62 @@ pub trait ProtocolBehaviour<'de> {
) -> Poll<
Option<
ProtocolBehaviourOut<
<Self::TProto as ProtocolSpec<'de>>::THandshake,
<Self::TProto as ProtocolSpec<'de>>::TMessage,
<Self::TProto as ProtocolSpec>::THandshake,
<Self::TProto as ProtocolSpec>::TMessage,
>,
>,
>;
}

pub struct BehaviourStream<'de, T, P>(T, PhantomData<&'de P>);
pub struct BehaviourStream<T, P>(T, PhantomData<P>);

impl<'de, T, P> BehaviourStream<'de, T, P> {
impl<T, P> BehaviourStream<T, P> {
pub fn new(behaviour: T) -> Self {
Self(behaviour, PhantomData)
}
}

impl<'de, T, P> Unpin for BehaviourStream<'de, T, P>
impl<T, P> Unpin for BehaviourStream<T, P>
where
P: ProtocolSpec<'de>,
T: ProtocolBehaviour<'de, TProto = P>,
P: ProtocolSpec,
T: ProtocolBehaviour<TProto = P>,
{
}

impl<'de, T, P> Stream for BehaviourStream<'de, T, P>
impl<T, P> Stream for BehaviourStream<T, P>
where
P: ProtocolSpec<'de>,
T: ProtocolBehaviour<'de, TProto = P>,
P: ProtocolSpec,
T: ProtocolBehaviour<TProto = P>,
{
type Item =
ProtocolBehaviourOut<<P as ProtocolSpec<'de>>::THandshake, <P as ProtocolSpec<'de>>::TMessage>;
ProtocolBehaviourOut<<P as ProtocolSpec>::THandshake, <P as ProtocolSpec>::TMessage>;

fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = &mut *self;
this.0.poll(cx)
}
}

impl<'de, T, P> FusedStream for BehaviourStream<'de, T, P>
impl<T, P> FusedStream for BehaviourStream<T, P>
where
P: ProtocolSpec<'de>,
T: ProtocolBehaviour<'de, TProto = P>,
P: ProtocolSpec,
T: ProtocolBehaviour<TProto = P>,
{
fn is_terminated(&self) -> bool {
false
}
}

/// A layer that facilitate massage transmission from protocol handlers to peers.
pub struct ProtocolHandler<'de, TBehaviour, TNetwork> {
pub struct ProtocolHandler<TBehaviour, TNetwork> {
peers: HashMap<PeerId, MessageSink>,
inbox: Receiver<ProtocolEvent>,
pub protocol: ProtocolId,
behaviour: TBehaviour,
network: TNetwork,
pd: PhantomData<&'de Void>,
}

impl<'de, TBehaviour, TNetwork> ProtocolHandler<'de, TBehaviour, TNetwork> {
impl<TBehaviour, TNetwork> ProtocolHandler<TBehaviour, TNetwork> {
pub fn new(
behaviour: TBehaviour,
network: TNetwork,
Expand All @@ -267,19 +266,18 @@ impl<'de, TBehaviour, TNetwork> ProtocolHandler<'de, TBehaviour, TNetwork> {
protocol,
behaviour,
network,
pd: PhantomData::default(),
};
(prot_handler, prot_mailbox)
}
}

impl<'de, TBehaviour, TNetwork> Stream for ProtocolHandler<'de, TBehaviour, TNetwork>
impl<TBehaviour, TNetwork> Stream for ProtocolHandler<TBehaviour, TNetwork>
where
TBehaviour: ProtocolBehaviour<'de> + Unpin,
TBehaviour: ProtocolBehaviour + Unpin,
TNetwork: NetworkAPI + Unpin,
{
#[cfg(feature = "integration_tests")]
type Item = <TBehaviour::TProto as ProtocolSpec<'de>>::TMessage;
type Item = <TBehaviour::TProto as ProtocolSpec>::TMessage;
#[cfg(not(feature = "integration_tests"))]
type Item = ();

Expand Down Expand Up @@ -430,7 +428,7 @@ where
}

/// The stream of protocol events never terminates, so we can implement fused for it.
impl<'de, TBehaviour, TNetwork> FusedStream for ProtocolHandler<'de, TBehaviour, TNetwork>
impl<TBehaviour, TNetwork> FusedStream for ProtocolHandler<TBehaviour, TNetwork>
where
Self: Stream,
{
Expand Down
2 changes: 1 addition & 1 deletion spectrum-network/src/protocol_handler/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn encode<T: Serialize>(obj: T) -> RawMessage {
RawMessage::from(encoded)
}

pub fn decode<'de, T: Deserialize<'de>>(msg: RawMessage) -> Result<T, Error<std::io::Error>> {
pub fn decode<T: for<'de> Deserialize<'de>>(msg: RawMessage) -> Result<T, Error<std::io::Error>> {
let bf: Vec<u8> = msg.into();
ciborium::de::from_reader(&bf[..])
}
2 changes: 1 addition & 1 deletion spectrum-network/src/protocol_handler/diffusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ fn decode_modifier(
res.map_err(|_| ())
}

impl<'a, 'de, THistory, TLedgerView> ProtocolBehaviour<'de> for DiffusionBehaviour<'a, THistory, TLedgerView>
impl<'a, THistory, TLedgerView> ProtocolBehaviour for DiffusionBehaviour<'a, THistory, TLedgerView>
where
THistory: HistoryReadAsync + 'a,
TLedgerView: LedgerViewWriteAsync + 'a,
Expand Down
2 changes: 1 addition & 1 deletion spectrum-network/src/protocol_handler/diffusion/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl DiffusionSpec {
}
}

impl<'de> ProtocolSpec<'de> for DiffusionSpec {
impl ProtocolSpec for DiffusionSpec {
type THandshake = DiffusionHandshake;
type TMessage = DiffusionMessage;
}
2 changes: 1 addition & 1 deletion spectrum-network/src/protocol_handler/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ where
}
}

impl<'de, TPeers> ProtocolBehaviour<'de> for DiscoveryBehaviour<TPeers>
impl<TPeers> ProtocolBehaviour for DiscoveryBehaviour<TPeers>
where
TPeers: Peers,
{
Expand Down
2 changes: 1 addition & 1 deletion spectrum-network/src/protocol_handler/discovery/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl DiscoverySpec {
}
}

impl<'de> ProtocolSpec<'de> for DiscoverySpec {
impl ProtocolSpec for DiscoverySpec {
type THandshake = DiscoveryHandshake;
type TMessage = DiscoveryMessage;
}
Loading