Skip to content

Commit 9e7776b

Browse files
committed
Integration tests via RPC calls
qa/rpc-tests/wallet.sh runs a three-node -regtest network, generates a fresh blockchain, and then exercises basic wallet sending/receiving functionality using command-line RPC.
1 parent 03b6a1c commit 9e7776b

File tree

4 files changed

+190
-0
lines changed

4 files changed

+190
-0
lines changed

qa/pull-tester/build-tests.sh.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ fi
6868
cd @abs_top_srcdir@/linux-build
6969
make check
7070

71+
# Run RPC integration test on Linux:
72+
@abs_top_srcdir@/qa/rpc-tests/wallet.sh @abs_top_srcdir@/linux-build/src
73+
7174
if [ $RUN_EXPENSIVE_TESTS = 1 ]; then
7275
# Run unit tests and blockchain-tester on Windows:
7376
cd @abs_top_srcdir@/win32-build

qa/rpc-tests/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Regression tests of RPC interface
2+
=================================
3+
4+
wallet.sh : Test wallet send/receive code (see comments for details)
5+
6+
util.sh : useful re-usable functions

qa/rpc-tests/util.sh

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env bash
2+
3+
# Functions used by more than one test
4+
5+
function echoerr {
6+
echo "$@" 1>&2;
7+
}
8+
9+
# Usage: ExtractKey <key> "<json_object_string>"
10+
# Warning: this will only work for the very-well-behaved
11+
# JSON produced by bitcoind, do NOT use it to try to
12+
# parse arbitrary/nested/etc JSON.
13+
function ExtractKey {
14+
echo $2 | tr -d ' "{}\n' | awk -v RS=',' -F: "\$1 ~ /$1/ { print \$2}"
15+
}
16+
17+
function CreateDataDir {
18+
DIR=$1
19+
mkdir -p $DIR
20+
CONF=$DIR/bitcoin.conf
21+
echo "regtest=1" >> $CONF
22+
echo "keypool=2" >> $CONF
23+
echo "rpcuser=rt" >> $CONF
24+
echo "rpcpassword=rt" >> $CONF
25+
echo "rpcwait=1" >> $CONF
26+
shift
27+
while (( "$#" )); do
28+
echo $1 >> $CONF
29+
shift
30+
done
31+
}
32+
33+
function AssertEqual {
34+
if (( $( echo "$1 == $2" | bc ) == 0 ))
35+
then
36+
echoerr "AssertEqual: $1 != $2"
37+
exit 1
38+
fi
39+
}
40+
41+
# CheckBalance -datadir=... amount account minconf
42+
function CheckBalance {
43+
B=$( $CLI $1 getbalance $3 $4 )
44+
if (( $( echo "$B == $2" | bc ) == 0 ))
45+
then
46+
echoerr "bad balance: $B (expected $2)"
47+
exit 1
48+
fi
49+
}
50+
51+
# Use: Address <datadir> [account]
52+
function Address {
53+
$CLI $1 getnewaddress $2
54+
}
55+
56+
# Send from to amount
57+
function Send {
58+
from=$1
59+
to=$2
60+
amount=$3
61+
address=$(Address $to)
62+
txid=$( $CLI $from sendtoaddress $address $amount )
63+
}
64+
65+
# Use: Unspent <datadir> <n'th-last-unspent> <var>
66+
function Unspent {
67+
local r=$( $CLI $1 listunspent | awk -F'[ |:,"]+' "\$2 ~ /$3/ { print \$3 }" | tail -n $2 | head -n 1)
68+
echo $r
69+
}
70+
71+
# Use: CreateTxn1 <datadir> <n'th-last-unspent> <destaddress>
72+
# produces hex from signrawtransaction
73+
function CreateTxn1 {
74+
TXID=$(Unspent $1 $2 txid)
75+
AMOUNT=$(Unspent $1 $2 amount)
76+
VOUT=$(Unspent $1 $2 vout)
77+
RAWTXN=$( $CLI $1 createrawtransaction "[{\"txid\":\"$TXID\",\"vout\":$VOUT}]" "{\"$3\":$AMOUNT}")
78+
ExtractKey hex "$( $CLI $1 signrawtransaction $RAWTXN )"
79+
}
80+
81+
# Use: SendRawTxn <datadir> <hex_txn_data>
82+
function SendRawTxn {
83+
$CLI $1 sendrawtransaction $2
84+
}

