Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 19add7f

Browse files
committed
rectructured
1 parent 6769c7c commit 19add7f

9 files changed

Lines changed: 92 additions & 49 deletions

File tree

.idea/block_chain.iml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 27 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
name = "block_chain"
33
version = "0.1.0"
44
authors = ["Bosswell <[email protected]>"]
5-
edition = "2020"
5+
edition = "2018"
66

77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10-
hex = "0.4"
10+
#hex = "0.4"
11+
#crypto-hashes = "0.9.0"
1112

1213
[[bin]]
1314
name = "block_chain"
14-
path = "main.rs"
15+
path = "src/main.rs"

main.exe

4.74 MB
Binary file not shown.

main.rs

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/block.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use std::fmt::{ Formatter, Debug, Result };
2+
use Hashable;
3+
4+
type BlockHash = Vec<u8>;
5+
6+
pub struct Block {
7+
pub index: u64,
8+
pub timestamp: u128,
9+
pub prev_block_hash: BlockHash,
10+
pub hash: BlockHash,
11+
pub nonce: u64,
12+
pub payload: String,
13+
}
14+
15+
impl Block {
16+
pub fn new(index: u64, timestamp: u128, prev_block_hash: BlockHash, nonce: u64, payload: String) -> Self {
17+
Block {
18+
index,
19+
timestamp,
20+
prev_block_hash,
21+
hash: vec![0; 32],
22+
nonce,
23+
payload
24+
}
25+
}
26+
}
27+
28+
impl Debug for Block {
29+
fn fmt(&self, f: &mut Formatter) -> Result {
30+
write!(f, "Block [{}]: at: {} with: {}",
31+
&self.index,
32+
// &hex::encode(&self.hash),
33+
&self.timestamp,
34+
&self.payload,
35+
)
36+
}
37+
}
38+
39+
impl Hashable for Block {
40+
41+
}

src/hashable.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pub trait Hashable {
2+
// fn bytes(&self) -> Vec<u8>;
3+
fn hash(&self) -> &str {
4+
"Hashing test"
5+
// crypto_hash::digest(crypto_hash::Alghorith::SHA256, &self.bytes());
6+
}
7+
}

src/main.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
mod block;
2+
pub use crate::block::Block;
3+
mod hashable;
4+
pub use crate::hashable::Hashable;
5+
6+
fn main() {
7+
let block = Block::new(0, 0, vec![0; 32], 0, "Genesis block".to_owned());
8+
println!("{:?}", &block);
9+
10+
println!("{:?}", block.hash());
11+
}

0 commit comments

Comments
 (0)