MCPcopy Create free account
hub / github.com/ddimaria/rust-blockchain-tutorial / serve

Function serve

chain/src/server.rs:26–86  ·  view source on GitHub ↗

jsonrpsee requires static lifetimes for state

(addr: &str, blockchain: Context)

Source from the content-addressed store, hash-verified

24
25// jsonrpsee requires static lifetimes for state
26pub(crate) async fn serve(addr: &str, blockchain: Context) -> Result<ServerHandle> {
27 if env::var("RUST_LOG").is_err() {
28 env::set_var("RUST_LOG", "info")
29 }
30
31 FmtSubscriber::builder().finish().try_init()?;
32
33 // generate keys if necessary
34 add_keys()?;
35
36 let addrs = addr.parse::<SocketAddr>()?;
37 let server = ServerBuilder::default()
38 .set_logger(Logger)
39 .build(addrs)
40 .await?;
41 let blockchain_for_transaction_processor = blockchain.clone();
42 let mut module = RpcModule::new(blockchain);
43
44 // register methods
45 eth_block_number(&mut module)?;
46 eth_get_block_by_number(&mut module)?;
47 eth_get_balance(&mut module)?;
48 eth_get_balance_by_block(&mut module)?;
49 eth_send_transaction(&mut module)?;
50 eth_send_raw_transaction(&mut module)?;
51 eth_get_transaction_receipt(&mut module)?;
52 eth_get_transaction_count(&mut module)?;
53 eth_get_code(&mut module)?;
54
55 let server_handle = server.start(module)?;
56
57 tracing::info!(
58 "Starting server on {}, with public address {:?}",
59 addrs,
60 *ADDRESS
61 );
62
63 // process transactions in a separate thread
64 let transaction_processor = task::spawn(async move {
65 let mut interval = time::interval(Duration::from_millis(1000));
66
67 loop {
68 interval.tick().await;
69
70 if let Err(error) = blockchain_for_transaction_processor
71 .lock()
72 .await
73 .process_transactions()
74 .await
75 {
76 tracing::error!("Error processing transactions {}", error.to_string());
77 }
78 }
79 });
80
81 transaction_processor
82 .await
83 .map_err(|e| ChainError::InternalError(e.to_string()))?;

Callers 2

mainFunction · 0.85
serverFunction · 0.85

Calls 11

add_keysFunction · 0.85
eth_block_numberFunction · 0.85
eth_get_block_by_numberFunction · 0.85
eth_get_balanceFunction · 0.85
eth_get_balance_by_blockFunction · 0.85
eth_send_transactionFunction · 0.85
eth_send_raw_transactionFunction · 0.85
eth_get_codeFunction · 0.85
process_transactionsMethod · 0.80

Tested by

no test coverage detected