qa/rpc-tests/wallet.sh

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env bash
2+
3+
# Test block generation and basic wallet sending
4+
5+
if [ $# -lt 1 ]; then
6+
echo "Usage: $0 path_to_binaries"
7+
echo "e.g. $0 ../../src"
8+
exit 1
9+
fi
10+
11+
BITCOIND=${1}/bitcoind
12+
CLI=${1}/bitcoin-cli
13+
14+
DIR="${BASH_SOURCE%/*}"
15+
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
16+
. "$DIR/util.sh"
17+
18+
D=$(mktemp -d test.XXXXX)
19+
20+
D1=${D}/node1
21+
CreateDataDir $D1 port=11000 rpcport=11001
22+
B1ARGS="-datadir=$D1"
23+
$BITCOIND $B1ARGS &
24+
B1PID=$!
25+
26+
D2=${D}/node2
27+
CreateDataDir $D2 port=11010 rpcport=11011 connect=127.0.0.1:11000
28+
B2ARGS="-datadir=$D2"
29+
$BITCOIND $B2ARGS &
30+
B2PID=$!
31+
32+
D3=${D}/node3
33+
CreateDataDir $D3 port=11020 rpcport=11021 connect=127.0.0.1:11000
34+
B3ARGS="-datadir=$D3"
35+
$BITCOIND $BITCOINDARGS $B3ARGS &
36+
B3PID=$!
37+
38+
trap "kill -9 $B1PID $B2PID $B3PID; rm -rf $D" EXIT
39+
40+
# 1 block, 50 XBT each == 50 XBT
41+
$CLI $B1ARGS setgenerate true 1
42+
sleep 1 # sleep is necessary to let block broadcast
43+
# 101 blocks, 1 mature == 50 XBT
44+
$CLI $B2ARGS setgenerate true 101
45+
sleep 1
46+
47+
CheckBalance $B1ARGS 50
48+
CheckBalance $B2ARGS 50
49+
50+
# Send 21 XBT from 1 to 3. Second
51+
# transaction will be child of first, and
52+
# will require a fee
53+
Send $B1ARGS $B3ARGS 11
54+
Send $B1ARGS $B3ARGS 10
55+
56+
# Have B1 mine a new block, and mature it
57+
# to recover transaction fees
58+
$CLI $B1ARGS setgenerate true 1
59+
sleep 1
60+
61+
# Have B2 mine 100 blocks so B1's block is mature:
62+
$CLI $B2ARGS setgenerate true 100
63+
sleep 1
64+
65+
# B1 should end up with 100 XBT in block rewards plus fees,
66+
# minus the 21 XBT sent to B3:
67+
CheckBalance $B1ARGS "100-21"
68+
CheckBalance $B3ARGS "21"
69+
70+
# B1 should have two unspent outputs; create a couple
71+
# of raw transactions to send them to B3, submit them through
72+
# B2, and make sure both B1 and B3 pick them up properly:
73+
RAW1=$(CreateTxn1 $B1ARGS 1 $(Address $B3ARGS "from1" ) )
74+
RAW2=$(CreateTxn1 $B1ARGS 2 $(Address $B3ARGS "from1" ) )
75+
RAWTXID1=$(SendRawTxn $B2ARGS $RAW1)
76+
RAWTXID2=$(SendRawTxn $B2ARGS $RAW2)
77+
78+
# Have B2 mine a block to confirm transactions:
79+
$CLI $B2ARGS setgenerate true 1
80+
sleep 1 # allow time for block to be relayed
81+
82+
# Check balances after confirmation
83+
CheckBalance $B1ARGS 0
84+
CheckBalance $B3ARGS 100
85+
CheckBalance $B3ARGS "100-21" "from1"
86+
87+
$CLI $B3ARGS stop > /dev/null 2>&1
88+
wait $B3PID
89+
$CLI $B2ARGS stop > /dev/null 2>&1
90+
wait $B2PID
91+
$CLI $B1ARGS stop > /dev/null 2>&1
92+
wait $B1PID
93+
94+
echo "Tests successful, cleaning up"
95+
trap "" EXIT
96+
rm -rf $D
97+
exit 0

0 commit comments

Comments
 (0)