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
using System.Numerics;
using System.Collections.Concurrent;
using System.Diagnostics;
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services;
using Neo.Sol.Runtime.Crypto;
using NeoFrameworkStorage = Neo.SmartContract.Framework.Services.Storage;

namespace Neo.Sol.Runtime.Storage;

/// <summary>
/// EVM-compatible storage manager preserving Solidity storage layout semantics
/// Maps EVM storage slots to Neo storage keys with collision resistance
/// Production-grade implementation with advanced caching, metrics, and optimization
/// </summary>
public sealed class StorageManager : IDisposable
{
    private readonly StorageContext _context;
    private readonly ConcurrentDictionary<BigInteger, CachedSlot> _cache = new();
    private readonly ConcurrentHashSet<BigInteger> _modifiedSlots = new();
    private readonly ReaderWriterLockSlim _lock = new();
    private readonly Timer _cacheCleanupTimer;
    private readonly Stopwatch _accessTimer = new();
    private bool _disposed = false;
    
    // Performance tracking
    private ulong _cacheHits = 0;
    private ulong _cacheMisses = 0;
    private ulong _storageReads = 0;
    private ulong _storageWrites = 0;
    private ulong _cacheEvictions = 0;
    
    // Storage layout constants
    private const int SLOT_SIZE = 32; // 32-byte storage slots
    private const int MAX_PACKED_SLOTS = 8; // Maximum number of variables that can be packed
    private const int MAX_CACHE_SIZE = 10000; // Maximum cached slots
    private const int CACHE_CLEANUP_INTERVAL_MS = 60000; // Cleanup every minute
    private const ulong CACHE_TTL_MS = 300000; // 5 minute TTL for cached slots
    
    public StorageManager(StorageContext context)
    {
        _context = context ?? throw new ArgumentNullException(nameof(context));
        _accessTimer.Start();
        _cacheCleanupTimer = new Timer(CleanupCache, null, CACHE_CLEANUP_INTERVAL_MS, CACHE_CLEANUP_INTERVAL_MS);
    }
    
    /// <summary>
    /// Generate Neo storage key from EVM storage slot
    /// Uses keccak256 to prevent collisions and maintain determinism
    /// </summary>
    /// <param name="slot">EVM storage slot</param>
    /// <returns>Neo storage key</returns>
    private byte[] GenerateStorageKey(BigInteger slot)
    {
        // Prefix with "evm_storage_" to separate from other Neo storage
        var prefix = "evm_storage_"u8.ToArray();
        var slotBytes = slot.ToByteArray(isUnsigned: true, isBigEndian: true);
        
        // Pad slot to 32 bytes for consistent key generation
        var paddedSlot = new byte[SLOT_SIZE];
        if (slotBytes.Length <= SLOT_SIZE)
        {
            Array.Copy(slotBytes, 0, paddedSlot, SLOT_SIZE - slotBytes.Length, slotBytes.Length);
        }
        else
        {
            Array.Copy(slotBytes, slotBytes.Length - SLOT_SIZE, paddedSlot, 0, SLOT_SIZE);
        }
        
        var combined = new byte[prefix.Length + paddedSlot.Length];
        Array.Copy(prefix, 0, combined, 0, prefix.Length);
        Array.Copy(paddedSlot, 0, combined, prefix.Length, paddedSlot.Length);
        
        // Use keccak256 for consistent, collision-resistant key generation
        return CryptoLib.Keccak256(combined);
    }
    
