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

implement a more accurate algorithm to find min_mc_wl in updateStableMcFlag() #145

Merged
merged 5 commits into from
Nov 21, 2018
Merged
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
1 change: 1 addition & 0 deletions divisible_asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ function composeDivisibleAssetPaymentJoint(params){
minimal: params.minimal,
outputs: arrBaseOutputs,
messages:params.messages,
spend_unconfirmed: params.spend_unconfirmed || 'own',
// function that creates additional messages to be added to the joint
retrieveMessages: function(conn, last_ball_mci, bMultiAuthored, arrPayingAddresses, onDone){
var arrAssetPayingAddresses = _.intersection(arrPayingAddresses, params.paying_addresses);
Expand Down
1 change: 1 addition & 0 deletions indivisible_asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,7 @@ function composeIndivisibleAssetPaymentJoint(params){
signing_addresses: params.signing_addresses,
minimal: params.minimal,
outputs: arrBaseOutputs,
spend_unconfirmed: params.spend_unconfirmed || 'own',

// function that creates additional messages to be added to the joint
retrieveMessages: function createAdditionalMessages(conn, last_ball_mci, bMultiAuthored, arrPayingAddresses, onDone){
Expand Down
51 changes: 40 additions & 11 deletions main_chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,38 @@ function updateMainChain(conn, from_unit, last_added_unit, onDone){
});
}

function findMinMcWitnessedLevel(tip_unit, first_unstable_mc_level, arrWitnesses, handleMinMcWl){
var arrCollectedWitnesses = [];
var min_mc_wl = Number.POSITIVE_INFINITY;

function addWitnessesAndGoUp(start_unit){
storage.readStaticUnitProps(conn, start_unit, function(props){
var best_parent_unit = props.best_parent_unit;
var level = props.level;
if (level === null)
throw Error("null level in findMinMcWitnessedLevel");
if (level < first_unstable_mc_level) {
console.log("unit " + start_unit + ", level=" + level + ", first_unstable_mc_level=" + first_unstable_mc_level + ", min_mc_wl=" + min_mc_wl);
return handleMinMcWl(-1);
}
storage.readUnitAuthors(conn, start_unit, function(arrAuthors){
for (var i=0; i<arrAuthors.length; i++){
var address = arrAuthors[i];
if (arrWitnesses.indexOf(address) !== -1 && arrCollectedWitnesses.indexOf(address) === -1) {
arrCollectedWitnesses.push(address);
var witnessed_level = props.witnessed_level;
if (min_mc_wl > witnessed_level)
min_mc_wl = witnessed_level;
}
}
(arrCollectedWitnesses.length < constants.MAJORITY_OF_WITNESSES)
? addWitnessesAndGoUp(best_parent_unit) : handleMinMcWl(min_mc_wl);
});
});
}

addWitnessesAndGoUp(tip_unit);
}

function updateStableMcFlag(){
console.log("updateStableMcFlag");
Expand Down Expand Up @@ -447,20 +479,17 @@ function updateMainChain(conn, from_unit, last_added_unit, onDone){
markMcIndexStable(conn, first_unstable_mc_index, updateStableMcFlag);
}

conn.query("SELECT witnessed_level FROM units WHERE is_free=1 AND is_on_main_chain=1", function(wl_rows){
conn.query("SELECT unit FROM units WHERE is_free=1 AND is_on_main_chain=1", function(wl_rows){
if (wl_rows.length !== 1)
throw Error("not a single mc wl");
// this is the level when we colect 7 witnesses if walking up the MC from its end
var mc_end_witnessed_level = wl_rows[0].witnessed_level;
conn.query(
// among these 7 witnesses, find min wl
"SELECT MIN(witnessed_level) AS min_mc_wl FROM units LEFT JOIN unit_authors USING(unit) \n\
WHERE is_on_main_chain=1 AND level>=? AND address IN(?)", // _left_ join enforces the best query plan in sqlite
[mc_end_witnessed_level, arrWitnesses],
function(min_wl_rows){
if (min_wl_rows.length !== 1)
throw Error("not a single min mc wl");
var min_mc_wl = min_wl_rows[0].min_mc_wl;
var tip_unit = wl_rows[0].unit;
findMinMcWitnessedLevel(tip_unit, first_unstable_mc_level, arrWitnesses,
function(min_mc_wl){
console.log("minimum witnessed level "+min_mc_wl);
if (min_mc_wl == -1)
return finish();

if (arrAltBranchRootUnits.length === 0){ // no alt branches
if (min_mc_wl >= first_unstable_mc_level)
return advanceLastStableMcUnitAndTryNext();
Expand Down