From 0d9ef9b5b7ebb8614344a8a8e25d5df4104c6a89 Mon Sep 17 00:00:00 2001 From: aitbc1 Date: Sun, 29 Mar 2026 16:52:31 +0200 Subject: [PATCH] refactor: add blockchain sync and network optimization scripts to workflow MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 📋 Workflow Enhancement: • Added step 6: Blockchain Sync Fix (08_blockchain_sync_fix.sh) • Renumbered step 6 to 7: Enhanced Transaction Manager (09_transaction_manager.sh) • Renumbered step 7 to 8: Final Verification (unchanged) • Added step 9: Complete Workflow orchestrator (10_complete_workflow.sh) • Added step 10: Network Optimization (11_network_optimizer.sh) • Renumbered step 8 to 11: Complete Sync (unchanged) 🔧 Script --- .../workflows/multi-node-blockchain-setup.md | 31 ++++- cli/simple_wallet.py | 2 +- scripts/workflow/08_blockchain_sync_fix.sh | 101 +++++++++++++++ scripts/workflow/09_transaction_manager.sh | 113 +++++++++++++++++ scripts/workflow/10_complete_workflow.sh | 116 ++++++++++++++++++ scripts/workflow/11_network_optimizer.sh | 81 ++++++++++++ 6 files changed, 438 insertions(+), 6 deletions(-) create mode 100755 scripts/workflow/08_blockchain_sync_fix.sh create mode 100755 scripts/workflow/09_transaction_manager.sh create mode 100755 scripts/workflow/10_complete_workflow.sh create mode 100755 scripts/workflow/11_network_optimizer.sh diff --git a/.windsurf/workflows/multi-node-blockchain-setup.md b/.windsurf/workflows/multi-node-blockchain-setup.md index dc3a3111..4cea9707 100644 --- a/.windsurf/workflows/multi-node-blockchain-setup.md +++ b/.windsurf/workflows/multi-node-blockchain-setup.md @@ -140,21 +140,42 @@ The newly created wallet on aitbc will: - The wallet can only transact after receiving coins from genesis - All wallets share the same blockchain, created by aitbc1 -### 6. Send 1000 AIT from Genesis to aitbc Wallet +### 6. Blockchain Sync Fix (Enhanced) ```bash -# Run the transaction sending script -/opt/aitbc/scripts/workflow/05_send_transaction.sh +# Fix blockchain synchronization issues between nodes +/opt/aitbc/scripts/workflow/08_blockchain_sync_fix.sh ``` -### 7. Final Verification +### 7. Send 1000 AIT from Genesis to aitbc Wallet (Enhanced) + +```bash +# Run the enhanced transaction manager +/opt/aitbc/scripts/workflow/09_transaction_manager.sh +``` + +### 8. Final Verification ```bash # Run the final verification script /opt/aitbc/scripts/workflow/06_final_verification.sh ``` -### 8. Complete Sync (Optional - for full demonstration) +### 9. Complete Workflow (All-in-One) + +```bash +# Execute the complete optimized workflow +/opt/aitbc/scripts/workflow/10_complete_workflow.sh +``` + +### 10. Network Optimization (Performance Enhancement) + +```bash +# Optimize network configuration and performance +/opt/aitbc/scripts/workflow/11_network_optimizer.sh +``` + +### 11. Complete Sync (Optional - for full demonstration) ```bash # If aitbc is still behind, complete the sync diff --git a/cli/simple_wallet.py b/cli/simple_wallet.py index 3121f6e4..fed4ae77 100644 --- a/cli/simple_wallet.py +++ b/cli/simple_wallet.py @@ -539,7 +539,7 @@ def submit_ai_job(wallet_name: str, job_type: str, prompt: str, payment: float, return None def get_balance(wallet_name: str, keystore_dir: Path = DEFAULT_KEYSTORE_DIR, rpc_url: str = DEFAULT_RPC_URL) -> Optional[Dict]: - """Get wallet balance and transaction info""" + """Get wallet balance and transaction info""" try: keystore_path = keystore_dir / f"{wallet_name}.json" if not keystore_path.exists(): diff --git a/scripts/workflow/08_blockchain_sync_fix.sh b/scripts/workflow/08_blockchain_sync_fix.sh new file mode 100755 index 00000000..1a0ff11a --- /dev/null +++ b/scripts/workflow/08_blockchain_sync_fix.sh @@ -0,0 +1,101 @@ +#!/bin/bash +# AITBC Blockchain Sync Fix Script +# Resolves synchronization issues between genesis and follower nodes + +echo "=== AITBC Blockchain Sync Fix ===" + +# Check current status +echo "1. Current blockchain status:" +AITBC1_HEIGHT=$(curl -s http://localhost:8006/rpc/head | jq .height 2>/dev/null || echo "0") +AITBC_HEIGHT=$(ssh aitbc 'curl -s http://localhost:8006/rpc/head | jq .height 2>/dev/null || echo "0"') + +echo "aitbc1 height: $AITBC1_HEIGHT" +echo "aitbc height: $AITBC_HEIGHT" + +# Check if aitbc has any blocks +if [ "$AITBC_HEIGHT" = "0" ] || [ "$AITBC_HEIGHT" = "null" ]; then + echo "2. aitbc has no blocks - performing manual sync..." + + # Copy genesis block with proper format + echo " Copying genesis block..." + scp /opt/aitbc/apps/blockchain-node/data/ait-mainnet/genesis.json aitbc:/tmp/ + + # Create proper genesis block format for import + ssh aitbc 'cat > /tmp/genesis_proper.json << EOF +{ + "height": 0, + "hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "parent_hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "timestamp": "1774794510", + "proposer": "ait128p577qftddusxvtu4yvxjkwlnx232jlr8lrq57u93getatdrkcsqghm0q", + "allocations": [ + {"address": "ait128p577qftddusxvtu4yvxjkwlnx232jlr8lrq57u93getatdrkcsqghm0q", "balance": 1000000000, "nonce": 0}, + {"address": "ait1uwunjewjrserytqzd28pmpkq46uyl2els8c2536f8e8496sahpcsy3r3cz", "balance": 0, "nonce": 0} + ], + "authorities": [ + {"address": "ait128p577qftddusxvtu4yvxjkwlnx232jlr8lrq57u93getatdrkcsqghm0q", "weight": 1} + ], + "params": { + "base_fee": 10, + "coordinator_ratio": 0.05, + "fee_per_byte": 1, + "mint_per_unit": 0 + } +} +EOF' + + # Import genesis block + echo " Importing genesis block..." + ssh aitbc 'curl -X POST http://localhost:8006/rpc/importBlock -H "Content-Type: application/json" -d @/tmp/genesis_proper.json' + + # Restart services + echo " Restarting aitbc services..." + ssh aitbc 'systemctl restart aitbc-blockchain-node aitbc-blockchain-rpc' + + # Wait for services to start + sleep 5 + + # Check status again + AITBC_HEIGHT=$(ssh aitbc 'curl -s http://localhost:8006/rpc/head | jq .height 2>/dev/null || echo "0"') + echo " aitbc height after genesis import: $AITBC_HEIGHT" +fi + +# Sync recent blocks if still behind +if [ "$AITBC_HEIGHT" -lt "$((AITBC1_HEIGHT - 5))" ]; then + echo "3. Syncing recent blocks from aitbc1..." + + # Get proposer address from genesis + PROPOSER=$(cat /opt/aitbc/apps/blockchain-node/keystore/aitbc1genesis.json | jq -r '.address') + + for height in $(seq $((AITBC_HEIGHT + 1)) $AITBC1_HEIGHT); do + echo " Importing block $height..." + + # Get block from aitbc1 + curl -s "http://localhost:8006/rpc/blocks-range?start=$height&end=$height" | \ + jq '.blocks[0] + {"proposer": "'$PROPOSER'"}' > /tmp/block$height.json + + # Import to aitbc + scp /tmp/block$height.json aitbc:/tmp/ + ssh aitbc "curl -X POST http://localhost:8006/rpc/importBlock -H 'Content-Type: application/json' -d @/tmp/block$height.json" + + sleep 1 + done + + echo " Block sync completed!" +fi + +# Final verification +echo "4. Final sync verification:" +AITBC1_FINAL=$(curl -s http://localhost:8006/rpc/head | jq .height) +AITBC_FINAL=$(ssh aitbc 'curl -s http://localhost:8006/rpc/head | jq .height') + +echo "aitbc1 final height: $AITBC1_FINAL" +echo "aitbc final height: $AITBC_FINAL" + +if [ "$AITBC_FINAL" -ge "$((AITBC1_FINAL - 2))" ]; then + echo "✅ Blockchain synchronization successful!" +else + echo "⚠️ Sync may still be in progress" +fi + +echo "=== Blockchain Sync Fix Complete ===" diff --git a/scripts/workflow/09_transaction_manager.sh b/scripts/workflow/09_transaction_manager.sh new file mode 100755 index 00000000..29f23c88 --- /dev/null +++ b/scripts/workflow/09_transaction_manager.sh @@ -0,0 +1,113 @@ +#!/bin/bash +# AITBC Transaction Manager Script +# Enhanced transaction sending with proper error handling + +echo "=== AITBC Transaction Manager ===" + +# Configuration +GENESIS_WALLET="aitbc1genesis" +TARGET_WALLET="aitbc-wallet" +AMOUNT=1000 +FEE=10 +PASSWORD_FILE="/var/lib/aitbc/keystore/.password" + +# Check prerequisites +echo "1. Checking prerequisites..." +if [ ! -f "$PASSWORD_FILE" ]; then + echo "❌ Password file not found: $PASSWORD_FILE" + exit 1 +fi + +# Get wallet addresses +echo "2. Getting wallet addresses..." +GENESIS_ADDR=$(cat /opt/aitbc/apps/blockchain-node/keystore/aitbc1genesis.json | jq -r '.address') +TARGET_ADDR=$(ssh aitbc 'cat /var/lib/aitbc/keystore/aitbc-wallet.json | jq -r ".address"') + +echo "Genesis address: $GENESIS_ADDR" +echo "Target address: $TARGET_ADDR" + +# Check balances +echo "3. Checking current balances..." +GENESIS_BALANCE=$(curl -s "http://localhost:8006/rpc/getBalance/$GENESIS_ADDR" | jq .balance) +TARGET_BALANCE=$(curl -s "http://localhost:8006/rpc/getBalance/$TARGET_ADDR" | jq .balance) + +echo "Genesis balance: $GENESIS_BALANCE AIT" +echo "Target balance: $TARGET_BALANCE AIT" + +# Create transaction using RPC +echo "4. Creating and sending transaction..." +TX_DATA=$(cat << EOF +{ + "type": "transfer", + "from": "$GENESIS_ADDR", + "sender": "$GENESIS_ADDR", + "to": "$TARGET_ADDR", + "amount": $AMOUNT, + "fee": $FEE, + "nonce": $GENESIS_BALANCE, + "payload": "0x" +} +EOF +) + +echo "Transaction data: $TX_DATA" + +# Send transaction +echo "5. Sending transaction..." +TX_RESULT=$(curl -X POST http://localhost:8006/rpc/sendTx \ + -H "Content-Type: application/json" \ + -d "$TX_DATA") + +echo "Transaction result: $TX_RESULT" + +# Extract transaction hash if successful +TX_HASH=$(echo "$TX_RESULT" | jq -r '.hash // .transaction_hash // empty') + +if [ -n "$TX_HASH" ] && [ "$TX_HASH" != "null" ]; then + echo "✅ Transaction sent successfully!" + echo "Transaction hash: $TX_HASH" + + # Wait for transaction to be mined + echo "6. Waiting for transaction to be mined..." + for i in {1..20}; do + sleep 2 + NEW_BALANCE=$(curl -s "http://localhost:8006/rpc/getBalance/$TARGET_ADDR" | jq .balance) + echo "Check $i/20: Target balance = $NEW_BALANCE AIT" + + if [ "$NEW_BALANCE" -gt "$TARGET_BALANCE" ]; then + echo "✅ Transaction mined successfully!" + echo "New balance: $NEW_BALANCE AIT" + break + fi + done +else + echo "❌ Transaction failed" + echo "Error: $TX_RESULT" + + # Try alternative method using CLI + echo "7. Trying alternative CLI method..." + /opt/aitbc/venv/bin/python /opt/aitbc/cli/simple_wallet.py send \ + --from $GENESIS_WALLET \ + --to $TARGET_ADDR \ + --amount $AMOUNT \ + --fee $FEE \ + --password-file $PASSWORD_FILE \ + --rpc-url http://localhost:8006 +fi + +# Final verification +echo "8. Final balance verification..." +FINAL_GENESIS_BALANCE=$(curl -s "http://localhost:8006/rpc/getBalance/$GENESIS_ADDR" | jq .balance) +FINAL_TARGET_BALANCE=$(curl -s "http://localhost:8006/rpc/getBalance/$TARGET_ADDR" | jq .balance) + +echo "Final genesis balance: $FINAL_GENESIS_BALANCE AIT" +echo "Final target balance: $FINAL_TARGET_BALANCE AIT" + +if [ "$FINAL_TARGET_BALANCE" -gt "$TARGET_BALANCE" ]; then + TRANSFERRED=$((FINAL_TARGET_BALANCE - TARGET_BALANCE)) + echo "✅ Transaction successful! Transferred: $TRANSFERRED AIT" +else + echo "❌ Transaction may have failed or is still pending" +fi + +echo "=== Transaction Manager Complete ===" diff --git a/scripts/workflow/10_complete_workflow.sh b/scripts/workflow/10_complete_workflow.sh new file mode 100755 index 00000000..ca87d779 --- /dev/null +++ b/scripts/workflow/10_complete_workflow.sh @@ -0,0 +1,116 @@ +#!/bin/bash +# AITBC Complete Multi-Node Workflow Script +# Runs the entire multi-node blockchain setup with error handling + +echo "=== AITBC Complete Multi-Node Workflow ===" +echo "This script sets up a complete two-node blockchain network" +echo "aitbc1: Genesis Authority | aitbc: Follower Node" +echo + +# Check if running on correct node +if [ "$(hostname)" != "aitbc1" ]; then + echo "❌ Error: This script must be run on aitbc1 (genesis authority node)" + exit 1 +fi + +# Define workflow steps +STEPS=( + "01_preflight_setup.sh:Pre-Flight Setup" + "02_genesis_authority_setup.sh:Genesis Authority Setup" + "03_follower_node_setup.sh:Follower Node Setup" + "08_blockchain_sync_fix.sh:Blockchain Sync Fix" + "04_create_wallet.sh:Wallet Creation" + "09_transaction_manager.sh:Transaction Manager" + "06_final_verification.sh:Final Verification" +) + +# Execute workflow steps +for step in "${STEPS[@]}"; do + SCRIPT=$(echo "$step" | cut -d: -f1) + DESCRIPTION=$(echo "$step" | cut -d: -f2) + + echo + echo "==========================================" + echo "STEP: $DESCRIPTION" + echo "SCRIPT: $SCRIPT" + echo "==========================================" + + if [ -f "/opt/aitbc/scripts/workflow/$SCRIPT" ]; then + echo "Executing $SCRIPT..." + bash "/opt/aitbc/scripts/workflow/$SCRIPT" + + if [ $? -eq 0 ]; then + echo "✅ $DESCRIPTION completed successfully" + else + echo "❌ $DESCRIPTION failed" + echo "Continue with next step? (y/N)" + read -r response + if [[ ! $response =~ ^[Yy]$ ]]; then + echo "Workflow stopped by user" + exit 1 + fi + fi + else + echo "❌ Script not found: $SCRIPT" + echo "Continue with next step? (y/N)" + read -r response + if [[ ! $response =~ ^[Yy]$ ]]; then + echo "Workflow stopped by user" + exit 1 + fi + fi + + echo "Press Enter to continue to next step..." + read -r +done + +echo +echo "==========================================" +echo "🎉 MULTI-NODE BLOCKCHAIN WORKFLOW COMPLETE!" +echo "==========================================" +echo + +# Final status check +echo "Final Status Check:" +echo "==================" + +AITBC1_HEIGHT=$(curl -s http://localhost:8006/rpc/head | jq .height 2>/dev/null || echo "0") +AITBC_HEIGHT=$(ssh aitbc 'curl -s http://localhost:8006/rpc/head | jq .height 2>/dev/null || echo "0"') + +echo "aitbc1 (Genesis):" +echo " Height: $AITBC1_HEIGHT" +echo " Services: $(systemctl is-active aitbc-blockchain-node aitbc-blockchain-rpc | tr '\n' ' ')" + +echo +echo "aitbc (Follower):" +echo " Height: $AITBC_HEIGHT" +echo " Services: $(ssh aitbc 'systemctl is-active aitbc-blockchain-node aitbc-blockchain-rpc 2>/dev/null | tr "\n" " ')"" + +echo +echo "Wallet Status:" +if [ -f "/var/lib/aitbc/keystore/aitbc-wallet.json" ]; then + WALLET_ADDR=$(cat /var/lib/aitbc/keystore/aitbc-wallet.json | jq -r '.address') + WALLET_BALANCE=$(curl -s "http://localhost:8006/rpc/getBalance/$WALLET_ADDR" | jq .balance 2>/dev/null || echo "0") + echo " Wallet: $WALLET_ADDR" + echo " Balance: $WALLET_BALANCE AIT" +else + echo " Wallet: Not created" +fi + +echo +echo "Network Status:" +HEIGHT_DIFF=$((AITBC1_HEIGHT - AITBC_HEIGHT)) +if [ $HEIGHT_DIFF -le 2 ]; then + echo " ✅ Nodes synchronized (diff: $HEIGHT_DIFF blocks)" +else + echo " ⚠️ Nodes not synchronized (diff: $HEIGHT_DIFF blocks)" +fi + +echo +echo "🚀 Multi-node blockchain setup is ready!" +echo "Next Steps:" +echo "1. Run enterprise automation: /opt/aitbc/scripts/workflow/07_enterprise_automation.sh" +echo "2. Monitor with health checks: /opt/aitbc/scripts/health_check.sh" +echo "3. Test with integration tests: /opt/aitbc/tests/integration_test.sh" + +echo "=== Complete Workflow Finished ===" diff --git a/scripts/workflow/11_network_optimizer.sh b/scripts/workflow/11_network_optimizer.sh new file mode 100755 index 00000000..d1ff14a4 --- /dev/null +++ b/scripts/workflow/11_network_optimizer.sh @@ -0,0 +1,81 @@ +#!/bin/bash +# AITBC Network Optimization Script +# Optimizes network configuration and performance + +echo "=== AITBC Network Optimization ===" + +# Check current network status +echo "1. Current network status:" +echo " aitbc1 height: $(curl -s http://localhost:8006/rpc/head | jq .height)" +echo " aitbc height: $(ssh aitbc 'curl -s http://localhost:8006/rpc/head | jq .height 2>/dev/null || echo "0"')" +echo " Network latency: $(ping -c 1 10.1.223.93 | grep "time=" | cut -d= -f2)" + +# Optimize Redis configuration +echo "2. Optimizing Redis configuration..." +redis-cli CONFIG SET maxmemory 2gb +redis-cli CONFIG SET maxmemory-policy allkeys-lru +redis-cli CONFIG SET tcp-keepalive 300 +redis-cli CONFIG SET timeout 0 + +# Optimize blockchain node configuration +echo "3. Optimizing blockchain node configuration..." +# Update environment file for better performance +sed -i 's|block_time_seconds=10|block_time_seconds=2|g' /etc/aitbc/blockchain.env +sed -i 's|p2p_bind_port=7070|p2p_bind_port=7070|g' /etc/aitbc/blockchain.env + +# Copy optimized config to aitbc +scp /etc/aitbc/blockchain.env aitbc:/etc/aitbc/blockchain.env + +# Restart services with new configuration +echo "4. Restarting services with optimized configuration..." +systemctl restart aitbc-blockchain-node aitbc-blockchain-rpc +ssh aitbc 'systemctl restart aitbc-blockchain-node aitbc-blockchain-rpc' + +# Wait for services to start +sleep 5 + +# Verify optimization +echo "5. Verifying optimization results..." +echo " aitbc1 RPC response time: $(curl -w "%{time_total}" -s -o /dev/null http://localhost:8006/rpc/head) seconds" +echo " aitbc RPC response time: $(ssh aitbc 'curl -w "%{time_total}" -s -o /dev/null http://localhost:8006/rpc/head') seconds" + +# Check system resources +echo "6. System resource optimization..." +# Optimize file descriptors +echo 'root soft nofile 65536' >> /etc/security/limits.conf +echo 'root hard nofile 65536' >> /etc/security/limits.conf + +# Optimize network parameters +echo 'net.core.somaxconn = 65535' >> /etc/sysctl.conf +echo 'net.ipv4.tcp_max_syn_backlog = 65535' >> /etc/sysctl.conf +echo 'vm.swappiness = 10' >> /etc/sysctl.conf + +# Apply sysctl changes +sysctl -p + +# Setup monitoring +echo "7. Setting up network monitoring..." +cat > /opt/aitbc/scripts/network_monitor.sh << 'EOF' +#!/bin/bash +# Network monitoring script +echo "=== Network Monitor ===" +echo "Time: $(date)" +echo "aitbc1 height: $(curl -s http://localhost:8006/rpc/head | jq .height)" +echo "aitbc height: $(ssh aitbc 'curl -s http://localhost:8006/rpc/head | jq .height 2>/dev/null || echo "0"')" +echo "Redis status: $(redis-cli ping)" +echo "Network latency: $(ping -c 1 10.1.223.93 | grep "time=" | cut -d= -f2)" +echo "Memory usage: $(free -h | grep Mem)" +echo "CPU usage: $(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d% -f1)%" +echo "================================" +EOF + +chmod +x /opt/aitbc/scripts/network_monitor.sh + +# Add to cron for continuous monitoring +(crontab -l 2>/dev/null; echo "*/2 * * * * /opt/aitbc/scripts/network_monitor.sh >> /var/log/aitbc/network_monitor.log") | crontab - + +echo "✅ Network optimization completed!" +echo "Monitoring script: /opt/aitbc/scripts/network_monitor.sh" +echo "Log file: /var/log/aitbc/network_monitor.log" + +echo "=== Network Optimization Complete ==="