    /// <summary>
    /// Load value from storage slot with advanced caching
    /// </summary>
    /// <param name="slot">Storage slot number</param>
    /// <returns>32-byte value (zero if not set)</returns>
    /// <exception cref="ObjectDisposedException">Storage manager disposed</exception>
    public byte[] Load(BigInteger slot)
    {
        if (_disposed)
            throw new ObjectDisposedException(nameof(StorageManager));
            
        var currentTime = (ulong)_accessTimer.ElapsedMilliseconds;
        
        // Check cache first
        if (_cache.TryGetValue(slot, out var cachedSlot))
        {
            cachedSlot.LastAccessed = currentTime;
            cachedSlot.AccessCount++;
            Interlocked.Increment(ref _cacheHits);
            return cachedSlot.Value;
        }
        
        Interlocked.Increment(ref _cacheMisses);
        Interlocked.Increment(ref _storageReads);
        
        var key = GenerateStorageKey(slot);
        var value = NeoFrameworkStorage.Get(_context, key);
        
        // EVM storage always returns 32 bytes, pad with zeros if needed
        var result = new byte[SLOT_SIZE];
        if (value != null && value.Length > 0)
        {
            var valueBytes = (byte[])value;
            if (value.Length <= SLOT_SIZE)
            {
                Array.Copy(valueBytes, 0, result, SLOT_SIZE - value.Length, value.Length);
            }
            else
            {
                Array.Copy(valueBytes, value.Length - SLOT_SIZE, result, 0, SLOT_SIZE);
            }
        }
        
        // Cache the result with metadata
        var cached = new CachedSlot
        {
            Value = result,
            LastAccessed = currentTime,
            AccessCount = 1,
            IsModified = false
        };
        
        // Implement cache eviction if necessary
        if (_cache.Count >= MAX_CACHE_SIZE)
        {
            EvictOldestCacheEntry();
        }
        
        _cache.TryAdd(slot, cached);
        return result;
    }
    
    /// <summary>
    /// Load BigInteger from storage slot
    /// </summary>
    /// <param name="slot">Storage slot number</param>
    /// <returns>BigInteger value</returns>
    public BigInteger LoadBigInteger(BigInteger slot)
    {
        var bytes = Load(slot);
        return new BigInteger(bytes, isUnsigned: true, isBigEndian: true);
    }
    
    /// <summary>
    /// Store value to storage slot with write-through caching
    /// </summary>
    /// <param name="slot">Storage slot number</param>
    /// <param name="value">32-byte value to store</param>
    /// <exception cref="ArgumentException">Invalid value size</exception>
    /// <exception cref="ObjectDisposedException">Storage manager disposed</exception>
    public void Store(BigInteger slot, byte[] value)
    {
        if (_disposed)
            throw new ObjectDisposedException(nameof(StorageManager));
            
        if (value.Length != SLOT_SIZE)
            throw new ArgumentException($"Value must be exactly {SLOT_SIZE} bytes");
        
        var currentTime = (ulong)_accessTimer.ElapsedMilliseconds;
        Interlocked.Increment(ref _storageWrites);
        
        // Update cache with metadata
        var cachedSlot = new CachedSlot
        {
            Value = (byte[])value.Clone(),
            LastAccessed = currentTime,
            AccessCount = _cache.TryGetValue(slot, out var existing) ? existing.AccessCount + 1 : 1,
            IsModified = true
        };
        
        _cache.AddOrUpdate(slot, cachedSlot, (_, _) => cachedSlot);
        _modifiedSlots.Add(slot);
        
        // Store to Neo storage immediately (write-through)
        var key = GenerateStorageKey(slot);
        
        // Optimize storage: don't store zero values
        if (IsZero(value))
        {
            NeoFrameworkStorage.Delete(_context, key);
        }
        else
        {
            // Compress storage if beneficial
            var compressedValue = CompressValueIfBeneficial(value);
            NeoFrameworkStorage.Put(_context, key, compressedValue);
        }
    }
    
    /// <summary>
    /// Store BigInteger to storage slot
    /// </summary>
    /// <param name="slot">Storage slot number</param>
    /// <param name="value">BigInteger value</param>
    public void Store(BigInteger slot, BigInteger value)
    {
        var bytes = value.ToByteArray(isUnsigned: true, isBigEndian: true);
        var paddedBytes = new byte[SLOT_SIZE];
        
        if (bytes.Length <= SLOT_SIZE)
        {
            Array.Copy(bytes, 0, paddedBytes, SLOT_SIZE - bytes.Length, bytes.Length);
        }
        else
        {
            Array.Copy(bytes, bytes.Length - SLOT_SIZE, paddedBytes, 0, SLOT_SIZE);
        }
        
        Store(slot, paddedBytes);
    }
    
