-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockchain_test.py
More file actions
87 lines (66 loc) · 2.64 KB
/
blockchain_test.py
File metadata and controls
87 lines (66 loc) · 2.64 KB
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
from block import Block
from blockchain import Blockchain
import uuid
BLOCK_SIZE = 25
# tests the functionality of the Blockchain.add_block() and validate_block() function
def test_add_block(bc, alice, bob):
# should not allow a lower or equal block number to the last block
b = Block(BLOCK_SIZE)
b.num = bc.current_block - 1
alice_init = bc.get_balance(alice)
new_address = uuid.uuid4().hex
b.tx(new_address, alice)
res = bc.add_block(b)
assert res == False, "Should not allow a lower or equal block number to the last block"
# block without a valid previous block should not be added
b = Block(BLOCK_SIZE)
b.num = bc.current_block + 2
bob_init = bc.get_balance(bob)
new_address = uuid.uuid4().hex
b.tx(new_address, bob)
res = bc.add_block(b)
assert res == False, "Block with a >1 higher block number should not be added"
# should not allow null type to be added as Block
b = None
res = bc.add_block(b)
assert res == False, "Should not allow null type to be added as Block"
# should not allow invalid object type to be added as Block
b = Blockchain()
res = bc.add_block(b)
assert res == False, "Should not allow Blockchain type to be added as Block"
# tx from empty balance should fail
b = Block(BLOCK_SIZE)
alice_init = bc.get_balance(alice)
new_address = uuid.uuid4().hex
b.tx(new_address, alice)
bc.add_block(b)
assert bc.get_balance(alice) == alice_init, "Tx from sender with zero-balance should not change chain's state"
# tests the functionality of the Blockchain.update_state() function
def test_update_state(bc, bob, alice):
# populate addresses with some cash through faucet
bc.faucet(bob)
bc.faucet(alice)
# tx from empty balance should fail
b = Block(BLOCK_SIZE)
alice_init = bc.get_balance(alice)
new_address = uuid.uuid4().hex
b.tx(new_address, alice)
bc.add_block(b)
assert bc.get_balance(alice) == alice_init, "Tx from sender with zero-balance should not change chain's state"
# blockchain state should initialize any new address to 0
b = Block(BLOCK_SIZE)
alice_init = bc.get_balance(alice)
new_address = uuid.uuid4().hex
b.tx(alice, new_address)
bc.add_block(b)
assert bc.state[new_address] != None , "Blockchain state should initialize any new address to 0"
if __name__=="__main__":
# init blockchain and genesis block
bc = Blockchain()
g = Block(BLOCK_SIZE)
# init addresses
bob = uuid.uuid4().hex
alice = uuid.uuid4().hex
# test functions
test_update_state(bc, bob, alice)
test_add_block(bc, bob, alice)