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
using System.Numerics;
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services;
using Neo.Sol.Runtime.Memory;
using Neo.Sol.Runtime.Storage;
using Neo.Sol.Runtime.Events;
using Neo.Sol.Runtime.Context;
using ExecutionContext = Neo.Sol.Runtime.Context.ExecutionContext;
using Neo.Sol.Runtime.Calls;
using Neo.Sol.Runtime.Registry;
using Neo.Sol.Runtime.ABI;
using Neo.SmartContract.Framework.Native;
using NeoFrameworkRuntime = Neo.SmartContract.Framework.Services.Runtime;
using NeoFrameworkStorage = Neo.SmartContract.Framework.Services.Storage;
namespace Neo.Sol.Runtime;
/// <summary>
/// Main EVM runtime providing semantic compatibility layer for Solidity contracts on NeoVM
/// Integrates memory management, storage, events, context, and cross-contract calls
/// </summary>
public sealed class EvmRuntime : IDisposable
{
private readonly StorageContext _storageContext;
private readonly EvmMemoryManager _memoryManager;
private readonly StorageManager _storageManager;
private readonly EventManager _eventManager;
private readonly ExternalCallManager _callManager;
private readonly AddressRegistry _addressRegistry;
private readonly ExecutionContext _executionContext;
private bool _disposed = false;
/// <summary>
/// Initialize EVM runtime for contract execution
/// </summary>
/// <param name="contractAddress">Address of the current contract</param>
public EvmRuntime(UInt160 contractAddress)
{
ContractAddress = contractAddress;
try
{
_storageContext = NeoFrameworkStorage.CurrentContext;
}
catch
{
_storageContext = new StorageContext();
}
_memoryManager = new EvmMemoryManager();
_storageManager = new StorageManager(_storageContext);
_eventManager = new EventManager(contractAddress);
_addressRegistry = new AddressRegistry(_storageContext);
_executionContext = ExecutionContext.Current;
_callManager = new ExternalCallManager(_executionContext);
}
/// <summary>
/// Current contract address
/// </summary>
public UInt160 ContractAddress { get; }
/// <summary>
/// Memory manager for EVM-compatible memory operations
/// </summary>
public EvmMemoryManager Memory => _memoryManager;
/// <summary>
/// Storage manager for contract storage
/// </summary>
public StorageManager Storage => _storageManager;
/// <summary>
/// Event manager for emitting events
/// </summary>
public EventManager Events => _eventManager;
/// <summary>
/// External call manager for cross-contract interactions
/// </summary>
public ExternalCallManager Calls => _callManager;
/// <summary>
/// Address registry for contract resolution
/// </summary>
public AddressRegistry Registry => _addressRegistry;
/// <summary>
/// Execution context (msg, tx, block)
/// </summary>
public ExecutionContext Context => _executionContext;
// EVM-compatible global variables and functions
/// <summary>
/// Current block information (block.*)
/// </summary>
public BlockContext Block => _executionContext.Block;
/// <summary>
/// Current message information (msg.*)
/// </summary>
public MsgContext Msg => _executionContext.Msg;
/// <summary>
/// Current transaction information (tx.*)
/// </summary>
public TxContext Tx => _executionContext.Tx;
/// <summary>
/// Get current block timestamp
/// </summary>
public ulong Now => Block.Timestamp;
/// <summary>
/// Get current block number
/// </summary>
public uint BlockNumber => Block.Number;
/// <summary>
/// Get current block hash
/// </summary>
public UInt256 BlockHash => Block.Hash;
// EVM-compatible cryptographic functions
/// <summary>
/// Compute Keccak256 hash
/// </summary>
/// <param name="data">Data to hash</param>
/// <returns>32-byte hash</returns>
public byte[] Keccak256(byte[] data) => Crypto.CryptoLib.Keccak256(data);
/// <summary>
/// Compute SHA256 hash
/// </summary>
/// <param name="data">Data to hash</param>
/// <returns>32-byte hash</returns>
public byte[] Sha256(byte[] data) => Crypto.CryptoLib.Sha256(data);
/// <summary>
/// Recover public key from signature (ecrecover)
/// </summary>
/// <param name="messageHash">Message hash</param>
/// <param name="signature">Signature</param>
/// <param name="recoveryId">Recovery ID</param>
/// <returns>Recovered public key</returns>
public byte[]? EcRecover(byte[] messageHash, byte[] signature, int recoveryId)
=> Crypto.CryptoLib.EcRecover(messageHash, signature, recoveryId);
/// <summary>
/// Convert public key to Ethereum address
/// </summary>
/// <param name="publicKey">Public key</param>
/// <returns>Ethereum address</returns>
public byte[] PublicKeyToAddress(byte[] publicKey)
=> Crypto.CryptoLib.PublicKeyToAddress(publicKey);
// EVM-compatible utility functions
/// <summary>
/// Revert transaction with error message
/// </summary>
/// <param name="reason">Revert reason</param>
public void Revert(string reason = "")
{
if (!string.IsNullOrEmpty(reason))
{
// Encode revert reason as ABI error
var errorSignature = "Error(string)";
var encodedReason = AbiEncoder.EncodeCall(errorSignature, reason);
throw new System.Exception($"Revert: {reason}");
}
throw new System.Exception("Revert");
}
/// <summary>
/// Require condition or revert
/// </summary>
/// <param name="condition">Condition to check</param>
/// <param name="message">Error message if condition fails</param>
public void Require(bool condition, string message = "Requirement failed")
{
if (!condition)
{
Revert(message);
}
}
/// <summary>
/// Assert condition (for internal errors)
/// </summary>
/// <param name="condition">Condition to check</param>
public void Assert(bool condition)
{
if (!condition)
{
// In EVM, assert failures consume all gas
throw new System.Exception("Assertion failed");
}
}
/// <summary>
/// Selfdestruct the contract (transfer remaining balance)
/// </summary>
/// <param name="recipient">Address to receive remaining balance</param>
public void SelfDestruct(UInt160 recipient)
{
// Transfer any remaining GAS to recipient
var balance = GetBalance(ContractAddress);
if (balance > 0)
{
TransferGas(recipient, balance);
}
// Mark contract as destroyed
Registry.UpdateContractStatus(ContractAddress, false, ContractAddress);
// Emit self-destruct event
Events.Log2("SelfDestruct(address)", ContractAddress, recipient);
// In a real implementation, this would halt execution
throw new System.Exception("Contract self-destructed");
}
/// <summary>
/// Get balance of an address
/// </summary>
/// <param name="address">Address to check</param>
/// <returns>Balance in smallest unit</returns>
public BigInteger GetBalance(UInt160 address)
{
try
{
return GAS.BalanceOf((Neo.SmartContract.Framework.UInt160)NeoTypeConversions.ToByteArray(address));
}
catch (Exception ex)
{
// Log the error for debugging
NeoFrameworkRuntime.Log($"Error getting balance for {address}: {ex.Message}");
return 0;
}
}
/// <summary>
/// Transfer GAS to address
/// </summary>
/// <param name="to">Recipient address</param>
/// <param name="amount">Amount to transfer</param>
/// <returns>True if successful</returns>
public bool TransferGas(UInt160 to, BigInteger amount)
{
try
{
if (amount <= 0)
{
throw new ArgumentException("Transfer amount must be positive");
}
if (to == UInt160.Zero)
{
throw new ArgumentException("Invalid recipient address");
}
// Check current balance
var currentBalance = GetBalance(ContractAddress);
if (currentBalance < amount)
{
NeoFrameworkRuntime.Log($"Insufficient balance: {currentBalance} < {amount}");
return false;
}
var success = GAS.Transfer(
(Neo.SmartContract.Framework.UInt160)NeoTypeConversions.ToByteArray(ContractAddress),
(Neo.SmartContract.Framework.UInt160)NeoTypeConversions.ToByteArray(to),
amount,
null
);
if (success)
{
// Emit transfer event for tracking
Events.Log3("Transfer(address,address,uint256)", ContractAddress, to, amount);
NeoFrameworkRuntime.Log($"Successfully transferred {amount} GAS from {ContractAddress} to {to}");
}
return success;
}
catch (Exception ex)
{
NeoFrameworkRuntime.Log($"Error transferring GAS: {ex.Message}");
return false;
}
}
/// <summary>
/// Get code size at address
/// </summary>
/// <param name="address">Address to check</param>
/// <returns>Code size in bytes</returns>
public uint GetCodeSize(UInt160 address)
{
try
{
var contract = ContractManagement.GetContract(
(Neo.SmartContract.Framework.UInt160)NeoTypeConversions.ToByteArray(address)
);
return contract?.Nef != null ? (uint)contract.Nef.Length : 0;
}
catch
{
return 0;
}
}
/// <summary>
/// Get code hash at address
/// </summary>
/// <param name="address">Address to check</param>
/// <returns>Code hash</returns>
public UInt256 GetCodeHash(UInt160 address)
{
try
{
// In Neo, we could use the contract hash
return new UInt256(NeoTypeConversions.ToByteArray(address).Concat(new byte[12]).ToArray());
}
catch
{
return UInt256.Zero;
}
}
/// <summary>
/// Get runtime statistics
/// </summary>
/// <returns>Runtime performance statistics</returns>
public RuntimeStats GetStats()
{
return new RuntimeStats
{
MemoryStats = _memoryManager.GetStats(),
StorageStats = _storageManager.GetStats(),
RegistryStats = _addressRegistry.GetStats(),
GasUsed = EstimateGasUsed(),
ExecutionTime = GetExecutionTime()
};
}
/// <summary>
/// Clear all runtime state (for testing)
/// </summary>
public void Reset()
{
_memoryManager.Clear();
_storageManager.ClearCache();
ExecutionContext.Reset();
}
private uint EstimateGasUsed()
{
try
{
// Calculate gas usage based on instruction count and complexity
var baseGas = 20_000u; // Base gas cost
// Add gas for memory operations
var memoryGas = (uint)(_memoryManager.GetStats().TotalSize / 32) * 3; // 3 gas per 32 bytes
// Add gas for storage operations
var storageStats = _storageManager.GetStats();
var storageGas = storageStats.StorageReads * 200u +
storageStats.StorageWrites * 20_000u;
// Add gas for external calls
var callGas = _callManager.GetCallCount() * 700u;
// Add gas for events
var eventGas = _eventManager.GetEventCount() * 375u;
var totalGas = baseGas + memoryGas + storageGas + callGas + eventGas;
// Cap at reasonable maximum
return (uint)Math.Min(totalGas, 100_000_000u);
}
catch
{
// Fallback to conservative estimate
return 50_000u;
}
}
private ulong GetExecutionTime()
{
try
{
// Get current runtime timestamp
var currentTime = NeoFrameworkRuntime.Time;
// Return execution duration from contract start
// In a real implementation, this would track from contract initialization
return currentTime;
}
catch
{
return 0;
}
}
/// <summary>
/// Dispose runtime resources
/// </summary>
public void Dispose()
{
if (!_disposed)
{
_memoryManager.Clear();
_storageManager.ClearCache();
_disposed = true;
}
}
}
/// <summary>
/// Runtime performance statistics
/// </summary>
public sealed record RuntimeStats
{
public MemoryStats MemoryStats { get; init; } = new();
public StorageStats StorageStats { get; init; } = new();
public RegistryStats RegistryStats { get; init; } = new();
public uint GasUsed { get; init; }
public ulong ExecutionTime { get; init; }
}
/// <summary>
/// Static helper class for common EVM operations
/// </summary>
public static class Evm
{
/// <summary>
/// Create runtime instance for current contract
/// </summary>
/// <returns>EVM runtime instance</returns>
public static EvmRuntime CreateRuntime()
{
return new EvmRuntime(NeoTypeConversions.ToCoreUInt160(NeoFrameworkRuntime.ExecutingScriptHash));
}
/// <summary>
/// Encode function call
/// </summary>
/// <param name="signature">Function signature</param>
/// <param name="parameters">Parameters</param>
/// <returns>Encoded call data</returns>
public static byte[] EncodeCall(string signature, params object[] parameters)
{
return AbiEncoder.EncodeCall(signature, parameters);
}
/// <summary>
/// Calculate function selector
/// </summary>
/// <param name="signature">Function signature</param>
/// <returns>4-byte selector</returns>
public static byte[] Selector(string signature)
{
return AbiEncoder.CalculateFunctionSelector(signature);
}
/// <summary>
/// Pack multiple values using ABI encoding
/// </summary>
/// <param name="values">Values to pack</param>
/// <returns>Packed bytes</returns>
public static byte[] Pack(params object[] values)
{
return AbiEncoder.EncodeParameters(values);
}
}