From ba48f9bae7eba055172a5a097dc0a7d3852e71b8 Mon Sep 17 00:00:00 2001 From: Joseph Knecht <83087510+JosephKnecht-lab@users.noreply.github.com> Date: Mon, 1 Jul 2024 12:08:17 +0200 Subject: [PATCH] add other params --- jam/src/blockchain.rs | 12 +++++++----- jam/src/config.rs | 8 +++++++- jam/src/main.rs | 4 ++-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/jam/src/blockchain.rs b/jam/src/blockchain.rs index 41eaca6..bc433c9 100644 --- a/jam/src/blockchain.rs +++ b/jam/src/blockchain.rs @@ -1,20 +1,21 @@ use crate::block::Block; use crate::transaction::Transaction; use serde_json::json; -use chrono::Utc; pub struct Blockchain { pub chain: Vec, pub pending_transactions: Vec, pub difficulty: usize, + pub block_reward: f64, } impl Blockchain { - pub fn new(difficulty: usize) -> Self { + pub fn new(difficulty: usize, block_reward: f64) -> Self { let mut blockchain = Blockchain { chain: Vec::new(), pending_transactions: Vec::new(), difficulty, + block_reward, }; blockchain.create_genesis_block(); blockchain @@ -24,7 +25,7 @@ impl Blockchain { let genesis_transaction = Transaction::new("system".to_string(), "genesis".to_string(), 0.0, 0); let metadata = json!({ "description": "Genesis block", - "timestamp": Utc::now().to_rfc3339(), + "timestamp": chrono::Utc::now().to_rfc3339(), }); let genesis_block = Block::new(0, "0".to_string(), vec![genesis_transaction], "genesis_producer".to_string(), metadata); self.chain.push(genesis_block); @@ -42,7 +43,7 @@ impl Blockchain { let latest_block = self.get_latest_block(); let metadata = json!({ "miner": miner_address.clone(), - "timestamp": Utc::now().to_rfc3339(), + "timestamp": chrono::Utc::now().to_rfc3339(), }); let mut new_block = Block::new( latest_block.index + 1, @@ -53,7 +54,7 @@ impl Blockchain { ); new_block.mine_block(self.difficulty); self.chain.push(new_block); - self.pending_transactions = vec![Transaction::new("system".to_string(), miner_address, 1.0, 0)]; + self.pending_transactions = vec![Transaction::new("system".to_string(), miner_address, self.block_reward, 0)]; } pub fn is_chain_valid(&self) -> bool { @@ -68,6 +69,7 @@ impl Blockchain { if current_block.previous_hash != previous_block.block_hash { return false; } + } true } diff --git a/jam/src/config.rs b/jam/src/config.rs index cdef7e5..04b1459 100644 --- a/jam/src/config.rs +++ b/jam/src/config.rs @@ -1,11 +1,17 @@ pub struct Config { pub block_production_interval: u64, + pub difficulty: usize, + pub block_reward: f64, + pub genesis_block_producer: String, } impl Config { - pub fn new(block_production_interval: u64) -> Self { + pub fn new(block_production_interval: u64, difficulty: usize, block_reward: f64, genesis_block_producer: String) -> Self { Config { block_production_interval, + difficulty, + block_reward, + genesis_block_producer, } } } diff --git a/jam/src/main.rs b/jam/src/main.rs index 85c9eab..8ff0249 100644 --- a/jam/src/main.rs +++ b/jam/src/main.rs @@ -13,9 +13,9 @@ use serde_json::json; use tokio::time::{self, Duration}; fn main() { - let config = Config::new(6); // Set block production interval to 6 seconds + let config = Config::new(6, 2, 50.0, "genesis_producer".to_string()); // Set configurations - let mut blockchain = Blockchain::new(2); + let mut blockchain = Blockchain::new(config.difficulty, config.block_reward); // Add initial transactions blockchain.add_transaction(Transaction::new("Alice".to_string(), "Bob".to_string(), 10.0, 1));