-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.sh
executable file
·226 lines (181 loc) · 5.12 KB
/
test.sh
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
#!/bin/bash
# Copyright (c) 2017-2018 The Swipp developers
# Distributed under the MIT/X11 software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
SWIPP_BINARY=src/swippd
SWIPP_ARGS="-debug -debugbacktrace -testnet -staking -datadir="
MINER_BINARY=cpuminer-opt/cpuminer
MINER_ARGS=" -a x11 --userpass=%s:%s --url=http://127.0.0.1:%d"
TMP_TEMPLATE=/tmp/swipp.XXXXXXX
STATUS_COMMAND="ps -eo pid,args | grep swippd | grep testnet"
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
MAGENTA=$(tput setaf 5)
RESET=$(tput sgr0)
# Return a randomly generated UUID.
uuid() {
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1
}
# Stall and wait for the process in $1 to exit. Returns instantly if the
# process is not running.
#
# $1 [pid] The process id to wait for.
waitpid() {
tail --pid=$1 -f /dev/null
}
# Return true/false exit value depending on if the process in $1 exists.
#
# $1 [pid] The process id to check for.
pidexists() {
kill -0 $1 &> /dev/null
}
# Start the swipp daemon executable specified in SWIPP_BINARY.
#
# $1 [ip] IP address to bind daemon to.
# $2 [port] Port to bind daemon to.
# $3 [rpcport] Port to run the rpc interface on.
start_swipp_exe() {
dir=$(mktemp -d $TMP_TEMPLATE)
user=$(uuid)
echo $user > $dir/.user
password=$(uuid)
echo $password > $dir/.password
extraargs="-rpcuser=$user -rpcpassword=$password "
extraargs+="-externalip=$1 -bind=$1:$2 -rpcport=$3 "
for i in "${peers[@]}"; do
extraargs+="-addnode=$i "
done
echo $GREEN"Starting swipp instance"$RESET ">" $SWIPP_BINARY $extraargs
$SWIPP_BINARY $SWIPP_ARGS$dir $extraargs &
pid=$!
echo $pid > $dir/.pid
sleep 2
if ! pidexists $pid; then
echo Failed to start swipp instance "$1$dir $extrargs" \
Aborting startup...
stop
exit 1
fi
}
# Create a list of peers in the given interval and put them into $peers.
# Exclude the IP specified in $1. Peers are generated frm 127.0.10.<startindex>
# to 127.0.10.<endindex>.
#
# $1 [ip] IP (peer) to exclude from the final list.
# $2 [startindex] The index to start from when generating peer list.
# $3 [endindex] The index to end at when generating peer list.
get_peers() {
tmp_peers=()
for j in $(seq $2 $3); do
tmp_peers[$j]="127.0.10."$j
done
peers=(${tmp_peers[@]/$1*/})
}
start() {
echo Preparing Swipp instances...
for i in $(seq 1 $1); do
ip="127.0.10."$i
get_peers $ip 1 $1
start_swipp_exe $ip 18065 $((15074 + $i))
done
}
stop() {
echo Stopping all running Swipp instances...
if ls /tmp/swipp.* 1> /dev/null 2>&1; then
for i in /tmp/swipp.*/.pid; do
kill $(<$i) &> /dev/null # We don't care if the kill fails
waitpid $(<$i)
done
fi
echo Deleting data directories...
rm -rf /tmp/swipp.*
}
command() {
get_node_parameters $1
$SWIPP_BINARY -rpcuser=$node_user -rpcpassword=$node_password\
-rpcport=$node_rpcport "${@:2}"
}
status() {
running_nodes=$(eval $STATUS_COMMAND)
SAVEIFS=$IFS; IFS=$'\n'; running_nodes=($running_nodes); IFS=$SAVEIFS
i=0;
if [ "$running_nodes" != "" ]; then
for (( ; i < ${#running_nodes[@]}; i++ )); do
privkey=$(command $i dumpprivkey $(command $i getaddressesbyaccount "" | grep -Po "[a-zA-Z0-9]+"))
echo $GREEN[$i, $privkey]$RESET: ${running_nodes[$i]}
done
fi
echo $MAGENTA"There are $i nodes up and running."$RESET
}
get_node_parameters() {
running_nodes=$(eval $STATUS_COMMAND)
SAVEIFS=$IFS; IFS=$'\n'; running_nodes=($running_nodes); IFS=$SAVEIFS
re="-datadir=([^ ]+) -rpcuser=([^ ]+) -rpcpassword=([^ ]+) [^ ]+ [^ ]+ -rpcport=([^ ]+)"
if [[ ${running_nodes[$1]} =~ $re ]]; then
node_datadir=${BASH_REMATCH[1]}
node_user=${BASH_REMATCH[2]}
node_password=${BASH_REMATCH[3]}
node_rpcport=${BASH_REMATCH[4]}
else
echo $RED"The" specified Swipp node is not running and could \
not be found. Exiting...$RESET
exit 1
fi
}
mine() {
echo Initiating mining on Swipp node instance $1. Please hit CTRL-C to stop...
get_node_parameters $1
$MINER_BINARY $(printf "$MINER_ARGS" $node_user $node_password $node_rpcport)
}
case $1 in
start)
if [ "$#" -lt 2 ]; then
echo $RED"Syntax" for starting swipp test instances is \
"\"start <n>\"", where n denotes the number of nodes. \
Exiting...$RESET
exit 1
fi
if [ "$(eval $STATUS_COMMAND)" != "" ]; then
echo $RED"Already started."$RESET
exit 1
fi
start $2
;;
stop)
stop
;;
command)
if [ "$#" -lt 3 ]; then
echo $RED"Syntax" for sending a command to a Swipp node \
is incorrect. The correct syntax is \
"\"command <n> <command> <args>\"", where n denotes \
the node number as shown by "\"status\"".$RESET
exit 1
fi
command $2 "${@:3}"
;;
status)
status
;;
mine)
if [ "$#" -lt 2 ]; then
echo $RED"Syntax" for initiating a mining session is \
"\"mine <n>\"", where n denotes the node number as shown \
by "\"status\"".$RESET
exit 1
fi
if [ ! -f $MINER_BINARY ]; then
echo $RED"Miner" binary not found. Please compile the miner \
before executing this operation.
exit 1
fi
mine $2
;;
# unknown option
*)
echo Please specify one of the following: start/stop/run. \
Exiting...
exit 1
;;
esac
exit 0