From 637b346d4da74fb81e0d6a766dfc8a7f2ba9bcde Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Sun, 11 Aug 2024 14:51:48 +0700 Subject: [PATCH] Update blockchain.rs --- pi-nexus-blockchain/src/blockchain.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/pi-nexus-blockchain/src/blockchain.rs b/pi-nexus-blockchain/src/blockchain.rs index a47764d72..321f6802f 100644 --- a/pi-nexus-blockchain/src/blockchain.rs +++ b/pi-nexus-blockchain/src/blockchain.rs @@ -1,10 +1,12 @@ -// blockchain.rs (new) +// blockchain.rs (update) use crate::block::Block; +use crate::transaction::Transaction; use std::collections::VecDeque; pub struct Blockchain { pub chain: VecDeque, + pub transactions: Vec, } impl Blockchain { @@ -12,12 +14,23 @@ impl Blockchain { 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) { + 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 { + self.transactions.clone() + } }