Created
February 23, 2018 09:30
-
-
Save liquidz/0ddb1b9ea63834592667dab3ae4923dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns clj-tiny-blockchain.core | |
(:require [buddy.hashers :as hashers] | |
[java-time :as t])) | |
(defn- now [] | |
(t/local-date-time)) | |
(defn- hash-block [m] | |
(let [{:keys [index timestamp data previous-hash]} m] | |
(-> (str index timestamp data previous-hash) | |
(hashers/derive {:alg :pbkdf2+sha256})))) | |
(defn block [m] | |
(assoc m :hash (hash-block m))) | |
(defn genesis-block [] | |
(-> {:index 0 | |
:timestamp (now) | |
:data "Genenis Block" | |
:previous-hash "0"} | |
block)) | |
(defn next-block [last-block] | |
(let [index (-> last-block :index inc)] | |
(-> last-block | |
(assoc :index index | |
:timestamp (now) | |
:data (str "Hey! I'm block " index) | |
:previous-hash (:hash last-block)) | |
block))) | |
(defn -main | |
[] | |
(doseq [blk (take 10 (iterate next-block (genesis-block)))] | |
(println (format "Block #%d : %s" (:index blk) (:hash blk))) | |
(println (format "Previous: %s" (:previous-hash blk))) | |
(println "----"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment