Skip to content

Commit

Permalink
make block production configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
stojanov-igor committed Jul 1, 2024
1 parent 1995f51 commit 5ff6d18
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
11 changes: 11 additions & 0 deletions jam/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub struct Config {
pub block_production_interval: u64,
}

impl Config {
pub fn new(block_production_interval: u64) -> Self {
Config {
block_production_interval,
}
}
}
11 changes: 8 additions & 3 deletions jam/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ mod blockchain;
mod smart_contract;
mod transaction;
mod utils;
mod config;

use crate::blockchain::Blockchain;
use crate::transaction::Transaction;
use crate::smart_contract::SmartContract;
use crate::config::Config;
use serde_json::json;
use tokio::time::{self, Duration};

fn main() {
let config = Config::new(6); // Set block production interval to 6 seconds

let mut blockchain = Blockchain::new(2);

// Add initial transactions
Expand Down Expand Up @@ -39,7 +43,8 @@ fn main() {
// Create a runtime and spawn the block production task
let runtime = tokio::runtime::Runtime::new().unwrap();
let blockchain = std::sync::Arc::new(tokio::sync::Mutex::new(blockchain));
runtime.spawn(start_block_production(blockchain.clone()));
let interval = config.block_production_interval;
runtime.spawn(start_block_production(blockchain.clone(), interval));

// Keep the main function alive
runtime.block_on(async {
Expand All @@ -49,8 +54,8 @@ fn main() {
});
}

async fn start_block_production(blockchain: std::sync::Arc<tokio::sync::Mutex<Blockchain>>) {
let mut interval = time::interval(Duration::from_secs(6));
async fn start_block_production(blockchain: std::sync::Arc<tokio::sync::Mutex<Blockchain>>, interval: u64) {
let mut interval = time::interval(Duration::from_secs(interval));
loop {
interval.tick().await;
let mut blockchain = blockchain.lock().await;
Expand Down

0 comments on commit 5ff6d18

Please sign in to comment.