Friday, November 22, 2024

javascript – Utilizing bitcore-p2p and bitcore-lib to get block knowledge

I’ve ready a Nodejs javascript script to (try to) fetch block info for an instance testnet block (3) from the P2P community, utilizing bitcore-p2p library (which makes use of the getdata P2P protocol, additionally this):

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

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

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

pool.join()

At current, I get no messages again. I do proceed to get “peerinv” messages although.

What is an effective method to get block knowledge utilizing bitcore-p2p?

Are there any different constructions I might use (at current I would solely just like the blockheader, the coinbasetx and its merkle accomplice (the foundation of the opposite transactions)?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles