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

feat(abci): ABCI shim for running against a standalone ABCI app #463

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
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
161 changes: 161 additions & 0 deletions code/Cargo.lock

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

14 changes: 13 additions & 1 deletion code/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ members = [
"crates/test",
"crates/test/mbt",
"crates/vote",
"crates/starknet/*"
"crates/abci/*",
"crates/starknet/*",
]

[workspace.package]
Expand All @@ -40,6 +41,7 @@ debug = true
unused_crate_dependencies = "warn"

[workspace.dependencies]
malachite-abci = { version = "0.1.0", path = "crates/abci" }
malachite-actors = { version = "0.1.0", path = "crates/actors" }
malachite-cli = { version = "0.1.0", path = "crates/cli" }
malachite-common = { version = "0.1.0", path = "crates/common" }
Expand All @@ -56,6 +58,12 @@ malachite-round = { version = "0.1.0", path = "crates/round" }
malachite-test = { version = "0.1.0", path = "crates/test" }
malachite-vote = { version = "0.1.0", path = "crates/vote" }

# ABCI
malachite-abci-host = { version = "0.1.0", path = "crates/abci/host" }
malachite-abci-app = { version = "0.1.0", path = "crates/abci/app" }
malachite-abci-p2p-proto = { version = "0.1.0", path = "crates/abci/p2p-proto" }
malachite-abci-p2p-types = { version = "0.1.0", path = "crates/abci/p2p-types" }

# Starknet
malachite-starknet-host = { version = "0.1.0", path = "crates/starknet/host" }
malachite-starknet-app = { version = "0.1.0", path = "crates/starknet/app" }
Expand Down Expand Up @@ -114,7 +122,11 @@ thiserror = "1.0"
time = "0.3"
tokio = "1.40.0"
tokio-stream = "0.1"
tokio-util = "0.7"
tendermint = "0.39.1"
tendermint-proto = "0.39.1"
toml = "0.8.19"
tower-abci = "0.15.0"
tikv-jemallocator = "0.6.0"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
35 changes: 35 additions & 0 deletions code/crates/abci/app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[package]
name = "malachite-abci-app"
version.workspace = true
edition.workspace = true
repository.workspace = true
license.workspace = true
publish.workspace = true
rust-version.workspace = true

[dependencies]
malachite-actors.workspace = true
malachite-common.workspace = true
malachite-consensus.workspace = true
malachite-gossip-consensus.workspace = true
malachite-gossip-mempool.workspace = true
malachite-metrics.workspace = true
malachite-node.workspace = true
malachite-proto.workspace = true
malachite-abci-host.workspace = true
malachite-abci-p2p-proto.workspace = true
malachite-abci-p2p-types.workspace = true
malachite-test.workspace = true

async-trait.workspace = true
bytesize.workspace = true
libp2p-identity.workspace = true
prost.workspace = true
rand.workspace = true
serde_json.workspace = true
tokio.workspace = true
tracing-subscriber.workspace = true
tracing.workspace = true

# [lints]
# workspace = true
87 changes: 87 additions & 0 deletions code/crates/abci/app/src/codec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use malachite_gossip_consensus::Bytes;
use prost::Message;

use malachite_abci_host::context::AbciContext;
use malachite_abci_host::types::Vote;
use malachite_abci_p2p_proto::consensus_message::Messages;
use malachite_abci_p2p_proto::ConsensusMessage;
use malachite_abci_p2p_types as p2p;
use malachite_actors::util::codec::NetworkCodec;
use malachite_actors::util::streaming::{StreamContent, StreamMessage};
use malachite_common::{SignedProposal, SignedVote};
use malachite_consensus::SignedConsensusMsg;
use malachite_proto::{Error as ProtoError, Protobuf};

pub struct ProtobufCodec;

impl NetworkCodec<AbciContext> for ProtobufCodec {
type Error = ProtoError;

fn decode_msg(bytes: Bytes) -> Result<SignedConsensusMsg<AbciContext>, Self::Error> {
let proto = ConsensusMessage::decode(bytes)?;

let proto_signature = proto
.signature
.ok_or_else(|| ProtoError::missing_field::<ConsensusMessage>("signature"))?;

let message = proto
.messages
.ok_or_else(|| ProtoError::missing_field::<ConsensusMessage>("messages"))?;

let signature = p2p::Signature::from_proto(proto_signature)?;

match message {
Messages::Vote(v) => {
Vote::from_proto(v).map(|v| SignedConsensusMsg::Vote(SignedVote::new(v, signature)))
}
Messages::Proposal(p) => p2p::Proposal::from_proto(p)
.map(|p| SignedConsensusMsg::Proposal(SignedProposal::new(p, signature))),
}
}

fn encode_msg(msg: SignedConsensusMsg<AbciContext>) -> Result<Bytes, Self::Error> {
let message = match msg {
SignedConsensusMsg::Vote(v) => ConsensusMessage {
messages: Some(Messages::Vote(v.to_proto()?)),
signature: Some(v.signature.to_proto()?),
},
SignedConsensusMsg::Proposal(p) => ConsensusMessage {
messages: Some(Messages::Proposal(p.to_proto()?)),
signature: Some(p.signature.to_proto()?),
},
};

Ok(Bytes::from(prost::Message::encode_to_vec(&message)))
}

fn decode_stream_msg<T>(bytes: Bytes) -> Result<StreamMessage<T>, Self::Error>
where
T: Protobuf,
{
let p2p_msg = p2p::StreamMessage::from_bytes(&bytes)?;
Ok(StreamMessage {
stream_id: p2p_msg.id,
sequence: p2p_msg.sequence,
content: match p2p_msg.content {
p2p::StreamContent::Data(data) => StreamContent::Data(T::from_bytes(&data)?),
p2p::StreamContent::Fin(fin) => StreamContent::Fin(fin),
},
})
}

fn encode_stream_msg<T>(msg: StreamMessage<T>) -> Result<Bytes, Self::Error>
where
T: Protobuf,
{
let p2p_msg = p2p::StreamMessage {
id: msg.stream_id,
sequence: msg.sequence,
content: match msg.content {
StreamContent::Data(data) => p2p::StreamContent::Data(data.to_bytes()?),
StreamContent::Fin(fin) => p2p::StreamContent::Fin(fin),
},
};

p2p_msg.to_bytes()
}
}
7 changes: 7 additions & 0 deletions code/crates/abci/app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// For coverage on nightly
#![allow(unexpected_cfgs)]
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]

pub mod codec;
pub mod node;
pub mod spawn;
Loading
Loading