Ver Fonte

Merge branch 'main' of https://github.com/DebenOldert/dsp-blockchain into main

justtheboss97 há 4 anos atrás
pai
commit
e7e983f933
5 ficheiros alterados com 57 adições e 14 exclusões
  1. 2 0
      lib/address.py
  2. 1 1
      lib/block.py
  3. 0 1
      lib/chain.py
  4. 46 3
      lib/contract.py
  5. 8 9
      main.py

+ 2 - 0
lib/address.py

@@ -0,0 +1,2 @@
+class Address:
+    pass

+ 1 - 1
lib/block.py

@@ -8,7 +8,7 @@ from lib.transaction import Transaction
 class Block:
     def __init__(self, previous=None):
         self.proof = None
-        self.previous_hash = previous.hash if previous is not None else None
+        self.previous_hash = previous.hash_value if previous is not None else None
         self.transactions = []
         self.hash_value = None
         self.locked = False

+ 0 - 1
lib/chain.py

@@ -3,7 +3,6 @@ from lib.block import Block
 
 class Chain:
     def __init__(self):
-        self.pending_transactions = []
         self.blocks = []
 
     def add_block(self, block: Block):

+ 46 - 3
lib/contract.py

@@ -1,4 +1,6 @@
 import json
+import uuid
+from lib.address import Address
 
 
 class Contract(object):
@@ -9,14 +11,28 @@ class Contract(object):
             self.description = None
             self.deadline = None
 
-        def set_term(self, title: str, desc: str, deadline: int):
+            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
             self.description = desc
             self.deadline = deadline
-            self.log(f'Set: {self.title}')
+            self.log(f'Created: {self.title}')
+
+        def update(self, key: str, value, by: Address, comment: str):
+            if hasattr(self, key):
+                setattr(self, key, value)
+                self.modified_by = by
+                self.last_comment = comment
+                return True
+            return False
 
         def serialize(self):
             obj = {
+                '@type': 'term',
                 'id': self.id,
                 'title': self.title,
                 'description': self.description,
@@ -39,11 +55,35 @@ class Contract(object):
         self.people = None
         self.initiator = None
         self.terms = []
+        self.initial_block = None
+
+        self.modified_by = None
+        self.comment = None
 
     def add_term(self, term: Term):
         self.log(f'Term added: {term.id} | {term.title}')
         self.terms.append(term)
 
+    def update(self, key: str, value, by: Address, comment: str):
+        if self.has_attribute(key):
+            self.log(f'Updating {key}. {getattr(self, key)} => {value}')
+            setattr(self, key, value)
+            self.modified_by = by
+            self.comment = comment
+            return True
+        return False
+
+    def create(self, title: str, desc: str, deadline: int, price: float, **kwargs):
+        if self.id is None:
+            self.id = str(uuid.uuid1())
+            self.title = title
+            self.description = desc
+            self.deadline = deadline
+            self.price = price
+            self.log(f'Contract created: {self.id}')
+            return True
+        return False
+
     def serialize(self):
         # TODO: Add all properties
         term_serialize = []
@@ -58,4 +98,7 @@ class Contract(object):
         return obj
 
     def log(self, text):
-        print(f'[ CONTR ] {text}')
+        print(f'[ CONTR ] {text}')
+
+    def has_attribute(self, key):
+        return hasattr(self, key)

+ 8 - 9
main.py

@@ -24,21 +24,20 @@ if __name__ == '__main__':
     # Chain work starts from here
 
     # Static chain interaction
-    block1 = Block()
-    block1.previous_hash = chain.last_block().hash_value
+    block1 = Block(previous=initial)
 
     # Create a new contract
     contract1 = Contract()
-    contract1.title = 'Digital legal handshake'
-    contract1.description = 'Hereby you declare to fulfill the following terms'
-    contract1.deadline = int(time.time())
-    contract1.price = 420
+    contract1.create(title='Digital legal handshake',
+                     desc='Hereby you declare to fulfill the following terms',
+                     deadline=int(time.time()),
+                     price=420.69)
 
     # Create a new term
     term1 = Contract.Term()
-    term1.title = 'U gotta work'
-    term1.description = 'Finish this'
-    term1.deadline = int(time.time())
+    term1.create(title='U gotta work',
+                 desc='Finish this',
+                 deadline=int(time.time()))
 
     # Add term to contract1
     contract1.add_term(term=term1)