<commit_before>var mysql = require('mysql');
var cluster = require('cluster');
module.exports = function(logger, poolConfig){

    var mposConfig = poolConfig.shareProcessing.mpos;
    var coin = poolConfig.coin.name;

    var connection;

    function connect(){
        connection = mysql.createConnection({
            host: mposConfig.host,
            port: mposConfig.port,
            user: mposConfig.user,
            password: <PASSWORD>,
            database: mposConfig.database
        });
        connection.connect(function(err){
            if (err)
                logger.error('mysql', 'Could not connect to mysql database: ' + JSON.stringify(err))
            else{
                logger.debug('mysql', 'Successful connection to MySQL database');
            }
        });
        connection.on('error', function(err){
            if(err.code === 'PROTOCOL_CONNECTION_LOST') {
                logger.warning('mysql', 'Lost connection to MySQL database, attempting reconnection...');
                connect();
            }
            else{
                logger.error('mysql', 'Database error: ' + JSON.stringify(err))
            }
        });
    }
    connect();

    this.handleAuth = function(workerName, password, authCallback){

        connection.query(
            'SELECT password FROM pool_worker WHERE username = LOWER(?)',
            [workerName],
            function(err, result){
                if (err){
                    logger.error('mysql', 'Database error when authenticating worker: ' +
                        JSON.stringify(err));
                    authCallback(false);
                }
                else if (!result[0])
                    authCallback(false);
                else if (data.authLevel === 'worker')
                    authCallback(true);
                else if (result[0].password === password)
                    authCallback(true)
                else
                    authCallback(false);
            }
        );

    };

    this.handleShare = function(isValidShare, isValidBlock, shareData){

        var dbData = [
            shareData.ip,
            shareData.worker,
            isValidShare ? 'Y' : 'N', 
            isValidBlock ? 'Y' : 'N',
            shareData.difficulty,
            typeof(shareData.error) === 'undefined' ? null : shareData.error,
            typeof(shareData.solution) === 'undefined' ? null : shareData.solution
        ];
        connection.query(
            'INSERT INTO `shares` SET time = NOW(), rem_host = ?, username = ?, our_result = ?, upstream_result = ?, difficulty = ?, reason = ?, solution = ?',
            dbData,
            function(err, result) {
                if (err)
                    logger.error('mysql', 'Insert error when adding share: ' +
                        JSON.stringify(err));<commit_msg>Bug in mpos compat auth level<commit_after>var mysql = require('mysql');
var cluster = require('cluster');
module.exports = function(logger, poolConfig){

    var mposConfig = poolConfig.shareProcessing.mpos;
    var coin = poolConfig.coin.name;

    var connection;

    function connect(){
        connection = mysql.createConnection({
            host: mposConfig.host,
            port: mposConfig.port,
            user: mposConfig.user,
            password: <PASSWORD>,
            database: mposConfig.database
        });
        connection.connect(function(err){
            if (err)
                logger.error('mysql', 'Could not connect to mysql database: ' + JSON.stringify(err))
            else{
                logger.debug('mysql', 'Successful connection to MySQL database');
            }
        });
        connection.on('error', function(err){
            if(err.code === 'PROTOCOL_CONNECTION_LOST') {
                logger.warning('mysql', 'Lost connection to MySQL database, attempting reconnection...');
                connect();
            }
            else{
                logger.error('mysql', 'Database error: ' + JSON.stringify(err))
            }
        });
    }
    connect();

    this.handleAuth = function(workerName, password, authCallback){

        connection.query(
            'SELECT password FROM pool_worker WHERE username = LOWER(?)',
            [workerName],
            function(err, result){
                if (err){
                    logger.error('mysql', 'Database error when authenticating worker: ' +
                        JSON.stringify(err));
                    authCallback(false);
                }
                else if (!result[0])
                    authCallback(false);
                else if (mposConfig.stratumAuth === 'worker')
                    authCallback(true);
                else if (result[0].password === password)
                    authCallback(true)
                else
                    authCallback(false);
            }
        );

    };

    this.handleShare = function(isValidShare, isValidBlock, shareData){

        var dbData = [
            shareData.ip,
            shareData.worker,
            isValidShare ? 'Y' : 'N', 
            isValidBlock ? 'Y' : 'N',
            shareData.difficulty,
            typeof(shareData.error) === 'undefined' ? null : shareData.error,
            typeof(shareData.solution) === 'undefined' ? null : shareData.solution
        ];
        connection.query(
            'INSERT INTO `shares` SET time = NOW(), rem_host = ?, username = ?, our_result = ?, upstream_result = ?, difficulty = ?, reason = ?, solution = ?',
            dbData,
            function(err, result) {
                if (err)
                    logger.error('mysql', 'Insert error when adding share: ' +
                        JSON.stringify(err));
{'fraction_non_alphanumeric': 0.11624649859943978, 'fraction_numerical': 0.0007002801120448179, 'mean_word_length': 1.6327188940092165, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1328017', 'n_tokens_mistral': 1452, 'n_tokens_neox': 1375, 'n_words': 320}