Skip to content

Commit 93dd866

Browse files
committed
docs: fix counter tutorial ownership semantics and add TXE tests
The counter tutorial framed the example as "your own private counter," but `increment(owner: AztecAddress)` let any caller mutate any owner's counter. Drop the `owner` parameter and read `self.msg_sender()` so the caller can only increment the counter mapped to their own address. Also add a sibling `counter_contract_test` package (mirroring `logging_example_test`) with TXE tests covering `initialize` and `increment`. This catches regressions in the example contract via `aztec test` when wired up.
1 parent b72976e commit 93dd866

5 files changed

Lines changed: 46 additions & 2 deletions

File tree

docs/Nargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
members = [
33
"examples/circuits/hello_circuit",
44
"examples/contracts/counter_contract",
5+
"examples/contracts/counter_contract_test",
56
"examples/contracts/bob_token_contract",
67
"examples/contracts/nft",
78
"examples/contracts/nft_bridge",

docs/docs-developers/docs/tutorials/contract_tutorials/counter_contract.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Now let's implement an `increment` function to increase the counter.
118118

119119
#include_code increment /docs/examples/contracts/counter_contract/src/main.nr rust
120120

121-
The `increment` function works similarly to the `initialize` function. It logs a debug message, then adds 1 to the owner's counter and delivers the note onchain.
121+
The `increment` function uses `self.msg_sender()` so each caller can only increment the counter mapped to their own address. It logs a debug message, then adds 1 to that counter and delivers the note onchain.
122122

123123
## Getting a counter
124124

docs/examples/contracts/counter_contract/src/main.nr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ pub contract Counter {
3535

3636
// docs:start:increment
3737
#[external("private")]
38-
fn increment(owner: AztecAddress) {
38+
fn increment() {
39+
let owner = self.msg_sender();
3940
debug_log_format("Incrementing counter for owner {0}", [owner.to_field()]);
4041
self.storage.counters.at(owner).add(1).deliver(MessageDelivery.ONCHAIN_CONSTRAINED);
4142
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "counter_contract_test"
3+
authors = [""]
4+
compiler_version = ">=0.25.0"
5+
type = "lib"
6+
7+
[dependencies]
8+
aztec = { path = "../../../../noir-projects/aztec-nr/aztec" }
9+
balance_set = { path = "../../../../noir-projects/aztec-nr/balance-set" }
10+
counter_contract = { path = "../counter_contract" }
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use counter_contract::Counter;
2+
use aztec::{protocol::address::AztecAddress, test::helpers::test_environment::TestEnvironment};
3+
4+
pub unconstrained fn setup(headstart: u128) -> (TestEnvironment, AztecAddress, AztecAddress) {
5+
let mut env = TestEnvironment::new();
6+
let owner = env.create_light_account();
7+
8+
let initializer = Counter::interface().initialize(headstart, owner);
9+
let contract_address =
10+
env.deploy("@counter_contract/Counter").with_private_initializer(owner, initializer);
11+
(env, contract_address, owner)
12+
}
13+
14+
#[test]
15+
unconstrained fn test_initialize_sets_headstart() {
16+
let headstart = 5;
17+
let (env, contract_address, owner) = setup(headstart);
18+
19+
let value = env.execute_utility(Counter::at(contract_address).get_counter(owner));
20+
assert(value == headstart, "initial counter should equal headstart");
21+
}
22+
23+
#[test]
24+
unconstrained fn test_increment_increases_caller_counter() {
25+
let headstart = 5;
26+
let (mut env, contract_address, owner) = setup(headstart);
27+
28+
env.call_private(owner, Counter::at(contract_address).increment());
29+
30+
let value = env.execute_utility(Counter::at(contract_address).get_counter(owner));
31+
assert(value == headstart + 1, "counter should be incremented by 1");
32+
}

0 commit comments

Comments
 (0)