This repository has been archived by the owner on May 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgosig.py
177 lines (127 loc) · 5.14 KB
/
gosig.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 4 14:09:35 2019
@author: gaurava
"""
import configparser
import logging
import os
from uuid import uuid4
from flask import Flask, request
from flask.logging import default_handler
from blockchain import *
from consensus import *
os.system("clear")
# Instantiate the Node
app = Flask(__name__)
# Generate a globally unique address for this node
node_identifier = str(uuid4()).replace('-', '')
@app.route('/balance', methods=['GET'])
def balance():
response = {
'utxo': blockchain.UTXO
}
return jsonify(response), 200
@app.route('/check_conflict', methods=['POST'])
def check_conflicts():
values = request.get_json()
return GoSigConsensus.check_conflict(blockchain, logger, values)
@app.route('/proposed', methods=['POST'])
def check_proposed():
values = request.get_json()
return GoSigConsensus.check_proposed(blockchain, logger, values)
@app.route('/check', methods=['GET'])
def check():
response = {
'isValid': list(blockchain.nodes),
'pub_key_list': blockchain.public_key_list,
'txn': blockchain.current_transactions,
'round_number': str(blockchain.roundNumber),
}
return jsonify(response), 200
@app.route('/commit', methods=['POST'])
def commit():
values = request.get_json()
return GoSigConsensus.commit(blockchain, logger, values)
@app.route('/chain', methods=['GET'])
def full_chain():
response = {
'chain': blockchain.chain,
'Hash': blockchain.Hash,
}
return jsonify(response), 200
@app.route('/fetch/txn', methods=['POST'])
def fetch_transaction():
values = request.get_json()
return GoSigConsensus.fetch_transaction(blockchain, values)
@app.route('/transactions/new', methods=['POST'])
def new_transaction():
values = request.get_json()
return GoSigConsensus.new_transaction(blockchain, logger, values)
@app.route('/ping', methods=['POST'])
def ping():
values = request.get_json()
return GoSigConsensus.ping(blockchain, logger, values)
@app.route('/reply_proposal', methods=['POST'])
def reply_proposed():
values = request.get_json()
return GoSigConsensus.reply_proposed(blockchain, logger, values)
@app.route('/reply_commit', methods=['POST'])
def reply_commit():
values = request.get_json()
return GoSigConsensus.reply_commit(blockchain, logger, values)
@app.route('/nodes/register', methods=['POST'])
def register_nodes():
values = request.get_json()
return GoSigConsensus.register_nodes(blockchain, logger, values)
@app.route('/verified_commit', methods=['POST'])
def verified_commit():
values = request.get_json()
return GoSigConsensus.verified_commit(blockchain, logger, values)
def read_config(_blockchain):
config = configparser.RawConfigParser()
config.read('configuration.properties')
_blockchain.round_time = float(config.get('time', 'round_time'))
_blockchain.sign_proposed_block_delay = float(config.get('time', 'sign_proposed_block_delay'))
_blockchain.propose_block_check = float(config.get('time', 'propose_block_check'))
_blockchain.sign_commit_block_delay = float(config.get('time', 'sign_commit_block_delay'))
_blockchain.commit_delay = float(config.get('time', 'commit_delay'))
_blockchain.min_participants = float(config.get('consensus', 'min_participants'))
_blockchain.leader_hash_check = config.get('consensus', 'leader_hash_check')
def setup_logger(name, log_file, _formatter, _level=logging.INFO):
"""Function setup as many loggers as you want"""
handler = logging.FileHandler(log_file)
handler.setFormatter(_formatter)
_logger = logging.getLogger(name)
_logger.setLevel(_level)
_logger.addHandler(handler)
return _logger
if __name__ == '__main__':
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('-p', '--port', default=5002, type=int, help='port to listen on')
args = parser.parse_args()
port = args.port
# Instantiate the Blockchain
LOG_FILENAME = "logFiles/" + str(port) + "_1" + time.strftime("-%m%d-%H%M") + ".log"
block_LOG_FILENAME = "logFiles/" + str(port) + time.strftime("-%m%d-%H%M") + ".log"
if not os.path.isdir("logFiles"):
os.makedirs("logFiles")
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
LOG_FILENAME = os.path.join(os.path.dirname(os.path.realpath(__file__)), LOG_FILENAME)
block_LOG_FILENAME = os.path.join(os.path.dirname(os.path.realpath(__file__)), block_LOG_FILENAME)
# first file logger
logger = setup_logger('flask_logger', LOG_FILENAME, formatter, _level=logging.DEBUG)
# second file logger
block_logger = setup_logger('block_logger', block_LOG_FILENAME, formatter, _level=logging.INFO)
logger.debug("Author : gaurav agarwal")
logger.debug("Application : GoSig Consensus")
logger.debug("")
logger.info('This is a debug message')
blockchain = Blockchain(port, block_logger)
blockchain_thread = threading.Thread(target=blockchain.leader_process)
blockchain_thread.setDaemon(True)
blockchain_thread.start()
read_config(blockchain)
app.run(host='0.0.0.0', port=port)