Skip to content

Commit

Permalink
Fix Ut (#3635)
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon authored Dec 20, 2024
1 parent e2acc64 commit 216c39e
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 86 deletions.
15 changes: 13 additions & 2 deletions tests/Neo.UnitTests/SmartContract/UT_ApplicationEngine.Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Extensions;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.VM;
using System.Linq;
Expand All @@ -20,11 +21,20 @@ namespace Neo.UnitTests.SmartContract
{
public partial class UT_ApplicationEngine
{
private DataCache _snapshotCache;

[TestInitialize]
public void TestSetup()
{
_snapshotCache = TestBlockchain.GetTestSnapshotCache();
}

[TestMethod]
public void TestCreateStandardAccount()
{
var snapshot = _snapshotCache.CloneCache();
var settings = TestProtocolSettings.Default;
using var engine = ApplicationEngine.Create(TriggerType.Application, null, null, settings: TestProtocolSettings.Default, gas: 1100_00000000);
using var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestProtocolSettings.Default, gas: 1100_00000000);

using var script = new ScriptBuilder();
script.EmitSysCall(ApplicationEngine.System_Contract_CreateStandardAccount, settings.StandbyCommittee[0].EncodePoint(true));
Expand All @@ -39,8 +49,9 @@ public void TestCreateStandardAccount()
[TestMethod]
public void TestCreateStandardMultisigAccount()
{
var snapshot = _snapshotCache.CloneCache();
var settings = TestProtocolSettings.Default;
using var engine = ApplicationEngine.Create(TriggerType.Application, null, null, settings: TestBlockchain.TheNeoSystem.Settings, gas: 1100_00000000);
using var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheNeoSystem.Settings, gas: 1100_00000000);

using var script = new ScriptBuilder();
script.EmitSysCall(ApplicationEngine.System_Contract_CreateMultisigAccount, new object[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ namespace Neo.UnitTests.SmartContract
[TestClass]
public class UT_ApplicationEngineProvider
{
private DataCache _snapshotCache;

[TestInitialize]
public void TestInitialize()
public void TestSetup()
{
_snapshotCache = TestBlockchain.GetTestSnapshotCache();
ApplicationEngine.Provider = null;
}

Expand All @@ -37,15 +40,17 @@ public void TestCleanup()
public void TestSetAppEngineProvider()
{
ApplicationEngine.Provider = new TestProvider();
var snapshot = _snapshotCache.CloneCache();

using var appEngine = ApplicationEngine.Create(TriggerType.Application, null, null, gas: 0, settings: TestBlockchain.TheNeoSystem.Settings);
using var appEngine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, gas: 0, settings: TestBlockchain.TheNeoSystem.Settings);
(appEngine is TestEngine).Should().BeTrue();
}

[TestMethod]
public void TestDefaultAppEngineProvider()
{
using var appEngine = ApplicationEngine.Create(TriggerType.Application, null, null, gas: 0, settings: TestBlockchain.TheNeoSystem.Settings);
var snapshot = _snapshotCache.CloneCache();
using var appEngine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, gas: 0, settings: TestBlockchain.TheNeoSystem.Settings);
(appEngine is ApplicationEngine).Should().BeTrue();
}

Expand Down
17 changes: 15 additions & 2 deletions tests/Neo.UnitTests/SmartContract/UT_Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Extensions;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
Expand All @@ -26,6 +27,14 @@ namespace Neo.UnitTests.SmartContract
[TestClass]
public class UT_Contract
{
private DataCache _snapshotCache;

[TestInitialize]
public void TestSetup()
{
_snapshotCache = TestBlockchain.GetTestSnapshotCache();
}

[TestMethod]
public void TestGetScriptHash()
{
Expand Down Expand Up @@ -158,6 +167,7 @@ public void TestCreateSignatureRedeemScript()
[TestMethod]
public void TestSignatureRedeemScriptFee()
{
var snapshot = _snapshotCache.CloneCache();
byte[] privateKey = new byte[32];
RandomNumberGenerator rng = RandomNumberGenerator.Create();
rng.GetBytes(privateKey);
Expand All @@ -167,7 +177,8 @@ public void TestSignatureRedeemScriptFee()

var fee = PolicyContract.DefaultExecFeeFactor * (ApplicationEngine.OpCodePriceTable[(byte)OpCode.PUSHDATA1] * 2 + ApplicationEngine.OpCodePriceTable[(byte)OpCode.SYSCALL] + ApplicationEngine.CheckSigPrice);

using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, new Transaction { Signers = Array.Empty<Signer>(), Attributes = Array.Empty<TransactionAttribute>() }, null, settings: TestBlockchain.TheNeoSystem.Settings))
using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification,
new Transaction { Signers = Array.Empty<Signer>(), Attributes = Array.Empty<TransactionAttribute>() }, snapshot, settings: TestBlockchain.TheNeoSystem.Settings))
{
engine.LoadScript(invocation.Concat(verification).ToArray(), configureState: p => p.CallFlags = CallFlags.None);
engine.Execute();
Expand All @@ -178,6 +189,7 @@ public void TestSignatureRedeemScriptFee()
[TestMethod]
public void TestCreateMultiSigRedeemScriptFee()
{
var snapshot = _snapshotCache.CloneCache();
byte[] privateKey1 = new byte[32];
RandomNumberGenerator rng1 = RandomNumberGenerator.Create();
rng1.GetBytes(privateKey1);
Expand All @@ -195,7 +207,8 @@ public void TestCreateMultiSigRedeemScriptFee()

long fee = PolicyContract.DefaultExecFeeFactor * (ApplicationEngine.OpCodePriceTable[(byte)OpCode.PUSHDATA1] * (2 + 2) + ApplicationEngine.OpCodePriceTable[(byte)OpCode.PUSHINT8] * 2 + ApplicationEngine.OpCodePriceTable[(byte)OpCode.SYSCALL] + ApplicationEngine.CheckSigPrice * 2);

using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, new Transaction { Signers = Array.Empty<Signer>(), Attributes = Array.Empty<TransactionAttribute>() }, null, settings: TestBlockchain.TheNeoSystem.Settings))
using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification,
new Transaction { Signers = Array.Empty<Signer>(), Attributes = Array.Empty<TransactionAttribute>() }, snapshot, settings: TestBlockchain.TheNeoSystem.Settings))
{
engine.LoadScript(invocation.Concat(verification).ToArray(), configureState: p => p.CallFlags = CallFlags.None);
engine.Execute();
Expand Down
11 changes: 8 additions & 3 deletions tests/Neo.UnitTests/SmartContract/UT_Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Cryptography.ECC;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
Expand All @@ -27,10 +28,12 @@ namespace Neo.UnitTests.SmartContract
public class UT_Helper
{
private KeyPair _key;
private DataCache _snapshotCache;

[TestInitialize]
public void Init()
public void TestSetup()
{
_snapshotCache = TestBlockchain.GetTestSnapshotCache();
var pk = new byte[32];
new Random().NextBytes(pk);
_key = new KeyPair(pk);
Expand Down Expand Up @@ -118,6 +121,7 @@ public void TestIsSignatureContract_WrongCurve()
[TestMethod]
public void TestSignatureContractCost()
{
var snapshot = _snapshotCache.CloneCache();
var contract = Contract.CreateSignatureContract(_key.PublicKey);

var tx = TestUtils.CreateRandomHashTransaction();
Expand All @@ -127,7 +131,7 @@ public void TestSignatureContractCost()
invocationScript.EmitPush(Neo.Wallets.Helper.Sign(tx, _key, TestProtocolSettings.Default.Network));
tx.Witnesses = new Witness[] { new Witness() { InvocationScript = invocationScript.ToArray(), VerificationScript = contract.Script } };

using var engine = ApplicationEngine.Create(TriggerType.Verification, tx, null, null, TestProtocolSettings.Default);
using var engine = ApplicationEngine.Create(TriggerType.Verification, tx, snapshot, null, TestProtocolSettings.Default);
engine.LoadScript(contract.Script);
engine.LoadScript(new Script(invocationScript.ToArray(), true), configureState: p => p.CallFlags = CallFlags.None);
Assert.AreEqual(VMState.HALT, engine.Execute());
Expand All @@ -139,6 +143,7 @@ public void TestSignatureContractCost()
[TestMethod]
public void TestMultiSignatureContractCost()
{
var snapshot = _snapshotCache.CloneCache();
var contract = Contract.CreateMultiSigContract(1, new ECPoint[] { _key.PublicKey });

var tx = TestUtils.CreateRandomHashTransaction();
Expand All @@ -147,7 +152,7 @@ public void TestMultiSignatureContractCost()
using ScriptBuilder invocationScript = new();
invocationScript.EmitPush(Neo.Wallets.Helper.Sign(tx, _key, TestProtocolSettings.Default.Network));

using var engine = ApplicationEngine.Create(TriggerType.Verification, tx, null, null, TestProtocolSettings.Default);
using var engine = ApplicationEngine.Create(TriggerType.Verification, tx, snapshot, null, TestProtocolSettings.Default);
engine.LoadScript(contract.Script);
engine.LoadScript(new Script(invocationScript.ToArray(), true), configureState: p => p.CallFlags = CallFlags.None);
Assert.AreEqual(VMState.HALT, engine.Execute());
Expand Down
48 changes: 29 additions & 19 deletions tests/Neo.UnitTests/SmartContract/UT_InteropPrices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.UnitTests.Extensions;
using Neo.VM;
Expand All @@ -20,28 +21,37 @@ namespace Neo.UnitTests.SmartContract
[TestClass]
public class UT_InteropPrices
{
private DataCache _snapshotCache;

[TestInitialize]
public void TestSetup()
{
_snapshotCache = TestBlockchain.GetTestSnapshotCache();
}

[TestMethod]
public void ApplicationEngineFixedPrices()
{
var snapshot = _snapshotCache.CloneCache();
// System.Runtime.CheckWitness: f827ec8c (price is 200)
byte[] SyscallSystemRuntimeCheckWitnessHash = new byte[] { 0x68, 0xf8, 0x27, 0xec, 0x8c };
using (ApplicationEngine ae = ApplicationEngine.Create(TriggerType.Application, null, null, gas: 0))
using (ApplicationEngine ae = ApplicationEngine.Create(TriggerType.Application, null, snapshot, gas: 0))
{
ae.LoadScript(SyscallSystemRuntimeCheckWitnessHash);
ApplicationEngine.System_Runtime_CheckWitness.FixedPrice.Should().Be(0_00001024L);
}

// System.Storage.GetContext: 9bf667ce (price is 1)
byte[] SyscallSystemStorageGetContextHash = new byte[] { 0x68, 0x9b, 0xf6, 0x67, 0xce };
using (ApplicationEngine ae = ApplicationEngine.Create(TriggerType.Application, null, null, gas: 0))
using (ApplicationEngine ae = ApplicationEngine.Create(TriggerType.Application, null, snapshot, gas: 0))
{
ae.LoadScript(SyscallSystemStorageGetContextHash);
ApplicationEngine.System_Storage_GetContext.FixedPrice.Should().Be(0_00000016L);
}

// System.Storage.Get: 925de831 (price is 100)
byte[] SyscallSystemStorageGetHash = new byte[] { 0x68, 0x92, 0x5d, 0xe8, 0x31 };
using (ApplicationEngine ae = ApplicationEngine.Create(TriggerType.Application, null, null, gas: 0))
using (ApplicationEngine ae = ApplicationEngine.Create(TriggerType.Application, null, snapshot, gas: 0))
{
ae.LoadScript(SyscallSystemStorageGetHash);
ApplicationEngine.System_Storage_Get.FixedPrice.Should().Be(32768L);
Expand All @@ -54,6 +64,7 @@ public void ApplicationEngineFixedPrices()
[TestMethod]
public void ApplicationEngineRegularPut()
{
var snapshot = _snapshotCache.CloneCache();
var key = new byte[] { (byte)OpCode.PUSH1 };
var value = new byte[] { (byte)OpCode.PUSH1 };

Expand All @@ -64,11 +75,10 @@ public void ApplicationEngineRegularPut()
StorageKey skey = TestUtils.GetStorageKey(contractState.Id, key);
StorageItem sItem = TestUtils.GetStorageItem(System.Array.Empty<byte>());

var snapshotCache = TestBlockchain.GetTestSnapshotCache();
snapshotCache.Add(skey, sItem);
snapshotCache.AddContract(script.ToScriptHash(), contractState);
snapshot.Add(skey, sItem);
snapshot.AddContract(script.ToScriptHash(), contractState);

using ApplicationEngine ae = ApplicationEngine.Create(TriggerType.Application, null, snapshotCache);
using ApplicationEngine ae = ApplicationEngine.Create(TriggerType.Application, null, snapshot);
Debugger debugger = new(ae);
ae.LoadScript(script);
debugger.StepInto();
Expand All @@ -85,6 +95,7 @@ public void ApplicationEngineRegularPut()
[TestMethod]
public void ApplicationEngineReusedStorage_FullReuse()
{
var snapshot = _snapshotCache.CloneCache();
var key = new byte[] { (byte)OpCode.PUSH1 };
var value = new byte[] { (byte)OpCode.PUSH1 };

Expand All @@ -95,11 +106,10 @@ public void ApplicationEngineReusedStorage_FullReuse()
StorageKey skey = TestUtils.GetStorageKey(contractState.Id, key);
StorageItem sItem = TestUtils.GetStorageItem(value);

var snapshotCache = TestBlockchain.GetTestSnapshotCache();
snapshotCache.Add(skey, sItem);
snapshotCache.AddContract(script.ToScriptHash(), contractState);
snapshot.Add(skey, sItem);
snapshot.AddContract(script.ToScriptHash(), contractState);

using ApplicationEngine applicationEngine = ApplicationEngine.Create(TriggerType.Application, null, snapshotCache);
using ApplicationEngine applicationEngine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);
Debugger debugger = new(applicationEngine);
applicationEngine.LoadScript(script);
debugger.StepInto();
Expand All @@ -117,6 +127,7 @@ public void ApplicationEngineReusedStorage_FullReuse()
[TestMethod]
public void ApplicationEngineReusedStorage_PartialReuse()
{
var snapshot = _snapshotCache.CloneCache();
var key = new byte[] { (byte)OpCode.PUSH1 };
var oldValue = new byte[] { (byte)OpCode.PUSH1 };
var value = new byte[] { (byte)OpCode.PUSH1, (byte)OpCode.PUSH1 };
Expand All @@ -128,11 +139,10 @@ public void ApplicationEngineReusedStorage_PartialReuse()
StorageKey skey = TestUtils.GetStorageKey(contractState.Id, key);
StorageItem sItem = TestUtils.GetStorageItem(oldValue);

var snapshotCache = TestBlockchain.GetTestSnapshotCache();
snapshotCache.Add(skey, sItem);
snapshotCache.AddContract(script.ToScriptHash(), contractState);
snapshot.Add(skey, sItem);
snapshot.AddContract(script.ToScriptHash(), contractState);

using ApplicationEngine ae = ApplicationEngine.Create(TriggerType.Application, null, snapshotCache);
using ApplicationEngine ae = ApplicationEngine.Create(TriggerType.Application, null, snapshot);
Debugger debugger = new(ae);
ae.LoadScript(script);
debugger.StepInto();
Expand All @@ -151,6 +161,7 @@ public void ApplicationEngineReusedStorage_PartialReuse()
[TestMethod]
public void ApplicationEngineReusedStorage_PartialReuseTwice()
{
var snapshot = _snapshotCache.CloneCache();
var key = new byte[] { (byte)OpCode.PUSH1 };
var oldValue = new byte[] { (byte)OpCode.PUSH1 };
var value = new byte[] { (byte)OpCode.PUSH1, (byte)OpCode.PUSH1 };
Expand All @@ -162,11 +173,10 @@ public void ApplicationEngineReusedStorage_PartialReuseTwice()
StorageKey skey = TestUtils.GetStorageKey(contractState.Id, key);
StorageItem sItem = TestUtils.GetStorageItem(oldValue);

var snapshotCache = TestBlockchain.GetTestSnapshotCache();
snapshotCache.Add(skey, sItem);
snapshotCache.AddContract(script.ToScriptHash(), contractState);
snapshot.Add(skey, sItem);
snapshot.AddContract(script.ToScriptHash(), contractState);

using ApplicationEngine ae = ApplicationEngine.Create(TriggerType.Application, null, snapshotCache);
using ApplicationEngine ae = ApplicationEngine.Create(TriggerType.Application, null, snapshot);
Debugger debugger = new(ae);
ae.LoadScript(script);
debugger.StepInto(); //push value
Expand Down
8 changes: 4 additions & 4 deletions tests/Neo.UnitTests/SmartContract/UT_InteropService.NEO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public void TestContract_Update_Invalid()
[TestMethod]
public void TestStorage_Find()
{
var snapshotCache = TestBlockchain.GetTestSnapshotCache();
var snapshot = _snapshotCache.CloneCache();
var state = TestUtils.GetContract();

var storageItem = new StorageItem
Expand All @@ -256,9 +256,9 @@ public void TestStorage_Find()
Id = state.Id,
Key = new byte[] { 0x01 }
};
snapshotCache.AddContract(state.Hash, state);
snapshotCache.Add(storageKey, storageItem);
var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshotCache);
snapshot.AddContract(state.Hash, state);
snapshot.Add(storageKey, storageItem);
var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot);
engine.LoadScript(new byte[] { 0x01 });

var iterator = engine.Find(new StorageContext
Expand Down
Loading

0 comments on commit 216c39e

Please sign in to comment.