<p>Crucially, the event name to capture block data is <a href="https://github.com/bitpay/bitcore/blob/f2a2195a860f5ae9a80bd590c54e30b2d8d71b3d/packages/bitcore-node/src/modules/bitcoin/p2p.ts#L118" rel="nofollow noreferrer"><code>peerblock</code></a> not <code>block</code> as I had it.</p>
<p>So adding :</p>
<pre class="lang-js prettyprint-override"><code>    pool.on('peerblock', (peer, message) =&gt; {
     console.log('Received peerblock: ',message)
    })
</code></pre>
<p>allowed me to handle the block information.</p>

CC BY-SA 4.0
2024-02-22T16:59:13.537Z
2024-02-23T07:32:45.107Z
2024-02-23T07:32:45.107Z
Lee
<p>I've prepared a Nodejs javascript script to (attempt to) fetch block information for an example <a href="https://live.blockcypher.com/btc-testnet/block/000000008b896e272758da5297bcd98fdc6d97c9b765ecec401e286dc1fdbe10/" rel="nofollow noreferrer">testnet block (3)</a> from the P2P network, using <a href="https://github.com/bitpay/bitcore/blob/master/packages/bitcore-p2p/docs/messages.md" rel="nofollow noreferrer">bitcore-p2p library</a> (which uses the <a href="https://en.bitcoin.it/wiki/Protocol_documentation#getdata" rel="nofollow noreferrer">getdata P2P protocol</a>, also <a href="https://developer.bitcoin.org/examples/p2p_networking.html#p2p-network" rel="nofollow noreferrer">this</a>):</p>
<pre class="lang-js prettyprint-override"><code>let p2p = require('bitcore-p2p')
let Pool = p2p.Pool;
let Networks = require('bitcore-lib').Networks
let Messages = p2p.Messages 
let pool = new Pool({network : Networks.testnet})
const endianness = require('endianness')

/* pool will receive &quot;I'm here&quot; peerinv messages from peers, respond with block request&quot; */
let blockrequestcounter = 0
pool.on('peerinv', (peer, message) =&gt; {
  console.log('peerinv message: ',message)
  if (blockrequestcounter &lt; 5) { //limit to 5 requests
    let exampleBlockHash = Buffer.from('000000008b896e272758da5297bcd98fdc6d97c9b765ecec401e286dc1fdbe10','hex') // block 3 in testnet
    endianness(exampleBlockHash,32) // flips endianness
    let message_out = new Messages({network:Networks.testnet}).GetData.forBlock(exampleBlockHash)
    peer.sendMessage(message_out)
    blockrequestcounter += 1
  }
})

/* Monitor other messages from pool peers; */
pool.on('ready', ()=&gt; { console.log('ready')})
pool.on('error', (err) =&gt; {console.error('Error: ',err)})
pool.on('inv', (peer, message) =&gt; { console.log('Received inv: ',message)})
pool.on('alert', (peer, message) =&gt; { console.log('Received alert: ',message)})
pool.on('block', (peer, message) =&gt; { console.log('Received block: ',message)})
pool.on('merkleblock', (peer, message) =&gt; { console.log('Received merkleblock: ',message)})
pool.on('peermerkleblock', (peer, message) =&gt; { console.log('Received peermerkleblock: ',message)})
pool.on('peertx', (peer, message) =&gt; { console.log('Received peertx:',message)})
pool.on('getblocks', (peer, message) =&gt; { console.log('Received getblocks: ',message)})
pool.on('tx', (peer, message) =&gt; { console.log('Received tx: ',message)})

