aboutsummaryrefslogtreecommitdiff
path: root/qa/rpc-tests/util.sh
blob: 9001c42fbc5878a06358d69b2f4bc1ab22f28af1 (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
#!/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 {
  declare -i EXPECT="$2"
  B=$( $CLI $1 getbalance $3 $4 )
  if (( $( echo "$B == $EXPECT" | 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 {
    $CLI $1 getblockcount
}