Skip to content

Commit

Permalink
initial commit - start here to follow along with the videos
Browse files Browse the repository at this point in the history
  • Loading branch information
encody committed Jan 17, 2019
0 parents commit 8f94f4d
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
/target
**/*.rs.bk
Cargo.lock
15 changes: 15 additions & 0 deletions Cargo.toml
@@ -0,0 +1,15 @@
[package]
name = "blockchain-in-rust"
version = "0.1.0"
authors = ["Jacob <jacob@geeklaunch.net>"]
edition = "2018"

[lib]
name = "blockchainlib"
path = "src/lib.rs"

[[bin]]
name = "blockchain"
path = "src/main.rs"

[dependencies]
13 changes: 13 additions & 0 deletions src/block.rs
@@ -0,0 +1,13 @@
use std::fmt::{ self, Debug, Formatter };

pub struct Block {
}

impl Debug for Block {
fn fmt (&self, f: &mut Formatter) -> fmt::Result {
write!(f, "Block")
}
}

impl Block {
}
63 changes: 63 additions & 0 deletions src/lib.rs
@@ -0,0 +1,63 @@
type BlockHash = Vec<u8>;

// Credit: https://stackoverflow.com/a/44378174/2773837
use std::time::{ SystemTime, UNIX_EPOCH };

pub fn now () -> u128 {
let duration = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
;

duration.as_secs() as u128 * 1000 + duration.subsec_millis() as u128
}

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

pub fn u64_bytes (u: &u64) -> [u8; 8] {
[
(u >> 8 * 0x0) as u8,
(u >> 8 * 0x1) as u8,
(u >> 8 * 0x2) as u8,
(u >> 8 * 0x3) as u8,

(u >> 8 * 0x4) as u8,
(u >> 8 * 0x5) as u8,
(u >> 8 * 0x6) as u8,
(u >> 8 * 0x7) as u8,
]
}

pub fn u128_bytes (u: &u128) -> [u8; 16] {
[
(u >> 8 * 0x0) as u8,
(u >> 8 * 0x1) as u8,
(u >> 8 * 0x2) as u8,
(u >> 8 * 0x3) as u8,

(u >> 8 * 0x4) as u8,
(u >> 8 * 0x5) as u8,
(u >> 8 * 0x6) as u8,
(u >> 8 * 0x7) as u8,

(u >> 8 * 0x8) as u8,
(u >> 8 * 0x9) as u8,
(u >> 8 * 0xa) as u8,
(u >> 8 * 0xb) as u8,

(u >> 8 * 0xc) as u8,
(u >> 8 * 0xd) as u8,
(u >> 8 * 0xe) as u8,
(u >> 8 * 0xf) as u8,
]
}

mod block;
pub use crate::block::Block;
3 changes: 3 additions & 0 deletions src/main.rs
@@ -0,0 +1,3 @@
fn main () {
println!("Hello, blockchain!");
}

0 comments on commit 8f94f4d

Please sign in to comment.