neo-devpack-solidity 0.22.0

Production-focused Solidity-to-NeoVM compilation system
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
using System.Numerics;
using System.Diagnostics;
using System.Runtime.ExceptionServices;
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services;
using Neo.Sol.Runtime.ABI;
using Neo.Sol.Runtime.Context;
using ExecutionContext = Neo.Sol.Runtime.Context.ExecutionContext;
using NeoFrameworkRuntime = Neo.SmartContract.Framework.Services.Runtime;

namespace Neo.Sol.Runtime.Exceptions;

/// <summary>
/// Production-grade exception handling system for EVM-compatible error propagation
/// Provides comprehensive error handling, debugging support, and error recovery
/// </summary>
public sealed class EvmExceptionHandler : IDisposable
{
    private readonly Stack<ExceptionFrame> _exceptionStack = new();
    private readonly Dictionary<string, ErrorHandler> _errorHandlers = new();
    private readonly Stopwatch _executionTimer = new();
    private bool _disposed = false;
    
    // Performance tracking
    private ulong _exceptionsHandled = 0;
    private ulong _recoveredErrors = 0;
    private readonly Dictionary<Type, ulong> _exceptionTypeCount = new();
    
    // Configuration
    private readonly ExceptionConfig _config;
    
    public EvmExceptionHandler(ExceptionConfig? config = null)
    {
        _config = config ?? ExceptionConfig.Default;
        _executionTimer.Start();
        RegisterStandardErrorHandlers();
    }

    private static void TryLog(string message)
    {
        try
        {
            NeoFrameworkRuntime.Log(message);
        }
        catch
        {
            // Ignore logging failures outside a Neo VM host.
        }
    }
    
    /// <summary>
    /// Execute operation with comprehensive exception handling
    /// </summary>
    /// <typeparam name="T">Return type</typeparam>
    /// <param name="operation">Operation to execute</param>
    /// <param name="context">Execution context</param>
    /// <param name="errorMessage">Custom error message</param>
    /// <returns>Operation result with error handling</returns>
    public ExecutionResult<T> Execute<T>(Func<T> operation, ExecutionContext? context = null, string errorMessage = "")
    {
        if (_disposed)
            throw new ObjectDisposedException(nameof(EvmExceptionHandler));
            
        var frame = PushFrame(operation.Method.Name, context);
        
        try
        {
            var result = operation();
            frame.Success = true;
            return ExecutionResult<T>.Success(result, frame.ExecutionTime);
        }
        catch (Exception ex)
        {
            return HandleException<T>(ex, frame, errorMessage);
        }
        finally
        {
            PopFrame();
        }
    }
    
    /// <summary>
    /// Execute async operation with exception handling
    /// </summary>
    /// <typeparam name="T">Return type</typeparam>
    /// <param name="operation">Async operation to execute</param>
    /// <param name="context">Execution context</param>
    /// <param name="errorMessage">Custom error message</param>
    /// <returns>Operation result with error handling</returns>
    public async Task<ExecutionResult<T>> ExecuteAsync<T>(Func<Task<T>> operation, ExecutionContext? context = null, string errorMessage = "")
    {
        if (_disposed)
            throw new ObjectDisposedException(nameof(EvmExceptionHandler));
            
        var frame = PushFrame(operation.Method.Name, context);
        
        try
        {
            var result = await operation();
            frame.Success = true;
            return ExecutionResult<T>.Success(result, frame.ExecutionTime);
        }
        catch (Exception ex)
        {
            return HandleException<T>(ex, frame, errorMessage);
        }
        finally
        {
            PopFrame();
        }
    }
    
    /// <summary>
    /// Handle EVM revert with reason
    /// </summary>
    /// <param name="reason">Revert reason</param>
    /// <param name="context">Execution context</param>
    /// <exception cref="EvmRevertException">Always throws revert exception</exception>
    public void Revert(string reason = "", ExecutionContext? context = null)
    {
        var revertData = string.IsNullOrEmpty(reason) ? Array.Empty<byte>() : EncodeRevertReason(reason);
        var exception = new EvmRevertException(reason, revertData);
        
        LogException(exception, context);
        throw exception;
    }
    