    /// <summary>
    /// Calculate storage slot for array element
    /// Based on Solidity's array storage layout
    /// </summary>
    /// <param name="arraySlot">Base slot of the array</param>
    /// <param name="index">Array index</param>
    /// <returns>Storage slot for the element</returns>
    public static BigInteger CalculateArrayElementSlot(BigInteger arraySlot, BigInteger index)
    {
        // For dynamic arrays: keccak256(arraySlot) + index
        var arraySlotBytes = arraySlot.ToByteArray(isUnsigned: true, isBigEndian: true);
        var paddedSlot = new byte[SLOT_SIZE];
        
        if (arraySlotBytes.Length <= SLOT_SIZE)
        {
            Array.Copy(arraySlotBytes, 0, paddedSlot, SLOT_SIZE - arraySlotBytes.Length, arraySlotBytes.Length);
        }
        
        var baseSlotHash = CryptoLib.Keccak256(paddedSlot);
        var baseSlot = new BigInteger(baseSlotHash, isUnsigned: true, isBigEndian: true);
        
        return baseSlot + index;
    }
    
    /// <summary>
    /// Calculate storage slot for mapping element
    /// Based on Solidity's mapping storage layout
    /// </summary>
    /// <param name="mappingSlot">Base slot of the mapping</param>
    /// <param name="key">Mapping key</param>
    /// <returns>Storage slot for the element</returns>
    public static BigInteger CalculateMappingElementSlot(BigInteger mappingSlot, byte[] key)
    {
        // For mappings: keccak256(key || mappingSlot)
        var mappingSlotBytes = mappingSlot.ToByteArray(isUnsigned: true, isBigEndian: true);
        var paddedSlot = new byte[SLOT_SIZE];
        
        if (mappingSlotBytes.Length <= SLOT_SIZE)
        {
            Array.Copy(mappingSlotBytes, 0, paddedSlot, SLOT_SIZE - mappingSlotBytes.Length, mappingSlotBytes.Length);
        }
        
        // Pad key to 32 bytes
        var paddedKey = new byte[SLOT_SIZE];
        if (key.Length <= SLOT_SIZE)
        {
            Array.Copy(key, 0, paddedKey, SLOT_SIZE - key.Length, key.Length);
        }
        else
        {
            Array.Copy(key, key.Length - SLOT_SIZE, paddedKey, 0, SLOT_SIZE);
        }
        
        var combined = new byte[SLOT_SIZE * 2];
        Array.Copy(paddedKey, 0, combined, 0, SLOT_SIZE);
        Array.Copy(paddedSlot, 0, combined, SLOT_SIZE, SLOT_SIZE);
        
        var hash = CryptoLib.Keccak256(combined);
        return new BigInteger(hash, isUnsigned: true, isBigEndian: true);
    }
    
    /// <summary>
    /// Calculate storage slot for mapping with BigInteger key
    /// </summary>
    /// <param name="mappingSlot">Base slot of the mapping</param>
    /// <param name="key">Mapping key as BigInteger</param>
    /// <returns>Storage slot for the element</returns>
    public static BigInteger CalculateMappingElementSlot(BigInteger mappingSlot, BigInteger key)
    {
        var keyBytes = key.ToByteArray(isUnsigned: true, isBigEndian: true);
        var paddedKey = new byte[SLOT_SIZE];
        
        if (keyBytes.Length <= SLOT_SIZE)
        {
            Array.Copy(keyBytes, 0, paddedKey, SLOT_SIZE - keyBytes.Length, keyBytes.Length);
        }
        else
        {
            Array.Copy(keyBytes, keyBytes.Length - SLOT_SIZE, paddedKey, 0, SLOT_SIZE);
        }
        
        return CalculateMappingElementSlot(mappingSlot, paddedKey);
    }
    
    /// <summary>
    /// Check if byte array represents zero
    /// </summary>
    /// <param name="value">Byte array to check</param>
    /// <returns>True if all bytes are zero</returns>
    private static bool IsZero(byte[] value)
    {
        return value.All(b => b == 0);
    }
    
    /// <summary>
    /// Get all modified storage slots since last commit
    /// </summary>
    /// <returns>Set of modified slot numbers</returns>
    public IReadOnlySet<BigInteger> GetModifiedSlots()
    {
        return new HashSet<BigInteger>(_modifiedSlots.Snapshot());
    }
    
