diff --git a/src/bin/tx.rs b/src/bin/tx.rs index aa1465e07..89e5a9f46 100644 --- a/src/bin/tx.rs +++ b/src/bin/tx.rs @@ -54,7 +54,7 @@ fn main() { }], inputs: vec![], outputs: vec![tx::TransactionBuilderOutputInfo { value: 110, public }], - clear_outputs: vec![] + clear_outputs: vec![], }; // We will 'compile' the tx, and then serialize it to this Vec @@ -162,8 +162,11 @@ fn main() { }], // We can add more outputs to this list. // The only constraint is that sum(value in) == sum(value out) - outputs: vec![tx::TransactionBuilderOutputInfo { value: 110, public: public2 }], - clear_outputs: vec![] + outputs: vec![tx::TransactionBuilderOutputInfo { + value: 110, + public: public2, + }], + clear_outputs: vec![], }; // Build the tx let mut tx_data = vec![]; diff --git a/src/error.rs b/src/error.rs index ef97f9326..c615e88d2 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,7 +1,6 @@ use std::fmt; use crate::net::error::NetError; -use crate::service::ServicesError; use crate::vm::ZKVMError; use rusqlite; @@ -44,7 +43,7 @@ pub enum Error { ServiceStopped, Utf8Error, NoteDecryptionFailed, - ServicesError(ServicesError), + ServicesError(&'static str), ZMQError(zeromq::ZmqError), } @@ -96,12 +95,6 @@ impl fmt::Display for Error { } } -impl From for Error { - fn from(err: ServicesError) -> Error { - Error::ServicesError(err) - } -} - impl From for Error { fn from(err: zeromq::ZmqError) -> Error { Error::ZMQError(err) diff --git a/src/service/error.rs b/src/service/error.rs deleted file mode 100644 index 99255e76d..000000000 --- a/src/service/error.rs +++ /dev/null @@ -1,18 +0,0 @@ -use std::fmt; - -pub type Result = std::result::Result; - -#[derive(Debug, Copy, Clone)] -pub enum ServicesError { - ResonseError(&'static str), -} - -impl std::error::Error for ServicesError {} - -impl fmt::Display for ServicesError { - fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result { - match *self { - ServicesError::ResonseError(ref err) => write!(f, "Response: {}", err), - } - } -} diff --git a/src/service/gateway.rs b/src/service/gateway.rs index f19536fec..e394d701e 100644 --- a/src/service/gateway.rs +++ b/src/service/gateway.rs @@ -1,9 +1,8 @@ use std::convert::TryInto; use super::reqrep::{Reply, Request}; -use super::ServicesError; use crate::serial::{deserialize, serialize}; -use crate::Result; +use crate::{Error, Result}; use async_executor::Executor; use async_std::sync::Arc; @@ -18,12 +17,12 @@ pub struct GatewayService { } enum NetEvent { - RECEIVE(zeromq::ZmqMessage), - SEND(zeromq::ZmqMessage), + Receive(zeromq::ZmqMessage), + Send(zeromq::ZmqMessage), } impl GatewayService { - pub async fn start(executor: Arc>) -> Result<()> { + pub async fn start(self: Arc, executor: Arc>) -> Result<()> { let mut worker = zeromq::RepSocket::new(); worker.connect("tcp://127.0.0.1:4444").await?; @@ -32,16 +31,16 @@ impl GatewayService { let ex2 = executor.clone(); loop { let event = futures::select! { - request = worker.recv().fuse() => NetEvent::RECEIVE(request?), - reply = send_queue_r.recv().fuse() => NetEvent::SEND(reply?) + request = worker.recv().fuse() => NetEvent::Receive(request?), + reply = send_queue_r.recv().fuse() => NetEvent::Send(reply?) }; + let self2 = self.clone(); match event { - NetEvent::RECEIVE(request) => { - ex2.spawn(Self::handle_request(send_queue_s.clone(), request)) - .detach(); + NetEvent::Receive(request) => { + let _ = ex2.spawn(self2.clone().handle_request(send_queue_s.clone(), request)); } - NetEvent::SEND(reply) => { + NetEvent::Send(reply) => { worker.send(reply).await?; } } @@ -49,6 +48,7 @@ impl GatewayService { } async fn handle_request( + self: Arc, send_queue: async_channel::Sender, request: zeromq::ZmqMessage, ) -> Result<()> { @@ -56,12 +56,26 @@ impl GatewayService { let request: Vec = request.to_vec(); let req: Request = deserialize(&request)?; - // TODO - // do things + let data = vec![]; + match req.get_command() { + 0 => { + // PUTSLAB + println!("receive PUTSLAB command"); + } + 1 => { + // GETSLAB + println!("receive GETSLAB command"); + } + 2 => { + // GETLASTINDEX + println!("receive GETLASTINDEX command"); + } + _ => { + return Err(Error::ServicesError("wrong command")); + } + } - println!("Gateway service received a msg {:?}", req); - - let rep = Reply::from(&req, 0, "text".as_bytes().to_vec()); + let rep = Reply::from(&req, 0, data); let rep: Vec = serialize(&rep); let rep = Bytes::from(rep); send_queue.send(rep.into()).await?; @@ -100,7 +114,7 @@ impl GatewayClient { let reply: Reply = deserialize(&rep)?; if reply.has_error() { - return Err(ServicesError::ResonseError("response has an error").into()); + return Err(crate::Error::ServicesError("response has an error")); } assert!(reply.get_id() == request.get_id()); @@ -109,16 +123,16 @@ impl GatewayClient { } pub async fn get_slab(&mut self, index: u32) -> Result> { - self.request(GatewayCommand::GETSLAB, index.to_be_bytes().to_vec()) + self.request(GatewayCommand::GetSlab, index.to_be_bytes().to_vec()) .await } pub async fn put_slab(&mut self, data: Vec) -> Result<()> { - self.request(GatewayCommand::GETSLAB, data).await?; + self.request(GatewayCommand::PutSlab, data).await?; Ok(()) } pub async fn get_last_index(&mut self) -> Result { - let rep = self.request(GatewayCommand::GETLASTINDEX, vec![]).await?; + let rep = self.request(GatewayCommand::GetLastIndex, vec![]).await?; let rep: [u8; 4] = rep.try_into().unwrap(); Ok(u32::from_be_bytes(rep)) } @@ -126,7 +140,7 @@ impl GatewayClient { #[repr(u8)] enum GatewayCommand { - PUTSLAB, - GETSLAB, - GETLASTINDEX, + PutSlab, + GetSlab, + GetLastIndex, } diff --git a/src/service/mod.rs b/src/service/mod.rs index 05bd8d00c..94f34055d 100644 --- a/src/service/mod.rs +++ b/src/service/mod.rs @@ -1,5 +1,2 @@ -mod error; pub mod gateway; pub mod reqrep; - -pub use error::ServicesError; diff --git a/src/service/reqrep.rs b/src/service/reqrep.rs index 0b5f80be8..ed5306aee 100644 --- a/src/service/reqrep.rs +++ b/src/service/reqrep.rs @@ -69,6 +69,10 @@ impl Request { pub fn get_id(&self) -> u32 { self.id } + + pub fn get_command(&self) -> u8 { + self.command + } } #[derive(Debug, PartialEq)] diff --git a/src/tx.rs b/src/tx.rs index fa536fd0e..afbe1c653 100644 --- a/src/tx.rs +++ b/src/tx.rs @@ -151,7 +151,12 @@ impl TransactionBuilder { for (i, output) in self.clear_outputs.into_iter().enumerate() { let valcom_blind = if self.outputs.len() + i == last_output_index { - Self::compute_remainder_blind(&clear_inputs, &input_blinds, &output_blinds, &clear_outputs) + Self::compute_remainder_blind( + &clear_inputs, + &input_blinds, + &output_blinds, + &clear_outputs, + ) } else { jubjub::Fr::random(&mut OsRng) }; @@ -159,7 +164,7 @@ impl TransactionBuilder { let output = TransactionClearOutput { value: output.value, valcom_blind, - instructions: output.instructions + instructions: output.instructions, }; clear_outputs.push(output); } @@ -168,7 +173,7 @@ impl TransactionBuilder { clear_inputs, inputs, outputs, - clear_outputs + clear_outputs, }; let mut unsigned_tx_data = vec![]; @@ -199,14 +204,14 @@ impl TransactionBuilder { clear_inputs, inputs, outputs: partial_tx.outputs, - clear_outputs: partial_tx.clear_outputs + clear_outputs: partial_tx.clear_outputs, } } } pub struct TransactionBuilderClearOutputInfo { pub value: u64, - pub instructions: String + pub instructions: String, } pub struct TransactionBuilderClearInputInfo { @@ -260,7 +265,7 @@ pub struct Transaction { pub clear_inputs: Vec, pub inputs: Vec, pub outputs: Vec, - pub clear_outputs: Vec + pub clear_outputs: Vec, } impl Encodable for Transaction { @@ -553,7 +558,7 @@ impl Decodable for TransactionOutput { pub struct TransactionClearOutput { pub value: u64, pub valcom_blind: jubjub::Fr, - pub instructions: String + pub instructions: String, } impl_vec!(TransactionClearOutput); @@ -577,4 +582,3 @@ impl Decodable for TransactionClearOutput { }) } } -