Skip to content

Instantly share code, notes, and snippets.

@mecab
Last active May 9, 2021 04:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mecab/9e47fdbc219546074178c14fb9e2234c to your computer and use it in GitHub Desktop.
Save mecab/9e47fdbc219546074178c14fb9e2234c to your computer and use it in GitHub Desktop.
Chia utility script to copy the finished plot to the destination directory by rsync in background. Remove original plot after verifying the destination file size.
#!/bin/bash
set -e
atexit() {
[[ -n ${temp_file1-} ]] && rm -f "$temp_file1"
[[ -n ${temp_file2-} ]] && rm -f "$temp_file2"
}
trap atexit EXIT
trap 'rc=$?; trap - EXIT; atexit; exit $?' INT PIPE TERM
CHIA_COMMAND=${CHIA_COMMAND:-chia}
VERIFY_COMMAND=${VERIFY_COMMAND:-"du -b"}
# Remove trailing space
tmp_dir="${1%/}"
dst_dir="${2%/}"
echo tmp_dir: $tmp_dir
echo dst_dir: $dst_dir
echo
if uname -r | grep 'microsoft-standard' &>/dev/null; then
tmp_dir_win_compat="$(wslpath -w "$tmp_dir")"
dst_dir_win_compat="$(wslpath -w "$dst_dir")"
echo 'Detected it is running on WSL. Directories are passed to chia in windows path format:'
echo tmp_dir: $tmp_dir_win_compat
echo dst_dir: $dst_dir_win_compat
echo
else
tmp_dir_win_compat="$tmp_dir"
dst_dir_win_compat="$dst_dir"
fi
#######################
# PREREQUISITES CHECK
#######################
if [[ ! -d "$tmp_dir" ]]; then
echo "tmp_dir is not exist. exiting"
echo
echo "Usage: chia-wrapper.sh TMP_DIR DST_DIR [OPTIONS_TO_CHIA]"
exit 100
elif [[ ! -d "$dst_dir" ]]; then
echo "dst_dir is not exist. exiting"
echo
echo "Usage: chia-wrapper.sh TMP_DIR DST_DIR [OPTIONS_TO_CHIA]"
exit 101
fi
echo VERIFY_COMMAND is ${VERIFY_COMMAND}
if ! $VERIFY_COMMAND /dev/null >/dev/null; then
echo "VERIFY_COMMAND /dev/null returned error."
exit 102
fi
if ! type -a rsync &>/dev/null; then
echo "rsync is not found."
exit 103
fi
###############################
# VERIFY_COMMAND SANITY CHECK
###############################
echo VERIFY_COMMAND sanity check...
temp_file1="$(mktemp)"
temp_file2="$(mktemp)"
cat /dev/urandom | head -c 16 > "$temp_file1"
val1=$($VERIFY_COMMAND "$temp_file1" | sed -e 's/ /\t/' | cut -f1)
cp $temp_file1 $temp_file2
val2=$($VERIFY_COMMAND "$temp_file2" | sed -e 's/ /\t/' | cut -f1)
echo Test result between same files:
echo val1: $val1
echo val2: $val2
if [[ "$val1" != "$val2" ]]; then
echo val1 and val2 must be same, but different. Sanity check failed.
exit 104
fi
echo val1 and val2 are same. ok.
cat /dev/urandom | head -c 24 > "$temp_file2"
val2=$($VERIFY_COMMAND "$temp_file2" | sed -e 's/ /\t/' | cut -f1)
echo Test result between different files:
echo val1: $val1
echo val2: $val2
if [[ "$val1" == "$val2" ]]; then
echo val1 and val2 must be different, but same. Sanity check failed.
exit 105
fi
echo val1 and val2 is different. ok.
rm "$temp_file1" "$temp_file2"
echo VERIFY_COMMAND sanity check complete.
echo
# TESTING PURPOSE
# test_str="aaa
# 2021-04-28T09:08:30.632 chia.plotting.create_plots : INFO plot-k32-2021-04-28-00-36-5647308dcd4dabb59db0a260c0589ff6ab7b1bf23265375e136aff91855e8a66.plot
# "
#############################
# THE MAIN STARTS FROM HERE
#############################
exec 3>&1;
# TESTING PURPOSE
# Use the following line to get dummy output faking chia
# filename=$(echo -e "$CHIA_COMMAND plots create -n1 -t \"$tmp_dir\" -d \"$dst_dir\" \"${@:3}\" \n \"$test_str\"" | tee /dev/fd/3 | grep -oP 'INFO.+(plot-.+\.plot)' | sed -E 's/INFO.+(plot-.+\.plot)/\1/')
filename=$($CHIA_COMMAND plots create -n1 -t "$tmp_dir_win_compat" -d "$tmp_dir_win_compat" -x "${@:3}" 2>&1 | tee -a /dev/fd/3 | grep -oP 'INFO.+(plot-.+\.plot)' | sed -E 's/INFO.+(plot-.+\.plot)/\1/')
tmp_plot_path="${tmp_dir}/${filename}"
dst_plot_path="${dst_dir}/${filename}"
echo filename: "$filename"
if [[ -z "$filename" ]]; then
echo "File name cannot be extracted successfully"
exit 1
fi
echo tmp_plot_path: "$tmp_plot_path"
echo dst_plot_path: "$dst_plot_path"
(
# echo rsync -avz --progress "$tmp_plot_path" "$dst_dir" || exit 2
rsync -av --progress "$tmp_plot_path" "$dst_dir" || exit 2
echo
echo "Copy finished: $tmp_plot_path => $dst_dir"
echo
echo "Verifying files..."
verify1=$($VERIFY_COMMAND "$tmp_plot_path" | sed -e 's/ /\t/' | cut -f1) || exit 3
verify2=$($VERIFY_COMMAND "$dst_plot_path" | sed -e 's/ /\t/' | cut -f1) || exit 4
echo verify1: $verify1
echo verify2: $verify2
echo
if [[ $verify1 == $verify2 ]]; then
# echo rm "$tmp_plot_path"
rm "$tmp_plot_path"
echo "Verified. Removed the tmp file."
else
echo "Verification results do not match. Keeping the tmp file."
exit 6
fi
) &
@mecab
Copy link
Author