    /// <summary>
    /// Handle EVM revert with custom data
    /// </summary>
    /// <param name="errorData">Custom error data</param>
    /// <param name="context">Execution context</param>
    /// <exception cref="EvmRevertException">Always throws revert exception</exception>
    public void RevertWithData(byte[] errorData, ExecutionContext? context = null)
    {
        var exception = new EvmRevertException("Custom revert", errorData);
        
        LogException(exception, context);
        throw exception;
    }
    
    /// <summary>
    /// Handle require assertion
    /// </summary>
    /// <param name="condition">Condition to check</param>
    /// <param name="message">Error message if condition fails</param>
    /// <param name="context">Execution context</param>
    /// <exception cref="EvmRevertException">Throws if condition is false</exception>
    public void Require(bool condition, string message = "Requirement failed", ExecutionContext? context = null)
    {
        if (!condition)
        {
            Revert(message, context);
        }
    }
    
    /// <summary>
    /// Handle assert statement (should never fail in correct code)
    /// </summary>
    /// <param name="condition">Condition to assert</param>
    /// <param name="context">Execution context</param>
    /// <exception cref="EvmAssertException">Throws if assertion fails</exception>
    public void Assert(bool condition, ExecutionContext? context = null)
    {
        if (!condition)
        {
            var exception = new EvmAssertException("Assertion failed");
            LogException(exception, context);
            throw exception;
        }
    }
    
    /// <summary>
    /// Try to recover from error using registered handlers
    /// </summary>
    /// <typeparam name="T">Return type</typeparam>
    /// <param name="operation">Operation to try</param>
    /// <param name="fallbackValue">Fallback value on failure</param>
    /// <param name="context">Execution context</param>
    /// <returns>Result or fallback value</returns>
    public T TryRecover<T>(Func<T> operation, T fallbackValue, ExecutionContext? context = null)
    {
        try
        {
            return operation();
        }
        catch (Exception ex)
        {
            LogException(ex, context);
            
            if (TryHandleError(ex, out var recoveredValue) && recoveredValue is T result)
            {
                Interlocked.Increment(ref _recoveredErrors);
                return result;
            }
            
            return fallbackValue;
        }
    }
    
    /// <summary>
    /// Register custom error handler
    /// </summary>
    /// <param name="errorType">Error type or pattern</param>
    /// <param name="handler">Error handler</param>
    public void RegisterErrorHandler(string errorType, ErrorHandler handler)
    {
        _errorHandlers[errorType] = handler;
    }
    
    /// <summary>
    /// Get current call stack
    /// </summary>
    /// <returns>Array of exception frames</returns>
    public ExceptionFrame[] GetCallStack()
    {
        return _exceptionStack.ToArray();
    }
    
    /// <summary>
    /// Get exception handling statistics
    /// </summary>
    /// <returns>Exception statistics</returns>
    public ExceptionStats GetStats()
    {
        return new ExceptionStats
        {
            ExceptionsHandled = _exceptionsHandled,
            RecoveredErrors = _recoveredErrors,
            CallStackDepth = (uint)_exceptionStack.Count,
            ExecutionTime = (ulong)_executionTimer.ElapsedMilliseconds,
            ExceptionTypeBreakdown = new Dictionary<string, ulong>(_exceptionTypeCount.ToDictionary(kvp => kvp.Key.Name, kvp => kvp.Value)),
            ErrorHandlerCount = (uint)_errorHandlers.Count
        };
    }
    
    /// <summary>
    /// Clear exception statistics
    /// </summary>
    public void ClearStats()
    {
        _exceptionsHandled = 0;
        _recoveredErrors = 0;
        _exceptionTypeCount.Clear();
    }
    
    private ExceptionFrame PushFrame(string operation, ExecutionContext? context)
    {
        var frame = new ExceptionFrame
        {
            Operation = operation,
            Context = context,
            StartTime = (ulong)_executionTimer.ElapsedMilliseconds,
            CallDepth = (uint)_exceptionStack.Count
        };
        
        _exceptionStack.Push(frame);
        return frame;
    }
    
    private void PopFrame()
    {
        if (_exceptionStack.Count > 0)
        {
            var frame = _exceptionStack.Pop();
            frame.EndTime = (ulong)_executionTimer.ElapsedMilliseconds;
        }
    }
    
