summaryrefslogtreecommitdiff
path: root/test/common.sh
blob: a28165eb22ab3e6c636c570794b434131cef2b14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/bin/bash

## Test utils

set -eu

# Cleanup to run whenever we exit
function cleanup() {
    pg_ctl stop -D $DB_DIR -w &> /dev/null
    for n in `jobs -p`; do
        kill $n &> /dev/null || true
    done
    for n in `jobs -p`; do
        wait $n || true
    done
    rm -rf $DIR &> /dev/null
    wait
}

# Install cleanup handler (except for kill -9)
trap cleanup EXIT

# Init temporary dirs
DIR=$(mktemp -d)
BTC_DIR=$DIR/bitcoin
BTC_DIR2=$DIR/bitcoin2
DB_DIR=$DIR/db
CONF=$DIR/taler.conf
for dir in $BTC_DIR $BTC_DIR2 $DB_DIR log; do
    mkdir -p $dir
done

# Clear logs
for log in log/*; do
    echo -n "" > $log
done

# Setup command helpers
BTC_CLI="bitcoin-cli -datadir=$BTC_DIR"
BTC_CLI2="bitcoin-cli -datadir=$BTC_DIR2"

# Load test.conf as bash variables
function load_config() {
    cp ${BASH_SOURCE%/*}/conf/${CONFIG:-taler_test.conf} $CONF
    echo -e "\nBTC_DATA_DIR = ${BTC_DIR}" >> $CONF
    source <(grep = $CONF | sed 's/ *= */=/' | sed 's/=\(.*\)/="\1"/g1')
    BANK_ENDPOINT=http://127.0.0.1:$PORT/
}

# Check process is running
function check_up() {
    if [ `ps -p $1 | grep -c $1` == 0 ]; then
        echo "${2:-process} with pid $1 should be up"
        ps -p $1
        exit 1
    fi
}

# Check process is not running
function check_down() {
    if [ `ps -p $1 | grep -c $1` != 0 ]; then 
        echo "${2:-process} with pid $1 should be down"
        ps -p $1
        exit 1
    fi
}

# ----- Database ----- #

# Create new postgresql cluster and init database schema
function setup_db() {
    pg_ctl init -D $DB_DIR &>> log/postgres.log
    echo "port=5454" >> $DB_DIR/postgresql.conf
    pg_ctl start -D $DB_DIR >> log/postgres.log
    echo "CREATE ROLE postgres LOGIN SUPERUSER PASSWORD 'password'" | psql -p 5454 postgres > /dev/null
    btc-wire-cli initdb $CONF > /dev/null
}

# Erase database
function reset_db() {
    btc-wire-utils cleardb $CONF
    btc-wire-cli initdb $CONF
}

# ----- Bitcoin node ----- #

