<commit_before>var util           = require('util');
var async          = require('async');
var request        = require('request');
var _              = require('underscore');
var events         = require('events');
var Status         = require('../lib/entity/status');
var logger         = require('../lib/logger')('peering');
var base58         = require('../lib/base58');
var moment         = require('moment');
var constants      = require('../lib/constants');
var localValidator = require('../lib/localValidator');

function PeeringService(conn, conf, pair, signFunc, dal) {
  
  var currency = conf.currency;

  var Peer        = require('../lib/entity/peer');

  var selfPubkey = undefined;
  this.pubkey = selfPubkey;

  var peer = null;
  var that = this;

  this.setKeyPair = function(keypair) {
    if (keypair) {
      pair = keypair;
      selfPubkey = base58.encode(pair.publicKey);
      that.pubkey = selfPubkey;
    }
  };

  this.setSignFunc = function(f) {
    signFunc = f;
  };

  this.peer = function (newPeer) {
    if (newPeer) {
      peer = newPeer;
    }
    return Peer.statics.peerize(peer);
  };

  this.load = function (done) {
  };

  this.submit = function(peering, done){
    var peer = new Peer(peering);
    var sp = peer.block.split('-');
    var number = sp[0];
    var sigTime = 0;
    async.waterfall([
      function (next) {
        localValidator(null).checkPeerSignature(peer, next);
      },
      function (next) {
        if (peer.block == constants.PEER.SPECIAL_BLOCK) {
          peer.statusTS = 0;
          peer.status = 'UP';
          next(null, null);
        }
        else
          // Check if document is based upon an existing block as time reference
          dal.getBlockOrNull(number, next);
      },
      function (block, next){
        sigTime = block ? block.medianTime : 0;
        peer.statusTS = sigTime;
        dal.getPeerOrNull(peer.pubkey, next);
      },
      function (found, next){
        var peerEntity = Peer.statics.peerize(found || peer);
        var previousHash = null;
        if(found){
          // Already existing peer
          var sp2 = found.block.split('-');
          var number2 = sp2[0];
          if(number <= number2){
            next(constants.ERROR.PEER.ALREADY_RECORDED);
            return;
          }
          peerEntity = Peer.statics.peerize(found);
          previousHash = peerEntity.hash;
          peer.copyValues(peerEntity);
          peerEntity.sigDate = new Date(sigTime*1000);
        }
        dal.savePeer(peerEntity, function (err) {
          next(err, peerEntity, previousHash);
        });
      },
      function (recordedPR, previousHash, next) {
        dal.updateMerkleForPeers(function(err) {
          next(err, Peer.statics.peerize(recordedPR));
        });
      }
    ], done);
  };

  this.submitStatus = function(obj, callback){
    var status = new Status(obj);
    var peer;
    var wasStatus = null;
    var sp = status.block.split('-');
    var number = sp[0], fpr = sp[1];
    var sigTime = new Date(0);
    async.waterfall([
      function (next) {
        if (status.to != that.pubkey) {
          next('Node is not concerned by this status');
          return;
        }
        localValidator(null).checkStatusSignature(status, next);
      },
      function (next) {
        if (status.block == constants.STATUS.SPECIAL_BLOCK)
          next(null, null);
        else
          // Check if document is based upon an existing block as time reference
          dal.getBlockOrNull(number, next);
      },
      function (block, next){
        sigTime = block ? block.medianTime : 0;
        dal.getPeer(status.from, next);
      },
      function (theOne, next){
        peer = theOne;
        if (peer.statusBlock) {
          var sp2 = peer.statusBlock.split('-');
          var number2 = sp2[0];
          if(number <= number2){
            next('Old status given');
            return;
          }
        }
        wasStatus = peer.status;
        peer.statusSigDate = new Date(sigTime*1000);
        peer.setStatus(status.status, dal, next);<commit_msg>Fix: status timestamp was not updated on STATUS reception<commit_after>var util           = require('util');
var async          = require('async');
var request        = require('request');
var _              = require('underscore');
var events         = require('events');
var Status         = require('../lib/entity/status');
var logger         = require('../lib/logger')('peering');
var base58         = require('../lib/base58');
var moment         = require('moment');
var constants      = require('../lib/constants');
var localValidator = require('../lib/localValidator');

function PeeringService(conn, conf, pair, signFunc, dal) {
  
  var currency = conf.currency;

  var Peer        = require('../lib/entity/peer');

  var selfPubkey = undefined;
  this.pubkey = selfPubkey;

  var peer = null;
  var that = this;

  this.setKeyPair = function(keypair) {
    if (keypair) {
      pair = keypair;
      selfPubkey = base58.encode(pair.publicKey);
      that.pubkey = selfPubkey;
    }
  };

  this.setSignFunc = function(f) {
    signFunc = f;
  };

  this.peer = function (newPeer) {
    if (newPeer) {
      peer = newPeer;
    }
    return Peer.statics.peerize(peer);
  };

  this.load = function (done) {
  };

  this.submit = function(peering, done){
    var peer = new Peer(peering);
    var sp = peer.block.split('-');
    var number = sp[0];
    var sigTime = 0;
    async.waterfall([
      function (next) {
        localValidator(null).checkPeerSignature(peer, next);
      },
      function (next) {
        if (peer.block == constants.PEER.SPECIAL_BLOCK) {
          peer.statusTS = 0;
          peer.status = 'UP';
          next(null, null);
        }
        else
          // Check if document is based upon an existing block as time reference
          dal.getBlockOrNull(number, next);
      },
      function (block, next){
        sigTime = block ? block.medianTime : 0;
        peer.statusTS = sigTime;
        dal.getPeerOrNull(peer.pubkey, next);
      },
      function (found, next){
        var peerEntity = Peer.statics.peerize(found || peer);
        var previousHash = null;
        if(found){
          // Already existing peer
          var sp2 = found.block.split('-');
          var number2 = sp2[0];
          if(number <= number2){
            next(constants.ERROR.PEER.ALREADY_RECORDED);
            return;
          }
          peerEntity = Peer.statics.peerize(found);
          previousHash = peerEntity.hash;
          peer.copyValues(peerEntity);
          peerEntity.sigDate = new Date(sigTime*1000);
        }
        dal.savePeer(peerEntity, function (err) {
          next(err, peerEntity, previousHash);
        });
      },
      function (recordedPR, previousHash, next) {
        dal.updateMerkleForPeers(function(err) {
          next(err, Peer.statics.peerize(recordedPR));
        });
      }
    ], done);
  };

  this.submitStatus = function(obj, callback){
    var status = new Status(obj);
    var peer;
    var wasStatus = null;
    var sp = status.block.split('-');
    var number = sp[0], fpr = sp[1];
    var sigTime = new Date(0);
    async.waterfall([
      function (next) {
        if (status.to != that.pubkey) {
          next('Node is not concerned by this status');
          return;
        }
        localValidator(null).checkStatusSignature(status, next);
      },
      function (next) {
        if (status.block == constants.STATUS.SPECIAL_BLOCK)
          next(null, null);
        else
          // Check if document is based upon an existing block as time reference
          dal.getBlockOrNull(number, next);
      },
      function (block, next){
        sigTime = block ? block.medianTime : 0;
        dal.getPeer(status.from, next);
      },
      function (theOne, next){
        peer = theOne;
        if (peer.statusBlock) {
          var sp2 = peer.statusBlock.split('-');
          var number2 = sp2[0];
          if(number <= number2){
            next('Old status given');
            return;
          }
        }
        wasStatus = peer.status;
        peer.statusTS = sigTime;
        peer.statusSigDate = new Date(sigTime*1000);
        peer.setStatus(status.status, dal, next);
{'fraction_non_alphanumeric': 0.1302089624350767, 'fraction_numerical': 0.007730402222490639, 'mean_word_length': 2.036303630363036, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '2735220', 'n_tokens_mistral': 2493, 'n_tokens_neox': 2422, 'n_words': 575}