Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RC2 to RC3 Comparison #8

Open
wants to merge 3 commits into
base: rc2-untouched
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/neo/SmartContract/Native/NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,18 @@ public ECPoint[] GetCommittee(DataCache snapshot)
return GetCommitteeFromCache(snapshot).Select(p => p.PublicKey).OrderBy(p => p).ToArray();
}

/// <summary>
/// Get account state.
/// </summary>
/// <param name="snapshot">The snapshot used to read data.</param>
/// <param name="account">account</param>
/// <returns>The state of the account.</returns>
[ContractMethod(CpuFee = 1 << 15, RequiredCallFlags = CallFlags.ReadStates)]
public NeoAccountState GetAccountState(DataCache snapshot, UInt160 account)
{
return snapshot.TryGet(CreateStorageKey(Prefix_Account).Add(account))?.GetInteroperable<NeoAccountState>();
}

/// <summary>
/// Gets the address of the committee.
/// </summary>
Expand Down
22 changes: 22 additions & 0 deletions src/neo/SmartContract/Native/StdLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,28 @@ public static byte[] Base58Decode([MaxLength(MaxInputLength)] string s)
return Base58.Decode(s);
}

/// <summary>
/// Converts a byte array to its equivalent <see cref="string"/> representation that is encoded with base-58 digits. The encoded <see cref="string"/> contains the checksum of the binary data.
/// </summary>
/// <param name="data">The byte array to be encoded.</param>
/// <returns>The encoded <see cref="string"/>.</returns>
[ContractMethod(CpuFee = 1 << 16)]
public static string Base58CheckEncode([MaxLength(MaxInputLength)] byte[] data)
{
return Base58.Base58CheckEncode(data);
}

/// <summary>
/// Converts the specified <see cref="string"/>, which encodes binary data as base-58 digits, to an equivalent byte array. The encoded <see cref="string"/> contains the checksum of the binary data.
/// </summary>
/// <param name="s">The base58 <see cref="string"/>.</param>
/// <returns>The decoded byte array.</returns>
[ContractMethod(CpuFee = 1 << 16)]
public static byte[] Base58CheckDecode([MaxLength(MaxInputLength)] string s)
{
return Base58.Base58CheckDecode(s);
}

[ContractMethod(CpuFee = 1 << 5)]
private static int MemoryCompare([MaxLength(MaxInputLength)] byte[] str1, [MaxLength(MaxInputLength)] byte[] str2)
{
Expand Down
8 changes: 4 additions & 4 deletions src/neo/neo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Copyright>2015-2021 The Neo Project</Copyright>
<AssemblyTitle>Neo</AssemblyTitle>
<VersionPrefix>3.0.0</VersionPrefix>
<VersionSuffix>rc2</VersionSuffix>
<VersionSuffix>rc3</VersionSuffix>
<Authors>The Neo Project</Authors>
<TargetFramework>net5.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand All @@ -26,11 +26,11 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Akka" Version="1.4.19" />
<PackageReference Include="Akka" Version="1.4.20" />
<PackageReference Include="BouncyCastle.NetCore" Version="1.8.8" />
<PackageReference Include="K4os.Compression.LZ4" Version="1.2.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.5" />
<PackageReference Include="Neo.VM" Version="3.0.0-rc2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.6" />
<PackageReference Include="Neo.VM" Version="3.0.0-rc3" />
</ItemGroup>

</Project>
32 changes: 32 additions & 0 deletions tests/neo.UnitTests/SmartContract/Native/UT_NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@ public void Check_Transfer()
NativeContract.NEO.BalanceOf(snapshot, from).Should().Be(99999999);
NativeContract.NEO.BalanceOf(snapshot, to).Should().Be(1);

var (from_balance, _, _) = GetAccountState(snapshot, new UInt160(from));
var (to_balance, _, _) = GetAccountState(snapshot, new UInt160(to));

from_balance.Should().Be(99999999);
to_balance.Should().Be(1);

// Check unclaim

unclaim = Check_UnclaimedGas(snapshot, from, persistingBlock);
Expand Down Expand Up @@ -853,6 +859,9 @@ public void TestVote()
ret.State.Should().BeTrue();
ret.Result.Should().BeFalse();

var (_, _, vote_to_null) = GetAccountState(snapshot, account);
vote_to_null.Should().BeNull();

snapshot.Delete(keyAccount);
snapshot.GetAndChange(keyAccount, () => new StorageItem(new NeoAccountState
{
Expand All @@ -862,6 +871,9 @@ public void TestVote()
ret = Check_Vote(snapshot, account.ToArray(), ECCurve.Secp256r1.G.ToArray(), true, _persistingBlock);
ret.State.Should().BeTrue();
ret.Result.Should().BeTrue();

var (_, _, voteto) = GetAccountState(snapshot, account);
voteto.ToHexString().Should().Be(ECCurve.Secp256r1.G.ToArray().ToHexString());
}

internal (bool State, bool Result) Transfer4TesingOnBalanceChanging(BigInteger amount, bool addVotes)
Expand Down Expand Up @@ -1080,5 +1092,25 @@ internal static (bool State, bool Result) Check_UnregisterCandidate(DataCache sn

return (true, result.GetBoolean());
}

internal static (BigInteger balance, BigInteger height, byte[] voteto) GetAccountState(DataCache snapshot, UInt160 account)
{
using var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheNeoSystem.Settings);

using var script = new ScriptBuilder();
script.EmitDynamicCall(NativeContract.NEO.Hash, "getAccountState", account);
engine.LoadScript(script.ToArray());

engine.Execute().Should().Be(VMState.HALT);

var result = engine.ResultStack.Pop();
result.Should().BeOfType(typeof(VM.Types.Struct));

VM.Types.Struct state = (result as VM.Types.Struct);
var balance = state[0].GetInteger();
var height = state[1].GetInteger();
var voteto = state[2].IsNull ? null : state[2].GetSpan().ToArray();
return (balance, height, voteto);
}
}
}
54 changes: 54 additions & 0 deletions tests/neo.UnitTests/SmartContract/Native/UT_StdLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,60 @@ public void MemoryCompare()
}
}

