electrum – How can I simulate a reorg for testing?

0
39
electrum – How can I simulate a reorg for testing?


I used to be in a position to efficiently get electrs to alter a transaction from confirmed to unconfirmed by utilizing a second mining node, as steered by Murch on twitter. My check setup appears to be like like this:

electrum -> electrs -> bitcoind(1) -> bitcoind(2) (generates/invalidates blocks)

The trick was that I needed to invalidate two blocks on my mining node (2) which additionally eliminated my check transaction from the mining nodes mempool. Then I needed to generate 3 extra to make a brand new longest chain that did not comprise my check transaction. In any case that then my electrs node change the transaction from confirmed to unconfirmed.

I do not know why electrs does not mark the transaction to unconfirmed after I invalidate the block on the one node it is related to. I attempted the identical check with electrumx and it behaved the identical method as electrs. For anybody else making an attempt to repro this check all of the gory particulars are beneath:

Set up electrs

sudo apt-get replace
sudo apt-get set up librocksdb-dev wget
sudo apt-get set up -qqy cargo clang cmake build-essential

From electrs git repo being examined:

ROCKSDB_INCLUDE_DIR=/usr/embrace
ROCKSDB_LIB_DIR=/usr/lib
cargo set up --locked --path .
electrs --version

Set up bitcoind

BITCOIND_VERSION=22.0

wget -q https://bitcoincore.org/bin/bitcoin-core-$BITCOIND_VERSION/bitcoin-$BITCOIND_VERSION-x86_64-linux-gnu.tar.gz
tar xvf bitcoin-$BITCOIND_VERSION-x86_64-linux-gnu.tar.gz
mv -v bitcoin-$BITCOIND_VERSION/bin/bitcoind .
mv -v bitcoin-$BITCOIND_VERSION/bin/bitcoin-cli .

Set up electrum shopper

sudo apt-get set up -qqy git libsecp256k1-0 python3-cryptography python3-setuptools python3-pip jq curl

git clone --recurse-submodules https://github.com/spesmilo/electrum/

python3 -m pip set up -e electrum/

electrum model --offline

Take away outdated information

rm -rf information/
mkdir -p information/{bitcoin1,bitcoin2,electrum,electrs}

Begin bitcoind node 1 and fund pockets

./bitcoind -version
./bitcoind -regtest -regtest -txindex=1 -datadir=information/bitcoin1 -printtoconsole=0 -fallbackfee=0.0001 &
BITCOIND1_PID=$!

Begin and fund mining bitcoind node 2 and add to node 1

./bitcoind -regtest -txindex=1 -datadir=information/bitcoin2 -port=28444 -rpcport=28443 -printtoconsole=0 -fallbackfee=0.0001 &
BITCOIND2_PID=$!

./bitcoin-cli -regtest -datadir=information/bitcoin2 -rpcport=28443 createwallet "check"
NODE_ADDR=`./bitcoin-cli -regtest -datadir=information/bitcoin2 -rpcport=28443 getnewaddress`
echo $NODE_ADDR

./bitcoin-cli -regtest -datadir=information/bitcoin2 -rpcport=28443 generatetoaddress 101 $NODE_ADDR 
./bitcoin-cli -regtest -datadir=information/bitcoin2 -rpcport=28443 getwalletinfo

./bitcoin-cli -regtest -datadir=information/bitcoin2 -rpcport=28443 addnode "127.0.0.1:18444" "add"

./bitcoin-cli -regtest -datadir=information/bitcoin1 getpeerinfo
./bitcoin-cli -regtest -datadir=information/bitcoin2 -rpcport=28443 getpeerinfo

Standing of bitcoind nodes

node # tip top confirmed mempool
1 101 none none
2 101 none none

Begin electrs and electrum

export RUST_LOG=electrs=debug
electrs 
  --db-dir=information/electrs 
  --daemon-dir=information/bitcoin1 
  --network=regtest 
  2> information/electrs/regtest-debug.log &
ELECTRS_PID=$!

electrum --regtest daemon --server localhost:60401:t -1 -vDEBUG 2> information/electrum/regtest-debug.log &
ELECTRUM_PID=$!

Fund electrum pockets

electrum --regtest --wallet=information/electrum/pockets getinfo
electrum --regtest --wallet=information/electrum/pockets create --seed_type=segwit
electrum --regtest --wallet=information/electrum/pockets load_wallet
electrum --regtest --wallet=information/electrum/pockets getbalance
WALLET_ADDR1=`electrum --regtest --wallet=information/electrum/pockets getunusedaddress`
echo $WALLET_ADDR1

TX1=`./bitcoin-cli -regtest -datadir=information/bitcoin2 -rpcport=28443 sendtoaddress $WALLET_ADDR1 1.11 "test1"`

