Skip to content

Commit

Permalink
Migrate last_vrf_output
Browse files Browse the repository at this point in the history
  • Loading branch information
It's me, CI committed Oct 31, 2023
1 parent f7c4c3c commit 142d260
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 34 deletions.
54 changes: 54 additions & 0 deletions scripts/migrate-itn-data.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash

if [ $# -lt 1 ] || [ $# -gt 3 ]; then
echo "Usage" $0 archive-db [data_file] [update_script]
echo "'data_file' and 'update_script' are created when running this script"
exit 0
fi

ARCHIVE_DB=$1
DATA_FILE=${2:-data_file.tmp}
UPDATE_SCRIPT=${3:-data_update.sql}

echo "Migrating receipt chain hashes in account preconditions in archive db '"$ARCHIVE_DB"'"

rm -f $DATA_FILE
rm -f $UPDATE_SCRIPT

echo "Creating temporary file with receipt chain hashes" "'"$DATA_FILE"'"
echo "select id,receipt_chain_hash from zkapp_account_precondition where receipt_chain_hash is not null;" | \
psql --csv -t -q $ARCHIVE_DB > $DATA_FILE

echo "Creating SQL script" "'"$UPDATE_SCRIPT"'"
for line in `cat $DATA_FILE`
do (
ID=$(echo $line | awk -F , '{print $1}');
FP=$(echo $line | awk -F , '{print $2}');
B58=$(echo $FP | _build/default/src/app/receipt_chain_hash_to_b58/receipt_chain_hash_to_b58.exe);
echo -n .
echo $ID "'"$B58"'" | awk '{print "UPDATE zkapp_account_precondition SET receipt_chain_hash=" $2 " WHERE id=" $1 ";"}' >> $UPDATE_SCRIPT)
done

echo
echo "Receipt chain hash pass done!"

rm -f $DATA_FILE

echo "Creating temporary file with last_vrf_ouput" "'"$DATA_FILE"'"
echo "select id,last_vrf_output from blocks;" | \
psql --csv -t -q $ARCHIVE_DB > $DATA_FILE

echo "Adding to SQL script" "'"$UPDATE_SCRIPT"'"
for line in `cat $DATA_FILE`
do (
ID=$(echo $line | awk -F , '{print $1}');
FP=$(echo $line | awk -F , '{print $2}');
B64=$(echo $FP | _build/default/src/app/last_vrf_output_to_b64/last_vrf_output_to_b64.exe);
echo -n .
echo $ID "'"$B64"'" | awk '{print "UPDATE blocks SET last_vrf_output=" $2 " WHERE id=" $1 ";"}' >> $UPDATE_SCRIPT)
done

echo
echo "Last VRF output pass done!"

echo "Now run:" "psql -d" $ARCHIVE_DB "<" $UPDATE_SCRIPT
34 changes: 0 additions & 34 deletions scripts/migrate-receipt-chain-hashes.sh

This file was deleted.

14 changes: 14 additions & 0 deletions src/app/last_vrf_output_to_b64/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(executable
(package last_vrf_output_to_b64)
(name last_vrf_output_to_b64)
(public_name last_vrf_output_to_b64)
(libraries
;; opam libraries
base64
core_kernel
hex
;; local libraries
)
(preprocessor_deps ../../config.mlh)
(instrumentation (backend bisect_ppx))
(preprocess (pps ppx_mina ppx_version)))
23 changes: 23 additions & 0 deletions src/app/last_vrf_output_to_b64/last_vrf_output_to_b64.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
open Core_kernel

let () =
let s = Stdlib.read_line () in
let b64_check () =
match Base64.decode ~alphabet:Base64.uri_safe_alphabet s with
| Ok _ ->
(* already base64 *)
s
| Error _ ->
failwith "Bad Base64 encoding"
in
let b64 =
(* try unhexing first, because hex chars are also base64 chars *)
try
match Hex.Safe.of_hex s with
| Some unhexed ->
Base64.encode_exn ~alphabet:Base64.uri_safe_alphabet unhexed
| None ->
b64_check ()
with _ -> b64_check ()
in
Format.printf "%s@." b64
1 change: 1 addition & 0 deletions src/dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
(package (name kimchi_backend))
(package (name kimchi_bindings))
(package (name kimchi_types))
(package (name last_vrf_output_to_b64))
(package (name ledger_catchup))
(package (name ledger_proof))
(package (name libp2p_ipc))
Expand Down

0 comments on commit 142d260

Please sign in to comment.