The first thing that surprises people coming to Solana from web development is that a program has no storage of its own. All state lives in accounts. Every account has a fixed size in bytes, somebody pays rent for those bytes, and the program must decide who is allowed to change them. "Add a row to a table" becomes "derive an address, allocate exactly enough bytes, pay for them, and check the signer".
A counter and a to-do list are the smallest programs that force you through all of that, so that is what I built. counter-todo is two small Anchor programs in one crate.
Before anything else, the warning from the README: not audited, devnet / localnet only. This is a learning project. Do not deploy it to mainnet or use it with real funds.
What it does
Counter. One PDA per user, derived from ["counter", authority], with increment and decrement using checked arithmetic. An overflow or underflow is an error, not a silent wrap-around.
To-do list. A list header PDA per user (["todo_list", owner]) and one PDA per item (["todo", owner, id.to_le_bytes()]). Item titles are variable length. Renaming an item resizes its account with realloc, and deleting it closes the account and refunds the rent.
The full instruction set:
| instruction | accounts | notes |
|---|---|---|
init_counter(start) |
authority (signer, payer), counter PDA | |
increment(by) / decrement(by)
|
authority (signer), counter |
has_one = authority, checked_add / checked_sub
|
init_list() |
owner (signer, payer), list PDA | |
add_item(title) |
owner, list, item PDA (next_id) |
account sized to the title |
rename_item(id, title) |
owner, item |
realloc to the new size; owner pays or is refunded |
toggle_item(id) |
owner, item | |
delete_item(id) |
owner, list, item | close = owner |
How to use it
You need Anchor 1.2 and the Agave 4.x CLI on your PATH. Clone the repository, build, and run the tests:
git clone https://github.com/AbdeldjalilChougui/counter-todo.git
cd counter-todo
anchor build # builds target/deploy/counter_todo.so + IDL
cargo test # LiteSVM in-process tests (no validator)
Two practical notes from the README:
- Anchor 1.2 defaults to SBF platform-tools v1.57. If you only have another version installed, pass it explicitly, for example
anchor build --tools-version v1.58. - The tests load
target/deploy/counter_todo.so, so runanchor buildbeforecargo test.
The tests use LiteSVM 0.16 (Agave 4.2 runtime, which supports the SBPF v3 binaries Anchor 1.2 emits) and need Rust 1.93 or later, so rust-toolchain.toml pins 1.96.0.
What it teaches: account sizing
This is the part of the project I would point a beginner to first.
Every Anchor account starts with an 8-byte discriminator, the first 8 bytes of sha256("account:<Name>"). After that comes the Borsh-serialized struct. Borsh has no padding, so sizes simply add up:
| type | bytes |
|---|---|
bool, u8
|
1 |
u32 |
4 |
u64, i64
|
8 |
Pubkey |
32 |
String |
4 (u32 length prefix) + UTF-8 bytes |
Vec<T> |
4 + n × size(T) |
Option<T> |
1 + size(T) |
Fixed-size structs use #[derive(InitSpace)], and you allocate 8 + Counter::INIT_SPACE:
Counter = 8 (disc) + 32 (authority) + 8 (count) + 1 (bump) = 49 bytes
TodoList = 8 (disc) + 32 (owner) + 8 (next_id) + 4 (open_items) + 1 (bump) = 53 bytes
TodoItem holds a String, so its size depends on the title. Rather than reserving the maximum (200 bytes) for every item, the program computes the exact size:
pub const fn space(title_len: usize) -> usize {
8 + 32 + 8 + 1 + 1 + 4 + title_len
}
"buy milk" therefore takes 62 bytes instead of 254. Since rent is paid per byte, exact sizing is not a micro-optimisation; it is the difference between paying for what you store and paying for the worst case on every item.
What it teaches: realloc
Exact sizing creates a new problem: what happens when the title changes? The realloc = TodoItem::space(new_len) constraint handles both directions:
- it grows the account and transfers the extra rent-exempt lamports from
realloc::payer; - it shrinks the account and sends the surplus lamports back to the payer.
realloc::zero = false skips zeroing new memory. That is safe here because Anchor re-serializes the whole struct on exit. Set it to true if you shrink and grow in the same transaction and read raw bytes.
Two more rules apply. A single realloc can grow an account by at most 10 KiB (MAX_PERMITTED_DATA_INCREASE), and the title is capped at MAX_TITLE_LEN = 200 so an item can never grow without bound. Rent is about 6.96 lamports per byte-year × 2 years, plus a 128-byte account overhead. The tests check that lamports == minimum_balance_for_rent_exemption(len) after each resize.
What I learned
PDAs are the data model. There is no table of items. The seeds are the schema: ["todo", owner, id] is at once the item's address, its primary key and its ownership proof. A client finds items by deriving PDAs from 0..next_id and skipping closed ones.
Seeds do access control for you. Because the owner's key is part of the item's seeds, a different signer derives a different address. Trying to touch someone else's item fails at seed derivation, before any of my own code runs. On the counter, the same job is done by has_one = authority.
Closing is part of the lifecycle. close = owner on delete_item returns the rent to the owner. Creating accounts and never closing them leaves lamports locked for nothing.
The failure tests are the real tests. The happy path is short. Most of the suite in programs/counter-todo/tests/ checks what must not work:
- overflow (
u64::MAX) and underflow are rejected and the state is unchanged -
wrong signer: another wallet signing for someone else's counter fails
has_one, and on items fails seed derivation -
authority must sign: stripping the signer flag gives
AccountNotSigner -
wrong owner: passing a system-owned account as a
CountergivesAccountOwnedByWrongProgram - title validation: empty, too long, and an exact-max title
The happy path covers counter increment and decrement, and the to-do lifecycle with grow and shrink realloc, toggle and close.
LiteSVM makes this fast. The tests run in process with no validator, so the loop of edit, build and test stays short enough to actually try the wrong-signer and wrong-owner cases instead of assuming them.
Things to know
- Not audited. Devnet / localnet only. Do not deploy to mainnet or use with real funds.
- It is learning code: there is no pagination or indexing of items. Clients derive item PDAs from
0..next_idand skip closed ones. - Ids are never reused after delete.
- Titles are capped at 200 bytes.
- The build depends on the toolchain versions above (Anchor 1.2, platform-tools, Rust 1.93 or later for the tests).
Why I built it
Tutorials often stop at a counter with one hard-coded account size. That skips the questions you hit in the first real program: how many bytes is this struct, what if a field has variable length, who pays when it grows, and who gets the lamports back when it shrinks or closes. I wanted one small codebase that answers those with exact numbers and with tests that prove each rejection, so the README can say "62 bytes instead of 254" and the tests can back it up.
Get the code
The programs, the sizing notes and the LiteSVM tests are on GitHub: https://github.com/AbdeldjalilChougui/counter-todo. It is MIT licensed. Issues and pull requests are welcome.
Originally published at abdeldjalilchougui.biz.
Top comments (0)