<commit_before>var crypto = require('crypto-browserify');
var path = require('path');
var async = require('async');
var util = require('util');
var extend = require('extend');

var private = {}, self = null,
	library = null, modules = null;
private.lastBlock = null;
private.genesisBlock = null;
private.loaded = false;

function Blocks(cb, _library) {
	self = this;
	library = _library;

	try {
		private.genesisBlock = require(path.join(__dirname, "../../genesis.json"));
	} catch (e) {
		library.logger("failed genesis file");
	}

	private.lastBlock = private.genesisBlock;

	cb(null, self);
}

private.deleteBlock = function (blockId, cb) {
	modules.api.sql.remove({
		table: 'blocks',
		condition: {
			id: blockId
		}
	}, cb);
}

private.popLastBlock = function (oldLastBlock, cb) {
	if (!oldLastBlock.prevBlockId) {
		return cb("Can´t remove genesis block");
	}
	self.getBlock(function (err, previousBlock) {
		if (err || !previousBlock) {
			return cb(err || 'previousBlock is null');
		}
		previousBlock = self.readDbRows(previousBlock);

		var fee = 0;
		async.eachSeries(oldLastBlock.transactions.reverse(), function (transaction, cb) {
			async.series([
				function (cb) {
					fee += transaction.fee;
					modules.blockchain.transactions.undo(transaction, cb);
				}, function (cb) {
					modules.blockchain.transactions.undoUnconfirmed(transaction, cb);
				}
			], cb);
		}, function (err) {
			if (err) {
				library.logger(err);
				process.exit(0);
			}

			modules.accounts.undoMerging({
				publicKey: oldLastBlock.generatorPublicKey,
				balance: fee
			}, function (err) {
				private.deleteBlock(oldLastBlock.id, function (err) {
					if (err) {
						return cb(err);
					}

					cb(null, previousBlock[0]);
				});
			});
		});
	}, {id: oldLastBlock.prevBlockId});
}

private.verify = function (block, cb, scope) {
	if (block.id == private.genesisBlock.id) {
		try {
			var valid = modules.logic.block.verifySignature(block);
		} catch (e) {
			return cb(e.toString());
		}
		if (!valid) {
			return cb("wrong block");
		}
		return cb();
	} else {
		if (block.prevBlockId != (scope || private).lastBlock.id) {
			return cb("wrong prev block");
		}

		if (block.pointHeight < (scope || private).lastBlock.pointHeight) {
			return cb("wrong point height")
		}
	}

	modules.api.blocks.getBlock(block.pointId, function (err, cryptiBlock) {
		if (err || !block) {
			return cb(err || "block doesn´t exist in crypti");
		}

		/// what happening here?
		modules.api.sql.select({
			table: "blocks",
			condition: {
				id: block.pointId
			},
			fields: ["id"]
		}, function (err, found) {
			if (err || found.length) {
				return cb("wrong block");
			}

			/*
			why we validate signature again or why we validate on cryptiBlock?
			try {
				var valid = modules.logic.block.verifySignature(cryptiBlock);
			} catch (e) {
				return cb(e.toString());
			}


			if (!valid) {
				return cb("wrong block");
			}*/

			return cb();<commit_msg>fix verify<commit_after>var crypto = require('crypto-browserify');
var path = require('path');
var async = require('async');
var util = require('util');
var extend = require('extend');

var private = {}, self = null,
	library = null, modules = null;
private.lastBlock = null;
private.genesisBlock = null;
private.loaded = false;

function Blocks(cb, _library) {
	self = this;
	library = _library;

	try {
		private.genesisBlock = require(path.join(__dirname, "../../genesis.json"));
	} catch (e) {
		library.logger("failed genesis file");
	}

	private.lastBlock = private.genesisBlock;

	cb(null, self);
}

private.deleteBlock = function (blockId, cb) {
	modules.api.sql.remove({
		table: 'blocks',
		condition: {
			id: blockId
		}
	}, cb);
}

private.popLastBlock = function (oldLastBlock, cb) {
	if (!oldLastBlock.prevBlockId) {
		return cb("Can´t remove genesis block");
	}
	self.getBlock(function (err, previousBlock) {
		if (err || !previousBlock) {
			return cb(err || 'previousBlock is null');
		}
		previousBlock = self.readDbRows(previousBlock);

		var fee = 0;
		async.eachSeries(oldLastBlock.transactions.reverse(), function (transaction, cb) {
			async.series([
				function (cb) {
					fee += transaction.fee;
					modules.blockchain.transactions.undo(transaction, cb);
				}, function (cb) {
					modules.blockchain.transactions.undoUnconfirmed(transaction, cb);
				}
			], cb);
		}, function (err) {
			if (err) {
				library.logger(err);
				process.exit(0);
			}

			modules.accounts.undoMerging({
				publicKey: oldLastBlock.generatorPublicKey,
				balance: fee
			}, function (err) {
				private.deleteBlock(oldLastBlock.id, function (err) {
					if (err) {
						return cb(err);
					}

					cb(null, previousBlock[0]);
				});
			});
		});
	}, {id: oldLastBlock.prevBlockId});
}

private.verify = function (block, cb, scope) {
	if (block.id == private.genesisBlock.id) {
		try {
			var valid = modules.logic.block.verifySignature(block);
		} catch (e) {
			return cb(e.toString());
		}
		if (!valid) {
			return cb("wrong block");
		}
		return cb();
	} else {
		if (block.prevBlockId != (scope || private).lastBlock.id) {
			return cb("wrong prev block");
		}

		if (block.pointHeight < (scope || private).lastBlock.pointHeight) {
			return cb("wrong point height")
		}
	}

	modules.api.blocks.getBlock(block.pointId, function (err, cryptiBlock) {
		if (err || !cryptiBlock) {
			return cb(err || "block doesn´t exist in crypti");
		}

		modules.api.sql.select({
			table: "blocks",
			condition: {
				id: block.pointId
			},
			fields: ["id"]
		}, function (err, found) {
			if (err || found.length) {
				return cb("block exists in dapp");
			}

			try {
				var valid = modules.logic.block.verifySignature(block);
			} catch (e) {
				return cb(e.toString());
			}

			if (!valid) {
				return cb("can´t verify block signature");
			}

			return cb();
{'fraction_non_alphanumeric': 0.1596163726665525, 'fraction_numerical': 0.0010275732145915396, 'mean_word_length': 3.397590361445783, 'pattern_counts': {'":': 0, '<': 5, '<?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': '7221174', 'n_tokens_mistral': 2286, 'n_tokens_neox': 1928, 'n_words': 444}