Skip to content

Commit

Permalink
add other params
Browse files Browse the repository at this point in the history
  • Loading branch information
stojanov-igor committed Jul 1, 2024
1 parent 5ff6d18 commit ba48f9b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
12 changes: 7 additions & 5 deletions jam/src/blockchain.rs
Original file line number Diff line number Diff line change
@@ -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<Block>,
pub pending_transactions: Vec<Transaction>,
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
Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -68,6 +69,7 @@ impl Blockchain {
if current_block.previous_hash != previous_block.block_hash {
return false;
}

}
true
}
Expand Down
8 changes: 7 additions & 1 deletion jam/src/config.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}
}
4 changes: 2 additions & 2 deletions jam/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit ba48f9b

Please sign in to comment.