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

feat: data commitment script #140

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 18 additions & 5 deletions scripts/deploy-blobstreamx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ SIERRA_FILE=$PROJECT_ROOT/target/dev/blobstream_sn_blobstreamx.contract_class.js
# Optional arguments
HERODOTUS_FACTS_REGISTRY_ADDRESS="0x07d3550237ecf2d6ddef9b78e59b38647ee511467fe000ce276f245a006b40bc"
BLOBSTREAMX_L1_ADDRESS="0x48B257EC1610d04191cC2c528d0c940AdbE1E439"
MAX_FEE="none"
# TODO : Change when Succint contracts are deployed
GATEWAY_ADDRESS="0x07e4220832ecf2d6ddef9b78e59b38647ee511467fe000ce225f245a006b32cb"
HEADER_RANGE_FUNCTION_ID="0x1"
Expand All @@ -35,7 +36,7 @@ display_help() {
echo " (required)"
echo " -a, --header U256 Header hash for the corresponding block height"
echo " (required)"

echo
echo " -g, --gateway ADDR Succint gateway contract address on Starknet"
echo " (default: $GATEWAY_ADDRESS (SEPOLIA))"
Expand All @@ -47,13 +48,15 @@ display_help() {
echo " (default: $HERODOTUS_FACTS_REGISTRY_ADDRESS (SEPOLIA))"
echo " -b, --blobstreamx-l1 ADDR BlobstreamX contract address on L1"
echo " (default: $BLOBSTREAMX_L1_ADDRESS (SEPOLIA))"
echo " -m, --max-fee ETH Max fee"
echo " (default: estimated automatically)"

echo
echo " -h, --help display help"
echo " -d, --debug save logs in debug_blobstreamx.log"

echo
echo "Example: $0 --owner 0x0 --height 0x1 --header 0x2"
echo "Example: $0 --owner 0x0 --height 1 --header 2"
}

# Transform long options to short ones
Expand All @@ -68,6 +71,7 @@ for arg in "$@"; do
"--next-header-function-id") set -- "$@" "-n" ;;
"--herodotus-facts-registry") set -- "$@" "-f" ;;
"--blobstreamx-l1") set -- "$@" "-b" ;;
"--max-fee") set -- "$@" "-m" ;;
"--help") set -- "$@" "-h" ;;
"--debug") set -- "$@" "-d" ;;
--*) unrecognized_options+=("$arg") ;;
Expand All @@ -82,7 +86,7 @@ if [ ! -z "${unrecognized_options[@]}" ]; then
fi

