From 5fe57077d7879656c2c56314beb0d6015238277e Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Fri, 17 Jan 2014 15:25:15 -0300 Subject: [PATCH 1/5] hello world version of address specific socket --- app/controllers/socket.js | 20 ++++++++++++++------ lib/PeerSync.js | 1 - public/js/controllers/address.js | 15 ++++++++++++++- public/js/controllers/index.js | 17 +++++++++++++---- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/app/controllers/socket.js b/app/controllers/socket.js index 7bcad79..3be31a5 100644 --- a/app/controllers/socket.js +++ b/app/controllers/socket.js @@ -1,23 +1,31 @@ 'use strict'; // server-side socket behaviour - // io is a variable already taken in express var ios = null; module.exports.init = function(app, io_ext) { ios = io_ext; ios.set('log level', 1); // reduce logging - ios.sockets.on('connection', function() { + ios.sockets.on('connection', function(socket) { + socket.on('subscribe', function(topic) { + socket.join(topic); + if (topic !== 'inv') { + module.exports.broadcast_address_tx(topic, 'hello world'); + } + }); }); }; - module.exports.broadcast_tx = function(tx) { - ios.sockets.emit('tx', tx); + ios.sockets.in('inv').emit('tx', tx); }; - module.exports.broadcast_block = function(block) { - ios.sockets.emit('block', block); + ios.sockets.in('inv').emit('block', block); }; + +module.exports.broadcast_address_tx = function(address, tx) { + ios.sockets.in(address).emit('tx', tx); +}; + diff --git a/lib/PeerSync.js b/lib/PeerSync.js index 49526cf..9f5a106 100644 --- a/lib/PeerSync.js +++ b/lib/PeerSync.js @@ -15,7 +15,6 @@ function spec() { PeerSync.prototype.init = function(config, cb) { if (!config) config = {}; - var that = this; var network = config && (config.network || 'testnet'); diff --git a/public/js/controllers/address.js b/public/js/controllers/address.js index 926f3f8..afc5c91 100644 --- a/public/js/controllers/address.js +++ b/public/js/controllers/address.js @@ -1,6 +1,13 @@ 'use strict'; -angular.module('insight.address').controller('AddressController', ['$scope', '$routeParams', '$location', 'Global', 'Address', function ($scope, $routeParams, $location, Global, Address) { +angular.module('insight.address').controller('AddressController', + ['$scope', + '$routeParams', + '$location', + 'Global', + 'Address', + 'socket', + function ($scope, $routeParams, $location, Global, Address, socket) { $scope.global = Global; $scope.findOne = function() { @@ -10,6 +17,12 @@ angular.module('insight.address').controller('AddressController', ['$scope', '$r $scope.address = address; }); }; + socket.on('connect', function() { + socket.emit('subscribe', $routeParams.addrStr); + }); + socket.on('tx', function(data) { + console.log('Incoming message:', data); + }); $scope.params = $routeParams; }]); diff --git a/public/js/controllers/index.js b/public/js/controllers/index.js index 12eec9d..763f952 100755 --- a/public/js/controllers/index.js +++ b/public/js/controllers/index.js @@ -2,9 +2,18 @@ var TRANSACTION_DISPLAYED = 5; var BLOCKS_DISPLAYED = 5; -angular.module('insight.system').controller('IndexController', ['$scope', 'Global', 'socket', 'Blocks', 'Transactions', function($scope, Global, socket, Blocks, Transactions) { +angular.module('insight.system').controller('IndexController', + ['$scope', + 'Global', + 'socket', + 'Blocks', + 'Transactions', + function($scope, Global, socket, Blocks, Transactions) { $scope.global = Global; + socket.on('connect', function() { + socket.emit('subscribe', 'inv'); + }); socket.on('tx', function(tx) { console.log('Transaction received! ' + JSON.stringify(tx)); if ($scope.txs.length === TRANSACTION_DISPLAYED) { @@ -15,9 +24,9 @@ angular.module('insight.system').controller('IndexController', ['$scope', 'Globa socket.on('block', function(block) { console.log('Block received! ' + JSON.stringify(block)); - if ($scope.blocks.length === BLOCKS_DISPLAYED) { - $scope.blocks.pop(); - } + if ($scope.blocks.length === BLOCKS_DISPLAYED) { + $scope.blocks.pop(); + } $scope.blocks.unshift(block); }); From 6c7ecef5d44d93c75457ab4be10ca38c211db119 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Fri, 17 Jan 2014 16:49:08 -0300 Subject: [PATCH 2/5] socket api for address transactions --- app/controllers/socket.js | 3 -- app/models/Transaction.js | 40 ++++++++++++++++----------- public/js/controllers/address.js | 3 -- public/js/controllers/transactions.js | 27 ++++++++++++++++-- public/views/transaction.html | 2 +- 5 files changed, 49 insertions(+), 26 deletions(-) diff --git a/app/controllers/socket.js b/app/controllers/socket.js index 3be31a5..8f6d254 100644 --- a/app/controllers/socket.js +++ b/app/controllers/socket.js @@ -10,9 +10,6 @@ module.exports.init = function(app, io_ext) { ios.sockets.on('connection', function(socket) { socket.on('subscribe', function(topic) { socket.join(topic); - if (topic !== 'inv') { - module.exports.broadcast_address_tx(topic, 'hello world'); - } }); }); }; diff --git a/app/models/Transaction.js b/app/models/Transaction.js index fbb3f35..f432123 100644 --- a/app/models/Transaction.js +++ b/app/models/Transaction.js @@ -4,17 +4,18 @@ * Module dependencies. */ -var mongoose = require('mongoose'), - Schema = mongoose.Schema, - async = require('async'), - RpcClient = require('bitcore/RpcClient').class(), - Transaction = require('bitcore/Transaction').class(), - Address = require('bitcore/Address').class(), - BitcoreBlock= require('bitcore/Block').class(), - networks = require('bitcore/networks'), - util = require('bitcore/util/util'), - bignum = require('bignum'), - config = require('../../config/config'), +var mongoose = require('mongoose'), + Schema = mongoose.Schema, + async = require('async'), + RpcClient = require('bitcore/RpcClient').class(), + Transaction = require('bitcore/Transaction').class(), + Address = require('bitcore/Address').class(), + BitcoreBlock = require('bitcore/Block').class(), + networks = require('bitcore/networks'), + util = require('bitcore/util/util'), + bignum = require('bignum'), + config = require('../../config/config'), + sockets = require('../controllers/socket.js'), TransactionItem = require('./TransactionItem'); var CONCURRENCY = 5; @@ -94,8 +95,14 @@ TransactionSchema.statics.createFromArray = function(txs, time, next) { async.forEachLimit(txs, CONCURRENCY, function(txid, cb) { - that.explodeTransactionItems( txid, time, function(err) { + that.explodeTransactionItems( txid, time, function(err, addrs) { if (err) return next(err); + if (addrs) { + async.each(addrs, function(addr){ + sockets.broadcast_address_tx(addr, {'txid': txid}); + }); + + } that.create({txid: txid, time: time}, function(err, new_tx) { if (err && ! err.toString().match(/E11000/)) return cb(err); @@ -112,7 +119,7 @@ TransactionSchema.statics.createFromArray = function(txs, time, next) { TransactionSchema.statics.explodeTransactionItems = function(txid, time, cb) { - + var addrs = []; this.queryInfo(txid, function(err, info) { if (err || !info) return cb(err); @@ -124,7 +131,6 @@ TransactionSchema.statics.explodeTransactionItems = function(txid, time, cb) { async.forEachLimit(info.vin, CONCURRENCY, function(i, next_in) { if (i.addr && i.value) { -//console.log("Creating IN %s %d", i.addr, i.valueSat); TransactionItem.create({ txid : txid, value_sat : -1 * i.valueSat, @@ -132,6 +138,9 @@ TransactionSchema.statics.explodeTransactionItems = function(txid, time, cb) { index : i.n, ts : time, }, next_in); + if (addrs.indexOf(i.addr) === -1) { + addrs.push(i.addr); + } } else { if ( !i.coinbase ) { @@ -148,7 +157,6 @@ TransactionSchema.statics.explodeTransactionItems = function(txid, time, cb) { * TODO Support multisigs */ if (o.value && o.scriptPubKey && o.scriptPubKey.addresses && o.scriptPubKey.addresses[0]) { -//console.log("Creating OUT %s %d", o.scriptPubKey.addresses[0], o.valueSat); TransactionItem.create({ txid : txid, value_sat : o.valueSat, @@ -164,7 +172,7 @@ TransactionSchema.statics.explodeTransactionItems = function(txid, time, cb) { }, function (err) { if (err && ! err.toString().match(/E11000/)) return cb(err); - return cb(); + return cb(null, addrs); }); }); }); diff --git a/public/js/controllers/address.js b/public/js/controllers/address.js index afc5c91..c66351e 100644 --- a/public/js/controllers/address.js +++ b/public/js/controllers/address.js @@ -20,9 +20,6 @@ angular.module('insight.address').controller('AddressController', socket.on('connect', function() { socket.emit('subscribe', $routeParams.addrStr); }); - socket.on('tx', function(data) { - console.log('Incoming message:', data); - }); $scope.params = $routeParams; }]); diff --git a/public/js/controllers/transactions.js b/public/js/controllers/transactions.js index 665929a..9408862 100644 --- a/public/js/controllers/transactions.js +++ b/public/js/controllers/transactions.js @@ -1,13 +1,28 @@ 'use strict'; -angular.module('insight.transactions').controller('transactionsController', ['$scope', '$routeParams', '$location', 'Global', 'Transaction', 'TransactionsByBlock', 'TransactionsByAddress', function ($scope, $routeParams, $location, Global, Transaction, TransactionsByBlock, TransactionsByAddress) { +angular.module('insight.transactions').controller('transactionsController', + ['$scope', + '$routeParams', + '$location', + 'Global', + 'Transaction', + 'TransactionsByBlock', + 'TransactionsByAddress', + 'socket', + function ($scope, $routeParams, $location, Global, Transaction, TransactionsByBlock, TransactionsByAddress, socket) { $scope.global = Global; - $scope.findOne = function() { + + $scope.findThis = function() { + $scope.findTx($routeParams.txId); + }; + + $scope.findTx = function(txid) { Transaction.get({ - txId: $routeParams.txId + txId: txid }, function(tx) { $scope.tx = tx; + $scope.txs.push(tx); }); }; @@ -26,6 +41,12 @@ angular.module('insight.transactions').controller('transactionsController', ['$s $scope.txs = txs; }); }; + socket.on('tx', function(tx) { + console.log('Incoming message for new transaction!', tx); + $scope.findTx(tx.txid); + }); + + $scope.txs = []; }]); diff --git a/public/views/transaction.html b/public/views/transaction.html index 7ba4744..7ef3cd4 100644 --- a/public/views/transaction.html +++ b/public/views/transaction.html @@ -1,4 +1,4 @@ -
+