-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.rb
121 lines (94 loc) · 2.58 KB
/
tests.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require "colorize"
require_relative "constants"
require_relative "utilities"
require_relative "models/block"
require_relative "models/blockchain"
require_relative "models/node"
require_relative "models/transaction"
def test(description)
is_true = yield()
print Utilities::fixed_length(description, 60) + "-> "
if is_true
puts "passed".green
else
puts "failed".red
end
end
alice = Node.new(port: 3000)
bob = Node.new(port: 3001)
will = Node.new(port: 3002)
t1 = Transaction.new(from: alice.public_key, to: will.public_key, amount: 10)
t2 = Transaction.new(from: will.public_key, to: bob.public_key, amount: 5 )
t3 = Transaction.new(from: bob.public_key, to: alice.public_key, amount: 3)
block1 = Block.new(
transactions: [t1],
previous_block: nil
)
test("transaction is not valid before being signed") {
!t1.is_valid?
}
test("block is not valid with invalid transaction") {
!block1.is_valid?
}
alice.sign(transaction: t1)
will.sign(transaction: t2)
bob.sign(transaction: t3)
test("transaction is valid after signature") {
t1.is_valid?
}
test("block is invalid with valid transaction but missing nonce") {
!block1.is_valid?
}
block1.find_matching_nonce
test("block is valid with valid transactions and valid nonce") {
block1.is_valid?
}
block2 = Block.generate(
transactions: [t2],
previous_block: block1
)
block3 = Block.generate(
transactions: [t3],
previous_block: block2
)
blockchain = Blockchain.new(blocks: [block1, block2, block3])
test("height is right") {
blockchain.height == 3
}
test("blockchain is invalid if balances go below zero") {
!blockchain.is_valid?
}
t0 = Transaction.new(to: alice.public_key, amount: 50, genesis: true)
test("genesis transactions need not be signed") {
t0.is_valid?
}
block0 = Block.generate(
transactions: [t0],
previous_block: nil
)
test("new gensis block with genesis transactions valid") {
block0.is_valid?
}
blockchain = Blockchain.new(blocks: [block0, block1, block2, block3])
test("blockchain with two genesis blocks shouldn't be valid") {
!blockchain.is_valid?
}
block1.set_previous_block(block: block0)
test("block becomes invalid after setting previous block") {
!block1.is_valid?
}
block1.find_matching_nonce
test("block re-valid after re-finding nonce") {
block1.is_valid?
}
test("after re-setting block pointers, blockchain is invalid since block hashes changed") {
!blockchain.is_valid?
}
block2.set_previous_block(block: block1)
block2.find_matching_nonce
block3.set_previous_block(block: block2)
block3.find_matching_nonce
test("finally, blockchain valid") {
blockchain.is_valid?
}
puts blockchain