mc/byzantine.js
function initByzantine(){
    if(!conf.IF_BYZANTINE)
        return;
    if(bByzantineUnderWay)
        return;
 
    db.query(
        "SELECT main_chain_index FROM units \n\
        WHERE is_on_main_chain=1 AND is_stable=1 AND +sequence='good' AND pow_type=? \n\
        ORDER BY main_chain_index DESC LIMIT 1", 
        [constants.POW_TYPE_TRUSTME], 
        function(rows){
            var hp = 1;     // just after genesis or catchup from fresh start
            if (rows.length === 0){  
                db.query(
                    "SELECT main_chain_index FROM units \n\
                    WHERE unit=?", 
                    [constants.GENESIS_UNIT],
                    function(rowGenesis){
                        if(rowGenesis.length === 0){
                            setTimeout(function(){
                                initByzantine();
                            }, 3000);
                        }
                        else{
                            startPhase(hp, 0);
                        }
                    }
                );
            }
            else if (rows.length === 1){  
                hp = rows[0].main_chain_index + 1;
                    
                if(maxGossipHp === hp) {
                    startPhase(hp, maxGossipPp);
                }
                else {
                    setTimeout(function(){
                        initByzantine();
                    }, 3000);
                }
            }
        }
    );  
}
function initByzantine(){
    if(!conf.IF_BYZANTINE)
        return;
    if(bByzantineUnderWay)
        return;
 
    db.query(
        "SELECT main_chain_index FROM units \n\
        WHERE is_on_main_chain=1 AND is_stable=1 AND +sequence='good' AND pow_type=? \n\
        ORDER BY main_chain_index DESC LIMIT 1", 
        [constants.POW_TYPE_TRUSTME], 
        function(rows){
            var hp = 1;     // just after genesis or catchup from fresh start
            if (rows.length === 0){  
                db.query(
                    "SELECT main_chain_index FROM units \n\
                    WHERE unit=?", 
                    [constants.GENESIS_UNIT],
                    function(rowGenesis){
                        if(rowGenesis.length === 0){
                            setTimeout(function(){
                                initByzantine();
                            }, 3000);
                        }
                        else{
                            startPhase(hp, 0);
                        }
                    }
                );
            }
            else if (rows.length === 1){  
                hp = rows[0].main_chain_index + 1;
                    
                if(maxGossipHp === hp) {
                    startPhase(hp, maxGossipPp);
                }
                else {
                    setTimeout(function(){
                        initByzantine();
                    }, 3000);
                }
            }
        }
    );  
}
function initByzantine ( ) { if ( ! conf . IF_BYZANTINE ) return ; if ( bByzantineUnderWay ) return ; db . query ( "SELECT main_chain_index FROM units \n\
        WHERE is_on_main_chain=1 AND is_stable=1 AND +sequence='good' AND pow_type=? \n\
        ORDER BY main_chain_index DESC LIMIT 1" , [ constants . POW_TYPE_TRUSTME ] , function ( rows ) { var hp = 1 ; // just after genesis or catchup from fresh start if ( rows . length === 0 ) { db . query ( "SELECT main_chain_index FROM units \n\
                    WHERE unit=?" , [ constants . GENESIS_UNIT ] , function ( rowGenesis ) { if ( rowGenesis . length === 0 ) { setTimeout ( function ( ) { initByzantine ( ) ; } , 3000 ) ; } else { startPhase ( hp , 0 ) ; } } ) ; } else if ( rows . length === 1 ) { hp = rows [ 0 ] . main_chain_index + 1 ; if ( maxGossipHp === hp ) { startPhase ( hp , maxGossipPp ) ; } else { setTimeout ( function ( ) { initByzantine ( ) ; } , 3000 ) ; } } } ) ; }
init function begin 
init byzantine, executes at startup
init function begin init byzantine executes at startup