# Start a bitcoind regtest node, generate money, wallet and addresses
function init_btc() {
    cp ${BASH_SOURCE%/*}/conf/${BTC_CONFIG:-bitcoin.conf} $BTC_DIR/bitcoin.conf
    bitcoind -datadir=$BTC_DIR $* &>> log/btc.log &
    BTC_PID="$!"
    # Wait for RPC server to be online
    $BTC_CLI -rpcwait getnetworkinfo > /dev/null
    # Create wire wallet
    btc-wire-cli initwallet $CONF > /dev/null
    # Load wallets
    for wallet in client reserve; do
        $BTC_CLI createwallet $wallet > /dev/null
    done
    # Generate addresses
    RESERVE=`$BTC_CLI -rpcwallet=reserve getnewaddress`
    CLIENT=`$BTC_CLI -rpcwallet=client getnewaddress`
    WIRE=`$BTC_CLI -rpcwallet=wire getnewaddress`
    # Generate money
    mine_btc 101
    $BTC_CLI -rpcwallet=reserve sendtoaddress $CLIENT 10 > /dev/null
    mine_btc
}

# Start a second bitcoind regtest node connected to the first one
function init_btc2() {
    cp ${BASH_SOURCE%/*}/conf/bitcoin2.conf $BTC_DIR2/bitcoin.conf
    bitcoind -datadir=$BTC_DIR2 $* &>> log/btc2.log &
    $BTC_CLI2 -rpcwait getnetworkinfo > /dev/null    
    $BTC_CLI addnode 127.0.0.1:8346 onetry
}

# Disconnect the two nodes
function btc2_deco() {
    $BTC_CLI disconnectnode 127.0.0.1:8346
}

# Create a fork on the second node and reconnect the two node
function btc2_fork() {
    $BTC_CLI2 generatetoaddress $1 $RESERVE > /dev/null
    $BTC_CLI addnode 127.0.0.1:8346 onetry
    sleep 1
}

# Restart a bitcoind regest server in a previously created temporary directory and load wallets
function resume_btc() {
    # Restart node
    bitcoind -datadir=$BTC_DIR $* &>> log/btc.log &
    BTC_PID="$!"
    # Wait for RPC server to be online
    $BTC_CLI -rpcwait getnetworkinfo > /dev/null    
    # Load wallets
    for wallet in wire client reserve; do
        $BTC_CLI loadwallet $wallet > /dev/null
    done
    # Connect second node
    $BTC_CLI addnode 127.0.0.1:8346 onetry
}

function stop_btc() {
    kill $BTC_PID 
    wait $BTC_PID
}

function restart_btc() {
    stop_btc
    resume_btc $*
}

# Mine blocks
function mine_btc() {
    $BTC_CLI generatetoaddress "${1:-1}" $RESERVE > /dev/null
}

# Mine previous transactions
function next_btc() {
    # Mine enough block to confirm previous transactions
    mine_btc ${1:-$CONFIRMATION}
    # Wait for btc_wire to catch up
    sleep 0.2
    # Mine one more block to trigger btc_wire
    mine_btc
}

# Remove money from wallet wire
function clear_wallet() {
    $BTC_CLI -rpcwallet=wire sendtoaddress $CLIENT `$BTC_CLI -rpcwallet=wire getbalance` "" "" true > /dev/null
    mine_btc
}

# Check client and wire balance
function check_balance() {
    local CLIENT_BALANCE=`$BTC_CLI -rpcwallet=client getbalance`
    local WIRE_BALANCE=`$BTC_CLI -rpcwallet=wire getbalance`
    local CLIENT="${1:-*}"
    if [ "$1" == "*" ]; then
        local CLIENT="$CLIENT_BALANCE"
    fi
    if [ "$CLIENT_BALANCE" != "$CLIENT" ] || [ "$WIRE_BALANCE" != "${2:-$WIRE_BALANCE}" ]; then
        echo "expected: client $CLIENT wire ${2:-$WIRE_BALANCE}    got: client $CLIENT_BALANCE wire $WIRE_BALANCE"
        exit 1
    fi
}

# ----- btc-wire ----- #

# Start btc_wire
function btc_wire() {    
    cargo build --bin btc-wire --release &> /dev/null
    target/release/btc-wire $CONF &>> log/btc_wire.log &
    WIRE_PID="$!"
}

# Start btc_wire with random failures
function fail_btc_wire() {    
    cargo build --bin btc-wire --release --features fail &> /dev/null
    target/release/btc-wire $CONF &>> log/btc_wire.log &
    WIRE_PID="$!"
}

# Start multiple btc_wire in parallel
function stressed_btc_wire() {
   cargo build --bin btc-wire --release &> /dev/null
   target/release/btc-wire $CONF &>> log/btc_wire.log & 
   target/release/btc-wire $CONF &>> log/btc_wire1.log & 
   target/release/btc-wire $CONF &>> log/btc_wire2.log & 
}

# ----- Gateway ------ #

# Start wire_gateway in test mode
function gateway() {
    cargo build --bin wire-gateway --release --features test &> /dev/null
    target/release/wire-gateway $CONF &>> log/gateway.log & 
    GATEWAY_PID="$!"
    for n in `seq 1 50`; do
        echo -n "."
        sleep 0.2
        curl -s $BANK_ENDPOINT -o /dev/null && break
    done
}

# Check wire-gateway is healthy
function gateway_up() {
    if [ `curl -w %{http_code} -s ${BANK_ENDPOINT}history/outgoing -o /dev/null` -ne 400 ]; then
        echo "gateway should be up"
        exit 1
    fi
}

# Check wire-gateway is down (backend is blocked)
function gateway_down() {
    if [ `curl -w %{http_code} -s ${BANK_ENDPOINT}history/outgoing -o /dev/null` -ne 502 ]; then 
        echo "gateway should be down"
        exit 1
    fi
}

# Check history endpoint request return a specific amount of transactions of specific amounts
# usage: check_delta endpoint nb_txs amount_sequence
function check_delta() {
    ALL=`curl -s ${BANK_ENDPOINT}history/$1`
    PRE=${3:-0.0000}
    for n in `$2`; do
        if ! `echo $ALL | grep BTC:$PRE$n > /dev/null`; then
            echo -n " missing tx with amount: BTC:$PRE$n"
            return 1
        fi
    done
    NB=`echo $ALL | grep -o BTC:$PRE | wc -l`
    EXPECTED=`$2 | wc -w`
    if [ "$EXPECTED" != "$NB" ]; then
        echo -n " expected: $EXPECTED txs found $NB"
        return 1
    fi
}