8000 feat(BlockStream): support filtered blocks · mappum/webcoin@705bd11 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 705bd11

Browse files
committed
feat(BlockStream): support filtered blocks
1 parent 36fbbfc commit 705bd11

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

lib/blockStream.js

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var Readable = require('stream').Readable
22
var util = require('util')
33
var Inventory = require('bitcore-p2p').Inventory
44
var u = require('./utils.js')
5+
var MerkleTree = require('./merkleTree.js')
56

67
var BlockStream = module.exports = function (opts) {
78
if (!opts.peer) throw new Error('"peer" option is required for BlockStream')
@@ -15,12 +16,17 @@ var BlockStream = module.exports = function (opts) {
1516
this.from = opts.from || 0
1617
this.to = opts.to || null
1718
this.filtered = typeof opts.filtered === 'boolean' ? opts.filtered : !!this.peer.filter
19+
this.fetchTransactions = typeof opts.fetchTransactions === 'boolean' ? opts.fetchTransactions : true
1820

1921
this.cursor = this.from
2022
this.expected = null
2123
this.ending = false
2224

23-
this.peer.on(this.filtered ? 'merkleblock' : 'block', this._onBlock.bind(this))
25+
if (!this.filtered) {
26+
this.peer.on('block', this._onBlock.bind(this))
27+
} else {
28+
this.peer.on('merkleblock', this._onMerkleBlock.bind(this))
29+
}
2430
}
2531
util.inherits(BlockStream, Readable)
2632

@@ -55,7 +61,7 @@ BlockStream.prototype._getData = function (block) {
5561
this.expected = { height: block.height, hash: hash }
5662
var inventory = [
5763
new Inventory({
58-
type: this.filtered ? Inventory.TYPE.MERKLE_BLOCK : Inventory.TYPE.BLOCK,
64+
type: this.filtered ? Inventory.TYPE.FILTERED_BLOCK : Inventory.TYPE.BLOCK,
5965
hash: hash
6066
})
6167
]
@@ -64,16 +70,34 @@ BlockStream.prototype._getData = function (block) {
6470
}
6571

6672
BlockStream.prototype._onBlock = function (message) {
67-
if (!this.expected) return
68-
var hash = u.toHash(message.block.header.hash)
69-
if (hash.compare(this.expected.hash) !== 0) return
70-
var block = {
73+
this._onData({
7174
height: this.expected.height,
7275
header: message.block.header,
7376
block: message.block
74-
}
77+
})
78+
}
79+
80+
BlockStream.prototype._onMerkleBlock = function (message) {
81+
if (!this.expected) return
82+
83+
var tree = MerkleTree.fromMerkleBlock(message.merkleBlock)
84+
85+
// TODO: fetch transactions
86+
87+
this._onData({
88+
height: this.expected.height,
89+
header: message.merkleBlock.header,
90+
tree: tree,
91+
transactions: []
92+
})
93+
}
94+
95+
BlockStream.prototype._onData = function (data) {
96+
if (!this.expected) return
97+
var hash = u.toHash(data.header.hash)
98+
if (hash.compare(this.expected.hash) !== 0) return
7599
this.expected = null
76-
var done = !this.push(block) || this.ending
100+
var done = !this.push(data) || this.ending
77101
if (done) this._end()
78102
else this._next()
79103
}

lib/transactionStream.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var Transform = require('stream').Transform
22
var util = require('util')
3-
var Block = require('bitcore').Block
3+
var bitcore = require('bitcore')
4+
var BlockHeader = bitcore.BlockHeader
45

56
var TransactionStream = module.exports = function (blocks, opts) {
67
var self = this
@@ -17,12 +18,13 @@ var TransactionStream = module.exports = function (blocks, opts) {
1718
util.inherits(TransactionStream, Transform)
1819

1920
TransactionStream.prototype._transform = function (block, enc, cb) {
20-
if (block.height == null || !(block.block instanceof Block)) {
21+
if (block.height == null || !(block.header instanceof BlockHeader)) {
2122
return cb(new Error('Input to TransactionStream must be a stream of blocks'))
2223
}
2324

2425
var self = this
25-
block.block.transactions.forEach(function (tx) {
26+
var txs = block.block ? block.block.transactions : block.transactions
27+
txs.forEach(function (tx) {
2628
if (this.verify && tx.verify() !== true) return
2729
self.push({ transaction: tx, block: block })
2830
})

0 commit comments

Comments
 (0)
0