stratum: test new tokio

This commit is contained in:
ardocrat 2023-07-05 23:36:33 +03:00
parent 82651a9137
commit 31addbc157

View file

@ -331,6 +331,7 @@ impl Handler {
// Build and return a JobTemplate for mining the current block // Build and return a JobTemplate for mining the current block
fn build_block_template(&self) -> JobTemplate { fn build_block_template(&self) -> JobTemplate {
println!("1 build template 12345");
let bh = self let bh = self
.current_state .current_state
.read() .read()
@ -339,6 +340,7 @@ impl Handler {
.unwrap() .unwrap()
.header .header
.clone(); .clone();
println!("2 build template 12345");
// Serialize the block header into pre and post nonce strings // Serialize the block header into pre and post nonce strings
let mut header_buf = vec![]; let mut header_buf = vec![];
{ {
@ -510,7 +512,7 @@ impl Handler {
} // handle submit a solution } // handle submit a solution
fn broadcast_job(&self) { fn broadcast_job(&self) {
debug!("broadcast job"); println!("12345 broadcast job");
// Package new block into RpcRequest // Package new block into RpcRequest
let job_template = self.build_block_template(); let job_template = self.build_block_template();
let job_template_json = serde_json::to_string(&job_template).unwrap(); let job_template_json = serde_json::to_string(&job_template).unwrap();
@ -536,8 +538,10 @@ impl Handler {
let mut head = self.chain.head().unwrap(); let mut head = self.chain.head().unwrap();
let mut current_hash = head.prev_block_h; let mut current_hash = head.prev_block_h;
loop { loop {
println!("12345 looping");
// Ping stratum socket on stop to handle TcpListener unbind. // Ping stratum socket on stop to handle TcpListener unbind.
if stop_state.is_stopped() { if stop_state.is_stopped() {
println!("12345 prepare ping to stop");
let listen_addr: SocketAddr = config let listen_addr: SocketAddr = config
.stratum_server_addr .stratum_server_addr
.clone() .clone()
@ -545,10 +549,14 @@ impl Handler {
.parse() .parse()
.expect("Stratum: Incorrect address "); .expect("Stratum: Incorrect address ");
thread::spawn(move || { thread::spawn(move || {
println!("12345 ping start");
let _ = TcpStream::connect(listen_addr).unwrap(); let _ = TcpStream::connect(listen_addr).unwrap();
println!("12345 ping end");
}); });
break; break;
} }
println!("12345 looping2");
// get the latest chain state // get the latest chain state
head = self.chain.head().unwrap(); head = self.chain.head().unwrap();
@ -561,8 +569,9 @@ impl Handler {
&& self.workers.count() > 0 && self.workers.count() > 0
{ {
{ {
debug!("resend updated block"); println!("12345 resend updated block");
let mut state = self.current_state.write(); let mut state = self.current_state.write();
println!("12345 after resend updated block");
let wallet_listener_url = if !config.burn_reward { let wallet_listener_url = if !config.burn_reward {
Some(config.wallet_listener_url.clone()) Some(config.wallet_listener_url.clone())
} else { } else {
@ -572,12 +581,14 @@ impl Handler {
let clear_blocks = current_hash != latest_hash; let clear_blocks = current_hash != latest_hash;
// Build the new block (version) // Build the new block (version)
println!("12345 get_block");
let (new_block, block_fees) = mine_block::get_block( let (new_block, block_fees) = mine_block::get_block(
&self.chain, &self.chain,
tx_pool, tx_pool,
state.current_key_id.clone(), state.current_key_id.clone(),
wallet_listener_url, wallet_listener_url,
); );
println!("12345 after get_block");
// scaled difficulty // scaled difficulty
state.current_difficulty = state.current_difficulty =
@ -598,14 +609,20 @@ impl Handler {
} }
// Update the mining stats // Update the mining stats
println!("12345 update_block_height");
self.workers.update_block_height(new_block.header.height); self.workers.update_block_height(new_block.header.height);
println!("12345 after update_block_height");
let difficulty = new_block.header.total_difficulty() - head.total_difficulty; let difficulty = new_block.header.total_difficulty() - head.total_difficulty;
println!("12345 update_network_difficulty");
self.workers.update_network_difficulty(difficulty.to_num()); self.workers.update_network_difficulty(difficulty.to_num());
println!("12345 after update_network_difficulty");
self.workers.update_network_hashrate(); self.workers.update_network_hashrate();
println!("12345 after update_network_hashrate");
// Add this new block candidate onto our list of block versions for this height // Add this new block candidate onto our list of block versions for this height
state.current_block_versions.push(new_block); state.current_block_versions.push(new_block);
} }
println!("12345 resend updated block exit");
// Send this job to all connected workers // Send this job to all connected workers
self.broadcast_job(); self.broadcast_job();
} }
@ -626,64 +643,82 @@ fn accept_connections(listen_addr: SocketAddr, handler: Arc<Handler>, stop_state
panic!("Stratum: Failed to bind to listen address {}", listen_addr) panic!("Stratum: Failed to bind to listen address {}", listen_addr)
}); });
let state_socket = &stop_state.clone(); let state_socket = &stop_state.clone();
let server = listener println!("12345 bind complete");
.incoming() loop {
.filter_map(|s| async { s.map_err(|e| error!("accept error = {:?}", e)).ok() }) println!("12345 socket starting");
.for_each(move |socket| { let (socket, _) = listener.accept().await.unwrap();
let handler = handler.clone(); // Stop listener on node server stop.
async move { {
// Stop listener on node server stop. if state_socket.is_stopped() {
if state_socket.is_stopped() { panic_any("Stopped");
panic_any("Stopped");
}
// Spawn a task to process the connection
let (tx, mut rx) = mpsc::unbounded();
let worker_id = handler.workers.add_worker(tx);
info!("Worker {} connected", worker_id);
let framed = Framed::new(socket, LinesCodec::new());
let (mut writer, mut reader) = framed.split();
let h = handler.clone();
let read = async move {
while let Some(line) = reader
.try_next()
.await
.map_err(|e| error!("error reading line: {}", e))?
{
let request = serde_json::from_str(&line)
.map_err(|e| error!("error serializing line: {}", e))?;
let resp = h.handle_rpc_requests(request, worker_id);
h.workers.send_to(worker_id, resp);
}
Result::<_, ()>::Ok(())
};
let write = async move {
while let Some(line) = rx.next().await {
writer
.send(line)
.await
.map_err(|e| error!("error writing line: {}", e))?;
}
Result::<_, ()>::Ok(())
};
let task = async move {
pin_mut!(read, write);
futures::future::select(read, write).await;
handler.workers.remove_worker(worker_id);
info!("Worker {} disconnected", worker_id);
};
tokio::spawn(task);
} }
}); }
server.await
println!("12345 socket");
let handler = handler.clone();
let process = || async move {
// Spawn a task to process the connection
let (tx, mut rx) = mpsc::unbounded();
let worker_id = handler.workers.add_worker(tx);
info!("Worker {} connected", worker_id);
let framed = Framed::new(socket, LinesCodec::new());
let (mut writer, mut reader) = framed.split();
let h = handler.clone();
let read = async move {
println!("12345 r: 1");
while let Some(line) = reader
.try_next()
.await
.map_err(|e| error!("error reading line: {}", e))?
{
println!("12345 r: 2: {}", line);
let request = serde_json::from_str(&line)
.map_err(|e| println!("error serializing line: {}", e))?;
let resp = h.handle_rpc_requests(request, worker_id);
h.workers.send_to(worker_id, resp);
}
println!("12345 r: 3");
Result::<_, ()>::Ok(())
};
let write = async move {
while let Some(line) = rx.next().await {
println!("12345 w: 1: {}", line);
writer
.send(line)
.await
.map_err(|e| println!("error writing line: {}", e))?;
}
println!("12345 w: 2");
Result::<_, ()>::Ok(())
};
let task = async move {
println!("12345 t: 1");
pin_mut!(read, write);
println!("12345 t: 2");
futures::future::select(read, write).await;
println!("12345 t: 3");
handler.workers.remove_worker(worker_id);
println!("12345 t: 4");
info!("Worker {} disconnected", worker_id);
};
tokio::spawn(task);
Result::<_, ()>::Ok(())
};
println!("12345 run process");
let _ = (process)().await;
println!("12345 after process");
}
}; };
let mut rt = Runtime::new().unwrap(); let rt = Runtime::new().unwrap();
rt.block_on(task); rt.block_on(task);
} }