Skip to content

Commit

Permalink
Update protobuf and minor fixes for DMC
Browse files Browse the repository at this point in the history
  • Loading branch information
wafflespeanut committed Nov 24, 2022
1 parent 928e4fd commit afff091
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 87 deletions.
13 changes: 7 additions & 6 deletions ain-grpc/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const TYPE_ATTRS: &[Attr] = &[
matcher: ".*",
attr: Some("#[derive(Serialize, Deserialize)] #[serde(rename_all=\"camelCase\")]"),
rename: None,
skip: &["^(Meta)?BlockResult", "NonUtxo", "^Transaction"],
skip: &["^BlockResult", "NonUtxo", "^Transaction"],
},
Attr {
matcher: ".*",
Expand All @@ -92,13 +92,13 @@ const FIELD_ATTRS: &[Attr] = &[
matcher: ":::prost::alloc::string::String",
attr: Some("#[serde(skip_serializing_if = \"String::is_empty\")]"),
rename: None,
skip: &["^(Meta)?BlockResult", "NonUtxo", "^Transaction"],
skip: &["^BlockResult", "NonUtxo", "^Transaction"],
},
Attr {
matcher: ":::prost::alloc::vec::Vec",
attr: Some("#[serde(skip_serializing_if = \"Vec::is_empty\")]"),
rename: None,
skip: &["MetaBlockResult"],
skip: &[],
},
Attr {
matcher: "req_sigs",
Expand Down Expand Up @@ -182,7 +182,7 @@ impl ServiceGenerator for WrappedGenerator {
let re = Regex::new("\\[rpc: (.*?)\\]").unwrap();
for method in &service.methods {
let mut ref_map = self.methods.borrow_mut();
let vec = ref_map.entry(service.name.clone()).or_insert(vec![]);
let vec = ref_map.entry(service.name.clone()).or_default();
let mut rpc = Rpc {
name: method.proto_name.clone(),
input_ty: method.input_proto_type.clone(),
Expand Down Expand Up @@ -303,7 +303,7 @@ fn modify_codegen(
Span::call_site(),
),
Ident::new(
&format!("{}Server", server_mod).to_snek_case(),
&format!("{server_mod}Server").to_snek_case(),
Span::call_site(),
),
Ident::new(&server_mod.to_pascal_case(), Span::call_site()),
Expand Down Expand Up @@ -455,6 +455,7 @@ fn apply_substitutions(
#[allow(non_snake_case)]
fn NewClient(addr: &str) -> Result<Box<Client>, Box<dyn std::error::Error>> {
if CLIENTS.read().unwrap().get(addr).is_none() {
log::info!("Initializing RPC client for {}", addr);
let c = Client {
inner: Arc::new(HttpClientBuilder::default().build(addr)?),
handle: RUNTIME.rt_handle.clone(),
Expand All @@ -480,7 +481,7 @@ fn apply_substitutions(
#[allow(dead_code)]
fn missing_param(field: &str) -> jsonrpsee_core::Error {
jsonrpsee_core::Error::Call(jsonrpsee_types::error::CallError::Custom(
jsonrpsee_types::ErrorObject::borrowed(-1, &format!("Missing required parameter '{}'", field), None).into_owned()
jsonrpsee_types::ErrorObject::borrowed(-1, &format!("Missing required parameter '{field}'"), None).into_owned()
))
}
};
Expand Down
55 changes: 1 addition & 54 deletions ain-grpc/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,48 +52,6 @@ impl<'de> Deserialize<'de> for types::BlockResult {
}
}

impl Serialize for types::MetaBlockResult {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
if !self.hash.is_empty() {
return serializer.serialize_str(&self.hash);
}

if !self.block.is_empty() {
return self.block.serialize(serializer);
}

serializer.serialize_str("")
}
}

impl<'de> Deserialize<'de> for types::MetaBlockResult {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum Res {
Hash(String),
Block(Vec<u8>),
}

match Res::deserialize(deserializer)? {
Res::Hash(s) => Ok(types::MetaBlockResult {
hash: s,
block: vec![],
}),
Res::Block(b) => Ok(types::MetaBlockResult {
hash: "".into(),
block: b,
}),
}
}
}

impl Serialize for types::Transaction {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down Expand Up @@ -135,7 +93,7 @@ impl<'de> Deserialize<'de> for types::Transaction {

#[cfg(test)]
mod tests {
use super::types::{BlockResult, MetaBlockResult, Transaction};
use super::types::{BlockResult, Transaction};

#[test]
fn test_block_result_json() {
Expand All @@ -148,17 +106,6 @@ mod tests {
assert_eq!(serde_json::to_value(&foo2).unwrap(), "foobar");
}

#[test]
fn test_meta_block_result_json() {
let foo = MetaBlockResult {
hash: "foobar".into(),
block: vec![],
};
let res = serde_json::to_value(&foo).unwrap();
let foo2: MetaBlockResult = serde_json::from_value(res).unwrap();
assert_eq!(serde_json::to_value(&foo2).unwrap(), "foobar");
}

#[test]
fn test_transaction() {
let foo = Transaction {
Expand Down
12 changes: 4 additions & 8 deletions protobuf/rpc/dmc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ package rpc;

import "types/dmc.proto";

service Metachain {
// [rpc: metaConsensusRpc_getBlockHash] [client]
rpc GetBlockHash(types.MetaBlockInput) returns (types.MetaBlockResult);

// [rpc: metaConsensusRpc_getBlock] [client]
rpc GetBlock(types.MetaBlockInput) returns (types.MetaBlockResult);
// NOTE: The definitions here must match those in metachain

service Metachain {
// [rpc: metaConsensusRpc_mintBlock] [client]
rpc MintBlock(types.MetaMintInput) returns (types.MetaMintResult);
rpc MetaMintBlock(types.MetaBlockInput) returns (types.MetaBlockResult);

// [rpc: metaConsensusRpc_connectBlock] [client]
rpc ConnectBlock(types.MetaConnectBlockInput) returns (types.MetaConnectBlockResult);
rpc MetaConnectBlock(types.MetaConnectBlockInput) returns (types.MetaConnectBlockResult);
}
25 changes: 6 additions & 19 deletions protobuf/types/dmc.proto
Original file line number Diff line number Diff line change
@@ -1,39 +1,26 @@
syntax = "proto3";
package types;

// NOTE: The definitions here must match those in metachain

message MetaTransaction {
string from = 1;
string to = 2;
int64 amount = 3;
string signature = 4;
}

message MetaBlockInput {
int64 at = 1; // [default: -1]
repeated MetaTransaction txs = 2;
}

message MetaBlockResult {
string hash = 1;
bytes block = 2;
}

message MetaMintInput {
repeated MetaTransaction dnc_txs = 1;
uint64 secret = 2; // secret obtained from handshake
}

message MetaMintResult {
bytes block = 1;
repeated MetaTransaction dmc_txs = 2;
bytes payload = 1;
}

message MetaConnectBlockInput {
bytes dmc_payload = 1;
repeated MetaTransaction dnc_txs = 2;
uint64 secret = 3; // secret obtained from handshake
bytes payload = 1;
}

message MetaConnectBlockResult {
bool imported = 1;
repeated MetaTransaction dmc_txs = 2;
string hash = 1;
}

0 comments on commit afff091

Please sign in to comment.