Skip to content

Commit 3ab958e

Browse files
committed
Merge branch 'master' of github.com:bitpay/mystery into test/test-p2p-sync
2 parents b689fd1 + 6c39688 commit 3ab958e

File tree

21 files changed

+517
-46
lines changed

21 files changed

+517
-46
lines changed

app/controllers/status.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
var Status = require('../models/Status');
8+
var async = require('async');
9+
10+
/**
11+
* Status
12+
*/
13+
exports.show = function(req, res) {
14+
15+
if (! req.query.q) {
16+
res.status(400).send('Bad Request');
17+
}
18+
else {
19+
var s = req.query.q;
20+
var d = Status.new();
21+
22+
if (s == 'getInfo') {
23+
d.getInfo(function(err) {
24+
if (err) next(err);
25+
res.jsonp(d);
26+
});
27+
}
28+
else if (s == 'getDifficulty') {
29+
d.getDifficulty(function(err) {
30+
if (err) next(err);
31+
res.jsonp(d);
32+
});
33+
}
34+
else if (s == 'getTxOutSetInfo') {
35+
d.getTxOutSetInfo(function(err) {
36+
if (err) next(err);
37+
res.jsonp(d);
38+
});
39+
}
40+
else if (s == 'getBestBlockHash') {
41+
d.getBestBlockHash(function(err) {
42+
if (err) next(err);
43+
res.jsonp(d);
44+
});
45+
}
46+
else {
47+
res.status(400).send('Bad Request');
48+
}
49+
}
50+
};
51+

app/models/Block.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var BlockSchema = new Schema({
2626
unique: true,
2727
},
2828
time: Number,
29+
nextBlockHash: String,
2930
});
3031

3132
/**
@@ -42,19 +43,23 @@ BlockSchema.path('title').validate(function(title) {
4243
* Statics
4344
*/
4445

