import json import time import rpyc 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 from lib.block import CURRENT if __name__ == '__main__': 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 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() block2 = Block(previous=block1) exit(0) else: chain = Chain() CURRENT = initial class Looper(threading.Thread): def __init__(self): super(Looper, self).__init__() 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) class Service(rpyc.Service): def exposed_get_current_block(self): 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() trans.set_serialized_contract(contract) return CURRENT.add_transaction(trans=trans) # Start the RPC server to listen for GUI instances server = ThreadPoolServer(Service, port=42069, protocol_config={'allow_public_attrs': True}) looper = Looper() looper.start() print(f'[ RPC ] Started') server.start() # Possible TODO's: # * Upon adding transaction to block check if transaction is already locked