Advertisement
theultraman20

server.srs

Jan 1st, 2025
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.58 KB | None | 0 0
  1.     loop {
  2.         let (socket, addr) = listener.accept().await.unwrap();
  3.         println!("New connection from: {addr}");
  4.  
  5.         let mut framed_stream = Framed::new(socket, ServerCodec);
  6.         let new_txs = new_txs.clone();
  7.         let state = state.clone();
  8.         let new_task = tokio::spawn(async move {
  9.  
  10.             while let Some(Ok(frame)) = framed_stream.next().await {
  11.                 match frame {
  12.                     TxFrame(txs) => {
  13.                         println!("New txs received");
  14.                         //todo: verify that txs are valid
  15.                         let mut new_txs = { new_txs.lock().unwrap().clone() };
  16.                         new_txs.extend(txs);
  17.                     },
  18.                     Mined(block) => {
  19.                         let mut state = state.lock().unwrap();
  20.                         if state.add_block_if_valid(block).is_ok() {
  21.                                 println!("New block accepted");
  22.                                 let mut new_txs = new_txs.lock().unwrap();
  23.                                 new_txs.clear();
  24.                                 //new_txs.retain(|item| !block_clone.txs.iter().any(|x| x == item));
  25.                         } else {
  26.                             println!("New block rejected");
  27.                         }
  28.                     },
  29.                     GetNewTxpool => {
  30.                         //println!("Tx pool requested");
  31.                         let new_txs = { new_txs.lock().unwrap().clone() };
  32.                         framed_stream.send(ServerFrame::NewTxPool(new_txs)).await;
  33.                     },
  34.                     GetVersion => {
  35.                         framed_stream.send(ServerFrame::Version(env!("CARGO_PKG_VERSION").to_string())).await;
  36.                     },
  37.                     GetLastHash => {
  38.                         println!("Last hash requested");
  39.                         let last_hash = {
  40.                             //change this later
  41.                             let blocks = state.lock().unwrap().blocks.clone();
  42.                             blocks.last().unwrap().get_hash()
  43.                         };
  44.  
  45.                         framed_stream.send(ServerFrame::LastBlockHash(last_hash)).await;
  46.                     },
  47.                     GetBlockchain => {
  48.                         println!("Blockchain requested");
  49.                         let block_chain = { state.lock().unwrap().blocks.clone() };
  50.                         framed_stream.send(ServerFrame::BlockChain(block_chain)).await;
  51.                     }
  52.  
  53.                 }
  54.             }
  55.  
  56.         });
  57.  
  58.         //match
  59.  
  60.     }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement