2016-01-25 08:01:18 -08:00
|
|
|
var server = require("./server");
|
|
|
|
var wire = require("js-wire");
|
|
|
|
var util = require("util");
|
|
|
|
var msg = require("./msgs");
|
|
|
|
var types = require("./types");
|
|
|
|
|
2015-12-18 20:44:48 -05:00
|
|
|
|
|
|
|
function CounterApp(){
|
|
|
|
this.hashCount = 0;
|
|
|
|
this.txCount = 0;
|
2016-01-08 16:52:02 -08:00
|
|
|
this.serial = false;
|
2015-12-18 20:44:48 -05:00
|
|
|
};
|
|
|
|
|
2016-01-25 08:01:18 -08:00
|
|
|
CounterApp.prototype.info = function(cb) {
|
|
|
|
return cb(util.format("hashes:%d, txs:%d", this.hashCount, this.txCount));
|
2015-12-18 20:44:48 -05:00
|
|
|
}
|
|
|
|
|
2016-01-25 08:01:18 -08:00
|
|
|
CounterApp.prototype.set_option = function(cb, key, value) {
|
|
|
|
if (key == "serial" && value == "on") {
|
2015-12-18 20:44:48 -05:00
|
|
|
this.serial = true;
|
|
|
|
}
|
2016-01-25 08:01:18 -08:00
|
|
|
return cb("");
|
2015-12-18 20:44:48 -05:00
|
|
|
}
|
|
|
|
|
2016-01-25 08:01:18 -08:00
|
|
|
CounterApp.prototype.append_tx = function(cb, txBytes) {
|
2015-12-18 20:44:48 -05:00
|
|
|
if (this.serial) {
|
2015-12-21 18:47:14 -05:00
|
|
|
if (txBytes.length >= 2 && txBytes.slice(0, 2) == "0x") {
|
2016-01-25 08:01:18 -08:00
|
|
|
var hexString = txBytes.toString("ascii", 2);
|
|
|
|
var hexBytes = new Buffer(hexString, "hex");
|
|
|
|
txBytes = hexBytes;
|
2015-12-18 20:44:48 -05:00
|
|
|
}
|
2016-01-25 08:01:18 -08:00
|
|
|
var txValue = txBytes.readIntBE(0, txBytes.length);
|
2015-12-21 18:47:14 -05:00
|
|
|
if (txValue != this.txCount){
|
2016-01-25 08:01:18 -08:00
|
|
|
return cb(types.RetCodeInvalidNonce, "", "Nonce is invalid");
|
2015-12-18 20:44:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.txCount += 1;
|
2016-01-25 08:01:18 -08:00
|
|
|
return cb(types.RetCodeOK, "", "");
|
2015-12-18 20:44:48 -05:00
|
|
|
}
|
|
|
|
|
2016-01-25 08:01:18 -08:00
|
|
|
CounterApp.prototype.check_tx = function(cb, txBytes) {
|
2016-01-08 16:52:02 -08:00
|
|
|
if (this.serial) {
|
|
|
|
if (txBytes.length >= 2 && txBytes.slice(0, 2) == "0x") {
|
2016-01-25 08:01:18 -08:00
|
|
|
var hexString = txBytes.toString("ascii", 2);
|
|
|
|
var hexBytes = new Buffer(hexString, "hex");
|
|
|
|
txBytes = hexBytes;
|
2016-01-08 16:52:02 -08:00
|
|
|
}
|
2016-01-25 08:01:18 -08:00
|
|
|
var txValue = txBytes.readIntBE(0, txBytes.length);
|
2016-01-08 16:52:02 -08:00
|
|
|
if (txValue < this.txCount){
|
2016-01-25 08:01:18 -08:00
|
|
|
return cb(types.RetCodeInvalidNonce, "", "Nonce is too low");
|
2016-01-08 16:52:02 -08:00
|
|
|
}
|
|
|
|
}
|
2016-01-25 08:01:18 -08:00
|
|
|
this.txCount += 1;
|
|
|
|
return cb(types.RetCodeOK, "", "");
|
2016-01-08 16:52:02 -08:00
|
|
|
}
|
|
|
|
|
2016-01-25 08:01:18 -08:00
|
|
|
CounterApp.prototype.get_hash = function(cb) {
|
2015-12-18 20:44:48 -05:00
|
|
|
this.hashCount += 1;
|
|
|
|
if (this.txCount == 0){
|
2016-01-25 08:01:18 -08:00
|
|
|
return cb("", "Zero tx count; hash is empth");
|
2015-12-18 20:44:48 -05:00
|
|
|
}
|
2016-01-25 08:01:18 -08:00
|
|
|
var buf = new Buffer(8);
|
|
|
|
buf.writeIntBE(this.txCount, 0, 8);
|
|
|
|
cb(buf, "");
|
2015-12-18 20:44:48 -05:00
|
|
|
}
|
|
|
|
|
2016-01-25 08:01:18 -08:00
|
|
|
CounterApp.prototype.query = function(cb) {
|
|
|
|
return cb("", "Query not yet supporrted");
|
2015-12-18 20:44:48 -05:00
|
|
|
}
|
|
|
|
|
2016-01-25 08:01:18 -08:00
|
|
|
console.log("Counter app in Javascript");
|
2015-12-18 20:44:48 -05:00
|
|
|
|
|
|
|
var app = new CounterApp();
|
|
|
|
var appServer = new server.AppServer(app);
|
2016-01-25 08:01:18 -08:00
|
|
|
appServer.server.listen(46658);
|