forked from anthonyamar/simple-blockchain-in-ruby
-
Notifications
You must be signed in to change notification settings - Fork 24
/
blockchain.rb
89 lines (78 loc) · 2.41 KB
/
blockchain.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
###########################
# Improved version of "Build your own blockchain from scratch in 20 lines of Ruby!"
# from https://github.com/openblockchains/awesome-blockchains/tree/master/blockchain.rb
#
# and inspired by
# Let's Build the Tiniest Blockchain In Less Than 50 Lines of Python by Gerald Nash
# see https://medium.com/crypto-currently/lets-build-the-tiniest-blockchain-e70965a248b
#
# Now, Blockchain with prompt transactions, transactions counter for each block,
# ledger, proof of work and dynamic variable name.
#
# This Blockchain can be set as a loop for infinite using of the Blockchain.
#
#
# to run use:
# $ ruby ./blockchain.rb
#
#
#
require 'digest' # For hash checksum digest function SHA256
require 'pp' # For pp => pretty printer
# require 'pry' # For on the fly debugging
require_relative 'block' # class Block
require_relative 'transaction' # method Transactions
LEDGER = []
#####
## Blockchain building, one block at a time.
## This will create a first block with fake transactions
## and then prompt user for transactions informations and set it in a new block.
##
## Each block can take multiple transaction
## when a user has finish to add transaction,
## the block is added to the blockchain and writen in the ledger
def create_first_block
i = 0
instance_variable_set( "@b#{i}",
Block.first(
{ from: "Dutchgrown", to: "Vincent", what: "Tulip Bloemendaal Sunset", qty: 10 },
{ from: "Keukenhof", to: "Anne", what: "Tulip Semper Augustus", qty: 7 } )
)
LEDGER << @b0
pp @b0
p "============================"
add_block
end
def add_block
i = 1
loop do
instance_variable_set("@b#{i}", Block.next( (instance_variable_get("@b#{i-1}")), get_transactions_data))
LEDGER << instance_variable_get("@b#{i}")
p "============================"
pp instance_variable_get("@b#{i}")
p "============================"
i += 1
end
end
def launcher
puts "==========================="
puts ""
puts "Welcome to Simple Blockchain In Ruby !"
puts ""
sleep 1.5
puts "This program was created by Anthony Amar for and educationnal purpose"
puts ""
sleep 1.5
puts "Wait for the genesis (the first block of the blockchain)"
puts ""
for i in 1..10
print "."
sleep 0.5
break if i == 10
end
puts ""
puts ""
puts "==========================="
create_first_block
end
launcher