miner.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/env python3
  2. import argparse
  3. from urllib.parse import urlparse
  4. from src.crypto import Signing
  5. from src.protocol import Protocol
  6. from src.block import GENESIS_BLOCK
  7. def parse_addr_port(val):
  8. url = urlparse("//" + val)
  9. assert url.scheme == ''
  10. assert url.path == ''
  11. assert url.params == ''
  12. assert url.query == ''
  13. assert url.fragment == ''
  14. assert url.port is not None
  15. assert url.hostname is not None
  16. return (url.hostname, url.port)
  17. def main():
  18. parser = argparse.ArgumentParser(description="Blockchain Miner.")
  19. parser.add_argument("--listen-address", default="",
  20. help="The IP address where the P2P server should bind to.")
  21. parser.add_argument("--listen-port", default=0, type=int,
  22. help="The port where the P2P server should listen. Defaults a dynamically assigned port.")
  23. parser.add_argument("--mining-pubkey", type=argparse.FileType('rb'),
  24. help="The public key where mining rewards should be sent to. No mining is performed if this is left unspecified.")
  25. parser.add_argument("--bootstrap-peer", action='append', type=parse_addr_port,
  26. help="Addresses of other P2P peers in the network.")
  27. args = parser.parse_args()
  28. proto = Protocol(args.bootstrap_peer, GENESIS_BLOCK, args.listen_port, args.listen_address)
  29. if args.mining_pubkey is not None:
  30. pubkey = Signing(args.mining_pubkey.read())
  31. args.mining_pubkey.close()
  32. miner = Miner(proto, pubkey)
  33. miner.start_mining()
  34. # TODO: start RPC
  35. import time
  36. while True:
  37. time.sleep(2**31)
  38. if __name__ == '__main__':
  39. main()