anya-core 1.2.0

Enterprise-grade Bitcoin Infrastructure Platform
Documentation
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#Requires -Version 5.0
#Requires -Modules NetworkTests

#region Configuration
$ErrorActionPreference = "Stop"

# Color definitions
$colors = @{
    Red      = [System.ConsoleColor]::Red
    Green    = [System.ConsoleColor]::Green 
    Yellow   = [System.ConsoleColor]::Yellow
    Cyan     = [System.ConsoleColor]::Cyan
    White    = [System.ConsoleColor]::White
    DarkCyan = [System.ConsoleColor]::DarkCyan
    Gray     = [System.ConsoleColor]::Gray
    Blue     = [System.ConsoleColor]::Blue
}

# Box drawing characters
$box = @{
    TopLeft     = '┌'
    TopRight    = '┐'
    BottomLeft  = '└'
    BottomRight = '┘'
    Horizontal  = '─'
    Vertical    = '│'
    TeeRight    = '├'
    TeeLeft     = '┤'
}

# Test categories configuration
$testCategories = @{
    "Minimal Tests" = @{
        RAM = 2
        CPU = 2
        Disk = 10
        Tests = @(
            @{Name="Core Library Tests"; Command="cargo test --lib --no-default-features"}
            @{Name="Basic Protocol Tests"; Command="cargo test --test basic_protocol_tests"}
        )
        Description = "Basic functionality tests requiring minimal resources"
    }
    "Standard Tests" = @{
        RAM = 4
        CPU = 4
        Disk = 20
        Tests = @(
            @{Name="Integration Tests"; Command="cargo test --test integration_tests"}
            @{Name="Web5 Protocol Tests"; Command="cargo test --test web5_protocols"}
            @{Name="RGB Asset Tests"; Command="cargo test --test rgb_asset_test"}
        )
        Description = "Standard test suite for protocol validation"
    }
    "Full Tests" = @{
        RAM = 8
        CPU = 4
        Disk = 50
        Tests = @(
            @{Name="Layer 2 Tests"; Command="cargo test --test layer2_integration_test"}
            @{Name="ML Operations"; Command="cargo test --test ml_operations_test"}
            @{Name="DLC Tests"; Command="cargo test --features bitcoin_integration --test dlc_tests"}
        )
        Description = "Complete test suite including advanced features"
    }
}
#endregion Configuration

#region UI Functions
function Write-ColorOutput {
    param(
        [System.ConsoleColor]$ForegroundColor,
        [string]$Message
    )
    
    $fc = $host.UI.RawUI.ForegroundColor
    $host.UI.RawUI.ForegroundColor = $ForegroundColor
    Write-Output $Message
    $host.UI.RawUI.ForegroundColor = $fc
}

function Write-DashboardBlock {
    param(
        [string]$Title,
        [string[]]$Content,
        [int]$Width = 50,
        [System.ConsoleColor]$TitleColor = $colors.Cyan
    )
    
    # Draw top border with title
    Write-ColorOutput $TitleColor "$($box.TopLeft)$($box.Horizontal * 2)[$Title]$($box.Horizontal * ($Width - 5 - $Title.Length))$($box.TopRight)"
    
    # Draw content
    foreach ($line in $Content) {
        $padding = $Width - $line.Length - 2
        Write-ColorOutput $TitleColor "$($box.Vertical) $line$(' ' * $padding)$($box.Vertical)"
    }
    
    # Draw bottom border
    Write-ColorOutput $TitleColor "$($box.BottomLeft)$($box.Horizontal * ($Width))$($box.BottomRight)"
}

function Show-Dashboard {
    param(
        [hashtable]$SystemCaps,
        [hashtable]$NetworkServices
    )
    
    Clear-Host
    Write-ColorOutput $colors.Cyan "`nAnya Core Test Dashboard"
    Write-ColorOutput $colors.Cyan "=========================="
    
    # System Block
    $systemContent = @(
        "RAM: $($SystemCaps.RAM)GB / Required: 8GB",
        "CPU: $($SystemCaps.CPU) cores / Required: 4",
        "Disk: $($SystemCaps.Disk)GB / Required: 50GB"
    )
    Write-DashboardBlock "System Status" $systemContent
    
    # Network Block
    $networkContent = $NetworkServices.GetEnumerator() | ForEach-Object {
        $status = if ($_.Value.Available) { "✓" } else { "✗" }
        $installed = if ($_.Value.Installed) { "Installed" } else { "Not Installed" }
        "$status $($_.Key): $installed"
    }
    Write-DashboardBlock "Network Services" $networkContent -TitleColor $colors.Blue
    
    # Tests Block
    $testContent = $testCategories.GetEnumerator() | ForEach-Object -Begin {$i = 1} {
        $reqs = "RAM:$($_.Value.RAM)GB CPU:$($_.Value.CPU) Disk:$($_.Value.Disk)GB"
        "$i. $($_.Key) - $reqs"
        $i++
    }
    Write-DashboardBlock "Available Tests" $testContent -TitleColor $colors.Green
}
#endregion UI Functions

