-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathwait_chain_sync.sh
More file actions
executable file
·61 lines (57 loc) · 1.75 KB
/
wait_chain_sync.sh
File metadata and controls
executable file
·61 lines (57 loc) · 1.75 KB
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
#!/bin/bash
set -Eeuo pipefail
# Maximum allowed age of the latest block (2 hours in seconds)
MAX_AGE=7200
# Function to get the latest block timestamp
get_latest_block_timestamp() {
local count
count=$(bitcoin-cli getblockcount 2>/dev/null) || {
echo "Error: Failed to get block count. Is bitcoind running?" >&2
return 1
}
local hash
hash=$(bitcoin-cli getblockhash "$count" 2>/dev/null) || {
echo "Error: Failed to get block hash for block $count" >&2
return 1
}
local timestamp
timestamp=$(bitcoin-cli getblock "$hash" 2>/dev/null | grep '"time"' | awk '{print $2}' | sed 's/,//g') || {
echo "Error: Failed to get timestamp for block $hash" >&2
return 1
}
if ! [[ "$timestamp" =~ ^[0-9]+$ ]]; then
echo "Error: Invalid timestamp format: $timestamp" >&2
return 1
fi
echo "$timestamp"
}
# Function to check if the latest block is recent enough
check_block_recency() {
local timestamp
timestamp=$(get_latest_block_timestamp) || return 1
local current_time
current_time=$(date +%s) || {
echo "Error: Failed to get current time" >&2
return 1
}
local time_diff=$((current_time - timestamp))
echo "Latest block timestamp: $timestamp ($(date -d "@$timestamp"))"
echo "Current time: $current_time ($(date))"
echo "Time difference: $time_diff seconds"
if (( time_diff <= MAX_AGE )); then
echo "Latest block is recent enough (less than 2 hours old). Starting miner..."
return 0
else
echo "Latest block is too old ($time_diff seconds). Waiting..."
return 1
fi
}
# Main loop to wait until the block is recent enough
while true; do
if check_block_recency; then
echo "Chain tip is recent enough. Proceeding with mining..."
exit 0
fi
echo "Retrying in 30 seconds..."
sleep 30
done