# Parse command line arguments
while getopts ":hdg:o:a:H:r:n:f:b:-:" opt; do
while getopts ":hdg:o:a:H:r:n:f:b:m:-:" opt; do
case ${opt} in
h )
display_help
Expand Down Expand Up @@ -115,6 +119,9 @@ while getopts ":hdg:o:a:H:r:n:f:b:-:" opt; do
b )
BLOBSTREAMX_L1_ADDRESS="$OPTARG"
;;
m )
MAX_FEE="$OPTARG"
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
display_help
Expand Down Expand Up @@ -171,11 +178,17 @@ declare() {
deploy() {
class_hash=$(declare | tail -n 1)

if [ "$MAX_FEE" = "none" ]; then
COMMAND="starkli deploy --network sepolia --keystore $STARKNET_KEYSTORE --account $STARKNET_ACCOUNT --watch $class_hash $GATEWAY_ADDRESS $OWNER $HEIGHT u256:$HEADER u256:$HEADER_RANGE_FUNCTION_ID u256:$NEXT_HEADER_FUNCTION_ID $HERODOTUS_FACTS_REGISTRY_ADDRESS $BLOBSTREAMX_L1_ADDRESS"
else
COMMAND="starkli deploy --network sepolia --keystore $STARKNET_KEYSTORE --account $STARKNET_ACCOUNT --max-fee $MAX_FEE --watch $class_hash $GATEWAY_ADDRESS $OWNER $HEIGHT u256:$HEADER u256:$HEADER_RANGE_FUNCTION_ID u256:$NEXT_HEADER_FUNCTION_ID $HERODOTUS_FACTS_REGISTRY_ADDRESS $BLOBSTREAMX_L1_ADDRESS"
fi

if [ "$DEBUG" = true ]; then
echo "deploy --network sepolia --keystore $STARKNET_KEYSTORE --account $STARKNET_ACCOUNT --watch $class_hash $GATEWAY_ADDRESS $OWNER $HEIGHT u256:$HEADER u256:$HEADER_RANGE_FUNCTION_ID u256:$NEXT_HEADER_FUNCTION_ID $HERODOTUS_FACTS_REGISTRY_ADDRESS $BLOBSTREAMX_L1_ADDRESS" >> debug_blobstreamx.log
echo "$COMMAND" >> debug_blobstreamx.log
fi

output=$(starkli deploy --network sepolia --keystore $STARKNET_KEYSTORE --account $STARKNET_ACCOUNT --watch $class_hash "$GATEWAY_ADDRESS" "$OWNER" "$HEIGHT" u256:"$HEADER" u256:"$HEADER_RANGE_FUNCTION_ID" u256:"$NEXT_HEADER_FUNCTION_ID" "$HERODOTUS_FACTS_REGISTRY_ADDRESS" "$BLOBSTREAMX_L1_ADDRESS" 2>&1)
output=$($COMMAND 2>&1)

if [ "$DEBUG" = true ]; then
echo "$output" >> debug_blobstreamx.log
Expand Down
38 changes: 20 additions & 18 deletions scripts/request-data-commitments.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
HERODOTUS_API_URL="https://api.herodotus.cloud/"
BLOBSTREAMX_SOURCE_CHAIN_ID="11155111"
BLOBSTREAMX_DESTINATION_CHAIN_ID="SN_SEPOLIA"
STATE_PROOF_NONCE_SLOT="0x00000000000000000000000000000000000000000000000000000000000000fc"
STATE_DATA_COMMITMENT_MAP_SLOT="0x00000000000000000000000000000000000000000000000000000000000000fe"
L1_STATE_PROOF_NONCE_SLOT="0x00000000000000000000000000000000000000000000000000000000000000fc"
L1_STATE_DATA_COMMITMENT_MAP_SLOT="0x00000000000000000000000000000000000000000000000000000000000000fe"
SN_STATE_PROOF_NONCE_SLOT="0x014b6daa0cec03ba4339e793f3875bb9b847d3153c67e97dc14fc96d048d4c7c"

# Optional arguments
L1_RPC_URL="https://sepolia.infura.io/v3/bed8a8401c894421bd7cd31050e7ced6"
STARKNET_RPC_URL="https://starknet-sepolia.infura.io/v3/bed8a8401c894421bd7cd31050e7ced6"
BLOBSTREAMX_L1_ADDRESS="0x48B257EC1610d04191cC2c528d0c940AdbE1E439"
#TODO : Change address to the Sepolia address once it is deployed
BLOBSTREAMX_STARKNET_ADDRESS="0x48B257EC1610d04191cC2c528d0c940AdbE1E439"
BLOBSTREAMX_STARKNET_ADDRESS="0x04179fb9990b3c7e44de802c4e40c8f395862d79a8c5eaa7340d999a5c1f625d"

NO_SEND=false
VERBOSE=false
Expand All @@ -25,6 +26,11 @@ WAIT=true
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
WORK_DIR=$SCRIPT_DIR/..

# Load env variable from `.env` only if they're not already set
if [ -z "$HERODOTUS_API_KEY" ] && [ "$NO_SEND" = false ]; then
source $WORK_DIR/.env
fi

display_help() {
echo "Usage: $0 [option...] {arguments...}"
echo
Expand Down Expand Up @@ -120,18 +126,14 @@ L1_BLOCK_NUM=$(echo $L1_BLOCK_NUM_RES | jq -r '.result')
LATEST_PROOF_NONCE_RES=$(curl $L1_RPC_URL \
-X POST \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"eth_getStorageAt","params":["'"$BLOBSTREAMX_L1_ADDRESS"'","'"$STATE_PROOF_NONCE_SLOT"'","'"$L1_BLOCK_NUM"'"],"id":1}' 2>/dev/null)
-d '{"jsonrpc":"2.0","method":"eth_getStorageAt","params":["'"$BLOBSTREAMX_L1_ADDRESS"'","'"$L1_STATE_PROOF_NONCE_SLOT"'","'"$L1_BLOCK_NUM"'"],"id":1}' 2>/dev/null)
LATEST_PROOF_NONCE=$(echo $LATEST_PROOF_NONCE_RES | jq -r '.result')

#TODO: Do a call to get_state_proof_nonce() once the function is deployed & replace hardcoded
#STARKNET_PROOF_NONCE_RES=$(curl $STARKNET_RPC_URL \
# -X POST \
# -H "Content-Type: application/json" \
# -d '{"jsonrpc":"2.0","method":"starknet_getState","params":["'"$BLOBSTREAMX_STARKNET_ADDRESS"'",'"$LATEST_PROOF_NONCE"'],"id":1}' 2>/dev/null)
#TODO: Remove the hardcoded value once the blobstreamx contract is deployed on Starknet
#STARKNET_PROOF_NONCE_RES=$(echo '{"jsonrpc":"2.0","id":1,"result":"0x00000000000000000000000000000000000000000000000000000000000006cb"}')
#STARKNET_PROOF_NONCE=$(echo $STARKNET_PROOF_NONCE_RES | jq -r '.result')
STARKNET_PROOF_NONCE=$((LATEST_PROOF_NONCE - 4))
STARKNET_PROOF_NONCE_RES=$(curl $STARKNET_RPC_URL \
-X POST \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"starknet_getStorageAt", "params":["'"$BLOBSTREAMX_STARKNET_ADDRESS"'", "'"$SN_STATE_PROOF_NONCE_SLOT"'", "pending"],"id":1}' 2>/dev/null)
STARKNET_PROOF_NONCE=$(echo $STARKNET_PROOF_NONCE_RES | jq -r '.result')

if [ "$VERBOSE" = true ]; then
echo "Latest L1 block number: $L1_BLOCK_NUM"
Expand All @@ -143,8 +145,8 @@ DATA_COMMITMENT_SLOTS=''
# Loop through each missing state_proofNonce to build the batch query slots
for ((i = $STARKNET_PROOF_NONCE; i <= LATEST_PROOF_NONCE - 1; i++)); do
# Data commitment slot for each proofNonce is located at
# keccak256(abi.encode(proofNonce, STATE_DATA_COMMITMENT_MAP_SLOT))
DATA_COMMITMENT_ENCODED_SLOT=$(printf "%064x%064x" $i $STATE_DATA_COMMITMENT_MAP_SLOT)
# keccak256(abi.encode(proofNonce, L1_STATE_DATA_COMMITMENT_MAP_SLOT))
DATA_COMMITMENT_ENCODED_SLOT=$(printf "%064x%064x" $i $L1_STATE_DATA_COMMITMENT_MAP_SLOT)
DATA_COMMITMENT_SLOT=$(echo $DATA_COMMITMENT_ENCODED_SLOT | keccak-256sum -x -l | awk '{print $1}')

if [ -z "$DATA_COMMITMENT_SLOTS" ]; then
Expand Down Expand Up @@ -173,7 +175,7 @@ HERODOTUS_QUERY='{
"accounts": {
"'$BLOBSTREAMX_L1_ADDRESS'": {
"slots": [
"'$STATE_PROOF_NONCE_SLOT'",
"'$L1_STATE_PROOF_NONCE_SLOT'",
'$DATA_COMMITMENT_SLOTS'
],
"props": []
Expand Down Expand Up @@ -220,8 +222,8 @@ fi

# Wait for state proof nonce slot to be relayed
echo
echo "Waiting for state proof nonce slot $STATE_PROOF_NONCE_SLOT to be relayed..."
$SCRIPT_DIR/wait-for-herodotus-fulfill.sh -b $L1_BLOCK_NUM_DEC -a $BLOBSTREAMX_L1_ADDRESS -S $STATE_PROOF_NONCE_SLOT $([ "$VERBOSE" = true ] && echo "-v")
echo "Waiting for state proof nonce slot $L1_STATE_PROOF_NONCE_SLOT to be relayed..."
$SCRIPT_DIR/wait-for-herodotus-fulfill.sh -b $L1_BLOCK_NUM_DEC -a $BLOBSTREAMX_L1_ADDRESS -S $L1_STATE_PROOF_NONCE_SLOT $([ "$VERBOSE" = true ] && echo "-v")
# Loop thru each data commitment slot to wait for the data to be relayed (comma separated)
for slot in $(echo $DATA_COMMITMENT_SLOTS | tr ',' '\n' | tr -d '"'); do
echo
Expand Down
114 changes: 114 additions & 0 deletions scripts/update-data-commitments.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/bin/bash

PROJECT_ROOT=`git rev-parse --show-toplevel`

# Load env variable from `.env` only if they're not already set
if [ -z "$STARKNET_KEYSTORE" ] || [ -z "$STARKNET_ACCOUNT" ]; then
source $PROJECT_ROOT/.env
fi

# Check if required env variables are set, if not exit
if [ -z "$STARKNET_KEYSTORE" ]; then
echo "Error: STARKNET_KEYSTORE is not set."
exit 1
elif [ -z "$STARKNET_ACCOUNT" ]; then
echo "Error: STARKNET_ACCOUNT is not set."
exit 1
fi

# Constants
BLOBSTREAMX_STARKNET_ADDRESS="0x04179fb9990b3c7e44de802c4e40c8f395862d79a8c5eaa7340d999a5c1f625d"
HERODOTUS_FACTS_REGISTRY_ADDRESS="0x07d3550237ecf2d6ddef9b78e59b38647ee511467fe000ce276f245a006b40bc"
MAX_FEE="none"

display_help() {
echo "Usage: $0 [option...]"
echo
echo " -n, --block-number U256 L1 block number"
echo " (required)"

echo
echo " -B, --blobstreamx-starknet ADDR BlobstreamX contract address on Starknet"
echo " (default: $BLOBSTREAMX_STARKNET_ADDRESS (SEPOLIA))"
echo " -m, --max-fee ETH Max fee"
echo " (default: estimated automatically)"

echo
echo " -h, --help display help"
echo " -v, --verbose verbose output"
echo
echo "Example: $0 --block-number 123456"
}

# Transform long options to short ones
for arg in "$@"; do
shift
case "$arg" in
"--block-number") set -- "$@" "-n" ;;
"--blobstreamx-starknet") set -- "$@" "-B" ;;
"--max-fee") set -- "$@" "-m" ;;
"--help") set -- "$@" "-h" ;;
"--verbose") set -- "$@" "-v" ;;
*) set -- "$@" "$arg"
esac
done

