handshake-org / handshake-org/hsd
Miner `createBlock` with Arbitrary Tip Causes Bad Template
- Dominant language
- JavaScript
- Stars
- 2.1k
- Forks
- 306
- PR merge metrics
- No merged PRs in 30d
Description
If the `miner.createBlock` function is called without the `tip` argument, it will mine on top of the chain's tip. In the case where a `tip` is passed in and its a `ChainEntry` that was created during a different Tree Root period (an epoch), it will template the block incorrectly because it uses the current tree root, `this.chain.db.treeRoot()` (scroll down in the snippet)
https://github.com/handshake-org/hsd/blob/bd1ba3dfca841655ada5ab96e70619112721c049/lib/mining/miner.js#L121-L138
I would expect the miner to roll back the state appropriately so that the correct `treeRoot` is calculated correctly irrespective of the height of the `ChainEntry` passed in or just use the previous block header's `treeRoot` field if its not a block height that starts the next epoch (which needs to calculate the epoch's root).
Its still possible to create reorgs but it would require the miner's chain to be at its local tip which is at a lesser height than the global tip. The function `miner.createBlock` can accept an arbitrary tip to mine on top of, which is why I think this function needs to be updated.
You can argue that its not worth time working on code that makes it easier to create reorgs, but then its security by obscurity.
Contributor guide
Assessment
This issue has not been assessed yet.