update
This commit is contained in:
parent
b45bb8a914
commit
71297bdf48
|
@ -70,6 +70,7 @@ dependencies = [
|
||||||
name = "anchor-attribute-account"
|
name = "anchor-attribute-account"
|
||||||
version = "0.21.0"
|
version = "0.21.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anchor-common",
|
||||||
"anchor-syn",
|
"anchor-syn",
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"bs58 0.4.0",
|
"bs58 0.4.0",
|
||||||
|
@ -102,6 +103,7 @@ dependencies = [
|
||||||
name = "anchor-attribute-event"
|
name = "anchor-attribute-event"
|
||||||
version = "0.21.0"
|
version = "0.21.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anchor-common",
|
||||||
"anchor-syn",
|
"anchor-syn",
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"proc-macro2 1.0.32",
|
"proc-macro2 1.0.32",
|
||||||
|
@ -190,6 +192,14 @@ dependencies = [
|
||||||
"url",
|
"url",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anchor-common"
|
||||||
|
version = "0.21.0"
|
||||||
|
dependencies = [
|
||||||
|
"arrayref",
|
||||||
|
"solana-program",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "anchor-derive-accounts"
|
name = "anchor-derive-accounts"
|
||||||
version = "0.21.0"
|
version = "0.21.0"
|
||||||
|
@ -213,6 +223,7 @@ dependencies = [
|
||||||
"anchor-attribute-interface",
|
"anchor-attribute-interface",
|
||||||
"anchor-attribute-program",
|
"anchor-attribute-program",
|
||||||
"anchor-attribute-state",
|
"anchor-attribute-state",
|
||||||
|
"anchor-common",
|
||||||
"anchor-derive-accounts",
|
"anchor-derive-accounts",
|
||||||
"arrayref",
|
"arrayref",
|
||||||
"base64 0.13.0",
|
"base64 0.13.0",
|
||||||
|
@ -239,6 +250,7 @@ name = "anchor-syn"
|
||||||
version = "0.21.0"
|
version = "0.21.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
|
"arrayref",
|
||||||
"bs58 0.3.1",
|
"bs58 0.3.1",
|
||||||
"heck 0.3.3",
|
"heck 0.3.3",
|
||||||
"proc-macro2 1.0.32",
|
"proc-macro2 1.0.32",
|
||||||
|
@ -247,6 +259,7 @@ dependencies = [
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"sha2",
|
"sha2",
|
||||||
|
"solana-program",
|
||||||
"syn 1.0.81",
|
"syn 1.0.81",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
]
|
]
|
||||||
|
|
|
@ -11,6 +11,7 @@ members = [
|
||||||
"client",
|
"client",
|
||||||
"lang",
|
"lang",
|
||||||
"lang/attribute/*",
|
"lang/attribute/*",
|
||||||
|
"lang/common",
|
||||||
"lang/derive/*",
|
"lang/derive/*",
|
||||||
"lang/syn",
|
"lang/syn",
|
||||||
"spl",
|
"spl",
|
||||||
|
|
|
@ -34,6 +34,7 @@ anchor-attribute-state = { path = "./attribute/state", version = "0.21.0" }
|
||||||
anchor-attribute-interface = { path = "./attribute/interface", version = "0.21.0" }
|
anchor-attribute-interface = { path = "./attribute/interface", version = "0.21.0" }
|
||||||
anchor-attribute-event = { path = "./attribute/event", version = "0.21.0" }
|
anchor-attribute-event = { path = "./attribute/event", version = "0.21.0" }
|
||||||
anchor-derive-accounts = { path = "./derive/accounts", version = "0.21.0" }
|
anchor-derive-accounts = { path = "./derive/accounts", version = "0.21.0" }
|
||||||
|
anchor-common = { path = "./common", version = "0.21.0" }
|
||||||
arrayref = "0.3.6"
|
arrayref = "0.3.6"
|
||||||
base64 = "0.13.0"
|
base64 = "0.13.0"
|
||||||
borsh = "0.9"
|
borsh = "0.9"
|
||||||
|
|
|
@ -19,5 +19,6 @@ quote = "1.0"
|
||||||
syn = { version = "1.0.60", features = ["full"] }
|
syn = { version = "1.0.60", features = ["full"] }
|
||||||
anyhow = "1.0.32"
|
anyhow = "1.0.32"
|
||||||
anchor-syn = { path = "../../syn", version = "0.21.0", features = ["hash"] }
|
anchor-syn = { path = "../../syn", version = "0.21.0", features = ["hash"] }
|
||||||
|
anchor-common = { path = "../../common", version = "0.21.0" }
|
||||||
rustversion = "1.0.3"
|
rustversion = "1.0.3"
|
||||||
bs58 = "0.4.0"
|
bs58 = "0.4.0"
|
||||||
|
|
|
@ -104,41 +104,19 @@ pub fn account(
|
||||||
};
|
};
|
||||||
|
|
||||||
let discriminator: proc_macro2::TokenStream = {
|
let discriminator: proc_macro2::TokenStream = {
|
||||||
// Namespace the discriminator to prevent collisions.
|
let discriminator = anchor_common::header::create_discriminator(
|
||||||
let discriminator_preimage = {
|
&account_name.to_string(),
|
||||||
// For now, zero copy accounts can't be namespaced.
|
|
||||||
if namespace.is_empty() {
|
if namespace.is_empty() {
|
||||||
format!("account:{}", account_name)
|
None
|
||||||
} else {
|
} else {
|
||||||
format!("{}:{}", namespace, account_name)
|
Some(&namespace)
|
||||||
}
|
},
|
||||||
};
|
);
|
||||||
|
format!("{:?}", discriminator).parse().unwrap()
|
||||||
if cfg!(feature = "deprecated-layout") {
|
|
||||||
let mut discriminator = [0u8; 8];
|
|
||||||
discriminator.copy_from_slice(
|
|
||||||
&anchor_syn::hash::hash(discriminator_preimage.as_bytes()).to_bytes()[..8],
|
|
||||||
);
|
|
||||||
format!("{:?}", discriminator).parse().unwrap()
|
|
||||||
} else {
|
|
||||||
let mut discriminator = [0u8; 4];
|
|
||||||
discriminator.copy_from_slice(
|
|
||||||
&anchor_syn::hash::hash(discriminator_preimage.as_bytes()).to_bytes()[..4],
|
|
||||||
);
|
|
||||||
format!("{:?}", discriminator).parse().unwrap()
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let disc_bytes = {
|
let disc_bytes = quote! {
|
||||||
if cfg!(feature = "deprecated-layout") {
|
let given_disc = anchor_lang::accounts::header::read_discriminator(&buf);
|
||||||
quote! {
|
|
||||||
let given_disc = &buf[..8];
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
quote! {
|
|
||||||
let given_disc = &buf[2..6];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let disc_fn = {
|
let disc_fn = {
|
||||||
|
|
|
@ -19,3 +19,4 @@ quote = "1.0"
|
||||||
syn = { version = "1.0.60", features = ["full"] }
|
syn = { version = "1.0.60", features = ["full"] }
|
||||||
anyhow = "1.0.32"
|
anyhow = "1.0.32"
|
||||||
anchor-syn = { path = "../../syn", version = "0.21.0", features = ["hash"] }
|
anchor-syn = { path = "../../syn", version = "0.21.0", features = ["hash"] }
|
||||||
|
anchor-common = { path = "../../common", version = "0.21.0" }
|
||||||
|
|
|
@ -17,24 +17,8 @@ pub fn event(
|
||||||
let event_name = &event_strct.ident;
|
let event_name = &event_strct.ident;
|
||||||
|
|
||||||
let discriminator: proc_macro2::TokenStream = {
|
let discriminator: proc_macro2::TokenStream = {
|
||||||
let discriminator_preimage = format!("event:{}", event_name);
|
let discriminator =
|
||||||
|
anchor_common::header::create_discriminator(&event_name.to_string(), Some("event"));
|
||||||
#[cfg(feature = "deprecated-layout")]
|
|
||||||
let discriminator = {
|
|
||||||
let mut discriminator = [0u8; 8];
|
|
||||||
discriminator.copy_from_slice(
|
|
||||||
&anchor_syn::hash::hash(discriminator_preimage.as_bytes()).to_bytes()[..8],
|
|
||||||
);
|
|
||||||
discriminator
|
|
||||||
};
|
|
||||||
#[cfg(not(feature = "deprecated-layout"))]
|
|
||||||
let discriminator = {
|
|
||||||
let mut discriminator = [0u8; 4];
|
|
||||||
discriminator.copy_from_slice(
|
|
||||||
&anchor_syn::hash::hash(discriminator_preimage.as_bytes()).to_bytes()[..4],
|
|
||||||
);
|
|
||||||
discriminator
|
|
||||||
};
|
|
||||||
format!("{:?}", discriminator).parse().unwrap()
|
format!("{:?}", discriminator).parse().unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
[package]
|
||||||
|
name = "anchor-common"
|
||||||
|
version = "0.21.0"
|
||||||
|
authors = ["Serum Foundation <foundation@projectserum.com>"]
|
||||||
|
repository = "https://github.com/project-serum/anchor"
|
||||||
|
license = "Apache-2.0"
|
||||||
|
description = "Common utilities for internal anchor lang crates"
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = []
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
arrayref = "0.3.6"
|
||||||
|
solana-program = "1.8.5"
|
|
@ -1,4 +1,6 @@
|
||||||
use arrayref::array_ref;
|
use arrayref::array_ref;
|
||||||
|
use solana_program::hash;
|
||||||
|
use std::io::{Cursor, Write};
|
||||||
|
|
||||||
#[cfg(feature = "deprecated-layout")]
|
#[cfg(feature = "deprecated-layout")]
|
||||||
pub fn read_discriminator(data: &[u8]) -> &[u8; 8] {
|
pub fn read_discriminator(data: &[u8]) -> &[u8; 8] {
|
||||||
|
@ -14,9 +16,7 @@ pub fn read_discriminator(data: &[u8]) -> &[u8; 4] {
|
||||||
pub fn create_discriminator(account_name: &str, namespace: Option<&str>) -> [u8; 8] {
|
pub fn create_discriminator(account_name: &str, namespace: Option<&str>) -> [u8; 8] {
|
||||||
let discriminator_preimage = format!("{}:{}", namespace.unwrap_or("account"), account_name);
|
let discriminator_preimage = format!("{}:{}", namespace.unwrap_or("account"), account_name);
|
||||||
let mut discriminator = [0u8; 8];
|
let mut discriminator = [0u8; 8];
|
||||||
discriminator.copy_from_slice(
|
discriminator.copy_from_slice(&hash::hash(discriminator_preimage.as_bytes()).to_bytes()[..8]);
|
||||||
&crate::solana_program::hash::hash(discriminator_preimage.as_bytes()).to_bytes()[..8],
|
|
||||||
);
|
|
||||||
discriminator
|
discriminator
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,9 +24,7 @@ pub fn create_discriminator(account_name: &str, namespace: Option<&str>) -> [u8;
|
||||||
pub fn create_discriminator(account_name: &str, namespace: Option<&str>) -> [u8; 4] {
|
pub fn create_discriminator(account_name: &str, namespace: Option<&str>) -> [u8; 4] {
|
||||||
let discriminator_preimage = format!("{}:{}", namespace.unwrap_or("account"), account_name);
|
let discriminator_preimage = format!("{}:{}", namespace.unwrap_or("account"), account_name);
|
||||||
let mut discriminator = [0u8; 4];
|
let mut discriminator = [0u8; 4];
|
||||||
discriminator.copy_from_slice(
|
discriminator.copy_from_slice(&hash::hash(discriminator_preimage.as_bytes()).to_bytes()[..4]);
|
||||||
&crate::solana_program::hash::hash(discriminator_preimage.as_bytes()).to_bytes()[..4],
|
|
||||||
);
|
|
||||||
discriminator
|
discriminator
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,3 +36,17 @@ pub fn read_data(account_data: &[u8]) -> &[u8] {
|
||||||
pub fn read_data_mut(account_data: &mut [u8]) -> &mut [u8] {
|
pub fn read_data_mut(account_data: &mut [u8]) -> &mut [u8] {
|
||||||
&mut account_data[8..]
|
&mut account_data[8..]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn write_discriminator(account_data: &mut [u8], discriminator: &[u8]) {
|
||||||
|
#[cfg(feature = "deprecated_layout")]
|
||||||
|
{
|
||||||
|
let mut cursor = Cursor::new(account_dst);
|
||||||
|
cursor.write_all(discriminator).unwrap();
|
||||||
|
}
|
||||||
|
#[cfg(not(feature = "deprecated_layout"))]
|
||||||
|
{
|
||||||
|
let dst: &mut [u8] = &mut account_data[2..];
|
||||||
|
let mut cursor = Cursor::new(dst);
|
||||||
|
cursor.write_all(discriminator).unwrap();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
pub mod header;
|
|
@ -10,7 +10,6 @@ pub mod cpi_account;
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
pub mod cpi_state;
|
pub mod cpi_state;
|
||||||
pub mod header;
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
pub mod loader;
|
pub mod loader;
|
||||||
|
@ -25,3 +24,5 @@ pub mod state;
|
||||||
pub mod system_account;
|
pub mod system_account;
|
||||||
pub mod sysvar;
|
pub mod sysvar;
|
||||||
pub mod unchecked_account;
|
pub mod unchecked_account;
|
||||||
|
|
||||||
|
pub use anchor_common::header;
|
||||||
|
|
|
@ -16,6 +16,7 @@ anchor-debug = []
|
||||||
seeds = []
|
seeds = []
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
arrayref = "0.3.6"
|
||||||
proc-macro2 = "1.0"
|
proc-macro2 = "1.0"
|
||||||
proc-macro2-diagnostics = "0.9"
|
proc-macro2-diagnostics = "0.9"
|
||||||
quote = "1.0"
|
quote = "1.0"
|
||||||
|
@ -25,5 +26,6 @@ heck = "0.3.1"
|
||||||
serde = { version = "1.0.122", features = ["derive"] }
|
serde = { version = "1.0.122", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
sha2 = "0.9.2"
|
sha2 = "0.9.2"
|
||||||
|
solana-program = "1.8.5"
|
||||||
thiserror = "1.0"
|
thiserror = "1.0"
|
||||||
bs58 = "0.3.1"
|
bs58 = "0.3.1"
|
||||||
|
|
|
@ -147,23 +147,10 @@ pub fn generate_constraint_zeroed(f: &Field, _c: &ConstraintZeroed) -> proc_macr
|
||||||
let ty_decl = f.ty_decl();
|
let ty_decl = f.ty_decl();
|
||||||
let account_ty = f.account_ty();
|
let account_ty = f.account_ty();
|
||||||
let from_account_info = f.from_account_info(None);
|
let from_account_info = f.from_account_info(None);
|
||||||
let header_write = {
|
let header_write = quote! {
|
||||||
if cfg!(feature = "deprecated-layout") {
|
{
|
||||||
quote! {
|
use anchor_lang::Discriminator;
|
||||||
use std::io::{Write, Cursor};
|
anchor_lang::accounts::header::write_discriminator(&mut __data, &#account_ty::discriminator());
|
||||||
use anchor_lang::Discriminator;
|
|
||||||
let __dst: &mut [u8] = &mut __data;
|
|
||||||
let mut __cursor = Cursor::new(__dst);
|
|
||||||
Write::write_all(&mut __cursor, &#account_ty::discriminator()).unwrap();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
quote! {
|
|
||||||
use std::io::{Write, Cursor};
|
|
||||||
use anchor_lang::Discriminator;
|
|
||||||
let __dst: &mut [u8] = &mut __data[2..];
|
|
||||||
let mut __cursor = Cursor::new(__dst);
|
|
||||||
Write::write_all(&mut __cursor, &#account_ty::discriminator()).unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// Check the *entire* account header is zero.
|
// Check the *entire* account header is zero.
|
||||||
|
@ -541,30 +528,14 @@ fn generate_constraint_init(f: &Field, c: &ConstraintInitGroup) -> proc_macro2::
|
||||||
| Ty::Loader(_)
|
| Ty::Loader(_)
|
||||||
| Ty::AccountLoader(_) => {
|
| Ty::AccountLoader(_) => {
|
||||||
let account_ty = f.account_ty();
|
let account_ty = f.account_ty();
|
||||||
if cfg!(feature = "deprecated-layout") {
|
quote! {
|
||||||
quote! {
|
{
|
||||||
{
|
|
||||||
use std::io::{Write, Cursor};
|
|
||||||
use anchor_lang::Discriminator;
|
|
||||||
|
|
||||||
let mut __data = actual_field.try_borrow_mut_data()?;
|
let mut __data = actual_field.try_borrow_mut_data()?;
|
||||||
let __dst: &mut [u8] = &mut __data;
|
anchor_lang::accounts::header::write_discriminator(
|
||||||
let mut __cursor = Cursor::new(__dst);
|
&mut __data,
|
||||||
Write::write_all(&mut __cursor, &#account_ty::discriminator()).unwrap();
|
&#account_ty::discriminator(),
|
||||||
}
|
);
|
||||||
}
|
|
||||||
} else {
|
|
||||||
quote! {
|
|
||||||
{
|
|
||||||
use std::io::{Write, Seek, SeekFrom, Cursor};
|
|
||||||
use anchor_lang::Discriminator;
|
|
||||||
|
|
||||||
let mut __data = actual_field.try_borrow_mut_data()?;
|
|
||||||
let __dst: &mut [u8] = &mut __data;
|
|
||||||
let mut __cursor = Cursor::new(__dst);
|
|
||||||
Seek::seek(&mut __cursor, SeekFrom::Start(2)).unwrap();
|
|
||||||
Write::write_all(&mut __cursor, &#account_ty::discriminator()).unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -202,32 +202,13 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
|
||||||
let ix_name: proc_macro2::TokenStream =
|
let ix_name: proc_macro2::TokenStream =
|
||||||
generate_ctor_variant_name().parse().unwrap();
|
generate_ctor_variant_name().parse().unwrap();
|
||||||
let ix_name_log = format!("Instruction: {}", ix_name);
|
let ix_name_log = format!("Instruction: {}", ix_name);
|
||||||
let header_write = {
|
let header_write = quote! {
|
||||||
if cfg!(feature = "deprecated-layout") {
|
{
|
||||||
quote! {
|
let mut __data = ctor_accounts.to.try_borrow_mut_data()?;
|
||||||
{
|
anchor_lang::accounts::header::write_discriminator(
|
||||||
use std::io::{Write, Cursor};
|
&mut __data,
|
||||||
use anchor_lang::Discriminator;
|
&#name::discriminator(),
|
||||||
|
);
|
||||||
let mut __data = ctor_accounts.to.try_borrow_mut_data()?;
|
|
||||||
let __dst: &mut [u8] = &mut __data;
|
|
||||||
let mut __cursor = Cursor::new(__dst);
|
|
||||||
Write::write_all(&mut __cursor, &#name::discriminator()).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
quote! {
|
|
||||||
{
|
|
||||||
use std::io::{Write, Cursor, SeekFrom, Seek};
|
|
||||||
use anchor_lang::Discriminator;
|
|
||||||
|
|
||||||
let mut __data = ctor_accounts.to.try_borrow_mut_data()?;
|
|
||||||
let __dst: &mut [u8] = &mut __data;
|
|
||||||
let mut __cursor = Cursor::new(__dst);
|
|
||||||
Seek::seek(&mut __cursor, SeekFrom::Start(2)).unwrap();
|
|
||||||
Write::write_all(&mut __cursor, &#name::discriminator()).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if state.is_zero_copy {
|
if state.is_zero_copy {
|
||||||
|
|
Loading…
Reference in New Issue