From 62e1941b744f2192ab2fd978f382b50aad79ca98 Mon Sep 17 00:00:00 2001 From: Dyslex7c Date: Mon, 17 Mar 2025 15:38:24 +0530 Subject: [PATCH 1/3] refactor and add scripts --- node/verifier.go | 29 +++++++++++----- scripts/light-node-runner.sh | 67 ++++++++++++------------------------ scripts/risczero-runner.sh | 60 ++++++++++++++++++++++++++++++++ scripts/runner.sh | 63 +++++++++++++++++++++++++++++++++ 4 files changed, 166 insertions(+), 53 deletions(-) create mode 100755 scripts/risczero-runner.sh create mode 100755 scripts/runner.sh diff --git a/node/verifier.go b/node/verifier.go index 272043a..c0f228b 100644 --- a/node/verifier.go +++ b/node/verifier.go @@ -167,32 +167,38 @@ func CollectSampleAndVerify() { // Proceed with this tree sample := utils.RandomElement[string](tree.Leaves) + // Track if verification was successful + verificationSuccessful := false + proof, err := proveProof(tree.Leaves, sample) if err != nil { log.Printf("failed to prove sample for tree %s: %v", treeId, err) + // Continue to the next tree if proving fails continue } receipt, rootHash, err := verifyProofs(tree.Leaves, *proof) if err != nil { log.Printf("failed to verify sample for tree %s: %v", treeId, err) + // Continue to the next tree if verification fails continue } - if receipt != nil { + if receipt != nil && rootHash != nil { walletAddress := "YOUR_WALLET_ADDRESS" // Replace with actual wallet address signature := "YOUR_SIGNATURE" // Replace with signature created for this proof err = SubmitVerifiedProof(walletAddress, signature, *proof, *receipt) if err != nil { log.Printf("Failed to submit verified proof: %v", err) + // Continue to the next tree if submission fails + continue } else { log.Printf("Successfully submitted verified proof for tree %s", treeId) + verificationSuccessful = true } - } - // Update the tree state with the verified root - if rootHash != nil { + // Update the tree state with the verified root stateMutex.Lock() if state, exists := treeStates[treeId]; exists { if state.LastRoot != *rootHash { @@ -201,15 +207,22 @@ func CollectSampleAndVerify() { } } stateMutex.Unlock() + + log.Printf("Tree %s - Sample Data %v verified with receipt %v\n", treeId, sample, *receipt) + } else { + log.Printf("Tree %s - Verification failed: missing receipt or root hash", treeId) + continue } - log.Printf("Tree %s - Sample Data %v verified with receipt %v\n", treeId, sample, *receipt) - activeTreeFound = true - break + // Only mark as complete if verification was successful + if verificationSuccessful { + activeTreeFound = true + break + } } if !activeTreeFound { - log.Println("No active trees available for verification") + log.Println("No active trees available for verification or all verification attempts failed") } } diff --git a/scripts/light-node-runner.sh b/scripts/light-node-runner.sh index ca72c43..66ec7c2 100755 --- a/scripts/light-node-runner.sh +++ b/scripts/light-node-runner.sh @@ -1,16 +1,16 @@ #!/bin/bash -# Light Node Run Script -# This script starts the ZK prover service and launches the light node client +# start-light-node.sh +# This script builds and runs the light node client # Configuration -ZK_PROVER_DIR="risc0-merkle-service" LIGHT_NODE_DIR="." LIGHT_NODE_BIN="light-node" +ZK_PROVER_PID_FILE="zk_prover_pid.txt" # Color codes -GREEN='\033[0;32m' BLUE='\033[0;34m' RED='\033[0;31m' +GREEN='\033[0;32m' NC='\033[0m' # No Color # Log function @@ -23,61 +23,38 @@ error() { exit 1 } -# Check if zk-prover directory exists -if [ ! -d "$ZK_PROVER_DIR" ]; then - error "ZK prover directory not found. Please run this script from the project root." -fi - -# Check if Go is installed -if ! command -v go &> /dev/null; then - error "Go is not installed. Please install Go before running this script." -fi +success() { + echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')] $1${NC}" +} -# Check if risc0 toolchain is installed -if ! command -v rzup &> /dev/null; then - error "The 'risc0' toolchain could not be found. Install it using: curl -L https://risczero.com/install | bash && rzup install" +# Check if ZK prover PID file exists +if [ ! -f "$ZK_PROVER_PID_FILE" ]; then + error "ZK prover PID file not found. Please run start-risc0-service.sh first." fi -# Start ZK prover service -log "Starting ZK prover service..." -cd "$ZK_PROVER_DIR" || error "Failed to navigate to ZK prover directory" -cargo run > zk-prover.log 2>&1 & -ZK_PROVER_PID=$! -cd - > /dev/null || error "Failed to return to original directory" - -# Wait for ZK prover to start -log "Waiting for ZK prover to initialize (5 seconds)..." -sleep 5 +# Read ZK prover PID +ZK_PROVER_PID=$(cat "$ZK_PROVER_PID_FILE") # Check if ZK prover is running -if ! ps -p $ZK_PROVER_PID > /dev/null; then - error "ZK prover failed to start. Check zk-prover.log for details." +if ! ps -p "$ZK_PROVER_PID" > /dev/null; then + error "ZK prover is not running. Please start it using start-risc0-service.sh." fi -log "ZK prover started successfully with PID: $ZK_PROVER_PID" +# Check if Go is installed +if ! command -v go &> /dev/null; then + error "Go is not installed. Please install Go before running this script." +fi # Build light node log "Building light node..." cd "$LIGHT_NODE_DIR" || error "Failed to navigate to light node directory" go build -o "$LIGHT_NODE_BIN" || error "Failed to build light node" -log "Light node built successfully" +success "Light node built successfully" # Run light node -log "Starting light node..." -log "Light node logs will appear below:" +success "Starting light node..." +success "Light node logs will appear below:" echo -e "${GREEN}----------------------------------------${NC}" ./"$LIGHT_NODE_BIN" -# Handle exit - cleanup -cleanup() { - log "Shutting down services..." - kill $ZK_PROVER_PID 2>/dev/null - log "ZK prover service stopped" - log "Cleanup complete" -} - -# Set trap to catch script termination -trap cleanup EXIT - -# Exit -exit 0 +exit 0 \ No newline at end of file diff --git a/scripts/risczero-runner.sh b/scripts/risczero-runner.sh new file mode 100755 index 0000000..d78b1ed --- /dev/null +++ b/scripts/risczero-runner.sh @@ -0,0 +1,60 @@ +#!/bin/bash +# start-risc0-service.sh +# This script builds and starts the ZK prover service + +# Configuration +ZK_PROVER_DIR="risc0-merkle-service" +LOG_FILE="zk-prover.log" + +# Color codes +BLUE='\033[0;34m' +RED='\033[0;31m' +GREEN='\033[0;32m' +NC='\033[0m' # No Color + +# Log function +log() { + echo -e "${BLUE}[$(date '+%Y-%m-%d %H:%M:%S')] $1${NC}" +} + +error() { + echo -e "${RED}[$(date '+%Y-%m-%d %H:%M:%S')] ERROR: $1${NC}" + exit 1 +} + +success() { + echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')] $1${NC}" +} + +# Check if zk-prover directory exists +if [ ! -d "$ZK_PROVER_DIR" ]; then + error "ZK prover directory not found. Please run this script from the project root." +fi + +# Check if risc0 toolchain is installed +if ! command -v rzup &> /dev/null; then + error "The 'risc0' toolchain could not be found. Install it using: curl -L https://risczero.com/install | bash && rzup install" +fi + +# Start ZK prover service +log "Starting ZK prover service..." +cd "$ZK_PROVER_DIR" || error "Failed to navigate to ZK prover directory" + +# Start the service +cargo run > "$LOG_FILE" 2>&1 & +ZK_PROVER_PID=$! + +# Store PID to a file for other scripts to read +echo "$ZK_PROVER_PID" > "../zk_prover_pid.txt" + +# Wait for ZK prover to start +log "Waiting for ZK prover to initialize (5 seconds)..." +sleep 5 + +# Check if ZK prover is running +if ! ps -p $ZK_PROVER_PID > /dev/null; then + error "ZK prover failed to start. Check $LOG_FILE for details." +fi + +success "ZK prover started successfully with PID: $ZK_PROVER_PID" +success "ZK prover is now running and ready to accept connections" \ No newline at end of file diff --git a/scripts/runner.sh b/scripts/runner.sh new file mode 100755 index 0000000..9041552 --- /dev/null +++ b/scripts/runner.sh @@ -0,0 +1,63 @@ +#!/bin/bash +# run-all.sh +# This script runs both the ZK prover service and the light node client + +# Color codes +BLUE='\033[0;34m' +RED='\033[0;31m' +GREEN='\033[0;32m' +NC='\033[0m' # No Color + +# Log function +log() { + echo -e "${BLUE}[$(date '+%Y-%m-%d %H:%M:%S')] $1${NC}" +} + +error() { + echo -e "${RED}[$(date '+%Y-%m-%d %H:%M:%S')] ERROR: $1${NC}" + exit 1 +} + +success() { + echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')] $1${NC}" +} + +# Ensure scripts are executable +chmod +x start-risc0-service.sh +chmod +x start-light-node.sh + +# Handle exit - cleanup +cleanup() { + log "Shutting down services..." + + # Check if PID file exists + if [ -f "zk_prover_pid.txt" ]; then + ZK_PROVER_PID=$(cat zk_prover_pid.txt) + kill $ZK_PROVER_PID 2>/dev/null + log "ZK prover service stopped (PID: $ZK_PROVER_PID)" + rm zk_prover_pid.txt + fi + + log "Cleanup complete" +} + +# Set trap to catch script termination +trap cleanup EXIT INT TERM + +# Start RISC0 Merkle Service +log "Starting RISC0 Merkle Service..." +scripts/risczero-runner.sh + +# Check exit status +if [ $? -ne 0 ]; then + error "Failed to start RISC0 Merkle Service" +fi + +# Start Light Node +log "Starting Light Node Client..." +scripts/light-node-runner.sh + +# Note: We don't need to check exit status here as the script will end + +success "All services have been shut down" +exit 0 \ No newline at end of file From 5a95fecd42a99163683978036610071d7b863012 Mon Sep 17 00:00:00 2001 From: Dyslex7c Date: Mon, 17 Mar 2025 17:29:01 +0530 Subject: [PATCH 2/3] add more scripts --- scripts/build-light-node.sh | 0 scripts/build-risczero.sh | 43 ++++++++++++++++++++++++++++++++++++ scripts/light-node-runner.sh | 14 ++++-------- scripts/runner.sh | 28 ++++++++++++++++------- 4 files changed, 67 insertions(+), 18 deletions(-) create mode 100644 scripts/build-light-node.sh create mode 100644 scripts/build-risczero.sh diff --git a/scripts/build-light-node.sh b/scripts/build-light-node.sh new file mode 100644 index 0000000..e69de29 diff --git a/scripts/build-risczero.sh b/scripts/build-risczero.sh new file mode 100644 index 0000000..1f7add2 --- /dev/null +++ b/scripts/build-risczero.sh @@ -0,0 +1,43 @@ +#!/bin/bash +# build-risc0-service.sh +# This script builds the ZK prover service + +# Configuration +ZK_PROVER_DIR="risc0-merkle-service" + +# Color codes +BLUE='\033[0;34m' +RED='\033[0;31m' +GREEN='\033[0;32m' +NC='\033[0m' # No Color + +# Log function +log() { + echo -e "${BLUE}[$(date '+%Y-%m-%d %H:%M:%S')] $1${NC}" +} + +error() { + echo -e "${RED}[$(date '+%Y-%m-%d %H:%M:%S')] ERROR: $1${NC}" + exit 1 +} + +success() { + echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')] $1${NC}" +} + +# Check if zk-prover directory exists +if [ ! -d "$ZK_PROVER_DIR" ]; then + error "ZK prover directory not found. Please run this script from the project root." +fi + +# Check if risc0 toolchain is installed +if ! command -v rzup &> /dev/null; then + error "The 'risc0' toolchain could not be found. Install it using: curl -L https://risczero.com/install | bash && rzup install" +fi + +# Build the service +log "Building RISC0 Merkle service..." +cd "$ZK_PROVER_DIR" || error "Failed to navigate to ZK prover directory" +cargo build --release || error "Failed to build RISC0 Merkle service" + +success "RISC0 Merkle service built successfully" \ No newline at end of file diff --git a/scripts/light-node-runner.sh b/scripts/light-node-runner.sh index 66ec7c2..34dd54b 100755 --- a/scripts/light-node-runner.sh +++ b/scripts/light-node-runner.sh @@ -1,6 +1,6 @@ #!/bin/bash # start-light-node.sh -# This script builds and runs the light node client +# This script runs the light node client (assumes it's already built) # Configuration LIGHT_NODE_DIR="." @@ -40,17 +40,11 @@ if ! ps -p "$ZK_PROVER_PID" > /dev/null; then error "ZK prover is not running. Please start it using start-risc0-service.sh." fi -# Check if Go is installed -if ! command -v go &> /dev/null; then - error "Go is not installed. Please install Go before running this script." +# Check if the light node binary exists +if [ ! -f "$LIGHT_NODE_BIN" ]; then + error "Light node binary not found. Please build it first with build-light-node.sh." fi -# Build light node -log "Building light node..." -cd "$LIGHT_NODE_DIR" || error "Failed to navigate to light node directory" -go build -o "$LIGHT_NODE_BIN" || error "Failed to build light node" -success "Light node built successfully" - # Run light node success "Starting light node..." success "Light node logs will appear below:" diff --git a/scripts/runner.sh b/scripts/runner.sh index 9041552..16b8bb1 100755 --- a/scripts/runner.sh +++ b/scripts/runner.sh @@ -1,6 +1,6 @@ #!/bin/bash # run-all.sh -# This script runs both the ZK prover service and the light node client +# This script builds and runs both the ZK prover service and the light node client # Color codes BLUE='\033[0;34m' @@ -22,8 +22,10 @@ success() { echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')] $1${NC}" } -# Ensure scripts are executable +# Ensure all scripts are executable +chmod +x build-risc0-service.sh chmod +x start-risc0-service.sh +chmod +x build-light-node.sh chmod +x start-light-node.sh # Handle exit - cleanup @@ -44,20 +46,30 @@ cleanup() { # Set trap to catch script termination trap cleanup EXIT INT TERM +# Build RISC0 Merkle Service +log "Building RISC0 Merkle Service..." +./build-risc0-service.sh +if [ $? -ne 0 ]; then + error "Failed to build RISC0 Merkle Service" +fi + +# Build Light Node +log "Building Light Node Client..." +./build-light-node.sh +if [ $? -ne 0 ]; then + error "Failed to build Light Node Client" +fi + # Start RISC0 Merkle Service log "Starting RISC0 Merkle Service..." -scripts/risczero-runner.sh - -# Check exit status +./start-risc0-service.sh if [ $? -ne 0 ]; then error "Failed to start RISC0 Merkle Service" fi # Start Light Node log "Starting Light Node Client..." -scripts/light-node-runner.sh - -# Note: We don't need to check exit status here as the script will end +./start-light-node.sh success "All services have been shut down" exit 0 \ No newline at end of file From 41f35ba93b7a4007e4b311cd7f219019a4c21c82 Mon Sep 17 00:00:00 2001 From: Dyslex7c Date: Mon, 17 Mar 2025 17:57:02 +0530 Subject: [PATCH 3/3] modify README.md and separate scripts to build and run --- README.md | 4 ++-- scripts/build-light-node.sh | 0 scripts/build-risczero.sh | 0 scripts/runner.sh | 14 ++++---------- 4 files changed, 6 insertions(+), 12 deletions(-) mode change 100644 => 100755 scripts/build-light-node.sh mode change 100644 => 100755 scripts/build-risczero.sh diff --git a/README.md b/README.md index e3dd9d4..b00c8e9 100644 --- a/README.md +++ b/README.md @@ -35,8 +35,8 @@ API_REQUEST_TIMEOUT=100 Grant execute permission and run the script ```bash -chmod +x scripts/light-node-runner.sh -scripts/light-node-runner.sh +chmod +x scripts/* +scripts/runner.sh ``` ## Logging and Monitoring diff --git a/scripts/build-light-node.sh b/scripts/build-light-node.sh old mode 100644 new mode 100755 diff --git a/scripts/build-risczero.sh b/scripts/build-risczero.sh old mode 100644 new mode 100755 diff --git a/scripts/runner.sh b/scripts/runner.sh index 16b8bb1..3aa37b0 100755 --- a/scripts/runner.sh +++ b/scripts/runner.sh @@ -22,12 +22,6 @@ success() { echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')] $1${NC}" } -# Ensure all scripts are executable -chmod +x build-risc0-service.sh -chmod +x start-risc0-service.sh -chmod +x build-light-node.sh -chmod +x start-light-node.sh - # Handle exit - cleanup cleanup() { log "Shutting down services..." @@ -48,28 +42,28 @@ trap cleanup EXIT INT TERM # Build RISC0 Merkle Service log "Building RISC0 Merkle Service..." -./build-risc0-service.sh +scripts/build-risczero.sh if [ $? -ne 0 ]; then error "Failed to build RISC0 Merkle Service" fi # Build Light Node log "Building Light Node Client..." -./build-light-node.sh +scripts/build-light-node.sh if [ $? -ne 0 ]; then error "Failed to build Light Node Client" fi # Start RISC0 Merkle Service log "Starting RISC0 Merkle Service..." -./start-risc0-service.sh +scripts/risczero-runner.sh if [ $? -ne 0 ]; then error "Failed to start RISC0 Merkle Service" fi # Start Light Node log "Starting Light Node Client..." -./start-light-node.sh +scripts/light-node-runner.sh success "All services have been shut down" exit 0 \ No newline at end of file