-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
199 lines (167 loc) · 6.61 KB
/
main.go
File metadata and controls
199 lines (167 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Proof-of-Causal-Work (PoCW) Per-Epoch Integration
//
// This is the main entry point for the PoCW subnet with real-time per-epoch
// blockchain integration, showcasing a distributed consensus system where
// AI agents (miners) process user tasks and immediately submit verified
// intelligence work to the blockchain for KEY token mining.
//
// Architecture:
// - Miners: AI entities that process user requests with VLC consistency
// - Validators: Quality assessors using Byzantine Fault Tolerant consensus
// - VLC: Vector Logical Clocks ensure causal ordering of operations
// - Per-Epoch Integration: Real-time blockchain submission every 3 rounds
// - Intelligence Money: Verifiable work tokens based on actual task success
package main
import (
"fmt"
"net/http"
"os"
"os/exec"
"time"
"github.com/hetu-project/Intelligence-KEY-Mining/dgraph"
"github.com/hetu-project/Intelligence-KEY-Mining/subnet"
"github.com/hetu-project/Intelligence-KEY-Mining/subnet/demo"
)
// EpochBridge handles the interface between Go and the Node.js mainnet bridge
type EpochBridge struct {
bridgeCmd *exec.Cmd
}
// NewEpochBridge creates a new bridge to the Node.js mainnet submission service
func NewEpochBridge() *EpochBridge {
return &EpochBridge{}
}
// StartBridge starts the Node.js bridge service
func (eb *EpochBridge) StartBridge() error {
fmt.Println("🌐 Starting Per-Epoch Mainnet Bridge...")
// Start the Node.js bridge service
cmd := exec.Command("node", "mainnet-bridge-per-epoch.js")
cmd.Dir = "."
err := cmd.Start()
if err != nil {
return fmt.Errorf("failed to start bridge: %v", err)
}
eb.bridgeCmd = cmd
// Wait for bridge to initialize
time.Sleep(3 * time.Second)
fmt.Println("✅ Mainnet bridge service started")
return nil
}
// SubmitEpoch sends epoch data to the mainnet bridge for submission
func (eb *EpochBridge) SubmitEpoch(epochNumber int, subnetID string, epochData *subnet.EpochData) {
fmt.Printf("🚀 Bridge: Epoch %d ready for mainnet submission\n", epochNumber)
// In a full implementation, this would:
// 1. Convert epoch data to JSON
// 2. Send HTTP request to Node.js bridge service
// 3. Bridge submits to mainnet and mines KEY tokens
// For demonstration, we'll simulate the submission
fmt.Printf(" 📊 Subnet: %s\n", subnetID)
fmt.Printf(" 🔗 Rounds: %v\n", epochData.CompletedRounds)
fmt.Printf(" ⏰ VLC State: %v\n", epochData.VLCClockState)
fmt.Printf(" 💰 Triggering KEY mining for epoch %d...\n", epochNumber)
// Simulate processing time
time.Sleep(2 * time.Second)
fmt.Printf("✅ Epoch %d submitted to mainnet successfully!\n", epochNumber)
}
// StopBridge stops the Node.js bridge service
func (eb *EpochBridge) StopBridge() {
if eb.bridgeCmd != nil {
eb.bridgeCmd.Process.Kill()
fmt.Println("🔴 Mainnet bridge service stopped")
}
}
// waitForDgraph waits for Dgraph to be fully ready
func waitForDgraph() error {
maxRetries := 15
retryInterval := 2 * time.Second
for i := 0; i < maxRetries; i++ {
resp, err := http.Get("http://localhost:8080/health")
if err == nil && resp.StatusCode == http.StatusOK {
resp.Body.Close()
return nil
}
if resp != nil {
resp.Body.Close()
}
fmt.Printf("Dgraph not ready yet (attempt %d/%d), waiting %v...\n", i+1, maxRetries, retryInterval)
time.Sleep(retryInterval)
}
return fmt.Errorf("dgraph not ready after %d attempts", maxRetries)
}
// main demonstrates the per-epoch PoCW integration
func main() {
// Check if running in subnet-only mode
subnetOnlyMode := os.Getenv("SUBNET_ONLY_MODE") == "true"
if subnetOnlyMode {
fmt.Println("=== PoCW Subnet-Only Demo ===")
fmt.Println("Architecture: Pure subnet consensus with VLC visualization")
fmt.Println("")
} else {
fmt.Println("=== PoCW Per-Epoch Mainnet Integration Demo ===")
fmt.Println("Architecture: Real-time epoch submission (every 3 rounds)")
fmt.Println("")
}
// Initialize mainnet bridge only if not in subnet-only mode
var bridge *EpochBridge
if !subnetOnlyMode {
bridge = NewEpochBridge()
err := bridge.StartBridge()
if err != nil {
fmt.Printf("⚠️ Bridge startup failed: %v\n", err)
fmt.Println("Continuing with demonstration mode...")
}
defer bridge.StopBridge()
}
// Try to initialize Dgraph gracefully
fmt.Println("Waiting for Dgraph to be ready...")
if err := waitForDgraph(); err != nil {
fmt.Printf("Dgraph not available: %v\n", err)
fmt.Println("Running demo without graph visualization...")
} else {
fmt.Println("Initializing Dgraph connection...")
dgraph.InitDgraph("localhost:9080")
fmt.Println("Dgraph initialized successfully!")
}
// Create demo coordinator with per-epoch callback integration
coordinator := demo.NewDemoCoordinator("per-epoch-subnet-001")
// Set up HTTP bridge URL only if not in subnet-only mode
if !subnetOnlyMode && coordinator.GraphAdapter != nil {
fmt.Println("🔗 Setting up per-epoch HTTP bridge integration...")
// Set the bridge URL for HTTP communication
coordinator.GraphAdapter.SetBridgeURL("http://localhost:3001")
fmt.Println("✅ Per-epoch HTTP bridge configured successfully")
fmt.Println("📡 Graph adapter will send HTTP requests to JavaScript bridge")
} else if subnetOnlyMode {
fmt.Println("🔹 Running in subnet-only mode - no blockchain integration")
} else {
fmt.Println("⚠️ GraphAdapter not available - running standard demo")
}
fmt.Println("")
if subnetOnlyMode {
fmt.Println("🎯 Subnet-Only Demo Flow:")
fmt.Println(" Round 1-7 → Pure subnet consensus")
fmt.Println(" 📊 VLC data visible at: http://localhost:8000")
fmt.Println(" ⚠️ No blockchain integration or KEY mining")
} else {
fmt.Println("🎯 Demo Flow:")
fmt.Println(" Round 1-3 → Epoch 1 → Immediate mainnet submission")
fmt.Println(" Round 4-6 → Epoch 2 → Immediate mainnet submission")
fmt.Println(" Round 7 → Partial Epoch 3 → Submit at demo end")
}
fmt.Println("")
// Run the subnet demo
coordinator.RunDemo()
fmt.Println("")
if subnetOnlyMode {
fmt.Println("🎉 Subnet-Only Demo Complete!")
} else {
fmt.Println("🎉 Per-Epoch Integration Demo Complete!")
}
fmt.Println("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
fmt.Println("✅ Demonstrated real-time epoch submission architecture")
fmt.Println("✅ Each completed epoch triggers immediate mainnet posting")
fmt.Println("✅ KEY tokens are mined in real-time per epoch")
fmt.Println("")
fmt.Println("🔍 Visualization Access:")
fmt.Println(" - Ratel UI: http://localhost:8000")
fmt.Println(" - Inspector: http://localhost:3000/pocw-inspector.html")
}