Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 17 additions & 9 deletions src/bitcoin-node-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ class BitcoinNodeConnection {
constructor (zmq, rpc) {
this.zmq = zmq
this.rpc = rpc
this.isRestApiEnabled = false
}

async connect (_height, _network) {
await this.zmq.connect()
this.isRestApiEnabled = await this.rpc.isRestApiEnabled()
}

async disconnect () {
Expand Down Expand Up @@ -49,15 +51,21 @@ class BitcoinNodeConnection {
return { reorg: true }
}

if (blockData.size >= 0xf000000) { // Avoids create a string longer than the limit
if (this.isRestApiEnabled) {
const block = this._parseBlock(
await this.rpc.getRawBlockByHash(blockData.hash),
targetBlockHeight
)
return this._buildBlockResponse(block, targetBlockHeight)
} else if (blockData.size >= 0xf000000) {
return this._responsefromBlockData(blockData)
} else {
const block = this._parseBlock(
await this.rpc.getBlockByHeight(targetBlockHeight, false),
targetBlockHeight
)
return this._buildBlockResponse(block, targetBlockHeight)
}

const block = this._parseBlock(
await this.rpc.getBlockByHeight(targetBlockHeight, false),
targetBlockHeight
)
return this._buildBlockResponse(block, targetBlockHeight)
}

async listenForMempool (mempoolTxCallback) {
Expand Down Expand Up @@ -90,8 +98,8 @@ class BitcoinNodeConnection {
return response
}

_parseBlock (rpcResponse, requestedHeight) {
const bsvBlock = new bsv.Block(Buffer.from(rpcResponse, 'hex'))
_parseBlock (blockBuffer, requestedHeight) {
const bsvBlock = new bsv.Block(blockBuffer)

return {
height: requestedHeight,
Expand Down
10 changes: 10 additions & 0 deletions src/bitcoin-rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class BitcoinRpc {
this.baseUrl = baseUrl
}

async isRestApiEnabled () {
const response = fetch(`${this.baseUrl}/rest/tx/9834daa6d34690981888f7db4c1c36686ebb9b685d37115abc38e0e75f9cd98d.hex`)
return response.ok
}

/**
* @param {String} txid
*/
Expand All @@ -47,6 +52,11 @@ class BitcoinRpc {
return this._rpcCall('getblockbyheight', [targetHeight, verbose])
}

async getRawBlockByHash (targetHash) {
const response = await fetch(`${this.baseUrl}/rest/block/${targetHash}.bin`)
return Buffer.from(await response.arrayBuffer())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this blocks are always just a buffer.

}

async _rpcCall (method, params) {
const response = await this._httpPost(this.baseUrl, {
jsonrpc: '1.0',
Expand Down
2 changes: 1 addition & 1 deletion src/indexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class Indexer {
}

_onAddTransaction (txid) {
this.logger.info('Added', txid)
this.logger.debug('Added', txid)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logs are impossible to interprete with the wall of "Added txid". I believe this should be considered debug information. If an specifc txid has to be requested it's easier just do a query.

}

_onDeleteTransaction (txid) {
Expand Down
58 changes: 56 additions & 2 deletions test/bitcoin-node-connection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class TestBitcoinRpc {
}
]
this.nextBlockHeight = 1001
this.isRestEnabled = true
}

async isRestApiEnabled () {
return this.isRestEnabled
}

async getRawTransaction (txid, verbose = true) {
Expand All @@ -41,11 +46,20 @@ class TestBitcoinRpc {
return {
size: block.size || block.hex.length,
previousblockhash: block.previousblockhash,
tx: block.txs.map(tx => tx.hash)
tx: block.txs.map(tx => tx.hash),
hash: block.hash
}
}
}

async getRawBlockByHash (targetHash) {
if (!this.isRestEnabled) {
throw new Error('rest api is not enabled')
}
const block = this.blocks.find(block => block.hash === targetHash)
return Buffer.from(block.hex, 'hex')
}

// Test

registerConfirmedTx (txid, rawTx) {
Expand Down Expand Up @@ -106,6 +120,10 @@ class TestZmq {
this.handler = handler
}

async connect () {
// nothing
}

// test

publishTx (tx) {
Expand Down Expand Up @@ -173,10 +191,11 @@ describe('BitcoinNodeConnection', () => {
let instance = null
let run = null

beforeEach(() => {
beforeEach(async () => {
bitcoinZmq = new TestZmq()
bitcoinRpc = new TestBitcoinRpc()
instance = new BitcoinNodeConnection(bitcoinZmq, bitcoinRpc)
await instance.connect()

run = new Run({
purse: {
Expand Down Expand Up @@ -373,6 +392,41 @@ describe('BitcoinNodeConnection', () => {
const nextBlock = await instance.getNextBlock(previousBlock.height, null)
expect(nextBlock.txids).to.eql([randomRunTx.hash])
})

describe('when rest-api is not enabled and a giant block arrives', () => {
beforeEach(() => {
bitcoinRpc.isRestEnabled = false
})
it('does not get txs one by one', async () => {
const randomTx = buildRandomTx()
const randomRunTx = await buildRandomRunTx(run)
bitcoinRpc.registerUnconfirmedTx(randomTx.hash, randomTx.toBuffer().toString('hex'))
bitcoinRpc.registerUnconfirmedTx(randomRunTx.hash, randomRunTx.toBuffer().toString('hex'))
bitcoinRpc.closeBlock(0x1fffffe8 + 1)
const previousBlock = bitcoinRpc.blocks[bitcoinRpc.blocks.length - 2]

expect(instance.getNextBlock(previousBlock.height, null)).not.to.eventually.throw()
})
})

describe('when rest-api is enabled and a giant block arrives', () => {
beforeEach(() => {
bitcoinRpc.isRestEnabled = true
})
it('does not call getRawTransaction', async () => {
const randomTx = buildRandomTx()
const randomRunTx = await buildRandomRunTx(run)
bitcoinRpc.registerUnconfirmedTx(randomTx.hash, randomTx.toBuffer().toString('hex'))
bitcoinRpc.registerUnconfirmedTx(randomRunTx.hash, randomRunTx.toBuffer().toString('hex'))
bitcoinRpc.closeBlock(0x1fffffe8 + 1)

bitcoinRpc.getRawTransaction = () => { throw new Error('should not call') }
const previousBlock = bitcoinRpc.blocks[bitcoinRpc.blocks.length - 2]

const nextBlock = await instance.getNextBlock(previousBlock.height, null)
expect(nextBlock.txhexs.length).to.eql(nextBlock.txids.length)
})
})
})

describe('#listenForMempool', () => {
Expand Down