Просмотр исходного кода

adding terms and updates now works, accepting these works too

justtheboss97 4 лет назад
Родитель
Сommit
59e8937a18
4 измененных файлов с 119 добавлено и 35 удалено
  1. 0 0
      data/block.json
  2. 2 1
      gui.py
  3. 116 33
      gui/data.py
  4. 1 1
      gui/encyption.py

Разница между файлами не показана из-за своего большого размера
+ 0 - 0
data/block.json


+ 2 - 1
gui.py

@@ -2,7 +2,7 @@ from tkinter import *
 from linked_data import Chain, format_dict
 import pprint
 # from gui.gui_helpers import add_contract
-from gui.data import new_contract, find_transaction, add_term
+from gui.data import new_contract, find_transaction, add_term, accept_updates
 
 from lib.transaction import Transaction
 from lib.contract import Contract
@@ -29,6 +29,7 @@ root.title('dsp blockchain')
 Button(root, text="New contract", command=new_contract).grid(row=1, column=0, padx=10, pady=5)
 Button(root, text="Find contract", command=find_transaction).grid(row=1, column=1, padx=10, pady=5)
 Button(root, text="Add term", command=add_term).grid(row=1, column=2, padx=10, pady=5)
+Button(root, text="Accept updates", command=accept_updates).grid(row=1, column=3, padx=10, pady=5)
 
 
 root.mainloop()

+ 116 - 33
gui/data.py

@@ -133,7 +133,7 @@ def add_term():
                 'updated':attr,
                 'type':t,
                 'last update':time.time(),
-                'accepted': True,
+                'accepted': False,
                 'change':{attr:value}}
         
         # check if comment is passed and add it if necassary
@@ -146,20 +146,20 @@ def add_term():
         conn.root.push_transaction(t.serialize(out_json=True))
 
         #### TEST CODE PLEASE IGONRE ####
-        update = {
-            'id':iden,
-            'update id':0,
-            'accept':True,
-            'type':'accept',
-            'last update':time.time()
-        }
-        if comment:
-            update['comment'] = comment
-
-        t = Transaction()
-        t.set_contract(update)
-        t.hash(contract=str(contract.get()))
-        conn.root.push_transaction(t.serialize(out_json=True))
+        # update = {
+        #     'id':iden,
+        #     'update id':0,
+        #     'accept':True,
+        #     'type':'accept',
+        #     'last update':time.time()
+        # }
+        # if comment:
+        #     update['comment'] = comment
+
+        # t = Transaction()
+        # t.set_contract(update)
+        # t.hash(contract=str(contract.get()))
+        # conn.root.push_transaction(t.serialize(out_json=True))
 
         #### END TEST CODE PLEASE DONT IGONRE ####
 
@@ -173,7 +173,7 @@ def add_term():
     Label(newWindow, text='Contract: ').grid(row=0, column=0)
     contract = Entry(newWindow, width=35, borderwidth=5)
     contract.grid(row=0, column=1, columnspan=3, padx=10, pady=10)
-    contract.insert(0, '1')
+    contract.insert(0, 'test')
 
     Label(newWindow, text='What to change or add: ').grid(row=1, column=0)
     change = Entry(newWindow, width=35, borderwidth=5)
@@ -185,13 +185,87 @@ def add_term():
     values.grid(row=2, column=1, columnspan=3, padx=10, pady=10)
     values.insert(0, '50%')
 
-    Label(newWindow, text='comment (optional): ').grid(row=2, column=0)
+    Label(newWindow, text='comment (optional): ').grid(row=3, column=0)
     comments = Entry(newWindow, width=35, borderwidth=5)
     comments.grid(row=3, column=1, columnspan=3, padx=10, pady=10)
     comments.insert(0, 'dit is een comment')
 
     Button(newWindow, text='Add term', command=add).grid(row=4, column=1)
 
