From f83f5bd070fc81264496fb0a8e521a61e10035a5 Mon Sep 17 00:00:00 2001 From: Michal Rostecki Date: Mon, 4 Nov 2024 22:13:39 +0100 Subject: [PATCH] fix: Derive discriminator --- .../name-service-without-macros/src/lib.rs | 15 +++++++++++---- .../name-service-without-macros/tests/test.rs | 4 ---- sdk/src/account_info.rs | 4 ++++ sdk/src/verify.rs | 5 +++++ 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/examples/name-service/programs/name-service-without-macros/src/lib.rs b/examples/name-service/programs/name-service-without-macros/src/lib.rs index 76bfb5b760..6d118905f9 100644 --- a/examples/name-service/programs/name-service-without-macros/src/lib.rs +++ b/examples/name-service/programs/name-service-without-macros/src/lib.rs @@ -16,6 +16,7 @@ declare_id!("7yucc7fL3JGbyMwg4neUaenNSdySS39hbAk89Ao3t1Hz"); #[program] pub mod name_service { + use light_hasher::Discriminator; use light_sdk::account_info::convert_metas_to_infos; use super::*; @@ -29,12 +30,12 @@ pub mod name_service { let inputs = LightInstructionData::deserialize(&inputs)?; let mut account_infos = convert_metas_to_infos(&inputs.accounts, &crate::ID)?; - msg!("name: {}", name); account_infos[0].derive_address( - &[b"name-serice", name.as_bytes()], + &[b"name-service", name.as_bytes()], &crate::ID, ctx.remaining_accounts, )?; + account_infos[0].set_discriminator(NameRecord::discriminator()); let mut light_accounts = LightCreateRecord::try_light_accounts(&account_infos)?; @@ -61,7 +62,10 @@ pub mod name_service { new_rdata: RData, ) -> Result<()> { let inputs = LightInstructionData::deserialize(&inputs)?; - let account_infos = convert_metas_to_infos(&inputs.accounts, &crate::ID)?; + let mut account_infos = convert_metas_to_infos(&inputs.accounts, &crate::ID)?; + + account_infos[0].set_discriminator(NameRecord::discriminator()); + let mut light_accounts = LightCreateRecord::try_light_accounts(&account_infos)?; if light_accounts.record.owner != ctx.accounts.signer.key() { @@ -88,7 +92,10 @@ pub mod name_service { inputs: Vec, ) -> Result<()> { let inputs = LightInstructionData::deserialize(&inputs)?; - let account_infos = convert_metas_to_infos(&inputs.accounts, &crate::ID)?; + let mut account_infos = convert_metas_to_infos(&inputs.accounts, &crate::ID)?; + + account_infos[0].set_discriminator(NameRecord::discriminator()); + let light_accounts = LightDeleteRecord::try_light_accounts(&account_infos)?; if light_accounts.record.owner != ctx.accounts.signer.key() { diff --git a/examples/name-service/programs/name-service-without-macros/tests/test.rs b/examples/name-service/programs/name-service-without-macros/tests/test.rs index a9d9d27122..3a554306d1 100644 --- a/examples/name-service/programs/name-service-without-macros/tests/test.rs +++ b/examples/name-service/programs/name-service-without-macros/tests/test.rs @@ -299,7 +299,6 @@ where address_queue_pubkey: env.address_merkle_tree_queue_pubkey, }; let account = LightAccountMeta::new_init( - None, None, &env.merkle_tree_pubkey, Some(&address_merkle_context), @@ -378,8 +377,6 @@ where compressed_account, rpc_result.root_indices[0], &merkle_tree_pubkey, - None, - None, false, remaining_accounts, ); @@ -451,7 +448,6 @@ where let compressed_account = LightAccountMeta::new_close( compressed_account, - None, rpc_result.root_indices[0], remaining_accounts, ); diff --git a/sdk/src/account_info.rs b/sdk/src/account_info.rs index fe67051883..72c0baf6d4 100644 --- a/sdk/src/account_info.rs +++ b/sdk/src/account_info.rs @@ -148,6 +148,10 @@ impl<'a> LightAccountInfo<'a> { } } + pub fn set_discriminator(&mut self, discriminator: [u8; 8]) { + self.discriminator = Some(discriminator); + } + /// Converts the given [LightAccountInfo] into a /// [PackedCompressedAccountWithMerkleContext] which can be sent to the /// light-system program. diff --git a/sdk/src/verify.rs b/sdk/src/verify.rs index 9f9de7501e..d09a7c23a5 100644 --- a/sdk/src/verify.rs +++ b/sdk/src/verify.rs @@ -237,6 +237,11 @@ pub fn verify<'info, 'a, 'b, 'c, T>( where T: AnchorSerialize, { + anchor_lang::prelude::msg!( + "light program: {}", + ctx.accounts.get_light_system_program().key() + ); + anchor_lang::prelude::msg!("expedted: {}", PROGRAM_ID_LIGHT_SYSTEM); if ctx.accounts.get_light_system_program().key() != PROGRAM_ID_LIGHT_SYSTEM { return err!(LightSdkError::InvalidLightSystemProgram); }