Skip to content

Commit

Permalink
start refactor read only accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
ananas-block committed Dec 6, 2024
1 parent 0429034 commit a664fcd
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion programs/system/src/invoke/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub struct NewAddressParams {
}

#[derive(Debug, PartialEq, Default, Clone, Copy, AnchorSerialize, AnchorDeserialize)]
pub struct ReadOnlyAddressParamsPacked {
pub struct PackedReadOnlyAddress {
pub address: [u8; 32],
pub address_merkle_tree_account_index: u8,
pub address_merkle_tree_root_index: u16,
Expand Down
4 changes: 2 additions & 2 deletions programs/system/src/invoke/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
InstructionDataInvoke,
};

use super::ReadOnlyAddressParamsPacked;
use super::PackedReadOnlyAddress;

// TODO: remove once upgraded to anchor 0.30.0 (right now it's required for idl generation)
#[derive(Debug, Clone, PartialEq, Eq, AnchorSerialize, AnchorDeserialize)]
Expand Down Expand Up @@ -60,7 +60,7 @@ pub fn process<
invoking_program: Option<Pubkey>,
ctx: Context<'a, 'b, 'c, 'info, A>,
cpi_context_inputs: usize,
read_only_addresses: Option<Vec<ReadOnlyAddressParamsPacked>>,
read_only_addresses: Option<Vec<PackedReadOnlyAddress>>,
) -> Result<()> {
if inputs.relay_fee.is_some() {
unimplemented!("Relay fee is not implemented yet.");
Expand Down
2 changes: 1 addition & 1 deletion programs/system/src/invoke/verify_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn input_compressed_accounts_signer_check(
&& compressed_account_with_context
.compressed_account
.data
.is_none() && !compressed_account_with_context.read_only
.is_none()
{
Ok(())
} else {
Expand Down
6 changes: 3 additions & 3 deletions programs/system/src/invoke/verify_state_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use light_verifier::{
};
use std::mem;

use super::ReadOnlyAddressParamsPacked;
use super::PackedReadOnlyAddress;

// TODO: add support for batched Merkle trees
#[inline(never)]
Expand Down Expand Up @@ -98,7 +98,7 @@ pub fn fetch_roots_address_merkle_tree<
A: InvokeAccounts<'info> + Bumps,
>(
new_address_params: &'a [NewAddressParamsPacked],
read_only_addresses: &'a [ReadOnlyAddressParamsPacked],
read_only_addresses: &'a [PackedReadOnlyAddress],
ctx: &'a Context<'a, 'b, 'c, 'info, A>,
roots: &'a mut Vec<[u8; 32]>,
) -> Result<()> {
Expand All @@ -125,7 +125,7 @@ pub fn fetch_roots_address_merkle_tree<
#[inline(always)]
pub fn verify_read_only_address_queue_non_inclusion<'a>(
remaining_accounts: &'a [AccountInfo<'_>],
read_only_addresses: &'a [ReadOnlyAddressParamsPacked],
read_only_addresses: &'a [PackedReadOnlyAddress],
) -> Result<()> {
for read_only_address in read_only_addresses.iter() {
let merkle_tree_account_info =
Expand Down
10 changes: 7 additions & 3 deletions programs/system/src/invoke_cpi/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ use crate::{
invoke::{processor::CompressedProof, sol_compression::SOL_POOL_PDA_SEED},
sdk::{
accounts::{InvokeAccounts, SignerAccounts},
compressed_account::PackedCompressedAccountWithMerkleContext,
compressed_account::{
MerkleContext, PackedCompressedAccountWithMerkleContext,
PackedReadOnlyCompressedAccount,
},
CompressedCpiContext,
},
NewAddressParamsPacked, OutputCompressedAccountWithPackedContext, ReadOnlyAddressParamsPacked,
NewAddressParamsPacked, OutputCompressedAccountWithPackedContext, PackedReadOnlyAddress,
};

#[derive(Accounts)]
Expand Down Expand Up @@ -116,7 +119,8 @@ impl InstructionDataInvokeCpi {
#[derive(Debug, PartialEq, Default, Clone, AnchorSerialize, AnchorDeserialize)]
pub struct InstructionDataInvokeCpiWithReadOnlyAddress {
pub invoke_cpi: InstructionDataInvokeCpi,
pub read_only_addresses: Option<Vec<ReadOnlyAddressParamsPacked>>,
pub read_only_addresses: Option<Vec<PackedReadOnlyAddress>>,
pub read_only_accounts: Option<Vec<PackedReadOnlyCompressedAccount>>,
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions programs/system/src/invoke_cpi/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::verify_signer::cpi_signer_checks;
use crate::{
invoke::processor::process, invoke_cpi::instruction::InvokeCpiInstruction,
sdk::accounts::SignerAccounts, InstructionDataInvoke, InstructionDataInvokeCpi,
ReadOnlyAddressParamsPacked,
PackedReadOnlyAddress,
};

/// Processes an `InvokeCpi` instruction.
Expand All @@ -16,7 +16,7 @@ use crate::{
pub fn process_invoke_cpi<'a, 'b, 'c: 'info + 'b, 'info>(
mut ctx: Context<'a, 'b, 'c, 'info, InvokeCpiInstruction<'info>>,
inputs: InstructionDataInvokeCpi,
read_only_addresses: Option<Vec<ReadOnlyAddressParamsPacked>>,
read_only_addresses: Option<Vec<PackedReadOnlyAddress>>,
) -> Result<()> {
bench_sbf_start!("cpda_cpi_signer_checks");
cpi_signer_checks(
Expand Down
10 changes: 5 additions & 5 deletions programs/system/src/sdk/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use anchor_lang::{err, solana_program::pubkey::Pubkey, Result};
use light_utils::{hash_to_bn254_field_size_be, hashv_to_bn254_field_size_be};

use crate::{
errors::SystemProgramError, NewAddressParams, NewAddressParamsPacked, ReadOnlyAddressParams,
ReadOnlyAddressParamsPacked,
errors::SystemProgramError, NewAddressParams, NewAddressParamsPacked, PackedReadOnlyAddress,
ReadOnlyAddressParams,
};
pub fn derive_address_legacy(merkle_tree_pubkey: &Pubkey, seed: &[u8; 32]) -> Result<[u8; 32]> {
let hash = match hash_to_bn254_field_size_be(
Expand Down Expand Up @@ -98,15 +98,15 @@ pub fn pack_new_address_params(
pub fn pack_read_only_address_params(
new_address_params: &[ReadOnlyAddressParams],
remaining_accounts: &mut HashMap<Pubkey, usize>,
) -> Vec<ReadOnlyAddressParamsPacked> {
) -> Vec<PackedReadOnlyAddress> {
let mut new_address_params_packed = new_address_params
.iter()
.map(|x| ReadOnlyAddressParamsPacked {
.map(|x| PackedReadOnlyAddress {
address: x.address,
address_merkle_tree_root_index: x.address_merkle_tree_root_index,
address_merkle_tree_account_index: 0, // will be assigned later
})
.collect::<Vec<ReadOnlyAddressParamsPacked>>();
.collect::<Vec<PackedReadOnlyAddress>>();
let mut next_index: usize = remaining_accounts.len();
for (i, params) in new_address_params.iter().enumerate() {
match remaining_accounts.get(&params.address_merkle_tree_pubkey) {
Expand Down
12 changes: 12 additions & 0 deletions programs/system/src/sdk/compressed_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ impl CompressedAccountWithMerkleContext {
}
}

#[derive(Debug, PartialEq, Default, Clone, AnchorSerialize, AnchorDeserialize)]
pub struct ReadOnlyCompressedAccount {
pub account_hash: [u8; 32],
pub account: MerkleContext,
}

#[derive(Debug, PartialEq, Default, Clone, AnchorSerialize, AnchorDeserialize)]
pub struct PackedReadOnlyCompressedAccount {
pub account_hash: [u8; 32],
pub account: PackedCompressedAccountWithMerkleContext,
}

#[derive(Debug, Clone, Copy, AnchorSerialize, AnchorDeserialize, PartialEq, Default)]
pub struct MerkleContext {
pub merkle_tree_pubkey: Pubkey,
Expand Down
1 change: 1 addition & 0 deletions test-programs/system-cpi-test/src/create_pda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ fn cpi_compressed_pda_transfer_as_program<'info>(
let inputs_struct = InstructionDataInvokeCpiWithReadOnlyAddress {
invoke_cpi: inputs_struct,
read_only_addresses: read_only_address,
read_only_accounts: None,
};
let mut inputs = Vec::new();
InstructionDataInvokeCpiWithReadOnlyAddress::serialize(&inputs_struct, &mut inputs)
Expand Down

0 comments on commit a664fcd

Please sign in to comment.