You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.1 KiB

8 years ago
8 years ago
8 years ago
  1. import sys
  2. from abci.wire import hex2bytes, decode_big_endian, encode_big_endian
  3. from abci.server import ABCIServer
  4. from abci.reader import BytesBuffer
  5. class CounterApplication():
  6. def __init__(self):
  7. sys.exit("The python example is out of date. Upgrading the Python examples is currently left as an exercise to you.")
  8. self.hashCount = 0
  9. self.txCount = 0
  10. self.serial = False
  11. def echo(self, msg):
  12. return msg, 0
  13. def info(self):
  14. return ["hashes:%d, txs:%d" % (self.hashCount, self.txCount)], 0
  15. def set_option(self, key, value):
  16. if key == "serial" and value == "on":
  17. self.serial = True
  18. return 0
  19. def deliver_tx(self, txBytes):
  20. if self.serial:
  21. txByteArray = bytearray(txBytes)
  22. if len(txBytes) >= 2 and txBytes[:2] == "0x":
  23. txByteArray = hex2bytes(txBytes[2:])
  24. txValue = decode_big_endian(
  25. BytesBuffer(txByteArray), len(txBytes))
  26. if txValue != self.txCount:
  27. return None, 6
  28. self.txCount += 1
  29. return None, 0
  30. def check_tx(self, txBytes):
  31. if self.serial:
  32. txByteArray = bytearray(txBytes)
  33. if len(txBytes) >= 2 and txBytes[:2] == "0x":
  34. txByteArray = hex2bytes(txBytes[2:])
  35. txValue = decode_big_endian(
  36. BytesBuffer(txByteArray), len(txBytes))
  37. if txValue < self.txCount:
  38. return 6
  39. return 0
  40. def commit(self):
  41. self.hashCount += 1
  42. if self.txCount == 0:
  43. return "", 0
  44. h = encode_big_endian(self.txCount, 8)
  45. h.reverse()
  46. return str(h), 0
  47. def add_listener(self):
  48. return 0
  49. def rm_listener(self):
  50. return 0
  51. def event(self):
  52. return
  53. if __name__ == '__main__':
  54. l = len(sys.argv)
  55. if l == 1:
  56. port = 26658
  57. elif l == 2:
  58. port = int(sys.argv[1])
  59. else:
  60. print "too many arguments"
  61. quit()
  62. print 'ABCI Demo APP (Python)'
  63. app = CounterApplication()
  64. server = ABCIServer(app, port)
  65. server.main_loop()