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,
    pub wallets: HashMap<String, Wallet>,
}
Expand description

Blockchain

Fields§

§chain: Vec<Block>

Chain of blocks

§current_transactions: Vec<Transaction>

List of transactions

§difficulty: f64

Current difficulty level of the network

§address: String

Blockchain genesis address

§reward: f64

Block reward

§fee: f64

Transaction fee

§wallets: HashMap<String, Wallet>

A map to associate wallets with their corresponding addresses and balances.

Implementations§

source§

impl Chain

source

pub fn new(difficulty: f64, reward: f64, fee: f64) -> Self

Initialize a new blockchain with the specified parameters.

Arguments
  • 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.

source

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.

source

pub fn get_transaction(&self, hash: String) -> Option<&Transaction>

Get a transaction by its hash.

Arguments
  • hash: The hash of the transaction to retrieve.
Returns

An option containing a reference to the transaction if found, or None if not found.

source

pub fn add_transaction(&mut self, from: String, to: String, amount: f64) -> bool

Add a new transaction to the blockchain.

Arguments
  • from: The sender’s address.
  • to: The receiver’s address.
  • amount: The amount of the transaction.
Returns

true if the transaction is successfully added to the current transactions.

source

pub fn validate_transaction(&self, from: &str, to: &str, amount: f64) -> bool

Validate a transaction.

Arguments
  • from: The sender’s address.
  • to: The receiver’s address.
  • amount: The amount of the transaction.
Returns

true if the transaction is valid, false otherwise.

source

pub fn create_wallet(&mut self, email: String) -> String

Create a new wallet with a unique email and an initial balance.

Arguments
  • email: The unique user email.
Returns

The newly created wallet address.

source

pub fn get_wallet_balance(&self, address: String) -> Option<f64>

Get a wallet’s balance based on its address.

Arguments
  • address: The unique wallet address.
Returns

The wallet balance.

source

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.

source

pub fn update_difficulty(&mut self, difficulty: f64) -> bool

Update the mining difficulty of the blockchain.

Arguments
  • difficulty: The new mining difficulty level.
Returns

true if the difficulty is successfully updated.

source

pub fn update_reward(&mut self, reward: f64) -> bool

Update the block reward.

Arguments
  • reward: The new block reward value.
Returns

true if the reward is successfully updated.

source

pub fn update_fee(&mut self, fee: f64) -> bool

Update the transaction fee.

Arguments
  • fee: The new transaction fee value.
Returns

true if the transaction fee is successfully updated.

source

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.

source

pub fn get_merkle(transactions: Vec<Transaction>) -> String

Calculate the Merkle root hash for a list of transactions.

Arguments
  • transactions: A vector of transactions for which the Merkle root hash is calculated.
Returns

The Merkle root hash as a string.

source

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.
source

pub fn hash<T: Serialize>(item: &T) -> String

Calculate the SHA-256 hash of a serializable item.

Arguments
  • item: A serializable item to be hashed.
Returns

The SHA-256 hash of the item as a string.

Trait Implementations§

source§

impl Debug for Chain

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for Chain

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Serialize for Chain

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where __S: Serializer,

Serialize this value into the given Serde serializer. 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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,