diff --git a/src/Neo/Neo.csproj b/src/Neo/Neo.csproj index 238333372b..7c6478c33e 100644 --- a/src/Neo/Neo.csproj +++ b/src/Neo/Neo.csproj @@ -30,6 +30,7 @@ + diff --git a/tests/Neo.Plugins.OracleService.Tests/TestBlockchain.cs b/tests/Neo.Plugins.OracleService.Tests/TestBlockchain.cs index bc6be0d8bf..a4cc2f6fbf 100644 --- a/tests/Neo.Plugins.OracleService.Tests/TestBlockchain.cs +++ b/tests/Neo.Plugins.OracleService.Tests/TestBlockchain.cs @@ -9,6 +9,8 @@ // 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; @@ -16,21 +18,36 @@ 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(); } } } diff --git a/tests/Neo.Plugins.OracleService.Tests/UT_OracleService.cs b/tests/Neo.Plugins.OracleService.Tests/UT_OracleService.cs index 9622ceb9e2..79771ee796 100644 --- a/tests/Neo.Plugins.OracleService.Tests/UT_OracleService.cs +++ b/tests/Neo.Plugins.OracleService.Tests/UT_OracleService.cs @@ -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); @@ -85,7 +85,7 @@ public void TestCreateOracleResponseTx() Filter = "", CallbackContract = UInt160.Zero, CallbackMethod = "callback", - UserData = System.Array.Empty() + UserData = [] }; byte Prefix_Transaction = 11; snapshot.Add(NativeContract.Ledger.CreateStorageKey(Prefix_Transaction, request.OriginalTxid), new StorageItem(new TransactionState() diff --git a/tests/Neo.UnitTests/IO/Caching/UT_CloneCache.cs b/tests/Neo.UnitTests/IO/Caching/UT_CloneCache.cs index 3dd3d2dd67..71eeb4ac25 100644 --- a/tests/Neo.UnitTests/IO/Caching/UT_CloneCache.cs +++ b/tests/Neo.UnitTests/IO/Caching/UT_CloneCache.cs @@ -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 ( diff --git a/tests/Neo.UnitTests/IO/Caching/UT_DataCache.cs b/tests/Neo.UnitTests/IO/Caching/UT_DataCache.cs index 242905a13a..224b1cc3f6 100644 --- a/tests/Neo.UnitTests/IO/Caching/UT_DataCache.cs +++ b/tests/Neo.UnitTests/IO/Caching/UT_DataCache.cs @@ -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] diff --git a/tests/Neo.UnitTests/Ledger/UT_Blockchain.cs b/tests/Neo.UnitTests/Ledger/UT_Blockchain.cs index 2c5ed88ef8..1ccfabbe92 100644 --- a/tests/Neo.UnitTests/Ledger/UT_Blockchain.cs +++ b/tests/Neo.UnitTests/Ledger/UT_Blockchain.cs @@ -38,14 +38,14 @@ public void Initialize() { system = TestBlockchain.TheNeoSystem; senderProbe = CreateTestProbe(); - txSample = new Transaction() + txSample = new Transaction { - Attributes = Array.Empty(), + Attributes = [], Script = Array.Empty(), - Signers = new Signer[] { new Signer() { Account = UInt160.Zero } }, - Witnesses = Array.Empty() + Signers = [new Signer { Account = UInt160.Zero }], + Witnesses = [] }; - system.MemPool.TryAdd(txSample, TestBlockchain.GetTestSnapshot()); + system.MemPool.TryAdd(txSample, TestBlockchain.GetTestSnapshotCache()); } [TestCleanup] diff --git a/tests/Neo.UnitTests/Ledger/UT_TransactionVerificationContext.cs b/tests/Neo.UnitTests/Ledger/UT_TransactionVerificationContext.cs index 32a0bd6237..7805da64b1 100644 --- a/tests/Neo.UnitTests/Ledger/UT_TransactionVerificationContext.cs +++ b/tests/Neo.UnitTests/Ledger/UT_TransactionVerificationContext.cs @@ -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); @@ -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); @@ -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); diff --git a/tests/Neo.UnitTests/Ledger/UT_TrimmedBlock.cs b/tests/Neo.UnitTests/Ledger/UT_TrimmedBlock.cs index ea797f8be3..12b56048c8 100644 --- a/tests/Neo.UnitTests/Ledger/UT_TrimmedBlock.cs +++ b/tests/Neo.UnitTests/Ledger/UT_TrimmedBlock.cs @@ -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, diff --git a/tests/Neo.UnitTests/Network/P2P/Payloads/UT_Block.cs b/tests/Neo.UnitTests/Network/P2P/Payloads/UT_Block.cs index 7793e27597..4a7b0262f6 100644 --- a/tests/Neo.UnitTests/Network/P2P/Payloads/UT_Block.cs +++ b/tests/Neo.UnitTests/Network/P2P/Payloads/UT_Block.cs @@ -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 }); diff --git a/tests/Neo.UnitTests/Network/P2P/Payloads/UT_Conflicts.cs b/tests/Neo.UnitTests/Network/P2P/Payloads/UT_Conflicts.cs index ce46c5106d..b024fed808 100644 --- a/tests/Neo.UnitTests/Network/P2P/Payloads/UT_Conflicts.cs +++ b/tests/Neo.UnitTests/Network/P2P/Payloads/UT_Conflicts.cs @@ -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. diff --git a/tests/Neo.UnitTests/Network/P2P/Payloads/UT_Header.cs b/tests/Neo.UnitTests/Network/P2P/Payloads/UT_Header.cs index 17e0cb894f..0b32ffd656 100644 --- a/tests/Neo.UnitTests/Network/P2P/Payloads/UT_Header.cs +++ b/tests/Neo.UnitTests/Network/P2P/Payloads/UT_Header.cs @@ -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(), VerificationScript = Array.Empty() }; diff --git a/tests/Neo.UnitTests/Network/P2P/Payloads/UT_HighPriorityAttribute.cs b/tests/Neo.UnitTests/Network/P2P/Payloads/UT_HighPriorityAttribute.cs index 76808072d5..e5222e33de 100644 --- a/tests/Neo.UnitTests/Network/P2P/Payloads/UT_HighPriorityAttribute.cs +++ b/tests/Neo.UnitTests/Network/P2P/Payloads/UT_HighPriorityAttribute.cs @@ -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() })); Assert.IsFalse(test.Verify(snapshot, new Transaction() { Signers = new Signer[] { new Signer() { Account = UInt160.Parse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff01") } } })); diff --git a/tests/Neo.UnitTests/Network/P2P/Payloads/UT_NotValidBefore.cs b/tests/Neo.UnitTests/Network/P2P/Payloads/UT_NotValidBefore.cs index 8e7ad9087b..e44d4f3636 100644 --- a/tests/Neo.UnitTests/Network/P2P/Payloads/UT_NotValidBefore.cs +++ b/tests/Neo.UnitTests/Network/P2P/Payloads/UT_NotValidBefore.cs @@ -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())); diff --git a/tests/Neo.UnitTests/Network/P2P/Payloads/UT_Transaction.cs b/tests/Neo.UnitTests/Network/P2P/Payloads/UT_Transaction.cs index 56d9a66bec..1739f1b830 100644 --- a/tests/Neo.UnitTests/Network/P2P/Payloads/UT_Transaction.cs +++ b/tests/Neo.UnitTests/Network/P2P/Payloads/UT_Transaction.cs @@ -100,16 +100,16 @@ public void Gas_Set() public void Size_Get() { uut.Script = TestUtils.GetByteArray(32, 0x42); - uut.Signers = Array.Empty(); - uut.Attributes = Array.Empty(); - uut.Witnesses = new[] - { + uut.Signers = []; + uut.Attributes = []; + uut.Witnesses = + [ new Witness { InvocationScript = Array.Empty(), VerificationScript = Array.Empty() } - }; + ]; uut.Version.Should().Be(0); uut.Script.Length.Should().Be(32); @@ -122,17 +122,16 @@ public void FeeIsMultiSigContract() { var walletA = TestUtils.GenerateTestWallet("123"); var walletB = TestUtils.GenerateTestWallet("123"); - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var a = walletA.CreateAccount(); var b = walletB.CreateAccount(); var multiSignContract = Contract.CreateMultiSigContract(2, - new ECPoint[] - { - a.GetKey().PublicKey, - b.GetKey().PublicKey - }); + [ + a.GetKey().PublicKey, + b.GetKey().PublicKey + ]); walletA.CreateAccount(multiSignContract, a.GetKey()); var acc = walletB.CreateAccount(multiSignContract, b.GetKey()); @@ -148,15 +147,14 @@ public void FeeIsMultiSigContract() // Make transaction - var tx = walletA.MakeTransaction(snapshot, new TransferOutput[] - { - new TransferOutput() - { - AssetId = NativeContract.GAS.Hash, - ScriptHash = acc.ScriptHash, - Value = new BigDecimal(BigInteger.One,8) - } - }, acc.ScriptHash); + var tx = walletA.MakeTransaction(snapshot, [ + new TransferOutput + { + AssetId = NativeContract.GAS.Hash, + ScriptHash = acc.ScriptHash, + Value = new BigDecimal(BigInteger.One, 8) + } + ], acc.ScriptHash); Assert.IsNotNull(tx); @@ -200,7 +198,7 @@ public void FeeIsMultiSigContract() public void FeeIsSignatureContractDetailed() { var wallet = TestUtils.GenerateTestWallet("123"); - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var acc = wallet.CreateAccount(); // Fake balance @@ -216,15 +214,14 @@ public void FeeIsSignatureContractDetailed() // Make transaction // self-transfer of 1e-8 GAS - var tx = wallet.MakeTransaction(snapshot, new TransferOutput[] - { - new TransferOutput() - { - AssetId = NativeContract.GAS.Hash, - ScriptHash = acc.ScriptHash, - Value = new BigDecimal(BigInteger.One,8) - } - }, acc.ScriptHash); + var tx = wallet.MakeTransaction(snapshot, [ + new TransferOutput + { + AssetId = NativeContract.GAS.Hash, + ScriptHash = acc.ScriptHash, + Value = new BigDecimal(BigInteger.One, 8) + } + ], acc.ScriptHash); Assert.IsNotNull(tx); Assert.IsNull(tx.Witnesses); @@ -257,7 +254,7 @@ public void FeeIsSignatureContractDetailed() long verificationGas = 0; foreach (var witness in tx.Witnesses) { - using ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, tx, snapshot, settings: TestBlockchain.TheNeoSystem.Settings, gas: tx.NetworkFee); + using var engine = ApplicationEngine.Create(TriggerType.Verification, tx, snapshot, settings: TestBlockchain.TheNeoSystem.Settings, gas: tx.NetworkFee); engine.LoadScript(witness.VerificationScript); engine.LoadScript(witness.InvocationScript); Assert.AreEqual(VMState.HALT, engine.Execute()); @@ -303,7 +300,7 @@ public void FeeIsSignatureContractDetailed() public void FeeIsSignatureContract_TestScope_Global() { var wallet = TestUtils.GenerateTestWallet(""); - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var acc = wallet.CreateAccount(); // Fake balance @@ -323,14 +320,14 @@ public void FeeIsSignatureContract_TestScope_Global() using (ScriptBuilder sb = new()) { // self-transfer of 1e-8 GAS - BigInteger value = new BigDecimal(BigInteger.One, 8).Value; + var value = new BigDecimal(BigInteger.One, 8).Value; sb.EmitDynamicCall(NativeContract.GAS.Hash, "transfer", acc.ScriptHash, acc.ScriptHash, value, null); sb.Emit(OpCode.ASSERT); script = sb.ToArray(); } // trying global scope - var signers = new Signer[]{ new Signer + var signers = new[]{ new Signer { Account = acc.ScriptHash, Scopes = WitnessScope.Global @@ -382,7 +379,7 @@ public void FeeIsSignatureContract_TestScope_Global() public void FeeIsSignatureContract_TestScope_CurrentHash_GAS() { var wallet = TestUtils.GenerateTestWallet(""); - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var acc = wallet.CreateAccount(); // Fake balance @@ -409,11 +406,11 @@ public void FeeIsSignatureContract_TestScope_CurrentHash_GAS() } // trying global scope - var signers = new Signer[]{ new Signer + var signers = new[]{ new Signer { Account = acc.ScriptHash, Scopes = WitnessScope.CustomContracts, - AllowedContracts = new[] { NativeContract.GAS.Hash } + AllowedContracts = [NativeContract.GAS.Hash] } }; // using this... @@ -462,7 +459,7 @@ public void FeeIsSignatureContract_TestScope_CurrentHash_GAS() public void FeeIsSignatureContract_TestScope_CalledByEntry_Plus_GAS() { var wallet = TestUtils.GenerateTestWallet(""); - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var acc = wallet.CreateAccount(); // Fake balance @@ -482,21 +479,21 @@ public void FeeIsSignatureContract_TestScope_CalledByEntry_Plus_GAS() using (ScriptBuilder sb = new()) { // self-transfer of 1e-8 GAS - BigInteger value = new BigDecimal(BigInteger.One, 8).Value; + var value = new BigDecimal(BigInteger.One, 8).Value; sb.EmitDynamicCall(NativeContract.GAS.Hash, "transfer", acc.ScriptHash, acc.ScriptHash, value, null); sb.Emit(OpCode.ASSERT); script = sb.ToArray(); } // trying CalledByEntry together with GAS - var signers = new Signer[]{ new Signer + var signers = new[]{ new Signer { Account = acc.ScriptHash, // This combination is supposed to actually be an OR, // where it's valid in both Entry and also for Custom hash provided (in any execution level) // it would be better to test this in the future including situations where a deeper call level uses this custom witness successfully Scopes = WitnessScope.CustomContracts | WitnessScope.CalledByEntry, - AllowedContracts = new[] { NativeContract.GAS.Hash } + AllowedContracts = [NativeContract.GAS.Hash] } }; // using this... @@ -545,7 +542,7 @@ public void FeeIsSignatureContract_TestScope_CalledByEntry_Plus_GAS() public void FeeIsSignatureContract_TestScope_CurrentHash_NEO_FAULT() { var wallet = TestUtils.GenerateTestWallet(""); - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var acc = wallet.CreateAccount(); // Fake balance @@ -570,11 +567,11 @@ public void FeeIsSignatureContract_TestScope_CurrentHash_NEO_FAULT() } // trying global scope - var signers = new Signer[]{ new Signer + var signers = new[]{ new Signer { Account = acc.ScriptHash, Scopes = WitnessScope.CustomContracts, - AllowedContracts = new[] { NativeContract.NEO.Hash } + AllowedContracts = [NativeContract.NEO.Hash] } }; // using this... @@ -590,7 +587,7 @@ public void FeeIsSignatureContract_TestScope_CurrentHash_NEO_FAULT() public void FeeIsSignatureContract_TestScope_CurrentHash_NEO_GAS() { var wallet = TestUtils.GenerateTestWallet(""); - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var acc = wallet.CreateAccount(); // Fake balance @@ -617,11 +614,11 @@ public void FeeIsSignatureContract_TestScope_CurrentHash_NEO_GAS() } // trying two custom hashes, for same target account - var signers = new Signer[]{ new Signer + var signers = new[]{ new Signer { Account = acc.ScriptHash, Scopes = WitnessScope.CustomContracts, - AllowedContracts = new[] { NativeContract.NEO.Hash, NativeContract.GAS.Hash } + AllowedContracts = [NativeContract.NEO.Hash, NativeContract.GAS.Hash] } }; // using this... @@ -675,7 +672,7 @@ public void FeeIsSignatureContract_TestScope_CurrentHash_NEO_GAS() public void FeeIsSignatureContract_TestScope_NoScopeFAULT() { var wallet = TestUtils.GenerateTestWallet(""); - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var acc = wallet.CreateAccount(); // Fake balance @@ -701,11 +698,11 @@ public void FeeIsSignatureContract_TestScope_NoScopeFAULT() // trying with no scope var attributes = Array.Empty(); - var signers = new Signer[]{ new Signer + var signers = new[]{ new Signer { Account = acc.ScriptHash, Scopes = WitnessScope.CustomContracts, - AllowedContracts = new[] { NativeContract.NEO.Hash, NativeContract.GAS.Hash } + AllowedContracts = [NativeContract.NEO.Hash, NativeContract.GAS.Hash] } }; // using this... @@ -721,7 +718,7 @@ public void FeeIsSignatureContract_TestScope_NoScopeFAULT() public void FeeIsSignatureContract_UnexistingVerificationContractFAULT() { var wallet = TestUtils.GenerateTestWallet(""); - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var acc = wallet.CreateAccount(); // Fake balance @@ -770,7 +767,7 @@ public void FeeIsSignatureContract_UnexistingVerificationContractFAULT() [TestMethod] public void Transaction_Reverify_Hashes_Length_Unequal_To_Witnesses_Length() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); Transaction txSimple = new() { Version = 0x00, @@ -805,10 +802,11 @@ public void Transaction_Serialize_Deserialize_Simple() SystemFee = (long)BigInteger.Pow(10, 8), // 1 GAS NetworkFee = 0x0000000000000001, ValidUntilBlock = 0x01020304, - Signers = new Signer[] { new Signer() { Account = UInt160.Zero } }, - Attributes = Array.Empty(), - Script = new byte[] { (byte)OpCode.PUSH1 }, - Witnesses = new Witness[] { new Witness() { InvocationScript = Array.Empty(), VerificationScript = Array.Empty() } } + Signers = [new Signer() { Account = UInt160.Zero }], + Attributes = [], + Script = new[] { (byte)OpCode.PUSH1 }, + Witnesses = [new Witness { InvocationScript = Array.Empty(), VerificationScript = Array.Empty() } + ] }; byte[] sTx = txSimple.ToArray(); @@ -826,7 +824,7 @@ public void Transaction_Serialize_Deserialize_Simple() "010000"); // empty witnesses // try to deserialize - Transaction tx2 = Neo.IO.Helper.AsSerializable(sTx); + var tx2 = sTx.AsSerializable(); tx2.Version.Should().Be(0x00); tx2.Nonce.Should().Be(0x01020304); @@ -835,17 +833,16 @@ public void Transaction_Serialize_Deserialize_Simple() tx2.NetworkFee.Should().Be(0x0000000000000001); tx2.ValidUntilBlock.Should().Be(0x01020304); tx2.Attributes.Should().BeEquivalentTo(Array.Empty()); - tx2.Signers.Should().BeEquivalentTo(new[] - { + tx2.Signers.Should().BeEquivalentTo([ new Signer { Account = UInt160.Zero, - AllowedContracts = Array.Empty(), - AllowedGroups = Array.Empty(), - Rules = Array.Empty() + AllowedContracts = [], + AllowedGroups = [], + Rules = [] } - }); - tx2.Script.Span.SequenceEqual(new byte[] { (byte)OpCode.PUSH1 }).Should().BeTrue(); + ]); + tx2.Script.Span.SequenceEqual([(byte)OpCode.PUSH1]).Should().BeTrue(); tx2.Witnesses[0].InvocationScript.Span.IsEmpty.Should().BeTrue(); tx2.Witnesses[0].VerificationScript.Span.IsEmpty.Should().BeTrue(); } @@ -862,25 +859,26 @@ public void Transaction_Serialize_Deserialize_DistinctCosigners() SystemFee = (long)BigInteger.Pow(10, 8), // 1 GAS NetworkFee = 0x0000000000000001, ValidUntilBlock = 0x01020304, - Attributes = Array.Empty(), - Signers = new Signer[] - { - new Signer() + Attributes = [], + Signers = + [ + new Signer { Account = UInt160.Parse("0x0001020304050607080900010203040506070809"), Scopes = WitnessScope.Global }, - new Signer() + new Signer { Account = UInt160.Parse("0x0001020304050607080900010203040506070809"), // same account as above Scopes = WitnessScope.CalledByEntry // different scope, but still, same account (cannot do that) } - }, - Script = new byte[] { (byte)OpCode.PUSH1 }, - Witnesses = new Witness[] { new Witness() { InvocationScript = Array.Empty(), VerificationScript = Array.Empty() } } + ], + Script = new[] { (byte)OpCode.PUSH1 }, + Witnesses = [new Witness { InvocationScript = Array.Empty(), VerificationScript = Array.Empty() } + ] }; - byte[] sTx = txDoubleCosigners.ToArray(); + var sTx = txDoubleCosigners.ToArray(); // no need for detailed hexstring here (see basic tests for it) sTx.ToHexString().Should().Be("000403020100e1f5050000000001000000000000000403020102090807060504030201000908070605040302010080090807060504030201000908070605040302010001000111010000"); @@ -924,16 +922,17 @@ public void Transaction_Serialize_Deserialize_MaxSizeCosigners() SystemFee = (long)BigInteger.Pow(10, 8), // 1 GAS NetworkFee = 0x0000000000000001, ValidUntilBlock = 0x01020304, - Attributes = Array.Empty(), + Attributes = [], Signers = cosigners1, // max + 1 (should fail) - Script = new byte[] { (byte)OpCode.PUSH1 }, - Witnesses = new Witness[] { new Witness() { InvocationScript = Array.Empty(), VerificationScript = Array.Empty() } } + Script = new[] { (byte)OpCode.PUSH1 }, + Witnesses = [new Witness { InvocationScript = Array.Empty(), VerificationScript = Array.Empty() } + ] }; byte[] sTx1 = txCosigners1.ToArray(); // back to transaction (should fail, due to non-distinct cosigners) - Assert.ThrowsException(() => Neo.IO.Helper.AsSerializable(sTx1)); + Assert.ThrowsException(() => sTx1.AsSerializable()); // ---------------------------- // this should fail (max + 1) @@ -941,7 +940,7 @@ public void Transaction_Serialize_Deserialize_MaxSizeCosigners() var cosigners = new Signer[maxCosigners + 1]; for (var i = 0; i < maxCosigners + 1; i++) { - string hex = i.ToString("X4"); + var hex = i.ToString("X4"); while (hex.Length < 40) hex = hex.Insert(0, "0"); cosigners[i] = new Signer @@ -957,10 +956,11 @@ public void Transaction_Serialize_Deserialize_MaxSizeCosigners() SystemFee = (long)BigInteger.Pow(10, 8), // 1 GAS NetworkFee = 0x0000000000000001, ValidUntilBlock = 0x01020304, - Attributes = Array.Empty(), + Attributes = [], Signers = cosigners, // max + 1 (should fail) - Script = new byte[] { (byte)OpCode.PUSH1 }, - Witnesses = new Witness[] { new Witness() { InvocationScript = Array.Empty(), VerificationScript = Array.Empty() } } + Script = new[] { (byte)OpCode.PUSH1 }, + Witnesses = [new Witness { InvocationScript = Array.Empty(), VerificationScript = Array.Empty() } + ] }; byte[] sTx2 = txCosigners.ToArray(); @@ -968,7 +968,7 @@ public void Transaction_Serialize_Deserialize_MaxSizeCosigners() // back to transaction (should fail, due to non-distinct cosigners) Transaction tx2 = null; Assert.ThrowsException(() => - tx2 = Neo.IO.Helper.AsSerializable(sTx2) + tx2 = sTx2.AsSerializable() ); Assert.IsNull(tx2); } @@ -982,7 +982,7 @@ public void FeeIsSignatureContract_TestScope_FeeOnly_Default() cosigner.Scopes.Should().Be(WitnessScope.None); var wallet = TestUtils.GenerateTestWallet(""); - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var acc = wallet.CreateAccount(); // Fake balance @@ -1009,7 +1009,7 @@ public void FeeIsSignatureContract_TestScope_FeeOnly_Default() } // try to use fee only inside the smart contract - var signers = new Signer[]{ new Signer + var signers = new[]{ new Signer { Account = acc.ScriptHash, Scopes = WitnessScope.None @@ -1065,16 +1065,16 @@ public void ToJson() { uut.Script = TestUtils.GetByteArray(32, 0x42); uut.SystemFee = 4200000000; - uut.Signers = new Signer[] { new Signer() { Account = UInt160.Zero } }; - uut.Attributes = Array.Empty(); - uut.Witnesses = new[] - { + uut.Signers = [new Signer { Account = UInt160.Zero }]; + uut.Attributes = []; + uut.Witnesses = + [ new Witness { InvocationScript = Array.Empty(), VerificationScript = Array.Empty() } - }; + ]; JObject jObj = uut.ToJson(ProtocolSettings.Default); jObj.Should().NotBeNull(); @@ -1090,23 +1090,23 @@ public void ToJson() [TestMethod] public void Test_GetAttribute() { - var tx = new Transaction() + var tx = new Transaction { - Attributes = Array.Empty(), + Attributes = [], NetworkFee = 0, Nonce = (uint)Environment.TickCount, Script = new byte[Transaction.MaxTransactionSize], - Signers = new Signer[] { new Signer() { Account = UInt160.Zero } }, + Signers = [new Signer { Account = UInt160.Zero }], SystemFee = 0, ValidUntilBlock = 0, Version = 0, - Witnesses = Array.Empty(), + Witnesses = [], }; Assert.IsNull(tx.GetAttribute()); Assert.IsNull(tx.GetAttribute()); - tx.Attributes = new TransactionAttribute[] { new HighPriorityAttribute() }; + tx.Attributes = [new HighPriorityAttribute()]; Assert.IsNull(tx.GetAttribute()); Assert.IsNotNull(tx.GetAttribute()); @@ -1115,24 +1115,24 @@ public void Test_GetAttribute() [TestMethod] public void Test_VerifyStateIndependent() { - var tx = new Transaction() + var tx = new Transaction { - Attributes = Array.Empty(), + Attributes = [], NetworkFee = 0, Nonce = (uint)Environment.TickCount, Script = new byte[Transaction.MaxTransactionSize], - Signers = new Signer[] { new Signer() { Account = UInt160.Zero } }, + Signers = [new Signer { Account = UInt160.Zero }], SystemFee = 0, ValidUntilBlock = 0, Version = 0, - Witnesses = new[] - { + Witnesses = + [ new Witness { InvocationScript = Array.Empty(), VerificationScript = Array.Empty() } - } + ] }; tx.VerifyStateIndependent(TestProtocolSettings.Default).Should().Be(VerifyResult.OverSize); tx.Script = Array.Empty(); @@ -1140,17 +1140,16 @@ public void Test_VerifyStateIndependent() var walletA = TestUtils.GenerateTestWallet("123"); var walletB = TestUtils.GenerateTestWallet("123"); - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var a = walletA.CreateAccount(); var b = walletB.CreateAccount(); var multiSignContract = Contract.CreateMultiSigContract(2, - new ECPoint[] - { - a.GetKey().PublicKey, - b.GetKey().PublicKey - }); + [ + a.GetKey().PublicKey, + b.GetKey().PublicKey + ]); walletA.CreateAccount(multiSignContract, a.GetKey()); var acc = walletB.CreateAccount(multiSignContract, b.GetKey()); @@ -1166,15 +1165,14 @@ public void Test_VerifyStateIndependent() // Make transaction - tx = walletA.MakeTransaction(snapshot, new TransferOutput[] - { - new TransferOutput() - { - AssetId = NativeContract.GAS.Hash, - ScriptHash = acc.ScriptHash, - Value = new BigDecimal(BigInteger.One,8) - } - }, acc.ScriptHash); + tx = walletA.MakeTransaction(snapshot, [ + new TransferOutput + { + AssetId = NativeContract.GAS.Hash, + ScriptHash = acc.ScriptHash, + Value = new BigDecimal(BigInteger.One, 8) + } + ], acc.ScriptHash); // Sign @@ -1199,35 +1197,36 @@ public void Test_VerifyStateIndependent() [TestMethod] public void Test_VerifyStateDependent() { - var snapshot = TestBlockchain.GetTestSnapshot(); - var height = NativeContract.Ledger.CurrentIndex(snapshot); + var snapshotCache = TestBlockchain.GetTestSnapshotCache(); + var height = NativeContract.Ledger.CurrentIndex(snapshotCache); var tx = new Transaction() { - Attributes = Array.Empty(), + Attributes = [], NetworkFee = 55000, Nonce = (uint)Environment.TickCount, Script = Array.Empty(), - Signers = new Signer[] { new Signer() { Account = UInt160.Zero } }, + Signers = [new Signer() { Account = UInt160.Zero }], SystemFee = 0, ValidUntilBlock = height + 1, Version = 0, - Witnesses = new Witness[] { - new Witness() { InvocationScript = Array.Empty(), VerificationScript = Array.Empty() }, - new Witness() { InvocationScript = Array.Empty(), VerificationScript = new byte[1] } - } + Witnesses = + [ + new Witness { InvocationScript = Array.Empty(), VerificationScript = Array.Empty() }, + new Witness { InvocationScript = Array.Empty(), VerificationScript = new byte[1] } + ] }; // Fake balance var key = NativeContract.GAS.CreateStorageKey(20, tx.Sender); - var balance = snapshot.GetAndChange(key, () => new StorageItem(new AccountState())); + var balance = snapshotCache.GetAndChange(key, () => new StorageItem(new AccountState())); balance.GetInteroperable().Balance = tx.NetworkFee; var conflicts = new List(); - tx.VerifyStateDependent(ProtocolSettings.Default, snapshot, new TransactionVerificationContext(), conflicts).Should().Be(VerifyResult.Invalid); + tx.VerifyStateDependent(ProtocolSettings.Default, snapshotCache, new TransactionVerificationContext(), conflicts).Should().Be(VerifyResult.Invalid); balance.GetInteroperable().Balance = 0; tx.SystemFee = 10; - tx.VerifyStateDependent(ProtocolSettings.Default, snapshot, new TransactionVerificationContext(), conflicts).Should().Be(VerifyResult.InsufficientFunds); + tx.VerifyStateDependent(ProtocolSettings.Default, snapshotCache, new TransactionVerificationContext(), conflicts).Should().Be(VerifyResult.InsufficientFunds); var walletA = TestUtils.GenerateTestWallet("123"); var walletB = TestUtils.GenerateTestWallet("123"); @@ -1236,26 +1235,25 @@ public void Test_VerifyStateDependent() var b = walletB.CreateAccount(); var multiSignContract = Contract.CreateMultiSigContract(2, - new ECPoint[] - { - a.GetKey().PublicKey, - b.GetKey().PublicKey - }); + [ + a.GetKey().PublicKey, + b.GetKey().PublicKey + ]); walletA.CreateAccount(multiSignContract, a.GetKey()); var acc = walletB.CreateAccount(multiSignContract, b.GetKey()); // Fake balance - snapshot = TestBlockchain.GetTestSnapshot(); + snapshotCache = TestBlockchain.GetTestSnapshotCache(); key = NativeContract.GAS.CreateStorageKey(20, acc.ScriptHash); - balance = snapshot.GetAndChange(key, () => new StorageItem(new AccountState())); + balance = snapshotCache.GetAndChange(key, () => new StorageItem(new AccountState())); balance.GetInteroperable().Balance = 10000 * NativeContract.GAS.Factor; // Make transaction - snapshot.Commit(); - tx = walletA.MakeTransaction(snapshot, new TransferOutput[] + snapshotCache.Commit(); + tx = walletA.MakeTransaction(snapshotCache, new[] { new TransferOutput() { @@ -1267,13 +1265,13 @@ public void Test_VerifyStateDependent() // Sign - var data = new ContractParametersContext(snapshot, tx, TestProtocolSettings.Default.Network); + var data = new ContractParametersContext(snapshotCache, tx, TestProtocolSettings.Default.Network); Assert.IsTrue(walletA.Sign(data)); Assert.IsTrue(walletB.Sign(data)); Assert.IsTrue(data.Completed); tx.Witnesses = data.GetWitnesses(); - tx.VerifyStateDependent(TestProtocolSettings.Default, snapshot, new TransactionVerificationContext(), new List()).Should().Be(VerifyResult.Succeed); + tx.VerifyStateDependent(TestProtocolSettings.Default, snapshotCache, new TransactionVerificationContext(), new List()).Should().Be(VerifyResult.Succeed); } [TestMethod] diff --git a/tests/Neo.UnitTests/Network/P2P/Payloads/UT_Witness.cs b/tests/Neo.UnitTests/Network/P2P/Payloads/UT_Witness.cs index 74898eb4fb..6c4f066008 100644 --- a/tests/Neo.UnitTests/Network/P2P/Payloads/UT_Witness.cs +++ b/tests/Neo.UnitTests/Network/P2P/Payloads/UT_Witness.cs @@ -43,7 +43,7 @@ private static Witness PrepareDummyWitness(int pubKeys, int m) { var address = new WalletAccount[pubKeys]; var wallets = new NEP6Wallet[pubKeys]; - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); for (int x = 0; x < pubKeys; x++) { diff --git a/tests/Neo.UnitTests/SmartContract/Native/UT_CryptoLib.cs b/tests/Neo.UnitTests/SmartContract/Native/UT_CryptoLib.cs index f65e061093..00ababb416 100644 --- a/tests/Neo.UnitTests/SmartContract/Native/UT_CryptoLib.cs +++ b/tests/Neo.UnitTests/SmartContract/Native/UT_CryptoLib.cs @@ -45,7 +45,7 @@ public class UT_CryptoLib [TestMethod] public void TestG1() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); using ScriptBuilder script = new(); script.EmitDynamicCall(NativeContract.CryptoLib.Hash, "bls12381Deserialize", g1); @@ -59,7 +59,7 @@ public void TestG1() [TestMethod] public void TestG2() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); using ScriptBuilder script = new(); script.EmitDynamicCall(NativeContract.CryptoLib.Hash, "bls12381Deserialize", g2); @@ -73,7 +73,7 @@ public void TestG2() [TestMethod] public void TestNotG1() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); using ScriptBuilder script = new(); script.EmitDynamicCall(NativeContract.CryptoLib.Hash, "bls12381Deserialize", not_g1); @@ -85,7 +85,7 @@ public void TestNotG1() [TestMethod] public void TestNotG2() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); using ScriptBuilder script = new(); script.EmitDynamicCall(NativeContract.CryptoLib.Hash, "bls12381Deserialize", not_g2); @@ -96,7 +96,7 @@ public void TestNotG2() [TestMethod] public void TestBls12381Add() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); using ScriptBuilder script = new(); script.EmitDynamicCall(NativeContract.CryptoLib.Hash, "bls12381Deserialize", gt); script.EmitDynamicCall(NativeContract.CryptoLib.Hash, "bls12381Deserialize", gt); @@ -119,7 +119,7 @@ public void TestBls12381Mul() { var data = new byte[32]; data[0] = 0x03; - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); using (ScriptBuilder script = new()) { script.EmitPush(false); @@ -161,7 +161,7 @@ public void TestBls12381Mul() [TestMethod] public void TestBls12381Pairing() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); using ScriptBuilder script = new(); script.EmitDynamicCall(NativeContract.CryptoLib.Hash, "bls12381Deserialize", g2); script.EmitDynamicCall(NativeContract.CryptoLib.Hash, "bls12381Deserialize", g1); @@ -182,7 +182,7 @@ public void TestBls12381Pairing() [TestMethod] public void Bls12381Equal() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); using ScriptBuilder script = new(); script.EmitDynamicCall(NativeContract.CryptoLib.Hash, "bls12381Deserialize", g1); script.EmitDynamicCall(NativeContract.CryptoLib.Hash, "bls12381Deserialize", g1); @@ -211,7 +211,7 @@ private void CheckBls12381ScalarMul_Compat(string point, string mul, bool negati { var data = new byte[32]; data[0] = 0x03; - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); using (ScriptBuilder script = new()) { script.EmitPush(negative); @@ -506,7 +506,7 @@ public void TestVerifyWithECDsa_CustomTxWitness_SingleSig() tx.VerifyStateIndependent(TestProtocolSettings.Default).Should().Be(VerifyResult.Succeed); - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); // Create fake balance to pay the fees. ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheNeoSystem.Settings, gas: long.MaxValue); @@ -534,19 +534,19 @@ public void TestVerifyWithECDsa_CustomTxWitness_SingleSig() // NEO-VM 0 > ops // INDEX OPCODE PARAMETER // 0 PUSHINT8 122 (7a) << - // 2 SWAP + // 2 SWAP // 3 PUSHDATA1 02fd0a8c1ce5ae5570fdd46e7599c16b175bf0ebdfe9c178f1ab848fb16dac74a5 // 38 SYSCALL System.Runtime.GetNetwork (c5fba0e0) // 43 PUSHINT64 4294967296 (0000000001000000) - // 52 ADD - // 53 PUSH4 - // 54 LEFT + // 52 ADD + // 53 PUSH4 + // 54 LEFT // 55 SYSCALL System.Runtime.GetScriptContainer (2d510830) - // 60 PUSH0 - // 61 PICKITEM - // 62 CAT - // 63 PUSH4 - // 64 PACK + // 60 PUSH0 + // 61 PICKITEM + // 62 CAT + // 63 PUSH4 + // 64 PACK // 65 PUSH0 // 66 PUSHDATA1 766572696679576974684543447361 ("verifyWithECDsa") // 83 PUSHDATA1 1bf575ab1189688413610a35a12886cde0b66c72 ("NNToUmdQBe5n8o53BTzjTFAnSEcpouyy3B", "0x726cb6e0cd8628a1350a611384688911ab75f51b") @@ -563,17 +563,17 @@ public void TestVerifyWithECDsa_CustomTxWitness_SingleSig() [TestMethod] public void TestVerifyWithECDsa_CustomTxWitness_MultiSig() { - byte[] privkey1 = "b2dde592bfce654ef03f1ceea452d2b0112e90f9f52099bcd86697a2bd0a2b60".HexToBytes(); - ECPoint pubKey1 = ECPoint.Parse("040486468683c112125978ffe876245b2006bfe739aca8539b67335079262cb27ad0dedc9e5583f99b61c6f46bf80b97eaec3654b87add0e5bd7106c69922a229d", ECCurve.Secp256k1); - byte[] privkey2 = "b9879e26941872ee6c9e6f01045681496d8170ed2cc4a54ce617b39ae1891b3a".HexToBytes(); - ECPoint pubKey2 = ECPoint.Parse("040d26fc2ad3b1aae20f040b5f83380670f8ef5c2b2ac921ba3bdd79fd0af0525177715fd4370b1012ddd10579698d186ab342c223da3e884ece9cab9b6638c7bb", ECCurve.Secp256k1); - byte[] privkey3 = "4e1fe2561a6da01ee030589d504d62b23c26bfd56c5e07dfc9b8b74e4602832a".HexToBytes(); - ECPoint pubKey3 = ECPoint.Parse("047b4e72ae854b6a0955b3e02d92651ab7fa641a936066776ad438f95bb674a269a63ff98544691663d91a6cfcd215831f01bfb7a226363a6c5c67ef14541dba07", ECCurve.Secp256k1); - byte[] privkey4 = "6dfd066bb989d3786043aa5c1f0476215d6f5c44f5fc3392dd15e2599b67a728".HexToBytes(); - ECPoint pubKey4 = ECPoint.Parse("04b62ac4c8a352a892feceb18d7e2e3a62c8c1ecbaae5523d89d747b0219276e225be2556a137e0e806e4915762d816cdb43f572730d23bb1b1cba750011c4edc6", ECCurve.Secp256k1); - - // Public keys must be sorted, exactly like for standard CreateMultiSigRedeemScript. - var keys = new List<(byte[], ECPoint)>() + var privkey1 = "b2dde592bfce654ef03f1ceea452d2b0112e90f9f52099bcd86697a2bd0a2b60".HexToBytes(); + var pubKey1 = ECPoint.Parse("040486468683c112125978ffe876245b2006bfe739aca8539b67335079262cb27ad0dedc9e5583f99b61c6f46bf80b97eaec3654b87add0e5bd7106c69922a229d", ECCurve.Secp256k1); + var privkey2 = "b9879e26941872ee6c9e6f01045681496d8170ed2cc4a54ce617b39ae1891b3a".HexToBytes(); + var pubKey2 = ECPoint.Parse("040d26fc2ad3b1aae20f040b5f83380670f8ef5c2b2ac921ba3bdd79fd0af0525177715fd4370b1012ddd10579698d186ab342c223da3e884ece9cab9b6638c7bb", ECCurve.Secp256k1); + var privkey3 = "4e1fe2561a6da01ee030589d504d62b23c26bfd56c5e07dfc9b8b74e4602832a".HexToBytes(); + var pubKey3 = ECPoint.Parse("047b4e72ae854b6a0955b3e02d92651ab7fa641a936066776ad438f95bb674a269a63ff98544691663d91a6cfcd215831f01bfb7a226363a6c5c67ef14541dba07", ECCurve.Secp256k1); + var privkey4 = "6dfd066bb989d3786043aa5c1f0476215d6f5c44f5fc3392dd15e2599b67a728".HexToBytes(); + var pubKey4 = ECPoint.Parse("04b62ac4c8a352a892feceb18d7e2e3a62c8c1ecbaae5523d89d747b0219276e225be2556a137e0e806e4915762d816cdb43f572730d23bb1b1cba750011c4edc6", ECCurve.Secp256k1); + + // Public keys must be sorted, exactly like for standard CreateMultiSigRedeemScript. + var keys = new List<(byte[], ECPoint)> { (privkey1, pubKey1), (privkey2, pubKey2), @@ -583,7 +583,7 @@ public void TestVerifyWithECDsa_CustomTxWitness_MultiSig() // Consider 4 users willing to sign 3/4 multisignature transaction with their Secp256k1 private keys. var m = 3; - var n = keys.Count(); + var n = keys.Count; // Must ensure the following conditions are met before verification script construction: n.Should().BeGreaterThan(0); @@ -607,7 +607,7 @@ public void TestVerifyWithECDsa_CustomTxWitness_MultiSig() // return sigCnt == m // } - // vrf is a builder of M out of N multisig witness verification script corresponding to the public keys. + // vrf is a builder of M out of N multisig witness verification script corresponding to the public keys. using ScriptBuilder vrf = new(); // Start the same way as regular multisig script. @@ -747,12 +747,16 @@ public void TestVerifyWithECDsa_CustomTxWitness_MultiSig() tx.VerifyStateIndependent(TestProtocolSettings.Default).Should().Be(VerifyResult.Succeed); - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); // Create fake balance to pay the fees. - ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheNeoSystem.Settings, gas: long.MaxValue); + var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheNeoSystem.Settings, gas: long.MaxValue); _ = NativeContract.GAS.Mint(engine, acc, 5_0000_0000, false); - snapshot.Commit(); + + // We should not use commit here cause once its committed, the value we get from the snapshot can be different + // from the underline storage. Thought there isn't any issue triggered here, its wrong to use it this way. + // We should either ignore the commit, or get a new snapshot of the store after the commit. + // snapshot.Commit(); // Check that witness verification passes. var txVrfContext = new TransactionVerificationContext(); @@ -780,65 +784,65 @@ public void TestVerifyWithECDsa_CustomTxWitness_MultiSig() // 36 PUSHDATA1 030d26fc2ad3b1aae20f040b5f83380670f8ef5c2b2ac921ba3bdd79fd0af05251 // 71 PUSHDATA1 037b4e72ae854b6a0955b3e02d92651ab7fa641a936066776ad438f95bb674a269 // 106 PUSHDATA1 02b62ac4c8a352a892feceb18d7e2e3a62c8c1ecbaae5523d89d747b0219276e22 - // 141 PUSH4 + // 141 PUSH4 // 142 INITSLOT 7 local, 0 arg - // 145 STLOC5 - // 146 LDLOC5 - // 147 PACK - // 148 STLOC1 - // 149 STLOC6 - // 150 DEPTH - // 151 LDLOC6 + // 145 STLOC5 + // 146 LDLOC5 + // 147 PACK + // 148 STLOC1 + // 149 STLOC6 + // 150 DEPTH + // 151 LDLOC6 // 152 JMPEQ 155 (3/03) - // 154 ABORT - // 155 LDLOC6 - // 156 PACK - // 157 STLOC0 + // 154 ABORT + // 155 LDLOC6 + // 156 PACK + // 157 STLOC0 // 158 SYSCALL System.Runtime.GetNetwork (c5fba0e0) // 163 PUSHINT64 4294967296 (0000000001000000) - // 172 ADD - // 173 PUSH4 - // 174 LEFT + // 172 ADD + // 173 PUSH4 + // 174 LEFT // 175 SYSCALL System.Runtime.GetScriptContainer (2d510830) - // 180 PUSH0 - // 181 PICKITEM - // 182 CAT - // 183 STLOC2 - // 184 PUSH0 - // 185 STLOC3 - // 186 PUSH0 - // 187 STLOC4 - // 188 LDLOC3 - // 189 LDLOC6 - // 190 GE - // 191 LDLOC4 - // 192 LDLOC5 - // 193 GE - // 194 OR + // 180 PUSH0 + // 181 PICKITEM + // 182 CAT + // 183 STLOC2 + // 184 PUSH0 + // 185 STLOC3 + // 186 PUSH0 + // 187 STLOC4 + // 188 LDLOC3 + // 189 LDLOC6 + // 190 GE + // 191 LDLOC4 + // 192 LDLOC5 + // 193 GE + // 194 OR // 195 JMPIF 261 (66/42) // 197 PUSHINT8 122 (7a) - // 199 LDLOC0 - // 200 LDLOC3 - // 201 PICKITEM - // 202 LDLOC1 - // 203 LDLOC4 - // 204 PICKITEM - // 205 LDLOC2 - // 206 PUSH4 - // 207 PACK + // 199 LDLOC0 + // 200 LDLOC3 + // 201 PICKITEM + // 202 LDLOC1 + // 203 LDLOC4 + // 204 PICKITEM + // 205 LDLOC2 + // 206 PUSH4 + // 207 PACK // 208 PUSH0 // 209 PUSHDATA1 766572696679576974684543447361 ("verifyWithECDsa") // 226 PUSHDATA1 1bf575ab1189688413610a35a12886cde0b66c72 ("NNToUmdQBe5n8o53BTzjTFAnSEcpouyy3B", "0x726cb6e0cd8628a1350a611384688911ab75f51b") // 248 SYSCALL System.Contract.Call (627d5b52) - // 253 LDLOC3 - // 254 ADD - // 255 STLOC3 - // 256 LDLOC4 - // 257 INC - // 258 STLOC4 + // 253 LDLOC3 + // 254 ADD + // 255 STLOC3 + // 256 LDLOC4 + // 257 INC + // 258 STLOC4 // 259 JMP 188 (-71/b9) - // 261 LDLOC3 - // 262 LDLOC6 + // 261 LDLOC3 + // 262 LDLOC6 // 263 NUMEQUAL } @@ -885,7 +889,7 @@ public void TestVerifyWithECDsa() private bool CallVerifyWithECDsa(byte[] message, ECPoint pub, byte[] signature, NamedCurveHash curveHash) { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); using (ScriptBuilder script = new()) { script.EmitPush((int)curveHash); diff --git a/tests/Neo.UnitTests/SmartContract/Native/UT_FungibleToken.cs b/tests/Neo.UnitTests/SmartContract/Native/UT_FungibleToken.cs index 5d6d00986e..6e3c18999a 100644 --- a/tests/Neo.UnitTests/SmartContract/Native/UT_FungibleToken.cs +++ b/tests/Neo.UnitTests/SmartContract/Native/UT_FungibleToken.cs @@ -22,7 +22,7 @@ public class UT_FungibleToken : TestKit [TestMethod] public void TestTotalSupply() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); NativeContract.GAS.TotalSupply(snapshot).Should().Be(5200000050000000); } } diff --git a/tests/Neo.UnitTests/SmartContract/Native/UT_GasToken.cs b/tests/Neo.UnitTests/SmartContract/Native/UT_GasToken.cs index 1557f9778d..ebb66f0dff 100644 --- a/tests/Neo.UnitTests/SmartContract/Native/UT_GasToken.cs +++ b/tests/Neo.UnitTests/SmartContract/Native/UT_GasToken.cs @@ -33,7 +33,7 @@ public class UT_GasToken [TestInitialize] public void TestSetup() { - _snapshot = TestBlockchain.GetTestSnapshot(); + _snapshot = TestBlockchain.GetTestSnapshotCache(); _persistingBlock = new Block { Header = new Header() }; } diff --git a/tests/Neo.UnitTests/SmartContract/Native/UT_NativeContract.cs b/tests/Neo.UnitTests/SmartContract/Native/UT_NativeContract.cs index 707f8e6754..7c1d874b1b 100644 --- a/tests/Neo.UnitTests/SmartContract/Native/UT_NativeContract.cs +++ b/tests/Neo.UnitTests/SmartContract/Native/UT_NativeContract.cs @@ -37,7 +37,7 @@ public class UT_NativeContract [TestInitialize] public void TestSetup() { - _snapshot = TestBlockchain.GetTestSnapshot(); + _snapshot = TestBlockchain.GetTestSnapshotCache(); _nativeStates = new Dictionary { {"ContractManagement", """{"id":-1,"updatecounter":0,"hash":"0xfffdc93764dbaddd97c48f252a53ea4643faa3fd","nef":{"magic":860243278,"compiler":"neo-core-v3.0","source":"","tokens":[],"script":"EEEa93tnQBBBGvd7Z0AQQRr3e2dAEEEa93tnQBBBGvd7Z0AQQRr3e2dAEEEa93tnQBBBGvd7Z0AQQRr3e2dAEEEa93tnQBBBGvd7Z0A=","checksum":1094259016},"manifest":{"name":"ContractManagement","groups":[],"features":{},"supportedstandards":[],"abi":{"methods":[{"name":"deploy","parameters":[{"name":"nefFile","type":"ByteArray"},{"name":"manifest","type":"ByteArray"}],"returntype":"Array","offset":0,"safe":false},{"name":"deploy","parameters":[{"name":"nefFile","type":"ByteArray"},{"name":"manifest","type":"ByteArray"},{"name":"data","type":"Any"}],"returntype":"Array","offset":7,"safe":false},{"name":"destroy","parameters":[],"returntype":"Void","offset":14,"safe":false},{"name":"getContract","parameters":[{"name":"hash","type":"Hash160"}],"returntype":"Array","offset":21,"safe":true},{"name":"getContractById","parameters":[{"name":"id","type":"Integer"}],"returntype":"Array","offset":28,"safe":true},{"name":"getContractHashes","parameters":[],"returntype":"InteropInterface","offset":35,"safe":true},{"name":"getMinimumDeploymentFee","parameters":[],"returntype":"Integer","offset":42,"safe":true},{"name":"hasMethod","parameters":[{"name":"hash","type":"Hash160"},{"name":"method","type":"String"},{"name":"pcount","type":"Integer"}],"returntype":"Boolean","offset":49,"safe":true},{"name":"setMinimumDeploymentFee","parameters":[{"name":"value","type":"Integer"}],"returntype":"Void","offset":56,"safe":false},{"name":"update","parameters":[{"name":"nefFile","type":"ByteArray"},{"name":"manifest","type":"ByteArray"}],"returntype":"Void","offset":63,"safe":false},{"name":"update","parameters":[{"name":"nefFile","type":"ByteArray"},{"name":"manifest","type":"ByteArray"},{"name":"data","type":"Any"}],"returntype":"Void","offset":70,"safe":false}],"events":[{"name":"Deploy","parameters":[{"name":"Hash","type":"Hash160"}]},{"name":"Update","parameters":[{"name":"Hash","type":"Hash160"}]},{"name":"Destroy","parameters":[{"name":"Hash","type":"Hash160"}]}]},"permissions":[{"contract":"*","methods":"*"}],"trusts":[],"extra":null}}""" }, diff --git a/tests/Neo.UnitTests/SmartContract/Native/UT_NeoToken.cs b/tests/Neo.UnitTests/SmartContract/Native/UT_NeoToken.cs index 92f1e0e5f2..b08898aad5 100644 --- a/tests/Neo.UnitTests/SmartContract/Native/UT_NeoToken.cs +++ b/tests/Neo.UnitTests/SmartContract/Native/UT_NeoToken.cs @@ -36,7 +36,7 @@ public class UT_NeoToken [TestInitialize] public void TestSetup() { - _snapshot = TestBlockchain.GetTestSnapshot(); + _snapshot = TestBlockchain.GetTestSnapshotCache(); _persistingBlock = new Block { Header = new Header(), @@ -569,7 +569,7 @@ public void TestCalculateBonus() [TestMethod] public void TestGetNextBlockValidators1() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var result = (VM.Types.Array)NativeContract.NEO.Call(snapshot, "getNextBlockValidators"); result.Count.Should().Be(7); result[0].GetSpan().ToHexString().Should().Be("02486fd15702c4490a26703112a5cc1d0923fd697a33406bd5a1c00e0013b09a70"); @@ -599,7 +599,7 @@ public void TestGetNextBlockValidators2() [TestMethod] public void TestGetCandidates1() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var array = (VM.Types.Array)NativeContract.NEO.Call(snapshot, "getCandidates"); array.Count.Should().Be(0); } @@ -671,7 +671,7 @@ public void TestCheckCandidate() [TestMethod] public void TestGetCommittee() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var result = (VM.Types.Array)NativeContract.NEO.Call(snapshot, "getCommittee"); result.Count.Should().Be(21); result[0].GetSpan().ToHexString().Should().Be("020f2887f41474cfeb11fd262e982051c1541418137c02a0f4961af911045de639"); diff --git a/tests/Neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs b/tests/Neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs index a8d9495b5c..62052ad4bf 100644 --- a/tests/Neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs +++ b/tests/Neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs @@ -31,7 +31,7 @@ public class UT_PolicyContract [TestInitialize] public void TestSetup() { - _snapshot = TestBlockchain.GetTestSnapshot(); + _snapshot = TestBlockchain.GetTestSnapshotCache(); ApplicationEngine engine = ApplicationEngine.Create(TriggerType.OnPersist, null, _snapshot, new Block { Header = new Header() }, settings: TestBlockchain.TheNeoSystem.Settings, gas: 0); NativeContract.ContractManagement.OnPersistAsync(engine); diff --git a/tests/Neo.UnitTests/SmartContract/Native/UT_RoleManagement.cs b/tests/Neo.UnitTests/SmartContract/Native/UT_RoleManagement.cs index 7db5ce4e01..1fbb2a47a0 100644 --- a/tests/Neo.UnitTests/SmartContract/Native/UT_RoleManagement.cs +++ b/tests/Neo.UnitTests/SmartContract/Native/UT_RoleManagement.cs @@ -34,7 +34,7 @@ public class UT_RoleManagement [TestInitialize] public void TestSetup() { - _snapshot = TestBlockchain.GetTestSnapshot(); + _snapshot = TestBlockchain.GetTestSnapshotCache(); } [TestCleanup] diff --git a/tests/Neo.UnitTests/SmartContract/Native/UT_StdLib.cs b/tests/Neo.UnitTests/SmartContract/Native/UT_StdLib.cs index b5c9c198ec..28835aa55e 100644 --- a/tests/Neo.UnitTests/SmartContract/Native/UT_StdLib.cs +++ b/tests/Neo.UnitTests/SmartContract/Native/UT_StdLib.cs @@ -62,7 +62,7 @@ public void TestItoaAtoi() [TestMethod] public void MemoryCompare() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); using (var script = new ScriptBuilder()) { @@ -87,7 +87,7 @@ public void MemoryCompare() [TestMethod] public void CheckDecodeEncode() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); using (ScriptBuilder script = new()) { @@ -141,7 +141,7 @@ public void CheckDecodeEncode() [TestMethod] public void MemorySearch() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); using (var script = new ScriptBuilder()) { @@ -207,7 +207,7 @@ public void MemorySearch() [TestMethod] public void StringSplit() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); using var script = new ScriptBuilder(); script.EmitDynamicCall(NativeContract.StdLib.Hash, "stringSplit", "a,b", ","); @@ -227,7 +227,7 @@ public void StringSplit() [TestMethod] public void StringElementLength() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); using var script = new ScriptBuilder(); script.EmitDynamicCall(NativeContract.StdLib.Hash, "strLen", "🦆"); @@ -250,7 +250,7 @@ public void TestInvalidUtf8Sequence() // Simulating invalid UTF-8 byte (0xff) decoded as a UTF-16 char const char badChar = (char)0xff; var badStr = badChar.ToString(); - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); using var script = new ScriptBuilder(); script.EmitDynamicCall(NativeContract.StdLib.Hash, "strLen", badStr); @@ -268,7 +268,7 @@ public void TestInvalidUtf8Sequence() [TestMethod] public void Json_Deserialize() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); // Good @@ -317,7 +317,7 @@ public void Json_Deserialize() [TestMethod] public void Json_Serialize() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); // Good @@ -367,7 +367,7 @@ public void Json_Serialize() [TestMethod] public void TestRuntime_Serialize() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); // Good @@ -388,7 +388,7 @@ public void TestRuntime_Serialize() [TestMethod] public void TestRuntime_Deserialize() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); // Good diff --git a/tests/Neo.UnitTests/SmartContract/UT_ApplicationEngine.cs b/tests/Neo.UnitTests/SmartContract/UT_ApplicationEngine.cs index 0969e12776..9a150c2b22 100644 --- a/tests/Neo.UnitTests/SmartContract/UT_ApplicationEngine.cs +++ b/tests/Neo.UnitTests/SmartContract/UT_ApplicationEngine.cs @@ -30,7 +30,7 @@ public partial class UT_ApplicationEngine [TestMethod] public void TestNotify() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); using var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheNeoSystem.Settings); engine.LoadScript(System.Array.Empty()); ApplicationEngine.Notify += Test_Notify1; @@ -66,7 +66,7 @@ private void Test_Notify2(object sender, NotifyEventArgs e) [TestMethod] public void TestCreateDummyBlock() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); byte[] SyscallSystemRuntimeCheckWitnessHash = new byte[] { 0x68, 0xf8, 0x27, 0xec, 0x8c }; ApplicationEngine engine = ApplicationEngine.Run(SyscallSystemRuntimeCheckWitnessHash, snapshot, settings: TestProtocolSettings.Default); engine.PersistingBlock.Version.Should().Be(0); @@ -111,7 +111,7 @@ public void TestCheckingHardfork() public void TestSystem_Contract_Call_Permissions() { UInt160 scriptHash; - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); // Setup: put a simple contract to the storage. using (var script = new ScriptBuilder()) diff --git a/tests/Neo.UnitTests/SmartContract/UT_ContractParameterContext.cs b/tests/Neo.UnitTests/SmartContract/UT_ContractParameterContext.cs index aba90cc6f0..8d22a0391c 100644 --- a/tests/Neo.UnitTests/SmartContract/UT_ContractParameterContext.cs +++ b/tests/Neo.UnitTests/SmartContract/UT_ContractParameterContext.cs @@ -43,7 +43,7 @@ public static void ClassSetUp(TestContext ctx) [TestMethod] public void TestGetComplete() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); Transaction tx = TestUtils.GetTransaction(UInt160.Parse("0x1bd5c777ec35768892bd3daab60fb7a1cb905066")); var context = new ContractParametersContext(snapshot, tx, TestProtocolSettings.Default.Network); context.Completed.Should().BeFalse(); @@ -52,7 +52,7 @@ public void TestGetComplete() [TestMethod] public void TestToString() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); Transaction tx = TestUtils.GetTransaction(UInt160.Parse("0x1bd5c777ec35768892bd3daab60fb7a1cb905066")); var context = new ContractParametersContext(snapshot, tx, TestProtocolSettings.Default.Network); context.Add(contract, 0, new byte[] { 0x01 }); @@ -63,7 +63,7 @@ public void TestToString() [TestMethod] public void TestParse() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var ret = ContractParametersContext.Parse("{\"type\":\"Neo.Network.P2P.Payloads.Transaction\",\"data\":\"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFmUJDLobcPtqo9vZKIdjXsd8fVGwEAARI=\",\"items\":{\"0xbecaad15c0ea585211faf99738a4354014f177f2\":{\"script\":\"IQJv8DuUkkHOHa3UNRnmlg4KhbQaaaBcMoEDqivOFZTKFmh0dHaq\",\"parameters\":[{\"type\":\"Signature\",\"value\":\"AQ==\"}],\"signatures\":{\"03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c\":\"AQ==\"}}},\"network\":" + TestProtocolSettings.Default.Network + "}", snapshot); ret.ScriptHashes[0].ToString().Should().Be("0x1bd5c777ec35768892bd3daab60fb7a1cb905066"); ((Transaction)ret.Verifiable).Script.Span.ToHexString().Should().Be(new byte[] { 18 }.ToHexString()); @@ -72,7 +72,7 @@ public void TestParse() [TestMethod] public void TestFromJson() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); Action action = () => ContractParametersContext.Parse("{\"type\":\"wrongType\",\"data\":\"00000000007c97764845172d827d3c863743293931a691271a0000000000000000000000000000000000000000000100\",\"items\":{\"0x1bd5c777ec35768892bd3daab60fb7a1cb905066\":{\"script\":\"21026ff03b949241ce1dadd43519e6960e0a85b41a69a05c328103aa2bce1594ca1650680a906ad4\",\"parameters\":[{\"type\":\"Signature\",\"value\":\"01\"}]}}}", snapshot); action.Should().Throw(); } @@ -80,7 +80,7 @@ public void TestFromJson() [TestMethod] public void TestAdd() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); Transaction tx = TestUtils.GetTransaction(UInt160.Zero); var context1 = new ContractParametersContext(snapshot, tx, TestProtocolSettings.Default.Network); context1.Add(contract, 0, new byte[] { 0x01 }).Should().BeFalse(); @@ -95,7 +95,7 @@ public void TestAdd() [TestMethod] public void TestGetParameter() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); Transaction tx = TestUtils.GetTransaction(UInt160.Parse("0x902e0d38da5e513b6d07c1c55b85e77d3dce8063")); var context = new ContractParametersContext(snapshot, tx, TestProtocolSettings.Default.Network); context.GetParameter(tx.Sender, 0).Should().BeNull(); @@ -108,7 +108,7 @@ public void TestGetParameter() [TestMethod] public void TestGetWitnesses() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); Transaction tx = TestUtils.GetTransaction(UInt160.Parse("0x902e0d38da5e513b6d07c1c55b85e77d3dce8063")); var context = new ContractParametersContext(snapshot, tx, TestProtocolSettings.Default.Network); context.Add(contract, 0, new byte[] { 0x01 }); @@ -121,7 +121,7 @@ public void TestGetWitnesses() [TestMethod] public void TestAddSignature() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var singleSender = UInt160.Parse("0x902e0d38da5e513b6d07c1c55b85e77d3dce8063"); Transaction tx = TestUtils.GetTransaction(singleSender); diff --git a/tests/Neo.UnitTests/SmartContract/UT_InteropPrices.cs b/tests/Neo.UnitTests/SmartContract/UT_InteropPrices.cs index 581bf42751..fff5ef1877 100644 --- a/tests/Neo.UnitTests/SmartContract/UT_InteropPrices.cs +++ b/tests/Neo.UnitTests/SmartContract/UT_InteropPrices.cs @@ -64,7 +64,7 @@ public void ApplicationEngineRegularPut() StorageKey skey = TestUtils.GetStorageKey(contractState.Id, key); StorageItem sItem = TestUtils.GetStorageItem(System.Array.Empty()); - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); snapshot.Add(skey, sItem); snapshot.AddContract(script.ToScriptHash(), contractState); @@ -95,7 +95,7 @@ public void ApplicationEngineReusedStorage_FullReuse() StorageKey skey = TestUtils.GetStorageKey(contractState.Id, key); StorageItem sItem = TestUtils.GetStorageItem(value); - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); snapshot.Add(skey, sItem); snapshot.AddContract(script.ToScriptHash(), contractState); @@ -128,7 +128,7 @@ public void ApplicationEngineReusedStorage_PartialReuse() StorageKey skey = TestUtils.GetStorageKey(contractState.Id, key); StorageItem sItem = TestUtils.GetStorageItem(oldValue); - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); snapshot.Add(skey, sItem); snapshot.AddContract(script.ToScriptHash(), contractState); @@ -162,7 +162,7 @@ public void ApplicationEngineReusedStorage_PartialReuseTwice() StorageKey skey = TestUtils.GetStorageKey(contractState.Id, key); StorageItem sItem = TestUtils.GetStorageItem(oldValue); - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); snapshot.Add(skey, sItem); snapshot.AddContract(script.ToScriptHash(), contractState); diff --git a/tests/Neo.UnitTests/SmartContract/UT_InteropService.NEO.cs b/tests/Neo.UnitTests/SmartContract/UT_InteropService.NEO.cs index 4f0767bf2c..a2e22dbff3 100644 --- a/tests/Neo.UnitTests/SmartContract/UT_InteropService.NEO.cs +++ b/tests/Neo.UnitTests/SmartContract/UT_InteropService.NEO.cs @@ -113,7 +113,7 @@ public void TestCrypto_CheckMultiSig() [TestMethod] public void TestContract_Create() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var nef = new NefFile() { Script = Enumerable.Repeat((byte)OpCode.RET, byte.MaxValue).ToArray(), @@ -160,7 +160,7 @@ public void TestContract_Create() [TestMethod] public void TestContract_Update() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var nef = new NefFile() { Script = new[] { (byte)OpCode.RET }, @@ -221,7 +221,7 @@ public void TestContract_Update_Invalid() }; nefFile.CheckSum = NefFile.ComputeChecksum(nefFile); - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); Assert.ThrowsException(() => snapshot.UpdateContract(null, null, new byte[] { 0x01 })); Assert.ThrowsException(() => snapshot.UpdateContract(null, nefFile.ToArray(), null)); @@ -243,7 +243,7 @@ public void TestContract_Update_Invalid() [TestMethod] public void TestStorage_Find() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var state = TestUtils.GetContract(); var storageItem = new StorageItem diff --git a/tests/Neo.UnitTests/SmartContract/UT_InteropService.cs b/tests/Neo.UnitTests/SmartContract/UT_InteropService.cs index 1edcfbd9ca..48203a644a 100644 --- a/tests/Neo.UnitTests/SmartContract/UT_InteropService.cs +++ b/tests/Neo.UnitTests/SmartContract/UT_InteropService.cs @@ -38,7 +38,7 @@ public partial class UT_InteropService : TestKit public void Runtime_GetNotifications_Test() { UInt160 scriptHash2; - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); using (var script = new ScriptBuilder()) { @@ -500,7 +500,7 @@ public void TestBlockchain_GetContract() 0x01, 0x01, 0x01, 0x01, 0x01 }; NativeContract.ContractManagement.GetContract(engine.Snapshot, new UInt160(data1)).Should().BeNull(); - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var state = TestUtils.GetContract(); snapshot.AddContract(state.Hash, state); engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot); @@ -532,7 +532,7 @@ public void TestBlockchain_ListContracts() var list = NativeContract.ContractManagement.ListContracts(engine.Snapshot); list.ForEach(p => p.Id.Should().BeLessThan(0)); - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var state = TestUtils.GetContract(); snapshot.AddContract(state.Hash, state); engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot); @@ -566,7 +566,7 @@ public void TestStorage_GetReadOnlyContext() [TestMethod] public void TestStorage_Get() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var state = TestUtils.GetContract(); var storageKey = new StorageKey @@ -624,7 +624,7 @@ public void TestStorage_Put() Assert.ThrowsException(() => engine.Put(storageContext, key, value)); //storage value is constant - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var storageKey = new StorageKey { @@ -654,7 +654,7 @@ public void TestStorage_Put() public void TestStorage_Delete() { var engine = GetEngine(false, true); - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var state = TestUtils.GetContract(); var storageKey = new StorageKey { @@ -697,7 +697,7 @@ public void TestStorageContext_AsReadOnly() [TestMethod] public void TestContract_Call() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var method = "method"; var args = new VM.Types.Array { 0, 1 }; var state = TestUtils.GetContract(method, args.Count); @@ -728,7 +728,7 @@ public void TestContract_Call() [TestMethod] public void TestContract_Destroy() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var state = TestUtils.GetContract(); var scriptHash = UInt160.Parse("0xcb9f3b7c6fb1cf2c13a40637c189bdd066a272b4"); var storageItem = new StorageItem @@ -769,7 +769,7 @@ public static void LogEvent(object sender, LogEventArgs args) 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 }); diff --git a/tests/Neo.UnitTests/SmartContract/UT_SmartContractHelper.cs b/tests/Neo.UnitTests/SmartContract/UT_SmartContractHelper.cs index f864fbe749..ceb13a2a64 100644 --- a/tests/Neo.UnitTests/SmartContract/UT_SmartContractHelper.cs +++ b/tests/Neo.UnitTests/SmartContract/UT_SmartContractHelper.cs @@ -124,7 +124,7 @@ public void TestIsStandardContract() [TestMethod] public void TestVerifyWitnesses() { - var snapshot1 = TestBlockchain.GetTestSnapshot().CloneCache(); + var snapshot1 = TestBlockchain.GetTestSnapshotCache().CreateSnapshot(); UInt256 index1 = UInt256.Parse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff01"); TestUtils.BlocksAdd(snapshot1, index1, new TrimmedBlock() { @@ -141,7 +141,7 @@ public void TestVerifyWitnesses() TestUtils.BlocksDelete(snapshot1, index1); Assert.AreEqual(false, Neo.SmartContract.Helper.VerifyWitnesses(new Header() { PrevHash = index1 }, TestProtocolSettings.Default, snapshot1, 100)); - var snapshot2 = TestBlockchain.GetTestSnapshot(); + var snapshot2 = TestBlockchain.GetTestSnapshotCache(); UInt256 index2 = UInt256.Parse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff01"); TrimmedBlock block2 = new() { @@ -162,7 +162,7 @@ public void TestVerifyWitnesses() snapshot2.DeleteContract(UInt160.Zero); Assert.AreEqual(false, Neo.SmartContract.Helper.VerifyWitnesses(header2, TestProtocolSettings.Default, snapshot2, 100)); - var snapshot3 = TestBlockchain.GetTestSnapshot(); + var snapshot3 = TestBlockchain.GetTestSnapshotCache(); UInt256 index3 = UInt256.Parse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff01"); TrimmedBlock block3 = new() { diff --git a/tests/Neo.UnitTests/SmartContract/UT_Syscalls.cs b/tests/Neo.UnitTests/SmartContract/UT_Syscalls.cs index e027fa4d88..43448ef218 100644 --- a/tests/Neo.UnitTests/SmartContract/UT_Syscalls.cs +++ b/tests/Neo.UnitTests/SmartContract/UT_Syscalls.cs @@ -62,7 +62,7 @@ public void System_Blockchain_GetBlock() Hashes = new[] { tx.Hash } }; - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); using ScriptBuilder script = new(); script.EmitDynamicCall(NativeContract.Ledger.Hash, "getBlock", block.Hash.ToArray()); @@ -117,7 +117,7 @@ public void System_Blockchain_GetBlock() [TestMethod] public void System_ExecutionEngine_GetScriptContainer() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); using ScriptBuilder script = new(); script.EmitSysCall(ApplicationEngine.System_Runtime_GetScriptContainer); @@ -166,7 +166,7 @@ public void System_ExecutionEngine_GetScriptContainer() [TestMethod] public void System_Runtime_GasLeft() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); using (var script = new ScriptBuilder()) { @@ -218,7 +218,7 @@ public void System_Runtime_GasLeft() public void System_Runtime_GetInvocationCounter() { ContractState contractA, contractB, contractC; - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); // Create dummy contracts diff --git a/tests/Neo.UnitTests/TestBlockchain.cs b/tests/Neo.UnitTests/TestBlockchain.cs index 7bf26edbd7..0596c629d2 100644 --- a/tests/Neo.UnitTests/TestBlockchain.cs +++ b/tests/Neo.UnitTests/TestBlockchain.cs @@ -41,9 +41,11 @@ internal static void ResetStore() TheNeoSystem.Blockchain.Ask(new Blockchain.Initialize()).Wait(); } - internal static DataCache GetTestSnapshot() + internal static SnapshotCache GetTestSnapshotCache(bool reset = true) { - return TheNeoSystem.GetSnapshotCache().CloneCache(); + if (reset) + ResetStore(); + return TheNeoSystem.GetSnapshot(); } } } diff --git a/tests/Neo.UnitTests/UT_DataCache.cs b/tests/Neo.UnitTests/UT_DataCache.cs index 01bd84c949..5271d446ff 100644 --- a/tests/Neo.UnitTests/UT_DataCache.cs +++ b/tests/Neo.UnitTests/UT_DataCache.cs @@ -22,8 +22,8 @@ public class UT_DataCache [TestMethod] public void TestCachedFind_Between() { - var snapshot = TestBlockchain.GetTestSnapshot(); - var storages = snapshot.CloneCache(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); + var storages = snapshot.CreateSnapshot(); var cache = new ClonedCache(storages); storages.Add @@ -61,8 +61,8 @@ public void TestCachedFind_Between() [TestMethod] public void TestCachedFind_Last() { - var snapshot = TestBlockchain.GetTestSnapshot(); - var storages = snapshot.CloneCache(); + var snapshotCache = TestBlockchain.GetTestSnapshotCache(); + var storages = snapshotCache.CreateSnapshot(); var cache = new ClonedCache(storages); storages.Add @@ -93,8 +93,8 @@ public void TestCachedFind_Last() [TestMethod] public void TestCachedFind_Empty() { - var snapshot = TestBlockchain.GetTestSnapshot(); - var storages = snapshot.CloneCache(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); + var storages = snapshot.CreateSnapshot(); var cache = new ClonedCache(storages); cache.Add diff --git a/tests/Neo.UnitTests/Wallets/NEP6/UT_NEP6Wallet.cs b/tests/Neo.UnitTests/Wallets/NEP6/UT_NEP6Wallet.cs index 71914c86f0..a914b235b7 100644 --- a/tests/Neo.UnitTests/Wallets/NEP6/UT_NEP6Wallet.cs +++ b/tests/Neo.UnitTests/Wallets/NEP6/UT_NEP6Wallet.cs @@ -89,10 +89,10 @@ public void TestCreateAccount() Script = new byte[1], Signers = new Signer[] { new Signer() { Account = acc.ScriptHash } }, }; - var ctx = new ContractParametersContext(TestBlockchain.GetTestSnapshot(), tx, TestProtocolSettings.Default.Network); + var ctx = new ContractParametersContext(TestBlockchain.GetTestSnapshotCache(), tx, TestProtocolSettings.Default.Network); Assert.IsTrue(uut.Sign(ctx)); tx.Witnesses = ctx.GetWitnesses(); - Assert.IsTrue(tx.VerifyWitnesses(TestProtocolSettings.Default, TestBlockchain.GetTestSnapshot(), long.MaxValue)); + Assert.IsTrue(tx.VerifyWitnesses(TestProtocolSettings.Default, TestBlockchain.GetTestSnapshotCache(), long.MaxValue)); Assert.ThrowsException(() => uut.CreateAccount((byte[])null)); Assert.ThrowsException(() => uut.CreateAccount("FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551".HexToBytes())); } diff --git a/tests/Neo.UnitTests/Wallets/UT_AssetDescriptor.cs b/tests/Neo.UnitTests/Wallets/UT_AssetDescriptor.cs index 8d938492bc..427af290b1 100644 --- a/tests/Neo.UnitTests/Wallets/UT_AssetDescriptor.cs +++ b/tests/Neo.UnitTests/Wallets/UT_AssetDescriptor.cs @@ -22,7 +22,7 @@ public class UT_AssetDescriptor [TestMethod] public void TestConstructorWithNonexistAssetId() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); Action action = () => { var descriptor = new Neo.Wallets.AssetDescriptor(snapshot, TestProtocolSettings.Default, UInt160.Parse("01ff00ff00ff00ff00ff00ff00ff00ff00ff00a4")); @@ -33,7 +33,7 @@ public void TestConstructorWithNonexistAssetId() [TestMethod] public void Check_GAS() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var descriptor = new Neo.Wallets.AssetDescriptor(snapshot, TestProtocolSettings.Default, NativeContract.GAS.Hash); descriptor.AssetId.Should().Be(NativeContract.GAS.Hash); descriptor.AssetName.Should().Be(nameof(GasToken)); @@ -45,7 +45,7 @@ public void Check_GAS() [TestMethod] public void Check_NEO() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var descriptor = new Neo.Wallets.AssetDescriptor(snapshot, TestProtocolSettings.Default, NativeContract.NEO.Hash); descriptor.AssetId.Should().Be(NativeContract.NEO.Hash); descriptor.AssetName.Should().Be(nameof(NeoToken)); diff --git a/tests/Neo.UnitTests/Wallets/UT_Wallet.cs b/tests/Neo.UnitTests/Wallets/UT_Wallet.cs index d134c6aed3..06619d74f5 100644 --- a/tests/Neo.UnitTests/Wallets/UT_Wallet.cs +++ b/tests/Neo.UnitTests/Wallets/UT_Wallet.cs @@ -217,7 +217,7 @@ public void TestGetAvailable() account.Lock = false; // Fake balance - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var key = NativeContract.GAS.CreateStorageKey(20, account.ScriptHash); var entry = snapshot.GetAndChange(key, () => new StorageItem(new AccountState())); entry.GetInteroperable().Balance = 10000 * NativeContract.GAS.Factor; @@ -237,7 +237,7 @@ public void TestGetBalance() account.Lock = false; // Fake balance - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); var key = NativeContract.GAS.CreateStorageKey(20, account.ScriptHash); var entry = snapshot.GetAndChange(key, () => new StorageItem(new AccountState())); entry.GetInteroperable().Balance = 10000 * NativeContract.GAS.Factor; @@ -290,7 +290,7 @@ public void TestImport2() [TestMethod] public void TestMakeTransaction1() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); MyWallet wallet = new(); Contract contract = Contract.Create(new ContractParameterType[] { ContractParameterType.Boolean }, new byte[] { 1 }); WalletAccount account = wallet.CreateAccount(contract, glkey.PrivateKey); @@ -373,7 +373,7 @@ public void TestMakeTransaction1() [TestMethod] public void TestMakeTransaction2() { - var snapshot = TestBlockchain.GetTestSnapshot(); + var snapshot = TestBlockchain.GetTestSnapshotCache(); MyWallet wallet = new(); Action action = () => wallet.MakeTransaction(snapshot, Array.Empty(), null, null, Array.Empty()); action.Should().Throw();