-
Notifications
You must be signed in to change notification settings - Fork 312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: renaming pxe_db.nr
as capsules.nr
#11905
Merged
benesjan
merged 6 commits into
master
from
02-11-refactor_renaming_pxe_db.nr_as_capsules.nr
Feb 12, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 0 additions & 19 deletions
19
...docs/developers/guides/smart_contracts/writing_contracts/how_to_pop_capsules.md
This file was deleted.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
...docs/developers/guides/smart_contracts/writing_contracts/how_to_use_capsules.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
--- | ||
title: Using Capsules | ||
sidebar_position: 5 | ||
tags: [functions, oracles] | ||
--- | ||
|
||
Capsules are a per-contract non-volatile database. | ||
It can be used for storing arbitrary data that can be retrieved later. | ||
The data is stored locally in PXE and it is scoped per contract address, so external contracts cannot access it. | ||
The capsule (data stored under a storage slot in the capsules database) persists until explicitly deleted with `delete`. | ||
|
||
The capsules module provides these main functions: | ||
|
||
- `store<T, N>` - Stores arbitrary data at a slot, overwriting any existing data | ||
- `load<T, N>` - Retrieves previously stored data from a slot | ||
- `delete` - Deletes data at a slot | ||
- `copy` - Efficiently copies contiguous entries between slots | ||
|
||
### 1. Import capsules into your smart contract | ||
|
||
Import the capsules module: | ||
|
||
#include_code import_capsules noir-projects/noir-contracts/contracts/contract_class_registerer_contract/src/main.nr rust | ||
|
||
### 2. Store and load data | ||
|
||
You can store any type that implements `Serialize` and `Deserialize`: | ||
|
||
#include_code load_capsule noir-projects/noir-contracts/contracts/contract_class_registerer_contract/src/main.nr rust | ||
|
||
The data is stored per contract address and slot. When loading, you'll get back an `Option<T>` - `None` if no data exists at that slot. | ||
|
||
### 3. Copying data | ||
|
||
You can use `copy` to move contiguous entries between slots without repeated loads and stores. | ||
This supports overlapping source and destination regions. | ||
|
||
Note that all values are scoped per contract address, so external contracts cannot access them. | ||
|
||
### 4. Using CapsuleArray | ||
|
||
The `CapsuleArray<T>` type provides a dynamically sized array backed by capsules. | ||
It handles the storage layout and management automatically. | ||
The array stores its length at a base slot, with elements stored in consecutive slots after it. | ||
|
||
Key functions: | ||
|
||
- `at(contract_address, base_slot)` - Creates/connects to an array at the given base slot | ||
- `len()` - Returns the number of elements in the array | ||
- `push(value)` - Appends a value to the end of the array | ||
- `get(index)` - Retrieves the value at the given index | ||
- `remove(index)` - Removes an element, shifting subsequent elements to maintain contiguous storage | ||
|
||
<!-- TODO: Document actual use case of CapsuleArray here once it's actually used. --> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🫡 🎻 (popping capsules)