Skip to content

Instantly share code, notes, and snippets.

@liquidz
Created February 23, 2018 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liquidz/0ddb1b9ea63834592667dab3ae4923dd to your computer and use it in GitHub Desktop.
Save liquidz/0ddb1b9ea63834592667dab3ae4923dd to your computer and use it in GitHub Desktop.
(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