File: app.py

package info (click to toggle)
abci 0.0~git20170124.0.f94ae5e-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 432 kB
  • sloc: python: 724; sh: 56; makefile: 23
file content (82 lines) | stat: -rw-r--r-- 2,163 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import sys

from abci.wire import hex2bytes, decode_big_endian, encode_big_endian
from abci.server import ABCIServer
from abci.reader import BytesBuffer


class CounterApplication():

    def __init__(self):
        sys.exit("The python example is out of date.  Upgrading the Python examples is currently left as an exercise to you.")
        self.hashCount = 0
        self.txCount = 0
        self.serial = False

    def echo(self, msg):
        return msg, 0

    def info(self):
        return ["hashes:%d, txs:%d" % (self.hashCount, self.txCount)], 0

    def set_option(self, key, value):
        if key == "serial" and value == "on":
            self.serial = True
        return 0

    def deliver_tx(self, txBytes):
        if self.serial:
            txByteArray = bytearray(txBytes)
            if len(txBytes) >= 2 and txBytes[:2] == "0x":
                txByteArray = hex2bytes(txBytes[2:])
            txValue = decode_big_endian(
                BytesBuffer(txByteArray), len(txBytes))
            if txValue != self.txCount:
                return None, 6
        self.txCount += 1
        return None, 0

    def check_tx(self, txBytes):
        if self.serial:
            txByteArray = bytearray(txBytes)
            if len(txBytes) >= 2 and txBytes[:2] == "0x":
                txByteArray = hex2bytes(txBytes[2:])
            txValue = decode_big_endian(
                BytesBuffer(txByteArray), len(txBytes))
            if txValue < self.txCount:
                return 6
        return 0

    def commit(self):
        self.hashCount += 1
        if self.txCount == 0:
            return "", 0
        h = encode_big_endian(self.txCount, 8)
        h.reverse()
        return str(h), 0

    def add_listener(self):
        return 0

    def rm_listener(self):
        return 0

    def event(self):
        return


if __name__ == '__main__':
    l = len(sys.argv)
    if l == 1:
        port = 46658
    elif l == 2:
        port = int(sys.argv[1])
    else:
        print "too many arguments"
        quit()

    print 'ABCI Demo APP (Python)'

    app = CounterApplication()
    server = ABCIServer(app, port)
    server.main_loop()