Sunday, November 24, 2024
HomeBitcoinelectrum - How can I simulate a reorg for testing?

electrum – How can I simulate a reorg for testing?


I used to be in a position to efficiently get electrs to vary a transaction from confirmed to unconfirmed through the use of a second mining node, as instructed by Murch on twitter. My check setup seems to be 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 spite of everything that then my electrs node change the transaction from confirmed to unconfirmed.

I do not know why electrs would 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 means 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/embody
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 consumer

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 knowledge

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

Begin bitcoind node 1 and fund pockets

./bitcoind -version
./bitcoind -regtest -regtest -txindex=1 -datadir=knowledge/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=knowledge/bitcoin2 -port=28444 -rpcport=28443 -printtoconsole=0 -fallbackfee=0.0001 &
BITCOIND2_PID=$!

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

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

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

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

Standing of bitcoind nodes

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

Begin electrs and electrum

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

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

Fund electrum pockets

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

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

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

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

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

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

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

Standing of bitcoind nodes

node # tip peak 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=knowledge/bitcoin2 -rpcport=28443  getbestblockhash`
echo $TIP

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

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

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

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

Standing of bitcoind nodes

node # tip peak 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=knowledge/bitcoin2 -rpcport=28443 getbestblockhash`
echo $TIP

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

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

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

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

Standing of bitcoind nodes

node # tip peak 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=knowledge/bitcoin2 -rpcport=28443 generatetoaddress 3 $NODE_ADDR

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

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

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

Standing of bitcoind nodes

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

Standing of electrum pockets

confirmed: 0
unconfirmed: 1.11

Cease daemons


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

kill -INT $ELECTRS_PID
wait $ELECTRS_PID

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

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



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments