Skip to content

Commit 1a69da7

Browse files
Merge pull request #1207 from dfinity/jessiemongeon1-patch-38
add: Deploy to ICP Ninja + standardize README
2 parents 85363b9 + 1dac73a commit 1a69da7

File tree

1 file changed

+19
-39
lines changed

1 file changed

+19
-39
lines changed

rust/parallel_calls/README.md

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,45 @@
22

33
This example demonstrates how to implement inter-canister calls that run in parallel in Rust, and highlights some differences between parallel and sequential calls. Running independent calls in parallel can lower the latency, especially when messages are sent across subnets. For example, a canister that swaps two tokens might want to launch both token transfer operations in parallel.
44

5-
## Architecture
6-
75
The sample code revolves around two simple canisters, `caller` and `callee`. `Caller` has three endpoints:
86
1. `setup_callee`, to set the ID of the callee canister.
97
2. `sequential_calls` and `parallel_calls`, which both take a number `n` and issue `n` calls to the callee, returning the number of successful calls. The former performs calls sequentially, the latter in parallel.
108

119
The callee exposes a simple `ping` endpoint that takes no parameters and returns nothing.
1210

13-
## Prerequisites
11+
## Deploying from ICP Ninja
1412

15-
- [x] Install the [IC
16-
SDK](https://internetcomputer.org/docs/current/developer-docs/getting-started/install). For local testing, `dfx >= 0.22.0` is required.
17-
- [x] Clone the example dapp project: `git clone https://github.com/dfinity/examples`
13+
[![](https://icp.ninja/assets/open.svg)](https://icp.ninja/editor?g=https://github.com/dfinity/examples/tree/master/rust/parallel_calls)
1814

19-
Begin by opening a terminal window.
15+
## Build and deploy from the command-line
2016

21-
## Step 1: Setup project environment
17+
### 1. [Download and install the IC SDK.](https://internetcomputer.org/docs/building-apps/getting-started/install)
2218

23-
Navigate into the folder containing the project's files and start a local instance of the replica with the commands:
19+
### 2. Download your project from ICP Ninja using the 'Download files' button on the upper left corner, or [clone the GitHub examples repository.](https://github.com/dfinity/examples/)
2420

25-
```bash
26-
cd examples/rust/parallel_calls
27-
dfx start --background
28-
```
21+
### 3. Navigate into the project's directory.
2922

30-
## Step 2: Deploy the canisters
23+
### 4. Deploy the project to your local environment:
3124

32-
```bash
33-
dfx deploy --no-wallet
3425
```
35-
36-
## Step 3: Set up the caller canister
37-
38-
We now provide the ID of the callee to the caller, such that the caller can initiate calls.
39-
```
40-
dfx canister call caller setup_callee "(principal \"`dfx canister id callee`\")"
26+
dfx start --background --clean && dfx deploy
4127
```
4228

43-
## Step 4: Invoke sequential and parallel calls
29+
### 5. Setup the caller canister.
4430

45-
Let's first call the different endpoints of the `caller` canister using `dfx`
31+
Provide the ID of the callee to the caller, such that the caller can initiate calls:
4632

47-
```bash
48-
dfx canister call caller sequential_calls 100
4933
```
50-
51-
This should output:
52-
```bash
53-
(100 : nat64)
34+
dfx canister call caller setup_callee "(principal \"`dfx canister id callee`\")"
5435
```
5536

56-
And the other endpoint:
37+
#### 6. Invoke sequential and parallel calls
5738

5839
```bash
40+
dfx canister call caller sequential_calls 100
5941
dfx canister call caller parallel_calls 100
6042
```
6143

62-
which outputs:
63-
64-
```bash
65-
(100 : nat64)
66-
```
67-
6844
The results are identical: all calls succeed. There also isn't a large difference in performance between these calls:
6945

7046
```bash
@@ -78,7 +54,7 @@ dfx canister call caller parallel_calls 100 0.11s user 0.03s system 8% cpu 1.72
7854

7955
The reason why the performance is similar is because the local replica has only a single subnet. Inter-canister calls normally have almost no latency on a single subnet, so it doesn't matter much if we run them sequentially or in parallel.
8056

81-
However, once we increase the number of calls, we observe a difference in both the results and performance.
57+
However, once you increase the number of calls, you will observe a difference in both the results and performance.
8258

8359
```bash
8460
time dfx canister call caller sequential_calls 2000
@@ -93,7 +69,7 @@ All the sequential calls succeed, but most parallel calls fail. The reason is th
9369

9470
Lastly, the parallel calls here complete sooner -- because most of them fail!
9571

96-
## Step 5: Multi-subnet setting
72+
## Multi-subnet setting
9773

9874
Parallel calls are a lot more useful in multi-subnet settings. We can create such a setting locally using Pocket IC.
9975

@@ -110,3 +86,7 @@ Parallel calls: 90/90 successful calls in 353.738958ms
11086
```
11187

11288
As you can see, parallel calls run a lot faster than sequential calls here. The difference on the IC mainnet would be significantly larger still, as Pocket IC executes rounds much faster than the IC mainnet.
89+
90+
## Security considerations and best practices
91+
92+
If you base your application on this example, it is recommended that you familiarize yourself with and adhere to the [security best practices](https://internetcomputer.org/docs/building-apps/security/overview) for developing on ICP. This example may not implement all the best practices.

0 commit comments

Comments
 (0)