|
|
@@ -1,67 +1,120 @@
|
|
|
import time
|
|
|
-from rpyc.utils.server import ThreadedServer
|
|
|
+
|
|
|
+import rpyc
|
|
|
+from rpyc.utils.server import ThreadPoolServer
|
|
|
+import argparse
|
|
|
+import threading
|
|
|
|
|
|
from lib.chain import Chain, Block
|
|
|
-from lib.rpc import RPC
|
|
|
from lib.transaction import Transaction
|
|
|
from lib.contract import Contract
|
|
|
+from lib.block import CURRENT
|
|
|
+
|
|
|
+
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
- # Initialize the chain (Creates empty chain)
|
|
|
- chain = Chain()
|
|
|
+ parser = argparse.ArgumentParser(description='DSP Blockchain')
|
|
|
+ mode = parser.add_mutually_exclusive_group()
|
|
|
+ mode.add_argument('--daemon', action='store_true', help='Run in daemon mode')
|
|
|
+ mode.add_argument('--gui', action='store_true', help='Run in GUI mode')
|
|
|
+
|
|
|
+ parser.add_argument('--static', action='store_true', help='Test run the static chain')
|
|
|
+
|
|
|
+ args = parser.parse_args()
|
|
|
+
|
|
|
+ if args.gui:
|
|
|
+ print('Not yest implemented')
|
|
|
+ exit(1)
|
|
|
|
|
|
# Initialize the genesis block
|
|
|
initial = Block()
|
|
|
initial.previous_hash = 'This is the founding block for our DSP project'
|
|
|
initial.proof = 69
|
|
|
|
|
|
- # Lock block
|
|
|
- initial.lock_hash_finish()
|
|
|
+ if args.static:
|
|
|
+ # Initialize the chain (Creates empty chain)
|
|
|
+ chain = Chain()
|
|
|
+
|
|
|
+ # Lock block
|
|
|
+ initial.lock_hash_finish()
|
|
|
+
|
|
|
+ # Add the genesis block to the chain
|
|
|
+ chain.add_block(block=initial)
|
|
|
+
|
|
|
+ # Chain work starts from here
|
|
|
+
|
|
|
+ # Static chain interaction
|
|
|
+ block1 = Block(previous=initial)
|
|
|
+
|
|
|
+ # Create a new contract
|
|
|
+ contract1 = Contract()
|
|
|
+ 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.create(title='U gotta work',
|
|
|
+ desc='Finish this',
|
|
|
+ deadline=int(time.time()))
|
|
|
+
|
|
|
+ # Add term to contract1
|
|
|
+ contract1.add_term(term=term1)
|
|
|
+
|
|
|
+ # Bind contract to transaction
|
|
|
+ transaction1 = Transaction()
|
|
|
+ transaction1.set_contract(contract=contract1)
|
|
|
+
|
|
|
+ # Add transaction to current block
|
|
|
+ block1.add_transaction(trans=transaction1)
|
|
|
+
|
|
|
+ # Finish block and all its transactions
|
|
|
+ block1.lock_hash_finish()
|
|
|
|
|
|
- # Add the genesis block to the chain
|
|
|
- chain.add_block(block=initial)
|
|
|
+ block2 = Block(previous=block1)
|
|
|
+ exit(0)
|
|
|
+ else:
|
|
|
+ chain = Chain()
|
|
|
+ CURRENT = initial
|
|
|
|
|
|
- # Chain work starts from here
|
|
|
+ class Looper(threading.Thread):
|
|
|
+ def __init__(self):
|
|
|
+ super(Looper, self).__init__()
|
|
|
|
|
|
- # Static chain interaction
|
|
|
- block1 = Block(previous=initial)
|
|
|
+ def run(self):
|
|
|
+ global CURRENT
|
|
|
+ while True:
|
|
|
+ print(f'[ THR ] Wait for new block')
|
|
|
+ time.sleep(10)
|
|
|
+ print(f'[ THR ] Work on new block. Height: {CURRENT.height}')
|
|
|
+ CURRENT.lock_hash_finish()
|
|
|
+ CURRENT = Block(previous=CURRENT)
|
|
|
|
|
|
- # Create a new contract
|
|
|
- contract1 = Contract()
|
|
|
- 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.create(title='U gotta work',
|
|
|
- desc='Finish this',
|
|
|
- deadline=int(time.time()))
|
|
|
+ class Service(rpyc.Service):
|
|
|
+ def exposed_get_current_block(self):
|
|
|
+ global CURRENT
|
|
|
+ return CURRENT
|
|
|
|
|
|
- # Add term to contract1
|
|
|
- contract1.add_term(term=term1)
|
|
|
+ def exposed_push_contract(self, contract):
|
|
|
+ global CURRENT
|
|
|
+ trans = Transaction()
|
|
|
+ trans.set_serialized_contract(contract)
|
|
|
+ return CURRENT.add_transaction(trans=trans)
|
|
|
|
|
|
- # Bind contract to transaction
|
|
|
- transaction1 = Transaction()
|
|
|
- transaction1.set_contract(contract=contract1)
|
|
|
|
|
|
- # Add transaction to current block
|
|
|
- block1.add_transaction(trans=transaction1)
|
|
|
+ # Start the RPC server to listen for GUI instances
|
|
|
+ server = ThreadPoolServer(Service, port=42069, protocol_config={'allow_public_attrs': True})
|
|
|
|
|
|
- # Finish block and all its transactions
|
|
|
- block1.lock_hash_finish()
|
|
|
+ looper = Looper()
|
|
|
+ looper.start()
|
|
|
|
|
|
- block2 = Block(previous=block1)
|
|
|
+ print(f'[ RPC ] Started')
|
|
|
+ server.start()
|
|
|
|
|
|
- server = ThreadedServer(RPC, port=42069)
|
|
|
- server.start()
|
|
|
|
|
|
|
|
|
- # Create a loop that starts to sleep for 10 seconds / mocking a 10 second block time
|
|
|
- while True:
|
|
|
- # Wait 10 seconds
|
|
|
- time.sleep(10)
|
|
|
|
|
|
# Possible TODO's:
|
|
|
# * Upon adding transaction to block check if transaction is already locked
|