Skip to content

Commit

Permalink
feat: Test SDK which uses Photon indexer
Browse files Browse the repository at this point in the history
To break the dependency of examples and third-party programs on Light
Protocol program crates, test them with a new test SDK and Photon
indexed, which has no dependency on them.
  • Loading branch information
vadorovsky committed Sep 12, 2024
1 parent 35241f1 commit c69a805
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 47 deletions.
2 changes: 0 additions & 2 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ light-indexed-merkle-tree = { workspace = true }
account-compression = { version = "1.0.0", path = "../programs/account-compression", features = ["cpi"] }
light-system-program = { version = "1.0.0", path = "../programs/system", features = ["cpi"] }
light-concurrent-merkle-tree = { path = "../merkle-tree/concurrent", version = "1.0.0" }
light-indexed-merkle-tree = { workspace = true }
light-utils = { version = "1.0.0", path = "../utils" }
groth16-solana = "0.0.3"
light-verifier = { path = "../circuit-lib/verifier", version = "1.0.0", features = ["solana"] }
Expand All @@ -59,7 +58,6 @@ reqwest = "0.12"
tokio = { workspace = true }
light-prover-client = { version = "1.0.0", path = "../circuit-lib/light-prover-client" }
light-merkle-tree-reference = { version = "1.0.0", path = "../merkle-tree/reference/" }
light-indexed-merkle-tree = { version = "1.0.0", path = "../merkle-tree/indexed/" }
num-bigint = "0.4.6"
num-traits = "0.2.19"
lazy_static = "1.4.0"
Expand Down
10 changes: 4 additions & 6 deletions sdk/src/compressed_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,15 +596,13 @@ where
Ok(
light_system_program::sdk::compressed_account::PackedCompressedAccountWithMerkleContext {
compressed_account,
merkle_context: crate::legacy::PackedMerkleContext {
merkle_context: PackedMerkleContext {
merkle_tree_pubkey_index: merkle_context.merkle_tree_pubkey_index,
nullifier_queue_pubkey_index: merkle_context.nullifier_queue_pubkey_index,
leaf_index: merkle_context.leaf_index,
queue_index: merkle_context.queue_index.map(|queue_index| {
crate::legacy::QueueIndex {
queue_id: queue_index.queue_id,
index: queue_index.index,
}
queue_index: merkle_context.queue_index.map(|queue_index| QueueIndex {
queue_id: queue_index.queue_id,
index: queue_index.index,
}),
},
root_index: merkle_tree_root_index,
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ where
})
}

pub fn verify(&mut self, proof: crate::legacy::CompressedProof) -> Result<()> {
pub fn verify(&mut self, proof: CompressedProof) -> Result<()> {
let bump = Pubkey::find_program_address(
&[CPI_AUTHORITY_PDA_SEED],
&self.anchor_context.accounts.get_invoking_program().key(),
Expand All @@ -112,7 +112,7 @@ where
.light_accounts
.output_accounts(self.anchor_context.remaining_accounts)?;

let instruction = crate::legacy::InstructionDataInvokeCpi {
let instruction = InstructionDataInvokeCpi {
proof: Some(proof),
new_address_params,
relay_fee: None,
Expand Down
12 changes: 0 additions & 12 deletions sdk/src/legacy.rs

This file was deleted.

1 change: 0 additions & 1 deletion sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pub mod constants;
pub use constants::*;
pub mod context;
pub mod event;
pub mod legacy;
pub mod merkle_context;
pub mod program_merkle_context;
pub mod proof;
Expand Down
47 changes: 23 additions & 24 deletions sdk/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ use crate::{
SignerAccounts,
},
};
use light_system_program::{
cpi::accounts::InvokeCpiInstruction, errors::SystemProgramError::CpiContextAccountUndefined,
};

#[derive(AnchorSerialize, AnchorDeserialize, Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct CompressedCpiContext {
Expand All @@ -41,30 +38,32 @@ pub struct InstructionDataInvokeCpi {
pub cpi_context: Option<CompressedCpiContext>,
}

// NOTE(vadorovsky): 'Not sure whether this function even makes sense now in
// the first place.
// TODO: properly document compressed-cpi-context
// TODO: turn into a simple check!
// TOOD: CHECK needed bc can be different from own, if called from another program.
pub fn get_compressed_cpi_context_account<'info>(
ctx: &Context<
'_,
'_,
'_,
'info,
impl InvokeAccounts<'info>
+ LightSystemAccount<'info>
+ InvokeCpiAccounts<'info>
+ SignerAccounts<'info>
+ Bumps,
>,
compressed_cpi_context: &CompressedCpiContext,
) -> Result<AccountInfo<'info>> {
let cpi_context_account = ctx
.remaining_accounts
.get(compressed_cpi_context.cpi_context_account_index as usize)
.map(|account| account.to_account_info())
.ok_or_else(|| Error::from(CpiContextAccountUndefined))?;
Ok(cpi_context_account)
}
// pub fn get_compressed_cpi_context_account<'info>(
// ctx: &Context<
// '_,
// '_,
// '_,
// 'info,
// impl InvokeAccounts<'info>
// + LightSystemAccount<'info>
// + InvokeCpiAccounts<'info>
// + SignerAccounts<'info>
// + Bumps,
// >,
// compressed_cpi_context: &CompressedCpiContext,
// ) -> Result<AccountInfo<'info>> {
// let cpi_context_account = ctx
// .remaining_accounts
// .get(compressed_cpi_context.cpi_context_account_index as usize)
// .map(|account| account.to_account_info())
// .ok_or_else(|| Error::from(CpiContextAccountUndefined))?;
// Ok(cpi_context_account)
// }

#[inline(always)]
pub fn setup_cpi_accounts<'info>(
Expand Down
20 changes: 20 additions & 0 deletions test-sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "light-test-sdk"
version = "0.8.0"
edition = "2021"

[dependencies]
anchor-lang = { workspace = true }

solana-client = { workspace = true }
solana-sdk = { workspace = true }

tokio = { workspace = true, features = ["process"] }
async-trait = { workspace = true }
async-dropper = { workspace = true }

light-macros = { path = "../macros/light", version = "1.0.0" }
light-sdk = { path = "../sdk", version = "0.8.0" }
photon-api = { path = "../photon-api", version = "0.44.0" }

cargo_metadata = { workspace = true }
86 changes: 86 additions & 0 deletions test-sdk/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use std::process::Stdio;

use cargo_metadata::MetadataCommand;
use light_macros::pubkey;
use light_sdk::merkle_context::AddressMerkleContext;
use photon_api::apis::configuration::Configuration;
use solana_client::rpc_client::RpcClient;
use solana_sdk::signature::Keypair;
use tokio::process::Command;

/// A test environment for programs using Light Protocol which manages:
///
/// - test validator
/// - Photon indexer
pub struct LightTest {
payer: Keypair,
pub photon_configuration: Configuration,
}

impl LightTest {
/// Creates a new test environment.
pub async fn new(program_name: &str) -> Self {
let validator = Command::new("light")
.arg("test-validator")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
// .spawn()
.status()
.await
.unwrap();
println!("{validator:?}");
if !validator.success() {
panic!("failed to run the validator");
}

let payer = Keypair::new();

let metadata = MetadataCommand::new().no_deps().exec().unwrap();
let workspace_root = metadata.workspace_root;

let program_so_path = format!("{workspace_root}/target/deploy/{program_name}.so");
let program_keypair_path =
format!("{workspace_root}/target/deploy/{program_name}-keypair.json");

let out = Command::new("solana")
.arg("program")
.arg("deploy")
.arg(program_so_path)
.arg("--program-id")
.arg(program_keypair_path)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.output()
.await
.unwrap();
println!("{out:?}");

let photon_configuration = Configuration {
base_path: "http://localhost:8784".to_string(),
api_key: None,
..Default::default()
};

Self {
payer,
photon_configuration,
}
}

/// Returns a Solana RPC client connected to the test validator.
pub fn client(&self) -> RpcClient {
RpcClient::new("http://localhost:8899")
}

pub fn address_merkle_context(&self) -> AddressMerkleContext {
AddressMerkleContext {
address_merkle_tree_pubkey: pubkey!("amt1Ayt45jfbdw5YSo7iz6WZxUmnZsQTYXy82hVwyC2"),
address_queue_pubkey: pubkey!("aq1S9z4reTSQAdgWHGD2zDaS39sjGrAxbR31vxJ2F4F"),
}
}

/// Returns a payer keypair.
pub fn payer(&self) -> Keypair {
self.payer.insecure_clone()
}
}

0 comments on commit c69a805

Please sign in to comment.