ghassmo 5 лет назад
Родитель
Сommit
26d1382f14
6 измененных файлов с 28 добавлено и 27 удалено
  1. 2 2
      src/bin/demowallet.rs
  2. 2 4
      src/error.rs
  3. 2 4
      src/service/error.rs
  4. 14 12
      src/service/gateway.rs
  5. 1 2
      src/service/mod.rs
  6. 7 3
      src/service/reqrep.rs

+ 2 - 2
src/bin/demowallet.rs

@@ -7,8 +7,8 @@ use zeromq::*;
 async fn connect() -> Result<()> {
     let mut requester = zeromq::ReqSocket::new();
     requester.connect("tcp://127.0.0.1:3333").await?;
-    
-    println!("connected") ;
+
+    println!("connected");
 
     for request_nbr in 0..10 {
         println!("start sending");

+ 2 - 4
src/error.rs

@@ -1,8 +1,8 @@
 use std::fmt;
 
 use crate::net::error::NetError;
-use crate::vm::ZKVMError;
 use crate::service::ServicesError;
+use crate::vm::ZKVMError;
 use rusqlite;
 
 pub type Result<T> = std::result::Result<T, Error>;
@@ -45,7 +45,7 @@ pub enum Error {
     Utf8Error,
     NoteDecryptionFailed,
     ServicesError(ServicesError),
-    ZMQError(zeromq::ZmqError)
+    ZMQError(zeromq::ZmqError),
 }
 
 impl std::error::Error for Error {}
@@ -96,7 +96,6 @@ impl fmt::Display for Error {
     }
 }
 
-
 impl From<ServicesError> for Error {
     fn from(err: ServicesError) -> Error {
         Error::ServicesError(err)
@@ -115,7 +114,6 @@ impl From<std::io::Error> for Error {
     }
 }
 
-
 impl From<rusqlite::Error> for Error {
     fn from(err: rusqlite::Error) -> Error {
         Error::RusqliteError(err)

+ 2 - 4
src/service/error.rs

@@ -4,7 +4,7 @@ pub type Result<T> = std::result::Result<T, ServicesError>;
 
 #[derive(Debug, Copy, Clone)]
 pub enum ServicesError {
-   ResonseError(&'static str),
+    ResonseError(&'static str),
 }
 
 impl std::error::Error for ServicesError {}
@@ -12,9 +12,7 @@ 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),
+            ServicesError::ResonseError(ref err) => write!(f, "Response: {}", err),
         }
     }
 }
-
-

+ 14 - 12
src/service/gateway.rs

@@ -13,7 +13,7 @@ use zeromq::*;
 
 pub type Slabs = Vec<Vec<u8>>;
 
-pub struct GatewayService{
+pub struct GatewayService {
     slabs: Slabs,
 }
 
@@ -40,7 +40,7 @@ impl GatewayService {
                 NetEvent::RECEIVE(request) => {
                     ex2.spawn(Self::handle_request(send_queue_s.clone(), request))
                         .detach();
-                    }
+                }
                 NetEvent::SEND(reply) => {
                     worker.send(reply).await?;
                 }
@@ -77,19 +77,20 @@ struct GatewayClient {
 impl GatewayClient {
     pub fn new() -> GatewayClient {
         let sender = zeromq::ReqSocket::new();
-        GatewayClient { slabs: vec![], sender}
+        GatewayClient {
+            slabs: vec![],
+            sender,
+        }
     }
     pub async fn start(&mut self) -> Result<()> {
-
         self.sender.connect("tcp://127.0.0.1:3333").await?;
         Ok(())
-
     }
-    async fn request(&mut self, command: GatewayCommand, data: Vec<u8>) ->  Result<Vec<u8>> {
+    async fn request(&mut self, command: GatewayCommand, data: Vec<u8>) -> Result<Vec<u8>> {
         let request = Request::new(command as u8, data);
         let req = serialize(&request);
         let req = bytes::Bytes::from(req);
-        
+
         self.sender.send(req.into()).await?;
 
         let rep: zeromq::ZmqMessage = self.sender.recv().await?;
@@ -97,9 +98,9 @@ impl GatewayClient {
         let rep: Vec<u8> = rep.to_vec();
 
         let reply: Reply = deserialize(&rep)?;
-        
+
         if reply.has_error() {
-            return Err(ServicesError::ResonseError("response has an error").into()); 
+            return Err(ServicesError::ResonseError("response has an error").into());
         }
 
         assert!(reply.get_id() == request.get_id());
@@ -108,14 +109,15 @@ impl GatewayClient {
     }
 
     pub async fn get_slab(&mut self, index: u32) -> Result<Vec<u8>> {
-        self.request(GatewayCommand::GETSLAB, index.to_be_bytes().to_vec()).await
+        self.request(GatewayCommand::GETSLAB, index.to_be_bytes().to_vec())
+            .await
     }
 
-    pub async fn put_slab(&mut self, data: Vec<u8>) -> Result<()>{
+    pub async fn put_slab(&mut self, data: Vec<u8>) -> Result<()> {
         self.request(GatewayCommand::GETSLAB, data).await?;
         Ok(())
     }
-    pub async fn get_last_index(&mut self) -> Result<u32>{
+    pub async fn get_last_index(&mut self) -> Result<u32> {
         let rep = self.request(GatewayCommand::GETLASTINDEX, vec![]).await?;
         let rep: [u8; 4] = rep.try_into().unwrap();
         Ok(u32::from_be_bytes(rep))

+ 1 - 2
src/service/mod.rs

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

+ 7 - 3
src/service/reqrep.rs

@@ -86,10 +86,14 @@ impl Reply {
             payload,
         }
     }
-    
+
     pub fn has_error(&self) -> bool {
-        if self.error == 0 { false } else { true } 
-    } 
+        if self.error == 0 {
+            false
+        } else {
+            true
+        }
+    }
 
     pub fn get_payload(&self) -> Vec<u8> {
         self.payload.clone()