Skip to main content
Bounty Ended with 50 reputation awarded by dvdblk
added 233 characters in body
Source Link
Undead8
  • 3.6k
  • 10
  • 28

Basically, what you want is to have a confirmation that a given block was included in the canonical chain. I assume that you do not need very high security because based on your examples a confirmation of only one block would be enough for your need.

I think that expending your second option could be a solution.

Alternative 1 - Validate double blocks with parent hash

Let's say that you received two blocks with the number 1000.

  1. Wait until you receive a block that has number +1 than the block you want to confirm (block number 1001).
  2. Check the parentHash of block 1001 and see if it matches the hash of any of the blocks with number 1000. If you have a match, you know that this is the block to include.
  3. If you have no match at all (not sure if it is possible if you run a full node), then you should make a call to get the block that has a hash equal to the parentHash of block 1001. With web3js, you would use web3.eth.getBlock(parentHashOfBlock1001). You would then have the right block number 1000.

Alternative 2 - Include only on confirmation

Another solution would be to always wait one block before including the transactions of the previous block.

  1. When you receive a block header, do not include its transactions in your databse. Instead, get the block corresponding to the parentHash web3.eth.getBlock(parentHashOfCurrentBlock).
  2. Check if you have already included that parent block in your database. This could happen when you receive the same block number twice (both blocks should have the same parent even if one of them is not valid). If it was not included:
  3. Include the transactions of that parent block in your database.

You will always be one block late, but it should solve your issue most of the time.

Alternative 3 - Wait... then get block by number

Your node should take care of removing invalid blocks. If you wait a certain number of blocks and then get the past block by number, you should only receive the valid block.

Basically, what you want is to have a confirmation that a given block was included in the canonical chain. I assume that you do not need very high security because based on your examples a confirmation of only one block would be enough for your need.

I think that expending your second option could be a solution.

Alternative 1 - Validate double blocks with parent hash

Let's say that you received two blocks with the number 1000.

  1. Wait until you receive a block that has number +1 than the block you want to confirm (block number 1001).
  2. Check the parentHash of block 1001 and see if it matches the hash of any of the blocks with number 1000. If you have a match, you know that this is the block to include.
  3. If you have no match at all (not sure if it is possible if you run a full node), then you should make a call to get the block that has a hash equal to the parentHash of block 1001. With web3js, you would use web3.eth.getBlock(parentHashOfBlock1001). You would then have the right block number 1000.

Alternative 2 - Include only on confirmation

Another solution would be to always wait one block before including the transactions of the previous block.

  1. When you receive a block header, do not include its transactions in your databse. Instead, get the block corresponding to the parentHash web3.eth.getBlock(parentHashOfCurrentBlock).
  2. Check if you have already included that parent block in your database. This could happen when you receive the same block number twice (both blocks should have the same parent even if one of them is not valid). If it was not included:
  3. Include the transactions of that parent block in your database.

You will always be one block late, but it should solve your issue most of the time.

Basically, what you want is to have a confirmation that a given block was included in the canonical chain. I assume that you do not need very high security because based on your examples a confirmation of only one block would be enough for your need.

I think that expending your second option could be a solution.

Alternative 1 - Validate double blocks with parent hash

Let's say that you received two blocks with the number 1000.

  1. Wait until you receive a block that has number +1 than the block you want to confirm (block number 1001).
  2. Check the parentHash of block 1001 and see if it matches the hash of any of the blocks with number 1000. If you have a match, you know that this is the block to include.
  3. If you have no match at all (not sure if it is possible if you run a full node), then you should make a call to get the block that has a hash equal to the parentHash of block 1001. With web3js, you would use web3.eth.getBlock(parentHashOfBlock1001). You would then have the right block number 1000.

Alternative 2 - Include only on confirmation

Another solution would be to always wait one block before including the transactions of the previous block.

  1. When you receive a block header, do not include its transactions in your databse. Instead, get the block corresponding to the parentHash web3.eth.getBlock(parentHashOfCurrentBlock).
  2. Check if you have already included that parent block in your database. This could happen when you receive the same block number twice (both blocks should have the same parent even if one of them is not valid). If it was not included:
  3. Include the transactions of that parent block in your database.

You will always be one block late, but it should solve your issue most of the time.

Alternative 3 - Wait... then get block by number

Your node should take care of removing invalid blocks. If you wait a certain number of blocks and then get the past block by number, you should only receive the valid block.

Source Link
Undead8
  • 3.6k
  • 10
  • 28

Basically, what you want is to have a confirmation that a given block was included in the canonical chain. I assume that you do not need very high security because based on your examples a confirmation of only one block would be enough for your need.

I think that expending your second option could be a solution.

Alternative 1 - Validate double blocks with parent hash

Let's say that you received two blocks with the number 1000.

  1. Wait until you receive a block that has number +1 than the block you want to confirm (block number 1001).
  2. Check the parentHash of block 1001 and see if it matches the hash of any of the blocks with number 1000. If you have a match, you know that this is the block to include.
  3. If you have no match at all (not sure if it is possible if you run a full node), then you should make a call to get the block that has a hash equal to the parentHash of block 1001. With web3js, you would use web3.eth.getBlock(parentHashOfBlock1001). You would then have the right block number 1000.

Alternative 2 - Include only on confirmation

Another solution would be to always wait one block before including the transactions of the previous block.

  1. When you receive a block header, do not include its transactions in your databse. Instead, get the block corresponding to the parentHash web3.eth.getBlock(parentHashOfCurrentBlock).
  2. Check if you have already included that parent block in your database. This could happen when you receive the same block number twice (both blocks should have the same parent even if one of them is not valid). If it was not included:
  3. Include the transactions of that parent block in your database.

You will always be one block late, but it should solve your issue most of the time.