Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix autosnapshot when the last slot in an epoch doesn't contain a block #66

Merged
merged 2 commits into from
Nov 20, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions tip-payment/scripts/autosnapshot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ create_snapshot_for_slot() {
# for some reason solana-ledger-tool error doesn't cause this script to exit out, so check status here
exit_status=$?
if [ $exit_status -ne 0 ]; then
echo "solana-ledger-tool returned $exit_status"
exit $exit_status
echo "solana-ledger-tool returned $exit_status"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this check not get executed due to set -eux on line 9?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eg

#!/bin/bash

set -exu
function foo {
  rm aldskjdjkldlkjadskjldkjlskjlkjdlslkjda
  echo $?
}

foo

echo 'finished'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

➜  ~ bash demo.sh
+ foo
+ rm aldskjdjkldlkjadskjldkjlskjlkjdlslkjda
rm: cannot remove 'aldskjdjkldlkjadskjldkjlskjlkjdlslkjda': No such file or directory
➜  ~ echo $?
1

the echo finished never runs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah im not totally sure why it doesn't work, there's a comment above it.

exit $exit_status
fi

# snapshot file should exist now, grab the filename here (snapshot-$slot-$hash.tar.zst)
Expand Down Expand Up @@ -219,6 +219,23 @@ upload_merkle_roots() {
--tip-distribution-program-id "$tip_distribution_program_id"
}

find_previous_epoch_last_slot() {
local slot_with_block=$1
local rpc_url=$2

block_result=$(curl -s "$rpc_url" -X POST -H "Content-Type: application/json" -d "
{\"jsonrpc\":\"2.0\",\"id\":1, \"method\":\"getBlock\", \"params\": [$slot_with_block, {\"transactionDetails\": \"none\"}]}
" | jq .result)

while [[ $block_result = null ]]; do
slot_with_block="$((slot_with_block - 1))"
block_result=$(curl -s "$rpc_url" -X POST -H "Content-Type: application/json" -d "
{\"jsonrpc\":\"2.0\",\"id\":1, \"method\":\"getBlock\", \"params\": [$slot_with_block, {\"transactionDetails\": \"none\"}]}
" | jq .result)
done
echo "$slot_with_block"
}

main() {
local epoch_info
local previous_epoch_final_slot
Expand Down Expand Up @@ -249,13 +266,16 @@ main() {
epoch_start_slot=$((current_absolute_slot - current_slot_index))
previous_epoch_final_slot="$((epoch_start_slot - 1))"

# The last slot in the epoch might not have a block, so search backwards until the last block is found.
previous_epoch_final_slot=$(find_previous_epoch_last_slot "$previous_epoch_final_slot" "$RPC_URL")

echo "epoch_info: $epoch_info"
echo "previous_epoch_final_slot: $previous_epoch_final_slot"

FILE="$SNAPSHOT_DIR/$last_epoch.done"
if [ -f "$FILE" ]; then
echo "epoch $last_epoch finished uploading, exiting"
exit 0
echo "epoch $last_epoch finished uploading, exiting"
exit 0
fi

# ---------------------------------------------------------------------------
Expand Down