Skip to content

Commit

Permalink
update session and iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim8y committed Sep 11, 2024
1 parent 292c3cc commit 9fabbf5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
8 changes: 3 additions & 5 deletions src/Plugins/RpcServer/RpcServer.SmartContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,20 +276,18 @@ protected internal virtual JToken InvokeScript(string scriptBase64, Signer[] sig
/// <param name="count">The number of values returned. It cannot exceed the value of the MaxIteratorResultItems field in config.json of the RpcServer plug-in.</param>
/// <returns>A JToken containing the iterator values.</returns>
[RpcMethodWithParams]
protected internal virtual JToken TraverseIterator(string session, string iteratorId, int count)
protected internal virtual JToken TraverseIterator(Guid session, Guid iteratorId, int count)
{
settings.SessionEnabled.True_Or(RpcError.SessionsDisabled);
Guid sid = Result.Ok_Or(() => Guid.Parse(session), RpcError.InvalidParams.WithData($"Invalid session id"));
Guid iid = Result.Ok_Or(() => Guid.Parse(iteratorId), RpcError.InvalidParams.WithData($"Invalid iterator id"));
Result.True_Or(() => count <= settings.MaxIteratorResultItems, RpcError.InvalidParams.WithData($"Invalid iterator items count: {count}"));

Session currentSession;
lock (sessions)
{
currentSession = Result.Ok_Or(() => sessions[sid], RpcError.UnknownSession);
currentSession = Result.Ok_Or(() => sessions[session], RpcError.UnknownSession);
currentSession.ResetExpiration();
}
IIterator iterator = Result.Ok_Or(() => currentSession.Iterators[iid], RpcError.UnknownIterator);
IIterator iterator = Result.Ok_Or(() => currentSession.Iterators[iteratorId], RpcError.UnknownIterator);
JArray json = new();
while (count-- > 0 && iterator.Next())
json.Add(iterator.Value(null).ToJson());
Expand Down
22 changes: 11 additions & 11 deletions tests/Neo.Plugins.RpcServer.Tests/UT_RpcServer.SmartContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ public void TestTraverseIterator()
{
// GetAllCandidates that should return 0 candidates
JObject resp = (JObject)_rpcServer.InvokeFunction(NeoToken.NEO.Hash.ToString(), "getAllCandidates", [], validatorSigner, true);
string sessionId = resp["session"].AsString();
string iteratorId = resp["stack"][0]["id"].AsString();
Guid sessionId = Guid.Parse(resp["session"].AsString());
Guid iteratorId = Guid.Parse(resp["stack"][0]["id"].AsString());
JArray respArray = (JArray)_rpcServer.TraverseIterator(sessionId, iteratorId, 100);
Assert.AreEqual(respArray.Count, 0);
_rpcServer.TerminateSession(Guid.Parse(sessionId));
_rpcServer.TerminateSession(sessionId);
Assert.ThrowsException<RpcException>(() => (JArray)_rpcServer.TraverseIterator(sessionId, iteratorId, 100), "Unknown session");

// register candidate in snapshot
Expand All @@ -171,8 +171,8 @@ [new ContractParameter

// GetAllCandidates that should return 1 candidate
resp = (JObject)_rpcServer.InvokeFunction(NeoToken.NEO.Hash.ToString(), "getAllCandidates", [], validatorSigner, true);
sessionId = resp["session"].AsString();
iteratorId = resp["stack"][0]["id"].AsString();
sessionId = Guid.Parse(resp["session"].AsString());
iteratorId = Guid.Parse(resp["stack"][0]["id"].AsString());
respArray = (JArray)_rpcServer.TraverseIterator(sessionId, iteratorId, 100);
Assert.AreEqual(respArray.Count, 1);
Assert.AreEqual(respArray[0]["type"], nameof(Neo.VM.Types.Struct));
Expand All @@ -189,8 +189,8 @@ [new ContractParameter

// GetAllCandidates again
resp = (JObject)_rpcServer.InvokeFunction(NeoToken.NEO.Hash.ToString(), "getAllCandidates", [], validatorSigner, true);
sessionId = resp["session"].AsString();
iteratorId = resp["stack"][0]["id"].AsString();
sessionId = Guid.Parse(resp["session"].AsString());
iteratorId = Guid.Parse(resp["stack"][0]["id"].AsString());

// Insufficient result count limit
respArray = (JArray)_rpcServer.TraverseIterator(sessionId, iteratorId, 0);
Expand All @@ -204,8 +204,8 @@ [new ContractParameter
Thread.Sleep((int)_rpcServerSettings.SessionExpirationTime.TotalMilliseconds + 1);
// build another session that did not expire
resp = (JObject)_rpcServer.InvokeFunction(NeoToken.NEO.Hash.ToString(), "getAllCandidates", [], validatorSigner, true);
string notExpiredSessionId = resp["session"].AsString();
string notExpiredIteratorId = resp["stack"][0]["id"].AsString();
Guid notExpiredSessionId = Guid.Parse(resp["session"].AsString());
Guid notExpiredIteratorId = Guid.Parse(resp["stack"][0]["id"].AsString());
_rpcServer.OnTimer(new object());
Assert.ThrowsException<RpcException>(() => (JArray)_rpcServer.TraverseIterator(sessionId, iteratorId, 100), "Unknown session");
// If you want to run the following line without exception,
Expand All @@ -215,8 +215,8 @@ [new ContractParameter

// Mocking disposal
resp = (JObject)_rpcServer.InvokeFunction(NeoToken.NEO.Hash.ToString(), "getAllCandidates", [], validatorSigner, true);
sessionId = resp["session"].AsString();
iteratorId = resp["stack"][0]["id"].AsString();
sessionId = Guid.Parse(resp["session"].AsString());
iteratorId = Guid.Parse(resp["stack"][0]["id"].AsString());
_rpcServer.Dispose_SmartContract();
Assert.ThrowsException<RpcException>(() => (JArray)_rpcServer.TraverseIterator(sessionId, iteratorId, 100), "Unknown session");
}
Expand Down

0 comments on commit 9fabbf5

Please sign in to comment.