Kaynağa Gözat

Prepare for json LD

DebenOldert 4 yıl önce
ebeveyn
işleme
db30c2cc09
5 değiştirilmiş dosya ile 35 ekleme ve 19 silme
  1. 1 2
      README.md
  2. 9 8
      gui/gui_helpers.py
  3. 20 3
      lib/chain.py
  4. 3 6
      lib/transaction.py
  5. 2 0
      main.py

+ 1 - 2
README.md

@@ -5,8 +5,7 @@ This is a small description of how the chain works and how data is being formatt
 ### Transaction stored in chain
 ```json
 {
-  "sender": "public key sender",
-  "receiver": "public key receiver",
+  "key": "public key of the contract",
   "id": "transaction id",
   "timestamp": "unix timestamp of transaction creation",
   "data": "encrypted string of contract"

+ 9 - 8
gui/gui_helpers.py

@@ -20,9 +20,9 @@ def add_contract():
                          desc=description.get(),
                          deadline=int(deadline.get()),
                          price=float(price.get()),
-                         state = False,
-                         client=s,
-                         contractor=r)
+                         state=False,
+                         client=str(s),
+                         contractor=str(r))
 
 
         add_term(contract1)
@@ -32,8 +32,8 @@ def add_contract():
         def process_term():
             term1 = Contract.Term()
             term1.create(title=title2.get(),
-                     desc=description2.get(),
-                     deadline=int(deadline2.get()))
+                         desc=description2.get(),
+                         deadline=int(deadline2.get()))
 
             contract1.add_term(term=term1)
 
@@ -52,10 +52,11 @@ def add_contract():
             transaction1.timestamp = time.time()
 
             # keys need to be in plain text, otherwise the json dump fucks up
-            transaction1.sender = get_keys(sender.get())
-            transaction1.receiver = get_keys(recipient.get())
-            transaction1.data = transaction1.hash_value
+            transaction1.sender = str(get_keys(sender.get()))
+            transaction1.receiver = str(get_keys(recipient.get()))
+            transaction1.data = str(transaction1.hash_value)
             output = transaction1.serialize()
+            output = json.dumps(output, sort_keys=True)
             print(output, type(output))
 
             conn.root.push_transaction(output)           

+ 20 - 3
lib/chain.py

@@ -1,23 +1,40 @@
+import json
+
 from lib.block import Block
 
 
 class Chain:
     def __init__(self):
         self.blocks = []
+        self.block_store = []
 
     def add_block(self, block: Block):
+        self.save()
         self.blocks.append(block)
+        self.log(f'Added block. Height: {block.height}')
 
     def last_block(self):
-        return self.blocks[-1]
+        return self.blocks[-1] if len(self.blocks) > 0 else None
 
     def get_height(self):
-        return self.last_block().height
+        return self.last_block().height if self.last_block() is not None else None
 
     def find_transactions(self, address):
         found = []
         for block in self.blocks:
             for transaction in block.transactions:
-                if transaction.sender == address or transaction.receiver == address:
+                if transaction.key == address.public:
                     found.append(transaction)
         return found
+
+    def save(self):
+        if self.last_block() is not None:
+            self.block_store.append(self.last_block().serialize())
+
+            with open('data/block.json', 'w+') as f:
+                json.dump(self.block_store, f)
+
+            self.log(f'Saved up to block {self.get_height()}')
+
+    def log(self, text):
+        print(f'[ CHAIN ] {text}')

+ 3 - 6
lib/transaction.py

@@ -17,16 +17,14 @@ class Transaction:
         self.hash_value = None
         self.locked = False
         self.timestamp = 0
-        self.sender = None
-        self.receiver = None
+        self.key = None
 
     def from_serialized(self, jsonobj: str):
         obj = json.loads(jsonobj)
         self.id = obj['id']
         self.data = obj['data']
         self.timestamp = obj['timestamp']
-        self.sender = obj['sender']
-        self.receiver = obj['receiver']
+        self.key = obj['key']
 
     def set_serialized_contract(self, serialized):
         if not self.locked:
@@ -49,8 +47,7 @@ class Transaction:
             'id': self.id,
             'timestamp': self.timestamp,
             'data': self.data,
-            'sender': self.sender,
-            'receiver': self.receiver
+            'key': self.key,
         }
         self.log(f'Serialized')
         if out_json:

+ 2 - 0
main.py

@@ -79,6 +79,7 @@ if __name__ == '__main__':
     else:
         chain = Chain()
         CURRENT = initial
+        chain.add_block(initial)
 
         class Looper(threading.Thread):
             def __init__(self):
@@ -92,6 +93,7 @@ if __name__ == '__main__':
                     print(f'[ THR ] Work on new block. Height: {CURRENT.height}')
                     CURRENT.lock_hash_finish()
                     CURRENT = Block(previous=CURRENT)
+                    chain.add_block(CURRENT)
 
 
         class Service(rpyc.Service):