mecab commented May 3, 2021

Chia Wrapper

Run chia plotter, copy the finished plot from the temporary directory to the destination directory, verify file size, then delete the plot in the temporary directory.

Usage

$ chia-wrapper.sh TMP_DIR DST_DIR [OPTIONS_TO_CHIA]

Example

$ chia-wrapper.sh /mnt/ssd/tmp /mnt/nfs/dst -k32 -r4 -b5000 -u128

Do not specify -n option. It only supports -n 1 and the scripts pass it to chia internally. If you need more than one plot, please iterate the script itself by yourself. For example;

$ while true; do chia-wrapper.sh /mnt/nvme0/plot_tmp0 /mnt/usb1/plot0; done

If you want to use a different command to check the destination file integrity, specify the VERIFY_COMMAND environment variable. Default is du -b -- just check the size of the file. Any command that takes a file name and prints a string representing a file on the first column of space-or-tab delimited program can be used. (In other words, any outputs after space or tab from the command is ignored.)

$  VERIFY_COMMAND=sha1sum chia-wrapper.sh /mnt/nvme0/plot_tmp0 /mnt/usb1/plot0

If you want to specific path of Chia excutable, please specify CHIA_COMMAND which defaults to chia.

$ CHIA_COMMAND=~/local/bin/chia chia-wrapper.sh /mnt/nvme0/plot_tmp0 /mnt/usb1/plot0

Specifying the secondary temp directory is not supported (just because I do not use it). Suggestion by code is appreciated.

WSL Support

The script supports to be running in WSL, kicking native Windows Chia executable. The temp and destination paths are passed to Chia in WIndows path formats so no significant performance difference (i.e., reduced performance by accessing filesystem via WSL layer) during plotting is expected.

Just pass Windows Chia excutable as CHIA_COMMAND. Keep the temp and destination path as how they are seen from Linux.

For example,

CHIA_COMMAND="/mnt/c/Users/{username}/AppData/Local/chia-blockchain/app-{chia-version}/resources/app.asar.unpacked/daemon/chia.exe" chia-wrapper.sh /mnt/k/plot_tmp_test /mnt/k/plot_dst_test

Todo

  • Support second temp directory.
  • Support different copy program other than rsync

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment