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

Improvement in Error Handling #299

Open
effizzieng opened this issue Feb 29, 2024 · 0 comments
Open

Improvement in Error Handling #299

effizzieng opened this issue Feb 29, 2024 · 0 comments

Comments

@effizzieng
Copy link

there are a few issues with the code that could be addressed to improve its reliability and maintainability.

Error Handling: The script checks for the presence of the DB_HOST, DB_USER, and DB_NAME environment variables, but it doesn't provide informative error messages if they are missing. It simply exits without indicating which variable is missing or how the user can resolve the issue. Providing clearer error messages would enhance the usability of the script.

Input Validation: While the script checks whether the batch_index argument is a numeric value, it doesn't handle the case where no argument is provided at all. In this scenario, the script should display a message prompting the user to specify the batch index.

Output File Handling: The script specifies a default output file name (full_proof_1.json) using the ${OUTPUT_FILE:-"full_proof_1.json"} syntax. However, it doesn't verify whether the specified output file path is valid or writable. Adding a check to ensure that the script can write to the specified output file would prevent potential errors during execution.

Security: The script constructs SQL queries by directly interpolating variables, which can be vulnerable to SQL injection attacks if the input is not properly sanitized. Using prepared statements or parameterized queries would mitigate this risk.

Code Duplication: There is duplication in the database query and processing logic. Both the chunk information and chunk proofs are retrieved separately with similar queries. Consolidating these queries and processing steps could simplify the code and improve maintainability.

By addressing these issues, you can enhance the robustness and usability of the script,

Below is the improved version.

# Check if the batch index argument is provided
batch_index=$1
if [ -z "$batch_index" ] || ! [[ $batch_index =~ ^[0-9]+$ ]]; then
    echo "Please specify a valid batch index as the first argument."
    exit
fi

# Check if required environment variables are set
if [ -z "$DB_HOST" ] || [ -z "$DB_USER" ] || [ -z "$DB_NAME" ]; then
    echo "Error: Please set the environment variables DB_HOST, DB_USER, and DB_NAME."
    exit
fi

# Specify the output file path, using a default if not provided
output_file="${OUTPUT_FILE:-"full_proof_1.json"}"

# Specify the chain ID, using a default if not provided
chain_id="${CHAIN_ID:-534351}"

# Retrieve chunk information and chunk proofs from the database
chunk_infos=$(psql -h "$DB_HOST" -U "$DB_USER" "$DB_NAME" --csv -c "
select json_agg(res) as infos from (
    select
        $chain_id as chain_id,
        false as is_padding,
        chunk.state_root              as post_state_root,
        chunk.parent_chunk_state_root as prev_state_root,
        chunk.withdraw_root           as withdraw_root,
        chunk.hash                    as data_hash
    from chunk join batch on chunk.batch_hash = batch.hash
    where batch.index = $batch_index
    order by chunk.index
) res;")

chunk_proofs=$(psql -h "$DB_HOST" -U "$DB_USER" "$DB_NAME" --csv -c "
select convert_from(chunk.proof, 'UTF-8') as proofs
    from chunk join batch on chunk.batch_hash = batch.hash
    where batch.index = $batch_index
    order by chunk.index;")

# Format and combine chunk information and chunk proofs into a JSON object
chunk_infos=$(echo "$chunk_infos" | sed 's/""/"/g')
chunk_infos=$(echo "$chunk_infos" | sed 's/^infos "\(.*\)"$/"chunk_infos": \1/g')

chunk_proofs=$(echo "$chunk_proofs" | sed 's/" "/,/g')
chunk_proofs=$(echo "$chunk_proofs" | sed 's/""/"/g')
chunk_proofs=$(echo "$chunk_proofs" | sed 's/^proofs "\(.*\)"$/"chunk_proofs": [\1]/g')

# Write the JSON object to the output file
echo "{$chunk_infos,$chunk_proofs}" | jq > "$output_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant