Skip to content

Commit

Permalink
[Neo Core UT] Fix ut and style (#3413)
Browse files Browse the repository at this point in the history
* fix ut and style

* clean

* update test

* Update tests/Neo.UnitTests/IO/Caching/UT_DataCache.cs

Co-authored-by: Hecate2 <hecate2@qq.com>

---------

Co-authored-by: Shargon <shargon@gmail.com>
Co-authored-by: Hecate2 <hecate2@qq.com>
Co-authored-by: Hecate2 <2474101468@qq.com>
Co-authored-by: NGD Admin <154295625+NGDAdmin@users.noreply.github.com>
  • Loading branch information
5 people authored Jul 23, 2024
1 parent 677b1fa commit 21765dc
Show file tree
Hide file tree
Showing 35 changed files with 342 additions and 314 deletions.
1 change: 1 addition & 0 deletions src/Neo/Neo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<InternalsVisibleTo Include="Neo.SmartContract.Testing" />
<InternalsVisibleTo Include="Neo.SmartContract.TestEngine" />
<InternalsVisibleTo Include="Neo.Plugins.RpcServer.Tests" />
<InternalsVisibleTo Include="Neo.Plugins.OracleService.Tests" />
</ItemGroup>

</Project>
25 changes: 21 additions & 4 deletions tests/Neo.Plugins.OracleService.Tests/TestBlockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,45 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Akka.Actor;
using Neo.Ledger;
using Neo.Persistence;
using System;

namespace Neo.Plugins.OracleService.Tests
{
public static class TestBlockchain
{
public static readonly NeoSystem TheNeoSystem;
private static readonly NeoSystem s_theNeoSystem;
private static readonly MemoryStore s_store = new();

private class StoreProvider : IStoreProvider
{
public string Name => "TestProvider";

public IStore GetStore(string path) => s_store;
}

static TestBlockchain()
{
Console.WriteLine("initialize NeoSystem");
TheNeoSystem = new NeoSystem(ProtocolSettings.Load("config.json"), new MemoryStoreProvider());
s_theNeoSystem = new NeoSystem(ProtocolSettings.Load("config.json"), new StoreProvider());
}

public static void InitializeMockNeoSystem()
{
}

internal static DataCache GetTestSnapshot()
internal static void ResetStore()
{
s_store.Reset();
s_theNeoSystem.Blockchain.Ask(new Blockchain.Initialize()).Wait();
}

internal static SnapshotCache GetTestSnapshotCache()
{
return TheNeoSystem.GetSnapshotCache().CloneCache();
ResetStore();
return s_theNeoSystem.GetSnapshot();
}
}
}
4 changes: 2 additions & 2 deletions tests/Neo.Plugins.OracleService.Tests/UT_OracleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void TestFilter()
[TestMethod]
public void TestCreateOracleResponseTx()
{
var snapshot = TestBlockchain.GetTestSnapshot();
var snapshot = TestBlockchain.GetTestSnapshotCache();

var executionFactor = NativeContract.Policy.GetExecFeeFactor(snapshot);
Assert.AreEqual(executionFactor, (uint)30);
Expand All @@ -85,7 +85,7 @@ public void TestCreateOracleResponseTx()
Filter = "",
CallbackContract = UInt160.Zero,
CallbackMethod = "callback",
UserData = System.Array.Empty<byte>()
UserData = []
};
byte Prefix_Transaction = 11;
snapshot.Add(NativeContract.Ledger.CreateStorageKey(Prefix_Transaction, request.OriginalTxid), new StorageItem(new TransactionState()
Expand Down
4 changes: 2 additions & 2 deletions tests/Neo.UnitTests/IO/Caching/UT_CloneCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ public void TestUpdateInternal()
[TestMethod]
public void TestCacheOverrideIssue2572()
{
var snapshot = TestBlockchain.GetTestSnapshot();
var storages = snapshot.CloneCache();
var snapshot = TestBlockchain.GetTestSnapshotCache();
var storages = snapshot.CreateSnapshot();

storages.Add
(
Expand Down
6 changes: 6 additions & 0 deletions tests/Neo.UnitTests/IO/Caching/UT_DataCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,18 @@ public void TestCommit()
myDataCache.Add(key3, value4);
Assert.AreEqual(TrackState.Changed, myDataCache.GetChangeSet().Where(u => u.Key.Equals(key3)).Select(u => u.State).FirstOrDefault());

// If we use myDataCache after it is committed, it will return wrong result.
myDataCache.Commit();
Assert.AreEqual(0, myDataCache.GetChangeSet().Count());

store.TryGet(key1.ToArray()).SequenceEqual(value1.ToArray()).Should().BeTrue();
store.TryGet(key2.ToArray()).Should().BeNull();
store.TryGet(key3.ToArray()).SequenceEqual(value4.ToArray()).Should().BeTrue();

myDataCache.TryGet(key1).Value.ToArray().SequenceEqual(value1.ToArray()).Should().BeTrue();
// Though value is deleted from the store, the value can still be gotten from the snapshot cache.
myDataCache.TryGet(key2).Value.ToArray().SequenceEqual(value2.ToArray()).Should().BeTrue();
myDataCache.TryGet(key3).Value.ToArray().SequenceEqual(value4.ToArray()).Should().BeTrue();
}

[TestMethod]
Expand Down
10 changes: 5 additions & 5 deletions tests/Neo.UnitTests/Ledger/UT_Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ public void Initialize()
{
system = TestBlockchain.TheNeoSystem;
senderProbe = CreateTestProbe();
txSample = new Transaction()
txSample = new Transaction
{
Attributes = Array.Empty<TransactionAttribute>(),
Attributes = [],
Script = Array.Empty<byte>(),
Signers = new Signer[] { new Signer() { Account = UInt160.Zero } },
Witnesses = Array.Empty<Witness>()
Signers = [new Signer { Account = UInt160.Zero }],
Witnesses = []
};
system.MemPool.TryAdd(txSample, TestBlockchain.GetTestSnapshot());
system.MemPool.TryAdd(txSample, TestBlockchain.GetTestSnapshotCache());
}

[TestCleanup]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private Transaction CreateTransactionWithFee(long networkFee, long systemFee)
public async Task TestDuplicateOracle()
{
// Fake balance
var snapshot = TestBlockchain.GetTestSnapshot();
var snapshot = TestBlockchain.GetTestSnapshotCache();

ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheNeoSystem.Settings, gas: long.MaxValue);
BigInteger balance = NativeContract.GAS.BalanceOf(snapshot, UInt160.Zero);
Expand All @@ -84,7 +84,7 @@ public async Task TestDuplicateOracle()
[TestMethod]
public async Task TestTransactionSenderFee()
{
var snapshot = TestBlockchain.GetTestSnapshot();
var snapshot = TestBlockchain.GetTestSnapshotCache();
ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheNeoSystem.Settings, gas: long.MaxValue);
BigInteger balance = NativeContract.GAS.BalanceOf(snapshot, UInt160.Zero);
await NativeContract.GAS.Burn(engine, UInt160.Zero, balance);
Expand All @@ -107,7 +107,7 @@ public async Task TestTransactionSenderFee()
[TestMethod]
public async Task TestTransactionSenderFeeWithConflicts()
{
var snapshot = TestBlockchain.GetTestSnapshot();
var snapshot = TestBlockchain.GetTestSnapshotCache();
ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheNeoSystem.Settings, gas: long.MaxValue);
BigInteger balance = NativeContract.GAS.BalanceOf(snapshot, UInt160.Zero);
await NativeContract.GAS.Burn(engine, UInt160.Zero, balance);
Expand Down
2 changes: 1 addition & 1 deletion tests/Neo.UnitTests/Ledger/UT_TrimmedBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static TrimmedBlock GetTrimmedBlockWithNoTransaction()
[TestMethod]
public void TestGetBlock()
{
var snapshot = TestBlockchain.GetTestSnapshot();
var snapshot = TestBlockchain.GetTestSnapshotCache();
var tx1 = TestUtils.GetTransaction(UInt160.Zero);
tx1.Script = new byte[] { 0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,
Expand Down
2 changes: 1 addition & 1 deletion tests/Neo.UnitTests/Network/P2P/Payloads/UT_Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class UT_Block
private static ApplicationEngine GetEngine(bool hasContainer = false, bool hasSnapshot = false, bool hasBlock = false, bool addScript = true, long gas = 20_00000000)
{
var tx = hasContainer ? TestUtils.GetTransaction(UInt160.Zero) : null;
var snapshot = hasSnapshot ? TestBlockchain.GetTestSnapshot() : null;
var snapshot = hasSnapshot ? TestBlockchain.GetTestSnapshotCache() : null;
var block = hasBlock ? new Block { Header = new Header() } : null;
var engine = ApplicationEngine.Create(TriggerType.Application, tx, snapshot, block, TestBlockchain.TheNeoSystem.Settings, gas: gas);
if (addScript) engine.LoadScript(new byte[] { 0x01 });
Expand Down
2 changes: 1 addition & 1 deletion tests/Neo.UnitTests/Network/P2P/Payloads/UT_Conflicts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void DeserializeAndSerialize()
public void Verify()
{
var test = new Conflicts() { Hash = _u };
var snapshot = TestBlockchain.GetTestSnapshot();
var snapshot = TestBlockchain.GetTestSnapshotCache();
var key = Ledger.UT_MemoryPool.CreateStorageKey(NativeContract.Ledger.Id, Prefix_Transaction, _u.ToArray());

// Conflicting transaction is in the Conflicts attribute of some other on-chain transaction.
Expand Down
2 changes: 1 addition & 1 deletion tests/Neo.UnitTests/Network/P2P/Payloads/UT_Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void GetHashCodeTest()
public void TrimTest()
{
UInt256 val256 = UInt256.Zero;
var snapshot = TestBlockchain.GetTestSnapshot().CreateSnapshot();
var snapshot = TestBlockchain.GetTestSnapshotCache().CreateSnapshot();
TestUtils.SetupHeaderWithValues(null, uut, val256, out _, out _, out _, out _, out _, out _);
uut.Witness = new Witness() { InvocationScript = Array.Empty<byte>(), VerificationScript = Array.Empty<byte>() };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void DeserializeAndSerialize()
public void Verify()
{
var test = new HighPriorityAttribute();
var snapshot = TestBlockchain.GetTestSnapshot();
var snapshot = TestBlockchain.GetTestSnapshotCache();

Assert.IsFalse(test.Verify(snapshot, new Transaction() { Signers = Array.Empty<Signer>() }));
Assert.IsFalse(test.Verify(snapshot, new Transaction() { Signers = new Signer[] { new Signer() { Account = UInt160.Parse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff01") } } }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void DeserializeAndSerialize()
public void Verify()
{
var test = new NotValidBefore();
var snapshot = TestBlockchain.GetTestSnapshot();
var snapshot = TestBlockchain.GetTestSnapshotCache();
test.Height = NativeContract.Ledger.CurrentIndex(snapshot) + 1;

Assert.IsFalse(test.Verify(snapshot, new Transaction()));
Expand Down
Loading

0 comments on commit 21765dc

Please sign in to comment.