Skip to content

Commit

Permalink
Merge branch 'master' into 3s-consensus
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim8y authored Dec 16, 2024
2 parents f016575 + 692cfbb commit 1570990
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 8 deletions.
5 changes: 4 additions & 1 deletion .devcontainer/devcontainer.dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
FROM mcr.microsoft.com/devcontainers/dotnet:9.0-noble
# https://github.com/dotnet/dotnet-docker/blob/main/README.sdk.md
# https://mcr.microsoft.com/en-us/artifact/mar/dotnet/sdk/tags <-- this shows all images
FROM mcr.microsoft.com/dotnet/sdk:9.0.101-noble

# Install the libleveldb-dev package
RUN apt-get update && apt-get install -y libleveldb-dev
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MSTest.TestFramework" Version="3.6.3" />
<ProjectReference Include="..\..\src\Neo.Extensions\Neo.Extensions.csproj" />
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
</ItemGroup>
Expand Down
2 changes: 0 additions & 2 deletions benchmarks/Neo.VM.Benchmarks/Neo.VM.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MSTest.TestFramework" Version="3.6.3" />
<ProjectReference Include="..\..\src\Neo.Extensions\Neo.Extensions.csproj" />
<ProjectReference Include="..\..\src\Neo.Json\Neo.Json.csproj" />
<ProjectReference Include="..\..\src\Neo.VM\Neo.VM.csproj" />
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
<ProjectReference Include="..\..\tests\Neo.VM.Tests\Neo.VM.Tests.csproj" />
</ItemGroup>

</Project>
24 changes: 22 additions & 2 deletions benchmarks/Neo.VM.Benchmarks/OpCode/BenchmarkEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Test.Types;
using Neo.VM.Types;
using System.Diagnostics;
using System.Runtime.CompilerServices;

Expand All @@ -18,12 +18,14 @@ namespace Neo.VM.Benchmark.OpCode
/// <summary>
/// A simple benchmark engine for <see cref="ExecutionEngine"/>.
/// </summary>
public class BenchmarkEngine : TestEngine
public class BenchmarkEngine : ExecutionEngine
{
private readonly Dictionary<VM.OpCode, (int Count, TimeSpan TotalTime)> _opcodeStats = new();
private readonly Dictionary<Script, HashSet<uint>> _breakPoints = new();
private long _gasConsumed = 0;

public BenchmarkEngine() : base(ComposeJumpTable()) { }

/// <summary>
/// Add a breakpoint at the specified position of the specified script. The VM will break the execution when it reaches the breakpoint.
/// </summary>
Expand Down Expand Up @@ -187,5 +189,23 @@ private void PrintOpcodeStats()
$"Avg Time: {kvp.Value.TotalTime.TotalMilliseconds * 1000 / kvp.Value.Count,10:F2} μs");
}
}

private static JumpTable ComposeJumpTable()
{
JumpTable jumpTable = new JumpTable();
jumpTable[VM.OpCode.SYSCALL] = OnSysCall;
return jumpTable;
}

private static void OnSysCall(ExecutionEngine engine, VM.Instruction instruction)
{
uint method = instruction.TokenU32;
if (method == 0x77777777)
engine.CurrentContext!.EvaluationStack.Push(StackItem.FromInterface(new object()));
else if (method == 0xaddeadde)
engine.JumpTable.ExecuteThrow(engine, "error");
else
throw new Exception();
}
}
}
1 change: 0 additions & 1 deletion benchmarks/Neo.VM.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
}

var methods = benchmarkType.GetMethods(BindingFlags.Public | BindingFlags.Instance);

foreach (var method in methods)
{
if (method.DeclaringType == benchmarkType && !method.GetCustomAttributes<GlobalSetupAttribute>().Any())
Expand Down
42 changes: 42 additions & 0 deletions src/Neo/Extensions/SmartContract/GasTokenExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// GasTokenExtensions.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using System;
using System.Collections.Generic;
using System.Numerics;

namespace Neo.Extensions
{
public static class GasTokenExtensions
{
public static IEnumerable<(UInt160 Address, BigInteger Balance)> GetAccounts(this GasToken gasToken, DataCache snapshot)
{
if (gasToken is null)
throw new ArgumentNullException(nameof(gasToken));

if (snapshot is null)
throw new ArgumentNullException(nameof(snapshot));

var kb = new KeyBuilder(gasToken.Id, GasToken.Prefix_Account);
var prefixKey = kb.ToArray();

foreach (var (key, value) in snapshot.Find(prefixKey, SeekDirection.Forward))
{
var keyBytes = key.ToArray();
var addressHash = new UInt160(keyBytes.AsSpan(prefixKey.Length));
yield return new(addressHash, value.GetInteroperable<AccountState>().Balance);
}
}
}
}
2 changes: 1 addition & 1 deletion src/Neo/SmartContract/Native/FungibleToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public abstract class FungibleToken<TState> : NativeContract
/// <summary>
/// The prefix for storing account states.
/// </summary>
protected const byte Prefix_Account = 20;
protected internal const byte Prefix_Account = 20;

/// <summary>
/// Initializes a new instance of the <see cref="FungibleToken{TState}"/> class.
Expand Down
53 changes: 53 additions & 0 deletions tests/Neo.UnitTests/Extensions/UT_GasTokenExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// UT_GasTokenExtensions.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Extensions;
using Neo.SmartContract.Native;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

namespace Neo.UnitTests.Extensions
{
[TestClass]
public class UT_GasTokenExtensions
{
private NeoSystem system;

[TestInitialize]
public void Initialize()
{
system = TestBlockchain.TheNeoSystem;
}

[TestCleanup]
public void Clean()
{
TestBlockchain.ResetStore();
}

[TestMethod]
public void TestGetAccounts()
{
UInt160 expected = "0x9f8f056a53e39585c7bb52886418c7bed83d126b";

var accounts = NativeContract.GAS.GetAccounts(system.StoreView);
var actual = accounts.FirstOrDefault();

Assert.AreEqual(expected, actual.Address);
Assert.AreEqual(5200000000000000, actual.Balance);
}
}
}

0 comments on commit 1570990

Please sign in to comment.