Skip to content

Commit

Permalink
Fix UpdateCounter issues (#2697)
Browse files Browse the repository at this point in the history
* Fix

* Fix

* private

* Check policy during deploy

Co-authored-by: Erik Zhang <[email protected]>
  • Loading branch information
shargon and erikzhang authored Apr 25, 2022
1 parent ed68274 commit 6c09dc0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/neo/SmartContract/Native/ContractManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ private async ContractTask<ContractState> Deploy(ApplicationEngine engine, byte[
ContractManifest parsedManifest = ContractManifest.Parse(manifest);
Helper.Check(nef.Script, parsedManifest.Abi);
UInt160 hash = Helper.GetContractHash(tx.Sender, nef.CheckSum, parsedManifest.Name);

if (Policy.IsBlocked(engine.Snapshot, hash))
throw new InvalidOperationException($"The contract {hash} has been blocked.");

StorageKey key = CreateStorageKey(Prefix_Contract).Add(hash);
if (engine.Snapshot.Contains(key))
throw new InvalidOperationException($"Contract Already Exists: {hash}");
Expand Down Expand Up @@ -215,6 +219,7 @@ private ContractTask Update(ApplicationEngine engine, byte[] nefFile, byte[] man

var contract = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_Contract).Add(engine.CallingScriptHash))?.GetInteroperable<ContractState>();
if (contract is null) throw new InvalidOperationException($"Updating Contract Does Not Exist: {engine.CallingScriptHash}");
if (contract.UpdateCounter == ushort.MaxValue) throw new InvalidOperationException($"The contract reached the maximum number of updates.");

if (nefFile != null)
{
Expand Down Expand Up @@ -250,6 +255,9 @@ private void Destroy(ApplicationEngine engine)
engine.Snapshot.Delete(ckey);
foreach (var (key, _) in engine.Snapshot.Find(StorageKey.CreateSearchPrefix(contract.Id, ReadOnlySpan<byte>.Empty)))
engine.Snapshot.Delete(key);
// lock contract
Policy.BlockAccount(engine.Snapshot, hash);
// emit event
engine.SendNotification(Hash, "Destroy", new VM.Types.Array { hash.ToArray() });
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/neo/SmartContract/Native/PolicyContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,17 @@ private void SetStoragePrice(ApplicationEngine engine, uint value)
private bool BlockAccount(ApplicationEngine engine, UInt160 account)
{
if (!CheckCommittee(engine)) throw new InvalidOperationException();
return BlockAccount(engine.Snapshot, account);
}

internal bool BlockAccount(DataCache snapshot, UInt160 account)
{
if (IsNative(account)) throw new InvalidOperationException("It's impossible to block a native contract.");

var key = CreateStorageKey(Prefix_BlockedAccount).Add(account);
if (engine.Snapshot.Contains(key)) return false;
if (snapshot.Contains(key)) return false;

engine.Snapshot.Add(key, new StorageItem(Array.Empty<byte>()));
snapshot.Add(key, new StorageItem(Array.Empty<byte>()));
return true;
}

Expand Down

0 comments on commit 6c09dc0

Please sign in to comment.