Skip to content

Commit

Permalink
Update rust readmes
Browse files Browse the repository at this point in the history
  • Loading branch information
jessiemongeon1 committed Dec 19, 2024
1 parent 80ea550 commit 323c768
Show file tree
Hide file tree
Showing 27 changed files with 293 additions and 617 deletions.
10 changes: 5 additions & 5 deletions rust/basic_bitcoin/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Basic Bitcoin
# Basic Bitcoin

This tutorial will walk you through how to deploy a sample [canister smart contract](https://internetcomputer.org/docs/current/developer-docs/multi-chain/bitcoin/overview) **that can send and receive Bitcoin** on the Internet Computer.

Expand All @@ -14,7 +14,7 @@ of the Internet Computer.

For a deeper understanding of the ICP < > BTC integration, see the [Bitcoin integration documentation](https://wiki.internetcomputer.org/wiki/Bitcoin_Integration).

### Prerequisites
## Prerequisites

* [x] Install the [IC
SDK](https://internetcomputer.org/docs/current/developer-docs/getting-started/install).
Expand Down Expand Up @@ -111,7 +111,7 @@ from three types of addresses:
key. This precaution is to prevent attacks that can occur when creating
taproot multisigner addresses using specific multisignature schemes. However,
the Schnorr API of the internet computer does not support Schnorr
multisignatures.
multisignatures.
3. A [P2TR
address](https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki)
where the funds can be spent using the provided public key with the script
Expand Down Expand Up @@ -209,7 +209,7 @@ reflected in your current balance.

## Step 6: Retrieving block headers

You can also get a range of Bitcoin block headers by using the `get_block_headers`
You can also get a range of Bitcoin block headers by using the `get_block_headers`
endpoint on your canister.

In the Candid UI, write the desired start height and optionally end height, and click on "Call":
Expand All @@ -233,7 +233,7 @@ In this tutorial, you were able to:
* Connect the canister to the Bitcoin testnet.
* Send the canister some testnet BTC.
* Check the testnet BTC balance of the canister.
* Use the canister to send testnet BTC to another testnet BTC address.
* Use the canister to send testnet BTC to another testnet BTC address.

This example is extensively documented in the following tutorials:

Expand Down
2 changes: 1 addition & 1 deletion rust/basic_dao/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Certain system parameters, like the number of `Yes` votes needed to pass a propo

View the [canister service definition](https://github.com/dfinity/examples/blob/master/rust/basic_dao/src/basic_dao/src/basic_dao.did) for more details.

### Prerequisites
## Prerequisites
This example requires an installation of:

- [x] The Rust toolchain (e.g. cargo).
Expand Down
2 changes: 1 addition & 1 deletion rust/basic_ethereum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ features of the Internet Computer.
For a deeper understanding of the ICP < > ETH integration, see
the [Ethereum integration overview](https://internetcomputer.org/docs/current/developer-docs/multi-chain/ethereum/overview).

### Prerequisites
## Prerequisites

- [x] Install the [IC SDK](https://internetcomputer.org/docs/current/developer-docs/getting-started/install).

Expand Down
2 changes: 1 addition & 1 deletion rust/canister-snapshots/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The `remove_spam` canister method intentionally includes a bug to simulate data
The example outlines the steps to install the canister, create a snapshot,
and subsequently restore the data after the simulated data loss.

### Prerequisites
## Prerequisites

This example requires an installation of:

Expand Down
2 changes: 1 addition & 1 deletion rust/canister_logs/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Canister logs

### Prerequisites
## Prerequisites

- [x] Install the [IC
SDK](https://internetcomputer.org/docs/current/developer-docs/getting-started/install). For local testing, `dfx >= 0.22.0` is required.
Expand Down
2 changes: 1 addition & 1 deletion rust/composite_query/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Composite queries

### Prerequisites
## Prerequisites
This example requires an installation of:

- [x] Install the [IC SDK](https://internetcomputer.org/docs/current/developer-docs/setup/install/index.mdx).
Expand Down
2 changes: 1 addition & 1 deletion rust/counter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This example demonstrates a counter application.

### Prerequisites
## Prerequisites
This example requires an installation of:

- [x] Install the [IC SDK](https://internetcomputer.org/docs/current/developer-docs/setup/install/).
Expand Down
2 changes: 1 addition & 1 deletion rust/dip721-nft-container/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ A running instance of the Rust canister for demonstration purposes is available
The interface is meant to be programmatic, but the Rust version additionally contains HTTP functionality so you can view a metadata file at `<canister URL>/<NFT ID>/<file ID>`.
It contains six NFTs, so you can look at items from `<canister URL>/0/0` to `<canister URL>/5/0`.

### Prerequisites
## Prerequisites

- [x] Install the [IC SDK](https://internetcomputer.org/docs/current/developer-docs/getting-started/install).
- [x] Clone the example dapp project: `git clone https://github.com/dfinity/examples`
Expand Down
2 changes: 1 addition & 1 deletion rust/face-recognition/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ torch.onnx.export(resnet, input, "face-recognition.onnx", verbose=False, opset_v

- #### Step 4: This should produce `face-recognition.onnx`. Copy the file to the root of this repository.

### Prerequisites
## Prerequisites

- [x] Install the [IC
SDK](https://internetcomputer.org/docs/current/developer-docs/getting-started/install). For local testing, `dfx >= 0.22.0` is required.
Expand Down
2 changes: 1 addition & 1 deletion rust/hello/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ This example is based on the default project created by running `dfx new hello`.

This example is based on the default project created by running `dfx new --type=rust hello`.

### Prerequisites
## Prerequisites

This example requires an installation of:
- [x] Install the [IC SDK](https://internetcomputer.org/docs/current/developer-docs/setup/install/index.mdx).
Expand Down
2 changes: 1 addition & 1 deletion rust/icp_transfer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The sample code revolves around one core transfer function which takes as input

This sample will use the Rust variant.

### Prerequisites
## Prerequisites

- [x] Install the [IC
SDK](https://internetcomputer.org/docs/current/developer-docs/getting-started/install). For local testing, `dfx >= 0.22.0` is required.
Expand Down
2 changes: 1 addition & 1 deletion rust/nft-wallet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This is an NFT wallet example dapp that utilizes minted NFTs from the Rust dip721-nft-container. Among some of its essential features, the wallet can register NFTs, transfer out NFTs, and check how many NFTs it contains. This dapp includes a frontend UI for interaction.

### Prerequisites
## Prerequisites

- [x] Install the [IC
SDK](https://internetcomputer.org/docs/current/developer-docs/getting-started/install). For local testing, `dfx >= 0.22.0` is required.
Expand Down
2 changes: 1 addition & 1 deletion rust/parallel_calls/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The sample code revolves around two simple canisters, `caller` and `callee`. `Ca

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

### Prerequisites
## Prerequisites

- [x] Install the [IC
SDK](https://internetcomputer.org/docs/current/developer-docs/getting-started/install). For local testing, `dfx >= 0.22.0` is required.
Expand Down
5 changes: 1 addition & 4 deletions rust/performance_counters/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ In the future, ICP might expose more performance counters.

## Prerequisites

## Prerequisites

- [x] Install the [IC
SDK](https://internetcomputer.org/docs/current/developer-docs/getting-started/install). For local testing, `dfx >= 0.22.0` is required.
- [x] Install the [IC SDK](https://internetcomputer.org/docs/current/developer-docs/getting-started/install). For local testing, `dfx >= 0.22.0` is required.
- [x] Clone the example dapp project: `git clone https://github.com/dfinity/examples`

## Step 1: Setup project environment
Expand Down
75 changes: 38 additions & 37 deletions rust/periodic_tasks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,17 @@ The example consists of two canisters named `heartbeat` and `timer`, both implem
## Prerequisites
This example requires an installation of:

- [x] Install the [IC SDK](https://internetcomputer.org/docs/current/developer-docs/setup/install/index.mdx).
- [x] Install the [IC SDK](https://internetcomputer.org/docs/current/developer-docs/getting-started/install).
- [x] Clone the example dapp project: `git clone https://github.com/dfinity/examples`

### Example 1:
## Example 1: heartbeats and timers

- #### Step 1: Begin by opening a terminal window and navigating into the project's directory.
- ### Step 1: Setup project environment

```sh
cd examples/rust/periodic_tasks
```

- #### Step 2: Start a clean local Internet Computer replica and a web server:
Navigate into the folder containing the project's files and start a local instance of the replica with the command:

```sh
dfx stop
cd examples/rust/periodic_tasks
dfx start --clean
```

Expand All @@ -37,18 +33,18 @@ This terminal will stay blocked, printing log messages, until the `Ctrl+C` is pr
Example output:

```sh
% dfx stop && dfx start --clean
dfx start --clean
[...]
Dashboard: http://localhost:63387/_/dashboard
```

- #### Step 3: Open another terminal window in the same directory:
- #### Step 2: Open another terminal window in the same directory:

```sh
cd examples/rust/periodic_tasks
```

- #### Step 4: Compile and deploy `heartbeat` and `timer` canisters, setting the interval for periodic tasks to 10s:
- #### Step 3: Compile and deploy the `heartbeat` and `timer` canisters, setting the interval for periodic tasks to 10s:

```sh
dfx deploy heartbeat --argument 10
Expand Down Expand Up @@ -77,25 +73,25 @@ URLs:
timer: http://127.0.0.1/...
```

- #### Step 5: After 10s, observe similar non-zero counters in both canisters:
- #### Step 4: After 10s, observe similar non-zero counters in both canisters:

```sh
dfx canister call heartbeat counter
dfx canister call timer counter
```

Note, as the canisters deployed one by one, there might be a minor discrepancy in the counters.
Note: As the canisters deployed one by one, there might be a minor discrepancy in the counters.

Example output:

```sh
% dfx canister call heartbeat counter
% dfx canister call heartbeat counter
(8 : nat32)
% dfx canister call timer counter
% dfx canister call timer counter
(7 : nat32)
```

- #### Step 6: Compare the amount of cycles used to schedule the periodic task with 10s interval:
- #### Step 5: Compare the amount of cycles used to schedule the periodic task with 10s interval:

```sh
dfx canister call heartbeat cycles_used
Expand All @@ -115,36 +111,40 @@ For periodic tasks with 10 sec intervals, the `heartbeat` canister uses *more* c

Not only do timers use fewer cycles, but they are also more composable. As there is no global state or methods to export, different libraries with timers could be easily used in the same project.

Also, timers provide isolation between the scheduling logic and the periodic task. If the periodic task fails, all the changes made by this task will be reverted, but the timers library state will be updated, i.e. the failed task will be removed from the list of timers to execute.
Also, timers provide isolation between the scheduling logic and the periodic task. If the periodic task fails, all the changes made by this task will be reverted, but the timers library state will be updated, i.e., the failed task will be removed from the list of timers to execute.

For such isolation of execution and scheduling contexts, the internal timers library uses self-canister calls:

```rust
# This is a pseudo-code of a self call:
ic_cdk::call(ic_cdk::id(), "periodic_task", ());
```
```rust
# This is a pseudo-code of a self call:
ic_cdk::call(ic_cdk::id(), "periodic_task", ());
```

Despite the [costs](https://internetcomputer.org/docs/current/developer-docs/gas-cost) associated with such self-canister calls, the timers library still uses fewer cycles than the heartbeats.

Despite the [costs](https://internetcomputer.org/docs/current/developer-docs/production/computation-and-storage-costs) associated with such self-canister calls, the timers library still uses fewer cycles than the heartbeats.
## Example 2: Cycles usage for tasks with 1s interval

### Example 2: Cycles usage for tasks with 1s interval

- ### Step 1: Setup project environment

- #### Step 1: Open a new terminal window in the example root directory:
Navigate into the folder containing the project's files and start a local instance of the replica with the command:

```sh
cd examples/rust/periodic_tasks
dfx start --clean
```

- #### Step 2: Start a clean local Internet Computer replica and a web server:
This terminal will stay blocked, printing log messages, until the `Ctrl+C` is pressed or `dfx stop` command is run.

Example output:

```sh
dfx stop
dfx start --clean
[...]
Dashboard: http://localhost:63387/_/dashboard
```

This terminal will stay blocked, printing log messages, until the `Ctrl+C` is pressed or `dfx stop` command is run.

- #### Step 3: Open another terminal window in the same directory:
- #### Step 2: Open another terminal window in the same directory:

```sh
cd examples/rust/periodic_tasks
Expand All @@ -153,12 +153,12 @@ cd examples/rust/periodic_tasks
Example output:

```sh
% dfx stop && dfx start --clean
dfx start --clean
[...]
Dashboard: http://localhost:63387/_/dashboard
```

- #### Step 4:. Compile and deploy `heartbeat` and `timer` canisters, setting the interval for periodic tasks to 1s:
- #### Step 3:. Compile and deploy `heartbeat` and `timer` canisters, setting the interval for periodic tasks to 1s:

```sh
dfx deploy --argument 1 heartbeat
Expand Down Expand Up @@ -187,25 +187,25 @@ URLs:
timer: http://127.0.0.1/...
```

- #### Step 5: After a few seconds, observe similar non-zero counters in both canisters:
- #### Step 4: After a few seconds, observe similar non-zero counters in both canisters:

```sh
dfx canister call heartbeat counter
dfx canister call timer counter
```

Note, as the canisters deployed one by one, there might be a minor discrepancy in the counters.
Note: As the canisters deployed one by one, there might be a minor discrepancy in the counters.

Example output:

```sh
% dfx canister call heartbeat counter
% dfx canister call heartbeat counter
(8 : nat32)
% dfx canister call timer counter
% dfx canister call timer counter
(9 : nat32)
```

- #### Step 6: Compare the number of cycles used to schedule the periodic task with 1s interval:
- #### Step 5: Compare the number of cycles used to schedule the periodic task with 1s interval:

```sh
dfx canister call heartbeat cycles_used
Expand All @@ -230,6 +230,7 @@ Also, there is no isolation between the scheduling logic and the periodic task.
For such isolation of execution and scheduling contexts, the timers library uses internal self-canister calls as described in `Demo 1`. Due to the [costs](https://internetcomputer.org/docs/current/developer-docs/production/computation-and-storage-costs) associated with such self-canister calls, `timer` canister uses more cycles for very frequent periodic tasks.

## Further learning

1. Have a look at the locally running dashboard. The URL is at the end of the `dfx start` command: `Dashboard: http://localhost/...`
2. Check out `heartbeat` and `timer` canisters Candid user interface. The URLs are at the end of the `dfx deploy` command: `heartbeat: http://127.0.0.1/...`
3. Find which interval makes even the costs of running periodic tasks in the `timer` and `heartbeat` canisters: `dfx deploy heartbeat --argument 5 && dfx deploy timer --argument 5`
Expand Down
Loading

0 comments on commit 323c768

Please sign in to comment.