forked from gauravvjn/blockchain-python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.py
More file actions
Latest commit
87 lines (53 loc) · 2.49 KB
/
Copy pathdriver.py
File metadata and controls
87 lines (53 loc) · 2.49 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
87
importrequests
fromblockchainimportBlockChain
blockchain=BlockChain()
# --------------------------- Testing The Blockchain Class ---------------------------------------------
defprint_blockchain(chain):
forblockinchain:
print(vars(block))
defclass_tests():
print("Length of Current blockchain is: {}".format(len(blockchain.chain)))
print_blockchain(blockchain.chain)
blockchain.mine_block('address_x')
print("\nAfter Mining . . .")
print("Length of Updated blockchain is: {}".format(len(blockchain.chain)))
print_blockchain(blockchain.chain)
blockchain.mine_block('address_y')
print("\nAfter One more Mining . . .")
print("Length of Updated blockchain is: {}".format(len(blockchain.chain)))
print_blockchain(blockchain.chain)
# --------------------------------- Testing Blockchain APIs --------------------------------------------
defregister_node(node_addr, parent_server):
resp=requests.post(parent_server+'/register-node', json={'address': node_addr})
print("\nOn Server {}: Node-{} has been registered successfully!\n".format(parent_server, node_addr))
returnresp
defcreate_transaction(server, data):
resp=requests.post(server+'/create-transaction', json=data).json()
print("On Server {}: Transaction has been processed!\n".format(server))
returnresp
defmine_block(server):
resp=requests.get(server+'/mine').json()
print("On Server {}: Block has been mined successfully!\n".format(server))
returnresp
defget_server_chain(server):
resp=requests.get(server+'/chain').json()
print("On Server {}: Chain is-\n{}\n".format(server, resp))
returnresp
defsync_chain(server):
print("On Server {}: Started Syncing Chain . . .".format(server))
resp=requests.get(server+'/sync-chain')
print("On Server {}: Chain synced!\n".format(server))
returnresp
defapi_tests():
server1='http://127.0.0.1:5000'
server2='http://127.0.0.1:5001'
register_node(server2, server1) # server2 node will be register inside server1
create_transaction(server2, {'sender': 'I', 'recipient': 'you', 'amount': 3})
mine_block(server2) # Mined a new block on server2
get_server_chain(server1) # server1's chain
get_server_chain(server2) # server2's chain
sync_chain(server1) # updating server1's chain with neighbour node's chain
get_server_chain(server1) # server1's chain after syncing
if__name__=="__main__":
class_tests()
api_tests()