jnordberg / dsteem

Steem blockchain RPC client
https://jnordberg.github.io/dsteem/
Other
82 stars 57 forks source link

Get the block id when streaming the chain #32

Closed bonustrack closed 5 years ago

bonustrack commented 5 years ago

I'm trying to get the block number when streaming the chain so in case the server stop i can restart the streaming at the proper block number.

I'm using something like this to stream the chain:

const from = 10000000; // The block num of the last block i saw
const stream = client.blockchain.getBlockStream({ from });

stream.on('data', (block) => {
  // Do some work here
});

The thing is that the block object may not include the block number, the block number is only visible in transactions array, which may be empty and not included in block object, example: https://api.steemjs.com/getBlock?blockNum=1000000

Could we have the block number directly on the stream callback? Something like this:

stream.on('data', (block, blockNum) => {
  // Do some work here
});
jnordberg commented 5 years ago

Agreed that it would be convenient to have but unfortunately due to the way streams are designed in node.js the data event has only one argument. We could emit {block: .., id: 123} but that would be an API breaking change.

However you can get the block number from the block_id property. Like so:

const blockNum = Number.parseInt(block.block_id.slice(0, 8), 16)
bonustrack commented 5 years ago

Wow that's a great tip, i will definitely use that one. Thank you!