From b57d5cc0e2a8e6f84604c3a35454af727107c9c6 Mon Sep 17 00:00:00 2001 From: Shargon Date: Wed, 11 Nov 2020 16:07:51 +0100 Subject: [PATCH 1/9] Change native deploy --- src/neo/Ledger/Blockchain.cs | 7 +++++- .../SmartContract/ApplicationEngine.Native.cs | 25 +++++++++---------- src/neo/SmartContract/ApplicationEngine.cs | 3 +++ tests/neo.UnitTests/Ledger/UT_Blockchain.cs | 6 ++--- .../Ledger/UT_TransactionState.cs | 2 +- 5 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/neo/Ledger/Blockchain.cs b/src/neo/Ledger/Blockchain.cs index 4418e461d8..0d05c9da46 100644 --- a/src/neo/Ledger/Blockchain.cs +++ b/src/neo/Ledger/Blockchain.cs @@ -169,7 +169,12 @@ private static Transaction DeployNativeContracts() byte[] script; using (ScriptBuilder sb = new ScriptBuilder()) { - sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy); + sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.Designate.Hash.ToArray()); + sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.Policy.Hash.ToArray()); + sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.NEO.Hash.ToArray()); + sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.GAS.Hash.ToArray()); + sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.Oracle.Hash.ToArray()); + script = sb.ToArray(); } return new Transaction diff --git a/src/neo/SmartContract/ApplicationEngine.Native.cs b/src/neo/SmartContract/ApplicationEngine.Native.cs index 90a8168942..31916b93f9 100644 --- a/src/neo/SmartContract/ApplicationEngine.Native.cs +++ b/src/neo/SmartContract/ApplicationEngine.Native.cs @@ -6,23 +6,22 @@ namespace Neo.SmartContract { partial class ApplicationEngine { - public static readonly InteropDescriptor Neo_Native_Deploy = Register("Neo.Native.Deploy", nameof(DeployNativeContracts), 0, CallFlags.AllowModifyStates, false); + public static readonly InteropDescriptor Neo_Native_Deploy = Register("Neo.Native.Deploy", nameof(DeployNativeContract), 0, CallFlags.AllowModifyStates, false); public static readonly InteropDescriptor Neo_Native_Call = Register("Neo.Native.Call", nameof(CallNativeContract), 0, CallFlags.None, false); - protected internal void DeployNativeContracts() + protected internal void DeployNativeContract(UInt160 hash) { - if (Snapshot.PersistingBlock.Index != 0) - throw new InvalidOperationException(); - foreach (NativeContract contract in NativeContract.Contracts) + NativeContract contract = NativeContract.GetContract(hash); + if (contract == null) throw new Exception($"Can't find a native contract with the hash: {hash}"); + if (Snapshot.Contracts.Contains(hash)) throw new Exception($"{hash} already deployed"); + + Snapshot.Contracts.Add(contract.Hash, new ContractState { - Snapshot.Contracts.Add(contract.Hash, new ContractState - { - Id = contract.Id, - Script = contract.Script, - Manifest = contract.Manifest - }); - contract.Initialize(this); - } + Id = contract.Id, + Script = contract.Script, + Manifest = contract.Manifest + }); + contract.Initialize(this); } protected internal void CallNativeContract(string name) diff --git a/src/neo/SmartContract/ApplicationEngine.cs b/src/neo/SmartContract/ApplicationEngine.cs index a527344570..7c7b01d99c 100644 --- a/src/neo/SmartContract/ApplicationEngine.cs +++ b/src/neo/SmartContract/ApplicationEngine.cs @@ -73,7 +73,10 @@ protected internal void AddGas(long gas) { GasConsumed = checked(GasConsumed + gas); if (GasConsumed > gas_amount) + { + if (Snapshot?.PersistingBlock?.Hash == Blockchain.GenesisBlock.Hash) return; throw new InvalidOperationException("Insufficient GAS."); + } } protected override void OnFault(Exception e) diff --git a/tests/neo.UnitTests/Ledger/UT_Blockchain.cs b/tests/neo.UnitTests/Ledger/UT_Blockchain.cs index e4bf204d47..1220e66e2b 100644 --- a/tests/neo.UnitTests/Ledger/UT_Blockchain.cs +++ b/tests/neo.UnitTests/Ledger/UT_Blockchain.cs @@ -73,13 +73,13 @@ public void TestContainsTransaction() [TestMethod] public void TestGetCurrentBlockHash() { - Blockchain.Singleton.CurrentBlockHash.Should().Be(UInt256.Parse("0xecaee33262f1bc7c7c28f2b25b54a5d61d50670871f45c0c6fe755a40cbde4a8")); + Blockchain.Singleton.CurrentBlockHash.Should().Be(UInt256.Parse("0xc1c0f6abacf1ba03d2e22c67a974a02ccc7a6d7916c169e38e64a9e3554775b3")); } [TestMethod] public void TestGetCurrentHeaderHash() { - Blockchain.Singleton.CurrentHeaderHash.Should().Be(UInt256.Parse("0xecaee33262f1bc7c7c28f2b25b54a5d61d50670871f45c0c6fe755a40cbde4a8")); + Blockchain.Singleton.CurrentHeaderHash.Should().Be(UInt256.Parse("0xc1c0f6abacf1ba03d2e22c67a974a02ccc7a6d7916c169e38e64a9e3554775b3")); } [TestMethod] @@ -91,7 +91,7 @@ public void TestGetBlock() [TestMethod] public void TestGetBlockHash() { - Blockchain.Singleton.GetBlockHash(0).Should().Be(UInt256.Parse("0xecaee33262f1bc7c7c28f2b25b54a5d61d50670871f45c0c6fe755a40cbde4a8")); + Blockchain.Singleton.GetBlockHash(0).Should().Be(UInt256.Parse("0xc1c0f6abacf1ba03d2e22c67a974a02ccc7a6d7916c169e38e64a9e3554775b3")); Blockchain.Singleton.GetBlockHash(10).Should().BeNull(); } diff --git a/tests/neo.UnitTests/Ledger/UT_TransactionState.cs b/tests/neo.UnitTests/Ledger/UT_TransactionState.cs index e30416a136..c08433de89 100644 --- a/tests/neo.UnitTests/Ledger/UT_TransactionState.cs +++ b/tests/neo.UnitTests/Ledger/UT_TransactionState.cs @@ -61,7 +61,7 @@ public void TestDeserialize() [TestMethod] public void TestGetSize() { - ((ISerializable)origin).Size.Should().Be(63); + ((ISerializable)origin).Size.Should().Be(193); } } } From 5abe0e14368a1cdee1617b97134f6f173fe9e32a Mon Sep 17 00:00:00 2001 From: Shargon Date: Wed, 11 Nov 2020 16:13:17 +0100 Subject: [PATCH 2/9] Speed up genesis check --- src/neo/SmartContract/ApplicationEngine.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/neo/SmartContract/ApplicationEngine.cs b/src/neo/SmartContract/ApplicationEngine.cs index 7c7b01d99c..313232ea96 100644 --- a/src/neo/SmartContract/ApplicationEngine.cs +++ b/src/neo/SmartContract/ApplicationEngine.cs @@ -74,7 +74,7 @@ protected internal void AddGas(long gas) GasConsumed = checked(GasConsumed + gas); if (GasConsumed > gas_amount) { - if (Snapshot?.PersistingBlock?.Hash == Blockchain.GenesisBlock.Hash) return; + if (Snapshot?.PersistingBlock?.Index == 0) return; throw new InvalidOperationException("Insufficient GAS."); } } From 0c7b3bc62ba9ba6c2fff4b1a6512a9e76b161c45 Mon Sep 17 00:00:00 2001 From: Shargon Date: Wed, 11 Nov 2020 16:22:17 +0100 Subject: [PATCH 3/9] Remove AddGas condition --- src/neo/Ledger/Blockchain.cs | 7 ++++++- src/neo/SmartContract/ApplicationEngine.cs | 3 --- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/neo/Ledger/Blockchain.cs b/src/neo/Ledger/Blockchain.cs index 0d05c9da46..bdd0b87148 100644 --- a/src/neo/Ledger/Blockchain.cs +++ b/src/neo/Ledger/Blockchain.cs @@ -439,6 +439,7 @@ private void Persist(Block block) header_index.Add(block.Hash); snapshot.HeaderHashIndex.GetAndChange().Set(block); } + long genesis_fee = 0; List all_application_executed = new List(); snapshot.PersistingBlock = block; if (block.Index > 0) @@ -450,6 +451,10 @@ private void Persist(Block block) Context.System.EventStream.Publish(application_executed); all_application_executed.Add(application_executed); } + else + { + genesis_fee = 5 * ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1]; + } snapshot.Blocks.Add(block.Hash, block.Trim()); StoreView clonedSnapshot = snapshot.Clone(); // Warning: Do not write into variable snapshot directly. Write into variable clonedSnapshot and commit instead. @@ -464,7 +469,7 @@ private void Persist(Block block) clonedSnapshot.Transactions.Add(tx.Hash, state); clonedSnapshot.Transactions.Commit(); - using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Application, tx, clonedSnapshot, tx.SystemFee)) + using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Application, tx, clonedSnapshot, tx.SystemFee + genesis_fee)) { engine.LoadScript(tx.Script); state.VMState = engine.Execute(); diff --git a/src/neo/SmartContract/ApplicationEngine.cs b/src/neo/SmartContract/ApplicationEngine.cs index 313232ea96..a527344570 100644 --- a/src/neo/SmartContract/ApplicationEngine.cs +++ b/src/neo/SmartContract/ApplicationEngine.cs @@ -73,10 +73,7 @@ protected internal void AddGas(long gas) { GasConsumed = checked(GasConsumed + gas); if (GasConsumed > gas_amount) - { - if (Snapshot?.PersistingBlock?.Index == 0) return; throw new InvalidOperationException("Insufficient GAS."); - } } protected override void OnFault(Exception e) From 8ee10c8ec04db3ab6cec96dd01eb2bc2d8264ac7 Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 12 Nov 2020 11:50:34 +0100 Subject: [PATCH 4/9] Add Name in Extra --- src/neo/SmartContract/Native/Tokens/GasToken.cs | 2 +- src/neo/SmartContract/Native/Tokens/NeoToken.cs | 2 +- .../Native/Tokens/{Nep5Token.cs => Nep17Token.cs} | 8 +++++--- .../Tokens/{UT_Nep5Token.cs => UT_Nep17Token.cs} | 14 ++++++++++---- 4 files changed, 17 insertions(+), 9 deletions(-) rename src/neo/SmartContract/Native/Tokens/{Nep5Token.cs => Nep17Token.cs} (96%) rename tests/neo.UnitTests/SmartContract/Native/Tokens/{UT_Nep5Token.cs => UT_Nep17Token.cs} (83%) diff --git a/src/neo/SmartContract/Native/Tokens/GasToken.cs b/src/neo/SmartContract/Native/Tokens/GasToken.cs index c3a6ed08f4..5bec05f471 100644 --- a/src/neo/SmartContract/Native/Tokens/GasToken.cs +++ b/src/neo/SmartContract/Native/Tokens/GasToken.cs @@ -4,7 +4,7 @@ namespace Neo.SmartContract.Native.Tokens { - public sealed class GasToken : Nep5Token + public sealed class GasToken : Nep17Token { public override int Id => -2; public override string Name => "GAS"; diff --git a/src/neo/SmartContract/Native/Tokens/NeoToken.cs b/src/neo/SmartContract/Native/Tokens/NeoToken.cs index 956f78d481..1204d39be6 100644 --- a/src/neo/SmartContract/Native/Tokens/NeoToken.cs +++ b/src/neo/SmartContract/Native/Tokens/NeoToken.cs @@ -15,7 +15,7 @@ namespace Neo.SmartContract.Native.Tokens { - public sealed class NeoToken : Nep5Token + public sealed class NeoToken : Nep17Token { public override int Id => -1; public override string Name => "NEO"; diff --git a/src/neo/SmartContract/Native/Tokens/Nep5Token.cs b/src/neo/SmartContract/Native/Tokens/Nep17Token.cs similarity index 96% rename from src/neo/SmartContract/Native/Tokens/Nep5Token.cs rename to src/neo/SmartContract/Native/Tokens/Nep17Token.cs index ed4308b73c..9a5057d491 100644 --- a/src/neo/SmartContract/Native/Tokens/Nep5Token.cs +++ b/src/neo/SmartContract/Native/Tokens/Nep17Token.cs @@ -10,7 +10,7 @@ namespace Neo.SmartContract.Native.Tokens { - public abstract class Nep5Token : NativeContract + public abstract class Nep17Token : NativeContract where TState : AccountState, new() { [ContractMethod(0, CallFlags.None)] @@ -22,12 +22,14 @@ public abstract class Nep5Token : NativeContract protected const byte Prefix_TotalSupply = 11; protected const byte Prefix_Account = 20; - protected Nep5Token() + protected Nep17Token() { this.Factor = BigInteger.Pow(10, Decimals); Manifest.Features = ContractFeatures.HasStorage; - Manifest.SupportedStandards = new[] { "NEP-5" }; + Manifest.SupportedStandards = new[] { "NEP-17" }; + Manifest.Extra = new IO.Json.JObject(); + Manifest.Extra["name"] = Name; var events = new List(Manifest.Abi.Events) { diff --git a/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_Nep5Token.cs b/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_Nep17Token.cs similarity index 83% rename from tests/neo.UnitTests/SmartContract/Native/Tokens/UT_Nep5Token.cs rename to tests/neo.UnitTests/SmartContract/Native/Tokens/UT_Nep17Token.cs index 57b2a25491..b18ef48621 100644 --- a/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_Nep5Token.cs +++ b/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_Nep17Token.cs @@ -9,7 +9,7 @@ namespace Neo.UnitTests.SmartContract.Native.Tokens { [TestClass] - public class UT_Nep5Token : TestKit + public class UT_Nep17Token : TestKit { [TestInitialize] public void TestSetup() @@ -18,7 +18,13 @@ public void TestSetup() } protected const byte Prefix_TotalSupply = 11; - private static readonly TestNep5Token test = new TestNep5Token(); + private static readonly TestNep17Token test = new TestNep17Token(); + + [TestMethod] + public void TestName() + { + Assert.AreEqual(test.Name, test.Manifest.Extra["name"].AsString()); + } [TestMethod] public void TestTotalSupply() @@ -71,11 +77,11 @@ public StorageKey CreateStorageKey(byte prefix, byte[] key = null) } } - public class TestNep5Token : Nep5Token + public class TestNep17Token : Nep17Token { public override int Id => 0x10000005; - public override string Name => "testNep5Token"; + public override string Name => "testNep17Token"; public override string Symbol => throw new NotImplementedException(); From 368e9f7d8a16ae4d4db5a766e54f105635994cae Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 12 Nov 2020 11:50:34 +0100 Subject: [PATCH 5/9] Revert "Add Name in Extra" This reverts commit 8ee10c8ec04db3ab6cec96dd01eb2bc2d8264ac7. --- src/neo/SmartContract/Native/Tokens/GasToken.cs | 2 +- src/neo/SmartContract/Native/Tokens/NeoToken.cs | 2 +- .../Native/Tokens/{Nep17Token.cs => Nep5Token.cs} | 8 +++----- .../Tokens/{UT_Nep17Token.cs => UT_Nep5Token.cs} | 14 ++++---------- 4 files changed, 9 insertions(+), 17 deletions(-) rename src/neo/SmartContract/Native/Tokens/{Nep17Token.cs => Nep5Token.cs} (96%) rename tests/neo.UnitTests/SmartContract/Native/Tokens/{UT_Nep17Token.cs => UT_Nep5Token.cs} (83%) diff --git a/src/neo/SmartContract/Native/Tokens/GasToken.cs b/src/neo/SmartContract/Native/Tokens/GasToken.cs index 5bec05f471..c3a6ed08f4 100644 --- a/src/neo/SmartContract/Native/Tokens/GasToken.cs +++ b/src/neo/SmartContract/Native/Tokens/GasToken.cs @@ -4,7 +4,7 @@ namespace Neo.SmartContract.Native.Tokens { - public sealed class GasToken : Nep17Token + public sealed class GasToken : Nep5Token { public override int Id => -2; public override string Name => "GAS"; diff --git a/src/neo/SmartContract/Native/Tokens/NeoToken.cs b/src/neo/SmartContract/Native/Tokens/NeoToken.cs index 1204d39be6..956f78d481 100644 --- a/src/neo/SmartContract/Native/Tokens/NeoToken.cs +++ b/src/neo/SmartContract/Native/Tokens/NeoToken.cs @@ -15,7 +15,7 @@ namespace Neo.SmartContract.Native.Tokens { - public sealed class NeoToken : Nep17Token + public sealed class NeoToken : Nep5Token { public override int Id => -1; public override string Name => "NEO"; diff --git a/src/neo/SmartContract/Native/Tokens/Nep17Token.cs b/src/neo/SmartContract/Native/Tokens/Nep5Token.cs similarity index 96% rename from src/neo/SmartContract/Native/Tokens/Nep17Token.cs rename to src/neo/SmartContract/Native/Tokens/Nep5Token.cs index 9a5057d491..ed4308b73c 100644 --- a/src/neo/SmartContract/Native/Tokens/Nep17Token.cs +++ b/src/neo/SmartContract/Native/Tokens/Nep5Token.cs @@ -10,7 +10,7 @@ namespace Neo.SmartContract.Native.Tokens { - public abstract class Nep17Token : NativeContract + public abstract class Nep5Token : NativeContract where TState : AccountState, new() { [ContractMethod(0, CallFlags.None)] @@ -22,14 +22,12 @@ public abstract class Nep17Token : NativeContract protected const byte Prefix_TotalSupply = 11; protected const byte Prefix_Account = 20; - protected Nep17Token() + protected Nep5Token() { this.Factor = BigInteger.Pow(10, Decimals); Manifest.Features = ContractFeatures.HasStorage; - Manifest.SupportedStandards = new[] { "NEP-17" }; - Manifest.Extra = new IO.Json.JObject(); - Manifest.Extra["name"] = Name; + Manifest.SupportedStandards = new[] { "NEP-5" }; var events = new List(Manifest.Abi.Events) { diff --git a/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_Nep17Token.cs b/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_Nep5Token.cs similarity index 83% rename from tests/neo.UnitTests/SmartContract/Native/Tokens/UT_Nep17Token.cs rename to tests/neo.UnitTests/SmartContract/Native/Tokens/UT_Nep5Token.cs index b18ef48621..57b2a25491 100644 --- a/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_Nep17Token.cs +++ b/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_Nep5Token.cs @@ -9,7 +9,7 @@ namespace Neo.UnitTests.SmartContract.Native.Tokens { [TestClass] - public class UT_Nep17Token : TestKit + public class UT_Nep5Token : TestKit { [TestInitialize] public void TestSetup() @@ -18,13 +18,7 @@ public void TestSetup() } protected const byte Prefix_TotalSupply = 11; - private static readonly TestNep17Token test = new TestNep17Token(); - - [TestMethod] - public void TestName() - { - Assert.AreEqual(test.Name, test.Manifest.Extra["name"].AsString()); - } + private static readonly TestNep5Token test = new TestNep5Token(); [TestMethod] public void TestTotalSupply() @@ -77,11 +71,11 @@ public StorageKey CreateStorageKey(byte prefix, byte[] key = null) } } - public class TestNep17Token : Nep17Token + public class TestNep5Token : Nep5Token { public override int Id => 0x10000005; - public override string Name => "testNep17Token"; + public override string Name => "testNep5Token"; public override string Symbol => throw new NotImplementedException(); From bd040d7cac4ee6d15cd58a1584761af33a9949d2 Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 26 Nov 2020 19:15:09 +0100 Subject: [PATCH 6/9] Change to onPersist --- src/neo/Ledger/Blockchain.cs | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/neo/Ledger/Blockchain.cs b/src/neo/Ledger/Blockchain.cs index bdd0b87148..55a6b94199 100644 --- a/src/neo/Ledger/Blockchain.cs +++ b/src/neo/Ledger/Blockchain.cs @@ -166,21 +166,10 @@ public bool ContainsTransaction(UInt256 hash) private static Transaction DeployNativeContracts() { - byte[] script; - using (ScriptBuilder sb = new ScriptBuilder()) - { - sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.Designate.Hash.ToArray()); - sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.Policy.Hash.ToArray()); - sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.NEO.Hash.ToArray()); - sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.GAS.Hash.ToArray()); - sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.Oracle.Hash.ToArray()); - - script = sb.ToArray(); - } return new Transaction { Version = 0, - Script = script, + Script = Array.Empty(), SystemFee = 0, Signers = new[] { @@ -439,7 +428,6 @@ private void Persist(Block block) header_index.Add(block.Hash); snapshot.HeaderHashIndex.GetAndChange().Set(block); } - long genesis_fee = 0; List all_application_executed = new List(); snapshot.PersistingBlock = block; if (block.Index > 0) @@ -453,7 +441,25 @@ private void Persist(Block block) } else { - genesis_fee = 5 * ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1]; + // Deploy genesis native contracts + + byte[] genesisOnPersistScript; + using (ScriptBuilder sb = new ScriptBuilder()) + { + sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.Designate.Hash.ToArray()); + sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.Policy.Hash.ToArray()); + sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.NEO.Hash.ToArray()); + sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.GAS.Hash.ToArray()); + sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.Oracle.Hash.ToArray()); + genesisOnPersistScript = sb.ToArray(); + } + + using ApplicationEngine engine = ApplicationEngine.Create(TriggerType.OnPersist, null, snapshot); + engine.LoadScript(genesisOnPersistScript); + if (engine.Execute() != VMState.HALT) throw new InvalidOperationException(); + ApplicationExecuted application_executed = new ApplicationExecuted(engine); + Context.System.EventStream.Publish(application_executed); + all_application_executed.Add(application_executed); } snapshot.Blocks.Add(block.Hash, block.Trim()); StoreView clonedSnapshot = snapshot.Clone(); @@ -469,7 +475,7 @@ private void Persist(Block block) clonedSnapshot.Transactions.Add(tx.Hash, state); clonedSnapshot.Transactions.Commit(); - using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Application, tx, clonedSnapshot, tx.SystemFee + genesis_fee)) + using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Application, tx, clonedSnapshot, tx.SystemFee)) { engine.LoadScript(tx.Script); state.VMState = engine.Execute(); From 3194c249f1ebbfcadc9749f109e3708180da4cd5 Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 26 Nov 2020 19:24:37 +0100 Subject: [PATCH 7/9] Fix UT --- src/neo/Ledger/Blockchain.cs | 2 +- src/neo/SmartContract/ApplicationEngine.Native.cs | 1 + tests/neo.UnitTests/Ledger/UT_Blockchain.cs | 6 +++--- tests/neo.UnitTests/Ledger/UT_TransactionState.cs | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/neo/Ledger/Blockchain.cs b/src/neo/Ledger/Blockchain.cs index 55a6b94199..85d7fee1f4 100644 --- a/src/neo/Ledger/Blockchain.cs +++ b/src/neo/Ledger/Blockchain.cs @@ -169,7 +169,7 @@ private static Transaction DeployNativeContracts() return new Transaction { Version = 0, - Script = Array.Empty(), + Script = new byte[] { (byte)OpCode.RET }, SystemFee = 0, Signers = new[] { diff --git a/src/neo/SmartContract/ApplicationEngine.Native.cs b/src/neo/SmartContract/ApplicationEngine.Native.cs index 31916b93f9..674a25486f 100644 --- a/src/neo/SmartContract/ApplicationEngine.Native.cs +++ b/src/neo/SmartContract/ApplicationEngine.Native.cs @@ -11,6 +11,7 @@ partial class ApplicationEngine protected internal void DeployNativeContract(UInt160 hash) { + if (Trigger != TriggerType.OnPersist) throw new InvalidOperationException("Invalid trigger"); NativeContract contract = NativeContract.GetContract(hash); if (contract == null) throw new Exception($"Can't find a native contract with the hash: {hash}"); if (Snapshot.Contracts.Contains(hash)) throw new Exception($"{hash} already deployed"); diff --git a/tests/neo.UnitTests/Ledger/UT_Blockchain.cs b/tests/neo.UnitTests/Ledger/UT_Blockchain.cs index 1220e66e2b..2070e4d4a2 100644 --- a/tests/neo.UnitTests/Ledger/UT_Blockchain.cs +++ b/tests/neo.UnitTests/Ledger/UT_Blockchain.cs @@ -73,13 +73,13 @@ public void TestContainsTransaction() [TestMethod] public void TestGetCurrentBlockHash() { - Blockchain.Singleton.CurrentBlockHash.Should().Be(UInt256.Parse("0xc1c0f6abacf1ba03d2e22c67a974a02ccc7a6d7916c169e38e64a9e3554775b3")); + Blockchain.Singleton.CurrentBlockHash.Should().Be(UInt256.Parse("0xb71ffaa6a2f4dd0190739e500b4e943ab889175858cc7f4936e9868f3407d70d")); } [TestMethod] public void TestGetCurrentHeaderHash() { - Blockchain.Singleton.CurrentHeaderHash.Should().Be(UInt256.Parse("0xc1c0f6abacf1ba03d2e22c67a974a02ccc7a6d7916c169e38e64a9e3554775b3")); + Blockchain.Singleton.CurrentHeaderHash.Should().Be(UInt256.Parse("0xb71ffaa6a2f4dd0190739e500b4e943ab889175858cc7f4936e9868f3407d70d")); } [TestMethod] @@ -91,7 +91,7 @@ public void TestGetBlock() [TestMethod] public void TestGetBlockHash() { - Blockchain.Singleton.GetBlockHash(0).Should().Be(UInt256.Parse("0xc1c0f6abacf1ba03d2e22c67a974a02ccc7a6d7916c169e38e64a9e3554775b3")); + Blockchain.Singleton.GetBlockHash(0).Should().Be(UInt256.Parse("0xb71ffaa6a2f4dd0190739e500b4e943ab889175858cc7f4936e9868f3407d70d")); Blockchain.Singleton.GetBlockHash(10).Should().BeNull(); } diff --git a/tests/neo.UnitTests/Ledger/UT_TransactionState.cs b/tests/neo.UnitTests/Ledger/UT_TransactionState.cs index c08433de89..5febbc0ab8 100644 --- a/tests/neo.UnitTests/Ledger/UT_TransactionState.cs +++ b/tests/neo.UnitTests/Ledger/UT_TransactionState.cs @@ -61,7 +61,7 @@ public void TestDeserialize() [TestMethod] public void TestGetSize() { - ((ISerializable)origin).Size.Should().Be(193); + ((ISerializable)origin).Size.Should().Be(58); } } } From a7e9cd7663004c19becc9a13303ad8522598eaf6 Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 26 Nov 2020 19:31:54 +0100 Subject: [PATCH 8/9] Fix UT --- tests/neo.UnitTests/Ledger/UT_Blockchain.cs | 6 +++--- tests/neo.UnitTests/Ledger/UT_TransactionState.cs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/neo.UnitTests/Ledger/UT_Blockchain.cs b/tests/neo.UnitTests/Ledger/UT_Blockchain.cs index 2070e4d4a2..ab7d2b9c77 100644 --- a/tests/neo.UnitTests/Ledger/UT_Blockchain.cs +++ b/tests/neo.UnitTests/Ledger/UT_Blockchain.cs @@ -73,13 +73,13 @@ public void TestContainsTransaction() [TestMethod] public void TestGetCurrentBlockHash() { - Blockchain.Singleton.CurrentBlockHash.Should().Be(UInt256.Parse("0xb71ffaa6a2f4dd0190739e500b4e943ab889175858cc7f4936e9868f3407d70d")); + Blockchain.Singleton.CurrentBlockHash.Should().Be(UInt256.Parse("0x905e6bca0b4c298436539fcaad820614fa58c8e7561721b47b5c11cccfee70a3")); } [TestMethod] public void TestGetCurrentHeaderHash() { - Blockchain.Singleton.CurrentHeaderHash.Should().Be(UInt256.Parse("0xb71ffaa6a2f4dd0190739e500b4e943ab889175858cc7f4936e9868f3407d70d")); + Blockchain.Singleton.CurrentHeaderHash.Should().Be(UInt256.Parse("0x905e6bca0b4c298436539fcaad820614fa58c8e7561721b47b5c11cccfee70a3")); } [TestMethod] @@ -91,7 +91,7 @@ public void TestGetBlock() [TestMethod] public void TestGetBlockHash() { - Blockchain.Singleton.GetBlockHash(0).Should().Be(UInt256.Parse("0xb71ffaa6a2f4dd0190739e500b4e943ab889175858cc7f4936e9868f3407d70d")); + Blockchain.Singleton.GetBlockHash(0).Should().Be(UInt256.Parse("0x905e6bca0b4c298436539fcaad820614fa58c8e7561721b47b5c11cccfee70a3")); Blockchain.Singleton.GetBlockHash(10).Should().BeNull(); } diff --git a/tests/neo.UnitTests/Ledger/UT_TransactionState.cs b/tests/neo.UnitTests/Ledger/UT_TransactionState.cs index 5febbc0ab8..fb503b35f4 100644 --- a/tests/neo.UnitTests/Ledger/UT_TransactionState.cs +++ b/tests/neo.UnitTests/Ledger/UT_TransactionState.cs @@ -61,7 +61,7 @@ public void TestDeserialize() [TestMethod] public void TestGetSize() { - ((ISerializable)origin).Size.Should().Be(58); + ((ISerializable)origin).Size.Should().Be(59); } } } From fb1808635393097c675abc907613d2ee0df934c7 Mon Sep 17 00:00:00 2001 From: Shargon Date: Fri, 27 Nov 2020 13:33:52 +0100 Subject: [PATCH 9/9] Optimize --- src/neo/Ledger/Blockchain.cs | 42 +++++++++++++++++------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/neo/Ledger/Blockchain.cs b/src/neo/Ledger/Blockchain.cs index 85d7fee1f4..3d0ac3feb1 100644 --- a/src/neo/Ledger/Blockchain.cs +++ b/src/neo/Ledger/Blockchain.cs @@ -430,37 +430,35 @@ private void Persist(Block block) } List all_application_executed = new List(); snapshot.PersistingBlock = block; - if (block.Index > 0) - { - using ApplicationEngine engine = ApplicationEngine.Create(TriggerType.OnPersist, null, snapshot); - engine.LoadScript(onPersistScript); - if (engine.Execute() != VMState.HALT) throw new InvalidOperationException(); - ApplicationExecuted application_executed = new ApplicationExecuted(engine); - Context.System.EventStream.Publish(application_executed); - all_application_executed.Add(application_executed); - } - else - { - // Deploy genesis native contracts - byte[] genesisOnPersistScript; - using (ScriptBuilder sb = new ScriptBuilder()) + using (ApplicationEngine engine = ApplicationEngine.Create(TriggerType.OnPersist, null, snapshot)) + { + if (block.Index > 0) { - sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.Designate.Hash.ToArray()); - sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.Policy.Hash.ToArray()); - sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.NEO.Hash.ToArray()); - sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.GAS.Hash.ToArray()); - sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.Oracle.Hash.ToArray()); - genesisOnPersistScript = sb.ToArray(); + engine.LoadScript(onPersistScript); } + else + { + // Deploy genesis native contracts + byte[] genesisOnPersistScript; + using (ScriptBuilder sb = new ScriptBuilder()) + { + sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.Designate.Hash.ToArray()); + sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.Policy.Hash.ToArray()); + sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.NEO.Hash.ToArray()); + sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.GAS.Hash.ToArray()); + sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy, NativeContract.Oracle.Hash.ToArray()); + genesisOnPersistScript = sb.ToArray(); + } - using ApplicationEngine engine = ApplicationEngine.Create(TriggerType.OnPersist, null, snapshot); - engine.LoadScript(genesisOnPersistScript); + engine.LoadScript(genesisOnPersistScript); + } if (engine.Execute() != VMState.HALT) throw new InvalidOperationException(); ApplicationExecuted application_executed = new ApplicationExecuted(engine); Context.System.EventStream.Publish(application_executed); all_application_executed.Add(application_executed); } + snapshot.Blocks.Add(block.Hash, block.Trim()); StoreView clonedSnapshot = snapshot.Clone(); // Warning: Do not write into variable snapshot directly. Write into variable clonedSnapshot and commit instead.