forked from helius-labs/photon
-
Notifications
You must be signed in to change notification settings - Fork 0
v2 API for compressed account proofs #10
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
Merged
SwenSchaeferjohann
merged 9 commits into
jorrit/feat-add-test
from
sergey/photon-ref-tree-context
Feb 28, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c423ece
* implement v2 API for compressed account proofs
sergeytimoshin 97d57dd
Update src/api/method/get_compressed_account_proof/v2.rs
sergeytimoshin f23ceeb
fix: add error handling for mismatched accounts and hashes in v2 API
sergeytimoshin 90d162b
fix: add error handling for missing account in v2 API
sergeytimoshin 62b3b9d
feat: integrate ContextInfo into GetCompressedAccountProofResponseVal…
sergeytimoshin d066125
refactor: clean up imports in tx_event_parser.rs
sergeytimoshin 14dfd6d
refactor: remove debug print statement from leaf_node.rs
sergeytimoshin d76e21e
refactor: simplify GetMultipleCompressedAccountProofsResponseValueV2 …
sergeytimoshin 7296ab4
refactor: simplify GetMultipleCompressedAccountProofsResponseV2 by re…
sergeytimoshin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| mod v1; | ||
| mod v2; | ||
|
|
||
| pub use v1::{ | ||
| get_compressed_account_proof, GetCompressedAccountProofResponse, | ||
| GetCompressedAccountProofResponseValueV1, | ||
| }; | ||
| pub use v2::{ | ||
| get_compressed_account_proof_v2, GetCompressedAccountProofResponseV2, | ||
| GetCompressedAccountProofResponseValueV2, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| use crate::api::error::PhotonApiError; | ||
| use crate::api::method::get_validity_proof::ContextInfo; | ||
| use crate::api::method::utils::HashRequest; | ||
| use crate::common::typedefs::context::Context; | ||
| use crate::common::typedefs::hash::Hash; | ||
| use crate::common::typedefs::serializable_pubkey::SerializablePubkey; | ||
| use crate::dao::generated::{accounts, state_trees}; | ||
| use crate::ingester::persist::{ | ||
| get_multiple_compressed_leaf_proofs, get_multiple_compressed_leaf_proofs_by_indices, | ||
| MerkleProofWithContext, | ||
| }; | ||
| use jsonrpsee_core::Serialize; | ||
| use sea_orm::{ | ||
| ColumnTrait, ConnectionTrait, DatabaseBackend, DatabaseConnection, EntityTrait, QueryFilter, | ||
| Statement, TransactionTrait, | ||
| }; | ||
| use serde::Deserialize; | ||
| use utoipa::ToSchema; | ||
|
|
||
| #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)] | ||
| #[serde(deny_unknown_fields, rename_all = "camelCase")] | ||
| pub struct GetCompressedAccountProofResponseV2 { | ||
| pub context: Context, | ||
| pub value: GetCompressedAccountProofResponseValueV2, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)] | ||
| #[serde(deny_unknown_fields, rename_all = "camelCase")] | ||
| pub struct GetCompressedAccountProofResponseValueV2 { | ||
| pub proof: Vec<Hash>, | ||
| pub root: Hash, | ||
| pub leaf_index: u32, | ||
| pub hash: Hash, | ||
| pub root_seq: u64, | ||
| pub prove_by_index: bool, | ||
| pub context: ContextInfo, | ||
| } | ||
|
|
||
| impl From<MerkleProofWithContext> for GetCompressedAccountProofResponseValueV2 { | ||
| fn from(proof: MerkleProofWithContext) -> Self { | ||
| GetCompressedAccountProofResponseValueV2 { | ||
| proof: proof.proof, | ||
| root: proof.root, | ||
| leaf_index: proof.leaf_index, | ||
| hash: proof.hash, | ||
| root_seq: proof.root_seq, | ||
| prove_by_index: false, | ||
| // Default values to be overridden as needed | ||
| context: ContextInfo { | ||
| tree_type: 0, | ||
| merkle_tree: proof.merkle_tree, | ||
| queue: Default::default(), | ||
| cpi_context: None, | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub async fn get_compressed_account_proof_v2( | ||
| conn: &DatabaseConnection, | ||
| request: HashRequest, | ||
| ) -> Result<GetCompressedAccountProofResponseV2, PhotonApiError> { | ||
| let context = Context::extract(conn).await?; | ||
| let hash = request.hash; | ||
| let tx = conn.begin().await?; | ||
| if tx.get_database_backend() == DatabaseBackend::Postgres { | ||
| tx.execute(Statement::from_string( | ||
| tx.get_database_backend(), | ||
| "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;".to_string(), | ||
| )) | ||
| .await?; | ||
| } | ||
|
|
||
| let account = accounts::Entity::find() | ||
| .filter(accounts::Column::Hash.eq(hash.to_vec())) | ||
| .one(&tx) | ||
| .await?; | ||
|
|
||
| if account.is_none() { | ||
| return Err(PhotonApiError::RecordNotFound( | ||
| "Account not found".to_string(), | ||
| )); | ||
| } | ||
|
|
||
| let leaf_node = state_trees::Entity::find() | ||
| .filter( | ||
| state_trees::Column::Hash | ||
| .eq(hash.to_vec()) | ||
| .and(state_trees::Column::Level.eq(0)), | ||
| ) | ||
| .one(&tx) | ||
| .await?; | ||
|
|
||
| // Determine how to generate the proof based on available data | ||
| let mut result: GetCompressedAccountProofResponseValueV2 = if leaf_node.is_some() { | ||
| let mut response: GetCompressedAccountProofResponseValueV2 = | ||
| get_multiple_compressed_leaf_proofs(&tx, vec![hash]) | ||
| .await? | ||
| .into_iter() | ||
| .next() | ||
| .ok_or(PhotonApiError::RecordNotFound( | ||
| "Account not found by hash".to_string(), | ||
| ))? | ||
| .into(); | ||
| response.prove_by_index = false; | ||
| response | ||
| } else if let Some(account) = account.clone() { | ||
| // Use index-based proof if we found the account in a queue but not in state_trees | ||
| let leaf_index = account.leaf_index as u64; | ||
| let merkle_tree = SerializablePubkey::try_from(account.tree.clone())?; | ||
| let mut response: GetCompressedAccountProofResponseValueV2 = | ||
| get_multiple_compressed_leaf_proofs_by_indices(&tx, merkle_tree, vec![leaf_index]) | ||
| .await? | ||
| .into_iter() | ||
| .next() | ||
| .ok_or(PhotonApiError::RecordNotFound( | ||
| "Account not found by index".to_string(), | ||
| ))? | ||
| .into(); | ||
| response.prove_by_index = true; | ||
| response | ||
| } else { | ||
| return Err(PhotonApiError::RecordNotFound( | ||
| "Account not found".to_string(), | ||
| )); | ||
| }; | ||
|
|
||
| // Enrich with account data if available | ||
| if let Some(account) = account { | ||
| result.context.tree_type = account.tree_type as u16; | ||
| result.context.queue = SerializablePubkey::try_from(account.queue)?; | ||
| } | ||
|
|
||
| let response = GetCompressedAccountProofResponseV2 { | ||
| value: result, | ||
| context, | ||
| }; | ||
|
|
||
| tx.commit().await?; | ||
| Ok(response) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't the call fail if no data is available?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/Lightprotocol/photon/blob/sergey/photon-ref-tree-context/src/api/method/get_compressed_account_proof/v2.rs#L77-L81