    /// <summary>
    /// Cleanup expired cache entries
    /// </summary>
    /// <param name="state">Timer state (unused)</param>
    private void CleanupCache(object? state)
    {
        if (_disposed) return;
        
        try
        {
            var currentTime = (ulong)_accessTimer.ElapsedMilliseconds;
            var expiredSlots = new System.Collections.Generic.List<BigInteger>();
            
            foreach (var kvp in _cache)
            {
                if (currentTime - kvp.Value.LastAccessed > CACHE_TTL_MS && 
                    !kvp.Value.IsModified)
                {
                    expiredSlots.Add(kvp.Key);
                }
            }
            
            foreach (var slot in expiredSlots)
            {
                if (_cache.TryRemove(slot, out _))
                {
                    Interlocked.Increment(ref _cacheEvictions);
                }
            }
        }
        catch
        {
            // Ignore cleanup errors
        }
    }
    
    /// <summary>
    /// Evict the oldest cache entry to make room for new entries
    /// </summary>
    private void EvictOldestCacheEntry()
    {
        BigInteger? oldestSlot = null;
        ulong oldestTime = ulong.MaxValue;
        
        foreach (var kvp in _cache)
        {
            if (kvp.Value.LastAccessed < oldestTime && !kvp.Value.IsModified)
            {
                oldestTime = kvp.Value.LastAccessed;
                oldestSlot = kvp.Key;
            }
        }
        
        if (oldestSlot.HasValue && _cache.TryRemove(oldestSlot.Value, out _))
        {
            Interlocked.Increment(ref _cacheEvictions);
        }
    }
    
    /// <summary>
    /// Compress value if it would reduce storage size
    /// </summary>
    /// <param name="value">Value to potentially compress</param>
    /// <returns>Original or compressed value</returns>
    private byte[] CompressValueIfBeneficial(byte[] value)
    {
        // Simple run-length encoding for values with repeated bytes
        var consecutiveZeros = 0;
        for (int i = value.Length - 1; i >= 0; i--)
        {
            if (value[i] == 0)
                consecutiveZeros++;
            else
                break;
        }
        
        // If more than half the value is trailing zeros, store compressed
        if (consecutiveZeros > SLOT_SIZE / 2)
        {
            var nonZeroLength = SLOT_SIZE - consecutiveZeros;
            var compressed = new byte[nonZeroLength + 1];
            compressed[0] = (byte)consecutiveZeros; // Store zero count
            Array.Copy(value, 0, compressed, 1, nonZeroLength);
            return compressed;
        }
        
        return value;
    }
    
    /// <summary>
    /// Clear the cache and modified slots tracking
    /// </summary>
    public void ClearCache()
    {
        if (_disposed)
            throw new ObjectDisposedException(nameof(StorageManager));
            
        _cache.Clear();
        _modifiedSlots.Clear();
        _cacheHits = 0;
        _cacheMisses = 0;
        _storageReads = 0;
        _storageWrites = 0;
        _cacheEvictions = 0;
    }
    
    /// <summary>
    /// Batch load multiple storage slots efficiently
    /// </summary>
    /// <param name="slots">Storage slot numbers</param>
    /// <returns>Dictionary mapping slots to their values</returns>
    public Dictionary<BigInteger, byte[]> BatchLoad(IEnumerable<BigInteger> slots)
    {
        if (_disposed)
            throw new ObjectDisposedException(nameof(StorageManager));
            
        var result = new Dictionary<BigInteger, byte[]>();
        var slotsToFetch = new System.Collections.Generic.List<BigInteger>();
        
        // Check cache first
        foreach (var slot in slots)
        {
            if (_cache.TryGetValue(slot, out var cached))
            {
                result[slot] = cached.Value;
                Interlocked.Increment(ref _cacheHits);
            }
            else
            {
                slotsToFetch.Add(slot);
                Interlocked.Increment(ref _cacheMisses);
            }
        }
        
        // Batch fetch missing slots
        foreach (var slot in slotsToFetch)
        {
            result[slot] = Load(slot);
        }
        
        return result;
    }
    
    /// <summary>
    /// Batch store multiple storage slots efficiently
    /// </summary>
    /// <param name="updates">Dictionary mapping slots to their new values</param>
    public void BatchStore(Dictionary<BigInteger, byte[]> updates)
    {
        if (_disposed)
            throw new ObjectDisposedException(nameof(StorageManager));
            
        foreach (var kvp in updates)
        {
            Store(kvp.Key, kvp.Value);
        }
    }
    
    /// <summary>
    /// Get comprehensive storage statistics
    /// </summary>
    /// <returns>Detailed storage usage statistics</returns>
    public StorageStats GetStats()
    {
        var totalOperations = _cacheHits + _cacheMisses;
        var modifiedCount = _cache.Values.Count(c => c.IsModified);
        
        return new StorageStats
        {
            CachedSlots = (uint)_cache.Count,
            ModifiedSlots = (uint)_modifiedSlots.Count,
            ModifiedCachedSlots = (uint)modifiedCount,
            CacheHitRatio = totalOperations > 0 ? (double)_cacheHits / totalOperations : 0.0,
            StorageReads = _storageReads,
            StorageWrites = _storageWrites,
            CacheEvictions = _cacheEvictions,
            CacheUtilization = MAX_CACHE_SIZE > 0 ? (double)_cache.Count / MAX_CACHE_SIZE : 0.0
        };
    }
    
    /// <summary>
    /// Dispose storage manager and cleanup resources
    /// </summary>
    public void Dispose()
    {
        if (!_disposed)
        {
            _cacheCleanupTimer?.Dispose();
            _lock?.Dispose();
            _accessTimer?.Stop();
            ClearCache();
            _disposed = true;
        }
    }
}

/// <summary>
/// Comprehensive storage usage statistics
/// </summary>
public record StorageStats
{
    /// <summary>Number of cached storage slots</summary>
    public uint CachedSlots { get; init; }
    
    /// <summary>Number of modified storage slots</summary>
    public uint ModifiedSlots { get; init; }
    
    /// <summary>Number of modified slots in cache</summary>
    public uint ModifiedCachedSlots { get; init; }
    
    /// <summary>Cache hit ratio (0.0 to 1.0)</summary>
    public double CacheHitRatio { get; init; }
    
    /// <summary>Total storage read operations</summary>
    public ulong StorageReads { get; init; }
    
    /// <summary>Total storage write operations</summary>
    public ulong StorageWrites { get; init; }
    
    /// <summary>Number of cache evictions performed</summary>
    public ulong CacheEvictions { get; init; }
    
    /// <summary>Cache utilization ratio (0.0 to 1.0)</summary>
    public double CacheUtilization { get; init; }
}

/// <summary>
/// Cached storage slot with metadata
/// </summary>
internal sealed class CachedSlot
{
    /// <summary>Slot value</summary>
    public byte[] Value { get; set; } = Array.Empty<byte>();
    
    /// <summary>Last access time in milliseconds</summary>
    public ulong LastAccessed { get; set; }
    
    /// <summary>Number of times accessed</summary>
    public ulong AccessCount { get; set; }
    
    /// <summary>Whether the slot has been modified</summary>
    public bool IsModified { get; set; }
}

/// <summary>
/// Thread-safe HashSet implementation
/// </summary>
internal sealed class ConcurrentHashSet<T> : IDisposable where T : notnull
{
    private readonly HashSet<T> _set = new();
    private readonly ReaderWriterLockSlim _lock = new();
    private bool _disposed = false;
    
    public void Add(T item)
    {
        if (_disposed) return;
        _lock.EnterWriteLock();
        try
        {
            _set.Add(item);
        }
        finally
        {
            _lock.ExitWriteLock();
        }
    }
    
    public bool Contains(T item)
    {
        if (_disposed) return false;
        _lock.EnterReadLock();
        try
        {
            return _set.Contains(item);
        }
        finally
        {
            _lock.ExitReadLock();
        }
    }
    
    public void Clear()
    {
        if (_disposed) return;
        _lock.EnterWriteLock();
        try
        {
            _set.Clear();
        }
        finally
        {
            _lock.ExitWriteLock();
        }
    }
    
    public int Count
    {
        get
        {
            if (_disposed) return 0;
            _lock.EnterReadLock();
            try
            {
                return _set.Count;
            }
            finally
            {
                _lock.ExitReadLock();
            }
        }
    }

    public T[] Snapshot()
    {
        if (_disposed) return Array.Empty<T>();
        _lock.EnterReadLock();
        try
        {
            return _set.ToArray();
        }
        finally
        {
            _lock.ExitReadLock();
        }
    }
    
    public void Dispose()
    {
        if (!_disposed)
        {
            _lock.Dispose();
            _disposed = true;
        }
    }
}