pool.connect()
</code></pre>
<p>At present, I get no messages back. I do continue to get &quot;peerinv&quot; messages though.</p>
<p>What is a good way to get block data using bitcore-p2p?</p>
<p>Are there any alternative constructions I could use (at present I'd only like the blockheader, the coinbasetx and its merkle partner (the root of the other transactions)?</p>

CC BY-SA 4.0
2024-02-22T15:40:13.403Z
Have you considered getting block data using the JSON-RPC interface?
Vojtěch Strnad
CC BY-SA 4.0
2024-02-22T15:48:22.210Z
Yes, you mean for a local node? I'd like something that'll run on machines without access to that interface though.
Lee
<p2p><javascript><bitcore>
Using bitcore-p2p and bitcore-lib to get block data
Lee
# Using bitcore-p2p and bitcore-lib to get block data

(Asked by: username_0 on 2024-02-22)

I've prepared a Nodejs javascript script to (attempt to) fetch block information for an example [testnet block (3)](https://live.blockcypher.com/btc-testnet/block/000000008b896e272758da5297bcd98fdc6d97c9b765ecec401e286dc1fdbe10/) from the P2P network, using [bitcore-p2p library](https://github.com/bitpay/bitcore/blob/master/packages/bitcore-p2p/docs/messages.md) (which uses the [getdata P2P protocol](https://en.bitcoin.it/wiki/Protocol_documentation#getdata), also [this](https://developer.bitcoin.org/examples/p2p_networking.html#p2p-network)):

```js
let p2p = require('bitcore-p2p')
let Pool = p2p.Pool;
let Networks = require('bitcore-lib').Networks
let Messages = p2p.Messages 
let pool = new Pool({network : Networks.testnet})
const endianness = require('endianness')

/* pool will receive "I'm here" peerinv messages from peers, respond with block request" */
let blockrequestcounter = 0
pool.on('peerinv', (peer, message) => {
  console.log('peerinv message: ',message)
  if (blockrequestcounter < 5) { //limit to 5 requests
    let exampleBlockHash = Buffer.from('000000008b896e272758da5297bcd98fdc6d97c9b765ecec401e286dc1fdbe10','hex') // block 3 in testnet
    endianness(exampleBlockHash,32) // flips endianness
    let message_out = new Messages({network:Networks.testnet}).GetData.forBlock(exampleBlockHash)
    peer.sendMessage(message_out)
    blockrequestcounter += 1
  }
})

/* Monitor other messages from pool peers; */
pool.on('ready', ()=> { console.log('ready')})
pool.on('error', (err) => {console.error('Error: ',err)})
pool.on('inv', (peer, message) => { console.log('Received inv: ',message)})
pool.on('alert', (peer, message) => { console.log('Received alert: ',message)})
pool.on('block', (peer, message) => { console.log('Received block: ',message)})
pool.on('merkleblock', (peer, message) => { console.log('Received merkleblock: ',message)})
pool.on('peermerkleblock', (peer, message) => { console.log('Received peermerkleblock: ',message)})
pool.on('peertx', (peer, message) => { console.log('Received peertx:',message)})
pool.on('getblocks', (peer, message) => { console.log('Received getblocks: ',message)})
pool.on('tx', (peer, message) => { console.log('Received tx: ',message)})

pool.connect()

```

At present, I get no messages back. I do continue to get "peerinv" messages though.

What is a good way to get block data using bitcore-p2p?

Are there any alternative constructions I could use (at present I'd only like the blockheader, the coinbasetx and its merkle partner (the root of the other transactions)?

**Tags:** p2p, javascript, bitcore

> **Comments:**
> **username_1**: Have you considered getting block data using the JSON-RPC interface?  
> **username_0**: Yes, you mean for a local node? I'd like something that'll run on machines without access to that interface though.  

---

## 1 answers

### Answer by username_0 on 2024-02-22. Score: 0 | Accepted answer

Crucially, the event name to capture block data is [`peerblock`](https://github.com/bitpay/bitcore/blob/f2a2195a860f5ae9a80bd590c54e30b2d8d71b3d/packages/bitcore-node/src/modules/bitcoin/p2p.ts#L118) not `block` as I had it.

So adding :

```js
    pool.on('peerblock', (peer, message) => {
     console.log('Received peerblock: ',message)
    })

```

allowed me to handle the block information.


---

