From db71798db2ba7ede27f2ad7f2428cffbf8f1e98c Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Tue, 21 Oct 2025 22:23:52 +0800 Subject: feat: peer cmd refactor: Ui now takes network --- src/ui.rs | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) (limited to 'src/ui.rs') diff --git a/src/ui.rs b/src/ui.rs index 697c7f3..a6a1cd6 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,17 +1,22 @@ -use std::sync::Arc; +use std::sync::{Arc, Weak}; -use bitcoin::{blocks::BlockChain, client::Client, network::Request, transaction::Transaction}; +use bitcoin::{ + blocks::BlockChain, + client::Client, + network::{Network, Request}, + transaction::Transaction, +}; use tokio::sync::Mutex; pub struct Ui { - blockchain: Arc>, + network: Weak>, me: Client, } impl Ui { - pub fn new(blockchain: Arc>, me: Client) -> Self { + pub fn new(network: Weak>, me: Client) -> Self { Self { - blockchain, + network, me, } } @@ -34,7 +39,12 @@ impl Ui { .unwrap(), ); return Some(Request::Block( - self.blockchain + self.network + .upgrade() + .unwrap() + .lock() + .await + .get_blockchain() .lock() .await .new_block(trans), @@ -44,11 +54,28 @@ impl Ui { if cmd[0] == "bc" { println!( "{:#?}", - self.blockchain + self.network + .upgrade() + .unwrap() + .lock() + .await + .get_blockchain() .lock() .await ); } + + if cmd[0] == "peer" { + if cmd[1] == "add" { + Network::try_peer( + self.network + .clone(), + cmd[2], + ) + .await; + } + } + None } } -- cgit v1.2.3