Skip to content

Commit

Permalink
remove package names from protos so that rpcs are compatible between …
Browse files Browse the repository at this point in the history
…hubs and snapchain (#120)

Remove package names so that rpcs are compatible between snapchain and
hubs.
  • Loading branch information
aditiharini authored Dec 5, 2024
1 parent 461c4d1 commit ff99cf1
Show file tree
Hide file tree
Showing 46 changed files with 251 additions and 309 deletions.
4 changes: 2 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// Custom type attributes required for malachite
builder = builder
.type_attribute("snapchain.ShardHash", "#[derive(Eq, PartialOrd, Ord)]")
.type_attribute("snapchain.Height", "#[derive(Copy, Eq, PartialOrd, Ord)]")
.type_attribute("ShardHash", "#[derive(Eq, PartialOrd, Ord)]")
.type_attribute("Height", "#[derive(Copy, Eq, PartialOrd, Ord)]")
// TODO: this generates a lot of code, perhaps choose specific structures
.type_attribute(".", "#[derive(serde::Serialize, serde::Deserialize)]");

Expand Down
3 changes: 2 additions & 1 deletion src/bin/submit_message.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use clap::Parser;
use ed25519_dalek::{SecretKey, SigningKey};
use hex::FromHex;
use snapchain::proto::hub_service_client::HubServiceClient;
use snapchain::utils::cli::compose_message;
use snapchain::{proto::rpc::hub_service_client::HubServiceClient, utils::cli::send_message};
use snapchain::utils::cli::send_message;

#[derive(Parser)]
struct Cli {
Expand Down
2 changes: 1 addition & 1 deletion src/consensus/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::core::types::{
SnapchainValidatorContext,
};
use crate::network::gossip::GossipEvent;
use crate::proto::snapchain::FullProposal;
use crate::proto::FullProposal;
pub use malachite_consensus::Params as ConsensusParams;
pub use malachite_consensus::State as ConsensusState;
use ractor::time::send_after;
Expand Down
6 changes: 3 additions & 3 deletions src/consensus/proposer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::core::types::{
proto, Address, Height, ShardHash, ShardId, SnapchainShard, SnapchainValidator,
};
use crate::proto::rpc::hub_service_client::HubServiceClient;
use crate::proto::rpc::{BlocksRequest, ShardChunksRequest};
use crate::proto::snapchain::{Block, BlockHeader, FullProposal, ShardChunk, ShardHeader};
use crate::proto::hub_service_client::HubServiceClient;
use crate::proto::{Block, BlockHeader, FullProposal, ShardChunk, ShardHeader};
use crate::proto::{BlocksRequest, ShardChunksRequest};
use crate::storage::store::engine::{BlockEngine, ShardEngine, ShardStateChange};
use crate::storage::store::BlockStorageError;
use malachite_common::{Round, Validity};
Expand Down
2 changes: 1 addition & 1 deletion src/consensus/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::core::types::{
Address, Height, ShardHash, SnapchainShard, SnapchainValidator, SnapchainValidatorContext,
SnapchainValidatorSet,
};
use crate::proto::snapchain::FullProposal;
use crate::proto::FullProposal;
use malachite_common::{Round, ValidatorSet};
use malachite_consensus::ProposedValue;
use std::collections::HashSet;
Expand Down
11 changes: 5 additions & 6 deletions src/core/message.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::proto::msg as message;
use crate::proto::msg::MessageType;
use crate::proto::snapchain;
use crate::proto;
use crate::proto::MessageType;

impl message::Message {
pub fn is_type(&self, message_type: message::MessageType) -> bool {
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
}

Expand All @@ -28,7 +27,7 @@ impl message::Message {
}
}

impl snapchain::ValidatorMessage {
impl proto::ValidatorMessage {
pub fn fid(&self) -> u32 {
if let Some(fname) = &self.fname_transfer {
if let Some(proof) = &fname.proof {
Expand Down
6 changes: 3 additions & 3 deletions src/core/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ use std::fmt::{Debug, Display};
use std::sync::Arc;
use tracing::warn;

pub use crate::proto::snapchain as proto; // TODO: reconsider how this is imported
pub use crate::proto; // TODO: reconsider how this is imported

use crate::proto::snapchain::full_proposal::ProposedValue;
use crate::proto::snapchain::{Block, FullProposal, ShardChunk};
use crate::proto::full_proposal::ProposedValue;
use crate::proto::{Block, FullProposal, ShardChunk};
pub use proto::Height;
pub use proto::ShardHash;

Expand Down
32 changes: 1 addition & 31 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,5 @@ pub mod utils;
mod tests;

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

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

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

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

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

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

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

pub mod onchain_event {
tonic::include_proto!("onchain_event");
}
tonic::include_proto!("_");
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use snapchain::network::gossip::GossipEvent;
use snapchain::network::gossip::SnapchainGossip;
use snapchain::network::server::MyHubService;
use snapchain::node::snapchain_node::SnapchainNode;
use snapchain::proto::admin_rpc::admin_service_server::AdminServiceServer;
use snapchain::proto::rpc::hub_service_server::HubServiceServer;
use snapchain::proto::admin_service_server::AdminServiceServer;
use snapchain::proto::hub_service_server::HubServiceServer;
use snapchain::storage::db::RocksDB;
use snapchain::utils::statsd_wrapper::StatsdClientWrapper;

Expand Down
12 changes: 6 additions & 6 deletions src/network/admin_server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::proto::admin_rpc::admin_service_server::AdminService;
use crate::proto::snapchain::ValidatorMessage;
use crate::proto::{admin_rpc, onchain_event::OnChainEvent};
use crate::proto::admin_service_server::AdminService;
use crate::proto::ValidatorMessage;
use crate::proto::{self, OnChainEvent};
use crate::storage::store::engine::{MempoolMessage, Senders};
use rocksdb;
use std::collections::HashMap;
Expand Down Expand Up @@ -91,8 +91,8 @@ impl MyAdminService {
impl AdminService for MyAdminService {
async fn terminate(
&self,
request: Request<admin_rpc::TerminateRequest>,
) -> Result<Response<admin_rpc::TerminateResponse>, Status> {
request: Request<proto::TerminateRequest>,
) -> Result<Response<proto::TerminateResponse>, Status> {
let destroy_database = request.get_ref().destroy_database;

if destroy_database {
Expand All @@ -110,7 +110,7 @@ impl AdminService for MyAdminService {
process::exit(0);
});

let response = Response::new(admin_rpc::TerminateResponse {});
let response = Response::new(proto::TerminateResponse {});
Ok(response)
}

Expand Down
26 changes: 13 additions & 13 deletions src/network/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ impl SnapchainGossip {
})) => {
match proto::GossipMessage::decode(&message.data[..]) {
Ok(gossip_message) => {
match gossip_message.message {
Some(proto::gossip_message::Message::FullProposal(full_proposal)) => {
match gossip_message.gossip_message{
Some(proto::gossip_message::GossipMessage::FullProposal(full_proposal)) => {
let height = full_proposal.height();
debug!("Received block with height {} from peer: {}", height, peer_id);
let consensus_message = ConsensusMsg::ReceivedFullProposal(full_proposal);
Expand All @@ -189,7 +189,7 @@ impl SnapchainGossip {
warn!("Failed to send system block message: {:?}", e);
}
},
Some(proto::gossip_message::Message::Validator(validator)) => {
Some(proto::gossip_message::GossipMessage::Validator(validator)) => {
debug!("Received validator registration from peer: {}", peer_id);
if let Some(validator) = validator.validator {
let public_key = libp2p::identity::ed25519::PublicKey::try_from_bytes(&validator.signer);
Expand All @@ -207,9 +207,9 @@ impl SnapchainGossip {
}
}
},
Some(proto::gossip_message::Message::Consensus(signed_consensus_msg)) => {
match signed_consensus_msg.message {
Some(proto::consensus_message::Message::Vote(vote)) => {
Some(proto::gossip_message::GossipMessage::Consensus(signed_consensus_msg)) => {
match signed_consensus_msg.consensus_message{
Some(proto::consensus_message::ConsensusMessage::Vote(vote)) => {
let vote = Vote::from_proto(vote);
let signed_vote = SignedVote {
message: vote,
Expand All @@ -221,7 +221,7 @@ impl SnapchainGossip {
warn!("Failed to send system vote message: {:?}", e);
}
},
Some(proto::consensus_message::Message::Proposal(proposal)) => {
Some(proto::consensus_message::ConsensusMessage::Proposal(proposal)) => {
let proposal = Proposal::from_proto(proposal);
let signed_proposal = SignedProposal {
message: proposal,
Expand Down Expand Up @@ -251,9 +251,9 @@ impl SnapchainGossip {
Some(GossipEvent::BroadcastSignedVote(vote)) => {
let vote_proto = vote.to_proto();
let gossip_message = proto::GossipMessage {
message: Some(proto::gossip_message::Message::Consensus(proto::ConsensusMessage {
gossip_message: Some(proto::gossip_message::GossipMessage::Consensus(proto::ConsensusMessage {
signature: vote.signature.0,
message: Some(proto::consensus_message::Message::Vote(vote_proto)),
consensus_message: Some(proto::consensus_message::ConsensusMessage::Vote(vote_proto)),
})),
};
let encoded_message = gossip_message.encode_to_vec();
Expand All @@ -262,25 +262,25 @@ impl SnapchainGossip {
Some(GossipEvent::BroadcastSignedProposal(proposal)) => {
let proposal_proto = proposal.to_proto();
let gossip_message = proto::GossipMessage {
message: Some(proto::gossip_message::Message::Consensus(proto::ConsensusMessage {
gossip_message: Some(proto::gossip_message::GossipMessage::Consensus(proto::ConsensusMessage {
signature: proposal.signature.0,
message: Some(proto::consensus_message::Message::Proposal(proposal_proto)),
consensus_message: Some(proto::consensus_message::ConsensusMessage::Proposal(proposal_proto)),
})),
};
let encoded_message = gossip_message.encode_to_vec();
self.publish(encoded_message);
}
Some(GossipEvent::BroadcastFullProposal(full_proposal)) => {
let gossip_message = proto::GossipMessage {
message: Some(proto::gossip_message::Message::FullProposal(full_proposal)),
gossip_message: Some(proto::gossip_message::GossipMessage::FullProposal(full_proposal)),
};
let encoded_message = gossip_message.encode_to_vec();
self.publish(encoded_message);
},
Some(GossipEvent::RegisterValidator(register_validator)) => {
debug!("Broadcasting validator registration");
let gossip_message = proto::GossipMessage {
message: Some(proto::gossip_message::Message::Validator(register_validator)),
gossip_message: Some(proto::gossip_message::GossipMessage::Validator(register_validator)),
};
let encoded_message = gossip_message.encode_to_vec();
self.publish(encoded_message);
Expand Down
14 changes: 7 additions & 7 deletions src/network/server.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::collections::HashMap;

use crate::core::error::HubError;
use crate::proto::hub_event::HubEvent;
use crate::proto::msg as message;
use crate::proto::rpc::hub_service_server::HubService;
use crate::proto::rpc::{BlocksRequest, ShardChunksRequest, ShardChunksResponse, SubscribeRequest};
use crate::proto::snapchain::Block;
use crate::proto;
use crate::proto::hub_service_server::HubService;
use crate::proto::Block;
use crate::proto::HubEvent;
use crate::proto::{BlocksRequest, ShardChunksRequest, ShardChunksResponse, SubscribeRequest};
use crate::storage::db::PageOptions;
use crate::storage::store::engine::{MempoolMessage, Senders};
use crate::storage::store::stores::Stores;
Expand Down Expand Up @@ -49,8 +49,8 @@ impl MyHubService {
impl HubService for MyHubService {
async fn submit_message(
&self,
request: Request<message::Message>,
) -> Result<Response<message::Message>, Status> {
request: Request<proto::Message>,
) -> Result<Response<proto::Message>, Status> {
let start_time = std::time::Instant::now();

let hash = request.get_ref().hash.encode_hex::<String>();
Expand Down
6 changes: 3 additions & 3 deletions src/network/server_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ mod tests {
use std::time::Duration;

use crate::network::server::MyHubService;
use crate::proto::hub_event::{HubEvent, HubEventType};
use crate::proto::rpc::hub_service_server::HubService;
use crate::proto::rpc::SubscribeRequest;
use crate::proto::hub_service_server::HubService;
use crate::proto::SubscribeRequest;
use crate::proto::{HubEvent, HubEventType};
use crate::storage::db::{self, RocksDB, RocksDbTransactionBatch};
use crate::storage::store::engine::Senders;
use crate::storage::store::stores::{StoreLimits, Stores};
Expand Down
2 changes: 1 addition & 1 deletion src/node/snapchain_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::core::types::{
SnapchainValidatorSet,
};
use crate::network::gossip::GossipEvent;
use crate::proto::snapchain::{Block, ShardChunk};
use crate::proto::{Block, ShardChunk};
use crate::storage::db::RocksDB;
use crate::storage::store::engine::{BlockEngine, Senders, ShardEngine};
use crate::storage::store::stores::StoreLimits;
Expand Down
2 changes: 1 addition & 1 deletion src/perf/engine_only_perftest.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::proto::snapchain::{Height, ShardChunk, ShardHeader};
use crate::proto::{Height, ShardChunk, ShardHeader};
use crate::storage::store::engine::{MempoolMessage, ShardStateChange};
use crate::storage::store::stores::StoreLimits;
use crate::storage::store::test_helper;
Expand Down
22 changes: 10 additions & 12 deletions src/perf/perftest.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::proto::onchain_event;
use crate::proto::rpc::hub_service_client::HubServiceClient;
use crate::proto::snapchain::Block;
use crate::proto;
use crate::proto::hub_service_client::HubServiceClient;
use crate::proto::Block;
use crate::storage::store::test_helper;
use crate::utils::cli::send_on_chain_event;
use crate::utils::cli::{compose_message, follow_blocks, send_message};
use crate::utils::factory::events_factory;
use crate::{
consensus::proposer::current_time, proto::admin_rpc::admin_service_client::AdminServiceClient,
consensus::proposer::current_time, proto::admin_service_client::AdminServiceClient,
utils::cli::compose_rent_event,
};
use crate::{proto::msg as message, utils::cli::send_on_chain_event};
use clap::Parser;
use ed25519_dalek::{SecretKey, SigningKey};
use figment::{
Expand Down Expand Up @@ -73,7 +73,7 @@ pub fn load_and_merge_config(config_path: &str) -> Result<Config, Box<dyn Error>
}

fn start_submit_messages(
messages_tx: mpsc::Sender<message::Message>,
messages_tx: mpsc::Sender<proto::Message>,
config: Config,
) -> Vec<tokio::task::JoinHandle<()>> {
let mut submit_message_handles = vec![];
Expand Down Expand Up @@ -115,10 +115,8 @@ fn start_submit_messages(
.await
.unwrap();

let id_register_event = events_factory::create_id_register_event(
FID,
onchain_event::IdRegisterEventType::Register,
);
let id_register_event =
events_factory::create_id_register_event(FID, proto::IdRegisterEventType::Register);

send_on_chain_event(&mut admin_client, id_register_event)
.await
Expand All @@ -127,7 +125,7 @@ fn start_submit_messages(
let signer_event = events_factory::create_signer_event(
FID,
test_helper::default_signer(),
onchain_event::SignerEventType::Add,
proto::SignerEventType::Add,
);

send_on_chain_event(&mut admin_client, signer_event)
Expand Down Expand Up @@ -167,7 +165,7 @@ pub async fn run() -> Result<(), Box<dyn Error>> {
println!("Starting scenario {:#?}", cfg);
let (blocks_tx, mut blocks_rx) = mpsc::channel::<Block>(10_000_000);

let (messages_tx, mut messages_rx) = mpsc::channel::<message::Message>(10_000_000);
let (messages_tx, mut messages_rx) = mpsc::channel::<proto::Message>(10_000_000);
let submit_message_handles = start_submit_messages(messages_tx, cfg.clone());

let follow_blocks_handle = tokio::spawn(async move {
Expand Down
3 changes: 1 addition & 2 deletions src/proto/admin_rpc.proto
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
syntax = "proto3";
package admin_rpc;

import "onchain_event.proto";

Expand All @@ -12,5 +11,5 @@ message TerminateResponse {

service AdminService {
rpc Terminate(TerminateRequest) returns (TerminateResponse);
rpc SubmitOnChainEvent(onchain_event.OnChainEvent) returns (onchain_event.OnChainEvent);
rpc SubmitOnChainEvent(OnChainEvent) returns (OnChainEvent);
}
Loading

0 comments on commit ff99cf1

Please sign in to comment.