Browse Source

lang, ts: Add deprecated state address feature flag (#446)

Armani Ferrante 4 years ago
parent
commit
6ad68ed368

+ 2 - 0
examples/lockup/programs/lockup/Cargo.toml

@@ -11,6 +11,8 @@ name = "lockup"
 [features]
 no-entrypoint = []
 cpi = ["no-entrypoint"]
+anchor-deprecated-state = []
+default = ["anchor-deprecated-state"]
 
 [dependencies]
 anchor-lang = { path = "../../../../lang" }

+ 2 - 0
examples/lockup/programs/registry/Cargo.toml

@@ -11,6 +11,8 @@ name = "registry"
 [features]
 no-entrypoint = []
 cpi = ["no-entrypoint"]
+anchor-deprecated-state = []
+default = ["anchor-deprecated-state"]
 
 [dependencies]
 anchor-lang = { path = "../../../../lang" }

+ 2 - 1
examples/lockup/tests/lockup.js

@@ -2,9 +2,10 @@ const assert = require("assert");
 const anchor = require("@project-serum/anchor");
 const serumCmn = require("@project-serum/common");
 const { TOKEN_PROGRAM_ID } = require("@solana/spl-token");
-
 const utils = require("./utils");
 
+anchor.utils.features.set('anchor-deprecated-state');
+
 describe("Lockup and Registry", () => {
   // Read the provider from the configured environmnet.
   const provider = anchor.Provider.env();

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

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

+ 3 - 1
ts/src/coder/state.ts

@@ -2,6 +2,7 @@ import { Layout } from "buffer-layout";
 import { sha256 } from "js-sha256";
 import { Idl } from "../idl";
 import { IdlCoder } from "./idl";
+import * as features from "../utils/features";
 
 export class StateCoder {
   private layout: Layout;
@@ -32,5 +33,6 @@ export class StateCoder {
 
 // Calculates unique 8 byte discriminator prepended to all anchor state accounts.
 export async function stateDiscriminator(name: string): Promise<Buffer> {
-  return Buffer.from(sha256.digest(`state:${name}`)).slice(0, 8);
+  let ns = features.isSet("anchor-deprecated-state") ? "account" : "state";
+  return Buffer.from(sha256.digest(`${ns}:${name}`)).slice(0, 8);
 }

+ 14 - 0
ts/src/utils/features.ts

@@ -0,0 +1,14 @@
+const _AVAILABLE_FEATURES = new Set(["anchor-deprecated-state"]);
+
+const _FEATURES = new Map();
+
+export function set(key: string) {
+  if (!_AVAILABLE_FEATURES.has(key)) {
+    throw new Error("Invalid feature");
+  }
+  _FEATURES.set(key, true);
+}
+
+export function isSet(key: string): boolean {
+  return _FEATURES.get(key) !== undefined;
+}

+ 1 - 0
ts/src/utils/index.ts

@@ -3,3 +3,4 @@ export * as rpc from "./rpc";
 export * as publicKey from "./pubkey";
 export * as bytes from "./bytes";
 export * as token from "./token";
+export * as features from "./features";