Skip to content

Commit

Permalink
when composing, do not allow witnessed level to retreat
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyofbyteball committed Nov 6, 2017
1 parent 42a04ac commit cb513a3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
26 changes: 20 additions & 6 deletions parent_composer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,24 @@ function pickParentUnits(conn, arrWitnesses, onDone){
var count_required_matches = constants.COUNT_WITNESSES - constants.MAX_WITNESS_LIST_MUTATIONS;
// we need at least one compatible parent, otherwise go deep
if (rows.filter(function(row){ return (row.count_matching_witnesses >= count_required_matches); }).length === 0)
return pickDeepParentUnits(conn, arrWitnesses, onDone);
onDone(null, rows.map(function(row){ return row.unit; }));
return pickDeepParentUnits(conn, arrWitnesses, null, onDone);
var arrParentUnits = rows.map(function(row){ return row.unit; });
checkWitnessedLevelNotRetreatingAndLookDeeper(conn, arrWitnesses, arrParentUnits, onDone);
}
);
}

// if we failed to find compatible parents among free units.
// (This may be the case if an attacker floods the network trying to shift the witness list)
function pickDeepParentUnits(conn, arrWitnesses, onDone){
function pickDeepParentUnits(conn, arrWitnesses, max_wl, onDone){
// fixed: an attacker could cover all free compatible units with his own incompatible ones, then those that were not on MC will be never included
//var cond = bDeep ? "is_on_main_chain=1" : "is_free=1";

var and_wl = (max_wl === null) ? '' : "AND witnessed_level<="+max_wl;
conn.query(
"SELECT unit \n\
FROM units \n\
WHERE +sequence='good' \n\
WHERE +sequence='good' "+and_wl+" \n\
AND ( \n\
SELECT COUNT(*) \n\
FROM unit_witnesses \n\
Expand All @@ -58,11 +60,23 @@ function pickDeepParentUnits(conn, arrWitnesses, onDone){
function(rows){
if (rows.length === 0)
return onDone("failed to find compatible parents: no deep units");
onDone(null, rows.map(function(row){ return row.unit; }));
var arrParentUnits = rows.map(function(row){ return row.unit; });
checkWitnessedLevelNotRetreatingAndLookDeeper(conn, arrWitnesses, arrParentUnits, onDone);
}
);
}

function checkWitnessedLevelNotRetreatingAndLookDeeper(conn, arrWitnesses, arrParentUnits, onDone){
storage.determineWitnessedLevelAndBestParent(conn, arrParentUnits, arrWitnesses, function(witnessed_level, best_parent_unit){
storage.readStaticUnitProps(conn, best_parent_unit, function(props){
if (witnessed_level >= props.witnessed_level)
return onDone(null, arrParentUnits);
console.log("witness level would retreat if parents = "+arrParentUnits.join(', ')+", will look for older parents");
pickDeepParentUnits(conn, arrWitnesses, witnessed_level, onDone);
});
});
}

function findLastStableMcBall(conn, arrWitnesses, onDone){
conn.query(
"SELECT ball, unit, main_chain_index FROM units JOIN balls USING(unit) \n\
Expand Down Expand Up @@ -94,7 +108,7 @@ function adjustLastStableMcBallAndParents(conn, last_stable_mc_ball_unit, arrPar
}
console.log('will adjust last stable ball because '+last_stable_mc_ball_unit+' is not stable in view of parents '+arrParentUnits.join(', '));
if (arrParentUnits.length > 1){ // select only one parent
pickDeepParentUnits(conn, arrWitnesses, function(err, arrAdjustedParentUnits){
pickDeepParentUnits(conn, arrWitnesses, null, function(err, arrAdjustedParentUnits){
if (err)
throw Error("pickDeepParentUnits in adjust failed: "+err);
adjustLastStableMcBallAndParents(conn, last_stable_mc_ball_unit, arrAdjustedParentUnits, arrWitnesses, handleAdjustedLastStableUnit);
Expand Down
35 changes: 34 additions & 1 deletion storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,38 @@ function determineIfWitnessAddressDefinitionsHaveReferences(conn, arrWitnesses,
);
}

function determineWitnessedLevelAndBestParent(conn, arrParentUnits, arrWitnesses, handleWitnessedLevelAndBestParent){
var arrCollectedWitnesses = [];
var my_best_parent_unit;

function addWitnessesAndGoUp(start_unit){
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 updateWitnessedLevel");
if (level === 0) // genesis
return handleWitnessedLevelAndBestParent(0, null);

This comment has been minimized.

Copy link
@pmiklos

pmiklos Nov 23, 2017

Contributor

When determineWitnessedLevelAndBestParent is called from parent_composer.determineWitnessedLevels() and level is 0, the best_parent_unit is set to null which causes an error in storage.readStaticUnitProps

I found this error when I was rerunning the byteball-devnet from scratch. The genesis unit is created, but then the second unit, the blackbytes definition fails.

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);
}
(arrCollectedWitnesses.length < constants.MAJORITY_OF_WITNESSES)
? addWitnessesAndGoUp(best_parent_unit) : handleWitnessedLevelAndBestParent(level, my_best_parent_unit);
});
});
}

determineBestParent(conn, {parent_units: arrParentUnits, witness_list_unit: 'none'}, arrWitnesses, function(best_parent_unit){
if (!best_parent_unit)
throw Error("no best parent of "+arrParentUnits.join(', '));
my_best_parent_unit = best_parent_unit;
addWitnessesAndGoUp(best_parent_unit);
});
}


/*
function readWitnessesOnMcUnit(conn, main_chain_index, handleWitnesses){
Expand Down Expand Up @@ -655,7 +687,7 @@ function isGenesisBall(ball){

function readUnitProps(conn, unit, handleProps){
conn.query(
"SELECT unit, level, latest_included_mc_index, main_chain_index, is_on_main_chain, is_free, is_stable FROM units WHERE unit=?",
"SELECT unit, level, latest_included_mc_index, main_chain_index, is_on_main_chain, is_free, is_stable, witnessed_level FROM units WHERE unit=?",
[unit],
function(rows){
if (rows.length !== 1)
Expand Down Expand Up @@ -1371,6 +1403,7 @@ exports.loadAssetWithListOfAttestedAuthors = loadAssetWithListOfAttestedAuthors;

exports.filterNewOrUnstableUnits = filterNewOrUnstableUnits;

exports.determineWitnessedLevelAndBestParent = determineWitnessedLevelAndBestParent;
exports.determineBestParent = determineBestParent;
exports.determineIfHasWitnessListMutationsAlongMc = determineIfHasWitnessListMutationsAlongMc;

Expand Down

0 comments on commit cb513a3

Please sign in to comment.