-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
15 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |