Skip to content
Permalink
Browse files

block submission via websocket + block/work info on ws hello

  • Loading branch information...
Lemmmy committed Apr 5, 2018
1 parent d738fe8 commit 162538229631cbcbbbfaae5667fec726ec42b947
Showing with 128 additions and 20 deletions.
  1. +3 −2 package.json
  2. +27 −18 src/routes/websockets.js
  3. +98 −0 src/websocket_routes/submission.js
@@ -1,6 +1,6 @@
{
"name": "Krist",
"version": "2.0.7",
"version": "2.0.8",
"description": "The new Krist node written in node.js.",
"main": "main.js",
"scripts": {
@@ -89,7 +89,8 @@
"WebsocketGroup",
"WebsocketStart",
"WSGetWork",
"WSMakeTransaction"
"WSMakeTransaction",
"WSSubmitBlock"
]
}
}
@@ -26,6 +26,7 @@ var krist = require('./../krist.js'),
redis = require('./../redis.js'),
websockets = require('./../websockets.js'),
addresses = require('./../addresses.js'),
blocks = require('./../controllers/blocks.js'),
fs = require('fs'),
uuid = require('node-uuid');

@@ -40,32 +41,40 @@ module.exports = function(app) {
redis.getClient().getAsync('ws-' + req.params.token).then(function(wsid) {
if (wsid) {
redis.getClient().getAsync('wspkey-' + req.params.token).then(function(pkey) {
fs.readFile('motd.txt', function(err, data) {
if (err) {
return utils.sendToWS(ws, {
ok: true,
type: "hello",
server_time: new Date(),
motd: "Welcome to Krist!"
});
}

fs.stat('motd.txt', function(err, stats) {
blocks.getLastBlock().then(function(block) {
fs.readFile('motd.txt', function(err, data) {
if (err) {
return utils.sendToWS(ws, {
ok: true,
type: "hello",
server_time: new Date(),
motd: data.toString()
motd: "Welcome to Krist!",
last_block: blocks.blockToJSON(block),
work: krist.getWork()
});
}

utils.sendToWS(ws, {
ok: true,
type: "hello",
server_time: new Date(),
motd: data.toString(),
motd_set: stats.mtime
fs.stat('motd.txt', function(err, stats) {
if (err) {
return utils.sendToWS(ws, {
ok: true,
type: "hello",
server_time: new Date(),
motd: data.toString(),
last_block: blocks.blockToJSON(block),
work: krist.getWork()
});
}

utils.sendToWS(ws, {
ok: true,
type: "hello",
server_time: new Date(),
motd: data.toString(),
motd_set: stats.mtime,
last_block: blocks.blockToJSON(block),
work: krist.getWork()
});
});
});
});
@@ -0,0 +1,98 @@
/**
* Created by Drew Lemmy, 2016
*
* This file is part of Krist.
*
* Krist is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Krist is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Krist. If not, see <http://www.gnu.org/licenses/>.
*
* For more project information, see <https://github.com/Lemmmy/Krist>.
*/

var errors = require("../errors/errors.js"),
addressesController = require('./../controllers/addresses.js');
blocksController = require('./../controllers/blocks.js');

module.exports = function(websockets) {
/**
* @api {ws} //ws:"type":"submit_block" Submit a block
* @apiName WSSubmitBlock
* @apiGroup WebsocketGroup
* @apiVersion 2.0.8
*
* @apiParam (WebsocketParameter) {Number} id
* @apiParam (WebsocketParameter) {String="submit_block"} type
* @apiParam (WebsocketParameter) {String} [address]
* @apiParam (WebsocketParameter) {String} nonce
*
* @apiSuccess {Boolean} success Whether the submission was successful or not.
* @apiSuccess {Number} [work] The new difficulty for block submission (if the solution was successful).
* @apiUse Address
* @apiUse Block
* @apiSuccess {Object} [address] The address of the solver (if the solution was successful).
* @apiSuccess {Object} [block] The block which was just submitted (if the solution was successful).
*
* @apiSuccessExample {json} Success
* {
* "ok": true,
* "success": true,
* "work": 18750,
* "address": {
* "address": "kre3w0i79j",
* "balance": 925378,
* "totalin": 925378,
* "totalout": 0,
* "firstseen": "2015-03-13T12:55:18.000Z"
* },
* "block": {
* "height": 122226,
* "address": "kre3w0i79j",
* "hash": "000000007abc9f0cafaa8bf85d19817ee4f5c41ae758de3ad419d62672423ef",
* "short_hash": "000000007ab",
* "value": 14,
* "time": "2016-02-06T19:22:41.746Z"
* }
* }
*
* @apiSuccessExample {json} Solution Incorrect
* {
* "ok": true,
* "success": false
* }
*
* @apiErrorExample {json} Invalid Nonce
* {
* "ok": false,
* "error": "invalid_parameter",
* "parameter": "nonce"
* }
*/

websockets.addMessageHandler('submit_block', function(ws, message) {
return new Promise(function(resolve, reject) {
if (ws.isGuest && !message.address) {
return reject(new errors.ErrorMissingParameter('address'));
}

blocksController.submitBlock(message.address || ws.auth, message.nonce).then(function(result) {
resolve({
ok: true,
success: true,
work: result.work,
address: addressesController.addressToJSON(result.address),
block: blocksController.blockToJSON(result.block)
});
}).catch(reject);
});
});
};

0 comments on commit 1625382

Please sign in to comment.
You can’t perform that action at this time.