Discarding blocks

+1 vote
Hello,

What would be the easiest way to discard some mined blocks? For example, I'd like to discard last 10 blocks. As a result all transactions that were in these blocks would go back into mempool and the multichain would mine them back into the chain, resulting different block hashes.

I know blockchains are not supposed to work this way but I'm working on a system that would detect such anomalies. If there is no solution available, I guess I could just discard the chain and generate a new one.
asked Dec 25, 2017 by Null

1 Answer

+1 vote

You can use the setlastblock command to do this, after running: pause incoming,mining

answered Dec 26, 2017 by MultiChain
I noticed that after I resume mining all blocks are recreated. For example, I have 149 blocks, then I run

pause incoming,mining
setlastblock 100
clearmempool
resume incoming,mining

after I run 'resume' command the blockchain again has 149 blocks with the same hashes. Is this the correct behavior or a bug?
This will be happening because the node on which you did this will simply re-update itself from the other nodes in the network. This is correct blockchain behavior in general! If you want to prevent it, you need to wind back all of the nodes in the same way, and use clearmempool, then resume them all together.
I'm running just a single node. I tried to create a fresh chain on  a new VM. Here is the command log:

multichain-util create testchain
multichaind testchain -daemon
multichain-cli testchain
create stream foo true
subscribe foo
publish foo bar DEADBEEF
getinfo # 'blocks' == 32
pause mining,incoming
setlastblock 20
clearmempool
getinfo # 'blocks' == 20
resume mining,incoming
getinfo # 'blocks' == 36

I tried the same after the chain mined setup blocks (60) but I got same result, stream items I created and tried to reset still reappear. For my purposes having a blockchain in a paused state plus setuplastblock is enough (a blockchain where a number of blocks decreased is still considered suspicious in my project) but otherwise this seems like a bug. Or maybe I'm doing something wrong
It looks like you're doing it right. I'll forward this to the dev team and get back to you shortly.
Thanks for your patience. We forgot one additional step. Even after setlastblock, the old branch is still stored by the local node, so the node will naturally switch back to it. If you want to prevent this happening, you need to use the bantx runtime parameter, which is also supported by the setruntimeparam API. The simplest transaction to ban is the coinbase transaction of the first block after the one you rewind to. So here are the steps:

* Decide which block number you want to rewind to, which we'll call [x]
* pause mining,incoming
* Note first txid in "tx" array of output of: getblock [x+1]
* setruntimeparam bantx [this txid]
* setlastblock [x]
* clearmempool
* resume mining,incoming
Hi,
sorry for the late reply. The blockchain seems to fall into safe mode when I add new transactions "Warning: We do not appear to fully agree with our peers! You may need to upgrade, or other nodes may need to upgrade.""

I guess I can live without this feature anyway. Thank you for your help!

EDIT: this looks like a temporary issue
...