-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
249 lines (226 loc) · 9.27 KB
/
script.py
File metadata and controls
249 lines (226 loc) · 9.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import random
import pygithub3
import git
from git import Repo
import os
import re
import threading
import mysql.connector
import time
db_config = {
'user' : '',
'password' : '',
'database' : '',
'host' : '',
'port' : ''
}
def get_verified_nodes():
db_connection = mysql.connector.connect(**db_config)
cursor = db_connection.cursor()
query = "SELECT * FROM information_schema.tables WHERE TABLE_TYPE = 'BASE TABLE'"
cursor.execute(query, (db_config['database']))
result = cursor.fetchall()
out = []
for table in result:
out.append(table[2])
return out
VERIFIED_NODES = get_verified_nodes()
def get_local_git_user():
if not os.path.isdir('.git'):
bare_repo = Repo.init('start',bare=True)
repo = Repo('start')
conf_reader = repo.config_reader()
return conf_reader.get_value('user', 'name')
def new_node_table_exists():
db_connection = mysql.connector.connect(**db_config)
cursor = db_connection.cursor()
query = "SELECT * FROM information_schema.tables WHERE TABLE_TYPE = 'BASE TABLE'"
cursor.execute(query, (db_config['database']))
result = cursor.fetchall()
out = False
for table in result:
if table[2] in VERIFIED_NODES:
out = True
return out
def create_git_repo_init():
gh = None
print "enter your username"
username = raw_input()
print "enter your password"
password = raw_input()
auth = dict(login=username, password=password)
if not os.path.isdir("start/Node"):
os.makedirs("start/Node")
username = get_local_git_user()
gh = pygithub3.Github(token = '')
repo_name = 'gitcoin'
gh.repos.create(dict(name=repo_name, description='desc'))
#repos = gh.create_repo(repo_name)
#random_node = random.choice(VERIFIED_NODES)
cloneUrl='https://github.com/karimchukfeh/gitcoin.git'
localRepopath = 'start/Node/'
repo = Repo.clone_from(cloneUrl, localRepopath)
another_url = 'https://github.com/'+username+'/gitcoin.git'
remote = repo.create_remote(repo_name, url=another_url)
remote.push()
create_new_node_table(username)
return True
def transaction_verification():
resp = ""
while(resp == ""):
resp = get_new_transaction_query()
if(verify_sender(resp[1],resp[3]) and verify_receiver(resp[2])):
db_connection = mysql.connector.connect(**db_config)
cursor = db_connection.cursor()
query = "UPDATE "+ db_config['database'] + "." +get_local_git_user() +" status = `confirmed` WHERE transaction_id = "+str(resp[0])
cursor.execute(query, (db_config['database']))
db_connection.close()
return True
else:
return False
def clone_repo(clone_username,type):
dir_name = "clone_"+type+"_"+clone_username
if not os.path.isdir(dir_name):
os.makedirs(dir_name)
cloneUrl="https://github.com/"+str(clone_username)+"/gitcoin.git"
localRepopath = dir_name+"/"
repo = Repo.clone_from(cloneUrl, localRepopath)
return repo
def verify_sender(sender, amount):
print "verifying sender "+ sender
repo_clone = clone_repo(sender,"sender_verif")
repo = Repo('start/Node')
print "verifying repo sender"+ sender
if(not(verif_commit_clone(repo,repo_clone))):
print "verifying crash repo sender"+ sender
return False
print "verifying balance of "+ sender
#if(not(verify_amount(amount,repo_clone))):
#return False
print "verifying balance of "+ sender+" success"
print "verifying success of "+ sender
return True
def verify_amount(amount,repo):
commits = list(repo.iter_commits())
user_name = get_local_git_user()
commits = [commit for commit in commits if user_name in commit.message]
if len(commits) > 0:
#Make sure all transactions involving current user add up
#and there are enough funds for the transaction to be made
sum = 0
transactions = []
for commit in commits:
if commit.message != '':
arr = commit.message.split('-')
if user_name in arr[0]:
sum -= int(arr[2])
transactions.append(commit)
elif user_name in arr[1]:
sum += int(arr[2])
if(amount>sum):
return False
return True
def verify_receiver(receiver):
print "verifying reciever "+ receiver
repo_clone = clone_repo(receiver,"receiver_verif")
repo = Repo('start/Node')
print "verifying repo reciever"+ receiver
if(not(verif_commit_clone(repo,repo_clone))):
return False
return True
def verif_commit_clone(repo,repo_clone):
commits_repo = list(repo.iter_commits('master'))
print(len(commits_repo))
commits_cloned_repo = list(repo_clone.iter_commits('master'))
print(len(commits_cloned_repo))
for commit in range(len(commits_repo)-1):
print(commits_repo[commit].hexsha)
print(commits_cloned_repo[commit].hexsha)
return True
def get_new_transaction_query():
db_connection = mysql.connector.connect(**db_config)
cursor = db_connection.cursor()
query = "SELECT * FROM "+ db_config['database'] + "." + get_local_git_user()
cursor.execute(query, (db_config['database']))
result = cursor.fetchall()
db_connection.close()
# TODO exec query get result
if(len(result)>0):
last = result[-1]
if last[-1] == 'broadcasted':
db_connection.close()
return last
else:
db_connection.close()
return ""
else:
db_connection.close()
return ""
def create_new_node_table(username):
db_connection = mysql.connector.connect(**db_config)
cursor = db_connection.cursor()
tableName = username.encode('utf8', 'replace')
query = "CREATE TABLE " + username + " (`transaction_id` VARCHAR(100) NOT NULL, `sender` VARCHAR(100) NULL, `reciever` VARCHAR(100) NULL, `amount` INT(100) NULL, `status` VARCHAR(45) NULL DEFAULT 'broadcasted', PRIMARY KEY (`transaction_id`));"
cursor.execute(query, (tableName))
db_connection.close()
return True
def node_broadcast():
repo = Repo('start/Node')
user_name = get_local_git_user()
commits = list(repo.iter_commits())
commits = [commit for commit in commits if user_name in commit.message]
if len(commits) > 0:
#Make sure all transactions involving current user add up
#and there are enough funds for the transaction to be made
sum = 0
transactions = []
for commit in commits:
if commit.message != '':
arr = commit.message.split('-')
if user_name in arr[0]:
sum -= int(arr[2])
transactions.append(commit)
elif user_name in arr[1]:
sum += int(arr[2])
if sum < 0:
print "Not Enough Funds for Transaction"
else:
for transaction in transactions:
f = open('log', 'a+')
#checks if the commit has already been broadcast
if ('BROADCASTING: ' + transaction.hexsha) not in f.read():
db_connection = mysql.connector.connect(**db_config)
cursor = db_connection.cursor()
tableName = username.encode('utf8', 'replace')
f.write('BROADCASTING: ' + transaction.hexsha + '\n')
split_transaction = transaction.message.split('-')
broadcast_targets = []
#broadcast to all nodes
for node in VERIFIED_NODES:
if node != split_transaction[0] or node != split_transaction[1]:
target_nodes.push(node)
query = 'INSERT INTO ' + node + ' (transaction_id, sender, reciever, amount) VALUES (`{0}`, `{1}`, `{2}`, `{4}`)'.format(transaction.hexsha, split-commit[0], split-commit[1], split_transaction[2])
cursor.execute(query, (tableName))
#wait for all nodes to confirm broadcast
confirmed_by_all = False
while len(broadcast_targets) > 0:
for target in broadcast_targets:
query = 'SELECT status FROM ' + target + 'WHERE transaction_id = ' + transaction.hexsha
cursor.execute(query, (tableName))
result = cursor.fetchall()
for status in result:
if status != 'broadcasted':
del broadcast_target[target]
time.sleep(30)
cursor.close()
db_connection.close()
f.close()
if __name__ == '__main__':
print "hi"
if not new_node_table_exists():
print "hi1"
if create_git_repo_init():
t1 = threading.Thread(target = node_broadcast())
t2 = threading.Thread(target = transaction_verification())
t1.start()
t2.start()