+def accept_updates():
+    def show():
+        conn = rpyc.connect(host='localhost', port=42069, keepalive=True)
+        name = str(contract.get())
+        transactions = conn.root.find_transactions(get_plain_key(name))
+        transactions = decrypt_transactions(transactions, name)
+        updates = retrieve_updates(transactions, name, accepted=False)
+        conn.close()
+        if len(updates) == 0:
+            print('No updates to accept')
+        else:
+            print('Updates that still need to be accepted: \n')
+            for update in updates:
+                print(update, '\n')
+
+    def accept():
+        conn = rpyc.connect(host='localhost', port=42069, keepalive=True)
+        name = str(contract.get())
+        update_id = int(change.get())
+        answer = accept_status.get()
+        if answer == 'True':
+            answer = True
+        else: answer = False
+        print(answer)
+        comment_text = str(comment.get())
+
+        print(answer)
+
+        update = {
+            'id':get_plain_key(name),
+            'update id':update_id,
+            'accept':answer,
+            'type':'accept',
+            'last update':time.time()
+        }
+        if comment_text:
+            update['comment'] = comment_text
+
+        t = Transaction()
+        t.set_contract(update)
+        t.hash(contract=str(contract.get()))
+        conn.root.push_transaction(t.serialize(out_json=True))
+        conn.close()
+        newWindow.destroy()
+
+
+    newWindow = Tk()
+    newWindow.title('Accept updates')
+
+    Label(newWindow, text='Contract: ').grid(row=0, column=0)
+    contract = Entry(newWindow, width=35, borderwidth=5)
+    contract.grid(row=0, column=1, columnspan=3, padx=10, pady=10)
+    contract.insert(0, 'test')
+
+    Label(newWindow, text='Id of update: ').grid(row=1, column=0)
+    change = Entry(newWindow, width=35, borderwidth=5)
+    change.grid(row=1, column=1, columnspan=3, padx=10, pady=10)
+    change.insert(0, '0')
+
+    Label(newWindow, text='Awnser: ').grid(row=2, column=0)
+    accept_status = StringVar(newWindow)
+    accept_status.set('False') # default value
+    OptionMenu(newWindow, accept_status, 'False', 'True').grid(row=2, column=1)
+
+    Label(newWindow, text='Comment (optional): ').grid(row=3, column=0)
+    comment = Entry(newWindow, width=35, borderwidth=5)
+    comment.grid(row=3, column=1, columnspan=3, padx=10, pady=10)
+    comment.insert(0, 'test test')
+
+
+    Button(newWindow, text='Show updates to accept', command=show).grid(row=4, column=0, padx=10, pady=5)
+    Button(newWindow, text='Accept', command=accept).grid(row=4, column=1, padx=10, pady=5)
+
+
 
 def current_contract_state(transactions, iden):
     # set up contract state
@@ -227,6 +301,7 @@ def current_contract_state(transactions, iden):
                 item = updates[item['update id']]
             
             # set attr to whatever has been updated
+            print(item)
             attr = item['updated']
             
             # check if the update was removal
@@ -297,20 +372,28 @@ def get_updates(transactions, iden=None, attr=None, accepted=None):
                     updates.append(item)
     return updates
 
+
+def retrieve_updates(all_updates, iden, accepted=None):
+
+    updates = {}
+    
+    for item in all_updates:
+        print(item)
+        if item['type'] == 'init':
+            continue
+        if item['type'] != 'accept' and item['updated'] != 'comments':
+            updates[item['update id']] = item
+        else:
+            try:
+                updates[item['update id']]['accepted'] = item['accept']
+            except:
+                pass
+    
+    if accepted == None:
+        return updates 
+    
+    return [updates[x] for x in list(updates.keys()) if updates[x]['accepted'] == accepted]
+
+
 def decrypt_transactions(transactions, name):
-    decrypted_data = [json.loads(decrypt_data(name, x)) for x in transactions]
-    print(type(decrypted_data[0]))
-
-    # old code
-    # for encrypted in transactions:
-    #     decrypting_message = private_key.decrypt(
-    #         encrypted,
-    #         padding.OAEP(
-    #             mgf=padding.MGF1(algorithm=hashes.SHA256()),
-    #             algorithm=hashes.SHA256(),
-    #             label=None
-    #         )
-    #     )
-    #     decrypted.append(decrypting_message)
-
-    return decrypted_data
+    return [json.loads(decrypt_data(name, x)) for x in transactions]

+ 1 - 1
gui/encyption.py

@@ -27,5 +27,5 @@ def decrypt_data(name, t):
     key = rsa.decrypt(en_key, private_key)
     cipher = Fernet(key)
     decrypted_data = cipher.decrypt(bytes(t.data, 'utf-8'))
-    print(decrypted_data)
+    # print(decrypted_data)
     return decrypted_data.decode('utf8').replace("'", '"')

Некоторые файлы не были показаны из-за большого количества измененных файлов