diff --git a/src/neo/Ledger/ContractState.cs b/src/neo/Ledger/ContractState.cs index 9827a62b6a..c0cafbc110 100644 --- a/src/neo/Ledger/ContractState.cs +++ b/src/neo/Ledger/ContractState.cs @@ -35,6 +35,8 @@ public UInt160 ScriptHash public UInt160 RedirectionHash = UInt160.Zero; + public bool HasRedirection => !RedirectionHash.Equals(UInt160.Zero); + public bool IsUpdated = false; ContractState ICloneable.Clone() diff --git a/src/neo/SmartContract/InteropService.Contract.cs b/src/neo/SmartContract/InteropService.Contract.cs index 8f145cd767..d7344fb1bd 100644 --- a/src/neo/SmartContract/InteropService.Contract.cs +++ b/src/neo/SmartContract/InteropService.Contract.cs @@ -68,26 +68,19 @@ private static bool Contract_Update(ApplicationEngine engine) Script = script, Manifest = oldcontract.Manifest }; - if (oldcontract.RedirectionHash.Equals(UInt160.Zero)) - { - newcontract.RedirectionHash = oldcontract.ScriptHash; - } - else + if (oldcontract.HasRedirection) { newcontract.RedirectionHash = oldcontract.RedirectionHash; + DeleteContractByHash(engine, oldcontract.ScriptHash, out _); } - newcontract.IsUpdated = false; - newcontract.Manifest.Abi.Hash = hash_new; - engine.Snapshot.Contracts.Add(hash_new, newcontract); - if (oldcontract.RedirectionHash.Equals(UInt160.Zero)) + else { + newcontract.RedirectionHash = oldcontract.ScriptHash; oldcontract = engine.Snapshot.Contracts.GetAndChange(oldcontract.ScriptHash); oldcontract.IsUpdated = true; } - else - { - Contract_UnAppend_Destroy(engine); - } + newcontract.Manifest.Abi.Hash = hash_new; + engine.Snapshot.Contracts.Add(hash_new, newcontract); } if (manifest.Length > 0) { @@ -103,30 +96,20 @@ private static bool Contract_Update(ApplicationEngine engine) private static bool Contract_Destroy(ApplicationEngine engine) { UInt160 hash = engine.CurrentScriptHash; - ContractState contract = engine.Snapshot.Contracts.TryGet(hash); - if (contract == null) return true; - if (!contract.RedirectionHash.Equals(UInt160.Zero)) + if (DeleteContractByHash(engine, hash, out ContractState contract)) { - ContractState initcontract = engine.Snapshot.Contracts.TryGet(contract.RedirectionHash); - if (initcontract != null) + if (contract != null && contract.HasRedirection) { - engine.Snapshot.Contracts.Delete(contract.RedirectionHash); - if (initcontract.HasStorage) - foreach (var (key, _) in engine.Snapshot.Storages.Find(contract.RedirectionHash.ToArray())) - engine.Snapshot.Storages.Delete(key); + DeleteContractByHash(engine, contract.RedirectionHash, out _); } + return true; } - engine.Snapshot.Contracts.Delete(hash); - if (contract.HasStorage) - foreach (var (key, _) in engine.Snapshot.Storages.Find(hash.ToArray())) - engine.Snapshot.Storages.Delete(key); - return true; + return false; } - private static bool Contract_UnAppend_Destroy(ApplicationEngine engine) + private static bool DeleteContractByHash(ApplicationEngine engine, UInt160 hash, out ContractState contract) { - UInt160 hash = engine.CurrentScriptHash; - ContractState contract = engine.Snapshot.Contracts.TryGet(hash); + contract = engine.Snapshot.Contracts.TryGet(hash); if (contract == null) return true; engine.Snapshot.Contracts.Delete(hash); if (contract.HasStorage) diff --git a/src/neo/SmartContract/InteropService.Storage.cs b/src/neo/SmartContract/InteropService.Storage.cs index ca157027ca..f7e967071b 100644 --- a/src/neo/SmartContract/InteropService.Storage.cs +++ b/src/neo/SmartContract/InteropService.Storage.cs @@ -34,7 +34,7 @@ private static bool CheckStorageContext(ApplicationEngine engine, StorageContext private static void RedirectionStorageContext(StorageContext context, ContractState contract) { - if (!contract.RedirectionHash.Equals(UInt160.Zero)) + if (contract.HasRedirection) { context.ScriptHash = contract.RedirectionHash; }