./bitcoin-cli -regtest -datadir=information/bitcoin1 getrawtransaction $TX1 true

electrum --regtest --wallet=information/electrum/pockets getaddresshistory $WALLET_ADDR1
electrum --regtest --wallet=information/electrum/pockets getbalance

./bitcoin-cli -regtest -datadir=information/bitcoin2 -rpcport=28443 generatetoaddress 1 $NODE_ADDR

./bitcoin-cli -regtest -datadir=information/bitcoin1 getrawtransaction $TX1 true
./bitcoin-cli -regtest -datadir=information/bitcoin2 -rpcport=28443 getrawtransaction $TX1 true

electrum --regtest --wallet=information/electrum/pockets getaddresshistory $WALLET_ADDR1
electrum --regtest --wallet=information/electrum/pockets getbalance

Standing of bitcoind nodes

node # tip top confirmed mempool
1 102 TX1 none
2 102 TX1 none

Standing of electrum pockets

confirmed: 1.11

Invalidate 1st block on node 2

TIP=`./bitcoin-cli -regtest -datadir=information/bitcoin2 -rpcport=28443  getbestblockhash`
echo $TIP

./bitcoin-cli -regtest -datadir=information/bitcoin2 -rpcport=28443 invalidateblock $TIP

./bitcoin-cli -regtest -datadir=information/bitcoin2 -rpcport=28443 getrawtransaction $TX1 true
./bitcoin-cli -regtest -datadir=information/bitcoin2 -rpcport=28443 getmempoolinfo 

./bitcoin-cli -regtest -datadir=information/bitcoin1 getrawtransaction $TX1 true
./bitcoin-cli -regtest -datadir=information/bitcoin1 getmempoolinfo 

electrum --regtest --wallet=information/electrum/pockets getbalance

Standing of bitcoind nodes

node # tip top confirmed mempool
1 102 TX1 none
2 101 none TX1

Standing of electrum pockets

confirmed: 1.11

Invalidate 2nd block on node 2

TIP=`./bitcoin-cli -regtest -datadir=information/bitcoin2 -rpcport=28443 getbestblockhash`
echo $TIP

./bitcoin-cli -regtest -datadir=information/bitcoin2 -rpcport=28443 invalidateblock $TIP

./bitcoin-cli -regtest -datadir=information/bitcoin2 -rpcport=28443 getblockchaininfo
./bitcoin-cli -regtest -datadir=information/bitcoin2 -rpcport=28443 getrawtransaction $TX1 true
./bitcoin-cli -regtest -datadir=information/bitcoin2 -rpcport=28443 getmempoolinfo 

./bitcoin-cli -regtest -datadir=information/bitcoin1 getblockchaininfo
./bitcoin-cli -regtest -datadir=information/bitcoin1 getrawtransaction $TX1 true
./bitcoin-cli -regtest -datadir=information/bitcoin1 getmempoolinfo

electrum --regtest --wallet=information/electrum/pockets getbalance
electrum --regtest --wallet=information/electrum/pockets listunspent

Standing of bitcoind nodes

node # tip top confirmed mempool
1 102 TX1 none
2 100 none none

Standing of electrum pockets

confirmed: 1.11

Generate 3 new blocks on node 2


./bitcoin-cli -regtest -datadir=information/bitcoin2 -rpcport=28443 generatetoaddress 3 $NODE_ADDR

./bitcoin-cli -regtest -datadir=information/bitcoin2 -rpcport=28443 getblockchaininfo
./bitcoin-cli -regtest -datadir=information/bitcoin2 -rpcport=28443 getrawtransaction $TX1 true
./bitcoin-cli -regtest -datadir=information/bitcoin2 -rpcport=28443 getmempoolinfo

./bitcoin-cli -regtest -datadir=information/bitcoin1 getblockchaininfo
./bitcoin-cli -regtest -datadir=information/bitcoin1 getrawtransaction $TX1 true
./bitcoin-cli -regtest -datadir=information/bitcoin1 getmempoolinfo

electrum --regtest --wallet=information/electrum/pockets getbalance

Standing of bitcoind nodes

node # tip top confirmed mempool
1 103 none TX1
2 103 none none

Standing of electrum pockets

confirmed: 0
unconfirmed: 1.11

Cease daemons


electrum --regtest --wallet=information/electrum/pockets cease
wait $ELECTRUM_PID

kill -INT $ELECTRS_PID
wait $ELECTRS_PID

./bitcoin-cli -regtest -datadir=information/bitcoin1 cease
wait $BITCOIND1_PID

./bitcoin-cli -regtest -datadir=information/bitcoin2 --rpcport=28443 cease
wait $BITCOIND2_PID



LEAVE A REPLY

Please enter your comment!
Please enter your name here