Skip to content

Commit

Permalink
refactor: Remove hard dependency on light-system-program in SDK (#1230
Browse files Browse the repository at this point in the history
)

* refactor: Remove hard dependency on `light-system-program` in SDK

First of all, add more types to the SDK, which are going to be used by
third-party applications, which were until now defined in program
crates:

- `address`
  - `NewAddressParams`
  - `NewAddressParamsPacked`
- `compressed_account`
  - `CompressedAccount`
  - `CompressedAccountData`
  - `CompressedAccountWithMerkleContext`
  - `PackedCompressedAccountWithMerkleContext`
  - `OutputCompressedAccountWithPackedContext`
- `event`
  - `MerkleTreeSequenceNumber`
  - `PublicTransactionEvent`
- `merkle_context`
  - `QueueIndex`
  - `MerkleContext`
  - `PackedMerkleContext`
- `proof`
  - `CompressedProof`
  - `ProofRpcResult`
- `verify`
  - `CompressedCpiContext`
  - `InstructionDataInvokeCpi`

Hide the imports from `light_system_program` behind a feature flag. The
long-term plan is to remove them all together.

Provide a dependency-free, tinier implementation of `TestIndexer` in
the `light-client` crate.

* Remove commented code

* Add a check for light-system-program ID
  • Loading branch information
vadorovsky authored Sep 17, 2024
1 parent 09d605a commit 298e10f
Show file tree
Hide file tree
Showing 37 changed files with 1,958 additions and 302 deletions.
19 changes: 13 additions & 6 deletions Cargo.lock

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

17 changes: 17 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,25 @@ thiserror = "1.0"

# Light Protocol
light-client = { path = "client", version = "0.8.0" }
light-concurrent-merkle-tree = { path = "merkle-tree/concurrent", version = "1.0.0" }
light-hasher = { path = "merkle-tree/hasher", version = "1.0.0" }
light-indexed-merkle-tree = { path = "merkle-tree/indexed", version = "1.0.0" }
light-macros = { path = "macros/light", version = "1.0.0" }
light-merkle-tree-reference = { path = "merkle-tree/reference", version = "1.0.0" }
light-prover-client = { path = "circuit-lib/light-prover-client", version = "1.0.0" }
light-sdk = { path = "sdk", version = "0.8.0" }
light-sdk-macros = { path = "macros/light-sdk-macros", version = "0.1.0" }
light-utils = { path = "utils", version = "1.0.0" }
light-verifier = { path = "circuit-lib/verifier", version = "1.0.0" }
photon-api = { path = "photon-api" }

# Math and crypto
num-bigint = "0.4.6"
num-traits = "0.2.19"

# HTTP client
reqwest = "0.11.26"

[patch.crates-io]
"solana-account-decoder" = { git = "https://github.com/lightprotocol/agave", branch = "v1.18.22-enforce-cpi-tracking" }
"solana-accounts-db" = { git = "https://github.com/lightprotocol/agave", branch = "v1.18.22-enforce-cpi-tracking" }
Expand Down
18 changes: 18 additions & 0 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ description = "Client library for Light Protocol"
solana-banks-client = { workspace = true }
solana-client = { workspace = true }
solana-program = { workspace = true }
solana-program-test = { workspace = true }
solana-sdk = { workspace = true }
solana-transaction-status = { workspace = true }

Expand All @@ -22,5 +23,22 @@ tokio = { workspace = true }
async-trait = { workspace = true }
bb8 = { workspace = true }

# Logging
log = { workspace = true }

# Error handling
thiserror = { workspace = true }

# Light Protocol
light-concurrent-merkle-tree = { workspace = true }
light-indexed-merkle-tree = { workspace = true }
light-merkle-tree-reference = { workspace = true }
light-prover-client = { workspace = true }
light-sdk = { workspace = true }

# Math and crypto
num-bigint = { workspace = true }
num-traits = { workspace = true }

