Struct blockchain::Chain
source · pub struct Chain {
pub chain: Vec<Block>,
pub current_transactions: Vec<Transaction>,
pub difficulty: f64,
pub address: String,
pub reward: f64,
pub fee: f64,
}Expand description
Blockchain
Fields§
§chain: Vec<Block>Chain of blocks
current_transactions: Vec<Transaction>List of transactions
difficulty: f64Current difficulty level of the network
address: StringBlockchain genesis address
reward: f64Block reward
fee: f64Transaction fee
Implementations§
source§impl Chain
impl Chain
sourcepub fn new(address: String, difficulty: f64, reward: f64, fee: f64) -> Self
pub fn new(address: String, difficulty: f64, reward: f64, fee: f64) -> Self
Initialize a new blockchain with the specified parameters.
Arguments
address: The address associated with the blockchain.difficulty: The initial mining difficulty level of the network.reward: The initial block reward for miners.fee: The transaction fee.
Returns
A new Chain instance with the given parameters and a 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.
Returns
A reference to a vector containing the current transactions.
sourcepub fn get_transaction(&self, hash: String) -> Option<&Transaction>
pub fn get_transaction(&self, hash: String) -> Option<&Transaction>
sourcepub fn validate_transaction(&self, from: &str, amount: f64) -> bool
pub fn validate_transaction(&self, from: &str, amount: f64) -> bool
sourcepub fn get_last_hash(&self) -> String
pub fn get_last_hash(&self) -> String
Get the hash of the last block in the blockchain.
Returns
The hash of the last block in the blockchain as a string.
sourcepub fn update_difficulty(&mut self, difficulty: f64) -> bool
pub fn update_difficulty(&mut self, difficulty: f64) -> bool
sourcepub fn update_reward(&mut self, reward: f64) -> bool
pub fn update_reward(&mut self, reward: f64) -> bool
sourcepub fn update_fee(&mut self, fee: f64) -> bool
pub fn update_fee(&mut self, fee: f64) -> bool
sourcepub fn generate_new_block(&mut self) -> bool
pub fn generate_new_block(&mut self) -> bool
Generate a new block and append it to the blockchain.
Returns
true if a new block is successfully generated and added to the blockchain.
sourcepub fn get_merkle(transactions: Vec<Transaction>) -> String
pub fn get_merkle(transactions: Vec<Transaction>) -> String
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.
Arguments
header: A mutable reference to the block header to be mined.
Trait Implementations§
source§impl<'de> Deserialize<'de> for Chain
impl<'de> Deserialize<'de> for Chain
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
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