Skip to content

Commit

Permalink
Async update calls with await
Browse files Browse the repository at this point in the history
  • Loading branch information
Gekctek committed Dec 22, 2024
1 parent 7d64b03 commit ebe6a3d
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 33 deletions.
7 changes: 7 additions & 0 deletions src/Candid/API.xml

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

10 changes: 10 additions & 0 deletions src/Candid/Models/RequestId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ public RequestId(byte[] rawValue)
this.RawValue = rawValue;
}

/// <summary>
/// Creates a request id from raw bytes, same as the constructor
/// </summary>
/// <param name="bytes">Raw request id bytes</param>
/// <returns>A request id object</returns>
public static RequestId FromBytes(byte[] bytes)
{
return new RequestId(bytes);
}

/// <summary>
/// Converts a hashable object into a request id
/// </summary>
Expand Down
39 changes: 33 additions & 6 deletions src/PocketIC/API.xml

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

14 changes: 7 additions & 7 deletions src/PocketIC/IPocketIcHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ Task<IngressStatus> GetIngressStatusAsync(
EffectivePrincipal effectivePrincipal);

/// <summary>
/// Submits an ingress message to a canister without waiting for execution
/// Executes an ingress message on a canister and waits for the response
/// </summary>
/// <param name="instanceId">The id of the PocketIC instance</param>
/// <param name="sender">The principal sending the message</param>
Expand All @@ -172,7 +172,7 @@ Task<IngressStatus> GetIngressStatusAsync(
/// <param name="request">The raw candid request argument</param>
/// <param name="effectivePrincipal">Optional effective principal for the call, defaults to canister id</param>
/// <returns>The raw candid response</returns>
Task<CandidArg> SubmitIngressMessageAsync(
Task<CandidArg> ExecuteIngressMessageAsync(
int instanceId,
Principal sender,
Principal canisterId,
Expand All @@ -181,16 +181,16 @@ Task<CandidArg> SubmitIngressMessageAsync(
EffectivePrincipal? effectivePrincipal = null);

/// <summary>
/// Executes an ingress message on a canister and waits for the response
/// Submits an ingress message to a canister without waiting for execution
/// </summary>
/// <param name="instanceId">The id of the PocketIC instance</param>
/// <param name="sender">The principal sending the message</param>
/// <param name="canisterId">The target canister id</param>
/// <param name="method">The method name to call</param>
/// <param name="request">The raw candid request argument</param>
/// <param name="effectivePrincipal">Optional effective principal for the call, defaults to canister id</param>
/// <returns>The raw candid response</returns>
Task<CandidArg> ExecuteIngressMessageAsync(
/// <returns>The request id for the message</returns>
Task<RequestId> SubmitIngressMessageAsync(
int instanceId,
Principal sender,
Principal canisterId,
Expand All @@ -202,9 +202,9 @@ Task<CandidArg> ExecuteIngressMessageAsync(
/// Waits for an ingress message to complete execution
/// </summary>
/// <param name="instanceId">The id of the PocketIC instance</param>
/// <param name="messageId">The id of the ingress message</param>
/// <param name="requestId">The id of the ingress message</param>
/// <param name="effectivePrincipal">Effective principal for the call</param>
Task AwaitIngressMessageAsync(int instanceId, RequestId messageId, EffectivePrincipal effectivePrincipal);
Task<CandidArg> AwaitIngressMessageAsync(int instanceId, RequestId requestId, EffectivePrincipal effectivePrincipal);

/// <summary>
/// Sets the current time of the IC instance
Expand Down
54 changes: 52 additions & 2 deletions src/PocketIC/PocketIc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using EdjCase.ICP.Candid.Models;
using EdjCase.ICP.PocketIC.Client;
using EdjCase.ICP.PocketIC.Models;
using Org.BouncyCastle.Asn1.Ocsp;

namespace EdjCase.ICP.PocketIC
{
Expand Down Expand Up @@ -745,7 +746,7 @@ public async Task<CandidArg> UpdateCallRawAsync(
Principal sender,
Principal canisterId,
string method,
CandidArg arg,
CandidArg? arg = null,
EffectivePrincipal? effectivePrincipal = null
)
{
Expand All @@ -754,11 +755,60 @@ public async Task<CandidArg> UpdateCallRawAsync(
sender,
canisterId,
method,
arg,
arg ?? CandidArg.Empty(),
effectivePrincipal
);
}

/// <summary>
/// Submits an update call on a canister with a raw CandidArg and gets a request id
/// in the response that can be awaited with <see cref="AwaitUpdateCallAsync(RequestId, Principal)"/> method
/// </summary>
/// <param name="sender">The principal making the call</param>
/// <param name="canisterId">The target canister ID</param>
/// <param name="method">The method name to call</param>
/// <param name="arg">The raw candid argument for the call</param>
/// <param name="effectivePrincipal">Optional effective principal for the call, defaults to canister id</param>
/// <returns>A raw candid argument from the response</returns>
public async Task<RequestId> UpdateCallRawAsynchronousAsync(
Principal sender,
Principal canisterId,
string method,
CandidArg? arg = null,
EffectivePrincipal? effectivePrincipal = null
)
{
return await this.HttpClient.SubmitIngressMessageAsync(
this.InstanceId,
sender,
canisterId,
method,
arg ?? CandidArg.Empty(),
effectivePrincipal
);
}

/// <summary>
/// Awaits an update call response for a given request id, from the <see cref="UpdateCallRawAsynchronousAsync"/> method
/// </summary>
/// <param name="requestId">The request id to await</param>
/// <param name="effectivePrincipal">The effective principal for the request</param>
/// <returns>The response from the update call</returns>
public async Task<CandidArg> AwaitUpdateCallAsync(RequestId requestId, EffectivePrincipal effectivePrincipal)
{
return await this.HttpClient.AwaitIngressMessageAsync(this.InstanceId, requestId, effectivePrincipal);
}

/// <summary>
/// Awaits an update call response for a given request id, from the <see cref="UpdateCallRawAsynchronousAsync"/> method
/// </summary>
/// <param name="requestId">The request id to await</param>
/// <param name="canisterId">The canister id for the request</param>
public async Task<CandidArg> AwaitUpdateCallAsync(RequestId requestId, Principal canisterId)
{
return await this.AwaitUpdateCallAsync(requestId, EffectivePrincipal.Canister(canisterId));
}

/// <summary>
/// Disposes of the PocketIC instance by deleting the instance
/// </summary>
Expand Down
Loading

0 comments on commit ebe6a3d

Please sign in to comment.