1

I've prepared a Nodejs javascript script to (attempt to) fetch block information for an example testnet block (3) from the P2P network, using bitcore-p2p library (which uses the getdata P2P protocol, also this):

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)?

2
  • Have you considered getting block data using the JSON-RPC interface? Feb 22 at 15:40
  • Yes, you mean for a local node? I'd like something that'll run on machines without access to that interface though.
    – Lee
    Feb 22 at 15:48

1 Answer 1

0

Crucially, the event name to capture block data is peerblock not block as I had it.

So adding :

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

allowed me to handle the block information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.