Skip to content

Commit

Permalink
lang: Namespaceable account discriminators (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
armaniferrante authored Mar 26, 2021
1 parent 429da09 commit 07a6523
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ incremented for features.
* cli: Specify test files to run ([#118](https://github.com/project-serum/anchor/pull/118)).
* lang: Allow overriding the `#[state]` account's size ([#121](https://github.com/project-serum/anchor/pull/121)).
* lang, client, ts: Add event emission and subscriptions ([#89](https://github.com/project-serum/anchor/pull/89)).
* lang/account: Allow namespacing account discriminators ([#128](https://github.com/project-serum/anchor/pull/128)).

## Breaking Changes

* client: Replace url str with `Cluster` struct when constructing clients ([#89](https://github.com/project-serum/anchor/pull/89)).
* lang: Changes the account discriminator of `IdlAccount` to be namespaced by `"internal"` ([#128](https://github.com/project-serum/anchor/pull/128)).

## [0.3.0] - 2021-03-12

Expand Down
13 changes: 11 additions & 2 deletions lang/attribute/account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,24 @@ use syn::parse_macro_input;
/// and the account deserialization will exit with an error.
#[proc_macro_attribute]
pub fn account(
_args: proc_macro::TokenStream,
args: proc_macro::TokenStream,
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let namespace = args.to_string().replace("\"", "");

let account_strct = parse_macro_input!(input as syn::ItemStruct);
let account_name = &account_strct.ident;

let discriminator: proc_macro2::TokenStream = {
// Namespace the discriminator to prevent collisions.
let discriminator_preimage = format!("account:{}", account_name.to_string());
let discriminator_preimage = {
if namespace.is_empty() {
format!("account:{}", account_name.to_string())
} else {
format!("{}:{}", namespace, account_name.to_string())
}
};

let mut discriminator = [0u8; 8];
discriminator.copy_from_slice(
&anchor_syn::hash::hash(discriminator_preimage.as_bytes()).to_bytes()[..8],
Expand Down
2 changes: 1 addition & 1 deletion lang/src/idl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub struct IdlSetBuffer<'info> {
//
// Note: we use the same account for the "write buffer", similar to the
// bpf upgradeable loader's mechanism.
#[account]
#[account("internal")]
#[derive(Debug)]
pub struct IdlAccount {
// Address that can modify the IDL.
Expand Down

0 comments on commit 07a6523

Please sign in to comment.