main.py 4.0 KB

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