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

This commit is contained in:
Armani Ferrante 2021-07-02 16:53:22 -07:00 committed by GitHub
parent 61625e73db
commit 6ad68ed368
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 32 additions and 4 deletions

View File

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

View File

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

View File

@ -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();

View File

@ -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! {

View File

@ -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
ts/src/utils/features.ts Normal file
View File

@ -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;
}

View File

@ -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";