# HTTP client
reqwest = { workspace = true }
111 changes: 110 additions & 1 deletion client/src/indexer/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,110 @@
pub trait Indexer: Sync + Send + Debug + 'static {}
use std::{fmt::Debug, future::Future};

use light_concurrent_merkle_tree::light_hasher::Poseidon;
use light_indexed_merkle_tree::{
array::{IndexedArray, IndexedElement},
reference::IndexedMerkleTree,
};
use light_merkle_tree_reference::MerkleTree;
use light_sdk::{
compressed_account::CompressedAccountWithMerkleContext, event::PublicTransactionEvent,
proof::ProofRpcResult, token::TokenDataWithMerkleContext,
};
use num_bigint::BigUint;
use solana_sdk::pubkey::Pubkey;
use thiserror::Error;

use crate::rpc::RpcConnection;

pub mod test_indexer;

#[derive(Error, Debug)]
pub enum IndexerError {
#[error("RPC Error: {0}")]
RpcError(#[from] solana_client::client_error::ClientError),
#[error("failed to deserialize account data")]
DeserializeError(#[from] solana_sdk::program_error::ProgramError),
#[error("failed to copy merkle tree")]
CopyMerkleTreeError(#[from] std::io::Error),
#[error("error: {0:?}")]
Custom(String),
#[error("unknown error")]
Unknown,
}

pub trait Indexer<R: RpcConnection>: Sync + Send + Debug + 'static {
fn add_event_and_compressed_accounts(
&mut self,
event: &PublicTransactionEvent,
) -> (
Vec<CompressedAccountWithMerkleContext>,
Vec<TokenDataWithMerkleContext>,
);

fn create_proof_for_compressed_accounts(
&mut self,
compressed_accounts: Option<&[[u8; 32]]>,
state_merkle_tree_pubkeys: Option<&[Pubkey]>,
new_addresses: Option<&[[u8; 32]]>,
address_merkle_tree_pubkeys: Option<Vec<Pubkey>>,
rpc: &mut R,
) -> impl Future<Output = ProofRpcResult>;

fn get_compressed_accounts_by_owner(
&self,
owner: &Pubkey,
) -> Vec<CompressedAccountWithMerkleContext>;
}

#[derive(Debug, Clone)]
pub struct MerkleProof {
pub hash: String,
pub leaf_index: u64,
pub merkle_tree: String,
pub proof: Vec<[u8; 32]>,
pub root_seq: u64,
}

// For consistency with the Photon API.
#[derive(Clone, Default, Debug, PartialEq)]
pub struct NewAddressProofWithContext {
pub merkle_tree: [u8; 32],
pub root: [u8; 32],
pub root_seq: u64,
pub low_address_index: u64,
pub low_address_value: [u8; 32],
pub low_address_next_index: u64,
pub low_address_next_value: [u8; 32],
pub low_address_proof: [[u8; 32]; 16],
pub new_low_element: Option<IndexedElement<usize>>,
pub new_element: Option<IndexedElement<usize>>,
pub new_element_next_value: Option<BigUint>,
}

#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
pub struct StateMerkleTreeAccounts {
pub merkle_tree: Pubkey,
pub nullifier_queue: Pubkey,
pub cpi_context: Pubkey,
}

#[derive(Debug, Clone, Copy)]
pub struct AddressMerkleTreeAccounts {
pub merkle_tree: Pubkey,
pub queue: Pubkey,
}

#[derive(Debug, Clone)]
pub struct StateMerkleTreeBundle {
pub rollover_fee: u64,
pub merkle_tree: Box<MerkleTree<Poseidon>>,
pub accounts: StateMerkleTreeAccounts,
}

#[derive(Debug, Clone)]
pub struct AddressMerkleTreeBundle {
pub rollover_fee: u64,
pub merkle_tree: Box<IndexedMerkleTree<Poseidon, usize>>,
pub indexed_array: Box<IndexedArray<Poseidon, usize>>,
pub accounts: AddressMerkleTreeAccounts,
}
Loading

0 comments on commit 298e10f

Please sign in to comment.