Sfoglia il codice sorgente

lang, ts: Namespace state discriminator (#320)

Armani Ferrante 4 anni fa
parent
commit
e1229362bc
4 ha cambiato i file con 23 aggiunte e 13 eliminazioni
  1. 1 0
      CHANGELOG.md
  2. 19 9
      lang/attribute/account/src/lib.rs
  3. 2 2
      lang/attribute/state/src/lib.rs
  4. 1 2
      ts/src/coder.ts

+ 1 - 0
CHANGELOG.md

@@ -20,6 +20,7 @@ incremented for features.
 ## Breaking Changes
 
 * lang: `#[account(associated)]` now requires `init` to be provided to create an associated account. If not provided, then the address will be assumed to exist, and a constraint will be added to ensure its correctness ([#318](https://github.com/project-serum/anchor/pull/318)).
+* lang, ts: Change account discriminator pre-image of the `#[state]` account discriminator to be namespaced by "state:". This change should only be noticed by library maintainers ([#320](https://github.com/project-serum/anchor/pull/320)).
 * lang, ts: Change domain delimiters for the pre-image of the instruciton sighash to be a single colon `:` to be consistent with accounts. This change should only be noticed by library maintainers.
 
 ## [0.6.0] - 2021-05-23

+ 19 - 9
lang/attribute/account/src/lib.rs

@@ -57,14 +57,24 @@ pub fn account(
     args: proc_macro::TokenStream,
     input: proc_macro::TokenStream,
 ) -> proc_macro::TokenStream {
-    let namespace = args.to_string().replace("\"", "");
-    let is_zero_copy = match args.into_iter().next() {
-        None => false,
-        Some(tt) => match tt {
-            proc_macro::TokenTree::Literal(_) => false,
-            _ => namespace == "zero_copy",
-        },
-    };
+    let mut namespace = "".to_string();
+    let mut is_zero_copy = false;
+    if args.to_string().split(",").collect::<Vec<_>>().len() > 2 {
+        panic!("Only two args are allowed to the account attribute.")
+    }
+    for arg in args.to_string().split(",") {
+        let ns = arg
+            .to_string()
+            .replace("\"", "")
+            .chars()
+            .filter(|c| !c.is_whitespace())
+            .collect();
+        if ns == "zero_copy" {
+            is_zero_copy = true;
+        } else {
+            namespace = ns;
+        }
+    }
 
     let account_strct = parse_macro_input!(input as syn::ItemStruct);
     let account_name = &account_strct.ident;
@@ -73,7 +83,7 @@ pub fn account(
         // Namespace the discriminator to prevent collisions.
         let discriminator_preimage = {
             // For now, zero copy accounts can't be namespaced.
-            if is_zero_copy || namespace.is_empty() {
+            if namespace.is_empty() {
                 format!("account:{}", account_name.to_string())
             } else {
                 format!("{}:{}", namespace, account_name.to_string())

+ 2 - 2
lang/attribute/state/src/lib.rs

@@ -71,8 +71,8 @@ pub fn state(
     };
 
     let attribute = match is_zero_copy {
-        false => quote! {#[account]},
-        true => quote! {#[account(zero_copy)]},
+        false => quote! {#[account("state")]},
+        true => quote! {#[account("state", zero_copy)]},
     };
 
     proc_macro::TokenStream::from(quote! {

+ 1 - 2
ts/src/coder.ts

@@ -340,7 +340,6 @@ class IdlCoder {
       case "publicKey": {
         return borsh.publicKey(fieldName);
       }
-      // TODO: all the other types that need to be exported by the borsh package.
       default: {
         // @ts-ignore
         if (field.type.vec) {
@@ -452,7 +451,7 @@ export async function accountDiscriminator(name: string): Promise<Buffer> {
 // Calculates unique 8 byte discriminator prepended to all anchor state accounts.
 export async function stateDiscriminator(name: string): Promise<Buffer> {
   // @ts-ignore
-  return Buffer.from(sha256.digest(`account:${name}`)).slice(0, 8);
+  return Buffer.from(sha256.digest(`state:${name}`)).slice(0, 8);
 }
 
 export function eventDiscriminator(name: string): Buffer {