diff --git a/README.md b/README.md index 14c7fce..a427781 100644 --- a/README.md +++ b/README.md @@ -38,8 +38,8 @@ PUBLIC_KEY='dashboard-wallet-public-key' 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/node/verifier.go b/node/verifier.go index 6998ece..743c32c 100644 --- a/node/verifier.go +++ b/node/verifier.go @@ -161,18 +161,22 @@ 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 { walletAddress, err := utils.GetWalletAddress() if err != nil { @@ -188,13 +192,14 @@ func CollectSampleAndVerify() { err = SubmitVerifiedProof(*walletAddress, *signature, *proof, *receipt, timestamp) 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 { @@ -203,15 +208,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/build-light-node.sh b/scripts/build-light-node.sh new file mode 100755 index 0000000..e69de29 diff --git a/scripts/build-risczero.sh b/scripts/build-risczero.sh new file mode 100755 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 ca72c43..34dd54b 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 runs the light node client (assumes it's already built) # 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,32 @@ 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" - -# 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" +# 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 # 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..3aa37b0 --- /dev/null +++ b/scripts/runner.sh @@ -0,0 +1,69 @@ +#!/bin/bash +# run-all.sh +# This script builds and 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}" +} + +# 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 + +# Build RISC0 Merkle Service +log "Building RISC0 Merkle Service..." +scripts/build-risczero.sh +if [ $? -ne 0 ]; then + error "Failed to build RISC0 Merkle Service" +fi + +# Build Light Node +log "Building Light Node Client..." +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..." +scripts/risczero-runner.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 + +success "All services have been shut down" +exit 0 \ No newline at end of file