main.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import time
  2. from rpyc.utils.server import ThreadedServer
  3. from lib.chain import Chain, Block
  4. from lib.rpc import RPC
  5. from lib.transaction import Transaction
  6. from lib.contract import Contract
  7. if __name__ == '__main__':
  8. # Initialize the chain (Creates empty chain)
  9. chain = Chain()
  10. # Initialize the genesis block
  11. initial = Block()
  12. initial.previous_hash = 'This is the founding block for our DSP project'
  13. initial.proof = 69
  14. # Lock block
  15. initial.lock_hash_finish()
  16. # Add the genesis block to the chain
  17. chain.add_block(block=initial)
  18. # Chain work starts from here
  19. # Static chain interaction
  20. block1 = Block(previous=initial)
  21. # Create a new contract
  22. contract1 = Contract()
  23. contract1.create(title='Digital legal handshake',
  24. desc='Hereby you declare to fulfill the following terms',
  25. deadline=int(time.time()),
  26. price=420.69)
  27. # Create a new term
  28. term1 = Contract.Term()
  29. term1.create(title='U gotta work',
  30. desc='Finish this',
  31. deadline=int(time.time()))
  32. # Add term to contract1
  33. contract1.add_term(term=term1)
  34. # Bind contract to transaction
  35. transaction1 = Transaction()
  36. transaction1.set_contract(contract=contract1)
  37. # Add transaction to current block
  38. block1.add_transaction(trans=transaction1)
  39. # Finish block and all its transactions
  40. block1.lock_hash_finish()
  41. block2 = Block(previous=block1)
  42. server = ThreadedServer(RPC, port=42069)
  43. server.start()
  44. # Create a loop that starts to sleep for 10 seconds / mocking a 10 second block time
  45. while True:
  46. # Wait 10 seconds
  47. time.sleep(10)
  48. # Possible TODO's:
  49. # * Upon adding transaction to block check if transaction is already locked