Skip to content

Commit 6389a61

Browse files
committed
chore(docs): Updated docs and CHANGELOG.md
1 parent 4a1503e commit 6389a61

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The minor version will be incremented upon a breaking change and the patch versi
1616
- lang: Replace `solana-program` crate with smaller crates ([#3819](https://github.com/solana-foundation/anchor/pull/3819)).
1717
- cli: Replace `anchor verify` to use `solana-verify` under the hood, adding automatic installation via AVM, local path support, and future-proof argument passing ([#3768](https://github.com/solana-foundation/anchor/pull/3768)).
1818
- cli: Make `anchor deploy` to upload the IDL to the cluster by default unless `--no-idl` is passed ([#3863](https://github.com/solana-foundation/anchor/pull/3863)).
19+
- lang: Disallow duplicate mutable accounts by default. But allows duplicate mutable accounts in instruction contexts using `dup` constraint ([#3899](https://github.com/solana-foundation/anchor/pull/3899)).
1920

2021
### Fixes
2122

docs/content/docs/references/account-constraints.mdx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,33 @@ Examples: [Github](https://github.com/solana-developers/anchor-examples/tree/mai
3838
#[account(mut @ <custom_error>)]
3939
```
4040

41+
### `#[account(dup)]`
42+
43+
Description: By default, Anchor will prevents duplicate mutable accounts to avoid potential security issues and unintended behavior.
44+
The `dup` constraint explicitly allows this for cases where it's intentional and safe.
45+
46+
**Note**: This constraint only applies to mutable accounts (`mut`). Readonly accounts naturally allow duplicates without requiring the `dup` constraint.
47+
48+
```rust title="attribute"
49+
#[account(mut, dup)]
50+
#[account(mut, dup @ <custom_error>)]
51+
```
52+
53+
```rust title="snippet"
54+
#[derive(Accounts)]
55+
pub struct AllowsDuplicateMutable<'info> {
56+
#[account(mut)]
57+
pub account1: Account<'info, Counter>,
58+
// This account can be the same as account1
59+
#[account(mut, dup)]
60+
pub account2: Account<'info, Counter>,
61+
}
62+
63+
pub fn allows_duplicate_mutable(ctx: Context<AllowsDuplicateMutable>) -> Result<()> {
64+
Ok(())
65+
}
66+
```
67+
4168
### `#[account(init)]`
4269

4370
Description: Creates the account via a CPI to the system program and initializes

lang/derive/accounts/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ use syn::parse_macro_input;
9090
/// </tr>
9191
/// <tr>
9292
/// <td>
93+
/// <code>#[account(dup)]</code> <br><br>
94+
/// </td>
95+
/// <td>
96+
/// Allows the same mutable account to be passed multiple times within the same instruction context.<br>
97+
/// By default, Anchor will prevents duplicate mutable accounts to avoid potential security issues and unintended behavior.<br>
98+
/// The <code>dup</code> constraint explicitly allows this for cases where it's intentional and safe.<br>
99+
/// This constraint only applies to mutable accounts (<code>mut</code>). Readonly accounts naturally allow duplicates without requiring the <code>dup</code> constraint.<br>
100+
/// Example:
101+
/// <pre><code>
102+
/// #[account(mut)]
103+
/// pub account1: Account<'info, Counter>,
104+
/// </code></pre>
105+
/// </td>
106+
/// </tr>
107+
/// <tr>
108+
/// <td>
93109
/// <code>#[account(init, payer = &lt;target_account&gt;, space = &lt;num_bytes&gt;)]</code>
94110
/// </td>
95111
/// <td>

0 commit comments

Comments
 (0)