Bitcoin

Get block data using bitcore-p2p and bitcore-lib – Stack Overflow

I have prepared a Nodejs JavaScript script that attempts to get block information for an example testnet block (3) on a P2P network using the bitcore-p2p library (which also uses the getdata P2P protocol).

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

We are unable to receive a reply at this time. Still, the “peerinv” message continues to appear.

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

Is there an alternative configuration I can use (currently I just want blockheader, coinbasetx and merkle partner (root for other transactions))?

Related Articles

Back to top button