8
2
I want to test if a service can manage a blockchain fork. However there is no way to get bitcoind to create test forks under the regtest mode. I've tried modifying it to generate blocks with arbitrary prevblock hashes but it is not easy due to the high code complexity. I've managed to get it to create forks on-top of earlier blocks, however, I can't get it to generate blocks for conflicting transactions (double-spends and malleability changes), largely due to all of the "pruning" code. Surely someone has already come up with a solution, as this is a vital thing to test? I can't find anything on-line to help with testing fork scenarios.
I'm thinking that perhaps I should have used another technique? That I should have run 3 bitcoind regtest nodes and isolate them so that I can create multiple blockchains. 2 of them generate competing chains, and the other interfaces with the application. I can generate a fork scenario with the first two nodes, sync one of the nodes to the 3rd node and then sync the other node to generate the fork? I'd need to use a combination of addnode and tcpkill (?) to sync and then re-isolate the nodes.
This seems a ridiculous thing to do to test something so simple, so is there a better way?
1You can also use the RPC commands
invalidateblockandreconsiderblockto aid with testing in this scenario. They allow you to disconnect and reorganize back and forwards in otherwise static situations. – Anonymous – 2015-04-07T02:54:47.700@MatthewMitchell: What is the rough idea of using an "interface node"? Isn't it sufficient to have 2 nodes only? – Aliakbar Ahmadi – 2015-05-14T15:39:15.263
1@AliakbarAhmadi: The idea is that you create two competing blockchains with to nodes and the "interface node" synchronises with either one. Once this interface node is synchronised, it can't make it's own fork (unless you used
invalidateblocklike Gavin suggested) so that's why you have the third node to keep isolated whilst the other two are synchronised. Then you use this third node to create another fork if desired for your test case. – Matthew Mitchell – 2015-08-31T17:14:41.827