Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(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