Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 2.51 KB | None | 0 0
  1. ;;; blockchain.scm
  2.  
  3. (load (spheres/crypto digest))
  4.  
  5. (define-macro (for var to . body)
  6.   `(let loop ((,var 0))
  7.      (if (< ,var ,to)
  8.          (begin ,@body (loop (+ ,var 1))))))
  9.  
  10. (define (char-is-zero? string index)
  11.   (if (eq? #\0 (string-ref string index)) #t #f))
  12.  
  13. ;; https://stackoverflow.com/a/6727536
  14. (define and-l (lambda x
  15.                 (if (null? x)
  16.                     #t
  17.                     (if (car x) (apply and-l (cdr x)) #f))))
  18.  
  19. (define-macro (list-range* n proc)
  20.   `(let loop ((i ,n) (lst '()))
  21.      (if (zero? i)
  22.          lst
  23.          (loop (- i 1) (cons ,proc lst)))))
  24.  
  25. (define (n-chars-are-zero? string n)
  26.   (let ((lst (list-range* n (char-is-zero? string (- i 1)))))
  27.     (apply and-l lst)))
  28.  
  29. ;; our blocks are just vectors of length 3
  30. (define-structure block
  31.   number
  32.   data
  33.   nonce
  34.   prevhash)
  35.  
  36. ;; required for block hashes to be identical between restarts
  37. (define (block-to-string block)
  38.   (let ((o (open-output-string)))
  39.     (write (block-number block) o)
  40.     (write (block-data block) o)
  41.     (write (block-nonce block) o)
  42.     (write (block-prevhash block) o)
  43.     (get-output-string o)))
  44.  
  45. (define (hash-block block)
  46.   (md5 (block-to-string block)))
  47.  
  48. ;; zeroth (genesis) block is pre-defined
  49. (define zeroth-block (make-block
  50.                       0
  51.                       ":)"
  52.                       70924
  53.                       "00000000000000000000000000000000"))
  54.  
  55. ;; Block found!
  56. ;; #<block #2 number: 0 data: ":)" nonce: 70924 prevhash: "00000000000000000000000000000000">
  57. ;; 00002983b797351053bd7a2fd1f9da8f
  58.  
  59. (define leading-zeros 4) ; 4 leading zeros of md5 hash
  60.  
  61. (define blockchain (list zeroth-block))
  62.  
  63. (define blockchain-height ; there is only 1 blockchain
  64.   (block-number (car blockchain)))
  65.  
  66. (define (new-block data)
  67.   (make-block
  68.    (+ 1 blockchain-height)
  69.    data
  70.    0
  71.    (hash-block (car blockchain))))
  72.  
  73. (define (mine-block block)
  74.   (let loop ((i 0) (block-hash (hash-block block)))
  75.     (if (n-chars-are-zero? block-hash leading-zeros)
  76.         (begin
  77.           (print "\nBlock found!\n")
  78.           (print block "\n")
  79.           (print (hash-block block) "\n")
  80.           (add-block-to-chain block))
  81.         (begin
  82.           (block-nonce-set! block (+ (block-nonce block) 1))
  83.           (loop (+ i 1) (hash-block block))))))
  84.  
  85. (define (add-block-to-chain block)
  86.   (if (and (equal? (hash-block (car blockchain)) (= (block-number block) (+ 1 (block-number (car blockchain))))))
  87.       (block-prevhash block)
  88.       (set! blockchain (cons block blockchain))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement