diff --git a/.github/workflows/verify.yml b/.github/workflows/verify.yml index c45b0f0..a25f0e0 100644 --- a/.github/workflows/verify.yml +++ b/.github/workflows/verify.yml @@ -14,6 +14,9 @@ concurrency: group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} cancel-in-progress: true +env: + MALACHITE_GIT_REF: "8a9f3702eb41199bc8a7f45139adba233a04744a" + jobs: build: timeout-minutes: 20 @@ -35,15 +38,26 @@ jobs: uses: arduino/setup-protoc@v3 - uses: actions/checkout@v4 + with: + repository: informalsystems/malachite + ref: ${{ env.MALACHITE_GIT_REF }} + path: ./malachite + + - uses: actions/checkout@v4 + with: + path: ./snapchain - - env: + - working-directory: ./snapchain + env: RUSTFLAGS: "-Dwarnings" run: cargo test - - env: + - working-directory: ./snapchain + env: RUSTFLAGS: "-Dwarnings" run: cargo build --bins - - env: + - working-directory: ./snapchain + env: RUSTFLAGS: "-Dwarnings" run: cargo fmt --all --check diff --git a/Cargo.lock b/Cargo.lock index 695e154..9614476 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1552,18 +1552,6 @@ dependencies = [ "crossbeam-utils", ] -[[package]] -name = "config" -version = "0.14.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68578f196d2a33ff61b27fae256c3164f65e36382648e30666dde05b8cc9dfdf" -dependencies = [ - "nom", - "pathdiff", - "serde", - "toml 0.8.19", -] - [[package]] name = "const-hex" version = "1.14.0" @@ -3509,105 +3497,6 @@ dependencies = [ "serde", ] -[[package]] -name = "informalsystems-malachitebft-config" -version = "0.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "298211c1623c3c52053985d5142889467ebfc69d1353b58adf3efaf1991f879d" -dependencies = [ - "bytesize", - "config", - "humantime-serde", - "informalsystems-malachitebft-core-types", - "multiaddr", - "serde", -] - -[[package]] -name = "informalsystems-malachitebft-core-consensus" -version = "0.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d30e3ac751eea2722a48628e5942df1d479b9294286cd68cf5c2cc837f50dc6" -dependencies = [ - "async-recursion", - "derive-where", - "genawaiter", - "informalsystems-malachitebft-core-driver", - "informalsystems-malachitebft-core-types", - "informalsystems-malachitebft-metrics", - "informalsystems-malachitebft-peer", - "multiaddr", - "thiserror 2.0.3", - "tracing", -] - -[[package]] -name = "informalsystems-malachitebft-core-driver" -version = "0.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6be2dc3e680a9ae3fc3f679b9be48276614d85227b79bfa1401b2c4fa739436a" -dependencies = [ - "derive-where", - "informalsystems-malachitebft-core-state-machine", - "informalsystems-malachitebft-core-types", - "informalsystems-malachitebft-core-votekeeper", - "thiserror 2.0.3", -] - -[[package]] -name = "informalsystems-malachitebft-core-state-machine" -version = "0.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c9efb2e03d77dc1c74341fbf2ce7c361dde971c48aa0b518218392ab5ddbac0" -dependencies = [ - "derive-where", - "displaydoc", - "informalsystems-malachitebft-core-types", -] - -[[package]] -name = "informalsystems-malachitebft-core-types" -version = "0.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99d291b3b39cdbf972bba3706458f9009dac26341c638270d3b62b77213a075a" -dependencies = [ - "bytes", - "derive-where", - "thiserror 2.0.3", -] - -[[package]] -name = "informalsystems-malachitebft-core-votekeeper" -version = "0.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64fa18f6a2f9165bdd2777e3e48355948db66da5f3d7ba50e7e6d7ac520d18f3" -dependencies = [ - "derive-where", - "informalsystems-malachitebft-core-types", - "thiserror 2.0.3", -] - -[[package]] -name = "informalsystems-malachitebft-metrics" -version = "0.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2c8c07b0209939dacf44613af1badccae12d426181ce542f06f50692318d312" -dependencies = [ - "informalsystems-malachitebft-core-state-machine", - "prometheus-client", -] - -[[package]] -name = "informalsystems-malachitebft-peer" -version = "0.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9bba9c096458e76678ea52c1956523e749a5a9740bda98716f3ba224139ba61e" -dependencies = [ - "bs58", - "multihash", - "thiserror 2.0.3", -] - [[package]] name = "inlinable_string" version = "0.1.15" @@ -4326,6 +4215,85 @@ dependencies = [ "libc", ] +[[package]] +name = "malachite-common" +version = "0.1.0" +dependencies = [ + "bytes", + "derive-where", +] + +[[package]] +name = "malachite-config" +version = "0.1.0" +dependencies = [ + "bytesize", + "humantime-serde", + "malachite-common", + "multiaddr", + "serde", +] + +[[package]] +name = "malachite-consensus" +version = "0.1.0" +dependencies = [ + "async-recursion", + "derive-where", + "genawaiter", + "libp2p-identity", + "malachite-common", + "malachite-driver", + "malachite-metrics", + "multiaddr", + "thiserror 1.0.69", + "tracing", +] + +[[package]] +name = "malachite-driver" +version = "0.1.0" +dependencies = [ + "derive-where", + "malachite-common", + "malachite-round", + "malachite-vote", +] + +[[package]] +name = "malachite-metrics" +version = "0.1.0" +dependencies = [ + "malachite-round", + "prometheus-client", +] + +[[package]] +name = "malachite-node" +version = "0.1.0" +dependencies = [ + "malachite-common", + "rand", + "serde", +] + +[[package]] +name = "malachite-round" +version = "0.1.0" +dependencies = [ + "derive-where", + "displaydoc", + "malachite-common", +] + +[[package]] +name = "malachite-vote" +version = "0.1.0" +dependencies = [ + "derive-where", + "malachite-common", +] + [[package]] name = "match_cfg" version = "0.1.0" @@ -4439,9 +4407,9 @@ dependencies = [ [[package]] name = "multihash" -version = "0.19.3" +version = "0.19.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b430e7953c29dd6a09afc29ff0bb69c6e306329ee6794700aee27b76a1aea8d" +checksum = "cc41f430805af9d1cf4adae4ed2149c759b877b01d909a1f40256188d09345d2" dependencies = [ "core2", "unsigned-varint 0.8.0", @@ -4864,12 +4832,6 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e91099d4268b0e11973f036e885d652fb0b21fedcf69738c627f94db6a44f42" -[[package]] -name = "pathdiff" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df94ce210e5bc13cb6651479fa48d14f601d9858cfe0467f43ae157023b938d3" - [[package]] name = "pear" version = "0.2.9" @@ -6309,12 +6271,13 @@ dependencies = [ "hex", "humantime", "humantime-serde", - "informalsystems-malachitebft-config", - "informalsystems-malachitebft-core-consensus", - "informalsystems-malachitebft-core-types", - "informalsystems-malachitebft-metrics", "itertools 0.13.0", "libp2p", + "malachite-common", + "malachite-config", + "malachite-consensus", + "malachite-metrics", + "malachite-node", "once_cell", "parking_lot", "pre-commit", @@ -7098,9 +7061,9 @@ checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" [[package]] name = "tracing" -version = "0.1.41" +version = "0.1.40" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" +checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" dependencies = [ "log", "pin-project-lite", @@ -7110,9 +7073,9 @@ dependencies = [ [[package]] name = "tracing-attributes" -version = "0.1.28" +version = "0.1.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" +checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", @@ -7121,9 +7084,9 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.33" +version = "0.1.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" +checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" dependencies = [ "once_cell", "valuable", diff --git a/Cargo.toml b/Cargo.toml index b430d14..4015e2d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,10 +25,11 @@ async-trait = "0.1.68" tracing-subscriber = { version = "0.3.18", features = ["env-filter", "fmt", "json"] } hex = "0.4.3" ractor = "0.11.2" -informalsystems-malachitebft-core-consensus = { version = "0.0.1" } -informalsystems-malachitebft-core-types = { version = "0.0.1"} -informalsystems-malachitebft-config = { version = "0.0.1"} -informalsystems-malachitebft-metrics = { version = "0.0.1" } +malachite-consensus = { path = "../malachite/code/crates/consensus" } +malachite-common = { path = "../malachite/code/crates/common" } +malachite-config = { path = "../malachite/code/crates/config" } +malachite-node = { path = "../malachite/code/crates/node" } +malachite-metrics = { path = "../malachite/code/crates/metrics" } blake3 = "1.4.1" tracing = "0.1.40" thiserror = "1.0.66" diff --git a/Dockerfile b/Dockerfile index b3c2b4c..8d8b865 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,11 +2,25 @@ FROM rust:1.83 AS builder WORKDIR /usr/src/app -RUN apt-get update && apt-get install -y libclang-dev libjemalloc-dev llvm-dev make protobuf-compiler cmake +# Build Malachite first +ARG MALACHITE_GIT_REPO_URL=https://github.com/informalsystems/malachite.git +ENV MALACHITE_GIT_REPO_URL=$MALACHITE_GIT_REPO_URL +ARG MALACHITE_GIT_REF=8a9f3702eb41199bc8a7f45139adba233a04744a +ENV RUST_BACKTRACE=1 +RUN < { /// Start consensus for the given height StartHeight(Ctx::Height), /// The proposal builder has built a value and can be used in a new proposal consensus message - ProposeValue( - Ctx::Height, - Round, - Ctx::Value, - Option>, - ), + ProposeValue(Ctx::Height, Round, Ctx::Value, Option), /// Received and assembled the full value proposed by a validator ReceivedProposedValue(ProposedValue), @@ -146,19 +136,22 @@ impl Timeouts { self.config = config; } - fn duration_for(&self, step: TimeoutKind) -> Duration { - self.config.timeout_duration(step) + fn duration_for(&self, step: TimeoutStep) -> Duration { + match step { + TimeoutStep::Propose => self.config.timeout_propose, + TimeoutStep::Prevote => self.config.timeout_prevote, + TimeoutStep::Precommit => self.config.timeout_precommit, + TimeoutStep::Commit => self.config.timeout_commit, + } } - fn increase_timeout(&mut self, step: TimeoutKind) { + fn increase_timeout(&mut self, step: TimeoutStep) { let c = &mut self.config; match step { - TimeoutKind::Propose => c.timeout_propose += c.timeout_propose_delta, - TimeoutKind::Prevote => c.timeout_prevote += c.timeout_prevote_delta, - TimeoutKind::Precommit => c.timeout_precommit += c.timeout_precommit_delta, - TimeoutKind::Commit => (), - TimeoutKind::PrevoteTimeLimit => (), - TimeoutKind::PrecommitTimeLimit => (), + TimeoutStep::Propose => c.timeout_propose += c.timeout_propose_delta, + TimeoutStep::Prevote => c.timeout_prevote += c.timeout_prevote_delta, + TimeoutStep::Precommit => c.timeout_precommit += c.timeout_precommit_delta, + TimeoutStep::Commit => (), }; } } @@ -173,7 +166,7 @@ pub struct Consensus { // pub type ConsensusMsg = ConsensusMsg; -type ConsensusInput = informalsystems_malachitebft_core_consensus::Input; +type ConsensusInput = malachite_consensus::Input; pub struct State { /// Scheduler for timers @@ -230,7 +223,7 @@ impl Consensus { state: &mut State, input: ConsensusInput, ) -> Result<(), ActorProcessingErr> { - informalsystems_malachitebft_core_consensus::process!( + malachite_consensus::process!( input: input, state: &mut state.consensus, metrics: &self.metrics, @@ -252,18 +245,12 @@ impl Consensus { Ok(()) } - ConsensusMsg::ProposeValue(height, round, value, extension) => { + ConsensusMsg::ProposeValue(height, round, value, _) => { let result = self .process_input( &myself, state, - ConsensusInput::Propose(ValueToPropose { - height, - round, - valid_round: Round::Nil, - value, - extension, - }), + ConsensusInput::ProposeValue(height, round, value, None), ) .await; @@ -391,7 +378,7 @@ impl Consensus { .process_input( &myself, state, - ConsensusInput::ProposedValue(proposed_value, ValueOrigin::Consensus), + ConsensusInput::ReceivedProposedValue(proposed_value), ) .await; @@ -408,10 +395,10 @@ impl Consensus { return Ok(()); }; - state.timeouts.increase_timeout(timeout.kind); + state.timeouts.increase_timeout(timeout.step); - if matches!(timeout.kind, TimeoutKind::Prevote | TimeoutKind::Precommit) { - warn!(step = ?timeout.kind, "Timeout elapsed"); + if matches!(timeout.step, TimeoutStep::Prevote | TimeoutStep::Precommit) { + warn!(step = ?timeout.step, "Timeout elapsed"); } let result = self @@ -430,11 +417,7 @@ impl Consensus { value.value, value.height, value.round, self.params.address ); let result = self - .process_input( - &myself, - state, - ConsensusInput::ProposedValue(value, ValueOrigin::Sync), - ) + .process_input(&myself, state, ConsensusInput::ReceivedProposedValue(value)) .await; if let Err(e) = result { @@ -491,138 +474,77 @@ impl Consensus { effect: Effect, ) -> Result, ActorProcessingErr> { match effect { - Effect::Publish(gossip_msg, r) => { - match gossip_msg { - SignedConsensusMsg::Proposal(proposal) => { - debug!( - "Broadcasting proposal gossip message: {:?} {:?} from {:?}", - proposal.height, proposal.round, proposal.proposer - ); - gossip_tx - .send(GossipEvent::BroadcastSignedProposal(proposal)) - .await?; - } - SignedConsensusMsg::Vote(vote) => { - debug!( - "Broadcasting vote gossip message: {:?} {:?} {:?} from {:?}", - vote.vote_type, vote.height, vote.round, vote.voter - ); - gossip_tx - .send(GossipEvent::BroadcastSignedVote(vote)) - .await?; - } - } - - Ok(r.resume_with(())) - } - - Effect::RestreamValue(_, _, _, _, _, r) => { - // This is for interacting with host actor - Ok(r.resume_with(())) - } - Effect::GetVoteSet(_, _, r) => { - // TODO(aditi): We need to incorporate the sync actor for this. Only relevant if consensus is stuck. - Ok(r.resume_with(())) - } - Effect::SendVoteSetResponse(_, _, _, _, r) => { - // TODO(aditi): We need to incorporate the sync actor for this. Only relevant if consensus is stuck. - Ok(r.resume_with(())) - } - Effect::PersistMessage(_, r) => { - // TODO(aditi): This is for the WAL, which is used for crash recovery. - Ok(r.resume_with(())) - } - Effect::PersistTimeout(_, r) => { - // TODO(aditi): This is for the WAL, which is used for crash recovery. - Ok(r.resume_with(())) - } - Effect::SignProposal(proposal, r) => { - let start = Instant::now(); - - let signed_proposal = self.ctx.signing_provider().sign_proposal(proposal); - - self.metrics - .signature_signing_time - .observe(start.elapsed().as_secs_f64()); - - Ok(r.resume_with(signed_proposal)) - } - - Effect::SignVote(vote, r) => { - let start = Instant::now(); - - let signed_vote = self.ctx.signing_provider().sign_vote(vote); - - self.metrics - .signature_signing_time - .observe(start.elapsed().as_secs_f64()); - - Ok(r.resume_with(signed_vote)) - } - - Effect::VerifyCertificate(certificate, validator_set, thresholds, r) => { - let valid = self.ctx.signing_provider().verify_certificate( - &certificate, - &validator_set, - thresholds, - ); - - Ok(r.resume_with(valid)) - } - - Effect::ResetTimeouts(r) => { + Effect::ResetTimeouts => { timeouts.reset(self.timeout_config); - Ok(r.resume_with(())) + Ok(Resume::Continue) } - Effect::CancelAllTimeouts(r) => { + Effect::CancelAllTimeouts => { timers.cancel_all(); - Ok(r.resume_with(())) + Ok(Resume::Continue) } - Effect::CancelTimeout(timeout, r) => { + Effect::CancelTimeout(timeout) => { timers.cancel(&timeout); - Ok(r.resume_with(())) + Ok(Resume::Continue) } - Effect::ScheduleTimeout(timeout, r) => { - let duration = timeouts.duration_for(timeout.kind); + Effect::ScheduleTimeout(timeout) => { + let duration = timeouts.duration_for(timeout.step); timers.start_timer(timeout, duration); - Ok(r.resume_with(())) + Ok(Resume::Continue) } - Effect::StartRound(height, round, proposer, r) => { + Effect::StartRound(height, round, proposer) => { debug!("Starting height: {height}, round: {round}, proposer: {proposer}"); shard_validator.start_round(height, round, proposer); - Ok(r.resume_with(())) + Ok(Resume::Continue) } - Effect::VerifySignature(msg, pk, r) => { - use informalsystems_malachitebft_core_consensus::ConsensusMsg as Msg; + Effect::VerifySignature(msg, pk) => { + use malachite_consensus::ConsensusMsg as Msg; let start = Instant::now(); let valid = match msg.message { - Msg::Vote(v) => { - self.ctx - .signing_provider() - .verify_signed_vote(&v, &msg.signature, &pk) - } - Msg::Proposal(p) => { - self.ctx - .signing_provider() - .verify_signed_proposal(&p, &msg.signature, &pk) - } + Msg::Vote(v) => self.ctx.verify_signed_vote(&v, &msg.signature, &pk), + Msg::Proposal(p) => self.ctx.verify_signed_proposal(&p, &msg.signature, &pk), }; self.metrics .signature_verification_time .observe(start.elapsed().as_secs_f64()); - Ok(r.resume_with(valid)) + Ok(Resume::SignatureValidity(valid)) + } + + Effect::Broadcast(gossip_msg) => { + match gossip_msg { + SignedConsensusMsg::Proposal(proposal) => { + debug!( + "Broadcasting proposal gossip message: {:?} {:?} from {:?}", + proposal.height, proposal.round, proposal.proposer + ); + gossip_tx + .send(GossipEvent::BroadcastSignedProposal(proposal)) + .await?; + } + SignedConsensusMsg::Vote(vote) => { + debug!( + "Broadcasting vote gossip message: {:?} {:?} {:?} from {:?}", + vote.vote_type, vote.height, vote.round, vote.voter + ); + gossip_tx + .send(GossipEvent::BroadcastSignedVote(vote)) + .await?; + } + } + + Ok(Resume::Continue) } - Effect::GetValue(height, round, timeout, r) => { - let timeout = timeouts.duration_for(timeout.kind); + + Effect::GetValue(height, round, timeout) => { + let timeout = timeouts.duration_for(timeout.step); let full_proposal = shard_validator.propose_value(height, round, timeout).await; let value = full_proposal.shard_hash(); @@ -637,33 +559,31 @@ impl Consensus { .send(GossipEvent::BroadcastFullProposal(full_proposal)) .await?; - Ok(r.resume_with(())) - } - - Effect::GetValidatorSet(_, r) => { - Ok(r.resume_with(Some(shard_validator.get_validator_set()))) + Ok(Resume::Continue) } - Effect::Decide( - CommitCertificate { - height, - round, - value_id: value, - aggregated_signature: commits, - }, - r, - ) => { + Effect::GetValidatorSet(height) => Ok(Resume::ValidatorSet( + height, + Some(shard_validator.get_validator_set()), + )), + + Effect::Decide { + height, + round, + value, + commits, + } => { info!( "Deciding value: {value} for height: {height} at {:?} with {:?} commits", self.params.address, - commits.signatures.len() + commits.len() ); shard_validator.decide(height, round, value.clone()).await; let result = myself.cast(ConsensusMsg::StartHeight(height.increment())); if let Err(e) = result { error!("Error when starting next height after decision on {height}: {e:?}"); } - Ok(r.resume_with(())) + Ok(Resume::Continue) } } } diff --git a/src/consensus/proposer.rs b/src/consensus/proposer.rs index d35179d..ea92968 100644 --- a/src/consensus/proposer.rs +++ b/src/consensus/proposer.rs @@ -7,7 +7,7 @@ use crate::proto::{BlocksRequest, ShardChunksRequest}; use crate::storage::store::engine::{BlockEngine, ShardEngine, ShardStateChange}; use crate::storage::store::BlockStorageError; use crate::utils::statsd_wrapper::StatsdClientWrapper; -use informalsystems_malachitebft_core_types::{Round, Validity}; +use malachite_common::{Round, Validity}; use prost::Message; use std::collections::BTreeMap; use std::time::Duration; diff --git a/src/consensus/validator.rs b/src/consensus/validator.rs index 7c0b7d4..6e9fc43 100644 --- a/src/consensus/validator.rs +++ b/src/consensus/validator.rs @@ -4,8 +4,8 @@ use crate::core::types::{ SnapchainValidatorSet, }; use crate::proto::FullProposal; -use informalsystems_malachitebft_core_consensus::ProposedValue; -use informalsystems_malachitebft_core_types::{Round, ValidatorSet}; +use malachite_common::{Round, ValidatorSet}; +use malachite_consensus::ProposedValue; use std::collections::HashSet; use std::time::Duration; use tracing::error; @@ -134,8 +134,7 @@ impl ShardValidator { ProposedValue { height: full_proposal.height(), round: full_proposal.round(), - valid_round: Round::Nil, - proposer: full_proposal.proposer_address(), + validator_address: full_proposal.proposer_address(), value, validity, extension: None, diff --git a/src/core/types.rs b/src/core/types.rs index 6cef222..c89ab95 100644 --- a/src/core/types.rs +++ b/src/core/types.rs @@ -1,10 +1,10 @@ use core::fmt; -use informalsystems_malachitebft_core_types::{self, SignedMessage, SigningProvider}; -use informalsystems_malachitebft_core_types::{ +use libp2p::identity::ed25519::Keypair; +use malachite_common; +use malachite_common::{ Extension, NilOrVal, Round, SignedProposal, SignedProposalPart, SignedVote, Validator, VoteType, VotingPower, }; -use libp2p::identity::ed25519::Keypair; use prost::Message; use serde::{Deserialize, Serialize}; use std::fmt::{Debug, Display}; @@ -48,10 +48,7 @@ pub trait ShardedContext { type ShardId: ShardId; } -pub trait SnapchainContext: - informalsystems_malachitebft_core_types::Context + ShardedContext -{ -} +pub trait SnapchainContext: malachite_common::Context + ShardedContext {} // TODO: Should validator keys be ECDSA? #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -89,116 +86,11 @@ impl fmt::Debug for Address { } } -impl informalsystems_malachitebft_core_types::Address for Address {} +impl malachite_common::Address for Address {} #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct Ed25519 {} -#[derive(Clone, Debug)] -pub struct Ed25519Provider { - keypair: Arc, -} - -impl Ed25519Provider { - pub fn new(keypair: Arc) -> Self { - Self { keypair } - } -} - -impl SigningProvider for Ed25519Provider { - fn sign_vote( - &self, - vote: ::Vote, - ) -> SignedMessage< - SnapchainValidatorContext, - ::Vote, - > { - let signature = self.keypair.sign(&vote.to_sign_bytes()); - SignedVote::new(vote, Signature(signature)) - } - - fn verify_signed_vote( - &self, - vote: &::Vote, - signature: &informalsystems_malachitebft_core_types::Signature, - public_key: &informalsystems_malachitebft_core_types::PublicKey, - ) -> bool { - let valid = public_key.verify(&vote.to_sign_bytes(), &signature.0); - if !valid { - panic!("Invalid signature"); - } - valid - } - - fn sign_proposal(&self, proposal: Proposal) -> SignedProposal { - let signature = self.keypair.sign(&proposal.to_sign_bytes()); - SignedProposal::new(proposal, Signature(signature)) - } - - fn verify_signed_proposal( - &self, - proposal: &::Proposal, - signature: &informalsystems_malachitebft_core_types::Signature, - public_key: &informalsystems_malachitebft_core_types::PublicKey, - ) -> bool { - let valid = public_key.verify(&proposal.to_sign_bytes(), &signature.0); - if !valid { - panic!("Invalid signature"); - } - valid - } - - fn sign_proposal_part( - &self, - proposal_part: ::ProposalPart, - ) -> SignedMessage::ProposalPart>{ - SignedProposalPart::new(proposal_part, Signature(vec![])) - } - - fn verify_signed_proposal_part( - &self, - _proposal_part: &::ProposalPart, - _signature: &informalsystems_malachitebft_core_types::Signature, - _public_key: &informalsystems_malachitebft_core_types::PublicKey, - ) -> bool { - // TODO(aditi): We don't handle proposal parts yet in consensus - todo!() - } - - fn verify_commit_signature( - &self, - certificate: &informalsystems_malachitebft_core_types::CommitCertificate< - SnapchainValidatorContext, - >, - commit_sig: &informalsystems_malachitebft_core_types::CommitSignature< - SnapchainValidatorContext, - >, - validator: &::Validator, - ) -> Result< - VotingPower, - informalsystems_malachitebft_core_types::CertificateError, - > { - // Reconstruct the vote that was signed - let vote = Vote::new_precommit( - certificate.height, - certificate.round, - NilOrVal::Val(certificate.value_id.clone()), - validator.address().clone(), - ); - - // Verify signature - if !self.verify_signed_vote(&vote, &commit_sig.signature, validator.public_key()) { - return Err( - informalsystems_malachitebft_core_types::CertificateError::InvalidSignature( - commit_sig.clone(), - ), - ); - } - - Ok(validator.voting_power()) - } -} - pub struct InvalidSignatureError(); impl fmt::Display for InvalidSignatureError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { @@ -213,7 +105,7 @@ pub struct Signature(pub Vec); pub type PublicKey = libp2p::identity::ed25519::PublicKey; pub type PrivateKey = libp2p::identity::ed25519::SecretKey; -impl informalsystems_malachitebft_core_types::SigningScheme for Ed25519 { +impl malachite_common::SigningScheme for Ed25519 { type DecodingError = InvalidSignatureError; type Signature = Signature; type PublicKey = PublicKey; @@ -267,13 +159,6 @@ impl Height { block_number, }) } - - pub fn decrement_by(&self, n: u64) -> Option { - self.block_number.checked_sub(n).map(|block_number| Self { - shard_index: self.shard_index, - block_number, - }) - } } impl fmt::Display for Height { @@ -282,7 +167,7 @@ impl fmt::Display for Height { } } -impl informalsystems_malachitebft_core_types::Height for Height { +impl malachite_common::Height for Height { fn increment(&self) -> Self { self.increment() } @@ -290,14 +175,6 @@ impl informalsystems_malachitebft_core_types::Height for Height { 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.decrement_by(n) - } } // #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] @@ -318,7 +195,7 @@ impl fmt::Display for ShardHash { // } // } -impl informalsystems_malachitebft_core_types::Value for ShardHash { +impl malachite_common::Value for ShardHash { type Id = ShardHash; fn id(&self) -> Self::Id { @@ -366,7 +243,7 @@ impl FullProposal { } pub fn round(&self) -> Round { - Round::new(self.round.try_into().unwrap()) + Round::new(self.round) } } @@ -530,7 +407,7 @@ impl Vote { Self { vote_type, height: proto.height.unwrap(), - round: Round::new(proto.round.try_into().unwrap()), + round: Round::new(proto.round), voter: Address::from_vec(proto.voter), shard_hash, extension: None, @@ -565,9 +442,9 @@ impl Proposal { pub fn from_proto(proto: proto::Proposal) -> Self { Self { height: proto.height.unwrap(), - round: Round::new(proto.round.try_into().unwrap()), + round: Round::new(proto.round), shard_hash: proto.value.unwrap(), - pol_round: Round::try_from(proto.pol_round).unwrap(), + pol_round: Round::new(proto.pol_round), proposer: Address::from_vec(proto.proposer), } } @@ -592,15 +469,12 @@ pub enum ProposalPart { #[derive(Clone, Debug)] pub struct SnapchainValidatorContext { keypair: Arc, - signing_provider: Ed25519Provider, } impl SnapchainValidatorContext { pub fn new(keypair: Keypair) -> Self { - let keypair = Arc::new(keypair); Self { - keypair: keypair.clone(), - signing_provider: Ed25519Provider::new(keypair), + keypair: Arc::new(keypair), } } @@ -613,7 +487,7 @@ impl ShardedContext for SnapchainValidatorContext { type ShardId = SnapchainShard; } -impl informalsystems_malachitebft_core_types::Context for SnapchainValidatorContext { +impl malachite_common::Context for SnapchainValidatorContext { type Address = Address; type Height = Height; type ProposalPart = ProposalPart; @@ -623,7 +497,6 @@ impl informalsystems_malachitebft_core_types::Context for SnapchainValidatorCont type Value = ShardHash; type Vote = Vote; type SigningScheme = Ed25519; - type SigningProvider = Ed25519Provider; fn select_proposer<'a>( &self, @@ -647,6 +520,55 @@ impl informalsystems_malachitebft_core_types::Context for SnapchainValidatorCont .expect("proposer_index is valid") } + fn sign_vote(&self, vote: Self::Vote) -> SignedVote { + let signature = self.keypair.sign(&vote.to_sign_bytes()); + SignedVote::new(vote, Signature(signature)) + } + + fn verify_signed_vote( + &self, + vote: &Vote, + signature: &Signature, + public_key: &PublicKey, + ) -> bool { + let valid = public_key.verify(&vote.to_sign_bytes(), &signature.0); + if !valid { + panic!("Invalid signature"); + } + valid + } + + fn sign_proposal(&self, proposal: Self::Proposal) -> SignedProposal { + let signature = self.keypair.sign(&proposal.to_sign_bytes()); + SignedProposal::new(proposal, Signature(signature)) + } + + fn verify_signed_proposal( + &self, + proposal: &Proposal, + signature: &Signature, + public_key: &PublicKey, + ) -> bool { + let valid = public_key.verify(&proposal.to_sign_bytes(), &signature.0); + if !valid { + panic!("Invalid signature"); + } + valid + } + + fn sign_proposal_part(&self, proposal_part: Self::ProposalPart) -> SignedProposalPart { + SignedProposalPart::new(proposal_part, Signature(vec![])) + } + + fn verify_signed_proposal_part( + &self, + _proposal_part: &ProposalPart, + _signature: &Signature, + _public_key: &PublicKey, + ) -> bool { + todo!() + } + fn new_proposal( height: Height, round: Round, @@ -680,17 +602,11 @@ impl informalsystems_malachitebft_core_types::Context for SnapchainValidatorCont ) -> Vote { Vote::new_precommit(height, round, value_id, address) } - - fn signing_provider(&self) -> &Self::SigningProvider { - &self.signing_provider - } } impl SnapchainContext for SnapchainValidatorContext {} -impl informalsystems_malachitebft_core_types::ProposalPart - for ProposalPart -{ +impl malachite_common::ProposalPart for ProposalPart { fn is_first(&self) -> bool { // Only one part for now true @@ -701,7 +617,7 @@ impl informalsystems_malachitebft_core_types::ProposalPart for Proposal { +impl malachite_common::Proposal for Proposal { fn height(&self) -> Height { self.height } @@ -727,7 +643,7 @@ impl informalsystems_malachitebft_core_types::Proposal for Vote { +impl malachite_common::Vote for Vote { fn height(&self) -> Height { self.height } @@ -752,23 +668,19 @@ impl informalsystems_malachitebft_core_types::Vote fo &self.voter } - fn extension( - &self, - ) -> std::option::Option<&SignedMessage> { + fn extension(&self) -> Option<&Extension> { None } - fn extend(self, extension: SignedMessage) -> Self { + fn extend(self, extension: Extension) -> Self { Self { - extension: Some(extension.message), + extension: Some(extension), ..self } } } -impl informalsystems_malachitebft_core_types::ValidatorSet - for SnapchainValidatorSet -{ +impl malachite_common::ValidatorSet for SnapchainValidatorSet { fn count(&self) -> usize { self.validators.len() } @@ -790,9 +702,7 @@ impl informalsystems_malachitebft_core_types::ValidatorSet - for SnapchainValidator -{ +impl malachite_common::Validator for SnapchainValidator { fn address(&self) -> &Address { &self.address } diff --git a/src/main.rs b/src/main.rs index 1d207c4..67c410f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use informalsystems_malachitebft_metrics::{Metrics, SharedRegistry}; +use malachite_metrics::{Metrics, SharedRegistry}; use snapchain::connectors::onchain_events::{L1Client, RealL1Client}; use snapchain::consensus::consensus::SystemMessage; use snapchain::core::types::proto; diff --git a/src/network/gossip.rs b/src/network/gossip.rs index 8d433e7..dcedeed 100644 --- a/src/network/gossip.rs +++ b/src/network/gossip.rs @@ -4,10 +4,10 @@ use crate::core::types::{ SnapchainValidatorContext, Vote, }; use futures::StreamExt; -use informalsystems_malachitebft_core_types::{SignedProposal, SignedVote}; use libp2p::identity::ed25519::Keypair; use libp2p::swarm::dial_opts::DialOpts; use libp2p::{gossipsub, noise, swarm::NetworkBehaviour, swarm::SwarmEvent, tcp, yamux, Swarm}; +use malachite_common::{SignedProposal, SignedVote}; use prost::Message; use serde::{Deserialize, Serialize}; use std::collections::hash_map::DefaultHasher; diff --git a/src/node/snapchain_node.rs b/src/node/snapchain_node.rs index 7458dd1..30024ae 100644 --- a/src/node/snapchain_node.rs +++ b/src/node/snapchain_node.rs @@ -15,10 +15,9 @@ use crate::storage::store::stores::Stores; use crate::storage::store::BlockStore; use crate::storage::trie::merkle_trie; use crate::utils::statsd_wrapper::StatsdClientWrapper; -use informalsystems_malachitebft_config::TimeoutConfig; -use informalsystems_malachitebft_core_consensus::ValuePayload; -use informalsystems_malachitebft_metrics::Metrics; use libp2p::identity::ed25519::Keypair; +use malachite_config::TimeoutConfig; +use malachite_metrics::Metrics; use ractor::ActorRef; use std::collections::{BTreeMap, HashMap}; use tokio::sync::mpsc; @@ -76,11 +75,10 @@ impl SnapchainNode { ); let shard_validator_set = SnapchainValidatorSet::new(vec![shard_validator]); let shard_consensus_params = ConsensusParams { - initial_height: Height::new(shard.shard_id(), 1), + start_height: Height::new(shard.shard_id(), 1), initial_validator_set: shard_validator_set, address: validator_address.clone(), threshold_params: Default::default(), - value_payload: ValuePayload::ProposalAndParts, }; let ctx = SnapchainValidatorContext::new(keypair.clone()); @@ -146,11 +144,10 @@ impl SnapchainNode { let block_validator_set = SnapchainValidatorSet::new(vec![block_validator]); let block_consensus_params = ConsensusParams { - initial_height: Height::new(block_shard.shard_id(), 1), + start_height: Height::new(block_shard.shard_id(), 1), initial_validator_set: block_validator_set, address: validator_address.clone(), threshold_params: Default::default(), - value_payload: ValuePayload::ProposalAndParts, }; let engine = BlockEngine::new(block_store.clone());