Block

Struct Block 

Source
pub struct Block {
    pub index: u32,
    pub timestamp: u128,
    pub hash: Vec<u8>,
    pub prev_block_hash: Vec<u8>,
    pub nonce: u64,
    pub transactions: Vec<Transaction>,
    pub difficulty: u128,
}
Expand description

Payload include transactions, difficulty,..

Order store of bytes: there are 2 types big-endian like 00 00 00 2a, little-endian like (our choice) 2a 00 00 00, u128 is in edian order, so because this material 16bytes of our hash will appear at the end of out hash’s byte sector.

nonce is just field for changes in block as an arbitary that hashed along with the data. so generating the correct hash for a block is like the puzzle , and the nonce is the key to that puzzle. the process of finding that key is called mining.



Overspending: where did the money come from? inputs must be >= sum of values of generated outputs.

OverSpending: Sum(inputs)>=Sum(Outputs). I can’t input 5 coins and be able to output 7. (on other hand inputs have to be greater since must be enough fee in input section for paying to miner.)

Impersonation: this can be solved by adding a “Signature” to outpus to verify they are being spent by their owner.(We can’t assume that whoever sent us the trx over the network is also the person who created the trx. For now we’ll kind of ignore solving this problem. we might come back to it when we go over smart contracts).

Impersonation: who owns the money and who is sending it? Solved by adding signature and smart contract(not cover in this example).

DoubleSpending: make sure that anyone output is never used as an input more than once. This can be done by maintaining a pool of unspent outputs and rejecting any trx that tries to spend outputs that don’t exist in the pool.

Double Spending: is the money avaliable? any one output is never used as an input more than once.



Inputs: unused outputs from prev TRXs, Outputs: new outouts That can be used in future TRXs.

Fields§

§index: u32§timestamp: u128§hash: Vec<u8>§prev_block_hash: Vec<u8>§nonce: u64§transactions: Vec<Transaction>§difficulty: u128

Implementations§

Source§

impl Block

Source

pub fn new( index: u32, timestamp: u128, prev_block_hash: Vec<u8>, transactions: &mut Rc<Cell<Vec<Transaction>>>, difficulty: u128, ) -> Self

Source

pub fn blockchain_mine(&mut self) -> Result<Vec<u8>, CustomError>

0xfff… lowest difficulty
0x000… => highest difficulty => taking more time=> more highest nonce=> the end of blockhash see more zero so nonce 0 means end of of blockchash there isn’nt any zero at the end of blockhash.

nonce is just field for changes in block as an arbitary that hashed along with the data. so generating the correct hash for a block is like the puzzle , and the nonce is the key to that puzzle. the process of finding that key is called mining.

mining sterategy:
1.Generate nonce 2.Hash bytes 3.Check hash against difficulty(Insufficant? Go Step1 and Sufficient Go Step 4) 4. Add block to chain 5. Submit to peers

Trait Implementations§

Source§

impl Debug for Block

Source§

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

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

impl Hashable for Block

Concatenate together all the bytes

Generate unique data fingerprint: the hash

Source§

fn bytes(&self) -> Vec<u8>

Source§

fn hash(&self) -> Vec<u8>

Source§

impl Serialize for Block

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 Freeze for Block

§

impl RefUnwindSafe for Block

§

impl Send for Block

§

impl Sync for Block

§

impl Unpin for Block

§

impl UnwindSafe for Block

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.