Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgraded ByteballCore to es6 using lebab #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions archiving.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
/*jslint node: true */
"use strict";
var db = require('./db.js');
const db = require('./db.js');


function generateQueriesToArchiveJoint(conn, objJoint, reason, arrQueries, cb){
var func = (reason === 'uncovered') ? generateQueriesToRemoveJoint : generateQueriesToVoidJoint;
func(conn, objJoint.unit.unit, arrQueries, function(){
conn.addQuery(arrQueries, "INSERT "+conn.getIgnore()+" INTO archived_joints (unit, reason, json) VALUES (?,?,?)",
const func = (reason === 'uncovered') ? generateQueriesToRemoveJoint : generateQueriesToVoidJoint;
func(conn, objJoint.unit.unit, arrQueries, () => {
conn.addQuery(arrQueries, `INSERT ${conn.getIgnore()} INTO archived_joints (unit, reason, json) VALUES (?,?,?)`,
[objJoint.unit.unit, reason, JSON.stringify(objJoint)]);
cb();
});
}

function generateQueriesToRemoveJoint(conn, unit, arrQueries, cb){
generateQueriesToUnspendOutputsSpentInArchivedUnit(conn, unit, arrQueries, function(){
generateQueriesToUnspendOutputsSpentInArchivedUnit(conn, unit, arrQueries, () => {
conn.addQuery(arrQueries, "DELETE FROM sent_mnemonics WHERE unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM witness_list_hashes WHERE witness_list_unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM earned_headers_commission_recipients WHERE unit=?", [unit]);
Expand Down Expand Up @@ -41,7 +40,7 @@ function generateQueriesToRemoveJoint(conn, unit, arrQueries, cb){
}

function generateQueriesToVoidJoint(conn, unit, arrQueries, cb){
generateQueriesToUnspendOutputsSpentInArchivedUnit(conn, unit, arrQueries, function(){
generateQueriesToUnspendOutputsSpentInArchivedUnit(conn, unit, arrQueries, () => {
// we keep witnesses, author addresses, and the unit itself
conn.addQuery(arrQueries, "DELETE FROM witness_list_hashes WHERE witness_list_unit=?", [unit]);
conn.addQuery(arrQueries, "DELETE FROM earned_headers_commission_recipients WHERE unit=?", [unit]);
Expand All @@ -65,8 +64,8 @@ function generateQueriesToVoidJoint(conn, unit, arrQueries, cb){
}

function generateQueriesToUnspendOutputsSpentInArchivedUnit(conn, unit, arrQueries, cb){
generateQueriesToUnspendTransferOutputsSpentInArchivedUnit(conn, unit, arrQueries, function(){
generateQueriesToUnspendHeadersCommissionOutputsSpentInArchivedUnit(conn, unit, arrQueries, function(){
generateQueriesToUnspendTransferOutputsSpentInArchivedUnit(conn, unit, arrQueries, () => {
generateQueriesToUnspendHeadersCommissionOutputsSpentInArchivedUnit(conn, unit, arrQueries, () => {
generateQueriesToUnspendWitnessingOutputsSpentInArchivedUnit(conn, unit, arrQueries, cb);
});
});
Expand All @@ -87,12 +86,12 @@ function generateQueriesToUnspendTransferOutputsSpentInArchivedUnit(conn, unit,
AND inputs.unit!=alt_inputs.unit \n\
)",
[unit],
function(rows){
rows.forEach(function(row){
rows => {
rows.forEach(({src_unit, src_message_index, src_output_index}) => {
conn.addQuery(
arrQueries,
"UPDATE outputs SET is_spent=0 WHERE unit=? AND message_index=? AND output_index=?",
[row.src_unit, row.src_message_index, row.src_output_index]
[src_unit, src_message_index, src_output_index]
);
});
cb();
Expand All @@ -119,12 +118,12 @@ function generateQueriesToUnspendHeadersCommissionOutputsSpentInArchivedUnit(con
AND inputs.unit!=alt_inputs.unit \n\
)",
[unit],
function(rows){
rows.forEach(function(row){
rows => {
rows.forEach(({address, main_chain_index}) => {
conn.addQuery(
arrQueries,
"UPDATE headers_commission_outputs SET is_spent=0 WHERE address=? AND main_chain_index=?",
[row.address, row.main_chain_index]
[address, main_chain_index]
);
});
cb();
Expand All @@ -151,12 +150,12 @@ function generateQueriesToUnspendWitnessingOutputsSpentInArchivedUnit(conn, unit
AND inputs.unit!=alt_inputs.unit \n\
)",
[unit],
function(rows){
rows.forEach(function(row){
rows => {
rows.forEach(({address, main_chain_index}) => {
conn.addQuery(
arrQueries,
"UPDATE witnessing_outputs SET is_spent=0 WHERE address=? AND main_chain_index=?",
[row.address, row.main_chain_index]
[address, main_chain_index]
);
});
cb();
Expand Down
137 changes: 68 additions & 69 deletions balances.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,52 @@
/*jslint node: true */
"use strict";
var _ = require('lodash');
var constants = require('./constants.js');
var db = require('./db');
const _ = require('lodash');
const constants = require('./constants.js');
const db = require('./db');

function readBalance(wallet, handleBalance){
var walletIsAddress = typeof wallet === 'string' && wallet.length === 32; // ValidationUtils.isValidAddress
var join_my_addresses = walletIsAddress ? "" : "JOIN my_addresses USING(address)";
var where_condition = walletIsAddress ? "address=?" : "wallet=?";
var assocBalances = {base: {stable: 0, pending: 0}};
const walletIsAddress = typeof wallet === 'string' && wallet.length === 32; // ValidationUtils.isValidAddress
const join_my_addresses = walletIsAddress ? "" : "JOIN my_addresses USING(address)";
const where_condition = walletIsAddress ? "address=?" : "wallet=?";
const assocBalances = {base: {stable: 0, pending: 0}};
assocBalances[constants.BLACKBYTES_ASSET] = {is_private: 1, stable: 0, pending: 0};
db.query(
"SELECT asset, is_stable, SUM(amount) AS balance \n\
FROM outputs "+join_my_addresses+" CROSS JOIN units USING(unit) \n\
WHERE is_spent=0 AND "+where_condition+" AND sequence='good' \n\
GROUP BY asset, is_stable",
`SELECT asset, is_stable, SUM(amount) AS balance \n\
FROM outputs ${join_my_addresses} CROSS JOIN units USING(unit) \n\
WHERE is_spent=0 AND ${where_condition} AND sequence='good' \n\
GROUP BY asset, is_stable`,
[wallet],
function(rows){
for (var i=0; i<rows.length; i++){
var row = rows[i];
var asset = row.asset || "base";
rows => {
for (let i=0; i<rows.length; i++){
const row = rows[i];
const asset = row.asset || "base";
if (!assocBalances[asset])
assocBalances[asset] = {stable: 0, pending: 0};
assocBalances[asset][row.is_stable ? 'stable' : 'pending'] = row.balance;
}
var my_addresses_join = walletIsAddress ? "" : "my_addresses CROSS JOIN";
var using = walletIsAddress ? "" : "USING(address)";
const my_addresses_join = walletIsAddress ? "" : "my_addresses CROSS JOIN";
const using = walletIsAddress ? "" : "USING(address)";
db.query(
"SELECT SUM(total) AS total FROM ( \n\
SELECT SUM(amount) AS total FROM "+my_addresses_join+" witnessing_outputs "+using+" WHERE is_spent=0 AND "+where_condition+" \n\
UNION ALL \n\
SELECT SUM(amount) AS total FROM "+my_addresses_join+" headers_commission_outputs "+using+" WHERE is_spent=0 AND "+where_condition+" ) AS t",
`SELECT SUM(total) AS total FROM ( \n\
SELECT SUM(amount) AS total FROM ${my_addresses_join} witnessing_outputs ${using} WHERE is_spent=0 AND ${where_condition} \n\
UNION ALL \n\
SELECT SUM(amount) AS total FROM ${my_addresses_join} headers_commission_outputs ${using} WHERE is_spent=0 AND ${where_condition} ) AS t`,
[wallet,wallet],
function(rows) {
rows => {
if(rows.length){
assocBalances["base"]["stable"] += rows[0].total;
}
// add 0-balance assets
db.query(
"SELECT DISTINCT outputs.asset, is_private \n\
FROM outputs "+join_my_addresses+" \n\
CROSS JOIN units USING(unit) \n\
LEFT JOIN assets ON outputs.asset=assets.unit \n\
WHERE "+where_condition+" AND sequence='good'",
`SELECT DISTINCT outputs.asset, is_private \n\
FROM outputs ${join_my_addresses} \n\
CROSS JOIN units USING(unit) \n\
LEFT JOIN assets ON outputs.asset=assets.unit \n\
WHERE ${where_condition} AND sequence='good'`,
[wallet],
function(rows){
for (var i=0; i<rows.length; i++){
var row = rows[i];
var asset = row.asset || "base";
rows => {
for (let i=0; i<rows.length; i++){
const row = rows[i];
const asset = row.asset || "base";
if (!assocBalances[asset])
assocBalances[asset] = {stable: 0, pending: 0};
assocBalances[asset].is_private = row.is_private;
Expand All @@ -62,20 +61,20 @@ function readBalance(wallet, handleBalance){
}

function readOutputsBalance(wallet, handleBalance){
var walletIsAddress = typeof wallet === 'string' && wallet.length === 32; // ValidationUtils.isValidAddress
var join_my_addresses = walletIsAddress ? "" : "JOIN my_addresses USING(address)";
var where_condition = walletIsAddress ? "address=?" : "wallet=?";
var assocBalances = {base: {stable: 0, pending: 0}};
const walletIsAddress = typeof wallet === 'string' && wallet.length === 32; // ValidationUtils.isValidAddress
const join_my_addresses = walletIsAddress ? "" : "JOIN my_addresses USING(address)";
const where_condition = walletIsAddress ? "address=?" : "wallet=?";
const assocBalances = {base: {stable: 0, pending: 0}};
db.query(
"SELECT asset, is_stable, SUM(amount) AS balance \n\
FROM outputs "+join_my_addresses+" CROSS JOIN units USING(unit) \n\
WHERE is_spent=0 AND "+where_condition+" AND sequence='good' \n\
GROUP BY asset, is_stable",
`SELECT asset, is_stable, SUM(amount) AS balance \n\
FROM outputs ${join_my_addresses} CROSS JOIN units USING(unit) \n\
WHERE is_spent=0 AND ${where_condition} AND sequence='good' \n\
GROUP BY asset, is_stable`,
[wallet],
function(rows){
for (var i=0; i<rows.length; i++){
var row = rows[i];
var asset = row.asset || "base";
rows => {
for (let i=0; i<rows.length; i++){
const row = rows[i];
const asset = row.asset || "base";
if (!assocBalances[asset])
assocBalances[asset] = {stable: 0, pending: 0};
assocBalances[asset][row.is_stable ? 'stable' : 'pending'] = row.balance;
Expand All @@ -86,52 +85,52 @@ function readOutputsBalance(wallet, handleBalance){
}

function readSharedAddressesOnWallet(wallet, handleSharedAddresses){
db.query("SELECT DISTINCT shared_address FROM my_addresses JOIN shared_address_signing_paths USING(address) WHERE wallet=?", [wallet], function(rows){
var arrSharedAddresses = rows.map(function(row){ return row.shared_address; });
db.query("SELECT DISTINCT shared_address FROM my_addresses JOIN shared_address_signing_paths USING(address) WHERE wallet=?", [wallet], rows => {
const arrSharedAddresses = rows.map(({shared_address}) => shared_address);
if (arrSharedAddresses.length === 0)
return handleSharedAddresses([]);
readSharedAddressesDependingOnAddresses(arrSharedAddresses, function(arrNewSharedAddresses){
readSharedAddressesDependingOnAddresses(arrSharedAddresses, arrNewSharedAddresses => {
handleSharedAddresses(arrSharedAddresses.concat(arrNewSharedAddresses));
});
});
}

function readSharedAddressesDependingOnAddresses(arrMemberAddresses, handleSharedAddresses){
var strAddressList = arrMemberAddresses.map(db.escape).join(', ');
db.query("SELECT DISTINCT shared_address FROM shared_address_signing_paths WHERE address IN("+strAddressList+")", function(rows){
var arrSharedAddresses = rows.map(function(row){ return row.shared_address; });
const strAddressList = arrMemberAddresses.map(db.escape).join(', ');
db.query(`SELECT DISTINCT shared_address FROM shared_address_signing_paths WHERE address IN(${strAddressList})`, rows => {
const arrSharedAddresses = rows.map(({shared_address}) => shared_address);
if (arrSharedAddresses.length === 0)
return handleSharedAddresses([]);
var arrNewMemberAddresses = _.difference(arrSharedAddresses, arrMemberAddresses);
const arrNewMemberAddresses = _.difference(arrSharedAddresses, arrMemberAddresses);
if (arrNewMemberAddresses.length === 0)
return handleSharedAddresses([]);
readSharedAddressesDependingOnAddresses(arrNewMemberAddresses, function(arrNewSharedAddresses){
readSharedAddressesDependingOnAddresses(arrNewMemberAddresses, arrNewSharedAddresses => {
handleSharedAddresses(arrNewMemberAddresses.concat(arrNewSharedAddresses));
});
});
}

function readSharedBalance(wallet, handleBalance){
var assocBalances = {};
readSharedAddressesOnWallet(wallet, function(arrSharedAddresses){
const assocBalances = {};
readSharedAddressesOnWallet(wallet, arrSharedAddresses => {
if (arrSharedAddresses.length === 0)
return handleBalance(assocBalances);
var strAddressList = arrSharedAddresses.map(db.escape).join(', ');
const strAddressList = arrSharedAddresses.map(db.escape).join(', ');
db.query(
"SELECT asset, address, is_stable, SUM(amount) AS balance \n\
FROM outputs CROSS JOIN units USING(unit) \n\
WHERE is_spent=0 AND sequence='good' AND address IN("+strAddressList+") \n\
GROUP BY asset, address, is_stable \n\
UNION ALL \n\
SELECT NULL AS asset, address, 1 AS is_stable, SUM(amount) AS balance FROM witnessing_outputs \n\
WHERE is_spent=0 AND address IN("+strAddressList+") GROUP BY address \n\
UNION ALL \n\
SELECT NULL AS asset, address, 1 AS is_stable, SUM(amount) AS balance FROM headers_commission_outputs \n\
WHERE is_spent=0 AND address IN("+strAddressList+") GROUP BY address",
function(rows){
for (var i=0; i<rows.length; i++){
var row = rows[i];
var asset = row.asset || "base";
`SELECT asset, address, is_stable, SUM(amount) AS balance \n\
FROM outputs CROSS JOIN units USING(unit) \n\
WHERE is_spent=0 AND sequence='good' AND address IN(${strAddressList}) \n\
GROUP BY asset, address, is_stable \n\
UNION ALL \n\
SELECT NULL AS asset, address, 1 AS is_stable, SUM(amount) AS balance FROM witnessing_outputs \n\
WHERE is_spent=0 AND address IN(${strAddressList}) GROUP BY address \n\
UNION ALL \n\
SELECT NULL AS asset, address, 1 AS is_stable, SUM(amount) AS balance FROM headers_commission_outputs \n\
WHERE is_spent=0 AND address IN(${strAddressList}) GROUP BY address`,
rows => {
for (let i=0; i<rows.length; i++){
const row = rows[i];
const asset = row.asset || "base";
if (!assocBalances[asset])
assocBalances[asset] = {};
if (!assocBalances[asset][row.address])
Expand Down
31 changes: 15 additions & 16 deletions bots.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
/*jslint node: true */
"use strict";
var db = require('./db.js');
var objectHash = require('./object_hash.js');
var device = require('./device.js');
var async = require('async');
const db = require('./db.js');
const objectHash = require('./object_hash.js');
const device = require('./device.js');
const async = require('async');

var bots_cache = [];
let bots_cache = [];

function getBotByID(id, cb) {
for (var i in bots_cache) {
var bot = bots_cache[i];
for (const i in bots_cache) {
const bot = bots_cache[i];
if (bot.id == id) {
return setPairingStatus(bot, cb);
}
}
}

function load(cb) {
device.requestFromHub("hub/get_bots", false, function(err, bots){
device.requestFromHub("hub/get_bots", false, (err, bots) => {
if (err != null) {
return cb(err, null);
}
async.eachSeries(bots,
function(bot, cb) {
setPairingStatus(bot, function(handled_bot){
bot.isPaired = handled_bot.isPaired;
(bot, cb) => {
setPairingStatus(bot, ({isPaired}) => {
bot.isPaired = isPaired;
cb();
})
},
function(){
() => {
bots_cache = bots;
cb(err, bots);
}
Expand All @@ -37,10 +36,10 @@ function load(cb) {
}

function setPairingStatus(bot, cb) {
var pubkey = bot.pairing_code.substr(0, bot.pairing_code.indexOf('@'));
const pubkey = bot.pairing_code.substr(0, bot.pairing_code.indexOf('@'));
bot.device_address = objectHash.getDeviceAddress(pubkey);
db.query("SELECT 1 FROM correspondent_devices WHERE device_address = ?", [bot.device_address], function(rows){
bot.isPaired = (rows.length == 1);
db.query("SELECT 1 FROM correspondent_devices WHERE device_address = ?", [bot.device_address], ({length}) => {
bot.isPaired = (length == 1);
cb(bot);
});
}
Expand Down
8 changes: 3 additions & 5 deletions breadcrumbs.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
/*jslint node: true */
'use strict';

/*
Used for debugging long sequences of calls not captured by stack traces.
Should be included with bug reports.
*/

var MAX_LENGTH = 200;
var arrBreadcrumbs = [];
const MAX_LENGTH = 200;
const arrBreadcrumbs = [];

function add(breadcrumb){
if (arrBreadcrumbs.length > MAX_LENGTH)
arrBreadcrumbs.shift(); // forget the oldest breadcrumbs
arrBreadcrumbs.push(Date().toString() + ': ' + breadcrumb);
arrBreadcrumbs.push(`${Date().toString()}: ${breadcrumb}`);
console.log(breadcrumb);
}

Expand Down
Loading