forked from samuelgoh1525/falcon-blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_blockchain.py
112 lines (74 loc) · 3.36 KB
/
test_blockchain.py
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
import hashlib
import json
from unittest import TestCase
from blockchain import Blockchain
class BlockchainTestCase(TestCase):
def setUp(self):
self.blockchain = Blockchain()
def create_block(self, previous_hash='abc'):
block = self.blockchain.new_block(previous_hash)
current_hash = self.blockchain.hash(block)
block['hash'] = current_hash
self.blockchain.add_block(block)
def create_transaction(self, sender='a', recipient='b', amount=1, signature='test'):
# TODO: check signature properly with sender public key
# change the bottom two tests also
self.blockchain.new_transaction(
sender=sender,
recipient=recipient,
amount=amount,
signature=signature
)
class TestRegisterNodes(BlockchainTestCase):
def test_valid_nodes(self):
blockchain = Blockchain()
blockchain.register_node('http://192.168.0.1:5000')
self.assertIn('192.168.0.1:5000', blockchain.nodes)
def test_malformed_nodes(self):
blockchain = Blockchain()
blockchain.register_node('http//192.168.0.1:5000')
self.assertNotIn('192.168.0.1:5000', blockchain.nodes)
def test_idempotency(self):
blockchain = Blockchain()
blockchain.register_node('http://192.168.0.1:5000')
blockchain.register_node('http://192.168.0.1:5000')
assert len(blockchain.nodes) == 1
class TestBlocksAndTransactions(BlockchainTestCase):
def test_block_creation(self):
self.create_block()
latest_block = self.blockchain.last_block
# The genesis block is create at initialization, so the length should be 2
assert len(self.blockchain.chain) == 2
assert latest_block['index'] == 2
assert latest_block['timestamp'] is not None
block_without_hash = latest_block.copy()
block_without_hash.pop('hash')
assert latest_block['hash'] == self.blockchain.hash(block_without_hash)
assert latest_block['previous_hash'] == 'abc'
def test_create_transaction(self):
self.create_transaction()
transaction = self.blockchain.current_transactions[-1]
assert transaction
assert transaction['sender'] == 'a'
assert transaction['recipient'] == 'b'
assert transaction['amount'] == 1
def test_block_resets_transactions(self):
self.create_transaction()
initial_length = len(self.blockchain.current_transactions)
self.create_block()
current_length = len(self.blockchain.current_transactions)
assert initial_length == 1
assert current_length == 0
def test_return_last_block(self):
self.create_block()
created_block = self.blockchain.last_block
assert len(self.blockchain.chain) == 2
assert created_block is self.blockchain.chain[-1]
class TestHashingAndProofs(BlockchainTestCase):
def test_hash_is_correct(self):
self.create_block()
new_block = self.blockchain.last_block
new_block_json = json.dumps(self.blockchain.last_block, sort_keys=True).encode()
new_hash = hashlib.sha256(new_block_json).hexdigest()
assert len(new_hash) == 64
assert new_hash == self.blockchain.hash(new_block)