Skip to content

Commit

Permalink
Create node.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 11, 2024
1 parent 69b742e commit 2ec09c8
Showing 1 changed file with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// node.rs
use tokio::prelude::*;
use tokio::runtime::Builder;
use tokio::sync::mpsc;

struct Node {
id: u64,
network: Vec<Node>,
tx_pool: Vec<Transaction>,
}

impl Node {
async fn start(self) {
// Start the node's event loop
let mut runtime = Builder::new_current_thread()
.enable_all()
.build()
.unwrap();

runtime.block_on(self.run());
}

async fn run(self) {
// Handle incoming transactions and blocks
loop {
// Receive transactions from the network
let tx = self.network.recv().await.unwrap();
self.tx_pool.push(tx);

// Process transactions and create new blocks
// ...
}
}
}

struct Transaction {
from: Address,
to: Address,
amount: u64,
}

// Start the node network
fn main() {
let mut nodes = vec![];

for i in 0..10 {
let node = Node {
id: i,
network: nodes.clone(),
tx_pool: vec![],
};
nodes.push(node);
}

for node in nodes {
node.start();
}
}

0 comments on commit 2ec09c8

Please sign in to comment.