    private ExecutionResult<T> HandleException<T>(Exception exception, ExceptionFrame frame, string customMessage)
    {
        frame.Exception = exception;
        frame.Success = false;
        frame.EndTime = (ulong)_executionTimer.ElapsedMilliseconds;
        
        Interlocked.Increment(ref _exceptionsHandled);
        TrackExceptionType(exception.GetType());
        
        LogException(exception, frame.Context);
        
        // Try to handle the error
        if (TryHandleError(exception, out var recoveredValue) && recoveredValue is T result)
        {
            Interlocked.Increment(ref _recoveredErrors);
            return ExecutionResult<T>.Success(result, frame.ExecutionTime);
        }
        
        // Create detailed error information
        var error = CreateErrorInfo(exception, frame, customMessage);
        return ExecutionResult<T>.Failure(error, frame.ExecutionTime);
    }
    
    private bool TryHandleError(Exception exception, out object? recoveredValue)
    {
        recoveredValue = null;
        
        // Try specific error handler first
        var exceptionTypeName = exception.GetType().Name;
        if (_errorHandlers.TryGetValue(exceptionTypeName, out var specificHandler))
        {
            return specificHandler.TryHandle(exception, out recoveredValue);
        }
        
        // Try generic handlers
        foreach (var handler in _errorHandlers.Values)
        {
            if (handler.CanHandle(exception) && handler.TryHandle(exception, out recoveredValue))
            {
                return true;
            }
        }
        
        return false;
    }
    
    private ErrorInfo CreateErrorInfo(Exception exception, ExceptionFrame frame, string customMessage)
    {
        var message = !string.IsNullOrEmpty(customMessage) ? customMessage : exception.Message;
        
        return new ErrorInfo
        {
            Type = exception.GetType().Name,
            Message = message,
            StackTrace = exception.StackTrace ?? "",
            Operation = frame.Operation,
            CallDepth = frame.CallDepth,
            ExecutionTime = frame.ExecutionTime,
            Context = GetContextInfo(frame.Context),
            Data = GetExceptionData(exception),
            Severity = GetSeverityLevel(exception)
        };
    }
    
    private void LogException(Exception exception, ExecutionContext? context)
    {
        if (_config.EnableLogging)
        {
            var contextInfo = GetContextInfo(context);
            var logMessage = $"Exception: {exception.GetType().Name} - {exception.Message}";
            
            if (!string.IsNullOrEmpty(contextInfo.ContractHash))
            {
                logMessage += $" | Contract: {contextInfo.ContractHash}";
            }
            
            if (_config.LogStackTrace)
            {
                logMessage += $" | Stack: {exception.StackTrace}";
            }
            
            TryLog(logMessage);
        }
    }
    
    private ContextInfo GetContextInfo(ExecutionContext? context)
    {
        if (context == null)
        {
            return new ContextInfo();
        }
        
        return new ContextInfo
        {
            ContractHash = context.Msg.Sender.ToString(),
            CallDepth = 0,
            GasConsumed = 0,
            ExecutionTime = 0
        };
    }
    
    private Dictionary<string, object> GetExceptionData(Exception exception)
    {
        var data = new Dictionary<string, object>();
        
        if (exception.Data.Count > 0)
        {
            foreach (var key in exception.Data.Keys)
            {
                if (key != null)
                {
                    data[key.ToString()!] = exception.Data[key] ?? "";
                }
            }
        }
        
        // Add EVM-specific data
        switch (exception)
        {
            case EvmRevertException revertEx:
                data["revertData"] = Convert.ToHexString(revertEx.RevertData);
                break;
            case EvmOutOfGasException gasEx:
                data["gasUsed"] = gasEx.GasUsed;
                data["gasLimit"] = gasEx.GasLimit;
                break;
            case EvmStackOverflowException stackEx:
                data["stackDepth"] = stackEx.StackDepth;
                data["maxDepth"] = stackEx.MaxDepth;
                break;
        }
        
        return data;
    }
    
    private ErrorSeverity GetSeverityLevel(Exception exception)
    {
        return exception switch
        {
            EvmRevertException => ErrorSeverity.Expected,
            EvmAssertException => ErrorSeverity.Critical,
            EvmOutOfGasException => ErrorSeverity.High,
            ArgumentException => ErrorSeverity.Medium,
            InvalidOperationException => ErrorSeverity.Medium,
            UnauthorizedAccessException => ErrorSeverity.High,
            OutOfMemoryException => ErrorSeverity.Critical,
            StackOverflowException => ErrorSeverity.Critical,
            _ => ErrorSeverity.Low
        };
    }
    
