1
+ #! /bin/bash
2
+ # Copyright 2020 DSR Corporation
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ set -euo pipefail
17
+
18
+ # common constants
19
+ node_p2p_port=26670
20
+ node_client_port=26671
21
+ chain_id=" dclchain"
22
+ node0conn=" tcp://192.167.10.2:26657"
23
+ docker_network=" distributed-compliance-ledger_localnet"
24
+ passphrase=" test1234"
25
+ LOCALNET_DIR=" .localnet"
26
+
27
+ # RED=`tput setaf 1`
28
+ # GREEN=`tput setaf 2`
29
+ # RESET=`tput sgr0`
30
+ GREEN=" "
31
+ RED=" "
32
+ RESET=" "
33
+
34
+ random_string () {
35
+ local __resultvar=" $1 "
36
+ local length=${2:- 6} # Default is 6
37
+ # Newer mac might have shasum instead of sha1sum
38
+ if command -v shasum & > /dev/null
39
+ then
40
+ eval $__resultvar =" '$( date +%s.%N | shasum | fold -w ${length} | head -n 1) '"
41
+ else
42
+ eval $__resultvar =" '$( date +%s.%N | sha1sum | fold -w ${length} | head -n 1) '"
43
+ fi
44
+ }
45
+
46
+ DEF_OUTPUT_MODE=json
47
+
48
+
49
+ # json: pretty (indented) json
50
+ # raw or otherwise: raw
51
+ _check_response () {
52
+ local _result=" $1 "
53
+ local _expected_string=" $2 "
54
+ local _mode=" ${3:- $DEF_OUTPUT_MODE } "
55
+
56
+ if [[ " $_mode " == " json" ]]; then
57
+ if [[ -n " $( echo " $_result " | jq | grep " $_expected_string " 2> /dev/null) " ]]; then
58
+ echo true
59
+ return
60
+ fi
61
+ else
62
+ if [[ -n " $( echo " $_result " | grep " $_expected_string " 2> /dev/null) " ]]; then
63
+ echo true
64
+ return
65
+ fi
66
+ fi
67
+
68
+ echo false
69
+ }
70
+
71
+ check_response () {
72
+ local _result=" $1 "
73
+ local _expected_string=" $2 "
74
+ local _mode=" ${3:- $DEF_OUTPUT_MODE } "
75
+
76
+ if [[ " $( _check_response " $_result " " $_expected_string " " $_mode " ) " != true ]]; then
77
+ echo " ${GREEN} ERROR:${RESET} command failed. The expected string: '$_expected_string ' not found in the result: $_result "
78
+ exit 1
79
+ fi
80
+ }
81
+
82
+ check_response_and_report () {
83
+ local _result=" $1 "
84
+ local _expected_string=" $2 "
85
+ local _mode=" ${3:- $DEF_OUTPUT_MODE } "
86
+
87
+ check_response " $_result " " $_expected_string " " $_mode "
88
+ echo " ${GREEN} SUCCESS: ${RESET} Result contains expected substring: '$_expected_string '"
89
+ }
90
+
91
+ response_does_not_contain () {
92
+ local _result=" $1 "
93
+ local _unexpected_string=" $2 "
94
+ local _mode=" ${3:- $DEF_OUTPUT_MODE } "
95
+
96
+ if [[ " $( _check_response " $_result " " $_unexpected_string " " $_mode " ) " == true ]]; then
97
+ echo " ERROR: command failed. The unexpected string: '$_unexpected_string ' found in the result: $_result "
98
+ exit 1
99
+ fi
100
+
101
+ echo " ${GREEN} SUCCESS: ${RESET} Result does not contain unexpected substring: '$_unexpected_string '"
102
+ }
103
+
104
+ create_new_account (){
105
+ local __resultvar=" $1 "
106
+ random_string name
107
+ eval $__resultvar =" '$name '"
108
+
109
+ local roles=" $2 "
110
+
111
+ echo " Account name: $name "
112
+
113
+ echo " Generate key for $name "
114
+ (echo $passphrase ; echo $passphrase ) | dcld keys add " $name "
115
+
116
+ address=$( echo $passphrase | dcld keys show $name -a)
117
+ pubkey=$( echo $passphrase | dcld keys show $name -p)
118
+
119
+ echo " Jack proposes account for \" $name \" with roles: \" $roles \" "
120
+ result=$( echo $passphrase | dcld tx auth propose-add-account --address=" $address " --pubkey=" $pubkey " --roles=$roles --from jack --yes)
121
+ check_response " $result " " \" code\" : 0"
122
+ echo " $result "
123
+
124
+ echo " Alice approves account for \" $name \" with roles: \" $roles \" "
125
+ result=$( echo $passphrase | dcld tx auth approve-add-account --address=" $address " --from alice --yes)
126
+ check_response " $result " " \" code\" : 0"
127
+ echo " $result "
128
+ }
129
+
130
+ create_new_vendor_account (){
131
+
132
+ local _name=" $1 "
133
+ local _vid=" $2 "
134
+
135
+ echo $passphrase | dcld keys add " $_name "
136
+ _address=$( echo $passphrase | dcld keys show $_name -a)
137
+ _pubkey=$( echo $passphrase | dcld keys show $_name -p)
138
+
139
+ local _result=" "
140
+ if [ $# -eq 3 ]; then
141
+ local _pid_ranges=" $3 "
142
+ echo " Jack proposes account for \" $_name \" with Vendor role and with [$_pid_ranges ] associated Product IDs"
143
+ _result=$( echo $passphrase | dcld tx auth propose-add-account --address=" $_address " --pubkey=" $_pubkey " --roles=Vendor --vid=$_vid --pid_ranges=$_pid_ranges --from jack --yes)
144
+ else
145
+ echo " Jack proposes account for \" $_name \" with Vendor role"
146
+ _result=$( echo $passphrase | dcld tx auth propose-add-account --address=" $_address " --pubkey=" $_pubkey " --roles=Vendor --vid=$_vid --from jack --yes)
147
+ fi
148
+ }
149
+
150
+
151
+ test_divider () {
152
+ echo " "
153
+ echo " --------------------------"
154
+ echo " "
155
+ }
156
+
157
+ get_height () {
158
+ local __resultvar=" $1 "
159
+ eval $__resultvar =" '$( dcld status | jq | grep latest_block_height | awk -F' "' ' {print $4}' ) '"
160
+ }
161
+
162
+ wait_for_height () {
163
+ local target_height=" ${1:- 1} " # Default is 1
164
+ local wait_time=" ${2:- 10} " # In seconds, default - 10
165
+ local mode=" ${3:- normal} " # normal or outage-safe
166
+ local node=" ${4:- " " } "
167
+
168
+ local _output=${DETAILED_OUTPUT_TARGET:-/ dev/ stdout}
169
+
170
+ local waited=0
171
+ local wait_interval=1
172
+
173
+ if [[ -n " $node " ]]; then
174
+ node=" --node $node "
175
+ fi
176
+
177
+ while true ; do
178
+ sleep " ${wait_interval} "
179
+ waited=$(( waited + wait_interval))
180
+
181
+ if [[ " $mode " == " outage-safe" ]]; then
182
+ current_height=" $( dcld status $node 2> /dev/null | jq | grep latest_block_height | awk -F' "' ' {print $4}' ) " || true
183
+ else
184
+ current_height=" $( dcld status $node | jq | grep latest_block_height | awk -F' "' ' {print $4}' ) "
185
+
186
+ if [[ -z " $current_height " ]]; then
187
+ echo " No height found in status"
188
+ exit 1
189
+ fi
190
+ fi
191
+
192
+ if [[ -n " $current_height " ]] && (( current_height >= target_height)) ; then
193
+ echo " Height $target_height is reached in $waited seconds" & > ${_output}
194
+ break
195
+ fi
196
+
197
+ if (( waited > wait_time)) ; then
198
+ echo " Height $target_height is not reached in $wait_time seconds"
199
+ exit 1
200
+ fi
201
+
202
+ echo " Waiting for height: $target_height ... Current height: ${current_height:- unavailable} , " \
203
+ " wait time: $waited , time limit: $wait_time ." & > ${_output}
204
+ done
205
+ }
206
+
207
+ get_txn_result () {
208
+ local _broadcast_result=${1}
209
+ local _txHash=$( echo " $_broadcast_result " | jq -r ' .txhash' )
210
+ local _command=" dcld query tx $_txHash "
211
+ local _result=$( $_command 2>&1 )
212
+
213
+ for i in {1..20}; do
214
+ if [[ " $( _check_response " $_result " " not found" " raw" ) " == true ]]; then
215
+ sleep 2
216
+ _result=$( $_command 2>&1 )
217
+ else
218
+ break
219
+ fi
220
+ done
221
+
222
+ echo " $_result "
223
+ }
224
+
225
+ execute_with_retry () {
226
+ local _command=${1}
227
+ local _error=${2:- " EOF" }
228
+ local _result=$( $_command )
229
+
230
+ for i in {1..20}; do
231
+ if [[ " $( _check_response " $_result " $_error " raw" ) " == true ]]; then
232
+ # echo "EOF detected, re-trying"
233
+ sleep 2
234
+ _result=$( $_command )
235
+ else
236
+ break
237
+ fi
238
+ done
239
+
240
+ echo " $_result "
241
+ }
0 commit comments