#!/usr/bin/env bash # NOTE: This script patches the genesis timestamp of a fork config so a new # network could be scheduled to genesis in some instant in the future. set -eux -o pipefail # ==================== Inputs to this script ==================== # The fork config we aim to patch on FORK_CONFIG_TO_PATCH=${FORK_CONFIG_JSON:=fork_config.json} # The hashes file generated by runtime-genesis-ledger POSTFORK_GENESIS_LEDGER_HASHES=${LEDGER_HASHES_JSON:=ledger_hashes.json} # The "base" config file, needed to know the genesis time of the prefork network PREFORK_GENESIS_CONFIG=${FORKING_FROM_CONFIG_JSON:=genesis_ledgers/mainnet.json} # Genesis timestamp of the postfork network, defaults to 10mins in the future POSTFORK_GENESIS_TIMESTAMP=${GENESIS_TIMESTAMP:=$(date -u +"%Y-%m-%dT%H:%M:%SZ" -d "10 mins")} # =============================================================== # Pull the original genesis timestamp from the pre-fork config file PREFORK_GENESIS_TIMESTAMP=$(jq -r '.genesis.genesis_state_timestamp' "$PREFORK_GENESIS_CONFIG") PREFORK_GENESIS_SLOT=$(jq -r '.proof.fork.global_slot_since_genesis' "$PREFORK_GENESIS_CONFIG") if [[ "$PREFORK_GENESIS_SLOT" == null ]]; then PREFORK_GENESIS_SLOT=0 fi PREFORK_NETWORK_LIFESPAN_IN_SECONDS=$(($(date -d "$POSTFORK_GENESIS_TIMESTAMP" "+%s") - $(date -d "$PREFORK_GENESIS_TIMESTAMP" "+%s"))) # Default: mainnet currently uses 180s per slot PREFORK_SLOTS_IN_SECONDS=${SECONDS_PER_SLOT:=180} PREFORK_NETWORK_LIFESPAN_IN_SLOTS=$(($PREFORK_NETWORK_LIFESPAN_IN_SECONDS / $PREFORK_SLOTS_IN_SECONDS)) POSTFORK_GENESIS_SLOT=$((PREFORK_GENESIS_SLOT+PREFORK_NETWORK_LIFESPAN_IN_SLOTS)) # jq expression below could be written with less code, # but we aimed for maximum verbosity jq -M \ --arg genesis_timestamp "$POSTFORK_GENESIS_TIMESTAMP" \ --argjson genesis_slot "$POSTFORK_GENESIS_SLOT" \ --slurpfile hashes "$POSTFORK_GENESIS_LEDGER_HASHES" \ ' { genesis: { genesis_state_timestamp: $genesis_timestamp }, proof: { fork: { state_hash: .proof.fork.state_hash, blockchain_length: .proof.fork.blockchain_length, global_slot_since_genesis: $genesis_slot } }, ledger: { add_genesis_winner: false, hash: $hashes[0].ledger.hash, s3_data_hash: $hashes[0].ledger.s3_data_hash }, epoch_data: { staking: { seed: .epoch_data.staking.seed, hash: $hashes[0].epoch_data.staking.hash, s3_data_hash: $hashes[0].epoch_data.staking.s3_data_hash }, next: { seed: .epoch_data.next.seed, hash: $hashes[0].epoch_data.next.hash, s3_data_hash: $hashes[0].epoch_data.next.s3_data_hash } } } ' "$FORK_CONFIG_TO_PATCH"