geeklaunch / blockchain-rust Goto Github PK
View Code? Open in Web Editor NEWA basic blockchain implementation in Rust for educational purposes only
Home Page: https://www.youtube.com/playlist?list=PLwnSaD6BDfXL0RiKT_5nOIdxTxZWpPtAv
A basic blockchain implementation in Rust for educational purposes only
Home Page: https://www.youtube.com/playlist?list=PLwnSaD6BDfXL0RiKT_5nOIdxTxZWpPtAv
This isn't a bug, but just a help. In your code pipe characters formatted like that:
pub fn difficulty_bytes_as_u128 (v: &Vec<u8>) -> u128 {
((v[31] as u128) << 0xf * 8) |
((v[30] as u128) << 0xe * 8) |
((v[29] as u128) << 0xd * 8) |
((v[28] as u128) << 0xc * 8) |
((v[27] as u128) << 0xb * 8) |
((v[26] as u128) << 0xa * 8) |
((v[25] as u128) << 0x9 * 8) |
((v[24] as u128) << 0x8 * 8) |
((v[23] as u128) << 0x7 * 8) |
((v[22] as u128) << 0x6 * 8) |
((v[21] as u128) << 0x5 * 8) |
((v[20] as u128) << 0x4 * 8) |
((v[19] as u128) << 0x3 * 8) |
((v[18] as u128) << 0x2 * 8) |
((v[17] as u128) << 0x1 * 8) |
((v[16] as u128) << 0x0 * 8)
}
But in my vscode formatting is that:
pub fn difficulty_bytes_as_u128(v: &Vec<u8>) -> u128 {
((v[31] as u128) << 0xf * 8)
| ((v[30] as u128) << 0xe * 8)
| ((v[29] as u128) << 0xd * 8)
| ((v[28] as u128) << 0xc * 8)
| ((v[27] as u128) << 0xb * 8)
| ((v[26] as u128) << 0xa * 8)
| ((v[25] as u128) << 0x9 * 8)
| ((v[24] as u128) << 0x8 * 8)
| ((v[23] as u128) << 0x7 * 8)
| ((v[22] as u128) << 0x6 * 8)
| ((v[21] as u128) << 0x5 * 8)
| ((v[20] as u128) << 0x4 * 8)
| ((v[19] as u128) << 0x3 * 8)
| ((v[18] as u128) << 0x2 * 8)
| ((v[17] as u128) << 0x1 * 8)
| ((v[16] as u128) << 0x0 * 8)
}
I want to format code like yours. How can i do that? Thanks.
I did not understand this code block, can you explain it pls:
pub fn u32_bytes (u: &u32) -> [u8; 4] {
[
(u >> 8 * 0x0) as u8,
(u >> 8 * 0x1) as u8,
(u >> 8 * 0x2) as u8,
(u >> 8 * 0x3) as u8,
]
}
Hi, thanks for your videos and explanation, I'm studying it to understand better the blockchain, I've a question about this code block:
pub fn mine (&mut self) {
for nonce_attempt in 0..(u64::max_value()) {
self.nonce = nonce_attempt;
let hash = self.hash();
if check_difficulty(&hash, self.difficulty) {
self.hash = hash;
return;
}
}
}
Here, the hash is created based on the block data and required difficulty, and conceptual wise (if my understood correctly), I should be able to catch any data tampering within the block by recalculating the hash then comparing it with the prev_hash_code
in the following block, how can I do this with the code above!!
thanks for your efforts and time , I have a warrning when I run cargo as it couldn't read last hash I couldn't figure the issue that cause the problem as well as Failed to add block: InsufficientInputValue' where should the value stored:
please check the result
warning: value assigned to last_hash
is never read
--> src/main.rs:63:5
|
63 | last_hash = block.hash.clone();
| ^^^^^^^^^
|
= note: #[warn(unused_assignments)]
on by default
= help: maybe it is overwritten before being read?
warning: hash
(bin "blockchain") generated 1 warning
Finished dev [unoptimized + debuginfo] target(s) in 0.12s
Running target/debug/blockchain
Mined genesis block Block[0]: e2937e782fc68a1a0f8f71830f69919136139f6f956fda1e6999f57327070100 at: 1651809036733 with: 1 nonce: 3458
Mined block Block[1]: cb8add22253b754895c232b4ca65f57dcbb42b1ebdb5bd80242e4fdea6b50500 at: 1651809036998 with: 2 nonce: 4312
thread 'main' panicked at 'Failed to add block: InsufficientInputValue', src/main.rs:65:41
note: run with RUST_BACKTRACE=1
environment variable to display a backtrace
thanks for the nice topic
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.