<commit_before>var sys = require('sys');
var logger = require('./logger');
var Util = require('./util');

var Accounting = exports.Accounting = function (storage, blockChain) {
	events.EventEmitter.call(this);

	this.storage = storage;

	var self = this;

	var Block = this.storage.Block;
	var Transaction = this.storage.Transaction;
	var Account = this.storage.Account;

	var addTxSynchro = Util.createSynchrotron(function (next, pubKey, tx, height, index) {
		Account.collection.update(
			// Find the account index for this public key
			{ pubKeyHash: pubKey.toString('base64') },
			// Atomically push this transaction
			{ $addToSet : { "txs" : {
				tx: tx.getHash().toString('base64'),
				height: height,
				n: index
			} } },
			// Insert if not exists
			{ upsert : true },
			// Callback
			function (err) {
				if (err) {
					logger.error("Error while registering tx for " +
								  "pub key " + Util.formatBuffer(outPubKey) +
								  ": " + err);
				}

				next();
			}
		);
	});

	this.handleTransaction = function (e) {
		var affectedAccounts = e.tx.getAffectedAccounts();

		for (var i in affectedAccounts) {
			if (affectedAccounts.hasOwnProperty(i)) {
				addTxSynchro(i, affectedAccounts[i], e.tx, e.block.height, e.index);

				// Notify anybody listening to this pubkey
				this.emit('txAdd:'+i, e);
			}
		}
	};

	blockChain.addListener('txAdd', this.handleTransaction.bind(this));
};

sys.inherits(Accounting, events.EventEmitter);<commit_msg>Add passing on of txRevoke messages via accounting.<commit_after>var sys = require('sys');
var logger = require('./logger');
var Util = require('./util');

var Accounting = exports.Accounting = function (storage, blockChain) {
	events.EventEmitter.call(this);

	this.storage = storage;

	var self = this;

	var Block = this.storage.Block;
	var Transaction = this.storage.Transaction;
	var Account = this.storage.Account;

	var addTxSynchro = Util.createSynchrotron(function (next, pubKey, tx, height, index) {
		Account.collection.update(
			// Find the account index for this public key
			{ pubKeyHash: pubKey.toString('base64') },
			// Atomically push this transaction
			{ $addToSet : { "txs" : {
				tx: tx.getHash().toString('base64'),
				height: height,
				n: index
			} } },
			// Insert if not exists
			{ upsert : true },
			// Callback
			function (err) {
				if (err) {
					logger.error("Error while registering tx for " +
								  "pub key " + Util.formatBuffer(outPubKey) +
								  ": " + err);
				}

				next();
			}
		);
	});

	this.handleAdd = function (e) {
		var affectedAccounts = e.tx.getAffectedAccounts();

		for (var i in affectedAccounts) {
			if (affectedAccounts.hasOwnProperty(i)) {
				addTxSynchro(i, affectedAccounts[i], e.tx, e.block.height, e.index);

				// Notify anybody listening to this pubkey
				this.emit('txAdd:'+i, e);
			}
		}
	};

	this.handleRevoke = function (e) {
		var affectedAccounts = e.tx.getAffectedAccounts();

		for (var i in affectedAccounts) {
			if (affectedAccounts.hasOwnProperty(i)) {
				// TODO: Remove tx from accounting index

				// Notify anybody listening to this pubkey
				this.emit('txRevoke:'+i, e);
			}
		}
	};

	blockChain.addListener('txAdd', this.handleAdd.bind(this));
	blockChain.addListener('txRevoke', this.handleRevoke.bind(this));
};

sys.inherits(Accounting, events.EventEmitter);
{'fraction_non_alphanumeric': 0.14285714285714285, 'fraction_numerical': 0.002390914524805738, 'mean_word_length': 3.346753246753247, 'pattern_counts': {'":': 2, '<': 3, '<?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': '1729369', 'n_tokens_mistral': 1289, 'n_tokens_neox': 1075, 'n_words': 259}