Struct library_blockchain::block::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
sourceimpl Block
impl Block
pub fn new(
index: u32,
timestamp: u128,
prev_block_hash: Vec<u8>,
transactions: &mut Rc<Cell<Vec<Transaction>>>,
difficulty: u128
) -> Self
sourcepub fn blockchain_mine(&mut self) -> Result<Vec<u8>, CustomError>
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
Auto Trait Implementations
impl RefUnwindSafe for Block
impl Send for Block
impl Sync for Block
impl Unpin for Block
impl UnwindSafe for Block
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more