Skip to content
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
72 changes: 13 additions & 59 deletions samples/Sample.PocketIC/PocketIc.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ public class PocketIcServerFixture : IDisposable
{
public PocketIcServer Server { get; private set; }


public PocketIcServerFixture()
{
// Start the server for all tests
this.Server = PocketIcServer.StartAsync(runtimeLogLevel: LogLevel.Debug, showErrorLogs: true).GetAwaiter().GetResult();

}

public void Dispose()
Expand Down Expand Up @@ -110,7 +112,7 @@ public async Task HttpGateway_CounterWasm__Basic__Valid()

await pocketIc.SetTimeAsync(ICTimestamp.Now());
// Let time progress so that update calls get processed
await using (await pocketIc.AutoProgressTimeAsync())
await using (await pocketIc.AutoProgressAsync())
{
await using (HttpGateway httpGateway = await pocketIc.RunHttpGatewayAsync())
{
Expand All @@ -121,72 +123,24 @@ public async Task HttpGateway_CounterWasm__Basic__Valid()
Assert.Equal((UnboundedUInt)0, getResponseValue);


var processInfo = new ProcessStartInfo
{
FileName = "dfx",
Arguments = $"canister call {canisterId} inc () --network {httpGateway.Url} --identity anonymous --verbose",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = "/home/gekctek/git/ICP.NET",
};
Debug.WriteLine("dfx " + processInfo.Arguments);
// RequestId requestId;
using (var process = Process.Start(processInfo))
{
string output = process!.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();

// Optionally log or handle the output/error
Debug.WriteLine($"Output: {output}");
Debug.WriteLine($"Error: {error}");
// requestId = new RequestId(Convert.FromHexString(output.Trim().Substring(2)));
}
Debug.WriteLine("---Start-----\n\n\n\n\n\n\n\n\n\n\n");

// CandidArg incResponseArg = await agent.CallAsync(canisterId, "inc", CandidArg.Empty(), cancellationToken: cts.Token);
// Assert.Equal(CandidArg.Empty(), incResponseArg);

// This alternative also doesnt work
RequestId requestId = await agent.CallAsynchronousAsync(canisterId, "inc", CandidArg.Empty());
// ICTimestamp currentTime = await pocketIc.GetTimeAsync();
// await pocketIc.SetTimeAsync(currentTime + TimeSpan.FromSeconds(5));
await pocketIc.TickAsync(1);
// await Task.Delay(5000);
IngressStatus ingressStatus = await pocketIc.GetIngressStatusAsync(requestId, EffectivePrincipal.Canister(canisterId));

switch (ingressStatus.Type)
{
case IngressStatusType.Ok:
break;
case IngressStatusType.NotFound:
throw new Exception("Ingress message not found");
default:
throw new Exception("Ingress message ????");
}

CancellationTokenSource cts = new(TimeSpan.FromSeconds(5));
CandidArg incResponseArg = await agent.WaitForRequestAsync(canisterId, requestId, cts.Token); // Waits indefinitely here
CandidArg incResponseArg = await agent.CallAsync(canisterId, "inc", CandidArg.Empty(), cancellationToken: cts.Token);
Assert.Equal(CandidArg.Empty(), incResponseArg);

getResponse = await agent.QueryAsync(canisterId, "get", CandidArg.Empty());
getResponseArg = getResponse.ThrowOrGetReply();
getResponseValue = getResponseArg.ToObjects<UnboundedUInt>();
Assert.Equal((UnboundedUInt)2, getResponseValue);

// CandidArg setRequestArg = CandidArg.FromObjects((UnboundedUInt)10);
// cts = new(TimeSpan.FromSeconds(5));
// CandidArg setResponseArg = await agent.CallAsync(canisterId, "set", setRequestArg, cancellationToken: cts.Token);
// Assert.Equal(CandidArg.Empty(), setResponseArg);
Assert.Equal((UnboundedUInt)1, getResponseValue);

// await pocketIc.TickAsync();
CandidArg setRequestArg = CandidArg.FromObjects((UnboundedUInt)10);
cts = new(TimeSpan.FromSeconds(5));
CandidArg setResponseArg = await agent.CallAsync(canisterId, "set", setRequestArg, cancellationToken: cts.Token);
Assert.Equal(CandidArg.Empty(), setResponseArg);

// getResponse = await agent.QueryAsync(canisterId, "get", CandidArg.Empty());
// getResponseArg = getResponse.ThrowOrGetReply();
// getResponseValue = getResponseArg.ToObjects<UnboundedUInt>();
// Assert.Equal((UnboundedUInt)10, getResponseValue);
getResponse = await agent.QueryAsync(canisterId, "get", CandidArg.Empty());
getResponseArg = getResponse.ThrowOrGetReply();
getResponseValue = getResponseArg.ToObjects<UnboundedUInt>();
Assert.Equal((UnboundedUInt)10, getResponseValue);

}
}
Expand Down
12 changes: 9 additions & 3 deletions src/Agent/Agents/HttpAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
using System.Linq;
using EdjCase.ICP.BLS;
using System.Threading;
using System.Security.Cryptography;
using System.Diagnostics;

namespace EdjCase.ICP.Agent.Agents
{
Expand Down Expand Up @@ -128,7 +130,9 @@ public async Task<CandidArg> CallAsync(

CallRequest BuildRequest(Principal sender, ICTimestamp now)
{
return new CallRequest(canisterId, method, arg, sender, now);
byte[] nonce = new byte[16];
RandomNumberGenerator.Fill(nonce);
return new CallRequest(canisterId, method, arg, sender, now, nonce);
}
}

Expand Down Expand Up @@ -172,7 +176,9 @@ public async Task<RequestId> CallAsynchronousAsync(

CallRequest BuildRequest(Principal sender, ICTimestamp now)
{
return new CallRequest(canisterId, method, arg, sender, now);
byte[] nonce = new byte[16];
RandomNumberGenerator.Fill(nonce);
return new CallRequest(canisterId, method, arg, sender, now, nonce);
}
}

Expand Down Expand Up @@ -313,7 +319,7 @@ public async Task<StatusResponse> GetReplicaStatusAsync(
SubjectPublicKeyInfo publicKey = this.Identity.GetPublicKey();
principal = publicKey.ToPrincipal();
}
TRequest request = getRequest(principal, ICTimestamp.Future(TimeSpan.FromSeconds(10)));
TRequest request = getRequest(principal, ICTimestamp.Future(TimeSpan.FromMinutes(3)));
Dictionary<string, IHashable> content = request.BuildHashableItem();

SignedContent signedContent;
Expand Down
5 changes: 3 additions & 2 deletions src/Candid/Models/ICTimestamp.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using EdjCase.ICP.Candid.Crypto;
using EdjCase.ICP.Candid.Encodings;
using EdjCase.ICP.Candid.Models.Values;
Expand Down Expand Up @@ -136,13 +137,13 @@ public override string ToString()

private static UnboundedUInt EpochNowInNanoseconds()
{
ulong nanoseconds = (ulong)(((DateTime.UtcNow - epoch).TotalMilliseconds + REPLICA_PERMITTED_DRIFT_MILLISECONDS) * 1_000_000);
ulong nanoseconds = (ulong)((DateTime.UtcNow - epoch).TotalMilliseconds * 1_000_000);
return UnboundedUInt.FromUInt64(nanoseconds);
}

private static UnboundedUInt GetNanosecondsFromTimeSpan(TimeSpan timespanFromNow)
{
return (ulong)(timespanFromNow.Ticks / 100L);
return (ulong)(timespanFromNow.TotalMilliseconds * 1_000_000);
}
}
}
87 changes: 85 additions & 2 deletions src/PocketIC/API.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading