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: u32

A current difficulty level of the network

§address: String

A blockchain genesis address

§reward: f32

A block reward

Implementations§

source§

impl Chain

source

pub fn new(address: String, difficulty: u32, reward: f32) -> Self

Initialize an empty chain and empty current transactions, and generates the genesis block

source

pub fn get_transactions(&mut self) -> &Vec<Transaction>

Get a list of current transactions in the blockchain

source

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

Get a transaction by hash

source

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

Add a new transaction to the blockchain

source

pub fn get_last_hash(&self) -> String

Get a hash of the last block in the blockchain

source

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

Update the mining difficulty of the blockchain

source

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

Update the block reward

source

pub fn generate_new_block(&mut self) -> bool

Generate a new block and appends it to the blockchain

source

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

Calculate the Merkle root hash for a list of transactions

source

pub fn proof_of_work(header: &mut BlockHeader)

Perform the proof-of-work process to mine a block

source

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

Calculate the SHA-256 hash

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 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 T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where 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 T
where 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.