main.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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()
  21. block1.previous_hash = chain.last_block().hash_value
  22. # Create a new contract
  23. contract1 = Contract()
  24. contract1.title = 'Digital legal handshake'
  25. contract1.description = 'Hereby you declare to fulfill the following terms'
  26. contract1.deadline = int(time.time())
  27. contract1.price = 420
  28. # Create a new term
  29. term1 = Contract.Term()
  30. term1.title = 'U gotta work'
  31. term1.description = 'Finish this'
  32. term1.deadline = int(time.time())
  33. # Add term to contract1
  34. contract1.add_term(term=term1)
  35. # Bind contract to transaction
  36. transaction1 = Transaction()
  37. transaction1.set_contract(contract=contract1)
  38. # Add transaction to current block
  39. block1.add_transaction(trans=transaction1)
  40. # Finish block and all its transactions
  41. block1.lock_hash_finish()
  42. block2 = Block(previous=block1)
  43. server = ThreadedServer(RPC, port=42069)
  44. server.start()
  45. # Create a loop that starts to sleep for 10 seconds / mocking a 10 second block time
  46. while True:
  47. # Wait 10 seconds
  48. time.sleep(10)
  49. # Possible TODO's:
  50. # * Upon adding transaction to block check if transaction is already locked