<commit_before>var util = require('../util');
var Script = require('./Script');
var Bignum = require('bignum');
var Binary = require('binary');
var Step = require('step');
var buffertools = require('buffertools');
var Transaction = require('./Transaction');
var TransactionIn = Transaction.In;
var TransactionOut = Transaction.Out;
var COINBASE_OP = Transaction.COINBASE_OP;
var VerificationError = require('../util/error').VerificationError;
var BlockRules = {
  maxTimeOffset: 2 * 60 * 60, // How far block timestamps can be into the future
  //largestHash: (new Bignum(2)).pow(256)
  //largestHash: new Bignum('115792089237316195423570985008687907853269984665640564039457584007913129639936') // = 2^256
  largestHash: new Bignum('10000000000000000000000000000000000000000000000000000000000000000', 16),
  checkPowFromTimestamp: 1394048078,
  lastPowVersion: 2,
  lastPowHeight: 260799
};

function Block(data) {
  if ("object" !== typeof data) {
    data = {};
  }
  this.hash = data.hash || null;
  this.prev_hash = data.prev_hash || util.NULL_HASH;
  this.merkle_root = data.merkle_root || util.NULL_HASH;
  this.timestamp = data.timestamp || 0;
  this.bits = data.bits || 0;
  this.nonce = data.nonce || 0;
  this.version = data.version || 0;
  this.height = data.height || 0;
  this.size = data.size || 0;
  this.active = data.active || false;
  this.chainWork = data.chainWork || util.EMPTY_BUFFER;
  this.txs = data.txs || [];
  this.signature = data.signature || util.EMPTY_BUFFER;
}

Block.prototype.getHeader = function getHeader() {
  var buf = new Buffer(80);
  var ofs = 0;
  buf.writeUInt32LE(this.version, ofs);
  ofs += 4;
  this.prev_hash.copy(buf, ofs);
  ofs += 32;
  this.merkle_root.copy(buf, ofs);
  ofs += 32;
  buf.writeUInt32LE(this.timestamp, ofs);
  ofs += 4;
  buf.writeUInt32LE(this.bits, ofs);
  ofs += 4;
  buf.writeUInt32LE(this.nonce, ofs);
  ofs += 4;
  return buf;
};

Block.prototype.parse = function parse(parser, headerOnly) {
  this.version = parser.word32le();
  this.prev_hash = parser.buffer(32);
  this.merkle_root = parser.buffer(32);
  this.timestamp = parser.word32le();
  this.bits = parser.word32le();
  this.nonce = parser.word32le();

  this.txs = [];
  this.size = 0;

  if (headerOnly)
    return;

  var txCount = parser.varInt();

  for (var i = 0; i < txCount; i++) {
    var tx = new Transaction();
    tx.parse(parser);
    this.txs.push(tx);
  }
  var lastByte = parser.buffer(1);

  //log.info('lastByte ', lastByte);
  if (lastByte.length > 0 && lastByte.readUInt8(0) > 0)
  {
    this.signature = parser.buffer(lastByte.readUInt8(0));
    //log.info('this.signature ' + this.signature.toString('hex'));
  }
};

Block.prototype.calcHash = function calcHash() {
  var header = this.getHeader();
  return util.scrypt(header);
};

Block.prototype.checkHash = function checkHash() {
  if (!this.hash || !this.hash.length) return false;
  return buffertools.compare(this.calcHash(), this.hash) == 0;
};

Block.prototype.getHash = function getHash() {
  if (!this.hash || !this.hash.length) this.hash = this.calcHash();
  return this.hash;
};

Block.prototype.isProofOfStake = function isProofOfStake() {<commit_msg>Correct serialisation of Block.<commit_after>var util = require('../util');
var Script = require('./Script');
var Bignum = require('bignum');
var Binary = require('binary');
var Step = require('step');
var buffertools = require('buffertools');
var Transaction = require('./Transaction');
var TransactionIn = Transaction.In;
var TransactionOut = Transaction.Out;
var COINBASE_OP = Transaction.COINBASE_OP;
var VerificationError = require('../util/error').VerificationError;
var BlockRules = {
  maxTimeOffset: 2 * 60 * 60, // How far block timestamps can be into the future
  //largestHash: (new Bignum(2)).pow(256)
  //largestHash: new Bignum('115792089237316195423570985008687907853269984665640564039457584007913129639936') // = 2^256
  largestHash: new Bignum('10000000000000000000000000000000000000000000000000000000000000000', 16),
  checkPowFromTimestamp: 1394048078,
  lastPowVersion: 2,
  lastPowHeight: 260799
};

function Block(data) {
  if ("object" !== typeof data) {
    data = {};
  }
  this.hash = data.hash || null;
  this.prev_hash = data.prev_hash || util.NULL_HASH;
  this.merkle_root = data.merkle_root || util.NULL_HASH;
  this.timestamp = data.timestamp || 0;
  this.bits = data.bits || 0;
  this.nonce = data.nonce || 0;
  this.version = data.version || 0;
  this.height = data.height || 0;
  this.size = data.size || 0;
  this.active = data.active || false;
  this.chainWork = data.chainWork || util.EMPTY_BUFFER;
  this.txs = data.txs || [];
  this.signature = data.signature || util.EMPTY_BUFFER;
}

Block.prototype.getHeader = function getHeader() {
  var buf = new Buffer(80);
  var ofs = 0;
  buf.writeUInt32LE(this.version, ofs);
  ofs += 4;
  this.prev_hash.copy(buf, ofs);
  ofs += 32;
  this.merkle_root.copy(buf, ofs);
  ofs += 32;
  buf.writeUInt32LE(this.timestamp, ofs);
  ofs += 4;
  buf.writeUInt32LE(this.bits, ofs);
  ofs += 4;
  buf.writeUInt32LE(this.nonce, ofs);
  ofs += 4;
  return buf;
};

Block.prototype.parse = function parse(parser, headerOnly) {
  this.version = parser.word32le();
  this.prev_hash = parser.buffer(32);
  this.merkle_root = parser.buffer(32);
  this.timestamp = parser.word32le();
  this.bits = parser.word32le();
  this.nonce = parser.word32le();

  this.txs = [];
  this.size = 0;

  if (headerOnly)
    return;

  var txCount = parser.varInt();

  for (var i = 0; i < txCount; i++) {
    var tx = new Transaction();
    tx.parse(parser);
    this.txs.push(tx);
  }

  if (this.version > BlockRules.lastPowVersion) {
      var lastByte = parser.buffer(1);
      //log.info('lastByte ', lastByte);
      if (lastByte.length > 0 && lastByte.readUInt8(0) > 0) {
          this.signature = parser.buffer(lastByte.readUInt8(0));
          //log.info('this.signature ' + this.signature.toString('hex'));
      }
  }
};

Block.prototype.calcHash = function calcHash() {
  var header = this.getHeader();
  return util.twoSha256(header);
};

Block.prototype.checkHash = function checkHash() {
  if (!this.hash || !this.hash.length) return false;
  return buffertools.compare(this.calcHash(), this.hash) == 0;
};

Block.prototype.getHash = function getHash() {
  if (!this.hash || !this.hash.length) this.hash = this.calcHash();
  return this.hash;
};

Block.prototype.isProofOfStake = function isProofOfStake() {
{'fraction_non_alphanumeric': 0.15546935608999224, 'fraction_numerical': 0.06935608999224205, 'mean_word_length': 4.00077579519007, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '5951052', 'n_tokens_mistral': 2560, 'n_tokens_neox': 2215, 'n_words': 468}