Skip to content

Commit

Permalink
Remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
doubiliu committed Jan 3, 2020
1 parent cb1bb22 commit 5606dcc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 31 deletions.
2 changes: 2 additions & 0 deletions src/neo/Ledger/ContractState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ContractState>.Clone()
Expand Down
43 changes: 13 additions & 30 deletions src/neo/SmartContract/InteropService.Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/neo/SmartContract/InteropService.Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 5606dcc

Please sign in to comment.