Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UpdateCounter issues #2697

Merged
merged 4 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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