Skip to content

Commit 41dcf61

Browse files
committed
Test for walletbackup/walletdump RPC calls
1 parent 4fd082d commit 41dcf61

File tree

2 files changed

+314
-2
lines changed

2 files changed

+314
-2
lines changed

qa/rpc-tests/README.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
Regression tests of RPC interface
22
=================================
33

4-
wallet.sh : Test wallet send/receive code (see comments for details)
4+
wallet.sh : Exercise wallet send/receive code.
55

6-
util.sh : useful re-usable functions
6+
walletbackup.sh : Exercise wallet backup / dump / import
7+
8+
txnmall.sh : Test proper accounting of malleable transactions
9+
10+
conflictedbalance.sh : More testing of malleable transaction handling
11+
12+
util.sh : useful re-usable bash functions
13+
14+
15+
Tips for creating new tests
16+
===========================
17+
18+
To cleanup after a failed or interrupted test:
19+
killall bitcoind
20+
rm -rf test.*
21+
22+
The most difficult part of writing reproducible tests is
23+
keeping multiple nodes in sync. See WaitBlocks,
24+
WaitPeers, and WaitMemPools for how other tests
25+
deal with this.

qa/rpc-tests/walletbackup.sh

Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
#!/usr/bin/env bash
2+
3+
# Test wallet backup / dump / restore functionality
4+
5+
# Test case is:
6+
# 4 nodes. 1 2 3 and send transactions between each other,
7+
# fourth node is a miner.
8+
# 1 2 3 and each mine a block to start, then
9+
# miner creates 100 blocks so 1 2 3 each have 50 mature
10+
# coins to spend.
11+
# Then 5 iterations of 1/2/3 sending coins amongst
12+
# themselves to get transactions in the wallets,
13+
# and the miner mining one block.
14+
#
15+
# Wallets are backed up using dumpwallet/backupwallet.
16+
# Then 5 more iterations of transactions, then block.
17+
#
18+
# Miner then generates 101 more blocks, so any
19+
# transaction fees paid mature.
20+
#
21+
# Sanity checks done:
22+
# Miner balance >= 150*50
23+
# Sum(1,2,3,4 balances) == 153*150
24+
#
25+
# 1/2/3 are shutdown, and their wallets erased.
26+
# Then restore using wallet.dat backup. And
27+
# confirm 1/2/3/4 balances are same as before.
28+
#
29+
# Shutdown again, restore using importwallet,
30+
# and confirm again balances are correct.
31+
#
32+
33+
if [ $# -lt 1 ]; then
34+
echo "Usage: $0 path_to_binaries"
35+
echo "e.g. $0 ../../src"
36+
exit 1
37+
fi
38+
39+
BITCOIND=${1}/bitcoind
40+
CLI=${1}/bitcoin-cli
41+
42+
DIR="${BASH_SOURCE%/*}"
43+
SENDANDWAIT="${DIR}/send.sh"
44+
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
45+
. "$DIR/util.sh"
46+
47+
D=$(mktemp -d test.XXXXX)
48+
49+
echo "Starting nodes..."
50+
51+
# "Miner":
52+
D4=${D}/node4
53+
CreateDataDir $D4 port=11030 rpcport=11031
54+
B4ARGS="-datadir=$D4"
55+
$BITCOIND $BITCOINDARGS $B4ARGS &
56+
B4PID=$!
57+
58+
# Want default keypool for 1/2/3, and
59+
# don't need send-and-wait functionality,
60+
# so don't use CreateDataDir:
61+
function CreateConfDir {
62+
DIR=$1
63+
mkdir -p $DIR
64+
CONF=$DIR/bitcoin.conf
65+
echo "regtest=1" >> $CONF
66+
echo "rpcuser=rt" >> $CONF
67+
echo "rpcpassword=rt" >> $CONF
68+
echo "rpcwait=1" >> $CONF
69+
shift
70+
while (( "$#" )); do
71+
echo $1 >> $CONF
72+
shift
73+
done
74+
}
75+
76+
# "Spenders" 1/2/3
77+
D1=${D}/node1
78+
CreateConfDir $D1 port=11000 rpcport=11001 addnode=127.0.0.1:11030
79+
B1ARGS="-datadir=$D1"
80+
$BITCOIND $B1ARGS &
81+
B1PID=$!
82+
D2=${D}/node2
83+
CreateConfDir $D2 port=11010 rpcport=11011 addnode=127.0.0.1:11030
84+
B2ARGS="-datadir=$D2"
85+
$BITCOIND $B2ARGS &
86+
B2PID=$!
87+
D3=${D}/node3
88+
CreateConfDir $D3 port=11020 rpcport=11021 addnode=127.0.0.1:11030 addnode=127.0.0.1:11000
89+
B3ARGS="-datadir=$D3"
90+
$BITCOIND $BITCOINDARGS $B3ARGS &
91+
B3PID=$!
92+
93+
# Wait until all nodes are at the same block number
94+
function WaitBlocks {
95+
while :
96+
do
97+
sleep 1
98+
BLOCKS1=$( GetBlocks "$B1ARGS" )
99+
BLOCKS2=$( GetBlocks "$B2ARGS" )
100+
BLOCKS3=$( GetBlocks "$B3ARGS" )
101+
BLOCKS4=$( GetBlocks "$B4ARGS" )
102+
if (( BLOCKS1 == BLOCKS4 && BLOCKS2 == BLOCKS4 && BLOCKS3 == BLOCKS4 ))
103+
then
104+
break
105+
fi
106+
done
107+
}
108+
109+
# Wait until all nodes have the same txns in
110+
# their memory pools
111+
function WaitMemPools {
112+
while :
113+
do
114+
sleep 1
115+
MEMPOOL1=$( $CLI "$B1ARGS" getrawmempool | sort | shasum )
116+
MEMPOOL2=$( $CLI "$B2ARGS" getrawmempool | sort | shasum )
117+
MEMPOOL3=$( $CLI "$B3ARGS" getrawmempool | sort | shasum )
118+
MEMPOOL4=$( $CLI "$B4ARGS" getrawmempool | sort | shasum )
119+
if [[ $MEMPOOL1 = $MEMPOOL4 && $MEMPOOL2 = $MEMPOOL4 && $MEMPOOL3 = $MEMPOOL4 ]]
120+
then
121+
break
122+
fi
123+
done
124+
}
125+
126+
echo "Generating initial blockchain..."
127+
128+
# 1 block, 50 XBT each == 50 BTC
129+
$CLI $B1ARGS setgenerate true 1
130+
WaitBlocks
131+
$CLI $B2ARGS setgenerate true 1
132+
WaitBlocks
133+
$CLI $B3ARGS setgenerate true 1
134+
WaitBlocks
135+
136+
# 100 blocks, 0 mature
137+
$CLI $B4ARGS setgenerate true 100
138+
WaitBlocks
139+
140+
CheckBalance "$B1ARGS" 50
141+
CheckBalance "$B2ARGS" 50
142+
CheckBalance "$B3ARGS" 50
143+
CheckBalance "$B4ARGS" 0
144+
145+
echo "Creating transactions..."
146+
147+
function S {
148+
TXID=$( $CLI -datadir=${D}/node${1} sendtoaddress ${2} "${3}" 0 )
149+
if [[ $TXID == "" ]] ; then
150+
echoerr "node${1}: error sending ${3} btc"
151+
echo -n "node${1} balance: "
152+
$CLI -datadir=${D}/node${1} getbalance "*" 0
153+
exit 1
154+
fi
155+
}
156+
157+
function OneRound {
158+
A1=$( $CLI $B1ARGS getnewaddress )
159+
A2=$( $CLI $B2ARGS getnewaddress )
160+
A3=$( $CLI $B3ARGS getnewaddress )
161+
if [[ $(( $RANDOM%2 )) < 1 ]] ; then
162+
N=$(( $RANDOM % 9 + 1 ))
163+
S 1 $A2 "0.$N"
164+
fi
165+
if [[ $(( $RANDOM%2 )) < 1 ]] ; then
166+
N=$(( $RANDOM % 9 + 1 ))
167+
S 1 $A3 "0.0$N"
168+
fi
169+
if [[ $(( $RANDOM%2 )) < 1 ]] ; then
170+
N=$(( $RANDOM % 9 + 1 ))
171+
S 2 $A1 "0.$N"
172+
fi
173+
if [[ $(( $RANDOM%2 )) < 1 ]] ; then
174+
N=$(( $RANDOM % 9 + 1 ))
175+
S 2 $A3 "0.$N"
176+
fi
177+
if [[ $(( $RANDOM%2 )) < 1 ]] ; then
178+
N=$(( $RANDOM % 9 + 1 ))
179+
S 3 $A1 "0.$N"
180+
fi
181+
if [[ $(( $RANDOM%2 )) < 1 ]] ; then
182+
N=$(( $RANDOM % 9 + 1 ))
183+
S 3 $A2 "0.0$N"
184+
fi
185+
$CLI "$B4ARGS" setgenerate true 1
186+
}
187+
188+
for i in {1..5}; do OneRound ; done
189+
190+
echo "Backing up..."
191+
192+
$CLI "$B1ARGS" backupwallet "$D1/wallet.bak"
193+
$CLI "$B1ARGS" dumpwallet "$D1/wallet.dump"
194+
$CLI "$B2ARGS" backupwallet "$D2/wallet.bak"
195+
$CLI "$B2ARGS" dumpwallet "$D2/wallet.dump"
196+
$CLI "$B3ARGS" backupwallet "$D3/wallet.bak"
197+
$CLI "$B3ARGS" dumpwallet "$D3/wallet.dump"
198+
199+
echo "More transactions..."
200+
for i in {1..5}; do OneRound ; done
201+
202+
WaitMemPools
203+
204+
# Generate 101 more blocks, so any fees paid
205+
# mature
206+
$CLI "$B4ARGS" setgenerate true 101
207+
208+
BALANCE1=$( $CLI "$B1ARGS" getbalance )
209+
BALANCE2=$( $CLI "$B2ARGS" getbalance )
210+
BALANCE3=$( $CLI "$B3ARGS" getbalance )
211+
BALANCE4=$( $CLI "$B4ARGS" getbalance )
212+
213+
TOTAL=$( dc -e "$BALANCE1 $BALANCE2 $BALANCE3 $BALANCE4 + + + p" )
214+
215+
AssertEqual $TOTAL 5700.00000000
216+
217+
function StopThree {
218+
$CLI $B1ARGS stop > /dev/null 2>&1
219+
$CLI $B2ARGS stop > /dev/null 2>&1
220+
$CLI $B3ARGS stop > /dev/null 2>&1
221+
wait $B1PID
222+
wait $B2PID
223+
wait $B3PID
224+
}
225+
function EraseThree {
226+
rm $D1/regtest/wallet.dat
227+
rm $D2/regtest/wallet.dat
228+
rm $D3/regtest/wallet.dat
229+
}
230+
function StartThree {
231+
$BITCOIND $BITCOINDARGS $B1ARGS &
232+
B1PID=$!
233+
$BITCOIND $BITCOINDARGS $B2ARGS &
234+
B2PID=$!
235+
$BITCOIND $BITCOINDARGS $B3ARGS &
236+
B3PID=$!
237+
}
238+
239+
echo "Restoring using wallet.dat"
240+
241+
StopThree
242+
EraseThree
243+
244+
# Start node3 with no chain
245+
rm -rf $D3/regtest/blocks
246+
rm -rf $D3/regtest/chainstate
247+
rm -rf $D3/regtest/database
248+
249+
cp $D1/wallet.bak $D1/regtest/wallet.dat
250+
cp $D2/wallet.bak $D2/regtest/wallet.dat
251+
cp $D3/wallet.bak $D3/regtest/wallet.dat
252+
253+
StartThree
254+
WaitBlocks
255+
256+
AssertEqual $BALANCE1 $( $CLI "$B1ARGS" getbalance )
257+
AssertEqual $BALANCE2 $( $CLI "$B2ARGS" getbalance )
258+
AssertEqual $BALANCE3 $( $CLI "$B3ARGS" getbalance )
259+
260+
echo "Restoring using dumped wallet"
261+
262+
StopThree
263+
EraseThree
264+
265+
# Start node3 with no chain
266+
rm -rf $D3/regtest/blocks
267+
rm -rf $D3/regtest/chainstate
268+
rm -rf $D3/regtest/database
269+
270+
StartThree
271+
272+
AssertEqual 0 $( $CLI "$B1ARGS" getbalance )
273+
AssertEqual 0 $( $CLI "$B2ARGS" getbalance )
274+
AssertEqual 0 $( $CLI "$B3ARGS" getbalance )
275+
276+
$CLI "$B1ARGS" importwallet $D1/wallet.dump
277+
$CLI "$B2ARGS" importwallet $D2/wallet.dump
278+
$CLI "$B3ARGS" importwallet $D3/wallet.dump
279+
280+
WaitBlocks
281+
282+
AssertEqual $BALANCE1 $( $CLI "$B1ARGS" getbalance )
283+
AssertEqual $BALANCE2 $( $CLI "$B2ARGS" getbalance )
284+
AssertEqual $BALANCE3 $( $CLI "$B3ARGS" getbalance )
285+
286+
StopThree
287+
$CLI $B4ARGS stop > /dev/null 2>&1
288+
wait $B4PID
289+
290+
echo "Tests successful, cleaning up"
291+
trap "" EXIT
292+
rm -rf $D
293+
exit 0

0 commit comments

Comments
 (0)