From 70fb5279231790c29642500f9daf149eeb47d28c Mon Sep 17 00:00:00 2001 From: Shargon Date: Tue, 3 Nov 2020 12:00:25 +0100 Subject: [PATCH] Shargon's Feedback --- src/neo/Ledger/ContractState.cs | 15 +++++++++------ src/neo/SmartContract/Native/NativeContract.cs | 8 ++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/neo/Ledger/ContractState.cs b/src/neo/Ledger/ContractState.cs index 20ea75d62f..6e1a962e96 100644 --- a/src/neo/Ledger/ContractState.cs +++ b/src/neo/Ledger/ContractState.cs @@ -75,17 +75,20 @@ public JObject ToJson() { JObject json = new JObject(); json["id"] = Id; + if (Id < 0) { - foreach (var native in NativeContract.Contracts) + var native = NativeContract.GetContract(Id); + if (native != null) json["name"] = native.Name; + } + else + { + if (Manifest.Extra != null && Manifest.Extra["name"] != null) { - if (Id == native.Id) - { - json["name"] = native.Name; - break; - } + json["name"] = Manifest.Extra["name"]; } } + json["hash"] = ScriptHash.ToString(); json["script"] = Convert.ToBase64String(Script); json["manifest"] = Manifest.ToJson(); diff --git a/src/neo/SmartContract/Native/NativeContract.cs b/src/neo/SmartContract/Native/NativeContract.cs index 81f944647c..e24a7a38dc 100644 --- a/src/neo/SmartContract/Native/NativeContract.cs +++ b/src/neo/SmartContract/Native/NativeContract.cs @@ -17,6 +17,7 @@ namespace Neo.SmartContract.Native public abstract class NativeContract { private static readonly List contractsList = new List(); + private static readonly Dictionary contractsIdDictionary = new Dictionary(); private static readonly Dictionary contractsNameDictionary = new Dictionary(); private static readonly Dictionary contractsHashDictionary = new Dictionary(); private readonly Dictionary methods = new Dictionary(); @@ -77,6 +78,7 @@ protected NativeContract() Extra = null }; contractsList.Add(this); + contractsIdDictionary.Add(Id, this); contractsNameDictionary.Add(Name, this); contractsHashDictionary.Add(Hash, this); } @@ -104,6 +106,12 @@ public static NativeContract GetContract(string name) return contract; } + public static NativeContract GetContract(int id) + { + contractsIdDictionary.TryGetValue(id, out var contract); + return contract; + } + internal void Invoke(ApplicationEngine engine) { if (!engine.CurrentScriptHash.Equals(Hash))