-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_hydra
executable file
·190 lines (166 loc) · 5.95 KB
/
run_hydra
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
#!/usr/bin/env bash
set -euo pipefail
HYDRA_SCRIPTS_TX_ID=74b587d08d12aa679bdfeb3eaa57d698e426045dd529d4130d7d8ca0c18d54b0
PATH=$PATH:/srv/cardano/:/srv/hydra/
secrets_dir=/srv/var/cardano/secrets
peers_config_dir=/srv/etc/hydra/peers
cardano_verification_key_file="${secrets_dir}/payment.vkey"
cardano_signing_key_file="${secrets_dir}/payment.skey"
cardano_address_file="${secrets_dir}/payment.addr"
hydra_key_file_prefix="${secrets_dir}/hydra"
hydra_verification_key_file="${hydra_key_file_prefix}.vk"
hydra_signing_key_file="${hydra_key_file_prefix}.sk"
main() {
display_cardano_identity
display_hydra_verification_key
start_cardano
wait_for_era Babbage
wait_for_hydra_scripts_to_be_published
start_hydra
wait -n # exit when any child exists. bash version >= 4.3
}
display_cardano_identity() {
test -e "${cardano_verification_key_file}" || gen_cardano_keys
test -e "${cardano_address_file}" || gen_cardano_address
echo "# my Cardano versification key" >&2
echo "cat << EOF >my-cardano-key.vk" >&2
cat ${cardano_verification_key_file} >&2
echo "EOF" >&2
echo >&2
echo "# my Cardano address" >&2
echo "cat << EOF >my-cardano.addr" >&2
cat ${cardano_address_file} >&2
echo >&2
echo "EOF" >&2
echo >&2
}
gen_cardano_keys() {
echo "# no Cardano key found, generating them" >&2
cardano-cli address key-gen \
--verification-key-file ${cardano_verification_key_file} \
--signing-key-file ${cardano_signing_key_file}
}
gen_cardano_address() {
echo "# no Cardano address found, generating it" >&2
cardano-cli address build \
--payment-verification-key-file ${cardano_verification_key_file} \
--out-file ${cardano_address_file} \
--testnet-magic 2
}
display_hydra_verification_key() {
test -e "${hydra_verification_key_file}" || gen_hydra_keys
echo "# my Hydra verification key" >&2
echo "cat << EOF > my-hydra-key.vk" >&2
cat ${hydra_verification_key_file} >&2
echo >&2
}
gen_hydra_keys() {
echo "# no Hydra key found, generating it" >&2
hydra-tools gen-hydra-key --output-file "${hydra_key_file_prefix}"
}
start_cardano() {
(
cardano-node run \
--topology /srv/etc/cardano/cardano-node/topology.json \
--database-path /srv/var/cardano/db \
--socket-path /srv/var/cardano/node.socket \
--host-addr 0.0.0.0 \
--port 3001 \
--config /srv/etc/cardano/cardano-node/config.json
echo cardano-node stopped with code $? >&2
) &
wait_for_cardano_to_start
}
wait_for_cardano_to_start() {
wait_for_cardano_socket() {
while ! test -e /srv/var/cardano/node.socket
do
sleep 1
done
}
export -f wait_for_cardano_socket
if ! timeout 600 bash -c wait_for_cardano_socket
then
usage "timeout while waiting for cardano-node to start"
fi
}
wait_for_era() {
local era="$1"
while [ "$era" != "$(/srv/cardano/cardano-cli query tip --testnet-magic 2 | jq -r .era)" ]
do
echo "Not yet in era ${era}..." >&2
sleep 5
done
}
wait_for_hydra_scripts_to_be_published() {
local tx_id="$HYDRA_SCRIPTS_TX_ID"
while ! tx_published "$tx_id"
do
echo "Hydra scripts not yet published ${tx_id}..." >&2
sleep 10
done
}
tx_published() {
local tx_id="$1"
/srv/cardano/cardano-cli query utxo --testnet-magic 2 --tx-in "$tx_id#1" | tail -n1 | grep "$tx_id"
}
start_hydra() {
check_peers_configuration
(
hydra-node \
--node-id 12043 \
--api-host 0.0.0.0 \
--host 0.0.0.0 \
--hydra-scripts-tx-id "${HYDRA_SCRIPTS_TX_ID}" \
--hydra-signing-key "${hydra_signing_key_file}" \
--cardano-signing-key "${cardano_signing_key_file}" \
--ledger-genesis /srv/etc/cardano/genesis/shelley.json \
--ledger-protocol-parameters /srv/etc/hydra/protocol-parameters.json \
--network-id 2 \
--node-socket /srv/var/cardano/node.socket \
--persistence-dir /srv/var/hydra/db \
$(hydra_peers_parameters)
echo hydra-node stopped with code $? >&2
) &
}
check_peers_configuration() {
for peer_dir in $(peer_config_dirs)
do
test -e ${peer_dir}/ip || peers_usage "no ip file for peer ${peer_dir}"
test -e ${peer_dir}/hydra.vk || peers_usage "no hydra.vk file for peer ${peer_dir}"
test -e ${peer_dir}/cardano.vk || peers_usage "no cardano.vk file for peer ${peer_dir}"
done
}
hydra_peers_parameters() {
for peer_dir in $(peer_config_dirs)
do
test -e ${peer_dir}/ip || peers_usage "no ip file for peer ${peer_dir}"
test -e ${peer_dir}/hydra.vk || peers_usage "no hydra.vk file for peer ${peer_dir}"
test -e ${peer_dir}/cardano.vk || peers_usage "no cardano.vk file for peer ${peer_dir}"
echo --peer "$(cat ${peer_dir}/ip)"
echo --hydra-verification-key "${peer_dir}/hydra.vk"
echo --cardano-verification-key "${peer_dir}/cardano.vk"
done
}
peer_config_dirs() {
find ${peers_config_dir} -mindepth 1 -type d
}
peers_usage() {
echo "Malformed peers configuration" >&2
echo >&2
echo "Peer configuration should be available in /srv/etc/hydra/peers/" >&2
echo "Create one directory per peer, named after the peers name" >&2
echo "Do not store anything else in this directory" >&2
echo >&2
echo "In a peer directory, store the followin three files:" >&2
echo " * ip - contain ip:port address of the peer" >&2
echo " * cardano.vk - contains the cardano verification key of the peer" >&2
echo " * hydra.vk - contains the hydra verification key of the peer" >&2
echo >&2
usage "$@"
}
usage() {
echo "$@" >&2
exit 1
}
main