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

[Neo Core UT] Fix ut and style #3413

Merged
merged 10 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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;
shargon marked this conversation as resolved.
Show resolved Hide resolved
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.GetSnapshot().CreateSnapshot();
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
2 changes: 1 addition & 1 deletion tests/Neo.UnitTests/IO/Caching/UT_CloneCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void TestUpdateInternal()
[TestMethod]
public void TestCacheOverrideIssue2572()
{
var snapshot = TestBlockchain.GetTestSnapshot();
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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This demonstrate how using snapshotcache after committing it is wrong.

myDataCache.TryGet(key2).Should().NotBeNull();
Jim8y marked this conversation as resolved.
Show resolved Hide resolved
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_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(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