Struct blockchain::Chain
source · pub struct Chain {
pub chain: Vec<Block>,
pub current_transactions: Vec<Transaction>,
pub difficulty: u32,
pub address: String,
pub reward: f32,
}Expand description
A blockchain
Fields§
§chain: Vec<Block>A chain of blocks
current_transactions: Vec<Transaction>A list of transactions
difficulty: u32A current difficulty level of the network
address: StringA blockchain genesis address
reward: f32A block reward
Implementations§
source§impl Chain
impl Chain
sourcepub fn new(address: String, difficulty: u32, reward: f32) -> Self
pub fn new(address: String, difficulty: u32, reward: f32) -> Self
Initialize an empty chain and empty current transactions, and generates the genesis block
sourcepub fn get_transactions(&mut self) -> &Vec<Transaction>
pub fn get_transactions(&mut self) -> &Vec<Transaction>
Get a list of current transactions in the blockchain
sourcepub fn get_transaction(&self, hash: String) -> Option<&Transaction>
pub fn get_transaction(&self, hash: String) -> Option<&Transaction>
Get a transaction by hash
sourcepub fn add_transaction(&mut self, from: String, to: String, amount: f32) -> bool
pub fn add_transaction(&mut self, from: String, to: String, amount: f32) -> bool
Add a new transaction to the blockchain
sourcepub fn get_last_hash(&self) -> String
pub fn get_last_hash(&self) -> String
Get a hash of the last block in the blockchain
sourcepub fn update_difficulty(&mut self, difficulty: u32) -> bool
pub fn update_difficulty(&mut self, difficulty: u32) -> bool
Update the mining difficulty of the blockchain
sourcepub fn update_reward(&mut self, reward: f32) -> bool
pub fn update_reward(&mut self, reward: f32) -> bool
Update the block reward
sourcepub fn generate_new_block(&mut self) -> bool
pub fn generate_new_block(&mut self) -> bool
Generate a new block and appends it to the blockchain
sourcepub fn get_merkle(transactions: Vec<Transaction>) -> String
pub fn get_merkle(transactions: Vec<Transaction>) -> String
Calculate the Merkle root hash for a list of transactions
sourcepub fn proof_of_work(header: &mut BlockHeader)
pub fn proof_of_work(header: &mut BlockHeader)
Perform the proof-of-work process to mine a block
Trait Implementations§
Auto Trait Implementations§
impl RefUnwindSafe for Chain
impl Send for Chain
impl Sync for Chain
impl Unpin for Chain
impl UnwindSafe for Chain
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more