Skip to content

Commit

Permalink
chore: add read only placeholder in compressed accounts with Merkle c…
Browse files Browse the repository at this point in the history
…ontext (#1065)

Changes:
- add placeholder marker to mark input compressed accounts as readonly
- readonly needs to be false else panic with `unimplemented`
  • Loading branch information
ananas-block authored Aug 9, 2024
1 parent 0ccb5b0 commit f6ff54a
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ fn create_compressed_pda_data_based_on_diff(
compressed_account: old_compressed_account.compressed_account,
merkle_context: input_compressed_pda.merkle_context,
root_index: input_compressed_pda.root_index,
read_only: false,
};
let new_timelock_compressed_pda = EscrowTimeLock {
slot: current_slot
Expand Down
14 changes: 14 additions & 0 deletions js/compressed-token/src/idl/light_compressed_token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,13 @@ export type LightCompressedToken = {
];
type: 'u16';
},
{
name: 'readOnly';
docs: [
'Placeholder to mark accounts read-only unimplemented set to false.',
];
type: 'bool';
},
];
};
},
Expand Down Expand Up @@ -2850,6 +2857,13 @@ export const IDL: LightCompressedToken = {
],
type: 'u16',
},
{
name: 'readOnly',
docs: [
'Placeholder to mark accounts read-only unimplemented set to false.',
],
type: 'bool',
},
],
},
},
Expand Down
14 changes: 14 additions & 0 deletions js/stateless.js/src/idls/light_compressed_token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,13 @@ export type LightCompressedToken = {
];
type: 'u16';
},
{
name: 'readOnly';
docs: [
'Placeholder to mark accounts read-only unimplemented set to false.',
];
type: 'bool';
},
];
};
},
Expand Down Expand Up @@ -2850,6 +2857,13 @@ export const IDL: LightCompressedToken = {
],
type: 'u16',
},
{
name: 'readOnly',
docs: [
'Placeholder to mark accounts read-only unimplemented set to false.',
],
type: 'bool',
},
],
},
},
Expand Down
14 changes: 14 additions & 0 deletions js/stateless.js/src/idls/light_system_program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,13 @@ export type LightSystemProgram = {
];
type: 'u16';
},
{
name: 'readOnly';
docs: [
'Placeholder to mark accounts read-only unimplemented set to false.',
];
type: 'bool';
},
];
};
},
Expand Down Expand Up @@ -1819,6 +1826,13 @@ export const IDL: LightSystemProgram = {
],
type: 'u16',
},
{
name: 'readOnly',
docs: [
'Placeholder to mark accounts read-only unimplemented set to false.',
],
type: 'bool',
},
],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export function packCompressedAccounts(
queueIndex: null,
},
rootIndex: inputStateRootIndices[index],
readOnly: false,
});
});

Expand Down
6 changes: 5 additions & 1 deletion js/stateless.js/src/state/compressed-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { BN254, bn } from './BN254';
import { Buffer } from 'buffer';

export type CompressedAccountWithMerkleContext = CompressedAccount &
MerkleContext;
MerkleContext & {
readOnly: boolean;
};

/**
* Context for compressed account inserted into a state Merkle tree
Expand Down Expand Up @@ -55,6 +57,7 @@ export const createCompressedAccountWithMerkleContext = (
): CompressedAccountWithMerkleContext => ({
...createCompressedAccount(owner, lamports, data, address),
...merkleContext,
readOnly: false,
});

export const createMerkleContext = (
Expand Down Expand Up @@ -134,6 +137,7 @@ if (import.meta.vitest) {
nullifierQueue,
hash,
leafIndex,
readOnly: false,
});
});
});
Expand Down
1 change: 1 addition & 0 deletions js/stateless.js/src/state/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface PackedCompressedAccountWithMerkleContext {
compressedAccount: CompressedAccount;
merkleContext: PackedMerkleContext;
rootIndex: number; // u16
readOnly: boolean;
}

export interface PackedMerkleContext {
Expand Down
4 changes: 4 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions programs/compressed-token/src/freeze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ pub mod test_freeze {
},
root_index: x.root_index,
merkle_context: x.merkle_context,
read_only: false,
}
})
.collect()
Expand Down
1 change: 1 addition & 0 deletions programs/compressed-token/src/process_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ pub fn get_input_compressed_accounts_with_merkle_context_and_check_signer<const
compressed_account,
merkle_context: input_token_data.merkle_context,
root_index: input_token_data.root_index,
read_only: false,
},
);
}
Expand Down
4 changes: 4 additions & 0 deletions programs/system/src/invoke/sum_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ pub fn sum_check(
) -> Result<()> {
let mut sum: u64 = 0;
for compressed_account_with_context in input_compressed_accounts_with_merkle_context.iter() {
if compressed_account_with_context.read_only {
unimplemented!("read_only accounts are not supported. Set read_only to false.");
}
sum = sum
.checked_add(compressed_account_with_context.compressed_account.lamports)
.ok_or(ProgramError::ArithmeticOverflow)
Expand Down Expand Up @@ -139,6 +142,7 @@ mod test {
queue_index: None,
},
root_index: 1,
read_only: false,
});
}
let mut outputs = Vec::new();
Expand Down
1 change: 1 addition & 0 deletions programs/system/src/invoke_cpi/process_cpi_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ mod tests {
queue_index: None,
},
root_index: iter.into(),
read_only: false,
},
],
output_compressed_accounts: vec![OutputCompressedAccountWithPackedContext {
Expand Down
2 changes: 2 additions & 0 deletions programs/system/src/sdk/compressed_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub struct PackedCompressedAccountWithMerkleContext {
pub merkle_context: PackedMerkleContext,
/// Index of root used in inclusion validity proof.
pub root_index: u16,
/// Placeholder to mark accounts read-only unimplemented set to false.
pub read_only: bool,
}

#[derive(Debug, PartialEq, Default, Clone, AnchorSerialize, AnchorDeserialize)]
Expand Down
1 change: 1 addition & 0 deletions programs/system/src/sdk/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ pub fn create_invoke_instruction_data_and_remaining_accounts(
leaf_index: context.leaf_index,
queue_index: None,
},
read_only: false,
root_index: input_root_indices[i],
});
}
Expand Down
1 change: 1 addition & 0 deletions test-programs/system-cpi-test/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,7 @@ pub async fn perform_with_input_accounts<R: RpcConnection>(
queue_index: None,
},
root_index: rpc_result.root_indices[0],
read_only: false,
},
token_transfer_data,
invalid_fee_payer: &invalid_fee_payer.pubkey(),
Expand Down

0 comments on commit f6ff54a

Please sign in to comment.