# Parse command line arguments
while getopts ":hvn:B:m:-:" opt; do
case ${opt} in
h )
display_help
exit 0
;;
v )
VERBOSE=true
;;
n )
L1_BLOCK_NUMBER="$OPTARG"
;;
B )
BLOBSTREAMX_STARKNET_ADDRESS="$OPTARG"
;;
m )
MAX_FEE="$OPTARG"
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
display_help
exit 1
;;
: )
echo "Invalid option: $OPTARG requires an argument" 1>&2
display_help
exit 1
;;
esac
done

# Check if required options are set, if not exit
if [ -z "$L1_BLOCK_NUMBER" ]; then
echo "Error: missing required option --block-number"
exit 1
fi

if [ "$MAX_FEE" = "none" ]; then
COMMAND="starkli invoke --network sepolia --keystore $STARKNET_KEYSTORE --account $STARKNET_ACCOUNT --watch $BLOBSTREAMX_STARKNET_ADDRESS update_data_commitments_from_facts u256:$L1_BLOCK_NUMBER"
else
COMMAND="starkli invoke --network sepolia --keystore $STARKNET_KEYSTORE --account $STARKNET_ACCOUNT --max-fee $MAX_FEE --watch $BLOBSTREAMX_STARKNET_ADDRESS update_data_commitments_from_facts u256:$L1_BLOCK_NUMBER"
fi

if [ "$VERBOSE" = true ]; then
echo "$COMMAND"
fi

output=$($COMMAND 2>&1)

if [[ $output == *"Error"* ]]; then
echo "Error: $output"
exit 1
fi

tx=$(echo "$output" | grep -oP '0x[0-9a-fA-F]+' | tail -n 1)

echo "Data commitments updated successfully for block $L1_BLOCK_NUMBER"
echo "See transaction: $tx"