OSDN Git Service

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