2
0

main.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import time
  2. import rpyc
  3. from rpyc.utils.server import ThreadPoolServer
  4. import argparse
  5. import threading
  6. from lib.chain import Chain, Block
  7. from lib.transaction import Transaction
  8. from lib.contract import Contract
  9. from lib.block import CURRENT
  10. if __name__ == '__main__':
  11. parser = argparse.ArgumentParser(description='DSP Blockchain')
  12. mode = parser.add_mutually_exclusive_group()
  13. mode.add_argument('--daemon', action='store_true', help='Run in daemon mode')
  14. mode.add_argument('--gui', action='store_true', help='Run in GUI mode')
  15. parser.add_argument('--static', action='store_true', help='Test run the static chain')
  16. args = parser.parse_args()
  17. if args.gui:
  18. print('Not yest implemented')
  19. exit(1)
  20. # Initialize the genesis block
  21. initial = Block()
  22. initial.previous_hash = 'This is the founding block for our DSP project'
  23. initial.proof = 69
  24. if args.static:
  25. # Initialize the chain (Creates empty chain)
  26. chain = Chain()
  27. # Lock block
  28. initial.lock_hash_finish()
  29. # Add the genesis block to the chain
  30. chain.add_block(block=initial)
  31. # Chain work starts from here
  32. # Static chain interaction
  33. block1 = Block(previous=initial)
  34. # Create a new contract
  35. contract1 = Contract()
  36. contract1.create(title='Digital legal handshake',
  37. desc='Hereby you declare to fulfill the following terms',
  38. deadline=int(time.time()),
  39. price=420.69)
  40. # Create a new term
  41. term1 = Contract.Term()
  42. term1.create(title='U gotta work',
  43. desc='Finish this',
  44. deadline=int(time.time()))
  45. # Add term to contract1
  46. contract1.add_term(term=term1)
  47. # Bind contract to transaction
  48. transaction1 = Transaction()
  49. transaction1.set_contract(contract=contract1)
  50. # Add transaction to current block
  51. block1.add_transaction(trans=transaction1)
  52. # Finish block and all its transactions
  53. block1.lock_hash_finish()
  54. block2 = Block(previous=block1)
  55. exit(0)
  56. else:
  57. chain = Chain()
  58. CURRENT = initial
  59. class Looper(threading.Thread):
  60. def __init__(self):
  61. super(Looper, self).__init__()
  62. def run(self):
  63. global CURRENT
  64. while True:
  65. print(f'[ THR ] Wait for new block')
  66. time.sleep(10)
  67. print(f'[ THR ] Work on new block. Height: {CURRENT.height}')
  68. CURRENT.lock_hash_finish()
  69. CURRENT = Block(previous=CURRENT)
  70. class Service(rpyc.Service):
  71. def exposed_get_current_block(self):
  72. global CURRENT
  73. return CURRENT
  74. def exposed_push_contract(self, contract):
  75. global CURRENT
  76. trans = Transaction()
  77. trans.set_serialized_contract(contract)
  78. return CURRENT.add_transaction(trans=trans)
  79. # Start the RPC server to listen for GUI instances
  80. server = ThreadPoolServer(Service, port=42069, protocol_config={'allow_public_attrs': True})
  81. looper = Looper()
  82. looper.start()
  83. print(f'[ RPC ] Started')
  84. server.start()
  85. # Possible TODO's:
  86. # * Upon adding transaction to block check if transaction is already locked