-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
161 lines (123 loc) · 5.13 KB
/
main.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from uuid import uuid4
import random #including this for testing data
from database import Transaction, sessionmaker, Block
import datetime
from flask import Flask, jsonify, request
from sqlalchemy import create_engine, Column, Integer, String, DateTime
from blockchain_class import Blockchain
engine = create_engine('sqlite:///blockchain.db') # add , echo = True for the logging
Session = sessionmaker(bind=engine)
session = Session()
Session1 = sessionmaker(bind=engine)
session1 = Session()
# Instantiate the Node
app = Flask(__name__)
# Generate a globally unique address for this node
node_identifier = str(uuid4()).replace('-', '')
# Instantiate the Blockchain
blockchain = Blockchain()
currentTime = datetime.datetime.now()
@app.route('/mine', methods=['GET'])
def mine():
# We run the proof of work algorithm to get the next proof...
last_block = blockchain.last_block
proof = blockchain.proof_of_work(last_block)
# We must receive a reward for finding the proof.
# The sender is "0" to signify that this node has mined a new coin.
# blockchain.new_transaction(
# sender=random.randrange(5, 100000000, 1),
# recipient=node_identifier,
# amount=random.randrange(5, 100000000, 1),
# )
# Forge the new Block by adding it to the chain
index = (session.query(Block.id).order_by(Block.id.desc()).first())[0]+1
previous_hash = blockchain.hash(last_block)
current_block = blockchain.new_block(proof, previous_hash,index)
response = {
'message': "New Block Forged",
# 'index': current_block['index'],
'index': index,
#'index': (session.query(Block.id).order_by(Block.id.desc()).first())[0]+1,
'transactions': current_block['transactions'],
'proof': current_block['proof'],
'previous_hash': current_block['previous_hash'],
}
session1.add(
Block(
id=(session.query(Block.id).order_by(Block.id.desc()).first())[0]+1,
currBlockHash=blockchain.hash(current_block),
prevBlockHash=blockchain.hash(last_block),
timestamp=currentTime
)
)
session1.commit() # id does not increase because of there is no id increasing via insert aka commit
#session.commit()
return jsonify(response), 200
@app.route('/transactions/new', methods=['POST'])
def new_transaction():
values = request.get_json()
# Check that the required fields are in the POST'ed data
required = ['sender', 'recipient', 'amount']
if not all(k in values for k in required):
return 'Missing values', 400
# Create a new Transaction
blockchain.new_transaction('sender pidaras', random.randrange(0,10000,1), random.randrange(0,100000,1))
#blockchain.new_transaction(values['sender'], values['recipient'], values['amount'])
response = {'message': f'Transaction №{str((session.query(Transaction.id).order_by(Transaction.id.desc()).first())[0]+1)} will be added to Block {str((session.query(Block.id).order_by(Block.id.desc()).first())[0]+1)}'}
session.add(
Transaction(
id=(session.query(Transaction.id).order_by(Transaction.id.desc()).first())[0]+1,
#data=values['amount'],
data='hash is const because transaction data is const',
transactionNo= (session.query(Transaction.id).order_by(Transaction.id.desc()).first())[0]+1,
hash=blockchain.hash(str(values['sender'])+str(values['recipient'])+ str(values['amount'])+str((session.query(Transaction.id).order_by(Transaction.id.desc()).first())[0]+1)),
prevHash='#####',#how to get prev hash?
timestamp=currentTime,
#proof=current_block['proof'],
nID_block=(session.query(Block.id).order_by(Block.id.desc()).first())[0]+1
)
)
session.commit()
return jsonify(response), 201
@app.route('/chain', methods=['GET'])
def full_chain():
response = {
'length': len(blockchain.chain),
'chain': blockchain.chain
}
print(blockchain)
return jsonify(response), 200
@app.route('/nodes/register', methods=['POST'])
def register_nodes():
values = request.get_json()
nodes = values.get('nodes')
if nodes is None:
return "Error: Please supply a valid list of nodes", 400
for node in nodes:
blockchain.register_node(node)
response = {
'message': 'New nodes have been added',
'total_nodes': list(blockchain.nodes),
}
return jsonify(response), 201
@app.route('/nodes/resolve', methods=['GET'])
def consensus():
replaced = blockchain.resolve_conflicts()
if replaced:
response = {
'message': 'Our chain was replaced',
'new_chain': blockchain.chain
}
else:
response = {
'message': 'Our chain is authoritative',
'chain': blockchain.chain
}
return jsonify(response), 200
if __name__ == '__main__':
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('-p', '--port', default=5000, type=int, help='port to listen on')
args = parser.parse_args()
port = args.port
app.run(host='0.0.0.0', port=port)