[TestMethod]
public void CheckDecodeEncode()
{
var snapshot = TestBlockchain.GetTestSnapshot();

using (ScriptBuilder script = new())
{
script.EmitDynamicCall(NativeContract.StdLib.Hash, "base58CheckEncode", new byte[] { 1, 2, 3 });

using var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheNeoSystem.Settings);
engine.LoadScript(script.ToArray());

Assert.AreEqual(engine.Execute(), VMState.HALT);
Assert.AreEqual(1, engine.ResultStack.Count);

Assert.AreEqual("3DUz7ncyT", engine.ResultStack.Pop<ByteString>().GetString());
}

using (ScriptBuilder script = new())
{
script.EmitDynamicCall(NativeContract.StdLib.Hash, "base58CheckDecode", "3DUz7ncyT");

using var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheNeoSystem.Settings);
engine.LoadScript(script.ToArray());

Assert.AreEqual(engine.Execute(), VMState.HALT);
Assert.AreEqual(1, engine.ResultStack.Count);

CollectionAssert.AreEqual(new byte[] { 1, 2, 3 }, engine.ResultStack.Pop<ByteString>().GetSpan().ToArray());
}

// Error

using (ScriptBuilder script = new())
{
script.EmitDynamicCall(NativeContract.StdLib.Hash, "base58CheckDecode", "AA");

using var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheNeoSystem.Settings);
engine.LoadScript(script.ToArray());

Assert.AreEqual(engine.Execute(), VMState.FAULT);
}

using (ScriptBuilder script = new())
{
script.EmitDynamicCall(NativeContract.StdLib.Hash, "base58CheckDecode", null);

using var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheNeoSystem.Settings);
engine.LoadScript(script.ToArray());

Assert.AreEqual(engine.Execute(), VMState.FAULT);
}
}

[TestMethod]
public void MemorySearch()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/neo.UnitTests/neo.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Akka.TestKit" Version="1.4.19" />
<PackageReference Include="Akka.TestKit.Xunit2" Version="1.4.19" />
<PackageReference Include="Akka.TestKit" Version="1.4.20" />
<PackageReference Include="Akka.TestKit.Xunit2" Version="1.4.20" />
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="Moq" Version="4.16.1" />
Expand Down