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

Optimization 001 #109

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
29 changes: 19 additions & 10 deletions graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ function readDescendantUnitsByAuthorsBeforeMcIndex(conn, objEarlierUnitProps, ar
function goDown(arrStartUnits){
profiler.start();
conn.query(
"SELECT units.unit, unit_authors.address AS author_in_list \n\
"SELECT units.unit, unit_authors.address, \n\
main_chain_index AS mci, (+sequence='good') AS good \n\
FROM parenthoods \n\
JOIN units ON child_unit=units.unit \n\
LEFT JOIN unit_authors ON unit_authors.unit=units.unit AND address IN(?) \n\
Expand All @@ -186,8 +187,9 @@ function readDescendantUnitsByAuthorsBeforeMcIndex(conn, objEarlierUnitProps, ar
for (var i=0; i<rows.length; i++){
var objUnitProps = rows[i];
arrNewStartUnits.push(objUnitProps.unit);
if (objUnitProps.author_in_list)
arrUnits.push(objUnitProps.unit);
if (objUnitProps.address) {
arrUnits.push(objUnitProps);
}
}
profiler.stop('mc-wc-descendants-goDown');
(arrNewStartUnits.length > 0) ? goDown(arrNewStartUnits) : handleUnits(arrUnits);
Expand All @@ -197,14 +199,21 @@ function readDescendantUnitsByAuthorsBeforeMcIndex(conn, objEarlierUnitProps, ar

profiler.start();

conn.query( // _left_ join forces use of indexes in units
"SELECT unit FROM units "+db.forceIndex("byMcIndex")+" LEFT JOIN unit_authors USING(unit) \n\
WHERE latest_included_mc_index>=? AND main_chain_index>? AND main_chain_index<=? AND latest_included_mc_index<? AND address IN(?)",
[objEarlierUnitProps.main_chain_index, objEarlierUnitProps.main_chain_index, to_main_chain_index, to_main_chain_index, arrAuthorAddresses],
// "SELECT unit FROM units WHERE latest_included_mc_index>=? AND main_chain_index<=?",
// [objEarlierUnitProps.main_chain_index, to_main_chain_index],
conn.query(
"SELECT units.unit, \n\
main_chain_index AS mci, (+sequence='good') AS good, \n\
unit_authors.address \n\
FROM units \n" +
db.forceIndex("byMcIndex") +
"LEFT JOIN unit_authors USING(unit) \n\
WHERE main_chain_index BETWEEN (?)+1 AND ? \n\
AND latest_included_mc_index BETWEEN ? AND (?)-1 ",
[objEarlierUnitProps.main_chain_index,
to_main_chain_index,
objEarlierUnitProps.main_chain_index,
to_main_chain_index],
function(rows){
arrUnits = rows.map(function(row) { return row.unit; });
arrUnits = rows.filter(function(row){ return arrAuthorAddresses.indexOf(row.address) !== -1; });
profiler.stop('mc-wc-descendants-initial');
goDown([objEarlierUnitProps.unit]);
}
Expand Down
65 changes: 39 additions & 26 deletions paid_witnessing.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,35 +192,48 @@ function buildPaidWitnesses(conn, objUnitProps, arrWitnesses, onDone){
graph.readDescendantUnitsByAuthorsBeforeMcIndex(conn, objUnitProps, arrWitnesses, to_main_chain_index, function(arrUnits){
rt+=Date.now()-t;
t=Date.now();
var strUnitsList = (arrUnits.length === 0) ? 'NULL' : arrUnits.map(function(unit){ return conn.escape(unit); }).join(', ');
//throw "no witnesses before mc "+to_main_chain_index+" for unit "+objUnitProps.unit;
profiler.start();
conn.query( // we don't care if the unit is majority witnessed by the unit-designated witnesses
// _left_ join forces use of indexes in units
// can't get rid of filtering by address because units can be co-authored by witness with somebody else
"SELECT address, MIN(main_chain_index-?) AS delay \n\
FROM units \n\
LEFT JOIN unit_authors USING(unit) \n\
WHERE unit IN("+strUnitsList+") AND address IN(?) AND +sequence='good' \n\
GROUP BY address",
[objUnitProps.main_chain_index, arrWitnesses],
function(rows){
et += Date.now()-t;
var count_paid_witnesses = rows.length;
var arrValues;
if (count_paid_witnesses === 0){ // nobody witnessed, pay equally to all
count_paid_witnesses = arrWitnesses.length;
arrValues = arrWitnesses.map(function(address){ return "("+conn.escape(unit)+", "+conn.escape(address)+", NULL)"; });
}
else
arrValues = rows.map(function(row){ return "("+conn.escape(unit)+", "+conn.escape(row.address)+", "+row.delay+")"; });
profiler.stop('mc-wc-select-events');
profiler.start();
conn.query("INSERT INTO paid_witness_events_tmp (unit, address, delay) VALUES "+arrValues.join(", "), function(){
updateCountPaidWitnesses(count_paid_witnesses);
});

arrUnits.forEach(function(objUnit){
objUnit.delay = objUnit.mci - objUnitProps.main_chain_index;
});

let minDelayByAddress = {};
arrUnits.forEach(function(row){
if (!row.good) { return; }
let address = row.address;
minDelayByAddress[address] =
Math.min(row.delay, minDelayByAddress[address] || +Infinity);
});
let rows = Object.keys(minDelayByAddress).map(function(address){
return { address: address, delay: minDelayByAddress[address] };
});

let count_paid_witnesses = 0;
rows.map(function(row){
if (arrWitnesses.indexOf(row.address) !== -1) {
count_paid_witnesses++;
} else {
// failpath
row._skip = true;
}
);
});

et += Date.now()-t;
//var count_paid_witnesses = rows.length;
var arrValues;
if (count_paid_witnesses === 0){ // nobody witnessed, pay equally to all
count_paid_witnesses = arrWitnesses.length;
arrValues = arrWitnesses.map(function(address){ return "("+conn.escape(unit)+", "+conn.escape(address)+", NULL)"; });
}
else
arrValues = rows.filter(function(row){ return !row._skip; }).map(function(row){ return "("+conn.escape(unit)+", "+conn.escape(row.address)+", "+row.delay+")"; });
profiler.stop('mc-wc-select-events');
profiler.start();
conn.query("INSERT INTO paid_witness_events_tmp (unit, address, delay) VALUES "+arrValues.join(", "), function(){
updateCountPaidWitnesses(count_paid_witnesses);
});
});

}
Expand Down