Skip to content

Commit

Permalink
Update blockchain.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 11, 2024
1 parent 0a21f2d commit 637b346
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions pi-nexus-blockchain/src/blockchain.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
// blockchain.rs (new)
// blockchain.rs (update)

use crate::block::Block;
use crate::transaction::Transaction;
use std::collections::VecDeque;

pub struct Blockchain {
pub chain: VecDeque<Block>,
pub transactions: Vec<Transaction>,
}

impl Blockchain {
pub fn new() -> Self {
let genesis_block = Block::new(0, "0", vec![]);
Blockchain {
chain: VecDeque::from(vec![genesis_block]),
transactions: vec![],
}
}

pub fn add_block(&mut self, transactions: Vec<crate::transaction::Transaction>) {
pub fn add_transaction(&mut self, transaction: Transaction) {
self.transactions.push(transaction);
}

pub fn add_block(&mut self) {
let transactions = self.transactions.clone();
self.transactions.clear();
let previous_hash = self.chain.back().unwrap().hash.clone();
let new_block = Block::new(self.chain.len() as u64, &previous_hash, transactions);
self.chain.push_back(new_block);
}

pub fn get_transactions(&self) -> Vec<Transaction> {
self.transactions.clone()
}
}

0 comments on commit 637b346

Please sign in to comment.