main_chain.js
function determineIfStableInLaterUnitsAndUpdateStableMcFlag(conn, earlier_unit, arrLaterUnits, bStableInDb, handleResult){
	determineIfStableInLaterUnits(conn, earlier_unit, arrLaterUnits, function(bStable){
		console.log("determineIfStableInLaterUnits", earlier_unit, arrLaterUnits, bStable);
		if (!bStable)
			return handleResult(bStable);
		if (bStable && bStableInDb)
			return handleResult(bStable);
		breadcrumbs.add('stable in parents, will wait for write lock');
		mutex.lock(["write"], function(unlock){
			breadcrumbs.add('stable in parents, got write lock');
			storage.readLastStableMcIndex(conn, function(last_stable_mci){
				storage.readUnitProps(conn, earlier_unit, function(objEarlierUnitProps){
					var new_last_stable_mci = objEarlierUnitProps.main_chain_index;
					if (new_last_stable_mci <= last_stable_mci) // fix: it could've been changed by parallel tasks - No, our SQL transaction doesn't see the changes
						throw Error("new last stable mci expected to be higher than existing");
					var mci = last_stable_mci;
					advanceLastStableMcUnitAndStepForward();

					function advanceLastStableMcUnitAndStepForward(){
						mci++;
						if (mci <= new_last_stable_mci)
							markMcIndexStable(conn, mci, advanceLastStableMcUnitAndStepForward);
						else{
							unlock();
							handleResult(bStable, true);
						}
					}            
				});
			});
		});
	});
}
function determineIfStableInLaterUnitsAndUpdateStableMcFlag(conn, earlier_unit, arrLaterUnits, bStableInDb, handleResult){
	determineIfStableInLaterUnits(conn, earlier_unit, arrLaterUnits, function(bStable){
		console.log("determineIfStableInLaterUnits", earlier_unit, arrLaterUnits, bStable);
		if (!bStable)
			return handleResult(bStable);
		if (bStable && bStableInDb)
			return handleResult(bStable);
		breadcrumbs.add('stable in parents, will wait for write lock');
		mutex.lock(["write"], function(unlock){
			breadcrumbs.add('stable in parents, got write lock');
			storage.readLastStableMcIndex(conn, function(last_stable_mci){
				storage.readUnitProps(conn, earlier_unit, function(objEarlierUnitProps){
					var new_last_stable_mci = objEarlierUnitProps.main_chain_index;
					if (new_last_stable_mci <= last_stable_mci) // fix: it could've been changed by parallel tasks - No, our SQL transaction doesn't see the changes
						throw Error("new last stable mci expected to be higher than existing");
					var mci = last_stable_mci;
					advanceLastStableMcUnitAndStepForward();

					function advanceLastStableMcUnitAndStepForward(){
						mci++;
						if (mci <= new_last_stable_mci)
							markMcIndexStable(conn, mci, advanceLastStableMcUnitAndStepForward);
						else{
							unlock();
							handleResult(bStable, true);
						}
					}            
				});
			});
		});
	});
}
function determineIfStableInLaterUnitsAndUpdateStableMcFlag ( conn , earlier_unit , arrLaterUnits , bStableInDb , handleResult ) { determineIfStableInLaterUnits ( conn , earlier_unit , arrLaterUnits , function ( bStable ) { console . log ( "determineIfStableInLaterUnits" , earlier_unit , arrLaterUnits , bStable ) ; if ( ! bStable ) return handleResult ( bStable ) ; if ( bStable && bStableInDb ) return handleResult ( bStable ) ; breadcrumbs . add ( 'stable in parents, will wait for write lock' ) ; mutex . lock ( [ "write" ] , function ( unlock ) { breadcrumbs . add ( 'stable in parents, got write lock' ) ; storage . readLastStableMcIndex ( conn , function ( last_stable_mci ) { storage . readUnitProps ( conn , earlier_unit , function ( objEarlierUnitProps ) { var new_last_stable_mci = objEarlierUnitProps . main_chain_index ; if ( new_last_stable_mci <= last_stable_mci ) // fix: it could've been changed by parallel tasks - No, our SQL transaction doesn't see the changes throw Error ( "new last stable mci expected to be higher than existing" ) ; var mci = last_stable_mci ; advanceLastStableMcUnitAndStepForward ( ) ; function advanceLastStableMcUnitAndStepForward ( ) { mci ++ ; if ( mci <= new_last_stable_mci ) markMcIndexStable ( conn , mci , advanceLastStableMcUnitAndStepForward ) ; else { unlock ( ) ; handleResult ( bStable , true ) ; } } } ) ; } ) ; } ) ; } ) ; }
It is assumed earlier_unit is not marked as stable yet If it appears to be stable, its MC index will be marked as stable, as well as all preceeding MC indexes
It is assumed earlier_unit is not marked as stable yet If it appears to be stable its MC index will be marked as stable as well as all preceeding MC indexes