You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A storage variable is declared as `storage {name}: {type}`. Storage variables contain singleton values. They are declared at program scope and are stored on chain, similar to mappings.
138
+
A storage variable is declared as `storage {name}: {type}`. Storage variables contain singleton values. They are declared at program scope and are stored on chain, similar to mappings.
139
+
139
140
```leo
140
141
// On-chain storage of an `counter` storage variable of type u32,
141
142
storage counter: u32;
142
143
```
144
+
143
145
A storage vector is declared as `storage {name}: [{type}]`. Storage vectors contain dynamic lists of values of a given type. They are declared at program scope and are stored on chain, similar to mappings.
146
+
144
147
```leo
145
148
// On-chain storage of an `accounts` storage vector of type address,
Copy file name to clipboardExpand all lines: documentation/language/03_data_types.md
+22-5Lines changed: 22 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -126,6 +126,12 @@ program test.aleo {
126
126
}
127
127
```
128
128
129
+
Signature literals can be also be used. For example:
130
+
131
+
```leo
132
+
let sig: signature = sign195m229jvzr0wmnshj6f8gwplhkrkhjumgjmad553r997u7pjfgpfz4j2w0c9lp53mcqqdsmut2g3a2zuvgst85w38hv273mwjec3sqjsv9w6uglcy58gjh7x3l55z68zsf24kx7a73ctp8x8klhuw7l2p4s3aq8um5jp304js7qcnwdqj56q5r5088tyvxsgektun0rnmvtsuxpe6sj
Struct types are declared and constructed with a familiar syntax. Note that there is a global namespace for struct
227
-
types across your program and its dependencies. If a dependency declares a struct type `T`, you may access that type
228
-
without any qualifier.
232
+
Struct types are declared and constructed with a familiar syntax.
233
+
234
+
Structs defined within a program can be referenced by their name. Structs defined in other programs must be referenced using the fully qualified form `program_name.aleo/StructName`.
229
235
230
236
```leo
231
237
program test.aleo {
@@ -244,7 +250,17 @@ program test.aleo {
244
250
}
245
251
```
246
252
247
-
As of v3.0.0, Leo now supports **const generics** for struct types:
253
+
Structs defined in external programs can be referenced and constructed using their fully qualified name:
254
+
255
+
```leo
256
+
let s: external_program.aleo/S2 = external_program.aleo/S2 {
Note that generic structs cannot currently be imported outside a program, but can be declared and used in submodules. Acceptable types for const generic parameters include integer types, `bool`, `scalar`, `group`, `field`, and `address`.
272
+
273
+
Acceptable types for const generic parameters include integer types, `bool`, `scalar`, `group`, `field`, and `address`. Const generic structs may be declared and used within a program and its submodules, but they cannot currently be imported from external programs.
@@ -75,26 +75,33 @@ Mapping operations are only allowed in an [async function](#async-function) or a
75
75
76
76
## Storage Variables
77
77
78
-
Storage variables behave similar to option types. There are several functions available to query and modify singleton storage variables. The examples below will reference the following:
78
+
Storage variables behave similar to option types. There are several functions available to query and modify singleton storage variables. The examples below will reference the following:
79
79
```leo
80
80
storage counter: u64;
81
81
```
82
82
83
83
### Querying
84
-
To query the value currrently stored at `counter`:
84
+
To query the value currently stored at `counter`:
85
+
85
86
```leo
86
87
counter.unwrap();
87
88
```
89
+
88
90
Note that if `counter` has not been initialized, then the program will fail to execute. To query the value with a fallback for this case:
91
+
89
92
```leo
90
93
counter.unwrap_or(fallback_value);
91
94
```
95
+
92
96
### Modifying
93
97
To set a value for `counter`:
98
+
94
99
```leo
95
100
counter = 5u64;
96
101
```
102
+
97
103
To unset the value at `counter`:
104
+
98
105
```leo
99
106
counter = none;
100
107
```
@@ -115,51 +122,105 @@ program storage_variable.aleo {
115
122
116
123
}
117
124
```
125
+
118
126
:::info
119
-
Storage variable operations are only allowed in an [async function](#async-function) or async block.
127
+
Storage variable operations are only allowed in an [async function](#async-function) or `async` block.
120
128
:::
121
129
130
+
### External Access
131
+
132
+
Storage variables defined in another program can be accessed using the fully qualified form `program_name.aleo/storage_name`. External storage variables are **read-only** and cannot be modified.
133
+
134
+
For example, suppose another program defines the following storage variable:
135
+
136
+
```leo
137
+
program external_program.aleo {
138
+
storage counter: u64;
139
+
140
+
...
141
+
}
142
+
```
143
+
144
+
You may query this value from your program using:
145
+
146
+
```leo
147
+
let value: u64 = external_program.aleo/counter.unwrap();
148
+
```
149
+
150
+
As with local storage variables, calling `unwrap()` will cause execution to fail if the storage variable has not been initialized. To safely query the value with a fallback:
151
+
152
+
```leo
153
+
let value: u64 = external_program.aleo/counter.unwrap_or(0u64);
154
+
```
155
+
156
+
External storage variables cannot be assigned to or unset. The following operations are invalid:
157
+
158
+
```leo
159
+
external_program.aleo/counter = 5u64; // invalid
160
+
external_program.aleo/counter = none; // invalid
161
+
```
122
162
123
163
## Storage Vectors
124
164
125
-
Storage vectors behave like dynamic arrays of values of a given types. There are several functions available to query and modify storage vectors. The examples below will reference the following:
165
+
Storage vectors behave like dynamic arrays of values of a given type. Several functions are available to query and modify storage vectors. The examples below reference the following declaration:
166
+
126
167
```leo
127
168
storage id_numbers: [u64];
128
169
```
129
170
130
171
### Querying
131
-
To query the element currrently stored in `id_numbers` at index `idx`:
172
+
173
+
To query the element currently stored in `id_numbers` at index `idx`:
174
+
132
175
```leo
133
176
id_numbers.get(idx);
134
177
```
178
+
179
+
This returns `u64?`. If `idx` is out of bounds, the result is `none`.
180
+
135
181
To get the current length of `id_numbers`:
182
+
136
183
```leo
137
184
id_numbers.len();
138
185
```
186
+
187
+
This always returns a `u32` and cannot fail.
188
+
139
189
### Modifying
190
+
140
191
To set an element at index `idx` in `id_numbers`:
192
+
141
193
```leo
142
194
id_numbers.set(idx, value);
143
195
```
196
+
144
197
To push an element onto the end of `id_numbers`:
198
+
145
199
```leo
146
200
id_numbers.push(value);
147
201
```
202
+
148
203
To pop and return the last element of `id_numbers`:
204
+
149
205
```leo
150
206
id_numbers.pop();
151
207
```
208
+
152
209
To remove the element at index `idx`, return it, and replace it with the final element of `id_numbers`:
210
+
153
211
```leo
154
212
id_numbers.swap_remove(idx);
155
213
```
156
-
To clear the every element in `id_numbers`:
214
+
215
+
To clear every element in `id_numbers`:
216
+
157
217
```leo
158
-
id_numbers.clear()
218
+
id_numbers.clear();
159
219
```
220
+
160
221
:::note
161
-
-`clear()` does not actually remove any values from the vector. It just sets the length to 0.
162
-
- Similarly `swap_remove()` and `pop()` do not actually remove values either. They just reduce the length by 1 to make sure the last element is no longer accessible.
222
+
-`clear()` does not actually remove any values from the vector. It simply sets the length to `0`.
223
+
- Similarly,`swap_remove()` and `pop()` do not physically remove values. They reduce the length by `1`, ensuring the final element is no longer accessible.
163
224
:::
164
225
165
226
### Usage
@@ -176,7 +237,6 @@ program storage_vector.aleo {
176
237
id_numbers.push(new_id);
177
238
}
178
239
179
-
180
240
async transition remove_id(idx: u32) -> Future {
181
241
return remove_id_onchain(idx);
182
242
}
@@ -186,10 +246,39 @@ program storage_vector.aleo {
186
246
}
187
247
}
188
248
```
249
+
189
250
:::info
190
-
Storage vector operations are only allowed in an [async function](#async-function) or async block.
251
+
Storage vector operations are only allowed in an [async function](#async-function) or `async` block.
191
252
:::
192
253
254
+
### External Access
255
+
256
+
Storage vectors defined in another program can be accessed using the fully qualified form `program_name.aleo/storage_name`. External storage vectors are **read-only** and cannot be modified.
257
+
258
+
For example, suppose another program defines the following storage vector:
259
+
260
+
```leo
261
+
program external_program.aleo {
262
+
storage id_numbers: [u64];
263
+
}
264
+
```
265
+
266
+
You may query elements or the length of this vector from your program:
267
+
268
+
```leo
269
+
let first: Option<u64> = external_program.aleo/id_numbers.get(0u32);
270
+
let length: u32 = external_program.aleo/id_numbers.len();
271
+
```
272
+
273
+
External storage vectors cannot be modified. The following operations are invalid:
@@ -381,9 +470,9 @@ snarkVM imposes the following limits on Aleo programs:
381
470
382
471
Some other protocol-level limits to be aware of are:
383
472
-**the maximum transaction size is 128 KB.** If your program exceeds this, perhaps by requiring large inputs or producing large outputs, consider optimizing the data types in your Leo code.
384
-
-**the maxmimum number of micro-credits your transaction can consume for on-chain execution is `100_000_000`.**. If your program exceeds this, consider optimizing on-chain components of your Leo code.
473
+
-**the maximum number of micro-credits your transaction can consume for on-chain execution is `100_000_000`.**. If your program exceeds this, consider optimizing on-chain components of your Leo code.
385
474
386
-
As with the above restructions. these limits can only be increased via the governance process.
475
+
As with the above restrictions, these limits can only be increased via the governance process.
Copy file name to clipboardExpand all lines: documentation/language/08_cheatsheet.md
+3-1Lines changed: 3 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -156,6 +156,8 @@ let sender_address: address = msg.sender;
156
156
let object_value: u64 = msg.object;
157
157
```
158
158
159
+
An struct `ExternalStruct` defined in program `external_program.aleo` can be referred outside the program using the syntax `external_program.aleo/ExternalStruct`.
let ecdsa_digest: bool = ECDSA::verify_digest(sig, addr, digest); // Verify an ECDSA signature against an ECDSA public key and a prehashed message
484
486
let ecdsa_digest_eth: bool = ECDSA::verify_digest_eth(sig, eth_addr, digest); Verify an ECDSA signature against an Ethereum address and a prehashed message
Copy file name to clipboardExpand all lines: documentation/language/programs_in_practice/limitations.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,7 @@ Some other protocol-level limits to be aware of are:
24
24
-**the maximum transaction size is 128 KB.** If your program exceeds this, perhaps by requiring large inputs or producing large outputs, consider optimizing the data types in your Leo code.
25
25
-**the maximum number of micro-credits your transaction can consume for on-chain execution is `100_000_000`.**. If your program exceeds this, consider optimizing on-chain components of your Leo code.
26
26
27
-
As with the above restructions. these limits can only be increased via the governance process.
27
+
As with the above restrictions, these limits can only be increased via the governance process.
0 commit comments