/
counter.js
53 lines (44 loc) · 1.06 KB
/
counter.js
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
let createABCIServer = require('abci')
// turn on debug logging
require('debug').enable('abci*')
let state = {
count: 0
}
let handlers = {
info (request) {
return {
data: 'Node.js counter app',
version: '0.0.0',
lastBlockHeight: 0,
lastBlockAppHash: Buffer.alloc(0)
}
},
checkTx (request) {
let tx = padTx(request.tx)
let number = tx.readUInt32BE(0)
if (number !== state.count) {
return { code: 1, log: 'tx does not match count' }
}
return { code: 0, log: 'tx succeeded' }
},
deliverTx (request) {
let tx = padTx(request.tx)
let number = tx.readUInt32BE(0)
if (number !== state.count) {
return { code: 1, log: 'tx does not match count' }
}
// update state
state.count += 1
return { code: 0, log: 'tx succeeded' }
}
}
// make sure the transaction data is 4 bytes long
function padTx (tx) {
let buf = Buffer.alloc(4)
tx.copy(buf, 4 - tx.length)
return buf
}
let port = 26658
createABCIServer(handlers).listen(port, () => {
console.log(`listening on port ${port}`)
})