2
0
Эх сурвалжийг харах

Add ability to find transactions per address

DebenOldert 4 жил өмнө
parent
commit
6d5658abff
6 өөрчлөгдсөн 45 нэмэгдсэн , 10 устгасан
  1. 9 1
      lib/address.py
  2. 5 0
      lib/block.py
  3. 9 1
      lib/chain.py
  4. 2 5
      lib/contract.py
  5. 5 1
      lib/transaction.py
  6. 15 2
      main.py

+ 9 - 1
lib/address.py

@@ -1,2 +1,10 @@
 class Address:
-    pass
+    def __init__(self):
+        self.public = None
+        self.private = None
+
+    def from_public(self, addr):
+        self.public = addr
+
+    def is_owned(self):
+        return self.private is not None

+ 5 - 0
lib/block.py

@@ -28,6 +28,11 @@ class Block:
         self.timestamp = 0
         self.height = previous.height + 1 if previous is not None else 0
 
+    def add_hashed_transaction(self, hashed: str):
+        self.transactions.append(hashed)
+        self.log(f'Transaction added: {hashed}')
+        return True
+
     def add_transaction(self, trans: Transaction):
         if not self.locked:
             trans.lock_hash_finish()

+ 9 - 1
lib/chain.py

@@ -12,4 +12,12 @@ class Chain:
         return self.blocks[-1]
 
     def get_height(self):
-        return self.last_block().height
+        return self.last_block().height
+
+    def find_transactions(self, address):
+        found = []
+        for block in self.blocks:
+            for transaction in block.transactions:
+                if transaction.sender == address or transaction.receiver == address:
+                    found.append(transaction)
+        return found

+ 2 - 5
lib/contract.py

@@ -11,10 +11,6 @@ class Contract(object):
             self.description = None
             self.deadline = None
 
-            self.modified_by = None
-            self.last_comment = None
-            self.state = None
-
         def create(self, title: str, desc: str, deadline: int):
             self.id = str(uuid.uuid1())
             self.title = title
@@ -60,7 +56,8 @@ class Contract(object):
         self.initial_block = None
 
         self.modified_by = None
-        self.comment = None
+        self.last_comment = None
+        self.state = None
 
     def add_term(self, term: Term):
         self.log(f'Term added: {term.id} | {term.title}')

+ 5 - 1
lib/transaction.py

@@ -13,6 +13,8 @@ class Transaction:
         self.hash_value = None
         self.locked = False
         self.timestamp = 0
+        self.sender = None
+        self.receiver = None
 
     def set_serialized_contract(self, serialized):
         if not self.locked:
@@ -34,7 +36,9 @@ class Transaction:
         obj = {
             'id': self.id,
             'timestamp': self.timestamp,
-            'data': self.data
+            'data': self.data,
+            'sender': self.sender,
+            'receiver': self.receiver
         }
         self.log(f'Serialized')
         if out_json:

+ 15 - 2
main.py

@@ -1,3 +1,4 @@
+import json
 import time
 
 import rpyc
@@ -5,6 +6,7 @@ from rpyc.utils.server import ThreadPoolServer
 import argparse
 import threading
 
+from lib.address import Address
 from lib.chain import Chain, Block
 from lib.transaction import Transaction
 from lib.contract import Contract
@@ -97,6 +99,19 @@ if __name__ == '__main__':
                 global CURRENT
                 return CURRENT
 
+            def exposed_push_transaction(self, hashed: str):
+                global CURRENT
+                CURRENT.add_hashed_transaction(hashed)
+                return True
+
+            def exposed_find_transactions(self, addr: str):
+                address = Address()
+                address.from_public(addr=addr)
+
+                found = chain.find_transactions(address)
+
+                return json.dumps(found, sort_keys=True)
+
             def exposed_push_contract(self, contract):
                 global CURRENT
                 trans = Transaction()
@@ -114,7 +129,5 @@ if __name__ == '__main__':
         server.start()
 
 
-
-
 # Possible TODO's:
 # * Upon adding transaction to block check if transaction is already locked