Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing chain reorganisation #139

Open
schjonhaug opened this issue Mar 10, 2022 · 2 comments
Open

Testing chain reorganisation #139

schjonhaug opened this issue Mar 10, 2022 · 2 comments

Comments

@schjonhaug
Copy link
Contributor

Is there any way we can use Nigiri to test chain reorgs?

@tiero
Copy link
Member

tiero commented Mar 10, 2022

I think it's matter to create manually a block via Bitcoin RPC?

Looks like to me a general Bitcoin Core question, rather Nigiri specific.

Maybe you need two nodes to do that? In that case you can tweak the docker compose and copy the bitcoin service and should have two nodes same network. But maybe single regtest node works fine

https://bitcoin.stackexchange.com/questions/52891/how-to-detect-reorganization-from-bitcoind-via-zmq

@schjonhaug
Copy link
Contributor Author

Yes, I was wondering whether this was something already supported ’out of the box‘ in Nigiri.

This is from our docs on how we are currently doing it – combining Nigiri with a standalone Bitcoin core instance. Would it be possible for Nigiri to automate this you think?

Testing chain reorg

To test chain reorgs, we need to run two instances of bitcoin core at the same time.

First time setup

Assuming that Nigiri is installed already, we need to also install Bitcoin core standalone.

Installing Bitcoin core on macOS with Homebrew:

brew install bitcoin

Let’s start by copying the bitcoin.conf to bitcoin core’s application directory:

cp bitcoin.conf $HOME/Library/Application\ Support/Bitcoin/

The contents of bitcoin.conf:

´´´
regtest=1

[regtest]
port=28444
rpcport=28443
bind=127.0.0.1:28445=onion

fallbackfee=0.00001
server=1
´´´

The configuration file makes sure that we’ll start regtest on a different port than the one being used by Nigiri.

Start Nigiri if it‘s not already running:

nigiri start

Then, start bitcoin core in regtest mode:

bitcoind -regtest

In a new terminal window, create a new wallet:

bitcoin-cli -regtest createwallet "wallet"

In case you’ve already created a wallet, load it:

bitcoin-cli -regtest loadwallet "wallet"

Then, we’ll connect this node with Nigiri so that they can sync

bitcoin-cli -regtest addnode localhost onetry

You should now see the standalone node syncing to the chain tip of Nigiri’s node, i.e.:

...
2022-03-25T19:24:02Z UpdateTip: new best=594f838b2bffad8d776094ea6ac98db55bd47f3ddd9d843d49377e7f6bdd0a86 height=97 version=0x20000000 log2_work=7.614710 tx=98 date='2022-03-25T19:24:17Z' progress=1.000000 cache=0.0MiB(97txo)
2022-03-25T19:24:02Z UpdateTip: new best=42ff21b3c6d2de7de96645ccada78e657e5eb3a3a460c443d3e67a0b3c7ffc69 height=98 version=0x20000000 log2_work=7.629357 tx=99 date='2022-03-25T19:24:18Z' progress=1.000000 cache=0.0MiB(98txo)
2022-03-25T19:24:02Z UpdateTip: new best=20ba5a4f21069da933bc53cf576fd8bcc78ff9ac522b93b3538ae712e0a78113 height=99 version=0x20000000 log2_work=7.643856 tx=100 date='2022-03-25T19:24:18Z' progress=1.000000 cache=0.0MiB(99txo)
2022-03-25T19:24:02Z UpdateTip: new best=65b8d0d88e435e78884cd45ff8e920a588bca1b4308c208a8832b0ff7ec735b2 height=100 version=0x20000000 log2_work=7.658211 tx=101 date='2022-03-25T19:24:18Z' progress=1.000000 cache=0.0MiB(100txo)
2022-03-25T19:24:02Z UpdateTip: new best=4ddb633f1690ec99d99e412095a8ae7482a1a91ce28b88ecee0e0015e25be5c0 height=101 version=0x20000000 log2_work=7.672425 tx=102 date='2022-03-25T19:24:18Z' progress=1.000000 cache=0.0MiB(101txo)

In this example, after the syncing is done, both nodes are at height 101. Let’s disconnect them again.

bitcoin-cli -regtest disconnectnode localhost

Now, you should make a transaction using Nigiri’s faucet. Then, Nigiri’s block height will be 102, as you can see with nigiri logs bitcoin:

bitcoin  | 2022-03-26T20:56:09Z UpdateTip: new best=2e1856d687f05c960164da927d861ae268e6c3ed4dfa9bb10a44374515974a43 height=102 version=0x20000000 log2_work=7.686501 tx=104 date='2022-03-26T20:56:09Z' progress=1.000000 cache=0.0MiB(104txo)

Then we’ll mine two new blocks on the standalone node making the block height one larger than Nigiri’s, i.e. 103:

bitcoin-cli -regtest -generate 2
2022-03-25T19:29:45Z UpdateTip: new best=66062033813d6da0ce77a9f0df994d135aab9ee9a9b2559055b60540c6983a0f height=102 version=0x20000000 log2_work=7.686501 tx=103 date='2022-03-25T19:29:45Z' progress=1.000000 cache=0.0MiB(102txo)
2022-03-25T19:29:45Z UpdateTip: new best=55ecd6b3b1c8ac589e3a7b83fb15b9455c6c6ba1cc05dbf4c8db0314c11b0fc9 height=103 version=0x20000000 log2_work=7.700440 tx=104 date='2022-03-25T19:29:45Z' progress=1.000000 cache=0.0MiB(103txo)

Finally, we reconnect the nodes to that they can sync again and complete the reorg:

bitcoin-cli -regtest addnode localhost onetry

If you do another nigiri logs bitcoin now, you’ll see that the faucet transaction originally found in block 102 has been replaced (effectively reverted) and that block 103 has been added:

bitcoin  | 2022-03-26T20:59:03Z UpdateTip: new best=658fb5b4e521e8f1c109cbefbcdf9bf1af11b86ca082928dbf9097810ab39ed6 height=102 version=0x20000000 log2_work=7.686501 tx=103 date='2022-03-26T20:58:45Z' progress=1.000000 cache=0.0MiB(103txo)
bitcoin  | 2022-03-26T20:59:03Z UpdateTip: new best=7366118d3e4d641dcb7dc4f6055502e8e02ca8733f0769c177864f039a6e46fe height=103 version=0x20000000 log2_work=7.700440 tx=104 date='2022-03-26T20:58:45Z' progress=1.000000 cache=0.0MiB(104txo)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants