Skip to content

Commit dd7cb8f

Browse files
committed
Adapt fake aggregator import script to join artifacts files
1 parent b9e7813 commit dd7cb8f

File tree

1 file changed

+58
-21
lines changed
  • mithril-test-lab/mithril-aggregator-fake/scripts

1 file changed

+58
-21
lines changed

mithril-test-lab/mithril-aggregator-fake/scripts/import.sh

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ error() {
3131

3232
clean_directory() {
3333
echo "Cleaning data directory…"
34-
rm "$DATA_DIR/*.json" || true
34+
rm "$DATA_DIR/"*.json || true
3535
}
3636

37-
# $1=URL $2=artifact_name
37+
# $1=URL $2=artifact_name $3=target_dir (default to $DATA_DIR)
3838
download_data() {
3939
local -r url=${1:?"No URL given to download_data function."};
4040
local -r artifact=${2:?"No artifact type given to download_data function."};
41+
local -r target_dir=${3:-$DATA_DIR}
4142

42-
echo "Downloading ${artifact} data."
43-
wget -O - --quiet "${url}" | jq > "$DATA_DIR/${artifact}.json";
43+
wget -O - --quiet "${url}" | jq > "$target_dir/${artifact}.json";
4444
}
4545

4646
# $1=URL $2=artifact_name $3=JSON field
@@ -50,17 +50,20 @@ download_artifacts() {
5050
local -r json_field=${3:?"No JSON field given to read from artifact list."};
5151
local -r download_missing_certificate=${4:-false};
5252
local -i nb=0
53+
local -r artifact_dir="$DATA_DIR/${artifact}"
54+
55+
mkdir -p "$artifact_dir"
5356

5457
echo -n "Downloading ${artifact} data: "
5558
tput sc;
5659

57-
for field in $(jq -r .[].${json_field} < $DATA_DIR/${artifact}s.json);
60+
for field in $(jq -r .[].${json_field} < $DATA_DIR/${artifact}s-list.json);
5861
do
5962
tput rc;
60-
download_data "${url}/${field}" "${artifact}-${field}"
63+
download_data "${url}/${field}" "${field}" "$artifact_dir"
6164

6265
if [ true = "$download_missing_certificate" ]; then
63-
download_missing_certificate $(jq -r .certificate_hash $DATA_DIR/${artifact}-${field}.json);
66+
download_missing_certificate $(jq -r ".certificate_hash" "$DATA_DIR/${artifact}/${field}.json");
6467
fi
6568

6669
let "nb=nb+1"
@@ -74,13 +77,13 @@ download_artifacts() {
7477
download_missing_certificate() {
7578
local -r certificate_hash=${1:?"No certificate hashes given to download_missing_certificate function."};
7679

77-
if [ ! -e "$DATA_DIR/certificate-${certificate_hash}.json" ]; then
78-
download_data "${BASE_URL}/certificate/${certificate_hash}" "certificate-${certificate_hash}"
80+
if [ ! -e "$DATA_DIR/certificate/${certificate_hash}.json" ]; then
81+
download_data "${BASE_URL}/certificate/${certificate_hash}" "${certificate_hash}" "$DATA_DIR/certificate"
7982
fi
8083
}
8184

8285
download_certificate_chain() {
83-
local parent_hash=$(jq -r .[0].hash $DATA_DIR/certificates.json);
86+
local parent_hash=$(jq -r .[0].hash $DATA_DIR/certificates-list.json);
8487
local certificate_hash;
8588
local -i nb=0
8689

@@ -90,9 +93,10 @@ download_certificate_chain() {
9093
until [ -z "$parent_hash" ];
9194
do
9295
tput rc;
96+
9397
certificate_hash=$parent_hash;
94-
download_data "${BASE_URL}/certificate/${certificate_hash}" "certificate-${certificate_hash}"
95-
parent_hash=$(jq -r .previous_hash $DATA_DIR/certificate-${certificate_hash}.json);
98+
download_data "${BASE_URL}/certificate/${certificate_hash}" "${certificate_hash}" "$DATA_DIR/certificate"
99+
parent_hash=$(jq -r .previous_hash "$DATA_DIR/certificate/${certificate_hash}.json");
96100
let "nb=nb+1"
97101
echo -n "$nb "
98102
done
@@ -103,15 +107,18 @@ download_certificate_chain() {
103107
download_ctx_proof() {
104108
local -r ctx_hashes=${@:?"No cardano transaction hashes given to download_ctx_proof function."};
105109
local -i nb=0
110+
local -r artifact_dir="$DATA_DIR/ctx-proof"
111+
112+
mkdir -p "$artifact_dir"
106113

107114
echo -n "Downloading cardano transaction proof: "
108115
tput sc;
109116

110117
for cardano_transaction_hash in $ctx_hashes;
111118
do
112119
tput rc;
113-
download_data "${BASE_URL}/proof/cardano-transaction?transaction_hashes=${cardano_transaction_hash}" "ctx-proof-${cardano_transaction_hash}"
114-
download_missing_certificate $(jq -r .certificate_hash $DATA_DIR/ctx-proof-${cardano_transaction_hash}.json);
120+
download_data "${BASE_URL}/proof/cardano-transaction?transaction_hashes=${cardano_transaction_hash}" "${cardano_transaction_hash}" "${artifact_dir}"
121+
download_missing_certificate $(jq -r ".certificate_hash" "$DATA_DIR/ctx-proof/${cardano_transaction_hash}.json");
115122

116123
let "nb=nb+1"
117124
echo -n "$nb "
@@ -127,13 +134,13 @@ write_ctx_proof_hashes_list() {
127134
echo -n "Writting cardano transaction proof ids to a file: "
128135
tput sc;
129136

130-
echo "[" > $DATA_DIR/ctx-proofs.json
137+
echo "[" > $DATA_DIR/ctx-proofs-list.json
131138

132139
local separator=" "
133140
for cardano_transaction_hash in $ctx_hashes;
134141
do
135142
tput rc;
136-
cat >> $DATA_DIR/ctx-proofs.json <<EOF
143+
cat >> $DATA_DIR/ctx-proofs-list.json <<EOF
137144
$separator { "transaction_hash": "$cardano_transaction_hash" }
138145
EOF
139146

@@ -142,11 +149,35 @@ EOF
142149
let "nb=nb+1"
143150
echo -n "$nb "
144151
done
145-
echo "]" >> $DATA_DIR/ctx-proofs.json
152+
echo "]" >> $DATA_DIR/ctx-proofs-list.json
146153

147154
echo "";
148155
}
149156

157+
# Join downloaded artifacts files into a single files
158+
# $1=name of the artifacts to join
159+
join_artifacts_files() {
160+
local -r name=${1:?"No artifact name given to join_artifacts_files function."};
161+
local -r src="${DATA_DIR:?}/${name}"
162+
local -r dest="$DATA_DIR"/"$name"s.json
163+
local buffer=""
164+
local separator=""
165+
166+
echo "Joining ${name} artifacts into ${dest}"
167+
168+
buffer+="{"
169+
for filename in "$src/"*; do
170+
local key=$(basename "${filename%.*}")
171+
172+
buffer+=$(echo "$separator\"$key\": " | cat - "$filename")
173+
separator=","
174+
done
175+
buffer+="}"
176+
177+
echo ${buffer} | jq > "$dest"
178+
rm -rf "$src"
179+
}
180+
150181
# MAIN execution
151182

152183
if [ -z "${1-""}" ]; then display_help "No data directory given to download JSON files."; fi;
@@ -172,22 +203,28 @@ export DATA_DIR URL;
172203
check_requirements;
173204
clean_directory;
174205

206+
echo "Downloading epoch-settings"
175207
download_data "$BASE_URL/epoch-settings" "epoch-settings"
176208

177-
download_data "$BASE_URL/certificates" "certificates"
209+
download_data "$BASE_URL/certificates" "certificates-list"
178210
download_artifacts "$BASE_URL/certificate" "certificate" "hash"
179211
download_certificate_chain
180212

181-
download_data "$BASE_URL/artifact/snapshots" "snapshots"
213+
download_data "$BASE_URL/artifact/snapshots" "snapshots-list"
182214
download_artifacts "$BASE_URL/artifact/snapshot" "snapshot" "digest" true
183215

184-
download_data "$BASE_URL/artifact/mithril-stake-distributions" "mithril-stake-distributions"
216+
download_data "$BASE_URL/artifact/mithril-stake-distributions" "mithril-stake-distributions-list"
185217
download_artifacts "$BASE_URL/artifact/mithril-stake-distribution" "mithril-stake-distribution" "hash" true
186218

187-
download_data "$BASE_URL/artifact/cardano-transactions" "ctx-snapshots"
219+
download_data "$BASE_URL/artifact/cardano-transactions" "ctx-snapshots-list"
188220
download_artifacts "$BASE_URL/artifact/cardano-transaction" "ctx-snapshot" "hash"
189221

190222
if [ -n "$CARDANO_TRANSACTIONS_HASHES" ]; then
191223
download_ctx_proof $CARDANO_TRANSACTIONS_HASHES
192224
write_ctx_proof_hashes_list $CARDANO_TRANSACTIONS_HASHES
225+
join_artifacts_files "ctx-proof"
193226
fi
227+
228+
for artifact_type in "snapshot" "mithril-stake-distribution" "ctx-snapshot" "certificate"; do
229+
join_artifacts_files "$artifact_type"
230+
done

0 commit comments

Comments
 (0)