aboutsummaryrefslogtreecommitdiff
path: root/qa/rpc-tests
diff options
context:
space:
mode:
Diffstat (limited to 'qa/rpc-tests')
-rw-r--r--qa/rpc-tests/README.md25
-rwxr-xr-xqa/rpc-tests/send.sh28
-rwxr-xr-xqa/rpc-tests/txnmall.sh148
-rw-r--r--qa/rpc-tests/util.sh91
-rwxr-xr-xqa/rpc-tests/wallet.sh113
-rwxr-xr-xqa/rpc-tests/walletbackup.sh293
6 files changed, 698 insertions, 0 deletions
diff --git a/qa/rpc-tests/README.md b/qa/rpc-tests/README.md
new file mode 100644
index 0000000000..ee9e8b35ca
--- /dev/null
+++ b/qa/rpc-tests/README.md
@@ -0,0 +1,25 @@
+Regression tests of RPC interface
+=================================
+
+wallet.sh : Exercise wallet send/receive code.
+
+walletbackup.sh : Exercise wallet backup / dump / import
+
+txnmall.sh : Test proper accounting of malleable transactions
+
+conflictedbalance.sh : More testing of malleable transaction handling
+
+util.sh : useful re-usable bash functions
+
+
+Tips for creating new tests
+===========================
+
+To cleanup after a failed or interrupted test:
+ killall bitcoind
+ rm -rf test.*
+
+The most difficult part of writing reproducible tests is
+keeping multiple nodes in sync. See WaitBlocks,
+WaitPeers, and WaitMemPools for how other tests
+deal with this.
diff --git a/qa/rpc-tests/send.sh b/qa/rpc-tests/send.sh
new file mode 100755
index 0000000000..2c0d5375c0
--- /dev/null
+++ b/qa/rpc-tests/send.sh
@@ -0,0 +1,28 @@
+#!/bin/bash
+TIMEOUT=10
+SIGNAL=HUP
+PIDFILE=.send.pid
+if [ $# -eq 0 ]; then
+ echo -e "Usage:\t$0 <cmd>"
+ echo -e "\tRuns <cmd> and wait ${TIMEOUT} seconds or until SIG${SIGNAL} is received."
+ echo -e "\tReturns: 0 if SIG${SIGNAL} is received, 1 otherwise."
+ echo -e "Or:\t$0 -STOP"
+ echo -e "\tsends SIG${SIGNAL} to running send.sh"
+ exit 0
+fi
+
+if [ $1 == "-STOP" ]; then
+ if [ -s ${PIDFILE} ]; then
+ kill -s ${SIGNAL} $(<${PIDFILE})
+ fi
+ exit 0
+fi
+
+trap '[[ ${PID} ]] && kill ${PID}' ${SIGNAL}
+trap 'rm -f ${PIDFILE}' EXIT
+echo $$ > ${PIDFILE}
+"$@"
+sleep ${TIMEOUT} & PID=$!
+wait ${PID} && exit 1
+
+exit 0
diff --git a/qa/rpc-tests/txnmall.sh b/qa/rpc-tests/txnmall.sh
new file mode 100755
index 0000000000..6bf92fce40
--- /dev/null
+++ b/qa/rpc-tests/txnmall.sh
@@ -0,0 +1,148 @@
+#!/usr/bin/env bash
+
+# Test block generation and basic wallet sending
+
+if [ $# -lt 1 ]; then
+ echo "Usage: $0 path_to_binaries"
+ echo "e.g. $0 ../../src"
+ exit 1
+fi
+
+BITCOIND=${1}/bitcoind
+CLI=${1}/bitcoin-cli
+
+DIR="${BASH_SOURCE%/*}"
+SENDANDWAIT="${DIR}/send.sh"
+if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
+. "$DIR/util.sh"
+
+D=$(mktemp -d test.XXXXX)
+
+# Two nodes; one will play the part of merchant, the
+# other an evil transaction-mutating miner.
+
+D1=${D}/node1
+CreateDataDir $D1 port=11000 rpcport=11001
+B1ARGS="-datadir=$D1 -debug"
+$BITCOIND $B1ARGS &
+B1PID=$!
+
+D2=${D}/node2
+CreateDataDir $D2 port=11010 rpcport=11011
+B2ARGS="-datadir=$D2 -debug"
+$BITCOIND $B2ARGS &
+B2PID=$!
+
+trap "kill -9 $B1PID $B2PID; rm -rf $D" EXIT
+
+# Wait until all four nodes are at the same block number
+function WaitBlocks {
+ while :
+ do
+ sleep 1
+ BLOCKS1=$( GetBlocks $B1ARGS )
+ BLOCKS2=$( GetBlocks $B2ARGS )
+ if (( $BLOCKS1 == $BLOCKS2 ))
+ then
+ break
+ fi
+ done
+}
+
+# Wait until node has $N peers
+function WaitPeers {
+ while :
+ do
+ PEERS=$( $CLI $1 getconnectioncount )
+ if (( "$PEERS" == $2 ))
+ then
+ break
+ fi
+ sleep 1
+ done
+}
+
+# Start with B2 connected to B1:
+$CLI $B2ARGS addnode 127.0.0.1:11000 onetry
+WaitPeers "$B1ARGS" 1
+
+# 1 block, 50 XBT each == 50 XBT
+$CLI $B1ARGS setgenerate true 1
+
+WaitBlocks
+# 100 blocks, 0 mature == 0 XBT
+$CLI $B2ARGS setgenerate true 100
+WaitBlocks
+
+CheckBalance $B1ARGS 50
+CheckBalance $B2ARGS 0
+
+# restart B2 with no connection
+$CLI $B2ARGS stop > /dev/null 2>&1
+wait $B2PID
+$BITCOIND $B2ARGS &
+B2PID=$!
+
+B2ADDRESS=$( $CLI $B2ARGS getnewaddress )
+
+# Have B1 create two transactions; second will
+# spend change from first, since B1 starts with only a single
+# 50 bitcoin output:
+$CLI $B1ARGS move "" "foo" 10.0
+$CLI $B1ARGS move "" "bar" 10.0
+TXID1=$( $CLI $B1ARGS sendfrom foo $B2ADDRESS 1.0 0)
+TXID2=$( $CLI $B1ARGS sendfrom bar $B2ADDRESS 2.0 0)
+
+# Mutate TXID1 and add it to B2's memory pool:
+RAWTX1=$( $CLI $B1ARGS getrawtransaction $TXID1 )
+RAWTX2=$( $CLI $B1ARGS getrawtransaction $TXID2 )
+# ... mutate RAWTX1:
+# RAWTX1 is hex-encoded, serialized transaction. So each
+# byte is two characters; we'll prepend the first
+# "push" in the scriptsig with OP_PUSHDATA1 (0x4c),
+# and add one to the length of the signature.
+# Fields are fixed; from the beginning:
+# 4-byte version
+# 1-byte varint number-of inputs (one in this case)
+# 32-byte previous txid
+# 4-byte previous output
+# 1-byte varint length-of-scriptsig
+# 1-byte PUSH this many bytes onto stack
+# ... etc
+# So: to mutate, we want to get byte 41 (hex characters 82-83),
+# increment it, and insert 0x4c after it.
+L=${RAWTX1:82:2}
+NEWLEN=$( printf "%x" $(( 16#$L + 1 )) )
+MUTATEDTX1=${RAWTX1:0:82}${NEWLEN}4c${RAWTX1:84}
+# ... give mutated tx1 to B2:
+MUTATEDTXID=$( $CLI $B2ARGS sendrawtransaction $MUTATEDTX1 )
+
+echo "TXID1: " $TXID1
+echo "Mutated: " $MUTATEDTXID
+
+# Re-connect nodes, and have B2 mine a block
+$CLI $B2ARGS addnode 127.0.0.1:11000 onetry
+WaitPeers "$B1ARGS" 1
+
+$CLI $B2ARGS setgenerate true 3
+WaitBlocks
+$CLI $B1ARGS setgenerate true 3
+WaitBlocks
+
+$CLI $B2ARGS stop > /dev/null 2>&1
+wait $B2PID
+$CLI $B1ARGS stop > /dev/null 2>&1
+wait $B1PID
+
+trap "" EXIT
+
+echo "Done, bitcoind's shut down. To rerun/poke around:"
+echo "${1}/bitcoind -datadir=$D1 -daemon"
+echo "${1}/bitcoind -datadir=$D2 -daemon -connect=127.0.0.1:11000"
+echo "To cleanup:"
+echo "killall bitcoind; rm -rf test.*"
+exit 0
+
+echo "Tests successful, cleaning up"
+rm -rf $D
+exit 0
diff --git a/qa/rpc-tests/util.sh b/qa/rpc-tests/util.sh
new file mode 100644
index 0000000000..d1e4c941cc
--- /dev/null
+++ b/qa/rpc-tests/util.sh
@@ -0,0 +1,91 @@
+#!/usr/bin/env bash
+
+# Functions used by more than one test
+
+function echoerr {
+ echo "$@" 1>&2;
+}
+
+# Usage: ExtractKey <key> "<json_object_string>"
+# Warning: this will only work for the very-well-behaved
+# JSON produced by bitcoind, do NOT use it to try to
+# parse arbitrary/nested/etc JSON.
+function ExtractKey {
+ echo $2 | tr -d ' "{}\n' | awk -v RS=',' -F: "\$1 ~ /$1/ { print \$2}"
+}
+
+function CreateDataDir {
+ DIR=$1
+ mkdir -p $DIR
+ CONF=$DIR/bitcoin.conf
+ echo "regtest=1" >> $CONF
+ echo "keypool=2" >> $CONF
+ echo "rpcuser=rt" >> $CONF
+ echo "rpcpassword=rt" >> $CONF
+ echo "rpcwait=1" >> $CONF
+ echo "walletnotify=${SENDANDWAIT} -STOP" >> $CONF
+ shift
+ while (( "$#" )); do
+ echo $1 >> $CONF
+ shift
+ done
+}
+
+function AssertEqual {
+ if (( $( echo "$1 == $2" | bc ) == 0 ))
+ then
+ echoerr "AssertEqual: $1 != $2"
+ exit 1
+ fi
+}
+
+# CheckBalance -datadir=... amount account minconf
+function CheckBalance {
+ B=$( $CLI $1 getbalance $3 $4 )
+ if (( $( echo "$B == $2" | bc ) == 0 ))
+ then
+ echoerr "bad balance: $B (expected $2)"
+ exit 1
+ fi
+}
+
+# Use: Address <datadir> [account]
+function Address {
+ $CLI $1 getnewaddress $2
+}
+
+# Send from to amount
+function Send {
+ from=$1
+ to=$2
+ amount=$3
+ address=$(Address $to)
+ txid=$( ${SENDANDWAIT} $CLI $from sendtoaddress $address $amount )
+}
+
+# Use: Unspent <datadir> <n'th-last-unspent> <var>
+function Unspent {
+ local r=$( $CLI $1 listunspent | awk -F'[ |:,"]+' "\$2 ~ /$3/ { print \$3 }" | tail -n $2 | head -n 1)
+ echo $r
+}
+
+# Use: CreateTxn1 <datadir> <n'th-last-unspent> <destaddress>
+# produces hex from signrawtransaction
+function CreateTxn1 {
+ TXID=$(Unspent $1 $2 txid)
+ AMOUNT=$(Unspent $1 $2 amount)
+ VOUT=$(Unspent $1 $2 vout)
+ RAWTXN=$( $CLI $1 createrawtransaction "[{\"txid\":\"$TXID\",\"vout\":$VOUT}]" "{\"$3\":$AMOUNT}")
+ ExtractKey hex "$( $CLI $1 signrawtransaction $RAWTXN )"
+}
+
+# Use: SendRawTxn <datadir> <hex_txn_data>
+function SendRawTxn {
+ ${SENDANDWAIT} $CLI $1 sendrawtransaction $2
+}
+
+# Use: GetBlocks <datadir>
+# returns number of blocks from getinfo
+function GetBlocks {
+ ExtractKey blocks "$( $CLI $1 getinfo )"
+}
diff --git a/qa/rpc-tests/wallet.sh b/qa/rpc-tests/wallet.sh
new file mode 100755
index 0000000000..8d5a6cdc78
--- /dev/null
+++ b/qa/rpc-tests/wallet.sh
@@ -0,0 +1,113 @@
+#!/usr/bin/env bash
+
+# Test block generation and basic wallet sending
+
+if [ $# -lt 1 ]; then
+ echo "Usage: $0 path_to_binaries"
+ echo "e.g. $0 ../../src"
+ exit 1
+fi
+
+BITCOIND=${1}/bitcoind
+CLI=${1}/bitcoin-cli
+
+DIR="${BASH_SOURCE%/*}"
+SENDANDWAIT="${DIR}/send.sh"
+if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
+. "$DIR/util.sh"
+
+D=$(mktemp -d test.XXXXX)
+
+D1=${D}/node1
+CreateDataDir $D1 port=11000 rpcport=11001
+B1ARGS="-datadir=$D1"
+$BITCOIND $B1ARGS &
+B1PID=$!
+
+D2=${D}/node2
+CreateDataDir $D2 port=11010 rpcport=11011 connect=127.0.0.1:11000
+B2ARGS="-datadir=$D2"
+$BITCOIND $B2ARGS &
+B2PID=$!
+
+D3=${D}/node3
+CreateDataDir $D3 port=11020 rpcport=11021 connect=127.0.0.1:11000
+B3ARGS="-datadir=$D3"
+$BITCOIND $BITCOINDARGS $B3ARGS &
+B3PID=$!
+
+trap "kill -9 $B1PID $B2PID $B3PID; rm -rf $D" EXIT
+
+# Wait until all three nodes are at the same block number
+function WaitBlocks {
+ while :
+ do
+ sleep 1
+ BLOCKS1=$( GetBlocks $B1ARGS )
+ BLOCKS2=$( GetBlocks $B2ARGS )
+ BLOCKS3=$( GetBlocks $B3ARGS )
+ if (( $BLOCKS1 == $BLOCKS2 && $BLOCKS2 == $BLOCKS3 ))
+ then
+ break
+ fi
+ done
+}
+
+# 1 block, 50 XBT each == 50 XBT
+$CLI $B1ARGS setgenerate true 1
+WaitBlocks
+# 101 blocks, 1 mature == 50 XBT
+$CLI $B2ARGS setgenerate true 101
+WaitBlocks
+
+CheckBalance $B1ARGS 50
+CheckBalance $B2ARGS 50
+
+# Send 21 XBT from 1 to 3. Second
+# transaction will be child of first, and
+# will require a fee
+Send $B1ARGS $B3ARGS 11
+Send $B1ARGS $B3ARGS 10
+
+# Have B1 mine a new block, and mature it
+# to recover transaction fees
+$CLI $B1ARGS setgenerate true 1
+WaitBlocks
+
+# Have B2 mine 100 blocks so B1's block is mature:
+$CLI $B2ARGS setgenerate true 100
+WaitBlocks
+
+# B1 should end up with 100 XBT in block rewards plus fees,
+# minus the 21 XBT sent to B3:
+CheckBalance $B1ARGS "100-21"
+CheckBalance $B3ARGS "21"
+
+# B1 should have two unspent outputs; create a couple
+# of raw transactions to send them to B3, submit them through
+# B2, and make sure both B1 and B3 pick them up properly:
+RAW1=$(CreateTxn1 $B1ARGS 1 $(Address $B3ARGS "from1" ) )
+RAW2=$(CreateTxn1 $B1ARGS 2 $(Address $B3ARGS "from1" ) )
+RAWTXID1=$(SendRawTxn $B2ARGS $RAW1)
+RAWTXID2=$(SendRawTxn $B2ARGS $RAW2)
+
+# Have B2 mine a block to confirm transactions:
+$CLI $B2ARGS setgenerate true 1
+WaitBlocks
+
+# Check balances after confirmation
+CheckBalance $B1ARGS 0
+CheckBalance $B3ARGS 100
+CheckBalance $B3ARGS "100-21" "from1"
+
+$CLI $B3ARGS stop > /dev/null 2>&1
+wait $B3PID
+$CLI $B2ARGS stop > /dev/null 2>&1
+wait $B2PID
+$CLI $B1ARGS stop > /dev/null 2>&1
+wait $B1PID
+
+echo "Tests successful, cleaning up"
+trap "" EXIT
+rm -rf $D
+exit 0
diff --git a/qa/rpc-tests/walletbackup.sh b/qa/rpc-tests/walletbackup.sh
new file mode 100755
index 0000000000..9207243b62
--- /dev/null
+++ b/qa/rpc-tests/walletbackup.sh
@@ -0,0 +1,293 @@
+#!/usr/bin/env bash
+
+# Test wallet backup / dump / restore functionality
+
+# Test case is:
+# 4 nodes. 1 2 3 and send transactions between each other,
+# fourth node is a miner.
+# 1 2 3 and each mine a block to start, then
+# miner creates 100 blocks so 1 2 3 each have 50 mature
+# coins to spend.
+# Then 5 iterations of 1/2/3 sending coins amongst
+# themselves to get transactions in the wallets,
+# and the miner mining one block.
+#
+# Wallets are backed up using dumpwallet/backupwallet.
+# Then 5 more iterations of transactions, then block.
+#
+# Miner then generates 101 more blocks, so any
+# transaction fees paid mature.
+#
+# Sanity checks done:
+# Miner balance >= 150*50
+# Sum(1,2,3,4 balances) == 153*150
+#
+# 1/2/3 are shutdown, and their wallets erased.
+# Then restore using wallet.dat backup. And
+# confirm 1/2/3/4 balances are same as before.
+#
+# Shutdown again, restore using importwallet,
+# and confirm again balances are correct.
+#
+
+if [ $# -lt 1 ]; then
+ echo "Usage: $0 path_to_binaries"
+ echo "e.g. $0 ../../src"
+ exit 1
+fi
+
+BITCOIND=${1}/bitcoind
+CLI=${1}/bitcoin-cli
+
+DIR="${BASH_SOURCE%/*}"
+SENDANDWAIT="${DIR}/send.sh"
+if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
+. "$DIR/util.sh"
+
+D=$(mktemp -d test.XXXXX)
+
+echo "Starting nodes..."
+
+# "Miner":
+D4=${D}/node4
+CreateDataDir $D4 port=11030 rpcport=11031
+B4ARGS="-datadir=$D4"
+$BITCOIND $BITCOINDARGS $B4ARGS &
+B4PID=$!
+
+# Want default keypool for 1/2/3, and
+# don't need send-and-wait functionality,
+# so don't use CreateDataDir:
+function CreateConfDir {
+ DIR=$1
+ mkdir -p $DIR
+ CONF=$DIR/bitcoin.conf
+ echo "regtest=1" >> $CONF
+ echo "rpcuser=rt" >> $CONF
+ echo "rpcpassword=rt" >> $CONF
+ echo "rpcwait=1" >> $CONF
+ shift
+ while (( "$#" )); do
+ echo $1 >> $CONF
+ shift
+ done
+}
+
+# "Spenders" 1/2/3
+D1=${D}/node1
+CreateConfDir $D1 port=11000 rpcport=11001 addnode=127.0.0.1:11030
+B1ARGS="-datadir=$D1"
+$BITCOIND $B1ARGS &
+B1PID=$!
+D2=${D}/node2
+CreateConfDir $D2 port=11010 rpcport=11011 addnode=127.0.0.1:11030
+B2ARGS="-datadir=$D2"
+$BITCOIND $B2ARGS &
+B2PID=$!
+D3=${D}/node3
+CreateConfDir $D3 port=11020 rpcport=11021 addnode=127.0.0.1:11030 addnode=127.0.0.1:11000
+B3ARGS="-datadir=$D3"
+$BITCOIND $BITCOINDARGS $B3ARGS &
+B3PID=$!
+
+# Wait until all nodes are at the same block number
+function WaitBlocks {
+ while :
+ do
+ sleep 1
+ BLOCKS1=$( GetBlocks "$B1ARGS" )
+ BLOCKS2=$( GetBlocks "$B2ARGS" )
+ BLOCKS3=$( GetBlocks "$B3ARGS" )
+ BLOCKS4=$( GetBlocks "$B4ARGS" )
+ if (( BLOCKS1 == BLOCKS4 && BLOCKS2 == BLOCKS4 && BLOCKS3 == BLOCKS4 ))
+ then
+ break
+ fi
+ done
+}
+
+# Wait until all nodes have the same txns in
+# their memory pools
+function WaitMemPools {
+ while :
+ do
+ sleep 1
+ MEMPOOL1=$( $CLI "$B1ARGS" getrawmempool | sort | shasum )
+ MEMPOOL2=$( $CLI "$B2ARGS" getrawmempool | sort | shasum )
+ MEMPOOL3=$( $CLI "$B3ARGS" getrawmempool | sort | shasum )
+ MEMPOOL4=$( $CLI "$B4ARGS" getrawmempool | sort | shasum )
+ if [[ $MEMPOOL1 = $MEMPOOL4 && $MEMPOOL2 = $MEMPOOL4 && $MEMPOOL3 = $MEMPOOL4 ]]
+ then
+ break
+ fi
+ done
+}
+
+echo "Generating initial blockchain..."
+
+# 1 block, 50 XBT each == 50 BTC
+$CLI $B1ARGS setgenerate true 1
+WaitBlocks
+$CLI $B2ARGS setgenerate true 1
+WaitBlocks
+$CLI $B3ARGS setgenerate true 1
+WaitBlocks
+
+# 100 blocks, 0 mature
+$CLI $B4ARGS setgenerate true 100
+WaitBlocks
+
+CheckBalance "$B1ARGS" 50
+CheckBalance "$B2ARGS" 50
+CheckBalance "$B3ARGS" 50
+CheckBalance "$B4ARGS" 0
+
+echo "Creating transactions..."
+
+function S {
+ TXID=$( $CLI -datadir=${D}/node${1} sendtoaddress ${2} "${3}" 0 )
+ if [[ $TXID == "" ]] ; then
+ echoerr "node${1}: error sending ${3} btc"
+ echo -n "node${1} balance: "
+ $CLI -datadir=${D}/node${1} getbalance "*" 0
+ exit 1
+ fi
+}
+
+function OneRound {
+ A1=$( $CLI $B1ARGS getnewaddress )
+ A2=$( $CLI $B2ARGS getnewaddress )
+ A3=$( $CLI $B3ARGS getnewaddress )
+ if [[ $(( $RANDOM%2 )) < 1 ]] ; then
+ N=$(( $RANDOM % 9 + 1 ))
+ S 1 $A2 "0.$N"
+ fi
+ if [[ $(( $RANDOM%2 )) < 1 ]] ; then
+ N=$(( $RANDOM % 9 + 1 ))
+ S 1 $A3 "0.0$N"
+ fi
+ if [[ $(( $RANDOM%2 )) < 1 ]] ; then
+ N=$(( $RANDOM % 9 + 1 ))
+ S 2 $A1 "0.$N"
+ fi
+ if [[ $(( $RANDOM%2 )) < 1 ]] ; then
+ N=$(( $RANDOM % 9 + 1 ))
+ S 2 $A3 "0.$N"
+ fi
+ if [[ $(( $RANDOM%2 )) < 1 ]] ; then
+ N=$(( $RANDOM % 9 + 1 ))
+ S 3 $A1 "0.$N"
+ fi
+ if [[ $(( $RANDOM%2 )) < 1 ]] ; then
+ N=$(( $RANDOM % 9 + 1 ))
+ S 3 $A2 "0.0$N"
+ fi
+ $CLI "$B4ARGS" setgenerate true 1
+}
+
+for i in {1..5}; do OneRound ; done
+
+echo "Backing up..."
+
+$CLI "$B1ARGS" backupwallet "$D1/wallet.bak"
+$CLI "$B1ARGS" dumpwallet "$D1/wallet.dump"
+$CLI "$B2ARGS" backupwallet "$D2/wallet.bak"
+$CLI "$B2ARGS" dumpwallet "$D2/wallet.dump"
+$CLI "$B3ARGS" backupwallet "$D3/wallet.bak"
+$CLI "$B3ARGS" dumpwallet "$D3/wallet.dump"
+
+echo "More transactions..."
+for i in {1..5}; do OneRound ; done
+
+WaitMemPools
+
+# Generate 101 more blocks, so any fees paid
+# mature
+$CLI "$B4ARGS" setgenerate true 101
+
+BALANCE1=$( $CLI "$B1ARGS" getbalance )
+BALANCE2=$( $CLI "$B2ARGS" getbalance )
+BALANCE3=$( $CLI "$B3ARGS" getbalance )
+BALANCE4=$( $CLI "$B4ARGS" getbalance )
+
+TOTAL=$( dc -e "$BALANCE1 $BALANCE2 $BALANCE3 $BALANCE4 + + + p" )
+
+AssertEqual $TOTAL 5700.00000000
+
+function StopThree {
+ $CLI $B1ARGS stop > /dev/null 2>&1
+ $CLI $B2ARGS stop > /dev/null 2>&1
+ $CLI $B3ARGS stop > /dev/null 2>&1
+ wait $B1PID
+ wait $B2PID
+ wait $B3PID
+}
+function EraseThree {
+ rm $D1/regtest/wallet.dat
+ rm $D2/regtest/wallet.dat
+ rm $D3/regtest/wallet.dat
+}
+function StartThree {
+ $BITCOIND $BITCOINDARGS $B1ARGS &
+ B1PID=$!
+ $BITCOIND $BITCOINDARGS $B2ARGS &
+ B2PID=$!
+ $BITCOIND $BITCOINDARGS $B3ARGS &
+ B3PID=$!
+}
+
+echo "Restoring using wallet.dat"
+
+StopThree
+EraseThree
+
+# Start node3 with no chain
+rm -rf $D3/regtest/blocks
+rm -rf $D3/regtest/chainstate
+rm -rf $D3/regtest/database
+
+cp $D1/wallet.bak $D1/regtest/wallet.dat
+cp $D2/wallet.bak $D2/regtest/wallet.dat
+cp $D3/wallet.bak $D3/regtest/wallet.dat
+
+StartThree
+WaitBlocks
+
+AssertEqual $BALANCE1 $( $CLI "$B1ARGS" getbalance )
+AssertEqual $BALANCE2 $( $CLI "$B2ARGS" getbalance )
+AssertEqual $BALANCE3 $( $CLI "$B3ARGS" getbalance )
+
+echo "Restoring using dumped wallet"
+
+StopThree
+EraseThree
+
+# Start node3 with no chain
+rm -rf $D3/regtest/blocks
+rm -rf $D3/regtest/chainstate
+rm -rf $D3/regtest/database
+
+StartThree
+
+AssertEqual 0 $( $CLI "$B1ARGS" getbalance )
+AssertEqual 0 $( $CLI "$B2ARGS" getbalance )
+AssertEqual 0 $( $CLI "$B3ARGS" getbalance )
+
+$CLI "$B1ARGS" importwallet $D1/wallet.dump
+$CLI "$B2ARGS" importwallet $D2/wallet.dump
+$CLI "$B3ARGS" importwallet $D3/wallet.dump
+
+WaitBlocks
+
+AssertEqual $BALANCE1 $( $CLI "$B1ARGS" getbalance )
+AssertEqual $BALANCE2 $( $CLI "$B2ARGS" getbalance )
+AssertEqual $BALANCE3 $( $CLI "$B3ARGS" getbalance )
+
+StopThree
+$CLI $B4ARGS stop > /dev/null 2>&1
+wait $B4PID
+
+echo "Tests successful, cleaning up"
+trap "" EXIT
+rm -rf $D
+exit 0