Skip to content

Commit

Permalink
Remove exception from GetInvocationCounter (#1948)
Browse files Browse the repository at this point in the history
* Remove exception from GetInvocationCounter

* Ensure invocationCounter

* Optimize

Co-authored-by: erikzhang <[email protected]>
  • Loading branch information
shargon and erikzhang authored Sep 23, 2020
1 parent a7ea1fa commit 7d77cab
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/neo/SmartContract/ApplicationEngine.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ protected internal bool CheckWitnessInternal(UInt160 hash)
protected internal int GetInvocationCounter()
{
if (!invocationCounter.TryGetValue(CurrentScriptHash, out var counter))
throw new InvalidOperationException();
{
invocationCounter[CurrentScriptHash] = counter = 1;
}
return counter;
}

Expand Down
5 changes: 4 additions & 1 deletion src/neo/SmartContract/ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ protected override void LoadContext(ExecutionContext context)
{
// Set default execution context state

context.GetState<ExecutionContextState>().ScriptHash ??= ((byte[])context.Script).ToScriptHash();
var state = context.GetState<ExecutionContextState>();
state.ScriptHash ??= ((byte[])context.Script).ToScriptHash();
invocationCounter.TryAdd(state.ScriptHash, 1);

base.LoadContext(context);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/neo.UnitTests/SmartContract/UT_InteropService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public void TestRuntime_Deserialize()
public void TestRuntime_GetInvocationCounter()
{
var engine = GetEngine();
Assert.ThrowsException<InvalidOperationException>(() => engine.GetInvocationCounter());
Assert.AreEqual(1, engine.GetInvocationCounter());
}

[TestMethod]
Expand Down

0 comments on commit 7d77cab

Please sign in to comment.