#region System Functions
function Get-SystemCapabilities {
    $systemInfo = Get-CimInstance Win32_ComputerSystem
    $diskInfo = Get-PSDrive C

    return @{
        RAM = [math]::Round($systemInfo.TotalPhysicalMemory/1GB, 2)
        CPU = $systemInfo.NumberOfLogicalProcessors
        Disk = [math]::Round($diskInfo.Free/1GB, 2)
        Description = "Current system capabilities"
    }
}

function Get-NetworkServices {
    $services = @{
        "Bitcoin Core" = @{
            Port = 8333
            InstallScript = "$PSScriptRoot\..\install\components\install_bitcoin.ps1"
            ConfigPath = "$env:APPDATA\Bitcoin\bitcoin.conf"
        }
        "Lightning Network" = @{
            Port = 9735
            InstallScript = "$PSScriptRoot\..\install\components\install_lnd.ps1"
            ConfigPath = "$env:APPDATA\Lnd\lnd.conf"
        }
        "RGB Node" = @{
            Port = 3000
            InstallScript = "$PSScriptRoot\..\install\components\install_rgb.ps1"
            ConfigPath = "$env:APPDATA\RGB\config.json"
        }
    }
    
    $results = @{ }
    foreach ($svc in $services.GetEnumerator()) {
        $results[$svc.Key] = @{
            Available = Test-NetConnection -ComputerName localhost -Port $svc.Value.Port -WarningAction SilentlyContinue -InformationLevel Quiet
            Installed = Test-Path $svc.Value.ConfigPath
            InstallScript = $svc.Value.InstallScript
        }
    }
    return $results
}

function Test-NetworkDependencies {
    param($systemCaps)
    
    Write-ColorOutput $colors.Cyan "`nTesting Network Dependencies..."
    
    $networkDevices = @("Bitcoin Node", "Lightning Node", "RGB Node")
    $results = @{ }
    
    foreach ($device in $networkDevices) {
        Write-ColorOutput $colors.Yellow "`nChecking $device..."
        $results[$device] = Test-NetworkDevice -deviceType $device
        
        if (-not $results[$device].Available) {
            Write-ColorOutput $colors.Yellow "Would you like to initialize $device? (Y/N)"
            $response = Read-Host
            if ($response -eq 'Y') {
                Write-ColorOutput $colors.Yellow "Initializing $device..."
                Initialize-NetworkDevice -deviceType $device
                $results[$device] = Test-NetworkDevice -deviceType $device
            }
        }
        
        # Display results
        $status = if ($results[$device].Available) { "Available" } else { "Not Available" }
        $color = if ($results[$device].Available) { $colors.Green } else { $colors.Red }
        Write-ColorOutput $color "$device Status: $status"
        
        if ($results[$device].Available) {
            Write-ColorOutput $colors.White "  Services: $($results[$device].Services.Count) found"
            Write-ColorOutput $colors.White "  Config: $($results[$device].ConfigStatus)"
        }
    }
    
    return $results
}

function Install-NetworkService {
    param(
        [string]$ServiceName,
        [hashtable]$ServiceInfo
    )
    
    if (-not $ServiceInfo.Installed) {
        Write-ColorOutput $colors.Yellow "Installing $ServiceName..."
        if (Test-Path $ServiceInfo.InstallScript) {
            & $ServiceInfo.InstallScript
            return $true
        } else {
            Write-ColorOutput $colors.Red "Installation script not found: $($ServiceInfo.InstallScript)"
            return $false
        }
    }
    return $true
}
#endregion System Functions

#region Test Functions
function Show-TestAvailability {
    param($systemCaps)
    
    Write-ColorOutput $colors.Cyan "`nSystem Capabilities:"
    Write-ColorOutput $colors.White ("RAM: {0:N2}GB" -f $systemCaps.RAM)
    Write-ColorOutput $colors.White "CPU Cores: $($systemCaps.CPU)"
    Write-ColorOutput $colors.White ("Free Disk Space: {0:N2}GB" -f $systemCaps.Disk)

    Write-ColorOutput $colors.Cyan "`nAvailable Test Categories:"
    $i = 1
    foreach ($category in $testCategories.GetEnumerator()) {
        $canRun = ($systemCaps.RAM -ge $category.Value.RAM) -and 
                 ($systemCaps.CPU -ge $category.Value.CPU) -and 
                 ($systemCaps.Disk -ge $category.Value.Disk)
        
        $color = if ($canRun) { $colors.Green } else { $colors.Yellow }
        $status = if ($canRun) { "AVAILABLE" } else { "INSUFFICIENT RESOURCES" }
        
        Write-ColorOutput $color "`n$i. $($category.Key) [$status]"
        Write-ColorOutput $colors.White "   Description: $($category.Value.Description)"
        Write-ColorOutput $colors.White ("   Requirements: RAM:{0}GB, CPU:{1} cores, Disk:{2}GB" -f $category.Value.RAM, $category.Value.CPU, $category.Value.Disk)
        
        if ($canRun) {
            Write-ColorOutput $colors.White "   Available Tests:"
            foreach ($test in $category.Value.Tests) {
                Write-ColorOutput $colors.White "     - $($test.Name)"
            }
        }
        $i++
    }
}

