Skip to content

Commit

Permalink
Store all data as strings for easier serialization/deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
bor0 committed Jul 12, 2018
1 parent 590d59c commit 93ec334
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 16 deletions.
5 changes: 3 additions & 2 deletions main-helper.rkt
@@ -1,6 +1,7 @@
#lang racket
(require "./src/blockchain.rkt")
(require "./src/utils.rkt")

(require (only-in sha bytes->hex-string))

(define (format-transaction t)
Expand All @@ -11,8 +12,8 @@

(define (print-block bl)
(printf "Block information\n=================\nHash:\t~a\nHash_p:\t~a\nStamp:\t~a\nNonce:\t~a\nData:\t~a\n"
(bytes->hex-string (block-hash bl))
(bytes->hex-string (block-previous-hash bl))
(block-hash bl)
(block-previous-hash bl)
(block-timestamp bl)
(block-nonce bl)
(format-transaction (block-transaction bl))))
Expand Down
2 changes: 1 addition & 1 deletion main.rkt
Expand Up @@ -22,7 +22,7 @@

; Blockchain initiation
(printf "Mining genesis block...\n")
(define blockchain (init-blockchain genesis-t (string->bytes/utf-8 "seedgenesis") utxo))
(define blockchain (init-blockchain genesis-t "1337cafe" utxo))
(print-wallets blockchain wallet-a wallet-b)

; Make a second transaction
Expand Down
14 changes: 8 additions & 6 deletions src/block.rkt
@@ -1,19 +1,21 @@
#lang racket
(require "utils.rkt")
(require (only-in sha sha256))
(require (only-in sha bytes->hex-string))
(require racket/serialize)

(define difficulty 2)
(define target (make-bytes difficulty 32))
(define target (bytes->hex-string (make-bytes difficulty 32)))

(struct block (hash previous-hash transaction timestamp nonce) #:prefab)

; Procedure for calculating block hash
(define (calculate-block-hash previous-hash timestamp transaction nonce)
(sha256 (bytes-append
previous-hash
(bytes->hex-string (sha256 (bytes-append
(string->bytes/utf-8 previous-hash)
(string->bytes/utf-8 (number->string timestamp))
(string->bytes/utf-8 (~a (serialize transaction)))
(string->bytes/utf-8 (number->string nonce)))))
(string->bytes/utf-8 (number->string nonce))))))

; A block is valid if...
(define (valid-block? bl)
Expand All @@ -27,8 +29,8 @@
; A block is mined if
(define (mined-block? hash)
; the hash matches the target, given the difficulty
(equal? (subbytes hash 1 difficulty)
(subbytes target 1 difficulty)))
(equal? (subbytes (hex-string->bytes hash) 1 difficulty)
(subbytes (hex-string->bytes target) 1 difficulty)))

; Hashcash implementation
(define (make-and-mine-block target previous-hash timestamp transaction nonce)
Expand Down
6 changes: 3 additions & 3 deletions src/transaction-io.rkt
@@ -1,16 +1,16 @@
#lang racket
(require "utils.rkt")
(require (only-in sha sha256))
(require (only-in sha bytes->hex-string))
(require racket/serialize)

(struct transaction-io (hash value owner timestamp) #:prefab)

; Procedure for calculating the hash of a transaction-io object
(define (calculate-transaction-io-hash value owner timestamp)
(sha256 (bytes-append
(bytes->hex-string (sha256 (bytes-append
(string->bytes/utf-8 (number->string value))
(string->bytes/utf-8 (~a (serialize owner)))
(string->bytes/utf-8 (number->string timestamp)))))
(string->bytes/utf-8 (number->string timestamp))))))

; Make a transaction-io object with calculated hash
(define (make-transaction-io value owner)
Expand Down
6 changes: 3 additions & 3 deletions src/transaction.rkt
Expand Up @@ -15,12 +15,12 @@
(define (sign-transaction from to value)
(let ([privkey (wallet-private-key from)]
[pubkey (wallet-public-key from)])
(digest/sign (datum->pk-key (hex-string->bytes privkey) 'PrivateKeyInfo)
(bytes->hex-string (digest/sign (datum->pk-key (hex-string->bytes privkey) 'PrivateKeyInfo)
'sha1
(bytes-append
(string->bytes/utf-8 (~a (serialize from)))
(string->bytes/utf-8 (~a (serialize to)))
(string->bytes/utf-8 (number->string value))))))
(string->bytes/utf-8 (number->string value)))))))

; Make an empty, unprocessed and unsigned transaction
(define (make-transaction from to value inputs)
Expand Down Expand Up @@ -64,7 +64,7 @@
(string->bytes/utf-8 (~a (serialize (transaction-from t))))
(string->bytes/utf-8 (~a (serialize (transaction-to t))))
(string->bytes/utf-8 (number->string (transaction-value t))))
(transaction-signature t))))
(hex-string->bytes (transaction-signature t)))))

; A transaction is valid if...
(define (valid-transaction? t)
Expand Down
1 change: 0 additions & 1 deletion src/utils.rkt
@@ -1,5 +1,4 @@
#lang racket
(require "block.rkt")
(require racket/serialize)

(define ASCII-ZERO (char->integer #\0))
Expand Down

0 comments on commit 93ec334

Please sign in to comment.