Skip to content
Open
Show file tree
Hide file tree
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
15 changes: 14 additions & 1 deletion Cargo.lock

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

9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[workspace]
members = ["proto"]
resolver = "2"

[package]
name = "snapchain"
version = "0.11.0"
Expand All @@ -7,7 +11,9 @@ default-run = "snapchain"
[lib]
name = "snapchain"
path = "src/lib.rs"

[dependencies]
snapchain-proto = { path = "proto" }
tokio = { version = "1.40.0", features = ["full"] }
tokio-stream = "0.1"
serde = { version = "1.0", features = ["derive"] }
Expand Down Expand Up @@ -98,8 +104,6 @@ solar-macros = "=0.1.1"
solar-config = "=0.1.1"
nix = { version = "0.29", features = ["resource"] }

[build-dependencies]
tonic-build = "0.9.2"

[dev-dependencies]
serial_test = "3.1.1"
Expand All @@ -108,3 +112,4 @@ insta = { version = "1.40", features = ["json"] }

[package.metadata.precommit]
fmt = "cargo fmt --check --quiet"

5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ EOF
# since the Cargo configuration references files in src.
# This means we'll re-fetch all crates every time the source code changes,
# which isn't ideal.
COPY Cargo.lock Cargo.toml build.rs ./
COPY Cargo.lock Cargo.toml ./
COPY proto ./proto
COPY src ./src

ENV RUST_BACKTRACE=full
Expand Down Expand Up @@ -59,7 +60,7 @@ RUN <<EOF
EOF

WORKDIR /app
COPY --from=builder /usr/src/app/src/proto /app/proto
COPY --from=builder /usr/src/app/proto/definitions /app/proto
COPY --from=builder /usr/src/app/nodes /app/nodes
COPY --from=builder \
/usr/src/app/target/release/snapchain \
Expand Down
17 changes: 17 additions & 0 deletions proto/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "snapchain-proto"
version = "0.11.0"
edition = "2021"
description = "Protocol buffer definitions for Snapchain"
license = "GPL-3.0"

[dependencies]
prost = "0.13.3"
tonic = { version = "0.12.3", features = ["tls", "tls-native-roots"] }
serde = { version = "1.0", features = ["derive"] }
futures-core = "0.3.31"
hex = "0.4.3"
informalsystems-malachitebft-core-types = { path = "../../malachite/code/crates/core-types" }

[build-dependencies]
tonic-build = "0.9.2"
26 changes: 13 additions & 13 deletions build.rs → proto/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// TODO: auto-discover proto files
builder.compile(
&[
"src/proto/admin_rpc.proto",
"src/proto/blocks.proto",
"src/proto/rpc.proto",
"src/proto/message.proto",
"src/proto/onchain_event.proto",
"src/proto/hub_event.proto",
"src/proto/username_proof.proto",
"src/proto/sync_trie.proto",
"src/proto/node_state.proto",
"src/proto/gossip.proto",
"src/proto/request_response.proto",
"src/proto/replication.proto",
"definitions/admin_rpc.proto",
"definitions/blocks.proto",
"definitions/rpc.proto",
"definitions/message.proto",
"definitions/onchain_event.proto",
"definitions/hub_event.proto",
"definitions/username_proof.proto",
"definitions/sync_trie.proto",
"definitions/node_state.proto",
"definitions/gossip.proto",
"definitions/request_response.proto",
"definitions/replication.proto",
],
&["src/proto"],
&["definitions"],
)?;

Ok(())
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
217 changes: 217 additions & 0 deletions proto/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
use core::fmt;

pub mod proto {
tonic::include_proto!("_");
}

pub use proto::*;

// Basic impls for proto types that don't depend on main crate

impl informalsystems_malachitebft_core_types::Height for proto::Height {
const ZERO: Self = Self::new(0, 0);
const INITIAL: Self = Self::new(0, 1);

fn increment(&self) -> Self {
self.increment()
}

fn as_u64(&self) -> u64 {
self.block_number
}

fn increment_by(&self, n: u64) -> Self {
self.increment_by(n)
}

fn decrement_by(&self, n: u64) -> Option<Self> {
self.decrement_by(n)
}
}

impl informalsystems_malachitebft_core_types::Value for proto::ShardHash {
type Id = proto::ShardHash;

fn id(&self) -> Self::Id {
self.clone()
}
}

impl proto::Height {
pub const fn new(shard_index: u32, block_number: u64) -> Self {
Self {
shard_index,
block_number,
}
}

pub const fn as_u64(&self) -> u64 {
self.block_number
}

pub const fn increment(&self) -> Self {
self.increment_by(1)
}

pub const fn increment_by(&self, n: u64) -> Self {
Self {
shard_index: self.shard_index,
block_number: self.block_number + n,
}
}

pub fn decrement(&self) -> Option<Self> {
self.block_number.checked_sub(1).map(|block_number| Self {
shard_index: self.shard_index,
block_number,
})
}

pub fn decrement_by(&self, n: u64) -> Option<Self> {
self.block_number.checked_sub(n).map(|block_number| Self {
shard_index: self.shard_index,
block_number,
})
}
}

impl fmt::Display for proto::Height {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}] {}", self.shard_index, self.block_number)
}
}

impl fmt::Display for proto::ShardHash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}] {:?}", self.shard_index, hex::encode(&self.hash))
}
}

impl proto::BlockEvent {
pub fn seqnum(&self) -> u64 {
self.data.as_ref().unwrap().seqnum
}

pub fn block_number(&self) -> u64 {
self.data.as_ref().unwrap().block_number
}

pub fn block_timestamp(&self) -> u64 {
self.data.as_ref().unwrap().block_timestamp
}

pub fn event_index(&self) -> u64 {
self.data.as_ref().unwrap().event_index
}
}

impl proto::Message {
pub fn is_type(&self, message_type: proto::MessageType) -> bool {
self.data.is_some() && self.data.as_ref().unwrap().r#type == message_type as i32
}

pub fn fid(&self) -> u64 {
if self.data.is_some() {
self.data.as_ref().unwrap().fid
} else {
0
}
}

pub fn msg_type(&self) -> proto::MessageType {
if self.data.is_some() {
proto::MessageType::try_from(self.data.as_ref().unwrap().r#type)
.unwrap_or(proto::MessageType::None)
} else {
proto::MessageType::None
}
}

pub fn hex_hash(&self) -> String {
hex::encode(&self.hash)
}
}

// Make malachite happy. Prost already implements PartialEq, should be safe to mark as Eq.
impl Eq for proto::FullProposal {}

impl proto::FullProposal {
pub fn shard_id(&self) -> Result<u32, String> {
if let Some(height) = &self.height {
Ok(height.shard_index)
} else {
Err("No height in FullProposal".to_string())
}
}

pub fn shard_hash(&self) -> proto::ShardHash {
match &self.proposed_value {
Some(proto::full_proposal::ProposedValue::Block(block)) => proto::ShardHash {
shard_index: self.height().shard_index as u32,
hash: block.hash.clone(),
},
Some(proto::full_proposal::ProposedValue::Shard(shard_chunk)) => proto::ShardHash {
shard_index: self.height().shard_index as u32,
hash: shard_chunk.hash.clone(),
},
_ => {
panic!("Invalid proposal type");
}
}
}

pub fn block(&self, commits: proto::Commits) -> Option<proto::Block> {
match &self.proposed_value {
Some(proto::full_proposal::ProposedValue::Block(block)) => {
let mut block = block.clone();
block.commits = Some(commits);
Some(block)
}
_ => None,
}
}

pub fn shard_chunk(&self, commits: proto::Commits) -> Option<proto::ShardChunk> {
match &self.proposed_value {
Some(proto::full_proposal::ProposedValue::Shard(chunk)) => {
let mut chunk = chunk.clone();
chunk.commits = Some(commits);
Some(chunk)
}
_ => None,
}
}

pub fn height(&self) -> proto::Height {
self.height.clone().unwrap()
}

pub fn round(&self) -> informalsystems_malachitebft_core_types::Round {
informalsystems_malachitebft_core_types::Round::new(self.round.try_into().unwrap())
}

pub fn to_sign_bytes(&self) -> Vec<u8> {
use prost::Message;
self.encode_to_vec()
}
}

impl proto::ConsensusMessage {
pub fn shard_id(&self) -> Result<u32, String> {
if let Some(msg) = &self.consensus_message {
match msg {
proto::consensus_message::ConsensusMessage::Vote(vote) => {
if let Some(height) = &vote.height {
return Ok(height.shard_index);
}
}
proto::consensus_message::ConsensusMessage::Proposal(vote) => {
if let Some(height) = &vote.height {
return Ok(height.shard_index);
}
}
}
}
Err("Could not determine shard id for ConsensusMessage".to_string())
}
}
2 changes: 1 addition & 1 deletion src/consensus/malachite/host.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Implementation of a host actor for bridiging consensus and the application via a set of channels.

use crate::consensus::validator::{ProposalSource, ShardValidator};
use crate::core::types::SnapchainValidatorContext;
use crate::core::types::{CommitsExt, FullProposalExt, SnapchainValidatorContext};
use crate::network::gossip::GossipEvent;
use crate::proto::{self, decided_value, full_proposal, Block, Commits, FullProposal, ShardChunk};
use crate::utils::statsd_wrapper::StatsdClientWrapper;
Expand Down
2 changes: 1 addition & 1 deletion src/consensus/malachite/snapchain_codec.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::core::types::{Proposal, Signature, SnapchainValidatorContext, Vote};
use crate::core::types::{CommitsExt, Proposal, Signature, SnapchainValidatorContext, Vote};
use crate::proto::sync_request::SyncRequest;
use crate::proto::{self};
use crate::proto::{consensus_message, ConsensusMessage, FullProposal, StatusMessage};
Expand Down
2 changes: 1 addition & 1 deletion src/consensus/read_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::BTreeMap;

use super::validator::StoredValidatorSets;
use crate::consensus::consensus::SystemMessage;
use crate::core::types::SnapchainValidatorContext;
use crate::core::types::{CommitsExt, SnapchainValidatorContext};
use crate::core::util::{verify_signatures, FarcasterTime};
use crate::proto::{self, DecidedValue, FarcasterNetwork, Height};
use crate::storage::store::block_engine::BlockEngine;
Expand Down
4 changes: 2 additions & 2 deletions src/consensus/validator.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::consensus::ValidatorSetConfig;
use crate::consensus::proposer::{BlockProposer, Proposer, ShardProposer};
use crate::core::types::{
Address, Height, ShardId, SnapchainShard, SnapchainValidator, SnapchainValidatorContext,
SnapchainValidatorSet,
Address, FullProposalExt, Height, ShardId, SnapchainShard, SnapchainValidator,
SnapchainValidatorContext, SnapchainValidatorSet,
};
use crate::proto::{full_proposal, Commits, FullProposal, ShardHash};
use crate::storage::store::node_local_state::LocalStateStore;
Expand Down
Loading
Loading