From 6ad68ed368f929d1995a62519bab3393e3660b01 Mon Sep 17 00:00:00 2001 From: Armani Ferrante Date: Fri, 2 Jul 2021 16:53:22 -0700 Subject: [PATCH] lang, ts: Add deprecated state address feature flag (#446) --- examples/lockup/programs/lockup/Cargo.toml | 2 ++ examples/lockup/programs/registry/Cargo.toml | 2 ++ examples/lockup/tests/lockup.js | 3 ++- lang/attribute/state/src/lib.rs | 10 ++++++++-- ts/src/coder/state.ts | 4 +++- ts/src/utils/features.ts | 14 ++++++++++++++ ts/src/utils/index.ts | 1 + 7 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 ts/src/utils/features.ts diff --git a/examples/lockup/programs/lockup/Cargo.toml b/examples/lockup/programs/lockup/Cargo.toml index 713c46c5..5d8c1d8a 100644 --- a/examples/lockup/programs/lockup/Cargo.toml +++ b/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" } diff --git a/examples/lockup/programs/registry/Cargo.toml b/examples/lockup/programs/registry/Cargo.toml index 7fd09ad9..702f7e0f 100644 --- a/examples/lockup/programs/registry/Cargo.toml +++ b/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" } diff --git a/examples/lockup/tests/lockup.js b/examples/lockup/tests/lockup.js index 346f102f..5415820b 100644 --- a/examples/lockup/tests/lockup.js +++ b/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(); diff --git a/lang/attribute/state/src/lib.rs b/lang/attribute/state/src/lib.rs index 825466e9..ec5b87c4 100644 --- a/lang/attribute/state/src/lib.rs +++ b/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! { diff --git a/ts/src/coder/state.ts b/ts/src/coder/state.ts index 293e7e6e..e3f24823 100644 --- a/ts/src/coder/state.ts +++ b/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 { - 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); } diff --git a/ts/src/utils/features.ts b/ts/src/utils/features.ts new file mode 100644 index 00000000..9fd8c8a3 --- /dev/null +++ b/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; +} diff --git a/ts/src/utils/index.ts b/ts/src/utils/index.ts index ae85240f..907a7a7a 100644 --- a/ts/src/utils/index.ts +++ b/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";