    private void TrackExceptionType(Type exceptionType)
    {
        if (_exceptionTypeCount.ContainsKey(exceptionType))
        {
            _exceptionTypeCount[exceptionType]++;
        }
        else
        {
            _exceptionTypeCount[exceptionType] = 1;
        }
    }
    
    private byte[] EncodeRevertReason(string reason)
    {
        // Encode as Error(string) ABI format
        var errorSignature = "Error(string)";
        return AbiEncoder.EncodeCall(errorSignature, reason);
    }
    
    private void RegisterStandardErrorHandlers()
    {
        // Register handler for common gas exhaustion
        RegisterErrorHandler("EvmOutOfGasException", new GasExhaustionHandler());
        
        // Register handler for memory issues
        RegisterErrorHandler("OutOfMemoryException", new MemoryHandler());
        
        // Register handler for arithmetic errors
        RegisterErrorHandler("OverflowException", new ArithmeticHandler());
        RegisterErrorHandler("DivideByZeroException", new ArithmeticHandler());
        
        // Register handler for access violations
        RegisterErrorHandler("UnauthorizedAccessException", new AccessHandler());
    }
    
    public void Dispose()
    {
        if (!_disposed)
        {
            _executionTimer.Stop();
            _exceptionStack.Clear();
            _errorHandlers.Clear();
            _exceptionTypeCount.Clear();
            _disposed = true;
        }
    }
}

/// <summary>
/// EVM-specific exception types
/// </summary>

/// <summary>
/// Exception thrown when EVM revert is executed
/// </summary>
public sealed class EvmRevertException : Exception
{
    public byte[] RevertData { get; }
    
    public EvmRevertException(string message, byte[] revertData) : base(message)
    {
        RevertData = revertData;
    }
}

/// <summary>
/// Exception thrown when EVM assert fails
/// </summary>
public sealed class EvmAssertException : Exception
{
    public EvmAssertException(string message) : base(message) { }
}

/// <summary>
/// Exception thrown when running out of gas
/// </summary>
public sealed class EvmOutOfGasException : Exception
{
    public ulong GasUsed { get; }
    public ulong GasLimit { get; }
    
    public EvmOutOfGasException(ulong gasUsed, ulong gasLimit) 
        : base($"Out of gas: used {gasUsed}, limit {gasLimit}")
    {
        GasUsed = gasUsed;
        GasLimit = gasLimit;
    }
}

/// <summary>
/// Exception thrown on stack overflow
/// </summary>
public sealed class EvmStackOverflowException : Exception
{
    public uint StackDepth { get; }
    public uint MaxDepth { get; }
    
    public EvmStackOverflowException(uint stackDepth, uint maxDepth)
        : base($"Stack overflow: depth {stackDepth}, max {maxDepth}")
    {
        StackDepth = stackDepth;
        MaxDepth = maxDepth;
    }
}

/// <summary>
/// Exception thrown when call depth exceeded
/// </summary>
public sealed class EvmCallDepthException : Exception
{
    public uint CallDepth { get; }
    public uint MaxCallDepth { get; }
    
    public EvmCallDepthException(uint callDepth, uint maxCallDepth)
        : base($"Call depth exceeded: {callDepth} > {maxCallDepth}")
    {
        CallDepth = callDepth;
        MaxCallDepth = maxCallDepth;
    }
}

/// <summary>
/// Data structures for exception handling
/// </summary>

/// <summary>
/// Exception frame for call stack tracking
/// </summary>
public sealed class ExceptionFrame
{
    public string Operation { get; set; } = "";
    public ExecutionContext? Context { get; set; }
    public ulong StartTime { get; set; }
    public ulong EndTime { get; set; }
    public uint CallDepth { get; set; }
    public Exception? Exception { get; set; }
    public bool Success { get; set; }
    
    public ulong ExecutionTime => EndTime > StartTime ? EndTime - StartTime : 0;
}

/// <summary>
/// Comprehensive error information
/// </summary>
public sealed record ErrorInfo
{
    public string Type { get; init; } = "";
    public string Message { get; init; } = "";
    public string StackTrace { get; init; } = "";
    public string Operation { get; init; } = "";
    public uint CallDepth { get; init; }
    public ulong ExecutionTime { get; init; }
    public ContextInfo Context { get; init; } = new();
    public Dictionary<string, object> Data { get; init; } = new();
    public ErrorSeverity Severity { get; init; }
}

