This repository was archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockchain.py
More file actions
104 lines (86 loc) · 2.27 KB
/
blockchain.py
File metadata and controls
104 lines (86 loc) · 2.27 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""
Matthew Sabo
2017
blockchain.py
Represents a blockchain
"""
from flask import Flask
from flask import request
import datetime
import json
import hashlib
from block import Block
class Blockchain:
def __init__(self):
self.num_blocks = 0
self.current = "0"
self.blocks = []
self.add_block("Genesis")
self.current = self.blocks[0]
def add_block(self, data):
time = datetime.datetime.now()
new_block = Block(self.num_blocks, time, data, self.current)
self.blocks.append(new_block)
print("Block #{0} added to chain.".format(self.num_blocks))
self.num_blocks += 1
self.current = new_block
chain = Blockchain()
node = Flask(__name__)
txions = []
@node.route('/txion', methods=['POST'])
def transaction():
if (request.method == "POST"):
new_txion = request.get_json()
txions.append(new_txion)
print("New Transaction")
print("From: {0}".format(new_txion['from']))
print("To: {0}".format(new_txion['to']))
print("Amount: {0}".format(new_txion['amount']))
return 1
miner_addr = hashlib.sha256().update(str(input("Name: ")).encode('utf-8'))
def proof_of_work(last_proof):
incrementor = last_proof + 1
while not (incrementor % 9 == 0 and incrementor % last_proof == 0):
incrementor += 1
return incrementor
@node.route('/mine', methods = ['GET'])
def mine():
last_proof = chain.current.data['proof_of_work']
proof = proof_of_work(last_proof)
txions.append(
{"from":"network", "to":miner_addr, "amount":1}
)
new_data = {
"proof_of_work":proof,
"transactions":list(get_txions().data)
}
del txions[:]
get_chain().add_block(new_data)
return 1
@node.route('/blocks', method = ['GET'])
def get_blocks():
chain_to_send = get_chain()
for block in chain_to_send.blocks:
block = {
"index":block.index,
"timestamp":block.timestamp,
"data":block.data,
"hash":block.hash
}
chain_to_send = json.dumps(chain_to_send)
return chain_to_send
def find_new_chains():
other_chains = []
for node_url in peer_nodes:
block = requests.get(node_url+"/blocks").content
block = json.loads(block)
other_chains.append(block)
return other_chains
def consensus():
other_chains = find_new_chains()
longest_chain = chain
for chain in other_chains:
if (chain.num_blocks > longest_chain.num_blocks):
longest_chain = chain
chain = longest_chain
node.run()