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: u32timestamp: u128hash: Vec<u8>prev_block_hash: Vec<u8>nonce: u64transactions: Vec<Transaction>difficulty: u128

Implementations

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

Formats the value using the given formatter. Read more

Concatenate together all the bytes

Generate unique data fingerprint: the hash

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.