45-
BlockSchema.statics.createTimestamped = function(block, cb) {
46+
BlockSchema.statics.customCreate = function(block, cb) {
4647

4748
var That= this;
48-
var now = Math.round(new Date().getTime() / 1000);
4949

5050
var BlockSchema = mongoose.model('Block', BlockSchema);
51+
5152
var newBlock = new That();
52-
newBlock.time = now;
5353

54-
Transaction.createFromArray(block.tx, function(err, inserted_txs) {
54+
newBlock.time = block.time ? block.time : Math.round(new Date().getTime() / 1000);
55+
newBlock.hash = block.hash;
56+
newBlock.nextBlockHash = block.nextBlockHash;
57+
58+
Transaction.createFromArray(block.tx, newBlock.time, function(err, inserted_txs) {
5559
if (err) return cb(err);
60+
5661
newBlock.save(function(err) {
57-
return cb(err, inserted_txs);
62+
return cb(err, newBlock, inserted_txs);
5863
});
5964
});
6065
};

app/models/Status.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'use strict';
2+
3+
require('classtool');
4+
5+
function spec() {
6+
var async = require('async');
7+
var RpcClient = require('bitcore/RpcClient').class();
8+
var config = require('../../config/config');
9+
var rpc = new RpcClient(config.bitcoind);
10+
11+
function Status() {
12+
this.info = {};
13+
this.difficulty = {};
14+
this.txoutsetinfo = {};
15+
this.bestblockhash = {};
16+
}
17+
18+
Status.prototype.getInfo = function(next) {
19+
var that = this;
20+
async.series([
21+
function (cb) {
22+
rpc.getInfo(function(err, info){
23+
if (err) return cb(err);
24+
25+
that.info = info.result;
26+
return cb();
27+
});
28+
}
29+
], function (err) {
30+
return next(err);
31+
});
32+
};
33+
34+
Status.prototype.getDifficulty = function(next) {
35+
var that = this;
36+
async.series([
37+
function (cb) {
38+
rpc.getDifficulty(function(err, df){
39+
if (err) return cb(err);
40+
41+
that.difficulty = df.result;
42+
return cb();
43+
});
44+
}
45+
], function (err) {
46+
return next(err);
47+
});
48+
};
49+
50+
Status.prototype.getTxOutSetInfo = function(next) {
51+
var that = this;
52+
async.series([
53+
function (cb) {
54+
rpc.getTxOutSetInfo(function(err, txout){
55+
if (err) return cb(err);
56+
57+
that.txoutsetinfo = txout.result;
58+
return cb();
59+
});
60+
}
61+
], function (err) {
62+
return next(err);
63+
});
64+
};
65+
66+
Status.prototype.getBestBlockHash = function(next) {
67+
var that = this;
68+
async.series([
69+
function (cb) {
70+
rpc.getBestBlockHash(function(err, bbh){
71+
if (err) return cb(err);
72+
73+
that.bestblockhash = bbh.result;
74+
return cb();
75+
});
76+
}
77+
], function (err) {
78+
return next(err);
79+
});
80+
};
81+
82+
return Status;
83+
84+
}
85+
module.defineClass(spec);
86+

app/models/Transaction.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,17 @@ TransactionSchema.statics.fromIdWithInfo = function(txid, cb) {
8787
};
8888

8989

90-
TransactionSchema.statics.createFromArray = function(txs, next) {
90+
TransactionSchema.statics.createFromArray = function(txs, time, next) {
9191
var that = this;
9292
if (!txs) return next();
9393
var mongo_txs = [];
94-
var now = Math.round(new Date().getTime() / 1000);
9594

9695
async.forEachLimit(txs, CONCURRENCY, function(txid, cb) {
9796

98-
that.explodeTransactionItems( txid, function(err) {
97+
that.explodeTransactionItems( txid, time, function(err) {
9998
if (err) return next(err);
10099

101-
that.create({txid: txid, time: now}, function(err, new_tx) {
100+
that.create({txid: txid, time: time}, function(err, new_tx) {
102101
if (err && ! err.toString().match(/E11000/)) return cb(err);
103102

104103
if (new_tx) mongo_txs.push(new_tx);
@@ -112,7 +111,7 @@ TransactionSchema.statics.createFromArray = function(txs, next) {
112111
};
113112

114113

115-
TransactionSchema.statics.explodeTransactionItems = function(txid, cb) {
114+
TransactionSchema.statics.explodeTransactionItems = function(txid, time, cb) {
116115

117116
this.queryInfo(txid, function(err, info) {
118117
if (err || !info) return cb(err);
@@ -131,7 +130,7 @@ TransactionSchema.statics.explodeTransactionItems = function(txid, cb) {
131130
value_sat : -1 * i.valueSat,
132131
addr : i.addr,
133132
index : i.n,
134-
ts : info.time,
133+
ts : time,
135134
}, next_in);
136135
}
137136
else {
@@ -155,7 +154,7 @@ TransactionSchema.statics.explodeTransactionItems = function(txid, cb) {
155154
value_sat : o.valueSat,
156155
addr : o.scriptPubKey.addresses[0],
157156
index : o.n,
158-
ts : info.time,
157+
ts : time,
159158
}, next_out);
160159
}
161160
else {

app/views/includes/foot.jade

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ script(type='text/javascript', src='/js/directives.js')
2727
script(type='text/javascript', src='/js/filters.js')
2828

2929
//Application Services
30+
script(type='text/javascript', src='/js/services/status.js')
3031
script(type='text/javascript', src='/js/services/address.js')
3132
script(type='text/javascript', src='/js/services/transactions.js')
3233
script(type='text/javascript', src='/js/services/blocks.js')
@@ -40,4 +41,5 @@ script(type='text/javascript', src='/js/controllers/blocks.js')
4041
script(type='text/javascript', src='/js/controllers/transactions.js')
4142
script(type='text/javascript', src='/js/controllers/address.js')
4243
script(type='text/javascript', src='/js/controllers/search.js')
44+
script(type='text/javascript', src='/js/controllers/status.js')
4345
script(type='text/javascript', src='/js/init.js')

config/env/development.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = {
1111
pass: process.env.BITCOIND_PASS || 'real_mystery',
1212
host: process.env.BITCOIND_HOST || '127.0.0.1',
1313
port: process.env.BITCOIND_PORT || '18332',
14+
disableAgent: true,
1415
},
1516
network: 'testnet',
1617
}

config/env/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212
pass: process.env.BITCOIND_PASS || 'real_mystery',
1313
host: process.env.BITCOIND_HOST || '127.0.0.1',
1414
port: process.env.BITCOIND_PORT || '18332',
15-
keepConnectionAlive: false,
15+
disableAgent: true,
1616
},
1717
network: 'testnet',
1818
}

config/routes.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ module.exports = function(app) {
2525
app.get('/api/addr/:addr', addresses.show);
2626
app.param('addr', addresses.address);
2727

28+
// Status route
29+
var st = require('../app/controllers/status');
30+
app.get('/api/status', st.show);
31+
2832
};

lib/PeerSync.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function spec() {
5757
PeerSync.prototype.handle_tx = function(info) {
5858
var tx = info.message.tx.getStandardizedObject();
5959
console.log('[p2p_sync] Handle tx: ' + tx.hash);
60-
this.sync.storeTxs([tx.hash], function(err) {
60+
this.sync.storeTxs([tx.hash], null, function(err) {
6161
if (err) {
6262
console.log('[p2p_sync] Error in handle TX: ' + err);
6363
}
@@ -78,6 +78,7 @@ function spec() {
7878
this.sync.storeBlock({
7979
'hash': blockHash,
8080
'tx': tx_hashes,
81+
// TODO NEXT BLOCK / PREV BLOCK?
8182
},
8283
function(err) {
8384
if (err) {

0 commit comments

Comments
 (0)