function Get-UserTestSelection {
    param($systemCaps)
    
    $availableCategories = $testCategories.GetEnumerator() | Where-Object {
        $_.Value.RAM -le $systemCaps.RAM -and 
        $_.Value.CPU -le $systemCaps.CPU -and 
        $_.Value.Disk -le $systemCaps.Disk
    }

    Write-ColorOutput $colors.Yellow "`nSelect test categories to run (comma-separated numbers):"
    Write-ColorOutput $colors.White "Enter '?' for help or 'all' for all available tests"
    
    while ($true) {
        $selection = Read-Host "Selection"
        
        if ($selection -eq '?') {
            Write-ColorOutput $colors.Cyan "`nHelp:"
            Write-ColorOutput $colors.White "- Enter single number (e.g. '1') for one category"
            Write-ColorOutput $colors.White "- Enter multiple numbers separated by commas (e.g. '1,2')"
            Write-ColorOutput $colors.White "- Enter 'all' to run all available tests"
            Write-ColorOutput $colors.White "- Enter 'q' to quit"
            continue
        }
        
        if ($selection -eq 'q') {
            exit 0
        }
        
        if ($selection -eq 'all') {
            return $availableCategories
        }
        
        try {
            $selected = $selection.Split(',') | ForEach-Object { $_.Trim() }
            $categoryMap = @{ }
            $i = 1
            foreach ($category in $testCategories.GetEnumerator()) {
                $categoryMap[$i] = $category.Value
                $i++
            }
            
            return $selected | ForEach-Object { 
                $num = [int]$_
                if ($num -lt 1 -or $num -gt $categoryMap.Count) {
                    throw "Invalid selection: $num. Please enter numbers between 1 and $($categoryMap.Count)"
                }
                $categoryMap[$num]
            }
        }
        catch {
            Write-ColorOutput $colors.Red "Error: $_"
            Write-ColorOutput $colors.Yellow "Please try again or enter '?' for help"
            continue
        }
    }
}

function Run-TestCategory {
    param(
        $category,
        $logFile
    )
    
    $results = @{
        Passed = 0
        Failed = 0
        Skipped = 0
    }

    foreach ($test in $category.Tests) {
        Write-ColorOutput $colors.Yellow "`nRunning: $($test.Name)"
        try {
            Invoke-Expression $test.Command
            if ($LASTEXITCODE -eq 0) {
                $results.Passed++
                Write-ColorOutput $colors.Green "✓ $($test.Name) passed"
            } else {
                $results.Failed++
                Write-ColorOutput $colors.Red "✗ $($test.Name) failed"
            }
        } catch {
            $results.Failed++
            Write-ColorOutput $colors.Red "✗ $($test.Name) error: $_"
            $_ | Out-File -Append $logFile
        }
    }
    
    return $results
}
#endregion Test Functions

#region Main Execution
try {
    # Initialize
    $timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
    $logFile = "test_run_${timestamp}.log"
    $systemCaps = Get-SystemCapabilities
    $networkServices = Get-NetworkServices

    # Main Loop
    while ($true) {
        Show-Dashboard $systemCaps $networkServices
        
        Write-ColorOutput $colors.Yellow "`nOptions:"
        @(
            "1. Run Tests",
            "2. Install Missing Services",
            "3. Refresh Status",
            "4. Exit"
        ) | ForEach-Object { Write-ColorOutput $colors.White $_ }
        
        switch (Read-Host "`nSelect option") {
            "1" { 
                # Test Execution
                $selectedCategories = Get-UserTestSelection $systemCaps
                $totalResults = @{Passed = 0; Failed = 0; Skipped = 0}
                
                foreach ($category in $selectedCategories) {
                    $results = Run-TestCategory $category $logFile
                    $totalResults.Passed += $results.Passed
                    $totalResults.Failed += $results.Failed
                    $totalResults.Skipped += $results.Skipped
                }
                
                # Show Results
                Write-DashboardBlock "Test Results" @(
                    "Passed: $($totalResults.Passed)",
                    "Failed: $($totalResults.Failed)",
                    "Skipped: $($totalResults.Skipped)"
                ) -TitleColor $(if ($totalResults.Failed -eq 0) { $colors.Green } else { $colors.Red })
            }
            "2" { 
                # Service Installation
                $networkServices.GetEnumerator() | Where-Object {
                    -not $_.Value.Available -or -not $_.Value.Installed
                } | ForEach-Object {
                    if ((Read-Host "Install $($_.Key)? (Y/N)") -eq 'Y') {
                        Install-NetworkService $_.Key $_.Value
                    }
                }
                $networkServices = Get-NetworkServices
            }
            "3" { 
                # Refresh Status
                $systemCaps = Get-SystemCapabilities
                $networkServices = Get-NetworkServices
            }
            "4" { exit 0 }
        }
    }
} catch {
    Write-ColorOutput $colors.Red "`nError: $_"
    Write-ColorOutput $colors.Red "Check the log file for details: $logFile"
    $_ | Out-File -Append $logFile
    exit 1
}
#endregion Main Execution