[ad_1]

I’ve prepared a Nodejs javascript script to (attempt to) fetch block information for and 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)?

[ad_2]

Source link

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *