Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: Support non-8-byte discriminators #3165

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- lang: Add `discriminator` argument to `#[event]` attribute ([#3152](https://github.com/coral-xyz/anchor/pull/3152)).
- idl: Check ambiguous discriminators ([#3157](https://github.com/coral-xyz/anchor/pull/3157)).
- idl: Disallow all zero account discriminators ([#3159](https://github.com/coral-xyz/anchor/pull/3159)).
- cli: Support non-8-byte discriminators ([#3165](https://github.com/coral-xyz/anchor/pull/3165)).

### Fixes

Expand Down
19 changes: 10 additions & 9 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::config::{
};
use anchor_client::Cluster;
use anchor_lang::idl::{IdlAccount, IdlInstruction, ERASED_AUTHORITY};
use anchor_lang::{AccountDeserialize, AnchorDeserialize, AnchorSerialize};
use anchor_lang::{AccountDeserialize, AnchorDeserialize, AnchorSerialize, Discriminator};
use anchor_lang_idl::convert::convert_idl;
use anchor_lang_idl::types::{Idl, IdlArrayLen, IdlDefinedFields, IdlType, IdlTypeDefTy};
use anyhow::{anyhow, Context, Result};
Expand Down Expand Up @@ -2256,7 +2256,7 @@ fn fetch_idl(cfg_override: &ConfigOverride, idl_addr: Pubkey) -> Result<Idl> {
}

// Cut off account discriminator.
let mut d: &[u8] = &account.data[8..];
let mut d: &[u8] = &account.data[IdlAccount::DISCRIMINATOR.len()..];
let idl_account: IdlAccount = AnchorDeserialize::deserialize(&mut d)?;

let compressed_len: usize = idl_account.data_len.try_into().unwrap();
Expand Down Expand Up @@ -2900,12 +2900,13 @@ fn account(
};

let data = create_client(cluster.url()).get_account_data(&address)?;
if data.len() < 8 {
return Err(anyhow!(
"The account has less than 8 bytes and is not an Anchor account."
));
}
let mut data_view = &data[8..];
let disc_len = idl
.accounts
.iter()
.find(|acc| acc.name == account_type_name)
.map(|acc| acc.discriminator.len())
.ok_or_else(|| anyhow!("Account `{account_type_name}` not found in IDL"))?;
let mut data_view = &data[disc_len..];

let deserialized_json =
deserialize_idl_defined_type_to_json(&idl, account_type_name, &mut data_view)?;
Expand Down Expand Up @@ -3941,7 +3942,7 @@ fn create_idl_buffer(

// Creates the new buffer account with the system program.
let create_account_ix = {
let space = 8 + 32 + 4 + serialize_idl(idl)?.len();
let space = IdlAccount::DISCRIMINATOR.len() + 32 + 4 + serialize_idl(idl)?.len();
let lamports = client.get_minimum_balance_for_rent_exemption(space)?;
solana_sdk::system_instruction::create_account(
&keypair.pubkey(),
Expand Down
Loading