/// <summary>
/// Execution context information
/// </summary>
public sealed record ContextInfo
{
    public string ContractHash { get; init; } = "";
    public uint CallDepth { get; init; }
    public ulong GasConsumed { get; init; }
    public ulong ExecutionTime { get; init; }
}

/// <summary>
/// Exception handling statistics
/// </summary>
public sealed record ExceptionStats
{
    public ulong ExceptionsHandled { get; init; }
    public ulong RecoveredErrors { get; init; }
    public uint CallStackDepth { get; init; }
    public ulong ExecutionTime { get; init; }
    public Dictionary<string, ulong> ExceptionTypeBreakdown { get; init; } = new();
    public uint ErrorHandlerCount { get; init; }
}

/// <summary>
/// Execution result with error handling
/// </summary>
public sealed record ExecutionResult<T>
{
    public bool IsSuccess { get; init; }
    public T? Value { get; init; }
    public ErrorInfo? Error { get; init; }
    public ulong ExecutionTime { get; init; }
    
    public static ExecutionResult<T> Success(T value, ulong executionTime = 0)
        => new() { IsSuccess = true, Value = value, ExecutionTime = executionTime };
        
    public static ExecutionResult<T> Failure(ErrorInfo error, ulong executionTime = 0)
        => new() { IsSuccess = false, Error = error, ExecutionTime = executionTime };
}

/// <summary>
/// Error severity levels
/// </summary>
public enum ErrorSeverity
{
    Low,        // Minor issues, easily recoverable
    Medium,     // Moderate issues, may need attention
    High,       // Serious issues, likely to impact execution
    Critical,   // Severe issues, execution cannot continue
    Expected    // Expected errors like revert
}

/// <summary>
/// Configuration for exception handling
/// </summary>
public sealed record ExceptionConfig
{
    public bool EnableLogging { get; init; } = true;
    public bool LogStackTrace { get; init; } = true;
    public bool EnableRecovery { get; init; } = true;
    public uint MaxCallDepth { get; init; } = 1024;
    public uint MaxStackSize { get; init; } = 1024;
    
    public static ExceptionConfig Default => new();
}

/// <summary>
/// Base error handler interface
/// </summary>
public abstract class ErrorHandler
{
    public abstract bool CanHandle(Exception exception);
    public abstract bool TryHandle(Exception exception, out object? recoveredValue);
}

/// <summary>
/// Standard error handlers
/// </summary>

public sealed class GasExhaustionHandler : ErrorHandler
{
    public override bool CanHandle(Exception exception)
        => exception is EvmOutOfGasException;
        
    public override bool TryHandle(Exception exception, out object? recoveredValue)
    {
        recoveredValue = null;
        
        if (exception is EvmOutOfGasException gasEx)
        {
            // Log gas exhaustion but cannot recover
            try
            {
                NeoFrameworkRuntime.Log($"Gas exhausted: {gasEx.GasUsed}/{gasEx.GasLimit}");
            }
            catch
            {
            }
            return false;
        }
        
        return false;
    }
}

public sealed class MemoryHandler : ErrorHandler
{
    public override bool CanHandle(Exception exception)
        => exception is OutOfMemoryException;
        
    public override bool TryHandle(Exception exception, out object? recoveredValue)
    {
        recoveredValue = null;
        
        // Trigger garbage collection
        GC.Collect();
        GC.WaitForPendingFinalizers();
        
        try
        {
            NeoFrameworkRuntime.Log("Memory exhausted - triggered GC");
        }
        catch
        {
        }
        return false;
    }
}

public sealed class ArithmeticHandler : ErrorHandler
{
    public override bool CanHandle(Exception exception)
        => exception is OverflowException || exception is DivideByZeroException;
        
    public override bool TryHandle(Exception exception, out object? recoveredValue)
    {
        recoveredValue = exception switch
        {
            OverflowException => BigInteger.Zero,
            DivideByZeroException => BigInteger.Zero,
            _ => null
        };
        
        return recoveredValue != null;
    }
}

public sealed class AccessHandler : ErrorHandler
{
    public override bool CanHandle(Exception exception)
        => exception is UnauthorizedAccessException;
        
        public override bool TryHandle(Exception exception, out object? recoveredValue)
        {
            recoveredValue = null;
            try
            {
                NeoFrameworkRuntime.Log($"Access denied: {exception.Message}");
            }
            catch
            {
            }
            return false;
        }
}