Przeglądaj źródła

cargo fmt & clean up

ghassmo 5 lat temu
rodzic
commit
a0928421e5
7 zmienionych plików z 60 dodań i 63 usunięć
  1. 6 3
      src/bin/tx.rs
  2. 1 8
      src/error.rs
  3. 0 18
      src/service/error.rs
  4. 37 23
      src/service/gateway.rs
  5. 0 3
      src/service/mod.rs
  6. 4 0
      src/service/reqrep.rs
  7. 12 8
      src/tx.rs

+ 6 - 3
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<u8>
@@ -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![];

+ 1 - 8
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<ServicesError> for Error {
-    fn from(err: ServicesError) -> Error {
-        Error::ServicesError(err)
-    }
-}
-
 impl From<zeromq::ZmqError> for Error {
     fn from(err: zeromq::ZmqError) -> Error {
         Error::ZMQError(err)

+ 0 - 18
src/service/error.rs

@@ -1,18 +0,0 @@
-use std::fmt;
-
-pub type Result<T> = std::result::Result<T, ServicesError>;
-
-#[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),
-        }
-    }
-}

+ 37 - 23
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<Executor<'_>>) -> Result<()> {
+    pub async fn start(self: Arc<Self>, executor: Arc<Executor<'_>>) -> 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<Self>,
         send_queue: async_channel::Sender<zeromq::ZmqMessage>,
         request: zeromq::ZmqMessage,
     ) -> Result<()> {
@@ -56,12 +56,26 @@ impl GatewayService {
         let request: Vec<u8> = request.to_vec();
         let req: Request = deserialize(&request)?;
 
-        // TODO
-        // do things
-
-        println!("Gateway service received a msg {:?}", req);
+        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"));
+            }
+        }
 
-        let rep = Reply::from(&req, 0, "text".as_bytes().to_vec());
+        let rep = Reply::from(&req, 0, data);
         let rep: Vec<u8> = 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<Vec<u8>> {
-        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<u8>) -> Result<()> {
-        self.request(GatewayCommand::GETSLAB, data).await?;
+        self.request(GatewayCommand::PutSlab, data).await?;
         Ok(())
     }
     pub async fn get_last_index(&mut self) -> Result<u32> {
-        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,
 }

+ 0 - 3
src/service/mod.rs

@@ -1,5 +1,2 @@
-mod error;
 pub mod gateway;
 pub mod reqrep;
-
-pub use error::ServicesError;

+ 4 - 0
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)]

+ 12 - 8
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<TransactionClearInput>,
     pub inputs: Vec<TransactionInput>,
     pub outputs: Vec<TransactionOutput>,
-    pub clear_outputs: Vec<TransactionClearOutput>
+    pub clear_outputs: Vec<TransactionClearOutput>,
 }
 
 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 {
         })
     }
 }
-