76 lines
1.9 KiB
JavaScript
Raw Normal View History

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;
this.serial = false;
2015-12-18 20:44:48 -05: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
}
CounterApp.prototype.set_option = function(cb, key, value) {
if (key == "serial" && value == "on") {
2015-12-18 20:44:48 -05:00
this.serial = true;
}
return cb("");
2015-12-18 20:44:48 -05: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") {
var hexString = txBytes.toString("ascii", 2);
var hexBytes = new Buffer(hexString, "hex");
txBytes = hexBytes;
2015-12-18 20:44:48 -05:00
}
var txValue = txBytes.readIntBE(0, txBytes.length);
2015-12-21 18:47:14 -05:00
if (txValue != this.txCount){
return cb(types.RetCodeInvalidNonce, "", "Nonce is invalid");
2015-12-18 20:44:48 -05:00
}
}
this.txCount += 1;
return cb(types.RetCodeOK, "", "");
2015-12-18 20:44:48 -05:00
}
CounterApp.prototype.check_tx = function(cb, txBytes) {
if (this.serial) {
if (txBytes.length >= 2 && txBytes.slice(0, 2) == "0x") {
var hexString = txBytes.toString("ascii", 2);
var hexBytes = new Buffer(hexString, "hex");
txBytes = hexBytes;
}
var txValue = txBytes.readIntBE(0, txBytes.length);
if (txValue < this.txCount){
return cb(types.RetCodeInvalidNonce, "", "Nonce is too low");
}
}
this.txCount += 1;
return cb(types.RetCodeOK, "", "");
}
CounterApp.prototype.get_hash = function(cb) {
2015-12-18 20:44:48 -05:00
this.hashCount += 1;
if (this.txCount == 0){
return cb("", "Zero tx count; hash is empth");
2015-12-18 20:44:48 -05:00
}
var buf = new Buffer(8);
buf.writeIntBE(this.txCount, 0, 8);
cb(buf, "");
2015-12-18 20:44:48 -05:00
}
CounterApp.prototype.query = function(cb) {
return cb("", "Query not yet supporrted");
2015-12-18 20:44:48 -05: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);
appServer.server.listen(46658);