node_sync_status.sh

Overview

node_sync_status.sh is a Bash script designed to analyze a log file generated by a node process and determine how far behind the node is from the current real-world time based on the timestamp of the last finalized block. The script reads the log file, extracts the timestamp of the most recent finalized block, calculates the difference between this timestamp and the current system time, and outputs the node's synchronization status in a human-readable format.

This utility is particularly useful for monitoring the synchronization progress of nodes in distributed ledger or blockchain systems, where timely block finalization is critical.


Detailed Explanation

Script Behavior


Variables

Variable Name

Description

LOG_FILE

The first argument passed to the script, expected to be a log file path.

NODE_BLOCK_TIME

Extracted Unix timestamp (seconds since epoch) of the last finalized block from the log file.

CURRENT_TIME

Current system time as Unix timestamp.

TIME_DIFF_SECOND

The difference between current time and block time in seconds.

TIME_DIFF

The difference converted to hours (integer).


Step-by-step Breakdown

  1. Log file validation

    if [ ! -e $LOG_FILE ]; then
      echo Log file $LOG_FILE not found.
      exit 1
    fi
    
    • Checks if the supplied log file exists.

    • If not, outputs an error message and exits with status code 1.

  2. Extracting last finalized block time

    NODE_BLOCK_TIME=$(grep -Po "Last finalized block data: seq_no: ([0-9]*).*time: \K[0-9]{10}" $1 | tail -n 1)
    
    • Uses grep with Perl-compatible regex options:

      • -P enables Perl regex.

      • -o outputs only the matched part.

    • The regex looks for the line containing:

      • "Last finalized block data: seq_no: <number> ... time: <10-digit timestamp>"

    • Extracts only the 10-digit timestamp.

    • Uses tail -n 1 to get the last occurrence (most recent block).

  3. Get current time and compute difference

    CURRENT_TIME=$(date +%s)
    TIME_DIFF_SECOND=$(( CURRENT_TIME-NODE_BLOCK_TIME ))
    TIME_DIFF=$(echo "$TIME_DIFF_SECOND / 3600" | bc)
    
    • CURRENT_TIME is the current Unix timestamp.

    • TIME_DIFF_SECOND is the difference in seconds.

    • TIME_DIFF converts seconds into hours using bc for integer division.

  4. Output synchronization status

    echo "Synced up to $(echo $NODE_BLOCK_TIME | xargs -I {} date +"%Y-%m-%d %H:%M:%S" -d @{}) ($TIME_DIFF hours behind real time)"
    
    • Converts the NODE_BLOCK_TIME timestamp into a formatted date/time string.

    • Prints a message indicating the last block time and the lag in hours.


Usage Example

Suppose you have a log file named node.log:

./node_sync_status.sh node.log

Expected output:

Synced up to 2024-06-01 12:34:56 (2 hours behind real time)

This indicates the node's last finalized block timestamp and how many hours it is behind the current time.


Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram: Workflow of node_sync_status.sh

flowchart TD
A[Start: Receive log file path] --> B{Log file exists?}
B -- No --> C[Output error and exit]
B -- Yes --> D[Extract last finalized block timestamp using grep]
D --> E[Get current system time]
E --> F[Calculate time difference in seconds]
F --> G[Convert seconds to hours]
G --> H[Format last block timestamp to human-readable date]
H --> I[Output sync status message]
I --> J[End]

This flowchart illustrates the sequential steps taken by the script from receiving input to outputting the synchronization status.