From 48b9edc439124db623470031f39cb7dab5e0b829 Mon Sep 17 00:00:00 2001 From: Swenschaeferjohann Date: Tue, 29 Jul 2025 12:08:35 -0400 Subject: [PATCH 1/2] cherrypicked hasher update: support sha in hasher and lighthasher macro lint remove unused _output_account_info update lightdiscriminator macro wip add compress_pda helper compress_pda compiling decompress_idempotent.rs wip wip decompress batch idempotent wip add compress_pda_new and compress_multiple_pdas_new native program with decompress done compress_dynamic, decompress_dynamic wip adding anchor testprogram uses sdk fix compilation wip experiment with procmacro skip SLOT check at compress_pda_new wip add_compressible_instructions() works for compress, idl gen works add proc macro decompress_multiple_pdas is working as it should fix decompress_idempotent impl rm expanded add remove_data force apps to pass the whole signer_seeds directly add compressible_config draft add create_config_unchecked and checked use config, add unified header struct (just last_written_slot) for now use hascompressioninfo and compressioninfo add config support to compressible macro add expanded.rs cleanup anchor-derived example add support for multiple address_trees per address space add support for multiple address_trees per address space update macro to multiple address_trees add test-sdk-derived program wip cleanup native macro-derive example wip fix compilation config tests working clean up test_config.rs testing add a separate anchor compress_pda_new version so we dont have redundant serde wip wip wip fix decompress_idempotent anchor add test with 2nd account decompress works with multiple different PDAs cleanup, remove anchor helper decompress_multiple_pdas, fix discriminator writes rm examples simplify add compress_multiple_new + test add test add test: compressing multiple accounts_new fix compress_pda and add test added test case: invalid compression cleanup cleanup fmt and lint for anchor-compressible-user program config ix helpers added tests working, added generic create_compress_account_instruction rm warnings added standardized decompress_multiple_accounts_idempotent client rm idl add cu logger util wip add test: decompress with accounts stored in 2 different v2 state trees, added 2nd tree impl. to program-test and cli, added util to xtask clean clean up librs add opt anchor program error conv for errors, fix compression_info usage with single account struct wip clean fmt comments wip sdk-tests working extend and fix sdk-test tests, cleanup csdk, anchor program, update macro clean clean clean data_hasher.rs clean derive program tests, renaming, better simulate_cu err handling renames, remove deadcode remove unused discriminator field from config account move variable length field to end of config account struct simplify anchor/borsh serde imports add DEFAULT_DATA_HASH to constants rename CompressionInfo::new() to CompressionInfo::new_decompressed() xtask, remove helper wip remove redundant owner_program param add compile time size() rename pda_account to solana_account fix doctest output.data correctly clean initconfig, updatteconfig accounts struct rename signer_seeds to solana_accounts__signer_seeds move programs to sdk-test dir move to light-compressible-client lint ci wip wip add check load config add sdk-tests to workspace rename sdk-tests program names fix ci fix asserts in tests fmt update pkg json for sdk-tests debug prints for ci in test_indexer fmt make macro robust, lint anchor-discriminator-compat feature for LightDiscriminator lint add test_discriminator test replace actionlint revert actionlint macro: allow flex imports wip fmt update macro add sha hash function support to LightAccount and LightHasher + tests DataHasher macro: explicit validation for sha fix rebase gmt add compressible macro --- .github/workflows/ci-lint.yml | 2 +- .github/workflows/light-examples-tests.yml | 10 +- .github/workflows/sdk-tests.yml | 89 ++ Cargo.lock | 584 +++++--- Cargo.toml | 7 +- ...2E9aWLjY8KuESaqurYpGGhEeJr7eynKrSgXwS.json | 1 + ...Yd46rtjeqDU6CrtT8unqLjPiheggzqhN9YsyB.json | 1 + ...FEXiWnzeMeWkMBzpQN45A95rTJNZmz1Z3pe8R.json | 14 + pnpm-lock.yaml | 10 +- pnpm-workspace.yaml | 1 + program-libs/hasher/src/keccak.rs | 2 + program-libs/hasher/src/lib.rs | 1 + program-libs/hasher/src/poseidon.rs | 2 + program-libs/hasher/src/sha256.rs | 1 + program-tests/package.json | 12 +- program-tests/sdk-anchor-test/Anchor.toml | 2 +- program-tests/sdk-anchor-test/package.json | 4 +- program-tests/sdk-test/src/lib.rs | 49 - program-tests/sdk-test/tests/test.rs | 177 --- .../tests/test_program_owned_trees.rs | 2 +- program-tests/utils/src/test_keypairs.rs | 9 + programs/package.json | 2 +- scripts/format.sh | 4 +- sdk-libs/client/Cargo.toml | 3 + sdk-libs/client/src/indexer/tree_info.rs | 24 + sdk-libs/client/src/lib.rs | 1 + sdk-libs/client/src/rpc/client.rs | 23 +- sdk-libs/light-compressible-client/Cargo.toml | 26 + sdk-libs/light-compressible-client/src/lib.rs | 422 ++++++ sdk-libs/macros/CHANGELOG.md | 106 ++ sdk-libs/macros/Cargo.toml | 6 +- sdk-libs/macros/src/EXAMPLE_USAGE.md | 165 ++ sdk-libs/macros/src/compressible.rs | 645 ++++++++ sdk-libs/macros/src/compressible_derive.rs | 241 +++ sdk-libs/macros/src/cpi_signer.rs | 2 + sdk-libs/macros/src/discriminator.rs | 70 +- sdk-libs/macros/src/hasher/data_hasher.rs | 53 +- sdk-libs/macros/src/hasher/input_validator.rs | 30 + sdk-libs/macros/src/hasher/light_hasher.rs | 317 +++- sdk-libs/macros/src/hasher/mod.rs | 2 +- sdk-libs/macros/src/hasher/to_byte_array.rs | 63 +- sdk-libs/macros/src/lib.rs | 341 +++-- sdk-libs/macros/src/native_compressible.rs | 524 +++++++ sdk-libs/program-test/Cargo.toml | 1 + .../program-test/src/accounts/initialize.rs | 29 +- .../src/accounts/test_accounts.rs | 52 +- .../src/accounts/test_keypairs.rs | 35 + .../program-test/src/indexer/test_indexer.rs | 17 +- sdk-libs/program-test/src/lib.rs | 5 +- .../src/program_test/compressible_setup.rs | 161 ++ sdk-libs/program-test/src/program_test/mod.rs | 2 + sdk-libs/program-test/src/utils/mod.rs | 1 + sdk-libs/program-test/src/utils/simulation.rs | 36 + sdk-libs/sdk-types/src/constants.rs | 5 + sdk-libs/sdk/Cargo.toml | 6 + sdk-libs/sdk/src/account.rs | 137 +- .../sdk/src/compressible/compress_account.rs | 163 ++ .../compressible/compress_account_on_init.rs | 373 +++++ .../sdk/src/compressible/compression_info.rs | 91 ++ sdk-libs/sdk/src/compressible/config.rs | 478 ++++++ .../src/compressible/decompress_idempotent.rs | 152 ++ sdk-libs/sdk/src/compressible/mod.rs | 25 + sdk-libs/sdk/src/error.rs | 8 + sdk-libs/sdk/src/lib.rs | 21 +- .../anchor-compressible-derived/Cargo.toml | 46 + .../anchor-compressible-derived/README.md | 278 ++++ .../anchor-compressible-derived/Xargo.toml | 2 + .../src/constraints.rs | 27 + .../anchor-compressible-derived/src/lib.rs | 276 ++++ .../anchor-compressible-derived/src/state.rs | 32 + .../tests/test_decompress_multiple.rs | 1164 +++++++++++++++ sdk-tests/anchor-compressible/CONFIG.md | 94 ++ sdk-tests/anchor-compressible/Cargo.toml | 44 + sdk-tests/anchor-compressible/Xargo.toml | 2 + sdk-tests/anchor-compressible/src/lib.rs | 772 ++++++++++ .../anchor-compressible/tests/test_config.rs | 628 ++++++++ .../tests/test_decompress_multiple.rs | 1324 +++++++++++++++++ .../tests/test_discriminator.rs | 18 + .../tests/test_instruction_builders.rs | 374 +++++ .../native-compressible}/Cargo.toml | 22 +- .../native-compressible}/Xargo.toml | 0 .../src/compress_dynamic_pda.rs | 85 ++ .../native-compressible/src/create_config.rs | 67 + .../src/create_dynamic_pda.rs | 142 ++ .../native-compressible}/src/create_pda.rs | 12 +- .../src/decompress_dynamic_pda.rs | 176 +++ sdk-tests/native-compressible/src/lib.rs | 284 ++++ .../native-compressible/src/update_config.rs | 37 + .../native-compressible}/src/update_pda.rs | 7 +- .../tests/test_compressible_flow.rs | 390 +++++ .../native-compressible/tests/test_config.rs | 160 ++ sdk-tests/package.json | 29 + xtask/Cargo.toml | 1 + xtask/src/create_batch_state_tree.rs | 16 +- xtask/src/new_deployment.rs | 3 + 95 files changed, 11678 insertions(+), 682 deletions(-) create mode 100644 .github/workflows/sdk-tests.yml create mode 100644 cli/accounts/batch_state_merkle_tree_2_2Yb3fGo2E9aWLjY8KuESaqurYpGGhEeJr7eynKrSgXwS.json create mode 100644 cli/accounts/batched_output_queue_2_12wJT3xYd46rtjeqDU6CrtT8unqLjPiheggzqhN9YsyB.json create mode 100644 cli/accounts/cpi_context_batched_2_HwtjxDvFEXiWnzeMeWkMBzpQN45A95rTJNZmz1Z3pe8R.json delete mode 100644 program-tests/sdk-test/src/lib.rs delete mode 100644 program-tests/sdk-test/tests/test.rs create mode 100644 sdk-libs/light-compressible-client/Cargo.toml create mode 100644 sdk-libs/light-compressible-client/src/lib.rs create mode 100644 sdk-libs/macros/CHANGELOG.md create mode 100644 sdk-libs/macros/src/EXAMPLE_USAGE.md create mode 100644 sdk-libs/macros/src/compressible.rs create mode 100644 sdk-libs/macros/src/compressible_derive.rs create mode 100644 sdk-libs/macros/src/native_compressible.rs create mode 100644 sdk-libs/program-test/src/program_test/compressible_setup.rs create mode 100644 sdk-libs/program-test/src/utils/simulation.rs create mode 100644 sdk-libs/sdk/src/compressible/compress_account.rs create mode 100644 sdk-libs/sdk/src/compressible/compress_account_on_init.rs create mode 100644 sdk-libs/sdk/src/compressible/compression_info.rs create mode 100644 sdk-libs/sdk/src/compressible/config.rs create mode 100644 sdk-libs/sdk/src/compressible/decompress_idempotent.rs create mode 100644 sdk-libs/sdk/src/compressible/mod.rs create mode 100644 sdk-tests/anchor-compressible-derived/Cargo.toml create mode 100644 sdk-tests/anchor-compressible-derived/README.md create mode 100644 sdk-tests/anchor-compressible-derived/Xargo.toml create mode 100644 sdk-tests/anchor-compressible-derived/src/constraints.rs create mode 100644 sdk-tests/anchor-compressible-derived/src/lib.rs create mode 100644 sdk-tests/anchor-compressible-derived/src/state.rs create mode 100644 sdk-tests/anchor-compressible-derived/tests/test_decompress_multiple.rs create mode 100644 sdk-tests/anchor-compressible/CONFIG.md create mode 100644 sdk-tests/anchor-compressible/Cargo.toml create mode 100644 sdk-tests/anchor-compressible/Xargo.toml create mode 100644 sdk-tests/anchor-compressible/src/lib.rs create mode 100644 sdk-tests/anchor-compressible/tests/test_config.rs create mode 100644 sdk-tests/anchor-compressible/tests/test_decompress_multiple.rs create mode 100644 sdk-tests/anchor-compressible/tests/test_discriminator.rs create mode 100644 sdk-tests/anchor-compressible/tests/test_instruction_builders.rs rename {program-tests/sdk-test => sdk-tests/native-compressible}/Cargo.toml (52%) rename {program-tests/sdk-test => sdk-tests/native-compressible}/Xargo.toml (100%) create mode 100644 sdk-tests/native-compressible/src/compress_dynamic_pda.rs create mode 100644 sdk-tests/native-compressible/src/create_config.rs create mode 100644 sdk-tests/native-compressible/src/create_dynamic_pda.rs rename {program-tests/sdk-test => sdk-tests/native-compressible}/src/create_pda.rs (90%) create mode 100644 sdk-tests/native-compressible/src/decompress_dynamic_pda.rs create mode 100644 sdk-tests/native-compressible/src/lib.rs create mode 100644 sdk-tests/native-compressible/src/update_config.rs rename {program-tests/sdk-test => sdk-tests/native-compressible}/src/update_pda.rs (92%) create mode 100644 sdk-tests/native-compressible/tests/test_compressible_flow.rs create mode 100644 sdk-tests/native-compressible/tests/test_config.rs create mode 100644 sdk-tests/package.json diff --git a/.github/workflows/ci-lint.yml b/.github/workflows/ci-lint.yml index 0bc791433a..0f0c455723 100644 --- a/.github/workflows/ci-lint.yml +++ b/.github/workflows/ci-lint.yml @@ -12,4 +12,4 @@ jobs: shell: bash - name: Check workflow files run: ${{ steps.get_actionlint.outputs.executable }} -color - shell: bash \ No newline at end of file + shell: bash diff --git a/.github/workflows/light-examples-tests.yml b/.github/workflows/light-examples-tests.yml index ea79b3fd81..8ec47ab8cc 100644 --- a/.github/workflows/light-examples-tests.yml +++ b/.github/workflows/light-examples-tests.yml @@ -4,12 +4,16 @@ on: - main paths: - "examples/**" + - "program-tests/sdk-anchor-test/**" + - "program-tests/sdk-pinocchio-test/**" - "sdk-libs/**" pull_request: branches: - "*" paths: - "examples/**" + - "program-tests/sdk-anchor-test/**" + - "program-tests/sdk-pinocchio-test/**" - "sdk-libs/**" types: - opened @@ -24,8 +28,8 @@ concurrency: cancel-in-progress: true jobs: - system-programs: - name: system-programs + examples-tests: + name: examples-tests if: github.event.pull_request.draft == false runs-on: ubuntu-latest timeout-minutes: 60 @@ -47,8 +51,6 @@ jobs: strategy: matrix: include: - - program: sdk-test-program - sub-tests: '["cargo-test-sbf -p sdk-test"]' - program: sdk-anchor-test-program sub-tests: '["cargo-test-sbf -p sdk-anchor-test", "cargo-test-sbf -p sdk-pinocchio-test"]' diff --git a/.github/workflows/sdk-tests.yml b/.github/workflows/sdk-tests.yml new file mode 100644 index 0000000000..852a15fee7 --- /dev/null +++ b/.github/workflows/sdk-tests.yml @@ -0,0 +1,89 @@ +on: + push: + branches: + - main + paths: + - "sdk-tests/**" + - "sdk-libs/**" + - "program-libs/**" + - ".github/workflows/sdk-tests.yml" + pull_request: + branches: + - "*" + paths: + - "sdk-tests/**" + - "sdk-libs/**" + - "program-libs/**" + - ".github/workflows/sdk-tests.yml" + types: + - opened + - synchronize + - reopened + - ready_for_review + +name: sdk-tests + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + sdk-tests: + name: sdk-tests + if: github.event.pull_request.draft == false + runs-on: warp-ubuntu-latest-x64-4x + timeout-minutes: 60 + + services: + redis: + image: redis:8.0.1 + ports: + - 6379:6379 + options: >- + --health-cmd "redis-cli ping" + --health-interval 10s + --health-timeout 5s + --health-retries 5 + + env: + REDIS_URL: redis://localhost:6379 + RUST_MIN_STACK: 8388608 + + steps: + - name: Checkout sources + uses: actions/checkout@v4 + + - name: Setup and build + uses: ./.github/actions/setup-and-build + with: + skip-components: "redis" + + - name: Build CLI + run: | + source ./scripts/devenv.sh + npx nx build @lightprotocol/zk-compression-cli + + - name: Build core programs + run: | + source ./scripts/devenv.sh + npx nx build @lightprotocol/programs + + - name: Build and test all sdk-tests programs + run: | + source ./scripts/devenv.sh + # Increase stack size for SBF compilation to avoid regex_automata stack overflow + export RUST_MIN_STACK=16777216 + # Remove -D warnings flag for SBF compilation to avoid compilation issues + export RUSTFLAGS="" + + echo "Building and testing all sdk-tests programs sequentially..." + # Build and test each program one by one to ensure .so files exist + + echo "Building and testing native-compressible" + cargo-test-sbf -p native-compressible + + echo "Building and testing anchor-compressible" + cargo-test-sbf -p anchor-compressible + + echo "Building and testing anchor-compressible-derived" + cargo-test-sbf -p anchor-compressible-derived diff --git a/Cargo.lock b/Cargo.lock index b6bf893a0f..5d1f0bf365 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -150,7 +150,7 @@ version = "1.1.0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -250,6 +250,48 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "anchor-compressible" +version = "0.1.0" +dependencies = [ + "anchor-lang", + "borsh 0.10.4", + "light-client", + "light-compressed-account", + "light-compressible-client", + "light-hasher", + "light-macros", + "light-program-test", + "light-sdk", + "light-sdk-types", + "light-test-utils", + "solana-logger", + "solana-program", + "solana-sdk", + "tokio", +] + +[[package]] +name = "anchor-compressible-derived" +version = "0.1.0" +dependencies = [ + "anchor-lang", + "borsh 0.10.4", + "light-client", + "light-compressed-account", + "light-compressible-client", + "light-hasher", + "light-macros", + "light-program-test", + "light-sdk", + "light-sdk-macros", + "light-sdk-types", + "light-test-utils", + "solana-program", + "solana-sdk", + "tokio", +] + [[package]] name = "anchor-derive-accounts" version = "0.31.1" @@ -565,7 +607,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62945a2f7e6de02a31fe400aa489f0e0f5b2502e69f95f853adb82a96c7a6b60" dependencies = [ "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -591,7 +633,7 @@ dependencies = [ "num-traits", "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -666,7 +708,7 @@ checksum = "213888f660fddcca0d257e88e54ac05bca01885f258ccdf695bafd77031bb69d" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -760,9 +802,9 @@ dependencies = [ [[package]] name = "async-compression" -version = "0.4.24" +version = "0.4.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d615619615a650c571269c00dca41db04b9210037fa76ed8239f70404ab56985" +checksum = "ddb939d66e4ae03cee6091612804ba446b12878410cfa17f785f4dd67d4014e8" dependencies = [ "brotli", "flate2", @@ -802,7 +844,7 @@ checksum = "c7c24de15d275a1ecfd47a380fb4d5ec9bfe0933f309ed5e705b775596a3574d" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -813,7 +855,7 @@ checksum = "e539d3fca749fcee5236ab05e93a52867dd549cc157c8cb7f99595f3cedffdb5" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -835,9 +877,9 @@ dependencies = [ [[package]] name = "autocfg" -version = "1.4.0" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" [[package]] name = "backtrace" @@ -1001,7 +1043,7 @@ dependencies = [ "proc-macro-crate 3.3.0", "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -1058,9 +1100,9 @@ dependencies = [ [[package]] name = "bumpalo" -version = "3.18.1" +version = "3.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "793db76d6187cd04dff33004d8e6c9cc4e05cd330500379d2394209271b4aeee" +checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43" [[package]] name = "bv" @@ -1089,13 +1131,13 @@ dependencies = [ [[package]] name = "bytemuck_derive" -version = "1.9.3" +version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ecc273b49b3205b83d648f0690daa588925572cc5063745bfe547fe7ec8e1a1" +checksum = "441473f2b4b0459a68628c744bc61d23e730fb00128b841d30fa4bb3972257e4" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -1132,9 +1174,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.2.27" +version = "1.2.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d487aa071b5f64da6f19a3e848e3578944b726ee5a4854b82172f02aa876bfdc" +checksum = "deec109607ca693028562ed836a5f1c4b8bd77755c4e132fc5ce11b0b6211ae7" dependencies = [ "jobserver", "libc", @@ -1167,7 +1209,7 @@ checksum = "45565fc9416b9896014f5732ac776f810ee53a66730c17e4020c3ec064a8f88f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -1212,9 +1254,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.40" +version = "4.5.41" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40b6887a1d8685cebccf115538db5c0efe625ccac9696ad45c409d96566e910f" +checksum = "be92d32e80243a54711e5d7ce823c35c41c9d929dc4ab58e1276f625841aadf9" dependencies = [ "clap_builder", "clap_derive", @@ -1222,9 +1264,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.40" +version = "4.5.41" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0c66c08ce9f0c698cbce5c0279d0bb6ac936d8674174fe48f736533b964f59e" +checksum = "707eab41e9622f9139419d573eca0900137718000c517d47da73045f54331c3d" dependencies = [ "anstream", "anstyle", @@ -1234,14 +1276,14 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.40" +version = "4.5.41" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2c7947ae4cc3d851207c1adb5b5e260ff0cca11446b1d6d1423788e442257ce" +checksum = "ef4f52386a59ca4c860f7393bcf8abd8dfd91ecccc0f774635ff68e92eeef491" dependencies = [ "heck 0.5.0", "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -1434,9 +1476,9 @@ dependencies = [ [[package]] name = "crc32fast" -version = "1.4.2" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" +checksum = "9481c1c90cbf2ac953f07c8d4a58aa3945c425b7185c9154d67a65e4230da511" dependencies = [ "cfg-if", ] @@ -1490,9 +1532,9 @@ checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" [[package]] name = "crunchy" -version = "0.2.3" +version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43da5946c66ffcc7745f48db692ffbb10a83bfe0afd96235c5c2a4fb23994929" +checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" [[package]] name = "crypto-common" @@ -1563,7 +1605,7 @@ checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -1587,7 +1629,7 @@ dependencies = [ "proc-macro2", "quote", "strsim 0.11.1", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -1598,7 +1640,7 @@ checksum = "fc34b93ccb385b40dc71c6fceac4b2ad23662c7eeb248cf10d529b7e055b6ead" dependencies = [ "darling_core", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -1757,7 +1799,7 @@ checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -1780,7 +1822,7 @@ checksum = "a6cbae11b3de8fce2a456e8ea3dada226b35fe791f0dc1d360c0941f0bb681f3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -1872,7 +1914,7 @@ dependencies = [ "enum-ordinalize 4.3.0", "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -1913,7 +1955,7 @@ checksum = "a1ab991c1362ac86c61ab6f556cff143daa22e5a15e4e189df818b2fd19fe65b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -1926,7 +1968,7 @@ dependencies = [ "num-traits", "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -1946,7 +1988,7 @@ checksum = "0d28318a75d4aead5c4db25382e8ef717932d0346600cacae6357eb5941bc5ff" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -1993,12 +2035,12 @@ checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" [[package]] name = "errno" -version = "0.3.12" +version = "0.3.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cea14ef9355e3beab063703aa9dab15afd25f0667c341310c1e5274bb1d0da18" +checksum = "778e2ac28f6c47af28e4907f13ffd1e1ddbd400980a9abd7c8df189bf578a5ad" dependencies = [ "libc", - "windows-sys 0.59.0", + "windows-sys 0.60.2", ] [[package]] @@ -2035,7 +2077,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "27cea6e7f512d43b098939ff4d5a5d6fe3db07971e1d05176fe26c642d33f5b8" dependencies = [ "getrandom 0.3.3", - "rand 0.9.1", + "rand 0.9.2", "siphasher 1.0.1", "wide", ] @@ -2116,7 +2158,7 @@ dependencies = [ "bb8", "borsh 0.10.4", "bs58", - "clap 4.5.40", + "clap 4.5.41", "create-address-test-program", "dashmap 6.1.0", "dotenvy", @@ -2143,7 +2185,7 @@ dependencies = [ "photon-api", "prometheus", "rand 0.8.5", - "reqwest 0.12.20", + "reqwest 0.12.22", "scopeguard", "serde", "serde_json", @@ -2189,7 +2231,7 @@ dependencies = [ "num-bigint 0.4.6", "num-traits", "rand 0.8.5", - "reqwest 0.12.20", + "reqwest 0.12.22", "serde", "serde_json", "solana-sdk", @@ -2269,7 +2311,7 @@ checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -2417,7 +2459,7 @@ dependencies = [ "parking_lot", "portable-atomic", "quanta", - "rand 0.9.1", + "rand 0.9.2", "smallvec", "spinning_top", "web-time", @@ -2440,9 +2482,9 @@ dependencies = [ [[package]] name = "h2" -version = "0.3.26" +version = "0.3.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81fe527a889e1532da5c525686d96d4c2e74cdd345badf8dfef9f6b39dd5f5e8" +checksum = "0beca50380b1fc32983fc1cb4587bfa4bb9e78fc259aad4a0032d2080309222d" dependencies = [ "bytes", "fnv", @@ -2450,7 +2492,7 @@ dependencies = [ "futures-sink", "futures-util", "http 0.2.12", - "indexmap 2.9.0", + "indexmap 2.10.0", "slab", "tokio", "tokio-util 0.7.15", @@ -2459,9 +2501,9 @@ dependencies = [ [[package]] name = "h2" -version = "0.4.10" +version = "0.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9421a676d1b147b16b82c9225157dc629087ef8ec4d5e2960f9437a90dac0a5" +checksum = "17da50a276f1e01e0ba6c029e47b7100754904ee8a278f886546e98575380785" dependencies = [ "atomic-waker", "bytes", @@ -2469,7 +2511,7 @@ dependencies = [ "futures-core", "futures-sink", "http 1.3.1", - "indexmap 2.9.0", + "indexmap 2.10.0", "slab", "tokio", "tokio-util 0.7.15", @@ -2548,6 +2590,12 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "heck" version = "0.5.0" @@ -2708,14 +2756,14 @@ dependencies = [ "futures-channel", "futures-core", "futures-util", - "h2 0.3.26", + "h2 0.3.27", "http 0.2.12", "http-body 0.4.6", "httparse", "httpdate", "itoa", "pin-project-lite", - "socket2", + "socket2 0.5.10", "tokio", "tower-service", "tracing", @@ -2731,7 +2779,7 @@ dependencies = [ "bytes", "futures-channel", "futures-util", - "h2 0.4.10", + "h2 0.4.11", "http 1.3.1", "http-body 1.0.1", "httparse", @@ -2765,12 +2813,12 @@ dependencies = [ "http 1.3.1", "hyper 1.6.0", "hyper-util", - "rustls 0.23.27", + "rustls 0.23.29", "rustls-pki-types", "tokio", "tokio-rustls 0.26.2", "tower-service", - "webpki-roots 1.0.0", + "webpki-roots 1.0.2", ] [[package]] @@ -2804,9 +2852,9 @@ dependencies = [ [[package]] name = "hyper-util" -version = "0.1.14" +version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc2fdfdbff08affe55bb779f33b053aa1fe5dd5b54c257343c17edfa55711bdb" +checksum = "8d9b05277c7e8da2c93a568989bb6207bef0112e8d17df7a6eda4a3cf143bc5e" dependencies = [ "base64 0.22.1", "bytes", @@ -2820,7 +2868,7 @@ dependencies = [ "libc", "percent-encoding", "pin-project-lite", - "socket2", + "socket2 0.6.0", "system-configuration 0.6.1", "tokio", "tower-service", @@ -2978,9 +3026,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.9.0" +version = "2.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cea70ddb795996207ad57735b50c5982d8844f38ba9ee5f1aedcfb708a2aa11e" +checksum = "fe4cd85333e22411419a0bcae1297d25e58c9443848b11dc6a86fefe8c78a661" dependencies = [ "equivalent", "hashbrown 0.15.4", @@ -3009,6 +3057,17 @@ dependencies = [ "generic-array", ] +[[package]] +name = "io-uring" +version = "0.7.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d93587f37623a1a17d94ef2bc9ada592f5465fe7732084ab7beefabe5c77c0c4" +dependencies = [ + "bitflags 2.9.1", + "cfg-if", + "libc", +] + [[package]] name = "ipnet" version = "2.11.0" @@ -3094,7 +3153,7 @@ checksum = "03343451ff899767262ec32146f6d559dd759fdadf42ff0e227c7c48f72594b4" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -3171,15 +3230,15 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" -version = "0.2.173" +version = "0.2.174" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8cfeafaffdbc32176b64fb251369d52ea9f0a8fbc6f8759edffef7b525d64bb" +checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776" [[package]] name = "libredox" -version = "0.1.3" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" +checksum = "4488594b9328dee448adb906d8b126d9b7deb7cf5c22161ee591610bb1be83c0" dependencies = [ "bitflags 2.9.1", "libc", @@ -3307,6 +3366,7 @@ dependencies = [ name = "light-client" version = "0.13.1" dependencies = [ + "anchor-lang", "async-trait", "base64 0.13.1", "borsh 0.10.4", @@ -3391,6 +3451,19 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "light-compressible-client" +version = "0.13.1" +dependencies = [ + "anchor-lang", + "borsh 0.10.4", + "light-client", + "light-sdk", + "solana-instruction", + "solana-pubkey", + "thiserror 2.0.12", +] + [[package]] name = "light-concurrent-merkle-tree" version = "2.1.0" @@ -3488,7 +3561,7 @@ dependencies = [ "bs58", "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -3556,6 +3629,7 @@ dependencies = [ "light-client", "light-compressed-account", "light-compressed-token", + "light-compressible-client", "light-concurrent-merkle-tree", "light-hasher", "light-indexed-array", @@ -3571,7 +3645,7 @@ dependencies = [ "num-traits", "photon-api", "rand 0.8.5", - "reqwest 0.12.20", + "reqwest 0.12.22", "solana-account", "solana-banks-client", "solana-compute-budget", @@ -3628,6 +3702,7 @@ name = "light-sdk" version = "0.13.0" dependencies = [ "anchor-lang", + "arrayvec", "borsh 0.10.4", "light-account-checks", "light-compressed-account", @@ -3638,11 +3713,15 @@ dependencies = [ "light-zero-copy", "num-bigint 0.4.6", "solana-account-info", + "solana-clock", "solana-cpi", "solana-instruction", "solana-msg", "solana-program-error", "solana-pubkey", + "solana-rent", + "solana-system-interface", + "solana-sysvar", "thiserror 2.0.12", ] @@ -3651,6 +3730,7 @@ name = "light-sdk-macros" version = "0.13.0" dependencies = [ "borsh 0.10.4", + "heck 0.4.1", "light-compressed-account", "light-hasher", "light-macros", @@ -3660,7 +3740,7 @@ dependencies = [ "proc-macro2", "quote", "solana-pubkey", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -3777,7 +3857,7 @@ dependencies = [ "num-bigint 0.4.6", "num-traits", "rand 0.8.5", - "reqwest 0.12.20", + "reqwest 0.12.22", "solana-banks-client", "solana-sdk", "spl-token", @@ -3820,7 +3900,7 @@ dependencies = [ "proc-macro2", "quote", "rand 0.8.5", - "syn 2.0.103", + "syn 2.0.104", "trybuild", "zerocopy", ] @@ -3845,7 +3925,7 @@ checksum = "bb7e5f4462f34439adcfcab58099bc7a89c67a17f8240b84a993b8b705c1becb" dependencies = [ "ansi_term", "bincode", - "indexmap 2.9.0", + "indexmap 2.10.0", "itertools 0.14.0", "log", "solana-account", @@ -4027,6 +4107,26 @@ dependencies = [ "version_check", ] +[[package]] +name = "native-compressible" +version = "1.0.0" +dependencies = [ + "borsh 0.10.4", + "light-client", + "light-compressed-account", + "light-compressible-client", + "light-hasher", + "light-macros", + "light-program-test", + "light-sdk", + "light-sdk-types", + "solana-clock", + "solana-program", + "solana-sdk", + "solana-sysvar", + "tokio", +] + [[package]] name = "native-tls" version = "0.2.14" @@ -4150,7 +4250,7 @@ checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -4206,23 +4306,24 @@ dependencies = [ [[package]] name = "num_enum" -version = "0.7.3" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e613fc340b2220f734a8595782c551f1250e969d87d3be1ae0579e8d4065179" +checksum = "a973b4e44ce6cad84ce69d797acf9a044532e4184c4f267913d1b546a0727b7a" dependencies = [ "num_enum_derive", + "rustversion", ] [[package]] name = "num_enum_derive" -version = "0.7.3" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af1844ef2428cc3e1cb900be36181049ef3d3193c63e43026cfe202983b27a56" +checksum = "77e878c846a8abae00dd069496dbe8751b16ac1c3d6bd2a7283a938e8228f90d" dependencies = [ "proc-macro-crate 3.3.0", "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -4290,7 +4391,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -4434,7 +4535,7 @@ dependencies = [ name = "photon-api" version = "0.51.0" dependencies = [ - "reqwest 0.12.20", + "reqwest 0.12.22", "serde", "serde_derive", "serde_json", @@ -4460,7 +4561,7 @@ checksum = "6e918e4ff8c4549eb882f14b3a4bc8c8bc93de829416eacf579f1207a8fbf861" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -4566,12 +4667,12 @@ checksum = "c6fa0831dd7cc608c38a5e323422a0077678fa5744aa2be4ad91c4ece8eec8d5" [[package]] name = "prettyplease" -version = "0.2.34" +version = "0.2.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6837b9e10d61f45f987d50808f83d1ee3d206c66acf650c3e4ae2e1f6ddedf55" +checksum = "061c1221631e079b26479d25bbf2275bfe5917ae8419cd7e34f13bfc2aa7539a" dependencies = [ "proc-macro2", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -4611,7 +4712,7 @@ dependencies = [ "proc-macro-error-attr2", "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -4661,7 +4762,7 @@ checksum = "9e2e25ee72f5b24d773cae88422baddefff7714f97aab68d96fe2b6fc4a28fb2" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -4691,8 +4792,8 @@ dependencies = [ "quinn-proto", "quinn-udp", "rustc-hash 2.1.1", - "rustls 0.23.27", - "socket2", + "rustls 0.23.29", + "socket2 0.5.10", "thiserror 2.0.12", "tokio", "tracing", @@ -4709,10 +4810,10 @@ dependencies = [ "fastbloom", "getrandom 0.3.3", "lru-slab", - "rand 0.9.1", + "rand 0.9.2", "ring", "rustc-hash 2.1.1", - "rustls 0.23.27", + "rustls 0.23.29", "rustls-pki-types", "rustls-platform-verifier", "slab", @@ -4724,14 +4825,14 @@ dependencies = [ [[package]] name = "quinn-udp" -version = "0.5.12" +version = "0.5.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee4e529991f949c5e25755532370b8af5d114acae52326361d68d47af64aa842" +checksum = "fcebb1209ee276352ef14ff8732e24cc2b02bbac986cd74a4c81bcb2f9881970" dependencies = [ "cfg_aliases", "libc", "once_cell", - "socket2", + "socket2 0.5.10", "tracing", "windows-sys 0.59.0", ] @@ -4747,9 +4848,9 @@ dependencies = [ [[package]] name = "r-efi" -version = "5.2.0" +version = "5.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" [[package]] name = "radium" @@ -4783,9 +4884,9 @@ dependencies = [ [[package]] name = "rand" -version = "0.9.1" +version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9fbfd9d094a40bf3ae768db9361049ace4c0e04a4fd6b359518bd7b73a73dd97" +checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1" dependencies = [ "rand_chacha 0.9.0", "rand_core 0.9.3", @@ -4888,9 +4989,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.13" +version = "0.5.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d04b7d0ee6b4a0207a0a7adb104d23ecb0b47d6beae7152d0fa34b692b29fd6" +checksum = "7e8af0dde094006011e6a740d4879319439489813bd0bcdc7d821beaeeff48ec" dependencies = [ "bitflags 2.9.1", ] @@ -4934,7 +5035,7 @@ checksum = "1165225c21bff1f3bbce98f5a1f889949bc902d3575308cc7b0de30b4f6d27c7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -5014,7 +5115,7 @@ dependencies = [ "encoding_rs", "futures-core", "futures-util", - "h2 0.3.26", + "h2 0.3.27", "http 0.2.12", "http-body 0.4.6", "hyper 0.14.32", @@ -5051,9 +5152,9 @@ dependencies = [ [[package]] name = "reqwest" -version = "0.12.20" +version = "0.12.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eabf4c97d9130e2bf606614eb937e86edac8292eaa6f422f995d7e8de1eb1813" +checksum = "cbc931937e6ca3a06e3b6c0aa7841849b160a90351d6ab467a8b9b9959767531" dependencies = [ "base64 0.22.1", "bytes", @@ -5061,7 +5162,7 @@ dependencies = [ "futures-channel", "futures-core", "futures-util", - "h2 0.4.10", + "h2 0.4.11", "http 1.3.1", "http-body 1.0.1", "http-body-util", @@ -5077,7 +5178,7 @@ dependencies = [ "percent-encoding", "pin-project-lite", "quinn", - "rustls 0.23.27", + "rustls 0.23.29", "rustls-pki-types", "serde", "serde_json", @@ -5093,7 +5194,7 @@ dependencies = [ "wasm-bindgen", "wasm-bindgen-futures", "web-sys", - "webpki-roots 1.0.0", + "webpki-roots 1.0.2", ] [[package]] @@ -5184,15 +5285,15 @@ dependencies = [ [[package]] name = "rustix" -version = "1.0.7" +version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c71e83d6afe7ff64890ec6b71d6a69bb8a610ab78ce364b3352876bb4c801266" +checksum = "11181fbabf243db407ef8df94a6ce0b2f9a733bd8be4ad02b4eda9602296cac8" dependencies = [ "bitflags 2.9.1", "errno", "libc", "linux-raw-sys", - "windows-sys 0.59.0", + "windows-sys 0.60.2", ] [[package]] @@ -5209,14 +5310,14 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.27" +version = "0.23.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "730944ca083c1c233a75c09f199e973ca499344a2b7ba9e755c457e86fb4a321" +checksum = "2491382039b29b9b11ff08b76ff6c97cf287671dbb74f0be44bda389fffe9bd1" dependencies = [ "once_cell", "ring", "rustls-pki-types", - "rustls-webpki 0.103.3", + "rustls-webpki 0.103.4", "subtle", "zeroize", ] @@ -5263,10 +5364,10 @@ dependencies = [ "jni", "log", "once_cell", - "rustls 0.23.27", + "rustls 0.23.29", "rustls-native-certs", "rustls-platform-verifier-android", - "rustls-webpki 0.103.3", + "rustls-webpki 0.103.4", "security-framework 3.2.0", "security-framework-sys", "webpki-root-certs 0.26.11", @@ -5291,9 +5392,9 @@ dependencies = [ [[package]] name = "rustls-webpki" -version = "0.103.3" +version = "0.103.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e4a72fe2bcf7a6ac6fd7d0b9e5cb68aeb7d4c0a0271730218b3e92d43b4eb435" +checksum = "0a17884ae0c1b773f1ccd2bd4a8c72f16da897310a98b0e84bf349ad5ead92fc" dependencies = [ "ring", "rustls-pki-types", @@ -5360,6 +5461,18 @@ dependencies = [ "serde_json", ] +[[package]] +name = "schemars" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82d20c4491bc164fa2f6c5d44565947a52ad80b9505d8e36f8d54c27c739fcd0" +dependencies = [ + "dyn-clone", + "ref-cast", + "serde", + "serde_json", +] + [[package]] name = "scoped-tls" version = "1.0.1" @@ -5384,9 +5497,9 @@ dependencies = [ [[package]] name = "sdd" -version = "3.0.8" +version = "3.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "584e070911c7017da6cb2eb0788d09f43d789029b5877d3e5ecc8acf86ceee21" +checksum = "490dcfcbfef26be6800d11870ff2df8774fa6e86d047e3e8c8a76b25655e41ca" [[package]] name = "sdk-anchor-test" @@ -5422,22 +5535,6 @@ dependencies = [ "tokio", ] -[[package]] -name = "sdk-test" -version = "1.0.0" -dependencies = [ - "borsh 0.10.4", - "light-compressed-account", - "light-hasher", - "light-macros", - "light-program-test", - "light-sdk", - "light-sdk-types", - "solana-program", - "solana-sdk", - "tokio", -] - [[package]] name = "security-framework" version = "2.11.1" @@ -5515,14 +5612,14 @@ checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] name = "serde_json" -version = "1.0.140" +version = "1.0.141" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373" +checksum = "30b9eff21ebe718216c6ec64e1d9ac57087aad11efc64e32002bce4a0d4c03d3" dependencies = [ "itoa", "memchr", @@ -5539,6 +5636,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40734c41988f7306bb04f0ecf60ec0f3f1caa34290e4e8ea471dcd3346483b83" +dependencies = [ + "serde", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" @@ -5553,16 +5659,17 @@ dependencies = [ [[package]] name = "serde_with" -version = "3.13.0" +version = "3.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf65a400f8f66fb7b0552869ad70157166676db75ed8181f8104ea91cf9d0b42" +checksum = "f2c45cd61fefa9db6f254525d46e392b852e0e61d9a1fd36e5bd183450a556d5" dependencies = [ "base64 0.22.1", "chrono", "hex", "indexmap 1.9.3", - "indexmap 2.9.0", - "schemars", + "indexmap 2.10.0", + "schemars 0.9.0", + "schemars 1.0.4", "serde", "serde_derive", "serde_json", @@ -5572,14 +5679,14 @@ dependencies = [ [[package]] name = "serde_with_macros" -version = "3.13.0" +version = "3.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81679d9ed988d5e9a5e6531dc3f2c28efbd639cbd1dfb628df08edea6004da77" +checksum = "de90945e6565ce0d9a25098082ed4ee4002e047cb59892c318d66821e14bb30f" dependencies = [ "darling", "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -5588,7 +5695,7 @@ version = "0.9.34+deprecated" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47" dependencies = [ - "indexmap 2.9.0", + "indexmap 2.10.0", "itoa", "ryu", "serde", @@ -5617,7 +5724,7 @@ checksum = "5d69265a08751de7844521fd15003ae0a888e035773ba05695c5c759a6f89eef" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -5725,12 +5832,9 @@ checksum = "56199f7ddabf13fe5074ce809e7d3f42b42ae711800501b5b16ea82ad029c39d" [[package]] name = "slab" -version = "0.4.9" +version = "0.4.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" -dependencies = [ - "autocfg", -] +checksum = "04dc19736151f35336d325007ac991178d504a119863a2fcb3758cdb5e52c50d" [[package]] name = "smallvec" @@ -5748,6 +5852,16 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "socket2" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "233504af464074f9d066d7b5416c5f9b894a5862a6506e306f7b816cdd6f1807" +dependencies = [ + "libc", + "windows-sys 0.59.0", +] + [[package]] name = "solana-account" version = "2.2.1" @@ -6166,7 +6280,7 @@ dependencies = [ "dashmap 5.5.3", "futures", "futures-util", - "indexmap 2.9.0", + "indexmap 2.10.0", "indicatif", "log", "quinn", @@ -6344,7 +6458,7 @@ dependencies = [ "bincode", "crossbeam-channel", "futures-util", - "indexmap 2.9.0", + "indexmap 2.10.0", "log", "rand 0.8.5", "rayon", @@ -6891,7 +7005,7 @@ dependencies = [ "rand 0.8.5", "serde", "serde_derive", - "socket2", + "socket2 0.5.10", "solana-serde", "tokio", "url", @@ -7296,7 +7410,7 @@ dependencies = [ "log", "quinn", "quinn-proto", - "rustls 0.23.27", + "rustls 0.23.29", "solana-connection-cache", "solana-keypair", "solana-measure", @@ -7616,7 +7730,7 @@ dependencies = [ "bs58", "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -7875,7 +7989,7 @@ dependencies = [ "futures-util", "governor 0.6.3", "histogram", - "indexmap 2.9.0", + "indexmap 2.10.0", "itertools 0.12.1", "libc", "log", @@ -7885,9 +7999,9 @@ dependencies = [ "quinn", "quinn-proto", "rand 0.8.5", - "rustls 0.23.27", + "rustls 0.23.29", "smallvec", - "socket2", + "socket2 0.5.10", "solana-keypair", "solana-measure", "solana-metrics", @@ -8078,7 +8192,7 @@ version = "2.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec21c6c242ee93642aa50b829f5727470cdbdf6b461fb7323fe4bc31d1b54c08" dependencies = [ - "rustls 0.23.27", + "rustls 0.23.29", "solana-keypair", "solana-pubkey", "solana-signer", @@ -8094,7 +8208,7 @@ dependencies = [ "async-trait", "bincode", "futures-util", - "indexmap 2.9.0", + "indexmap 2.10.0", "indicatif", "log", "rayon", @@ -8527,7 +8641,7 @@ checksum = "d9e8418ea6269dcfb01c712f0444d2c75542c04448b480e87de59d2865edc750" dependencies = [ "quote", "spl-discriminator-syn", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -8539,7 +8653,7 @@ dependencies = [ "proc-macro2", "quote", "sha2 0.10.9", - "syn 2.0.103", + "syn 2.0.104", "thiserror 1.0.69", ] @@ -8612,7 +8726,7 @@ dependencies = [ "proc-macro2", "quote", "sha2 0.10.9", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -8882,9 +8996,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.103" +version = "2.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e4307e30089d6fd6aff212f2da3a1f9e32f3223b1f010fb09b7c95f90f3ca1e8" +checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40" dependencies = [ "proc-macro2", "quote", @@ -8926,7 +9040,7 @@ checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -9077,7 +9191,7 @@ dependencies = [ "proc-macro-error2", "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -9202,7 +9316,7 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -9213,7 +9327,7 @@ checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -9302,18 +9416,20 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.45.1" +version = "1.46.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75ef51a33ef1da925cea3e4eb122833cb377c61439ca401b770f54902b806779" +checksum = "0cc3a2344dafbe23a245241fe8b09735b521110d30fcefbbd5feb1797ca35d17" dependencies = [ "backtrace", "bytes", + "io-uring", "libc", "mio", "parking_lot", "pin-project-lite", "signal-hook-registry", - "socket2", + "slab", + "socket2 0.5.10", "tokio-macros", "windows-sys 0.52.0", ] @@ -9326,7 +9442,7 @@ checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -9355,7 +9471,7 @@ version = "0.26.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e727b36a1a0e8b74c376ac2211e40c2c8af09fb4013c60d910495810f008e9b" dependencies = [ - "rustls 0.23.27", + "rustls 0.23.29", "tokio", ] @@ -9457,11 +9573,26 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc1beb996b9d83529a9e75c17a1686767d148d70663143c7854d8b4a09ced362" dependencies = [ "serde", - "serde_spanned", - "toml_datetime", + "serde_spanned 0.6.9", + "toml_datetime 0.6.11", "toml_edit", ] +[[package]] +name = "toml" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed0aee96c12fa71097902e0bb061a5e1ebd766a6636bb605ba401c45c1650eac" +dependencies = [ + "indexmap 2.10.0", + "serde", + "serde_spanned 1.0.0", + "toml_datetime 0.7.0", + "toml_parser", + "toml_writer", + "winnow", +] + [[package]] name = "toml_datetime" version = "0.6.11" @@ -9471,26 +9602,50 @@ dependencies = [ "serde", ] +[[package]] +name = "toml_datetime" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bade1c3e902f58d73d3f294cd7f20391c1cb2fbcb643b73566bc773971df91e3" +dependencies = [ + "serde", +] + [[package]] name = "toml_edit" version = "0.22.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a" dependencies = [ - "indexmap 2.9.0", + "indexmap 2.10.0", "serde", - "serde_spanned", - "toml_datetime", + "serde_spanned 0.6.9", + "toml_datetime 0.6.11", "toml_write", "winnow", ] +[[package]] +name = "toml_parser" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97200572db069e74c512a14117b296ba0a80a30123fbbb5aa1f4a348f639ca30" +dependencies = [ + "winnow", +] + [[package]] name = "toml_write" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801" +[[package]] +name = "toml_writer" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcc842091f2def52017664b53082ecbbeb5c7731092bad69d2c63050401dfd64" + [[package]] name = "tower" version = "0.5.2" @@ -9562,13 +9717,13 @@ dependencies = [ [[package]] name = "tracing-attributes" -version = "0.1.29" +version = "0.1.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b1ffbcf9c6f6b99d386e7444eb608ba646ae452a36b39737deb9663b610f662" +checksum = "81383ab64e72a7a8b8e13130c49e3dab29def6d0c7d76a03087b3cf71c5c6903" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -9644,9 +9799,9 @@ checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" [[package]] name = "trybuild" -version = "1.0.105" +version = "1.0.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c9bf9513a2f4aeef5fdac8677d7d349c79fdbcc03b9c86da6e9d254f1e43be2" +checksum = "65af40ad689f2527aebbd37a0a816aea88ff5f774ceabe99de5be02f2f91dae2" dependencies = [ "glob", "serde", @@ -9654,7 +9809,7 @@ dependencies = [ "serde_json", "target-triple", "termcolor", - "toml 0.8.23", + "toml 0.9.2", ] [[package]] @@ -9951,7 +10106,7 @@ dependencies = [ "log", "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", "wasm-bindgen-shared", ] @@ -9986,7 +10141,7 @@ checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -10026,14 +10181,14 @@ version = "0.26.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "75c7f0ef91146ebfb530314f5f1d24528d7f0767efbfd31dce919275413e393e" dependencies = [ - "webpki-root-certs 1.0.0", + "webpki-root-certs 1.0.2", ] [[package]] name = "webpki-root-certs" -version = "1.0.0" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01a83f7e1a9f8712695c03eabe9ed3fbca0feff0152f33f12593e5a6303cb1a4" +checksum = "4e4ffd8df1c57e87c325000a3d6ef93db75279dc3a231125aac571650f22b12a" dependencies = [ "rustls-pki-types", ] @@ -10055,18 +10210,18 @@ checksum = "5f20c57d8d7db6d3b86154206ae5d8fba62dd39573114de97c2cb0578251f8e1" [[package]] name = "webpki-roots" -version = "1.0.0" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2853738d1cc4f2da3a225c18ec6c3721abb31961096e9dbf5ab35fa88b19cfdb" +checksum = "7e8983c3ab33d6fb807cfcdad2491c4ea8cbc8ed839181c7dfd9c67c83e261b2" dependencies = [ "rustls-pki-types", ] [[package]] name = "wide" -version = "0.7.32" +version = "0.7.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41b5576b9a81633f3e8df296ce0063042a73507636cbe956c61133dd7034ab22" +checksum = "0ce5da8ecb62bcd8ec8b7ea19f69a51275e91299be594ea5cc6ef7819e16cd03" dependencies = [ "bytemuck", "safe_arch", @@ -10124,7 +10279,7 @@ checksum = "a47fddd13af08290e67f4acabf4b459f647552718f683a7b415d290ac744a836" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -10135,7 +10290,7 @@ checksum = "bd9211b69f8dcdfa817bfd14bf1c97c9188afa36f4750130fcdf3f400eca9fa8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -10146,9 +10301,9 @@ checksum = "5e6ad25900d524eaabdbbb96d20b4311e1e7ae1699af4fb28c17ae66c80d798a" [[package]] name = "windows-registry" -version = "0.5.2" +version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3bab093bdd303a1240bb99b8aba8ea8a69ee19d34c9e2ef9594e708a4878820" +checksum = "5b8a9ed28765efc97bbc954883f4e6796c33a06546ebafacbabee9696967499e" dependencies = [ "windows-link", "windows-result", @@ -10462,9 +10617,9 @@ checksum = "271414315aff87387382ec3d271b52d7ae78726f5d44ac98b4f4030c91880486" [[package]] name = "winnow" -version = "0.7.11" +version = "0.7.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74c7b26e3480b707944fc872477815d29a8e429d2f93a1ce000f5fa84a15cbcd" +checksum = "f3edebf492c8125044983378ecb5766203ad3b4c2f7a922bd7dd207f6d443e95" dependencies = [ "memchr", ] @@ -10529,7 +10684,8 @@ dependencies = [ "anyhow", "ark-bn254 0.5.0", "ark-ff 0.5.0", - "clap 4.5.40", + "base64 0.13.1", + "clap 4.5.41", "dirs", "groth16-solana", "light-batched-merkle-tree", @@ -10576,28 +10732,28 @@ checksum = "38da3c9736e16c5d3c8c597a9aaa5d1fa565d0532ae05e27c24aa62fb32c0ab6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", "synstructure 0.13.2", ] [[package]] name = "zerocopy" -version = "0.8.25" +version = "0.8.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1702d9583232ddb9174e01bb7c15a2ab8fb1bc6f227aa1233858c351a3ba0cb" +checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f" dependencies = [ "zerocopy-derive", ] [[package]] name = "zerocopy-derive" -version = "0.8.25" +version = "0.8.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28a6e20d751156648aa063f3800b706ee209a32c0b4d9f24be3d980b01be55ef" +checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -10617,7 +10773,7 @@ checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", "synstructure 0.13.2", ] @@ -10638,7 +10794,7 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] @@ -10671,7 +10827,7 @@ checksum = "5b96237efa0c878c64bd89c436f661be4e46b2f3eff1ebb976f7ef2321d2f58f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.103", + "syn 2.0.104", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index ae4693fe8a..cce2fc0ea1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ members = [ "programs/registry", "anchor-programs/system", "sdk-libs/client", + "sdk-libs/light-compressible-client", "sdk-libs/macros", "sdk-libs/sdk", "sdk-libs/sdk-pinocchio", @@ -35,7 +36,6 @@ members = [ "program-tests/system-cpi-v2-test", "program-tests/system-test", "program-tests/sdk-anchor-test/programs/sdk-anchor-test", - "program-tests/sdk-test", "program-tests/sdk-pinocchio-test", "program-tests/create-address-test-program", "program-tests/utils", @@ -44,6 +44,9 @@ members = [ "forester-utils", "forester", "sparse-merkle-tree", + "sdk-tests/anchor-compressible", + "sdk-tests/anchor-compressible-derived", + "sdk-tests/native-compressible", ] resolver = "2" @@ -86,6 +89,7 @@ solana-transaction = { version = "2.2" } solana-transaction-error = { version = "2.2" } solana-hash = { version = "2.2" } solana-clock = { version = "2.2" } +solana-rent = { version = "2.2" } solana-signature = { version = "2.2" } solana-commitment-config = { version = "2.2" } solana-account = { version = "2.2" } @@ -150,6 +154,7 @@ light-indexed-merkle-tree = { version = "2.1.0", path = "program-libs/indexed-me light-concurrent-merkle-tree = { version = "2.1.0", path = "program-libs/concurrent-merkle-tree" } light-sparse-merkle-tree = { version = "0.1.0", path = "sparse-merkle-tree" } light-client = { path = "sdk-libs/client", version = "0.13.1" } +light-compressible-client = { path = "sdk-libs/light-compressible-client", version = "0.13.1" } light-hasher = { path = "program-libs/hasher", version = "3.1.0" } light-macros = { path = "program-libs/macros", version = "2.1.0" } light-merkle-tree-reference = { path = "program-tests/merkle-tree", version = "2.0.0" } diff --git a/cli/accounts/batch_state_merkle_tree_2_2Yb3fGo2E9aWLjY8KuESaqurYpGGhEeJr7eynKrSgXwS.json b/cli/accounts/batch_state_merkle_tree_2_2Yb3fGo2E9aWLjY8KuESaqurYpGGhEeJr7eynKrSgXwS.json new file mode 100644 index 0000000000..d9d7c50e84 --- /dev/null +++ b/cli/accounts/batch_state_merkle_tree_2_2Yb3fGo2E9aWLjY8KuESaqurYpGGhEeJr7eynKrSgXwS.json @@ -0,0 +1 @@ +{"pubkey":"2Yb3fGo2E9aWLjY8KuESaqurYpGGhEeJr7eynKrSgXwS","account":{"lamports":291095040,"data":["QmF0Y2hNdGEDAAAAAAAAAA/Y1EfToz5VLJjxHxd2rjLiDsKHFAg5RA9dMMbnV0jYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABfAAAAAAAAAIgTAAAAAAAA/////////////////////wAAAAAAAAAATy/C0Fr8KxLYTClxCKFxErzKz3N965dup6b5TkvdJtsAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAFAAAAAAAAAABAAAAAgAAAAAAAAAyAAAAAAAAAAoAAAAAAAAAAHECAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAAAAAAAAAHECAAAAAAAyAAAAAAAAAAoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAAAAAAAAAHECAAAAAAAyAAAAAAAAAAoAAAAAAAAAAAAAAAAAAAAyAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA5fn8H5ciLJn71SqM5QGCNQboCywgGwdP3kaAoW+6wQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAABQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=","base64"],"owner":"compr6CUsB5m2jS4Y3831ztGSTnDpnKJTKS95d64XVq","executable":false,"rentEpoch":18446744073709551615,"space":41696}} \ No newline at end of file diff --git a/cli/accounts/batched_output_queue_2_12wJT3xYd46rtjeqDU6CrtT8unqLjPiheggzqhN9YsyB.json b/cli/accounts/batched_output_queue_2_12wJT3xYd46rtjeqDU6CrtT8unqLjPiheggzqhN9YsyB.json new file mode 100644 index 0000000000..0000b8d1b3 --- /dev/null +++ b/cli/accounts/batched_output_queue_2_12wJT3xYd46rtjeqDU6CrtT8unqLjPiheggzqhN9YsyB.json @@ -0,0 +1 @@ +{"pubkey":"12wJT3xYd46rtjeqDU6CrtT8unqLjPiheggzqhN9YsyB","account":{"lamports":29677440,"data":["cXVldWVhY2MP2NRH06M+VSyY8R8Xdq4y4g7ChxQIOUQPXTDG51dI2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAXwAAAAAAAACIEwAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=","base64"],"owner":"compr6CUsB5m2jS4Y3831ztGSTnDpnKJTKS95d64XVq","executable":false,"rentEpoch":18446744073709551615,"space":4136}} \ No newline at end of file diff --git a/cli/accounts/cpi_context_batched_2_HwtjxDvFEXiWnzeMeWkMBzpQN45A95rTJNZmz1Z3pe8R.json b/cli/accounts/cpi_context_batched_2_HwtjxDvFEXiWnzeMeWkMBzpQN45A95rTJNZmz1Z3pe8R.json new file mode 100644 index 0000000000..c226613fd6 --- /dev/null +++ b/cli/accounts/cpi_context_batched_2_HwtjxDvFEXiWnzeMeWkMBzpQN45A95rTJNZmz1Z3pe8R.json @@ -0,0 +1,14 @@ +{ + "account": { + "data": [ + "FhSV2krMgKYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABbzMBKdt4AzervcnTq70mQaynPIcOKwjsz2UC4spE/VAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==", + "base64" + ], + "executable": false, + "lamports": 143487360, + "owner": "SySTEM1eSU2p4BGQfQpimFEWWSC1XDFeun3Nqzz3rT7", + "rentEpoch": 18446744073709551615, + "space": 20488 + }, + "pubkey": "HwtjxDvFEXiWnzeMeWkMBzpQN45A95rTJNZmz1Z3pe8R" +} \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7acade393e..fe72a67fc8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -479,6 +479,8 @@ importers: programs: {} + sdk-tests: {} + tsconfig: {} packages: @@ -4520,8 +4522,8 @@ packages: nanoassert@2.0.0: resolution: {integrity: sha512-7vO7n28+aYO4J+8w96AzhmU8G+Y/xpPDJz/se19ICsqj/momRbb9mh9ZUtkoJ5X3nTnPdhEJyc0qnM6yAsHBaA==} - nanoid@3.3.8: - resolution: {integrity: sha512-WNLf5Sd8oZxOm+TzppcYk8gVOgP+l58xNy58D0nbUnOxOWRWvlcCV4kUF7ltmI6PsrLl/BgKEyS4mqsGChFN0w==} + nanoid@3.3.11: + resolution: {integrity: sha512-N8SpfPUnUp1bK+PMYW8qSWdl9U+wwNWI4QKxOYDy9JAro3WMX7p2OeVRF9v+347pnakNevPmiHhNmZ2HbFA76w==} engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1} hasBin: true @@ -11251,7 +11253,7 @@ snapshots: nanoassert@2.0.0: {} - nanoid@3.3.8: {} + nanoid@3.3.11: {} natural-compare-lite@1.4.0: {} @@ -11704,7 +11706,7 @@ snapshots: postcss@8.5.1: dependencies: - nanoid: 3.3.8 + nanoid: 3.3.11 picocolors: 1.1.1 source-map-js: 1.2.1 diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 974314d2d4..ad881a78f6 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -10,3 +10,4 @@ packages: - "examples/**" - "forester/**" - "program-tests/**" + - "sdk-tests/**" diff --git a/program-libs/hasher/src/keccak.rs b/program-libs/hasher/src/keccak.rs index 81d81d810c..ab1c666ee8 100644 --- a/program-libs/hasher/src/keccak.rs +++ b/program-libs/hasher/src/keccak.rs @@ -9,6 +9,8 @@ use crate::{ pub struct Keccak; impl Hasher for Keccak { + const ID: u8 = 2; + fn hash(val: &[u8]) -> Result { Self::hashv(&[val]) } diff --git a/program-libs/hasher/src/lib.rs b/program-libs/hasher/src/lib.rs index 9f4e4758c0..83a0875ae9 100644 --- a/program-libs/hasher/src/lib.rs +++ b/program-libs/hasher/src/lib.rs @@ -24,6 +24,7 @@ pub const HASH_BYTES: usize = 32; pub type Hash = [u8; HASH_BYTES]; pub trait Hasher { + const ID: u8; fn hash(val: &[u8]) -> Result; fn hashv(vals: &[&[u8]]) -> Result; fn zero_bytes() -> ZeroBytes; diff --git a/program-libs/hasher/src/poseidon.rs b/program-libs/hasher/src/poseidon.rs index 0cd6c670da..b13d4a6a83 100644 --- a/program-libs/hasher/src/poseidon.rs +++ b/program-libs/hasher/src/poseidon.rs @@ -78,6 +78,8 @@ impl From for u64 { pub struct Poseidon; impl Hasher for Poseidon { + const ID: u8 = 0; + fn hash(val: &[u8]) -> Result { Self::hashv(&[val]) } diff --git a/program-libs/hasher/src/sha256.rs b/program-libs/hasher/src/sha256.rs index 8a4b985a52..acf55cc21a 100644 --- a/program-libs/hasher/src/sha256.rs +++ b/program-libs/hasher/src/sha256.rs @@ -9,6 +9,7 @@ use crate::{ pub struct Sha256; impl Hasher for Sha256 { + const ID: u8 = 1; fn hash(val: &[u8]) -> Result { Self::hashv(&[val]) } diff --git a/program-tests/package.json b/program-tests/package.json index cfb09042fb..71a9760235 100644 --- a/program-tests/package.json +++ b/program-tests/package.json @@ -4,7 +4,17 @@ "license": "Apache-2.0", "description": "Test programs for Light Protocol uses test-sbf to build because build-sbf -- -p creates an infinite loop.", "scripts": { - "build": "cargo test-sbf -p create-address-test-program" + "build": "cargo test-sbf -p create-address-test-program", + "test": "RUSTFLAGS=\"-D warnings\" && pnpm test-account-compression && pnpm test-system && pnpm test-registry && pnpm test-compressed-token && pnpm test-system-cpi && pnpm test-system-cpi-v2 && pnpm test-e2e && pnpm test-sdk-anchor && pnpm test-sdk-pinocchio", + "test-account-compression": "cargo test-sbf -p account-compression-test", + "test-system": "cargo test-sbf -p system-test", + "test-registry": "cargo test-sbf -p registry-test", + "test-compressed-token": "cargo test-sbf -p compressed-token-test", + "test-system-cpi": "cargo test-sbf -p system-cpi-test", + "test-system-cpi-v2": "cargo test-sbf -p system-cpi-v2-test", + "test-e2e": "cargo test-sbf -p e2e-test", + "test-sdk-anchor": "cargo test-sbf -p sdk-anchor-test", + "test-sdk-pinocchio": "cargo test-sbf -p sdk-pinocchio-test" }, "nx": { "targets": { diff --git a/program-tests/sdk-anchor-test/Anchor.toml b/program-tests/sdk-anchor-test/Anchor.toml index a443e6fb8c..0071604adb 100644 --- a/program-tests/sdk-anchor-test/Anchor.toml +++ b/program-tests/sdk-anchor-test/Anchor.toml @@ -5,7 +5,7 @@ seeds = false skip-lint = false [programs.localnet] -sdk_test = "2tzfijPBGbrR5PboyFUFKzfEoLTwdDSHUjANCw929wyt" +sdk-anchor-test = "2tzfijPBGbrR5PboyFUFKzfEoLTwdDSHUjANCw929wyt" [registry] url = "https://api.apr.dev" diff --git a/program-tests/sdk-anchor-test/package.json b/program-tests/sdk-anchor-test/package.json index 04f87d32e3..3002c9b4b7 100644 --- a/program-tests/sdk-anchor-test/package.json +++ b/program-tests/sdk-anchor-test/package.json @@ -1,6 +1,6 @@ { "scripts": { - "test": "cargo test-sbf -p sdk-test" + "test": "cargo test-sbf -p sdk-anchor-test" }, "dependencies": { "@coral-xyz/anchor": "^0.29.0" @@ -17,4 +17,4 @@ "typescript": "^5.8.3", "prettier": "^3.4.2" } -} +} \ No newline at end of file diff --git a/program-tests/sdk-test/src/lib.rs b/program-tests/sdk-test/src/lib.rs deleted file mode 100644 index 8fb2b71b2c..0000000000 --- a/program-tests/sdk-test/src/lib.rs +++ /dev/null @@ -1,49 +0,0 @@ -use light_macros::pubkey; -use light_sdk::{cpi::CpiSigner, derive_light_cpi_signer, error::LightSdkError}; -use solana_program::{ - account_info::AccountInfo, entrypoint, program_error::ProgramError, pubkey::Pubkey, -}; - -pub mod create_pda; -pub mod update_pda; - -pub const ID: Pubkey = pubkey!("FNt7byTHev1k5x2cXZLBr8TdWiC3zoP5vcnZR4P682Uy"); -pub const LIGHT_CPI_SIGNER: CpiSigner = - derive_light_cpi_signer!("FNt7byTHev1k5x2cXZLBr8TdWiC3zoP5vcnZR4P682Uy"); - -entrypoint!(process_instruction); - -#[repr(u8)] -pub enum InstructionType { - CreatePdaBorsh = 0, - UpdatePdaBorsh = 1, -} - -impl TryFrom for InstructionType { - type Error = LightSdkError; - - fn try_from(value: u8) -> Result { - match value { - 0 => Ok(InstructionType::CreatePdaBorsh), - 1 => Ok(InstructionType::UpdatePdaBorsh), - _ => panic!("Invalid instruction discriminator."), - } - } -} - -pub fn process_instruction( - _program_id: &Pubkey, - accounts: &[AccountInfo], - instruction_data: &[u8], -) -> Result<(), ProgramError> { - let discriminator = InstructionType::try_from(instruction_data[0]).unwrap(); - match discriminator { - InstructionType::CreatePdaBorsh => { - create_pda::create_pda::(accounts, &instruction_data[1..]) - } - InstructionType::UpdatePdaBorsh => { - update_pda::update_pda::(accounts, &instruction_data[1..]) - } - }?; - Ok(()) -} diff --git a/program-tests/sdk-test/tests/test.rs b/program-tests/sdk-test/tests/test.rs deleted file mode 100644 index 5008995923..0000000000 --- a/program-tests/sdk-test/tests/test.rs +++ /dev/null @@ -1,177 +0,0 @@ -#![cfg(feature = "test-sbf")] - -use borsh::BorshSerialize; -use light_compressed_account::{ - address::derive_address, compressed_account::CompressedAccountWithMerkleContext, - hashv_to_bn254_field_size_be, -}; -use light_program_test::{ - program_test::LightProgramTest, AddressWithTree, Indexer, ProgramTestConfig, Rpc, RpcError, -}; -use light_sdk::instruction::{ - account_meta::CompressedAccountMeta, PackedAccounts, SystemAccountMetaConfig, -}; -use sdk_test::{ - create_pda::CreatePdaInstructionData, - update_pda::{UpdateMyCompressedAccount, UpdatePdaInstructionData}, -}; -use solana_sdk::{ - instruction::Instruction, - pubkey::Pubkey, - signature::{Keypair, Signer}, -}; - -#[tokio::test] -async fn test_sdk_test() { - let config = ProgramTestConfig::new_v2(true, Some(vec![("sdk_test", sdk_test::ID)])); - let mut rpc = LightProgramTest::new(config).await.unwrap(); - let payer = rpc.get_payer().insecure_clone(); - - let address_tree_pubkey = rpc.get_address_merkle_tree_v2(); - let account_data = [1u8; 31]; - - // // V1 trees - // let (address, _) = light_sdk::address::derive_address( - // &[b"compressed", &account_data], - // &address_tree_info, - // &sdk_test::ID, - // ); - // Batched trees - let address_seed = hashv_to_bn254_field_size_be(&[b"compressed", account_data.as_slice()]); - let address = derive_address( - &address_seed, - &address_tree_pubkey.to_bytes(), - &sdk_test::ID.to_bytes(), - ); - let ouput_queue = rpc.get_random_state_tree_info().unwrap().queue; - create_pda( - &payer, - &mut rpc, - &ouput_queue, - account_data, - address_tree_pubkey, - address, - ) - .await - .unwrap(); - - let compressed_pda = rpc - .indexer() - .unwrap() - .get_compressed_account(address, None) - .await - .unwrap() - .value - .clone(); - assert_eq!(compressed_pda.address.unwrap(), address); - - update_pda(&payer, &mut rpc, [2u8; 31], compressed_pda.into()) - .await - .unwrap(); -} - -pub async fn create_pda( - payer: &Keypair, - rpc: &mut LightProgramTest, - merkle_tree_pubkey: &Pubkey, - account_data: [u8; 31], - address_tree_pubkey: Pubkey, - address: [u8; 32], -) -> Result<(), RpcError> { - let system_account_meta_config = SystemAccountMetaConfig::new(sdk_test::ID); - let mut accounts = PackedAccounts::default(); - accounts.add_pre_accounts_signer(payer.pubkey()); - accounts.add_system_accounts(system_account_meta_config); - - let rpc_result = rpc - .get_validity_proof( - vec![], - vec![AddressWithTree { - address, - tree: address_tree_pubkey, - }], - None, - ) - .await? - .value; - - let output_merkle_tree_index = accounts.insert_or_get(*merkle_tree_pubkey); - let packed_address_tree_info = rpc_result.pack_tree_infos(&mut accounts).address_trees[0]; - let (accounts, system_accounts_offset, tree_accounts_offset) = accounts.to_account_metas(); - - let instruction_data = CreatePdaInstructionData { - proof: rpc_result.proof.0.unwrap().into(), - address_tree_info: packed_address_tree_info, - data: account_data, - output_merkle_tree_index, - system_accounts_offset: system_accounts_offset as u8, - tree_accounts_offset: tree_accounts_offset as u8, - }; - let inputs = instruction_data.try_to_vec().unwrap(); - - let instruction = Instruction { - program_id: sdk_test::ID, - accounts, - data: [&[0u8][..], &inputs[..]].concat(), - }; - - rpc.create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) - .await?; - Ok(()) -} - -pub async fn update_pda( - payer: &Keypair, - rpc: &mut LightProgramTest, - new_account_data: [u8; 31], - compressed_account: CompressedAccountWithMerkleContext, -) -> Result<(), RpcError> { - let system_account_meta_config = SystemAccountMetaConfig::new(sdk_test::ID); - let mut accounts = PackedAccounts::default(); - accounts.add_pre_accounts_signer(payer.pubkey()); - accounts.add_system_accounts(system_account_meta_config); - - let rpc_result = rpc - .get_validity_proof(vec![compressed_account.hash().unwrap()], vec![], None) - .await? - .value; - - let packed_accounts = rpc_result - .pack_tree_infos(&mut accounts) - .state_trees - .unwrap(); - - let meta = CompressedAccountMeta { - tree_info: packed_accounts.packed_tree_infos[0], - address: compressed_account.compressed_account.address.unwrap(), - output_state_tree_index: packed_accounts.output_tree_index, - }; - - let (accounts, system_accounts_offset, _) = accounts.to_account_metas(); - let instruction_data = UpdatePdaInstructionData { - my_compressed_account: UpdateMyCompressedAccount { - meta, - data: compressed_account - .compressed_account - .data - .unwrap() - .data - .try_into() - .unwrap(), - }, - proof: rpc_result.proof, - new_data: new_account_data, - system_accounts_offset: system_accounts_offset as u8, - }; - let inputs = instruction_data.try_to_vec().unwrap(); - - let instruction = Instruction { - program_id: sdk_test::ID, - accounts, - data: [&[1u8][..], &inputs[..]].concat(), - }; - - rpc.create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) - .await?; - Ok(()) -} diff --git a/program-tests/system-cpi-test/tests/test_program_owned_trees.rs b/program-tests/system-cpi-test/tests/test_program_owned_trees.rs index 1fdf5636d0..1c61376140 100644 --- a/program-tests/system-cpi-test/tests/test_program_owned_trees.rs +++ b/program-tests/system-cpi-test/tests/test_program_owned_trees.rs @@ -126,7 +126,7 @@ async fn test_program_owned_merkle_tree() { assert_ne!(post_merkle_tree.root(), pre_merkle_tree.root()); assert_eq!( post_merkle_tree.root(), - test_indexer.state_merkle_trees[2].merkle_tree.root() + test_indexer.state_merkle_trees[3].merkle_tree.root() ); let invalid_program_owned_merkle_tree_keypair = Keypair::new(); diff --git a/program-tests/utils/src/test_keypairs.rs b/program-tests/utils/src/test_keypairs.rs index 27312ac4d8..14ad5df98b 100644 --- a/program-tests/utils/src/test_keypairs.rs +++ b/program-tests/utils/src/test_keypairs.rs @@ -64,10 +64,15 @@ pub fn from_target_folder() -> TestKeypairs { nullifier_queue_2: Keypair::new(), cpi_context_2: Keypair::new(), group_pda_seed: Keypair::new(), + batched_state_merkle_tree_2: Keypair::from_bytes(&BATCHED_STATE_MERKLE_TREE_TEST_KEYPAIR_2) + .unwrap(), + batched_output_queue_2: Keypair::from_bytes(&BATCHED_OUTPUT_QUEUE_TEST_KEYPAIR_2).unwrap(), + batched_cpi_context_2: Keypair::from_bytes(&BATCHED_CPI_CONTEXT_TEST_KEYPAIR_2).unwrap(), } } pub fn for_regenerate_accounts() -> TestKeypairs { + // Note: this requries your machine to have the light-keypairs dir with the correct keypairs. let prefix = String::from("../../../light-keypairs/"); let state_merkle_tree = read_keypair_file(format!( "{}smt1NamzXdq4AMqS2fS2F1i5KTYPZRhoHgWx38d8WsT.json", @@ -144,5 +149,9 @@ pub fn for_regenerate_accounts() -> TestKeypairs { nullifier_queue_2, cpi_context_2, group_pda_seed: Keypair::new(), + batched_state_merkle_tree_2: Keypair::from_bytes(&BATCHED_STATE_MERKLE_TREE_TEST_KEYPAIR_2) + .unwrap(), + batched_output_queue_2: Keypair::from_bytes(&BATCHED_OUTPUT_QUEUE_TEST_KEYPAIR_2).unwrap(), + batched_cpi_context_2: Keypair::from_bytes(&BATCHED_CPI_CONTEXT_TEST_KEYPAIR_2).unwrap(), } } diff --git a/programs/package.json b/programs/package.json index c505a6e42c..0ce4a5faef 100644 --- a/programs/package.json +++ b/programs/package.json @@ -12,7 +12,7 @@ "test-compressed-token": "cargo test-sbf -p compressed-token-test", "e2e-test": "cargo-test-sbf -p e2e-test", "test-registry": "cargo-test-sbf -p registry-test", - "sdk-test-program": "cargo test-sbf -p sdk-test", + "sdk-test-program": "cargo test-sbf -p native-compressible", "test-system": "cargo test-sbf -p system-test", "test-system-cpi": "cargo test-sbf -p system-cpi-test", "ignored-program-owned-account-test": "cargo-test-sbf -p program-owned-account-test" diff --git a/scripts/format.sh b/scripts/format.sh index 58968a906f..0d0450cc27 100755 --- a/scripts/format.sh +++ b/scripts/format.sh @@ -26,7 +26,7 @@ cargo test-sbf -p system-cpi-test --no-run cargo test-sbf -p system-cpi-v2-test --no-run cargo test-sbf -p e2e-test --no-run cargo test-sbf -p compressed-token-test --no-run -cargo test-sbf -p sdk-test --no-run +cargo test-sbf -p native-compressible --no-run cargo test-sbf -p sdk-anchor-test --no-run cargo test-sbf -p client-test --no-run -cargo test-sbf -p sdk-pinocchio-test --no-run +cargo test-sbf -p sdk-pinocchio-test --no-run \ No newline at end of file diff --git a/sdk-libs/client/Cargo.toml b/sdk-libs/client/Cargo.toml index 895d272ee4..8c9784091a 100644 --- a/sdk-libs/client/Cargo.toml +++ b/sdk-libs/client/Cargo.toml @@ -35,6 +35,7 @@ solana-address-lookup-table-interface = { version = "2.2.1", features = [ "bytemuck", "bincode", ] } +anchor-lang = { workspace = true, features = ["idl-build"], optional = true } # Light Protocol dependencies light-merkle-tree-metadata = { workspace = true, features = ["solana"] } @@ -63,5 +64,7 @@ tracing = { workspace = true } lazy_static = { workspace = true } rand = { workspace = true } + + # Tests are in program-tests/client-test/tests/light-client.rs # [dev-dependencies] diff --git a/sdk-libs/client/src/indexer/tree_info.rs b/sdk-libs/client/src/indexer/tree_info.rs index a4a0a29cdc..57bd47d946 100644 --- a/sdk-libs/client/src/indexer/tree_info.rs +++ b/sdk-libs/client/src/indexer/tree_info.rs @@ -292,6 +292,30 @@ lazy_static! { }, ); + // v2 queue 2 + m.insert( + "12wJT3xYd46rtjeqDU6CrtT8unqLjPiheggzqhN9YsyB".to_string(), + TreeInfo { + tree: pubkey!("2Yb3fGo2E9aWLjY8KuESaqurYpGGhEeJr7eynKrSgXwS"), + queue: pubkey!("12wJT3xYd46rtjeqDU6CrtT8unqLjPiheggzqhN9YsyB"), + cpi_context: None, + tree_type: TreeType::StateV2, + next_tree_info: None, + }, + ); + + // v2 tree 2 + m.insert( + "2Yb3fGo2E9aWLjY8KuESaqurYpGGhEeJr7eynKrSgXwS".to_string(), + TreeInfo { + tree: pubkey!("2Yb3fGo2E9aWLjY8KuESaqurYpGGhEeJr7eynKrSgXwS"), + queue: pubkey!("12wJT3xYd46rtjeqDU6CrtT8unqLjPiheggzqhN9YsyB"), + cpi_context: None, + tree_type: TreeType::StateV2, + next_tree_info: None, + }, + ); + m }; } diff --git a/sdk-libs/client/src/lib.rs b/sdk-libs/client/src/lib.rs index a5159c310d..095cf2a8e7 100644 --- a/sdk-libs/client/src/lib.rs +++ b/sdk-libs/client/src/lib.rs @@ -81,6 +81,7 @@ pub mod fee; pub mod indexer; pub mod local_test_validator; pub mod rpc; +pub mod utils; /// Reexport for ProverConfig and other types. pub use light_prover_client; diff --git a/sdk-libs/client/src/rpc/client.rs b/sdk-libs/client/src/rpc/client.rs index 05419e8a2b..ef2cded5fa 100644 --- a/sdk-libs/client/src/rpc/client.rs +++ b/sdk-libs/client/src/rpc/client.rs @@ -691,13 +691,22 @@ impl Rpc for LightClient { use crate::indexer::TreeInfo; #[cfg(feature = "v2")] - let default_trees = vec![TreeInfo { - tree: pubkey!("HLKs5NJ8FXkJg8BrzJt56adFYYuwg5etzDtBbQYTsixu"), - queue: pubkey!("6L7SzhYB3anwEQ9cphpJ1U7Scwj57bx2xueReg7R9cKU"), - cpi_context: Some(pubkey!("7Hp52chxaew8bW1ApR4fck2bh6Y8qA1pu3qwH6N9zaLj")), - next_tree_info: None, - tree_type: TreeType::StateV2, - }]; + let default_trees = vec![ + TreeInfo { + tree: pubkey!("HLKs5NJ8FXkJg8BrzJt56adFYYuwg5etzDtBbQYTsixu"), + queue: pubkey!("6L7SzhYB3anwEQ9cphpJ1U7Scwj57bx2xueReg7R9cKU"), + cpi_context: Some(pubkey!("7Hp52chxaew8bW1ApR4fck2bh6Y8qA1pu3qwH6N9zaLj")), + next_tree_info: None, + tree_type: TreeType::StateV2, + }, + TreeInfo { + tree: pubkey!("2Yb3fGo2E9aWLjY8KuESaqurYpGGhEeJr7eynKrSgXwS"), + queue: pubkey!("12wJT3xYd46rtjeqDU6CrtT8unqLjPiheggzqhN9YsyB"), + cpi_context: Some(pubkey!("HwtjxDvFEXiWnzeMeWkMBzpQN45A95rTJNZmz1Z3pe8R")), + next_tree_info: None, + tree_type: TreeType::StateV2, + }, + ]; #[cfg(not(feature = "v2"))] let default_trees = vec![TreeInfo { diff --git a/sdk-libs/light-compressible-client/Cargo.toml b/sdk-libs/light-compressible-client/Cargo.toml new file mode 100644 index 0000000000..fc29e3bd0a --- /dev/null +++ b/sdk-libs/light-compressible-client/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "light-compressible-client" +version = "0.13.1" +edition = "2021" +license = "Apache-2.0" +repository = "https://github.com/lightprotocol/light-protocol" +description = "Client instruction builders for Light Protocol compressible accounts" + +[features] +anchor = ["anchor-lang", "light-sdk/anchor"] + +[dependencies] +# Solana dependencies +solana-instruction = { workspace = true } +solana-pubkey = { workspace = true } + +# Light Protocol dependencies +light-client = { workspace = true, features = ["v2"] } +light-sdk = { workspace = true, features = ["v2"] } + +# Conditional dependencies +anchor-lang = { workspace = true, features = ["idl-build"], optional = true } +borsh = { workspace = true } + +# External dependencies +thiserror = { workspace = true } diff --git a/sdk-libs/light-compressible-client/src/lib.rs b/sdk-libs/light-compressible-client/src/lib.rs new file mode 100644 index 0000000000..7f4920d4bb --- /dev/null +++ b/sdk-libs/light-compressible-client/src/lib.rs @@ -0,0 +1,422 @@ +#[cfg(feature = "anchor")] +use anchor_lang::{AnchorDeserialize, AnchorSerialize}; +#[cfg(not(feature = "anchor"))] +use borsh::{BorshDeserialize as AnchorDeserialize, BorshSerialize as AnchorSerialize}; +use light_client::indexer::{CompressedAccount, TreeInfo, ValidityProofWithContext}; +pub use light_sdk::compressible::config::CompressibleConfig; +use light_sdk::instruction::{ + account_meta::CompressedAccountMeta, PackedAccounts, SystemAccountMetaConfig, ValidityProof, +}; +use solana_instruction::{AccountMeta, Instruction}; +use solana_pubkey::Pubkey; + +/// Generic compressed account data structure for decompress operations +/// This is generic over the account variant type, allowing programs to use their specific enums +/// +/// # Type Parameters +/// * `T` - The program-specific compressed account variant enum (e.g., CompressedAccountVariant) +/// +/// # Fields +/// * `meta` - The compressed account metadata containing tree info, address, and output index +/// * `data` - The program-specific account variant enum +/// * `seeds` - The PDA seeds (without bump) used to derive the PDA address +#[derive(AnchorSerialize, AnchorDeserialize, Clone, Debug)] +pub struct CompressedAccountData { + pub meta: CompressedAccountMeta, + /// Program-specific account variant enum + pub data: T, + /// PDA seeds (without bump) used to derive the PDA address + pub seeds: Vec>, +} + +/// Instruction data structure for decompress_accounts_idempotent +/// This matches the exact format expected by Anchor programs +#[derive(AnchorSerialize, AnchorDeserialize, Clone, Debug)] +pub struct DecompressMultipleAccountsIdempotentData { + pub proof: ValidityProof, + pub compressed_accounts: Vec>, + pub bumps: Vec, + pub system_accounts_offset: u8, +} + +/// Instruction builders for compressible accounts, following Solana SDK patterns +/// These are generic builders that work with any program implementing the compressible pattern +pub struct CompressibleInstruction; + +impl CompressibleInstruction { + pub const INITIALIZE_COMPRESSION_CONFIG_DISCRIMINATOR: [u8; 8] = + [133, 228, 12, 169, 56, 76, 222, 61]; + pub const UPDATE_COMPRESSION_CONFIG_DISCRIMINATOR: [u8; 8] = + [135, 215, 243, 81, 163, 146, 33, 70]; + /// Hardcoded discriminator for the standardized decompress_accounts_idempotent instruction + /// This is calculated as SHA256("global:decompress_accounts_idempotent")[..8] (Anchor format) + pub const DECOMPRESS_ACCOUNTS_IDEMPOTENT_DISCRIMINATOR: [u8; 8] = + [114, 67, 61, 123, 234, 31, 1, 112]; + + /// Creates an initialize_compression_config instruction + /// + /// Following Solana SDK patterns like system_instruction::transfer() + /// Returns Instruction directly - errors surface at execution time + /// + /// # Arguments + /// * `program_id` - The program ID + /// * `discriminator` - The instruction discriminator bytes (flexible length) + /// * `payer` - The payer account + /// * `authority` - The authority account + /// * `compression_delay` - The compression delay + /// * `rent_recipient` - The rent recipient + /// * `address_space` - The address space + /// * `config_bump` - The config bump + #[allow(clippy::too_many_arguments)] + pub fn initialize_compression_config( + program_id: &Pubkey, + discriminator: &[u8], + payer: &Pubkey, + authority: &Pubkey, + compression_delay: u32, + rent_recipient: Pubkey, + address_space: Vec, + config_bump: Option, + ) -> Instruction { + let config_bump = config_bump.unwrap_or(0); + let (config_pda, _) = CompressibleConfig::derive_pda(program_id, config_bump); + + // Get program data account for BPF Loader Upgradeable + let bpf_loader_upgradeable_id = + solana_pubkey::pubkey!("BPFLoaderUpgradeab1e11111111111111111111111"); + let (program_data_pda, _) = + Pubkey::find_program_address(&[program_id.as_ref()], &bpf_loader_upgradeable_id); + + let system_program_id = solana_pubkey::pubkey!("11111111111111111111111111111111"); + let accounts = vec![ + AccountMeta::new(*payer, true), // payer + AccountMeta::new(config_pda, false), // config + AccountMeta::new_readonly(program_data_pda, false), // program_data + AccountMeta::new_readonly(*authority, true), // authority + AccountMeta::new_readonly(system_program_id, false), // system_program + ]; + + let instruction_data = InitializeCompressionConfigData { + compression_delay, + rent_recipient, + address_space, + config_bump, + }; + + // Prepend discriminator to serialized data, following Solana SDK pattern + let serialized_data = instruction_data + .try_to_vec() + .expect("Failed to serialize instruction data"); + + let mut data = Vec::new(); + data.extend_from_slice(discriminator); + data.extend_from_slice(&serialized_data); + + Instruction { + program_id: *program_id, + accounts, + data, + } + } + + /// Creates an update config instruction + /// + /// Following Solana SDK patterns - returns Instruction directly + /// + /// # Arguments + /// * `program_id` - The program ID + /// * `discriminator` - The instruction discriminator bytes (flexible length) + /// * `authority` - The authority account + /// * `new_compression_delay` - Optional new compression delay + /// * `new_rent_recipient` - Optional new rent recipient + /// * `new_address_space` - Optional new address space + /// * `new_update_authority` - Optional new update authority + pub fn update_compression_config( + program_id: &Pubkey, + discriminator: &[u8], + authority: &Pubkey, + new_compression_delay: Option, + new_rent_recipient: Option, + new_address_space: Option>, + new_update_authority: Option, + ) -> Instruction { + let (config_pda, _) = CompressibleConfig::derive_pda(program_id, 0); + + let accounts = vec![ + AccountMeta::new(config_pda, false), // config + AccountMeta::new_readonly(*authority, true), // authority + ]; + + let instruction_data = UpdateCompressionConfigData { + new_compression_delay, + new_rent_recipient, + new_address_space, + new_update_authority, + }; + + // Prepend discriminator to serialized data, following Solana SDK pattern + let serialized_data = instruction_data + .try_to_vec() + .expect("Failed to serialize instruction data"); + let mut data = Vec::with_capacity(discriminator.len() + serialized_data.len()); + data.extend_from_slice(discriminator); + data.extend_from_slice(&serialized_data); + + Instruction { + program_id: *program_id, + accounts, + data, + } + } + + /// Creates a generic compress account instruction for any compressible account + /// + /// This is a generic helper that can be used by any program client to build + /// a compress account instruction. The caller must provide the instruction + /// discriminator specific to their program. + /// + /// # Arguments + /// * `program_id` - The program that owns the compressible account + /// * `discriminator` - The instruction discriminator bytes (flexible length) + /// * `payer` - The account paying for the transaction + /// * `pda_to_compress` - The PDA account to compress + /// * `rent_recipient` - The account to receive the reclaimed rent + /// * `compressed_account` - The compressed account to be nullified + /// * `validity_proof_with_context` - The validity proof with context from the indexer + /// * `output_state_tree_info` - The output state tree info + /// + /// # Returns + /// * `Result>` - The complete instruction ready to be sent + #[allow(clippy::too_many_arguments)] + pub fn compress_account( + program_id: &Pubkey, + discriminator: &[u8], + payer: &Pubkey, + pda_to_compress: &Pubkey, + rent_recipient: &Pubkey, + compressed_account: &CompressedAccount, + validity_proof_with_context: ValidityProofWithContext, + output_state_tree_info: TreeInfo, + ) -> Result> { + let config_pda = CompressibleConfig::derive_pda(program_id, 0).0; + + // Create system accounts internally (same pattern as decompress_accounts_idempotent) + let mut remaining_accounts = PackedAccounts::default(); + let system_config = SystemAccountMetaConfig::new(*program_id); + remaining_accounts.add_system_accounts(system_config); + + // Pack tree infos into remaining accounts + let packed_tree_infos = + validity_proof_with_context.pack_tree_infos(&mut remaining_accounts); + + // Get output state tree index + let output_state_tree_index = + remaining_accounts.insert_or_get(output_state_tree_info.queue); + + // Find the tree info index for this compressed account's queue + let queue_index = remaining_accounts.insert_or_get(compressed_account.tree_info.queue); + + // Create compressed account meta + let compressed_account_meta = CompressedAccountMeta { + tree_info: packed_tree_infos + .state_trees + .as_ref() + .unwrap() + .packed_tree_infos + .iter() + .find(|pti| { + pti.queue_pubkey_index == queue_index + && pti.leaf_index == compressed_account.leaf_index + }) + .copied() + .ok_or( + "Matching PackedStateTreeInfo (queue_pubkey_index + leaf_index) not found", + )?, + address: compressed_account.address.unwrap_or([0u8; 32]), + output_state_tree_index, + }; + + // Get system accounts for the instruction + let (system_accounts, _, _) = remaining_accounts.to_account_metas(); + + // Create the instruction account metas + let accounts = vec![ + AccountMeta::new(*payer, true), // user (signer) + AccountMeta::new(*pda_to_compress, false), // pda_to_compress (writable) + AccountMeta::new_readonly(config_pda, false), // config + AccountMeta::new(*rent_recipient, false), // rent_recipient (writable) + ]; + + // Create instruction data + let instruction_data = GenericCompressAccountInstruction { + proof: validity_proof_with_context.proof, + compressed_account_meta, + }; + + // Manually serialize instruction data with discriminator + let serialized_data = instruction_data + .try_to_vec() + .expect("Failed to serialize instruction data"); + let mut data = Vec::new(); + data.extend_from_slice(discriminator); + data.extend_from_slice(&serialized_data); + + // Build the instruction + Ok(Instruction { + program_id: *program_id, + accounts: [accounts, system_accounts].concat(), + data, + }) + } + + /// Build a `decompress_accounts_idempotent` instruction for any program's compressed account variant. + /// + /// # Arguments + /// * `program_id` - Target program + /// * `discriminator` - The instruction discriminator bytes (flexible length) + /// * `fee_payer` - Fee payer signer + /// * `rent_payer` - Rent payer signer + /// * `solana_accounts` - PDAs to decompress into + /// * `compressed_accounts` - (meta, variant, seeds) tuples where seeds are PDA seeds without bump + /// * `bumps` - PDA bump seeds + /// * `validity_proof_with_context` - Validity proof with context + /// * `output_state_tree_info` - Output state tree info + /// + /// Returns `Ok(Instruction)` or error. + #[allow(clippy::too_many_arguments)] + pub fn decompress_accounts_idempotent( + program_id: &Pubkey, + discriminator: &[u8], + fee_payer: &Pubkey, + rent_payer: &Pubkey, + solana_accounts: &[Pubkey], + compressed_accounts: &[(CompressedAccount, T, Vec>)], + bumps: &[u8], + validity_proof_with_context: ValidityProofWithContext, + output_state_tree_info: TreeInfo, + ) -> Result> + where + T: AnchorSerialize + Clone + std::fmt::Debug, + { + // Setup remaining accounts to get tree infos + let mut remaining_accounts = PackedAccounts::default(); + let system_config = SystemAccountMetaConfig::new(*program_id); + remaining_accounts.add_system_accounts(system_config); + + for pda in solana_accounts { + remaining_accounts.add_pre_accounts_meta(AccountMeta::new(*pda, false)); + } + + let packed_tree_infos = + validity_proof_with_context.pack_tree_infos(&mut remaining_accounts); + + // get output state tree index + let output_state_tree_index = + remaining_accounts.insert_or_get(output_state_tree_info.queue); + + // Validation + if solana_accounts.len() != compressed_accounts.len() { + return Err("PDA accounts and compressed accounts must have the same length".into()); + } + if solana_accounts.len() != bumps.len() { + return Err("PDA accounts and bumps must have the same length".into()); + } + + let config_pda = CompressibleConfig::derive_pda(program_id, 0).0; + + // Build instruction accounts + let mut accounts = vec![ + AccountMeta::new(*fee_payer, true), // fee_payer + AccountMeta::new(*rent_payer, true), // rent_payer + AccountMeta::new_readonly(config_pda, false), // config + ]; + + // Add Light Protocol system accounts (already packed by caller) + let (system_accounts, _, _) = remaining_accounts.to_account_metas(); + accounts.extend(system_accounts); + + // Convert to typed compressed account data + let typed_compressed_accounts: Vec> = compressed_accounts + .iter() + .map(|(compressed_account, data, seeds)| { + // Find the tree info index for this compressed account's queue + let queue_index = + remaining_accounts.insert_or_get(compressed_account.tree_info.queue); + let compressed_meta = CompressedAccountMeta { + // TODO: Find cleaner way to do this. + tree_info: packed_tree_infos + .state_trees + .as_ref() + .unwrap() + .packed_tree_infos + .iter() + .find(|pti| { + pti.queue_pubkey_index == queue_index + && pti.leaf_index == compressed_account.leaf_index + }) + .copied() + .ok_or("Matching PackedStateTreeInfo (queue_pubkey_index + leaf_index) not found")?, + address: compressed_account.address.unwrap_or([0u8; 32]), + output_state_tree_index, + }; + Ok(CompressedAccountData { + meta: compressed_meta, + data: data.clone(), + seeds: seeds.clone(), + }) + }) + .collect::, Box>>()?; + + // Build instruction data + let instruction_data = DecompressMultipleAccountsIdempotentData { + proof: validity_proof_with_context.proof, + compressed_accounts: typed_compressed_accounts, + bumps: bumps.to_vec(), + system_accounts_offset: solana_accounts.len() as u8, + }; + + // Serialize instruction data with discriminator + let serialized_data = instruction_data.try_to_vec()?; + let mut data = Vec::new(); + data.extend_from_slice(discriminator); + data.extend_from_slice(&serialized_data); + + Ok(Instruction { + program_id: *program_id, + accounts, + data, + }) + } +} + +/// Generic instruction data for initialize config +/// Note: Real programs should use their specific instruction format +#[derive(AnchorSerialize, AnchorDeserialize)] +pub struct InitializeCompressionConfigData { + pub compression_delay: u32, + pub rent_recipient: Pubkey, + pub address_space: Vec, + pub config_bump: u8, +} + +/// Generic instruction data for update config +/// Note: Real programs should use their specific instruction format +#[derive(AnchorSerialize, AnchorDeserialize)] +pub struct UpdateCompressionConfigData { + pub new_compression_delay: Option, + pub new_rent_recipient: Option, + pub new_address_space: Option>, + pub new_update_authority: Option, +} + +/// Generic instruction data for compress account +/// This matches the expected format for compress account instructions +#[derive(AnchorSerialize, AnchorDeserialize)] +pub struct GenericCompressAccountInstruction { + pub proof: ValidityProof, + pub compressed_account_meta: CompressedAccountMeta, +} + +/// Generic instruction data for decompress multiple PDAs +// Re-export for easy access following Solana SDK patterns +pub use CompressibleInstruction as compressible_instruction; diff --git a/sdk-libs/macros/CHANGELOG.md b/sdk-libs/macros/CHANGELOG.md new file mode 100644 index 0000000000..e6f0223b7b --- /dev/null +++ b/sdk-libs/macros/CHANGELOG.md @@ -0,0 +1,106 @@ +# Changelog + +## [Unreleased] + +### Changed + +- **BREAKING**: `add_compressible_instructions` macro no longer generates `create_*` instructions: + - Removed automatic generation of `create_user_record`, `create_game_session`, etc. + - Developers must implement their own create instructions with custom initialization logic + - This change recognizes that create instructions typically need custom business logic +- Updated `add_compressible_instructions` macro to align with new SDK patterns: + - Now generates `create_compression_config` and `update_compression_config` instructions + - Uses `HasCompressionInfo` trait instead of deprecated `CompressionTiming` + - `compress_*` instructions validate against config rent recipient + - `decompress_multiple_pdas` now accepts seeds in `CompressedAccountData` + - All generated instructions follow the pattern used in `anchor-compressible` + - Automatically uses Anchor's `INIT_SPACE` for account size calculation (no manual SIZE needed) + +### Added + +- **MAJOR**: Enhanced external file module support: + - Comprehensive pattern matching for common AMM/DEX structures (PoolState, Vault, Position, etc.) + - Explicit seed specification syntax: `#[add_compressible_instructions(PoolState@[POOL_SEED.as_bytes(), amm_config.key().as_ref()])]` + - Improved import detection for `pub use` statements and CamelCase account structs + - Intelligent seed inference for 7+ common DeFi patterns (pools, vaults, positions, configs, etc.) + - Enhanced error messages with debugging info and actionable solutions + - Support for complex multi-file project structures like Raydium CP-Swap +- Config management support in generated code: + - `CreateCompressibleConfig` accounts struct + - `UpdateCompressibleConfig` accounts struct + - Automatic config validation in create/compress instructions +- `CompressedAccountData` now includes `seeds` field for flexible PDA derivation +- Generated error codes for config validation +- `CompressionInfo` now implements `anchor_lang::Space` trait for automatic size calculation + +### Fixed + +- External file module parsing that previously threw "External file modules require explicit seed definitions" +- Import resolution for `pub use` statements across multiple files +- Pattern detection for account structs with various naming conventions + +### Removed + +- Deprecated `CompressionTiming` trait support +- Hardcoded constants (RENT_RECIPIENT, ADDRESS_SPACE, COMPRESSION_DELAY) +- Manual SIZE constant requirement - now uses Anchor's built-in space calculation + +## Migration Guide + +1. **Implement your own create instructions** (macro no longer generates them): + + ```rust + #[derive(Accounts)] + pub struct CreateUserRecord<'info> { + #[account(mut)] + pub user: Signer<'info>, + #[account( + init, + payer = user, + space = 8 + UserRecord::INIT_SPACE, + seeds = [b"user_record", user.key().as_ref()], + bump, + )] + pub user_record: Account<'info, UserRecord>, + pub system_program: Program<'info, System>, + } + + pub fn create_user_record(ctx: Context, name: String) -> Result<()> { + let user_record = &mut ctx.accounts.user_record; + user_record.compression_info = CompressionInfo::new_decompressed()?; + user_record.owner = ctx.accounts.user.key(); + user_record.name = name; + user_record.score = 0; + Ok(()) + } + ``` + +2. Update account structs to use `CompressionInfo` field and derive `InitSpace`: + + ```rust + #[derive(Debug, LightHasher, LightDiscriminator, Default, InitSpace)] + #[account] + pub struct UserRecord { + #[skip] + pub compression_info: CompressionInfo, + #[hash] + pub owner: Pubkey, + #[max_len(32)] // Required for String fields + pub name: String, + pub score: u64, + } + ``` + +3. Implement `HasCompressionInfo` trait instead of `CompressionTiming` + +4. Create config after program deployment: + + ```typescript + await program.methods + .createCompressibleConfig(compressionDelay, rentRecipient, addressSpace) + .rpc(); + ``` + +5. Update client code to use new instruction names: + - `create_record` → `create_user_record` (based on struct name) + - Pass entire struct data instead of individual fields diff --git a/sdk-libs/macros/Cargo.toml b/sdk-libs/macros/Cargo.toml index 791a4e9787..caea3eaed2 100644 --- a/sdk-libs/macros/Cargo.toml +++ b/sdk-libs/macros/Cargo.toml @@ -6,12 +6,16 @@ repository = "https://github.com/Lightprotocol/light-protocol" license = "Apache-2.0" edition = "2021" +[features] +default = [] +anchor-discriminator-compat = [] + [dependencies] proc-macro2 = { workspace = true } quote = { workspace = true } syn = { workspace = true } solana-pubkey = { workspace = true, features = ["curve25519", "sha2"] } - +heck = "0.4.1" light-hasher = { workspace = true } light-poseidon = { workspace = true } diff --git a/sdk-libs/macros/src/EXAMPLE_USAGE.md b/sdk-libs/macros/src/EXAMPLE_USAGE.md new file mode 100644 index 0000000000..457f949da7 --- /dev/null +++ b/sdk-libs/macros/src/EXAMPLE_USAGE.md @@ -0,0 +1,165 @@ +# Example Usage + +## Basic Usage + +```rust +#[add_compressible_instructions(UserRecord, GameSession)] +#[program] +pub mod my_program { + use super::*; + // ... your instructions +} +``` + +## External File Module Support - NEW APPROACH! 🚀 + +For complex projects with multi-file structures (like Raydium CP-Swap), you can now use the new `derive(Compressible)` approach for **completely automatic seed detection**: + +### Step 1: Add derive(Compressible) to your instruction struct + +```rust +// instructions/initialize.rs +use anchor_lang::prelude::*; +use light_sdk_macros::Compressible; // Import the derive macro + +#[derive(Accounts, Compressible)] // ← Add Compressible derive! +pub struct Initialize<'info> { + #[account(mut)] + pub creator: Signer<'info>, + + #[account( + init, + seeds = [ + POOL_SEED.as_bytes(), + amm_config.key().as_ref(), + token_0_mint.key().as_ref(), + token_1_mint.key().as_ref(), + ], + bump, + payer = creator, + space = PoolState::LEN + )] + pub pool_state: Box>, // ← Automatically detected! + + pub amm_config: Box>, + pub token_0_mint: Box>, + pub token_1_mint: Box>, + // ... other fields +} +``` + +### Step 2: Import and use normally + +```rust +// lib.rs +pub use crate::instructions::initialize::Initialize; // Import your instruction struct +pub use crate::states::PoolState; + +#[add_compressible_instructions(PoolState)] // ← Works automatically now! +#[program] +pub mod raydium_cp_swap { + use super::*; + + pub fn initialize(ctx: Context, ...) -> Result<()> { + // Your initialization logic + } + + // ... other instructions +} +``` + +**That's it!** The macro automatically: + +- ✅ Finds the `Initialize` struct with `derive(Compressible)` +- ✅ Extracts the exact seeds from the `#[account(init, seeds = [...], bump)]` attribute +- ✅ Generates compression instructions using those seeds +- ✅ Works with any account types and seed patterns +- ✅ No hardcoded patterns or guessing required + +## Multiple Account Types + +You can use the same approach for multiple account types: + +```rust +// Different instruction structs with different account types +#[derive(Accounts, Compressible)] +pub struct CreateUser<'info> { + #[account(init, seeds = [b"user", authority.key().as_ref()], bump)] + pub user_account: Account<'info, UserAccount>, + pub authority: Signer<'info>, +} + +#[derive(Accounts, Compressible)] +pub struct InitializeVault<'info> { + #[account(init, seeds = [b"vault", mint.key().as_ref()], bump)] + pub vault: Account<'info, TokenVault>, + pub mint: Account<'info, Mint>, +} + +// All work automatically +#[add_compressible_instructions(PoolState, UserAccount, TokenVault)] +#[program] +pub mod my_program { + // ... +} +``` + +## Generated Instructions + +For each account type, the macro generates: + +- **`compress_{type_name}`** - Compresses the PDA using the exact same seeds +- **`decompress_accounts_idempotent`** - Batch decompress multiple accounts +- **`initialize_compression_config`** - Set up compression configuration +- **`update_compression_config`** - Update compression settings + +## Key Benefits of the New Approach + +1. **🎯 100% Accurate**: Uses the exact seeds from your instruction structs +2. **🔄 Zero Duplication**: No need to specify seeds twice +3. **🛡️ Type Safe**: Compile-time verification of account types +4. **📁 Multi-File Support**: Works with any project structure +5. **🚀 Future Proof**: Supports any seed patterns, not just common ones +6. **⚡ Automatic**: No configuration or setup required + +## Migration from Previous Versions + +If you were using the old pattern-matching approach, simply: + +1. Add `#[derive(Compressible)]` to your instruction structs +2. Remove any workaround code or manual seed specifications +3. The macro now works automatically! + +```diff +// Before (workarounds needed) +- #[add_compressible_instructions(PoolState@[POOL_SEED.as_bytes(), ...])] + +// After (completely automatic) ++ #[derive(Accounts, Compressible)] ++ pub struct Initialize<'info> { /* seeds automatically detected */ } ++ #[add_compressible_instructions(PoolState)] +``` + +## Error Messages + +If you forget to add `derive(Compressible)`, you'll get helpful guidance: + +``` +No seed registry found for type 'PoolState'. + +To use this type with #[add_compressible_instructions], you need to: + +1. Apply #[derive(Compressible)] to an instruction struct that initializes this account type: + +#[derive(Accounts, Compressible)] +pub struct Initialize<'info> { + #[account(init, seeds = [...], bump)] + pub pool_state: Account<'info, PoolState>, +} + +2. Make sure the instruction struct is imported in the same module where #[add_compressible_instructions] is used: + +pub use crate::instructions::initialize::Initialize; +``` + +This approach completely solves the external file module limitation while being more robust and user-friendly than any pattern matching could be! diff --git a/sdk-libs/macros/src/compressible.rs b/sdk-libs/macros/src/compressible.rs new file mode 100644 index 0000000000..e0c60ddf88 --- /dev/null +++ b/sdk-libs/macros/src/compressible.rs @@ -0,0 +1,645 @@ +use heck::ToSnakeCase; +use proc_macro2::TokenStream; +use quote::{format_ident, quote}; +use syn::{ + parse::{Parse, ParseStream}, + punctuated::Punctuated, + Expr, Ident, Item, ItemEnum, ItemFn, ItemMod, ItemStruct, Result, Token, +}; + +/// Parse a comma-separated list of identifiers +struct IdentList { + idents: Punctuated, +} + +impl Parse for IdentList { + fn parse(input: ParseStream) -> Result { + Ok(IdentList { + idents: Punctuated::parse_terminated(input)?, + }) + } +} + +/// Information about seeds extracted from registry functions +#[derive(Debug, Clone)] +struct SeedInfo { + seeds: Vec, + bump_field: Option, +} + +/// Generate compress instructions for the specified account types (Anchor version) +pub(crate) fn add_compressible_instructions( + args: TokenStream, + mut module: ItemMod, +) -> Result { + let ident_list = syn::parse2::(args)?; + + // Check if module has content + if module.content.is_none() { + return Err(syn::Error::new_spanned(&module, "Module must have a body")); + } + + // Get the module content + let content = module.content.as_mut().unwrap(); + + // Collect all struct names for the enum + let struct_names: Vec<_> = ident_list.idents.iter().cloned().collect(); + + // Generate the CompressedAccountVariant enum + let enum_variants = struct_names.iter().map(|name| { + quote! { #name(#name) } + }); + + let compressed_account_variant_enum: ItemEnum = syn::parse_quote! { + #[derive(Clone, Debug, light_sdk::AnchorSerialize, light_sdk::AnchorDeserialize)] + pub enum CompressedAccountVariant { + #(#enum_variants),* + } + }; + + // Generate Default implementation for the enum + if struct_names.is_empty() { + return Err(syn::Error::new_spanned( + &module, + "At least one account struct must be specified", + )); + } + + let first_struct = struct_names.first().expect("At least one struct required"); + let default_impl: Item = syn::parse_quote! { + impl Default for CompressedAccountVariant { + fn default() -> Self { + CompressedAccountVariant::#first_struct(Default::default()) + } + } + }; + + // Generate DataHasher implementation for the enum + let hash_match_arms = struct_names.iter().map(|name| { + quote! { + CompressedAccountVariant::#name(data) => data.hash::() + } + }); + + let data_hasher_impl: Item = syn::parse_quote! { + impl light_hasher::DataHasher for CompressedAccountVariant { + fn hash(&self) -> std::result::Result<[u8; 32], light_hasher::errors::HasherError> { + match self { + #(#hash_match_arms),* + } + } + } + }; + + // Generate LightDiscriminator implementation for the enum + let light_discriminator_impl: Item = syn::parse_quote! { + impl light_sdk::LightDiscriminator for CompressedAccountVariant { + const LIGHT_DISCRIMINATOR: [u8; 8] = [0; 8]; // This won't be used directly + const LIGHT_DISCRIMINATOR_SLICE: &'static [u8] = &Self::LIGHT_DISCRIMINATOR; + } + }; + + // Generate HasCompressionInfo implementation for the enum + let has_compression_info_impl: Item = syn::parse_quote! { + impl light_sdk::compressible::HasCompressionInfo for CompressedAccountVariant { + fn compression_info(&self) -> &light_sdk::compressible::CompressionInfo { + match self { + #(CompressedAccountVariant::#struct_names(data) => data.compression_info()),* + } + } + + fn compression_info_mut(&mut self) -> &mut light_sdk::compressible::CompressionInfo { + match self { + #(CompressedAccountVariant::#struct_names(data) => data.compression_info_mut()),* + } + } + + fn compression_info_mut_opt(&mut self) -> &mut Option { + match self { + #(CompressedAccountVariant::#struct_names(data) => data.compression_info_mut_opt()),* + } + } + + fn set_compression_info_none(&mut self) { + match self { + #(CompressedAccountVariant::#struct_names(data) => data.set_compression_info_none()),* + } + } + } + }; + + // Generate Size implementation for the enum + let size_match_arms = struct_names.iter().map(|name| { + quote! { + CompressedAccountVariant::#name(data) => data.size() + } + }); + + let size_impl: Item = syn::parse_quote! { + impl light_sdk::Size for CompressedAccountVariant { + fn size(&self) -> usize { + match self { + #(#size_match_arms),* + } + } + } + }; + + // Generate the CompressedAccountData struct + let compressed_account_data: ItemStruct = syn::parse_quote! { + #[derive(Clone, Debug, light_sdk::AnchorDeserialize, light_sdk::AnchorSerialize)] + pub struct CompressedAccountData { + pub meta: light_sdk_types::instruction::account_meta::CompressedAccountMeta, + pub data: CompressedAccountVariant, + pub seeds: Vec>, // Seeds for PDA derivation (without bump) + } + }; + + // Generate config-related structs and instructions + let initialize_config_accounts: ItemStruct = syn::parse_quote! { + #[derive(Accounts)] + pub struct InitializeCompressionConfig<'info> { + #[account(mut)] + pub payer: Signer<'info>, + /// The config PDA to be created + /// CHECK: Config PDA is checked by the SDK + #[account(mut)] + pub config: AccountInfo<'info>, + /// The program's data account + /// CHECK: Program data account is validated by the SDK + pub program_data: AccountInfo<'info>, + /// The program's upgrade authority (must sign) + pub authority: Signer<'info>, + pub system_program: Program<'info, System>, + } + }; + + // Generate the update_compression_config accounts struct + let update_config_accounts: ItemStruct = syn::parse_quote! { + #[derive(Accounts)] + pub struct UpdateCompressionConfig<'info> { + /// CHECK: Config is checked by the SDK's load_checked method + #[account(mut)] + pub config: AccountInfo<'info>, + /// Must match the update authority stored in config + pub authority: Signer<'info>, + } + }; + + let initialize_compression_config_fn: ItemFn = syn::parse_quote! { + /// Create compressible config - only callable by program upgrade authority + pub fn initialize_compression_config( + ctx: Context, + compression_delay: u32, + rent_recipient: Pubkey, + address_space: Vec, + config_bump: Option, + ) -> anchor_lang::Result<()> { + let config_bump = config_bump.unwrap_or(0); + light_sdk::compressible::process_initialize_compression_config_checked( + &ctx.accounts.config.to_account_info(), + &ctx.accounts.authority.to_account_info(), + &ctx.accounts.program_data.to_account_info(), + &rent_recipient, + address_space, + compression_delay, + config_bump, + &ctx.accounts.payer.to_account_info(), + &ctx.accounts.system_program.to_account_info(), + &crate::ID, + )?; + + Ok(()) + } + }; + + let update_compression_config_fn: ItemFn = syn::parse_quote! { + /// Update compressible config - only callable by config's update authority + pub fn update_compression_config( + ctx: Context, + new_compression_delay: Option, + new_rent_recipient: Option, + new_address_space: Option>, + new_update_authority: Option, + ) -> anchor_lang::Result<()> { + light_sdk::compressible::process_update_compression_config( + &ctx.accounts.config.to_account_info(), + &ctx.accounts.authority.to_account_info(), + new_update_authority.as_ref(), + new_rent_recipient.as_ref(), + new_address_space, + new_compression_delay, + &crate::ID, + )?; + + Ok(()) + } + }; + + // Generate the decompress_accounts_idempotent accounts struct + let decompress_accounts: ItemStruct = syn::parse_quote! { + #[derive(Accounts)] + pub struct DecompressAccountsIdempotent<'info> { + #[account(mut)] + pub fee_payer: Signer<'info>, + /// UNCHECKED: Anyone can pay to init. + #[account(mut)] + pub rent_payer: Signer<'info>, + /// The global config account + /// CHECK: load_checked. + pub config: AccountInfo<'info>, + // Remaining accounts: + // - First N accounts: PDA accounts to decompress into + // - After system_accounts_offset: Light Protocol system accounts for CPI + } + }; + + // Generate the decompress_accounts_idempotent instruction + let decompress_instruction: ItemFn = syn::parse_quote! { + /// Decompresses multiple compressed PDAs of any supported account type in a single transaction + pub fn decompress_accounts_idempotent<'info>( + ctx: Context<'_, '_, '_, 'info, DecompressAccountsIdempotent<'info>>, + proof: light_sdk::instruction::ValidityProof, + compressed_accounts: Vec, + bumps: Vec, + system_accounts_offset: u8, + ) -> anchor_lang::Result<()> { + // Get PDA accounts from remaining accounts + let pda_accounts_end = system_accounts_offset as usize; + let solana_accounts = &ctx.remaining_accounts[..pda_accounts_end]; + + // Validate we have matching number of PDAs, compressed accounts, and bumps + if solana_accounts.len() != compressed_accounts.len() || solana_accounts.len() != bumps.len() { + return err!(ErrorCode::InvalidAccountCount); + } + + let cpi_accounts = light_sdk::cpi::CpiAccounts::new( + &ctx.accounts.fee_payer, + &ctx.remaining_accounts[system_accounts_offset as usize..], + LIGHT_CPI_SIGNER, + ); + + // Get address space from config checked. + let config = light_sdk::compressible::CompressibleConfig::load_checked(&ctx.accounts.config, &crate::ID)?; + let address_space = config.address_space[0]; + + let mut all_compressed_infos = Vec::with_capacity(compressed_accounts.len()); + + for (i, (compressed_data, &bump)) in compressed_accounts + .into_iter() + .zip(bumps.iter()) + .enumerate() + { + let bump_slice = [bump]; + + match compressed_data.data { + #( + CompressedAccountVariant::#struct_names(data) => { + let mut seeds_refs = Vec::with_capacity(compressed_data.seeds.len() + 1); + for seed in &compressed_data.seeds { + seeds_refs.push(seed.as_slice()); + } + seeds_refs.push(&bump_slice); + + // Create LightAccount with correct discriminator + let light_account = light_sdk::account::sha::LightAccount::<'_, #struct_names>::new_mut( + &crate::ID, + &compressed_data.meta, + data, + )?; + + // Process this single account + let compressed_infos = light_sdk::compressible::prepare_accounts_for_decompress_idempotent::<#struct_names>( + &[&solana_accounts[i]], + vec![light_account], + &[seeds_refs.as_slice()], + &cpi_accounts, + &ctx.accounts.rent_payer, + address_space, + )?; + + all_compressed_infos.extend(compressed_infos); + } + ),* + } + } + + if all_compressed_infos.is_empty() { + msg!("No compressed accounts to decompress"); + } else { + let cpi_inputs = light_sdk::cpi::CpiInputs::new(proof, all_compressed_infos); + cpi_inputs.invoke_light_system_program(cpi_accounts)?; + } + + Ok(()) + } + }; + + // Generate error code enum if it doesn't exist + let error_code: Item = syn::parse_quote! { + #[error_code] + pub enum ErrorCode { + #[msg("Invalid account count: PDAs and compressed accounts must match")] + InvalidAccountCount, + #[msg("Rent recipient does not match config")] + InvalidRentRecipient, + } + }; + + // Add all generated items to the module + content.1.push(Item::Enum(compressed_account_variant_enum)); + content.1.push(default_impl); + content.1.push(data_hasher_impl); + content.1.push(light_discriminator_impl); + content.1.push(has_compression_info_impl); + content.1.push(size_impl); + content.1.push(Item::Struct(compressed_account_data)); + content.1.push(Item::Struct(initialize_config_accounts)); + content.1.push(Item::Struct(update_config_accounts)); + content.1.push(Item::Fn(initialize_compression_config_fn)); + content.1.push(Item::Fn(update_compression_config_fn)); + content.1.push(Item::Struct(decompress_accounts)); + content.1.push(Item::Fn(decompress_instruction)); + content.1.push(error_code); + + // Generate compress instructions for each struct + for struct_name in ident_list.idents { + let compress_fn_name = + format_ident!("compress_{}", struct_name.to_string().to_snake_case()); + let compress_accounts_name = format_ident!("Compress{}", struct_name); + + // Look for registry module generated by derive(Compressible) + let seeds_info = find_seeds_from_registry_in_module(&struct_name, &content.1)? + .ok_or_else(|| { + generate_helpful_error_message(&struct_name) + })?; + + let seeds_expr = &seeds_info.seeds; + let bump_constraint = if seeds_info.bump_field.is_some() { + quote! { bump, } + } else { + quote! {} + }; + + // Generate the compress accounts struct with extracted seeds + let compress_accounts_struct: ItemStruct = syn::parse_quote! { + #[derive(Accounts)] + pub struct #compress_accounts_name<'info> { + #[account(mut)] + pub user: Signer<'info>, + #[account( + mut, + seeds = [#(#seeds_expr),*], + #bump_constraint + )] + pub solana_account: Account<'info, #struct_name>, + /// The global config account + /// CHECK: load_checked. + pub config: AccountInfo<'info>, + /// Rent recipient - validated against config + pub rent_recipient: AccountInfo<'info>, + } + }; + + // Generate the compress instruction function + let compress_instruction_fn: ItemFn = syn::parse_quote! { + /// Compresses a #struct_name PDA using config values + pub fn #compress_fn_name<'info>( + ctx: Context<'_, '_, '_, 'info, #compress_accounts_name<'info>>, + proof: light_sdk::instruction::ValidityProof, + compressed_account_meta: light_sdk_types::instruction::account_meta::CompressedAccountMeta, + ) -> anchor_lang::Result<()> { + // Load config from AccountInfo + let config = light_sdk::compressible::CompressibleConfig::load_checked( + &ctx.accounts.config, + &crate::ID + ).map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotDeserialize)?; + + // Verify rent recipient matches config + if ctx.accounts.rent_recipient.key() != config.rent_recipient { + return err!(ErrorCode::InvalidRentRecipient); + } + + let cpi_accounts = light_sdk::cpi::CpiAccounts::new( + &ctx.accounts.user, + &ctx.remaining_accounts[..], + LIGHT_CPI_SIGNER, + ); + + light_sdk::compressible::compress_account::<#struct_name>( + &mut ctx.accounts.solana_account, + &compressed_account_meta, + proof, + cpi_accounts, + &ctx.accounts.rent_recipient, + &config.compression_delay, + ) + .map_err(|e| anchor_lang::prelude::ProgramError::from(e))?; + + Ok(()) + } + }; + + // Generate Size implementation for the struct + let size_impl: Item = syn::parse_quote! { + impl light_sdk::Size for #struct_name { + fn size(&self) -> usize { + Self::LIGHT_DISCRIMINATOR.len() + Self::INIT_SPACE + } + } + }; + + // Add the generated items to the module (only compress, not create) + content.1.push(Item::Struct(compress_accounts_struct)); + content.1.push(Item::Fn(compress_instruction_fn)); + content.1.push(size_impl); + } + + Ok(quote! { + #module + }) +} + +/// Find seeds from registry functions generated by derive(Compressible) +fn find_seeds_from_registry(account_type: &Ident) -> Result> { + // For now, return a placeholder - we'll implement the actual registry lookup later + // The registry approach needs access to the module content to scan for generated modules + + // Return None for now - this will trigger the error message + // We need to pass the module content to this function to make it work + Ok(None) +} + +/// Find seeds from registry by scanning module content for generated seed modules +fn find_seeds_from_registry_in_module(account_type: &Ident, module_items: &[Item]) -> Result> { + let expected_module_name = format!("__compressible_seeds_{}", account_type.to_string().to_lowercase()); + + // Look for the generated seed module + for item in module_items { + if let Item::Mod(item_mod) = item { + if item_mod.ident.to_string() == expected_module_name { + // Found the seed module! Parse its contents + if let Some((_, ref mod_items)) = &item_mod.content { + return parse_seed_module_contents(mod_items); + } + } + } + } + + Ok(None) +} + +/// Parse the contents of a generated seed module to extract seed information +fn parse_seed_module_contents(module_items: &[Item]) -> Result> { + let mut has_bump = false; + let mut seeds = Vec::new(); + + // Look for the HAS_BUMP constant and get_seeds function + for item in module_items { + match item { + Item::Const(item_const) => { + if item_const.ident == "HAS_BUMP" { + // Parse the boolean value + if let syn::Expr::Lit(expr_lit) = &*item_const.expr { + if let syn::Lit::Bool(lit_bool) = &expr_lit.lit { + has_bump = lit_bool.value; + } + } + } + } + Item::Fn(item_fn) => { + if item_fn.sig.ident == "get_seeds" { + // Parse the function body to extract seed expressions + seeds = extract_seeds_from_function_body(&item_fn.block)?; + } + } + _ => {} + } + } + + if seeds.is_empty() { + return Ok(None); + } + + Ok(Some(SeedInfo { + seeds, + bump_field: if has_bump { Some(format_ident!("bump")) } else { None }, + })) +} + +/// Extract seed expressions from the get_seeds function body +fn extract_seeds_from_function_body(block: &syn::Block) -> Result> { + // Look for the pattern: let _ = vec![seed1, seed2, ...]; + for stmt in &block.stmts { + if let syn::Stmt::Local(local) = stmt { + if let Some(init) = &local.init { + if let syn::Expr::Macro(expr_macro) = &*init.expr { + // Check if this is a vec![] macro + if expr_macro.mac.path.is_ident("vec") { + // Parse the vec![] contents as a bracketed list + let seeds_tokens = &expr_macro.mac.tokens; + + // Use syn::parse::ParseBuffer to parse the comma-separated expressions + let parsed_seeds = syn::parse::Parser::parse2( + syn::punctuated::Punctuated::::parse_terminated, + seeds_tokens.clone() + )?; + + return Ok(parsed_seeds.into_iter().collect()); + } + } + } + } + } + + Ok(Vec::new()) +} + +/// Generate a helpful error message for missing seeds +fn generate_helpful_error_message(struct_name: &Ident) -> syn::Error { + let error_msg = format!( + "No seed registry found for type '{}'.\n\n\ + To use this type with #[add_compressible_instructions], you need to:\n\n\ + 1. Apply #[derive(Compressible)] to an instruction struct that initializes this account type:\n\n\ + #[derive(Accounts, Compressible)]\n\ + pub struct Initialize<'info> {{\n\ + #[account(\n\ + init,\n\ + seeds = [\n\ + // Your seeds here\n\ + b\"my_seed\",\n\ + authority.key().as_ref(),\n\ + ],\n\ + bump\n\ + )]\n\ + pub {}: Account<'info, {}>,\n\ + pub authority: Signer<'info>,\n\ + }}\n\n\ + 2. Make sure the instruction struct is imported in the same module where #[add_compressible_instructions] is used:\n\n\ + pub use crate::instructions::initialize::Initialize;\n\n\ + The derive(Compressible) macro will generate a seed registry that this macro can automatically discover.", + struct_name, + struct_name.to_string().to_snake_case(), + struct_name + ); + + syn::Error::new_spanned(struct_name, error_msg) +} + +/// Generates HasCompressionInfo trait implementation for a struct with compression_info field +pub fn derive_has_compression_info(input: syn::ItemStruct) -> Result { + let struct_name = input.ident.clone(); + + // Find the compression_info field + let compression_info_field = match &input.fields { + syn::Fields::Named(fields) => fields.named.iter().find(|field| { + field + .ident + .as_ref() + .map(|ident| ident == "compression_info") + .unwrap_or(false) + }), + _ => { + return Err(syn::Error::new_spanned( + &struct_name, + "HasCompressionInfo can only be derived for structs with named fields", + )) + } + }; + + let _compression_info_field = compression_info_field.ok_or_else(|| { + syn::Error::new_spanned( + &struct_name, + "HasCompressionInfo requires a field named 'compression_info' of type Option" + ) + })?; + + // Validate that the field is Option + // For now, we'll assume it's correct and let the compiler catch type errors + + let has_compression_info_impl = quote! { + impl light_sdk::compressible::HasCompressionInfo for #struct_name { + fn compression_info(&self) -> &light_sdk::compressible::CompressionInfo { + self.compression_info + .as_ref() + .expect("CompressionInfo must be Some on-chain") + } + + fn compression_info_mut(&mut self) -> &mut light_sdk::compressible::CompressionInfo { + self.compression_info + .as_mut() + .expect("CompressionInfo must be Some on-chain") + } + + fn compression_info_mut_opt(&mut self) -> &mut Option { + &mut self.compression_info + } + + fn set_compression_info_none(&mut self) { + self.compression_info = None; + } + } + }; + + Ok(has_compression_info_impl) +} diff --git a/sdk-libs/macros/src/compressible_derive.rs b/sdk-libs/macros/src/compressible_derive.rs new file mode 100644 index 0000000000..b0435cf983 --- /dev/null +++ b/sdk-libs/macros/src/compressible_derive.rs @@ -0,0 +1,241 @@ +use proc_macro2::TokenStream; +use quote::{format_ident, quote}; +use syn::{ + bracketed, parse::Parse, punctuated::Punctuated, Attribute, DeriveInput, Expr, Field, Fields, + GenericArgument, Ident, PathArguments, Result, Token, Type, TypePath, +}; + +/// Information about a compressible account field found in an instruction struct +#[derive(Debug, Clone)] +struct CompressibleFieldInfo { + /// The account type (e.g., PoolState) + account_type: Ident, + /// The field name in the instruction struct (e.g., pool_state) + field_name: Ident, + /// The seeds expressions from the #[account] attribute + seeds: Vec, + /// Whether the field has a bump constraint + has_bump: bool, +} + +/// Parse a derive input and generate compressible registry functions +pub(crate) fn derive_compressible(input: DeriveInput) -> Result { + let struct_name = &input.ident; + + // Extract fields from the struct + let fields = match &input.data { + syn::Data::Struct(data_struct) => match &data_struct.fields { + Fields::Named(fields) => &fields.named, + _ => { + return Err(syn::Error::new_spanned( + struct_name, + "Compressible can only be derived for structs with named fields", + )) + } + }, + _ => { + return Err(syn::Error::new_spanned( + struct_name, + "Compressible can only be derived for structs", + )) + } + }; + + // Find all fields that have init + seeds constraints + let mut compressible_fields = Vec::new(); + + for field in fields { + if let Some(field_info) = extract_compressible_field_info(field)? { + compressible_fields.push(field_info); + } + } + + if compressible_fields.is_empty() { + return Err(syn::Error::new_spanned( + struct_name, + "No compressible fields found. Expected at least one field with #[account(init, seeds = [...], bump)]", + )); + } + + // Generate registry functions for each compressible field + let mut generated_functions = Vec::new(); + + for field_info in compressible_fields { + let registry_fn = generate_seed_registry_function(&field_info)?; + generated_functions.push(registry_fn); + } + + Ok(quote! { + #(#generated_functions)* + }) +} + +/// Extract compressible field information from a struct field +fn extract_compressible_field_info(field: &Field) -> Result> { + let field_name = field.ident.as_ref().ok_or_else(|| { + syn::Error::new_spanned(field, "Field must have a name") + })?; + + // Extract account type from the field type (e.g., Account<'info, PoolState> -> PoolState) + let account_type = extract_account_type(&field.ty)?; + + if account_type.is_none() { + // This field is not an Account type, skip it + return Ok(None); + } + + let account_type = account_type.unwrap(); + + // Look for #[account] attribute with init and seeds + for attr in &field.attrs { + if attr.path().is_ident("account") { + if let Some((has_init, seeds, has_bump)) = parse_account_attribute(attr)? { + if has_init && !seeds.is_empty() { + return Ok(Some(CompressibleFieldInfo { + account_type, + field_name: field_name.clone(), + seeds, + has_bump, + })); + } + } + } + } + + Ok(None) +} + +/// Extract the account type from a field type like Account<'info, T> -> T +fn extract_account_type(ty: &Type) -> Result> { + match ty { + Type::Path(type_path) => { + if let Some(last_segment) = type_path.path.segments.last() { + let segment_name = last_segment.ident.to_string(); + + // Check for Account, Box, etc. + if is_account_wrapper(&segment_name) { + return extract_account_type_from_generics(&last_segment.arguments); + } + + // Handle Box> + if segment_name == "Box" { + if let PathArguments::AngleBracketed(args) = &last_segment.arguments { + for arg in &args.args { + if let GenericArgument::Type(inner_type) = arg { + if let Some(account_type) = extract_account_type(inner_type)? { + return Ok(Some(account_type)); + } + } + } + } + } + } + } + Type::Reference(type_ref) => { + // Handle &Account<...> or &mut Account<...> + return extract_account_type(&type_ref.elem); + } + _ => {} + } + + Ok(None) +} + +/// Check if a type name is an account wrapper (Account, AccountLoader, InterfaceAccount, etc.) +fn is_account_wrapper(type_name: &str) -> bool { + matches!(type_name, "Account" | "AccountLoader" | "InterfaceAccount") +} + +/// Extract account type from generic arguments like Account<'info, PoolState> -> PoolState +fn extract_account_type_from_generics(args: &PathArguments) -> Result> { + if let PathArguments::AngleBracketed(args) = args { + // Look for the account type (usually the second generic argument after lifetime) + for arg in &args.args { + if let GenericArgument::Type(Type::Path(TypePath { path, .. })) = arg { + if let Some(last_segment) = path.segments.last() { + // Skip lifetime parameters + if last_segment.ident.to_string().starts_with('_') || + last_segment.ident.to_string() == "info" { + continue; + } + return Ok(Some(last_segment.ident.clone())); + } + } + } + } + Ok(None) +} + +/// Parse account attribute to extract init, seeds, and bump information +fn parse_account_attribute(attr: &Attribute) -> Result, bool)>> { + if !attr.path().is_ident("account") { + return Ok(None); + } + + let mut has_init = false; + let mut seeds = Vec::new(); + let mut has_bump = false; + + // Parse the attribute content + attr.parse_nested_meta(|meta| { + if meta.path.is_ident("init") { + has_init = true; + Ok(()) + } else if meta.path.is_ident("bump") { + has_bump = true; + Ok(()) + } else if meta.path.is_ident("seeds") { + // Parse seeds = [...] + if meta.input.peek(Token![=]) { + meta.input.parse::()?; // Consume the equals sign + let content; + bracketed!(content in meta.input); + let seed_exprs: Punctuated = + content.parse_terminated(Expr::parse, Token![,])?; + seeds = seed_exprs.into_iter().collect(); + } + Ok(()) + } else { + // Skip other attributes like payer, space, etc. + if meta.input.peek(Token![=]) { + meta.input.parse::()?; + meta.input.parse::()?; + } + Ok(()) + } + })?; + + Ok(Some((has_init, seeds, has_bump))) +} + +/// Generate a seed registry function for a compressible field +fn generate_seed_registry_function(field_info: &CompressibleFieldInfo) -> Result { + let account_type = &field_info.account_type; + let seeds = &field_info.seeds; + let has_bump = field_info.has_bump; + + // Generate a module with a predictable name that the main macro can find + let module_name = format_ident!("__compressible_seeds_{}", account_type.to_string().to_lowercase()); + + Ok(quote! { + #[doc(hidden)] + #[allow(non_snake_case)] + pub mod #module_name { + use super::*; + + // Export the account type for verification + pub type AccountType = super::#account_type; + + // Export the seed information in a format the main macro can parse + pub const HAS_BUMP: bool = #has_bump; + + // Generate a function that returns the seeds + // The main macro will look for this function signature and extract the seeds from its body + pub fn get_seeds() -> Vec<()> { + // The main macro will parse the expressions inside this block + let _ = vec![#(#seeds),*]; + vec![] + } + } + }) +} \ No newline at end of file diff --git a/sdk-libs/macros/src/cpi_signer.rs b/sdk-libs/macros/src/cpi_signer.rs index d27403df1d..87747e20b4 100644 --- a/sdk-libs/macros/src/cpi_signer.rs +++ b/sdk-libs/macros/src/cpi_signer.rs @@ -2,6 +2,8 @@ use proc_macro::TokenStream; use quote::quote; use syn::{parse_macro_input, LitStr}; +// TODO: review where needed. +#[allow(dead_code)] pub fn derive_light_cpi_signer_pda(input: TokenStream) -> TokenStream { // Parse the input - just a program ID string literal let program_id_lit = parse_macro_input!(input as LitStr); diff --git a/sdk-libs/macros/src/discriminator.rs b/sdk-libs/macros/src/discriminator.rs index 1d289db888..be711224c0 100644 --- a/sdk-libs/macros/src/discriminator.rs +++ b/sdk-libs/macros/src/discriminator.rs @@ -4,14 +4,34 @@ use quote::quote; use syn::{ItemStruct, Result}; pub(crate) fn discriminator(input: ItemStruct) -> Result { + discriminator_with_hasher(input, false) +} + +pub(crate) fn discriminator_sha(input: ItemStruct) -> Result { + discriminator_with_hasher(input, true) +} + +fn discriminator_with_hasher(input: ItemStruct, is_sha: bool) -> Result { let account_name = &input.ident; let (impl_gen, type_gen, where_clause) = input.generics.split_for_impl(); let mut discriminator = [0u8; 8]; - discriminator.copy_from_slice(&Sha256::hash(account_name.to_string().as_bytes()).unwrap()[..8]); + + // When anchor-discriminator-compat feature is enabled, use "account:" prefix like Anchor does + #[cfg(feature = "anchor-discriminator-compat")] + let hash_input = format!("account:{}", account_name); + + #[cfg(not(feature = "anchor-discriminator-compat"))] + let hash_input = account_name.to_string(); + + discriminator.copy_from_slice(&Sha256::hash(hash_input.as_bytes()).unwrap()[..8]); let discriminator: proc_macro2::TokenStream = format!("{discriminator:?}").parse().unwrap(); + // For SHA256 variant, we could add specific logic here if needed + // Currently both variants work the same way since discriminator is just based on struct name + let _variant_marker = if is_sha { "sha256" } else { "poseidon" }; + Ok(quote! { impl #impl_gen LightDiscriminator for #account_name #type_gen #where_clause { const LIGHT_DISCRIMINATOR: [u8; 8] = #discriminator; @@ -44,7 +64,55 @@ mod tests { let output = discriminator(input).unwrap(); let output = output.to_string(); + assert!(output.contains("impl LightDiscriminator for MyAccount")); + + // The discriminator value will be different based on whether anchor-discriminator-compat is enabled + #[cfg(feature = "anchor-discriminator-compat")] + assert!(output.contains("account:MyAccount")); // This won't be visible in output, but logic uses it + + #[cfg(not(feature = "anchor-discriminator-compat"))] + assert!(output.contains("[181 , 255 , 112 , 42 , 17 , 188 , 66 , 199]")); + } + + #[test] + fn test_discriminator_sha() { + let input: ItemStruct = parse_quote! { + struct MyAccount { + a: u32, + b: i32, + c: u64, + d: i64, + } + }; + + let output = discriminator_sha(input).unwrap(); + let output = output.to_string(); + assert!(output.contains("impl LightDiscriminator for MyAccount")); assert!(output.contains("[181 , 255 , 112 , 42 , 17 , 188 , 66 , 199]")); } + + #[test] + fn test_discriminator_sha_large_struct() { + // Test that SHA256 discriminator can handle large structs (that would fail with regular hasher) + let input: ItemStruct = parse_quote! { + struct LargeAccount { + pub field1: u64, pub field2: u64, pub field3: u64, pub field4: u64, + pub field5: u64, pub field6: u64, pub field7: u64, pub field8: u64, + pub field9: u64, pub field10: u64, pub field11: u64, pub field12: u64, + pub field13: u64, pub field14: u64, pub field15: u64, + pub owner: solana_program::pubkey::Pubkey, + pub authority: solana_program::pubkey::Pubkey, + } + }; + + let result = discriminator_sha(input); + assert!( + result.is_ok(), + "SHA256 discriminator should handle large structs" + ); + + let output = result.unwrap().to_string(); + assert!(output.contains("impl LightDiscriminator for LargeAccount")); + } } diff --git a/sdk-libs/macros/src/hasher/data_hasher.rs b/sdk-libs/macros/src/hasher/data_hasher.rs index 2486fdd4b7..7d27bdc619 100644 --- a/sdk-libs/macros/src/hasher/data_hasher.rs +++ b/sdk-libs/macros/src/hasher/data_hasher.rs @@ -37,7 +37,14 @@ pub(crate) fn generate_data_hasher_impl( slices[num_flattned_fields] = element.as_slice(); } - H::hashv(slices.as_slice()) + let mut result = H::hashv(slices.as_slice())?; + + // Apply field size truncation for non-Poseidon hashers + if H::ID != 0 { + result[0] = 0; + } + + Ok(result) } } } @@ -59,10 +66,50 @@ pub(crate) fn generate_data_hasher_impl( println!("DataHasher::hash inputs {:?}", debug_prints); } } - H::hashv(&[ + let mut result = H::hashv(&[ #(#data_hasher_assignments.as_slice(),)* - ]) + ])?; + + // Apply field size truncation for non-Poseidon hashers + if H::ID != 0 { + result[0] = 0; + } + + Ok(result) + } + } + } + }; + + Ok(hasher_impl) +} + +/// SHA256-specific DataHasher implementation that serializes the whole struct +pub(crate) fn generate_data_hasher_impl_sha( + struct_name: &syn::Ident, + generics: &syn::Generics, +) -> Result { + let (impl_gen, type_gen, where_clause) = generics.split_for_impl(); + + let hasher_impl = quote! { + impl #impl_gen ::light_hasher::DataHasher for #struct_name #type_gen #where_clause { + fn hash(&self) -> ::std::result::Result<[u8; 32], ::light_hasher::HasherError> + where + H: ::light_hasher::Hasher + { + use ::light_hasher::Hasher; + use borsh::BorshSerialize; + + // For SHA256, we serialize the whole struct and hash it in one go + let serialized = self.try_to_vec().map_err(|_| ::light_hasher::HasherError::BorshError)?; + let mut result = H::hash(&serialized)?; + + // Truncate field size for non-Poseidon hashers + if H::ID != 0 { + result[0] = 0; } + + Ok(result) } } }; diff --git a/sdk-libs/macros/src/hasher/input_validator.rs b/sdk-libs/macros/src/hasher/input_validator.rs index af57976b8d..0b2800e15a 100644 --- a/sdk-libs/macros/src/hasher/input_validator.rs +++ b/sdk-libs/macros/src/hasher/input_validator.rs @@ -60,6 +60,36 @@ pub(crate) fn validate_input(input: &ItemStruct) -> Result<()> { Ok(()) } +/// SHA256-specific validation - much more relaxed constraints +pub(crate) fn validate_input_sha(input: &ItemStruct) -> Result<()> { + // Check that we have a struct with named fields + match &input.fields { + Fields::Named(_) => (), + _ => { + return Err(Error::new_spanned( + input, + "Only structs with named fields are supported", + )) + } + }; + + // For SHA256, we don't limit field count or require specific attributes + // Just ensure flatten is not used (not implemented for SHA256 path) + let flatten_field_exists = input + .fields + .iter() + .any(|field| get_field_attribute(field) == FieldAttribute::Flatten); + + if flatten_field_exists { + return Err(Error::new_spanned( + input, + "Flatten attribute is not supported in SHA256 hasher.", + )); + } + + Ok(()) +} + /// Gets the primary attribute for a field (only one attribute can be active) pub(crate) fn get_field_attribute(field: &Field) -> FieldAttribute { if field.attrs.iter().any(|attr| attr.path().is_ident("hash")) { diff --git a/sdk-libs/macros/src/hasher/light_hasher.rs b/sdk-libs/macros/src/hasher/light_hasher.rs index 911cc35f73..fbb9da4271 100644 --- a/sdk-libs/macros/src/hasher/light_hasher.rs +++ b/sdk-libs/macros/src/hasher/light_hasher.rs @@ -3,10 +3,10 @@ use quote::quote; use syn::{Fields, ItemStruct, Result}; use crate::hasher::{ - data_hasher::generate_data_hasher_impl, + data_hasher::{generate_data_hasher_impl, generate_data_hasher_impl_sha}, field_processor::{process_field, FieldProcessingContext}, - input_validator::{get_field_attribute, validate_input, FieldAttribute}, - to_byte_array::generate_to_byte_array_impl, + input_validator::{get_field_attribute, validate_input, validate_input_sha, FieldAttribute}, + to_byte_array::{generate_to_byte_array_impl_sha, generate_to_byte_array_impl_with_hasher}, }; /// - ToByteArray: @@ -49,6 +49,33 @@ use crate::hasher::{ /// - Enums, References, SmartPointers: /// - Not supported pub(crate) fn derive_light_hasher(input: ItemStruct) -> Result { + derive_light_hasher_with_hasher(input, "e!(::light_hasher::Poseidon)) +} + +pub(crate) fn derive_light_hasher_sha(input: ItemStruct) -> Result { + // Use SHA256-specific validation (no field count limits) + validate_input_sha(&input)?; + + let generics = input.generics.clone(); + + let fields = match &input.fields { + Fields::Named(fields) => fields.clone(), + _ => unreachable!("Validation should have caught this"), + }; + + let field_count = fields.named.len(); + + let to_byte_array_impl = generate_to_byte_array_impl_sha(&input.ident, &generics, field_count)?; + let data_hasher_impl = generate_data_hasher_impl_sha(&input.ident, &generics)?; + + Ok(quote! { + #to_byte_array_impl + + #data_hasher_impl + }) +} + +fn derive_light_hasher_with_hasher(input: ItemStruct, hasher: &TokenStream) -> Result { // Validate the input structure validate_input(&input)?; @@ -74,8 +101,13 @@ pub(crate) fn derive_light_hasher(input: ItemStruct) -> Result { process_field(field, i, &mut context); }); - let to_byte_array_impl = - generate_to_byte_array_impl(&input.ident, &generics, field_count, &context)?; + let to_byte_array_impl = generate_to_byte_array_impl_with_hasher( + &input.ident, + &generics, + field_count, + &context, + hasher, + )?; let data_hasher_impl = generate_data_hasher_impl(&input.ident, &generics, &context)?; @@ -244,7 +276,7 @@ impl ::light_hasher::DataHasher for TruncateOptionStruct { #[cfg(debug_assertions)] { if std::env::var("RUST_BACKTRACE").is_ok() { - let debug_prints: Vec<[u8; 32]> = vec![ + let debug_prints: Vec<[u8;32]> = vec![ if let Some(a) = & self.a { let result = a.hash_to_field_size() ?; if result == [0u8; 32] { return Err(::light_hasher::errors::HasherError::OptionHashToFieldSizeZero); } @@ -405,4 +437,277 @@ impl ::light_hasher::DataHasher for OuterStruct { }; assert!(derive_light_hasher(input).is_ok()); } + + #[test] + fn test_sha256_large_struct_with_pubkeys() { + // Test that SHA256 can handle large structs with Pubkeys that would fail with Poseidon + // This struct has 15 fields including Pubkeys without #[hash] attribute + let input: ItemStruct = parse_quote! { + struct LargeAccountSha { + pub field1: u64, + pub field2: u64, + pub field3: u64, + pub field4: u64, + pub field5: u64, + pub field6: u64, + pub field7: u64, + pub field8: u64, + pub field9: u64, + pub field10: u64, + pub field11: u64, + pub field12: u64, + pub field13: u64, + // Pubkeys without #[hash] attribute - this would fail with Poseidon + pub owner: solana_program::pubkey::Pubkey, + pub authority: solana_program::pubkey::Pubkey, + } + }; + + // SHA256 should handle this fine + let sha_result = derive_light_hasher_sha(input.clone()); + assert!( + sha_result.is_ok(), + "SHA256 should handle large structs with Pubkeys" + ); + + // Regular Poseidon hasher should fail due to field count (>12) and Pubkey without #[hash] + let poseidon_result = derive_light_hasher(input); + assert!( + poseidon_result.is_err(), + "Poseidon should fail with >12 fields and unhashed Pubkeys" + ); + } + + #[test] + fn test_sha256_vs_poseidon_hashing_behavior() { + // Test a struct that both can handle to show the difference in hashing approach + let input: ItemStruct = parse_quote! { + struct TestAccount { + pub data: [u8; 31], + pub counter: u64, + } + }; + + // Both should succeed + let sha_result = derive_light_hasher_sha(input.clone()); + assert!(sha_result.is_ok()); + + let poseidon_result = derive_light_hasher(input); + assert!(poseidon_result.is_ok()); + + // Verify SHA256 implementation serializes whole struct + let sha_output = sha_result.unwrap(); + let sha_code = sha_output.to_string(); + + // SHA256 should use try_to_vec() for whole struct serialization (account for spaces) + assert!( + sha_code.contains("try_to_vec") && sha_code.contains("BorshSerialize"), + "SHA256 should serialize whole struct using try_to_vec. Actual code: {}", + sha_code + ); + assert!( + sha_code.contains("result [0] = 0") || sha_code.contains("result[0] = 0"), + "SHA256 should truncate first byte. Actual code: {}", + sha_code + ); + + // Poseidon should use field-by-field hashing + let poseidon_output = poseidon_result.unwrap(); + let poseidon_code = poseidon_output.to_string(); + + assert!( + poseidon_code.contains("to_byte_array") && poseidon_code.contains("as_slice"), + "Poseidon should use field-by-field hashing with to_byte_array. Actual code: {}", + poseidon_code + ); + } + + #[test] + fn test_sha256_no_field_limit() { + // Test that SHA256 doesn't enforce the 12-field limit + let input: ItemStruct = parse_quote! { + struct ManyFieldsStruct { + pub f1: u32, pub f2: u32, pub f3: u32, pub f4: u32, + pub f5: u32, pub f6: u32, pub f7: u32, pub f8: u32, + pub f9: u32, pub f10: u32, pub f11: u32, pub f12: u32, + pub f13: u32, pub f14: u32, pub f15: u32, pub f16: u32, + pub f17: u32, pub f18: u32, pub f19: u32, pub f20: u32, + } + }; + + // SHA256 should handle 20 fields without issue + let result = derive_light_hasher_sha(input); + assert!(result.is_ok(), "SHA256 should handle any number of fields"); + } + + #[test] + fn test_sha256_flatten_not_supported() { + // Test that SHA256 rejects flatten attribute (not implemented) + let input: ItemStruct = parse_quote! { + struct FlattenStruct { + #[flatten] + pub inner: InnerStruct, + pub data: u64, + } + }; + + let result = derive_light_hasher_sha(input); + assert!(result.is_err(), "SHA256 should reject flatten attribute"); + + let error_msg = result.unwrap_err().to_string(); + assert!( + error_msg.contains("not supported in SHA256"), + "Should mention SHA256 limitation" + ); + } + + #[test] + fn test_sha256_with_discriminator_integration() { + // Test that shows LightHasherSha works with LightDiscriminatorSha for large structs + // This would be impossible with regular Poseidon-based macros + let input: ItemStruct = parse_quote! { + struct LargeIntegratedAccount { + pub field1: u64, pub field2: u64, pub field3: u64, pub field4: u64, + pub field5: u64, pub field6: u64, pub field7: u64, pub field8: u64, + pub field9: u64, pub field10: u64, pub field11: u64, pub field12: u64, + pub field13: u64, pub field14: u64, pub field15: u64, pub field16: u64, + pub field17: u64, pub field18: u64, pub field19: u64, pub field20: u64, + // Pubkeys without #[hash] attribute + pub owner: solana_program::pubkey::Pubkey, + pub authority: solana_program::pubkey::Pubkey, + pub delegate: solana_program::pubkey::Pubkey, + } + }; + + // Both SHA256 hasher and discriminator should work + let sha_hasher_result = derive_light_hasher_sha(input.clone()); + assert!( + sha_hasher_result.is_ok(), + "SHA256 hasher should work with large structs" + ); + + let sha_discriminator_result = crate::discriminator::discriminator_sha(input.clone()); + assert!( + sha_discriminator_result.is_ok(), + "SHA256 discriminator should work with large structs" + ); + + // Regular Poseidon variants should fail + let poseidon_hasher_result = derive_light_hasher(input); + assert!( + poseidon_hasher_result.is_err(), + "Poseidon hasher should fail with large structs" + ); + + // Verify the generated code contains expected patterns + let sha_hasher_code = sha_hasher_result.unwrap().to_string(); + assert!( + sha_hasher_code.contains("try_to_vec"), + "Should use serialization approach" + ); + assert!( + sha_hasher_code.contains("BorshSerialize"), + "Should use Borsh serialization" + ); + + let sha_discriminator_code = sha_discriminator_result.unwrap().to_string(); + assert!( + sha_discriminator_code.contains("LightDiscriminator"), + "Should implement LightDiscriminator" + ); + assert!( + sha_discriminator_code.contains("LIGHT_DISCRIMINATOR"), + "Should provide discriminator constant" + ); + } + + #[test] + fn test_complete_sha256_ecosystem_practical_example() { + // Demonstrates a real-world scenario where SHA256 variants are essential + // This struct would be impossible with Poseidon due to: + // 1. >12 fields (23+ fields) + // 2. Multiple Pubkeys without #[hash] attribute + // 3. Large data structures + let input: ItemStruct = parse_quote! { + pub struct ComplexGameState { + // Game metadata (13 fields) + pub game_id: u64, + pub round: u32, + pub turn: u8, + pub phase: u8, + pub start_time: i64, + pub end_time: i64, + pub max_players: u8, + pub current_players: u8, + pub entry_fee: u64, + pub prize_pool: u64, + pub game_mode: u32, + pub difficulty: u8, + pub status: u8, + + // Player information (6 Pubkey fields - would require #[hash] with Poseidon) + pub creator: solana_program::pubkey::Pubkey, + pub winner: solana_program::pubkey::Pubkey, + pub current_player: solana_program::pubkey::Pubkey, + pub authority: solana_program::pubkey::Pubkey, + pub treasury: solana_program::pubkey::Pubkey, + pub program_id: solana_program::pubkey::Pubkey, + + // Game state data (4+ more fields) + pub board_state: [u8; 64], // Large array + pub player_scores: [u32; 8], // Array of scores + pub moves_history: [u16; 32], // Move history + pub special_flags: u32, + + // This gives us 23+ fields total - way beyond Poseidon's 12-field limit + } + }; + + // SHA256 variants should handle this complex struct effortlessly + let sha_hasher_result = derive_light_hasher_sha(input.clone()); + assert!( + sha_hasher_result.is_ok(), + "SHA256 hasher must handle complex real-world structs" + ); + + let sha_discriminator_result = crate::discriminator::discriminator_sha(input.clone()); + assert!( + sha_discriminator_result.is_ok(), + "SHA256 discriminator must handle complex real-world structs" + ); + + // Poseidon would fail with this struct + let poseidon_result = derive_light_hasher(input); + assert!( + poseidon_result.is_err(), + "Poseidon cannot handle structs with >12 fields and unhashed Pubkeys" + ); + + // Verify SHA256 generates efficient serialization-based code + let hasher_code = sha_hasher_result.unwrap().to_string(); + assert!( + hasher_code.contains("try_to_vec"), + "Should serialize entire struct efficiently" + ); + assert!( + hasher_code.contains("BorshSerialize"), + "Should use Borsh for serialization" + ); + assert!( + hasher_code.contains("result [0] = 0") || hasher_code.contains("result[0] = 0"), + "Should apply field size truncation. Actual code: {}", + hasher_code + ); + + // Verify discriminator works correctly + let discriminator_code = sha_discriminator_result.unwrap().to_string(); + assert!( + discriminator_code.contains("ComplexGameState"), + "Should target correct struct" + ); + assert!( + discriminator_code.contains("LIGHT_DISCRIMINATOR"), + "Should provide discriminator constant" + ); + } } diff --git a/sdk-libs/macros/src/hasher/mod.rs b/sdk-libs/macros/src/hasher/mod.rs index 5c81807edf..c2ebd8034e 100644 --- a/sdk-libs/macros/src/hasher/mod.rs +++ b/sdk-libs/macros/src/hasher/mod.rs @@ -4,4 +4,4 @@ mod input_validator; mod light_hasher; mod to_byte_array; -pub(crate) use light_hasher::derive_light_hasher; +pub(crate) use light_hasher::{derive_light_hasher, derive_light_hasher_sha}; diff --git a/sdk-libs/macros/src/hasher/to_byte_array.rs b/sdk-libs/macros/src/hasher/to_byte_array.rs index 27d49ae232..9cec46c117 100644 --- a/sdk-libs/macros/src/hasher/to_byte_array.rs +++ b/sdk-libs/macros/src/hasher/to_byte_array.rs @@ -4,11 +4,12 @@ use syn::Result; use crate::hasher::field_processor::FieldProcessingContext; -pub(crate) fn generate_to_byte_array_impl( +pub(crate) fn generate_to_byte_array_impl_with_hasher( struct_name: &syn::Ident, generics: &syn::Generics, field_count: usize, context: &FieldProcessingContext, + hasher: &TokenStream, ) -> Result { let (impl_gen, type_gen, where_clause) = generics.split_for_impl(); @@ -20,34 +21,70 @@ pub(crate) fn generate_to_byte_array_impl( Some(s) => s, None => &alt_res, }; - let field_assignment: TokenStream = syn::parse_str(str)?; - - // Create a token stream with the field_assignment and the import code - let mut hash_imports = proc_macro2::TokenStream::new(); - for code in &context.hash_to_field_size_code { - hash_imports.extend(code.clone()); - } + let content: TokenStream = str.parse().expect("Invalid generated code"); Ok(quote! { impl #impl_gen ::light_hasher::to_byte_array::ToByteArray for #struct_name #type_gen #where_clause { - const NUM_FIELDS: usize = #field_count; + const NUM_FIELDS: usize = 1; fn to_byte_array(&self) -> ::std::result::Result<[u8; 32], ::light_hasher::HasherError> { - #hash_imports - #field_assignment + use ::light_hasher::to_byte_array::ToByteArray; + use ::light_hasher::hash_to_field_size::HashToFieldSize; + #content } } }) } else { + let data_hasher_assignments = &context.data_hasher_assignments; Ok(quote! { impl #impl_gen ::light_hasher::to_byte_array::ToByteArray for #struct_name #type_gen #where_clause { const NUM_FIELDS: usize = #field_count; fn to_byte_array(&self) -> ::std::result::Result<[u8; 32], ::light_hasher::HasherError> { - ::light_hasher::DataHasher::hash::<::light_hasher::Poseidon>(self) - } + use ::light_hasher::to_byte_array::ToByteArray; + use ::light_hasher::hash_to_field_size::HashToFieldSize; + use ::light_hasher::Hasher; + let mut result = #hasher::hashv(&[ + #(#data_hasher_assignments.as_slice(),)* + ])?; + + // Truncate field size for non-Poseidon hashers + if #hasher::ID != 0 { + result[0] = 0; + } + Ok(result) + } } }) } } + +/// SHA256-specific ToByteArray implementation that serializes the whole struct +pub(crate) fn generate_to_byte_array_impl_sha( + struct_name: &syn::Ident, + generics: &syn::Generics, + field_count: usize, +) -> Result { + let (impl_gen, type_gen, where_clause) = generics.split_for_impl(); + + Ok(quote! { + impl #impl_gen ::light_hasher::to_byte_array::ToByteArray for #struct_name #type_gen #where_clause { + const NUM_FIELDS: usize = #field_count; + + fn to_byte_array(&self) -> ::std::result::Result<[u8; 32], ::light_hasher::HasherError> { + use borsh::BorshSerialize; + use ::light_hasher::Hasher; + + // For SHA256, we can serialize the whole struct and hash it in one go + let serialized = self.try_to_vec().map_err(|_| ::light_hasher::HasherError::BorshError)?; + let mut result = ::light_hasher::Sha256::hash(&serialized)?; + + // Truncate field size for non-Poseidon hashers + result[0] = 0; + + Ok(result) + } + } + }) +} diff --git a/sdk-libs/macros/src/lib.rs b/sdk-libs/macros/src/lib.rs index 324660c861..a8ac74c60c 100644 --- a/sdk-libs/macros/src/lib.rs +++ b/sdk-libs/macros/src/lib.rs @@ -1,15 +1,19 @@ extern crate proc_macro; use accounts::{process_light_accounts, process_light_system_accounts}; -use hasher::derive_light_hasher; +use discriminator::{discriminator, discriminator_sha}; +use hasher::{derive_light_hasher, derive_light_hasher_sha}; use proc_macro::TokenStream; -use syn::{parse_macro_input, DeriveInput, ItemMod, ItemStruct}; +use syn::{parse_macro_input, DeriveInput, ItemStruct}; use traits::process_light_traits; mod account; mod accounts; +mod compressible; +mod compressible_derive; mod cpi_signer; mod discriminator; mod hasher; +mod native_compressible; mod program; mod traits; @@ -135,7 +139,35 @@ pub fn light_traits_derive(input: TokenStream) -> TokenStream { #[proc_macro_derive(LightDiscriminator)] pub fn light_discriminator(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as ItemStruct); - discriminator::discriminator(input) + discriminator(input) + .unwrap_or_else(|err| err.to_compile_error()) + .into() +} + +/// SHA256 variant of the LightDiscriminator derive macro. +/// +/// This derive macro provides the same discriminator functionality as LightDiscriminator +/// but is designed to be used with SHA256-based hashing for consistency. +/// +/// ## Example +/// +/// ```ignore +/// use light_sdk::sha::{LightHasher, LightDiscriminator}; +/// +/// #[derive(LightHasher, LightDiscriminator)] +/// pub struct LargeGameState { +/// pub field1: u64, pub field2: u64, pub field3: u64, pub field4: u64, +/// pub field5: u64, pub field6: u64, pub field7: u64, pub field8: u64, +/// pub field9: u64, pub field10: u64, pub field11: u64, pub field12: u64, +/// pub field13: u64, pub field14: u64, pub field15: u64, +/// pub owner: Pubkey, +/// pub authority: Pubkey, +/// } +/// ``` +#[proc_macro_derive(LightDiscriminatorSha)] +pub fn light_discriminator_sha(input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as ItemStruct); + discriminator_sha(input) .unwrap_or_else(|err| err.to_compile_error()) .into() } @@ -152,106 +184,67 @@ pub fn light_discriminator(input: TokenStream) -> TokenStream { /// `AsByteVec` trait. The trait is implemented by default for the most of /// standard Rust types (primitives, `String`, arrays and options carrying the /// former). If there is a field of a type not implementing the trait, there -/// are two options: -/// -/// 1. The most recommended one - annotating that type with the `light_hasher` -/// macro as well. -/// 2. Manually implementing the `AsByteVec` trait. -/// -/// # Attributes -/// -/// - `skip` - skips the given field, it doesn't get included neither in -/// `AsByteVec` nor `DataHasher` implementation. -/// - `hash` - makes sure that the byte value does not exceed the BN254 -/// prime field modulus, by hashing it (with Keccak) and truncating it to 31 -/// bytes. It's generally a good idea to use it on any field which is -/// expected to output more than 31 bytes. +/// will be a compilation error. /// -/// # Examples -/// -/// Compressed account with only primitive types as fields: +/// ## Example /// /// ```ignore +/// use light_sdk::LightHasher; +/// use solana_pubkey::Pubkey; +/// /// #[derive(LightHasher)] -/// pub struct MyCompressedAccount { -/// a: i64, -/// b: Option, +/// pub struct UserRecord { +/// pub owner: Pubkey, +/// pub name: String, +/// pub score: u64, /// } /// ``` /// -/// Compressed account with fields which might exceed the BN254 prime field: +/// ## Hash attribute +/// +/// Fields marked with `#[hash]` will be hashed to field size (31 bytes) before +/// being included in the main hash calculation. This is useful for fields that +/// exceed the field size limit (like Pubkeys which are 32 bytes). /// /// ```ignore /// #[derive(LightHasher)] -/// pub struct MyCompressedAccount { -/// a: i64 -/// b: Option, +/// pub struct GameState { /// #[hash] -/// c: [u8; 32], -/// #[hash] -/// d: String, +/// pub player: Pubkey, // Will be hashed to 31 bytes +/// pub level: u32, /// } /// ``` +#[proc_macro_derive(LightHasher, attributes(hash, skip))] +pub fn light_hasher(input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as ItemStruct); + + derive_light_hasher(input) + .unwrap_or_else(|err| err.to_compile_error()) + .into() +} + +/// SHA256 variant of the LightHasher derive macro. /// -/// Compressed account with fields we want to skip: -/// -/// ```ignore -/// #[derive(LightHasher)] -/// pub struct MyCompressedAccount { -/// a: i64 -/// b: Option, -/// #[skip] -/// c: [u8; 32], -/// } -/// ``` +/// This derive macro automatically implements the `DataHasher` and `ToByteArray` traits +/// for structs, using SHA256 as the hashing algorithm instead of Poseidon. /// -/// Compressed account with a nested struct: +/// ## Example /// /// ```ignore -/// #[derive(LightHasher)] -/// pub struct MyCompressedAccount { -/// a: i64 -/// b: Option, -/// c: MyStruct, -/// } +/// use light_sdk::sha::LightHasher; /// /// #[derive(LightHasher)] -/// pub struct MyStruct { -/// a: i32 -/// b: u32, -/// } -/// ``` -/// -/// Compressed account with a type with a custom `AsByteVec` implementation: -/// -/// ```ignore -/// #[derive(LightHasher)] -/// pub struct MyCompressedAccount { -/// a: i64 -/// b: Option, -/// c: RData, -/// } -/// -/// pub enum RData { -/// A(Ipv4Addr), -/// AAAA(Ipv6Addr), -/// CName(String), -/// } -/// -/// impl AsByteVec for RData { -/// fn as_byte_vec(&self) -> Vec> { -/// match self { -/// Self::A(ipv4_addr) => vec![ipv4_addr.octets().to_vec()], -/// Self::AAAA(ipv6_addr) => vec![ipv6_addr.octets().to_vec()], -/// Self::CName(cname) => cname.as_byte_vec(), -/// } -/// } +/// pub struct GameState { +/// #[hash] +/// pub player: Pubkey, // Will be hashed to 31 bytes +/// pub level: u32, /// } /// ``` -#[proc_macro_derive(LightHasher, attributes(skip, hash))] -pub fn light_hasher(input: TokenStream) -> TokenStream { +#[proc_macro_derive(LightHasherSha, attributes(hash, skip))] +pub fn light_hasher_sha(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as ItemStruct); - derive_light_hasher(input) + + derive_light_hasher_sha(input) .unwrap_or_else(|err| err.to_compile_error()) .into() } @@ -260,70 +253,182 @@ pub fn light_hasher(input: TokenStream) -> TokenStream { #[proc_macro_derive(DataHasher, attributes(skip, hash))] pub fn data_hasher(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as ItemStruct); - derive_light_hasher(input) + + derive_light_hasher_sha(input) .unwrap_or_else(|err| err.to_compile_error()) .into() } -#[proc_macro_attribute] -pub fn light_account(_: TokenStream, input: TokenStream) -> TokenStream { +/// Automatically implements the HasCompressionInfo trait for structs that have a +/// `compression_info: Option` field. +/// +/// This derive macro generates the required trait methods for managing compression +/// information in compressible account structs. +/// +/// ## Example +/// +/// ```ignore +/// use light_sdk::compressible::{CompressionInfo, HasCompressionInfo}; +/// +/// #[derive(HasCompressionInfo)] +/// pub struct UserRecord { +/// #[skip] +/// pub compression_info: Option, +/// pub owner: Pubkey, +/// pub name: String, +/// pub score: u64, +/// } +/// ``` +/// +/// ## Requirements +/// +/// The struct must have exactly one field named `compression_info` of type +/// `Option`. The field should be marked with `#[skip]` to +/// exclude it from hashing. +#[proc_macro_derive(HasCompressionInfo)] +pub fn has_compression_info(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as ItemStruct); - account::account(input) + + compressible::derive_has_compression_info(input) .unwrap_or_else(|err| err.to_compile_error()) .into() } +/// Adds compress instructions for the specified account types (Anchor version) +/// +/// This macro must be placed BEFORE the #[program] attribute to ensure +/// the generated instructions are visible to Anchor's macro processing. +/// +/// ## Usage +/// ``` +/// #[add_compressible_instructions(UserRecord, GameSession)] +/// #[program] +/// pub mod my_program { +/// // Your regular instructions here +/// } +/// ``` #[proc_macro_attribute] -pub fn light_program(_: TokenStream, input: TokenStream) -> TokenStream { - let input = parse_macro_input!(input as ItemMod); - program::program(input) +pub fn add_compressible_instructions(args: TokenStream, input: TokenStream) -> TokenStream { + let input = syn::parse_macro_input!(input as syn::ItemMod); + + compressible::add_compressible_instructions(args.into(), input) .unwrap_or_else(|err| err.to_compile_error()) .into() } -/// Derives a Light Protocol CPI signer address at compile time +/// Adds native compressible instructions for the specified account types /// -/// This macro computes the CPI signer PDA using the "cpi_authority" seed -/// for the given program ID at compile time. +/// This macro generates thin wrapper processor functions that you dispatch manually. /// /// ## Usage -/// /// ``` -/// use light_sdk_macros::derive_light_cpi_signer_pda; -/// // Derive CPI signer for your program -/// const CPI_SIGNER_DATA: ([u8; 32], u8) = derive_light_cpi_signer_pda!("SySTEM1eSU2p4BGQfQpimFEWWSC1XDFeun3Nqzz3rT7"); -/// const CPI_SIGNER: [u8; 32] = CPI_SIGNER_DATA.0; -/// const CPI_SIGNER_BUMP: u8 = CPI_SIGNER_DATA.1; +/// #[add_native_compressible_instructions(MyPdaAccount, AnotherAccount)] +/// pub mod compression {} /// ``` /// -/// This macro computes the PDA during compile time and returns a tuple of ([u8; 32], bump). -#[proc_macro] -pub fn derive_light_cpi_signer_pda(input: TokenStream) -> TokenStream { - cpi_signer::derive_light_cpi_signer_pda(input) +/// This generates: +/// - Unified data structures (CompressedAccountVariant enum, etc.) +/// - Instruction data structs (CreateCompressionConfigData, etc.) +/// - Processor functions (create_compression_config, compress_my_pda_account, etc.) +/// +/// You then dispatch these in your process_instruction function. +#[proc_macro_attribute] +pub fn add_native_compressible_instructions(args: TokenStream, input: TokenStream) -> TokenStream { + let input = syn::parse_macro_input!(input as syn::ItemMod); + + native_compressible::add_native_compressible_instructions(args.into(), input) + .unwrap_or_else(|err| err.to_compile_error()) + .into() +} + +#[proc_macro_attribute] +pub fn account(_: TokenStream, input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as ItemStruct); + + account::account(input) + .unwrap_or_else(|err| err.to_compile_error()) + .into() } -/// Derives a complete Light Protocol CPI configuration at compile time +/// Derive the CPI signer from the program ID. The program ID must be a string +/// literal. /// -/// This macro computes the program ID, CPI signer PDA, and bump seed -/// for the given program ID at compile time. +/// ## Example /// -/// ## Usage +/// ```ignore +/// use light_sdk::derive_light_cpi_signer; /// +/// pub const LIGHT_CPI_SIGNER: CpiSigner = +/// derive_light_cpi_signer!("8Ld9pGkCNfU6A7KdKe1YrTNYJWKMCFqVHqmUvjNmER7B"); /// ``` -/// use light_sdk_macros::derive_light_cpi_signer; -/// use light_sdk_types::CpiSigner; -/// // Derive complete CPI signer for your program -/// const LIGHT_CPI_SIGNER: CpiSigner = derive_light_cpi_signer!("SySTEM1eSU2p4BGQfQpimFEWWSC1XDFeun3Nqzz3rT7"); -/// -/// // Access individual fields: -/// const PROGRAM_ID: [u8; 32] = LIGHT_CPI_SIGNER.program_id; -/// const CPI_SIGNER: [u8; 32] = LIGHT_CPI_SIGNER.cpi_signer; -/// const BUMP: u8 = LIGHT_CPI_SIGNER.bump; -/// ``` -/// -/// This macro computes all values during compile time and returns a CpiSigner struct -/// containing the program ID, CPI signer address, and bump seed. #[proc_macro] pub fn derive_light_cpi_signer(input: TokenStream) -> TokenStream { cpi_signer::derive_light_cpi_signer(input) } + +/// Generates a Light program for the given module. +/// +/// ## Example +/// +/// ```ignore +/// use light_sdk::light_program; +/// +/// #[light_program] +/// pub mod my_program { +/// pub fn my_instruction(ctx: Context) -> Result<()> { +/// // Your instruction logic here +/// Ok(()) +/// } +/// } +/// ``` +#[proc_macro_attribute] +pub fn light_program(_: TokenStream, input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as syn::ItemMod); + + program::program(input) + .unwrap_or_else(|err| err.to_compile_error()) + .into() +} + +/// Derive seed registry for compressible accounts. +/// +/// This derive macro should be applied to Anchor instruction structs that initialize +/// compressible accounts. It extracts seed information and makes it available to +/// the `#[add_compressible_instructions]` macro. +/// +/// ## Usage +/// +/// ```ignore +/// #[derive(Accounts, Compressible)] +/// pub struct Initialize<'info> { +/// #[account( +/// init, +/// seeds = [ +/// POOL_SEED.as_bytes(), +/// amm_config.key().as_ref(), +/// token_0_mint.key().as_ref(), +/// token_1_mint.key().as_ref(), +/// ], +/// bump +/// )] +/// pub pool_state: Box>, +/// pub amm_config: AccountInfo<'info>, +/// pub token_0_mint: AccountInfo<'info>, +/// pub token_1_mint: AccountInfo<'info>, +/// } +/// ``` +/// +/// This generates seed registry functions that `#[add_compressible_instructions(PoolState)]` +/// can automatically discover and use. +/// +/// ## Requirements +/// +/// - Must be applied alongside `#[derive(Accounts)]` +/// - At least one field must have `#[account(init, seeds = [...], bump)]` +/// - The account type in the field must match the type used in `#[add_compressible_instructions]` +#[proc_macro_derive(Compressible)] +pub fn compressible_derive(input: TokenStream) -> TokenStream { + compressible_derive::derive_compressible(syn::parse_macro_input!(input)) + .unwrap_or_else(syn::Error::into_compile_error) + .into() +} diff --git a/sdk-libs/macros/src/native_compressible.rs b/sdk-libs/macros/src/native_compressible.rs new file mode 100644 index 0000000000..fd02104c27 --- /dev/null +++ b/sdk-libs/macros/src/native_compressible.rs @@ -0,0 +1,524 @@ +use heck::ToSnakeCase; +use proc_macro2::TokenStream; +use quote::{format_ident, quote}; +use syn::{ + parse::{Parse, ParseStream}, + punctuated::Punctuated, + Ident, Item, ItemMod, Result, Token, +}; + +/// Parse a comma-separated list of identifiers +struct IdentList { + idents: Punctuated, +} + +impl Parse for IdentList { + fn parse(input: ParseStream) -> Result { + if input.is_empty() { + return Err(syn::Error::new( + input.span(), + "Expected at least one account type", + )); + } + + // Try to parse as a simple identifier first + if input.peek(Ident) && !input.peek2(Token![,]) { + // Single identifier case + let ident: Ident = input.parse()?; + let mut idents = Punctuated::new(); + idents.push(ident); + return Ok(IdentList { idents }); + } + + // Otherwise parse as comma-separated list + Ok(IdentList { + idents: Punctuated::parse_terminated(input)?, + }) + } +} + +/// Generate compress instructions for the specified account types (Native Solana version) +pub(crate) fn add_native_compressible_instructions( + args: TokenStream, + mut module: ItemMod, +) -> Result { + // Try to parse the arguments + let ident_list = match syn::parse2::(args) { + Ok(list) => list, + Err(e) => { + return Err(syn::Error::new( + e.span(), + format!("Failed to parse arguments: {}", e), + )); + } + }; + + // Check if module has content + if module.content.is_none() { + return Err(syn::Error::new_spanned(&module, "Module must have a body")); + } + + // Get the module content + let content = module.content.as_mut().unwrap(); + + // Collect all struct names + let struct_names: Vec<_> = ident_list.idents.iter().collect(); + + // Add necessary imports at the beginning + let imports: Item = syn::parse_quote! { + use super::*; + }; + content.1.insert(0, imports); + + // Add borsh imports + let borsh_imports: Item = syn::parse_quote! { + use borsh::{BorshDeserialize, BorshSerialize}; + }; + content.1.insert(1, borsh_imports); + + // Generate unified data structures + let unified_structures = generate_unified_structures(&struct_names); + for item in unified_structures { + content.1.push(item); + } + + // Generate instruction data structures + let instruction_data_structs = generate_instruction_data_structs(&struct_names); + for item in instruction_data_structs { + content.1.push(item); + } + + // Generate thin wrapper processor functions + let processor_functions = generate_thin_processors(&struct_names); + for item in processor_functions { + content.1.push(item); + } + + Ok(quote! { + #module + }) +} + +fn generate_unified_structures(struct_names: &[&Ident]) -> Vec { + let mut items = Vec::new(); + + // Generate the CompressedAccountVariant enum + let enum_variants = struct_names.iter().map(|name| { + quote! { + #name(#name) + } + }); + + let compressed_variant_enum: Item = syn::parse_quote! { + #[derive(Clone, Debug, borsh::BorshSerialize, borsh::BorshDeserialize)] + pub enum CompressedAccountVariant { + #(#enum_variants),* + } + }; + items.push(compressed_variant_enum); + + // Generate Default implementation + if let Some(first_struct) = struct_names.first() { + let default_impl: Item = syn::parse_quote! { + impl Default for CompressedAccountVariant { + fn default() -> Self { + CompressedAccountVariant::#first_struct(Default::default()) + } + } + }; + items.push(default_impl); + } + + // Generate DataHasher implementation with correct signature + let hash_match_arms = struct_names.iter().map(|name| { + quote! { + CompressedAccountVariant::#name(data) => data.hash::() + } + }); + + let data_hasher_impl: Item = syn::parse_quote! { + impl light_hasher::DataHasher for CompressedAccountVariant { + fn hash(&self) -> Result<[u8; 32], light_hasher::errors::HasherError> { + match self { + #(#hash_match_arms),* + } + } + } + }; + items.push(data_hasher_impl); + + // Generate LightDiscriminator implementation with correct constants and method signature + let light_discriminator_impl: Item = syn::parse_quote! { + impl light_sdk::LightDiscriminator for CompressedAccountVariant { + const LIGHT_DISCRIMINATOR: [u8; 8] = [0; 8]; // Default discriminator for enum + const LIGHT_DISCRIMINATOR_SLICE: &'static [u8] = &Self::LIGHT_DISCRIMINATOR; + + fn discriminator() -> [u8; 8] { + Self::LIGHT_DISCRIMINATOR + } + } + }; + items.push(light_discriminator_impl); + + // Generate HasCompressionInfo implementation with correct method signatures + let compression_info_match_arms = struct_names.iter().map(|name| { + quote! { + CompressedAccountVariant::#name(data) => data.compression_info() + } + }); + + let compression_info_mut_match_arms = struct_names.iter().map(|name| { + quote! { + CompressedAccountVariant::#name(data) => data.compression_info_mut() + } + }); + + let has_compression_info_impl: Item = syn::parse_quote! { + impl light_sdk::compressible::HasCompressionInfo for CompressedAccountVariant { + fn compression_info(&self) -> &light_sdk::compressible::CompressionInfo { + match self { + #(#compression_info_match_arms),* + } + } + + fn compression_info_mut(&mut self) -> &mut light_sdk::compressible::CompressionInfo { + match self { + #(#compression_info_mut_match_arms),* + } + } + } + }; + items.push(has_compression_info_impl); + + // Generate CompressedAccountData struct + let compressed_account_data: Item = syn::parse_quote! { + #[derive(Clone, Debug, borsh::BorshSerialize, borsh::BorshDeserialize)] + pub struct CompressedAccountData { + pub meta: light_sdk_types::instruction::account_meta::CompressedAccountMeta, + pub data: CompressedAccountVariant, + pub seeds: Vec>, // Seeds for PDA derivation (without bump) + } + }; + items.push(compressed_account_data); + + items +} + +fn generate_instruction_data_structs(struct_names: &[&Ident]) -> Vec { + let mut items = Vec::new(); + + // Create config instruction data + let create_config: Item = syn::parse_quote! { + #[derive(Clone, Debug, BorshSerialize, BorshDeserialize)] + pub struct CreateCompressionConfigData { + pub compression_delay: u32, + pub rent_recipient: solana_program::pubkey::Pubkey, + pub address_space: Vec, + } + }; + items.push(create_config); + + // Update config instruction data + let update_config: Item = syn::parse_quote! { + #[derive(Clone, Debug, BorshSerialize, BorshDeserialize)] + pub struct UpdateCompressionConfigData { + pub new_compression_delay: Option, + pub new_rent_recipient: Option, + pub new_address_space: Option>, + pub new_update_authority: Option, + } + }; + items.push(update_config); + + // Decompress multiple PDAs instruction data + let decompress_multiple: Item = syn::parse_quote! { + #[derive(Clone, Debug, BorshSerialize, BorshDeserialize)] + pub struct DecompressMultiplePdasData { + pub proof: light_sdk::instruction::ValidityProof, + pub compressed_accounts: Vec, + pub bumps: Vec, + pub system_accounts_offset: u8, + } + }; + items.push(decompress_multiple); + + // Generate compress instruction data for each struct + for struct_name in struct_names { + let compress_data_name = format_ident!("Compress{}Data", struct_name); + let compress_data: Item = syn::parse_quote! { + #[derive(Clone, Debug, BorshSerialize, BorshDeserialize)] + pub struct #compress_data_name { + pub proof: light_sdk::instruction::ValidityProof, + pub compressed_account_meta: light_sdk_types::instruction::account_meta::CompressedAccountMeta, + } + }; + items.push(compress_data); + } + + items +} + +fn generate_thin_processors(struct_names: &[&Ident]) -> Vec { + let mut functions = Vec::new(); + + // Create config processor + let create_config_fn: Item = syn::parse_quote! { + /// Creates a compression config for this program + /// + /// Accounts expected: + /// 0. `[writable, signer]` Payer account + /// 1. `[writable]` Config PDA (seeds: [b"compressible_config"]) + /// 2. `[]` Program data account + /// 3. `[signer]` Program upgrade authority + /// 4. `[]` System program + pub fn create_compression_config( + accounts: &[solana_program::account_info::AccountInfo], + compression_delay: u32, + rent_recipient: solana_program::pubkey::Pubkey, + address_space: Vec, + ) -> solana_program::entrypoint::ProgramResult { + if accounts.len() < 5 { + return Err(solana_program::program_error::ProgramError::NotEnoughAccountKeys); + } + + let payer = &accounts[0]; + let config_account = &accounts[1]; + let program_data = &accounts[2]; + let authority = &accounts[3]; + let system_program = &accounts[4]; + + light_sdk::compressible::create_compression_config_checked( + config_account, + authority, + program_data, + &rent_recipient, + address_space, + compression_delay, + payer, + system_program, + &crate::ID, + ) + .map_err(|e| solana_program::program_error::ProgramError::from(e))?; + + Ok(()) + } + }; + functions.push(create_config_fn); + + // Update config processor + let update_config_fn: Item = syn::parse_quote! { + /// Updates the compression config + /// + /// Accounts expected: + /// 0. `[writable]` Config PDA (seeds: [b"compressible_config"]) + /// 1. `[signer]` Update authority (must match config) + pub fn update_compression_config( + accounts: &[solana_program::account_info::AccountInfo], + new_compression_delay: Option, + new_rent_recipient: Option, + new_address_space: Option>, + new_update_authority: Option, + ) -> solana_program::entrypoint::ProgramResult { + if accounts.len() < 2 { + return Err(solana_program::program_error::ProgramError::NotEnoughAccountKeys); + } + + let config_account = &accounts[0]; + let authority = &accounts[1]; + + light_sdk::compressible::update_compression_config( + config_account, + authority, + new_update_authority.as_ref(), + new_rent_recipient.as_ref(), + new_address_space, + new_compression_delay, + &crate::ID, + ) + .map_err(|e| solana_program::program_error::ProgramError::from(e))?; + + Ok(()) + } + }; + functions.push(update_config_fn); + + // Decompress multiple PDAs processor + let variant_match_arms = struct_names.iter().map(|name| { + quote! { + CompressedAccountVariant::#name(data) => { + CompressedAccountVariant::#name(data) + } + } + }); + + let decompress_fn: Item = syn::parse_quote! { + /// Decompresses multiple compressed PDAs in a single transaction + /// + /// Accounts expected: + /// 0. `[writable, signer]` Fee payer + /// 1. `[writable, signer]` Rent payer + /// 2. `[]` System program + /// 3..N. `[writable]` PDA accounts to decompress into + /// N+1... `[]` Light Protocol system accounts + pub fn decompress_multiple_pdas( + accounts: &[solana_program::account_info::AccountInfo], + proof: light_sdk::instruction::ValidityProof, + compressed_accounts: Vec, + bumps: Vec, + system_accounts_offset: u8, + ) -> solana_program::entrypoint::ProgramResult { + if accounts.len() < 3 { + return Err(solana_program::program_error::ProgramError::NotEnoughAccountKeys); + } + + let fee_payer = &accounts[0]; + let rent_payer = &accounts[1]; + + // Get PDA accounts from remaining accounts + let pda_accounts_end = system_accounts_offset as usize; + let solana_accounts = &accounts[3..3 + pda_accounts_end]; + let system_accounts = &accounts[3 + pda_accounts_end..]; + + // Validate we have matching number of PDAs, compressed accounts, and bumps + if solana_accounts.len() != compressed_accounts.len() + || solana_accounts.len() != bumps.len() { + return Err(solana_program::program_error::ProgramError::InvalidAccountData); + } + + let cpi_accounts = light_sdk::cpi::CpiAccounts::new( + fee_payer, + system_accounts, + crate::LIGHT_CPI_SIGNER, + ); + + // Convert to unified enum accounts + let mut light_accounts = Vec::new(); + let mut pda_account_refs = Vec::new(); + let mut signer_seeds_storage = Vec::new(); + + for (i, (compressed_data, bump)) in compressed_accounts.into_iter() + .zip(bumps.iter()).enumerate() { + + // Convert to unified enum type + let unified_account = match compressed_data.data { + #(#variant_match_arms)* + }; + + let light_account = light_sdk::account::sha::LightAccount::<'_, CompressedAccountVariant>::new_mut( + &crate::ID, + &compressed_data.meta, + unified_account.clone(), + ) + .map_err(|e| solana_program::program_error::ProgramError::from(e))?; + + // Build signer seeds based on account type + let seeds = match &unified_account { + #( + CompressedAccountVariant::#struct_names(_) => { + // Get the seeds from the instruction data and append bump + let mut seeds = compressed_data.seeds.clone(); + seeds.push(vec![*bump]); + seeds + } + ),* + }; + + signer_seeds_storage.push(seeds); + light_accounts.push(light_account); + pda_account_refs.push(&solana_accounts[i]); + } + + // Convert to the format needed by the SDK + let signer_seeds_refs: Vec> = signer_seeds_storage + .iter() + .map(|seeds| seeds.iter().map(|s| s.as_slice()).collect()) + .collect(); + let signer_seeds_slices: Vec<&[&[u8]]> = signer_seeds_refs + .iter() + .map(|seeds| seeds.as_slice()) + .collect(); + + // Single CPI call with unified enum type + light_sdk::compressible::decompress_multiple_idempotent::( + &pda_account_refs, + light_accounts, + &signer_seeds_slices, + proof, + cpi_accounts, + &crate::ID, + rent_payer, + ) + .map_err(|e| solana_program::program_error::ProgramError::from(e))?; + + Ok(()) + } + }; + functions.push(decompress_fn); + + // Generate compress processors for each account type + for struct_name in struct_names { + let compress_fn_name = + format_ident!("compress_{}", struct_name.to_string().to_snake_case()); + + let compress_processor: Item = syn::parse_quote! { + /// Compresses a #struct_name PDA + /// + /// Accounts expected: + /// 0. `[signer]` Authority + /// 1. `[writable]` PDA account to compress + /// 2. `[]` System program + /// 3. `[]` Config PDA + /// 4. `[]` Rent recipient (must match config) + /// 5... `[]` Light Protocol system accounts + pub fn #compress_fn_name( + accounts: &[solana_program::account_info::AccountInfo], + proof: light_sdk::instruction::ValidityProof, + compressed_account_meta: light_sdk_types::instruction::account_meta::CompressedAccountMeta, + ) -> solana_program::entrypoint::ProgramResult { + if accounts.len() < 6 { + return Err(solana_program::program_error::ProgramError::NotEnoughAccountKeys); + } + + let authority = &accounts[0]; + let solana_account = &accounts[1]; + let _system_program = &accounts[2]; + let config_account = &accounts[3]; + let rent_recipient = &accounts[4]; + let system_accounts = &accounts[5..]; + + // Load config from AccountInfo + let config = light_sdk::compressible::CompressibleConfig::load_checked( + config_account, + &crate::ID + ).map_err(|_| solana_program::program_error::ProgramError::InvalidAccountData)?; + + // Verify rent recipient matches config + if rent_recipient.key != &config.rent_recipient { + return Err(solana_program::program_error::ProgramError::InvalidAccountData); + } + + let cpi_accounts = light_sdk::cpi::CpiAccounts::new( + authority, + system_accounts, + crate::LIGHT_CPI_SIGNER, + ); + + light_sdk::compressible::compress_account::<#struct_name>( + solana_account, + &compressed_account_meta, + proof, + cpi_accounts, + &crate::ID, + rent_recipient, + &config.compression_delay, + ) + .map_err(|e| solana_program::program_error::ProgramError::from(e))?; + + Ok(()) + } + }; + functions.push(compress_processor); + } + + functions +} diff --git a/sdk-libs/program-test/Cargo.toml b/sdk-libs/program-test/Cargo.toml index c9a826ebc7..8fc4316153 100644 --- a/sdk-libs/program-test/Cargo.toml +++ b/sdk-libs/program-test/Cargo.toml @@ -20,6 +20,7 @@ light-concurrent-merkle-tree = { workspace = true } light-hasher = { workspace = true } light-compressed-account = { workspace = true, features = ["anchor"] } light-batched-merkle-tree = { workspace = true, features = ["test-only"] } +light-compressible-client = { workspace = true, features = ["anchor"] } # unreleased light-client = { workspace = true, features = ["program-test"] } diff --git a/sdk-libs/program-test/src/accounts/initialize.rs b/sdk-libs/program-test/src/accounts/initialize.rs index 7781a87af9..431fcc9358 100644 --- a/sdk-libs/program-test/src/accounts/initialize.rs +++ b/sdk-libs/program-test/src/accounts/initialize.rs @@ -177,6 +177,18 @@ pub async fn initialize_accounts( *v2_state_tree_config, ) .await?; + + // Initialize the second v2 state tree + create_batched_state_merkle_tree( + &keypairs.governance_authority, + true, + context, + &keypairs.batched_state_merkle_tree_2, + &keypairs.batched_output_queue_2, + &keypairs.batched_cpi_context_2, + *v2_state_tree_config, + ) + .await?; } #[cfg(feature = "v2")] if let Some(params) = _v2_address_tree_config { @@ -211,11 +223,18 @@ pub async fn initialize_accounts( merkle_tree: keypairs.address_merkle_tree.pubkey(), queue: keypairs.address_merkle_tree_queue.pubkey(), }], - v2_state_trees: vec![StateMerkleTreeAccountsV2 { - merkle_tree: keypairs.batched_state_merkle_tree.pubkey(), - output_queue: keypairs.batched_output_queue.pubkey(), - cpi_context: keypairs.batched_cpi_context.pubkey(), - }], + v2_state_trees: vec![ + StateMerkleTreeAccountsV2 { + merkle_tree: keypairs.batched_state_merkle_tree.pubkey(), + output_queue: keypairs.batched_output_queue.pubkey(), + cpi_context: keypairs.batched_cpi_context.pubkey(), + }, + StateMerkleTreeAccountsV2 { + merkle_tree: keypairs.batched_state_merkle_tree_2.pubkey(), + output_queue: keypairs.batched_output_queue_2.pubkey(), + cpi_context: keypairs.batched_cpi_context_2.pubkey(), + }, + ], v2_address_trees: vec![keypairs.batch_address_merkle_tree.pubkey()], }) } diff --git a/sdk-libs/program-test/src/accounts/test_accounts.rs b/sdk-libs/program-test/src/accounts/test_accounts.rs index ea4284c30d..f6f1516647 100644 --- a/sdk-libs/program-test/src/accounts/test_accounts.rs +++ b/sdk-libs/program-test/src/accounts/test_accounts.rs @@ -80,11 +80,18 @@ impl TestAccounts { }], v2_address_trees: vec![pubkey!("EzKE84aVTkCUhDHLELqyJaq1Y7UVVmqxXqZjVHwHY3rK")], - v2_state_trees: vec![StateMerkleTreeAccountsV2 { - merkle_tree: pubkey!("HLKs5NJ8FXkJg8BrzJt56adFYYuwg5etzDtBbQYTsixu"), - output_queue: pubkey!("6L7SzhYB3anwEQ9cphpJ1U7Scwj57bx2xueReg7R9cKU"), - cpi_context: pubkey!("7Hp52chxaew8bW1ApR4fck2bh6Y8qA1pu3qwH6N9zaLj"), - }], + v2_state_trees: vec![ + StateMerkleTreeAccountsV2 { + merkle_tree: pubkey!("HLKs5NJ8FXkJg8BrzJt56adFYYuwg5etzDtBbQYTsixu"), + output_queue: pubkey!("6L7SzhYB3anwEQ9cphpJ1U7Scwj57bx2xueReg7R9cKU"), + cpi_context: pubkey!("7Hp52chxaew8bW1ApR4fck2bh6Y8qA1pu3qwH6N9zaLj"), + }, + StateMerkleTreeAccountsV2 { + merkle_tree: pubkey!("2Yb3fGo2E9aWLjY8KuESaqurYpGGhEeJr7eynKrSgXwS"), + output_queue: pubkey!("12wJT3xYd46rtjeqDU6CrtT8unqLjPiheggzqhN9YsyB"), + cpi_context: pubkey!("HwtjxDvFEXiWnzeMeWkMBzpQN45A95rTJNZmz1Z3pe8R"), // TODO: replace. + }, + ], } } @@ -127,17 +134,30 @@ impl TestAccounts { merkle_tree: pubkey!("amt1Ayt45jfbdw5YSo7iz6WZxUmnZsQTYXy82hVwyC2"), queue: pubkey!("aq1S9z4reTSQAdgWHGD2zDaS39sjGrAxbR31vxJ2F4F"), }], - v2_state_trees: vec![StateMerkleTreeAccountsV2 { - merkle_tree: Keypair::from_bytes(&BATCHED_STATE_MERKLE_TREE_TEST_KEYPAIR) - .unwrap() - .pubkey(), - output_queue: Keypair::from_bytes(&BATCHED_OUTPUT_QUEUE_TEST_KEYPAIR) - .unwrap() - .pubkey(), - cpi_context: Keypair::from_bytes(&BATCHED_CPI_CONTEXT_TEST_KEYPAIR) - .unwrap() - .pubkey(), - }], + v2_state_trees: vec![ + StateMerkleTreeAccountsV2 { + merkle_tree: Keypair::from_bytes(&BATCHED_STATE_MERKLE_TREE_TEST_KEYPAIR) + .unwrap() + .pubkey(), + output_queue: Keypair::from_bytes(&BATCHED_OUTPUT_QUEUE_TEST_KEYPAIR) + .unwrap() + .pubkey(), + cpi_context: Keypair::from_bytes(&BATCHED_CPI_CONTEXT_TEST_KEYPAIR) + .unwrap() + .pubkey(), + }, + StateMerkleTreeAccountsV2 { + merkle_tree: Keypair::from_bytes(&BATCHED_STATE_MERKLE_TREE_TEST_KEYPAIR_2) + .unwrap() + .pubkey(), + output_queue: Keypair::from_bytes(&BATCHED_OUTPUT_QUEUE_TEST_KEYPAIR_2) + .unwrap() + .pubkey(), + cpi_context: Keypair::from_bytes(&BATCHED_CPI_CONTEXT_TEST_KEYPAIR_2) + .unwrap() + .pubkey(), + }, + ], v2_address_trees: vec![ Keypair::from_bytes(&BATCHED_ADDRESS_MERKLE_TREE_TEST_KEYPAIR) .unwrap() diff --git a/sdk-libs/program-test/src/accounts/test_keypairs.rs b/sdk-libs/program-test/src/accounts/test_keypairs.rs index 0a0a59aeec..2cae5319fd 100644 --- a/sdk-libs/program-test/src/accounts/test_keypairs.rs +++ b/sdk-libs/program-test/src/accounts/test_keypairs.rs @@ -14,6 +14,9 @@ pub struct TestKeypairs { pub batched_state_merkle_tree: Keypair, pub batched_output_queue: Keypair, pub batched_cpi_context: Keypair, + pub batched_state_merkle_tree_2: Keypair, + pub batched_output_queue_2: Keypair, + pub batched_cpi_context_2: Keypair, pub batch_address_merkle_tree: Keypair, pub state_merkle_tree_2: Keypair, pub nullifier_queue_2: Keypair, @@ -38,6 +41,14 @@ impl TestKeypairs { .unwrap(), batched_output_queue: Keypair::from_bytes(&BATCHED_OUTPUT_QUEUE_TEST_KEYPAIR).unwrap(), batched_cpi_context: Keypair::from_bytes(&BATCHED_CPI_CONTEXT_TEST_KEYPAIR).unwrap(), + batched_state_merkle_tree_2: Keypair::from_bytes( + &BATCHED_STATE_MERKLE_TREE_TEST_KEYPAIR_2, + ) + .unwrap(), + batched_output_queue_2: Keypair::from_bytes(&BATCHED_OUTPUT_QUEUE_TEST_KEYPAIR_2) + .unwrap(), + batched_cpi_context_2: Keypair::from_bytes(&BATCHED_CPI_CONTEXT_TEST_KEYPAIR_2) + .unwrap(), batch_address_merkle_tree: Keypair::from_bytes( &BATCHED_ADDRESS_MERKLE_TREE_TEST_KEYPAIR, ) @@ -152,3 +163,27 @@ pub const BATCHED_ADDRESS_MERKLE_TREE_TEST_KEYPAIR: [u8; 64] = [ 28, 24, 35, 87, 72, 11, 158, 224, 210, 70, 207, 214, 165, 6, 152, 46, 60, 129, 118, 32, 27, 128, 68, 73, 71, 250, 6, 83, 176, 199, 153, 140, 237, 11, 55, 237, 3, 179, 242, 138, 37, 12, ]; + +// 2Yb3fGo2E9aWLjY8KuESaqurYpGGhEeJr7eynKrSgXwS +pub const BATCHED_STATE_MERKLE_TREE_TEST_KEYPAIR_2: [u8; 64] = [ + 90, 177, 184, 7, 31, 2, 75, 156, 206, 95, 137, 254, 248, 143, 80, 51, 244, 47, 172, 66, 49, 28, + 209, 135, 246, 185, 1, 215, 203, 206, 45, 205, 22, 243, 48, 18, 157, 183, 128, 51, 122, 187, + 220, 157, 58, 187, 210, 100, 26, 202, 115, 200, 112, 226, 176, 142, 204, 246, 80, 46, 44, 164, + 79, 213, +]; + +// 12wJT3xYd46rtjeqDU6CrtT8unqLjPiheggzqhN9YsyB +pub const BATCHED_OUTPUT_QUEUE_TEST_KEYPAIR_2: [u8; 64] = [ + 22, 251, 188, 220, 48, 112, 152, 88, 12, 111, 253, 20, 152, 160, 181, 28, 52, 135, 176, 56, 37, + 253, 214, 155, 207, 174, 40, 34, 120, 168, 220, 48, 0, 126, 250, 157, 250, 233, 33, 126, 217, + 161, 223, 128, 212, 172, 27, 168, 153, 70, 78, 223, 110, 234, 56, 119, 236, 165, 128, 65, 219, + 103, 124, 58, +]; + +// HwtjxDvFEXiWnzeMeWkMBzpQN45A95rTJNZmz1Z3pe8R +pub const BATCHED_CPI_CONTEXT_TEST_KEYPAIR_2: [u8; 64] = [ + 192, 190, 219, 50, 49, 251, 81, 115, 108, 69, 25, 24, 64, 192, 70, 119, 227, 163, 244, 162, + 151, 22, 202, 75, 143, 238, 60, 231, 45, 143, 70, 166, 251, 202, 219, 148, 255, 199, 4, 181, 2, + 206, 241, 189, 231, 73, 214, 93, 163, 87, 254, 68, 179, 132, 226, 66, 188, 189, 86, 84, 143, + 190, 33, 218, +]; diff --git a/sdk-libs/program-test/src/indexer/test_indexer.rs b/sdk-libs/program-test/src/indexer/test_indexer.rs index 13803284b3..43e781318f 100644 --- a/sdk-libs/program-test/src/indexer/test_indexer.rs +++ b/sdk-libs/program-test/src/indexer/test_indexer.rs @@ -85,8 +85,9 @@ use crate::accounts::{ use crate::{ accounts::{ address_tree::create_address_merkle_tree_and_queue_account, - state_tree::create_state_merkle_tree_and_queue_account, test_accounts::TestAccounts, - test_keypairs::BATCHED_OUTPUT_QUEUE_TEST_KEYPAIR, + state_tree::create_state_merkle_tree_and_queue_account, + test_accounts::TestAccounts, + test_keypairs::{BATCHED_OUTPUT_QUEUE_TEST_KEYPAIR, BATCHED_OUTPUT_QUEUE_TEST_KEYPAIR_2}, }, indexer::TestIndexerExtensions, }; @@ -1286,9 +1287,12 @@ impl TestIndexer { for state_merkle_tree_account in state_merkle_tree_accounts.iter() { let test_batched_output_queue = Keypair::from_bytes(&BATCHED_OUTPUT_QUEUE_TEST_KEYPAIR).unwrap(); + let test_batched_output_queue_2 = + Keypair::from_bytes(&BATCHED_OUTPUT_QUEUE_TEST_KEYPAIR_2).unwrap(); let (tree_type, merkle_tree, output_queue_batch_size) = if state_merkle_tree_account .nullifier_queue == test_batched_output_queue.pubkey() + || state_merkle_tree_account.nullifier_queue == test_batched_output_queue_2.pubkey() { let merkle_tree = Box::new(MerkleTree::::new_with_history( DEFAULT_BATCH_STATE_TREE_HEIGHT as usize, @@ -1990,11 +1994,20 @@ impl TestIndexer { let mut address_root_indices = Vec::new(); let mut tree_heights = Vec::new(); for (i, address) in addresses.iter().enumerate() { + // TODO: Remove + println!("Processing non-inclusion proof for address {:?}", address); + println!( + "address_merkle_tree_pubkeys[i]: {:?}", + address_merkle_tree_pubkeys[i] + ); + println!("address_merkle_trees: {:?}", self.address_merkle_trees); let address_tree = self .address_merkle_trees .iter() .find(|x| x.accounts.merkle_tree == address_merkle_tree_pubkeys[i]) .unwrap(); + // TODO: Remove after debugging. + println!("address_tree: {:?}", address_tree); tree_heights.push(address_tree.height()); let proof_inputs = address_tree.get_non_inclusion_proof_inputs(address)?; diff --git a/sdk-libs/program-test/src/lib.rs b/sdk-libs/program-test/src/lib.rs index e1825673de..031a6af133 100644 --- a/sdk-libs/program-test/src/lib.rs +++ b/sdk-libs/program-test/src/lib.rs @@ -121,4 +121,7 @@ pub use light_client::{ indexer::{AddressWithTree, Indexer}, rpc::{Rpc, RpcError}, }; -pub use program_test::{config::ProgramTestConfig, LightProgramTest}; +pub use program_test::{ + config::ProgramTestConfig, initialize_compression_config, setup_mock_program_data, + update_compression_config, LightProgramTest, +}; diff --git a/sdk-libs/program-test/src/program_test/compressible_setup.rs b/sdk-libs/program-test/src/program_test/compressible_setup.rs new file mode 100644 index 0000000000..18f9cd0fbf --- /dev/null +++ b/sdk-libs/program-test/src/program_test/compressible_setup.rs @@ -0,0 +1,161 @@ +//! Test helpers for compressible account operations +//! +//! This module provides common functionality for testing compressible accounts, +//! including mock program data setup and configuration management. + +use light_compressible_client::CompressibleInstruction; +use solana_sdk::{ + bpf_loader_upgradeable, + pubkey::Pubkey, + signature::{Keypair, Signer}, +}; + +use crate::{ + program_test::{LightProgramTest, TestRpc}, + Rpc, RpcError, +}; + +/// Create mock program data account for testing +/// +/// This creates a minimal program data account structure that mimics +/// what the BPF loader would create for deployed programs. +pub fn create_mock_program_data(authority: Pubkey) -> Vec { + let mut data = vec![0u8; 1024]; + data[0..4].copy_from_slice(&3u32.to_le_bytes()); // Program data discriminator + data[4..12].copy_from_slice(&0u64.to_le_bytes()); // Slot + data[12] = 1; // Option Some(authority) + data[13..45].copy_from_slice(authority.as_ref()); // Authority pubkey + data +} + +/// Setup mock program data account for testing +/// +/// For testing without ledger, LiteSVM does not create program data accounts, +/// so we need to create them manually. This is required for programs that +/// check their upgrade authority. +/// +/// # Arguments +/// * `rpc` - The test RPC client +/// * `payer` - The payer keypair (used as authority) +/// * `program_id` - The program ID to create data account for +/// +/// # Returns +/// The pubkey of the created program data account +pub fn setup_mock_program_data( + rpc: &mut LightProgramTest, + payer: &Keypair, + program_id: &Pubkey, +) -> Pubkey { + let (program_data_pda, _) = + Pubkey::find_program_address(&[program_id.as_ref()], &bpf_loader_upgradeable::ID); + let mock_data = create_mock_program_data(payer.pubkey()); + let mock_account = solana_sdk::account::Account { + lamports: 1_000_000, + data: mock_data, + owner: bpf_loader_upgradeable::ID, + executable: false, + rent_epoch: 0, + }; + rpc.set_account(program_data_pda, mock_account); + program_data_pda +} + +/// Initialize compression config for a program +/// +/// This is a high-level helper that handles the complete flow of initializing +/// a compression configuration for a program, including proper signer management. +/// +/// # Arguments +/// * `rpc` - The test RPC client +/// * `payer` - The transaction fee payer +/// * `program_id` - The program to initialize config for +/// * `authority` - The config authority (can be same as payer) +/// * `compression_delay` - Number of slots to wait before compression +/// * `rent_recipient` - Where to send rent from compressed accounts +/// * `address_space` - List of address trees for this program +/// +/// # Returns +/// Transaction signature on success +#[allow(clippy::too_many_arguments)] +pub async fn initialize_compression_config( + rpc: &mut LightProgramTest, + payer: &Keypair, + program_id: &Pubkey, + authority: &Keypair, + compression_delay: u32, + rent_recipient: Pubkey, + address_space: Vec, + discriminator: &[u8], + config_bump: Option, +) -> Result { + if address_space.is_empty() { + return Err(RpcError::CustomError( + "At least one address space must be provided".to_string(), + )); + } + + // Use the mid-level instruction builder + let instruction = CompressibleInstruction::initialize_compression_config( + program_id, + discriminator, + &payer.pubkey(), + &authority.pubkey(), + compression_delay, + rent_recipient, + address_space, + config_bump, + ); + + let signers = if payer.pubkey() == authority.pubkey() { + vec![payer] + } else { + vec![payer, authority] + }; + + rpc.create_and_send_transaction(&[instruction], &payer.pubkey(), &signers) + .await +} + +/// Update compression config for a program +/// +/// This is a high-level helper for updating an existing compression configuration. +/// All parameters except the required ones are optional - pass None to keep existing values. +/// +/// # Arguments +/// * `rpc` - The test RPC client +/// * `payer` - The transaction fee payer +/// * `program_id` - The program to update config for +/// * `authority` - The current config authority +/// * `new_compression_delay` - New compression delay (optional) +/// * `new_rent_recipient` - New rent recipient (optional) +/// * `new_address_space` - New address space list (optional) +/// * `new_update_authority` - New authority (optional) +/// +/// # Returns +/// Transaction signature on success +#[allow(clippy::too_many_arguments)] +pub async fn update_compression_config( + rpc: &mut LightProgramTest, + payer: &Keypair, + program_id: &Pubkey, + authority: &Keypair, + new_compression_delay: Option, + new_rent_recipient: Option, + new_address_space: Option>, + new_update_authority: Option, + discriminator: &[u8], +) -> Result { + // Use the mid-level instruction builder + let instruction = CompressibleInstruction::update_compression_config( + program_id, + discriminator, + &authority.pubkey(), + new_compression_delay, + new_rent_recipient, + new_address_space, + new_update_authority, + ); + + rpc.create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer, authority]) + .await +} diff --git a/sdk-libs/program-test/src/program_test/mod.rs b/sdk-libs/program-test/src/program_test/mod.rs index c9eee711e3..fe14c39909 100644 --- a/sdk-libs/program-test/src/program_test/mod.rs +++ b/sdk-libs/program-test/src/program_test/mod.rs @@ -1,3 +1,4 @@ +pub mod compressible_setup; pub mod config; #[cfg(feature = "devenv")] pub mod extensions; @@ -7,4 +8,5 @@ pub mod test_rpc; pub use light_program_test::LightProgramTest; pub mod indexer; +pub use compressible_setup::*; pub use test_rpc::TestRpc; diff --git a/sdk-libs/program-test/src/utils/mod.rs b/sdk-libs/program-test/src/utils/mod.rs index e1b9d7be63..768d68ac5c 100644 --- a/sdk-libs/program-test/src/utils/mod.rs +++ b/sdk-libs/program-test/src/utils/mod.rs @@ -3,4 +3,5 @@ pub mod create_account; pub mod find_light_bin; pub mod register_test_forester; pub mod setup_light_programs; +pub mod simulation; pub mod tree_accounts; diff --git a/sdk-libs/program-test/src/utils/simulation.rs b/sdk-libs/program-test/src/utils/simulation.rs new file mode 100644 index 0000000000..78987c6c18 --- /dev/null +++ b/sdk-libs/program-test/src/utils/simulation.rs @@ -0,0 +1,36 @@ +use solana_sdk::{ + instruction::Instruction, + signature::{Keypair, Signer}, + transaction::{Transaction, VersionedTransaction}, +}; + +use crate::{program_test::LightProgramTest, Rpc}; + +/// Simulate a transaction and return the compute units consumed. +/// +/// This is a test utility function for measuring transaction costs. +pub async fn simulate_cu( + rpc: &mut LightProgramTest, + payer: &Keypair, + instruction: &Instruction, +) -> u64 { + let blockhash = rpc + .get_latest_blockhash() + .await + .expect("Failed to get latest blockhash") + .0; + let tx = Transaction::new_signed_with_payer( + &[instruction.clone()], + Some(&payer.pubkey()), + &[payer], + blockhash, + ); + let simulate_tx = VersionedTransaction::from(tx); + + let simulate_result = rpc + .context + .simulate_transaction(simulate_tx) + .unwrap_or_else(|err| panic!("Transaction simulation failed: {:?}", err)); + + simulate_result.meta.compute_units_consumed +} diff --git a/sdk-libs/sdk-types/src/constants.rs b/sdk-libs/sdk-types/src/constants.rs index 80e36ab550..ced8f4e21b 100644 --- a/sdk-libs/sdk-types/src/constants.rs +++ b/sdk-libs/sdk-types/src/constants.rs @@ -38,3 +38,8 @@ pub const ADDRESS_QUEUE_V1: [u8; 32] = pubkey_array!("aq1S9z4reTSQAdgWHGD2zDaS39 pub const CPI_CONTEXT_ACCOUNT_DISCRIMINATOR: [u8; 8] = [22, 20, 149, 218, 74, 204, 128, 166]; pub const SOL_POOL_PDA: [u8; 32] = pubkey_array!("CHK57ywWSDncAoRu1F8QgwYJeXuAJyyBYT4LixLXvMZ1"); + +// For input accounts with empty data. +pub const DEFAULT_DATA_HASH: [u8; 32] = [ + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +]; diff --git a/sdk-libs/sdk/Cargo.toml b/sdk-libs/sdk/Cargo.toml index 9afeb4af92..1016fe9314 100644 --- a/sdk-libs/sdk/Cargo.toml +++ b/sdk-libs/sdk/Cargo.toml @@ -18,6 +18,7 @@ anchor = [ "light-compressed-account/anchor", "light-sdk-types/anchor", ] +anchor-discriminator-compat = ["light-sdk-macros/anchor-discriminator-compat"] v2 = ["light-sdk-types/v2"] small_ix = ["light-sdk-types/small_ix"] @@ -28,6 +29,10 @@ solana-msg = { workspace = true } solana-cpi = { workspace = true } solana-program-error = { workspace = true } solana-instruction = { workspace = true } +solana-system-interface = { workspace = true } +solana-clock = { workspace = true } +solana-sysvar = { workspace = true } +solana-rent = { workspace = true } anchor-lang = { workspace = true, optional = true } num-bigint = { workspace = true } @@ -35,6 +40,7 @@ num-bigint = { workspace = true } # only needed with solana-program borsh = { workspace = true, optional = true } thiserror = { workspace = true } +arrayvec = { workspace = true } light-sdk-macros = { workspace = true } light-sdk-types = { workspace = true } diff --git a/sdk-libs/sdk/src/account.rs b/sdk-libs/sdk/src/account.rs index 8206696040..9cd82cf6e1 100644 --- a/sdk-libs/sdk/src/account.rs +++ b/sdk-libs/sdk/src/account.rs @@ -65,33 +65,54 @@ //! ``` // TODO: add example for manual hashing -use std::ops::{Deref, DerefMut}; +use std::{ + marker::PhantomData, + ops::{Deref, DerefMut}, +}; use light_compressed_account::{ compressed_account::PackedMerkleContext, instruction_data::with_account_info::{CompressedAccountInfo, InAccountInfo, OutAccountInfo}, }; -use light_sdk_types::instruction::account_meta::CompressedAccountMetaTrait; +use light_sdk_types::{instruction::account_meta::CompressedAccountMetaTrait, DEFAULT_DATA_HASH}; use solana_pubkey::Pubkey; use crate::{ error::LightSdkError, - light_hasher::{DataHasher, Poseidon}, + light_hasher::{DataHasher, Hasher, Poseidon, Sha256}, AnchorDeserialize, AnchorSerialize, LightDiscriminator, }; +pub trait Size { + fn size(&self) -> usize; +} + +pub type LightAccount<'a, A> = LightAccountInner<'a, Poseidon, A>; + +pub mod sha { + use super::*; + /// LightAccount variant that uses SHA256 hashing + pub type LightAccount<'a, A> = super::LightAccountInner<'a, Sha256, A>; +} + #[derive(Debug, PartialEq)] -pub struct LightAccount< +pub struct LightAccountInner< 'a, + H: Hasher, A: AnchorSerialize + AnchorDeserialize + LightDiscriminator + DataHasher + Default, > { owner: &'a Pubkey, pub account: A, account_info: CompressedAccountInfo, + should_remove_data: bool, + _hasher: PhantomData, } -impl<'a, A: AnchorSerialize + AnchorDeserialize + LightDiscriminator + DataHasher + Default> - LightAccount<'a, A> +impl< + 'a, + H: Hasher, + A: AnchorSerialize + AnchorDeserialize + LightDiscriminator + DataHasher + Default, + > LightAccountInner<'a, H, A> { pub fn new_init( owner: &'a Pubkey, @@ -111,6 +132,8 @@ impl<'a, A: AnchorSerialize + AnchorDeserialize + LightDiscriminator + DataHashe input: None, output: Some(output_account_info), }, + should_remove_data: false, + _hasher: PhantomData, } } @@ -120,7 +143,7 @@ impl<'a, A: AnchorSerialize + AnchorDeserialize + LightDiscriminator + DataHashe input_account: A, ) -> Result { let input_account_info = { - let input_data_hash = input_account.hash::()?; + let input_data_hash = input_account.hash::()?; let tree_info = input_account_meta.get_tree_info(); InAccountInfo { data_hash: input_data_hash, @@ -155,6 +178,57 @@ impl<'a, A: AnchorSerialize + AnchorDeserialize + LightDiscriminator + DataHashe input: Some(input_account_info), output: Some(output_account_info), }, + should_remove_data: false, + _hasher: PhantomData, + }) + } + + /// Create a new LightAccount for compression from an empty compressed + /// account. This is used when compressing a PDA - we know the compressed + /// account exists but is empty (data: [], data_hash: [0, 1, 1, 1, 1, 1, 1, + /// 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + /// 1]). + pub fn new_mut_without_data( + owner: &'a Pubkey, + input_account_meta: &impl CompressedAccountMetaTrait, + ) -> Result { + let input_account_info = { + let tree_info = input_account_meta.get_tree_info(); + InAccountInfo { + data_hash: DEFAULT_DATA_HASH, // TODO: review security. + lamports: input_account_meta.get_lamports().unwrap_or_default(), + merkle_context: PackedMerkleContext { + merkle_tree_pubkey_index: tree_info.merkle_tree_pubkey_index, + queue_pubkey_index: tree_info.queue_pubkey_index, + leaf_index: tree_info.leaf_index, + prove_by_index: tree_info.prove_by_index, + }, + root_index: input_account_meta.get_root_index().unwrap_or_default(), + discriminator: A::LIGHT_DISCRIMINATOR, + } + }; + let output_account_info = { + let output_merkle_tree_index = input_account_meta + .get_output_state_tree_index() + .ok_or(LightSdkError::OutputStateTreeIndexIsNone)?; + OutAccountInfo { + lamports: input_account_meta.get_lamports().unwrap_or_default(), + output_merkle_tree_index, + discriminator: A::LIGHT_DISCRIMINATOR, + ..Default::default() + } + }; + + Ok(Self { + owner, + account: A::default(), // Start with default, will be filled with PDA data + account_info: CompressedAccountInfo { + address: input_account_meta.get_address(), + input: Some(input_account_info), + output: Some(output_account_info), + }, + should_remove_data: false, + _hasher: PhantomData, }) } @@ -164,7 +238,7 @@ impl<'a, A: AnchorSerialize + AnchorDeserialize + LightDiscriminator + DataHashe input_account: A, ) -> Result { let input_account_info = { - let input_data_hash = input_account.hash::()?; + let input_data_hash = input_account.hash::()?; let tree_info = input_account_meta.get_tree_info(); InAccountInfo { data_hash: input_data_hash, @@ -179,6 +253,7 @@ impl<'a, A: AnchorSerialize + AnchorDeserialize + LightDiscriminator + DataHashe discriminator: A::LIGHT_DISCRIMINATOR, } }; + Ok(Self { owner, account: input_account, @@ -187,6 +262,8 @@ impl<'a, A: AnchorSerialize + AnchorDeserialize + LightDiscriminator + DataHashe input: Some(input_account_info), output: None, }, + should_remove_data: false, + _hasher: PhantomData, }) } @@ -230,6 +307,20 @@ impl<'a, A: AnchorSerialize + AnchorDeserialize + LightDiscriminator + DataHashe &self.account_info.output } + /// Get the byte size of the account type. + pub fn size(&self) -> Result + where + A: Size, + { + Ok(self.account.size()) + } + + /// Remove the data from this account by setting it to default. + /// This is used when decompressing to ensure the compressed account is properly zeroed. + pub fn remove_data(&mut self) { + self.should_remove_data = true; + } + /// 1. Serializes the account data and sets the output data hash. /// 2. Returns CompressedAccountInfo. /// @@ -237,18 +328,28 @@ impl<'a, A: AnchorSerialize + AnchorDeserialize + LightDiscriminator + DataHashe /// that should only be called once per instruction. pub fn to_account_info(mut self) -> Result { if let Some(output) = self.account_info.output.as_mut() { - output.data_hash = self.account.hash::()?; - output.data = self - .account - .try_to_vec() - .map_err(|_| LightSdkError::Borsh)?; + if self.should_remove_data { + // TODO: review security. + output.data_hash = DEFAULT_DATA_HASH; + } else { + output.data_hash = self.account.hash::()?; + if H::ID != 0 { + output.data_hash[0] = 0; + } + output.data = self + .account + .try_to_vec() + .map_err(|_| LightSdkError::Borsh)?; + } } Ok(self.account_info) } } -impl Deref - for LightAccount<'_, A> +impl< + H: Hasher, + A: AnchorSerialize + AnchorDeserialize + LightDiscriminator + DataHasher + Default, + > Deref for LightAccountInner<'_, H, A> { type Target = A; @@ -257,8 +358,10 @@ impl DerefMut - for LightAccount<'_, A> +impl< + H: Hasher, + A: AnchorSerialize + AnchorDeserialize + LightDiscriminator + DataHasher + Default, + > DerefMut for LightAccountInner<'_, H, A> { fn deref_mut(&mut self) -> &mut ::Target { &mut self.account diff --git a/sdk-libs/sdk/src/compressible/compress_account.rs b/sdk-libs/sdk/src/compressible/compress_account.rs new file mode 100644 index 0000000000..08d9ec90dd --- /dev/null +++ b/sdk-libs/sdk/src/compressible/compress_account.rs @@ -0,0 +1,163 @@ +#[cfg(feature = "anchor")] +use anchor_lang::{prelude::Account, AccountDeserialize, AccountSerialize, AccountsClose}; +use light_hasher::DataHasher; +use solana_account_info::AccountInfo; +use solana_clock::Clock; +use solana_msg::msg; +use solana_sysvar::Sysvar; + +use crate::{ + account::sha::LightAccount, + compressible::{compress_account_on_init::close, compression_info::HasCompressionInfo}, + cpi::{CpiAccounts, CpiInputs}, + error::LightSdkError, + instruction::{account_meta::CompressedAccountMeta, ValidityProof}, + AnchorDeserialize, AnchorSerialize, LightDiscriminator, +}; + +/// Helper function to compress a PDA and reclaim rent. +/// +/// 1. closes onchain PDA +/// 2. transfers PDA lamports to rent_recipient +/// 3. updates the empty compressed PDA with onchain PDA data +/// +/// This requires the compressed PDA that is tied to the onchain PDA to already +/// exist. +/// +/// # Arguments +/// * `solana_account` - The PDA account to compress (will be closed) +/// * `compressed_account_meta` - Metadata for the compressed account (must be +/// empty but have an address) +/// * `proof` - Validity proof +/// * `cpi_accounts` - Accounts needed for CPI +/// * `owner_program` - The program that will own the compressed account +/// * `rent_recipient` - The account to receive the PDA's rent +/// * `compression_delay` - The number of slots to wait before compression is +/// allowed +#[cfg(feature = "anchor")] +pub fn compress_account<'info, A>( + solana_account: &mut Account<'info, A>, + compressed_account_meta: &CompressedAccountMeta, + proof: ValidityProof, + cpi_accounts: CpiAccounts<'_, 'info>, + rent_recipient: &AccountInfo<'info>, + compression_delay: &u32, +) -> Result<(), crate::ProgramError> +where + A: DataHasher + + LightDiscriminator + + AnchorSerialize + + AnchorDeserialize + + Default + + Clone + + HasCompressionInfo + + std::fmt::Debug, + A: AccountSerialize + AccountDeserialize, +{ + let current_slot = Clock::get()?.slot; + + let last_written_slot = solana_account.compression_info().last_written_slot(); + + if current_slot < last_written_slot + *compression_delay as u64 { + msg!( + "Cannot compress yet. {} slots remaining", + (last_written_slot + *compression_delay as u64).saturating_sub(current_slot) + ); + return Err(LightSdkError::ConstraintViolation.into()); + } + // ensure re-init attack is not possible + solana_account.compression_info_mut().set_compressed(); + + let owner_program_id = cpi_accounts.self_program_id(); + let mut compressed_account = + LightAccount::<'_, A>::new_mut_without_data(&owner_program_id, compressed_account_meta)?; + + let mut compressed_data = (**solana_account).clone(); + + compressed_data.set_compression_info_none(); + compressed_account.account = compressed_data; + + // Create CPI inputs + let cpi_inputs = CpiInputs::new(proof, vec![compressed_account.to_account_info()?]); + + // Invoke light system program to create the compressed account + cpi_inputs.invoke_light_system_program(cpi_accounts)?; + + // Close the PDA account using Anchor's close method + solana_account.close(rent_recipient.clone())?; + + Ok(()) +} + +/// Native Solana variant of compress_account that works with AccountInfo and pre-deserialized data. +/// +/// Helper function to compress a PDA and reclaim rent. +/// +/// 1. closes onchain PDA +/// 2. transfers PDA lamports to rent_recipient +/// 3. updates the empty compressed PDA with onchain PDA data +/// +/// This requires the compressed PDA that is tied to the onchain PDA to already +/// exist. +/// +/// # Arguments +/// * `pda_account_info` - The PDA AccountInfo to compress (will be closed) +/// * `pda_account_data` - The pre-deserialized PDA account data +/// * `compressed_account_meta` - Metadata for the compressed account (must be +/// empty but have an address) +/// * `proof` - Validity proof +/// * `cpi_accounts` - Accounts needed for CPI +/// * `owner_program` - The program that will own the compressed account +/// * `rent_recipient` - The account to receive the PDA's rent +/// * `compression_delay` - The number of slots to wait before compression is +/// allowed +pub fn compress_pda_native<'info, A>( + pda_account_info: &mut AccountInfo<'info>, + pda_account_data: &mut A, + compressed_account_meta: &CompressedAccountMeta, + proof: ValidityProof, + cpi_accounts: CpiAccounts<'_, 'info>, + rent_recipient: &AccountInfo<'info>, + compression_delay: &u32, +) -> Result<(), crate::ProgramError> +where + A: DataHasher + + LightDiscriminator + + AnchorSerialize + + AnchorDeserialize + + Default + + Clone + + HasCompressionInfo, +{ + let current_slot = Clock::get()?.slot; + + let last_written_slot = pda_account_data.compression_info().last_written_slot(); + + if current_slot < last_written_slot + *compression_delay as u64 { + msg!( + "Cannot compress yet. {} slots remaining", + (last_written_slot + *compression_delay as u64).saturating_sub(current_slot) + ); + return Err(LightSdkError::ConstraintViolation.into()); + } + // ensure re-init attack is not possible + pda_account_data.compression_info_mut().set_compressed(); + + // Create the compressed account with the PDA data + let owner_program_id = cpi_accounts.self_program_id(); + let mut compressed_account = + LightAccount::<'_, A>::new_mut_without_data(&owner_program_id, compressed_account_meta)?; + + let mut compressed_data = pda_account_data.clone(); + compressed_data.set_compression_info_none(); + compressed_account.account = compressed_data; + + // Create CPI inputs + let cpi_inputs = CpiInputs::new(proof, vec![compressed_account.to_account_info()?]); + + // Invoke light system program to create the compressed account + cpi_inputs.invoke_light_system_program(cpi_accounts)?; + // Close PDA account manually + close(pda_account_info, rent_recipient.clone())?; + Ok(()) +} diff --git a/sdk-libs/sdk/src/compressible/compress_account_on_init.rs b/sdk-libs/sdk/src/compressible/compress_account_on_init.rs new file mode 100644 index 0000000000..1186471def --- /dev/null +++ b/sdk-libs/sdk/src/compressible/compress_account_on_init.rs @@ -0,0 +1,373 @@ +#[cfg(feature = "anchor")] +use anchor_lang::{ + AccountsClose, + {prelude::Account, AccountDeserialize, AccountSerialize}, +}; +use light_hasher::DataHasher; +use solana_account_info::AccountInfo; +use solana_msg::msg; +use solana_pubkey::Pubkey; + +use crate::{ + account::sha::LightAccount, + address::PackedNewAddressParams, + compressible::HasCompressionInfo, + cpi::{CpiAccounts, CpiInputs}, + error::{LightSdkError, Result}, + instruction::ValidityProof, + light_account_checks::AccountInfoTrait, + AnchorDeserialize, AnchorSerialize, LightDiscriminator, +}; + +/// Wrapper to process a single onchain PDA for compression into a new +/// compressed account. Calls `process_accounts_for_compression_on_init` with +/// single-element slices and invokes the CPI. +#[cfg(feature = "anchor")] +#[allow(clippy::too_many_arguments)] +pub fn compress_account_on_init<'info, A>( + solana_account: &mut Account<'info, A>, + address: &[u8; 32], + new_address_param: &PackedNewAddressParams, + output_state_tree_index: u8, + cpi_accounts: CpiAccounts<'_, 'info>, + address_space: &[Pubkey], + rent_recipient: &AccountInfo<'info>, + proof: ValidityProof, +) -> Result<()> +where + A: DataHasher + + LightDiscriminator + + AnchorSerialize + + AnchorDeserialize + + Default + + Clone + + HasCompressionInfo + + std::fmt::Debug, + A: AccountSerialize + AccountDeserialize, +{ + let mut solana_accounts: [&mut Account<'info, A>; 1] = [solana_account]; + let addresses: [[u8; 32]; 1] = [*address]; + let new_address_params: [PackedNewAddressParams; 1] = [*new_address_param]; + let output_state_tree_indices: [u8; 1] = [output_state_tree_index]; + + let compressed_infos = prepare_accounts_for_compression_on_init( + &mut solana_accounts, + &addresses, + &new_address_params, + &output_state_tree_indices, + &cpi_accounts, + address_space, + rent_recipient, + )?; + + let cpi_inputs = CpiInputs::new_with_address(proof, compressed_infos, vec![*new_address_param]); + + cpi_inputs.invoke_light_system_program(cpi_accounts)?; + + Ok(()) +} + +/// Helper function to process multiple onchain PDAs for compression into new +/// compressed accounts. +/// +/// This function processes accounts of a single type and returns +/// CompressedAccountInfo for CPI batching. It allows the caller to handle the +/// CPI invocation separately, enabling batching of multiple different account +/// types. +/// +/// # Arguments +/// * `solana_accounts` - The PDA accounts to compress +/// * `addresses` - The addresses for the compressed accounts +/// * `new_address_params` - Address parameters for the compressed accounts +/// * `output_state_tree_indices` - Output state tree indices for the compressed +/// accounts +/// * `cpi_accounts` - Accounts needed for validation +/// * `owner_program` - The program that will own the compressed accounts +/// * `address_space` - The address space to validate uniqueness against +/// +/// # Returns +/// * `Ok(Vec)` - CompressedAccountInfo for CPI batching +/// * `Err(LightSdkError)` if there was an error +#[cfg(feature = "anchor")] +#[allow(clippy::too_many_arguments)] +pub fn prepare_accounts_for_compression_on_init<'info, A>( + solana_accounts: &mut [&mut Account<'info, A>], + addresses: &[[u8; 32]], + new_address_params: &[PackedNewAddressParams], + output_state_tree_indices: &[u8], + cpi_accounts: &CpiAccounts<'_, 'info>, + address_space: &[Pubkey], + rent_recipient: &AccountInfo<'info>, +) -> Result> +where + A: DataHasher + + LightDiscriminator + + AnchorSerialize + + AnchorDeserialize + + Default + + Clone + + HasCompressionInfo + + std::fmt::Debug, + A: AccountSerialize + AccountDeserialize, +{ + if solana_accounts.len() != addresses.len() + || solana_accounts.len() != new_address_params.len() + || solana_accounts.len() != output_state_tree_indices.len() + { + return Err(LightSdkError::ConstraintViolation); + } + + // Address space validation + for params in new_address_params { + let tree = cpi_accounts + .get_tree_account_info(params.address_merkle_tree_account_index as usize) + .map_err(|_| LightSdkError::ConstraintViolation)? + .pubkey(); + if !address_space.iter().any(|a| a == &tree) { + return Err(LightSdkError::ConstraintViolation); + } + } + + let mut compressed_account_infos = Vec::new(); + + for (((solana_account, &address), &_new_address_param), &output_state_tree_index) in + solana_accounts + .iter_mut() + .zip(addresses.iter()) + .zip(new_address_params.iter()) + .zip(output_state_tree_indices.iter()) + { + // Ensure the account is marked as compressed We need to init first + // because it's none. Setting to compressed prevents lamports funding + // attack. + + *solana_account.compression_info_mut_opt() = + Some(super::CompressionInfo::new_decompressed()?); + solana_account.compression_info_mut().set_compressed(); + + let owner_program_id = cpi_accounts.self_program_id(); + // Create the compressed account with the PDA data + let mut compressed_account = LightAccount::<'_, A>::new_init( + &owner_program_id, + Some(address), + output_state_tree_index, + ); + + // Clone the PDA data and set compression_info to None for compressed + // storage + let mut compressed_data = (***solana_account).clone(); + compressed_data.set_compression_info_none(); + compressed_account.account = compressed_data; + + compressed_account_infos.push(compressed_account.to_account_info()?); + + // Close both PDA accounts + solana_account + .close(rent_recipient.clone()) + .map_err(|_| LightSdkError::ConstraintViolation)?; + } + + Ok(compressed_account_infos) +} + +/// Native Solana variant of compress_account_on_init that works with AccountInfo and pre-deserialized data. +/// +/// Wrapper to process a single onchain PDA for compression into a new +/// compressed account. Calls `prepare_accounts_for_compression_on_init_native` with +/// single-element slices and invokes the CPI. +#[allow(clippy::too_many_arguments)] +pub fn compress_account_on_init_native<'info, A>( + pda_account_info: &mut AccountInfo<'info>, + pda_account_data: &mut A, + address: &[u8; 32], + new_address_param: &PackedNewAddressParams, + output_state_tree_index: u8, + cpi_accounts: CpiAccounts<'_, 'info>, + address_space: &[Pubkey], + rent_recipient: &AccountInfo<'info>, + proof: ValidityProof, +) -> Result<()> +where + A: DataHasher + + LightDiscriminator + + AnchorSerialize + + AnchorDeserialize + + Default + + Clone + + HasCompressionInfo + + std::fmt::Debug, +{ + // let pda_accounts_info: = &[pda_account_info]; + let mut pda_accounts_data: [&mut A; 1] = [pda_account_data]; + let addresses: [[u8; 32]; 1] = [*address]; + let new_address_params: [PackedNewAddressParams; 1] = [*new_address_param]; + let output_state_tree_indices: [u8; 1] = [output_state_tree_index]; + + msg!("0 hi?"); + let compressed_infos = prepare_accounts_for_compression_on_init_native( + &mut [pda_account_info], + &mut pda_accounts_data, + &addresses, + &new_address_params, + &output_state_tree_indices, + &cpi_accounts, + address_space, + rent_recipient, + )?; + + let cpi_inputs = CpiInputs::new_with_address(proof, compressed_infos, vec![*new_address_param]); + + cpi_inputs.invoke_light_system_program(cpi_accounts)?; + + Ok(()) +} + +/// Native Solana variant of prepare_accounts_for_compression_on_init that works +/// with AccountInfo and pre-deserialized data. +/// +/// Helper function to process multiple onchain PDAs for compression into new +/// compressed accounts. +/// +/// This function processes accounts of a single type and returns +/// CompressedAccountInfo for CPI batching. It allows the caller to handle the +/// CPI invocation separately, enabling batching of multiple different account +/// types. +/// +/// # Arguments +/// * `pda_accounts_info` - The PDA AccountInfos to compress +/// * `pda_accounts_data` - The pre-deserialized PDA account data +/// * `addresses` - The addresses for the compressed accounts +/// * `new_address_params` - Address parameters for the compressed accounts +/// * `output_state_tree_indices` - Output state tree indices for the compressed +/// accounts +/// * `cpi_accounts` - Accounts needed for validation +/// * `address_space` - The address space to validate uniqueness against +/// * `rent_recipient` - The account to receive the PDAs' rent +/// +/// # Returns +/// * `Ok(Vec)` - CompressedAccountInfo for CPI batching +/// * `Err(LightSdkError)` if there was an error +#[allow(clippy::too_many_arguments)] +pub fn prepare_accounts_for_compression_on_init_native<'info, A>( + pda_accounts_info: &mut [&mut AccountInfo<'info>], + pda_accounts_data: &mut [&mut A], + addresses: &[[u8; 32]], + new_address_params: &[PackedNewAddressParams], + output_state_tree_indices: &[u8], + cpi_accounts: &CpiAccounts<'_, 'info>, + address_space: &[Pubkey], + rent_recipient: &AccountInfo<'info>, +) -> Result> +where + A: DataHasher + + LightDiscriminator + + AnchorSerialize + + AnchorDeserialize + + Default + + Clone + + HasCompressionInfo + + std::fmt::Debug, +{ + if pda_accounts_info.len() != pda_accounts_data.len() + || pda_accounts_info.len() != addresses.len() + || pda_accounts_info.len() != new_address_params.len() + || pda_accounts_info.len() != output_state_tree_indices.len() + { + msg!("pda_accounts_info.len(): {:?}", pda_accounts_info.len()); + msg!("pda_accounts_data.len(): {:?}", pda_accounts_data.len()); + msg!("addresses.len(): {:?}", addresses.len()); + msg!("new_address_params.len(): {:?}", new_address_params.len()); + msg!( + "output_state_tree_indices.len(): {:?}", + output_state_tree_indices.len() + ); + return Err(LightSdkError::ConstraintViolation); + } + + // Address space validation + for params in new_address_params { + let tree = cpi_accounts + .get_tree_account_info(params.address_merkle_tree_account_index as usize) + .map_err(|_| LightSdkError::ConstraintViolation)? + .pubkey(); + if !address_space.iter().any(|a| a == &tree) { + msg!("address tree: {:?}", tree); + msg!("expected address_space: {:?}", address_space); + return Err(LightSdkError::ConstraintViolation); + } + } + + let mut compressed_account_infos = Vec::new(); + + for ( + (((pda_account_info, pda_account_data), &address), &_new_address_param), + &output_state_tree_index, + ) in pda_accounts_info + .iter_mut() + .zip(pda_accounts_data.iter_mut()) + .zip(addresses.iter()) + .zip(new_address_params.iter()) + .zip(output_state_tree_indices.iter()) + { + // Ensure the account is marked as compressed We need to init first + // because it's none. Setting to compressed prevents lamports funding + // attack. + *pda_account_data.compression_info_mut_opt() = + Some(super::CompressionInfo::new_decompressed()?); + pda_account_data.compression_info_mut().set_compressed(); + + // Create the compressed account with the PDA data + let owner_program_id = cpi_accounts.self_program_id(); + let mut compressed_account = LightAccount::<'_, A>::new_init( + &owner_program_id, + Some(address), + output_state_tree_index, + ); + + // Clone the PDA data and set compression_info to None for compressed + // storage + let mut compressed_data = (*pda_account_data).clone(); + compressed_data.set_compression_info_none(); + compressed_account.account = compressed_data; + + compressed_account_infos.push(compressed_account.to_account_info()?); + + // Close PDA account manually + close(pda_account_info, rent_recipient.clone())?; + } + + Ok(compressed_account_infos) +} + +// Proper native Solana account closing implementation +pub fn close<'info>( + info: &mut AccountInfo<'info>, + sol_destination: AccountInfo<'info>, +) -> Result<()> { + // Transfer all lamports from the account to the destination + let lamports_to_transfer = info.lamports(); + + // Use try_borrow_mut_lamports for proper borrow management + **info + .try_borrow_mut_lamports() + .map_err(|_| LightSdkError::ConstraintViolation)? = 0; + + let dest_lamports = sol_destination.lamports(); + **sol_destination + .try_borrow_mut_lamports() + .map_err(|_| LightSdkError::ConstraintViolation)? = + dest_lamports.checked_add(lamports_to_transfer).unwrap(); + + // Assign to system program first + let system_program_id = solana_pubkey::pubkey!("11111111111111111111111111111111"); + info.assign(&system_program_id); + + // Realloc to 0 size - this should work after assigning to system program + info.realloc(0, false).map_err(|e| { + msg!("Error during realloc: {:?}", e); + LightSdkError::ConstraintViolation + })?; + + msg!("Account closed successfully"); + Ok(()) +} diff --git a/sdk-libs/sdk/src/compressible/compression_info.rs b/sdk-libs/sdk/src/compressible/compression_info.rs new file mode 100644 index 0000000000..7fbefa437e --- /dev/null +++ b/sdk-libs/sdk/src/compressible/compression_info.rs @@ -0,0 +1,91 @@ +use solana_clock::Clock; +use solana_sysvar::Sysvar; + +use crate::{AnchorDeserialize, AnchorSerialize}; + +/// Trait for accounts that contain CompressionInfo +pub trait HasCompressionInfo { + fn compression_info(&self) -> &CompressionInfo; + fn compression_info_mut(&mut self) -> &mut CompressionInfo; + fn compression_info_mut_opt(&mut self) -> &mut Option; + fn set_compression_info_none(&mut self); +} + +/// Information for compressible accounts that tracks when the account was last +/// written +#[derive(Clone, Debug, Default, AnchorSerialize, AnchorDeserialize)] +pub struct CompressionInfo { + /// The slot when this account was last written/decompressed + pub last_written_slot: u64, + /// 0 not inited, 1 decompressed, 2 compressed + pub state: CompressionState, +} + +#[derive(Clone, Default, Debug, AnchorSerialize, AnchorDeserialize, PartialEq)] +pub enum CompressionState { + #[default] + Uninitialized, + Decompressed, + Compressed, +} + +impl CompressionInfo { + /// Creates new compression info with the current slot + pub fn new_decompressed() -> Result { + Ok(Self { + last_written_slot: Clock::get()?.slot, + state: CompressionState::Decompressed, + }) + } + + /// Updates the last written slot to the current slot + pub fn set_last_written_slot(&mut self) -> Result<(), crate::ProgramError> { + self.last_written_slot = Clock::get()?.slot; + Ok(()) + } + + /// Sets the last written slot to a specific value + pub fn set_last_written_slot_value(&mut self, slot: u64) { + self.last_written_slot = slot; + } + + /// Gets the last written slot + pub fn last_written_slot(&self) -> u64 { + self.last_written_slot + } + + /// Checks if the account can be compressed based on the delay + pub fn can_compress(&self, compression_delay: u64) -> Result { + let current_slot = Clock::get()?.slot; + Ok(current_slot >= self.last_written_slot + compression_delay) + } + + /// Gets the number of slots remaining before compression is allowed + pub fn slots_until_compressible( + &self, + compression_delay: u64, + ) -> Result { + let current_slot = Clock::get()?.slot; + Ok((self.last_written_slot + compression_delay).saturating_sub(current_slot)) + } + + /// Set compressed + pub fn set_compressed(&mut self) { + self.state = CompressionState::Compressed; + } + + /// Set decompressed + pub fn set_decompressed(&mut self) { + self.state = CompressionState::Decompressed; + } + + /// Check if the account is compressed + pub fn is_compressed(&self) -> bool { + self.state == CompressionState::Compressed + } +} + +#[cfg(feature = "anchor")] +impl anchor_lang::Space for CompressionInfo { + const INIT_SPACE: usize = 8 + 1; // u64 + state enum +} diff --git a/sdk-libs/sdk/src/compressible/config.rs b/sdk-libs/sdk/src/compressible/config.rs new file mode 100644 index 0000000000..20b91ff300 --- /dev/null +++ b/sdk-libs/sdk/src/compressible/config.rs @@ -0,0 +1,478 @@ +use std::collections::HashSet; + +use solana_account_info::AccountInfo; +use solana_cpi::invoke_signed; +use solana_msg::msg; +use solana_pubkey::Pubkey; +use solana_rent::Rent; +use solana_system_interface::instruction as system_instruction; +use solana_sysvar::Sysvar; + +use crate::{error::LightSdkError, AnchorDeserialize, AnchorSerialize}; + +pub const COMPRESSIBLE_CONFIG_SEED: &[u8] = b"compressible_config"; +pub const MAX_ADDRESS_TREES_PER_SPACE: usize = 1; +const BPF_LOADER_UPGRADEABLE_ID: Pubkey = + Pubkey::from_str_const("BPFLoaderUpgradeab1e11111111111111111111111"); + +/// Global configuration for compressible accounts +#[derive(Clone, Debug, AnchorDeserialize, AnchorSerialize)] +pub struct CompressibleConfig { + /// Config version for future upgrades + pub version: u8, + /// Number of slots to wait before compression is allowed + pub compression_delay: u32, + /// Authority that can update the config + pub update_authority: Pubkey, + /// Account that receives rent from compressed PDAs + pub rent_recipient: Pubkey, + /// Config bump seed (for multiple configs per program) + pub config_bump: u8, + /// PDA bump seed + pub bump: u8, + /// Address space for compressed accounts (exactly 1 address_tree allowed) + pub address_space: Vec, +} + +impl Default for CompressibleConfig { + fn default() -> Self { + Self { + version: 0, + compression_delay: 216_000, // 24h + update_authority: Pubkey::default(), + rent_recipient: Pubkey::default(), + config_bump: 0, + bump: 0, + address_space: vec![Pubkey::default()], + } + } +} + +impl CompressibleConfig { + pub const LEN: usize = 1 + 4 + 32 + 32 + 1 + 4 + (32 * MAX_ADDRESS_TREES_PER_SPACE) + 1; // 107 bytes max + + /// Calculate the exact size needed for a CompressibleConfig with the given + /// number of address spaces + pub fn size_for_address_spaces(num_address_spaces: usize) -> usize { + 1 + 4 + 32 + 32 + 1 + 4 + (32 * num_address_spaces) + 1 + } + + /// Derives the config PDA address with config bump + pub fn derive_pda(program_id: &Pubkey, config_bump: u8) -> (Pubkey, u8) { + Pubkey::find_program_address(&[COMPRESSIBLE_CONFIG_SEED, &[config_bump]], program_id) + } + + /// Derives the default config PDA address (config_bump = 0) + pub fn derive_default_pda(program_id: &Pubkey) -> (Pubkey, u8) { + Self::derive_pda(program_id, 0) + } + + /// Returns the primary address space (first in the list) + pub fn primary_address_space(&self) -> &Pubkey { + &self.address_space[0] + } + + /// Validates the config account + pub fn validate(&self) -> Result<(), crate::ProgramError> { + if self.version != 1 { + msg!("Unsupported config version: {}", self.version); + return Err(LightSdkError::ConstraintViolation.into()); + } + if self.address_space.len() != 1 { + msg!( + "Address space must contain exactly 1 pubkey, found: {}", + self.address_space.len() + ); + return Err(LightSdkError::ConstraintViolation.into()); + } + // For now, only allow config_bump = 0 to keep it simple + if self.config_bump != 0 { + msg!("Config bump must be 0 for now, found: {}", self.config_bump); + return Err(LightSdkError::ConstraintViolation.into()); + } + Ok(()) + } + + /// Loads and validates config from account, checking owner and PDA derivation + pub fn load_checked( + account: &AccountInfo, + program_id: &Pubkey, + ) -> Result { + if account.owner != program_id { + msg!( + "Config account owner mismatch. Expected: {}. Found: {}.", + program_id, + account.owner + ); + return Err(LightSdkError::ConstraintViolation.into()); + } + let data = account.try_borrow_data()?; + let config = Self::try_from_slice(&data).map_err(|_| LightSdkError::Borsh)?; + config.validate()?; + + // CHECK: PDA derivation + let (expected_pda, _) = Self::derive_pda(program_id, config.config_bump); + if expected_pda != *account.key { + msg!( + "Config account key mismatch. Expected PDA: {}. Found: {}.", + expected_pda, + account.key + ); + return Err(LightSdkError::ConstraintViolation.into()); + } + + Ok(config) + } +} + +/// Creates a new compressible config PDA +/// +/// # Security - Solana Best Practice +/// This function follows the standard Solana pattern where only the program's +/// upgrade authority can create the initial config. This prevents unauthorized +/// parties from hijacking the config system. +/// +/// # Arguments +/// * `config_account` - The config PDA account to initialize +/// * `update_authority` - Authority that can update the config after creation +/// * `rent_recipient` - Account that receives rent from compressed PDAs +/// * `address_space` - Address spaces for compressed accounts (exactly 1 allowed) +/// * `compression_delay` - Number of slots to wait before compression +/// * `config_bump` - Config bump seed (must be 0 for now) +/// * `payer` - Account paying for the PDA creation +/// * `system_program` - System program +/// * `program_id` - The program that owns the config +/// +/// # Required Validation (must be done by caller) +/// The caller MUST validate that the signer is the program's upgrade authority +/// by checking against the program data account. This cannot be done in the SDK +/// due to dependency constraints. +/// +/// # Returns +/// * `Ok(())` if config was created successfully +/// * `Err(ProgramError)` if there was an error +#[allow(clippy::too_many_arguments)] +pub fn process_initialize_compression_config_account_info<'info>( + config_account: &AccountInfo<'info>, + update_authority: &AccountInfo<'info>, + rent_recipient: &Pubkey, + address_space: Vec, + compression_delay: u32, + config_bump: u8, + payer: &AccountInfo<'info>, + system_program: &AccountInfo<'info>, + program_id: &Pubkey, +) -> Result<(), crate::ProgramError> { + // CHECK: only 1 address_space + if config_bump != 0 { + msg!("Config bump must be 0 for now, found: {}", config_bump); + return Err(LightSdkError::ConstraintViolation.into()); + } + + // CHECK: not already initialized + if config_account.data_len() > 0 { + msg!("Config account already initialized"); + return Err(LightSdkError::ConstraintViolation.into()); + } + + // CHECK: only 1 address_space + if address_space.len() != 1 { + msg!( + "Address space must contain exactly 1 pubkey, found: {}", + address_space.len() + ); + return Err(LightSdkError::ConstraintViolation.into()); + } + + // CHECK: unique pubkeys in address_space + validate_address_space_no_duplicates(&address_space)?; + + // CHECK: signer + if !update_authority.is_signer { + msg!("Update authority must be signer for initial config creation"); + return Err(LightSdkError::ConstraintViolation.into()); + } + + // CHECK: pda derivation + let (derived_pda, bump) = CompressibleConfig::derive_pda(program_id, config_bump); + if derived_pda != *config_account.key { + msg!("Invalid config PDA"); + return Err(LightSdkError::ConstraintViolation.into()); + } + + let rent = Rent::get().map_err(LightSdkError::from)?; + let account_size = CompressibleConfig::size_for_address_spaces(address_space.len()); + let rent_lamports = rent.minimum_balance(account_size); + + let seeds = &[COMPRESSIBLE_CONFIG_SEED, &[config_bump], &[bump]]; + let create_account_ix = system_instruction::create_account( + payer.key, + config_account.key, + rent_lamports, + account_size as u64, + program_id, + ); + + invoke_signed( + &create_account_ix, + &[ + payer.clone(), + config_account.clone(), + system_program.clone(), + ], + &[seeds], + ) + .map_err(LightSdkError::from)?; + + let config = CompressibleConfig { + version: 1, + compression_delay, + update_authority: *update_authority.key, + rent_recipient: *rent_recipient, + config_bump, + address_space, + bump, + }; + + let mut data = config_account + .try_borrow_mut_data() + .map_err(LightSdkError::from)?; + config + .serialize(&mut &mut data[..]) + .map_err(|_| LightSdkError::Borsh)?; + + Ok(()) +} + +/// Updates an existing compressible config +/// +/// # Arguments +/// * `config_account` - The config PDA account to update +/// * `authority` - Current update authority (must match config) +/// * `new_update_authority` - Optional new update authority +/// * `new_rent_recipient` - Optional new rent recipient +/// * `new_address_space` - Optional new address spaces (exactly 1 allowed) +/// * `new_compression_delay` - Optional new compression delay +/// * `owner_program_id` - The program that owns the config +/// +/// # Returns +/// * `Ok(())` if config was updated successfully +/// * `Err(ProgramError)` if there was an error +pub fn process_update_compression_config<'info>( + config_account: &AccountInfo<'info>, + authority: &AccountInfo<'info>, + new_update_authority: Option<&Pubkey>, + new_rent_recipient: Option<&Pubkey>, + new_address_space: Option>, + new_compression_delay: Option, + owner_program_id: &Pubkey, +) -> Result<(), crate::ProgramError> { + // CHECK: PDA derivation + let mut config = CompressibleConfig::load_checked(config_account, owner_program_id)?; + + // Check authority + if !authority.is_signer { + msg!("Update authority must be signer"); + return Err(LightSdkError::ConstraintViolation.into()); + } + if *authority.key != config.update_authority { + msg!("Invalid update authority"); + return Err(LightSdkError::ConstraintViolation.into()); + } + + // Apply updates + if let Some(new_authority) = new_update_authority { + config.update_authority = *new_authority; + } + if let Some(new_recipient) = new_rent_recipient { + config.rent_recipient = *new_recipient; + } + if let Some(new_spaces) = new_address_space { + if new_spaces.len() != 1 { + msg!( + "Address space must contain exactly 1 pubkey, found: {}", + new_spaces.len() + ); + return Err(LightSdkError::ConstraintViolation.into()); + } + + // Validate no duplicate pubkeys in new address_space + validate_address_space_no_duplicates(&new_spaces)?; + + // Validate that we're only adding, not removing existing pubkeys + validate_address_space_only_adds(&config.address_space, &new_spaces)?; + + config.address_space = new_spaces; + } + if let Some(new_delay) = new_compression_delay { + config.compression_delay = new_delay; + } + + // Write updated config + let mut data = config_account + .try_borrow_mut_data() + .map_err(LightSdkError::from)?; + config + .serialize(&mut &mut data[..]) + .map_err(|_| LightSdkError::Borsh)?; + + Ok(()) +} + +/// Verifies that the signer is the program's upgrade authority +/// +/// # Arguments +/// * `program_id` - The program to check +/// * `program_data_account` - The program's data account (ProgramData) +/// * `authority` - The authority to verify +/// +/// # Returns +/// * `Ok(())` if authority is valid +/// * `Err(LightSdkError)` if authority is invalid or verification fails +pub fn verify_program_upgrade_authority( + program_id: &Pubkey, + program_data_account: &AccountInfo, + authority: &AccountInfo, +) -> Result<(), crate::ProgramError> { + // Verify program data account PDA + let (expected_program_data, _) = + Pubkey::find_program_address(&[program_id.as_ref()], &BPF_LOADER_UPGRADEABLE_ID); + if program_data_account.key != &expected_program_data { + msg!("Invalid program data account"); + return Err(LightSdkError::ConstraintViolation.into()); + } + + // Verify that the signer is the program's upgrade authority + let data = program_data_account.try_borrow_data()?; + + // The UpgradeableLoaderState::ProgramData format: + // 4 bytes discriminator + 8 bytes slot + 1 byte option + 32 bytes authority + if data.len() < 45 { + msg!("Program data account too small"); + return Err(LightSdkError::ConstraintViolation.into()); + } + + // Check discriminator (should be 3 for ProgramData) + let discriminator = u32::from_le_bytes([data[0], data[1], data[2], data[3]]); + if discriminator != 3 { + msg!("Invalid program data discriminator"); + return Err(LightSdkError::ConstraintViolation.into()); + } + + // Skip slot (8 bytes) and check if authority exists (1 byte flag) + let has_authority = data[12] == 1; + if !has_authority { + msg!("Program has no upgrade authority"); + return Err(LightSdkError::ConstraintViolation.into()); + } + + // Read the upgrade authority pubkey (32 bytes) + let mut authority_bytes = [0u8; 32]; + authority_bytes.copy_from_slice(&data[13..45]); + let upgrade_authority = Pubkey::new_from_array(authority_bytes); + + // Verify the signer matches the upgrade authority + if !authority.is_signer { + msg!("Authority must be signer"); + return Err(LightSdkError::ConstraintViolation.into()); + } + + if *authority.key != upgrade_authority { + msg!("Signer is not the program's upgrade authority"); + return Err(LightSdkError::ConstraintViolation.into()); + } + + Ok(()) +} + +/// Creates a new compressible config PDA with program upgrade authority +/// validation +/// +/// # Security +/// This function verifies that the signer is the program's upgrade authority +/// before creating the config. This ensures only the program deployer can +/// initialize the configuration. +/// +/// # Arguments +/// * `config_account` - The config PDA account to initialize +/// * `update_authority` - Must be the program's upgrade authority +/// * `program_data_account` - The program's data account for validation +/// * `rent_recipient` - Account that receives rent from compressed PDAs +/// * `address_space` - Address spaces for compressed accounts (exactly 1 +/// allowed) +/// * `compression_delay` - Number of slots to wait before compression +/// * `config_bump` - Config bump seed (must be 0 for now) +/// * `payer` - Account paying for the PDA creation +/// * `system_program` - System program +/// * `program_id` - The program that owns the config +/// +/// # Returns +/// * `Ok(())` if config was created successfully +/// * `Err(ProgramError)` if there was an error or authority validation fails +#[allow(clippy::too_many_arguments)] +pub fn process_initialize_compression_config_checked<'info>( + config_account: &AccountInfo<'info>, + update_authority: &AccountInfo<'info>, + program_data_account: &AccountInfo<'info>, + rent_recipient: &Pubkey, + address_space: Vec, + compression_delay: u32, + config_bump: u8, + payer: &AccountInfo<'info>, + system_program: &AccountInfo<'info>, + program_id: &Pubkey, +) -> Result<(), crate::ProgramError> { + msg!( + "create_compression_config_checked program_data_account: {:?}", + program_data_account.key.log() + ); + msg!( + "create_compression_config_checked program_id: {:?}", + program_id.log() + ); + // Verify the signer is the program's upgrade authority + verify_program_upgrade_authority(program_id, program_data_account, update_authority)?; + + // Create the config with validated authority + process_initialize_compression_config_account_info( + config_account, + update_authority, + rent_recipient, + address_space, + compression_delay, + config_bump, + payer, + system_program, + program_id, + ) +} + +/// Validates that address_space contains no duplicate pubkeys +fn validate_address_space_no_duplicates(address_space: &[Pubkey]) -> Result<(), LightSdkError> { + let mut seen = HashSet::new(); + for pubkey in address_space { + if !seen.insert(pubkey) { + msg!("Duplicate pubkey found in address_space: {}", pubkey); + return Err(LightSdkError::ConstraintViolation); + } + } + Ok(()) +} + +/// Validates that new_address_space only adds to existing address_space (no removals) +fn validate_address_space_only_adds( + existing_address_space: &[Pubkey], + new_address_space: &[Pubkey], +) -> Result<(), LightSdkError> { + // Check that all existing pubkeys are still present in new address space + for existing_pubkey in existing_address_space { + if !new_address_space.contains(existing_pubkey) { + msg!( + "Cannot remove existing pubkey from address_space: {}", + existing_pubkey + ); + return Err(LightSdkError::ConstraintViolation); + } + } + Ok(()) +} diff --git a/sdk-libs/sdk/src/compressible/decompress_idempotent.rs b/sdk-libs/sdk/src/compressible/decompress_idempotent.rs new file mode 100644 index 0000000000..e1e0ce4478 --- /dev/null +++ b/sdk-libs/sdk/src/compressible/decompress_idempotent.rs @@ -0,0 +1,152 @@ +use light_compressed_account::{ + address::derive_address, instruction_data::with_account_info::CompressedAccountInfo, +}; +use light_hasher::DataHasher; +use solana_account_info::AccountInfo; +use solana_cpi::invoke_signed; +use solana_msg::msg; +use solana_pubkey::Pubkey; +use solana_rent::Rent; +use solana_system_interface::instruction as system_instruction; +use solana_sysvar::Sysvar; + +use crate::{ + account::sha::LightAccount, compressible::compression_info::HasCompressionInfo, + cpi::CpiAccounts, error::LightSdkError, AnchorDeserialize, AnchorSerialize, LightDiscriminator, +}; + +/// Helper function to decompress multiple compressed accounts into PDAs +/// idempotently with seeds. Does not invoke the zk compression CPI. This +/// function processes accounts of a single type and returns +/// CompressedAccountInfo for CPI batching. It's idempotent, meaning it can be +/// called multiple times with the same compressed accounts and it will only +/// decompress them once. If a PDA already exists and is initialized, it skips +/// that account. +/// +/// # Arguments +/// * `solana_accounts` - The PDA accounts to decompress into +/// * `compressed_accounts` - The compressed accounts to decompress +/// * `solana_accounts_signer_seeds` - Signer seeds for each PDA including bump (standard Solana +/// format) +/// * `cpi_accounts` - Accounts needed for CPI +/// * `rent_payer` - The account to pay for PDA rent +/// * `address_space` - The address space for the compressed accounts +/// +/// # Returns +/// * `Ok(Vec)` - CompressedAccountInfo for CPI batching +/// * `Err(LightSdkError)` if there was an error +pub fn prepare_accounts_for_decompress_idempotent<'info, T>( + solana_accounts: &[&AccountInfo<'info>], + compressed_accounts: Vec>, + solana_accounts_signer_seeds: &[&[&[u8]]], + cpi_accounts: &CpiAccounts<'_, 'info>, + rent_payer: &AccountInfo<'info>, + address_space: Pubkey, +) -> Result, LightSdkError> +where + T: DataHasher + + LightDiscriminator + + AnchorSerialize + + AnchorDeserialize + + Default + + Clone + + HasCompressionInfo + + crate::account::Size, +{ + // Validate input lengths + if solana_accounts.len() != compressed_accounts.len() + || solana_accounts.len() != solana_accounts_signer_seeds.len() + { + return Err(LightSdkError::ConstraintViolation); + } + + let rent = Rent::get().map_err(|_| LightSdkError::Borsh)?; + + let mut compressed_accounts_for_cpi = Vec::new(); + + for ((solana_account, mut compressed_account), seeds) in solana_accounts + .iter() + .zip(compressed_accounts.into_iter()) + .zip(solana_accounts_signer_seeds.iter()) + { + msg!("solana_account: {:?}", solana_account); + // Check if PDA is already initialized + if !solana_account.data_is_empty() { + msg!( + "PDA DATA {} already initialized, skipping decompression", + solana_account.key + ); + continue; + } + + // Get the compressed account address + let c_pda = compressed_account + .address() + .ok_or(LightSdkError::ConstraintViolation)?; + + let derived_c_pda = derive_address( + &solana_account.key.to_bytes(), + &address_space.to_bytes(), + &cpi_accounts.self_program_id().to_bytes(), + ); + + // CHECK: + // pda and c_pda are related + if c_pda != derived_c_pda { + msg!( + "cPDA {:?} does not match derived cPDA {:?} for PDA {:?} with address space {:?}", + c_pda, + derived_c_pda, + solana_account.key, + address_space, + ); + return Err(LightSdkError::ConstraintViolation); + } + + let space = T::size(&compressed_account.account); + let rent_minimum_balance = rent.minimum_balance(space); + + // Create PDA account + let create_account_ix = system_instruction::create_account( + rent_payer.key, + solana_account.key, + rent_minimum_balance, + space as u64, + &cpi_accounts.self_program_id(), + ); + + invoke_signed( + &create_account_ix, + &[ + rent_payer.clone(), + (*solana_account).clone(), + cpi_accounts.system_program()?.clone(), + ], + &[seeds], + )?; + + // Initialize PDA with decompressed data and current slot + let mut decompressed_pda = compressed_account.account.clone(); + *decompressed_pda.compression_info_mut_opt() = + Some(super::CompressionInfo::new_decompressed()?); + + // This forces all programs to implement the LightDiscriminator trait but + // since anchor 0.31.0 this can be any length. + let discriminator_len = T::LIGHT_DISCRIMINATOR.len(); + solana_account.try_borrow_mut_data()?[..discriminator_len] + .copy_from_slice(&T::LIGHT_DISCRIMINATOR); + + decompressed_pda + .serialize(&mut &mut solana_account.try_borrow_mut_data()?[discriminator_len..]) + .map_err(|err| { + msg!("Failed to serialize decompressed PDA: {:?}", err); + LightSdkError::Borsh + })?; + + compressed_account.remove_data(); + + compressed_accounts_for_cpi.push(compressed_account.to_account_info()?); + } + + Ok(compressed_accounts_for_cpi) +} diff --git a/sdk-libs/sdk/src/compressible/mod.rs b/sdk-libs/sdk/src/compressible/mod.rs new file mode 100644 index 0000000000..6e1fa27709 --- /dev/null +++ b/sdk-libs/sdk/src/compressible/mod.rs @@ -0,0 +1,25 @@ +//! SDK helpers for compressing and decompressing PDAs. + +pub mod compress_account; +pub mod compress_account_on_init; +pub mod compression_info; +pub mod config; +pub mod decompress_idempotent; + +#[cfg(feature = "anchor")] +pub use compress_account::compress_account; +pub use compress_account::compress_pda_native; +#[cfg(feature = "anchor")] +pub use compress_account_on_init::{ + compress_account_on_init, prepare_accounts_for_compression_on_init, +}; +pub use compress_account_on_init::{ + compress_account_on_init_native, prepare_accounts_for_compression_on_init_native, +}; +pub use compression_info::{CompressionInfo, HasCompressionInfo}; +pub use config::{ + process_initialize_compression_config_account_info, + process_initialize_compression_config_checked, process_update_compression_config, + CompressibleConfig, COMPRESSIBLE_CONFIG_SEED, MAX_ADDRESS_TREES_PER_SPACE, +}; +pub use decompress_idempotent::prepare_accounts_for_decompress_idempotent; diff --git a/sdk-libs/sdk/src/error.rs b/sdk-libs/sdk/src/error.rs index 3f797a71a6..c2af21c28d 100644 --- a/sdk-libs/sdk/src/error.rs +++ b/sdk-libs/sdk/src/error.rs @@ -92,6 +92,14 @@ impl From for ProgramError { } } +#[cfg(feature = "anchor")] +impl From for anchor_lang::error::Error { + fn from(e: LightSdkError) -> Self { + let error_code = u32::from(e); + anchor_lang::error::Error::from(anchor_lang::prelude::ProgramError::Custom(error_code)) + } +} + impl From for LightSdkError { fn from(e: LightSdkTypesError) -> Self { match e { diff --git a/sdk-libs/sdk/src/lib.rs b/sdk-libs/sdk/src/lib.rs index b8eef1be97..06abfce7ad 100644 --- a/sdk-libs/sdk/src/lib.rs +++ b/sdk-libs/sdk/src/lib.rs @@ -103,8 +103,21 @@ /// Compressed account abstraction similar to anchor Account. pub mod account; +pub use account::LightAccount; + +/// SHA256-based variants +pub mod sha { + pub use light_sdk_macros::{ + LightDiscriminatorSha as LightDiscriminator, LightHasherSha as LightHasher, + }; + + pub use crate::account::sha::LightAccount; +} + /// Functions to derive compressed account addresses. pub mod address; +/// SDK helpers for compressing and decompressing PDAs. +pub mod compressible; /// Utilities to invoke the light-system-program via cpi. pub mod cpi; pub mod error; @@ -116,14 +129,16 @@ pub mod token; pub mod transfer; pub mod utils; +pub use account::Size; #[cfg(feature = "anchor")] -use anchor_lang::{AnchorDeserialize, AnchorSerialize}; +pub use anchor_lang::{AnchorDeserialize, AnchorSerialize}; #[cfg(not(feature = "anchor"))] -use borsh::{BorshDeserialize as AnchorDeserialize, BorshSerialize as AnchorSerialize}; +pub use borsh::{BorshDeserialize as AnchorDeserialize, BorshSerialize as AnchorSerialize}; pub use light_account_checks::{self, discriminator::Discriminator as LightDiscriminator}; pub use light_hasher; pub use light_sdk_macros::{ - derive_light_cpi_signer, light_system_accounts, LightDiscriminator, LightHasher, LightTraits, + derive_light_cpi_signer, light_system_accounts, LightDiscriminator, LightDiscriminatorSha, + LightHasher, LightHasherSha, LightTraits, }; pub use light_sdk_types::constants; use solana_account_info::AccountInfo; diff --git a/sdk-tests/anchor-compressible-derived/Cargo.toml b/sdk-tests/anchor-compressible-derived/Cargo.toml new file mode 100644 index 0000000000..0897d587c6 --- /dev/null +++ b/sdk-tests/anchor-compressible-derived/Cargo.toml @@ -0,0 +1,46 @@ +[package] +name = "anchor-compressible-derived" +version = "0.1.0" +description = "Anchor program template with user records and derived accounts" +edition = "2021" + +[lib] +crate-type = ["cdylib", "lib"] +name = "anchor_compressible_derived" + +[features] +no-entrypoint = [] +no-idl = [] +no-log-ix-name = [] +cpi = ["no-entrypoint"] +default = ["idl-build"] +idl-build = ["anchor-lang/idl-build", "light-sdk/idl-build"] + +test-sbf = [] + + +[dependencies] +light-sdk = { workspace = true, features = ["anchor", "idl-build", "anchor-discriminator-compat"] } +light-sdk-types = { workspace = true } +light-sdk-macros = { workspace = true } +light-hasher = { workspace = true, features = ["solana"] } +light-macros = { workspace = true, features = ["solana"] } +solana-program = { workspace = true } +borsh = { workspace = true } +light-compressed-account = { workspace = true, features = ["solana"] } +anchor-lang = { workspace = true, features = ["idl-build"] } + +[dev-dependencies] +light-program-test = { workspace = true, features = ["v2"] } +light-client = { workspace = true, features = ["v2"] } +light-compressible-client = { workspace = true, features = ["anchor"] } +light-test-utils = { workspace = true } +tokio = { workspace = true } +solana-sdk = { workspace = true } + +[lints.rust.unexpected_cfgs] +level = "allow" +check-cfg = [ + 'cfg(target_os, values("solana"))', + 'cfg(feature, values("frozen-abi", "no-entrypoint"))', +] diff --git a/sdk-tests/anchor-compressible-derived/README.md b/sdk-tests/anchor-compressible-derived/README.md new file mode 100644 index 0000000000..de24ffffcc --- /dev/null +++ b/sdk-tests/anchor-compressible-derived/README.md @@ -0,0 +1,278 @@ +# Example: Using the add_compressible_instructions Macro + +This example shows how to use the `add_compressible_instructions` macro to automatically generate compression-related instructions for your Anchor program. + +## Basic Setup + +```rust +use anchor_lang::prelude::*; +use light_sdk::{ + compressible::{CompressionInfo, HasCompressionInfo}, + derive_light_cpi_signer, LightDiscriminator, LightHasher, +}; +use light_sdk_macros::add_compressible_instructions; + +declare_id!("YourProgramId11111111111111111111111111111"); + +// Define your CPI signer +pub const LIGHT_CPI_SIGNER: CpiSigner = + derive_light_cpi_signer!("YourCpiSignerPubkey11111111111111111111111"); + +// Apply the macro to your program module +#[add_compressible_instructions(UserRecord, GameSession)] +#[program] +pub mod my_program { + use super::*; + + // The macro automatically generates these instructions: + // - create_compression_config (config management) + // - update_compression_config (config management) + // - compress_user_record (compress existing PDA) + // - compress_game_session (compress existing PDA) + // - decompress_multiple_pdas (decompress compressed accounts) + // + // NOTE: create_user_record and create_game_session are NOT generated + // because they typically need custom initialization logic + + // You can still add your own custom instructions here +} +``` + +## Define Your Account Structures + +```rust +#[derive(Debug, LightHasher, LightDiscriminator, Default)] +#[account] +pub struct UserRecord { + #[skip] // Skip compression_info from hashing + pub compression_info: CompressionInfo, + #[hash] // Include in hash + pub owner: Pubkey, + #[hash] + pub name: String, + pub score: u64, +} + +// Implement the required trait +impl HasCompressionInfo for UserRecord { + fn compression_info(&self) -> &CompressionInfo { + &self.compression_info + } + + fn compression_info_mut(&mut self) -> &mut CompressionInfo { + &mut self.compression_info + } +} +``` + +## Generated Instructions + +### 1. Config Management + +```typescript +// Create config (only program upgrade authority can call) +await program.methods + .createCompressibleConfig( + 100, // compression_delay + rentRecipient, + [addressSpace] // Now accepts an array of address trees (1-4 allowed) + ) + .accounts({ + payer: wallet.publicKey, + config: configPda, + programData: programDataPda, + authority: upgradeAuthority, + systemProgram: SystemProgram.programId, + }) + .signers([upgradeAuthority]) + .rpc(); + +// Update config +await program.methods + .updateCompressibleConfig( + 200, // new_compression_delay (optional) + newRentRecipient, // (optional) + [newAddressSpace1, newAddressSpace2], // (optional) - array of 1-4 address trees + newUpdateAuthority // (optional) + ) + .accounts({ + config: configPda, + authority: configUpdateAuthority, + }) + .signers([configUpdateAuthority]) + .rpc(); +``` + +### 2. Compress Existing PDA + +```typescript +await program.methods + .compressUserRecord(proof, compressedAccountMeta) + .accounts({ + user: user.publicKey, + pdaAccount: userRecordPda, + systemProgram: SystemProgram.programId, + config: configPda, + rentRecipient: rentRecipient, + }) + .remainingAccounts(lightSystemAccounts) + .signers([user]) + .rpc(); +``` + +### 3. Decompress Multiple PDAs + +```typescript +const compressedAccounts = [ + { + meta: compressedAccountMeta1, + data: { userRecord: userData }, + seeds: [Buffer.from("user_record"), user.publicKey.toBuffer()], + }, + { + meta: compressedAccountMeta2, + data: { gameSession: gameData }, + seeds: [ + Buffer.from("game_session"), + sessionId.toArrayLike(Buffer, "le", 8), + ], + }, +]; + +await program.methods + .decompressMultiplePdas( + proof, + compressedAccounts, + [userBump, gameBump], // PDA bumps + systemAccountsOffset + ) + .accounts({ + feePayer: payer.publicKey, + rentPayer: payer.publicKey, + systemProgram: SystemProgram.programId, + }) + .remainingAccounts([ + ...pdaAccounts, // PDAs to decompress into + ...lightSystemAccounts, // Light Protocol system accounts + ]) + .signers([payer]) + .rpc(); +``` + +## Address Space Configuration + +The config now supports multiple address trees per address space (1-4 allowed): + +```typescript +// Single address tree (backward compatible) +const addressSpace = [addressTree1]; + +// Multiple address trees for better scalability +const addressSpace = [addressTree1, addressTree2, addressTree3]; + +// When creating config +await program.methods + .createCompressibleConfig( + 100, + rentRecipient, + addressSpace // Array of 1-4 unique address tree pubkeys + ) + // ... accounts + .rpc(); +``` + +### Address Space Validation Rules + +**Create Config:** + +- Must contain 1-4 unique address tree pubkeys +- No duplicate pubkeys allowed +- All pubkeys must be valid address trees + +**Update Config:** + +- Can only **add** new address trees, never remove existing ones +- No duplicate pubkeys allowed in the new configuration +- Must maintain all existing address trees + +```typescript +// Valid update: adding new trees +const currentAddressSpace = [tree1, tree2]; +const newAddressSpace = [tree1, tree2, tree3]; // ✅ Valid: adds tree3 + +// Invalid update: removing existing trees +const invalidAddressSpace = [tree2, tree3]; // ❌ Invalid: removes tree1 +``` + +The system validates that compressed accounts use address trees from the configured address space, providing flexibility while maintaining security and preventing accidental removal of active trees. + +## What You Need to Implement + +Since the macro only generates compression-related instructions, you need to implement: + +### 1. Create Instructions + +Implement your own create instructions for each account type: + +```rust +#[derive(Accounts)] +pub struct CreateUserRecord<'info> { + #[account(mut)] + pub user: Signer<'info>, + #[account( + init, + payer = user, + space = 8 + UserRecord::INIT_SPACE, + seeds = [b"user_record", user.key().as_ref()], + bump, + )] + pub user_record: Account<'info, UserRecord>, + pub system_program: Program<'info, System>, +} + +pub fn create_user_record( + ctx: Context, + name: String, +) -> Result<()> { + let user_record = &mut ctx.accounts.user_record; + + // Your custom initialization logic here + user_record.compression_info = CompressionInfo::new_decompressed()?; + user_record.owner = ctx.accounts.user.key(); + user_record.name = name; + user_record.score = 0; + + Ok(()) +} +``` + +### 2. Update Instructions + +Implement update instructions for your account types with your custom business logic. + +## Customization + +### Custom Seeds + +Use custom seeds in your PDA derivation and pass them in the `seeds` parameter when decompressing: + +```rust +seeds = [b"custom_prefix", user.key().as_ref(), &session_id.to_le_bytes()] +``` + +## Best Practices + +1. **Create Config Early**: Create the config immediately after program deployment +2. **Use Config Values**: Always use config values instead of hardcoded constants +3. **Validate Rent Recipient**: The macro automatically validates rent recipient matches config +4. **Handle Compression Timing**: Respect the compression delay from config +5. **Batch Operations**: Use decompress_multiple_pdas for efficiency + +## Migration from Manual Implementation + +If migrating from a manual implementation: + +1. Update your account structs to use `CompressionInfo` instead of separate fields +2. Implement the `HasCompressionInfo` trait +3. Replace your manual instructions with the macro +4. Update client code to use the new instruction names diff --git a/sdk-tests/anchor-compressible-derived/Xargo.toml b/sdk-tests/anchor-compressible-derived/Xargo.toml new file mode 100644 index 0000000000..9e7d95be7f --- /dev/null +++ b/sdk-tests/anchor-compressible-derived/Xargo.toml @@ -0,0 +1,2 @@ +[target.bpfel-unknown-unknown.dependencies.std] +features = [] \ No newline at end of file diff --git a/sdk-tests/anchor-compressible-derived/src/constraints.rs b/sdk-tests/anchor-compressible-derived/src/constraints.rs new file mode 100644 index 0000000000..9a6a9669b5 --- /dev/null +++ b/sdk-tests/anchor-compressible-derived/src/constraints.rs @@ -0,0 +1,27 @@ +use anchor_lang::prelude::*; + +use crate::state::UserRecord; + +// In a standalone file to test macro support. +#[derive(Accounts)] +pub struct CreateRecord<'info> { + #[account(mut)] + pub user: Signer<'info>, + #[account( + init, + payer = user, + // Manually add 10 bytes! Discriminator + owner + string len + name + + // score + option + space = 8 + 32 + 4 + 32 + 8 + 10, + seeds = [b"user_record", user.key().as_ref()], + bump, + )] + pub user_record: Account<'info, UserRecord>, + /// UNCHECKED: checked via config. + #[account(mut)] + pub rent_recipient: AccountInfo<'info>, + /// The global config account + /// UNCHECKED: checked via load_checked. + pub config: AccountInfo<'info>, + pub system_program: Program<'info, System>, +} diff --git a/sdk-tests/anchor-compressible-derived/src/lib.rs b/sdk-tests/anchor-compressible-derived/src/lib.rs new file mode 100644 index 0000000000..b213908e00 --- /dev/null +++ b/sdk-tests/anchor-compressible-derived/src/lib.rs @@ -0,0 +1,276 @@ +use anchor_lang::prelude::*; +use light_sdk::{ + compressible::{ + compress_account_on_init, prepare_accounts_for_compression_on_init, CompressibleConfig, + CompressionInfo, HasCompressionInfo, + }, + cpi::{CpiAccounts, CpiInputs}, + derive_light_cpi_signer, + instruction::{PackedAddressTreeInfo, ValidityProof}, + LightDiscriminator, +}; +use light_sdk_macros::add_compressible_instructions; +use light_sdk_types::CpiSigner; + +pub mod constraints; +pub mod state; +// Re-export structs so they're accessible to tests and external users +pub use constraints::CreateRecord; +use constraints::*; +// pub use state::*; +pub use state::{GameSession, UserRecord}; + +// Re-export the generated types for client access Explicitly re-export only the +// macro-generated types you need to expose. This avoids any name clash with the +// module itself. +pub use crate::anchor_compressible_derived::{CompressedAccountData, CompressedAccountVariant}; + +declare_id!("GRLu2hKaAiMbxpkAM1HeXzks9YeGuz18SEgXEizVvPqX"); +pub const LIGHT_CPI_SIGNER: CpiSigner = + derive_light_cpi_signer!("GRLu2hKaAiMbxpkAM1HeXzks9YeGuz18SEgXEizVvPqX"); + +#[add_compressible_instructions(UserRecord, GameSession)] +#[program] +pub mod anchor_compressible_derived { + + use super::*; + + /// Creates a new compressed user record using global config. + pub fn create_record<'info>( + ctx: Context<'_, '_, '_, 'info, CreateRecord<'info>>, + name: String, + compressed_address: [u8; 32], + address_tree_info: PackedAddressTreeInfo, + proof: ValidityProof, + output_state_tree_index: u8, + ) -> Result<()> { + let user_record = &mut ctx.accounts.user_record; + + // Load config from the config account + let config = CompressibleConfig::load_checked(&ctx.accounts.config, &crate::ID) + .map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotDeserialize)?; + + user_record.owner = ctx.accounts.user.key(); + user_record.name = name; + user_record.score = 11; + // Initialize compression info with current slot + user_record.compression_info = Some( + CompressionInfo::new_decompressed() + .map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotDeserialize)?, + ); + + // Verify rent recipient matches config + if ctx.accounts.rent_recipient.key() != config.rent_recipient { + return err!(ErrorCode::InvalidRentRecipient); + } + + let cpi_accounts = + CpiAccounts::new(&ctx.accounts.user, ctx.remaining_accounts, LIGHT_CPI_SIGNER); + let new_address_params = + address_tree_info.into_new_address_params_packed(user_record.key().to_bytes()); + + compress_account_on_init::( + user_record, + &compressed_address, + &new_address_params, + output_state_tree_index, + cpi_accounts, + &config.address_space, + &ctx.accounts.rent_recipient, + proof, + )?; + Ok(()) + } + + pub fn update_record( + ctx: Context, + name: String, + score: u64, + ) -> anchor_lang::Result<()> { + let user_record = &mut ctx.accounts.user_record; + + // Update the record data + user_record.name = name; + user_record.score = score; + + // MANUALLY set the last written slot using the trait + user_record + .compression_info_mut() + .set_last_written_slot() + .map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotDeserialize)?; + + Ok(()) + } + + /// Creates both a user record and game session in one instruction. + /// Must be manually implemented. + pub fn create_record_and_session<'info>( + ctx: Context<'_, '_, '_, 'info, CreateRecordAndSession<'info>>, + account_data: AccountCreationData, + compression_params: CompressionParams, + ) -> Result<()> { + let user_record = &mut ctx.accounts.user_record; + let game_session = &mut ctx.accounts.game_session; + + // Load config checked + let config = CompressibleConfig::load_checked(&ctx.accounts.config, &crate::ID) + .map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotDeserialize)?; + + // Check that rent recipient matches config + if ctx.accounts.rent_recipient.key() != config.rent_recipient { + return err!(ErrorCode::InvalidRentRecipient); + } + + // Set user record data + user_record.owner = ctx.accounts.user.key(); + user_record.name = account_data.user_name; + user_record.score = 11; + // Initialize compression info with current slot + user_record.compression_info = Some( + CompressionInfo::new_decompressed() + .map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotDeserialize)?, + ); + + // Set game session data + game_session.session_id = account_data.session_id; + game_session.player = ctx.accounts.user.key(); + game_session.game_type = account_data.game_type; + game_session.start_time = Clock::get()?.unix_timestamp as u64; + game_session.end_time = None; + game_session.score = 0; + // Initialize compression info with current slot + game_session.compression_info = Some( + CompressionInfo::new_decompressed() + .map_err(|_| anchor_lang::error::ErrorCode::AccountDidNotDeserialize)?, + ); + + // Create CPI accounts + let cpi_accounts = + CpiAccounts::new(&ctx.accounts.user, ctx.remaining_accounts, LIGHT_CPI_SIGNER); + + // Prepare new address params for both accounts + let user_new_address_params = compression_params + .user_address_tree_info + .into_new_address_params_packed(user_record.key().to_bytes()); + let game_new_address_params = compression_params + .game_address_tree_info + .into_new_address_params_packed(game_session.key().to_bytes()); + + let mut all_compressed_infos = Vec::new(); + + // Prepare user record for compression + let user_compressed_infos = prepare_accounts_for_compression_on_init::( + &mut [user_record], + &[compression_params.user_compressed_address], + &[user_new_address_params], + &[compression_params.user_output_state_tree_index], + &cpi_accounts, + &config.address_space, + &ctx.accounts.rent_recipient, + )?; + + all_compressed_infos.extend(user_compressed_infos); + + // Prepare game session for compression + let game_compressed_infos = prepare_accounts_for_compression_on_init::( + &mut [game_session], + &[compression_params.game_compressed_address], + &[game_new_address_params], + &[compression_params.game_output_state_tree_index], + &cpi_accounts, + &config.address_space, + &ctx.accounts.rent_recipient, + )?; + + all_compressed_infos.extend(game_compressed_infos); + + // Create CPI inputs with all compressed accounts and new addresses + let cpi_inputs = CpiInputs::new_with_address( + compression_params.proof, + all_compressed_infos, + vec![user_new_address_params, game_new_address_params], + ); + + // Invoke light system program to create all compressed accounts in one CPI + cpi_inputs.invoke_light_system_program(cpi_accounts)?; + + Ok(()) + } + + // The add_compressible_instructions macro will generate: + // - initialize_compression_config (config management) + // - update_compression_config (config management) + // - compress_record (compress existing PDA) + // - compress_session (compress existing PDA) + // - decompress_accounts_idempotent (decompress compressed accounts) + // Plus all the necessary structs and enums + + #[derive(Accounts)] + #[instruction(account_data: AccountCreationData)] + pub struct CreateRecordAndSession<'info> { + #[account(mut)] + pub user: Signer<'info>, + #[account( + init, + payer = user, + // discriminator + owner + string len + name + score + option + space = 8 + 32 + 4 + 32 + 8 + 10, + seeds = [b"user_record", user.key().as_ref()], + bump, + )] + pub user_record: Account<'info, UserRecord>, + #[account( + init, + payer = user, + // discriminator + option + session_id + player + string len + game_type + start_time + end_time(Option) + score + space = 8 + 10 + 8 + 32 + 4 + 32 + 8 + 9 + 8, + seeds = [b"game_session", account_data.session_id.to_le_bytes().as_ref()], + bump, + )] + pub game_session: Account<'info, GameSession>, + pub system_program: Program<'info, System>, + /// The global config account + /// UNCHECKED: checked via load_checked. + pub config: AccountInfo<'info>, + /// UNCHECKED: checked via config. + #[account(mut)] + pub rent_recipient: AccountInfo<'info>, + } +} + +#[derive(Accounts)] +pub struct UpdateRecord<'info> { + #[account(mut)] + pub user: Signer<'info>, + #[account( + mut, + seeds = [b"user_record", user.key().as_ref()], + bump, + constraint = user_record.owner == user.key() + )] + pub user_record: Account<'info, UserRecord>, +} + +#[error_code] +pub enum ErrorCode { + #[msg("Rent recipient does not match config")] + InvalidRentRecipient, +} + +#[derive(AnchorSerialize, AnchorDeserialize)] +pub struct AccountCreationData { + pub user_name: String, + pub session_id: u64, + pub game_type: String, +} + +#[derive(AnchorSerialize, AnchorDeserialize)] +pub struct CompressionParams { + pub proof: ValidityProof, + pub user_compressed_address: [u8; 32], + pub user_address_tree_info: PackedAddressTreeInfo, + pub user_output_state_tree_index: u8, + pub game_compressed_address: [u8; 32], + pub game_address_tree_info: PackedAddressTreeInfo, + pub game_output_state_tree_index: u8, +} diff --git a/sdk-tests/anchor-compressible-derived/src/state.rs b/sdk-tests/anchor-compressible-derived/src/state.rs new file mode 100644 index 0000000000..fe4ba6ba68 --- /dev/null +++ b/sdk-tests/anchor-compressible-derived/src/state.rs @@ -0,0 +1,32 @@ +use anchor_lang::prelude::*; +use light_sdk::{compressible::CompressionInfo, LightDiscriminator, LightHasher}; +use light_sdk_macros::HasCompressionInfo; + +#[derive(Debug, LightHasher, LightDiscriminator, HasCompressionInfo, Default, InitSpace)] +#[account] +pub struct UserRecord { + #[skip] + pub compression_info: Option, + #[hash] + pub owner: Pubkey, + #[hash] + #[max_len(32)] + pub name: String, + pub score: u64, +} + +#[derive(Debug, LightHasher, LightDiscriminator, Default, InitSpace, HasCompressionInfo)] +#[account] +pub struct GameSession { + #[skip] + pub compression_info: Option, + pub session_id: u64, + #[hash] + pub player: Pubkey, + #[hash] + #[max_len(32)] + pub game_type: String, + pub start_time: u64, + pub end_time: Option, + pub score: u64, +} diff --git a/sdk-tests/anchor-compressible-derived/tests/test_decompress_multiple.rs b/sdk-tests/anchor-compressible-derived/tests/test_decompress_multiple.rs new file mode 100644 index 0000000000..42bd7c14dc --- /dev/null +++ b/sdk-tests/anchor-compressible-derived/tests/test_decompress_multiple.rs @@ -0,0 +1,1164 @@ +#![cfg(feature = "test-sbf")] + +use anchor_compressible_derived::{CompressedAccountVariant, GameSession, UserRecord}; +use anchor_lang::{ + AccountDeserialize, AnchorDeserialize, Discriminator, InstructionData, ToAccountMetas, +}; +use light_compressed_account::address::derive_address; +use light_compressible_client::CompressibleInstruction; +use light_macros::pubkey; +use light_program_test::{ + initialize_compression_config, + program_test::{LightProgramTest, TestRpc}, + setup_mock_program_data, + utils::simulation::simulate_cu, + AddressWithTree, Indexer, ProgramTestConfig, Rpc, RpcError, +}; +use light_sdk::{ + compressible::CompressibleConfig, + instruction::{PackedAccounts, SystemAccountMetaConfig}, +}; +use solana_sdk::{ + instruction::Instruction, + pubkey::Pubkey, + signature::{Keypair, Signer}, +}; + +// test values +pub const ADDRESS_SPACE: [Pubkey; 1] = [pubkey!("EzKE84aVTkCUhDHLELqyJaq1Y7UVVmqxXqZjVHwHY3rK")]; +pub const RENT_RECIPIENT: Pubkey = pubkey!("CLEuMG7pzJX9xAuKCFzBP154uiG1GaNo4Fq7x6KAcAfG"); + +#[tokio::test] +async fn test_create_and_decompress_two_accounts() { + let program_id = anchor_compressible_derived::ID; + let config = ProgramTestConfig::new_v2( + true, + Some(vec![("anchor_compressible_derived", program_id)]), + ); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + + let config_pda = CompressibleConfig::derive_pda(&program_id, 0).0; + let _program_data_pda = setup_mock_program_data(&mut rpc, &payer, &program_id); + + let result = initialize_compression_config( + &mut rpc, + &payer, + &program_id, + &payer, + 100, + RENT_RECIPIENT, + vec![ADDRESS_SPACE[0]], + &CompressibleInstruction::INITIALIZE_COMPRESSION_CONFIG_DISCRIMINATOR, + None, + ) + .await; + assert!(result.is_ok(), "Initialize config should succeed"); + + let combined_user = Keypair::new(); + let fund_user_ix = solana_sdk::system_instruction::transfer( + &payer.pubkey(), + &combined_user.pubkey(), + 1e9 as u64, + ); + let fund_result = rpc + .create_and_send_transaction(&[fund_user_ix], &payer.pubkey(), &[&payer]) + .await; + assert!(fund_result.is_ok(), "Funding combined user should succeed"); + let combined_session_id = 99999u64; + let (combined_user_record_pda, combined_user_record_bump) = Pubkey::find_program_address( + &[b"user_record", combined_user.pubkey().as_ref()], + &program_id, + ); + let (combined_game_session_pda, combined_game_bump) = Pubkey::find_program_address( + &[b"game_session", combined_session_id.to_le_bytes().as_ref()], + &program_id, + ); + + test_create_user_record_and_game_session( + &mut rpc, + &combined_user, + &program_id, + &config_pda, + &combined_user_record_pda, + &combined_game_session_pda, + combined_session_id, + ) + .await; + + rpc.warp_to_slot(200).unwrap(); + + test_decompress_multiple_pdas( + &mut rpc, + &combined_user, + &program_id, + &config_pda, + &combined_user_record_pda, + &combined_user_record_bump, + &combined_game_session_pda, + &combined_game_bump, + combined_session_id, + "Combined User", + "Combined Game", + 200, + ) + .await; +} + +#[tokio::test] +async fn test_create_decompress_compress_single_account() { + let program_id = anchor_compressible_derived::ID; + let config = ProgramTestConfig::new_v2( + true, + Some(vec![("anchor_compressible_derived", program_id)]), + ); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + let _program_data_pda = setup_mock_program_data(&mut rpc, &payer, &program_id); + + let result = initialize_compression_config( + &mut rpc, + &payer, + &program_id, + &payer, + 100, + RENT_RECIPIENT, + vec![ADDRESS_SPACE[0]], + &CompressibleInstruction::INITIALIZE_COMPRESSION_CONFIG_DISCRIMINATOR, + None, + ) + .await; + assert!(result.is_ok(), "Initialize config should succeed"); + + let (user_record_pda, user_record_bump) = + Pubkey::find_program_address(&[b"user_record", payer.pubkey().as_ref()], &program_id); + + test_create_record(&mut rpc, &payer, &program_id, &user_record_pda, None).await; + + rpc.warp_to_slot(100).unwrap(); + + println!("decompress single"); + test_decompress_single_user_record( + &mut rpc, + &payer, + &program_id, + &user_record_pda, + &user_record_bump, + "Test User", + 100, + ) + .await; + + rpc.warp_to_slot(101).unwrap(); + + println!("compress record"); + + let result = test_compress_record(&mut rpc, &payer, &program_id, &user_record_pda, true).await; + assert!(result.is_err(), "Compression should fail due to slot delay"); + if let Err(err) = result { + let err_msg = format!("{:?}", err); + assert!( + err_msg.contains("Custom(16001)"), + "Expected error message about slot delay, got: {}", + err_msg + ); + } + rpc.warp_to_slot(200).unwrap(); + let _result = + test_compress_record(&mut rpc, &payer, &program_id, &user_record_pda, false).await; +} + +async fn test_create_record( + rpc: &mut LightProgramTest, + payer: &Keypair, + program_id: &Pubkey, + user_record_pda: &Pubkey, + state_tree_queue: Option, +) { + let config_pda = CompressibleConfig::derive_pda(program_id, 0).0; + // Setup remaining accounts for Light Protocol + let mut remaining_accounts = PackedAccounts::default(); + let system_config = SystemAccountMetaConfig::new(*program_id); + remaining_accounts.add_system_accounts(system_config); + + // Get address tree info + let address_tree_pubkey = rpc.get_address_merkle_tree_v2(); + + // Create the instruction + let accounts = anchor_compressible_derived::accounts::CreateRecord { + user: payer.pubkey(), + user_record: *user_record_pda, + system_program: solana_sdk::system_program::ID, + config: config_pda, + rent_recipient: RENT_RECIPIENT, + }; + + // Derive a new address for the compressed account + let compressed_address = derive_address( + &user_record_pda.to_bytes(), + &address_tree_pubkey.to_bytes(), + &program_id.to_bytes(), + ); + + // Get validity proof from RPC + let rpc_result = rpc + .get_validity_proof( + vec![], + vec![AddressWithTree { + address: compressed_address, + tree: address_tree_pubkey, + }], + None, + ) + .await + .unwrap() + .value; + + // Pack tree infos into remaining accounts + let packed_tree_infos = rpc_result.pack_tree_infos(&mut remaining_accounts); + + // Get the packed address tree info + let address_tree_info = packed_tree_infos.address_trees[0]; + + // Get output state tree index + let output_state_tree_index = remaining_accounts.insert_or_get( + state_tree_queue.unwrap_or_else(|| rpc.get_random_state_tree_info().unwrap().queue), + ); + + // Get system accounts for the instruction + let (system_accounts, _, _) = remaining_accounts.to_account_metas(); + + // Create instruction data + let instruction_data = anchor_compressible_derived::instruction::CreateRecord { + name: "Test User".to_string(), + proof: rpc_result.proof, + compressed_address, + address_tree_info, + output_state_tree_index, + }; + + // Build the instruction + let instruction = Instruction { + program_id: *program_id, + accounts: [accounts.to_account_metas(None), system_accounts].concat(), + data: instruction_data.data(), + }; + + let cu = simulate_cu(rpc, payer, &instruction).await; + println!("CreateRecord CU consumed: {}", cu); + + // Create and send transaction + let result = rpc + .create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await; + + assert!(result.is_ok(), "Transaction should succeed"); + + // should be empty + let user_record_account = rpc.get_account(*user_record_pda).await.unwrap(); + assert!( + user_record_account.is_some(), + "Account should exist after compression" + ); + + let account = user_record_account.unwrap(); + assert_eq!(account.lamports, 0, "Account lamports should be 0"); + + let user_record_data = account.data; + + assert!(user_record_data.is_empty(), "Account data should be empty"); +} + +#[allow(clippy::too_many_arguments)] +async fn test_decompress_multiple_pdas( + rpc: &mut LightProgramTest, + payer: &Keypair, + program_id: &Pubkey, + _config_pda: &Pubkey, + user_record_pda: &Pubkey, + user_record_bump: &u8, + game_session_pda: &Pubkey, + game_bump: &u8, + session_id: u64, + expected_user_name: &str, + expected_game_type: &str, + expected_slot: u64, +) { + let address_tree_pubkey = rpc.get_address_merkle_tree_v2(); + + // c pda USER_RECORD + let user_compressed_address = derive_address( + &user_record_pda.to_bytes(), + &address_tree_pubkey.to_bytes(), + &program_id.to_bytes(), + ); + let c_user_pda = rpc + .get_compressed_account(user_compressed_address, None) + .await + .unwrap() + .value; + + let user_account_data = c_user_pda.data.as_ref().unwrap(); + + let c_user_record = UserRecord::deserialize(&mut &user_account_data.data[..]).unwrap(); + + // c pda GAME_SESSION + let game_compressed_address = derive_address( + &game_session_pda.to_bytes(), + &address_tree_pubkey.to_bytes(), + &program_id.to_bytes(), + ); + let c_game_pda = rpc + .get_compressed_account(game_compressed_address, None) + .await + .unwrap() + .value; + let game_account_data = c_game_pda.data.as_ref().unwrap(); + + let c_game_session = GameSession::deserialize(&mut &game_account_data.data[..]).unwrap(); + + // Get validity proof for both compressed accounts + let rpc_result = rpc + .get_validity_proof(vec![c_user_pda.hash, c_game_pda.hash], vec![], None) + .await + .unwrap() + .value; + + let output_state_tree_info = rpc.get_random_state_tree_info().unwrap(); + + // Use the new SDK helper function with typed data + let instruction = + light_compressible_client::CompressibleInstruction::decompress_accounts_idempotent( + program_id, + &CompressibleInstruction::DECOMPRESS_ACCOUNTS_IDEMPOTENT_DISCRIMINATOR, + &payer.pubkey(), + &payer.pubkey(), // rent_payer can be the same as fee_payer + &[*user_record_pda, *game_session_pda], + &[ + ( + c_user_pda, + CompressedAccountVariant::UserRecord(c_user_record), + vec![b"user_record".to_vec(), payer.pubkey().to_bytes().to_vec()], + ), + ( + c_game_pda, + CompressedAccountVariant::GameSession(c_game_session), + vec![b"game_session".to_vec(), session_id.to_le_bytes().to_vec()], + ), + ], + &[*user_record_bump, *game_bump], + rpc_result, + output_state_tree_info, + ) + .unwrap(); + + let cu = simulate_cu(rpc, payer, &instruction).await; + println!("decompress_multiple_pdas CU consumed: {}", cu); + + // Verify PDAs are uninitialized before decompression + let user_pda_account = rpc.get_account(*user_record_pda).await.unwrap(); + assert_eq!( + user_pda_account.as_ref().map(|a| a.data.len()).unwrap_or(0), + 0, + "User PDA account data len must be 0 before decompression" + ); + + let game_pda_account = rpc.get_account(*game_session_pda).await.unwrap(); + assert_eq!( + game_pda_account.as_ref().map(|a| a.data.len()).unwrap_or(0), + 0, + "Game PDA account data len must be 0 before decompression" + ); + + let cu = simulate_cu(rpc, payer, &instruction).await; + println!("decompress_multiple_pdas CU consumed: {}", cu); + + let result = rpc + .create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await; + assert!(result.is_ok(), "Decompress transaction should succeed"); + + // Verify UserRecord PDA is decompressed + let user_pda_account = rpc.get_account(*user_record_pda).await.unwrap(); + println!( + "user_pda_account after decompression: {:?}", + user_pda_account + ); + assert!( + user_pda_account.as_ref().map(|a| a.data.len()).unwrap_or(0) > 0, + "User PDA account data len must be > 0 after decompression" + ); + + let user_pda_data = user_pda_account.unwrap().data; + assert_eq!( + &user_pda_data[0..8], + UserRecord::DISCRIMINATOR, + "User account anchor discriminator mismatch" + ); + + let decompressed_user_record = UserRecord::try_deserialize(&mut &user_pda_data[..]).unwrap(); + assert_eq!(decompressed_user_record.name, expected_user_name); + assert_eq!(decompressed_user_record.score, 11); + assert_eq!(decompressed_user_record.owner, payer.pubkey()); + assert!(!decompressed_user_record + .compression_info + .as_ref() + .unwrap() + .is_compressed()); + assert_eq!( + decompressed_user_record + .compression_info + .as_ref() + .unwrap() + .last_written_slot(), + expected_slot + ); + + // Verify GameSession PDA is decompressed + let game_pda_account = rpc.get_account(*game_session_pda).await.unwrap(); + assert!( + game_pda_account.as_ref().map(|a| a.data.len()).unwrap_or(0) > 0, + "Game PDA account data len must be > 0 after decompression" + ); + + let game_pda_data = game_pda_account.unwrap().data; + assert_eq!( + &game_pda_data[0..8], + anchor_compressible_derived::GameSession::DISCRIMINATOR, + "Game account anchor discriminator mismatch" + ); + + let decompressed_game_session = + anchor_compressible_derived::GameSession::try_deserialize(&mut &game_pda_data[..]).unwrap(); + assert_eq!(decompressed_game_session.session_id, session_id); + assert_eq!(decompressed_game_session.game_type, expected_game_type); + assert_eq!(decompressed_game_session.player, payer.pubkey()); + assert_eq!(decompressed_game_session.score, 0); + assert!(!decompressed_game_session + .compression_info + .as_ref() + .unwrap() + .is_compressed()); + assert_eq!( + decompressed_game_session + .compression_info + .as_ref() + .unwrap() + .last_written_slot(), + expected_slot + ); + + // Verify compressed accounts exist and have correct data + let c_game_pda = rpc + .get_compressed_account(game_compressed_address, None) + .await + .unwrap() + .value; + + assert!(c_game_pda.data.is_some()); + assert_eq!(c_game_pda.data.unwrap().data.len(), 0); +} + +async fn test_create_user_record_and_game_session( + rpc: &mut LightProgramTest, + user: &Keypair, + program_id: &Pubkey, + config_pda: &Pubkey, + user_record_pda: &Pubkey, + game_session_pda: &Pubkey, + session_id: u64, +) { + // Setup remaining accounts for Light Protocol + let mut remaining_accounts = PackedAccounts::default(); + let system_config = SystemAccountMetaConfig::new(*program_id); + remaining_accounts.add_system_accounts(system_config); + + // Get address tree info + let address_tree_pubkey = rpc.get_address_merkle_tree_v2(); + + // Create the instruction + let accounts = anchor_compressible_derived::accounts::CreateRecordAndSession { + user: user.pubkey(), + user_record: *user_record_pda, + game_session: *game_session_pda, + system_program: solana_sdk::system_program::ID, + config: *config_pda, + rent_recipient: RENT_RECIPIENT, + }; + + // Derive addresses for both compressed accounts + let user_compressed_address = derive_address( + &user_record_pda.to_bytes(), + &address_tree_pubkey.to_bytes(), + &program_id.to_bytes(), + ); + let game_compressed_address = derive_address( + &game_session_pda.to_bytes(), + &address_tree_pubkey.to_bytes(), + &program_id.to_bytes(), + ); + + // Get validity proof from RPC + let rpc_result = rpc + .get_validity_proof( + vec![], + vec![ + AddressWithTree { + address: user_compressed_address, + tree: address_tree_pubkey, + }, + AddressWithTree { + address: game_compressed_address, + tree: address_tree_pubkey, + }, + ], + None, + ) + .await + .unwrap() + .value; + + // Pack tree infos into remaining accounts + let packed_tree_infos = rpc_result.pack_tree_infos(&mut remaining_accounts); + + // Get the packed address tree info (both should use the same tree) + let user_address_tree_info = packed_tree_infos.address_trees[0]; + let game_address_tree_info = packed_tree_infos.address_trees[1]; + + // Get output state tree indices + let user_output_state_tree_index = + remaining_accounts.insert_or_get(rpc.get_random_state_tree_info().unwrap().queue); + let game_output_state_tree_index = + remaining_accounts.insert_or_get(rpc.get_random_state_tree_info().unwrap().queue); + + // Get system accounts for the instruction + let (system_accounts, _, _) = remaining_accounts.to_account_metas(); + + // Create instruction data + let instruction_data = anchor_compressible_derived::instruction::CreateRecordAndSession { + account_data: anchor_compressible_derived::AccountCreationData { + user_name: "Combined User".to_string(), + session_id, + game_type: "Combined Game".to_string(), + }, + compression_params: anchor_compressible_derived::CompressionParams { + proof: rpc_result.proof, + user_compressed_address, + user_address_tree_info, + user_output_state_tree_index, + game_compressed_address, + game_address_tree_info, + game_output_state_tree_index, + }, + }; + + // Build the instruction + let instruction = Instruction { + program_id: *program_id, + accounts: [accounts.to_account_metas(None), system_accounts].concat(), + data: instruction_data.data(), + }; + let cu = simulate_cu(rpc, user, &instruction).await; + println!("CreateUserRecordAndGameSession CU consumed: {}", cu); + // Create and send transaction + let result = rpc + .create_and_send_transaction(&[instruction], &user.pubkey(), &[user]) + .await; + + assert!( + result.is_ok(), + "Combined creation transaction should succeed" + ); + + // Verify both accounts are empty after compression + let user_record_account = rpc.get_account(*user_record_pda).await.unwrap(); + assert!( + user_record_account.is_some(), + "User record account should exist after compression" + ); + let account = user_record_account.unwrap(); + assert_eq!( + account.lamports, 0, + "User record account lamports should be 0" + ); + assert!( + account.data.is_empty(), + "User record account data should be empty" + ); + + let game_session_account = rpc.get_account(*game_session_pda).await.unwrap(); + assert!( + game_session_account.is_some(), + "Game session account should exist after compression" + ); + let account = game_session_account.unwrap(); + assert_eq!( + account.lamports, 0, + "Game session account lamports should be 0" + ); + assert!( + account.data.is_empty(), + "Game session account data should be empty" + ); + + // Verify compressed accounts exist and have correct data + let compressed_user_record = rpc + .get_compressed_account(user_compressed_address, None) + .await + .unwrap() + .value; + + assert_eq!( + compressed_user_record.address, + Some(user_compressed_address) + ); + assert!(compressed_user_record.data.is_some()); + + let user_buf = compressed_user_record.data.unwrap().data; + + let user_record = UserRecord::deserialize(&mut &user_buf[..]).unwrap(); + + assert_eq!(user_record.name, "Combined User"); + assert_eq!(user_record.score, 11); + assert_eq!(user_record.owner, user.pubkey()); + + let compressed_game_session = rpc + .get_compressed_account(game_compressed_address, None) + .await + .unwrap() + .value; + + assert_eq!( + compressed_game_session.address, + Some(game_compressed_address) + ); + assert!(compressed_game_session.data.is_some()); + + let game_buf = compressed_game_session.data.unwrap().data; + let game_session = GameSession::deserialize(&mut &game_buf[..]).unwrap(); + assert_eq!(game_session.session_id, session_id); + assert_eq!(game_session.game_type, "Combined Game"); + assert_eq!(game_session.player, user.pubkey()); + assert_eq!(game_session.score, 0); +} + +async fn test_compress_record( + rpc: &mut LightProgramTest, + payer: &Keypair, + program_id: &Pubkey, + user_record_pda: &Pubkey, + should_fail: bool, +) -> Result { + // Get the current decompressed user record data + let user_pda_account = rpc.get_account(*user_record_pda).await.unwrap(); + assert!( + user_pda_account.is_some(), + "User PDA account should exist before compression" + ); + let account = user_pda_account.unwrap(); + assert!( + account.lamports > 0, + "Account should have lamports before compression" + ); + assert!( + !account.data.is_empty(), + "Account data should not be empty before compression" + ); + + // Setup remaining accounts for Light Protocol + let mut remaining_accounts = PackedAccounts::default(); + let system_config = SystemAccountMetaConfig::new(*program_id); + remaining_accounts.add_system_accounts(system_config); + + // Get address tree info + let address_tree_pubkey = rpc.get_address_merkle_tree_v2(); + + let address = derive_address( + &user_record_pda.to_bytes(), + &address_tree_pubkey.to_bytes(), + &program_id.to_bytes(), + ); + + let compressed_account = rpc + .get_compressed_account(address, None) + .await + .unwrap() + .value; + let compressed_address = compressed_account.address.unwrap(); + + // Get validity proof from RPC + let rpc_result = rpc + .get_validity_proof(vec![compressed_account.hash], vec![], None) + .await + .unwrap() + .value; + + let output_state_tree_info = rpc.get_random_state_tree_info().unwrap(); + + let instruction = CompressibleInstruction::compress_account( + program_id, + anchor_compressible_derived::instruction::CompressUserRecord::DISCRIMINATOR, + &payer.pubkey(), + user_record_pda, + &RENT_RECIPIENT, // rent_recipient + &compressed_account, // compressed_account + rpc_result, // validity_proof_with_context + output_state_tree_info, // output_state_tree_info + ) + .unwrap(); + + if !should_fail { + let cu = simulate_cu(rpc, payer, &instruction).await; + println!("CompressRecord CU consumed: {}", cu); + } + + // Create and send transaction + let result = rpc + .create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await; + + if should_fail { + assert!(result.is_err(), "Compress transaction should fail"); + return result; + } else { + assert!(result.is_ok(), "Compress transaction should succeed"); + } + + // Verify the PDA account is now empty (compressed) + let user_pda_account = rpc.get_account(*user_record_pda).await.unwrap(); + assert!( + user_pda_account.is_some(), + "Account should exist after compression" + ); + let account = user_pda_account.unwrap(); + assert_eq!( + account.lamports, 0, + "Account lamports should be 0 after compression" + ); + assert!( + account.data.is_empty(), + "Account data should be empty after compression" + ); + + // Verify the compressed account exists + let compressed_user_record = rpc + .get_compressed_account(compressed_address, None) + .await + .unwrap() + .value; + + assert_eq!(compressed_user_record.address, Some(compressed_address)); + assert!(compressed_user_record.data.is_some()); + + let buf = compressed_user_record.data.unwrap().data; + let user_record: UserRecord = UserRecord::deserialize(&mut &buf[..]).unwrap(); + + assert_eq!(user_record.name, "Test User"); + assert_eq!(user_record.score, 11); + assert_eq!(user_record.owner, payer.pubkey()); + assert!(user_record.compression_info.is_none()); + Ok(result.unwrap()) +} + +async fn test_decompress_single_user_record( + rpc: &mut LightProgramTest, + payer: &Keypair, + program_id: &Pubkey, + user_record_pda: &Pubkey, + user_record_bump: &u8, + expected_user_name: &str, + expected_slot: u64, +) { + let address_tree_pubkey = rpc.get_address_merkle_tree_v2(); + + // Get compressed user record + let user_compressed_address = derive_address( + &user_record_pda.to_bytes(), + &address_tree_pubkey.to_bytes(), + &program_id.to_bytes(), + ); + let c_user_pda = rpc + .get_compressed_account(user_compressed_address, None) + .await + .unwrap() + .value; + + let user_account_data = c_user_pda.data.as_ref().unwrap(); + let c_user_record = UserRecord::deserialize(&mut &user_account_data.data[..]).unwrap(); + + // Get validity proof for the compressed account + let rpc_result = rpc + .get_validity_proof(vec![c_user_pda.hash], vec![], None) + .await + .unwrap() + .value; + + let output_state_tree_info = rpc.get_random_state_tree_info().unwrap(); + // Use the new SDK helper function with typed data + let instruction = + light_compressible_client::CompressibleInstruction::decompress_accounts_idempotent( + program_id, + &CompressibleInstruction::DECOMPRESS_ACCOUNTS_IDEMPOTENT_DISCRIMINATOR, + &payer.pubkey(), + &payer.pubkey(), // rent_payer can be the same as fee_payer + &[*user_record_pda], + &[( + c_user_pda, + CompressedAccountVariant::UserRecord(c_user_record), + vec![b"user_record".to_vec(), payer.pubkey().to_bytes().to_vec()], + )], + &[*user_record_bump], + rpc_result, + output_state_tree_info, + ) + .unwrap(); + + // Verify PDA is uninitialized before decompression + let user_pda_account = rpc.get_account(*user_record_pda).await.unwrap(); + assert_eq!( + user_pda_account.as_ref().map(|a| a.data.len()).unwrap_or(0), + 0, + "User PDA account data len must be 0 before decompression" + ); + + let result = rpc + .create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await; + assert!(result.is_ok(), "Decompress transaction should succeed"); + + // Verify UserRecord PDA is decompressed + let user_pda_account = rpc.get_account(*user_record_pda).await.unwrap(); + println!( + "user_pda_account after decompression: {:?}", + user_pda_account + ); + assert!( + user_pda_account.as_ref().map(|a| a.data.len()).unwrap_or(0) > 0, + "User PDA account data len must be > 0 after decompression" + ); + + let user_pda_data = user_pda_account.unwrap().data; + assert_eq!( + &user_pda_data[0..8], + UserRecord::DISCRIMINATOR, + "User account anchor discriminator mismatch" + ); + + let decompressed_user_record = UserRecord::try_deserialize(&mut &user_pda_data[..]).unwrap(); + assert_eq!(decompressed_user_record.name, expected_user_name); + assert_eq!(decompressed_user_record.score, 11); + assert_eq!(decompressed_user_record.owner, payer.pubkey()); + assert!(!decompressed_user_record + .compression_info + .as_ref() + .unwrap() + .is_compressed()); + assert_eq!( + decompressed_user_record + .compression_info + .as_ref() + .unwrap() + .last_written_slot(), + expected_slot + ); +} + +#[tokio::test] +async fn test_double_decompression_attack() { + let program_id = anchor_compressible_derived::ID; + let config = ProgramTestConfig::new_v2( + true, + Some(vec![("anchor_compressible_derived", program_id)]), + ); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + + let _program_data_pda = setup_mock_program_data(&mut rpc, &payer, &program_id); + + let result = initialize_compression_config( + &mut rpc, + &payer, + &program_id, + &payer, + 100, + RENT_RECIPIENT, + vec![ADDRESS_SPACE[0]], + &CompressibleInstruction::INITIALIZE_COMPRESSION_CONFIG_DISCRIMINATOR, + None, + ) + .await; + assert!(result.is_ok(), "Initialize config should succeed"); + + let (user_record_pda, user_record_bump) = + Pubkey::find_program_address(&[b"user_record", payer.pubkey().as_ref()], &program_id); + + // Create and compress the account + test_create_record(&mut rpc, &payer, &program_id, &user_record_pda, None).await; + let address_tree_pubkey = rpc.get_address_merkle_tree_v2(); + let user_compressed_address = derive_address( + &user_record_pda.to_bytes(), + &address_tree_pubkey.to_bytes(), + &program_id.to_bytes(), + ); + let compressed_user_record = rpc + .get_compressed_account(user_compressed_address, None) + .await + .unwrap() + .value; + let c_user_record = + UserRecord::deserialize(&mut &compressed_user_record.data.unwrap().data[..]).unwrap(); + + rpc.warp_to_slot(100).unwrap(); + + // First decompression - should succeed + test_decompress_single_user_record( + &mut rpc, + &payer, + &program_id, + &user_record_pda, + &user_record_bump, + "Test User", + 100, + ) + .await; + + // Verify account is now decompressed + let user_pda_account = rpc.get_account(user_record_pda).await.unwrap(); + assert!( + user_pda_account.as_ref().map(|a| a.data.len()).unwrap_or(0) > 0, + "User PDA should be decompressed after first operation" + ); + + // Second decompression attempt - should be idempotent (skip already initialized account) + + let c_user_pda = rpc + .get_compressed_account(user_compressed_address, None) + .await + .unwrap() + .value; + + let rpc_result = rpc + .get_validity_proof(vec![c_user_pda.hash], vec![], None) + .await + .unwrap() + .value; + + let output_state_tree_info = rpc.get_random_state_tree_info().unwrap(); + + // Second decompression instruction - should still work (idempotent) + let instruction = + light_compressible_client::CompressibleInstruction::decompress_accounts_idempotent( + &program_id, + &CompressibleInstruction::DECOMPRESS_ACCOUNTS_IDEMPOTENT_DISCRIMINATOR, + &payer.pubkey(), + &payer.pubkey(), + &[user_record_pda], + &[( + c_user_pda, + CompressedAccountVariant::UserRecord(c_user_record), + vec![b"user_record".to_vec(), payer.pubkey().to_bytes().to_vec()], + )], + &[user_record_bump], + rpc_result, + output_state_tree_info, + ) + .unwrap(); + + let result = rpc + .create_and_send_transaction(&[instruction], &payer.pubkey(), &[&payer]) + .await; + + // Should succeed due to idempotent behavior (skips already initialized accounts) + assert!( + result.is_ok(), + "Second decompression should succeed idempotently" + ); + + // Verify account state is still correct and not corrupted + let user_pda_account = rpc.get_account(user_record_pda).await.unwrap(); + let user_pda_data = user_pda_account.unwrap().data; + let decompressed_user_record = UserRecord::try_deserialize(&mut &user_pda_data[..]).unwrap(); + + assert_eq!(decompressed_user_record.name, "Test User"); + assert_eq!(decompressed_user_record.score, 11); + assert_eq!(decompressed_user_record.owner, payer.pubkey()); + assert!(!decompressed_user_record + .compression_info + .as_ref() + .unwrap() + .is_compressed()); +} + +#[tokio::test] +async fn test_create_and_decompress_accounts_with_different_state_trees() { + let program_id = anchor_compressible_derived::ID; + let config = ProgramTestConfig::new_v2( + true, + Some(vec![("anchor_compressible_derived", program_id)]), + ); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + + let config_pda = CompressibleConfig::derive_default_pda(&program_id).0; + let _program_data_pda = setup_mock_program_data(&mut rpc, &payer, &program_id); + + let result = initialize_compression_config( + &mut rpc, + &payer, + &program_id, + &payer, + 100, + RENT_RECIPIENT, + vec![ADDRESS_SPACE[0]], + &CompressibleInstruction::INITIALIZE_COMPRESSION_CONFIG_DISCRIMINATOR, + None, + ) + .await; + assert!(result.is_ok(), "Initialize config should succeed"); + + let (user_record_pda, user_record_bump) = + Pubkey::find_program_address(&[b"user_record", payer.pubkey().as_ref()], &program_id); + + let session_id = 54321u64; + let (game_session_pda, game_bump) = Pubkey::find_program_address( + &[b"game_session", session_id.to_le_bytes().as_ref()], + &program_id, + ); + + test_create_user_record_and_game_session( + &mut rpc, + &payer, + &program_id, + &config_pda, + &user_record_pda, + &game_session_pda, + session_id, + ) + .await; + + rpc.warp_to_slot(100).unwrap(); + println!("created game session!, now decompressing..."); + + // Now decompress both accounts together - they come from different state trees + // This should succeed and validate that our decompression can handle mixed state tree sources + test_decompress_multiple_pdas( + &mut rpc, + &payer, + &program_id, + &config_pda, + &user_record_pda, + &user_record_bump, + &game_session_pda, + &game_bump, + session_id, + "Combined User", + "Combined Game", + 100, + ) + .await; +} + +#[tokio::test] +async fn test_update_record_compression_info() { + let program_id = anchor_compressible_derived::ID; + let config = ProgramTestConfig::new_v2( + true, + Some(vec![("anchor_compressible_derived", program_id)]), + ); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + + let _program_data_pda = setup_mock_program_data(&mut rpc, &payer, &program_id); + + let result = initialize_compression_config( + &mut rpc, + &payer, + &program_id, + &payer, + 100, + RENT_RECIPIENT, + vec![ADDRESS_SPACE[0]], + &CompressibleInstruction::INITIALIZE_COMPRESSION_CONFIG_DISCRIMINATOR, + None, + ) + .await; + assert!(result.is_ok(), "Initialize config should succeed"); + + let (user_record_pda, user_record_bump) = + Pubkey::find_program_address(&[b"user_record", payer.pubkey().as_ref()], &program_id); + + // Create and compress the account + test_create_record(&mut rpc, &payer, &program_id, &user_record_pda, None).await; + + // Warp to slot 100 and decompress + rpc.warp_to_slot(100).unwrap(); + test_decompress_single_user_record( + &mut rpc, + &payer, + &program_id, + &user_record_pda, + &user_record_bump, + "Test User", + 100, + ) + .await; + + // Warp to slot 150 for the update + rpc.warp_to_slot(150).unwrap(); + + // Create update instruction + let accounts = anchor_compressible_derived::accounts::UpdateRecord { + user: payer.pubkey(), + user_record: user_record_pda, + }; + + let instruction_data = anchor_compressible_derived::instruction::UpdateRecord { + name: "Updated User".to_string(), + score: 42, + }; + + let instruction = Instruction { + program_id, + accounts: accounts.to_account_metas(None), + data: instruction_data.data(), + }; + + // Execute the update + let result = rpc + .create_and_send_transaction(&[instruction], &payer.pubkey(), &[&payer]) + .await; + assert!(result.is_ok(), "Update record transaction should succeed"); + + // Warp to slot 200 to ensure we're past the update + rpc.warp_to_slot(200).unwrap(); + + // Fetch the account and verify compression_info.last_written_slot + let user_pda_account = rpc.get_account(user_record_pda).await.unwrap(); + assert!( + user_pda_account.is_some(), + "User record account should exist after update" + ); + + let account_data = user_pda_account.unwrap().data; + let updated_user_record = UserRecord::try_deserialize(&mut &account_data[..]).unwrap(); + + // Verify the data was updated + assert_eq!(updated_user_record.name, "Updated User"); + assert_eq!(updated_user_record.score, 42); + assert_eq!(updated_user_record.owner, payer.pubkey()); + + // Verify compression_info.last_written_slot was updated to slot 150 + assert_eq!( + updated_user_record + .compression_info + .as_ref() + .unwrap() + .last_written_slot(), + 150 + ); + assert!(!updated_user_record + .compression_info + .as_ref() + .unwrap() + .is_compressed()); +} diff --git a/sdk-tests/anchor-compressible/CONFIG.md b/sdk-tests/anchor-compressible/CONFIG.md new file mode 100644 index 0000000000..387007e594 --- /dev/null +++ b/sdk-tests/anchor-compressible/CONFIG.md @@ -0,0 +1,94 @@ +# Compressible Config in anchor-compressible + +This program demonstrates how to use the Light SDK's compressible config system to manage compression parameters globally. + +## Overview + +The compressible config allows programs to: + +- Set global compression parameters (delay, rent recipient, address space) +- Ensure only authorized parties can modify these parameters +- Validate configuration at runtime + +## Instructions + +### 1. `initialize_compression_config` + +Creates the global config PDA. **Can only be called by the program's upgrade authority**. + +**Accounts:** + +- `payer`: Transaction fee payer +- `config`: Config PDA (derived with seed `"compressible_config"`) +- `program_data`: Program's data account (for upgrade authority validation) +- `authority`: Program's upgrade authority (must sign) +- `system_program`: System program + +**Parameters:** + +- `compression_delay`: Number of slots to wait before compression is allowed +- `rent_recipient`: Account that receives rent from compressed PDAs +- `address_space`: Address space for compressed accounts + +### 2. `update_compression_config` + +Updates the config. **Can only be called by the config's update authority**. + +**Accounts:** + +- `config`: Config PDA +- `authority`: Config's update authority (must sign) + +**Parameters (all optional):** + +- `new_compression_delay`: New compression delay +- `new_rent_recipient`: New rent recipient +- `new_address_space`: New address space +- `new_update_authority`: Transfer update authority to a new account + +### 3. `create_record` + +Creates a compressed user record using config values. + +**Additional Accounts:** + +- `config`: Config PDA +- `rent_recipient`: Must match the config's rent recipient + +### 4. `compress_record` + +Compresses a PDA using config values. + +**Additional Accounts:** + +- `config`: Config PDA +- `rent_recipient`: Must match the config's rent recipient + +The compression delay from the config is used to determine if enough time has passed since the last write. + +## Security Model + +1. **Config Creation**: Only the program's upgrade authority can create the initial config +2. **Config Updates**: Only the config's update authority can modify settings +3. **Rent Recipient Validation**: Instructions validate that the provided rent recipient matches the config +4. **Compression Delay**: Enforced based on config value + +## Deployment Process + +1. Deploy your program +2. **Immediately** call `initialize_compression_config` with the upgrade authority +3. Optionally transfer config update authority to a multisig or DAO +4. Monitor config changes + +## Example Usage + +See `examples/config_usage.rs` for complete examples. + +## Legacy Instructions + +The program still supports legacy instructions that use hardcoded values: + +- `create_record`: Uses hardcoded `ADDRESS_SPACE` and `RENT_RECIPIENT` +- `compress_record`: Uses hardcoded `COMPRESSION_DELAY` + +These are maintained for backward compatibility but new integrations should use the config-based versions. diff --git a/sdk-tests/anchor-compressible/Cargo.toml b/sdk-tests/anchor-compressible/Cargo.toml new file mode 100644 index 0000000000..b390196b7a --- /dev/null +++ b/sdk-tests/anchor-compressible/Cargo.toml @@ -0,0 +1,44 @@ +[package] +name = "anchor-compressible" +version = "0.1.0" +description = "Simple Anchor program template with user records" +edition = "2021" + +[lib] +crate-type = ["cdylib", "lib"] +name = "anchor_compressible" + +[features] +no-entrypoint = [] +no-idl = [] +no-log-ix-name = [] +cpi = ["no-entrypoint"] +default = ["idl-build"] +idl-build = ["anchor-lang/idl-build", "light-sdk/idl-build"] +test-sbf = [] + +[dependencies] +light-sdk = { workspace = true, features = ["anchor", "idl-build", "v2", "anchor-discriminator-compat"] } +light-sdk-types = { workspace = true, features = ["v2"] } +light-hasher = { workspace = true, features = ["solana"] } +solana-program = { workspace = true } +light-macros = { workspace = true, features = ["solana"] } +borsh = { workspace = true } +light-compressed-account = { workspace = true, features = ["solana"] } +anchor-lang = { workspace = true, features = ["idl-build"] } + +[dev-dependencies] +light-program-test = { workspace = true, features = ["devenv", "v2"] } +light-client = { workspace = true, features = ["devenv", "v2"] } +light-compressible-client = { workspace = true, features = ["anchor"] } +light-test-utils = { workspace = true, features = ["devenv"] } +tokio = { workspace = true } +solana-sdk = { workspace = true } +solana-logger = { workspace = true } + +[lints.rust.unexpected_cfgs] +level = "allow" +check-cfg = [ + 'cfg(target_os, values("solana"))', + 'cfg(feature, values("frozen-abi", "no-entrypoint"))', +] diff --git a/sdk-tests/anchor-compressible/Xargo.toml b/sdk-tests/anchor-compressible/Xargo.toml new file mode 100644 index 0000000000..9e7d95be7f --- /dev/null +++ b/sdk-tests/anchor-compressible/Xargo.toml @@ -0,0 +1,2 @@ +[target.bpfel-unknown-unknown.dependencies.std] +features = [] \ No newline at end of file diff --git a/sdk-tests/anchor-compressible/src/lib.rs b/sdk-tests/anchor-compressible/src/lib.rs new file mode 100644 index 0000000000..9e8fb0330c --- /dev/null +++ b/sdk-tests/anchor-compressible/src/lib.rs @@ -0,0 +1,772 @@ +use anchor_lang::{prelude::*, solana_program::pubkey::Pubkey}; +use light_sdk::{ + account::Size, + compressible::{ + compress_account, compress_account_on_init, prepare_accounts_for_compression_on_init, + prepare_accounts_for_decompress_idempotent, process_initialize_compression_config_checked, + process_update_compression_config, CompressibleConfig, CompressionInfo, HasCompressionInfo, + }, + cpi::{CpiAccounts, CpiInputs}, + derive_light_cpi_signer, + instruction::{account_meta::CompressedAccountMeta, PackedAddressTreeInfo, ValidityProof}, + light_hasher::{DataHasher, Hasher}, + sha::LightAccount, + LightDiscriminator, LightHasher, +}; +use light_sdk_types::CpiSigner; + +declare_id!("FAMipfVEhN4hjCLpKCvjDXXfzLsoVTqQccXzePz1L1ah"); +pub const LIGHT_CPI_SIGNER: CpiSigner = + derive_light_cpi_signer!("FAMipfVEhN4hjCLpKCvjDXXfzLsoVTqQccXzePz1L1ah"); + +// Simple anchor program retrofitted with compressible accounts. +#[program] +pub mod anchor_compressible { + + use super::*; + + pub fn create_record<'info>( + ctx: Context<'_, '_, '_, 'info, CreateRecord<'info>>, + name: String, + proof: ValidityProof, + compressed_address: [u8; 32], + address_tree_info: PackedAddressTreeInfo, + output_state_tree_index: u8, + ) -> Result<()> { + let user_record = &mut ctx.accounts.user_record; + + // 1. Load config from the config account + let config = CompressibleConfig::load_checked(&ctx.accounts.config, &crate::ID)?; + + user_record.owner = ctx.accounts.user.key(); + user_record.name = name; + user_record.score = 11; + + // 2. Verify rent recipient matches config + if ctx.accounts.rent_recipient.key() != config.rent_recipient { + return err!(ErrorCode::InvalidRentRecipient); + } + + // 3. Create CPI accounts + let cpi_accounts = + CpiAccounts::new(&ctx.accounts.user, ctx.remaining_accounts, LIGHT_CPI_SIGNER); + + let new_address_params = + address_tree_info.into_new_address_params_packed(user_record.key().to_bytes()); + + compress_account_on_init::( + user_record, + &compressed_address, + &new_address_params, + output_state_tree_index, + cpi_accounts, + &config.address_space, + &ctx.accounts.rent_recipient, + proof, + )?; + + Ok(()) + } + + pub fn update_record(ctx: Context, name: String, score: u64) -> Result<()> { + let user_record = &mut ctx.accounts.user_record; + + user_record.name = name; + user_record.score = score; + + // 1. Must manually set compression info + user_record.compression_info_mut().set_last_written_slot()?; + + Ok(()) + } + + // auto-derived via macro. + pub fn initialize_compression_config( + ctx: Context, + compression_delay: u32, + rent_recipient: Pubkey, + address_space: Vec, + ) -> Result<()> { + process_initialize_compression_config_checked( + &ctx.accounts.config.to_account_info(), + &ctx.accounts.authority.to_account_info(), + &ctx.accounts.program_data.to_account_info(), + &rent_recipient, + address_space, + compression_delay, + 0, // one global config for now, so bump is 0. + &ctx.accounts.payer.to_account_info(), + &ctx.accounts.system_program.to_account_info(), + &crate::ID, + )?; + + Ok(()) + } + + // auto-derived via macro. + pub fn update_compression_config( + ctx: Context, + new_compression_delay: Option, + new_rent_recipient: Option, + new_address_space: Option>, + new_update_authority: Option, + ) -> Result<()> { + process_update_compression_config( + &ctx.accounts.config.to_account_info(), + &ctx.accounts.authority.to_account_info(), + new_update_authority.as_ref(), + new_rent_recipient.as_ref(), + new_address_space, + new_compression_delay, + &crate::ID, + )?; + + Ok(()) + } + + // auto-derived via macro. takes the tagged account structs via + // add_compressible_accounts macro and derives the relevant variant type and + // dispatcher. The instruction can be used with any number of any of the + // tagged account structs. It's idempotent; it will not fail if the accounts + // are already decompressed. + pub fn decompress_accounts_idempotent<'info>( + ctx: Context<'_, '_, '_, 'info, DecompressAccountsIdempotent<'info>>, + proof: ValidityProof, + compressed_accounts: Vec, + bumps: Vec, + system_accounts_offset: u8, + ) -> Result<()> { + // Get PDA accounts from remaining accounts + let pda_accounts_end = system_accounts_offset as usize; + let solana_accounts = &ctx.remaining_accounts[..pda_accounts_end]; + + // Validate we have matching number of PDAs, compressed accounts, and bumps + if solana_accounts.len() != compressed_accounts.len() + || solana_accounts.len() != bumps.len() + { + return err!(ErrorCode::InvalidAccountCount); + } + + let cpi_accounts = CpiAccounts::new( + &ctx.accounts.fee_payer, + &ctx.remaining_accounts[system_accounts_offset as usize..], + LIGHT_CPI_SIGNER, + ); + + // Get address space from config checked. + let config = CompressibleConfig::load_checked(&ctx.accounts.config, &crate::ID)?; + let address_space = config.address_space[0]; + + let mut all_compressed_infos = Vec::with_capacity(compressed_accounts.len()); + + for (i, (compressed_data, &bump)) in compressed_accounts + .into_iter() + .zip(bumps.iter()) + .enumerate() + { + let bump_slice = [bump]; + + match compressed_data.data { + CompressedAccountVariant::UserRecord(data) => { + let mut seeds_refs = Vec::with_capacity(compressed_data.seeds.len() + 1); + for seed in &compressed_data.seeds { + seeds_refs.push(seed.as_slice()); + } + seeds_refs.push(&bump_slice); + + // Create sha::LightAccount with correct UserRecord discriminator + let light_account = LightAccount::<'_, UserRecord>::new_mut( + &crate::ID, + &compressed_data.meta, + data, + )?; + + // Process this single UserRecord account + let compressed_infos = prepare_accounts_for_decompress_idempotent::( + &[&solana_accounts[i]], + vec![light_account], + &[seeds_refs.as_slice()], + &cpi_accounts, + &ctx.accounts.rent_payer, + address_space, + )?; + + all_compressed_infos.extend(compressed_infos); + } + CompressedAccountVariant::GameSession(data) => { + // Build seeds refs without cloning - pre-allocate capacity + let mut seeds_refs = Vec::with_capacity(compressed_data.seeds.len() + 1); + for seed in &compressed_data.seeds { + seeds_refs.push(seed.as_slice()); + } + seeds_refs.push(&bump_slice); + + // Create sha::LightAccount with correct GameSession discriminator + let light_account = LightAccount::<'_, GameSession>::new_mut( + &crate::ID, + &compressed_data.meta, + data, + )?; + + // Process this single GameSession account + let compressed_infos = prepare_accounts_for_decompress_idempotent::( + &[&solana_accounts[i]], + vec![light_account], + &[seeds_refs.as_slice()], + &cpi_accounts, + &ctx.accounts.rent_payer, + address_space, + )?; + all_compressed_infos.extend(compressed_infos); + } + } + } + + if all_compressed_infos.is_empty() { + msg!("No compressed accounts to decompress"); + } else { + let cpi_inputs = CpiInputs::new(proof, all_compressed_infos); + cpi_inputs.invoke_light_system_program(cpi_accounts)?; + } + Ok(()) + } + + // Must be manually implemented. + pub fn create_game_session<'info>( + ctx: Context<'_, '_, '_, 'info, CreateGameSession<'info>>, + session_id: u64, + game_type: String, + proof: ValidityProof, + compressed_address: [u8; 32], + address_tree_info: PackedAddressTreeInfo, + output_state_tree_index: u8, + ) -> Result<()> { + let game_session = &mut ctx.accounts.game_session; + + // Load config from the config account + let config = CompressibleConfig::load_checked(&ctx.accounts.config, &crate::ID)?; + + // Set your account data. + game_session.session_id = session_id; + game_session.player = ctx.accounts.player.key(); + game_session.game_type = game_type; + game_session.start_time = Clock::get()?.unix_timestamp as u64; + game_session.end_time = None; + game_session.score = 0; + + // Check that rent recipient matches your config. + if ctx.accounts.rent_recipient.key() != config.rent_recipient { + return err!(ErrorCode::InvalidRentRecipient); + } + + // Create CPI accounts. + let cpi_accounts = CpiAccounts::new( + &ctx.accounts.player, + ctx.remaining_accounts, + LIGHT_CPI_SIGNER, + ); + + // Prepare new address params. The cpda takes the address of the + // compressible pda account as seed. + let new_address_params = + address_tree_info.into_new_address_params_packed(game_session.key().to_bytes()); + + // Call at the end of your init instruction to compress the pda account + // safely. This also closes the pda account. The account can then be + // decompressed by anyone at any time via the + // decompress_accounts_idempotent instruction. Creates a unique cPDA to + // ensure that the account cannot be re-inited only decompressed. + compress_account_on_init::( + game_session, + &compressed_address, + &new_address_params, + output_state_tree_index, + cpi_accounts, + &config.address_space, + &ctx.accounts.rent_recipient, + proof, + )?; + + Ok(()) + } + + // Must be manually implemented. + pub fn create_user_record_and_game_session<'info>( + ctx: Context<'_, '_, '_, 'info, CreateUserRecordAndGameSession<'info>>, + account_data: AccountCreationData, + compression_params: CompressionParams, + ) -> Result<()> { + let user_record = &mut ctx.accounts.user_record; + let game_session = &mut ctx.accounts.game_session; + + // Load your config checked. + let config = CompressibleConfig::load_checked(&ctx.accounts.config, &crate::ID)?; + + // Check that rent recipient matches your config. + if ctx.accounts.rent_recipient.key() != config.rent_recipient { + return err!(ErrorCode::InvalidRentRecipient); + } + + // Set your account data. + user_record.owner = ctx.accounts.user.key(); + user_record.name = account_data.user_name; + user_record.score = 11; + game_session.session_id = account_data.session_id; + game_session.player = ctx.accounts.user.key(); + game_session.game_type = account_data.game_type; + game_session.start_time = Clock::get()?.unix_timestamp as u64; + game_session.end_time = None; + game_session.score = 0; + + // Create CPI accounts. + let cpi_accounts = + CpiAccounts::new(&ctx.accounts.user, ctx.remaining_accounts, LIGHT_CPI_SIGNER); + + // Prepare new address params. One per pda account. + let user_new_address_params = compression_params + .user_address_tree_info + .into_new_address_params_packed(user_record.key().to_bytes()); + let game_new_address_params = compression_params + .game_address_tree_info + .into_new_address_params_packed(game_session.key().to_bytes()); + + let mut all_compressed_infos = Vec::new(); + + // Prepares the firstpda account for compression. compress the pda + // account safely. This also closes the pda account. safely. This also + // closes the pda account. The account can then be decompressed by + // anyone at any time via the decompress_accounts_idempotent + // instruction. Creates a unique cPDA to ensure that the account cannot + // be re-inited only decompressed. + let user_compressed_infos = prepare_accounts_for_compression_on_init::( + &mut [user_record], + &[compression_params.user_compressed_address], + &[user_new_address_params], + &[compression_params.user_output_state_tree_index], + &cpi_accounts, + &config.address_space, + &ctx.accounts.rent_recipient, + )?; + + all_compressed_infos.extend(user_compressed_infos); + + // Process GameSession for compression. compress the pda account safely. + // This also closes the pda account. The account can then be + // decompressed by anyone at any time via the + // decompress_accounts_idempotent instruction. Creates a unique cPDA to + // ensure that the account cannot be re-inited only decompressed. + let game_compressed_infos = prepare_accounts_for_compression_on_init::( + &mut [game_session], + &[compression_params.game_compressed_address], + &[game_new_address_params], + &[compression_params.game_output_state_tree_index], + &cpi_accounts, + &config.address_space, + &ctx.accounts.rent_recipient, + )?; + all_compressed_infos.extend(game_compressed_infos); + + // Create CPI inputs with all compressed accounts and new addresses + let cpi_inputs = CpiInputs::new_with_address( + compression_params.proof, + all_compressed_infos, + vec![user_new_address_params, game_new_address_params], + ); + + // Invoke light system program to create all compressed accounts in one + // CPI. Call at the end of your init instruction. + cpi_inputs.invoke_light_system_program(cpi_accounts)?; + + Ok(()) + } + + // Auto-derived via macro. Based on target account type, it will compress + // the pda account safely. This also closes the pda account. The account can + // then be decompressed by anyone at any time via the + // decompress_accounts_idempotent instruction. Does not create a new cPDA. + // but requires the existing (empty) compressed account to be passed in. + pub fn compress_record<'info>( + ctx: Context<'_, '_, '_, 'info, CompressRecord<'info>>, + proof: ValidityProof, + compressed_account_meta: CompressedAccountMeta, + ) -> Result<()> { + let user_record = &mut ctx.accounts.pda_to_compress; + + // Load config from the config account + let config = CompressibleConfig::load_checked(&ctx.accounts.config, &crate::ID)?; + + // Verify rent recipient matches config + if ctx.accounts.rent_recipient.key() != config.rent_recipient { + return err!(ErrorCode::InvalidRentRecipient); + } + + let cpi_accounts = + CpiAccounts::new(&ctx.accounts.user, ctx.remaining_accounts, LIGHT_CPI_SIGNER); + + compress_account::( + user_record, + &compressed_account_meta, + proof, + cpi_accounts, + &ctx.accounts.rent_recipient, + &config.compression_delay, + )?; + + Ok(()) + } +} + +#[derive(Accounts)] +pub struct CreateRecord<'info> { + #[account(mut)] + pub user: Signer<'info>, + #[account( + init, + payer = user, + // discriminator + owner + string len + name + score + + // option. Note that in the onchain space + // CompressionInfo is always Some. + space = 8 + 32 + 4 + 32 + 8 + 10, + seeds = [b"user_record", user.key().as_ref()], + bump, + )] + pub user_record: Account<'info, UserRecord>, + /// Needs to be here for the init anchor macro to work. + pub system_program: Program<'info, System>, + /// The global config account + /// CHECK: Config is validated by the SDK's load_checked method + pub config: AccountInfo<'info>, + /// Rent recipient - must match config + /// CHECK: Rent recipient is validated against the config + #[account(mut)] + pub rent_recipient: AccountInfo<'info>, +} + +#[derive(Accounts)] +#[instruction(account_data: AccountCreationData)] +pub struct CreateUserRecordAndGameSession<'info> { + #[account(mut)] + pub user: Signer<'info>, + #[account( + init, + payer = user, + // discriminator + owner + string len + name + score + + // option. Note that in the onchain space + // CompressionInfo is always Some. + space = 8 + 32 + 4 + 32 + 8 + 10, + seeds = [b"user_record", user.key().as_ref()], + bump, + )] + pub user_record: Account<'info, UserRecord>, + #[account( + init, + payer = user, + // discriminator + option + session_id + player + + // string len + game_type + start_time + end_time(Option) + score + space = 8 + 10 + 8 + 32 + 4 + 32 + 8 + 9 + 8, + seeds = [b"game_session", account_data.session_id.to_le_bytes().as_ref()], + bump, + )] + pub game_session: Account<'info, GameSession>, + /// Needs to be here for the init anchor macro to work. + pub system_program: Program<'info, System>, + /// The global config account + /// CHECK: Config is validated by the SDK's load_checked method + pub config: AccountInfo<'info>, + /// Rent recipient - must match config + /// CHECK: Rent recipient is validated against the config + #[account(mut)] + pub rent_recipient: AccountInfo<'info>, +} + +#[derive(Accounts)] +#[instruction(session_id: u64)] +pub struct CreateGameSession<'info> { + #[account(mut)] + pub player: Signer<'info>, + #[account( + init, + payer = player, + space = 8 + 9 + 8 + 32 + 4 + 32 + 8 + 9 + 8, // discriminator + compression_info + session_id + player + string len + game_type + start_time + end_time(Option) + score + seeds = [b"game_session", session_id.to_le_bytes().as_ref()], + bump, + )] + pub game_session: Account<'info, GameSession>, + pub system_program: Program<'info, System>, + /// The global config account + /// CHECK: Config is validated by the SDK's load_checked method + pub config: AccountInfo<'info>, + /// Rent recipient - must match config + /// CHECK: Rent recipient is validated against the config + #[account(mut)] + pub rent_recipient: AccountInfo<'info>, +} + +#[derive(Accounts)] +pub struct UpdateRecord<'info> { + #[account(mut)] + pub user: Signer<'info>, + #[account( + mut, + seeds = [b"user_record", user.key().as_ref()], + bump, + constraint = user_record.owner == user.key() + )] + pub user_record: Account<'info, UserRecord>, +} + +#[derive(Accounts)] +pub struct CompressRecord<'info> { + #[account(mut)] + pub user: Signer<'info>, + #[account( + mut, + seeds = [b"user_record", user.key().as_ref()], + bump, + constraint = pda_to_compress.owner == user.key() + )] + pub pda_to_compress: Account<'info, UserRecord>, + // pub system_program: Program<'info, System>, + /// The global config account + /// CHECK: Config is validated by the SDK's load_checked method + pub config: AccountInfo<'info>, + /// Rent recipient - must match config + /// CHECK: Rent recipient is validated against the config + #[account(mut)] + pub rent_recipient: AccountInfo<'info>, +} + +#[derive(Accounts)] +pub struct DecompressAccountsIdempotent<'info> { + #[account(mut)] + pub fee_payer: Signer<'info>, + /// UNCHECKED: Anyone can pay to init. + #[account(mut)] + pub rent_payer: Signer<'info>, + /// The global config account + /// CHECK: load_checked. + pub config: AccountInfo<'info>, + // Remaining accounts: + // - First N accounts: PDA accounts to decompress into + // - After system_accounts_offset: Light Protocol system accounts for CPI +} + +#[derive(Accounts)] +pub struct InitializeCompressionConfig<'info> { + #[account(mut)] + pub payer: Signer<'info>, + /// CHECK: Config PDA is created and validated by the SDK + #[account(mut)] + pub config: AccountInfo<'info>, + /// The program's data account + /// CHECK: Program data account is validated by the SDK + pub program_data: AccountInfo<'info>, + /// The program's upgrade authority (must sign) + pub authority: Signer<'info>, + pub system_program: Program<'info, System>, +} + +#[derive(Accounts)] +pub struct UpdateCompressionConfig<'info> { + /// CHECK: Config PDA is created and validated by the SDK + #[account(mut)] + pub config: AccountInfo<'info>, + /// Must match the update authority stored in config + pub authority: Signer<'info>, +} + +/// Auto-derived via macro. Unified enum that can hold any account type. Crucial +/// for dispatching multiple compressed accounts of different types in +/// decompress_accounts_idempotent. +/// Implements: Default, DataHasher, LightDiscriminator, HasCompressionInfo. +#[derive(Clone, Debug, AnchorSerialize, AnchorDeserialize)] +pub enum CompressedAccountVariant { + UserRecord(UserRecord), + GameSession(GameSession), +} + +impl Default for CompressedAccountVariant { + fn default() -> Self { + Self::UserRecord(UserRecord::default()) + } +} + +impl DataHasher for CompressedAccountVariant { + fn hash(&self) -> std::result::Result<[u8; 32], light_hasher::HasherError> { + match self { + Self::UserRecord(data) => data.hash::(), + Self::GameSession(data) => data.hash::(), + } + } +} + +impl LightDiscriminator for CompressedAccountVariant { + const LIGHT_DISCRIMINATOR: [u8; 8] = [0; 8]; // This won't be used directly + const LIGHT_DISCRIMINATOR_SLICE: &'static [u8] = &Self::LIGHT_DISCRIMINATOR; +} + +impl HasCompressionInfo for CompressedAccountVariant { + fn compression_info(&self) -> &CompressionInfo { + match self { + Self::UserRecord(data) => data.compression_info(), + Self::GameSession(data) => data.compression_info(), + } + } + + fn compression_info_mut(&mut self) -> &mut CompressionInfo { + match self { + Self::UserRecord(data) => data.compression_info_mut(), + Self::GameSession(data) => data.compression_info_mut(), + } + } + + fn compression_info_mut_opt(&mut self) -> &mut Option { + match self { + Self::UserRecord(data) => data.compression_info_mut_opt(), + Self::GameSession(data) => data.compression_info_mut_opt(), + } + } + + fn set_compression_info_none(&mut self) { + match self { + Self::UserRecord(data) => data.set_compression_info_none(), + Self::GameSession(data) => data.set_compression_info_none(), + } + } +} + +impl Size for CompressedAccountVariant { + fn size(&self) -> usize { + match self { + Self::UserRecord(data) => data.size(), + Self::GameSession(data) => data.size(), + } + } +} + +// Auto-derived via macro. Ix data implemented for Variant. +#[derive(Clone, Debug, AnchorDeserialize, AnchorSerialize)] +pub struct CompressedAccountData { + pub meta: CompressedAccountMeta, + pub data: CompressedAccountVariant, + pub seeds: Vec>, +} + +#[derive(Default, Debug, LightHasher, LightDiscriminator, InitSpace)] +#[account] +pub struct UserRecord { + #[skip] + pub compression_info: Option, + #[hash] + pub owner: Pubkey, + #[max_len(32)] + pub name: String, + pub score: u64, +} + +// Auto-derived via macro. +impl HasCompressionInfo for UserRecord { + fn compression_info(&self) -> &CompressionInfo { + self.compression_info + .as_ref() + .expect("CompressionInfo must be Some on-chain") + } + + fn compression_info_mut(&mut self) -> &mut CompressionInfo { + self.compression_info + .as_mut() + .expect("CompressionInfo must be Some on-chain") + } + + fn compression_info_mut_opt(&mut self) -> &mut Option { + &mut self.compression_info + } + + fn set_compression_info_none(&mut self) { + self.compression_info = None; + } +} + +impl Size for UserRecord { + fn size(&self) -> usize { + Self::LIGHT_DISCRIMINATOR.len() + Self::INIT_SPACE + } +} + +// Your existing account structs must be manually extended: +// 1. Add compression_info field to the struct, with type +// Option. +// 2. add a #[skip] field for the compression_info field. +// 3. Add LightHasher, LightDiscriminator. +// 4. Add #[hash] attribute to ALL fields that can be >31 bytes. (eg Pubkeys, +// Strings) +#[derive(Default, Debug, LightHasher, LightDiscriminator, InitSpace)] +#[account] +pub struct GameSession { + #[skip] + pub compression_info: Option, + pub session_id: u64, + #[hash] + pub player: Pubkey, + #[max_len(32)] + pub game_type: String, + pub start_time: u64, + pub end_time: Option, + pub score: u64, +} + +// Auto-derived via macro. +impl HasCompressionInfo for GameSession { + fn compression_info(&self) -> &CompressionInfo { + self.compression_info + .as_ref() + .expect("CompressionInfo must be Some on-chain") + } + + fn compression_info_mut(&mut self) -> &mut CompressionInfo { + self.compression_info + .as_mut() + .expect("CompressionInfo must be Some on-chain") + } + + fn compression_info_mut_opt(&mut self) -> &mut Option { + &mut self.compression_info + } + + fn set_compression_info_none(&mut self) { + self.compression_info = None; + } +} + +impl Size for GameSession { + fn size(&self) -> usize { + Self::LIGHT_DISCRIMINATOR.len() + Self::INIT_SPACE + } +} + +#[error_code] +pub enum ErrorCode { + #[msg("Invalid account count: PDAs and compressed accounts must match")] + InvalidAccountCount, + #[msg("Rent recipient does not match config")] + InvalidRentRecipient, +} + +// Add these struct definitions before the program module +#[derive(AnchorSerialize, AnchorDeserialize)] +pub struct AccountCreationData { + pub user_name: String, + pub session_id: u64, + pub game_type: String, +} + +#[derive(AnchorSerialize, AnchorDeserialize)] +pub struct CompressionParams { + pub proof: ValidityProof, + pub user_compressed_address: [u8; 32], + pub user_address_tree_info: PackedAddressTreeInfo, + pub user_output_state_tree_index: u8, + pub game_compressed_address: [u8; 32], + pub game_address_tree_info: PackedAddressTreeInfo, + pub game_output_state_tree_index: u8, +} diff --git a/sdk-tests/anchor-compressible/tests/test_config.rs b/sdk-tests/anchor-compressible/tests/test_config.rs new file mode 100644 index 0000000000..4a024557de --- /dev/null +++ b/sdk-tests/anchor-compressible/tests/test_config.rs @@ -0,0 +1,628 @@ +//! # Config Tests: anchor-compressible +//! +//! Checks covered: +//! - Successful config init +//! - Authority check (init/update) +//! - Config update by authority +//! - Prevent re-init +//! - Program data account check +//! - Prevent address space removal +//! - Update with non-authority +//! - Rent recipient check +#![cfg(feature = "test-sbf")] + +use anchor_lang::{InstructionData, ToAccountMetas}; +use light_compressible_client::CompressibleInstruction; +use light_macros::pubkey; +use light_program_test::{ + initialize_compression_config, + program_test::{create_mock_program_data, LightProgramTest, TestRpc}, + setup_mock_program_data, update_compression_config, ProgramTestConfig, Rpc, +}; +use light_sdk::compressible::CompressibleConfig; +use solana_sdk::{ + bpf_loader_upgradeable, + instruction::Instruction, + pubkey::Pubkey, + signature::{Keypair, Signer}, +}; + +pub const ADDRESS_SPACE: [Pubkey; 1] = [pubkey!("EzKE84aVTkCUhDHLELqyJaq1Y7UVVmqxXqZjVHwHY3rK")]; +pub const RENT_RECIPIENT: Pubkey = pubkey!("CLEuMG7pzJX9xAuKCFzBP154uiG1GaNo4Fq7x6KAcAfG"); + +#[tokio::test] +async fn test_initialize_compression_config() { + // Success: config can be initialized + let program_id = anchor_compressible::ID; + let config = ProgramTestConfig::new_v2(true, Some(vec![("anchor_compressible", program_id)])); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + let _program_data_pda = setup_mock_program_data(&mut rpc, &payer, &program_id); + + let result = initialize_compression_config( + &mut rpc, + &payer, + &program_id, + &payer, + 100, + RENT_RECIPIENT, + vec![ADDRESS_SPACE[0]], + &CompressibleInstruction::INITIALIZE_COMPRESSION_CONFIG_DISCRIMINATOR, + None, + ) + .await; + assert!(result.is_ok(), "Initialize config should succeed"); +} + +#[tokio::test] +async fn test_config_validation() { + // Fail: non-authority cannot init + let program_id = anchor_compressible::ID; + let config = ProgramTestConfig::new_v2(true, Some(vec![("anchor_compressible", program_id)])); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + let non_authority = Keypair::new(); + let _program_data_pda = setup_mock_program_data(&mut rpc, &payer, &program_id); + + rpc.airdrop_lamports(&non_authority.pubkey(), 1_000_000_000) + .await + .unwrap(); + let result = initialize_compression_config( + &mut rpc, + &payer, + &program_id, + &non_authority, + 100, + RENT_RECIPIENT, + vec![ADDRESS_SPACE[0]], + &CompressibleInstruction::INITIALIZE_COMPRESSION_CONFIG_DISCRIMINATOR, + None, + ) + .await; + assert!(result.is_err(), "Should fail with wrong authority"); +} + +#[tokio::test] +async fn test_config_multiple_address_spaces_validation() { + // Fail: cannot init with multiple address spaces + let program_id = anchor_compressible::ID; + let config = ProgramTestConfig::new_v2(true, Some(vec![("anchor_compressible", program_id)])); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + let _program_data_pda = setup_mock_program_data(&mut rpc, &payer, &program_id); + + // Try to init with multiple address spaces - should fail + let multiple_address_spaces = vec![ADDRESS_SPACE[0], Pubkey::new_unique()]; + let result = initialize_compression_config( + &mut rpc, + &payer, + &program_id, + &payer, + 100, + RENT_RECIPIENT, + multiple_address_spaces, + &CompressibleInstruction::INITIALIZE_COMPRESSION_CONFIG_DISCRIMINATOR, + None, + ) + .await; + assert!(result.is_err(), "Should fail with multiple address spaces"); + + // Try to init with empty address space - should also fail + let empty_address_space = vec![]; + let result = initialize_compression_config( + &mut rpc, + &payer, + &program_id, + &payer, + 100, + RENT_RECIPIENT, + empty_address_space, + &CompressibleInstruction::INITIALIZE_COMPRESSION_CONFIG_DISCRIMINATOR, + None, + ) + .await; + assert!(result.is_err(), "Should fail with empty address space"); +} + +#[tokio::test] +async fn test_update_compression_config() { + // Success: authority can update config + let program_id = anchor_compressible::ID; + let config = ProgramTestConfig::new_v2(true, Some(vec![("anchor_compressible", program_id)])); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + let (config_pda, _) = CompressibleConfig::derive_pda(&program_id, 0); + let _program_data_pda = setup_mock_program_data(&mut rpc, &payer, &program_id); + + let init_result = initialize_compression_config( + &mut rpc, + &payer, + &program_id, + &payer, + 100, + RENT_RECIPIENT, + ADDRESS_SPACE.to_vec(), + &CompressibleInstruction::INITIALIZE_COMPRESSION_CONFIG_DISCRIMINATOR, + None, + ) + .await; + assert!(init_result.is_ok(), "Init should succeed"); + let config_account = rpc.get_account(config_pda).await.unwrap(); + assert!(config_account.is_some(), "Config account should exist"); + + // Use the new mid-level helper - much cleaner! + let update_result = update_compression_config( + &mut rpc, + &payer, + &program_id, + &payer, + Some(200), + Some(RENT_RECIPIENT), + Some(vec![ADDRESS_SPACE[0]]), + None, + &CompressibleInstruction::UPDATE_COMPRESSION_CONFIG_DISCRIMINATOR, + ) + .await; + assert!(update_result.is_ok(), "Update config should succeed"); +} + +#[tokio::test] +async fn test_config_reinit_attack_prevention() { + // Fail: cannot re-init config + let program_id = anchor_compressible::ID; + let config = ProgramTestConfig::new_v2(true, Some(vec![("anchor_compressible", program_id)])); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + setup_mock_program_data(&mut rpc, &payer, &program_id); + let result = initialize_compression_config( + &mut rpc, + &payer, + &program_id, + &payer, + 100, + RENT_RECIPIENT, + vec![ADDRESS_SPACE[0]], + &CompressibleInstruction::INITIALIZE_COMPRESSION_CONFIG_DISCRIMINATOR, + None, + ) + .await; + assert!(result.is_ok(), "First init should succeed"); + let reinit_result = initialize_compression_config( + &mut rpc, + &payer, + &program_id, + &payer, + 100, + RENT_RECIPIENT, + vec![ADDRESS_SPACE[0]], + &CompressibleInstruction::INITIALIZE_COMPRESSION_CONFIG_DISCRIMINATOR, + None, + ) + .await; + assert!(reinit_result.is_err(), "Config reinit should fail"); +} + +#[tokio::test] +async fn test_wrong_program_data_account() { + // Fail: wrong program data account + let program_id = anchor_compressible::ID; + let config = ProgramTestConfig::new_v2(true, Some(vec![("anchor_compressible", program_id)])); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + let fake_program_data = Keypair::new(); + let mock_data = create_mock_program_data(payer.pubkey()); + let mock_account = solana_sdk::account::Account { + lamports: 1_000_000, + data: mock_data, + owner: bpf_loader_upgradeable::ID, + executable: false, + rent_epoch: 0, + }; + rpc.set_account(fake_program_data.pubkey(), mock_account); + let result = initialize_compression_config( + &mut rpc, + &payer, + &program_id, + &payer, + 100, + RENT_RECIPIENT, + vec![ADDRESS_SPACE[0]], + &CompressibleInstruction::INITIALIZE_COMPRESSION_CONFIG_DISCRIMINATOR, + None, + ) + .await; + + assert!( + result.is_err(), + "Should fail with wrong program data account" + ); +} + +#[tokio::test] +async fn test_update_remove_address_space() { + // Fail: cannot remove/replace address space + let program_id = anchor_compressible::ID; + let config = ProgramTestConfig::new_v2(true, Some(vec![("anchor_compressible", program_id)])); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + setup_mock_program_data(&mut rpc, &payer, &program_id); + let address_space_1 = vec![ADDRESS_SPACE[0]]; + let address_space_2 = vec![Pubkey::new_unique()]; + let init_result = initialize_compression_config( + &mut rpc, + &payer, + &program_id, + &payer, + 100, + RENT_RECIPIENT, + address_space_1, + &CompressibleInstruction::INITIALIZE_COMPRESSION_CONFIG_DISCRIMINATOR, + None, + ) + .await; + assert!(init_result.is_ok(), "Init should succeed"); + let update_result = update_compression_config( + &mut rpc, + &payer, + &program_id, + &payer, + None, + None, + Some(address_space_2), + None, + &CompressibleInstruction::UPDATE_COMPRESSION_CONFIG_DISCRIMINATOR, + ) + .await; + assert!( + update_result.is_err(), + "Should fail when trying to replace address space" + ); +} + +#[tokio::test] +async fn test_update_with_non_authority() { + // Fail: non-authority cannot update + let program_id = anchor_compressible::ID; + let config = ProgramTestConfig::new_v2(true, Some(vec![("anchor_compressible", program_id)])); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + let non_authority = Keypair::new(); + rpc.airdrop_lamports(&non_authority.pubkey(), 1_000_000_000) + .await + .unwrap(); + setup_mock_program_data(&mut rpc, &payer, &program_id); + let init_result = initialize_compression_config( + &mut rpc, + &payer, + &program_id, + &payer, + 100, + RENT_RECIPIENT, + vec![ADDRESS_SPACE[0]], + &CompressibleInstruction::INITIALIZE_COMPRESSION_CONFIG_DISCRIMINATOR, + None, + ) + .await; + assert!(init_result.is_ok(), "Init should succeed"); + + // Use the new mid-level helper to test non-authority update + let update_result = update_compression_config( + &mut rpc, + &payer, + &program_id, + &non_authority, // This should fail - non_authority tries to update + Some(200), + None, + None, + None, + &CompressibleInstruction::UPDATE_COMPRESSION_CONFIG_DISCRIMINATOR, + ) + .await; + assert!( + update_result.is_err(), + "Should fail with non-authority update" + ); +} + +#[tokio::test] +async fn test_config_with_wrong_rent_recipient() { + // Fail: wrong rent recipient + let program_id = anchor_compressible::ID; + let config = ProgramTestConfig::new_v2(true, Some(vec![("anchor_compressible", program_id)])); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + let (config_pda, _) = CompressibleConfig::derive_pda(&program_id, 0); + setup_mock_program_data(&mut rpc, &payer, &program_id); + let init_result = initialize_compression_config( + &mut rpc, + &payer, + &program_id, + &payer, + 100, + RENT_RECIPIENT, + vec![ADDRESS_SPACE[0]], + &CompressibleInstruction::INITIALIZE_COMPRESSION_CONFIG_DISCRIMINATOR, + None, + ) + .await; + assert!(init_result.is_ok(), "Init should succeed"); + let user = payer; + let (user_record_pda, _bump) = + Pubkey::find_program_address(&[b"user_record", user.pubkey().as_ref()], &program_id); + let wrong_rent_recipient = Pubkey::new_unique(); + let accounts = anchor_compressible::accounts::CreateRecord { + user: user.pubkey(), + user_record: user_record_pda, + system_program: solana_sdk::system_program::ID, + config: config_pda, + rent_recipient: wrong_rent_recipient, + }; + let instruction_data = anchor_compressible::instruction::CreateRecord { + name: "Test".to_string(), + proof: light_sdk::instruction::ValidityProof::default(), + compressed_address: [0u8; 32], + address_tree_info: light_sdk::instruction::PackedAddressTreeInfo::default(), + output_state_tree_index: 0, + }; + let instruction = Instruction { + program_id, + accounts: accounts.to_account_metas(None), + data: instruction_data.data(), + }; + let result = rpc + .create_and_send_transaction(&[instruction], &user.pubkey(), &[&user]) + .await; + assert!(result.is_err(), "Should fail with wrong rent recipient"); +} + +#[tokio::test] +async fn test_config_discriminator_attacks() { + let program_id = anchor_compressible::ID; + let config = ProgramTestConfig::new_v2(true, Some(vec![("anchor_compressible", program_id)])); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + let (config_pda, _) = CompressibleConfig::derive_pda(&program_id, 0); + + setup_mock_program_data(&mut rpc, &payer, &program_id); + + // First, create a valid config + let init_result = initialize_compression_config( + &mut rpc, + &payer, + &program_id, + &payer, + 100, + RENT_RECIPIENT, + vec![ADDRESS_SPACE[0]], + &CompressibleInstruction::INITIALIZE_COMPRESSION_CONFIG_DISCRIMINATOR, + None, + ) + .await; + assert!(init_result.is_ok(), "Init should succeed"); + + // Test 1: Corrupt the discriminator in config account + { + let config_account = rpc.get_account(config_pda).await.unwrap().unwrap(); + let mut corrupted_data = config_account.data.clone(); + + // Corrupt the discriminator (first 8 bytes) + corrupted_data[0] = 0xFF; + corrupted_data[1] = 0xFF; + corrupted_data[7] = 0xFF; + + let corrupted_account = solana_sdk::account::Account { + lamports: config_account.lamports, + data: corrupted_data, + owner: config_account.owner, + executable: config_account.executable, + rent_epoch: config_account.rent_epoch, + }; + + // Set the corrupted account + rpc.set_account(config_pda, corrupted_account); + + // Try to use config with create_record - should fail + let user = rpc.get_payer().insecure_clone(); + let (user_record_pda, _bump) = + Pubkey::find_program_address(&[b"user_record", user.pubkey().as_ref()], &program_id); + + let accounts = anchor_compressible::accounts::CreateRecord { + user: user.pubkey(), + user_record: user_record_pda, + system_program: solana_sdk::system_program::ID, + config: config_pda, + rent_recipient: RENT_RECIPIENT, + }; + + let instruction_data = anchor_compressible::instruction::CreateRecord { + name: "Test".to_string(), + proof: light_sdk::instruction::ValidityProof::default(), + compressed_address: [0u8; 32], + address_tree_info: light_sdk::instruction::PackedAddressTreeInfo::default(), + output_state_tree_index: 0, + }; + + let instruction = Instruction { + program_id, + accounts: accounts.to_account_metas(None), + data: instruction_data.data(), + }; + + let result = rpc + .create_and_send_transaction(&[instruction], &user.pubkey(), &[&user]) + .await; + + assert!(result.is_err(), "Should fail with corrupted discriminator"); + + // Restore the original config for next test + let original_config_account = solana_sdk::account::Account { + lamports: config_account.lamports, + data: config_account.data, + owner: config_account.owner, + executable: config_account.executable, + rent_epoch: config_account.rent_epoch, + }; + rpc.set_account(config_pda, original_config_account); + } + + // Test 2: Corrupt the version field + { + let config_account = rpc.get_account(config_pda).await.unwrap().unwrap(); + let mut corrupted_data = config_account.data.clone(); + + // Corrupt the version (byte 8 - after discriminator) + corrupted_data[8] = 99; // Invalid version + + let corrupted_account = solana_sdk::account::Account { + lamports: config_account.lamports, + data: corrupted_data, + owner: config_account.owner, + executable: config_account.executable, + rent_epoch: config_account.rent_epoch, + }; + + rpc.set_account(config_pda, corrupted_account); + + // Try to use config - should fail due to invalid version + let user = rpc.get_payer().insecure_clone(); + let (user_record_pda, _bump) = + Pubkey::find_program_address(&[b"user_record", user.pubkey().as_ref()], &program_id); + + let accounts = anchor_compressible::accounts::CreateRecord { + user: user.pubkey(), + user_record: user_record_pda, + system_program: solana_sdk::system_program::ID, + config: config_pda, + rent_recipient: RENT_RECIPIENT, + }; + + let instruction_data = anchor_compressible::instruction::CreateRecord { + name: "Test".to_string(), + proof: light_sdk::instruction::ValidityProof::default(), + compressed_address: [0u8; 32], + address_tree_info: light_sdk::instruction::PackedAddressTreeInfo::default(), + output_state_tree_index: 0, + }; + + let instruction = Instruction { + program_id, + accounts: accounts.to_account_metas(None), + data: instruction_data.data(), + }; + + let result = rpc + .create_and_send_transaction(&[instruction], &user.pubkey(), &[&user]) + .await; + + assert!(result.is_err(), "Should fail with invalid version"); + } + + // Test 3: Corrupt the address_space field (set length to 0) + { + let config_account = rpc.get_account(config_pda).await.unwrap().unwrap(); + let mut corrupted_data = config_account.data.clone(); + + // Find and corrupt address_space length (4 bytes after: discriminator + + // version + compression_delay + update_authority + rent_recipient) + // discriminator (8) + version (1) + compression_delay (4) + + // update_authority (32) + rent_recipient (32) = 77 bytes The + // address_space length is at byte 77 + let address_space_len_offset = 8 + 1 + 4 + 32 + 32; // 77 + corrupted_data[address_space_len_offset] = 0; // Set length to 0 + corrupted_data[address_space_len_offset + 1] = 0; + corrupted_data[address_space_len_offset + 2] = 0; + corrupted_data[address_space_len_offset + 3] = 0; + + let corrupted_account = solana_sdk::account::Account { + lamports: config_account.lamports, + data: corrupted_data, + owner: config_account.owner, + executable: config_account.executable, + rent_epoch: config_account.rent_epoch, + }; + + rpc.set_account(config_pda, corrupted_account); + + // Try to use config - should fail due to empty address_space + let user = rpc.get_payer().insecure_clone(); + let (user_record_pda, _bump) = + Pubkey::find_program_address(&[b"user_record", user.pubkey().as_ref()], &program_id); + + let accounts = anchor_compressible::accounts::CreateRecord { + user: user.pubkey(), + user_record: user_record_pda, + system_program: solana_sdk::system_program::ID, + config: config_pda, + rent_recipient: RENT_RECIPIENT, + }; + + let instruction_data = anchor_compressible::instruction::CreateRecord { + name: "Test".to_string(), + proof: light_sdk::instruction::ValidityProof::default(), + compressed_address: [0u8; 32], + address_tree_info: light_sdk::instruction::PackedAddressTreeInfo::default(), + output_state_tree_index: 0, + }; + + let instruction = Instruction { + program_id, + accounts: accounts.to_account_metas(None), + data: instruction_data.data(), + }; + + let result = rpc + .create_and_send_transaction(&[instruction], &user.pubkey(), &[&user]) + .await; + + assert!(result.is_err(), "Should fail with empty address_space"); + } + + // Test 4: Try to load config with wrong owner (should fail in load_checked) + { + let config_account = rpc.get_account(config_pda).await.unwrap().unwrap(); + let wrong_owner = Pubkey::new_unique(); + + let wrong_owner_account = solana_sdk::account::Account { + lamports: config_account.lamports, + data: config_account.data, + owner: wrong_owner, // Wrong owner + executable: config_account.executable, + rent_epoch: config_account.rent_epoch, + }; + + rpc.set_account(config_pda, wrong_owner_account); + + // Try to use config - should fail due to wrong owner + let user = rpc.get_payer().insecure_clone(); + let (user_record_pda, _bump) = + Pubkey::find_program_address(&[b"user_record", user.pubkey().as_ref()], &program_id); + + let accounts = anchor_compressible::accounts::CreateRecord { + user: user.pubkey(), + user_record: user_record_pda, + system_program: solana_sdk::system_program::ID, + config: config_pda, + rent_recipient: RENT_RECIPIENT, + }; + + let instruction_data = anchor_compressible::instruction::CreateRecord { + name: "Test".to_string(), + proof: light_sdk::instruction::ValidityProof::default(), + compressed_address: [0u8; 32], + address_tree_info: light_sdk::instruction::PackedAddressTreeInfo::default(), + output_state_tree_index: 0, + }; + + let instruction = Instruction { + program_id, + accounts: accounts.to_account_metas(None), + data: instruction_data.data(), + }; + + let result = rpc + .create_and_send_transaction(&[instruction], &user.pubkey(), &[&user]) + .await; + + assert!(result.is_err(), "Should fail with wrong owner"); + } +} diff --git a/sdk-tests/anchor-compressible/tests/test_decompress_multiple.rs b/sdk-tests/anchor-compressible/tests/test_decompress_multiple.rs new file mode 100644 index 0000000000..6ac0cdc0d7 --- /dev/null +++ b/sdk-tests/anchor-compressible/tests/test_decompress_multiple.rs @@ -0,0 +1,1324 @@ +#![cfg(feature = "test-sbf")] + +use anchor_compressible::{CompressedAccountVariant, GameSession, UserRecord}; +use anchor_lang::{ + AccountDeserialize, AnchorDeserialize, Discriminator, InstructionData, ToAccountMetas, +}; +use light_compressed_account::address::derive_address; +use light_compressible_client::CompressibleInstruction; +use light_macros::pubkey; +use light_program_test::{ + initialize_compression_config, + program_test::{LightProgramTest, TestRpc}, + setup_mock_program_data, + utils::simulation::simulate_cu, + AddressWithTree, Indexer, ProgramTestConfig, Rpc, RpcError, +}; +use light_sdk::{ + compressible::CompressibleConfig, + instruction::{PackedAccounts, SystemAccountMetaConfig}, +}; +use solana_sdk::{ + instruction::Instruction, + pubkey::Pubkey, + signature::{Keypair, Signer}, +}; + +pub const ADDRESS_SPACE: [Pubkey; 1] = [pubkey!("EzKE84aVTkCUhDHLELqyJaq1Y7UVVmqxXqZjVHwHY3rK")]; +pub const RENT_RECIPIENT: Pubkey = pubkey!("CLEuMG7pzJX9xAuKCFzBP154uiG1GaNo4Fq7x6KAcAfG"); + +#[tokio::test] +async fn test_create_and_decompress_two_accounts() { + let program_id = anchor_compressible::ID; + let config = ProgramTestConfig::new_v2(true, Some(vec![("anchor_compressible", program_id)])); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + + let config_pda = CompressibleConfig::derive_pda(&program_id, 0).0; + let _program_data_pda = setup_mock_program_data(&mut rpc, &payer, &program_id); + + let result = initialize_compression_config( + &mut rpc, + &payer, + &program_id, + &payer, + 100, + RENT_RECIPIENT, + vec![ADDRESS_SPACE[0]], + &CompressibleInstruction::INITIALIZE_COMPRESSION_CONFIG_DISCRIMINATOR, + None, + ) + .await; + assert!(result.is_ok(), "Initialize config should succeed"); + + let (user_record_pda, user_record_bump) = + Pubkey::find_program_address(&[b"user_record", payer.pubkey().as_ref()], &program_id); + + test_create_record(&mut rpc, &payer, &program_id, &user_record_pda, None).await; + + let session_id = 12345u64; + let (game_session_pda, game_bump) = Pubkey::find_program_address( + &[b"game_session", session_id.to_le_bytes().as_ref()], + &program_id, + ); + + test_create_game_session( + &mut rpc, + &payer, + &program_id, + &config_pda, + &game_session_pda, + session_id, + None, + ) + .await; + + rpc.warp_to_slot(100).unwrap(); + + test_decompress_multiple_pdas( + &mut rpc, + &payer, + &program_id, + &config_pda, + &user_record_pda, + &user_record_bump, + &game_session_pda, + &game_bump, + session_id, + "Test User", + "Battle Royale", + 100, + ) + .await; + + let combined_user = Keypair::new(); + let fund_user_ix = solana_sdk::system_instruction::transfer( + &payer.pubkey(), + &combined_user.pubkey(), + 1e9 as u64, + ); + let fund_result = rpc + .create_and_send_transaction(&[fund_user_ix], &payer.pubkey(), &[&payer]) + .await; + assert!(fund_result.is_ok(), "Funding combined user should succeed"); + let combined_session_id = 99999u64; + let (combined_user_record_pda, combined_user_record_bump) = Pubkey::find_program_address( + &[b"user_record", combined_user.pubkey().as_ref()], + &program_id, + ); + let (combined_game_session_pda, combined_game_bump) = Pubkey::find_program_address( + &[b"game_session", combined_session_id.to_le_bytes().as_ref()], + &program_id, + ); + + test_create_user_record_and_game_session( + &mut rpc, + &combined_user, + &program_id, + &config_pda, + &combined_user_record_pda, + &combined_game_session_pda, + combined_session_id, + ) + .await; + + rpc.warp_to_slot(200).unwrap(); + + test_decompress_multiple_pdas( + &mut rpc, + &combined_user, + &program_id, + &config_pda, + &combined_user_record_pda, + &combined_user_record_bump, + &combined_game_session_pda, + &combined_game_bump, + combined_session_id, + "Combined User", + "Combined Game", + 200, + ) + .await; +} + +#[tokio::test] +async fn test_create_decompress_compress_single_account() { + let program_id = anchor_compressible::ID; + let config = ProgramTestConfig::new_v2(true, Some(vec![("anchor_compressible", program_id)])); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + let _program_data_pda = setup_mock_program_data(&mut rpc, &payer, &program_id); + + let result = initialize_compression_config( + &mut rpc, + &payer, + &program_id, + &payer, + 100, + RENT_RECIPIENT, + vec![ADDRESS_SPACE[0]], + &CompressibleInstruction::INITIALIZE_COMPRESSION_CONFIG_DISCRIMINATOR, + None, + ) + .await; + assert!(result.is_ok(), "Initialize config should succeed"); + + let (user_record_pda, user_record_bump) = + Pubkey::find_program_address(&[b"user_record", payer.pubkey().as_ref()], &program_id); + + test_create_record(&mut rpc, &payer, &program_id, &user_record_pda, None).await; + + rpc.warp_to_slot(100).unwrap(); + + println!("decompress single"); + test_decompress_single_user_record( + &mut rpc, + &payer, + &program_id, + &user_record_pda, + &user_record_bump, + "Test User", + 100, + ) + .await; + + rpc.warp_to_slot(101).unwrap(); + + println!("compress record"); + + let result = test_compress_record(&mut rpc, &payer, &program_id, &user_record_pda, true).await; + assert!(result.is_err(), "Compression should fail due to slot delay"); + if let Err(err) = result { + let err_msg = format!("{:?}", err); + assert!( + err_msg.contains("Custom(16001)"), + "Expected error message about slot delay, got: {}", + err_msg + ); + } + rpc.warp_to_slot(200).unwrap(); + let _result = + test_compress_record(&mut rpc, &payer, &program_id, &user_record_pda, false).await; +} + +async fn test_create_record( + rpc: &mut LightProgramTest, + payer: &Keypair, + program_id: &Pubkey, + user_record_pda: &Pubkey, + state_tree_queue: Option, +) { + let config_pda = CompressibleConfig::derive_pda(program_id, 0).0; + // Setup remaining accounts for Light Protocol + let mut remaining_accounts = PackedAccounts::default(); + let system_config = SystemAccountMetaConfig::new(*program_id); + remaining_accounts.add_system_accounts(system_config); + + // Get address tree info + let address_tree_pubkey = rpc.get_address_merkle_tree_v2(); + + // Create the instruction + let accounts = anchor_compressible::accounts::CreateRecord { + user: payer.pubkey(), + user_record: *user_record_pda, + system_program: solana_sdk::system_program::ID, + config: config_pda, + rent_recipient: RENT_RECIPIENT, + }; + + // Derive a new address for the compressed account + let compressed_address = derive_address( + &user_record_pda.to_bytes(), + &address_tree_pubkey.to_bytes(), + &program_id.to_bytes(), + ); + + // Get validity proof from RPC + let rpc_result = rpc + .get_validity_proof( + vec![], + vec![AddressWithTree { + address: compressed_address, + tree: address_tree_pubkey, + }], + None, + ) + .await + .unwrap() + .value; + + // Pack tree infos into remaining accounts + let packed_tree_infos = rpc_result.pack_tree_infos(&mut remaining_accounts); + + // Get the packed address tree info + let address_tree_info = packed_tree_infos.address_trees[0]; + + // Get output state tree index + let output_state_tree_index = remaining_accounts.insert_or_get( + state_tree_queue.unwrap_or_else(|| rpc.get_random_state_tree_info().unwrap().queue), + ); + + // Get system accounts for the instruction + let (system_accounts, _, _) = remaining_accounts.to_account_metas(); + + // Create instruction data + let instruction_data = anchor_compressible::instruction::CreateRecord { + name: "Test User".to_string(), + proof: rpc_result.proof, + compressed_address, + address_tree_info, + output_state_tree_index, + }; + + // Build the instruction + let instruction = Instruction { + program_id: *program_id, + accounts: [accounts.to_account_metas(None), system_accounts].concat(), + data: instruction_data.data(), + }; + + let cu = simulate_cu(rpc, payer, &instruction).await; + println!("CreateRecord CU consumed: {}", cu); + + // Create and send transaction + let result = rpc + .create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await; + + assert!(result.is_ok(), "Transaction should succeed"); + + // should be empty + let user_record_account = rpc.get_account(*user_record_pda).await.unwrap(); + assert!( + user_record_account.is_some(), + "Account should exist after compression" + ); + + let account = user_record_account.unwrap(); + assert_eq!(account.lamports, 0, "Account lamports should be 0"); + + let user_record_data = account.data; + + assert!(user_record_data.is_empty(), "Account data should be empty"); +} + +async fn test_create_game_session( + rpc: &mut LightProgramTest, + payer: &Keypair, + program_id: &Pubkey, + config_pda: &Pubkey, + game_session_pda: &Pubkey, + session_id: u64, + state_tree_queue: Option, +) { + // Setup remaining accounts for Light Protocol + let mut remaining_accounts = PackedAccounts::default(); + let system_config = SystemAccountMetaConfig::new(*program_id); + remaining_accounts.add_system_accounts(system_config); + + // Get address tree info + let address_tree_pubkey = rpc.get_address_merkle_tree_v2(); + + // Create the instruction + let accounts = anchor_compressible::accounts::CreateGameSession { + player: payer.pubkey(), + game_session: *game_session_pda, + system_program: solana_sdk::system_program::ID, + config: *config_pda, + rent_recipient: RENT_RECIPIENT, + }; + + // Derive a new address for the compressed account + let compressed_address = derive_address( + &game_session_pda.to_bytes(), + &address_tree_pubkey.to_bytes(), + &program_id.to_bytes(), + ); + + // Get validity proof from RPC + let rpc_result = rpc + .get_validity_proof( + vec![], + vec![AddressWithTree { + address: compressed_address, + tree: address_tree_pubkey, + }], + None, + ) + .await + .unwrap() + .value; + + // Pack tree infos into remaining accounts + let packed_tree_infos = rpc_result.pack_tree_infos(&mut remaining_accounts); + + // Get the packed address tree info + let address_tree_info = packed_tree_infos.address_trees[0]; + + // Get output state tree index + let output_state_tree_index = remaining_accounts.insert_or_get( + state_tree_queue.unwrap_or_else(|| rpc.get_random_state_tree_info().unwrap().queue), + ); + + // Get system accounts for the instruction + let (system_accounts, _, _) = remaining_accounts.to_account_metas(); + + // Create instruction data + let instruction_data = anchor_compressible::instruction::CreateGameSession { + session_id, + game_type: "Battle Royale".to_string(), + proof: rpc_result.proof, + compressed_address, + address_tree_info, + output_state_tree_index, + }; + + // Build the instruction + let instruction = Instruction { + program_id: *program_id, + accounts: [accounts.to_account_metas(None), system_accounts].concat(), + data: instruction_data.data(), + }; + + // Create and send transaction + let result = rpc + .create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await; + + assert!(result.is_ok(), "Transaction should succeed"); + + // Verify the account is empty after compression + let game_session_account = rpc.get_account(*game_session_pda).await.unwrap(); + assert!( + game_session_account.is_some(), + "Account should exist after compression" + ); + + let account = game_session_account.unwrap(); + assert_eq!(account.lamports, 0, "Account lamports should be 0"); + assert!(account.data.is_empty(), "Account data should be empty"); + + let compressed_game_session = rpc + .get_compressed_account(compressed_address, None) + .await + .unwrap() + .value; + + assert_eq!(compressed_game_session.address, Some(compressed_address)); + assert!(compressed_game_session.data.is_some()); + + let buf = compressed_game_session.data.unwrap().data; + + let game_session = GameSession::deserialize(&mut &buf[..]).unwrap(); + + println!("COMPRESSED game_session: {:?}", game_session); + assert_eq!(game_session.session_id, session_id); + assert_eq!(game_session.game_type, "Battle Royale"); + assert_eq!(game_session.player, payer.pubkey()); + assert_eq!(game_session.score, 0); + assert!(game_session.compression_info.is_none()); +} + +#[allow(clippy::too_many_arguments)] +async fn test_decompress_multiple_pdas( + rpc: &mut LightProgramTest, + payer: &Keypair, + program_id: &Pubkey, + _config_pda: &Pubkey, + user_record_pda: &Pubkey, + user_record_bump: &u8, + game_session_pda: &Pubkey, + game_bump: &u8, + session_id: u64, + expected_user_name: &str, + expected_game_type: &str, + expected_slot: u64, +) { + let address_tree_pubkey = rpc.get_address_merkle_tree_v2(); + + // c pda USER_RECORD + let user_compressed_address = derive_address( + &user_record_pda.to_bytes(), + &address_tree_pubkey.to_bytes(), + &program_id.to_bytes(), + ); + let c_user_pda = rpc + .get_compressed_account(user_compressed_address, None) + .await + .unwrap() + .value; + + let user_account_data = c_user_pda.data.as_ref().unwrap(); + + let c_user_record = UserRecord::deserialize(&mut &user_account_data.data[..]).unwrap(); + + // c pda GAME_SESSION + let game_compressed_address = derive_address( + &game_session_pda.to_bytes(), + &address_tree_pubkey.to_bytes(), + &program_id.to_bytes(), + ); + let c_game_pda = rpc + .get_compressed_account(game_compressed_address, None) + .await + .unwrap() + .value; + let game_account_data = c_game_pda.data.as_ref().unwrap(); + + let c_game_session = GameSession::deserialize(&mut &game_account_data.data[..]).unwrap(); + + // Get validity proof for both compressed accounts + let rpc_result = rpc + .get_validity_proof(vec![c_user_pda.hash, c_game_pda.hash], vec![], None) + .await + .unwrap() + .value; + + let output_state_tree_info = rpc.get_random_state_tree_info().unwrap(); + + // Use the new SDK helper function with typed data + let instruction = + light_compressible_client::CompressibleInstruction::decompress_accounts_idempotent( + program_id, + &CompressibleInstruction::DECOMPRESS_ACCOUNTS_IDEMPOTENT_DISCRIMINATOR, + &payer.pubkey(), + &payer.pubkey(), // rent_payer can be the same as fee_payer + &[*user_record_pda, *game_session_pda], + &[ + ( + c_user_pda, + CompressedAccountVariant::UserRecord(c_user_record), + vec![b"user_record".to_vec(), payer.pubkey().to_bytes().to_vec()], + ), + ( + c_game_pda, + CompressedAccountVariant::GameSession(c_game_session), + vec![b"game_session".to_vec(), session_id.to_le_bytes().to_vec()], + ), + ], + &[*user_record_bump, *game_bump], + rpc_result, + output_state_tree_info, + ) + .unwrap(); + + let cu = simulate_cu(rpc, payer, &instruction).await; + println!("decompress_multiple_pdas CU consumed: {}", cu); + + // Verify PDAs are uninitialized before decompression + let user_pda_account = rpc.get_account(*user_record_pda).await.unwrap(); + assert_eq!( + user_pda_account.as_ref().map(|a| a.data.len()).unwrap_or(0), + 0, + "User PDA account data len must be 0 before decompression" + ); + + let game_pda_account = rpc.get_account(*game_session_pda).await.unwrap(); + assert_eq!( + game_pda_account.as_ref().map(|a| a.data.len()).unwrap_or(0), + 0, + "Game PDA account data len must be 0 before decompression" + ); + + let cu = simulate_cu(rpc, payer, &instruction).await; + println!("decompress_multiple_pdas CU consumed: {}", cu); + + let result = rpc + .create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await; + assert!(result.is_ok(), "Decompress transaction should succeed"); + + // Verify UserRecord PDA is decompressed + let user_pda_account = rpc.get_account(*user_record_pda).await.unwrap(); + println!( + "user_pda_account after decompression: {:?}", + user_pda_account + ); + assert!( + user_pda_account.as_ref().map(|a| a.data.len()).unwrap_or(0) > 0, + "User PDA account data len must be > 0 after decompression" + ); + + let user_pda_data = user_pda_account.unwrap().data; + assert_eq!( + &user_pda_data[0..8], + UserRecord::DISCRIMINATOR, + "User account anchor discriminator mismatch" + ); + + let decompressed_user_record = UserRecord::try_deserialize(&mut &user_pda_data[..]).unwrap(); + assert_eq!(decompressed_user_record.name, expected_user_name); + assert_eq!(decompressed_user_record.score, 11); + assert_eq!(decompressed_user_record.owner, payer.pubkey()); + assert!(!decompressed_user_record + .compression_info + .as_ref() + .unwrap() + .is_compressed()); + assert_eq!( + decompressed_user_record + .compression_info + .as_ref() + .unwrap() + .last_written_slot(), + expected_slot + ); + + // Verify GameSession PDA is decompressed + let game_pda_account = rpc.get_account(*game_session_pda).await.unwrap(); + assert!( + game_pda_account.as_ref().map(|a| a.data.len()).unwrap_or(0) > 0, + "Game PDA account data len must be > 0 after decompression" + ); + + let game_pda_data = game_pda_account.unwrap().data; + assert_eq!( + &game_pda_data[0..8], + anchor_compressible::GameSession::DISCRIMINATOR, + "Game account anchor discriminator mismatch" + ); + + let decompressed_game_session = + anchor_compressible::GameSession::try_deserialize(&mut &game_pda_data[..]).unwrap(); + assert_eq!(decompressed_game_session.session_id, session_id); + assert_eq!(decompressed_game_session.game_type, expected_game_type); + assert_eq!(decompressed_game_session.player, payer.pubkey()); + assert_eq!(decompressed_game_session.score, 0); + assert!(!decompressed_game_session + .compression_info + .as_ref() + .unwrap() + .is_compressed()); + assert_eq!( + decompressed_game_session + .compression_info + .as_ref() + .unwrap() + .last_written_slot(), + expected_slot + ); + + // Verify compressed accounts exist and have correct data + let c_game_pda = rpc + .get_compressed_account(game_compressed_address, None) + .await + .unwrap() + .value; + + assert!(c_game_pda.data.is_some()); + assert_eq!(c_game_pda.data.unwrap().data.len(), 0); +} + +async fn test_create_user_record_and_game_session( + rpc: &mut LightProgramTest, + user: &Keypair, + program_id: &Pubkey, + config_pda: &Pubkey, + user_record_pda: &Pubkey, + game_session_pda: &Pubkey, + session_id: u64, +) { + // Setup remaining accounts for Light Protocol + let mut remaining_accounts = PackedAccounts::default(); + let system_config = SystemAccountMetaConfig::new(*program_id); + remaining_accounts.add_system_accounts(system_config); + + // Get address tree info + let address_tree_pubkey = rpc.get_address_merkle_tree_v2(); + + // Create the instruction + let accounts = anchor_compressible::accounts::CreateUserRecordAndGameSession { + user: user.pubkey(), + user_record: *user_record_pda, + game_session: *game_session_pda, + system_program: solana_sdk::system_program::ID, + config: *config_pda, + rent_recipient: RENT_RECIPIENT, + }; + + // Derive addresses for both compressed accounts + let user_compressed_address = derive_address( + &user_record_pda.to_bytes(), + &address_tree_pubkey.to_bytes(), + &program_id.to_bytes(), + ); + let game_compressed_address = derive_address( + &game_session_pda.to_bytes(), + &address_tree_pubkey.to_bytes(), + &program_id.to_bytes(), + ); + + // Get validity proof from RPC + let rpc_result = rpc + .get_validity_proof( + vec![], + vec![ + AddressWithTree { + address: user_compressed_address, + tree: address_tree_pubkey, + }, + AddressWithTree { + address: game_compressed_address, + tree: address_tree_pubkey, + }, + ], + None, + ) + .await + .unwrap() + .value; + + // Pack tree infos into remaining accounts + let packed_tree_infos = rpc_result.pack_tree_infos(&mut remaining_accounts); + + // Get the packed address tree info (both should use the same tree) + let user_address_tree_info = packed_tree_infos.address_trees[0]; + let game_address_tree_info = packed_tree_infos.address_trees[1]; + + // Get output state tree indices + let user_output_state_tree_index = + remaining_accounts.insert_or_get(rpc.get_random_state_tree_info().unwrap().queue); + let game_output_state_tree_index = + remaining_accounts.insert_or_get(rpc.get_random_state_tree_info().unwrap().queue); + + // Get system accounts for the instruction + let (system_accounts, _, _) = remaining_accounts.to_account_metas(); + + // Create instruction data + let instruction_data = anchor_compressible::instruction::CreateUserRecordAndGameSession { + account_data: anchor_compressible::AccountCreationData { + user_name: "Combined User".to_string(), + session_id, + game_type: "Combined Game".to_string(), + }, + compression_params: anchor_compressible::CompressionParams { + proof: rpc_result.proof, + user_compressed_address, + user_address_tree_info, + user_output_state_tree_index, + game_compressed_address, + game_address_tree_info, + game_output_state_tree_index, + }, + }; + + // Build the instruction + let instruction = Instruction { + program_id: *program_id, + accounts: [accounts.to_account_metas(None), system_accounts].concat(), + data: instruction_data.data(), + }; + let cu = simulate_cu(rpc, user, &instruction).await; + println!("CreateUserRecordAndGameSession CU consumed: {}", cu); + // Create and send transaction + let result = rpc + .create_and_send_transaction(&[instruction], &user.pubkey(), &[user]) + .await; + + assert!( + result.is_ok(), + "Combined creation transaction should succeed" + ); + + // Verify both accounts are empty after compression + let user_record_account = rpc.get_account(*user_record_pda).await.unwrap(); + assert!( + user_record_account.is_some(), + "User record account should exist after compression" + ); + let account = user_record_account.unwrap(); + assert_eq!( + account.lamports, 0, + "User record account lamports should be 0" + ); + assert!( + account.data.is_empty(), + "User record account data should be empty" + ); + + let game_session_account = rpc.get_account(*game_session_pda).await.unwrap(); + assert!( + game_session_account.is_some(), + "Game session account should exist after compression" + ); + let account = game_session_account.unwrap(); + assert_eq!( + account.lamports, 0, + "Game session account lamports should be 0" + ); + assert!( + account.data.is_empty(), + "Game session account data should be empty" + ); + + // Verify compressed accounts exist and have correct data + let compressed_user_record = rpc + .get_compressed_account(user_compressed_address, None) + .await + .unwrap() + .value; + + assert_eq!( + compressed_user_record.address, + Some(user_compressed_address) + ); + assert!(compressed_user_record.data.is_some()); + + let user_buf = compressed_user_record.data.unwrap().data; + + let user_record = UserRecord::deserialize(&mut &user_buf[..]).unwrap(); + + assert_eq!(user_record.name, "Combined User"); + assert_eq!(user_record.score, 11); + assert_eq!(user_record.owner, user.pubkey()); + + let compressed_game_session = rpc + .get_compressed_account(game_compressed_address, None) + .await + .unwrap() + .value; + + assert_eq!( + compressed_game_session.address, + Some(game_compressed_address) + ); + assert!(compressed_game_session.data.is_some()); + + let game_buf = compressed_game_session.data.unwrap().data; + let game_session = GameSession::deserialize(&mut &game_buf[..]).unwrap(); + assert_eq!(game_session.session_id, session_id); + assert_eq!(game_session.game_type, "Combined Game"); + assert_eq!(game_session.player, user.pubkey()); + assert_eq!(game_session.score, 0); +} + +async fn test_compress_record( + rpc: &mut LightProgramTest, + payer: &Keypair, + program_id: &Pubkey, + user_record_pda: &Pubkey, + should_fail: bool, +) -> Result { + // Get the current decompressed user record data + let user_pda_account = rpc.get_account(*user_record_pda).await.unwrap(); + assert!( + user_pda_account.is_some(), + "User PDA account should exist before compression" + ); + let account = user_pda_account.unwrap(); + assert!( + account.lamports > 0, + "Account should have lamports before compression" + ); + assert!( + !account.data.is_empty(), + "Account data should not be empty before compression" + ); + + // Setup remaining accounts for Light Protocol + let mut remaining_accounts = PackedAccounts::default(); + let system_config = SystemAccountMetaConfig::new(*program_id); + remaining_accounts.add_system_accounts(system_config); + + // Get address tree info + let address_tree_pubkey = rpc.get_address_merkle_tree_v2(); + + let address = derive_address( + &user_record_pda.to_bytes(), + &address_tree_pubkey.to_bytes(), + &program_id.to_bytes(), + ); + + let compressed_account = rpc + .get_compressed_account(address, None) + .await + .unwrap() + .value; + let compressed_address = compressed_account.address.unwrap(); + + // Get validity proof from RPC + let rpc_result = rpc + .get_validity_proof(vec![compressed_account.hash], vec![], None) + .await + .unwrap() + .value; + + let output_state_tree_info = rpc.get_random_state_tree_info().unwrap(); + + let instruction = CompressibleInstruction::compress_account( + program_id, + anchor_compressible::instruction::CompressRecord::DISCRIMINATOR, + &payer.pubkey(), + user_record_pda, + &RENT_RECIPIENT, // rent_recipient + &compressed_account, // compressed_account + rpc_result, // validity_proof_with_context + output_state_tree_info, // output_state_tree_info + ) + .unwrap(); + + if !should_fail { + let cu = simulate_cu(rpc, payer, &instruction).await; + println!("CompressRecord CU consumed: {}", cu); + } + + // Create and send transaction + let result = rpc + .create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await; + + if should_fail { + assert!(result.is_err(), "Compress transaction should fail"); + return result; + } else { + assert!(result.is_ok(), "Compress transaction should succeed"); + } + + // Verify the PDA account is now empty (compressed) + let user_pda_account = rpc.get_account(*user_record_pda).await.unwrap(); + assert!( + user_pda_account.is_some(), + "Account should exist after compression" + ); + let account = user_pda_account.unwrap(); + assert_eq!( + account.lamports, 0, + "Account lamports should be 0 after compression" + ); + assert!( + account.data.is_empty(), + "Account data should be empty after compression" + ); + + // Verify the compressed account exists + let compressed_user_record = rpc + .get_compressed_account(compressed_address, None) + .await + .unwrap() + .value; + + assert_eq!(compressed_user_record.address, Some(compressed_address)); + assert!(compressed_user_record.data.is_some()); + + let buf = compressed_user_record.data.unwrap().data; + let user_record: UserRecord = UserRecord::deserialize(&mut &buf[..]).unwrap(); + + assert_eq!(user_record.name, "Test User"); + assert_eq!(user_record.score, 11); + assert_eq!(user_record.owner, payer.pubkey()); + assert!(user_record.compression_info.is_none()); + Ok(result.unwrap()) +} + +async fn test_decompress_single_user_record( + rpc: &mut LightProgramTest, + payer: &Keypair, + program_id: &Pubkey, + user_record_pda: &Pubkey, + user_record_bump: &u8, + expected_user_name: &str, + expected_slot: u64, +) { + let address_tree_pubkey = rpc.get_address_merkle_tree_v2(); + + // Get compressed user record + let user_compressed_address = derive_address( + &user_record_pda.to_bytes(), + &address_tree_pubkey.to_bytes(), + &program_id.to_bytes(), + ); + let c_user_pda = rpc + .get_compressed_account(user_compressed_address, None) + .await + .unwrap() + .value; + + let user_account_data = c_user_pda.data.as_ref().unwrap(); + let c_user_record = UserRecord::deserialize(&mut &user_account_data.data[..]).unwrap(); + + // Get validity proof for the compressed account + let rpc_result = rpc + .get_validity_proof(vec![c_user_pda.hash], vec![], None) + .await + .unwrap() + .value; + + let output_state_tree_info = rpc.get_random_state_tree_info().unwrap(); + // Use the new SDK helper function with typed data + let instruction = + light_compressible_client::CompressibleInstruction::decompress_accounts_idempotent( + program_id, + &CompressibleInstruction::DECOMPRESS_ACCOUNTS_IDEMPOTENT_DISCRIMINATOR, + &payer.pubkey(), + &payer.pubkey(), // rent_payer can be the same as fee_payer + &[*user_record_pda], + &[( + c_user_pda, + CompressedAccountVariant::UserRecord(c_user_record), + vec![b"user_record".to_vec(), payer.pubkey().to_bytes().to_vec()], + )], + &[*user_record_bump], + rpc_result, + output_state_tree_info, + ) + .unwrap(); + + // Verify PDA is uninitialized before decompression + let user_pda_account = rpc.get_account(*user_record_pda).await.unwrap(); + assert_eq!( + user_pda_account.as_ref().map(|a| a.data.len()).unwrap_or(0), + 0, + "User PDA account data len must be 0 before decompression" + ); + + // let cu = simulate_cu(rpc, &payer, &instruction).await; + // println!("DecompressSingleUserRecord CU consumed: {}", cu); + println!("skipping cu sim"); + + let result = rpc + .create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await; + assert!(result.is_ok(), "Decompress transaction should succeed"); + + // Verify UserRecord PDA is decompressed + let user_pda_account = rpc.get_account(*user_record_pda).await.unwrap(); + println!( + "user_pda_account after decompression: {:?}", + user_pda_account + ); + assert!( + user_pda_account.as_ref().map(|a| a.data.len()).unwrap_or(0) > 0, + "User PDA account data len must be > 0 after decompression" + ); + + let user_pda_data = user_pda_account.unwrap().data; + assert_eq!( + &user_pda_data[0..8], + UserRecord::DISCRIMINATOR, + "User account anchor discriminator mismatch" + ); + + let decompressed_user_record = UserRecord::try_deserialize(&mut &user_pda_data[..]).unwrap(); + assert_eq!(decompressed_user_record.name, expected_user_name); + assert_eq!(decompressed_user_record.score, 11); + assert_eq!(decompressed_user_record.owner, payer.pubkey()); + assert!(!decompressed_user_record + .compression_info + .as_ref() + .unwrap() + .is_compressed()); + assert_eq!( + decompressed_user_record + .compression_info + .as_ref() + .unwrap() + .last_written_slot(), + expected_slot + ); +} + +#[tokio::test] +async fn test_double_decompression_attack() { + let program_id = anchor_compressible::ID; + let config = ProgramTestConfig::new_v2(true, Some(vec![("anchor_compressible", program_id)])); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + + let _program_data_pda = setup_mock_program_data(&mut rpc, &payer, &program_id); + + let result = initialize_compression_config( + &mut rpc, + &payer, + &program_id, + &payer, + 100, + RENT_RECIPIENT, + vec![ADDRESS_SPACE[0]], + &CompressibleInstruction::INITIALIZE_COMPRESSION_CONFIG_DISCRIMINATOR, + None, + ) + .await; + assert!(result.is_ok(), "Initialize config should succeed"); + + let (user_record_pda, user_record_bump) = + Pubkey::find_program_address(&[b"user_record", payer.pubkey().as_ref()], &program_id); + + // Create and compress the account + test_create_record(&mut rpc, &payer, &program_id, &user_record_pda, None).await; + let address_tree_pubkey = rpc.get_address_merkle_tree_v2(); + let user_compressed_address = derive_address( + &user_record_pda.to_bytes(), + &address_tree_pubkey.to_bytes(), + &program_id.to_bytes(), + ); + let compressed_user_record = rpc + .get_compressed_account(user_compressed_address, None) + .await + .unwrap() + .value; + let c_user_record = + UserRecord::deserialize(&mut &compressed_user_record.data.unwrap().data[..]).unwrap(); + + rpc.warp_to_slot(100).unwrap(); + + // First decompression - should succeed + test_decompress_single_user_record( + &mut rpc, + &payer, + &program_id, + &user_record_pda, + &user_record_bump, + "Test User", + 100, + ) + .await; + + // Verify account is now decompressed + let user_pda_account = rpc.get_account(user_record_pda).await.unwrap(); + assert!( + user_pda_account.as_ref().map(|a| a.data.len()).unwrap_or(0) > 0, + "User PDA should be decompressed after first operation" + ); + + // Second decompression attempt - should be idempotent (skip already initialized account) + + let c_user_pda = rpc + .get_compressed_account(user_compressed_address, None) + .await + .unwrap() + .value; + + let rpc_result = rpc + .get_validity_proof(vec![c_user_pda.hash], vec![], None) + .await + .unwrap() + .value; + + let output_state_tree_info = rpc.get_random_state_tree_info().unwrap(); + + // Second decompression instruction - should still work (idempotent) + let instruction = + light_compressible_client::CompressibleInstruction::decompress_accounts_idempotent( + &program_id, + &CompressibleInstruction::DECOMPRESS_ACCOUNTS_IDEMPOTENT_DISCRIMINATOR, + &payer.pubkey(), + &payer.pubkey(), + &[user_record_pda], + &[( + c_user_pda, + CompressedAccountVariant::UserRecord(c_user_record), + vec![b"user_record".to_vec(), payer.pubkey().to_bytes().to_vec()], + )], + &[user_record_bump], + rpc_result, + output_state_tree_info, + ) + .unwrap(); + + let result = rpc + .create_and_send_transaction(&[instruction], &payer.pubkey(), &[&payer]) + .await; + + // Should succeed due to idempotent behavior (skips already initialized accounts) + assert!( + result.is_ok(), + "Second decompression should succeed idempotently" + ); + + // Verify account state is still correct and not corrupted + let user_pda_account = rpc.get_account(user_record_pda).await.unwrap(); + let user_pda_data = user_pda_account.unwrap().data; + let decompressed_user_record = UserRecord::try_deserialize(&mut &user_pda_data[..]).unwrap(); + + assert_eq!(decompressed_user_record.name, "Test User"); + assert_eq!(decompressed_user_record.score, 11); + assert_eq!(decompressed_user_record.owner, payer.pubkey()); + assert!(!decompressed_user_record + .compression_info + .as_ref() + .unwrap() + .is_compressed()); +} + +#[tokio::test] +async fn test_create_and_decompress_accounts_with_different_state_trees() { + let program_id = anchor_compressible::ID; + let config = ProgramTestConfig::new_v2(true, Some(vec![("anchor_compressible", program_id)])); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + + let config_pda = CompressibleConfig::derive_pda(&program_id, 0).0; + let _program_data_pda = setup_mock_program_data(&mut rpc, &payer, &program_id); + + let result = initialize_compression_config( + &mut rpc, + &payer, + &program_id, + &payer, + 100, + RENT_RECIPIENT, + vec![ADDRESS_SPACE[0]], + &CompressibleInstruction::INITIALIZE_COMPRESSION_CONFIG_DISCRIMINATOR, + None, + ) + .await; + assert!(result.is_ok(), "Initialize config should succeed"); + + let (user_record_pda, user_record_bump) = + Pubkey::find_program_address(&[b"user_record", payer.pubkey().as_ref()], &program_id); + + let session_id = 54321u64; + let (game_session_pda, game_bump) = Pubkey::find_program_address( + &[b"game_session", session_id.to_le_bytes().as_ref()], + &program_id, + ); + + // Get two different state trees + let first_state_tree_info = rpc.get_state_tree_infos()[0]; + let second_state_tree_info = rpc.get_state_tree_infos()[1]; + + // Create user record using first state tree + test_create_record( + &mut rpc, + &payer, + &program_id, + &user_record_pda, + Some(first_state_tree_info.queue), + ) + .await; + + // Create game session using second state tree + test_create_game_session( + &mut rpc, + &payer, + &program_id, + &config_pda, + &game_session_pda, + session_id, + Some(second_state_tree_info.queue), + ) + .await; + + rpc.warp_to_slot(100).unwrap(); + println!("created game session!, now decompressing..."); + + // Now decompress both accounts together - they come from different state trees + // This should succeed and validate that our decompression can handle mixed state tree sources + test_decompress_multiple_pdas( + &mut rpc, + &payer, + &program_id, + &config_pda, + &user_record_pda, + &user_record_bump, + &game_session_pda, + &game_bump, + session_id, + "Test User", + "Battle Royale", + 100, + ) + .await; +} + +#[tokio::test] +async fn test_update_record_compression_info() { + let program_id = anchor_compressible::ID; + let config = ProgramTestConfig::new_v2(true, Some(vec![("anchor_compressible", program_id)])); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + + let _program_data_pda = setup_mock_program_data(&mut rpc, &payer, &program_id); + + let result = initialize_compression_config( + &mut rpc, + &payer, + &program_id, + &payer, + 100, + RENT_RECIPIENT, + vec![ADDRESS_SPACE[0]], + &CompressibleInstruction::INITIALIZE_COMPRESSION_CONFIG_DISCRIMINATOR, + None, + ) + .await; + assert!(result.is_ok(), "Initialize config should succeed"); + + let (user_record_pda, user_record_bump) = + Pubkey::find_program_address(&[b"user_record", payer.pubkey().as_ref()], &program_id); + + // Create and compress the account + test_create_record(&mut rpc, &payer, &program_id, &user_record_pda, None).await; + + // Warp to slot 100 and decompress + rpc.warp_to_slot(100).unwrap(); + test_decompress_single_user_record( + &mut rpc, + &payer, + &program_id, + &user_record_pda, + &user_record_bump, + "Test User", + 100, + ) + .await; + + // Warp to slot 150 for the update + rpc.warp_to_slot(150).unwrap(); + + // Create update instruction + let accounts = anchor_compressible::accounts::UpdateRecord { + user: payer.pubkey(), + user_record: user_record_pda, + }; + + let instruction_data = anchor_compressible::instruction::UpdateRecord { + name: "Updated User".to_string(), + score: 42, + }; + + let instruction = Instruction { + program_id, + accounts: accounts.to_account_metas(None), + data: instruction_data.data(), + }; + + // Execute the update + let result = rpc + .create_and_send_transaction(&[instruction], &payer.pubkey(), &[&payer]) + .await; + assert!(result.is_ok(), "Update record transaction should succeed"); + + // Warp to slot 200 to ensure we're past the update + rpc.warp_to_slot(200).unwrap(); + + // Fetch the account and verify compression_info.last_written_slot + let user_pda_account = rpc.get_account(user_record_pda).await.unwrap(); + assert!( + user_pda_account.is_some(), + "User record account should exist after update" + ); + + let account_data = user_pda_account.unwrap().data; + let updated_user_record = UserRecord::try_deserialize(&mut &account_data[..]).unwrap(); + + // Verify the data was updated + assert_eq!(updated_user_record.name, "Updated User"); + assert_eq!(updated_user_record.score, 42); + assert_eq!(updated_user_record.owner, payer.pubkey()); + + // Verify compression_info.last_written_slot was updated to slot 150 + assert_eq!( + updated_user_record + .compression_info + .as_ref() + .unwrap() + .last_written_slot(), + 150 + ); + assert!(!updated_user_record + .compression_info + .as_ref() + .unwrap() + .is_compressed()); +} diff --git a/sdk-tests/anchor-compressible/tests/test_discriminator.rs b/sdk-tests/anchor-compressible/tests/test_discriminator.rs new file mode 100644 index 0000000000..b5fb4d20c1 --- /dev/null +++ b/sdk-tests/anchor-compressible/tests/test_discriminator.rs @@ -0,0 +1,18 @@ +#[test] +fn test_discriminator() { + use anchor_compressible::UserRecord; + use anchor_lang::Discriminator; + use light_sdk::LightDiscriminator; + + // anchor + let light_discriminator = UserRecord::DISCRIMINATOR; + println!("light discriminator: {:?}", light_discriminator); + + // ours (should be anchor compatible.) + let anchor_discriminator = UserRecord::LIGHT_DISCRIMINATOR; + + println!("Anchor discriminator: {:?}", anchor_discriminator); + println!("Match: {}", light_discriminator == anchor_discriminator); + + assert_eq!(light_discriminator, anchor_discriminator); +} diff --git a/sdk-tests/anchor-compressible/tests/test_instruction_builders.rs b/sdk-tests/anchor-compressible/tests/test_instruction_builders.rs new file mode 100644 index 0000000000..111b4c1612 --- /dev/null +++ b/sdk-tests/anchor-compressible/tests/test_instruction_builders.rs @@ -0,0 +1,374 @@ +mod test_instruction_builders { + + use light_client::indexer::{CompressedAccount, TreeInfo, ValidityProofWithContext}; + use light_compressed_account::TreeType; + use light_compressible_client::{CompressibleConfig, CompressibleInstruction}; + use light_sdk::instruction::ValidityProof; + use solana_sdk::{pubkey::Pubkey, system_program}; + + /// Test that our instruction builders follow Solana SDK patterns correctly + /// They should return Instruction directly, not Result + #[test] + fn test_initialize_compression_config_instruction_builder() { + let program_id = Pubkey::new_unique(); + let payer = Pubkey::new_unique(); + let authority = Pubkey::new_unique(); + let compression_delay = 100u32; + let rent_recipient = Pubkey::new_unique(); + let address_space = vec![Pubkey::new_unique()]; + + // Following Solana SDK patterns like system_instruction::transfer() + // Should return Instruction directly, not Result + let instruction = CompressibleInstruction::initialize_compression_config( + &program_id, + &[5u8], + &payer, + &authority, + compression_delay, + rent_recipient, + address_space, + Some(0), + ); + + // Verify instruction structure + assert_eq!(instruction.program_id, program_id); + assert_eq!(instruction.accounts.len(), 5); // payer, config, program_data, authority, system_program + + // Verify account order and permissions + assert_eq!(instruction.accounts[0].pubkey, payer); + assert!(instruction.accounts[0].is_signer); // payer signs + assert!(instruction.accounts[0].is_writable); // payer pays + + let (expected_config_pda, _) = CompressibleConfig::derive_pda(&program_id, 0); + assert_eq!(instruction.accounts[1].pubkey, expected_config_pda); + assert!(!instruction.accounts[1].is_signer); // config doesn't sign + assert!(instruction.accounts[1].is_writable); // config is created/written + + assert_eq!(instruction.accounts[3].pubkey, authority); + assert!(instruction.accounts[3].is_signer); // authority must sign + assert!(!instruction.accounts[3].is_writable); // authority is read-only + + assert_eq!(instruction.accounts[4].pubkey, system_program::ID); + assert!(!instruction.accounts[4].is_signer); // system program doesn't sign + assert!(!instruction.accounts[4].is_writable); // system program is read-only + + // Verify instruction data is present + assert!(!instruction.data.is_empty()); + + println!("✅ Instruction builder follows Solana SDK patterns correctly!"); + } + + #[test] + fn test_update_config_instruction_builder() { + let program_id = Pubkey::new_unique(); + let authority = Pubkey::new_unique(); + let new_compression_delay = Some(200u32); + let new_rent_recipient = Some(Pubkey::new_unique()); + + // Should return Instruction directly, following Solana SDK patterns + let instruction = CompressibleInstruction::update_compression_config( + &program_id, + &[6u8], + &authority, + new_compression_delay, + new_rent_recipient, + None, + None, + ); + + // Verify instruction structure + assert_eq!(instruction.program_id, program_id); + assert_eq!(instruction.accounts.len(), 2); // config, authority + + let (expected_config_pda, _) = CompressibleConfig::derive_pda(&program_id, 0); + assert_eq!(instruction.accounts[0].pubkey, expected_config_pda); + assert!(!instruction.accounts[0].is_signer); // config doesn't sign + assert!(instruction.accounts[0].is_writable); // config is updated + + assert_eq!(instruction.accounts[1].pubkey, authority); + assert!(instruction.accounts[1].is_signer); // authority must sign + assert!(!instruction.accounts[1].is_writable); // authority is read-only + + // Verify instruction data is present + assert!(!instruction.data.is_empty()); + + println!("✅ Update instruction builder follows Solana SDK patterns correctly!"); + } + + #[test] + fn test_decompress_accounts_idempotent_instruction_builder() { + use light_client::indexer::{AccountProofInputs, RootIndex}; + + let program_id = Pubkey::new_unique(); + let fee_payer = Pubkey::new_unique(); + let rent_payer = Pubkey::new_unique(); + let pda1 = Pubkey::new_unique(); + let pda2 = Pubkey::new_unique(); + let solana_accounts = vec![pda1, pda2]; + let config_pda = CompressibleConfig::derive_pda(&program_id, 0).0; + + // Create mock compressed accounts with tree info + let tree_info = TreeInfo { + queue: Pubkey::new_unique(), + tree: Pubkey::new_unique(), + tree_type: TreeType::StateV1, + cpi_context: None, + next_tree_info: None, + }; + + let compressed_account1 = CompressedAccount { + address: Some([1u8; 32]), + data: None, + hash: [1u8; 32], + lamports: 1000, + leaf_index: 0, + owner: program_id, + prove_by_index: false, + seq: Some(1), + slot_created: 100, + tree_info, + }; + + let compressed_account2 = CompressedAccount { + address: Some([2u8; 32]), + data: None, + hash: [2u8; 32], + lamports: 2000, + leaf_index: 1, + owner: program_id, + prove_by_index: false, + seq: Some(2), + slot_created: 101, + tree_info, + }; + + // Create account variant data (mock data for testing) + let account_variant1 = vec![1u8, 2, 3, 4]; // Mock compressed account variant + let account_variant2 = vec![5u8, 6, 7, 8]; // Mock compressed account variant + + let compressed_accounts = vec![ + ( + compressed_account1.clone(), + account_variant1, + vec![b"user_record".to_vec(), fee_payer.to_bytes().to_vec()], + ), + ( + compressed_account2.clone(), + account_variant2, + vec![b"game_session".to_vec(), 12345u64.to_le_bytes().to_vec()], + ), + ]; + + let bumps = vec![250u8, 251u8]; // typical PDA bumps + + // Create proper AccountProofInputs for the ValidityProofWithContext + let account_proof_inputs = vec![ + AccountProofInputs { + hash: compressed_account1.hash, + root: [0u8; 32], // Mock root + root_index: RootIndex::new_some(0), + leaf_index: compressed_account1.leaf_index as u64, + tree_info: compressed_account1.tree_info, + }, + AccountProofInputs { + hash: compressed_account2.hash, + root: [0u8; 32], // Mock root + root_index: RootIndex::new_some(0), + leaf_index: compressed_account2.leaf_index as u64, + tree_info: compressed_account2.tree_info, + }, + ]; + + // Create mock validity proof with context + let validity_proof_with_context = ValidityProofWithContext { + proof: ValidityProof::default(), + accounts: account_proof_inputs, // Provide proper account proof inputs + addresses: vec![], // Mock address proof inputs + }; + + let output_state_tree_info = tree_info; + + // Should return Result for the new API + let result = CompressibleInstruction::decompress_accounts_idempotent( + &program_id, + &[7u8], + &fee_payer, + &rent_payer, + &solana_accounts, + &compressed_accounts, + &bumps, + validity_proof_with_context, + output_state_tree_info, + ); + + // Verify instruction was created successfully + assert!(result.is_ok(), "Instruction creation should succeed"); + let instruction = result.unwrap(); + + // Verify instruction structure + assert_eq!(instruction.program_id, program_id); + + // Expected accounts: fee_payer, rent_payer, system_program, plus system accounts + assert!(instruction.accounts.len() >= 3); // At least the basic accounts + + // Verify account order and permissions + assert_eq!(instruction.accounts[0].pubkey, fee_payer); + assert!(instruction.accounts[0].is_signer); // fee_payer signs + assert!(instruction.accounts[0].is_writable); // fee_payer pays + + assert_eq!(instruction.accounts[1].pubkey, rent_payer); + assert!(instruction.accounts[1].is_signer); // rent_payer signs + assert!(instruction.accounts[1].is_writable); // rent_payer pays rent + + assert_eq!(instruction.accounts[2].pubkey, config_pda); + assert!(!instruction.accounts[2].is_signer); // system program doesn't sign + assert!(!instruction.accounts[2].is_writable); // system program is read-only + + // Verify instruction data is present and starts with discriminator + assert!(!instruction.data.is_empty()); + assert_eq!(&instruction.data[0..8], &[7, 0, 2, 0, 0, 0, 0, 0]); + + println!("✅ Decompress multiple accounts idempotent instruction builder follows Solana SDK patterns correctly!"); + } + + #[test] + fn test_decompress_accounts_idempotent_validation_accounts_mismatch() { + let program_id = Pubkey::new_unique(); + let fee_payer = Pubkey::new_unique(); + let rent_payer = Pubkey::new_unique(); + let solana_accounts = vec![Pubkey::new_unique()]; // 1 PDA + + // Create tree info + let tree_info = TreeInfo { + queue: Pubkey::new_unique(), + tree: Pubkey::new_unique(), + tree_type: TreeType::StateV1, + cpi_context: None, + next_tree_info: None, + }; + + // But 2 compressed accounts - should return error + let compressed_account1 = CompressedAccount { + address: Some([1u8; 32]), + data: None, + hash: [1u8; 32], + lamports: 1000, + leaf_index: 0, + owner: program_id, + prove_by_index: false, + seq: Some(1), + slot_created: 100, + tree_info, + }; + + let compressed_account2 = CompressedAccount { + address: Some([2u8; 32]), + data: None, + hash: [2u8; 32], + lamports: 2000, + leaf_index: 1, + owner: program_id, + prove_by_index: false, + seq: Some(2), + slot_created: 101, + tree_info, + }; + + let compressed_accounts = vec![ + ( + compressed_account1, + vec![1u8, 2, 3, 4], + vec![b"user_record".to_vec(), fee_payer.to_bytes().to_vec()], + ), + ( + compressed_account2, + vec![5u8, 6, 7, 8], + vec![b"game_session".to_vec(), 12345u64.to_le_bytes().to_vec()], + ), + ]; + + let bumps = vec![250u8]; + + let validity_proof_with_context = ValidityProofWithContext { + proof: ValidityProof::default(), + accounts: vec![], + addresses: vec![], + }; + + let result = CompressibleInstruction::decompress_accounts_idempotent( + &program_id, + &[7u8], + &fee_payer, + &rent_payer, + &solana_accounts, + &compressed_accounts, + &bumps, + validity_proof_with_context, + tree_info, + ); + + assert!( + result.is_err(), + "Should return error for mismatched accounts" + ); + assert!(result.unwrap_err().to_string().contains("same length")); + } + + #[test] + fn test_decompress_accounts_idempotent_validation_bumps_mismatch() { + let program_id = Pubkey::new_unique(); + let fee_payer = Pubkey::new_unique(); + let rent_payer = Pubkey::new_unique(); + let solana_accounts = vec![Pubkey::new_unique()]; // 1 PDA + + let tree_info = TreeInfo { + queue: Pubkey::new_unique(), + tree: Pubkey::new_unique(), + tree_type: TreeType::StateV1, + cpi_context: None, + next_tree_info: None, + }; + + let compressed_account = CompressedAccount { + address: Some([1u8; 32]), + data: None, + hash: [1u8; 32], + lamports: 1000, + leaf_index: 0, + owner: program_id, + prove_by_index: false, + seq: Some(1), + slot_created: 100, + tree_info, + }; + + let compressed_accounts = vec![( + compressed_account, + vec![1u8, 2, 3, 4], + vec![b"user_record".to_vec(), fee_payer.to_bytes().to_vec()], + )]; + + let bumps = vec![250u8, 251u8]; // 2 bumps but 1 PDA - should return error + + let validity_proof_with_context = ValidityProofWithContext { + proof: ValidityProof::default(), + accounts: vec![], + addresses: vec![], + }; + + let result = CompressibleInstruction::decompress_accounts_idempotent( + &program_id, + &[7u8], + &fee_payer, + &rent_payer, + &solana_accounts, + &compressed_accounts, + &bumps, + validity_proof_with_context, + tree_info, + ); + + assert!(result.is_err(), "Should return error for mismatched bumps"); + assert!(result.unwrap_err().to_string().contains("same length")); + } +} diff --git a/program-tests/sdk-test/Cargo.toml b/sdk-tests/native-compressible/Cargo.toml similarity index 52% rename from program-tests/sdk-test/Cargo.toml rename to sdk-tests/native-compressible/Cargo.toml index 6929b36a55..b449ff16f8 100644 --- a/program-tests/sdk-test/Cargo.toml +++ b/sdk-tests/native-compressible/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "sdk-test" +name = "native-compressible" version = "1.0.0" description = "Test program using generalized account compression" repository = "https://github.com/Lightprotocol/light-protocol" @@ -8,7 +8,8 @@ edition = "2021" [lib] crate-type = ["cdylib", "lib"] -name = "sdk_test" +name = "native_compressible" +doctest = false [features] no-entrypoint = [] @@ -19,16 +20,20 @@ test-sbf = [] default = [] [dependencies] -light-sdk = { workspace = true } -light-sdk-types = { workspace = true } -light-hasher = { workspace = true, features = ["solana"] } +light-sdk = { workspace = true, default-features = false, features = ["borsh"] } +light-sdk-types = { workspace = true, default-features = false } +light-hasher = { workspace = true, features = ["solana"], default-features = false } solana-program = { workspace = true } -light-macros = { workspace = true, features = ["solana"] } +light-macros = { workspace = true, features = ["solana"], default-features = false } borsh = { workspace = true } -light-compressed-account = { workspace = true, features = ["solana"] } +light-compressed-account = { workspace = true, features = ["solana"], default-features = false } +solana-clock = { workspace = true } +solana-sysvar = { workspace = true } [dev-dependencies] -light-program-test = { workspace = true, features = ["devenv"] } +light-program-test = { workspace = true, features = ["devenv"], default-features = false } +light-client = { workspace = true } +light-compressible-client = { workspace = true } tokio = { workspace = true } solana-sdk = { workspace = true } @@ -38,3 +43,4 @@ check-cfg = [ 'cfg(target_os, values("solana"))', 'cfg(feature, values("frozen-abi", "no-entrypoint"))', ] + diff --git a/program-tests/sdk-test/Xargo.toml b/sdk-tests/native-compressible/Xargo.toml similarity index 100% rename from program-tests/sdk-test/Xargo.toml rename to sdk-tests/native-compressible/Xargo.toml diff --git a/sdk-tests/native-compressible/src/compress_dynamic_pda.rs b/sdk-tests/native-compressible/src/compress_dynamic_pda.rs new file mode 100644 index 0000000000..bfbacf1101 --- /dev/null +++ b/sdk-tests/native-compressible/src/compress_dynamic_pda.rs @@ -0,0 +1,85 @@ +use borsh::{BorshDeserialize, BorshSerialize}; +use light_sdk::{ + compressible::{compress_pda_native, CompressibleConfig}, + cpi::CpiAccounts, + error::LightSdkError, + instruction::{account_meta::CompressedAccountMeta, ValidityProof}, +}; +use light_sdk_types::CpiAccountsConfig; +use solana_program::{account_info::AccountInfo, msg}; + +use crate::MyPdaAccount; + +/// Generic instruction data for compress account +/// This matches the expected format for compress account instructions +#[derive(BorshDeserialize, BorshSerialize)] +pub struct GenericCompressAccountInstruction { + pub proof: ValidityProof, + pub compressed_account_meta: CompressedAccountMeta, +} + +/// Compresses a PDA back into a compressed account +/// Anyone can call this after the timeout period has elapsed +pub fn compress_dynamic_pda( + accounts: &[AccountInfo], + instruction_data: &[u8], +) -> Result<(), LightSdkError> { + let mut instruction_data = instruction_data; + let instruction_data = GenericCompressAccountInstruction::deserialize(&mut instruction_data) + .map_err(|e| { + solana_program::msg!( + "Failed to deserialize GenericCompressAccountInstruction: {:?}", + e + ); + LightSdkError::Borsh + })?; + + let solana_account = &mut accounts[1].clone(); + let config_account = &accounts[2]; + let rent_recipient = &accounts[3]; + + msg!("solana_account?: {:?}", solana_account.key); + msg!("config_account?: {:?}", config_account.key); + msg!("rent_recipient?: {:?}", rent_recipient.key); + + // Load config + let config = CompressibleConfig::load_checked(config_account, &crate::ID)?; + + // CHECK: rent recipient from config + if rent_recipient.key != &config.rent_recipient { + solana_program::msg!( + "Rent recipient does not match config: {:?} != {:?}", + rent_recipient.key, + config.rent_recipient + ); + return Err(LightSdkError::ConstraintViolation); + } + + // Cpi accounts + let cpi_config = CpiAccountsConfig::new(crate::LIGHT_CPI_SIGNER); + let cpi_accounts = CpiAccounts::new_with_config(&accounts[0], &accounts[4..], cpi_config); + + // Deserialize the PDA account data (skip the 8-byte discriminator) + // Use a scope to ensure the borrow is dropped before compression + let mut pda_data = { + let account_data = solana_account.data.borrow(); + msg!("pda account: {:?}", account_data); + + MyPdaAccount::deserialize(&mut &account_data[8..]).map_err(|e| { + solana_program::msg!("Failed to deserialize MyPdaAccount: {:?}", e); + LightSdkError::Borsh + })? + }; // account_data borrow is dropped here + + compress_pda_native::( + solana_account, + &mut pda_data, + &instruction_data.compressed_account_meta, + instruction_data.proof, + cpi_accounts, + rent_recipient, + &config.compression_delay, + )?; + + Ok(()) +} diff --git a/sdk-tests/native-compressible/src/create_config.rs b/sdk-tests/native-compressible/src/create_config.rs new file mode 100644 index 0000000000..009bc3664f --- /dev/null +++ b/sdk-tests/native-compressible/src/create_config.rs @@ -0,0 +1,67 @@ +use borsh::{BorshDeserialize, BorshSerialize}; +use light_sdk::{ + compressible::process_initialize_compression_config_checked as sdk_process_initialize_compression_config_checked, + error::LightSdkError, +}; +use solana_program::{account_info::AccountInfo, msg, pubkey::Pubkey}; + +/// Creates a new compressible config PDA +pub fn process_initialize_compression_config_checked( + accounts: &[AccountInfo], + instruction_data: &[u8], +) -> Result<(), LightSdkError> { + let mut instruction_data = instruction_data; + msg!("instruction_data: {:?}", instruction_data.len()); + let instruction_data = InitializeCompressionConfigData::deserialize(&mut instruction_data) + .map_err(|err| { + msg!( + "InitializeCompressionConfigData::deserialize error: {:?}", + err + ); + LightSdkError::Borsh + })?; + + // Get accounts + let payer = &accounts[0]; + let config_account = &accounts[1]; + let program_data_account = &accounts[2]; + let update_authority = &accounts[3]; + let system_program = &accounts[4]; + + sdk_process_initialize_compression_config_checked( + config_account, + update_authority, + program_data_account, + &instruction_data.rent_recipient, + instruction_data.address_space, + instruction_data.compression_delay, + 0, // one global config for now, so bump is 0. + payer, + system_program, + &crate::ID, + )?; + + Ok(()) +} + +/// Generic instruction data for initialize config +/// Note: Real programs should use their specific instruction format +#[derive(BorshDeserialize, BorshSerialize)] +pub struct InitializeCompressionConfigData { + pub compression_delay: u32, + pub rent_recipient: Pubkey, + pub address_space: Vec, +} + +// Type alias for backward compatibility with tests +pub type CreateConfigInstructionData = InitializeCompressionConfigData; + +/// Generic instruction data for update config +/// Note: Real programs should use their specific instruction format +#[derive(BorshDeserialize, BorshSerialize)] +pub struct UpdateCompressionConfigData { + pub new_compression_delay: Option, + pub new_rent_recipient: Option, + pub new_address_space: Option>, + pub new_update_authority: Option, +} diff --git a/sdk-tests/native-compressible/src/create_dynamic_pda.rs b/sdk-tests/native-compressible/src/create_dynamic_pda.rs new file mode 100644 index 0000000000..4aeab43701 --- /dev/null +++ b/sdk-tests/native-compressible/src/create_dynamic_pda.rs @@ -0,0 +1,142 @@ +use borsh::{BorshDeserialize, BorshSerialize}; +use light_sdk::{ + compressible::{compress_account_on_init_native, CompressibleConfig, CompressionInfo}, + cpi::CpiAccounts, + error::LightSdkError, + instruction::{PackedAddressTreeInfo, ValidityProof}, +}; +use solana_program::{ + account_info::AccountInfo, program::invoke_signed, pubkey::Pubkey, rent::Rent, + system_instruction, sysvar::Sysvar, +}; + +use crate::MyPdaAccount; + +/// INITS a PDA and compresses it into a new compressed account. +pub fn create_dynamic_pda( + accounts: &[AccountInfo], + instruction_data: &[u8], +) -> Result<(), LightSdkError> { + let mut instruction_data = instruction_data; + let instruction_data = CreateDynamicPdaInstructionData::deserialize(&mut instruction_data) + .map_err(|e| { + solana_program::msg!("Borsh deserialization error: {:?}", e); + LightSdkError::ProgramError(e.into()) + })?; + + let fee_payer = &accounts[0]; + // UNCHECKED: ...caller program checks this. + let solana_account = &accounts[1]; + let rent_recipient = &accounts[2]; + let config_account = &accounts[3]; + let system_program = &accounts[4]; + + // Load config + let config = CompressibleConfig::load_checked(config_account, &crate::ID)?; + + // CHECK: rent recipient from config + if rent_recipient.key != &config.rent_recipient { + solana_program::msg!( + "rent recipient mismatch {:?} != {:?}", + rent_recipient.key, + config.rent_recipient + ); + return Err(LightSdkError::ConstraintViolation); + } + + // Derive PDA with seeds and bump + // For this example, we'll use a simple seed pattern + let seed_data = b"dynamic_pda"; // You can customize this based on your needs + let (derived_pda, bump_seed) = Pubkey::find_program_address(&[seed_data], &crate::ID); + + // Verify the PDA matches what was passed in + if derived_pda != *solana_account.key { + solana_program::msg!( + "PDA derivation mismatch. derived_pda: {:?} != solana_account.key: {:?}", + derived_pda, + solana_account.key + ); + return Err(LightSdkError::ConstraintViolation); + } + + // Calculate space needed for MyPdaAccount + let account_space = std::mem::size_of::() + 8; // 8 bytes for discriminator + + // Calculate rent + let rent = Rent::get()?; + let rent_lamports = rent.minimum_balance(account_space); + + // Create the PDA account using system program + let create_account_ix = system_instruction::create_account( + fee_payer.key, + solana_account.key, + rent_lamports, + account_space as u64, + &crate::ID, + ); + + invoke_signed( + &create_account_ix, + &[ + fee_payer.clone(), + solana_account.clone(), + system_program.clone(), + ], + &[&[seed_data, &[bump_seed]]], + ) + .map_err(|e| { + solana_program::msg!("pda account create error: {:?}", e); + LightSdkError::ProgramError(e) + })?; + + // Initialize the PDA account data + let mut pda_account_data = MyPdaAccount { + compression_info: Some(CompressionInfo::new_decompressed()?), + data: [1; 31], // Initialize with default data + }; + + // Serialize the initial data into the account - use scope to ensure borrow is dropped + { + let mut account_data = solana_account.data.borrow_mut(); + pda_account_data + .serialize(&mut &mut account_data[..]) + .map_err(|e| { + solana_program::msg!("pda account serialization error: {:?}", e); + LightSdkError::ProgramError(e.into()) + })?; + } // account_data borrow is dropped here + + // Cpi accounts + let cpi_accounts_struct = CpiAccounts::new(fee_payer, &accounts[5..], crate::LIGHT_CPI_SIGNER); + + // the onchain PDA is the seed for the cPDA. this way devs don't have to + // change their onchain PDA checks. + let new_address_params = instruction_data + .address_tree_info + .into_new_address_params_packed(solana_account.key.to_bytes()); + + solana_program::msg!("pda account data: {:?}", pda_account_data); + + // Use the efficient native variant that accepts pre-deserialized data + compress_account_on_init_native::( + &mut solana_account.clone(), + &mut pda_account_data, + &instruction_data.compressed_address, + &new_address_params, + instruction_data.output_state_tree_index, + cpi_accounts_struct, + &config.address_space, + rent_recipient, + instruction_data.proof, + )?; + + Ok(()) +} + +#[derive(Clone, Debug, Default, BorshDeserialize, BorshSerialize)] +pub struct CreateDynamicPdaInstructionData { + pub proof: ValidityProof, + pub compressed_address: [u8; 32], + pub address_tree_info: PackedAddressTreeInfo, + pub output_state_tree_index: u8, +} diff --git a/program-tests/sdk-test/src/create_pda.rs b/sdk-tests/native-compressible/src/create_pda.rs similarity index 90% rename from program-tests/sdk-test/src/create_pda.rs rename to sdk-tests/native-compressible/src/create_pda.rs index 95a7293589..081d9bbf09 100644 --- a/program-tests/sdk-test/src/create_pda.rs +++ b/sdk-tests/native-compressible/src/create_pda.rs @@ -5,10 +5,11 @@ use light_sdk::{ error::LightSdkError, instruction::{PackedAddressTreeInfo, ValidityProof}, light_hasher::hash_to_field_size::hashv_to_bn254_field_size_be_const_array, - LightDiscriminator, LightHasher, }; use solana_program::account_info::AccountInfo; +use crate::MyPdaAccount; + /// TODO: write test program with A8JgviaEAByMVLBhcebpDQ7NMuZpqBTBigC1b83imEsd (inconvenient program id) /// CU usage: /// - sdk pre system program cpi 10,942 CU @@ -52,7 +53,7 @@ pub fn create_pda( }; let new_address_params = address_tree_info.into_new_address_params_packed(address_seed); - let mut my_compressed_account = LightAccount::<'_, MyCompressedAccount>::new_init( + let mut my_compressed_account = LightAccount::<'_, MyPdaAccount>::new_init( &crate::ID, Some(address), instruction_data.output_merkle_tree_index, @@ -69,13 +70,6 @@ pub fn create_pda( Ok(()) } -#[derive( - Clone, Debug, Default, LightHasher, LightDiscriminator, BorshDeserialize, BorshSerialize, -)] -pub struct MyCompressedAccount { - pub data: [u8; 31], -} - #[derive(Clone, Debug, Default, BorshDeserialize, BorshSerialize)] pub struct CreatePdaInstructionData { pub proof: ValidityProof, diff --git a/sdk-tests/native-compressible/src/decompress_dynamic_pda.rs b/sdk-tests/native-compressible/src/decompress_dynamic_pda.rs new file mode 100644 index 0000000000..b70da33ece --- /dev/null +++ b/sdk-tests/native-compressible/src/decompress_dynamic_pda.rs @@ -0,0 +1,176 @@ +use borsh::{BorshDeserialize, BorshSerialize}; +use light_sdk::{ + account::sha::LightAccount, + compressible::{prepare_accounts_for_decompress_idempotent, CompressibleConfig}, + cpi::{CpiAccounts, CpiInputs}, + error::LightSdkError, + instruction::{account_meta::CompressedAccountMeta, ValidityProof}, +}; +use solana_program::{account_info::AccountInfo, msg}; + +use crate::MyPdaAccount; + +#[derive(Clone, Debug, BorshDeserialize, BorshSerialize)] +pub struct CompressedAccountData { + pub meta: CompressedAccountMeta, + /// Program-specific account variant enum + pub data: T, + /// PDA seeds (without bump) used to derive the PDA address + pub seeds: Vec>, +} +/// Example: Decompresses multiple compressed accounts into PDAs in a single transaction. +pub fn decompress_multiple_dynamic_pdas( + accounts: &[AccountInfo], + instruction_data: &[u8], +) -> Result<(), LightSdkError> { + #[derive(Clone, Debug, Default, BorshDeserialize, BorshSerialize)] + pub struct DecompressMultipleInstructionData { + pub proof: ValidityProof, + pub compressed_accounts: Vec>, + pub bumps: Vec, + pub system_accounts_offset: u8, + } + + let mut instruction_data = instruction_data; + let instruction_data = DecompressMultipleInstructionData::deserialize(&mut instruction_data) + .map_err(|e| { + solana_program::msg!( + "Failed to deserialize DecompressMultipleInstructionData: {:?}", + e + ); + LightSdkError::Borsh + })?; + + msg!("decompress_multiple_dynamic_pdas accounts: {:?}", accounts); + + // Account structure from CompressibleInstruction: + // [0] fee_payer (signer) + // [1] rent_payer (signer) + // [2] system_program + // [3..3+system_accounts_offset] PDA accounts + // [3+system_accounts_offset..] Light Protocol system accounts + + let fee_payer = &accounts[0]; + let rent_payer = &accounts[1]; + let config_account = &accounts[2]; + let config = CompressibleConfig::load_checked(config_account, &crate::ID)?; + + // PDA accounts start at index 3 and go for system_accounts_offset accounts + let pda_accounts_start = 3; + let pda_accounts_end = pda_accounts_start + instruction_data.system_accounts_offset as usize; + msg!("pda_accounts_start: {:?}", pda_accounts_start); + msg!("pda_accounts_end: {:?}", pda_accounts_end); + let solana_accounts = &accounts[pda_accounts_start..pda_accounts_end]; + msg!("solana_accounts: {:?}", solana_accounts); + + // Light Protocol system accounts start after PDA accounts + let system_accounts_start = pda_accounts_end; + let cpi_accounts = CpiAccounts::new( + fee_payer, + &accounts[system_accounts_start..], + crate::LIGHT_CPI_SIGNER, + ); + + // Validate we have matching number of PDAs, compressed accounts, and bumps + if solana_accounts.len() != instruction_data.compressed_accounts.len() + || solana_accounts.len() != instruction_data.bumps.len() + { + return Err(LightSdkError::ConstraintViolation); + } + + // First pass: validate PDAs and collect data + let mut compressed_accounts = Vec::new(); + let mut pda_account_refs = Vec::new(); + let stored_bumps = instruction_data.bumps.clone(); // Store bumps to avoid borrowing issues + + for (i, compressed_account_data) in instruction_data.compressed_accounts.iter().enumerate() { + let compressed_account = LightAccount::<'_, MyPdaAccount>::new_mut( + &crate::ID, + &compressed_account_data.meta, + compressed_account_data.data.clone(), + )?; + + let bump = stored_bumps[i]; + + // Derive PDA for verification using the provided bump + let seeds: Vec<&[u8]> = vec![b"dynamic_pda"]; + let (derived_pda, expected_bump) = + solana_program::pubkey::Pubkey::find_program_address(&seeds, &crate::ID); + + // Verify the PDA matches + if derived_pda != *solana_accounts[i].key { + msg!( + "derived_pda: {:?} does not match passed pda: {:?}", + derived_pda, + solana_accounts[i].key + ); + return Err(LightSdkError::ConstraintViolation); + } + + // Verify the provided bump matches the expected bump + if bump != expected_bump { + msg!( + "provided bump: {:?}, expected bump: {:?}", + bump, + expected_bump + ); + return Err(LightSdkError::ConstraintViolation); + } + + compressed_accounts.push(compressed_account); + pda_account_refs.push(&solana_accounts[i]); + } + + // Second pass: build signer seeds with stable references using seeds from instruction data + let mut all_signer_seeds_storage = Vec::new(); + for (i, compressed_account_data) in instruction_data.compressed_accounts.iter().enumerate() { + // Use seeds from instruction data and append bump + let mut seeds_with_bump = compressed_account_data.seeds.clone(); + seeds_with_bump.push(vec![stored_bumps[i]]); + all_signer_seeds_storage.push(seeds_with_bump); + } + + // Convert to the format needed by the SDK + let signer_seeds_refs: Vec> = all_signer_seeds_storage + .iter() + .map(|seeds| seeds.iter().map(|s| s.as_slice()).collect()) + .collect(); + let signer_seeds_slices: Vec<&[&[u8]]> = signer_seeds_refs + .iter() + .map(|seeds| seeds.as_slice()) + .collect(); + + // For native-compressible, we'll use a hardcoded address space that matches the test setup + // This should match the address space used in tests + let address_space = config.address_space[0]; + + // Use prepare_accounts_for_decompress_idempotent directly and handle CPI manually + let compressed_infos = prepare_accounts_for_decompress_idempotent::( + &pda_account_refs, + compressed_accounts, + &signer_seeds_slices, + &cpi_accounts, + rent_payer, + address_space, + )?; + + if !compressed_infos.is_empty() { + let cpi_inputs = CpiInputs::new(instruction_data.proof, compressed_infos); + cpi_inputs.invoke_light_system_program(cpi_accounts)?; + } + + Ok(()) +} + +#[derive(Clone, Debug, Default, BorshDeserialize, BorshSerialize)] +pub struct DecompressToPdaInstructionData { + pub proof: ValidityProof, + pub compressed_account: MyCompressedAccount, + pub system_accounts_offset: u8, +} + +#[derive(Clone, Debug, Default, BorshDeserialize, BorshSerialize)] +pub struct MyCompressedAccount { + pub meta: CompressedAccountMeta, + pub data: MyPdaAccount, +} diff --git a/sdk-tests/native-compressible/src/lib.rs b/sdk-tests/native-compressible/src/lib.rs new file mode 100644 index 0000000000..bda5cfb4fa --- /dev/null +++ b/sdk-tests/native-compressible/src/lib.rs @@ -0,0 +1,284 @@ +use borsh::{BorshDeserialize, BorshSerialize}; +use light_macros::pubkey; +use light_sdk::{ + account::Size, + compressible::{CompressionInfo, HasCompressionInfo}, + cpi::CpiSigner, + derive_light_cpi_signer, + error::LightSdkError, + sha::LightHasher, + LightDiscriminator, +}; +use solana_program::{ + account_info::AccountInfo, entrypoint, program_error::ProgramError, pubkey::Pubkey, +}; + +pub mod compress_dynamic_pda; +pub mod create_config; +pub mod create_dynamic_pda; +pub mod create_pda; +pub mod decompress_dynamic_pda; +pub mod update_config; +pub mod update_pda; + +pub const ID: Pubkey = pubkey!("FNt7byTHev1k5x2cXZLBr8TdWiC3zoP5vcnZR4P682Uy"); +pub const LIGHT_CPI_SIGNER: CpiSigner = + derive_light_cpi_signer!("FNt7byTHev1k5x2cXZLBr8TdWiC3zoP5vcnZR4P682Uy"); + +entrypoint!(process_instruction); + +#[repr(u8)] +pub enum InstructionType { + CreatePdaBorsh = 0, + UpdatePdaBorsh = 1, + CompressDynamicPda = 2, + CreateDynamicPda = 3, + InitializeCompressionConfig = 4, + UpdateCompressionConfig = 5, + DecompressAccountsIdempotent = 6, +} + +impl TryFrom for InstructionType { + type Error = LightSdkError; + + fn try_from(value: u8) -> Result { + match value { + 0 => Ok(InstructionType::CreatePdaBorsh), + 1 => Ok(InstructionType::UpdatePdaBorsh), + 2 => Ok(InstructionType::CompressDynamicPda), + 3 => Ok(InstructionType::CreateDynamicPda), + 4 => Ok(InstructionType::InitializeCompressionConfig), + 5 => Ok(InstructionType::UpdateCompressionConfig), + 6 => Ok(InstructionType::DecompressAccountsIdempotent), + + _ => panic!("Invalid instruction discriminator."), + } + } +} + +pub fn process_instruction( + _program_id: &Pubkey, + accounts: &[AccountInfo], + instruction_data: &[u8], +) -> Result<(), ProgramError> { + let discriminator = InstructionType::try_from(instruction_data[0]) + .map_err(|_| ProgramError::InvalidInstructionData)?; + + match discriminator { + InstructionType::CreatePdaBorsh => { + create_pda::create_pda::(accounts, &instruction_data[1..]) + } + InstructionType::UpdatePdaBorsh => { + update_pda::update_pda::(accounts, &instruction_data[1..]) + } + InstructionType::CompressDynamicPda => { + compress_dynamic_pda::compress_dynamic_pda(accounts, &instruction_data[1..]) + } + InstructionType::CreateDynamicPda => { + create_dynamic_pda::create_dynamic_pda(accounts, &instruction_data[1..]) + } + + InstructionType::InitializeCompressionConfig => { + create_config::process_initialize_compression_config_checked( + accounts, + &instruction_data[1..], + ) + } + InstructionType::UpdateCompressionConfig => { + update_config::process_update_config(accounts, &instruction_data[1..]) + } + InstructionType::DecompressAccountsIdempotent => { + decompress_dynamic_pda::decompress_multiple_dynamic_pdas( + accounts, + &instruction_data[1..], + ) + } + }?; + Ok(()) +} + +#[derive( + Clone, Debug, Default, LightHasher, LightDiscriminator, BorshDeserialize, BorshSerialize, +)] +pub struct MyPdaAccount { + #[skip] + pub compression_info: Option, + pub data: [u8; 31], +} + +// Implement the HasCompressionInfo trait +impl HasCompressionInfo for MyPdaAccount { + fn compression_info(&self) -> &CompressionInfo { + self.compression_info + .as_ref() + .expect("CompressionInfo must be Some on-chain") + } + + fn compression_info_mut(&mut self) -> &mut CompressionInfo { + self.compression_info + .as_mut() + .expect("CompressionInfo must be Some on-chain") + } + + fn compression_info_mut_opt(&mut self) -> &mut Option { + &mut self.compression_info + } + + fn set_compression_info_none(&mut self) { + self.compression_info = None; + } +} + +impl Size for MyPdaAccount { + fn size(&self) -> usize { + // compression_info is #[skip], so not serialized + Self::LIGHT_DISCRIMINATOR_SLICE.len() + 31 + 1 + 9 // discriminator + data: [u8; 31] + compression_info: Option + } +} + +#[cfg(test)] +mod test_sha_hasher { + use light_hasher::{to_byte_array::ToByteArray, DataHasher, Sha256}; + use light_sdk::sha::LightHasher; + + use super::*; + + #[derive( + Clone, Debug, Default, LightDiscriminator, BorshDeserialize, BorshSerialize, LightHasher, + )] + pub struct TestShaAccount { + #[skip] + pub compression_info: Option, + pub data: [u8; 31], + } + + #[test] + fn test_sha256_vs_poseidon_hashing() { + let account = MyPdaAccount { + compression_info: None, + data: [42u8; 31], + }; + + // Test Poseidon hashing (default) + let poseidon_hash = account.hash::().unwrap(); + + // Test SHA256 hashing + let sha256_hash = account.hash::().unwrap(); + + // They should be different + assert_ne!(poseidon_hash, sha256_hash); + + // Both should have first byte as 0 (field size truncated) or be different due to different hashing + println!("Poseidon hash: {:?}", poseidon_hash); + println!("SHA256 hash: {:?}", sha256_hash); + } + + #[test] + fn test_sha_hasher_derive_macro() { + let sha_account = TestShaAccount { + compression_info: None, + data: [99u8; 31], + }; + + // Test the to_byte_array implementation (which should use SHA256 internally) + let sha_byte_array = sha_account.to_byte_array().unwrap(); + + // Test DataHasher implementation with SHA256 + let sha_data_hash = sha_account.hash::().unwrap(); + + // Both should have first byte truncated to 0 for field size + assert_eq!(sha_byte_array[0], 0); + assert_eq!(sha_data_hash[0], 0); + + assert_eq!(sha_byte_array.len(), 32); + assert_eq!(sha_data_hash.len(), 32); + + println!("SHA account to_byte_array: {:?}", sha_byte_array); + println!("SHA account DataHasher: {:?}", sha_data_hash); + + // Test that this is different from Poseidon hashing + let poseidon_hash = sha_account.hash::().unwrap(); + // Poseidon hash should not have first byte truncated (ID=0) + assert_ne!(sha_byte_array, poseidon_hash); + assert_ne!(sha_data_hash, poseidon_hash); + + println!("Same account with Poseidon: {:?}", poseidon_hash); + } + + #[test] + fn test_large_struct_with_sha_hasher() { + // This demonstrates that SHA256 can handle arbitrary-sized data + // while Poseidon is limited to 12 fields in the current implementation + + use light_hasher::{Hasher, Sha256}; + + // Create a large struct that would exceed Poseidon's field limits + #[derive(Clone, Debug, Default, BorshDeserialize, BorshSerialize)] + struct LargeStruct { + pub field1: u64, + pub field2: u64, + pub field3: u64, + pub field4: u64, + pub field5: u64, + pub field6: u64, + pub field7: u64, + pub field8: u64, + pub field9: u64, + pub field10: u64, + pub field11: u64, + pub field12: u64, + pub field13: u64, + // Pubkeys that would require #[hash] attribute with Poseidon + pub owner: solana_program::pubkey::Pubkey, + pub authority: solana_program::pubkey::Pubkey, + } + + let large_account = LargeStruct { + field1: 1, + field2: 2, + field3: 3, + field4: 4, + field5: 5, + field6: 6, + field7: 7, + field8: 8, + field9: 9, + field10: 10, + field11: 11, + field12: 12, + field13: 13, + owner: solana_program::pubkey::Pubkey::new_unique(), + authority: solana_program::pubkey::Pubkey::new_unique(), + }; + + // Test that SHA256 can hash large data by serializing the whole struct + let serialized = large_account.try_to_vec().unwrap(); + println!("Serialized struct size: {} bytes", serialized.len()); + + // SHA256 can hash arbitrary amounts of data + let sha_hash = Sha256::hash(&serialized).unwrap(); + println!("SHA256 hash: {:?}", sha_hash); + + // Verify the hash is truncated properly (first byte should be 0 for field size) + // Note: Since SHA256::ID = 1 (not 0), the system program expects truncation + let mut expected_hash = sha_hash; + expected_hash[0] = 0; + + assert_eq!(sha_hash.len(), 32); + // For demonstration - in real usage, the truncation would be applied by the system + println!("SHA256 hash truncated: {:?}", expected_hash); + + // Show that this would be different from a smaller struct + let small_struct = MyPdaAccount { + compression_info: None, + data: [42u8; 31], + }; + + let small_serialized = small_struct.try_to_vec().unwrap(); + let small_hash = Sha256::hash(&small_serialized).unwrap(); + + // Different data should produce different hashes + assert_ne!(sha_hash, small_hash); + println!("Different struct produces different hash: {:?}", small_hash); + } +} diff --git a/sdk-tests/native-compressible/src/update_config.rs b/sdk-tests/native-compressible/src/update_config.rs new file mode 100644 index 0000000000..37b4caed13 --- /dev/null +++ b/sdk-tests/native-compressible/src/update_config.rs @@ -0,0 +1,37 @@ +use borsh::{BorshDeserialize, BorshSerialize}; +use light_sdk::{compressible::process_update_compression_config, error::LightSdkError}; +use solana_program::{account_info::AccountInfo, pubkey::Pubkey}; + +/// Updates an existing compressible config +pub fn process_update_config( + accounts: &[AccountInfo], + instruction_data: &[u8], +) -> Result<(), LightSdkError> { + let mut instruction_data = instruction_data; + let instruction_data = UpdateConfigInstructionData::deserialize(&mut instruction_data) + .map_err(|_| LightSdkError::Borsh)?; + + // Get accounts + let config_account = &accounts[0]; + let authority = &accounts[1]; + + process_update_compression_config( + config_account, + authority, + instruction_data.new_update_authority.as_ref(), + instruction_data.new_rent_recipient.as_ref(), + instruction_data.new_address_space, + instruction_data.new_compression_delay, + &crate::ID, + )?; + + Ok(()) +} + +#[derive(Clone, Debug, BorshDeserialize, BorshSerialize)] +pub struct UpdateConfigInstructionData { + pub new_update_authority: Option, + pub new_rent_recipient: Option, + pub new_address_space: Option>, + pub new_compression_delay: Option, +} diff --git a/program-tests/sdk-test/src/update_pda.rs b/sdk-tests/native-compressible/src/update_pda.rs similarity index 92% rename from program-tests/sdk-test/src/update_pda.rs rename to sdk-tests/native-compressible/src/update_pda.rs index 2e2fcd4257..ffd102b9eb 100644 --- a/program-tests/sdk-test/src/update_pda.rs +++ b/sdk-tests/native-compressible/src/update_pda.rs @@ -7,7 +7,7 @@ use light_sdk::{ }; use solana_program::{account_info::AccountInfo, log::sol_log_compute_units}; -use crate::create_pda::MyCompressedAccount; +use crate::MyPdaAccount; /// CU usage: /// - sdk pre system program 9,183k CU @@ -22,10 +22,11 @@ pub fn update_pda( let instruction_data = UpdatePdaInstructionData::deserialize(&mut instruction_data) .map_err(|_| LightSdkError::Borsh)?; - let mut my_compressed_account = LightAccount::<'_, MyCompressedAccount>::new_mut( + let mut my_compressed_account = LightAccount::<'_, MyPdaAccount>::new_mut( &crate::ID, &instruction_data.my_compressed_account.meta, - MyCompressedAccount { + MyPdaAccount { + compression_info: None, data: instruction_data.my_compressed_account.data, }, )?; diff --git a/sdk-tests/native-compressible/tests/test_compressible_flow.rs b/sdk-tests/native-compressible/tests/test_compressible_flow.rs new file mode 100644 index 0000000000..a63d605279 --- /dev/null +++ b/sdk-tests/native-compressible/tests/test_compressible_flow.rs @@ -0,0 +1,390 @@ +#![cfg(feature = "test-sbf")] + +use core::panic; + +use borsh::{BorshDeserialize, BorshSerialize}; +use light_compressed_account::address::derive_address; +use light_compressible_client::CompressibleInstruction; +use light_program_test::{ + initialize_compression_config, + program_test::{LightProgramTest, TestRpc}, + setup_mock_program_data, AddressWithTree, Indexer, ProgramTestConfig, Rpc, +}; +use light_sdk::{ + compressible::CompressibleConfig, + instruction::{PackedAccounts, SystemAccountMetaConfig}, +}; +use native_compressible::{ + create_dynamic_pda::CreateDynamicPdaInstructionData, InstructionType, MyPdaAccount, +}; +use solana_sdk::{ + instruction::{AccountMeta, Instruction}, + pubkey::Pubkey, + signature::{Keypair, Signer}, +}; + +// Test constants +const RENT_RECIPIENT: Pubkey = + light_macros::pubkey!("CLEuMG7pzJX9xAuKCFzBP154uiG1GaNo4Fq7x6KAcAfG"); +const COMPRESSION_DELAY: u64 = 200; + +#[tokio::test] +async fn test_complete_compressible_flow() { + let config = ProgramTestConfig::new_v2( + true, + Some(vec![("native_compressible", native_compressible::ID)]), + ); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + + let _config_pda = CompressibleConfig::derive_default_pda(&native_compressible::ID).0; + let _program_data_pda = setup_mock_program_data(&mut rpc, &payer, &native_compressible::ID); + + // Get address tree for the address space + let address_tree = rpc.get_address_merkle_tree_v2(); + + let result = initialize_compression_config( + &mut rpc, + &payer, + &native_compressible::ID, + &payer, + 200, + RENT_RECIPIENT, + vec![address_tree], + &[InstructionType::InitializeCompressionConfig as u8], + None, + ) + .await; + assert!(result.is_ok(), "Initialize config should succeed"); + + // 1. Create and compress account on init + let test_data = [1u8; 31]; + + let seeds: &[&[u8]] = &[b"dynamic_pda"]; + let (pda_pubkey, _bump) = Pubkey::find_program_address(seeds, &native_compressible::ID); + + let address_tree_pubkey = rpc.get_address_merkle_tree_v2(); + + let compressed_address = derive_address( + &pda_pubkey.to_bytes(), + &address_tree_pubkey.to_bytes(), + &native_compressible::ID.to_bytes(), + ); + + let pda_pubkey = create_and_compress_account(&mut rpc, &payer, test_data).await; + + // get account + let account = rpc.get_account(pda_pubkey).await.unwrap(); + assert!(account.is_some()); + assert_eq!(account.unwrap().lamports, 0); + + // get compressed account + let compressed_account = rpc.get_compressed_account(compressed_address, None).await; + assert!(compressed_account.is_ok()); + + // 2. Wait for compression delay to pass + rpc.warp_to_slot(COMPRESSION_DELAY + 1).unwrap(); + + // 3. Decompress the account + decompress_account(&mut rpc, &payer, &pda_pubkey, test_data).await; + + // get account + let account = rpc.get_account(pda_pubkey).await.unwrap(); + assert!(account.is_some()); + assert!(account.unwrap().lamports > 0); + // assert_eq!(account.unwrap().data.len(), 31); + + // 4. Verify PDA is decompressed + verify_decompressed_account(&mut rpc, &pda_pubkey, &compressed_address, test_data).await; + + // 5. Wait for compression delay to pass again + rpc.warp_to_slot(COMPRESSION_DELAY * 2 + 1).unwrap(); + + // 6. Compress the account again + compress_existing_account(&mut rpc, &payer, &pda_pubkey).await; + + // 7. Verify account is compressed again + verify_compressed_account(&mut rpc, &pda_pubkey).await; +} + +async fn create_and_compress_account( + rpc: &mut LightProgramTest, + payer: &Keypair, + _test_data: [u8; 31], +) -> Pubkey { + // Derive PDA + let seeds: &[&[u8]] = &[b"dynamic_pda"]; + let (pda_pubkey, _bump) = Pubkey::find_program_address(seeds, &native_compressible::ID); + + // Get address tree + let address_tree_pubkey = rpc.get_address_merkle_tree_v2(); + + // Derive compressed address + let compressed_address = derive_address( + &pda_pubkey.to_bytes(), + &address_tree_pubkey.to_bytes(), + &native_compressible::ID.to_bytes(), + ); + + // Get validity proof + let rpc_result = rpc + .get_validity_proof( + vec![], + vec![AddressWithTree { + address: compressed_address, + tree: address_tree_pubkey, + }], + None, + ) + .await + .unwrap() + .value; + + // Setup remaining accounts + let mut remaining_accounts = PackedAccounts::default(); + let system_config = SystemAccountMetaConfig::new(native_compressible::ID); + remaining_accounts.add_system_accounts(system_config); + + // Pack tree infos + let packed_tree_infos = rpc_result.pack_tree_infos(&mut remaining_accounts); + let address_tree_info = packed_tree_infos.address_trees[0]; + + // Get output state tree index + let output_state_tree_index = + remaining_accounts.insert_or_get(rpc.get_random_state_tree_info().unwrap().queue); + + let (system_accounts, _, _) = remaining_accounts.to_account_metas(); + + // Create instruction data for create_dynamic_pda + let instruction_data = CreateDynamicPdaInstructionData { + proof: rpc_result.proof, + compressed_address, + address_tree_info, + output_state_tree_index, + }; + + // Build instruction + let instruction = Instruction { + program_id: native_compressible::ID, + accounts: [ + vec![ + AccountMeta::new(payer.pubkey(), true), // fee_payer + AccountMeta::new(pda_pubkey, false), // solana_account + AccountMeta::new(RENT_RECIPIENT, false), // rent_recipient + AccountMeta::new_readonly( + CompressibleConfig::derive_default_pda(&native_compressible::ID).0, + false, + ), // config + AccountMeta::new_readonly(solana_sdk::system_program::ID, false), // system_program + ], + system_accounts, + ] + .concat(), + data: [ + &[InstructionType::CreateDynamicPda as u8][..], + &instruction_data.try_to_vec().unwrap()[..], + ] + .concat(), + }; + + let result = rpc + .create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await; + + assert!( + result.is_ok(), + "Create and compress failed error: {:?}", + result.err() + ); + + pda_pubkey +} + +async fn decompress_account( + rpc: &mut LightProgramTest, + payer: &Keypair, + pda_pubkey: &Pubkey, + test_data: [u8; 31], +) { + // Get the compressed address + let address_tree_pubkey = rpc.get_address_merkle_tree_v2(); + let compressed_address = derive_address( + &pda_pubkey.to_bytes(), + &address_tree_pubkey.to_bytes(), + &native_compressible::ID.to_bytes(), + ); + + // Try to get the compressed account from the indexer + let compressed_account_result = rpc.get_compressed_account(compressed_address, None).await; + + if compressed_account_result.is_err() { + panic!("Could not get compressed account"); + } + + let compressed_account = compressed_account_result.unwrap().value; + + // Create MyPdaAccount from the test data + let my_pda_account = MyPdaAccount { + compression_info: None, // Will be set during decompression + data: test_data, + }; + + // Get validity proof + let rpc_result = rpc + .get_validity_proof(vec![compressed_account.hash], vec![], None) + .await + .unwrap() + .value; + + let instruction = CompressibleInstruction::decompress_accounts_idempotent( + &native_compressible::ID, + &[InstructionType::DecompressAccountsIdempotent as u8], // Use sdk-test's DecompressAccountsIdempotent discriminator + &payer.pubkey(), + &payer.pubkey(), + &[*pda_pubkey], + &[( + compressed_account.clone(), + my_pda_account.clone(), // MyPdaAccount implements required trait + vec![b"dynamic_pda".to_vec()], // PDA seeds without bump + )], + &[Pubkey::find_program_address(&[b"dynamic_pda"], &native_compressible::ID).1], // bump seed, must match the seeds used in create_dynamic_pda + rpc_result, + compressed_account.tree_info, + ) + .unwrap(); + + let result = rpc + .create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await; + + assert!( + result.is_ok(), + "Decompress failed error: {:?}", + result.err() + ); +} + +async fn compress_existing_account( + rpc: &mut LightProgramTest, + payer: &Keypair, + pda_pubkey: &Pubkey, +) { + // Get the account data first + let account = rpc.get_account(*pda_pubkey).await.unwrap(); + if account.is_none() { + println!("PDA account not found, cannot compress"); + return; + } + + let account = account.unwrap(); + assert!(account.lamports > 0, "PDA account should have lamports"); + + // Get the compressed address + let address_tree_pubkey = rpc.get_address_merkle_tree_v2(); + let compressed_address = derive_address( + &pda_pubkey.to_bytes(), + &address_tree_pubkey.to_bytes(), + &native_compressible::ID.to_bytes(), + ); + + // Try to get the existing compressed account + let compressed_account_result = rpc.get_compressed_account(compressed_address, None).await; + + if compressed_account_result.is_err() { + panic!("Could not get compressed account"); + } + + let compressed_account = compressed_account_result.unwrap().value; + + // Get validity proof + let rpc_result = rpc + .get_validity_proof(vec![compressed_account.hash], vec![], None) + .await + .unwrap() + .value; + + let instruction = CompressibleInstruction::compress_account( + &native_compressible::ID, + &[InstructionType::CompressDynamicPda as u8], // Use sdk-test's CompressFromPda discriminator + &payer.pubkey(), + pda_pubkey, + &RENT_RECIPIENT, + &compressed_account, + rpc_result, + compressed_account.tree_info, + ) + .unwrap(); + + let result = rpc + .create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await; + + assert!(result.is_ok(), "Compress failed error: {:?}", result.err()); +} + +async fn verify_decompressed_account( + rpc: &mut LightProgramTest, + pda_pubkey: &Pubkey, + compressed_address: &[u8; 32], + expected_data: [u8; 31], +) { + let account = rpc.get_account(*pda_pubkey).await.unwrap(); + + assert!( + account.is_some(), + "PDA account not found after decompression" + ); + + let account = account.unwrap(); + assert!( + account.data.len() > 8, + "PDA account not properly decompressed (empty data)" + ); + + // Try to deserialize the account data (skip the 8-byte discriminator) + let solana_account = MyPdaAccount::deserialize(&mut &account.data[8..]) + .expect("Could not deserialize PDA account data"); + assert!(solana_account.compression_info.is_some()); + assert_eq!(solana_account.data, expected_data); // data matches the expected data + assert!( + !solana_account + .compression_info + .as_ref() + .unwrap() + .is_compressed(), + "PDA account should not be compressed" + ); + // slot matches the slot of the last write + assert_eq!( + &solana_account.compression_info.unwrap().last_written_slot(), + &rpc.get_slot().await.unwrap() + ); + + let compressed_account = rpc.get_compressed_account(*compressed_address, None).await; + assert!(compressed_account.is_ok()); + let compressed_account = compressed_account.unwrap().value; + // After decompression, the compressed account data should be cleared + // This is a known behavior - commenting out for now to see if test passes + + assert!( + compressed_account.data.unwrap().data.as_slice().is_empty(), + "Compressed account data must be empty" + ); +} + +async fn verify_compressed_account(rpc: &mut LightProgramTest, pda_pubkey: &Pubkey) { + let account = rpc.get_account(*pda_pubkey).await.unwrap(); + + if let Some(account) = account { + assert_eq!( + account.lamports, 0, + "PDA account should have 0 lamports when compressed" + ); + assert!( + account.data.is_empty(), + "PDA account should have empty data when compressed" + ); + } else { + panic!("PDA account not found"); + } +} diff --git a/sdk-tests/native-compressible/tests/test_config.rs b/sdk-tests/native-compressible/tests/test_config.rs new file mode 100644 index 0000000000..bdc0be31e1 --- /dev/null +++ b/sdk-tests/native-compressible/tests/test_config.rs @@ -0,0 +1,160 @@ +#![cfg(feature = "test-sbf")] + +use borsh::BorshSerialize; +use light_macros::pubkey; +use light_program_test::{program_test::LightProgramTest, ProgramTestConfig, Rpc}; +use light_sdk::compressible::CompressibleConfig; +use native_compressible::create_config::CreateConfigInstructionData; +use solana_sdk::{ + bpf_loader_upgradeable, + instruction::{AccountMeta, Instruction}, + pubkey::Pubkey, + signature::{Keypair, Signer}, +}; + +pub const ADDRESS_SPACE: Pubkey = pubkey!("CLEuMG7pzJX9xAuKCFzBP154uiG1GaNo4Fq7x6KAcAfG"); +pub const RENT_RECIPIENT: Pubkey = pubkey!("CLEuMG7pzJX9xAuKCFzBP154uiG1GaNo4Fq7x6KAcAfG"); + +#[tokio::test] +async fn test_create_and_update_config() { + let config = ProgramTestConfig::new_v2( + true, + Some(vec![("native_compressible", native_compressible::ID)]), + ); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + + // Derive config PDA + let (config_pda, _) = CompressibleConfig::derive_pda(&native_compressible::ID, 0); + + // Derive program data account + let (program_data_pda, _) = Pubkey::find_program_address( + &[native_compressible::ID.as_ref()], + &bpf_loader_upgradeable::ID, + ); + + // Test create config + let create_ix_data = CreateConfigInstructionData { + rent_recipient: RENT_RECIPIENT, + address_space: vec![ADDRESS_SPACE], // Can add more for multi-address-space support + compression_delay: 100, + }; + + let create_ix = Instruction { + program_id: native_compressible::ID, + accounts: vec![ + AccountMeta::new(payer.pubkey(), true), + AccountMeta::new(config_pda, false), + AccountMeta::new_readonly(payer.pubkey(), true), // update_authority (signer) + AccountMeta::new_readonly(program_data_pda, false), // program data account + AccountMeta::new_readonly(solana_sdk::system_program::ID, false), + ], + data: [&[5u8][..], &create_ix_data.try_to_vec().unwrap()[..]].concat(), + }; + + // Note: This will fail in the test environment because the program data account + // doesn't exist in the test validator. In a real deployment, this would work. + let result = rpc + .create_and_send_transaction(&[create_ix], &payer.pubkey(), &[&payer]) + .await; + + // We expect this to fail in test environment + assert!( + result.is_err(), + "Should fail without proper program data account" + ); +} + +#[tokio::test] +async fn test_config_validation() { + let config = ProgramTestConfig::new_v2( + true, + Some(vec![("native_compressible", native_compressible::ID)]), + ); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + let non_authority = Keypair::new(); + + // Derive PDAs + let (config_pda, _) = CompressibleConfig::derive_default_pda(&native_compressible::ID); + let (program_data_pda, _) = Pubkey::find_program_address( + &[native_compressible::ID.as_ref()], + &bpf_loader_upgradeable::ID, + ); + + // Try to create config with non-authority (should fail) + let create_ix_data = CreateConfigInstructionData { + rent_recipient: RENT_RECIPIENT, + address_space: vec![ADDRESS_SPACE], + compression_delay: 100, + }; + + let create_ix = Instruction { + program_id: native_compressible::ID, + accounts: vec![ + AccountMeta::new(payer.pubkey(), true), + AccountMeta::new(config_pda, false), + AccountMeta::new_readonly(non_authority.pubkey(), true), // wrong authority (signer) + AccountMeta::new_readonly(program_data_pda, false), + AccountMeta::new_readonly(solana_sdk::system_program::ID, false), + ], + data: [&[5u8][..], &create_ix_data.try_to_vec().unwrap()[..]].concat(), + }; + + // Fund the non-authority account + rpc.airdrop_lamports(&non_authority.pubkey(), 1_000_000_000) + .await + .unwrap(); + + let result = rpc + .create_and_send_transaction(&[create_ix], &payer.pubkey(), &[&payer, &non_authority]) + .await; + + assert!(result.is_err(), "Should fail with wrong authority"); +} + +#[tokio::test] +async fn test_config_creation_requires_signer() { + let config = ProgramTestConfig::new_v2( + true, + Some(vec![("native_compressible", native_compressible::ID)]), + ); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + let non_signer = Keypair::new(); + + // Derive PDAs + let (config_pda, _) = CompressibleConfig::derive_default_pda(&native_compressible::ID); + let (program_data_pda, _) = Pubkey::find_program_address( + &[native_compressible::ID.as_ref()], + &bpf_loader_upgradeable::ID, + ); + + // Try to create config with non-signer as update authority (should fail) + let create_ix_data = CreateConfigInstructionData { + rent_recipient: RENT_RECIPIENT, + address_space: vec![ADDRESS_SPACE], + compression_delay: 100, + }; + + let create_ix = Instruction { + program_id: native_compressible::ID, + accounts: vec![ + AccountMeta::new(payer.pubkey(), true), + AccountMeta::new(config_pda, false), + AccountMeta::new_readonly(non_signer.pubkey(), false), // update_authority (NOT a signer) + AccountMeta::new_readonly(program_data_pda, false), + AccountMeta::new_readonly(solana_sdk::system_program::ID, false), + ], + data: [&[5u8][..], &create_ix_data.try_to_vec().unwrap()[..]].concat(), + }; + + let result = rpc + .create_and_send_transaction(&[create_ix], &payer.pubkey(), &[&payer]) + .await; + + assert!( + result.is_err(), + "Config creation without signer should fail" + ); +} diff --git a/sdk-tests/package.json b/sdk-tests/package.json new file mode 100644 index 0000000000..35b879ef57 --- /dev/null +++ b/sdk-tests/package.json @@ -0,0 +1,29 @@ +{ + "name": "@lightprotocol/sdk-tests", + "version": "0.1.0", + "license": "Apache-2.0", + "scripts": { + "build": "pnpm build-anchor-compressible && pnpm build-anchor-compressible-derived && pnpm build-native-compressible", + "build-anchor-compressible": "cd anchor-compressible/ && cargo build-sbf && cd ..", + "build-anchor-compressible-derived": "cd anchor-compressible-derived/ && cargo build-sbf && cd ..", + "build-native-compressible": "cd native-compressible/ && cargo build-sbf && cd ..", + "test": "RUSTFLAGS=\"-D warnings\" && pnpm test-anchor-compressible && pnpm test-anchor-compressible-derived && pnpm test-native-compressible", + "test-anchor-compressible": "cargo test-sbf -p anchor-compressible", + "test-anchor-compressible-derived": "cargo test-sbf -p anchor-compressible-derived", + "test-native-compressible": "cargo test-sbf -p native-compressible" + }, + "nx": { + "targets": { + "build": { + "outputs": [ + "{workspaceRoot}/target/deploy", + "{workspaceRoot}/target/idl", + "{workspaceRoot}/target/types" + ] + }, + "test": { + "outputs": [] + } + } + } +} diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 8258907b2f..d27c605bfa 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -33,3 +33,4 @@ solana-client = { workspace = true } solana-transaction-status = { workspace = true } light-batched-merkle-tree = { workspace = true } light-registry = { workspace = true } +base64 = { workspace = true } \ No newline at end of file diff --git a/xtask/src/create_batch_state_tree.rs b/xtask/src/create_batch_state_tree.rs index 7afb0b411b..37b691765d 100644 --- a/xtask/src/create_batch_state_tree.rs +++ b/xtask/src/create_batch_state_tree.rs @@ -62,9 +62,6 @@ pub async fn create_batch_state_tree(options: Options) -> anyhow::Result<()> { let mt_keypair = Keypair::new(); let nfq_keypair = Keypair::new(); let cpi_keypair = Keypair::new(); - println!("new mt: {:?}", mt_keypair.pubkey()); - println!("new nfq: {:?}", nfq_keypair.pubkey()); - println!("new cpi: {:?}", cpi_keypair.pubkey()); write_keypair_file(&mt_keypair, format!("./target/mt-{}", mt_keypair.pubkey())).unwrap(); write_keypair_file( @@ -81,12 +78,12 @@ pub async fn create_batch_state_tree(options: Options) -> anyhow::Result<()> { nfq_keypairs.push(nfq_keypair); cpi_keypairs.push(cpi_keypair); } else { - let mt_keypair = read_keypair_file(options.mt_pubkey.unwrap()).unwrap(); - let nfq_keypair = read_keypair_file(options.nfq_pubkey.unwrap()).unwrap(); - let cpi_keypair = read_keypair_file(options.cpi_pubkey.unwrap()).unwrap(); - println!("read mt: {:?}", mt_keypair.pubkey()); - println!("read nfq: {:?}", nfq_keypair.pubkey()); - println!("read cpi: {:?}", cpi_keypair.pubkey()); + let mt_keypair = + read_keypair_file(format!("./target/mt-{}", options.mt_pubkey.unwrap())).unwrap(); + let nfq_keypair = + read_keypair_file(format!("./target/nfq-{}", options.nfq_pubkey.unwrap())).unwrap(); + let cpi_keypair = + read_keypair_file(format!("./target/cpi-{}", options.cpi_pubkey.unwrap())).unwrap(); mt_keypairs.push(mt_keypair); nfq_keypairs.push(nfq_keypair); cpi_keypairs.push(cpi_keypair); @@ -102,7 +99,6 @@ pub async fn create_batch_state_tree(options: Options) -> anyhow::Result<()> { read_keypair_file(keypair_path.clone()) .unwrap_or_else(|_| panic!("Keypair not found in default path {:?}", keypair_path)) }; - println!("read payer: {:?}", payer.pubkey()); let config = if let Some(config) = options.config { if config == "testnet" { diff --git a/xtask/src/new_deployment.rs b/xtask/src/new_deployment.rs index 14d13788e3..73fbcac825 100644 --- a/xtask/src/new_deployment.rs +++ b/xtask/src/new_deployment.rs @@ -310,6 +310,9 @@ pub fn new_testnet_setup() -> TestKeypairs { nullifier_queue_2: Keypair::new(), cpi_context_2: Keypair::new(), group_pda_seed: Keypair::new(), + batched_state_merkle_tree_2: Keypair::new(), + batched_output_queue_2: Keypair::new(), + batched_cpi_context_2: Keypair::new(), } } From 86e30d3e7308bf8bf405b1ffd3b1770aeb6b0869 Mon Sep 17 00:00:00 2001 From: ananas-block Date: Wed, 2 Jul 2025 22:22:48 +0100 Subject: [PATCH 2/2] feat: zero-copy-derive feat: ctoken pinocchio --- .cargo/config.toml | 40 - Cargo.lock | 175 +++- Cargo.toml | 19 +- metadata.md | 242 +++++ program-libs/account-checks/Cargo.toml | 1 + .../src/account_info/pinocchio.rs | 3 + .../account-checks/src/account_iterator.rs | 78 ++ program-libs/account-checks/src/error.rs | 3 + program-libs/account-checks/src/lib.rs | 3 + program-libs/compressed-account/Cargo.toml | 4 +- .../src/compressed_account.rs | 22 +- .../src/instruction_data/compressed_proof.rs | 3 +- .../src/instruction_data/cpi_context.rs | 6 +- .../src/instruction_data/data.rs | 16 +- .../src/instruction_data/invoke_cpi.rs | 4 +- .../src/instruction_data/mod.rs | 1 + .../src/instruction_data/with_readonly.rs | 8 +- .../src/instruction_data/zero_copy_set.rs | 170 ++++ program-libs/compressed-account/src/lib.rs | 23 + program-libs/compressed-account/src/pubkey.rs | 41 +- program-libs/ctoken-types/Cargo.toml | 40 + program-libs/ctoken-types/src/context.rs | 61 ++ program-libs/ctoken-types/src/error.rs | 157 ++++ .../create_associated_token_account.rs | 18 + .../instructions/create_compressed_mint.rs | 103 +++ .../src/instructions/create_spl_mint.rs | 13 + .../instructions/extensions/compressible.rs | 29 + .../extensions/metadata_pointer.rs | 107 +++ .../src/instructions/extensions/mod.rs | 166 ++++ .../instructions/extensions/token_metadata.rs | 91 ++ .../src/instructions/mint_to_compressed.rs | 31 + .../ctoken-types/src/instructions/mod.rs | 7 + .../src/instructions/transfer2.rs | 311 +++++++ program-libs/ctoken-types/src/lib.rs | 28 + .../src/state/extensions/compressible.rs | 85 ++ .../src/state/extensions/extension_struct.rs | 339 +++++++ .../src/state/extensions/extension_type.rs | 111 +++ .../ctoken-types/src/state/extensions/mod.rs | 9 + .../src/state/extensions/token_metadata.rs | 310 +++++++ program-libs/ctoken-types/src/state/mint.rs | 239 +++++ program-libs/ctoken-types/src/state/mod.rs | 9 + .../ctoken-types/src/state/solana_ctoken.rs | 672 ++++++++++++++ .../ctoken-types/src/state/token_data.rs | 149 +++ .../ctoken-types/tests/solana_ctoken.rs | 494 ++++++++++ program-libs/ctoken-types/tests/token_data.rs | 284 ++++++ program-libs/hasher/Cargo.toml | 2 + program-libs/hasher/src/to_byte_array.rs | 30 + program-libs/zero-copy-derive/Cargo.toml | 2 + program-libs/zero-copy-derive/src/lib.rs | 15 +- .../zero-copy-derive/src/shared/utils.rs | 7 + .../zero-copy-derive/src/shared/z_struct.rs | 21 +- .../src/shared/zero_copy_new.rs | 12 +- .../zero-copy-derive/src/zero_copy.rs | 578 ++++++++---- .../tests/cross_crate_copy.rs | 2 +- .../tests/instruction_data.rs | 6 +- program-libs/zero-copy/src/init_mut.rs | 133 +++ .../compressed-token-test/Cargo.toml | 27 +- .../compressed-token-test/tests/account.rs | 435 +++++++++ .../compressed-token-test/tests/mint.rs | 863 ++++++++++++++++++ .../compressed-token-test/tests/test.rs | 8 +- .../create-address-test-program/src/lib.rs | 4 + .../programs/sdk-anchor-test/tests/test.rs | 4 +- .../sdk-pinocchio-test/tests/test.rs | 8 +- program-tests/sdk-test/tests/test.rs | 181 ++++ program-tests/sdk-token-test/CLAUDE.md | 170 ++++ program-tests/sdk-token-test/Cargo.toml | 49 + program-tests/sdk-token-test/Xargo.toml | 2 + program-tests/sdk-token-test/src/lib.rs | 290 ++++++ .../src/process_batch_compress_tokens.rs | 57 ++ .../src/process_compress_full_and_close.rs | 121 +++ .../src/process_compress_tokens.rs | 43 + .../src/process_create_compressed_account.rs | 148 +++ .../src/process_create_escrow_pda.rs | 49 + .../src/process_decompress_tokens.rs | 50 + .../src/process_four_invokes.rs | 196 ++++ .../src/process_four_transfer2.rs | 252 +++++ .../src/process_transfer_tokens.rs | 48 + .../src/process_update_deposit.rs | 306 +++++++ program-tests/sdk-token-test/tests/test.rs | 614 +++++++++++++ .../tests/test_4_invocations.rs | 597 ++++++++++++ .../sdk-token-test/tests/test_4_transfer2.rs | 578 ++++++++++++ .../tests/test_compress_full_and_close.rs | 361 ++++++++ .../sdk-token-test/tests/test_deposit.rs | 483 ++++++++++ program-tests/utils/Cargo.toml | 4 + .../utils/src/assert_close_token_account.rs | 115 +++ .../utils/src/assert_create_token_account.rs | 127 +++ .../utils/src/assert_mint_to_compressed.rs | 192 ++++ program-tests/utils/src/assert_rollover.rs | 1 + program-tests/utils/src/assert_spl_mint.rs | 97 ++ program-tests/utils/src/assert_transfer2.rs | 265 ++++++ program-tests/utils/src/conversions.rs | 17 +- program-tests/utils/src/lib.rs | 6 + program-tests/utils/src/mint_assert.rs | 71 ++ program-tests/utils/src/spl.rs | 36 +- programs/compressed-token/README.md | 13 +- .../compressed-token/{ => anchor}/Cargo.toml | 9 +- programs/compressed-token/anchor/README.md | 13 + .../compressed-token/{ => anchor}/Xargo.toml | 0 .../{ => anchor}/src/batch_compress.rs | 0 .../compressed-token/{ => anchor}/src/burn.rs | 27 +- .../{ => anchor}/src/constants.rs | 4 + .../anchor/src/create_mint.rs | 422 +++++++++ .../{ => anchor}/src/delegation.rs | 26 +- .../{ => anchor}/src/freeze.rs | 64 +- .../{ => anchor}/src/instructions/burn.rs | 0 .../instructions/create_compressed_mint.rs | 48 + .../src/instructions/create_token_pool.rs | 0 .../{ => anchor}/src/instructions/freeze.rs | 0 .../{ => anchor}/src/instructions/generic.rs | 0 .../{ => anchor}/src/instructions/mod.rs | 2 + .../{ => anchor}/src/instructions/transfer.rs | 0 .../compressed-token/{ => anchor}/src/lib.rs | 16 +- .../src/process_compress_spl_token_account.rs | 0 .../{ => anchor}/src/process_mint.rs | 434 ++++++++- .../{ => anchor}/src/process_transfer.rs | 59 +- .../{ => anchor}/src/spl_compression.rs | 0 programs/compressed-token/program/Cargo.toml | 63 ++ programs/compressed-token/program/README.md | 13 + programs/compressed-token/program/Xargo.toml | 2 + .../src/close_token_account/accounts.rs | 32 + .../program/src/close_token_account/mod.rs | 2 + .../src/close_token_account/processor.rs | 100 ++ .../compressed-token/program/src/constants.rs | 5 + .../program/src/convert_account_infos.rs | 62 ++ .../accounts.rs | 70 ++ .../create_associated_token_account/mod.rs | 4 + .../processor.rs | 120 +++ .../program/src/create_spl_mint/accounts.rs | 73 ++ .../program/src/create_spl_mint/mod.rs | 2 + .../program/src/create_spl_mint/processor.rs | 436 +++++++++ .../src/create_token_account/accounts.rs | 27 + .../create_token_account/instruction_data.rs | 12 + .../program/src/create_token_account/mod.rs | 5 + .../src/create_token_account/processor.rs | 38 + .../src/extensions/metadata_pointer.rs | 63 ++ .../program/src/extensions/mod.rs | 83 ++ .../program/src/extensions/processor.rs | 54 ++ .../program/src/extensions/token_metadata.rs | 54 ++ .../src/extensions/token_metadata_ui.rs | 41 + programs/compressed-token/program/src/lib.rs | 139 +++ .../program/src/mint/accounts.rs | 58 ++ .../program/src/mint/mint_input.rs | 87 ++ .../program/src/mint/mint_output.rs | 128 +++ .../compressed-token/program/src/mint/mod.rs | 5 + .../program/src/mint/processor.rs | 105 +++ .../program/src/mint/zero_copy_config.rs | 63 ++ .../src/mint_to_compressed/accounts.rs | 81 ++ .../program/src/mint_to_compressed/mod.rs | 2 + .../src/mint_to_compressed/processor.rs | 262 ++++++ .../program/src/shared/accounts.rs | 101 ++ .../program/src/shared/cpi.rs | 188 ++++ .../program/src/shared/cpi_bytes_size.rs | 146 +++ .../src/shared/initialize_token_account.rs | 80 ++ .../program/src/shared/mint_to_token_pool.rs | 59 ++ .../program/src/shared/mod.rs | 12 + .../program/src/shared/owner_validation.rs | 95 ++ .../program/src/shared/token_input.rs | 77 ++ .../program/src/shared/token_output.rs | 153 ++++ .../program/src/transfer2/accounts.rs | 188 ++++ .../program/src/transfer2/change_account.rs | 90 ++ .../program/src/transfer2/cpi.rs | 40 + .../program/src/transfer2/mod.rs | 8 + .../src/transfer2/native_compression.rs | 108 +++ .../program/src/transfer2/processor.rs | 149 +++ .../program/src/transfer2/sum_check.rs | 121 +++ .../program/src/transfer2/token_inputs.rs | 46 + .../program/src/transfer2/token_outputs.rs | 72 ++ .../program/tests/allocation_test.rs | 143 +++ .../program/tests/exact_allocation_test.rs | 311 +++++++ .../program/tests/metadata_hash.rs | 51 ++ .../program/tests/metadata_pointer.rs | 194 ++++ .../compressed-token/program/tests/mint.rs | 556 +++++++++++ .../program/tests/multi_sum_check.rs | 378 ++++++++ .../program/tests/token_input.rs | 196 ++++ .../program/tests/token_output.rs | 161 ++++ programs/compressed-token/src/token_data.rs | 445 --------- programs/package.json | 2 +- .../system/src/accounts/account_checks.rs | 7 + programs/system/src/invoke_cpi/instruction.rs | 4 - .../src/invoke_cpi/process_cpi_context.rs | 4 +- programs/system/src/invoke_cpi/processor.rs | 1 - scripts/devenv.sh | 1 + sdk-libs/client/src/indexer/indexer_trait.rs | 8 +- sdk-libs/client/src/indexer/mod.rs | 8 +- sdk-libs/client/src/indexer/photon_indexer.rs | 17 +- sdk-libs/client/src/indexer/types.rs | 26 +- sdk-libs/client/src/rpc/client.rs | 10 + sdk-libs/client/src/rpc/indexer.rs | 13 +- sdk-libs/client/src/rpc/rpc_trait.rs | 3 +- sdk-libs/compressed-token-sdk/Cargo.toml | 36 + sdk-libs/compressed-token-sdk/src/account.rs | 209 +++++ sdk-libs/compressed-token-sdk/src/account2.rs | 271 ++++++ sdk-libs/compressed-token-sdk/src/error.rs | 68 ++ .../src/instructions/approve/account_metas.rs | 136 +++ .../src/instructions/approve/instruction.rs | 91 ++ .../src/instructions/approve/mod.rs | 5 + .../batch_compress/account_metas.rs | 183 ++++ .../batch_compress/instruction.rs | 88 ++ .../src/instructions/batch_compress/mod.rs | 5 + .../src/instructions/burn.rs | 40 + .../src/instructions/close.rs | 25 + .../create_associated_token_account.rs | 108 +++ .../create_compressed_mint/account_metas.rs | 130 +++ .../create_compressed_mint/instruction.rs | 112 +++ .../create_compressed_mint/mod.rs | 11 + .../src/instructions/create_spl_mint.rs | 128 +++ .../create_token_account/instruction.rs | 66 ++ .../instructions/create_token_account/mod.rs | 3 + .../src/instructions/ctoken_accounts.rs | 36 + .../src/instructions/mint_to.rs | 43 + .../mint_to_compressed/account_metas.rs | 201 ++++ .../mint_to_compressed/instruction.rs | 125 +++ .../instructions/mint_to_compressed/mod.rs | 10 + .../src/instructions/mod.rs | 32 + .../instructions/transfer/account_infos.rs | 112 +++ .../instructions/transfer/account_metas.rs | 221 +++++ .../src/instructions/transfer/instruction.rs | 282 ++++++ .../src/instructions/transfer/mod.rs | 8 + .../instructions/transfer2/account_metas.rs | 94 ++ .../src/instructions/transfer2/instruction.rs | 196 ++++ .../src/instructions/transfer2/mod.rs | 4 + sdk-libs/compressed-token-sdk/src/lib.rs | 13 + .../compressed-token-sdk/src/token_pool.rs | 21 + sdk-libs/compressed-token-sdk/src/utils.rs | 18 + .../tests/account_metas_test.rs | 129 +++ sdk-libs/compressed-token-types/Cargo.toml | 21 + .../src/account_infos/batch_compress.rs | 192 ++++ .../src/account_infos/burn.rs | 174 ++++ .../src/account_infos/config.rs | 16 + .../account_infos/create_compressed_mint.rs | 143 +++ .../src/account_infos/freeze.rs | 158 ++++ .../src/account_infos/mint_to.rs | 233 +++++ .../src/account_infos/mint_to_compressed.rs | 320 +++++++ .../src/account_infos/mod.rs | 16 + .../src/account_infos/transfer.rs | 285 ++++++ .../compressed-token-types/src/constants.rs | 52 ++ sdk-libs/compressed-token-types/src/error.rs | 35 + .../src/instruction/batch_compress.rs | 13 + .../src/instruction/burn.rs | 15 + .../src/instruction/delegation.rs | 27 + .../src/instruction/freeze.rs | 21 + .../src/instruction/generic.rs | 10 + .../src/instruction/mint_to.rs | 12 + .../src/instruction/mod.rs | 19 + .../src/instruction/transfer.rs | 99 ++ sdk-libs/compressed-token-types/src/lib.rs | 16 + .../compressed-token-types/src/token_data.rs | 25 + .../program-test/src/indexer/test_indexer.rs | 34 +- .../program-test/src/program_test/indexer.rs | 13 +- .../src/program_test/light_program_test.rs | 5 - sdk-libs/program-test/src/program_test/rpc.rs | 20 +- sdk-libs/sdk-types/src/cpi_accounts.rs | 30 +- .../sdk-types/src/instruction/tree_info.rs | 2 +- sdk-libs/sdk/src/cpi/invoke.rs | 8 +- sdk-libs/sdk/src/error.rs | 7 +- sdk-libs/sdk/src/instruction/pack_accounts.rs | 70 +- sdk-libs/token-client/Cargo.toml | 26 + .../token-client/src/actions/create_mint.rs | 56 ++ .../src/actions/create_spl_mint.rs | 67 ++ .../src/actions/mint_to_compressed.rs | 51 ++ sdk-libs/token-client/src/actions/mod.rs | 7 + .../src/actions/transfer2/compress.rs | 72 ++ .../src/actions/transfer2/decompress.rs | 54 ++ .../token-client/src/actions/transfer2/mod.rs | 7 + .../src/actions/transfer2/transfer.rs | 53 ++ .../src/instructions/create_mint.rs | 92 ++ .../src/instructions/create_spl_mint.rs | 105 +++ .../src/instructions/mint_to_compressed.rs | 89 ++ sdk-libs/token-client/src/instructions/mod.rs | 4 + .../src/instructions/transfer2.rs | 291 ++++++ sdk-libs/token-client/src/lib.rs | 2 + 271 files changed, 25421 insertions(+), 1014 deletions(-) delete mode 100644 .cargo/config.toml create mode 100644 metadata.md create mode 100644 program-libs/account-checks/src/account_iterator.rs create mode 100644 program-libs/compressed-account/src/instruction_data/zero_copy_set.rs create mode 100644 program-libs/ctoken-types/Cargo.toml create mode 100644 program-libs/ctoken-types/src/context.rs create mode 100644 program-libs/ctoken-types/src/error.rs create mode 100644 program-libs/ctoken-types/src/instructions/create_associated_token_account.rs create mode 100644 program-libs/ctoken-types/src/instructions/create_compressed_mint.rs create mode 100644 program-libs/ctoken-types/src/instructions/create_spl_mint.rs create mode 100644 program-libs/ctoken-types/src/instructions/extensions/compressible.rs create mode 100644 program-libs/ctoken-types/src/instructions/extensions/metadata_pointer.rs create mode 100644 program-libs/ctoken-types/src/instructions/extensions/mod.rs create mode 100644 program-libs/ctoken-types/src/instructions/extensions/token_metadata.rs create mode 100644 program-libs/ctoken-types/src/instructions/mint_to_compressed.rs create mode 100644 program-libs/ctoken-types/src/instructions/mod.rs create mode 100644 program-libs/ctoken-types/src/instructions/transfer2.rs create mode 100644 program-libs/ctoken-types/src/lib.rs create mode 100644 program-libs/ctoken-types/src/state/extensions/compressible.rs create mode 100644 program-libs/ctoken-types/src/state/extensions/extension_struct.rs create mode 100644 program-libs/ctoken-types/src/state/extensions/extension_type.rs create mode 100644 program-libs/ctoken-types/src/state/extensions/mod.rs create mode 100644 program-libs/ctoken-types/src/state/extensions/token_metadata.rs create mode 100644 program-libs/ctoken-types/src/state/mint.rs create mode 100644 program-libs/ctoken-types/src/state/mod.rs create mode 100644 program-libs/ctoken-types/src/state/solana_ctoken.rs create mode 100644 program-libs/ctoken-types/src/state/token_data.rs create mode 100644 program-libs/ctoken-types/tests/solana_ctoken.rs create mode 100644 program-libs/ctoken-types/tests/token_data.rs create mode 100644 program-tests/compressed-token-test/tests/account.rs create mode 100644 program-tests/compressed-token-test/tests/mint.rs create mode 100644 program-tests/sdk-test/tests/test.rs create mode 100644 program-tests/sdk-token-test/CLAUDE.md create mode 100644 program-tests/sdk-token-test/Cargo.toml create mode 100644 program-tests/sdk-token-test/Xargo.toml create mode 100644 program-tests/sdk-token-test/src/lib.rs create mode 100644 program-tests/sdk-token-test/src/process_batch_compress_tokens.rs create mode 100644 program-tests/sdk-token-test/src/process_compress_full_and_close.rs create mode 100644 program-tests/sdk-token-test/src/process_compress_tokens.rs create mode 100644 program-tests/sdk-token-test/src/process_create_compressed_account.rs create mode 100644 program-tests/sdk-token-test/src/process_create_escrow_pda.rs create mode 100644 program-tests/sdk-token-test/src/process_decompress_tokens.rs create mode 100644 program-tests/sdk-token-test/src/process_four_invokes.rs create mode 100644 program-tests/sdk-token-test/src/process_four_transfer2.rs create mode 100644 program-tests/sdk-token-test/src/process_transfer_tokens.rs create mode 100644 program-tests/sdk-token-test/src/process_update_deposit.rs create mode 100644 program-tests/sdk-token-test/tests/test.rs create mode 100644 program-tests/sdk-token-test/tests/test_4_invocations.rs create mode 100644 program-tests/sdk-token-test/tests/test_4_transfer2.rs create mode 100644 program-tests/sdk-token-test/tests/test_compress_full_and_close.rs create mode 100644 program-tests/sdk-token-test/tests/test_deposit.rs create mode 100644 program-tests/utils/src/assert_close_token_account.rs create mode 100644 program-tests/utils/src/assert_create_token_account.rs create mode 100644 program-tests/utils/src/assert_mint_to_compressed.rs create mode 100644 program-tests/utils/src/assert_spl_mint.rs create mode 100644 program-tests/utils/src/assert_transfer2.rs create mode 100644 program-tests/utils/src/mint_assert.rs rename programs/compressed-token/{ => anchor}/Cargo.toml (86%) create mode 100644 programs/compressed-token/anchor/README.md rename programs/compressed-token/{ => anchor}/Xargo.toml (100%) rename programs/compressed-token/{ => anchor}/src/batch_compress.rs (100%) rename programs/compressed-token/{ => anchor}/src/burn.rs (98%) rename programs/compressed-token/{ => anchor}/src/constants.rs (54%) create mode 100644 programs/compressed-token/anchor/src/create_mint.rs rename programs/compressed-token/{ => anchor}/src/delegation.rs (98%) rename programs/compressed-token/{ => anchor}/src/freeze.rs (92%) rename programs/compressed-token/{ => anchor}/src/instructions/burn.rs (100%) create mode 100644 programs/compressed-token/anchor/src/instructions/create_compressed_mint.rs rename programs/compressed-token/{ => anchor}/src/instructions/create_token_pool.rs (100%) rename programs/compressed-token/{ => anchor}/src/instructions/freeze.rs (100%) rename programs/compressed-token/{ => anchor}/src/instructions/generic.rs (100%) rename programs/compressed-token/{ => anchor}/src/instructions/mod.rs (74%) rename programs/compressed-token/{ => anchor}/src/instructions/transfer.rs (100%) rename programs/compressed-token/{ => anchor}/src/lib.rs (96%) rename programs/compressed-token/{ => anchor}/src/process_compress_spl_token_account.rs (100%) rename programs/compressed-token/{ => anchor}/src/process_mint.rs (58%) rename programs/compressed-token/{ => anchor}/src/process_transfer.rs (95%) rename programs/compressed-token/{ => anchor}/src/spl_compression.rs (100%) create mode 100644 programs/compressed-token/program/Cargo.toml create mode 100644 programs/compressed-token/program/README.md create mode 100644 programs/compressed-token/program/Xargo.toml create mode 100644 programs/compressed-token/program/src/close_token_account/accounts.rs create mode 100644 programs/compressed-token/program/src/close_token_account/mod.rs create mode 100644 programs/compressed-token/program/src/close_token_account/processor.rs create mode 100644 programs/compressed-token/program/src/constants.rs create mode 100644 programs/compressed-token/program/src/convert_account_infos.rs create mode 100644 programs/compressed-token/program/src/create_associated_token_account/accounts.rs create mode 100644 programs/compressed-token/program/src/create_associated_token_account/mod.rs create mode 100644 programs/compressed-token/program/src/create_associated_token_account/processor.rs create mode 100644 programs/compressed-token/program/src/create_spl_mint/accounts.rs create mode 100644 programs/compressed-token/program/src/create_spl_mint/mod.rs create mode 100644 programs/compressed-token/program/src/create_spl_mint/processor.rs create mode 100644 programs/compressed-token/program/src/create_token_account/accounts.rs create mode 100644 programs/compressed-token/program/src/create_token_account/instruction_data.rs create mode 100644 programs/compressed-token/program/src/create_token_account/mod.rs create mode 100644 programs/compressed-token/program/src/create_token_account/processor.rs create mode 100644 programs/compressed-token/program/src/extensions/metadata_pointer.rs create mode 100644 programs/compressed-token/program/src/extensions/mod.rs create mode 100644 programs/compressed-token/program/src/extensions/processor.rs create mode 100644 programs/compressed-token/program/src/extensions/token_metadata.rs create mode 100644 programs/compressed-token/program/src/extensions/token_metadata_ui.rs create mode 100644 programs/compressed-token/program/src/lib.rs create mode 100644 programs/compressed-token/program/src/mint/accounts.rs create mode 100644 programs/compressed-token/program/src/mint/mint_input.rs create mode 100644 programs/compressed-token/program/src/mint/mint_output.rs create mode 100644 programs/compressed-token/program/src/mint/mod.rs create mode 100644 programs/compressed-token/program/src/mint/processor.rs create mode 100644 programs/compressed-token/program/src/mint/zero_copy_config.rs create mode 100644 programs/compressed-token/program/src/mint_to_compressed/accounts.rs create mode 100644 programs/compressed-token/program/src/mint_to_compressed/mod.rs create mode 100644 programs/compressed-token/program/src/mint_to_compressed/processor.rs create mode 100644 programs/compressed-token/program/src/shared/accounts.rs create mode 100644 programs/compressed-token/program/src/shared/cpi.rs create mode 100644 programs/compressed-token/program/src/shared/cpi_bytes_size.rs create mode 100644 programs/compressed-token/program/src/shared/initialize_token_account.rs create mode 100644 programs/compressed-token/program/src/shared/mint_to_token_pool.rs create mode 100644 programs/compressed-token/program/src/shared/mod.rs create mode 100644 programs/compressed-token/program/src/shared/owner_validation.rs create mode 100644 programs/compressed-token/program/src/shared/token_input.rs create mode 100644 programs/compressed-token/program/src/shared/token_output.rs create mode 100644 programs/compressed-token/program/src/transfer2/accounts.rs create mode 100644 programs/compressed-token/program/src/transfer2/change_account.rs create mode 100644 programs/compressed-token/program/src/transfer2/cpi.rs create mode 100644 programs/compressed-token/program/src/transfer2/mod.rs create mode 100644 programs/compressed-token/program/src/transfer2/native_compression.rs create mode 100644 programs/compressed-token/program/src/transfer2/processor.rs create mode 100644 programs/compressed-token/program/src/transfer2/sum_check.rs create mode 100644 programs/compressed-token/program/src/transfer2/token_inputs.rs create mode 100644 programs/compressed-token/program/src/transfer2/token_outputs.rs create mode 100644 programs/compressed-token/program/tests/allocation_test.rs create mode 100644 programs/compressed-token/program/tests/exact_allocation_test.rs create mode 100644 programs/compressed-token/program/tests/metadata_hash.rs create mode 100644 programs/compressed-token/program/tests/metadata_pointer.rs create mode 100644 programs/compressed-token/program/tests/mint.rs create mode 100644 programs/compressed-token/program/tests/multi_sum_check.rs create mode 100644 programs/compressed-token/program/tests/token_input.rs create mode 100644 programs/compressed-token/program/tests/token_output.rs delete mode 100644 programs/compressed-token/src/token_data.rs create mode 100644 sdk-libs/compressed-token-sdk/Cargo.toml create mode 100644 sdk-libs/compressed-token-sdk/src/account.rs create mode 100644 sdk-libs/compressed-token-sdk/src/account2.rs create mode 100644 sdk-libs/compressed-token-sdk/src/error.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/approve/account_metas.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/approve/instruction.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/approve/mod.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/batch_compress/account_metas.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/batch_compress/instruction.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/batch_compress/mod.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/burn.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/close.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/create_associated_token_account.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/create_compressed_mint/account_metas.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/create_compressed_mint/instruction.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/create_compressed_mint/mod.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/create_spl_mint.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/create_token_account/instruction.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/create_token_account/mod.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/ctoken_accounts.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/mint_to.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/mint_to_compressed/account_metas.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/mint_to_compressed/instruction.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/mint_to_compressed/mod.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/mod.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/transfer/account_infos.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/transfer/account_metas.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/transfer/instruction.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/transfer/mod.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/transfer2/account_metas.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/transfer2/instruction.rs create mode 100644 sdk-libs/compressed-token-sdk/src/instructions/transfer2/mod.rs create mode 100644 sdk-libs/compressed-token-sdk/src/lib.rs create mode 100644 sdk-libs/compressed-token-sdk/src/token_pool.rs create mode 100644 sdk-libs/compressed-token-sdk/src/utils.rs create mode 100644 sdk-libs/compressed-token-sdk/tests/account_metas_test.rs create mode 100644 sdk-libs/compressed-token-types/Cargo.toml create mode 100644 sdk-libs/compressed-token-types/src/account_infos/batch_compress.rs create mode 100644 sdk-libs/compressed-token-types/src/account_infos/burn.rs create mode 100644 sdk-libs/compressed-token-types/src/account_infos/config.rs create mode 100644 sdk-libs/compressed-token-types/src/account_infos/create_compressed_mint.rs create mode 100644 sdk-libs/compressed-token-types/src/account_infos/freeze.rs create mode 100644 sdk-libs/compressed-token-types/src/account_infos/mint_to.rs create mode 100644 sdk-libs/compressed-token-types/src/account_infos/mint_to_compressed.rs create mode 100644 sdk-libs/compressed-token-types/src/account_infos/mod.rs create mode 100644 sdk-libs/compressed-token-types/src/account_infos/transfer.rs create mode 100644 sdk-libs/compressed-token-types/src/constants.rs create mode 100644 sdk-libs/compressed-token-types/src/error.rs create mode 100644 sdk-libs/compressed-token-types/src/instruction/batch_compress.rs create mode 100644 sdk-libs/compressed-token-types/src/instruction/burn.rs create mode 100644 sdk-libs/compressed-token-types/src/instruction/delegation.rs create mode 100644 sdk-libs/compressed-token-types/src/instruction/freeze.rs create mode 100644 sdk-libs/compressed-token-types/src/instruction/generic.rs create mode 100644 sdk-libs/compressed-token-types/src/instruction/mint_to.rs create mode 100644 sdk-libs/compressed-token-types/src/instruction/mod.rs create mode 100644 sdk-libs/compressed-token-types/src/instruction/transfer.rs create mode 100644 sdk-libs/compressed-token-types/src/lib.rs create mode 100644 sdk-libs/compressed-token-types/src/token_data.rs create mode 100644 sdk-libs/token-client/Cargo.toml create mode 100644 sdk-libs/token-client/src/actions/create_mint.rs create mode 100644 sdk-libs/token-client/src/actions/create_spl_mint.rs create mode 100644 sdk-libs/token-client/src/actions/mint_to_compressed.rs create mode 100644 sdk-libs/token-client/src/actions/mod.rs create mode 100644 sdk-libs/token-client/src/actions/transfer2/compress.rs create mode 100644 sdk-libs/token-client/src/actions/transfer2/decompress.rs create mode 100644 sdk-libs/token-client/src/actions/transfer2/mod.rs create mode 100644 sdk-libs/token-client/src/actions/transfer2/transfer.rs create mode 100644 sdk-libs/token-client/src/instructions/create_mint.rs create mode 100644 sdk-libs/token-client/src/instructions/create_spl_mint.rs create mode 100644 sdk-libs/token-client/src/instructions/mint_to_compressed.rs create mode 100644 sdk-libs/token-client/src/instructions/mod.rs create mode 100644 sdk-libs/token-client/src/instructions/transfer2.rs create mode 100644 sdk-libs/token-client/src/lib.rs diff --git a/.cargo/config.toml b/.cargo/config.toml deleted file mode 100644 index 57679c16a4..0000000000 --- a/.cargo/config.toml +++ /dev/null @@ -1,40 +0,0 @@ -[alias] -xtask = "run --package xtask --" - -# On Windows -# ``` -# cargo install -f cargo-binutils -# rustup component add llvm-tools-preview -# ``` -[target.x86_64-pc-windows-msvc] -rustflags = ["-C", "link-arg=-fuse-ld=lld"] - -[target.x86_64-pc-windows-gnu] -rustflags = ["-C", "link-arg=-fuse-ld=lld"] - -# On Linux: -# - Ubuntu, `sudo apt-get install lld clang` -# - Arch, `sudo pacman -S lld clang` -[target.x86_64-unknown-linux-gnu] -rustflags = ["-C", "linker=clang", "-C", "link-arg=-fuse-ld=lld"] - -[target.aarch64-unknown-linux-gnu] -rustflags = ["-C", "linker=clang", "-C", "link-arg=-fuse-ld=lld"] - -[target.x86_64-unknown-linux-musl] -rustflags = ["-C", "linker=clang", "-C", "link-arg=-fuse-ld=lld"] - -[target.aarch64-unknown-linux-musl] -rustflags = ["-C", "linker=clang", "-C", "link-arg=-fuse-ld=lld"] - -# On MacOS, `brew install llvm` and follow steps in `brew info llvm` -[target.x86_64-apple-darwin] -rustflags = ["-C", "link-arg=-fuse-ld=lld"] - -[target.aarch64-apple-darwin] -rustflags = ["-C", "link-arg=-fuse-ld=lld"] - - - - - diff --git a/Cargo.lock b/Cargo.lock index 5d1f0bf365..f88593d824 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -290,6 +290,26 @@ dependencies = [ "solana-program", "solana-sdk", "tokio", + +name = "anchor-compressed-token" +version = "2.0.0" +dependencies = [ + "account-compression", + "anchor-lang", + "anchor-spl", + "light-compressed-account", + "light-ctoken-types", + "light-hasher", + "light-heap", + "light-system-program-anchor", + "light-zero-copy", + "num-bigint 0.4.6", + "rand 0.8.5", + "solana-sdk", + "solana-security-txt", + "spl-token", + "spl-token-2022 7.0.0", + "zerocopy", ] [[package]] @@ -1377,16 +1397,21 @@ dependencies = [ "light-client", "light-compressed-account", "light-compressed-token", + "light-compressed-token-sdk", + "light-ctoken-types", "light-program-test", "light-prover-client", "light-registry", "light-sdk", "light-system-program-anchor", "light-test-utils", + "light-token-client", "light-verifier", + "light-zero-copy", "rand 0.8.5", "serial_test", "solana-sdk", + "spl-pod", "spl-token", "tokio", ] @@ -3300,6 +3325,7 @@ dependencies = [ "pinocchio", "rand 0.8.5", "solana-account-info", + "solana-msg", "solana-program-error", "solana-pubkey", "solana-sysvar", @@ -3424,6 +3450,7 @@ dependencies = [ "num-bigint 0.4.6", "pinocchio", "rand 0.8.5", + "solana-msg", "solana-program-error", "solana-pubkey", "thiserror 2.0.12", @@ -3435,17 +3462,26 @@ name = "light-compressed-token" version = "2.0.0" dependencies = [ "account-compression", + "anchor-compressed-token", "anchor-lang", - "anchor-spl", + "arrayvec", + "borsh 0.10.4", + "light-account-checks", "light-compressed-account", + "light-ctoken-types", "light-hasher", "light-heap", + "light-sdk", + "light-sdk-pinocchio", + "light-sdk-types", "light-system-program-anchor", "light-zero-copy", "num-bigint 0.4.6", + "pinocchio", "rand 0.8.5", - "solana-sdk", + "solana-pubkey", "solana-security-txt", + "spl-pod", "spl-token", "spl-token-2022 7.0.0", "zerocopy", @@ -3461,6 +3497,44 @@ dependencies = [ "light-sdk", "solana-instruction", "solana-pubkey", +] + +[[package]] +name = "light-compressed-token-sdk" +version = "0.1.0" +dependencies = [ + "anchor-lang", + "arrayvec", + "borsh 0.10.4", + "light-account-checks", + "light-compressed-account", + "light-compressed-token", + "light-compressed-token-types", + "light-ctoken-types", + "light-macros", + "light-sdk", + "solana-account-info", + "solana-cpi", + "solana-instruction", + "solana-msg", + "solana-program-error", + "solana-pubkey", + "spl-pod", + "spl-token-2022 7.0.0", + "thiserror 2.0.12", +] + +[[package]] +name = "light-compressed-token-types" +version = "0.1.0" +dependencies = [ + "anchor-lang", + "borsh 0.10.4", + "light-account-checks", + "light-compressed-account", + "light-macros", + "light-sdk-types", + "solana-msg", "thiserror 2.0.12", ] @@ -3485,6 +3559,30 @@ dependencies = [ "tokio", ] +[[package]] +name = "light-ctoken-types" +version = "0.1.0" +dependencies = [ + "anchor-lang", + "arrayvec", + "borsh 0.10.4", + "light-compressed-account", + "light-hasher", + "light-macros", + "light-zero-copy", + "num-bigint 0.4.6", + "pinocchio", + "rand 0.8.5", + "solana-msg", + "solana-program-error", + "solana-pubkey", + "solana-sysvar", + "spl-pod", + "spl-token-2022 7.0.0", + "thiserror 2.0.12", + "zerocopy", +] + [[package]] name = "light-hash-set" version = "2.1.0" @@ -3517,6 +3615,7 @@ dependencies = [ "solana-program-error", "solana-pubkey", "thiserror 2.0.12", + "zerocopy", ] [[package]] @@ -3841,7 +3940,9 @@ dependencies = [ "light-client", "light-compressed-account", "light-compressed-token", + "light-compressed-token-sdk", "light-concurrent-merkle-tree", + "light-ctoken-types", "light-hasher", "light-indexed-array", "light-indexed-merkle-tree", @@ -3853,6 +3954,8 @@ dependencies = [ "light-sdk", "light-sparse-merkle-tree", "light-system-program-anchor", + "light-token-client", + "light-zero-copy", "log", "num-bigint 0.4.6", "num-traits", @@ -3865,6 +3968,27 @@ dependencies = [ "thiserror 2.0.12", ] +[[package]] +name = "light-token-client" +version = "0.1.0" +dependencies = [ + "borsh 0.10.4", + "light-client", + "light-compressed-account", + "light-compressed-token-sdk", + "light-compressed-token-types", + "light-ctoken-types", + "light-sdk", + "solana-instruction", + "solana-keypair", + "solana-msg", + "solana-pubkey", + "solana-signature", + "solana-signer", + "spl-pod", + "spl-token-2022 7.0.0", +] + [[package]] name = "light-verifier" version = "2.1.0" @@ -3896,6 +4020,8 @@ version = "0.1.0" dependencies = [ "borsh 0.10.4", "lazy_static", + "light-hasher", + "light-sdk-macros", "light-zero-copy", "proc-macro2", "quote", @@ -5535,6 +5661,45 @@ dependencies = [ "tokio", ] +[[package]] +name = "sdk-test" +version = "1.0.0" +dependencies = [ + "borsh 0.10.4", + "light-compressed-account", + "light-hasher", + "light-macros", + "light-program-test", + "light-sdk", + "light-sdk-types", + "solana-program", + "solana-sdk", + "tokio", +] + +[[package]] +name = "sdk-token-test" +version = "1.0.0" +dependencies = [ + "anchor-lang", + "anchor-spl", + "arrayvec", + "light-batched-merkle-tree", + "light-client", + "light-compressed-account", + "light-compressed-token-sdk", + "light-ctoken-types", + "light-hasher", + "light-program-test", + "light-sdk", + "light-sdk-types", + "light-test-utils", + "light-token-client", + "serial_test", + "solana-sdk", + "tokio", +] + [[package]] name = "security-framework" version = "2.11.1" @@ -9799,9 +9964,9 @@ checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" [[package]] name = "trybuild" -version = "1.0.106" +version = "1.0.105" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65af40ad689f2527aebbd37a0a816aea88ff5f774ceabe99de5be02f2f91dae2" +checksum = "1c9bf9513a2f4aeef5fdac8677d7d349c79fdbcc03b9c86da6e9d254f1e43be2" dependencies = [ "glob", "serde", @@ -9809,7 +9974,7 @@ dependencies = [ "serde_json", "target-triple", "termcolor", - "toml 0.9.2", + "toml 0.8.23", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index cce2fc0ea1..34e0477d57 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,9 +14,11 @@ members = [ "program-libs/indexed-merkle-tree", "program-libs/indexed-array", "program-libs/zero-copy-derive", + "program-libs/ctoken-types", "programs/account-compression", "programs/system", - "programs/compressed-token", + "programs/compressed-token/program", + "programs/compressed-token/anchor", "programs/registry", "anchor-programs/system", "sdk-libs/client", @@ -27,6 +29,9 @@ members = [ "sdk-libs/sdk-types", "sdk-libs/photon-api", "sdk-libs/program-test", + "sdk-libs/compressed-token-types", + "sdk-libs/compressed-token-sdk", + "sdk-libs/token-client", "xtask", "program-tests/account-compression-test", "program-tests/compressed-token-test", @@ -36,6 +41,7 @@ members = [ "program-tests/system-cpi-v2-test", "program-tests/system-test", "program-tests/sdk-anchor-test/programs/sdk-anchor-test", + "program-tests/sdk-token-test", "program-tests/sdk-pinocchio-test", "program-tests/create-address-test-program", "program-tests/utils", @@ -59,6 +65,10 @@ strip = "none" [profile.release] overflow-checks = true +[workspace.package] +version = "0.1.0" +edition = "2021" + [workspace.dependencies] solana-banks-client = { version = "2.2" } solana-banks-interface = { version = "2.2" } @@ -105,6 +115,7 @@ solana-system-interface = { version = "1" } solana-security-txt = "1.1.1" spl-token = "7.0.0" spl-token-2022 = { version = "7", features = ["no-entrypoint"] } +spl-pod = "0.5.1" pinocchio = { version = "0.8.4" } bs58 = "^0.5.1" litesvm = "0.6.1" @@ -169,14 +180,18 @@ light-account-checks = { path = "program-libs/account-checks", version = "0.3.0" light-verifier = { path = "program-libs/verifier", version = "2.1.0" } light-zero-copy = { path = "program-libs/zero-copy", version = "0.2.0" } light-zero-copy-derive = { path = "program-libs/zero-copy-derive", version = "0.1.0" } +light-ctoken-types = { path = "program-libs/ctoken-types", version = "0.1.0" } photon-api = { path = "sdk-libs/photon-api", version = "0.51.0" } forester-utils = { path = "forester-utils", version = "2.0.0" } account-compression = { path = "programs/account-compression", version = "2.0.0", features = [ "cpi", ] } -light-compressed-token = { path = "programs/compressed-token", version = "2.0.0", features = [ +light-compressed-token = { path = "programs/compressed-token/program", version = "2.0.0", features = [ "cpi", ] } +light-compressed-token-types = { path = "sdk-libs/compressed-token-types", name = "light-compressed-token-types" } +light-compressed-token-sdk = { path = "sdk-libs/compressed-token-sdk" } +light-token-client = { path = "sdk-libs/token-client" } light-system-program-anchor = { path = "anchor-programs/system", version = "2.0.0", features = [ "cpi", ] } diff --git a/metadata.md b/metadata.md new file mode 100644 index 0000000000..520a17636f --- /dev/null +++ b/metadata.md @@ -0,0 +1,242 @@ +# Token 2022 Metadata Pointer Extension Analysis + +## Overview +The Token 2022 metadata pointer extension provides a mechanism for SPL Token 2022 mints to reference metadata accounts using a **Type-Length-Value (TLV)** encoding system. This allows metadata to be stored either directly in the mint account or pointed to external metadata accounts. + +## Core Architecture + +### 1. MetadataPointer Extension Structure +```rust +#[repr(C)] +#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)] +pub struct MetadataPointer { + /// Authority that can set the metadata address + pub authority: OptionalNonZeroPubkey, + /// Account address that holds the metadata + pub metadata_address: OptionalNonZeroPubkey, +} +``` + +### 2. TLV Extension System +Extensions are stored using TLV format: +- **Type**: 2 bytes (ExtensionType enum) +- **Length**: 2 bytes (data length) +- **Value**: Variable length data + +Account layout: +``` +[Base Mint: 82 bytes][Padding: 83 bytes][Account Type: 1 byte][TLV Extensions...] +``` + +### 3. Extension Types +- `MetadataPointer`: Points to metadata account +- `TokenMetadata`: Contains metadata directly +- Extensions are parsed sequentially through TLV data + +## Token 2022 Metadata Account Structure + +The account that a `MetadataPointer` points to contains the actual `TokenMetadata` stored in a **TLV (Type-Length-Value)** format. Here's the detailed structure: + +### Account Layout + +``` +┌─────────────────────────────────────────────────────────────────┐ +│ Complete Account Structure │ +├─────────────────────────────────────────────────────────────────┤ +│ Base Mint Data (82 bytes) │ +│ ┌─ supply: u64 │ +│ ├─ decimals: u8 │ +│ ├─ is_initialized: bool │ +│ ├─ freeze_authority: Option │ +│ └─ mint_authority: Option │ +├─────────────────────────────────────────────────────────────────┤ +│ Extension Data (Variable Length) │ +│ │ +│ ┌─ MetadataPointer Extension (TLV Entry) │ +│ │ ├─ Type: ExtensionType::MetadataPointer (2 bytes) │ +│ │ ├─ Length: 64 (4 bytes) │ +│ │ └─ Value: MetadataPointer struct (64 bytes) │ +│ │ ├─ authority: OptionalNonZeroPubkey (32 bytes) │ +│ │ └─ metadata_address: OptionalNonZeroPubkey (32 bytes) │ +│ │ │ +│ └─ TokenMetadata Extension (TLV Entry) │ +│ ├─ Type: ExtensionType::TokenMetadata (2 bytes) │ +│ ├─ Length: Variable (4 bytes) │ +│ └─ Value: Borsh-serialized TokenMetadata │ +│ ├─ update_authority: OptionalNonZeroPubkey (32 bytes) │ +│ ├─ mint: Pubkey (32 bytes) │ +│ ├─ name: String (4 bytes length + data) │ +│ ├─ symbol: String (4 bytes length + data) │ +│ ├─ uri: String (4 bytes length + data) │ +│ └─ additional_metadata: Vec<(String, String)> │ +│ └─ (4 bytes count + entries) │ +└─────────────────────────────────────────────────────────────────┘ +``` + +### TokenMetadata Structure Details + +```rust +#[derive(Clone, Debug, Default, PartialEq, BorshDeserialize, BorshSerialize)] +pub struct TokenMetadata { + /// Authority that can update the metadata + pub update_authority: OptionalNonZeroPubkey, + /// Associated mint (prevents spoofing) + pub mint: Pubkey, + /// Token name (e.g., "Solana Token") + pub name: String, + /// Token symbol (e.g., "SOL") + pub symbol: String, + /// URI to external metadata JSON + pub uri: String, + /// Additional key-value pairs + pub additional_metadata: Vec<(String, String)>, +} +``` + +### Two Storage Patterns + +#### Pattern 1: Self-Referential (Common) +``` +Mint Account (Same Account) +├─ MetadataPointer Extension +│ └─ metadata_address: [points to same account] +└─ TokenMetadata Extension + └─ [actual metadata data] +``` + +#### Pattern 2: External Account +``` +Mint Account External Metadata Account +├─ MetadataPointer Extension ├─ TokenMetadata Extension +│ └─ metadata_address ────────→│ └─ [actual metadata data] +└─ [no TokenMetadata] └─ [account owned by token program] +``` + +### Serialization Format + +The `TokenMetadata` is serialized using **Borsh** format: +- **Discriminator**: `[112, 132, 90, 90, 11, 88, 157, 87]` (not stored in account) +- **Variable Length**: Strings and Vec fields make the size dynamic +- **TLV Wrapper**: Type + Length headers allow efficient parsing + +## Key Functions + +### Metadata Creation Process +1. **Initialize MetadataPointer**: Set authority and metadata address +2. **Create/Update Metadata**: Store metadata in referenced account +3. **Authority Validation**: Ensure proper permissions for updates + +### Extension Parsing +- Sequential TLV parsing using `get_tlv_indices()` +- Type-based lookup for specific extensions +- Support for both fixed-size (Pod) and variable-length extensions + +## Integration with Compressed Token Mint + +### Current Implementation Analysis +Your compressed token mint in `programs/compressed-token/program/src/mint/state.rs`: + +```rust +pub struct CompressedMint { + pub spl_mint: Pubkey, + pub supply: u64, + pub decimals: u8, + pub is_decompressed: bool, + pub mint_authority: Option, + pub freeze_authority: Option, + pub num_extensions: u8, // ← Already supports extensions! +} +``` + +### Integration Recommendations + +#### 1. **Extension Data Structure** +Add metadata pointer extension to your compressed mint: + +```rust +#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)] +pub struct CompressedMintMetadataPointer { + pub authority: Option, + pub metadata_address: Option, +} + +// Add to extension system +pub enum CompressedMintExtension { + MetadataPointer(CompressedMintMetadataPointer), + // Other extensions... +} +``` + +#### 2. **Hashing Integration** +The metadata pointer would need to be included in the hash calculation: + +```rust +// In hash_with_hashed_values, add metadata pointer handling +if let Some(metadata_pointer) = metadata_pointer_extension { + // Hash metadata pointer data + let metadata_pointer_bytes = [0u8; 32]; + // Set prefix for metadata pointer + metadata_pointer_bytes[30] = 4; // metadata_pointer prefix + // Include in hash_inputs +} +``` + +#### 3. **Processing Integration** +Update `process_create_compressed_mint` to handle metadata pointer: + +```rust +// In processor.rs, add metadata pointer initialization +if let Some(metadata_pointer_data) = parsed_instruction_data.metadata_pointer { + // Validate metadata pointer authority + // Set metadata address + // Update num_extensions count +} +``` + +### Key Considerations + +#### 1. **Compression-Specific Challenges** +- **Hash State**: Metadata pointer must be included in compressed account hash +- **Proof Generation**: Changes to metadata pointer affect merkle tree proofs +- **Extension Counting**: `num_extensions` field needs proper management + +#### 2. **Authority Model** +- Metadata pointer authority separate from mint authority +- Authority validation needed for metadata updates +- Consider compressed account ownership model + +#### 3. **Storage Efficiency** +- Compressed accounts store data efficiently +- Metadata pointer adds minimal overhead (64 bytes) +- Consider storing metadata directly vs. pointer for small metadata + +### Implementation Steps + +1. **Define Extension Types**: Create compressed mint extension enum +2. **Update State Structure**: Add extension parsing to CompressedMint +3. **Modify Hash Function**: Include extensions in hash calculation +4. **Update Instructions**: Add metadata pointer initialization/update +5. **Authority Validation**: Implement permission checks +6. **Testing**: Ensure compatibility with existing compressed token functionality + +## Account Reading Process + +```rust +// 1. Load account data +let buffer = account_info.try_borrow_data()?; + +// 2. Parse as mint with extensions +let mint = PodStateWithExtensions::::unpack(&buffer)?; + +// 3. Get metadata pointer +let metadata_pointer = mint.get_extension::()?; + +// 4. If self-referential, read metadata from same account +if metadata_pointer.metadata_address == Some(mint_pubkey) { + let metadata = mint.get_variable_len_extension::()?; +} +``` + +## Summary + +The Token 2022 metadata pointer extension is well-designed for integration with compressed tokens, requiring mainly adaptation of the TLV parsing logic and hash computation for the compressed account model. The metadata account structure is designed for flexibility, allowing metadata to be stored either directly in the mint account or in a separate dedicated account, while maintaining efficient TLV parsing and Borsh serialization. \ No newline at end of file diff --git a/program-libs/account-checks/Cargo.toml b/program-libs/account-checks/Cargo.toml index 9ab6edaf1a..3f77092681 100644 --- a/program-libs/account-checks/Cargo.toml +++ b/program-libs/account-checks/Cargo.toml @@ -25,6 +25,7 @@ solana-pubkey = { workspace = true, optional = true, features = [ "curve25519", "sha2", ] } +solana-msg = { workspace = true } pinocchio = { workspace = true, optional = true } thiserror = { workspace = true } rand = { workspace = true, optional = true } diff --git a/program-libs/account-checks/src/account_info/pinocchio.rs b/program-libs/account-checks/src/account_info/pinocchio.rs index 2b4f6ff2a9..b6b7c83134 100644 --- a/program-libs/account-checks/src/account_info/pinocchio.rs +++ b/program-libs/account-checks/src/account_info/pinocchio.rs @@ -19,14 +19,17 @@ impl AccountInfoTrait for pinocchio::account_info::AccountInfo { bytes } + #[inline(always)] fn is_writable(&self) -> bool { self.is_writable() } + #[inline(always)] fn is_signer(&self) -> bool { self.is_signer() } + #[inline(always)] fn executable(&self) -> bool { self.executable() } diff --git a/program-libs/account-checks/src/account_iterator.rs b/program-libs/account-checks/src/account_iterator.rs new file mode 100644 index 0000000000..55e6190491 --- /dev/null +++ b/program-libs/account-checks/src/account_iterator.rs @@ -0,0 +1,78 @@ +use std::panic::Location; + +use crate::{AccountError, AccountInfoTrait}; + +/// Iterator over accounts that provides detailed error messages when accounts are missing. +/// +/// This iterator helps with debugging account setup issues by tracking which accounts +/// are requested and providing clear error messages when there are insufficient accounts. +pub struct AccountIterator<'info, T: AccountInfoTrait> { + accounts: &'info [T], + position: usize, +} + +impl<'info, T: AccountInfoTrait> AccountIterator<'info, T> { + /// Create a new AccountIterator from a slice of AccountInfo. + pub fn new(accounts: &'info [T]) -> Self { + Self { + accounts, + position: 0, + } + } + + /// Get the next account with a descriptive name. + /// + /// # Arguments + /// * `account_name` - A descriptive name for the account being requested (for debugging) + /// + /// # Returns + /// * `Ok(&T)` - The next account in the iterator + /// * `Err(AccountError::NotEnoughAccountKeys)` - If no more accounts are available + #[track_caller] + pub fn next_account(&mut self, account_name: &str) -> Result<&'info T, AccountError> { + let location = Location::caller(); + + if self.position >= self.accounts.len() { + solana_msg::msg!( + "ERROR: Not enough accounts. Requested '{}' at index {} but only {} accounts available. {}:{}:{}", + account_name, self.position, self.accounts.len(), location.file(), location.line(), location.column() + ); + return Err(AccountError::NotEnoughAccountKeys); + } + + let account = &self.accounts[self.position]; + self.position += 1; + + Ok(account) + } + + /// Get all remaining accounts in the iterator. + #[track_caller] + pub fn remaining(&self) -> Result<&'info [T], AccountError> { + let location = Location::caller(); + if self.position >= self.accounts.len() { + let account_name = "remaining accounts"; + solana_msg::msg!( + "ERROR: Not enough accounts. Requested '{}' at index {} but only {} accounts available. {}:{}:{}", + account_name, self.position, self.accounts.len(), location.file(), location.line(), location.column() + ); + return Err(AccountError::NotEnoughAccountKeys); + } + Ok(&self.accounts[self.position..]) + } + + /// Get the current position in the iterator. + pub fn position(&self) -> usize { + self.position + } + + /// Get the total number of accounts. + pub fn len(&self) -> usize { + self.accounts.len() + } + + /// Check if the iterator is empty. + pub fn is_empty(&self) -> bool { + self.accounts.is_empty() + } +} diff --git a/program-libs/account-checks/src/error.rs b/program-libs/account-checks/src/error.rs index 82b2fe0fc2..ccc477c6d9 100644 --- a/program-libs/account-checks/src/error.rs +++ b/program-libs/account-checks/src/error.rs @@ -30,6 +30,8 @@ pub enum AccountError { ProgramNotExecutable, #[error("Account not zeroed.")] AccountNotZeroed, + #[error("Not enough account keys provided.")] + NotEnoughAccountKeys, #[error("Pinocchio program error with code: {0}")] PinocchioProgramError(u32), } @@ -52,6 +54,7 @@ impl From for u32 { AccountError::InvalidProgramId => 12017, AccountError::ProgramNotExecutable => 12018, AccountError::AccountNotZeroed => 12019, + AccountError::NotEnoughAccountKeys => 12020, AccountError::PinocchioProgramError(code) => code, } } diff --git a/program-libs/account-checks/src/lib.rs b/program-libs/account-checks/src/lib.rs index 1a45262277..3430f064cc 100644 --- a/program-libs/account-checks/src/lib.rs +++ b/program-libs/account-checks/src/lib.rs @@ -1,6 +1,9 @@ pub mod account_info; +pub mod account_iterator; pub mod checks; pub mod discriminator; pub mod error; pub use account_info::account_info_trait::AccountInfoTrait; +pub use account_iterator::AccountIterator; +pub use error::AccountError; diff --git a/program-libs/compressed-account/Cargo.toml b/program-libs/compressed-account/Cargo.toml index 8623b20991..bfac7652e2 100644 --- a/program-libs/compressed-account/Cargo.toml +++ b/program-libs/compressed-account/Cargo.toml @@ -18,11 +18,11 @@ new-unique = ["dep:solana-pubkey"] thiserror = { workspace = true } zerocopy = { workspace = true, features = ["derive"] } light-hasher = { workspace = true } -light-zero-copy = { workspace = true, features = ["std"] } +light-zero-copy = { workspace = true, features = ["std", "mut", "derive"] } light-macros = { workspace = true } pinocchio = { workspace = true, optional = true } solana-program-error = { workspace = true, optional = true } - +solana-msg = { workspace = true } # Feature-gated dependencies anchor-lang = { workspace = true, optional = true } bytemuck = { workspace = true, optional = true, features = ["derive"] } diff --git a/program-libs/compressed-account/src/compressed_account.rs b/program-libs/compressed-account/src/compressed_account.rs index 62159d135d..64e476e6a9 100644 --- a/program-libs/compressed-account/src/compressed_account.rs +++ b/program-libs/compressed-account/src/compressed_account.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use light_hasher::{Hasher, Poseidon}; +use light_zero_copy::{ZeroCopy, ZeroCopyMut}; use crate::{ address::pack_account, @@ -11,7 +12,7 @@ use crate::{ AnchorDeserialize, AnchorSerialize, CompressedAccountError, Pubkey, TreeType, }; -#[derive(Debug, PartialEq, Default, Clone, AnchorSerialize, AnchorDeserialize)] +#[derive(Debug, PartialEq, Default, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopyMut)] pub struct PackedCompressedAccountWithMerkleContext { pub compressed_account: CompressedAccount, pub merkle_context: PackedMerkleContext, @@ -133,7 +134,7 @@ pub struct ReadOnlyCompressedAccount { pub root_index: u16, } -#[derive(Debug, PartialEq, Default, Clone, AnchorSerialize, AnchorDeserialize)] +#[derive(Debug, PartialEq, Default, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopyMut)] pub struct PackedReadOnlyCompressedAccount { pub account_hash: [u8; 32], pub merkle_context: PackedMerkleContext, @@ -149,7 +150,17 @@ pub struct MerkleContext { pub tree_type: TreeType, } -#[derive(Debug, Clone, Copy, AnchorSerialize, AnchorDeserialize, PartialEq, Default)] +#[derive( + Debug, + Clone, + Copy, + AnchorSerialize, + AnchorDeserialize, + PartialEq, + Default, + ZeroCopy, + ZeroCopyMut, +)] pub struct PackedMerkleContext { pub merkle_tree_pubkey_index: u8, pub queue_pubkey_index: u8, @@ -217,7 +228,7 @@ pub fn pack_merkle_context( .collect::>() } -#[derive(Debug, PartialEq, Default, Clone, AnchorSerialize, AnchorDeserialize)] +#[derive(Debug, PartialEq, Default, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopyMut)] pub struct CompressedAccount { pub owner: Pubkey, pub lamports: u64, @@ -234,7 +245,7 @@ pub struct InCompressedAccount { pub address: Option<[u8; 32]>, } -#[derive(Debug, PartialEq, Default, Clone, AnchorSerialize, AnchorDeserialize)] +#[derive(Debug, PartialEq, Default, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopyMut)] pub struct CompressedAccountData { pub discriminator: [u8; 8], pub data: Vec, @@ -295,7 +306,6 @@ pub fn hash_with_hashed_values( vec.push(&discriminator_bytes); vec.push(data_hash); } - Ok(Poseidon::hashv(&vec)?) } diff --git a/program-libs/compressed-account/src/instruction_data/compressed_proof.rs b/program-libs/compressed-account/src/instruction_data/compressed_proof.rs index 9c79f9ca24..d5c69381d8 100644 --- a/program-libs/compressed-account/src/instruction_data/compressed_proof.rs +++ b/program-libs/compressed-account/src/instruction_data/compressed_proof.rs @@ -1,4 +1,4 @@ -use light_zero_copy::{borsh::Deserialize, errors::ZeroCopyError}; +use light_zero_copy::{borsh::Deserialize, errors::ZeroCopyError, ZeroCopyMut}; use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Ref, Unaligned}; use crate::{AnchorDeserialize, AnchorSerialize}; @@ -17,6 +17,7 @@ use crate::{AnchorDeserialize, AnchorSerialize}; FromBytes, IntoBytes, Unaligned, + ZeroCopyMut, )] pub struct CompressedProof { pub a: [u8; 32], diff --git a/program-libs/compressed-account/src/instruction_data/cpi_context.rs b/program-libs/compressed-account/src/instruction_data/cpi_context.rs index d91a4e11bb..05d9306559 100644 --- a/program-libs/compressed-account/src/instruction_data/cpi_context.rs +++ b/program-libs/compressed-account/src/instruction_data/cpi_context.rs @@ -1,6 +1,10 @@ +use light_zero_copy::ZeroCopyMut; + use crate::{AnchorDeserialize, AnchorSerialize}; -#[derive(AnchorSerialize, AnchorDeserialize, Debug, Clone, Copy, PartialEq, Eq, Default)] +#[derive( + AnchorSerialize, AnchorDeserialize, Debug, Clone, Copy, PartialEq, Eq, Default, ZeroCopyMut, +)] pub struct CompressedCpiContext { /// Is set by the program that is invoking the CPI to signal that is should /// set the cpi context. diff --git a/program-libs/compressed-account/src/instruction_data/data.rs b/program-libs/compressed-account/src/instruction_data/data.rs index 4c5ff5c261..e7a50d5d2e 100644 --- a/program-libs/compressed-account/src/instruction_data/data.rs +++ b/program-libs/compressed-account/src/instruction_data/data.rs @@ -1,5 +1,7 @@ use std::collections::HashMap; +use light_zero_copy::ZeroCopyMut; + use crate::{ compressed_account::{CompressedAccount, PackedCompressedAccountWithMerkleContext}, instruction_data::compressed_proof::CompressedProof, @@ -24,13 +26,15 @@ pub struct OutputCompressedAccountWithContext { pub merkle_tree: Pubkey, } -#[derive(Debug, PartialEq, Default, Clone, AnchorDeserialize, AnchorSerialize)] +#[derive(Debug, PartialEq, Default, Clone, AnchorDeserialize, AnchorSerialize, ZeroCopyMut)] pub struct OutputCompressedAccountWithPackedContext { pub compressed_account: CompressedAccount, pub merkle_tree_index: u8, } -#[derive(Debug, PartialEq, Default, Clone, Copy, AnchorDeserialize, AnchorSerialize)] +#[derive( + Debug, PartialEq, Default, Clone, Copy, AnchorDeserialize, AnchorSerialize, ZeroCopyMut, +)] pub struct NewAddressParamsPacked { pub seed: [u8; 32], pub address_queue_account_index: u8, @@ -38,7 +42,9 @@ pub struct NewAddressParamsPacked { pub address_merkle_tree_root_index: u16, } -#[derive(Debug, PartialEq, Default, Clone, Copy, AnchorDeserialize, AnchorSerialize)] +#[derive( + Debug, PartialEq, Default, Clone, Copy, AnchorDeserialize, AnchorSerialize, ZeroCopyMut, +)] pub struct NewAddressParamsAssignedPacked { pub seed: [u8; 32], pub address_queue_account_index: u8, @@ -86,7 +92,9 @@ pub struct NewAddressParamsAssigned { pub assigned_account_index: Option, } -#[derive(Debug, PartialEq, Default, Clone, Copy, AnchorDeserialize, AnchorSerialize)] +#[derive( + Debug, PartialEq, Default, Clone, Copy, AnchorDeserialize, AnchorSerialize, ZeroCopyMut, +)] pub struct PackedReadOnlyAddress { pub address: [u8; 32], pub address_merkle_tree_root_index: u16, diff --git a/program-libs/compressed-account/src/instruction_data/invoke_cpi.rs b/program-libs/compressed-account/src/instruction_data/invoke_cpi.rs index eaed16c3cd..59299dcaa1 100644 --- a/program-libs/compressed-account/src/instruction_data/invoke_cpi.rs +++ b/program-libs/compressed-account/src/instruction_data/invoke_cpi.rs @@ -1,3 +1,5 @@ +use light_zero_copy::ZeroCopyMut; + use super::{ cpi_context::CompressedCpiContext, data::{NewAddressParamsPacked, OutputCompressedAccountWithPackedContext}, @@ -8,7 +10,7 @@ use crate::{ }; #[repr(C)] -#[derive(Debug, PartialEq, Default, Clone, AnchorDeserialize, AnchorSerialize)] +#[derive(Debug, PartialEq, Default, Clone, AnchorDeserialize, AnchorSerialize, ZeroCopyMut)] pub struct InstructionDataInvokeCpi { pub proof: Option, pub new_address_params: Vec, diff --git a/program-libs/compressed-account/src/instruction_data/mod.rs b/program-libs/compressed-account/src/instruction_data/mod.rs index b264ac6a2d..77ff8db0bb 100644 --- a/program-libs/compressed-account/src/instruction_data/mod.rs +++ b/program-libs/compressed-account/src/instruction_data/mod.rs @@ -7,3 +7,4 @@ pub mod traits; pub mod with_account_info; pub mod with_readonly; pub mod zero_copy; +pub mod zero_copy_set; diff --git a/program-libs/compressed-account/src/instruction_data/with_readonly.rs b/program-libs/compressed-account/src/instruction_data/with_readonly.rs index e591f45444..28b169b206 100644 --- a/program-libs/compressed-account/src/instruction_data/with_readonly.rs +++ b/program-libs/compressed-account/src/instruction_data/with_readonly.rs @@ -1,6 +1,8 @@ use std::ops::Deref; -use light_zero_copy::{borsh::Deserialize, errors::ZeroCopyError, slice::ZeroCopySliceBorsh}; +use light_zero_copy::{ + borsh::Deserialize, errors::ZeroCopyError, slice::ZeroCopySliceBorsh, ZeroCopyMut, +}; use zerocopy::{ little_endian::{U16, U32, U64}, FromBytes, Immutable, IntoBytes, KnownLayout, Ref, Unaligned, @@ -30,7 +32,7 @@ use crate::{ AnchorDeserialize, AnchorSerialize, CompressedAccountError, }; -#[derive(Debug, Default, PartialEq, Clone, AnchorSerialize, AnchorDeserialize)] +#[derive(Debug, Default, PartialEq, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopyMut)] pub struct InAccount { pub discriminator: [u8; 8], /// Data hash @@ -193,7 +195,7 @@ impl<'a> Deref for ZInAccount<'a> { } } -#[derive(Debug, PartialEq, Default, Clone, AnchorSerialize, AnchorDeserialize)] +#[derive(Debug, PartialEq, Default, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopyMut)] pub struct InstructionDataInvokeCpiWithReadOnly { /// 0 With program ids /// 1 without program ids diff --git a/program-libs/compressed-account/src/instruction_data/zero_copy_set.rs b/program-libs/compressed-account/src/instruction_data/zero_copy_set.rs new file mode 100644 index 0000000000..9252fb4874 --- /dev/null +++ b/program-libs/compressed-account/src/instruction_data/zero_copy_set.rs @@ -0,0 +1,170 @@ +use light_zero_copy::borsh::Deserialize; +use zerocopy::little_endian::U16; + +use crate::{ + compressed_account::PackedMerkleContext, + instruction_data::{ + compressed_proof::CompressedProof, + cpi_context::CompressedCpiContext, + data::{ZNewAddressParamsAssignedPackedMut, ZOutputCompressedAccountWithPackedContextMut}, + with_readonly::{ZInAccountMut, ZInstructionDataInvokeCpiWithReadOnlyMut}, + }, + CompressedAccountError, Pubkey, +}; + +// TODO: unit test +impl ZOutputCompressedAccountWithPackedContextMut<'_> { + #[inline] + pub fn set( + &mut self, + owner: Pubkey, + lamports: u64, + address: Option<[u8; 32]>, + merkle_tree_index: u8, + discriminator: [u8; 8], + data_hash: [u8; 32], + ) -> Result<(), CompressedAccountError> { + self.compressed_account.owner = owner; + self.compressed_account.lamports = lamports.into(); + if let Some(self_address) = self.compressed_account.address.as_deref_mut() { + let input_address = + address.ok_or(CompressedAccountError::InstructionDataExpectedAddress)?; + *self_address = input_address; + } + if self.compressed_account.address.is_none() && address.is_some() { + return Err(CompressedAccountError::ZeroCopyExpectedAddress); + } + *self.merkle_tree_index = merkle_tree_index; + let data = self + .compressed_account + .data + .as_mut() + .ok_or(CompressedAccountError::CompressedAccountDataNotInitialized)?; + data.discriminator = discriminator; + *data.data_hash = data_hash; + + Ok(()) + } +} + +// TODO: unit test +impl ZInAccountMut<'_> { + #[inline] + pub fn set_z( + &mut self, + discriminator: [u8; 8], + data_hash: [u8; 32], + merkle_context: &::Output, + root_index: U16, + lamports: u64, + address: Option<&[u8]>, + ) -> Result<(), CompressedAccountError> { + self.discriminator = discriminator; + // Set merkle context fields manually due to mutability constraints + self.merkle_context.merkle_tree_pubkey_index = merkle_context.merkle_tree_pubkey_index; + self.merkle_context.queue_pubkey_index = merkle_context.queue_pubkey_index; + self.merkle_context + .leaf_index + .set(merkle_context.leaf_index.get()); + self.merkle_context.prove_by_index = merkle_context.prove_by_index() as u8; + *self.root_index = root_index; + self.data_hash = data_hash; + *self.lamports = lamports.into(); + if let Some(address) = address { + self.address + .as_mut() + .ok_or(CompressedAccountError::InstructionDataExpectedAddress)? + .copy_from_slice(address); + } + if self.address.is_some() && address.is_none() { + return Err(CompressedAccountError::ZeroCopyExpectedAddress); + } + Ok(()) + } + + #[inline] + pub fn set( + &mut self, + discriminator: [u8; 8], + data_hash: [u8; 32], + merkle_context: &PackedMerkleContext, + root_index: U16, + lamports: u64, + address: Option<&[u8]>, + ) -> Result<(), CompressedAccountError> { + self.discriminator = discriminator; + // Set merkle context fields manually due to mutability constraints + self.merkle_context.merkle_tree_pubkey_index = merkle_context.merkle_tree_pubkey_index; + self.merkle_context.queue_pubkey_index = merkle_context.queue_pubkey_index; + self.merkle_context + .leaf_index + .set(merkle_context.leaf_index); + self.merkle_context.prove_by_index = merkle_context.prove_by_index as u8; + *self.root_index = root_index; + self.data_hash = data_hash; + *self.lamports = lamports.into(); + if let Some(address) = address { + self.address + .as_mut() + .ok_or(CompressedAccountError::InstructionDataExpectedAddress)? + .copy_from_slice(address); + } + if self.address.is_some() && address.is_none() { + return Err(CompressedAccountError::ZeroCopyExpectedAddress); + } + Ok(()) + } +} + +impl ZInstructionDataInvokeCpiWithReadOnlyMut<'_> { + #[inline] + pub fn initialize( + &mut self, + bump: u8, + invoking_program_id: &Pubkey, + input_proof: Option<::Output>, + cpi_context: Option, + ) -> Result<(), CompressedAccountError> { + self.bump = bump; + self.invoking_program_id = *invoking_program_id; + if let Some(proof) = self.proof.as_deref_mut() { + let input_proof = + input_proof.ok_or(CompressedAccountError::InstructionDataExpectedProof)?; + proof.a = input_proof.a; + proof.b = input_proof.b; + proof.c = input_proof.c; + } + if self.proof.is_none() && input_proof.is_some() { + return Err(CompressedAccountError::ZeroCopyExpectedProof); + } + if let Some(cpi_context) = cpi_context { + self.with_cpi_context = 1; + self.cpi_context.cpi_context_account_index = cpi_context.cpi_context_account_index; + self.cpi_context.first_set_context = cpi_context.first_set_context as u8; + self.cpi_context.set_context = cpi_context.set_context as u8; + } + + Ok(()) + } +} + +impl ZNewAddressParamsAssignedPackedMut<'_> { + #[inline] + pub fn set( + &mut self, + seed: [u8; 32], + address_merkle_tree_root_index: U16, + assigned_account_index: Option, + address_merkle_tree_account_index: u8, + ) { + self.seed = seed; + self.address_merkle_tree_root_index = address_merkle_tree_root_index; + self.address_queue_account_index = 0; // always 0 for v2 address trees. + if let Some(assigned_account_index) = assigned_account_index { + self.assigned_account_index = assigned_account_index; + self.assigned_to_account = 1; // set to true + } + // Note we can skip address derivation since we are assigning it to the account in index 0. + self.address_merkle_tree_account_index = address_merkle_tree_account_index; + } +} diff --git a/program-libs/compressed-account/src/lib.rs b/program-libs/compressed-account/src/lib.rs index 46bcf7fbad..f3131768c6 100644 --- a/program-libs/compressed-account/src/lib.rs +++ b/program-libs/compressed-account/src/lib.rs @@ -55,6 +55,20 @@ pub enum CompressedAccountError { DeriveAddressError, #[error("Invalid argument.")] InvalidArgument, + #[error("Expected address for compressed account got None.")] + ZeroCopyExpectedAddress, + #[error("Expected address for compressed account got None.")] + InstructionDataExpectedAddress, + #[error("Compressed account data not initialized.")] + CompressedAccountDataNotInitialized, + #[error("Expected discriminator for compressed account got None.")] + ExpectedDiscriminator, + #[error("Expected data hash for compressed account got None.")] + ExpectedDataHash, + #[error("Expected proof for compressed account got None.")] + InstructionDataExpectedProof, + #[error("Expected proof for compressed account got None.")] + ZeroCopyExpectedProof, } // NOTE(vadorovsky): Unfortunately, we need to do it by hand. @@ -74,6 +88,13 @@ impl From for u32 { CompressedAccountError::FailedBorrowRentSysvar => 12014, CompressedAccountError::DeriveAddressError => 12015, CompressedAccountError::InvalidArgument => 12016, + CompressedAccountError::ZeroCopyExpectedAddress => 12017, + CompressedAccountError::InstructionDataExpectedAddress => 12018, + CompressedAccountError::CompressedAccountDataNotInitialized => 12019, + CompressedAccountError::ExpectedDiscriminator => 12020, + CompressedAccountError::ExpectedDataHash => 12020, + CompressedAccountError::InstructionDataExpectedProof => 12021, + CompressedAccountError::ZeroCopyExpectedProof => 12022, CompressedAccountError::HasherError(e) => u32::from(e), } } @@ -168,3 +189,5 @@ impl From for TreeType { } } } + +pub type CompressedAddress = [u8; 32]; diff --git a/program-libs/compressed-account/src/pubkey.rs b/program-libs/compressed-account/src/pubkey.rs index 9dc74ea35f..7693a19d26 100644 --- a/program-libs/compressed-account/src/pubkey.rs +++ b/program-libs/compressed-account/src/pubkey.rs @@ -1,6 +1,11 @@ #[cfg(feature = "bytemuck-des")] use bytemuck::{Pod, Zeroable}; -use light_zero_copy::{borsh::Deserialize, errors::ZeroCopyError}; +use light_zero_copy::{ + borsh::{Deserialize, ZeroCopyStructInner}, + borsh_mut::{DeserializeMut, ZeroCopyStructInnerMut}, + errors::ZeroCopyError, + ZeroCopyNew, +}; use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Ref, Unaligned}; use crate::{AnchorDeserialize, AnchorSerialize}; @@ -46,6 +51,21 @@ pub struct Pubkey(pub(crate) [u8; 32]); #[repr(C)] pub struct Pubkey(pub(crate) [u8; 32]); +impl<'a> ZeroCopyNew<'a> for Pubkey { + type ZeroCopyConfig = (); + type Output = zerocopy::Ref<&'a mut [u8], Pubkey>; + fn byte_len(_config: &Self::ZeroCopyConfig) -> usize { + 32 + } + fn new_zero_copy( + bytes: &'a mut [u8], + _config: Self::ZeroCopyConfig, + ) -> Result<(Self::Output, &'a mut [u8]), ZeroCopyError> { + let (key, rest) = zerocopy::Ref::from_prefix(bytes)?; + Ok((key, rest)) + } +} + impl Pubkey { pub fn new_from_array(array: [u8; 32]) -> Self { Self(array) @@ -91,6 +111,25 @@ impl<'a> Deserialize<'a> for Pubkey { Ok(Ref::<&[u8], Pubkey>::from_prefix(bytes)?) } } + +impl<'a> DeserializeMut<'a> for Pubkey { + type Output = Ref<&'a mut [u8], Pubkey>; + + #[inline] + fn zero_copy_at_mut( + bytes: &'a mut [u8], + ) -> Result<(Self::Output, &'a mut [u8]), ZeroCopyError> { + Ok(Ref::<&mut [u8], Pubkey>::from_prefix(bytes)?) + } +} + +impl ZeroCopyStructInner for Pubkey { + type ZeroCopyInner = Pubkey; +} + +impl ZeroCopyStructInnerMut for Pubkey { + type ZeroCopyInnerMut = Pubkey; +} impl From for [u8; 32] { fn from(pubkey: Pubkey) -> Self { pubkey.to_bytes() diff --git a/program-libs/ctoken-types/Cargo.toml b/program-libs/ctoken-types/Cargo.toml new file mode 100644 index 0000000000..557bfedeab --- /dev/null +++ b/program-libs/ctoken-types/Cargo.toml @@ -0,0 +1,40 @@ +[package] +name = "light-ctoken-types" +version = { workspace = true } +edition = { workspace = true } + +[features] +anchor = ["light-compressed-account/anchor", "dep:anchor-lang"] +solana = ["dep:solana-program-error", "dep:solana-sysvar"] +default = [] + +[dependencies] +borsh = { workspace = true } +# Solana dependencies +solana-pubkey = { workspace = true } +solana-program-error = { workspace = true, optional = true } +light-zero-copy = { workspace = true, features = ["derive", "mut"] } +light-compressed-account = { workspace = true } +light-hasher = { workspace = true } +arrayvec = { workspace = true } +zerocopy = { workspace = true } +thiserror = { workspace = true } +pinocchio = { workspace = true } +anchor-lang = { workspace = true, optional = true } +light-macros = { workspace = true } +solana-sysvar = { workspace = true, optional = true } +spl-pod = { workspace = true } +spl-token-2022 = { workspace = true } +solana-msg = { workspace = true } + +[dev-dependencies] +rand = { workspace = true } +num-bigint = { workspace = true } +light-compressed-account = { workspace = true, features = ["new-unique"] } + +[lints.rust.unexpected_cfgs] +level = "allow" +check-cfg = [ + 'cfg(target_os, values("solana"))', + 'cfg(feature, values("frozen-abi", "no-entrypoint"))', +] diff --git a/program-libs/ctoken-types/src/context.rs b/program-libs/ctoken-types/src/context.rs new file mode 100644 index 0000000000..a526f652f8 --- /dev/null +++ b/program-libs/ctoken-types/src/context.rs @@ -0,0 +1,61 @@ +use arrayvec::ArrayVec; +use light_compressed_account::hash_to_bn254_field_size_be; +use pinocchio::pubkey::Pubkey; + +use crate::error::CTokenError; + +/// Context for caching hashed values to avoid recomputation +pub struct TokenContext { + /// Cache for mint hashes: (mint_pubkey, hashed_mint) + pub hashed_mints: ArrayVec<(Pubkey, [u8; 32]), 5>, + /// Cache for pubkey hashes: (pubkey, hashed_pubkey) + pub hashed_pubkeys: Vec<(Pubkey, [u8; 32])>, +} + +impl TokenContext { + /// Create a new empty context + pub fn new() -> Self { + Self { + hashed_mints: ArrayVec::new(), + hashed_pubkeys: Vec::new(), + } + } + + /// Get or compute hash for a mint pubkey + pub fn get_or_hash_mint(&mut self, mint: &Pubkey) -> Result<[u8; 32], CTokenError> { + let hashed_mint = self.hashed_mints.iter().find(|a| &a.0 == mint).map(|a| a.1); + match hashed_mint { + Some(hashed_mint) => Ok(hashed_mint), + None => { + let hashed_mint = hash_to_bn254_field_size_be(mint); + self.hashed_mints + .try_push((*mint, hashed_mint)) + .map_err(|_| CTokenError::InvalidAccountData)?; + Ok(hashed_mint) + } + } + } + + /// Get or compute hash for a pubkey (owner, delegate, etc.) + pub fn get_or_hash_pubkey(&mut self, pubkey: &Pubkey) -> [u8; 32] { + let hashed_pubkey = self + .hashed_pubkeys + .iter() + .find(|a| &a.0 == pubkey) + .map(|a| a.1); + match hashed_pubkey { + Some(hashed_pubkey) => hashed_pubkey, + None => { + let hashed_pubkey = hash_to_bn254_field_size_be(pubkey); + self.hashed_pubkeys.push((*pubkey, hashed_pubkey)); + hashed_pubkey + } + } + } +} + +impl Default for TokenContext { + fn default() -> Self { + Self::new() + } +} diff --git a/program-libs/ctoken-types/src/error.rs b/program-libs/ctoken-types/src/error.rs new file mode 100644 index 0000000000..46aab68210 --- /dev/null +++ b/program-libs/ctoken-types/src/error.rs @@ -0,0 +1,157 @@ +use light_zero_copy::errors::ZeroCopyError; +use thiserror::Error; + +#[derive(Debug, PartialEq, Error)] +pub enum CTokenError { + #[error("Invalid instruction data provided")] + InvalidInstructionData, + + #[error("Invalid account data format")] + InvalidAccountData, + + #[error("Arithmetic operation resulted in overflow")] + ArithmeticOverflow, + + #[error("Failed to compute hash for data")] + HashComputationError, + + #[error("Invalid or malformed extension data")] + InvalidExtensionData, + + #[error("Missing required mint authority")] + MissingMintAuthority, + + #[error("Missing required freeze authority")] + MissingFreezeAuthority, + + #[error("Invalid metadata pointer configuration")] + InvalidMetadataPointer, + + #[error("Token metadata validation failed")] + InvalidTokenMetadata, + + #[error("Insufficient token supply for operation")] + InsufficientSupply, + + #[error("Token account is frozen and cannot be modified")] + AccountFrozen, + + #[error("Invalid compressed proof provided")] + InvalidProof, + + #[error("Address derivation failed")] + AddressDerivationFailed, + + #[error("Extension type not supported")] + UnsupportedExtension, + + #[error("Maximum number of extensions exceeded")] + TooManyExtensions, + + #[error("Invalid merkle tree root index")] + InvalidRootIndex, + + #[error("Compressed account data size exceeds limit")] + DataSizeExceeded, + + #[error("Invalid compression mode")] + InvalidCompressionMode, + + #[error("Insufficient funds for compression.")] + CompressInsufficientFunds, + + #[error("Failed to access sysvar")] + SysvarAccessError, + + #[error("Compressed token account TLV is unimplemented.")] + CompressedTokenAccountTlvUnimplemented, + + #[error("Input accounts lamports length mismatch")] + InputAccountsLamportsLengthMismatch, + + #[error("Output accounts lamports length mismatch")] + OutputAccountsLamportsLengthMismatch, + + #[error("Invalid token data version")] + InvalidTokenDataVersion, + + #[error("Instruction data expected mint authority")] + InstructionDataExpectedMintAuthority, + + #[error("Instruction data expected freeze authority")] + ZeroCopyExpectedMintAuthority, + + #[error("Instruction data expected freeze authority")] + InstructionDataExpectedFreezeAuthority, + + #[error("Instruction data expected freeze authority")] + ZeroCopyExpectedFreezeAuthority, + + #[error("Light hasher error: {0}")] + HasherError(#[from] light_hasher::HasherError), + + #[error("Light zero copy error: {0}")] + ZeroCopyError(#[from] ZeroCopyError), + + #[error("Light compressed account error: {0}")] + CompressedAccountError(#[from] light_compressed_account::CompressedAccountError), +} + +impl From for u32 { + fn from(e: CTokenError) -> u32 { + match e { + CTokenError::InvalidInstructionData => 18001, + CTokenError::InvalidAccountData => 18002, + CTokenError::ArithmeticOverflow => 18003, + CTokenError::HashComputationError => 18004, + CTokenError::InvalidExtensionData => 18005, + CTokenError::MissingMintAuthority => 18006, + CTokenError::MissingFreezeAuthority => 18007, + CTokenError::InvalidMetadataPointer => 18008, + CTokenError::InvalidTokenMetadata => 18009, + CTokenError::InsufficientSupply => 18010, + CTokenError::AccountFrozen => 18011, + CTokenError::InvalidProof => 18012, + CTokenError::AddressDerivationFailed => 18013, + CTokenError::UnsupportedExtension => 18014, + CTokenError::TooManyExtensions => 18015, + CTokenError::InvalidRootIndex => 18016, + CTokenError::DataSizeExceeded => 18017, + CTokenError::InvalidCompressionMode => 18018, + CTokenError::CompressInsufficientFunds => 18019, + CTokenError::SysvarAccessError => 18020, + CTokenError::CompressedTokenAccountTlvUnimplemented => 18021, + CTokenError::InputAccountsLamportsLengthMismatch => 18022, + CTokenError::OutputAccountsLamportsLengthMismatch => 18023, + CTokenError::InvalidTokenDataVersion => 18028, + CTokenError::InstructionDataExpectedMintAuthority => 18024, + CTokenError::ZeroCopyExpectedMintAuthority => 18025, + CTokenError::InstructionDataExpectedFreezeAuthority => 18026, + CTokenError::ZeroCopyExpectedFreezeAuthority => 18027, + CTokenError::HasherError(e) => u32::from(e), + CTokenError::ZeroCopyError(e) => u32::from(e), + CTokenError::CompressedAccountError(e) => u32::from(e), + } + } +} + +#[cfg(feature = "solana")] +#[cfg(all(feature = "solana", not(feature = "anchor")))] +impl From for solana_program_error::ProgramError { + fn from(e: CTokenError) -> Self { + solana_program_error::ProgramError::Custom(e.into()) + } +} + +impl From for pinocchio::program_error::ProgramError { + fn from(e: CTokenError) -> Self { + pinocchio::program_error::ProgramError::Custom(e.into()) + } +} + +#[cfg(feature = "anchor")] +impl From for anchor_lang::prelude::ProgramError { + fn from(e: CTokenError) -> Self { + anchor_lang::prelude::ProgramError::Custom(e.into()) + } +} diff --git a/program-libs/ctoken-types/src/instructions/create_associated_token_account.rs b/program-libs/ctoken-types/src/instructions/create_associated_token_account.rs new file mode 100644 index 0000000000..07c5ed09fc --- /dev/null +++ b/program-libs/ctoken-types/src/instructions/create_associated_token_account.rs @@ -0,0 +1,18 @@ +use light_compressed_account::Pubkey; +use light_zero_copy::ZeroCopy; + +use crate::{ + instructions::extensions::compressible::CompressibleExtensionInstructionData, + AnchorDeserialize, AnchorSerialize, +}; + +#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopy)] +pub struct CreateAssociatedTokenAccountInstructionData { + /// The owner of the associated token account + pub owner: Pubkey, + /// The mint for the associated token account + pub mint: Pubkey, + pub bump: u8, + /// Optional compressible configuration for the token account + pub compressible_config: Option, +} diff --git a/program-libs/ctoken-types/src/instructions/create_compressed_mint.rs b/program-libs/ctoken-types/src/instructions/create_compressed_mint.rs new file mode 100644 index 0000000000..d6b12abf0c --- /dev/null +++ b/program-libs/ctoken-types/src/instructions/create_compressed_mint.rs @@ -0,0 +1,103 @@ +use light_compressed_account::{instruction_data::compressed_proof::CompressedProof, Pubkey}; +use light_zero_copy::ZeroCopy; + +use crate::{ + instructions::extensions::ExtensionInstructionData, + state::{CompressedMint, ExtensionStruct}, + AnchorDeserialize, AnchorSerialize, CTokenError, +}; + +#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopy)] +pub struct CreateCompressedMintInstructionData { + pub decimals: u8, + pub mint_authority: Pubkey, + pub proof: CompressedProof, + pub mint_bump: u8, + pub address_merkle_tree_root_index: u16, + // compressed address TODO: make a type CompressedAddress (not straight forward because of AnchorSerialize) + pub mint_address: [u8; 32], + pub freeze_authority: Option, + pub version: u8, + pub extensions: Option>, +} + +#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopy)] +pub struct UpdateCompressedMintInstructionData { + // pub merkle_context: PackedMerkleContext, + pub leaf_index: u32, + pub prove_by_index: bool, + pub root_index: u16, + pub address: [u8; 32], + pub proof: Option, + pub mint: CompressedMintInstructionData, +} + +#[derive(Debug, PartialEq, Eq, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopy)] +pub struct CompressedMintInstructionData { + /// Version for upgradability + pub version: u8, + /// Pda with seed address of compressed mint + pub spl_mint: Pubkey, + /// Total supply of tokens. + pub supply: u64, + /// Number of base 10 digits to the right of the decimal place. + pub decimals: u8, + /// Extension, necessary for mint to. + pub is_decompressed: bool, + // /// Optional authority used to mint new tokens. The mint authority may only + // /// be provided during mint creation. If no mint authority is present + // /// then the mint has a fixed supply and no further tokens may be + // /// minted. + // pub mint_authority: Option, + /// Optional authority to freeze token accounts. + pub freeze_authority: Option, + pub extensions: Option>, +} +impl TryFrom for CompressedMintInstructionData { + type Error = CTokenError; + + fn try_from(mint: CompressedMint) -> Result { + let extensions = match mint.extensions { + Some(exts) => { + let converted_exts: Result, Self::Error> = exts + .into_iter() + .map(|ext| match ext { + /* ExtensionStruct::MetadataPointer(metadata_pointer) => { + Ok(ExtensionInstructionData::MetadataPointer( + crate::instructions::extensions::metadata_pointer::InitMetadataPointer { + authority: metadata_pointer.authority, + metadata_address: metadata_pointer.metadata_address, + }, + )) + }*/ + ExtensionStruct::TokenMetadata(token_metadata) => { + Ok(ExtensionInstructionData::TokenMetadata( + crate::instructions::extensions::token_metadata::TokenMetadataInstructionData { + update_authority: token_metadata.update_authority, + metadata: token_metadata.metadata, + additional_metadata: Some(token_metadata.additional_metadata), + version: token_metadata.version, + }, + )) + } + _ => { + Err(CTokenError::UnsupportedExtension) + } + }) + .collect(); + Some(converted_exts?) + } + None => None, + }; + + Ok(Self { + version: mint.version, + spl_mint: mint.spl_mint, + supply: mint.supply, + decimals: mint.decimals, + is_decompressed: mint.is_decompressed, + freeze_authority: mint.freeze_authority, + extensions, + }) + } +} diff --git a/program-libs/ctoken-types/src/instructions/create_spl_mint.rs b/program-libs/ctoken-types/src/instructions/create_spl_mint.rs new file mode 100644 index 0000000000..429dd13ed3 --- /dev/null +++ b/program-libs/ctoken-types/src/instructions/create_spl_mint.rs @@ -0,0 +1,13 @@ +use light_zero_copy::ZeroCopy; + +use crate::{ + instructions::create_compressed_mint::UpdateCompressedMintInstructionData, AnchorDeserialize, + AnchorSerialize, +}; + +#[derive(ZeroCopy, AnchorDeserialize, AnchorSerialize, Clone, Debug)] +pub struct CreateSplMintInstructionData { + pub mint_bump: u8, + pub mint: UpdateCompressedMintInstructionData, + pub mint_authority_is_none: bool, // if mint authority is None anyone can create the spl mint. +} diff --git a/program-libs/ctoken-types/src/instructions/extensions/compressible.rs b/program-libs/ctoken-types/src/instructions/extensions/compressible.rs new file mode 100644 index 0000000000..66b2aabb83 --- /dev/null +++ b/program-libs/ctoken-types/src/instructions/extensions/compressible.rs @@ -0,0 +1,29 @@ +use light_compressed_account::Pubkey; +use light_zero_copy::{ZeroCopy, ZeroCopyMut}; +use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout}; + +use crate::{AnchorDeserialize, AnchorSerialize}; + +#[derive( + Debug, + Clone, + Copy, + PartialEq, + Eq, + AnchorSerialize, + AnchorDeserialize, + ZeroCopy, + ZeroCopyMut, + KnownLayout, + Immutable, + FromBytes, + IntoBytes, +)] +#[repr(C)] +pub struct CompressibleExtensionInstructionData { + /// Number of slots that must pass before compression is allowed + pub slots_until_compression: u64, + /// Authority that can close this account (in addition to owner) + pub rent_authority: Pubkey, + pub rent_recipient: Pubkey, +} diff --git a/program-libs/ctoken-types/src/instructions/extensions/metadata_pointer.rs b/program-libs/ctoken-types/src/instructions/extensions/metadata_pointer.rs new file mode 100644 index 0000000000..58c4c1ff27 --- /dev/null +++ b/program-libs/ctoken-types/src/instructions/extensions/metadata_pointer.rs @@ -0,0 +1,107 @@ +use light_compressed_account::Pubkey; +use light_hasher::{ + hash_to_field_size::hashv_to_bn254_field_size_be_const_array, DataHasher, Hasher, HasherError, +}; +use light_zero_copy::{ZeroCopy, ZeroCopyMut}; + +use crate::{context::TokenContext, AnchorDeserialize, AnchorSerialize, CTokenError, state::ExtensionType}; + +/// Metadata pointer extension data for compressed mints. +#[derive( + Debug, Clone, PartialEq, Eq, AnchorSerialize, ZeroCopy, AnchorDeserialize, ZeroCopyMut, +)] +pub struct MetadataPointer { + /// Authority that can set the metadata address + pub authority: Option, + /// (Compressed) address that holds the metadata (in token 22) + pub metadata_address: Option, +} + +impl DataHasher for MetadataPointer { + fn hash(&self) -> Result<[u8; 32], HasherError> { + let mut discriminator = [0u8; 32]; + discriminator[31] = ExtensionType::MetadataPointer as u8; + let hashed_metadata_address = if let Some(metadata_address) = self.metadata_address { + hashv_to_bn254_field_size_be_const_array::<2>(&[metadata_address.as_ref()])? + } else { + [0u8; 32] + }; + let hashed_authority = if let Some(authority) = self.authority { + hashv_to_bn254_field_size_be_const_array::<2>(&[authority.as_ref()])? + } else { + [0u8; 32] + }; + H::hashv(&[ + discriminator.as_slice(), + hashed_metadata_address.as_slice(), + hashed_authority.as_slice(), + ]) + } +} + +/// Instruction data for initializing metadata pointer +#[derive(Debug, Clone, PartialEq, Eq, AnchorSerialize, AnchorDeserialize, ZeroCopy)] +pub struct InitMetadataPointer { + /// The authority that can set the metadata address + pub authority: Option, + /// The account address that holds the metadata + pub metadata_address: Option, +} + +impl InitMetadataPointer { + pub fn hash_metadata_pointer( + &self, + context: &mut TokenContext, + ) -> Result<[u8; 32], CTokenError> { + let mut discriminator = [0u8; 32]; + discriminator[31] = ExtensionType::MetadataPointer as u8; + + let hashed_metadata_address = if let Some(metadata_address) = self.metadata_address { + context.get_or_hash_pubkey(&metadata_address.into()) + } else { + [0u8; 32] + }; + + let hashed_authority = if let Some(authority) = self.authority { + context.get_or_hash_pubkey(&authority.into()) + } else { + [0u8; 32] + }; + + H::hashv(&[ + discriminator.as_slice(), + hashed_metadata_address.as_slice(), + hashed_authority.as_slice(), + ]) + .map_err(CTokenError::from) + } +} + +impl ZInitMetadataPointer<'_> { + pub fn hash_metadata_pointer( + &self, + context: &mut TokenContext, + ) -> Result<[u8; 32], CTokenError> { + let mut discriminator = [0u8; 32]; + discriminator[31] = ExtensionType::MetadataPointer as u8; + + let hashed_metadata_address = if let Some(metadata_address) = self.metadata_address { + context.get_or_hash_pubkey(&(*metadata_address).into()) + } else { + [0u8; 32] + }; + + let hashed_authority = if let Some(authority) = self.authority { + context.get_or_hash_pubkey(&(*authority).into()) + } else { + [0u8; 32] + }; + + H::hashv(&[ + discriminator.as_slice(), + hashed_metadata_address.as_slice(), + hashed_authority.as_slice(), + ]) + .map_err(CTokenError::from) + } +} diff --git a/program-libs/ctoken-types/src/instructions/extensions/mod.rs b/program-libs/ctoken-types/src/instructions/extensions/mod.rs new file mode 100644 index 0000000000..cf15fa39d0 --- /dev/null +++ b/program-libs/ctoken-types/src/instructions/extensions/mod.rs @@ -0,0 +1,166 @@ +use light_hasher::{Hasher, Poseidon, Sha256}; +pub mod compressible; +//pub mod metadata_pointer; +pub mod token_metadata; +use pinocchio::log::sol_log_compute_units; +use solana_msg::msg; +//pub use metadata_pointer::{InitMetadataPointer, ZInitMetadataPointer}; +pub use token_metadata::{TokenMetadataInstructionData, ZTokenMetadataInstructionData}; + +use crate::{ + context::TokenContext, state::Version, AnchorDeserialize, AnchorSerialize, CTokenError, +}; + +#[derive(Debug, Clone, PartialEq, Eq, AnchorSerialize, AnchorDeserialize)] +pub enum ExtensionInstructionData { + Placeholder0, + Placeholder1, + Placeholder2, + Placeholder3, + Placeholder4, + Placeholder5, + Placeholder6, + Placeholder7, + Placeholder8, + Placeholder9, + Placeholder10, + Placeholder11, + Placeholder12, + Placeholder13, + Placeholder14, + Placeholder15, + Placeholder16, + Placeholder17, + Placeholder18, // MetadataPointer(InitMetadataPointer), + TokenMetadata(TokenMetadataInstructionData), +} + +#[derive(Debug, Clone, PartialEq)] +pub enum ZExtensionInstructionData<'a> { + Placeholder0, + Placeholder1, + Placeholder2, + Placeholder3, + Placeholder4, + Placeholder5, + Placeholder6, + Placeholder7, + Placeholder8, + Placeholder9, + Placeholder10, + Placeholder11, + Placeholder12, + Placeholder13, + Placeholder14, + Placeholder15, + Placeholder16, + Placeholder17, + Placeholder18, // MetadataPointer(ZInitMetadataPointer<'a>), + TokenMetadata(ZTokenMetadataInstructionData<'a>), +} + +impl ExtensionInstructionData { + pub fn hash( + &self, + mint: light_compressed_account::Pubkey, + context: &mut TokenContext, + ) -> Result<[u8; 32], CTokenError> { + match self { + /* ExtensionInstructionData::MetadataPointer(metadata_pointer) => { + metadata_pointer.hash_metadata_pointer::(context) + }*/ + ExtensionInstructionData::TokenMetadata(token_metadata) => { + token_metadata.hash_token_metadata::(mint, context) + } + _ => Err(CTokenError::UnsupportedExtension), + } + } +} + +impl ZExtensionInstructionData<'_> { + pub fn hash( + &self, + hashed_mint: &[u8; 32], + context: &mut TokenContext, + ) -> Result<[u8; 32], CTokenError> { + match self { + /*ZExtensionInstructionData::MetadataPointer(metadata_pointer) => { + metadata_pointer.hash_metadata_pointer::(context) + }*/ + ZExtensionInstructionData::TokenMetadata(token_metadata) => { + match Version::try_from(token_metadata.version)? { + Version::Poseidon => { + // TODO: cleanup other hashing code + msg!("poseidon"); + sol_log_compute_units(); + let hash = + token_metadata.hash_token_metadata::(hashed_mint, context); + sol_log_compute_units(); + hash + } + Version::Sha256 => { + msg!("sha256"); + sol_log_compute_units(); + let mut hash = + token_metadata.hash_token_metadata::(hashed_mint, context)?; + sol_log_compute_units(); + hash[0] = 0; + Ok(hash) + } + _ => { + msg!( + "TokenMetadata hash version not supported {} (0 Poseidon, 1 Sha256 are supported).", + token_metadata.version + ); + unimplemented!( + "TokenMetadata hash version not supported {}", + token_metadata.version + ) + } // Version::Keccak256 => ::hash::(self), + // Version::Sha256Flat => self.sha_flat(), + } + } + _ => Err(CTokenError::UnsupportedExtension), + } + } +} + +// Manual implementation of zero-copy traits for ExtensionInstructionData +impl<'a> light_zero_copy::borsh::Deserialize<'a> for ExtensionInstructionData { + type Output = ZExtensionInstructionData<'a>; + + fn zero_copy_at( + data: &'a [u8], + ) -> Result<(Self::Output, &'a [u8]), light_zero_copy::errors::ZeroCopyError> { + // Read discriminant (first 1 byte for borsh enum) + if data.is_empty() { + return Err(light_zero_copy::errors::ZeroCopyError::ArraySize( + 1, + data.len(), + )); + } + + let discriminant = data[0]; + let remaining_data = &data[1..]; + + match discriminant { + /* 18 => { + let (metadata_pointer, remaining_bytes) = + InitMetadataPointer::zero_copy_at(remaining_data)?; + Ok(( + ZExtensionInstructionData::MetadataPointer(metadata_pointer), + remaining_bytes, + )) + }*/ + 19 => { + let (token_metadata, remaining_bytes) = + TokenMetadataInstructionData::zero_copy_at(remaining_data)?; + Ok(( + ZExtensionInstructionData::TokenMetadata(token_metadata), + remaining_bytes, + )) + } + _ => Err(light_zero_copy::errors::ZeroCopyError::InvalidConversion), + } + } +} diff --git a/program-libs/ctoken-types/src/instructions/extensions/token_metadata.rs b/program-libs/ctoken-types/src/instructions/extensions/token_metadata.rs new file mode 100644 index 0000000000..b992831d1a --- /dev/null +++ b/program-libs/ctoken-types/src/instructions/extensions/token_metadata.rs @@ -0,0 +1,91 @@ +use light_compressed_account::Pubkey; +use light_zero_copy::ZeroCopy; + +use crate::{ + context::TokenContext, + state::{ + token_metadata_hash, token_metadata_hash_with_hashed_values, AdditionalMetadata, Metadata, + }, + AnchorDeserialize, AnchorSerialize, CTokenError, +}; + +// TODO: double check hashing scheme, add tests with partial data +#[derive(Debug, Clone, PartialEq, Eq, AnchorSerialize, AnchorDeserialize, ZeroCopy)] +pub struct TokenMetadataInstructionData { + pub update_authority: Option, + pub metadata: Metadata, + pub additional_metadata: Option>, + pub version: u8, +} + +impl TokenMetadataInstructionData { + pub fn hash_token_metadata( + &self, + mint: light_compressed_account::Pubkey, + context: &mut TokenContext, + ) -> Result<[u8; 32], CTokenError> { + let metadata_hash = light_hasher::DataHasher::hash::(&self.metadata) + .map_err(|_| CTokenError::InvalidAccountData)?; + + let additional_metadata: arrayvec::ArrayVec<(&[u8], &[u8]), 32> = + if let Some(ref additional_metadata) = self.additional_metadata { + additional_metadata + .iter() + .map(|item| (item.key.as_slice(), item.value.as_slice())) + .collect() + } else { + arrayvec::ArrayVec::new() + }; + + let hashed_update_authority = self + .update_authority + .map(|update_authority| context.get_or_hash_pubkey(&update_authority.into())); + + let hashed_mint = context.get_or_hash_mint(&mint.into())?; + + token_metadata_hash::( + hashed_update_authority + .as_ref() + .map(|h: &[u8; 32]| h.as_slice()), + hashed_mint.as_slice(), + metadata_hash.as_slice(), + &additional_metadata, + self.version, + ) + .map_err(|_| CTokenError::InvalidAccountData) + } +} + +impl ZTokenMetadataInstructionData<'_> { + pub fn hash_token_metadata( + &self, + hashed_mint: &[u8; 32], + context: &mut TokenContext, + ) -> Result<[u8; 32], CTokenError> { + let metadata_hash = light_hasher::DataHasher::hash::(&self.metadata) + .map_err(|_| CTokenError::InvalidAccountData)?; + + let additional_metadata: arrayvec::ArrayVec<(&[u8], &[u8]), 32> = + if let Some(ref additional_metadata) = self.additional_metadata { + additional_metadata + .iter() + .map(|item| (item.key, item.value)) + .collect() + } else { + arrayvec::ArrayVec::new() + }; + + let hashed_update_authority = self + .update_authority + .map(|update_authority| context.get_or_hash_pubkey(&(*update_authority).into())); + + token_metadata_hash_with_hashed_values::( + hashed_update_authority.as_ref(), + hashed_mint, + metadata_hash.as_slice(), + &additional_metadata, + self.version, + ) + .map_err(|_| CTokenError::InvalidAccountData) + } +} diff --git a/program-libs/ctoken-types/src/instructions/mint_to_compressed.rs b/program-libs/ctoken-types/src/instructions/mint_to_compressed.rs new file mode 100644 index 0000000000..913f70a235 --- /dev/null +++ b/program-libs/ctoken-types/src/instructions/mint_to_compressed.rs @@ -0,0 +1,31 @@ +use light_compressed_account::{instruction_data::compressed_proof::CompressedProof, Pubkey}; +use light_zero_copy::ZeroCopy; + +use crate::{ + instructions::create_compressed_mint::UpdateCompressedMintInstructionData, + state::CompressedMint, AnchorDeserialize, AnchorSerialize, +}; + +#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopy)] +pub struct CompressedMintInputs { + pub leaf_index: u32, + pub prove_by_index: bool, + pub root_index: u16, + pub address: [u8; 32], + pub compressed_mint_input: CompressedMint, //TODO: move supply and authority last so that we can send only the hash chain. +} + +#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopy)] +pub struct Recipient { + pub recipient: Pubkey, + pub amount: u64, +} + +#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopy)] +pub struct MintToCompressedInstructionData { + pub token_account_version: u8, + pub compressed_mint_inputs: UpdateCompressedMintInstructionData, + pub lamports: Option, + pub recipients: Vec, + pub proof: Option, +} diff --git a/program-libs/ctoken-types/src/instructions/mod.rs b/program-libs/ctoken-types/src/instructions/mod.rs new file mode 100644 index 0000000000..193887935b --- /dev/null +++ b/program-libs/ctoken-types/src/instructions/mod.rs @@ -0,0 +1,7 @@ +pub mod create_associated_token_account; +pub mod create_compressed_mint; +pub mod create_spl_mint; +pub mod mint_to_compressed; +pub mod transfer2; + +pub mod extensions; diff --git a/program-libs/ctoken-types/src/instructions/transfer2.rs b/program-libs/ctoken-types/src/instructions/transfer2.rs new file mode 100644 index 0000000000..a1a953fa9c --- /dev/null +++ b/program-libs/ctoken-types/src/instructions/transfer2.rs @@ -0,0 +1,311 @@ +use std::fmt::Debug; + +use light_compressed_account::{ + compressed_account::PackedMerkleContext, + instruction_data::{compressed_proof::CompressedProof, cpi_context::CompressedCpiContext}, +}; +use light_zero_copy::{ + borsh::Deserialize, borsh_mut::DeserializeMut, ZeroCopy, ZeroCopyMut, ZeroCopyNew, +}; +use spl_pod::solana_msg::msg; +use zerocopy::Ref; + +use crate::{AnchorDeserialize, AnchorSerialize, CTokenError}; +// TODO: move to token data +#[repr(u8)] +pub enum TokenAccountVersion { + V1 = 1u8, + V2 = 2u8, +} + +impl TokenAccountVersion { + pub fn discriminator(&self) -> [u8; 8] { + match self { + TokenAccountVersion::V1 => [2, 0, 0, 0, 0, 0, 0, 0], // 2 le + TokenAccountVersion::V2 => [0, 0, 0, 0, 0, 0, 0, 3], // 3 be + } + } + + /// Serializes amount to bytes using version-specific endianness + /// V1: little-endian, V2: big-endian + pub fn serialize_amount_bytes(&self, amount: u64) -> [u8; 32] { + let mut amount_bytes = [0u8; 32]; + match self { + TokenAccountVersion::V1 => { + amount_bytes[24..].copy_from_slice(&amount.to_le_bytes()); + } + TokenAccountVersion::V2 => { + amount_bytes[24..].copy_from_slice(&amount.to_be_bytes()); + } + } + amount_bytes + } +} + +impl TryFrom for TokenAccountVersion { + type Error = crate::CTokenError; + + fn try_from(value: u8) -> Result { + match value { + 1 => Ok(TokenAccountVersion::V1), + 2 => Ok(TokenAccountVersion::V2), + _ => Err(crate::CTokenError::InvalidTokenDataVersion), + } + } +} + +#[derive( + Debug, Clone, Default, PartialEq, AnchorSerialize, AnchorDeserialize, ZeroCopy, ZeroCopyMut, +)] +pub struct MultiInputTokenDataWithContext { + pub amount: u64, + pub merkle_context: PackedMerkleContext, + pub root_index: u16, + // From remaining accounts. + pub mint: u8, + pub owner: u8, + pub with_delegate: bool, + // Only used if with_delegate is true, we could also use 255 to indicate no delegate + pub delegate: u8, + pub version: u8, +} + +#[derive( + Clone, + Copy, + Debug, + Default, + PartialEq, + Eq, + AnchorSerialize, + AnchorDeserialize, + ZeroCopy, + ZeroCopyMut, +)] +pub struct MultiTokenTransferOutputData { + pub owner: u8, + pub amount: u64, + pub merkle_tree: u8, + pub delegate: u8, // TODO: check whether we need delegate is set + pub mint: u8, + pub version: u8, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, AnchorSerialize, AnchorDeserialize)] +#[repr(u8)] +pub enum CompressionMode { + Compress = COMPRESS, + Decompress = DECOMPRESS, + // CompressFull = COMPRESS_FULL, // Ignores the amount, we keep the amount for efficient zero copy + //CompressAndClose = COMPRESS_AND_CLOSE, // Compresses the token and closes the account +} + +pub const COMPRESS: u8 = 0u8; +pub const DECOMPRESS: u8 = 1u8; +//pub const COMPRESS_FULL: u8 = 2u8; +//pub const COMPRESS_AND_CLOSE: u8 = 3u8; + +impl Deserialize<'_> for CompressionMode { + type Output = CompressionMode; + fn zero_copy_at( + bytes: &'_ [u8], + ) -> Result<(Self::Output, &'_ [u8]), light_zero_copy::errors::ZeroCopyError> { + let (mode, bytes) = bytes.split_at(1); + let enm = match mode[0] { + COMPRESS => Ok(CompressionMode::Compress), + DECOMPRESS => Ok(CompressionMode::Decompress), + // COMPRESS_FULL => Ok(CompressionMode::CompressFull), + // COMPRESS_AND_CLOSE => Ok(CompressionMode::CompressAndClose), + // TODO: add enum error + _ => Err(light_zero_copy::errors::ZeroCopyError::IterFromOutOfBounds), + }?; + Ok((enm, bytes)) + } +} + +impl<'a> DeserializeMut<'a> for CompressionMode { + type Output = Ref<&'a mut [u8], u8>; + fn zero_copy_at_mut( + bytes: &'a mut [u8], + ) -> Result<(Self::Output, &'a mut [u8]), light_zero_copy::errors::ZeroCopyError> { + let (mode, bytes) = zerocopy::Ref::<&mut [u8], u8>::from_prefix(bytes)?; + + Ok((mode, bytes)) + } +} + +impl<'a> ZeroCopyNew<'a> for CompressionMode { + type ZeroCopyConfig = (); + type Output = Ref<&'a mut [u8], u8>; + + fn byte_len(_config: &Self::ZeroCopyConfig) -> usize { + 1 // CompressionMode is always 1 byte + } + + fn new_zero_copy( + bytes: &'a mut [u8], + _config: Self::ZeroCopyConfig, + ) -> Result<(Self::Output, &'a mut [u8]), light_zero_copy::errors::ZeroCopyError> { + let (mode, remaining_bytes) = zerocopy::Ref::<&mut [u8], u8>::from_prefix(bytes)?; + + Ok((mode, remaining_bytes)) + } +} + +#[derive( + Clone, Copy, Debug, PartialEq, Eq, AnchorSerialize, AnchorDeserialize, ZeroCopy, ZeroCopyMut, +)] +pub struct Compression { + pub mode: CompressionMode, + pub amount: u64, + pub mint: u8, + pub source_or_recipient: u8, + pub authority: u8, // Index of owner or delegate account +} + +impl Compression { + pub fn compress(amount: u64, mint: u8, source_or_recipient: u8, authority: u8) -> Self { + Compression { + amount, + mode: CompressionMode::Compress, + mint, + source_or_recipient, + authority, + } + } + pub fn decompress(amount: u64, mint: u8, source_or_recipient: u8) -> Self { + Compression { + amount, + mode: CompressionMode::Decompress, + mint, + source_or_recipient, + authority: 0, + } + } +} + +impl ZCompressionMut<'_> { + pub fn mode(&self) -> Result { + match *self.mode { + COMPRESS => Ok(CompressionMode::Compress), + DECOMPRESS => Ok(CompressionMode::Decompress), + // COMPRESS_FULL => Ok(CompressionMode::CompressFull), + // COMPRESS_AND_CLOSE => Ok(CompressionMode::CompressAndClose), + _ => Err(CTokenError::InvalidCompressionMode), + } + } +} + +impl ZCompression<'_> { + pub fn new_balance_compressed_account(&self, current_balance: u64) -> Result { + let new_balance = match self.mode { + CompressionMode::Compress => { + // Compress: add to balance (tokens are being added to compressed pool) + current_balance + .checked_add((*self.amount).into()) + .ok_or(CTokenError::ArithmeticOverflow) + } + CompressionMode::Decompress => { + // Decompress: subtract from balance (tokens are being removed from compressed pool) + current_balance + .checked_sub((*self.amount).into()) + .ok_or(CTokenError::CompressInsufficientFunds) + } // CompressionMode::CompressFull => { + // // CompressFull: add entire amount to compressed pool (amount will be set to actual balance in preprocessing) + // current_balance + // .checked_add((*self.amount).into()) + // .ok_or(CTokenError::ArithmeticOverflow) + // } + // CompressionMode::CompressAndClose => { + // // CompressAndClose: add entire amount to compressed pool (amount will be set to actual balance in preprocessing) + // current_balance + // .checked_add((*self.amount).into()) + // .ok_or(CTokenError::ArithmeticOverflow) + // } + }?; + Ok(new_balance) + } + + pub fn new_balance_solana_account(&self, current_balance: u64) -> Result { + let new_balance = match self.mode { + CompressionMode::Compress => { + // Compress: add to balance (tokens are being added to compressed pool) + current_balance + .checked_sub((*self.amount).into()) + .ok_or(CTokenError::ArithmeticOverflow) + } + CompressionMode::Decompress => { + // Decompress: subtract from balance (tokens are being removed from compressed pool) + current_balance + .checked_add((*self.amount).into()) + .ok_or(CTokenError::CompressInsufficientFunds) + } // CompressionMode::CompressFull => { + // // CompressFull: subtract entire amount from solana account (amount will be set to actual balance in preprocessing) + // current_balance + //// .checked_sub((*self.amount).into()) + // .ok_or(CTokenError::ArithmeticOverflow) + // } + // CompressionMode::CompressAndClose => { + // // CompressAndClose: subtract entire amount from solana account (amount will be set to actual balance in preprocessing) + // current_balance + // .checked_sub((*self.amount).into()) + // .ok_or(CTokenError::ArithmeticOverflow) + // } + }?; + Ok(new_balance) + } +} + +#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopy, ZeroCopyMut)] +pub struct CompressedTokenInstructionDataTransfer2 { + pub with_transaction_hash: bool, + pub with_lamports_change_account_merkle_tree_index: bool, + // Set zero if unused + pub lamports_change_account_merkle_tree_index: u8, + pub lamports_change_account_owner_index: u8, + pub proof: Option, + pub in_token_data: Vec, + pub out_token_data: Vec, + // put accounts with lamports first, stop adding values after TODO: only access by get to prevent oob errors + pub in_lamports: Option>, + // TODO: put accounts with lamports first, stop adding values after TODO: only access by get to prevent oob errors + pub out_lamports: Option>, + // TODO: put accounts with tlv first, stop adding values after TODO: only access by get to prevent oob errors + pub in_tlv: Option>>, + pub out_tlv: Option>>, + pub compressions: Option>, + pub cpi_context: Option, +} + +/// Validate instruction data consistency (lamports and TLV checks) +pub fn validate_instruction_data( + inputs: &ZCompressedTokenInstructionDataTransfer2, +) -> Result<(), crate::CTokenError> { + if let Some(ref in_lamports) = inputs.in_lamports { + if in_lamports.len() != inputs.in_token_data.len() { + msg!( + "in_lamports {} != inputs in_token_data {}", + in_lamports.len(), + inputs.in_token_data.len() + ); + return Err(CTokenError::InputAccountsLamportsLengthMismatch); + } + } + if let Some(ref out_lamports) = inputs.out_lamports { + if out_lamports.len() != inputs.out_token_data.len() { + msg!( + "outlamports {} != inputs out_token_data {}", + out_lamports.len(), + inputs.out_token_data.len() + ); + return Err(CTokenError::OutputAccountsLamportsLengthMismatch); + } + } + if inputs.in_tlv.is_some() { + return Err(CTokenError::CompressedTokenAccountTlvUnimplemented); + } + if inputs.out_tlv.is_some() { + return Err(CTokenError::CompressedTokenAccountTlvUnimplemented); + } + Ok(()) +} diff --git a/program-libs/ctoken-types/src/lib.rs b/program-libs/ctoken-types/src/lib.rs new file mode 100644 index 0000000000..b054a01134 --- /dev/null +++ b/program-libs/ctoken-types/src/lib.rs @@ -0,0 +1,28 @@ +pub mod instructions; + +pub mod context; + +pub mod error; + +pub use error::*; +pub mod state; + +// Re-export Pubkey type +#[cfg(feature = "anchor")] +use anchor_lang::{AnchorDeserialize, AnchorSerialize}; +#[cfg(not(feature = "anchor"))] +use borsh::{BorshDeserialize as AnchorDeserialize, BorshSerialize as AnchorSerialize}; +use light_macros::pubkey_array; + +pub const CPI_AUTHORITY: [u8; 32] = pubkey_array!("GXtd2izAiMJPwMEjfgTRH3d7k9mjn4Jq3JrWFv9gySYy"); +pub const COMPRESSED_TOKEN_PROGRAM_ID: [u8; 32] = + pubkey_array!("cTokenmWW8bLPjZEBAUgYy3zKxQZW6VKi7bqNFEVv3m"); + +/// Account size constants +/// Size of a basic SPL token account +pub const BASIC_TOKEN_ACCOUNT_SIZE: u64 = 165; + +/// Size of a token account with compressible extension +pub const COMPRESSIBLE_TOKEN_ACCOUNT_SIZE: u64 = 257; +pub const COMPRESSED_MINT_SEED: &[u8] = b"compressed_mint"; +pub const NATIVE_MINT: [u8; 32] = pubkey_array!("So11111111111111111111111111111111111111112"); diff --git a/program-libs/ctoken-types/src/state/extensions/compressible.rs b/program-libs/ctoken-types/src/state/extensions/compressible.rs new file mode 100644 index 0000000000..64f8074e73 --- /dev/null +++ b/program-libs/ctoken-types/src/state/extensions/compressible.rs @@ -0,0 +1,85 @@ +use light_compressed_account::Pubkey; +use light_zero_copy::{ZeroCopy, ZeroCopyMut}; +use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout}; + +use crate::{AnchorDeserialize, AnchorSerialize}; + +/// Compressible extension for token accounts +/// Contains timing data for compression/decompression and rent authority +#[derive( + Debug, + Clone, + Copy, + PartialEq, + Eq, + AnchorSerialize, + AnchorDeserialize, + ZeroCopy, + ZeroCopyMut, + KnownLayout, + Immutable, + FromBytes, + IntoBytes, +)] +#[repr(C)] +pub struct CompressibleExtension { + /// The slot when this account was last written to + pub last_written_slot: u64, + /// Number of slots that must pass before compression is allowed + pub slots_until_compression: u64, + /// Authority that can close this account (in addition to owner) + pub rent_authority: Pubkey, + pub rent_recipient: Pubkey, + // TODO: add state variable +} + +// Implement PdaTimingData trait for integration with light-protocol2's compression SDK +impl CompressibleExtension { + pub fn last_written_slot(&self) -> u64 { + self.last_written_slot + } + + pub fn slots_until_compression(&self) -> u64 { + self.slots_until_compression + } + + pub fn set_last_written_slot(&mut self, slot: u64) { + self.last_written_slot = slot; + } +} + +impl ZCompressibleExtension<'_> { + /// Get the remaining slots until compression is allowed + /// Returns 0 if compression is already allowed + #[cfg(target_os = "solana")] + pub fn remaining_slots(&self) -> Result { + let current_slot = { + use pinocchio::sysvars::{clock::Clock, Sysvar}; + Clock::get() + .map_err(|_| crate::CTokenError::SysvarAccessError)? + .slot + }; + let target_slot = self.last_written_slot + self.slots_until_compression; + Ok(u64::from(target_slot).saturating_sub(current_slot)) + } + + /// Get the remaining slots until compression is allowed (non-Solana target) + /// Returns 0 if compression is already allowed + #[cfg(not(target_os = "solana"))] + pub fn remaining_slots(&self, current_slot: u64) -> u64 { + let target_slot = self.last_written_slot + self.slots_until_compression; + u64::from(target_slot).saturating_sub(current_slot) + } + + /// Check if the account is compressible (timing constraints have elapsed) + #[cfg(target_os = "solana")] + pub fn is_compressible(&self) -> Result { + Ok(self.remaining_slots()? == 0) + } + + /// Check if the account is compressible (timing constraints have elapsed) - non-Solana target + #[cfg(not(target_os = "solana"))] + pub fn is_compressible(&self, current_slot: u64) -> bool { + self.remaining_slots(current_slot) == 0 + } +} diff --git a/program-libs/ctoken-types/src/state/extensions/extension_struct.rs b/program-libs/ctoken-types/src/state/extensions/extension_struct.rs new file mode 100644 index 0000000000..c5027a63a2 --- /dev/null +++ b/program-libs/ctoken-types/src/state/extensions/extension_struct.rs @@ -0,0 +1,339 @@ +use light_hasher::Hasher; +use spl_pod::solana_msg::msg; + +use crate::{ + state::{ + extensions::{ + CompressibleExtension, TokenMetadata, TokenMetadataConfig, ZTokenMetadata, + ZTokenMetadataMut, + }, + CompressibleExtensionConfig, + }, + AnchorDeserialize, AnchorSerialize, CTokenError, +}; + +#[derive(Debug, Clone, PartialEq, Eq, AnchorSerialize, AnchorDeserialize)] +pub enum ExtensionStruct { + Placeholder0, + Placeholder1, + Placeholder2, + Placeholder3, + Placeholder4, + Placeholder5, + Placeholder6, + Placeholder7, + Placeholder8, + Placeholder9, + Placeholder10, + Placeholder11, + Placeholder12, + Placeholder13, + Placeholder14, + Placeholder15, + Placeholder16, + Placeholder17, + Placeholder18, // MetadataPointer(MetadataPointer), + TokenMetadata(TokenMetadata), + Placeholder20, + Placeholder21, + Placeholder22, + Placeholder23, + Placeholder24, + Placeholder25, + /// Account contains compressible timing data and rent authority + Compressible(CompressibleExtension), +} + +#[derive(Debug, Clone, PartialEq)] +pub enum ZExtensionStruct<'a> { + Placeholder0, + Placeholder1, + Placeholder2, + Placeholder3, + Placeholder4, + Placeholder5, + Placeholder6, + Placeholder7, + Placeholder8, + Placeholder9, + Placeholder10, + Placeholder11, + Placeholder12, + Placeholder13, + Placeholder14, + Placeholder15, + Placeholder16, + Placeholder17, + Placeholder18, // MetadataPointer(ZMetadataPointer<'a>), + TokenMetadata(ZTokenMetadata<'a>), + Placeholder20, + Placeholder21, + Placeholder22, + Placeholder23, + Placeholder24, + Placeholder25, + /// Account contains compressible timing data and rent authority + Compressible(>::Output), +} + +#[derive(Debug)] +pub enum ZExtensionStructMut<'a> { + Placeholder0, + Placeholder1, + Placeholder2, + Placeholder3, + Placeholder4, + Placeholder5, + Placeholder6, + Placeholder7, + Placeholder8, + Placeholder9, + Placeholder10, + Placeholder11, + Placeholder12, + Placeholder13, + Placeholder14, + Placeholder15, + Placeholder16, + Placeholder17, + Placeholder18, // MetadataPointer(ZMetadataPointerMut<'a>), + TokenMetadata(ZTokenMetadataMut<'a>), + Placeholder20, + Placeholder21, + Placeholder22, + Placeholder23, + Placeholder24, + Placeholder25, + /// Account contains compressible timing data and rent authority + Compressible(>::Output), +} + +// Manual implementation of zero-copy traits for ExtensionStruct +impl<'a> light_zero_copy::borsh::Deserialize<'a> for ExtensionStruct { + type Output = ZExtensionStruct<'a>; + + fn zero_copy_at( + data: &'a [u8], + ) -> Result<(Self::Output, &'a [u8]), light_zero_copy::errors::ZeroCopyError> { + // Read discriminant (first 1 byte for borsh enum) + if data.is_empty() { + return Err(light_zero_copy::errors::ZeroCopyError::ArraySize( + 1, + data.len(), + )); + } + + let discriminant = data[0]; + let remaining_data = &data[1..]; + match discriminant { + /* 18 => { + // MetadataPointer variant + let (metadata_pointer, remaining_bytes) = + MetadataPointer::zero_copy_at(remaining_data)?; + Ok(( + ZExtensionStruct::MetadataPointer(metadata_pointer), + remaining_bytes, + )) + }*/ + 19 => { + let (token_metadata, remaining_bytes) = + TokenMetadata::zero_copy_at(remaining_data)?; + Ok(( + ZExtensionStruct::TokenMetadata(token_metadata), + remaining_bytes, + )) + } + 26 => { + // Compressible variant + let (compressible_ext, remaining_bytes) = + CompressibleExtension::zero_copy_at(remaining_data)?; + Ok(( + ZExtensionStruct::Compressible(compressible_ext), + remaining_bytes, + )) + } + _ => Err(light_zero_copy::errors::ZeroCopyError::InvalidConversion), + } + } +} + +impl<'a> light_zero_copy::borsh_mut::DeserializeMut<'a> for ExtensionStruct { + type Output = ZExtensionStructMut<'a>; + + fn zero_copy_at_mut( + data: &'a mut [u8], + ) -> Result<(Self::Output, &'a mut [u8]), light_zero_copy::errors::ZeroCopyError> { + // Read discriminant (first 1 byte for borsh enum) + if data.is_empty() { + return Err(light_zero_copy::errors::ZeroCopyError::ArraySize( + 1, + data.len(), + )); + } + + let discriminant = data[0]; + let remaining_data = &mut data[1..]; + match discriminant { + /* 18 => { + // MetadataPointer variant + let (metadata_pointer, remaining_bytes) = + MetadataPointer::zero_copy_at_mut(remaining_data)?; + Ok(( + ZExtensionStructMut::MetadataPointer(metadata_pointer), + remaining_bytes, + )) + }*/ + 19 => { + let (token_metadata, remaining_bytes) = + TokenMetadata::zero_copy_at_mut(remaining_data)?; + Ok(( + ZExtensionStructMut::TokenMetadata(token_metadata), + remaining_bytes, + )) + } + 26 => { + // Compressible variant + let (compressible_ext, remaining_bytes) = + CompressibleExtension::zero_copy_at_mut(remaining_data)?; + Ok(( + ZExtensionStructMut::Compressible(compressible_ext), + remaining_bytes, + )) + } + _ => Err(light_zero_copy::errors::ZeroCopyError::InvalidConversion), + } + } +} + +impl<'a> light_zero_copy::ZeroCopyNew<'a> for ExtensionStruct { + type ZeroCopyConfig = ExtensionStructConfig; + type Output = ZExtensionStructMut<'a>; + // TODO: return Result + fn byte_len(config: &Self::ZeroCopyConfig) -> usize { + match config { + /* ExtensionStructConfig::MetadataPointer(metadata_config) => { + // 1 byte for discriminant + MetadataPointer size + 1 + MetadataPointer::byte_len(metadata_config) + } */ + ExtensionStructConfig::TokenMetadata(token_metadata_config) => { + // 1 byte for discriminant + TokenMetadata size + 1 + TokenMetadata::byte_len(token_metadata_config) + } + ExtensionStructConfig::Compressible => { + // 1 byte for discriminant + CompressibleExtension size + 1 + std::mem::size_of::() + } + _ => { + msg!("Invalid extension type returning 0"); + 0 + } + } + } + + fn new_zero_copy( + bytes: &'a mut [u8], + config: Self::ZeroCopyConfig, + ) -> Result<(Self::Output, &'a mut [u8]), light_zero_copy::errors::ZeroCopyError> { + match config { + /* ExtensionStructConfig::MetadataPointer(metadata_config) => { + // Write discriminant (18 for MetadataPointer) + if bytes.is_empty() { + return Err(light_zero_copy::errors::ZeroCopyError::ArraySize( + 1, + bytes.len(), + )); + } + bytes[0] = 18u8; + + // Create MetadataPointer at offset 1 + let (metadata_pointer, remaining_bytes) = + MetadataPointer::new_zero_copy(&mut bytes[1..], metadata_config)?; + Ok(( + ZExtensionStructMut::MetadataPointer(metadata_pointer), + remaining_bytes, + )) + } */ + ExtensionStructConfig::TokenMetadata(config) => { + // Write discriminant (19 for TokenMetadata) + if bytes.is_empty() { + return Err(light_zero_copy::errors::ZeroCopyError::ArraySize( + 1, + bytes.len(), + )); + } + bytes[0] = 19u8; + + let (token_metadata, remaining_bytes) = + TokenMetadata::new_zero_copy(&mut bytes[1..], config)?; + Ok(( + ZExtensionStructMut::TokenMetadata(token_metadata), + remaining_bytes, + )) + } + ExtensionStructConfig::Compressible => { + // Write discriminant (26 for Compressible) + if bytes.is_empty() { + return Err(light_zero_copy::errors::ZeroCopyError::ArraySize( + 1, + bytes.len(), + )); + } + bytes[0] = 26u8; + + let (compressible_ext, remaining_bytes) = CompressibleExtension::new_zero_copy( + &mut bytes[1..], + CompressibleExtensionConfig {}, + )?; + Ok(( + ZExtensionStructMut::Compressible(compressible_ext), + remaining_bytes, + )) + } + _ => Err(light_zero_copy::errors::ZeroCopyError::InvalidConversion), + } + } +} + +#[derive(Debug, Clone, PartialEq)] +pub enum ExtensionStructConfig { + Placeholder0, + Placeholder1, + Placeholder2, + Placeholder3, + Placeholder4, + Placeholder5, + Placeholder6, + Placeholder7, + Placeholder8, + Placeholder9, + Placeholder10, + Placeholder11, + Placeholder12, + Placeholder13, + Placeholder14, + Placeholder15, + Placeholder16, + Placeholder17, + Placeholder18, // MetadataPointer(MetadataPointerConfig), + TokenMetadata(TokenMetadataConfig), + Placeholder20, + Placeholder21, + Placeholder22, + Placeholder23, + Placeholder24, + Placeholder25, + Compressible, +} + +impl ExtensionStruct { + pub fn hash(&self) -> Result<[u8; 32], CTokenError> { + match self { + // ExtensionStruct::MetadataPointer(metadata_pointer) => Ok(metadata_pointer.hash::()?), + ExtensionStruct::TokenMetadata(token_metadata) => { + // hash function is defined on the metadata level + Ok(token_metadata.hash()?) + } + _ => Err(CTokenError::UnsupportedExtension), + } + } +} diff --git a/program-libs/ctoken-types/src/state/extensions/extension_type.rs b/program-libs/ctoken-types/src/state/extensions/extension_type.rs new file mode 100644 index 0000000000..b499f579d0 --- /dev/null +++ b/program-libs/ctoken-types/src/state/extensions/extension_type.rs @@ -0,0 +1,111 @@ +use crate::{AnchorDeserialize, AnchorSerialize}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, AnchorDeserialize, AnchorSerialize)] +#[repr(u8)] // Note: token22 uses u16 +pub enum ExtensionType { + // /// Used as padding if the account size would otherwise be 355, same as a + // /// multisig + // Uninitialized, + // /// Includes transfer fee rate info and accompanying authorities to withdraw + // /// and set the fee + // TransferFeeConfig, + // /// Includes withheld transfer fees + // TransferFeeAmount, + // /// Includes an optional mint close authority + // MintCloseAuthority, + // /// Auditor configuration for confidential transfers + // ConfidentialTransferMint, + // /// State for confidential transfers + // ConfidentialTransferAccount, + // /// Specifies the default Account::state for new Accounts + // DefaultAccountState, + // /// Indicates that the Account owner authority cannot be changed + // ImmutableOwner, + // /// Require inbound transfers to have memo + // MemoTransfer, + // /// Indicates that the tokens from this mint can't be transferred + // NonTransferable, + // /// Tokens accrue interest over time, + // InterestBearingConfig, + // /// Locks privileged token operations from happening via CPI + // CpiGuard, + // /// Includes an optional permanent delegate + // PermanentDelegate, + // /// Indicates that the tokens in this account belong to a non-transferable + // /// mint + // NonTransferableAccount, + // /// Mint requires a CPI to a program implementing the "transfer hook" + // /// interface + // TransferHook, + // /// Indicates that the tokens in this account belong to a mint with a + // /// transfer hook + // TransferHookAccount, + // /// Includes encrypted withheld fees and the encryption public that they are + // /// encrypted under + // ConfidentialTransferFeeConfig, + // /// Includes confidential withheld transfer fees + // ConfidentialTransferFeeAmount, + /// Mint contains a pointer to another account (or the same account) that + /// holds metadata. Must not point to itself. + Placeholder0, + Placeholder1, + Placeholder2, + Placeholder3, + Placeholder4, + Placeholder5, + Placeholder6, + Placeholder7, + Placeholder8, + Placeholder9, + Placeholder10, + Placeholder11, + Placeholder12, + Placeholder13, + Placeholder14, + Placeholder15, + Placeholder16, + Placeholder17, + Placeholder18, //MetadataPointer = 18, + /// Mint contains token-metadata. + /// Unlike token22 there is no metadata pointer. + TokenMetadata = 19, + Placeholder20, + Placeholder21, + Placeholder22, + Placeholder23, + Placeholder24, + Placeholder25, + // /// Mint contains a pointer to another account (or the same account) that + // /// holds group configurations + // GroupPointer, + // /// Mint contains token group configurations + // TokenGroup, + // /// Mint contains a pointer to another account (or the same account) that + // /// holds group member configurations + // GroupMemberPointer, + // /// Mint contains token group member configurations + // TokenGroupMember, + // /// Mint allowing the minting and burning of confidential tokens + // ConfidentialMintBurn, + // /// Tokens whose UI amount is scaled by a given amount + // ScaledUiAmount, + // /// Tokens where minting / burning / transferring can be paused + // Pausable, + // /// Indicates that the account belongs to a pausable mint + // PausableAccount, + /// Account contains compressible timing data and rent authority + Compressible = 26, +} + +impl TryFrom for ExtensionType { + type Error = crate::CTokenError; + + fn try_from(value: u8) -> Result { + match value { + // 18 => Ok(ExtensionType::MetadataPointer), + 19 => Ok(ExtensionType::TokenMetadata), + 26 => Ok(ExtensionType::Compressible), + _ => Err(crate::CTokenError::UnsupportedExtension), + } + } +} diff --git a/program-libs/ctoken-types/src/state/extensions/mod.rs b/program-libs/ctoken-types/src/state/extensions/mod.rs new file mode 100644 index 0000000000..77ef196caa --- /dev/null +++ b/program-libs/ctoken-types/src/state/extensions/mod.rs @@ -0,0 +1,9 @@ +mod extension_struct; +mod extension_type; + +pub use extension_struct::*; +pub use extension_type::*; +mod token_metadata; +pub use token_metadata::*; +pub mod compressible; +pub use compressible::*; diff --git a/program-libs/ctoken-types/src/state/extensions/token_metadata.rs b/program-libs/ctoken-types/src/state/extensions/token_metadata.rs new file mode 100644 index 0000000000..3613a6932a --- /dev/null +++ b/program-libs/ctoken-types/src/state/extensions/token_metadata.rs @@ -0,0 +1,310 @@ +use light_compressed_account::Pubkey; +use light_hasher::{ + hash_to_field_size::hashv_to_bn254_field_size_be_const_array, DataHasher, HasherError, + Poseidon, Sha256, +}; +use light_zero_copy::{ZeroCopy, ZeroCopyMut}; +use pinocchio::msg; + +use crate::{AnchorDeserialize, AnchorSerialize}; + +// TODO: decide whether to keep Shaflat +pub enum Version { + Poseidon, + Sha256, + Keccak256, + Sha256Flat, +} + +impl TryFrom for Version { + type Error = HasherError; + + fn try_from(value: u8) -> Result { + match value { + 0 => Ok(Version::Poseidon), + 1 => Ok(Version::Sha256), + // 2 => Ok(Version::Keccak256), + // 3 => Ok(Version::Sha256Flat), + // TODO: use real error + _ => Err(HasherError::InvalidInputLength(value as usize, 3)), + } + } +} +// TODO: impl string for zero copy +// TODO: test deserialization equivalence +/// Used for onchain serialization +#[derive( + Debug, Clone, PartialEq, Eq, AnchorSerialize, AnchorDeserialize, ZeroCopy, ZeroCopyMut, +)] +pub struct TokenMetadata { + // TODO: decide whether to move down for more efficient zero copy. Or impl manual zero copy. + /// The authority that can sign to update the metadata + pub update_authority: Option, + // TODO: decide whether to keep this. + /// The associated mint, used to counter spoofing to be sure that metadata + /// belongs to a particular mint + pub mint: Pubkey, + pub metadata: Metadata, + /// Any additional metadata about the token as key-value pairs. The program + /// must avoid storing the same key twice. + pub additional_metadata: Vec, + // TODO: decide whether to do this on this or MintAccount level + /// 0: Poseidon, 1: Sha256, 2: Keccak256, 3: Sha256Flat + pub version: u8, +} + +impl TokenMetadata { + pub fn hash(&self) -> Result<[u8; 32], HasherError> { + match Version::try_from(self.version)? { + Version::Poseidon => { + msg!("poseidon"); + ::hash::(self) + } + Version::Sha256 => { + msg!("sha256"); + ::hash::(self) + } + _ => unimplemented!("TokenMetadata hash version not supported {}", self.version), + // Version::Keccak256 => ::hash::(self), + // Version::Sha256Flat => self.sha_flat(), + } + } +} + +pub fn token_metadata_hash( + update_authority: Option<&[u8]>, + mint: &[u8], + metadata_hash: &[u8], + additional_metadata: &[(&[u8], &[u8])], + version: u8, +) -> Result<[u8; 32], HasherError> { + let mut vec = [[0u8; 32]; 5]; + let mut slice_vec: [&[u8]; 5] = [&[]; 5]; + + if let Some(update_authority) = update_authority { + vec[0].copy_from_slice( + hashv_to_bn254_field_size_be_const_array::<2>(&[update_authority])?.as_slice(), + ); + } + + vec[1] = hashv_to_bn254_field_size_be_const_array::<2>(&[mint])?; + + for (key, value) in additional_metadata { + // TODO: add check is poseidon and throw meaningful error. + vec[3] = H::hashv(&[vec[3].as_slice(), key, value])?; + } + vec[4][31] = version; + + slice_vec[0] = vec[0].as_slice(); + slice_vec[1] = vec[2].as_slice(); + slice_vec[2] = metadata_hash; + slice_vec[3] = vec[3].as_slice(); + slice_vec[4] = vec[4].as_slice(); + + if vec[4] != [0u8; 32] { + H::hashv(&slice_vec[..4]) + } else { + H::hashv(slice_vec.as_slice()) + } +} + +pub fn token_metadata_hash_with_hashed_values( + hashed_update_authority: Option<&[u8; 32]>, + hashed_mint: &[u8; 32], + metadata_hash: &[u8], + additional_metadata: &[(&[u8], &[u8])], + version: u8, +) -> Result<[u8; 32], HasherError> { + let mut vec = [[0u8; 32]; 5]; + let mut slice_vec: [&[u8]; 5] = [&[]; 5]; + + if let Some(hashed_update_authority) = hashed_update_authority { + vec[0] = *hashed_update_authority; + } + + vec[1] = *hashed_mint; + + for (key, value) in additional_metadata { + // TODO: add check is poseidon and throw meaningful error. + vec[3] = H::hashv(&[vec[3].as_slice(), key, value])?; + } + vec[4][31] = version; + + slice_vec[0] = vec[0].as_slice(); + slice_vec[1] = vec[2].as_slice(); + slice_vec[2] = metadata_hash; + slice_vec[3] = vec[3].as_slice(); + slice_vec[4] = vec[4].as_slice(); + + if vec[4] != [0u8; 32] { + H::hashv(&slice_vec[..4]) + } else { + H::hashv(slice_vec.as_slice()) + } +} + +impl DataHasher for TokenMetadata { + fn hash(&self) -> Result<[u8; 32], HasherError> { + let metadata_hash = light_hasher::DataHasher::hash::(&self.metadata)?; + let additional_metadata: arrayvec::ArrayVec<(&[u8], &[u8]), 32> = self + .additional_metadata + .iter() + .map(|item| (item.key.as_slice(), item.value.as_slice())) + .collect(); + + token_metadata_hash::( + self.update_authority.as_ref().map(|auth| (*auth).as_ref()), + self.mint.as_ref(), + metadata_hash.as_slice(), + &additional_metadata, + self.version, + ) + } +} + +impl DataHasher for ZTokenMetadataMut<'_> { + fn hash(&self) -> Result<[u8; 32], HasherError> { + let metadata_hash = light_hasher::DataHasher::hash::(&self.metadata)?; + let additional_metadata: arrayvec::ArrayVec<(&[u8], &[u8]), 32> = self + .additional_metadata + .iter() + .map(|item| (&*item.key, &*item.value)) + .collect(); + + token_metadata_hash::( + self.update_authority.as_ref().map(|auth| (*auth).as_ref()), + self.mint.as_ref(), + metadata_hash.as_slice(), + &additional_metadata, + *self.version, + ) + } +} + +impl DataHasher for ZTokenMetadata<'_> { + fn hash(&self) -> Result<[u8; 32], HasherError> { + let metadata_hash = light_hasher::DataHasher::hash::(&self.metadata)?; + let additional_metadata: arrayvec::ArrayVec<(&[u8], &[u8]), 32> = self + .additional_metadata + .iter() + .map(|item| (item.key, item.value)) + .collect(); + + token_metadata_hash::( + self.update_authority.as_ref().map(|auth| (*auth).as_ref()), + self.mint.as_ref(), + metadata_hash.as_slice(), + &additional_metadata, + self.version, + ) + } +} + +// TODO: if version 0 we check all string len for less than 31 bytes +#[derive( + Debug, Clone, PartialEq, Eq, AnchorSerialize, AnchorDeserialize, ZeroCopy, ZeroCopyMut, +)] +pub struct Metadata { + /// The longer name of the token + pub name: Vec, + /// The shortened symbol for the token + pub symbol: Vec, + /// The URI pointing to richer metadata + pub uri: Vec, +} + +// Manual LightHasher implementation for Metadata struct +impl light_hasher::to_byte_array::ToByteArray for Metadata { + const NUM_FIELDS: usize = 3; + + fn to_byte_array(&self) -> Result<[u8; 32], light_hasher::HasherError> { + light_hasher::DataHasher::hash::(self) + } +} + +impl light_hasher::DataHasher for Metadata { + fn hash(&self) -> Result<[u8; 32], light_hasher::HasherError> + where + H: light_hasher::Hasher, + { + use light_hasher::hash_to_field_size::hash_to_bn254_field_size_be; + + // Hash each Vec field using as_slice() and hash_to_bn254_field_size_be for consistency + let name_hash = hash_to_bn254_field_size_be(self.name.as_slice()); + let symbol_hash = hash_to_bn254_field_size_be(self.symbol.as_slice()); + let uri_hash = hash_to_bn254_field_size_be(self.uri.as_slice()); + + H::hashv(&[ + name_hash.as_slice(), + symbol_hash.as_slice(), + uri_hash.as_slice(), + ]) + } +} + +// Manual LightHasher implementation for ZMetadata ZStruct +impl light_hasher::to_byte_array::ToByteArray for ZMetadata<'_> { + const NUM_FIELDS: usize = 3; + + fn to_byte_array(&self) -> Result<[u8; 32], light_hasher::HasherError> { + light_hasher::DataHasher::hash::(self) + } +} + +impl light_hasher::DataHasher for ZMetadata<'_> { + fn hash(&self) -> Result<[u8; 32], light_hasher::HasherError> + where + H: light_hasher::Hasher, + { + use light_hasher::hash_to_field_size::hash_to_bn254_field_size_be; + + // Hash each &[u8] slice field using hash_to_bn254_field_size_be for consistency + let name_hash = hash_to_bn254_field_size_be(self.name); + let symbol_hash = hash_to_bn254_field_size_be(self.symbol); + let uri_hash = hash_to_bn254_field_size_be(self.uri); + + H::hashv(&[ + name_hash.as_slice(), + symbol_hash.as_slice(), + uri_hash.as_slice(), + ]) + } +} + +impl light_hasher::to_byte_array::ToByteArray for ZMetadataMut<'_> { + const NUM_FIELDS: usize = 3; + + fn to_byte_array(&self) -> Result<[u8; 32], light_hasher::HasherError> { + light_hasher::DataHasher::hash::(self) + } +} + +impl light_hasher::DataHasher for ZMetadataMut<'_> { + fn hash(&self) -> Result<[u8; 32], light_hasher::HasherError> + where + H: light_hasher::Hasher, + { + use light_hasher::hash_to_field_size::hash_to_bn254_field_size_be; + + // Hash each &[u8] slice field using hash_to_bn254_field_size_be for consistency + let name_hash = hash_to_bn254_field_size_be(self.name); + let symbol_hash = hash_to_bn254_field_size_be(self.symbol); + let uri_hash = hash_to_bn254_field_size_be(self.uri); + + H::hashv(&[ + name_hash.as_slice(), + symbol_hash.as_slice(), + uri_hash.as_slice(), + ]) + } +} + +#[derive( + Debug, Clone, PartialEq, Eq, AnchorSerialize, AnchorDeserialize, ZeroCopy, ZeroCopyMut, +)] +pub struct AdditionalMetadata { + /// The key of the metadata + pub key: Vec, + /// The value of the metadata + pub value: Vec, +} diff --git a/program-libs/ctoken-types/src/state/mint.rs b/program-libs/ctoken-types/src/state/mint.rs new file mode 100644 index 0000000000..27dc844758 --- /dev/null +++ b/program-libs/ctoken-types/src/state/mint.rs @@ -0,0 +1,239 @@ +use light_compressed_account::{hash_to_bn254_field_size_be, Pubkey}; +use light_hasher::{errors::HasherError, Hasher, Poseidon}; +use light_zero_copy::{ZeroCopy, ZeroCopyMut}; +use zerocopy::{little_endian::U64, IntoBytes}; + +use crate::{ + context::TokenContext, state::ExtensionStruct, AnchorDeserialize, AnchorSerialize, CTokenError, +}; + +// Order is optimized for hashing. +// freeze_authority option is skipped if None. +#[derive( + Debug, PartialEq, Eq, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopyMut, ZeroCopy, +)] +pub struct CompressedMint { + /// Version for upgradability + pub version: u8, + /// Pda with seed address of compressed mint + pub spl_mint: Pubkey, + /// Total supply of tokens. + pub supply: u64, + /// Number of base 10 digits to the right of the decimal place. + pub decimals: u8, + /// Extension, necessary for mint to. + pub is_decompressed: bool, + /// Optional authority used to mint new tokens. The mint authority may only + /// be provided during mint creation. If no mint authority is present + /// then the mint has a fixed supply and no further tokens may be + /// minted. + pub mint_authority: Option, + /// Optional authority to freeze token accounts. + pub freeze_authority: Option, + pub extensions: Option>, +} + +// use nested token metadata layout for data extension +// pub extension_hash: [u8; 32], +impl CompressedMint { + #[allow(dead_code)] + pub fn hash(&self) -> std::result::Result<[u8; 32], CTokenError> { + let hashed_spl_mint = hash_to_bn254_field_size_be(self.spl_mint.to_bytes().as_slice()); + let mut supply_bytes = [0u8; 32]; + supply_bytes[24..].copy_from_slice(self.supply.to_be_bytes().as_slice()); + + let hashed_mint_authority; + let hashed_mint_authority_option = if let Some(mint_authority) = self.mint_authority { + hashed_mint_authority = + hash_to_bn254_field_size_be(mint_authority.to_bytes().as_slice()); + Some(&hashed_mint_authority) + } else { + None + }; + + let hashed_freeze_authority; + let hashed_freeze_authority_option = if let Some(freeze_authority) = self.freeze_authority { + hashed_freeze_authority = + hash_to_bn254_field_size_be(freeze_authority.to_bytes().as_slice()); + Some(&hashed_freeze_authority) + } else { + None + }; + + let mint_hash = Self::hash_with_hashed_values( + &hashed_spl_mint, + &supply_bytes, + self.decimals, + self.is_decompressed, + &hashed_mint_authority_option, + &hashed_freeze_authority_option, + self.version, + )?; + // TODO: consider to make hasher generic. could use version for that. + if let Some(extensions) = self.extensions.as_ref() { + let mut extension_hashchain = [0u8; 32]; + for extension in extensions { + extension_hashchain = Poseidon::hashv(&[ + extension_hashchain.as_slice(), + extension.hash::()?.as_slice(), + ])?; + } + Ok(Poseidon::hashv(&[ + mint_hash.as_slice(), + extension_hashchain.as_slice(), + ])?) + } else { + Ok(mint_hash) + } + } + + pub fn hash_with_hashed_values( + hashed_spl_mint: &[u8; 32], + supply_bytes: &[u8; 32], + decimals: u8, + is_decompressed: bool, + hashed_mint_authority: &Option<&[u8; 32]>, + hashed_freeze_authority: &Option<&[u8; 32]>, + version: u8, + ) -> std::result::Result<[u8; 32], HasherError> { + let mut hash_inputs = vec![hashed_spl_mint.as_slice(), supply_bytes.as_slice()]; + + // Add decimals with prefix if not 0 + let mut decimals_bytes = [0u8; 32]; + if decimals != 0 { + decimals_bytes[30] = 1; // decimals prefix + decimals_bytes[31] = decimals; + hash_inputs.push(&decimals_bytes[..]); + } + + // Add is_decompressed with prefix if true + let mut is_decompressed_bytes = [0u8; 32]; + if is_decompressed { + is_decompressed_bytes[30] = 2; // is_decompressed prefix + is_decompressed_bytes[31] = 1; // true as 1 + hash_inputs.push(&is_decompressed_bytes[..]); + } + + // Add mint authority if present + if let Some(hashed_mint_authority) = hashed_mint_authority { + hash_inputs.push(hashed_mint_authority.as_slice()); + } + + // Add freeze authority if present + let empty_authority = [0u8; 32]; + if let Some(hashed_freeze_authority) = hashed_freeze_authority { + // If there is freeze authority but no mint authority, add empty mint authority + if hashed_mint_authority.is_none() { + hash_inputs.push(&empty_authority[..]); + } + hash_inputs.push(hashed_freeze_authority.as_slice()); + } + + // Add version with prefix if not 0 + let mut num_extensions_bytes = [0u8; 32]; + if version != 0 { + num_extensions_bytes[30] = 3; // version prefix + num_extensions_bytes[31] = version; + hash_inputs.push(&num_extensions_bytes[..]); + } + + Poseidon::hashv(hash_inputs.as_slice()) + } +} + +impl ZCompressedMintMut<'_> { + pub fn hash( + &self, + extension_hashchain: Option<[u8; 32]>, + context: &mut TokenContext, + ) -> std::result::Result<[u8; 32], CTokenError> { + // let hashed_spl_mint = hash_to_bn254_field_size_be(self.spl_mint.to_bytes().as_slice()); + let hashed_spl_mint = context.get_or_hash_mint(&self.spl_mint.into())?; + let mut supply_bytes = [0u8; 32]; + // TODO: copy from slice + self.supply + .as_bytes() + .iter() + .rev() + .zip(supply_bytes[24..].iter_mut()) + .for_each(|(x, y)| *y = *x); + + let hashed_mint_authority; + let hashed_mint_authority_option = + if let Some(mint_authority) = self.mint_authority.as_ref() { + hashed_mint_authority = context.get_or_hash_pubkey(&(*mint_authority).to_bytes()); + // hash_to_bn254_field_size_be(mint_authority.to_bytes().as_slice()); + Some(&hashed_mint_authority) + } else { + None + }; + + let hashed_freeze_authority; + let hashed_freeze_authority_option = if let Some(freeze_authority) = + self.freeze_authority.as_ref() + { + hashed_freeze_authority = context.get_or_hash_pubkey(&(*freeze_authority).to_bytes()); + // hash_to_bn254_field_size_be(freeze_authority.to_bytes().as_slice()); + Some(&hashed_freeze_authority) + } else { + None + }; + + let mint_hash = CompressedMint::hash_with_hashed_values( + &hashed_spl_mint, + &supply_bytes, + self.decimals, + self.is_decompressed(), + &hashed_mint_authority_option, + &hashed_freeze_authority_option, + self.version, + )?; + if let Some(extension_hashchain) = extension_hashchain { + Ok(Poseidon::hashv(&[ + mint_hash.as_slice(), + extension_hashchain.as_slice(), + ])?) + } else { + Ok(mint_hash) + } + } +} +// Implementation for zero-copy mutable CompressedMint +impl ZCompressedMintMut<'_> { + /// Set all fields of the CompressedMint struct at once + #[inline] + #[allow(clippy::too_many_arguments)] + pub fn set( + &mut self, + version: u8, + spl_mint: Pubkey, + supply: U64, + decimals: u8, + is_decompressed: bool, + mint_authority: Option, + freeze_authority: Option, + ) -> Result<(), CTokenError> { + self.version = version; + self.spl_mint = spl_mint; + self.supply = supply; + self.decimals = decimals; + self.is_decompressed = if is_decompressed { 1 } else { 0 }; + if let Some(self_mint_authority) = self.mint_authority.as_deref_mut() { + *self_mint_authority = + mint_authority.ok_or(CTokenError::InstructionDataExpectedMintAuthority)?; + } + if self.mint_authority.is_some() && mint_authority.is_none() { + return Err(CTokenError::ZeroCopyExpectedMintAuthority); + } + + if let Some(self_freeze_authority) = self.freeze_authority.as_deref_mut() { + *self_freeze_authority = + freeze_authority.ok_or(CTokenError::InstructionDataExpectedFreezeAuthority)?; + } + if self.freeze_authority.is_some() && freeze_authority.is_none() { + return Err(CTokenError::ZeroCopyExpectedFreezeAuthority); + } + // extensions are handled separately as they require special processing + Ok(()) + } +} diff --git a/program-libs/ctoken-types/src/state/mod.rs b/program-libs/ctoken-types/src/state/mod.rs new file mode 100644 index 0000000000..657ac65010 --- /dev/null +++ b/program-libs/ctoken-types/src/state/mod.rs @@ -0,0 +1,9 @@ +pub mod extensions; +pub mod mint; +pub mod solana_ctoken; +pub mod token_data; + +pub use extensions::*; +pub use mint::*; +pub use solana_ctoken::*; +pub use token_data::*; diff --git a/program-libs/ctoken-types/src/state/solana_ctoken.rs b/program-libs/ctoken-types/src/state/solana_ctoken.rs new file mode 100644 index 0000000000..0283ad4d86 --- /dev/null +++ b/program-libs/ctoken-types/src/state/solana_ctoken.rs @@ -0,0 +1,672 @@ +use std::ops::{Deref, DerefMut}; + +use light_compressed_account::Pubkey; +use light_zero_copy::{ + borsh::Deserialize, borsh_mut::DeserializeMut, errors::ZeroCopyError, init_mut::ZeroCopyNew, +}; +use spl_pod::solana_msg::msg; + +use crate::{ + state::{ExtensionStruct, ExtensionStructConfig, ZExtensionStruct, ZExtensionStructMut}, + AnchorDeserialize, AnchorSerialize, +}; + +/// Compressed token account structure (same as SPL Token Account but with extensions) +#[derive(Debug, PartialEq, Eq, Clone)] +pub struct CompressedToken { + /// The mint associated with this account + pub mint: Pubkey, + /// The owner of this account. + pub owner: Pubkey, + /// The amount of tokens this account holds. + pub amount: u64, + /// If `delegate` is `Some` then `delegated_amount` represents + /// the amount authorized by the delegate + pub delegate: Option, + /// The account's state + pub state: u8, + /// If `is_some`, this is a native token, and the value logs the rent-exempt + /// reserve. An Account is required to be rent-exempt, so the value is + /// used by the Processor to ensure that wrapped SOL accounts do not + /// drop below this threshold. + pub is_native: Option, + /// The amount delegated + pub delegated_amount: u64, + /// Optional authority to close the account. + pub close_authority: Option, + /// Extensions for the token account (including compressible config) + pub extensions: Option>, +} + +#[derive(Debug, PartialEq, Eq, Clone, AnchorSerialize, AnchorDeserialize)] +pub struct CompressedTokenMeta { + /// The mint associated with this account + pub mint: Pubkey, + /// The owner of this account. + pub owner: Pubkey, + /// The amount of tokens this account holds. + pub amount: u64, + /// If `delegate` is `Some` then `delegated_amount` represents + /// the amount authorized by the delegate + pub delegate: Option, + /// The account's state + pub state: u8, + /// If `is_some`, this is a native token, and the value logs the rent-exempt + /// reserve. An Account is required to be rent-exempt, so the value is + /// used by the Processor to ensure that wrapped SOL accounts do not + /// drop below this threshold. + pub is_native: Option, + /// The amount delegated + pub delegated_amount: u64, + /// Optional authority to close the account. + pub close_authority: Option, +} + +// Note: spl zero-copy compatibility is implemented in fn zero_copy_at +#[derive(Debug, PartialEq, Clone)] +pub struct ZCompressedTokenMeta<'a> { + pub mint: >::Output, + pub owner: >::Output, + pub amount: zerocopy::Ref<&'a [u8], zerocopy::little_endian::U64>, + pub delegate: Option<>::Output>, + pub state: u8, + pub is_native: Option>, + pub delegated_amount: zerocopy::Ref<&'a [u8], zerocopy::little_endian::U64>, + pub close_authority: Option<>::Output>, +} + +#[derive(Debug, PartialEq)] +pub struct ZCompressedTokenMetaMut<'a> { + pub mint: >::Output, + pub owner: >::Output, + pub amount: zerocopy::Ref<&'a mut [u8], zerocopy::little_endian::U64>, + // 4 option bytes (spl compat) + 32 pubkey bytes + delegate_option: zerocopy::Ref<&'a mut [u8], [u8; 36]>, + pub delegate: Option<>::Output>, + pub state: zerocopy::Ref<&'a mut [u8], u8>, + // 4 option bytes (spl compat) + 8 u64 bytes + is_native_option: zerocopy::Ref<&'a mut [u8], [u8; 12]>, + pub is_native: Option>, + pub delegated_amount: zerocopy::Ref<&'a mut [u8], zerocopy::little_endian::U64>, + // 4 option bytes (spl compat) + 32 pubkey bytes + close_authority_option: zerocopy::Ref<&'a mut [u8], [u8; 36]>, + pub close_authority: Option<>::Output>, +} + +impl<'a> Deserialize<'a> for CompressedTokenMeta { + type Output = ZCompressedTokenMeta<'a>; + + fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self::Output, &'a [u8]), ZeroCopyError> { + use zerocopy::{ + little_endian::{U32 as ZU32, U64 as ZU64}, + Ref, + }; + + if bytes.len() < 165 { + // SPL Token Account size + return Err(ZeroCopyError::Size); + } + + let (mint, bytes) = Pubkey::zero_copy_at(bytes)?; + + // owner: 32 bytes + let (owner, bytes) = Pubkey::zero_copy_at(bytes)?; + + // amount: 8 bytes + let (amount, bytes) = Ref::<&[u8], ZU64>::from_prefix(bytes)?; + + // delegate: 36 bytes (4 byte COption + 32 byte pubkey) + let (delegate_option, bytes) = Ref::<&[u8], ZU32>::from_prefix(bytes)?; + let (delegate_pubkey, bytes) = Pubkey::zero_copy_at(bytes)?; + let delegate = if u32::from(*delegate_option) == 1 { + Some(delegate_pubkey) + } else { + None + }; + + // state: 1 byte + let (state, bytes) = u8::zero_copy_at(bytes)?; + + // is_native: 12 bytes (4 byte COption + 8 byte u64) + let (native_option, bytes) = Ref::<&[u8], ZU32>::from_prefix(bytes)?; + let (native_value, bytes) = Ref::<&[u8], ZU64>::from_prefix(bytes)?; + let is_native = if u32::from(*native_option) == 1 { + Some(native_value) + } else { + None + }; + + // delegated_amount: 8 bytes + let (delegated_amount, bytes) = Ref::<&[u8], ZU64>::from_prefix(bytes)?; + + // close_authority: 36 bytes (4 byte COption + 32 byte pubkey) + let (close_option, bytes) = Ref::<&[u8], ZU32>::from_prefix(bytes)?; + let (close_pubkey, bytes) = Pubkey::zero_copy_at(bytes)?; + let close_authority = if u32::from(*close_option) == 1 { + Some(close_pubkey) + } else { + None + }; + + let meta = ZCompressedTokenMeta { + mint, + owner, + amount, + delegate, + state, + is_native, + delegated_amount, + close_authority, + }; + + Ok((meta, bytes)) + } +} + +impl<'a> light_zero_copy::borsh_mut::DeserializeMut<'a> for CompressedTokenMeta { + type Output = ZCompressedTokenMetaMut<'a>; + + fn zero_copy_at_mut( + bytes: &'a mut [u8], + ) -> Result<(Self::Output, &'a mut [u8]), ZeroCopyError> { + use zerocopy::{little_endian::U64 as ZU64, Ref}; + + if bytes.len() < 165 { + return Err(ZeroCopyError::Size); + } + + let (mint, bytes) = Pubkey::zero_copy_at_mut(bytes)?; + let (owner, bytes) = Pubkey::zero_copy_at_mut(bytes)?; + let (amount, bytes) = Ref::<&mut [u8], ZU64>::from_prefix(bytes)?; + + let (mut delegate_option, bytes) = Ref::<&mut [u8], [u8; 36]>::from_prefix(bytes)?; + let pubkey_bytes = + unsafe { std::slice::from_raw_parts_mut(delegate_option.as_mut_ptr().add(4), 32) }; + let (delegate_pubkey, _) = Pubkey::zero_copy_at_mut(pubkey_bytes)?; + let delegate = if delegate_option[0] == 1 { + Some(delegate_pubkey) + } else { + None + }; + + // state: 1 byte + let (state, bytes) = Ref::<&mut [u8], u8>::from_prefix(bytes)?; + + // is_native: 12 bytes (4 byte COption + 8 byte u64) + let (mut is_native_option, bytes) = Ref::<&mut [u8], [u8; 12]>::from_prefix(bytes)?; + let value_bytes = + unsafe { std::slice::from_raw_parts_mut(is_native_option.as_mut_ptr().add(4), 8) }; + let (native_value, _) = Ref::<&mut [u8], ZU64>::from_prefix(value_bytes)?; + let is_native = if is_native_option[0] == 1 { + Some(native_value) + } else { + None + }; + + // delegated_amount: 8 bytes + let (delegated_amount, bytes) = Ref::<&mut [u8], ZU64>::from_prefix(bytes)?; + + // close_authority: 36 bytes (4 byte COption + 32 byte pubkey) + let (mut close_authority_option, bytes) = Ref::<&mut [u8], [u8; 36]>::from_prefix(bytes)?; + let pubkey_bytes = unsafe { + std::slice::from_raw_parts_mut(close_authority_option.as_mut_ptr().add(4), 32) + }; + let (close_pubkey, _) = Pubkey::zero_copy_at_mut(pubkey_bytes)?; + let close_authority = if close_authority_option[0] == 1 { + Some(close_pubkey) + } else { + None + }; + + let meta = ZCompressedTokenMetaMut { + mint, + owner, + amount, + delegate_option, + delegate, + state, + is_native_option, + is_native, + delegated_amount, + close_authority_option, + close_authority, + }; + + Ok((meta, bytes)) + } +} + +#[derive(Debug, PartialEq, Clone)] +pub struct ZCompressedToken<'a> { + __meta: ZCompressedTokenMeta<'a>, + /// Extensions for the token account (including compressible config) + pub extensions: Option>>, +} + +impl<'a> Deref for ZCompressedToken<'a> { + type Target = >::Output; + + fn deref(&self) -> &Self::Target { + &self.__meta + } +} + +// TODO: add randomized tests +impl PartialEq for ZCompressedToken<'_> { + fn eq(&self, other: &CompressedToken) -> bool { + // Compare basic fields + if self.mint.to_bytes() != other.mint.to_bytes() + || self.owner.to_bytes() != other.owner.to_bytes() + || u64::from(*self.amount) != other.amount + || self.state != other.state + || u64::from(*self.delegated_amount) != other.delegated_amount + { + return false; + } + + // Compare delegate + match (&self.delegate, &other.delegate) { + (Some(zc_delegate), Some(regular_delegate)) => { + if zc_delegate.to_bytes() != regular_delegate.to_bytes() { + return false; + } + } + (None, None) => {} + _ => return false, + } + + // Compare is_native + match (&self.is_native, &other.is_native) { + (Some(zc_native), Some(regular_native)) => { + if u64::from(**zc_native) != *regular_native { + return false; + } + } + (None, None) => {} + _ => return false, + } + + // Compare close_authority + match (&self.close_authority, &other.close_authority) { + (Some(zc_close), Some(regular_close)) => { + if zc_close.to_bytes() != regular_close.to_bytes() { + return false; + } + } + (None, None) => {} + _ => return false, + } + + // Compare extensions + match (&self.extensions, &other.extensions) { + (Some(zc_extensions), Some(regular_extensions)) => { + if zc_extensions.len() != regular_extensions.len() { + return false; + } + for (zc_ext, regular_ext) in zc_extensions.iter().zip(regular_extensions.iter()) { + match (zc_ext, regular_ext) { + ( + crate::state::extensions::ZExtensionStruct::Compressible(zc_comp), + crate::state::extensions::ExtensionStruct::Compressible(regular_comp), + ) => { + if u64::from(zc_comp.last_written_slot) + != regular_comp.last_written_slot + || u64::from(zc_comp.slots_until_compression) + != regular_comp.slots_until_compression + || zc_comp.rent_authority.to_bytes() + != regular_comp.rent_authority.to_bytes() + || zc_comp.rent_recipient.to_bytes() + != regular_comp.rent_recipient.to_bytes() + { + return false; + } + } + /*( + crate::state::extensions::ZExtensionStruct::MetadataPointer(zc_mp), + crate::state::extensions::ExtensionStruct::MetadataPointer(regular_mp), + ) => { + match (&zc_mp.authority, ®ular_mp.authority) { + (Some(zc_auth), Some(regular_auth)) => { + if zc_auth.to_bytes() != regular_auth.to_bytes() { + return false; + } + } + (None, None) => {} + _ => return false, + } + match (&zc_mp.metadata_address, ®ular_mp.metadata_address) { + (Some(zc_addr), Some(regular_addr)) => { + if zc_addr.to_bytes() != regular_addr.to_bytes() { + return false; + } + } + (None, None) => {} + _ => return false, + } + }*/ + ( + crate::state::extensions::ZExtensionStruct::TokenMetadata(zc_tm), + crate::state::extensions::ExtensionStruct::TokenMetadata(regular_tm), + ) => { + if zc_tm.mint.to_bytes() != regular_tm.mint.to_bytes() + || zc_tm.metadata.name != regular_tm.metadata.name.as_slice() + || zc_tm.metadata.symbol != regular_tm.metadata.symbol.as_slice() + || zc_tm.metadata.uri != regular_tm.metadata.uri.as_slice() + || zc_tm.version != regular_tm.version + { + return false; + } + match (&zc_tm.update_authority, ®ular_tm.update_authority) { + (Some(zc_auth), Some(regular_auth)) => { + if zc_auth.to_bytes() != regular_auth.to_bytes() { + return false; + } + } + (None, None) => {} + _ => return false, + } + if zc_tm.additional_metadata.len() + != regular_tm.additional_metadata.len() + { + return false; + } + for (zc_meta, regular_meta) in zc_tm + .additional_metadata + .iter() + .zip(regular_tm.additional_metadata.iter()) + { + if zc_meta.key != regular_meta.key.as_slice() + || zc_meta.value != regular_meta.value.as_slice() + { + return false; + } + } + } + _ => return false, // Different extension types + } + } + } + (None, None) => {} + _ => return false, + } + + true + } +} + +impl PartialEq> for CompressedToken { + fn eq(&self, other: &ZCompressedToken<'_>) -> bool { + other.eq(self) + } +} + +#[derive(Debug)] +pub struct ZCompressedTokenMut<'a> { + __meta: >::Output, + /// Extensions for the token account (including compressible config) + pub extensions: Option>>, +} +impl<'a> Deref for ZCompressedTokenMut<'a> { + type Target = >::Output; + + fn deref(&self) -> &Self::Target { + &self.__meta + } +} + +impl DerefMut for ZCompressedTokenMut<'_> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.__meta + } +} + +impl<'a> Deserialize<'a> for CompressedToken { + type Output = ZCompressedToken<'a>; + + fn zero_copy_at(bytes: &'a [u8]) -> Result<(Self::Output, &'a [u8]), ZeroCopyError> { + let (__meta, bytes) = >::zero_copy_at(bytes)?; + let (extensions, bytes) = if !bytes.is_empty() { + let (extensions, bytes) = + > as Deserialize<'a>>::zero_copy_at(bytes)?; + (extensions, bytes) + } else { + (None, bytes) + }; + Ok((ZCompressedToken { __meta, extensions }, bytes)) + } +} + +impl<'a> light_zero_copy::borsh_mut::DeserializeMut<'a> for CompressedToken { + type Output = ZCompressedTokenMut<'a>; + + fn zero_copy_at_mut( + bytes: &'a mut [u8], + ) -> Result<(Self::Output, &'a mut [u8]), ZeroCopyError> { + let (__meta, bytes) = >::zero_copy_at_mut(bytes)?; + let (extensions, bytes) = if !bytes.is_empty() { + let (extensions, bytes) = + > as light_zero_copy::borsh_mut::DeserializeMut<'a>>::zero_copy_at_mut(bytes)?; + (extensions, bytes) + } else { + (None, bytes) + }; + Ok((ZCompressedTokenMut { __meta, extensions }, bytes)) + } +} + +impl ZCompressedTokenMetaMut<'_> { + /// Set the delegate field by updating both the COption discriminator and value + pub fn set_delegate(&mut self, delegate: Option) -> Result<(), ZeroCopyError> { + match (&mut self.delegate, delegate) { + (Some(delegate), Some(new)) => { + **delegate = new; + } + (Some(delegate), None) => { + // Set discriminator to 0 (None) + self.delegate_option[0] = 0; + **delegate = Pubkey::default(); + } + (None, Some(new)) => { + self.delegate_option[0] = 1; + let pubkey_bytes = unsafe { + std::slice::from_raw_parts_mut(self.delegate_option.as_mut_ptr().add(4), 32) + }; + let (mut delegate_pubkey, _) = Pubkey::zero_copy_at_mut(pubkey_bytes)?; + *delegate_pubkey = new; + self.delegate = Some(delegate_pubkey); + } + (None, None) => {} + } + Ok(()) + } + + /// Set the is_native field by updating both the COption discriminator and value + pub fn set_is_native(&mut self, is_native: Option) -> Result<(), ZeroCopyError> { + match (&mut self.is_native, is_native) { + (Some(native_value), Some(new)) => { + **native_value = new.into(); + } + (Some(native_value), None) => { + // Set discriminator to 0 (None) + self.is_native_option[0] = 0; + **native_value = 0u64.into(); + self.is_native = None; + } + (None, Some(new)) => { + self.is_native_option[0] = 1; + let value_bytes = unsafe { + std::slice::from_raw_parts_mut(self.is_native_option.as_mut_ptr().add(4), 8) + }; + let (mut native_value, _) = + zerocopy::Ref::<&mut [u8], zerocopy::little_endian::U64>::from_prefix( + value_bytes, + )?; + *native_value = new.into(); + self.is_native = Some(native_value); + } + (None, None) => {} + } + Ok(()) + } + + /// Set the close_authority field by updating both the COption discriminator and value + pub fn set_close_authority( + &mut self, + close_authority: Option, + ) -> Result<(), ZeroCopyError> { + match (&mut self.close_authority, close_authority) { + (Some(authority), Some(new)) => { + **authority = new; + } + (Some(authority), None) => { + // Set discriminator to 0 (None) + self.close_authority_option[0] = 0; + **authority = Pubkey::default(); + self.close_authority = None; + } + (None, Some(new)) => { + self.close_authority_option[0] = 1; + let pubkey_bytes = unsafe { + std::slice::from_raw_parts_mut( + self.close_authority_option.as_mut_ptr().add(4), + 32, + ) + }; + let (mut close_authority_pubkey, _) = Pubkey::zero_copy_at_mut(pubkey_bytes)?; + *close_authority_pubkey = new; + self.close_authority = Some(close_authority_pubkey); + } + (None, None) => {} + } + Ok(()) + } +} + +impl CompressedToken { + /// Checks if account is frozen + pub fn is_frozen(&self) -> bool { + self.state == 2 // AccountState::Frozen + } + + /// Checks if account is native + pub fn is_native(&self) -> bool { + self.is_native.is_some() + } + + /// Checks if account is initialized + pub fn is_initialized(&self) -> bool { + self.state != 0 // AccountState::Uninitialized + } +} + +// Configuration for initializing a compressed token +#[derive(Debug, Clone)] +pub struct CompressedTokenConfig { + pub delegate: bool, + pub is_native: bool, + pub close_authority: bool, + pub extensions: Vec, +} + +impl CompressedTokenConfig { + pub fn new(delegate: bool, is_native: bool, close_authority: bool) -> Self { + Self { + delegate, + is_native, + close_authority, + extensions: vec![], + } + } + pub fn new_compressible(delegate: bool, is_native: bool, close_authority: bool) -> Self { + Self { + delegate, + is_native, + close_authority, + extensions: vec![ExtensionStructConfig::Compressible], + } + } +} + +impl<'a> ZeroCopyNew<'a> for CompressedToken { + type ZeroCopyConfig = CompressedTokenConfig; + type Output = ZCompressedTokenMut<'a>; + + fn byte_len(config: &Self::ZeroCopyConfig) -> usize { + let mut len = 0; + + // mint: 32 bytes + len += 32; + // owner: 32 bytes + len += 32; + // amount: 8 bytes + len += 8; + // delegate: 4 bytes discriminator + 32 bytes pubkey + len += 36; + // state: 1 byte + len += 1; + // is_native: 4 bytes discriminator + 8 bytes u64 + len += 12; + // delegated_amount: 8 bytes + len += 8; + // close_authority: 4 bytes discriminator + 32 bytes pubkey + len += 36; + + // Only add extension bytes if there are extensions + if !config.extensions.is_empty() { + len += 1; + len += as ZeroCopyNew<'a>>::byte_len(&config.extensions); + } + + len + } + + fn new_zero_copy( + bytes: &'a mut [u8], + config: Self::ZeroCopyConfig, + ) -> Result<(Self::Output, &'a mut [u8]), ZeroCopyError> { + if bytes.len() < Self::byte_len(&config) { + msg!("CompressedToken new_zero_copy Insufficient buffer size"); + return Err(ZeroCopyError::ArraySize( + bytes.len(), + Self::byte_len(&config), + )); + } + + // Set the state to Initialized (1) at offset 108 (32 mint + 32 owner + 8 amount + 36 delegate) + bytes[108] = 1; // AccountState::Initialized + + // Set discriminator bytes based on config + // delegate discriminator at offset 72 (32 mint + 32 owner + 8 amount) + bytes[72] = if config.delegate { 1 } else { 0 }; + + // is_native discriminator at offset 109 (72 + 36 delegate + 1 state) + bytes[109] = if config.is_native { 1 } else { 0 }; + + // close_authority discriminator at offset 129 (109 + 12 is_native + 8 delegated_amount) + bytes[129] = if config.close_authority { 1 } else { 0 }; + + // Initialize extensions if present + if !config.extensions.is_empty() { + // Set Option discriminant for extensions (Some = 1) at position 165 + bytes[165] = 1; + + // Extensions Vec starts after the Option discriminant (166 bytes) + let extension_bytes = &mut bytes[166..]; + + // Write Vec length (4 bytes little-endian) + let len = config.extensions.len() as u32; + extension_bytes[0..4].copy_from_slice(&len.to_le_bytes()); + + // Initialize each extension + let mut current_bytes = &mut extension_bytes[4..]; + for extension_config in &config.extensions { + let (_, remaining_bytes) = >::new_zero_copy( + current_bytes, + extension_config.clone(), + )?; + current_bytes = remaining_bytes; + } + } + CompressedToken::zero_copy_at_mut(bytes) + } +} diff --git a/program-libs/ctoken-types/src/state/token_data.rs b/program-libs/ctoken-types/src/state/token_data.rs new file mode 100644 index 0000000000..6f6550bcb8 --- /dev/null +++ b/program-libs/ctoken-types/src/state/token_data.rs @@ -0,0 +1,149 @@ +use std::vec; + +use light_compressed_account::{hash_to_bn254_field_size_be, Pubkey}; +use light_hasher::{errors::HasherError, Hasher, Poseidon}; + +use crate::{AnchorDeserialize, AnchorSerialize, NATIVE_MINT}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, AnchorSerialize, AnchorDeserialize)] +#[repr(u8)] +pub enum AccountState { + Initialized, + Frozen, +} + +#[derive(Debug, PartialEq, Eq, AnchorSerialize, AnchorDeserialize, Clone)] +pub struct TokenData { + /// The mint associated with this account + pub mint: Pubkey, + /// The owner of this account. + pub owner: Pubkey, + /// The amount of tokens this account holds. + pub amount: u64, + /// If `delegate` is `Some` then `delegated_amount` represents + /// the amount authorized by the delegate + pub delegate: Option, + /// The account's state + pub state: AccountState, + /// Placeholder for TokenExtension tlv data (unimplemented) + pub tlv: Option>, +} + +/// Hashing schema: H(mint, owner, amount, delegate, delegated_amount, +/// is_native, state) +/// +/// delegate, delegated_amount, is_native and state have dynamic positions. +/// Always hash mint, owner and amount If delegate hash delegate and +/// delegated_amount together. If is native hash is_native else is omitted. +/// If frozen hash AccountState::Frozen else is omitted. +/// +/// Security: to prevent the possibility that different fields with the same +/// value to result in the same hash we add a prefix to the delegated amount, is +/// native and state fields. This way we can have a dynamic hashing schema and +/// hash only used values. +impl TokenData { + /// Only the spl representation of native tokens (wrapped SOL) is + /// compressed. + /// The sol value is stored in the token pool account. + /// The sol value in the compressed account is independent from + /// the wrapped sol amount. + pub fn is_native(&self) -> bool { + self.mint == NATIVE_MINT + } + pub fn hash_with_hashed_values( + hashed_mint: &[u8; 32], + hashed_owner: &[u8; 32], + amount_bytes: &[u8; 32], + hashed_delegate: &Option<&[u8; 32]>, + ) -> std::result::Result<[u8; 32], HasherError> { + Self::hash_inputs_with_hashed_values::( + hashed_mint, + hashed_owner, + amount_bytes, + hashed_delegate, + ) + } + + pub fn hash_frozen_with_hashed_values( + hashed_mint: &[u8; 32], + hashed_owner: &[u8; 32], + amount_bytes: &[u8; 32], + hashed_delegate: &Option<&[u8; 32]>, + ) -> std::result::Result<[u8; 32], HasherError> { + Self::hash_inputs_with_hashed_values::( + hashed_mint, + hashed_owner, + amount_bytes, + hashed_delegate, + ) + } + + /// We should not hash pubkeys multiple times. For all we can assume mints + /// are equal. For all input compressed accounts we assume owners are + /// equal. + pub fn hash_inputs_with_hashed_values( + mint: &[u8; 32], + owner: &[u8; 32], + amount_bytes: &[u8], + hashed_delegate: &Option<&[u8; 32]>, + ) -> std::result::Result<[u8; 32], HasherError> { + let mut hash_inputs = vec![mint.as_slice(), owner.as_slice(), amount_bytes]; + if let Some(hashed_delegate) = hashed_delegate { + hash_inputs.push(hashed_delegate.as_slice()); + } + let mut state_bytes = [0u8; 32]; + if FROZEN_INPUTS { + state_bytes[31] = AccountState::Frozen as u8; + hash_inputs.push(&state_bytes[..]); + } + Poseidon::hashv(hash_inputs.as_slice()) + } +} + +impl TokenData { + /// Hashes token data of token accounts. + /// + /// Note, hashing changed for token account data in batched Merkle trees. + /// For hashing of token account data stored in concurrent Merkle trees use hash_legacy(). + pub fn hash(&self) -> std::result::Result<[u8; 32], HasherError> { + self._hash::() + } + + /// Hashes token data of token accounts stored in concurrent Merkle trees. + pub fn hash_legacy(&self) -> std::result::Result<[u8; 32], HasherError> { + self._hash::() + } + + fn _hash(&self) -> std::result::Result<[u8; 32], HasherError> { + let hashed_mint = hash_to_bn254_field_size_be(self.mint.to_bytes().as_slice()); + let hashed_owner = hash_to_bn254_field_size_be(self.owner.to_bytes().as_slice()); + let mut amount_bytes = [0u8; 32]; + if BATCHED { + amount_bytes[24..].copy_from_slice(self.amount.to_be_bytes().as_slice()); + } else { + amount_bytes[24..].copy_from_slice(self.amount.to_le_bytes().as_slice()); + } + let hashed_delegate; + let hashed_delegate_option = if let Some(delegate) = self.delegate { + hashed_delegate = hash_to_bn254_field_size_be(delegate.to_bytes().as_slice()); + Some(&hashed_delegate) + } else { + None + }; + if self.state != AccountState::Initialized { + Self::hash_inputs_with_hashed_values::( + &hashed_mint, + &hashed_owner, + &amount_bytes, + &hashed_delegate_option, + ) + } else { + Self::hash_inputs_with_hashed_values::( + &hashed_mint, + &hashed_owner, + &amount_bytes, + &hashed_delegate_option, + ) + } + } +} diff --git a/program-libs/ctoken-types/tests/solana_ctoken.rs b/program-libs/ctoken-types/tests/solana_ctoken.rs new file mode 100644 index 0000000000..0667699f4f --- /dev/null +++ b/program-libs/ctoken-types/tests/solana_ctoken.rs @@ -0,0 +1,494 @@ +use light_compressed_account::Pubkey; +use light_ctoken_types::state::solana_ctoken::{ + CompressedToken, CompressedTokenConfig, ZCompressedToken, +}; +use light_zero_copy::{borsh::Deserialize, init_mut::ZeroCopyNew}; +use rand::Rng; +use spl_pod::{bytemuck::pod_from_bytes, primitives::PodU64, solana_program_option::COption}; +use spl_token_2022::{ + pod::PodAccount, + solana_program::program_pack::Pack, + state::{Account, AccountState}, +}; + +/// Generate random token account data using SPL Token's pack method +fn generate_random_token_account_data(rng: &mut impl Rng) -> Vec { + let account = Account { + mint: solana_pubkey::Pubkey::new_from_array(rng.gen::<[u8; 32]>()), + owner: solana_pubkey::Pubkey::new_from_array(rng.gen::<[u8; 32]>()), + amount: rng.gen::(), + delegate: if rng.gen_bool(0.3) { + COption::Some(solana_pubkey::Pubkey::new_from_array(rng.gen::<[u8; 32]>())) + } else { + COption::None + }, + state: if rng.gen_bool(0.9) { + AccountState::Initialized + } else { + AccountState::Frozen + }, + is_native: if rng.gen_bool(0.2) { + COption::Some(rng.gen_range(1_000_000..=10_000_000u64)) + } else { + COption::None + }, + delegated_amount: rng.gen::(), + close_authority: if rng.gen_bool(0.25) { + COption::Some(solana_pubkey::Pubkey::new_from_array(rng.gen::<[u8; 32]>())) + } else { + COption::None + }, + }; + println!("Expected Account: {:?}", account); + + let mut account_data = vec![0u8; Account::LEN]; + Account::pack(account, &mut account_data).unwrap(); + account_data +} + +/// Compare all fields between our CompressedToken zero-copy implementation and Pod account +fn compare_compressed_token_with_pod_account( + compressed_token: &ZCompressedToken, + pod_account: &PodAccount, +) -> bool { + // Extensions should be None for basic SPL Token accounts + if compressed_token.extensions.is_some() { + return false; + } + + // Compare mint + if compressed_token.mint.to_bytes() != pod_account.mint.to_bytes() { + println!( + "Mint mismatch: compressed={:?}, pod={:?}", + compressed_token.mint.to_bytes(), + pod_account.mint.to_bytes() + ); + return false; + } + + // Compare owner + if compressed_token.owner.to_bytes() != pod_account.owner.to_bytes() { + return false; + } + + // Compare amount + if u64::from(*compressed_token.amount) != u64::from(pod_account.amount) { + return false; + } + + // Compare delegate + let pod_delegate_option: Option = if pod_account.delegate.is_some() { + Some( + pod_account + .delegate + .unwrap_or(solana_pubkey::Pubkey::default()) + .to_bytes() + .into(), + ) + } else { + None + }; + match (compressed_token.delegate, pod_delegate_option) { + (Some(compressed_delegate), Some(pod_delegate)) => { + if compressed_delegate.to_bytes() != pod_delegate.to_bytes() { + return false; + } + } + (None, None) => { + // Both are None, which is correct + } + _ => { + // One is Some, the other is None - mismatch + return false; + } + } + + // Compare state + if compressed_token.state != pod_account.state { + return false; + } + + // Compare is_native + let pod_native_option: Option = if pod_account.is_native.is_some() { + Some(u64::from( + pod_account.is_native.unwrap_or(PodU64::default()), + )) + } else { + None + }; + match (compressed_token.is_native, pod_native_option) { + (Some(compressed_native), Some(pod_native)) => { + if u64::from(*compressed_native) != pod_native { + return false; + } + } + (None, None) => { + // Both are None, which is correct + } + _ => { + // One is Some, the other is None - mismatch + return false; + } + } + + // Compare delegated_amount + if u64::from(*compressed_token.delegated_amount) != u64::from(pod_account.delegated_amount) { + return false; + } + + // Compare close_authority + let pod_close_option: Option = if pod_account.close_authority.is_some() { + Some( + pod_account + .close_authority + .unwrap_or(solana_pubkey::Pubkey::default()) + .to_bytes() + .into(), + ) + } else { + None + }; + match (compressed_token.close_authority, pod_close_option) { + (Some(compressed_close), Some(pod_close)) => { + if compressed_close.to_bytes() != pod_close.to_bytes() { + return false; + } + } + (None, None) => { + // Both are None, which is correct + } + _ => { + // One is Some, the other is None - mismatch + return false; + } + } + + true +} + +/// Compare all fields between our CompressedToken mutable zero-copy implementation and Pod account +fn compare_compressed_token_mut_with_pod_account( + compressed_token: &light_ctoken_types::state::solana_ctoken::ZCompressedTokenMut, + pod_account: &PodAccount, +) -> bool { + // Extensions should be None for basic SPL Token accounts + if compressed_token.extensions.is_some() { + return false; + } + + // Compare mint + if compressed_token.mint.to_bytes() != pod_account.mint.to_bytes() { + println!( + "Mint mismatch: compressed={:?}, pod={:?}", + compressed_token.mint.to_bytes(), + pod_account.mint.to_bytes() + ); + return false; + } + + // Compare owner + if compressed_token.owner.to_bytes() != pod_account.owner.to_bytes() { + return false; + } + + // Compare amount + if u64::from(*compressed_token.amount) != u64::from(pod_account.amount) { + return false; + } + + // Compare delegate + let pod_delegate_option: Option = if pod_account.delegate.is_some() { + Some( + pod_account + .delegate + .unwrap_or(solana_pubkey::Pubkey::default()) + .to_bytes() + .into(), + ) + } else { + None + }; + match (compressed_token.delegate.as_ref(), pod_delegate_option) { + (Some(compressed_delegate), Some(pod_delegate)) => { + if compressed_delegate.to_bytes() != pod_delegate.to_bytes() { + return false; + } + } + (None, None) => { + // Both are None, which is correct + } + _ => { + // One is Some, the other is None - mismatch + return false; + } + } + + // Compare state + if *compressed_token.state != pod_account.state { + println!( + "State mismatch: compressed={}, pod={}", + *compressed_token.state, pod_account.state + ); + return false; + } + + // Compare is_native + let pod_native_option: Option = if pod_account.is_native.is_some() { + Some(u64::from( + pod_account.is_native.unwrap_or(PodU64::default()), + )) + } else { + None + }; + match (compressed_token.is_native.as_ref(), pod_native_option) { + (Some(compressed_native), Some(pod_native)) => { + if u64::from(**compressed_native) != pod_native { + return false; + } + } + (None, None) => { + // Both are None, which is correct + } + _ => { + // One is Some, the other is None - mismatch + return false; + } + } + + // Compare delegated_amount + if u64::from(*compressed_token.delegated_amount) != u64::from(pod_account.delegated_amount) { + return false; + } + + // Compare close_authority + let pod_close_option: Option = if pod_account.close_authority.is_some() { + Some( + pod_account + .close_authority + .unwrap_or(solana_pubkey::Pubkey::default()) + .to_bytes() + .into(), + ) + } else { + None + }; + match (compressed_token.close_authority.as_ref(), pod_close_option) { + (Some(compressed_close), Some(pod_close)) => { + if compressed_close.to_bytes() != pod_close.to_bytes() { + return false; + } + } + (None, None) => { + // Both are None, which is correct + } + _ => { + // One is Some, the other is None - mismatch + return false; + } + } + + true +} + +#[test] +fn test_compressed_token_equivalent_to_pod_account() { + use light_zero_copy::borsh_mut::DeserializeMut; + let mut rng = rand::thread_rng(); + + for _ in 0..10000 { + let mut account_data = generate_random_token_account_data(&mut rng); + let account_data_clone = account_data.clone(); + let pod_account = pod_from_bytes::(&account_data_clone).unwrap(); + + // Test immutable version + let (compressed_token, _) = CompressedToken::zero_copy_at(&account_data).unwrap(); + println!("Compressed Token: {:?}", compressed_token); + println!("Pod Account: {:?}", pod_account); + assert!(compare_compressed_token_with_pod_account( + &compressed_token, + pod_account + )); + { + let account_data_clone = account_data.clone(); + let pod_account = pod_from_bytes::(&account_data_clone).unwrap(); + // Test mutable version + let (mut compressed_token_mut, _) = + CompressedToken::zero_copy_at_mut(&mut account_data).unwrap(); + println!("Compressed Token Mut: {:?}", compressed_token_mut); + println!("Pod Account: {:?}", pod_account); + + assert!(compare_compressed_token_mut_with_pod_account( + &compressed_token_mut, + pod_account + )); + + // Test mutation: modify every mutable field in the zero-copy struct + { + // Modify mint (first 32 bytes) + *compressed_token_mut.mint = solana_pubkey::Pubkey::new_unique().to_bytes().into(); + + // Modify owner (next 32 bytes) + *compressed_token_mut.owner = solana_pubkey::Pubkey::new_unique().to_bytes().into(); + // Modify amount + *compressed_token_mut.amount = rng.gen::().into(); + + // Modify delegate if it exists + if let Some(ref mut delegate) = compressed_token_mut.delegate { + **delegate = solana_pubkey::Pubkey::new_unique().to_bytes().into(); + } + + // Modify state (0 = Uninitialized, 1 = Initialized, 2 = Frozen) + *compressed_token_mut.state = rng.gen_range(0..=2); + + // Modify is_native if it exists + if let Some(ref mut native_value) = compressed_token_mut.is_native { + **native_value = rng.gen::().into(); + } + + // Modify delegated_amount + *compressed_token_mut.delegated_amount = rng.gen::().into(); + + // Modify close_authority if it exists + if let Some(ref mut close_auth) = compressed_token_mut.close_authority { + **close_auth = solana_pubkey::Pubkey::new_unique().to_bytes().into(); + } + } + // Clone the modified bytes and create a new Pod account to verify changes + let modified_account_data = account_data.clone(); + let modified_pod_account = + pod_from_bytes::(&modified_account_data).unwrap(); + + // Create a new immutable compressed token from the modified data to compare + let (modified_compressed_token, _) = + CompressedToken::zero_copy_at(&modified_account_data).unwrap(); + + println!("Modified zero copy account {:?}", modified_compressed_token); + println!("Modified Pod Account: {:?}", modified_pod_account); + // Use the comparison function to verify all modifications + assert!(compare_compressed_token_with_pod_account( + &modified_compressed_token, + modified_pod_account + )); + } + } +} + +#[test] +fn test_compressed_token_new_zero_copy() { + let config = CompressedTokenConfig { + delegate: false, + is_native: false, + close_authority: false, + extensions: vec![], + }; + + // Calculate required buffer size + let required_size = CompressedToken::byte_len(&config); + assert_eq!(required_size, 165); // SPL Token account size + + // Create buffer and initialize + let mut buffer = vec![0u8; required_size]; + let (compressed_token, remaining_bytes) = CompressedToken::new_zero_copy(&mut buffer, config) + .expect("Failed to initialize compressed token"); + + // Verify the remaining bytes length + assert_eq!(remaining_bytes.len(), 0); + // Verify the zero-copy structure reflects the discriminators + assert!(compressed_token.delegate.is_none()); + assert!(compressed_token.is_native.is_none()); + assert!(compressed_token.close_authority.is_none()); + assert!(compressed_token.extensions.is_none()); + // Verify the discriminator bytes are set correctly + assert_eq!(buffer[72], 0); // delegate discriminator should be 0 (None) + assert_eq!(buffer[109], 0); // is_native discriminator should be 0 (None) + assert_eq!(buffer[129], 0); // close_authority discriminator should be 0 (None) +} + +#[test] +fn test_compressed_token_new_zero_copy_with_delegate() { + let config = CompressedTokenConfig { + delegate: true, + is_native: false, + close_authority: false, + extensions: vec![], + }; + + // Create buffer and initialize + let mut buffer = vec![0u8; CompressedToken::byte_len(&config)]; + let (compressed_token, _) = CompressedToken::new_zero_copy(&mut buffer, config) + .expect("Failed to initialize compressed token with delegate"); + // The delegate field should be Some (though the pubkey will be zero) + assert!(compressed_token.delegate.is_some()); + assert!(compressed_token.is_native.is_none()); + assert!(compressed_token.close_authority.is_none()); + // Verify delegate discriminator is set to 1 (Some) + assert_eq!(buffer[72], 1); // delegate discriminator should be 1 (Some) + assert_eq!(buffer[109], 0); // is_native discriminator should be 0 (None) + assert_eq!(buffer[129], 0); // close_authority discriminator should be 0 (None) +} + +#[test] +fn test_compressed_token_new_zero_copy_with_is_native() { + let config = CompressedTokenConfig { + delegate: false, + is_native: true, + close_authority: false, + extensions: vec![], + }; + + // Create buffer and initialize + let mut buffer = vec![0u8; CompressedToken::byte_len(&config)]; + let (compressed_token, _) = CompressedToken::new_zero_copy(&mut buffer, config) + .expect("Failed to initialize compressed token with is_native"); + + // The is_native field should be Some (though the value will be zero) + assert!(compressed_token.delegate.is_none()); + assert!(compressed_token.is_native.is_some()); + assert!(compressed_token.close_authority.is_none()); + + // Verify is_native discriminator is set to 1 (Some) + assert_eq!(buffer[72], 0); // delegate discriminator should be 0 (None) + assert_eq!(buffer[109], 1); // is_native discriminator should be 1 (Some) + assert_eq!(buffer[129], 0); // close_authority discriminator should be 0 (None) +} + +#[test] +fn test_compressed_token_new_zero_copy_buffer_too_small() { + let config = CompressedTokenConfig { + delegate: false, + is_native: false, + close_authority: false, + extensions: vec![], + }; + + // Create buffer that's too small + let mut buffer = vec![0u8; 100]; // Less than 165 bytes required + let result = CompressedToken::new_zero_copy(&mut buffer, config); + + // Should fail with size error + assert!(result.is_err()); +} + +#[test] +fn test_compressed_token_new_zero_copy_all_options() { + let config = CompressedTokenConfig { + delegate: true, + is_native: true, + close_authority: true, + extensions: vec![], + }; + + // Create buffer and initialize + let mut buffer = vec![0u8; CompressedToken::byte_len(&config)]; + let (compressed_token, _) = CompressedToken::new_zero_copy(&mut buffer, config) + .expect("Failed to initialize compressed token with all options"); + + // All optional fields should be Some + assert!(compressed_token.delegate.is_some()); + assert!(compressed_token.is_native.is_some()); + assert!(compressed_token.close_authority.is_some()); + // Verify all discriminators are set to 1 (Some) + assert_eq!(buffer[72], 1); // delegate discriminator should be 1 (Some) + assert_eq!(buffer[109], 1); // is_native discriminator should be 1 (Some) + assert_eq!(buffer[129], 1); // close_authority discriminator should be 1 (Some) +} diff --git a/program-libs/ctoken-types/tests/token_data.rs b/program-libs/ctoken-types/tests/token_data.rs new file mode 100644 index 0000000000..5ba6094ff8 --- /dev/null +++ b/program-libs/ctoken-types/tests/token_data.rs @@ -0,0 +1,284 @@ +use light_compressed_account::{hash_to_bn254_field_size_be, Pubkey}; +use light_ctoken_types::state::{AccountState, TokenData}; +use light_hasher::HasherError; +use num_bigint::BigUint; +use rand::Rng; + +#[test] +fn equivalency_of_hash_functions() { + let token_data = TokenData { + mint: Pubkey::new_unique(), + owner: Pubkey::new_unique(), + amount: 100, + delegate: Some(Pubkey::new_unique()), + state: AccountState::Initialized, + tlv: None, + }; + let hashed_token_data = token_data.hash_legacy().unwrap(); + let hashed_mint = hash_to_bn254_field_size_be(token_data.mint.to_bytes().as_slice()); + let hashed_owner = hash_to_bn254_field_size_be(token_data.owner.to_bytes().as_slice()); + let hashed_delegate = + hash_to_bn254_field_size_be(token_data.delegate.unwrap().to_bytes().as_slice()); + let mut amount_bytes = [0u8; 32]; + amount_bytes[24..].copy_from_slice(token_data.amount.to_le_bytes().as_slice()); + let hashed_token_data_with_hashed_values = TokenData::hash_inputs_with_hashed_values::( + &hashed_mint, + &hashed_owner, + &amount_bytes, + &Some(&hashed_delegate), + ) + .unwrap(); + assert_eq!(hashed_token_data, hashed_token_data_with_hashed_values); + + let token_data = TokenData { + mint: Pubkey::new_unique(), + owner: Pubkey::new_unique(), + amount: 101, + delegate: None, + state: AccountState::Initialized, + tlv: None, + }; + let hashed_token_data = token_data.hash_legacy().unwrap(); + let hashed_mint = hash_to_bn254_field_size_be(token_data.mint.to_bytes().as_slice()); + let hashed_owner = hash_to_bn254_field_size_be(token_data.owner.to_bytes().as_slice()); + let mut amount_bytes = [0u8; 32]; + amount_bytes[24..].copy_from_slice(token_data.amount.to_le_bytes().as_slice()); + let hashed_token_data_with_hashed_values = + TokenData::hash_with_hashed_values(&hashed_mint, &hashed_owner, &amount_bytes, &None) + .unwrap(); + assert_eq!(hashed_token_data, hashed_token_data_with_hashed_values); +} + +fn legacy_hash(token_data: &TokenData) -> std::result::Result<[u8; 32], HasherError> { + let hashed_mint = hash_to_bn254_field_size_be(token_data.mint.to_bytes().as_slice()); + let hashed_owner = hash_to_bn254_field_size_be(token_data.owner.to_bytes().as_slice()); + let amount_bytes = token_data.amount.to_le_bytes(); + let hashed_delegate; + let hashed_delegate_option = if let Some(delegate) = token_data.delegate { + hashed_delegate = hash_to_bn254_field_size_be(delegate.to_bytes().as_slice()); + Some(&hashed_delegate) + } else { + None + }; + if token_data.state != AccountState::Initialized { + TokenData::hash_inputs_with_hashed_values::( + &hashed_mint, + &hashed_owner, + &amount_bytes, + &hashed_delegate_option, + ) + } else { + TokenData::hash_inputs_with_hashed_values::( + &hashed_mint, + &hashed_owner, + &amount_bytes, + &hashed_delegate_option, + ) + } +} + +fn equivalency_of_hash_functions_rnd_iters() { + let mut rng = rand::thread_rng(); + + for _ in 0..ITERS { + let token_data = TokenData { + mint: Pubkey::new_unique(), + owner: Pubkey::new_unique(), + amount: rng.gen(), + delegate: Some(Pubkey::new_unique()), + state: AccountState::Initialized, + tlv: None, + }; + let hashed_token_data = token_data.hash_legacy().unwrap(); + let hashed_mint = hash_to_bn254_field_size_be(token_data.mint.to_bytes().as_slice()); + let hashed_owner = hash_to_bn254_field_size_be(token_data.owner.to_bytes().as_slice()); + let hashed_delegate = + hash_to_bn254_field_size_be(token_data.delegate.unwrap().to_bytes().as_slice()); + let mut amount_bytes = [0u8; 32]; + amount_bytes[24..].copy_from_slice(token_data.amount.to_le_bytes().as_slice()); + let hashed_token_data_with_hashed_values = TokenData::hash_with_hashed_values( + &hashed_mint, + &hashed_owner, + &amount_bytes, + &Some(&hashed_delegate), + ) + .unwrap(); + assert_eq!(hashed_token_data, hashed_token_data_with_hashed_values); + { + let legacy_hash = legacy_hash(&token_data).unwrap(); + assert_eq!(hashed_token_data, legacy_hash); + } + let token_data = TokenData { + mint: Pubkey::new_unique(), + owner: Pubkey::new_unique(), + amount: rng.gen(), + delegate: None, + state: AccountState::Initialized, + tlv: None, + }; + let hashed_token_data = token_data.hash_legacy().unwrap(); + let hashed_mint = hash_to_bn254_field_size_be(token_data.mint.to_bytes().as_slice()); + let hashed_owner = hash_to_bn254_field_size_be(token_data.owner.to_bytes().as_slice()); + let mut amount_bytes = [0u8; 32]; + amount_bytes[24..].copy_from_slice(token_data.amount.to_le_bytes().as_slice()); + let hashed_token_data_with_hashed_values: [u8; 32] = + TokenData::hash_with_hashed_values(&hashed_mint, &hashed_owner, &amount_bytes, &None) + .unwrap(); + assert_eq!(hashed_token_data, hashed_token_data_with_hashed_values); + let legacy_hash = legacy_hash(&token_data).unwrap(); + assert_eq!(hashed_token_data, legacy_hash); + } +} + +#[test] +fn equivalency_of_hash_functions_iters_poseidon() { + equivalency_of_hash_functions_rnd_iters::<10_000>(); +} + +#[test] +fn test_circuit_equivalence() { + // Convert hex strings to Pubkeys + let mint_pubkey = Pubkey::new_from_array([ + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, + ]); + let owner_pubkey = Pubkey::new_from_array([ + 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, + ]); + let delegate_pubkey = Pubkey::new_from_array([ + 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, + ]); + + let token_data = TokenData { + mint: mint_pubkey, + owner: owner_pubkey, + amount: 1000000u64, + delegate: Some(delegate_pubkey), + state: AccountState::Initialized, // Using Frozen state to match our circuit test + tlv: None, + }; + + // Calculate the hash with the Rust code + let rust_hash = token_data.hash().unwrap(); + + let circuit_hash_str = + "12698830169693734517877055378728747723888091986541703429186543307137690361131"; + use std::str::FromStr; + let circuit_hash = BigUint::from_str(circuit_hash_str).unwrap().to_bytes_be(); + let rust_hash_string = BigUint::from_bytes_be(rust_hash.as_slice()).to_string(); + println!("Circuit hash string: {}", circuit_hash_str); + println!("rust_hash_string {}", rust_hash_string); + assert_eq!(rust_hash.to_vec(), circuit_hash); +} + +#[test] +fn test_frozen_equivalence() { + let token_data = TokenData { + mint: Pubkey::new_unique(), + owner: Pubkey::new_unique(), + amount: 100, + delegate: Some(Pubkey::new_unique()), + state: AccountState::Initialized, + tlv: None, + }; + let hashed_mint = hash_to_bn254_field_size_be(token_data.mint.to_bytes().as_slice()); + let hashed_owner = hash_to_bn254_field_size_be(token_data.owner.to_bytes().as_slice()); + let hashed_delegate = + hash_to_bn254_field_size_be(token_data.delegate.unwrap().to_bytes().as_slice()); + let mut amount_bytes = [0u8; 32]; + amount_bytes[24..].copy_from_slice(token_data.amount.to_le_bytes().as_slice()); + let hash = TokenData::hash_with_hashed_values( + &hashed_mint, + &hashed_owner, + &amount_bytes, + &Some(&hashed_delegate), + ) + .unwrap(); + let other_hash = token_data.hash_legacy().unwrap(); + assert_eq!(hash, other_hash); +} + +#[test] +fn failing_tests_hashing() { + let mut vec_previous_hashes = Vec::new(); + let token_data = TokenData { + mint: Pubkey::new_unique(), + owner: Pubkey::new_unique(), + amount: 100, + delegate: None, + state: AccountState::Initialized, + tlv: None, + }; + let hashed_mint = hash_to_bn254_field_size_be(token_data.mint.to_bytes().as_slice()); + let hashed_owner = hash_to_bn254_field_size_be(token_data.owner.to_bytes().as_slice()); + let mut amount_bytes = [0u8; 32]; + amount_bytes[24..].copy_from_slice(token_data.amount.to_le_bytes().as_slice()); + let hash = + TokenData::hash_with_hashed_values(&hashed_mint, &hashed_owner, &amount_bytes, &None) + .unwrap(); + vec_previous_hashes.push(hash); + // different mint + let hashed_mint_2 = hash_to_bn254_field_size_be(Pubkey::new_unique().to_bytes().as_slice()); + let mut amount_bytes = [0u8; 32]; + amount_bytes[24..].copy_from_slice(token_data.amount.to_le_bytes().as_slice()); + let hash2 = + TokenData::hash_with_hashed_values(&hashed_mint_2, &hashed_owner, &amount_bytes, &None) + .unwrap(); + assert_to_previous_hashes(hash2, &mut vec_previous_hashes); + + // different owner + let hashed_owner_2 = hash_to_bn254_field_size_be(Pubkey::new_unique().to_bytes().as_slice()); + let mut amount_bytes = [0u8; 32]; + amount_bytes[24..].copy_from_slice(token_data.amount.to_le_bytes().as_slice()); + let hash3 = + TokenData::hash_with_hashed_values(&hashed_mint, &hashed_owner_2, &amount_bytes, &None) + .unwrap(); + assert_to_previous_hashes(hash3, &mut vec_previous_hashes); + + // different amount + let different_amount: u64 = 101; + let mut different_amount_bytes = [0u8; 32]; + different_amount_bytes[24..].copy_from_slice(different_amount.to_le_bytes().as_slice()); + let hash4 = TokenData::hash_with_hashed_values( + &hashed_mint, + &hashed_owner, + &different_amount_bytes, + &None, + ) + .unwrap(); + assert_to_previous_hashes(hash4, &mut vec_previous_hashes); + + // different delegate + let delegate = Pubkey::new_unique(); + let hashed_delegate = hash_to_bn254_field_size_be(delegate.to_bytes().as_slice()); + let mut amount_bytes = [0u8; 32]; + amount_bytes[24..].copy_from_slice(token_data.amount.to_le_bytes().as_slice()); + let hash7 = TokenData::hash_with_hashed_values( + &hashed_mint, + &hashed_owner, + &amount_bytes, + &Some(&hashed_delegate), + ) + .unwrap(); + + assert_to_previous_hashes(hash7, &mut vec_previous_hashes); + // different account state + let mut token_data = token_data; + token_data.state = AccountState::Frozen; + let hash9 = token_data.hash_legacy().unwrap(); + assert_to_previous_hashes(hash9, &mut vec_previous_hashes); + // different account state with delegate + token_data.delegate = Some(delegate); + let hash10 = token_data.hash_legacy().unwrap(); + assert_to_previous_hashes(hash10, &mut vec_previous_hashes); +} + +fn assert_to_previous_hashes(hash: [u8; 32], previous_hashes: &mut Vec<[u8; 32]>) { + for previous_hash in previous_hashes.iter() { + assert_ne!(hash, *previous_hash); + } + println!("len previous hashes: {}", previous_hashes.len()); + previous_hashes.push(hash); +} diff --git a/program-libs/hasher/Cargo.toml b/program-libs/hasher/Cargo.toml index 0bf82253ac..6f086851ab 100644 --- a/program-libs/hasher/Cargo.toml +++ b/program-libs/hasher/Cargo.toml @@ -10,6 +10,7 @@ edition = "2021" default = [] solana = ["solana-program-error", "solana-pubkey"] pinocchio = ["dep:pinocchio"] +zero-copy = ["dep:zerocopy"] [dependencies] @@ -22,6 +23,7 @@ num-bigint = { workspace = true } solana-program-error = { workspace = true, optional = true } solana-pubkey = { workspace = true, optional = true } pinocchio = { workspace = true, optional = true } +zerocopy = { workspace = true, optional = true } borsh = { workspace = true } solana-nostd-keccak = "0.1.3" diff --git a/program-libs/hasher/src/to_byte_array.rs b/program-libs/hasher/src/to_byte_array.rs index ac56df5d2d..fcb0351fb2 100644 --- a/program-libs/hasher/src/to_byte_array.rs +++ b/program-libs/hasher/src/to_byte_array.rs @@ -70,6 +70,36 @@ impl_to_byte_array_for_integer_type!(u64); impl_to_byte_array_for_integer_type!(i128); impl_to_byte_array_for_integer_type!(u128); +// Macro for implementing ToByteArray for zero-copy types +#[cfg(feature = "zero-copy")] +macro_rules! impl_to_byte_array_for_zero_copy_type { + ($zero_copy_type:ty, $primitive_type:ty) => { + impl ToByteArray for $zero_copy_type { + const IS_PRIMITIVE: bool = true; + const NUM_FIELDS: usize = 1; + + fn to_byte_array(&self) -> Result<[u8; 32], HasherError> { + let value: $primitive_type = (*self).into(); + value.to_byte_array() + } + } + }; +} + +// ToByteArray implementations for zero-copy types +#[cfg(feature = "zero-copy")] +impl_to_byte_array_for_zero_copy_type!(zerocopy::little_endian::U16, u16); +#[cfg(feature = "zero-copy")] +impl_to_byte_array_for_zero_copy_type!(zerocopy::little_endian::U32, u32); +#[cfg(feature = "zero-copy")] +impl_to_byte_array_for_zero_copy_type!(zerocopy::little_endian::U64, u64); +#[cfg(feature = "zero-copy")] +impl_to_byte_array_for_zero_copy_type!(zerocopy::little_endian::I16, i16); +#[cfg(feature = "zero-copy")] +impl_to_byte_array_for_zero_copy_type!(zerocopy::little_endian::I32, i32); +#[cfg(feature = "zero-copy")] +impl_to_byte_array_for_zero_copy_type!(zerocopy::little_endian::I64, i64); + /// Example usage: /// impl_to_byte_array_for_array! { /// MyCustomType, diff --git a/program-libs/zero-copy-derive/Cargo.toml b/program-libs/zero-copy-derive/Cargo.toml index 1cdc8254e8..2ac89effe2 100644 --- a/program-libs/zero-copy-derive/Cargo.toml +++ b/program-libs/zero-copy-derive/Cargo.toml @@ -24,3 +24,5 @@ rand = "0.8" borsh = { workspace = true } light-zero-copy = { workspace = true, features = ["std", "derive"] } zerocopy = { workspace = true, features = ["derive"] } +light-sdk-macros = { workspace = true } +light-hasher = { workspace = true, features = ["zero-copy"] } diff --git a/program-libs/zero-copy-derive/src/lib.rs b/program-libs/zero-copy-derive/src/lib.rs index becac18087..73fcbcf48d 100644 --- a/program-libs/zero-copy-derive/src/lib.rs +++ b/program-libs/zero-copy-derive/src/lib.rs @@ -40,6 +40,19 @@ mod zero_copy_mut; /// } /// ``` /// +/// To derive LightHasher for the generated ZStruct, use the #[light_hasher] attribute: +/// ```ignore +/// use light_zero_copy_derive::ZeroCopy; +/// #[derive(ZeroCopy)] +/// #[light_hasher] // Currently disabled due to Vec/&[u8] hash inconsistency +/// pub struct MyStruct { +/// pub a: u8, +/// } +/// ``` +/// +/// Note: #[light_hasher] is currently disabled due to hash inconsistency between +/// Vec fields in the original struct and &[u8] slice fields in the generated ZStruct. +/// /// # Macro Rules /// 1. Create zero copy structs Z and ZMut for the struct /// 1.1. The first fields are extracted into a meta struct until we reach a Vec, Option or type that does not implement Copy @@ -54,7 +67,7 @@ mod zero_copy_mut; /// 3. Implement From> for StructName and FromMut> for StructName /// /// Note: Options are not supported in ZeroCopyEq -#[proc_macro_derive(ZeroCopy)] +#[proc_macro_derive(ZeroCopy, attributes(light_hasher, hash, skip))] pub fn derive_zero_copy(input: TokenStream) -> TokenStream { let res = zero_copy::derive_zero_copy_impl(input); TokenStream::from(match res { diff --git a/program-libs/zero-copy-derive/src/shared/utils.rs b/program-libs/zero-copy-derive/src/shared/utils.rs index e92e56bb29..ed224d23a9 100644 --- a/program-libs/zero-copy-derive/src/shared/utils.rs +++ b/program-libs/zero-copy-derive/src/shared/utils.rs @@ -235,6 +235,13 @@ fn struct_has_copy_derive(attrs: &[Attribute]) -> bool { }) } +/// Checks if a struct has a #[light_hasher] attribute +pub fn struct_has_light_hasher_attribute(attrs: &[Attribute]) -> bool { + attrs + .iter() + .any(|attr| attr.path().is_ident("light_hasher")) +} + /// Determines whether a struct implements Copy by checking for the #[derive(Copy)] attribute. /// Results are cached for performance. /// diff --git a/program-libs/zero-copy-derive/src/shared/z_struct.rs b/program-libs/zero-copy-derive/src/shared/z_struct.rs index e7cf42b4e1..70906b0ebc 100644 --- a/program-libs/zero-copy-derive/src/shared/z_struct.rs +++ b/program-libs/zero-copy-derive/src/shared/z_struct.rs @@ -293,6 +293,12 @@ fn generate_struct_fields_with_zerocopy_types<'a, const MUT: bool>( pub #field_name: <#field_type as #trait_name<'a>>::Output } } + // FieldType::Bool(field_name) => { + // quote! { + // #(#attributes)* + // pub #field_name: >::Output + // } + // } FieldType::Copy(field_name, field_type) => { let zerocopy_type = utils::convert_to_zerocopy_type(field_type); quote! { @@ -377,13 +383,14 @@ pub fn generate_z_struct( } else { quote! {} }; - let hasher_flatten = if hasher { - quote! { - #[flatten] - } - } else { - quote! {} - }; + // let hasher_flatten = if hasher { + // quote! { + // #[flatten] + // } + // } else { + // quote! {} + // }; + let hasher_flatten = quote! {}; let partial_eq_derive = if MUT { quote!() } else { quote!(, PartialEq) }; diff --git a/program-libs/zero-copy-derive/src/shared/zero_copy_new.rs b/program-libs/zero-copy-derive/src/shared/zero_copy_new.rs index 495977cbf0..d6190fdefa 100644 --- a/program-libs/zero-copy-derive/src/shared/zero_copy_new.rs +++ b/program-libs/zero-copy-derive/src/shared/zero_copy_new.rs @@ -86,16 +86,16 @@ pub fn generate_init_mut_impl( let result = quote! { impl<'a> light_zero_copy::init_mut::ZeroCopyNew<'a> for #struct_name { - type Config = #config_name; + type ZeroCopyConfig = #config_name; type Output = >::Output; - fn byte_len(config: &Self::Config) -> usize { + fn byte_len(config: &Self::ZeroCopyConfig) -> usize { #meta_size_calculation #(+ #byte_len_calculations)* } fn new_zero_copy( bytes: &'a mut [u8], - config: Self::Config, + config: Self::ZeroCopyConfig, ) -> Result<(Self::Output, &'a mut [u8]), light_zero_copy::errors::ZeroCopyError> { use zerocopy::Ref; @@ -145,7 +145,7 @@ pub fn config_type(field_type: &FieldType) -> syn::Result { // Complex Vec types: need config for each element FieldType::VecDynamicZeroCopy(_, vec_type) => { if let Some(inner_type) = utils::get_vec_inner_type(vec_type) { - quote! { Vec<<#inner_type as light_zero_copy::init_mut::ZeroCopyNew<'static>>::Config> } + quote! { Vec<<#inner_type as light_zero_copy::init_mut::ZeroCopyNew<'static>>::ZeroCopyConfig> } } else { return Err(syn::Error::new_spanned( vec_type, @@ -156,7 +156,7 @@ pub fn config_type(field_type: &FieldType) -> syn::Result { // Option types: delegate to the Option's Config type FieldType::Option(_, option_type) => { - quote! { <#option_type as light_zero_copy::init_mut::ZeroCopyNew<'static>>::Config } + quote! { <#option_type as light_zero_copy::init_mut::ZeroCopyNew<'static>>::ZeroCopyConfig } } // Fixed-size types don't need configuration @@ -173,7 +173,7 @@ pub fn config_type(field_type: &FieldType) -> syn::Result { // DynamicZeroCopy types: delegate to their Config type (Config is typically 'static) FieldType::DynamicZeroCopy(_, field_type) => { let field_type = utils::convert_to_zerocopy_type(field_type); - quote! { <#field_type as light_zero_copy::init_mut::ZeroCopyNew<'static>>::Config } + quote! { <#field_type as light_zero_copy::init_mut::ZeroCopyNew<'static>>::ZeroCopyConfig } } }; Ok(result) diff --git a/program-libs/zero-copy-derive/src/zero_copy.rs b/program-libs/zero-copy-derive/src/zero_copy.rs index 7e89094a6f..afc4c30495 100644 --- a/program-libs/zero-copy-derive/src/zero_copy.rs +++ b/program-libs/zero-copy-derive/src/zero_copy.rs @@ -194,6 +194,375 @@ pub fn generate_deserialize_impl( Ok(result) } +// #[cfg(test)] +// mod tests { +// use quote::format_ident; +// use rand::{prelude::SliceRandom, rngs::StdRng, thread_rng, Rng, SeedableRng}; +// use syn::parse_quote; + +// use super::*; + +// /// Generate a safe field name for testing +// fn random_ident(rng: &mut StdRng) -> String { +// // Use predetermined safe field names +// const FIELD_NAMES: &[&str] = &[ +// "field1", "field2", "field3", "field4", "field5", "value", "data", "count", "size", +// "flag", "name", "id", "code", "index", "key", "amount", "balance", "total", "result", +// "status", +// ]; + +// FIELD_NAMES.choose(rng).unwrap().to_string() +// } + +// /// Generate a random Rust type +// fn random_type(rng: &mut StdRng, _depth: usize) -> syn::Type { +// // Define our available types +// let types = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + +// // Randomly select a type index +// let selected = *types.choose(rng).unwrap(); + +// // Return the corresponding type +// match selected { +// 0 => parse_quote!(u8), +// 1 => parse_quote!(u16), +// 2 => parse_quote!(u32), +// 3 => parse_quote!(u64), +// 4 => parse_quote!(bool), +// 5 => parse_quote!(Vec), +// 6 => parse_quote!(Vec), +// 7 => parse_quote!(Vec), +// 8 => parse_quote!([u32; 12]), +// 9 => parse_quote!([Vec; 12]), +// 10 => parse_quote!([Vec; 20]), +// _ => unreachable!(), +// } +// } + +// /// Generate a random field +// fn random_field(rng: &mut StdRng) -> Field { +// let name = random_ident(rng); +// let ty = random_type(rng, 0); + +// // Use a safer approach to create the field +// let name_ident = format_ident!("{}", name); +// parse_quote!(pub #name_ident: #ty) +// } + +// /// Generate a list of random fields +// fn random_fields(rng: &mut StdRng, count: usize) -> Vec { +// (0..count).map(|_| random_field(rng)).collect() +// } + +// // Test for Vec field deserialization +// #[test] +// fn test_deserialize_vec_u8() { +// let field: Field = parse_quote!(pub data: Vec); +// let struct_fields = vec![&field]; + +// let result = generate_deserialize_fields::(&struct_fields).collect::>(); +// let result_str = result[0].to_string(); +// let expected = +// "let (data , bytes) = light_zero_copy :: borsh :: borsh_vec_u8_as_slice (bytes) ?"; + +// assert!(result_str.contains(expected)); +// } + +// // Test for Vec with Copy inner type deserialization +// #[test] +// fn test_deserialize_vec_copy_type() { +// let field: Field = parse_quote!(pub values: Vec); +// let struct_fields = vec![&field]; + +// let result = generate_deserialize_fields::(&struct_fields).collect::>(); +// let result_str = result[0].to_string(); +// let expected = "let (values , bytes) = light_zero_copy :: slice :: ZeroCopySliceBorsh :: < 'a , < u32 as light_zero_copy :: borsh :: ZeroCopyStructInner > :: ZeroCopyInner > :: from_bytes_at (bytes) ?"; + +// assert!(result_str.contains(expected)); +// } + +// // Test for Vec with non-Copy inner type deserialization +// #[test] +// fn test_deserialize_vec_non_copy_type() { +// // This is a synthetic test as we're treating String as a non-Copy type +// let field: Field = parse_quote!(pub names: Vec); +// let struct_fields = vec![&field]; + +// let result = generate_deserialize_fields::(&struct_fields).collect::>(); +// let result_str = result[0].to_string(); +// let expected = "let (names , bytes) = < Vec < String > as light_zero_copy :: borsh :: Deserialize > :: zero_copy_at (bytes) ?"; + +// assert!(result_str.contains(expected)); +// } + +// // Test for Option type deserialization +// #[test] +// fn test_deserialize_option_type() { +// let field: Field = parse_quote!(pub maybe_value: Option); +// let struct_fields = vec![&field]; + +// let result = generate_deserialize_fields::(&struct_fields).collect::>(); +// let result_str = result[0].to_string(); +// let expected = "let (maybe_value , bytes) = < Option < u32 > as light_zero_copy :: borsh :: Deserialize > :: zero_copy_at (bytes) ?"; + +// assert!(result_str.contains(expected)); +// } + +// // Test for non-Copy type deserialization +// #[test] +// fn test_deserialize_non_copy_type() { +// // Using String as a non-Copy type example +// let field: Field = parse_quote!(pub name: String); +// let struct_fields = vec![&field]; + +// let result = generate_deserialize_fields::(&struct_fields).collect::>(); +// let result_str = result[0].to_string(); +// let expected = "let (name , bytes) = < String as light_zero_copy :: borsh :: Deserialize > :: zero_copy_at (bytes) ?"; + +// assert!(result_str.contains(expected)); +// } + +// // Test for Copy type deserialization (primitive types) +// #[test] +// fn test_deserialize_copy_type() { +// let field: Field = parse_quote!(pub count: u32); +// let struct_fields = vec![&field]; + +// let result = generate_deserialize_fields::(&struct_fields).collect::>(); +// let result_str = result[0].to_string(); +// let expected = "let (count , bytes) = light_zero_copy :: Ref :: < & 'a [u8] , light_zero_copy :: little_endian :: U32 > :: from_prefix (bytes) ?"; +// println!("{}", result_str); +// assert!(result_str.contains(expected)); +// } + +// // Test for boolean type deserialization +// #[test] +// fn test_deserialize_bool_type() { +// let field: Field = parse_quote!(pub flag: bool); +// let struct_fields = vec![&field]; + +// let result = generate_deserialize_fields::(&struct_fields).collect::>(); +// let result_str = result[0].to_string(); +// let expected = +// "let (flag , bytes) = < u8 as light_zero_copy :: borsh :: Deserialize > :: zero_copy_at (bytes) ?"; +// println!("{}", result_str); +// assert!(result_str.contains(expected)); +// } + +// // Test for field initialization code generation +// #[test] +// fn test_init_fields() { +// let field1: Field = parse_quote!(pub id: u32); +// let field2: Field = parse_quote!(pub name: String); +// let struct_fields = vec![&field1, &field2]; + +// let result = generate_init_fields(&struct_fields).collect::>(); +// let result_str = format!("{} {}", result[0], result[1]); +// assert!(result_str.contains("id")); +// assert!(result_str.contains("name")); +// } + +// // Test for complete deserialize implementation generation +// #[test] +// fn test_generate_deserialize_impl() { +// let struct_name = format_ident!("TestStruct"); +// let z_struct_name = format_ident!("ZTestStruct"); +// let z_struct_meta_name = format_ident!("ZTestStructMeta"); + +// let field1: Field = parse_quote!(pub id: u32); +// let field2: Field = parse_quote!(pub values: Vec); +// let struct_fields = vec![&field1, &field2]; + +// let result = generate_deserialize_impl::( +// &struct_name, +// &z_struct_name, +// &z_struct_meta_name, +// &struct_fields, +// false, +// ) +// .to_string(); + +// // Check impl header +// assert!(result +// .contains("impl < 'a > light_zero_copy :: borsh :: Deserialize < 'a > for TestStruct")); + +// // Check Output type +// assert!(result.contains("type Output = ZTestStruct < 'a >")); + +// // Check method signature +// assert!(result.contains("fn zero_copy_at (bytes : & 'a [u8]) -> Result")); + +// // Check meta field extraction +// assert!(result.contains("let (__meta , bytes) = light_zero_copy :: Ref :: < & 'a [u8] , ZTestStructMeta > :: from_prefix (bytes) ?")); + +// // Check field deserialization +// assert!(result.contains("let (id , bytes) = light_zero_copy :: Ref :: < & 'a [u8] , light_zero_copy :: little_endian :: U32 > :: from_prefix (bytes) ?")); +// assert!(result.contains("let (values , bytes) = light_zero_copy :: slice :: ZeroCopySliceBorsh :: < 'a , < u16 as light_zero_copy :: borsh :: ZeroCopyStructInner > :: ZeroCopyInner > :: from_bytes_at (bytes) ?")); + +// // Check result structure +// assert!(result.contains("Ok ((ZTestStruct { __meta , id , values ,")); +// } + +// // Test for complete deserialize implementation generation +// #[test] +// fn test_generate_deserialize_impl_no_meta() { +// let struct_name = format_ident!("TestStruct"); +// let z_struct_name = format_ident!("ZTestStruct"); +// let z_struct_meta_name = format_ident!("ZTestStructMeta"); + +// let field1: Field = parse_quote!(pub id: u32); +// let field2: Field = parse_quote!(pub values: Vec); +// let struct_fields = vec![&field1, &field2]; + +// let result = generate_deserialize_impl::( +// &struct_name, +// &z_struct_name, +// &z_struct_meta_name, +// &struct_fields, +// true, +// ) +// .to_string(); + +// // Check impl header +// assert!(result +// .contains("impl < 'a > light_zero_copy :: borsh :: Deserialize < 'a > for TestStruct")); + +// // Check Output type +// assert!(result.contains("type Output = ZTestStruct < 'a >")); + +// // Check method signature +// assert!(result.contains("fn zero_copy_at (bytes : & 'a [u8]) -> Result")); + +// // Check meta field extraction +// assert!(!result.contains("let (meta , bytes) = light_zero_copy :: Ref :: < & 'a [u8] , ZTestStructMeta > :: from_prefix (bytes) ?")); + +// // Check field deserialization +// assert!(result.contains("let (id , bytes) = light_zero_copy :: Ref :: < & 'a [u8] , light_zero_copy :: little_endian :: U32 > :: from_prefix (bytes) ?")); +// assert!(result.contains("let (values , bytes) = light_zero_copy :: slice :: ZeroCopySliceBorsh :: < 'a , < u16 as light_zero_copy :: borsh :: ZeroCopyStructInner > :: ZeroCopyInner > :: from_bytes_at (bytes) ?")); + +// // Check result structure +// assert!(result.contains("Ok ((ZTestStruct { id , values ,")); +// } + +// #[test] +// fn test_fuzz_generate_deserialize_impl() { +// // Set up RNG with a seed for reproducibility +// let seed = thread_rng().gen(); +// println!("seed {}", seed); +// let mut rng = StdRng::seed_from_u64(seed); + +// // Number of iterations for the test +// let num_iters = 10000; + +// for i in 0..num_iters { +// // Generate a random struct name +// let struct_name = format_ident!("{}", random_ident(&mut rng)); +// let z_struct_name = format_ident!("Z{}", struct_name); +// let z_struct_meta_name = format_ident!("Z{}Meta", struct_name); + +// // Generate random number of fields (1-10) +// let field_count = rng.gen_range(1..11); +// let fields = random_fields(&mut rng, field_count); + +// // Create a named fields collection +// let syn_fields = syn::punctuated::Punctuated::from_iter(fields.iter().cloned()); +// let fields_named = syn::FieldsNamed { +// brace_token: syn::token::Brace::default(), +// named: syn_fields, +// }; + +// // Split into meta fields and struct fields +// let (_, struct_fields) = crate::utils::process_fields(&fields_named); + +// // Call the function we're testing +// let result = generate_deserialize_impl::( +// &struct_name, +// &z_struct_name, +// &z_struct_meta_name, +// &struct_fields, +// false, +// ); + +// // Get the generated code as a string for validation +// let result_str = result.to_string(); + +// // Print the first result for debugging +// if i == 0 { +// println!("Generated deserialize_impl code format:\n{}", result_str); +// } + +// // Verify the result contains expected elements +// // Basic validation - must be non-empty +// assert!( +// !result_str.is_empty(), +// "Failed to generate TokenStream for iteration {}", +// i +// ); + +// // Validate that the generated code contains the expected impl definition +// let impl_pattern = format!( +// "impl < 'a > light_zero_copy :: borsh :: Deserialize < 'a > for {}", +// struct_name +// ); +// assert!( +// result_str.contains(&impl_pattern), +// "Generated code missing impl definition for iteration {}. Expected: {}", +// i, +// impl_pattern +// ); + +// // Validate type Output is defined +// let output_pattern = format!("type Output = {} < 'a >", z_struct_name); +// assert!( +// result_str.contains(&output_pattern), +// "Generated code missing Output type for iteration {}. Expected: {}", +// i, +// output_pattern +// ); + +// // Validate the zero_copy_at method is present +// assert!( +// result_str.contains("fn zero_copy_at (bytes : & 'a [u8])"), +// "Generated code missing zero_copy_at method for iteration {}", +// i +// ); + +// // Check for meta field extraction +// let meta_extraction_pattern = format!( +// "let (__meta , bytes) = light_zero_copy :: Ref :: < & 'a [u8] , {} > :: from_prefix (bytes) ?", +// z_struct_meta_name +// ); +// assert!( +// result_str.contains(&meta_extraction_pattern), +// "Generated code missing meta field extraction for iteration {}", +// i +// ); + +// // Check for return with Ok pattern +// assert!( +// result_str.contains("Ok (("), +// "Generated code missing Ok return statement for iteration {}", +// i +// ); + +// // Check for the struct initialization +// let struct_init_pattern = format!("{} {{", z_struct_name); +// assert!( +// result_str.contains(&struct_init_pattern), +// "Generated code missing struct initialization for iteration {}", +// i +// ); + +// // Check for meta field in the returned struct +// assert!( +// result_str.contains("__meta ,"), +// "Generated code missing meta field in struct initialization for iteration {}", +// i +// ); +// } +// } +// } + /// Generates the ZeroCopyStructInner implementation as a TokenStream pub fn generate_zero_copy_struct_inner( name: &Ident, @@ -221,7 +590,15 @@ pub fn derive_zero_copy_impl(input: ProcTokenStream) -> syn::Result/&[u8] hash inconsistency + if hasher { + return Err(syn::Error::new_spanned( + &input, + "#[light_hasher] attribute is currently disabled due to hash inconsistency between Vec and &[u8] slice representations in ZStruct vs original struct. The original struct hashes Vec fields while the ZStruct hashes &[u8] slice fields, producing different hash values.", + )); + } // Process the input to extract struct information let (name, z_struct_name, z_struct_meta_name, fields) = utils::process_input(&input)?; @@ -265,202 +642,3 @@ pub fn derive_zero_copy_impl(input: ProcTokenStream) -> syn::Result String { - // Use predetermined safe field names - const FIELD_NAMES: &[&str] = &[ - "field1", "field2", "field3", "field4", "field5", "value", "data", "count", "size", - "flag", "name", "id", "code", "index", "key", "amount", "balance", "total", "result", - "status", - ]; - - FIELD_NAMES.choose(rng).unwrap().to_string() - } - - /// Generate a random Rust type - fn random_type(rng: &mut StdRng, _depth: usize) -> syn::Type { - // Define our available types - let types = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; - - // Randomly select a type index - let selected = *types.choose(rng).unwrap(); - - // Return the corresponding type - match selected { - 0 => parse_quote!(u8), - 1 => parse_quote!(u16), - 2 => parse_quote!(u32), - 3 => parse_quote!(u64), - 4 => parse_quote!(bool), - 5 => parse_quote!(Vec), - 6 => parse_quote!(Vec), - 7 => parse_quote!(Vec), - 8 => parse_quote!([u32; 12]), - 9 => parse_quote!([Vec; 12]), - 10 => parse_quote!([Vec; 20]), - _ => unreachable!(), - } - } - - /// Generate a random field - fn random_field(rng: &mut StdRng) -> Field { - let name = random_ident(rng); - let ty = random_type(rng, 0); - - // Use a safer approach to create the field - let name_ident = format_ident!("{}", name); - parse_quote!(pub #name_ident: #ty) - } - - /// Generate a list of random fields - fn random_fields(rng: &mut StdRng, count: usize) -> Vec { - (0..count).map(|_| random_field(rng)).collect() - } - - // Test for field initialization code generation - behavioral test - #[test] - fn test_init_fields() { - let field1: Field = parse_quote!(pub id: u32); - let field2: Field = parse_quote!(pub name: String); - let struct_fields = vec![&field1, &field2]; - - let result = generate_init_fields(&struct_fields).collect::>(); - assert_eq!( - result.len(), - 2, - "Should generate exactly 2 field initializations" - ); - - let result_str = format!("{} {}", result[0], result[1]); - assert!(result_str.contains("id"), "Should contain 'id' field"); - assert!(result_str.contains("name"), "Should contain 'name' field"); - } - - #[test] - fn test_fuzz_generate_deserialize_impl() { - // Set up RNG with a seed for reproducibility - let seed = thread_rng().gen(); - println!("seed {}", seed); - let mut rng = StdRng::seed_from_u64(seed); - - // Number of iterations for the test - let num_iters = 10000; - - for i in 0..num_iters { - // Generate a random struct name - let struct_name = format_ident!("{}", random_ident(&mut rng)); - let z_struct_name = format_ident!("Z{}", struct_name); - let z_struct_meta_name = format_ident!("Z{}Meta", struct_name); - - // Generate random number of fields (1-10) - let field_count = rng.gen_range(1..11); - let fields = random_fields(&mut rng, field_count); - - // Create a named fields collection - let syn_fields = syn::punctuated::Punctuated::from_iter(fields.iter().cloned()); - let fields_named = syn::FieldsNamed { - brace_token: syn::token::Brace::default(), - named: syn_fields, - }; - - // Split into meta fields and struct fields - let (_, struct_fields) = crate::shared::utils::process_fields(&fields_named); - - // Call the function we're testing - let result = generate_deserialize_impl::( - &struct_name, - &z_struct_name, - &z_struct_meta_name, - &struct_fields, - false, - quote! {}, - ); - - // Get the generated code as a string for validation - let result_str = result.unwrap().to_string(); - - // Print the first result for debugging - if i == 0 { - println!("Generated deserialize_impl code format:\n{}", result_str); - } - - // Verify the result contains expected elements - // Basic validation - must be non-empty - assert!( - !result_str.is_empty(), - "Failed to generate TokenStream for iteration {}", - i - ); - - // Validate that the generated code contains the expected impl definition - let impl_pattern = format!( - "impl < 'a > light_zero_copy :: borsh :: Deserialize < 'a > for {}", - struct_name - ); - assert!( - result_str.contains(&impl_pattern), - "Generated code missing impl definition for iteration {}. Expected: {}", - i, - impl_pattern - ); - - // Validate type Output is defined - let output_pattern = format!("type Output = {} < 'a >", z_struct_name); - assert!( - result_str.contains(&output_pattern), - "Generated code missing Output type for iteration {}. Expected: {}", - i, - output_pattern - ); - - // Validate the zero_copy_at method is present - assert!( - result_str.contains("fn zero_copy_at (bytes : & 'a [u8])"), - "Generated code missing zero_copy_at method for iteration {}", - i - ); - - // Check for meta field extraction - let meta_extraction_pattern = format!( - "let (__meta , bytes) = light_zero_copy :: Ref :: < & 'a [u8] , {} > :: from_prefix (bytes) ?", - z_struct_meta_name - ); - assert!( - result_str.contains(&meta_extraction_pattern), - "Generated code missing meta field extraction for iteration {}", - i - ); - - // Check for return with Ok pattern - assert!( - result_str.contains("Ok (("), - "Generated code missing Ok return statement for iteration {}", - i - ); - - // Check for the struct initialization - let struct_init_pattern = format!("{} {{", z_struct_name); - assert!( - result_str.contains(&struct_init_pattern), - "Generated code missing struct initialization for iteration {}", - i - ); - - // Check for meta field in the returned struct - assert!( - result_str.contains("__meta ,"), - "Generated code missing meta field in struct initialization for iteration {}", - i - ); - } - } -} diff --git a/program-libs/zero-copy-derive/tests/cross_crate_copy.rs b/program-libs/zero-copy-derive/tests/cross_crate_copy.rs index dad348eee2..10dbd5f51e 100644 --- a/program-libs/zero-copy-derive/tests/cross_crate_copy.rs +++ b/program-libs/zero-copy-derive/tests/cross_crate_copy.rs @@ -165,8 +165,8 @@ mod tests { assert_eq!(deserialized.b.get(), 20); // U16 via .get() assert_eq!(deserialized.c.get(), 30); // U32 via .get() assert_eq!(deserialized.d.get(), 40); // U64 via .get() - assert_eq!(deserialized.g.get(), 50); // U32 via .get() assert!(deserialized.e()); // bool accessor method + assert_eq!(deserialized.g.get(), 50); // U32 via .get() // Test ZeroCopyEq (PartialEq implementation) let original = PrimitiveCopyStruct2 { diff --git a/program-libs/zero-copy-derive/tests/instruction_data.rs b/program-libs/zero-copy-derive/tests/instruction_data.rs index 094248e4c8..74c7a76179 100644 --- a/program-libs/zero-copy-derive/tests/instruction_data.rs +++ b/program-libs/zero-copy-derive/tests/instruction_data.rs @@ -66,16 +66,16 @@ impl PartialEq<>::Output> for Pubkey { } impl<'a> light_zero_copy::init_mut::ZeroCopyNew<'a> for Pubkey { - type Config = (); + type ZeroCopyConfig = (); type Output = >::Output; - fn byte_len(_config: &Self::Config) -> usize { + fn byte_len(_config: &Self::ZeroCopyConfig) -> usize { 32 // Pubkey is always 32 bytes } fn new_zero_copy( bytes: &'a mut [u8], - _config: Self::Config, + _config: Self::ZeroCopyConfig, ) -> Result<(Self::Output, &'a mut [u8]), ZeroCopyError> { Self::zero_copy_at_mut(bytes) } diff --git a/program-libs/zero-copy/src/init_mut.rs b/program-libs/zero-copy/src/init_mut.rs index c16d371176..eca0e6ab05 100644 --- a/program-libs/zero-copy/src/init_mut.rs +++ b/program-libs/zero-copy/src/init_mut.rs @@ -12,7 +12,11 @@ where Self: Sized, { /// Configuration type needed to initialize this type +<<<<<<< HEAD type Config; +======= + type ZeroCopyConfig; +>>>>>>> 37c039ad1 (feat: zero-copy-derive) /// Output type - the mutable zero-copy view of this type type Output; @@ -20,14 +24,22 @@ where /// Calculate the byte length needed for this type with the given configuration /// /// This is essential for allocating the correct buffer size before calling new_zero_copy +<<<<<<< HEAD fn byte_len(config: &Self::Config) -> usize; +======= + fn byte_len(config: &Self::ZeroCopyConfig) -> usize; +>>>>>>> 37c039ad1 (feat: zero-copy-derive) /// Initialize this type in a mutable byte slice with the given configuration /// /// Returns the initialized mutable view and remaining bytes fn new_zero_copy( bytes: &'a mut [u8], +<<<<<<< HEAD config: Self::Config, +======= + config: Self::ZeroCopyConfig, +>>>>>>> 37c039ad1 (feat: zero-copy-derive) ) -> Result<(Self::Output, &'a mut [u8]), ZeroCopyError>; } @@ -36,10 +48,17 @@ impl<'a, T> ZeroCopyNew<'a> for Option where T: ZeroCopyNew<'a>, { +<<<<<<< HEAD type Config = (bool, T::Config); // (enabled, inner_config) type Output = Option; fn byte_len(config: &Self::Config) -> usize { +======= + type ZeroCopyConfig = (bool, T::ZeroCopyConfig); // (enabled, inner_config) + type Output = Option; + + fn byte_len(config: &Self::ZeroCopyConfig) -> usize { +>>>>>>> 37c039ad1 (feat: zero-copy-derive) let (enabled, inner_config) = config; if *enabled { // 1 byte for Some discriminant + inner type's byte_len @@ -52,7 +71,11 @@ where fn new_zero_copy( bytes: &'a mut [u8], +<<<<<<< HEAD config: Self::Config, +======= + config: Self::ZeroCopyConfig, +>>>>>>> 37c039ad1 (feat: zero-copy-derive) ) -> Result<(Self::Output, &'a mut [u8]), ZeroCopyError> { if bytes.is_empty() { return Err(ZeroCopyError::ArraySize(1, bytes.len())); @@ -75,16 +98,27 @@ where // Implementation for primitive types (no configuration needed) impl<'a> ZeroCopyNew<'a> for u64 { +<<<<<<< HEAD type Config = (); type Output = zerocopy::Ref<&'a mut [u8], zerocopy::little_endian::U64>; fn byte_len(_config: &Self::Config) -> usize { +======= + type ZeroCopyConfig = (); + type Output = zerocopy::Ref<&'a mut [u8], zerocopy::little_endian::U64>; + + fn byte_len(_config: &Self::ZeroCopyConfig) -> usize { +>>>>>>> 37c039ad1 (feat: zero-copy-derive) size_of::() } fn new_zero_copy( bytes: &'a mut [u8], +<<<<<<< HEAD _config: Self::Config, +======= + _config: Self::ZeroCopyConfig, +>>>>>>> 37c039ad1 (feat: zero-copy-derive) ) -> Result<(Self::Output, &'a mut [u8]), ZeroCopyError> { // Return U64 little-endian type for generated structs Ok(zerocopy::Ref::<&mut [u8], zerocopy::little_endian::U64>::from_prefix(bytes)?) @@ -92,16 +126,27 @@ impl<'a> ZeroCopyNew<'a> for u64 { } impl<'a> ZeroCopyNew<'a> for u32 { +<<<<<<< HEAD type Config = (); type Output = zerocopy::Ref<&'a mut [u8], zerocopy::little_endian::U32>; fn byte_len(_config: &Self::Config) -> usize { +======= + type ZeroCopyConfig = (); + type Output = zerocopy::Ref<&'a mut [u8], zerocopy::little_endian::U32>; + + fn byte_len(_config: &Self::ZeroCopyConfig) -> usize { +>>>>>>> 37c039ad1 (feat: zero-copy-derive) size_of::() } fn new_zero_copy( bytes: &'a mut [u8], +<<<<<<< HEAD _config: Self::Config, +======= + _config: Self::ZeroCopyConfig, +>>>>>>> 37c039ad1 (feat: zero-copy-derive) ) -> Result<(Self::Output, &'a mut [u8]), ZeroCopyError> { // Return U32 little-endian type for generated structs Ok(zerocopy::Ref::<&mut [u8], zerocopy::little_endian::U32>::from_prefix(bytes)?) @@ -109,16 +154,27 @@ impl<'a> ZeroCopyNew<'a> for u32 { } impl<'a> ZeroCopyNew<'a> for u16 { +<<<<<<< HEAD type Config = (); type Output = zerocopy::Ref<&'a mut [u8], zerocopy::little_endian::U16>; fn byte_len(_config: &Self::Config) -> usize { +======= + type ZeroCopyConfig = (); + type Output = zerocopy::Ref<&'a mut [u8], zerocopy::little_endian::U16>; + + fn byte_len(_config: &Self::ZeroCopyConfig) -> usize { +>>>>>>> 37c039ad1 (feat: zero-copy-derive) size_of::() } fn new_zero_copy( bytes: &'a mut [u8], +<<<<<<< HEAD _config: Self::Config, +======= + _config: Self::ZeroCopyConfig, +>>>>>>> 37c039ad1 (feat: zero-copy-derive) ) -> Result<(Self::Output, &'a mut [u8]), ZeroCopyError> { // Return U16 little-endian type for generated structs Ok(zerocopy::Ref::<&mut [u8], zerocopy::little_endian::U16>::from_prefix(bytes)?) @@ -126,16 +182,27 @@ impl<'a> ZeroCopyNew<'a> for u16 { } impl<'a> ZeroCopyNew<'a> for u8 { +<<<<<<< HEAD type Config = (); type Output = >::Output; fn byte_len(_config: &Self::Config) -> usize { +======= + type ZeroCopyConfig = (); + type Output = >::Output; + + fn byte_len(_config: &Self::ZeroCopyConfig) -> usize { +>>>>>>> 37c039ad1 (feat: zero-copy-derive) size_of::() } fn new_zero_copy( bytes: &'a mut [u8], +<<<<<<< HEAD _config: Self::Config, +======= + _config: Self::ZeroCopyConfig, +>>>>>>> 37c039ad1 (feat: zero-copy-derive) ) -> Result<(Self::Output, &'a mut [u8]), ZeroCopyError> { // Use the DeserializeMut trait to create the proper output Self::zero_copy_at_mut(bytes) @@ -143,16 +210,27 @@ impl<'a> ZeroCopyNew<'a> for u8 { } impl<'a> ZeroCopyNew<'a> for bool { +<<<<<<< HEAD type Config = (); type Output = >::Output; fn byte_len(_config: &Self::Config) -> usize { +======= + type ZeroCopyConfig = (); + type Output = >::Output; + + fn byte_len(_config: &Self::ZeroCopyConfig) -> usize { +>>>>>>> 37c039ad1 (feat: zero-copy-derive) size_of::() // bool is serialized as u8 } fn new_zero_copy( bytes: &'a mut [u8], +<<<<<<< HEAD _config: Self::Config, +======= + _config: Self::ZeroCopyConfig, +>>>>>>> 37c039ad1 (feat: zero-copy-derive) ) -> Result<(Self::Output, &'a mut [u8]), ZeroCopyError> { // Treat bool as u8 u8::zero_copy_at_mut(bytes) @@ -166,16 +244,27 @@ impl< const N: usize, > ZeroCopyNew<'a> for [T; N] { +<<<<<<< HEAD type Config = (); type Output = >::Output; fn byte_len(_config: &Self::Config) -> usize { +======= + type ZeroCopyConfig = (); + type Output = >::Output; + + fn byte_len(_config: &Self::ZeroCopyConfig) -> usize { +>>>>>>> 37c039ad1 (feat: zero-copy-derive) size_of::() } fn new_zero_copy( bytes: &'a mut [u8], +<<<<<<< HEAD _config: Self::Config, +======= + _config: Self::ZeroCopyConfig, +>>>>>>> 37c039ad1 (feat: zero-copy-derive) ) -> Result<(Self::Output, &'a mut [u8]), ZeroCopyError> { // Use the DeserializeMut trait to create the proper output Self::zero_copy_at_mut(bytes) @@ -184,48 +273,81 @@ impl< // Implementation for zerocopy little-endian types impl<'a> ZeroCopyNew<'a> for zerocopy::little_endian::U16 { +<<<<<<< HEAD type Config = (); type Output = zerocopy::Ref<&'a mut [u8], zerocopy::little_endian::U16>; fn byte_len(_config: &Self::Config) -> usize { +======= + type ZeroCopyConfig = (); + type Output = zerocopy::Ref<&'a mut [u8], zerocopy::little_endian::U16>; + + fn byte_len(_config: &Self::ZeroCopyConfig) -> usize { +>>>>>>> 37c039ad1 (feat: zero-copy-derive) size_of::() } fn new_zero_copy( bytes: &'a mut [u8], +<<<<<<< HEAD _config: Self::Config, +======= + _config: Self::ZeroCopyConfig, +>>>>>>> 37c039ad1 (feat: zero-copy-derive) ) -> Result<(Self::Output, &'a mut [u8]), ZeroCopyError> { Ok(zerocopy::Ref::<&mut [u8], zerocopy::little_endian::U16>::from_prefix(bytes)?) } } impl<'a> ZeroCopyNew<'a> for zerocopy::little_endian::U32 { +<<<<<<< HEAD type Config = (); type Output = zerocopy::Ref<&'a mut [u8], zerocopy::little_endian::U32>; fn byte_len(_config: &Self::Config) -> usize { +======= + type ZeroCopyConfig = (); + type Output = zerocopy::Ref<&'a mut [u8], zerocopy::little_endian::U32>; + + fn byte_len(_config: &Self::ZeroCopyConfig) -> usize { +>>>>>>> 37c039ad1 (feat: zero-copy-derive) size_of::() } fn new_zero_copy( bytes: &'a mut [u8], +<<<<<<< HEAD _config: Self::Config, +======= + _config: Self::ZeroCopyConfig, +>>>>>>> 37c039ad1 (feat: zero-copy-derive) ) -> Result<(Self::Output, &'a mut [u8]), ZeroCopyError> { Ok(zerocopy::Ref::<&mut [u8], zerocopy::little_endian::U32>::from_prefix(bytes)?) } } impl<'a> ZeroCopyNew<'a> for zerocopy::little_endian::U64 { +<<<<<<< HEAD type Config = (); type Output = zerocopy::Ref<&'a mut [u8], zerocopy::little_endian::U64>; fn byte_len(_config: &Self::Config) -> usize { +======= + type ZeroCopyConfig = (); + type Output = zerocopy::Ref<&'a mut [u8], zerocopy::little_endian::U64>; + + fn byte_len(_config: &Self::ZeroCopyConfig) -> usize { +>>>>>>> 37c039ad1 (feat: zero-copy-derive) size_of::() } fn new_zero_copy( bytes: &'a mut [u8], +<<<<<<< HEAD _config: Self::Config, +======= + _config: Self::ZeroCopyConfig, +>>>>>>> 37c039ad1 (feat: zero-copy-derive) ) -> Result<(Self::Output, &'a mut [u8]), ZeroCopyError> { Ok(zerocopy::Ref::<&mut [u8], zerocopy::little_endian::U64>::from_prefix(bytes)?) } @@ -233,10 +355,17 @@ impl<'a> ZeroCopyNew<'a> for zerocopy::little_endian::U64 { // Implementation for Vec impl<'a, T: ZeroCopyNew<'a>> ZeroCopyNew<'a> for Vec { +<<<<<<< HEAD type Config = Vec; // Vector of configs for each item type Output = Vec; fn byte_len(config: &Self::Config) -> usize { +======= + type ZeroCopyConfig = Vec; // Vector of configs for each item + type Output = Vec; + + fn byte_len(config: &Self::ZeroCopyConfig) -> usize { +>>>>>>> 37c039ad1 (feat: zero-copy-derive) // 4 bytes for length prefix + sum of byte_len for each element config 4 + config .iter() @@ -246,7 +375,11 @@ impl<'a, T: ZeroCopyNew<'a>> ZeroCopyNew<'a> for Vec { fn new_zero_copy( bytes: &'a mut [u8], +<<<<<<< HEAD configs: Self::Config, +======= + configs: Self::ZeroCopyConfig, +>>>>>>> 37c039ad1 (feat: zero-copy-derive) ) -> Result<(Self::Output, &'a mut [u8]), ZeroCopyError> { use zerocopy::{little_endian::U32, Ref}; diff --git a/program-tests/compressed-token-test/Cargo.toml b/program-tests/compressed-token-test/Cargo.toml index 8f7ba53810..a2644d89d2 100644 --- a/program-tests/compressed-token-test/Cargo.toml +++ b/program-tests/compressed-token-test/Cargo.toml @@ -17,28 +17,29 @@ test-sbf = [] custom-heap = [] default = ["custom-heap"] -[dependencies] -anchor-lang = { workspace = true } -light-compressed-token = { workspace = true } -light-system-program-anchor = { workspace = true } -account-compression = { workspace = true } -light-compressed-account = { workspace = true } -light-batched-merkle-tree = { workspace = true } -light-registry = { workspace = true } - -[target.'cfg(not(target_os = "solana"))'.dependencies] -solana-sdk = { workspace = true } - [dev-dependencies] forester-utils = { workspace = true } -light-client = { workspace = true, features = ["devenv"] } +light-client = { workspace = true, features = ["devenv", "v2"] } light-sdk = { workspace = true, features = ["anchor"] } light-verifier = { workspace = true } light-test-utils = { workspace = true, features = ["devenv"] } light-program-test = { workspace = true, features = ["devenv"] } +light-compressed-token-sdk = { workspace = true } +light-zero-copy = { workspace = true } tokio = { workspace = true } light-prover-client = { workspace = true, features = ["devenv"] } spl-token = { workspace = true } +spl-pod = { workspace = true } anchor-spl = { workspace = true } rand = { workspace = true } serial_test = { workspace = true } +anchor-lang = { workspace = true } +light-compressed-token = { workspace = true } +light-ctoken-types = { workspace = true } +light-token-client = { workspace = true } +light-system-program-anchor = { workspace = true } +account-compression = { workspace = true } +light-compressed-account = { workspace = true } +light-batched-merkle-tree = { workspace = true } +light-registry = { workspace = true } +solana-sdk = { workspace = true } diff --git a/program-tests/compressed-token-test/tests/account.rs b/program-tests/compressed-token-test/tests/account.rs new file mode 100644 index 0000000000..e5d7fc5422 --- /dev/null +++ b/program-tests/compressed-token-test/tests/account.rs @@ -0,0 +1,435 @@ +// #![cfg(feature = "test-sbf")] + +use light_compressed_token_sdk::instructions::{ + close::close_account, create_associated_token_account::derive_ctoken_ata, create_token_account, +}; +use light_ctoken_types::{BASIC_TOKEN_ACCOUNT_SIZE, COMPRESSIBLE_TOKEN_ACCOUNT_SIZE}; +use light_program_test::{LightProgramTest, ProgramTestConfig}; +use light_test_utils::{ + assert_close_token_account::assert_close_token_account, + assert_create_token_account::{ + assert_create_associated_token_account, assert_create_token_account, CompressibleData, + }, + Rpc, +}; +use solana_sdk::{pubkey::Pubkey, signature::Keypair, signer::Signer}; + +#[tokio::test] +async fn test_create_and_close_token_account() { + let mut rpc = LightProgramTest::new(ProgramTestConfig::new_v2(false, None)) + .await + .unwrap(); + let payer = rpc.get_payer().insecure_clone(); + let payer_pubkey = payer.pubkey(); + + // Create a mock mint pubkey (we don't need actual mint for this test) + let mint_pubkey = Pubkey::new_unique(); + + // Create owner for the token account + let owner_keypair = Keypair::new(); + let owner_pubkey = owner_keypair.pubkey(); + + // Create a new keypair for the token account + let token_account_keypair = Keypair::new(); + let token_account_pubkey = token_account_keypair.pubkey(); + + // First create the account using system program + let create_account_system_ix = solana_sdk::system_instruction::create_account( + &payer_pubkey, + &token_account_pubkey, + rpc.get_minimum_balance_for_rent_exemption(165) + .await + .unwrap(), // SPL token account size + 165, + &light_compressed_token::ID, // Our program owns the account + ); + + // Then use SPL token SDK format but with our compressed token program ID + // This tests that our create_token_account instruction is compatible with SPL SDKs + let mut initialize_account_ix = + create_token_account(token_account_pubkey, mint_pubkey, owner_pubkey).unwrap(); + initialize_account_ix.data.push(0); + // Execute both instructions in one transaction + rpc.create_and_send_transaction( + &[create_account_system_ix, initialize_account_ix], + &payer.pubkey(), + &[&payer, &token_account_keypair], + ) + .await + .expect("Failed to create token account using SPL SDK"); + + // Verify the token account was created correctly + assert_create_token_account( + &mut rpc, + token_account_pubkey, + mint_pubkey, + owner_pubkey, + None, // Basic token account + ) + .await; + + // Now test closing the account using SPL SDK format + let destination_keypair = Keypair::new(); + let destination_pubkey = destination_keypair.pubkey(); + + // Airdrop some lamports to destination account so it exists + rpc.context.airdrop(&destination_pubkey, 1_000_000).unwrap(); + + // Get initial destination lamports before closing + let initial_destination_lamports = rpc + .get_account(destination_pubkey) + .await + .unwrap() + .unwrap() + .lamports; + + // Create close account instruction using SPL SDK format + let close_account_ix = close_account( + &light_compressed_token::ID, + &token_account_pubkey, + &destination_pubkey, + &owner_pubkey, + ); + + rpc.create_and_send_transaction( + &[close_account_ix], + &payer.pubkey(), + &[&payer, &owner_keypair], + ) + .await + .expect("Failed to close token account using SPL SDK"); + + // Verify the account was closed correctly + assert_close_token_account( + &mut rpc, + token_account_pubkey, + None, + destination_pubkey, + initial_destination_lamports, + ) + .await; +} + +#[tokio::test] +async fn test_create_and_close_account_with_rent_authority() { + use solana_sdk::{signature::Signer, system_instruction}; + + let mut rpc = LightProgramTest::new(ProgramTestConfig::new_v2(false, None)) + .await + .unwrap(); + let payer = rpc.get_payer().insecure_clone(); + let payer_pubkey = payer.pubkey(); + + // Create mint + let mint_pubkey = Pubkey::new_unique(); + + // Create account owner + let owner_keypair = Keypair::new(); + let owner_pubkey = owner_keypair.pubkey(); + + // Create rent authority + let rent_authority_keypair = Keypair::new(); + let rent_authority_pubkey = rent_authority_keypair.pubkey(); + + // Create rent recipient + let rent_recipient_keypair = Keypair::new(); + let rent_recipient_pubkey = rent_recipient_keypair.pubkey(); + + // Airdrop lamports to rent recipient so it exists + rpc.context + .airdrop(&rent_recipient_pubkey, 1_000_000) + .unwrap(); + + // Create token account keypair + let token_account_keypair = Keypair::new(); + let token_account_pubkey = token_account_keypair.pubkey(); + + // Create system account for token account with space for compressible extension + let rent_exempt_lamports = rpc + .get_minimum_balance_for_rent_exemption(COMPRESSIBLE_TOKEN_ACCOUNT_SIZE as usize) + .await + .unwrap(); + + let create_account_ix = system_instruction::create_account( + &payer_pubkey, + &token_account_pubkey, + rent_exempt_lamports, + COMPRESSIBLE_TOKEN_ACCOUNT_SIZE, + &light_compressed_token::ID, + ); + + // Create token account using SDK function with compressible extension + let create_token_account_ix = + light_compressed_token_sdk::instructions::create_compressible_token_account( + light_compressed_token_sdk::instructions::CreateCompressibleTokenAccount { + account_pubkey: token_account_pubkey, + mint_pubkey, + owner_pubkey, + rent_authority: rent_authority_pubkey, + rent_recipient: rent_recipient_pubkey, + slots_until_compression: 0, // Allow immediate compression + }, + ) + .unwrap(); + + rpc.create_and_send_transaction( + &[create_account_ix, create_token_account_ix], + &payer.pubkey(), + &[&payer, &token_account_keypair], + ) + .await + .expect("Failed to create token account"); + + // Verify the account was created correctly + assert_create_token_account( + &mut rpc, + token_account_pubkey, + mint_pubkey, + owner_pubkey, + Some(CompressibleData { + rent_authority: rent_authority_pubkey, + rent_recipient: rent_recipient_pubkey, + slots_until_compression: 0, + }), + ) + .await; + + // Get initial recipient lamports before closing + let initial_recipient_lamports = rpc + .get_account(rent_recipient_pubkey) + .await + .unwrap() + .unwrap() + .lamports; + + // First, try to close with rent authority (should fail for basic token account) + let close_account_ix = close_account( + &light_compressed_token::ID, + &token_account_pubkey, + &rent_recipient_pubkey, // Use rent recipient as destination + &rent_authority_pubkey, // Use rent authority as authority + ); + + // Get account data before closing for assertion + let account_data_before_close = rpc + .get_account(token_account_pubkey) + .await + .unwrap() + .unwrap() + .data; + + rpc.create_and_send_transaction( + &[close_account_ix], + &payer.pubkey(), + &[&payer, &rent_authority_keypair], + ) + .await + .unwrap(); + + // Verify the account was closed correctly + assert_close_token_account( + &mut rpc, + token_account_pubkey, + Some(&account_data_before_close), + rent_recipient_pubkey, + initial_recipient_lamports, + ) + .await; +} + +#[tokio::test] +async fn test_create_compressible_account_insufficient_size() { + use light_test_utils::spl::create_mint_helper; + use solana_sdk::{signature::Signer, system_instruction}; + + let mut rpc = LightProgramTest::new(ProgramTestConfig::new_v2(false, None)) + .await + .unwrap(); + let payer = rpc.get_payer().insecure_clone(); + let payer_pubkey = payer.pubkey(); + + // Create mint + let mint_pubkey = create_mint_helper(&mut rpc, &payer).await; + + // Create owner and rent authority keypairs + let owner_keypair = Keypair::new(); + let owner_pubkey = owner_keypair.pubkey(); + let rent_authority_keypair = Keypair::new(); + let rent_authority_pubkey = rent_authority_keypair.pubkey(); + let rent_recipient_keypair = Keypair::new(); + let rent_recipient_pubkey = rent_recipient_keypair.pubkey(); + + // Create token account keypair + let token_account_keypair = Keypair::new(); + let token_account_pubkey = token_account_keypair.pubkey(); + + // Create system account with INSUFFICIENT size - too small for compressible extension + let rent_exempt_lamports = rpc + .get_minimum_balance_for_rent_exemption(BASIC_TOKEN_ACCOUNT_SIZE as usize) + .await + .unwrap(); + + let create_account_ix = system_instruction::create_account( + &payer_pubkey, + &token_account_pubkey, + rent_exempt_lamports, + light_ctoken_types::BASIC_TOKEN_ACCOUNT_SIZE, // Intentionally too small for compressible extension + &light_compressed_token::ID, + ); + + // Create token account using SDK function with compressible extension + let create_token_account_ix = + light_compressed_token_sdk::instructions::create_compressible_token_account( + light_compressed_token_sdk::instructions::CreateCompressibleTokenAccount { + account_pubkey: token_account_pubkey, + mint_pubkey, + owner_pubkey, + rent_authority: rent_authority_pubkey, + rent_recipient: rent_recipient_pubkey, + slots_until_compression: 0, + }, + ) + .unwrap(); + + // Execute account creation - this should fail with account size error + let result = rpc + .create_and_send_transaction( + &[create_account_ix, create_token_account_ix], + &payer.pubkey(), + &[&payer, &token_account_keypair], + ) + .await; + assert!( + result.is_err(), + "Expected account creation to fail due to insufficient account size" + ); + + println!("✅ Correctly failed to create compressible token account with insufficient size"); +} + +#[tokio::test] +async fn test_create_associated_token_account() { + let mut rpc = LightProgramTest::new(ProgramTestConfig::new_v2(false, None)) + .await + .unwrap(); + let payer = rpc.get_payer().insecure_clone(); + let payer_pubkey = payer.pubkey(); + + // Create a mock mint pubkey + let mint_pubkey = Pubkey::new_unique(); + + // Create owner for the associated token account + let owner_keypair = Keypair::new(); + let owner_pubkey = owner_keypair.pubkey(); + + // Create basic ATA instruction using SDK function + let instruction = light_compressed_token_sdk::instructions::create_associated_token_account( + payer_pubkey, + owner_pubkey, + mint_pubkey, + ) + .unwrap(); + + // Execute the instruction + rpc.create_and_send_transaction(&[instruction], &payer.pubkey(), &[&payer]) + .await + .expect("Failed to create associated token account"); + + // Verify the associated token account was created correctly + assert_create_associated_token_account(&mut rpc, owner_pubkey, mint_pubkey, None).await; + + // Test compressible associated token account creation + println!("🧪 Testing compressible associated token account creation..."); + + // Create rent authority and recipient for compressible account + let rent_authority_keypair = Keypair::new(); + let rent_authority_pubkey = rent_authority_keypair.pubkey(); + let rent_recipient_keypair = Keypair::new(); + let rent_recipient_pubkey = rent_recipient_keypair.pubkey(); + + // Airdrop lamports to rent recipient so it exists + rpc.context + .airdrop(&rent_recipient_pubkey, 1_000_000) + .unwrap(); + + // Create a different owner for the compressible account + let compressible_owner_keypair = Keypair::new(); + let compressible_owner_pubkey = compressible_owner_keypair.pubkey(); + + // Create compressible ATA instruction using SDK function + let compressible_instruction = light_compressed_token_sdk::instructions::create_compressible_associated_token_account( + light_compressed_token_sdk::instructions::CreateCompressibleAssociatedTokenAccountInputs { + payer: payer_pubkey, + owner: compressible_owner_pubkey, + mint: mint_pubkey, + rent_authority: rent_authority_pubkey, + rent_recipient: rent_recipient_pubkey, + slots_until_compression: 0, + } + ).unwrap(); + + rpc.create_and_send_transaction(&[compressible_instruction], &payer.pubkey(), &[&payer]) + .await + .expect("Failed to create compressible associated token account"); + + // Verify the compressible associated token account was created correctly + assert_create_associated_token_account( + &mut rpc, + compressible_owner_pubkey, + mint_pubkey, + Some(CompressibleData { + rent_authority: rent_authority_pubkey, + rent_recipient: rent_recipient_pubkey, + slots_until_compression: 0, + }), + ) + .await; + + // Test that we can close the compressible account using rent authority + // Re-derive the ATA address for closing test + let (expected_compressible_ata_pubkey, _) = + derive_ctoken_ata(&compressible_owner_pubkey, &mint_pubkey); + + let initial_recipient_lamports = rpc + .get_account(rent_recipient_pubkey) + .await + .unwrap() + .unwrap() + .lamports; + + // Close account with rent authority + let close_account_ix = close_account( + &light_compressed_token::ID, + &expected_compressible_ata_pubkey, + &rent_recipient_pubkey, + &rent_authority_pubkey, + ); + + // Get account data before closing for assertion + let account_data_before_close = rpc + .get_account(expected_compressible_ata_pubkey) + .await + .unwrap() + .unwrap() + .data; + + rpc.create_and_send_transaction( + &[close_account_ix], + &payer.pubkey(), + &[&payer, &rent_authority_keypair], + ) + .await + .unwrap(); + + // Verify the compressible account was closed correctly + assert_close_token_account( + &mut rpc, + expected_compressible_ata_pubkey, + Some(&account_data_before_close), + rent_recipient_pubkey, + initial_recipient_lamports, + ) + .await; + + println!("✅ Both basic and compressible associated token accounts work correctly!"); +} diff --git a/program-tests/compressed-token-test/tests/mint.rs b/program-tests/compressed-token-test/tests/mint.rs new file mode 100644 index 0000000000..4e0d88fb6b --- /dev/null +++ b/program-tests/compressed-token-test/tests/mint.rs @@ -0,0 +1,863 @@ +// #![cfg(feature = "test-sbf")] + +use anchor_lang::{prelude::borsh::BorshDeserialize, solana_program::program_pack::Pack}; +use anchor_spl::token_2022::spl_token_2022; +use light_client::indexer::Indexer; +use light_compressed_token_sdk::instructions::{ + create_associated_token_account, derive_compressed_mint_address, derive_ctoken_ata, + find_spl_mint_address, +}; +use light_ctoken_types::{ + instructions::{ + extensions::token_metadata::TokenMetadataInstructionData, mint_to_compressed::Recipient, + }, + state::{ + extensions::{AdditionalMetadata, Metadata}, + CompressedMint, + }, + COMPRESSED_MINT_SEED, +}; +use light_program_test::{LightProgramTest, ProgramTestConfig}; +use light_test_utils::{ + assert_mint_to_compressed::assert_mint_to_compressed_one, + assert_spl_mint::assert_spl_mint, + assert_transfer2::{ + assert_transfer2, assert_transfer2_compress, assert_transfer2_decompress, + assert_transfer2_transfer, + }, + mint_assert::assert_compressed_mint_account, + Rpc, +}; +use light_token_client::{ + actions::{create_mint, create_spl_mint, mint_to_compressed, transfer2}, + instructions::transfer2::{ + create_decompress_instruction, create_generic_transfer2_instruction, CompressInput, + DecompressInput, Transfer2InstructionType, TransferInput, + }, +}; +use serial_test::serial; +use solana_sdk::{pubkey::Pubkey, signature::Keypair, signer::Signer}; + +/// 1. Create compressed mint (no metadata) +/// 2. Mint tokens with compressed mint +/// 3. Create SPL mint from compressed mint +/// 4. Transfer compressed tokens to new recipient +/// 5. Decompress compressed tokens to SPL tokens +/// 6. Compress SPL tokens to compressed tokens +/// 7. Multi-operation transaction (transfer + decompress + compress) +#[tokio::test] +#[serial] +async fn test_create_compressed_mint() { + let mut rpc = LightProgramTest::new(ProgramTestConfig::new_v2(false, None)) + .await + .unwrap(); + let payer = rpc.get_payer().insecure_clone(); + + // Get necessary values for the rest of the test + let address_tree_pubkey = rpc.get_address_tree_v2().tree; + let output_queue = rpc.get_random_state_tree_info().unwrap().queue; + + // Test parameters + let decimals = 6u8; + let mint_authority_keypair = Keypair::new(); // Create keypair so we can sign + let mint_authority = mint_authority_keypair.pubkey(); + let freeze_authority = Pubkey::new_unique(); + let mint_seed = Keypair::new(); + // Derive compressed mint address for verification + let compressed_mint_address = + derive_compressed_mint_address(&mint_seed.pubkey(), &address_tree_pubkey); + + // Find mint PDA for the rest of the test + let (spl_mint_pda, _) = find_spl_mint_address(&mint_seed.pubkey()); + + // 1. Create compressed mint (no metadata) + { + // Create compressed mint using the action + create_mint( + &mut rpc, + &mint_seed, + decimals, + mint_authority, + Some(freeze_authority), + None, // No metadata + &payer, + ) + .await + .unwrap(); + + // Verify the compressed mint was created + let compressed_mint_account = rpc + .indexer() + .unwrap() + .get_compressed_account(compressed_mint_address, None) + .await + .unwrap() + .value; + + assert_compressed_mint_account( + &compressed_mint_account, + compressed_mint_address, + spl_mint_pda, + decimals, + mint_authority, + freeze_authority, + None, // No metadata + ); + } + // 2. Mint tokens with compressed mint + // Test mint_to_compressed functionality + let recipient_keypair = Keypair::new(); + let recipient = recipient_keypair.pubkey(); + let mint_amount = 1000u64; + let expected_supply = mint_amount; // After minting tokens, SPL mint should have this supply + let lamports = Some(10000u64); + + // Use our mint_to_compressed action helper + { + mint_to_compressed( + &mut rpc, + spl_mint_pda, + vec![Recipient { + recipient: recipient.into(), + amount: mint_amount, + }], + &mint_authority_keypair, + &payer, + lamports, + ) + .await + .unwrap(); + + // Get pre-compressed mint for assertion + let pre_compressed_mint_account = rpc + .indexer() + .unwrap() + .get_compressed_account(compressed_mint_address, None) + .await + .unwrap() + .value; + let pre_compressed_mint: CompressedMint = BorshDeserialize::deserialize( + &mut pre_compressed_mint_account.data.unwrap().data.as_slice(), + ) + .unwrap(); + + // Verify minted tokens using our assertion helper + assert_mint_to_compressed_one( + &mut rpc, + spl_mint_pda, + recipient, + mint_amount, + expected_supply, + None, // No pre-token pool account for compressed mint + pre_compressed_mint, + None, // No pre-spl mint for compressed mint + ) + .await; + } + // 3. Create SPL mint from compressed mint + // Get compressed mint data before creating SPL mint + { + let pre_compressed_mint_account = rpc + .indexer() + .unwrap() + .get_compressed_account(compressed_mint_address, None) + .await + .unwrap() + .value; + let pre_compressed_mint: CompressedMint = BorshDeserialize::deserialize( + &mut pre_compressed_mint_account.data.unwrap().data.as_slice(), + ) + .unwrap(); + + // Use our create_spl_mint action helper (automatically handles proofs, PDAs, and transaction) + create_spl_mint( + &mut rpc, + compressed_mint_address, + &mint_seed, + &mint_authority_keypair, + &payer, + ) + .await + .unwrap(); + + // Verify SPL mint was created using our assertion helper + assert_spl_mint(&mut rpc, mint_seed.pubkey(), &pre_compressed_mint).await; + } + + // 4. Transfer compressed tokens to new recipient + // Get the compressed token account for decompression + let compressed_token_accounts = rpc + .indexer() + .unwrap() + .get_compressed_token_accounts_by_owner(&recipient, None, None) + .await + .unwrap() + .value + .items; + + let new_recipient_keypair = Keypair::new(); + let new_recipient = new_recipient_keypair.pubkey(); + let transfer_amount = mint_amount; // Transfer all tokens (1000) + transfer2::transfer( + &mut rpc, + &compressed_token_accounts, + new_recipient, + transfer_amount, + &recipient_keypair, + &payer, + ) + .await + .unwrap(); + + // Verify the transfer was successful using new transfer wrapper + assert_transfer2_transfer( + &mut rpc, + light_token_client::instructions::transfer2::TransferInput { + compressed_token_account: &compressed_token_accounts, + to: new_recipient, + amount: transfer_amount, + }, + ) + .await; + + // Get fresh compressed token accounts after the multi-transfer + let fresh_token_accounts = rpc + .indexer() + .unwrap() + .get_compressed_token_accounts_by_owner(&new_recipient, None, None) + .await + .unwrap() + .value + .items; + + assert!( + !fresh_token_accounts.is_empty(), + "Recipient should have compressed tokens after transfer" + ); + let compressed_token_account = &fresh_token_accounts[0]; + + let decompress_amount = 300u64; + + // 5. Decompress compressed tokens to SPL tokens + // Create compressed token associated token account for decompression + let (ctoken_ata_pubkey, _bump) = derive_ctoken_ata(&new_recipient, &spl_mint_pda); + let create_ata_instruction = + create_associated_token_account(payer.pubkey(), new_recipient, spl_mint_pda).unwrap(); + rpc.create_and_send_transaction(&[create_ata_instruction], &payer.pubkey(), &[&payer]) + .await + .unwrap(); + + // Get pre-decompress SPL token account state + let pre_decompress_account_data = rpc.get_account(ctoken_ata_pubkey).await.unwrap().unwrap(); + let pre_decompress_spl_account = + spl_token_2022::state::Account::unpack(&pre_decompress_account_data.data).unwrap(); + + // Create decompression instruction using the wrapper + let decompress_instruction = create_decompress_instruction( + &mut rpc, + std::slice::from_ref(compressed_token_account), + decompress_amount, + ctoken_ata_pubkey, + payer.pubkey(), + ) + .await + .unwrap(); + + // Send the decompression transaction + let tx_result = rpc + .create_and_send_transaction( + &[decompress_instruction], + &payer.pubkey(), + &[&payer, &new_recipient_keypair], + ) + .await; + + match tx_result { + Ok(_) => { + println!("✅ Decompression transaction sent successfully!"); + + // Use comprehensive decompress assertion + assert_transfer2_decompress( + &mut rpc, + light_token_client::instructions::transfer2::DecompressInput { + compressed_token_account: std::slice::from_ref(compressed_token_account), + decompress_amount, + solana_token_account: ctoken_ata_pubkey, + amount: decompress_amount, + }, + pre_decompress_spl_account, + ) + .await; + + println!(" - Decompression assertion completed successfully"); + } + Err(e) => { + println!("❌ Decompression transaction failed: {:?}", e); + panic!("Decompression transaction failed"); + } + } + + // 6. Compress SPL tokens to compressed tokens + // Test compressing tokens to a new account + + let compress_recipient = Keypair::new(); + let compress_amount = 100u64; // Compress 100 tokens + + // Get pre-compress SPL token account state + let pre_compress_account_data = rpc.get_account(ctoken_ata_pubkey).await.unwrap().unwrap(); + let pre_compress_spl_account = + spl_token_2022::state::Account::unpack(&pre_compress_account_data.data).unwrap(); + + // Create compress instruction using the multi-transfer functionality + let compress_instruction = create_generic_transfer2_instruction( + &mut rpc, + vec![Transfer2InstructionType::Compress(CompressInput { + compressed_token_account: None, // No existing compressed tokens + solana_token_account: ctoken_ata_pubkey, // Source SPL token account + to: compress_recipient.pubkey(), // New recipient for compressed tokens + mint: spl_mint_pda, + amount: compress_amount, + authority: new_recipient_keypair.pubkey(), // Authority for compression + output_queue, + })], + payer.pubkey(), + ) + .await + .unwrap(); + println!("Compress 0 in 1 out"); + // Execute compression + rpc.create_and_send_transaction( + &[compress_instruction], + &payer.pubkey(), + &[&payer, &new_recipient_keypair], + ) + .await + .unwrap(); + + // Use comprehensive compress assertion + assert_transfer2_compress( + &mut rpc, + light_token_client::instructions::transfer2::CompressInput { + compressed_token_account: None, + solana_token_account: ctoken_ata_pubkey, + to: compress_recipient.pubkey(), + mint: spl_mint_pda, + amount: compress_amount, + authority: new_recipient_keypair.pubkey(), + output_queue, + }, + pre_compress_spl_account, + ) + .await; + + // Create completely fresh compressed tokens for the transfer operation to avoid double spending + let transfer_source_recipient = Keypair::new(); + let transfer_compress_amount = 100u64; + let transfer_compress_instruction = create_generic_transfer2_instruction( + &mut rpc, + vec![Transfer2InstructionType::Compress(CompressInput { + compressed_token_account: None, + solana_token_account: ctoken_ata_pubkey, + to: transfer_source_recipient.pubkey(), + mint: spl_mint_pda, + amount: transfer_compress_amount, + authority: new_recipient_keypair.pubkey(), // Authority for compression + output_queue, + })], + payer.pubkey(), + ) + .await + .unwrap(); + println!("Compress 0 in 1 out"); + rpc.create_and_send_transaction( + &[transfer_compress_instruction], + &payer.pubkey(), + &[&payer, &new_recipient_keypair], + ) + .await + .unwrap(); + + let remaining_compressed_tokens = rpc + .indexer() + .unwrap() + .get_compressed_token_accounts_by_owner(&transfer_source_recipient.pubkey(), None, None) + .await + .unwrap() + .value + .items; + + // Create new compressed tokens specifically for the multi-operation test to avoid double spending + let multi_test_recipient = Keypair::new(); + let multi_compress_amount = 50u64; + let compress_for_multi_instruction = create_generic_transfer2_instruction( + &mut rpc, + vec![Transfer2InstructionType::Compress(CompressInput { + compressed_token_account: None, + solana_token_account: ctoken_ata_pubkey, + to: multi_test_recipient.pubkey(), + mint: spl_mint_pda, + amount: multi_compress_amount, + authority: new_recipient_keypair.pubkey(), // Authority for compression + output_queue, + })], + payer.pubkey(), + ) + .await + .unwrap(); + println!("Compress 0 in 1 out"); + rpc.create_and_send_transaction( + &[compress_for_multi_instruction], + &payer.pubkey(), + &[&payer, &new_recipient_keypair], + ) + .await + .unwrap(); + + let compressed_tokens_for_compress = rpc + .indexer() + .unwrap() + .get_compressed_token_accounts_by_owner(&multi_test_recipient.pubkey(), None, None) + .await + .unwrap() + .value + .items; + + // Create recipients for our multi-operation + let transfer_recipient = Keypair::new(); + let decompress_recipient = Keypair::new(); + let compress_from_spl_recipient = Keypair::new(); + + // Create SPL token account for compression source + let (compress_source_ata, _) = derive_ctoken_ata(&new_recipient, &spl_mint_pda); + // This already exists from our previous test + + // Create SPL token account for decompression destination + let (decompress_dest_ata, _) = derive_ctoken_ata(&decompress_recipient.pubkey(), &spl_mint_pda); + let create_decompress_ata_instruction = create_associated_token_account( + payer.pubkey(), + decompress_recipient.pubkey(), + spl_mint_pda, + ) + .unwrap(); + + rpc.create_and_send_transaction( + &[create_decompress_ata_instruction], + &payer.pubkey(), + &[&payer], + ) + .await + .unwrap(); + // 7. Multi-operation transaction (transfer + decompress + compress) + // Test transfer + compress + decompress + { + // Define amounts for each operation (ensure they don't exceed available balances) + let transfer_amount = 50u64; // From 700 compressed tokens - safe + let decompress_amount = 30u64; // From 100 compressed tokens - safe + let compress_amount_multi = 20u64; // From 200 SPL tokens - very conservative to avoid conflicts + + // Get output queues for the operations + let multi_output_queue = rpc.get_random_state_tree_info().unwrap().queue; + + // Get pre-account states for SPL token accounts + let pre_compress_source_data = rpc.get_account(compress_source_ata).await.unwrap().unwrap(); + let pre_compress_source_account = + spl_token_2022::state::Account::unpack(&pre_compress_source_data.data).unwrap(); + + let pre_decompress_dest_data = rpc.get_account(decompress_dest_ata).await.unwrap().unwrap(); + let pre_decompress_dest_account = + spl_token_2022::state::Account::unpack(&pre_decompress_dest_data.data).unwrap(); + let instruction_actions = vec![ + // 1. Transfer compressed tokens to a new recipient + Transfer2InstructionType::Transfer(TransferInput { + compressed_token_account: &remaining_compressed_tokens, + to: transfer_recipient.pubkey(), + amount: transfer_amount, + }), + // 2. Decompress some compressed tokens to SPL tokens + Transfer2InstructionType::Decompress(DecompressInput { + compressed_token_account: &compressed_tokens_for_compress, + decompress_amount, + solana_token_account: decompress_dest_ata, + amount: decompress_amount, + }), + // 3. Compress SPL tokens to compressed tokens + Transfer2InstructionType::Compress(CompressInput { + compressed_token_account: None, + solana_token_account: compress_source_ata, // Use remaining SPL tokens + to: compress_from_spl_recipient.pubkey(), + mint: spl_mint_pda, + amount: compress_amount_multi, + authority: new_recipient_keypair.pubkey(), // Authority for compression + output_queue: multi_output_queue, + }), + ]; + // Create the combined multi-transfer instruction + let transfer2_instruction = create_generic_transfer2_instruction( + &mut rpc, + instruction_actions.clone(), + payer.pubkey(), + ) + .await + .unwrap(); + + // Execute the combined instruction with multiple signers + println!( + "Transfer {} in 2 out, compress 0 in 1 out, decompress {} in 1 out", + remaining_compressed_tokens.len(), + compressed_tokens_for_compress.len() + ); + rpc.create_and_send_transaction( + &[transfer2_instruction], + &payer.pubkey(), + &[ + &payer, + &transfer_source_recipient, + &multi_test_recipient, + &new_recipient_keypair, + ], // Both token owners need to sign + ) + .await + .unwrap(); + + let pre_token_accounts = vec![ + None, // Transfer operation - no pre-account needed + Some(pre_decompress_dest_account), // Decompress operation - needs pre-account + Some(pre_compress_source_account), // Compress operation - needs pre-account + ]; + + assert_transfer2(&mut rpc, instruction_actions, pre_token_accounts).await; + } +} + +/// 1. Create compressed mint with metadata +/// 2. Create spl mint +/// 3. mint tokens with compressed mint +#[tokio::test] +#[serial] +async fn test_create_compressed_mint_with_token_metadata_poseidon() { + let mut rpc = LightProgramTest::new(ProgramTestConfig::new_v2(false, None)) + .await + .unwrap(); + let payer = rpc.get_payer().insecure_clone(); + + // Test parameters + let decimals = 6u8; + let mint_authority_keypair = Keypair::new(); + let mint_authority = mint_authority_keypair.pubkey(); + let freeze_authority = Pubkey::new_unique(); + let mint_seed = Keypair::new(); + + // Get address tree for creating compressed mint address + let address_tree_pubkey = rpc.get_address_tree_v2().tree; + // 1. Create compressed mint with metadata + + // Create token metadata extension with additional metadata + let additional_metadata = vec![ + AdditionalMetadata { + key: b"website".to_vec(), + value: b"https://mytoken.com".to_vec(), + }, + AdditionalMetadata { + key: b"category".to_vec(), + value: b"DeFi".to_vec(), + }, + AdditionalMetadata { + key: b"creator".to_vec(), + value: b"TokenMaker Inc.".to_vec(), + }, + ]; + + let token_metadata = TokenMetadataInstructionData { + update_authority: None, + metadata: Metadata { + name: b"Test Token".to_vec(), + symbol: b"TEST".to_vec(), + uri: b"https://example.com/token.json".to_vec(), + }, + additional_metadata: Some(additional_metadata.clone()), + version: 0, // Poseidon hash version + }; + light_token_client::actions::create_mint( + &mut rpc, + &mint_seed, + decimals, + mint_authority, + Some(freeze_authority), + Some(token_metadata.clone()), + &payer, + ) + .await + .unwrap(); + let (spl_mint_pda, _) = Pubkey::find_program_address( + &[COMPRESSED_MINT_SEED, mint_seed.pubkey().as_ref()], + &light_compressed_token::ID, + ); + let compressed_mint_address = light_compressed_token_sdk::instructions::create_compressed_mint::derive_compressed_mint_address(&mint_seed.pubkey(), &address_tree_pubkey); + + // Verify the compressed mint was created + let compressed_mint_account = rpc + .indexer() + .unwrap() + .get_compressed_account(compressed_mint_address, None) + .await + .unwrap() + .value; + + assert_compressed_mint_account( + &compressed_mint_account, + compressed_mint_address, + spl_mint_pda, + decimals, + mint_authority, + freeze_authority, + Some(token_metadata.clone()), + ); + + // 2. Create SPL mint + { + // Get compressed mint data before creating SPL mint + let pre_compressed_mint: CompressedMint = BorshDeserialize::deserialize( + &mut compressed_mint_account.data.unwrap().data.as_slice(), + ) + .unwrap(); + + // Use our create_spl_mint action helper (automatically handles proofs, PDAs, and transaction) + create_spl_mint( + &mut rpc, + compressed_mint_address, + &mint_seed, + &mint_authority_keypair, + &payer, + ) + .await + .unwrap(); + + // Verify SPL mint was created using our assertion helper + assert_spl_mint(&mut rpc, mint_seed.pubkey(), &pre_compressed_mint).await; + } + // 3. Mint to compressed + { + // Get pre-token pool account state for decompressed mint + let (token_pool_pda, _) = + light_compressed_token::instructions::create_token_pool::find_token_pool_pda_with_index( + &spl_mint_pda, + 0, + ); + let pre_pool_data = rpc.get_account(token_pool_pda).await.unwrap().unwrap(); + let pre_token_pool_account = + spl_token_2022::state::Account::unpack(&pre_pool_data.data).unwrap(); + + let mint_amount = 100_000u64; // Mint 100,000 tokens + let recipient_keypair = Keypair::new(); + let recipient = recipient_keypair.pubkey(); + + // Use our mint_to_compressed action helper (automatically handles decompressed mint config) + mint_to_compressed( + &mut rpc, + spl_mint_pda, + vec![Recipient { + recipient: recipient.into(), + amount: mint_amount, + }], + &mint_authority_keypair, + &payer, + None, // No lamports + ) + .await + .unwrap(); + + // Get pre-compressed mint and pre-spl mint for assertion + let pre_compressed_mint_account = rpc + .indexer() + .unwrap() + .get_compressed_account(compressed_mint_address, None) + .await + .unwrap() + .value; + let pre_compressed_mint: CompressedMint = BorshDeserialize::deserialize( + &mut pre_compressed_mint_account.data.unwrap().data.as_slice(), + ) + .unwrap(); + + let pre_spl_mint_data = rpc.get_account(spl_mint_pda).await.unwrap().unwrap(); + let pre_spl_mint = spl_token_2022::state::Mint::unpack(&pre_spl_mint_data.data).unwrap(); + + // Verify minted tokens using our assertion helper + assert_mint_to_compressed_one( + &mut rpc, + spl_mint_pda, + recipient, + mint_amount, + mint_amount, // Expected total supply after minting + Some(pre_token_pool_account), // Pass pre-token pool account for decompressed mint validation + pre_compressed_mint, + Some(pre_spl_mint), + ) + .await; + } +} + +#[tokio::test] +#[serial] +async fn test_create_compressed_mint_with_token_metadata_sha() { + let mut rpc = LightProgramTest::new(ProgramTestConfig::new_v2(false, None)) + .await + .unwrap(); + let payer = rpc.get_payer().insecure_clone(); + + // Test parameters + let decimals = 6u8; + let mint_authority_keypair = Keypair::new(); + let mint_authority = mint_authority_keypair.pubkey(); + let freeze_authority = Pubkey::new_unique(); + let mint_seed = Keypair::new(); + + // Get address tree for creating compressed mint address + let address_tree_pubkey = rpc.get_address_tree_v2().tree; + // 1. Create compressed mint with metadata + + // Create token metadata extension with additional metadata + let additional_metadata = vec![ + AdditionalMetadata { + key: b"website".to_vec(), + value: b"https://mytoken.com".to_vec(), + }, + AdditionalMetadata { + key: b"category".to_vec(), + value: b"DeFi".to_vec(), + }, + AdditionalMetadata { + key: b"creator".to_vec(), + value: b"TokenMaker Inc.".to_vec(), + }, + ]; + + let token_metadata = TokenMetadataInstructionData { + update_authority: None, + metadata: Metadata { + name: b"Test Token".to_vec(), + symbol: b"TEST".to_vec(), + uri: b"https://example.com/token.json".to_vec(), + }, + additional_metadata: Some(additional_metadata.clone()), + version: 1, // Sha hash version + }; + light_token_client::actions::create_mint( + &mut rpc, + &mint_seed, + decimals, + mint_authority, + Some(freeze_authority), + Some(token_metadata.clone()), + &payer, + ) + .await + .unwrap(); + let (spl_mint_pda, _) = Pubkey::find_program_address( + &[COMPRESSED_MINT_SEED, mint_seed.pubkey().as_ref()], + &light_compressed_token::ID, + ); + let compressed_mint_address = light_compressed_token_sdk::instructions::create_compressed_mint::derive_compressed_mint_address(&mint_seed.pubkey(), &address_tree_pubkey); + + // Verify the compressed mint was created + let compressed_mint_account = rpc + .indexer() + .unwrap() + .get_compressed_account(compressed_mint_address, None) + .await + .unwrap() + .value; + + assert_compressed_mint_account( + &compressed_mint_account, + compressed_mint_address, + spl_mint_pda, + decimals, + mint_authority, + freeze_authority, + Some(token_metadata.clone()), + ); + + // 2. Create SPL mint + { + // Get compressed mint data before creating SPL mint + let pre_compressed_mint: CompressedMint = BorshDeserialize::deserialize( + &mut compressed_mint_account.data.unwrap().data.as_slice(), + ) + .unwrap(); + + // Use our create_spl_mint action helper (automatically handles proofs, PDAs, and transaction) + create_spl_mint( + &mut rpc, + compressed_mint_address, + &mint_seed, + &mint_authority_keypair, + &payer, + ) + .await + .unwrap(); + + // Verify SPL mint was created using our assertion helper + assert_spl_mint(&mut rpc, mint_seed.pubkey(), &pre_compressed_mint).await; + } + // 3. Mint to compressed + { + // Get pre-token pool account state for decompressed mint + let (token_pool_pda, _) = + light_compressed_token::instructions::create_token_pool::find_token_pool_pda_with_index( + &spl_mint_pda, + 0, + ); + let pre_pool_data = rpc.get_account(token_pool_pda).await.unwrap().unwrap(); + let pre_token_pool_account = + spl_token_2022::state::Account::unpack(&pre_pool_data.data).unwrap(); + + let mint_amount = 100_000u64; // Mint 100,000 tokens + let recipient_keypair = Keypair::new(); + let recipient = recipient_keypair.pubkey(); + + // Use our mint_to_compressed action helper (automatically handles decompressed mint config) + mint_to_compressed( + &mut rpc, + spl_mint_pda, + vec![Recipient { + recipient: recipient.into(), + amount: mint_amount, + }], + &mint_authority_keypair, + &payer, + None, // No lamports + ) + .await + .unwrap(); + + // Get pre-compressed mint and pre-spl mint for assertion + let pre_compressed_mint_account = rpc + .indexer() + .unwrap() + .get_compressed_account(compressed_mint_address, None) + .await + .unwrap() + .value; + let pre_compressed_mint: CompressedMint = BorshDeserialize::deserialize( + &mut pre_compressed_mint_account.data.unwrap().data.as_slice(), + ) + .unwrap(); + + let pre_spl_mint_data = rpc.get_account(spl_mint_pda).await.unwrap().unwrap(); + let pre_spl_mint = spl_token_2022::state::Mint::unpack(&pre_spl_mint_data.data).unwrap(); + + // Verify minted tokens using our assertion helper + assert_mint_to_compressed_one( + &mut rpc, + spl_mint_pda, + recipient, + mint_amount, + mint_amount, // Expected total supply after minting + Some(pre_token_pool_account), // Pass pre-token pool account for decompressed mint validation + pre_compressed_mint, + Some(pre_spl_mint), + ) + .await; + } +} diff --git a/program-tests/compressed-token-test/tests/test.rs b/program-tests/compressed-token-test/tests/test.rs index c8e165d856..8590de9088 100644 --- a/program-tests/compressed-token-test/tests/test.rs +++ b/program-tests/compressed-token-test/tests/test.rs @@ -1,11 +1,11 @@ -#![cfg(feature = "test-sbf")] +// #![cfg(feature = "test-sbf")] use std::{assert_eq, str::FromStr}; use account_compression::errors::AccountCompressionErrorCode; use anchor_lang::{ - prelude::AccountMeta, system_program, AccountDeserialize, AnchorDeserialize, AnchorSerialize, - InstructionData, ToAccountMetas, + prelude::{borsh::BorshSerialize, AccountMeta}, + system_program, AccountDeserialize, AnchorDeserialize, InstructionData, ToAccountMetas, }; use anchor_spl::{ token::{Mint, TokenAccount}, @@ -5280,7 +5280,7 @@ async fn perform_transfer_failing_test( let mint = if invalid_mint { Pubkey::new_unique() } else { - input_compressed_account_token_data[0].mint + input_compressed_account_token_data[0].mint.into() }; let instruction = create_transfer_instruction( &payer.pubkey(), diff --git a/program-tests/create-address-test-program/src/lib.rs b/program-tests/create-address-test-program/src/lib.rs index 90c6d100d3..c54e47c75c 100644 --- a/program-tests/create-address-test-program/src/lib.rs +++ b/program-tests/create-address-test-program/src/lib.rs @@ -80,11 +80,15 @@ pub mod system_cpi_test { let cpi_accounts = CpiAccounts::new_with_config(&fee_payer, ctx.remaining_accounts, config); +<<<<<<< HEAD let account_infos = cpi_accounts .to_account_infos() .into_iter() .cloned() .collect::>(); +======= + let account_infos = cpi_accounts.to_account_infos(); +>>>>>>> 37c039ad1 (feat: zero-copy-derive) let config = CpiInstructionConfig::try_from(&cpi_accounts) .map_err(|_| ErrorCode::AccountNotEnoughKeys)?; diff --git a/program-tests/sdk-anchor-test/programs/sdk-anchor-test/tests/test.rs b/program-tests/sdk-anchor-test/programs/sdk-anchor-test/tests/test.rs index 96949c26e5..8b2b2b0a7a 100644 --- a/program-tests/sdk-anchor-test/programs/sdk-anchor-test/tests/test.rs +++ b/program-tests/sdk-anchor-test/programs/sdk-anchor-test/tests/test.rs @@ -92,7 +92,7 @@ async fn create_compressed_account( ) -> Result { let config = SystemAccountMetaConfig::new(sdk_anchor_test::ID); let mut remaining_accounts = PackedAccounts::default(); - remaining_accounts.add_system_accounts(config); + remaining_accounts.add_system_accounts(config).unwrap(); let address_merkle_tree_info = rpc.get_address_tree_v1(); @@ -149,7 +149,7 @@ async fn update_compressed_account( let mut remaining_accounts = PackedAccounts::default(); let config = SystemAccountMetaConfig::new(sdk_anchor_test::ID); - remaining_accounts.add_system_accounts(config); + remaining_accounts.add_system_accounts(config).unwrap(); let hash = compressed_account.hash; let rpc_result = rpc diff --git a/program-tests/sdk-pinocchio-test/tests/test.rs b/program-tests/sdk-pinocchio-test/tests/test.rs index a4a62e7eab..53872d7fb1 100644 --- a/program-tests/sdk-pinocchio-test/tests/test.rs +++ b/program-tests/sdk-pinocchio-test/tests/test.rs @@ -94,7 +94,9 @@ pub async fn create_pda( SystemAccountMetaConfig::new(Pubkey::new_from_array(sdk_pinocchio_test::ID)); let mut accounts = PackedAccounts::default(); accounts.add_pre_accounts_signer(payer.pubkey()); - accounts.add_system_accounts(system_account_meta_config); + accounts + .add_system_accounts(system_account_meta_config) + .unwrap(); let rpc_result = rpc .get_validity_proof( @@ -142,7 +144,9 @@ pub async fn update_pda( SystemAccountMetaConfig::new(Pubkey::new_from_array(sdk_pinocchio_test::ID)); let mut accounts = PackedAccounts::default(); accounts.add_pre_accounts_signer(payer.pubkey()); - accounts.add_system_accounts(system_account_meta_config); + accounts + .add_system_accounts(system_account_meta_config) + .unwrap(); let rpc_result = rpc .get_validity_proof(vec![compressed_account.hash().unwrap()], vec![], None) diff --git a/program-tests/sdk-test/tests/test.rs b/program-tests/sdk-test/tests/test.rs new file mode 100644 index 0000000000..6d126a52c3 --- /dev/null +++ b/program-tests/sdk-test/tests/test.rs @@ -0,0 +1,181 @@ +#![cfg(feature = "test-sbf")] + +use borsh::BorshSerialize; +use light_compressed_account::{ + address::derive_address, compressed_account::CompressedAccountWithMerkleContext, + hashv_to_bn254_field_size_be, +}; +use light_program_test::{ + program_test::LightProgramTest, AddressWithTree, Indexer, ProgramTestConfig, Rpc, RpcError, +}; +use light_sdk::instruction::{ + account_meta::CompressedAccountMeta, PackedAccounts, SystemAccountMetaConfig, +}; +use sdk_test::{ + create_pda::CreatePdaInstructionData, + update_pda::{UpdateMyCompressedAccount, UpdatePdaInstructionData}, +}; +use solana_sdk::{ + instruction::Instruction, + pubkey::Pubkey, + signature::{Keypair, Signer}, +}; + +#[tokio::test] +async fn test_sdk_test() { + let config = ProgramTestConfig::new_v2(true, Some(vec![("sdk_test", sdk_test::ID)])); + let mut rpc = LightProgramTest::new(config).await.unwrap(); + let payer = rpc.get_payer().insecure_clone(); + + let address_tree_pubkey = rpc.get_address_merkle_tree_v2(); + let account_data = [1u8; 31]; + + // // V1 trees + // let (address, _) = light_sdk::address::derive_address( + // &[b"compressed", &account_data], + // &address_tree_info, + // &sdk_test::ID, + // ); + // Batched trees + let address_seed = hashv_to_bn254_field_size_be(&[b"compressed", account_data.as_slice()]); + let address = derive_address( + &address_seed, + &address_tree_pubkey.to_bytes(), + &sdk_test::ID.to_bytes(), + ); + let ouput_queue = rpc.get_random_state_tree_info().unwrap().queue; + create_pda( + &payer, + &mut rpc, + &ouput_queue, + account_data, + address_tree_pubkey, + address, + ) + .await + .unwrap(); + + let compressed_pda = rpc + .indexer() + .unwrap() + .get_compressed_account(address, None) + .await + .unwrap() + .value + .clone(); + assert_eq!(compressed_pda.address.unwrap(), address); + + update_pda(&payer, &mut rpc, [2u8; 31], compressed_pda.into()) + .await + .unwrap(); +} + +pub async fn create_pda( + payer: &Keypair, + rpc: &mut LightProgramTest, + merkle_tree_pubkey: &Pubkey, + account_data: [u8; 31], + address_tree_pubkey: Pubkey, + address: [u8; 32], +) -> Result<(), RpcError> { + let system_account_meta_config = SystemAccountMetaConfig::new(sdk_test::ID); + let mut accounts = PackedAccounts::default(); + accounts.add_pre_accounts_signer(payer.pubkey()); + accounts + .add_system_accounts(system_account_meta_config) + .unwrap(); + + let rpc_result = rpc + .get_validity_proof( + vec![], + vec![AddressWithTree { + address, + tree: address_tree_pubkey, + }], + None, + ) + .await? + .value; + + let output_merkle_tree_index = accounts.insert_or_get(*merkle_tree_pubkey); + let packed_address_tree_info = rpc_result.pack_tree_infos(&mut accounts).address_trees[0]; + let (accounts, system_accounts_offset, tree_accounts_offset) = accounts.to_account_metas(); + + let instruction_data = CreatePdaInstructionData { + proof: rpc_result.proof.0.unwrap().into(), + address_tree_info: packed_address_tree_info, + data: account_data, + output_merkle_tree_index, + system_accounts_offset: system_accounts_offset as u8, + tree_accounts_offset: tree_accounts_offset as u8, + }; + let inputs = instruction_data.try_to_vec().unwrap(); + + let instruction = Instruction { + program_id: sdk_test::ID, + accounts, + data: [&[0u8][..], &inputs[..]].concat(), + }; + + rpc.create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await?; + Ok(()) +} + +pub async fn update_pda( + payer: &Keypair, + rpc: &mut LightProgramTest, + new_account_data: [u8; 31], + compressed_account: CompressedAccountWithMerkleContext, +) -> Result<(), RpcError> { + let system_account_meta_config = SystemAccountMetaConfig::new(sdk_test::ID); + let mut accounts = PackedAccounts::default(); + accounts.add_pre_accounts_signer(payer.pubkey()); + accounts + .add_system_accounts(system_account_meta_config) + .unwrap(); + + let rpc_result = rpc + .get_validity_proof(vec![compressed_account.hash().unwrap()], vec![], None) + .await? + .value; + + let packed_accounts = rpc_result + .pack_tree_infos(&mut accounts) + .state_trees + .unwrap(); + + let meta = CompressedAccountMeta { + tree_info: packed_accounts.packed_tree_infos[0], + address: compressed_account.compressed_account.address.unwrap(), + output_state_tree_index: packed_accounts.output_tree_index, + }; + + let (accounts, system_accounts_offset, _) = accounts.to_account_metas(); + let instruction_data = UpdatePdaInstructionData { + my_compressed_account: UpdateMyCompressedAccount { + meta, + data: compressed_account + .compressed_account + .data + .unwrap() + .data + .try_into() + .unwrap(), + }, + proof: rpc_result.proof, + new_data: new_account_data, + system_accounts_offset: system_accounts_offset as u8, + }; + let inputs = instruction_data.try_to_vec().unwrap(); + + let instruction = Instruction { + program_id: sdk_test::ID, + accounts, + data: [&[1u8][..], &inputs[..]].concat(), + }; + + rpc.create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await?; + Ok(()) +} diff --git a/program-tests/sdk-token-test/CLAUDE.md b/program-tests/sdk-token-test/CLAUDE.md new file mode 100644 index 0000000000..6d7ec5636d --- /dev/null +++ b/program-tests/sdk-token-test/CLAUDE.md @@ -0,0 +1,170 @@ +# SDK Token Test Debugging Guide + +## Error Code Reference + +| Error Code | Error Name | Description | Common Fix | +|------------|------------|-------------|------------| +| 16031 | `CpiAccountsIndexOutOfBounds` | Missing account in accounts array | Add signer with `add_pre_accounts_signer_mut()` | +| 6020 | `CpiContextAccountUndefined` | CPI context expected but not provided | Set `cpi_context: None` for simple operations | + +### Light System Program Errors (Full Reference) +| 6017 | `ProofIsNone` | 6018 | `ProofIsSome` | 6019 | `EmptyInputs` | 6020 | `CpiContextAccountUndefined` | +| 6021 | `CpiContextEmpty` | 6022 | `CpiContextMissing` | 6023 | `DecompressionRecipientDefined` | + +## Common Issues and Solutions + +### 1. `CpiAccountsIndexOutOfBounds` (Error 16031) +Missing signer account. **Fix**: `remaining_accounts.add_pre_accounts_signer_mut(payer.pubkey())` + +### 2. Privilege Escalation Error +Manually adding accounts instead of using PackedAccounts. **Fix**: Use `add_pre_accounts_signer_mut()` instead of manual account concatenation. + +### 3. Account Structure Mismatch +Wrong context type. **Fix**: Use `Generic<'info>` for single signer, `GenericWithAuthority<'info>` for signer + authority. + +### 4. `CpiContextAccountUndefined` (Error 6020) +**Root Cause**: Using functions designed for CPI context when you don't need it. + +**CPI Context Purpose**: Optimize multi-program transactions by using one proof instead of multiple. Flow: +1. First program: Cache signer checks in CPI context +2. Second program: Read context, combine data, execute with single proof + +**Solutions**: +```rust +// ✅ Simple operations - no CPI context +let cpi_inputs = CpiInputs { + proof, + account_infos: Some(vec![account.to_account_info().unwrap()]), + new_addresses: Some(vec![new_address_params]), + cpi_context: None, // ← Key + ..Default::default() +}; + +// ✅ Complex multi-program operations - use CPI context +let config = SystemAccountMetaConfig::new_with_cpi_context(program_id, cpi_context_account); +``` + +### 5. Avoid Complex Function Reuse +**Problem**: Functions like `process_create_compressed_account` expect CPI context setup. + +**Fix**: Use direct Light SDK approach: +```rust +// ❌ Complex function with CPI context dependency +process_create_compressed_account(...) + +// ✅ Direct approach +let mut account = LightAccount::<'_, CompressedEscrowPda>::new_init(&crate::ID, Some(address), tree_index); +account.amount = amount; +account.owner = *cpi_accounts.fee_payer().key; +let cpi_inputs = CpiInputs { proof, account_infos: Some(vec![account.to_account_info().unwrap()]), cpi_context: None, ..Default::default() }; +cpi_inputs.invoke_light_system_program(cpi_accounts) +``` + +### 6. Critical Four Invokes Implementation Learnings + +**CompressInputs Structure for CPI Context Operations**: +```rust +let compress_inputs = CompressInputs { + fee_payer: *cpi_accounts.fee_payer().key, + authority: *cpi_accounts.fee_payer().key, + mint, + recipient, + sender_token_account: *remaining_accounts[0].key, // ← Use remaining_accounts index + amount, + output_tree_index, + // ❌ Wrong: output_queue_pubkey: *cpi_accounts.tree_accounts().unwrap()[0].key, + token_pool_pda: *remaining_accounts[1].key, // ← From remaining_accounts + transfer_config: Some(TransferConfig { + cpi_context: Some(CompressedCpiContext { + set_context: true, + first_set_context: true, + cpi_context_account_index: 0, + }), + cpi_context_pubkey: Some(cpi_context_pubkey), + ..Default::default() + }), + spl_token_program: *remaining_accounts[2].key, // ← SPL_TOKEN_PROGRAM_ID + tree_accounts: cpi_accounts.tree_pubkeys().unwrap(), // ← From CPI accounts +}; +``` + +**Critical Account Ordering for Four Invokes**: +```rust +// Test setup - exact order matters for remaining_accounts indices +remaining_accounts.add_pre_accounts_signer_mut(payer.pubkey()); +// Remaining accounts 0 - compression token account +remaining_accounts.add_pre_accounts_meta(AccountMeta::new(compression_token_account, false)); +// Remaining accounts 1 - token pool PDA +remaining_accounts.add_pre_accounts_meta(AccountMeta::new(token_pool_pda1, false)); +// Remaining accounts 2 - SPL token program +remaining_accounts.add_pre_accounts_meta(AccountMeta::new(SPL_TOKEN_PROGRAM_ID.into(), false)); +// Remaining accounts 3 - compressed token program +remaining_accounts.add_pre_accounts_meta(AccountMeta::new(compressed_token_program, false)); +// Remaining accounts 4 - CPI authority PDA +remaining_accounts.add_pre_accounts_meta(AccountMeta::new(cpi_authority_pda, false)); +``` + +**Validity Proof and Tree Info Management**: +```rust +// Get escrow account directly by address (more efficient) +let escrow_account = rpc.get_compressed_account(escrow_address, None).await?.value; + +// Pack tree infos BEFORE constructing TokenAccountMeta +let packed_tree_info = rpc_result.pack_tree_infos(&mut remaining_accounts); + +// Use correct tree info indices for each compressed account +let mint2_tree_info = packed_tree_info.state_trees.as_ref().unwrap().packed_tree_infos[1]; +let mint3_tree_info = packed_tree_info.state_trees.as_ref().unwrap().packed_tree_infos[2]; +let escrow_tree_info = packed_tree_info.state_trees.as_ref().unwrap().packed_tree_infos[0]; +``` + +**System Accounts Start Offset**: +```rust +// Use the actual offset returned by to_account_metas() +let (accounts, system_accounts_start_offset, _) = remaining_accounts.to_account_metas(); +// Pass this offset to the instruction +system_accounts_start_offset: system_accounts_start_offset as u8, +``` + +## Best Practices + +### CPI Context Decision +- **Use**: Multi-program transactions with compressed accounts (saves proofs) +- **Avoid**: Simple single-program operations (PDA creation, basic transfers) + +### Account Management +- Use `PackedAccounts` and `add_pre_accounts_signer_mut()` +- Choose `Generic<'info>` (1 account) vs `GenericWithAuthority<'info>` (2 accounts) +- Set `cpi_context: None` for simple operations + +### Working Patterns +```rust +// Compress tokens pattern +let mut remaining_accounts = PackedAccounts::default(); +remaining_accounts.add_pre_accounts_signer_mut(payer.pubkey()); +let metas = get_transfer_instruction_account_metas(config); +remaining_accounts.add_pre_accounts_metas(metas.as_slice()); +let output_tree_index = rpc.get_random_state_tree_info().unwrap().pack_output_tree_index(&mut remaining_accounts).unwrap(); + +// Test flow: Setup → Compress → Create PDA → Execute +``` + +## Implementation Status + +### ✅ Working Features +1. **Basic PDA Creation**: `create_escrow_pda` instruction works correctly +2. **Token Compression**: Individual token compression operations work +3. **Four Invokes Instruction**: Complete CPI context implementation working + - Account structure: Uses `Generic<'info>` (single signer) + - CPI context: Proper multi-program proof optimization + - Token accounts: Correct account ordering and tree info management + - Compress CPI: Working with proper `CompressInputs` structure + - Transfer CPI: Custom `transfer_tokens_with_cpi_context` wrapper replaces `transfer_tokens_to_escrow_pda` +4. **Error Handling**: Comprehensive error code documentation and fixes + +### Key Implementation Success +The `four_invokes` instruction successfully demonstrates the complete CPI context pattern for Light Protocol, enabling: +- **Single Proof Optimization**: One validity proof for multiple compressed account operations +- **Cross-Program Integration**: Token program + system program coordination +- **Production Ready**: Complete account setup and tree info management +- **Custom Transfer Wrapper**: Purpose-built transfer function for four invokes instruction \ No newline at end of file diff --git a/program-tests/sdk-token-test/Cargo.toml b/program-tests/sdk-token-test/Cargo.toml new file mode 100644 index 0000000000..21ecedcf6f --- /dev/null +++ b/program-tests/sdk-token-test/Cargo.toml @@ -0,0 +1,49 @@ +[package] +name = "sdk-token-test" +version = "1.0.0" +description = "Test program using compressed token SDK" +repository = "https://github.com/Lightprotocol/light-protocol" +license = "Apache-2.0" +edition = "2021" + +[lib] +crate-type = ["cdylib", "lib"] +name = "sdk_token_test" + +[features] +no-entrypoint = [] +no-idl = [] +no-log-ix-name = [] +cpi = ["no-entrypoint"] +test-sbf = [] +default = [] + +[dependencies] +light-compressed-token-sdk = { workspace = true, features = ["anchor"] } +anchor-lang = { workspace = true } +light-hasher = { workspace = true } +light-sdk = { workspace = true } +light-sdk-types = { workspace = true } +light-compressed-account = { workspace = true } +arrayvec = { workspace = true } +light-batched-merkle-tree = { workspace = true } +light-ctoken-types = { workspace = true, features = ["anchor"] } + +[dev-dependencies] +light-program-test = { workspace = true, features = ["devenv"] } +light-test-utils = { workspace = true } +tokio = { workspace = true } +serial_test = { workspace = true } +solana-sdk = { workspace = true } +anchor-spl = { workspace = true } +light-sdk = { workspace = true } +light-compressed-account = { workspace = true, features = ["anchor"] } +light-client = { workspace = true, features = ["devenv"] } +light-token-client = { workspace = true } + +[lints.rust.unexpected_cfgs] +level = "allow" +check-cfg = [ + 'cfg(target_os, values("solana"))', + 'cfg(feature, values("frozen-abi", "no-entrypoint"))', +] diff --git a/program-tests/sdk-token-test/Xargo.toml b/program-tests/sdk-token-test/Xargo.toml new file mode 100644 index 0000000000..1744f098ae --- /dev/null +++ b/program-tests/sdk-token-test/Xargo.toml @@ -0,0 +1,2 @@ +[target.bpfel-unknown-unknown.dependencies.std] +features = [] \ No newline at end of file diff --git a/program-tests/sdk-token-test/src/lib.rs b/program-tests/sdk-token-test/src/lib.rs new file mode 100644 index 0000000000..aac67681bc --- /dev/null +++ b/program-tests/sdk-token-test/src/lib.rs @@ -0,0 +1,290 @@ +#![allow(unexpected_cfgs)] +#![allow(clippy::too_many_arguments)] + +use anchor_lang::prelude::*; +use light_compressed_token_sdk::{instructions::Recipient, TokenAccountMeta, ValidityProof}; +use light_sdk::instruction::{PackedAddressTreeInfo, ValidityProof as LightValidityProof}; + +mod process_batch_compress_tokens; +mod process_compress_full_and_close; +mod process_compress_tokens; +mod process_create_compressed_account; +mod process_create_escrow_pda; +mod process_decompress_tokens; +mod process_four_invokes; +pub mod process_four_transfer2; +mod process_transfer_tokens; +mod process_update_deposit; + +use light_sdk::{cpi::CpiAccounts, instruction::account_meta::CompressedAccountMeta}; +use process_batch_compress_tokens::process_batch_compress_tokens; +use process_compress_full_and_close::process_compress_full_and_close; +use process_compress_tokens::process_compress_tokens; +use process_create_compressed_account::process_create_compressed_account; +use process_create_escrow_pda::process_create_escrow_pda; +use process_decompress_tokens::process_decompress_tokens; +use process_four_invokes::process_four_invokes; +pub use process_four_invokes::{CompressParams, FourInvokesParams, TransferParams}; +use process_four_transfer2::process_four_transfer2; +use process_transfer_tokens::process_transfer_tokens; + +declare_id!("5p1t1GAaKtK1FKCh5Hd2Gu8JCu3eREhJm4Q2qYfTEPYK"); + +use light_sdk::{cpi::CpiSigner, derive_light_cpi_signer}; + +pub const LIGHT_CPI_SIGNER: CpiSigner = + derive_light_cpi_signer!("5p1t1GAaKtK1FKCh5Hd2Gu8JCu3eREhJm4Q2qYfTEPYK"); + +#[derive(Clone, AnchorSerialize, AnchorDeserialize)] +pub struct TokenParams { + pub deposit_amount: u64, + pub depositing_token_metas: Vec, + pub mint: Pubkey, + pub escrowed_token_meta: TokenAccountMeta, + pub recipient_bump: u8, +} + +#[derive(Clone, AnchorSerialize, AnchorDeserialize)] +pub struct PdaParams { + pub account_meta: CompressedAccountMeta, + pub existing_amount: u64, +} +use crate::{ + process_create_compressed_account::deposit_tokens, process_four_transfer2::FourTransfer2Params, + process_update_deposit::process_update_deposit, +}; +#[program] +pub mod sdk_token_test { + use light_sdk::address::v1::derive_address; + use light_sdk_types::CpiAccountsConfig; + + use super::*; + + pub fn compress_tokens<'info>( + ctx: Context<'_, '_, '_, 'info, Generic<'info>>, + output_tree_index: u8, + recipient: Pubkey, + mint: Pubkey, + amount: u64, + ) -> Result<()> { + process_compress_tokens(ctx, output_tree_index, recipient, mint, amount) + } + + pub fn compress_full_and_close<'info>( + ctx: Context<'_, '_, '_, 'info, Generic<'info>>, + output_tree_index: u8, + recipient_index: u8, + mint_index: u8, + source_index: u8, + authority_index: u8, + close_recipient_index: u8, + system_accounts_offset: u8, + ) -> Result<()> { + process_compress_full_and_close( + ctx, + output_tree_index, + recipient_index, + mint_index, + source_index, + authority_index, + close_recipient_index, + system_accounts_offset, + ) + } + + pub fn transfer_tokens<'info>( + ctx: Context<'_, '_, '_, 'info, Generic<'info>>, + validity_proof: ValidityProof, + token_metas: Vec, + output_tree_index: u8, + mint: Pubkey, + recipient: Pubkey, + ) -> Result<()> { + process_transfer_tokens( + ctx, + validity_proof, + token_metas, + output_tree_index, + mint, + recipient, + ) + } + + pub fn decompress_tokens<'info>( + ctx: Context<'_, '_, '_, 'info, Generic<'info>>, + validity_proof: ValidityProof, + token_data: Vec, + output_tree_index: u8, + mint: Pubkey, + ) -> Result<()> { + process_decompress_tokens(ctx, validity_proof, token_data, output_tree_index, mint) + } + + pub fn batch_compress_tokens<'info>( + ctx: Context<'_, '_, '_, 'info, Generic<'info>>, + recipients: Vec, + token_pool_index: u8, + token_pool_bump: u8, + ) -> Result<()> { + process_batch_compress_tokens(ctx, recipients, token_pool_index, token_pool_bump) + } + + pub fn deposit<'info>( + ctx: Context<'_, '_, '_, 'info, Generic<'info>>, + proof: LightValidityProof, + address_tree_info: PackedAddressTreeInfo, + output_tree_index: u8, + deposit_amount: u64, + token_metas: Vec, + mint: Pubkey, + system_accounts_start_offset: u8, + recipient_bump: u8, + ) -> Result<()> { + // It makes sense to parse accounts once. + let config = CpiAccountsConfig { + cpi_signer: crate::LIGHT_CPI_SIGNER, + // TODO: add sanity check that account is a cpi context account. + cpi_context: true, + // TODO: add sanity check that account is a sol_pool_pda account. + sol_pool_pda: false, + sol_compression_recipient: false, + }; + let (_, system_account_infos) = ctx + .remaining_accounts + .split_at(system_accounts_start_offset as usize); + // Could add with pre account infos Option + let light_cpi_accounts = CpiAccounts::new_with_config( + ctx.accounts.signer.as_ref(), + system_account_infos, + config, + ); + let (address, address_seed) = derive_address( + &[ + b"escrow", + light_cpi_accounts.fee_payer().key.to_bytes().as_ref(), + ], + &address_tree_info + .get_tree_pubkey(&light_cpi_accounts) + .map_err(|_| ErrorCode::AccountNotEnoughKeys)?, + &crate::ID, + ); + msg!("seeds: {:?}", b"escrow"); + msg!("seeds: {:?}", address); + msg!("recipient_bump: {:?}", recipient_bump); + let recipient = Pubkey::create_program_address( + &[b"escrow", &address, &[recipient_bump]], + ctx.program_id, + ) + .unwrap(); + deposit_tokens( + &light_cpi_accounts, + token_metas, + output_tree_index, + mint, + recipient, + deposit_amount, + ctx.remaining_accounts, + )?; + let new_address_params = address_tree_info.into_new_address_params_packed(address_seed); + + process_create_compressed_account( + light_cpi_accounts, + proof, + output_tree_index, + deposit_amount, + address, + new_address_params, + ) + } + + pub fn update_deposit<'info>( + ctx: Context<'_, '_, '_, 'info, GenericWithAuthority<'info>>, + proof: LightValidityProof, + output_tree_index: u8, + output_tree_queue_index: u8, + system_accounts_start_offset: u8, + token_params: TokenParams, + pda_params: PdaParams, + ) -> Result<()> { + process_update_deposit( + ctx, + output_tree_index, + output_tree_queue_index, + proof, + system_accounts_start_offset, + token_params, + pda_params, + ) + } + + pub fn four_invokes<'info>( + ctx: Context<'_, '_, '_, 'info, Generic<'info>>, + output_tree_index: u8, + proof: LightValidityProof, + system_accounts_start_offset: u8, + four_invokes_params: FourInvokesParams, + pda_params: PdaParams, + ) -> Result<()> { + process_four_invokes( + ctx, + output_tree_index, + proof, + system_accounts_start_offset, + four_invokes_params, + pda_params, + ) + } + + pub fn four_transfer2<'info>( + ctx: Context<'_, '_, '_, 'info, Generic<'info>>, + output_tree_index: u8, + proof: LightValidityProof, + system_accounts_start_offset: u8, + packed_accounts_start_offset: u8, + four_transfer2_params: FourTransfer2Params, + pda_params: PdaParams, + ) -> Result<()> { + process_four_transfer2( + ctx, + output_tree_index, + proof, + system_accounts_start_offset, + packed_accounts_start_offset, + four_transfer2_params, + pda_params, + ) + } + + pub fn create_escrow_pda<'info>( + ctx: Context<'_, '_, '_, 'info, Generic<'info>>, + proof: LightValidityProof, + output_tree_index: u8, + amount: u64, + address: [u8; 32], + new_address_params: light_sdk::address::PackedNewAddressParams, + ) -> Result<()> { + process_create_escrow_pda( + ctx, + proof, + output_tree_index, + amount, + address, + new_address_params, + ) + } +} + +#[derive(Accounts)] +pub struct Generic<'info> { + // fee payer and authority are the same + #[account(mut)] + pub signer: Signer<'info>, +} + +#[derive(Accounts)] +pub struct GenericWithAuthority<'info> { + // fee payer and authority are the same + #[account(mut)] + pub signer: Signer<'info>, + pub authority: AccountInfo<'info>, +} diff --git a/program-tests/sdk-token-test/src/process_batch_compress_tokens.rs b/program-tests/sdk-token-test/src/process_batch_compress_tokens.rs new file mode 100644 index 0000000000..58100ec998 --- /dev/null +++ b/program-tests/sdk-token-test/src/process_batch_compress_tokens.rs @@ -0,0 +1,57 @@ +use anchor_lang::{prelude::*, solana_program::program::invoke}; +use light_compressed_token_sdk::{ + account_infos::BatchCompressAccountInfos, + instructions::{ + batch_compress::{create_batch_compress_instruction, BatchCompressInputs}, + Recipient, + }, +}; + +use crate::Generic; + +pub fn process_batch_compress_tokens<'info>( + ctx: Context<'_, '_, '_, 'info, Generic<'info>>, + recipients: Vec, + token_pool_index: u8, + token_pool_bump: u8, +) -> Result<()> { + let light_cpi_accounts = BatchCompressAccountInfos::new( + ctx.accounts.signer.as_ref(), + ctx.accounts.signer.as_ref(), + ctx.remaining_accounts, + ); + + let sdk_recipients: Vec = + recipients + .into_iter() + .map( + |r| light_compressed_token_sdk::instructions::batch_compress::Recipient { + pubkey: r.pubkey, + amount: r.amount, + }, + ) + .collect(); + + let batch_compress_inputs = BatchCompressInputs { + fee_payer: *ctx.accounts.signer.key, + authority: *ctx.accounts.signer.key, + token_pool_pda: *light_cpi_accounts.token_pool_pda().unwrap().key, + sender_token_account: *light_cpi_accounts.sender_token_account().unwrap().key, + token_program: *light_cpi_accounts.token_program().unwrap().key, + merkle_tree: *light_cpi_accounts.merkle_tree().unwrap().key, + recipients: sdk_recipients, + lamports: None, + token_pool_index, + token_pool_bump, + sol_pool_pda: None, + }; + + let instruction = + create_batch_compress_instruction(batch_compress_inputs).map_err(ProgramError::from)?; + msg!("batch compress instruction {:?}", instruction); + let account_infos = light_cpi_accounts.to_account_infos(); + + invoke(&instruction, account_infos.as_slice())?; + + Ok(()) +} diff --git a/program-tests/sdk-token-test/src/process_compress_full_and_close.rs b/program-tests/sdk-token-test/src/process_compress_full_and_close.rs new file mode 100644 index 0000000000..d4a3440bcc --- /dev/null +++ b/program-tests/sdk-token-test/src/process_compress_full_and_close.rs @@ -0,0 +1,121 @@ +use anchor_lang::{prelude::*, solana_program::program::invoke}; +use light_compressed_token_sdk::{ + account2::CTokenAccount2, + instructions::{ + close::close_account, + transfer2::{ + account_metas::Transfer2AccountsMetaConfig, create_transfer2_instruction, + Transfer2Inputs, + }, + }, +}; +use light_sdk::cpi::CpiAccounts; +use light_sdk_types::CpiAccountsConfig; + +use crate::Generic; + +pub fn process_compress_full_and_close<'info>( + ctx: Context<'_, '_, '_, 'info, Generic<'info>>, + // All offsets are static and could be hardcoded + output_tree_index: u8, + recipient_index: u8, + mint_index: u8, + source_index: u8, + authority_index: u8, + close_recipient_index: u8, + system_accounts_offset: u8, +) -> Result<()> { + // Parse CPI accounts (following four_transfer2 pattern) + let config = CpiAccountsConfig::new(crate::LIGHT_CPI_SIGNER); + // _token_account_infos should be in the anchor account struct. + let (_token_account_infos, system_account_infos) = ctx + .remaining_accounts + .split_at(system_accounts_offset as usize); + + let cpi_accounts = + CpiAccounts::new_with_config(ctx.accounts.signer.as_ref(), system_account_infos, config); + let token_account_info = cpi_accounts + .get_tree_account_info(source_index as usize) + .unwrap(); + // should be in the anchor account struct + let close_recipient_info = cpi_accounts + .get_tree_account_info(close_recipient_index as usize) + .unwrap(); + // Create CTokenAccount2 for compression (following four_transfer2 pattern) + let mut token_account_compress = + CTokenAccount2::new_empty(recipient_index, mint_index, output_tree_index); + + // Use compress_full method + token_account_compress + .compress_full( + source_index, // source account index + authority_index, // authority index + token_account_info, + ) + .map_err(ProgramError::from)?; + + msg!( + "Compressing {} tokens", + token_account_compress.compression_amount().unwrap_or(0) + ); + + // Create packed accounts for transfer2 instruction (following four_transfer2 pattern) + let tree_accounts = cpi_accounts.tree_accounts().unwrap(); + let packed_accounts = account_infos_to_metas(tree_accounts); + + // create_transfer2_instruction::compress + // create_transfer2_instruction::compress_full + // create_transfer2_instruction::decompress + // create_transfer2_instruction::transfer, all should hide indices completely + // + // Advanced: + // 1. advanced multi transfer + // 2. compress full and close + // 3. + let inputs = Transfer2Inputs { + meta_config: Transfer2AccountsMetaConfig::new(*ctx.accounts.signer.key, packed_accounts), + token_accounts: vec![token_account_compress], + ..Default::default() + }; + + let instruction = create_transfer2_instruction(inputs).map_err(ProgramError::from)?; + + // Execute the transfer2 instruction with all accounts + let account_infos = [ + &[cpi_accounts.fee_payer().clone()][..], + ctx.remaining_accounts, + ] + .concat(); + invoke(&instruction, account_infos.as_slice())?; + + let compressed_token_program_id = + Pubkey::new_from_array(light_ctoken_types::COMPRESSED_TOKEN_PROGRAM_ID); + let close_instruction = close_account( + &compressed_token_program_id, + token_account_info.key, + close_recipient_info.key, + ctx.accounts.signer.key, + ); + + invoke( + &close_instruction, + &[ + token_account_info.clone(), + close_recipient_info.clone(), + ctx.accounts.signer.to_account_info(), + ], + )?; + Ok(()) +} + +pub fn account_infos_to_metas(account_infos: &[AccountInfo]) -> Vec { + let mut packed_accounts = Vec::with_capacity(account_infos.len()); + for account_info in account_infos { + packed_accounts.push(AccountMeta { + pubkey: *account_info.key, + is_signer: account_info.is_signer, + is_writable: account_info.is_writable, + }); + } + packed_accounts +} diff --git a/program-tests/sdk-token-test/src/process_compress_tokens.rs b/program-tests/sdk-token-test/src/process_compress_tokens.rs new file mode 100644 index 0000000000..d3e82cfefa --- /dev/null +++ b/program-tests/sdk-token-test/src/process_compress_tokens.rs @@ -0,0 +1,43 @@ +use anchor_lang::{prelude::*, solana_program::program::invoke}; +use light_compressed_token_sdk::instructions::transfer::{ + instruction::{compress, CompressInputs}, + TransferAccountInfos, +}; + +use crate::Generic; + +pub fn process_compress_tokens<'info>( + ctx: Context<'_, '_, '_, 'info, Generic<'info>>, + output_tree_index: u8, + recipient: Pubkey, + mint: Pubkey, + amount: u64, +) -> Result<()> { + let light_cpi_accounts = TransferAccountInfos::new_compress( + ctx.accounts.signer.as_ref(), + ctx.accounts.signer.as_ref(), + ctx.remaining_accounts, + ); + + let compress_inputs = CompressInputs { + fee_payer: *ctx.accounts.signer.key, + authority: *ctx.accounts.signer.key, + mint, + recipient, + sender_token_account: *light_cpi_accounts.sender_token_account().unwrap().key, + amount, + output_tree_index, + token_pool_pda: *light_cpi_accounts.token_pool_pda().unwrap().key, + transfer_config: None, + spl_token_program: *light_cpi_accounts.spl_token_program().unwrap().key, + tree_accounts: light_cpi_accounts.tree_pubkeys().unwrap(), + }; + + let instruction = compress(compress_inputs).map_err(ProgramError::from)?; + msg!("instruction {:?}", instruction); + let account_infos = light_cpi_accounts.to_account_infos(); + + invoke(&instruction, account_infos.as_slice())?; + + Ok(()) +} diff --git a/program-tests/sdk-token-test/src/process_create_compressed_account.rs b/program-tests/sdk-token-test/src/process_create_compressed_account.rs new file mode 100644 index 0000000000..315704ef2b --- /dev/null +++ b/program-tests/sdk-token-test/src/process_create_compressed_account.rs @@ -0,0 +1,148 @@ +use anchor_lang::{prelude::*, solana_program::log::sol_log_compute_units}; +use light_compressed_account::instruction_data::cpi_context::CompressedCpiContext; +use light_compressed_token_sdk::{ + account::CTokenAccount, + instructions::transfer::instruction::{TransferConfig, TransferInputs}, + TokenAccountMeta, +}; +use light_sdk::{ + account::LightAccount, + cpi::{CpiAccounts, CpiInputs}, + instruction::ValidityProof, + light_account_checks::AccountInfoTrait, + LightDiscriminator, LightHasher, +}; + +#[event] +#[derive(Clone, Debug, Default, LightHasher, LightDiscriminator)] +pub struct CompressedEscrowPda { + pub amount: u64, + #[hash] + pub owner: Pubkey, +} + +pub fn process_create_compressed_account( + cpi_accounts: CpiAccounts, + proof: ValidityProof, + output_tree_index: u8, + amount: u64, + address: [u8; 32], + new_address_params: light_sdk::address::PackedNewAddressParams, +) -> Result<()> { + let mut my_compressed_account = LightAccount::<'_, CompressedEscrowPda>::new_init( + &crate::ID, + Some(address), + output_tree_index, + ); + + my_compressed_account.amount = amount; + my_compressed_account.owner = *cpi_accounts.fee_payer().key; + + let cpi_inputs = CpiInputs { + proof, + account_infos: Some(vec![my_compressed_account + .to_account_info() + .map_err(ProgramError::from)?]), + new_addresses: Some(vec![new_address_params]), + cpi_context: Some(CompressedCpiContext { + set_context: false, + first_set_context: false, + cpi_context_account_index: 0, // seems to be useless. Seems to be unused. + // TODO: unify the account meta generation on and offchain. + }), + ..Default::default() + }; + msg!("invoke"); + sol_log_compute_units(); + cpi_inputs + .invoke_light_system_program(cpi_accounts) + .map_err(ProgramError::from)?; + sol_log_compute_units(); + + Ok(()) +} + +pub fn deposit_tokens<'info>( + cpi_accounts: &CpiAccounts<'_, 'info>, + token_metas: Vec, + output_tree_index: u8, + mint: Pubkey, + recipient: Pubkey, + amount: u64, + remaining_accounts: &[AccountInfo<'info>], +) -> Result<()> { + let sender_account = CTokenAccount::new( + mint, + *cpi_accounts.fee_payer().key, + token_metas, + output_tree_index, + ); + + // We need to be careful what accounts we pass. + // Big accounts cost many CU. + // TODO: replace + let tree_account_infos = cpi_accounts.tree_accounts().unwrap(); + let tree_account_len = tree_account_infos.len(); + // skip cpi context account and omit the address tree and queue accounts. + let tree_account_infos = &tree_account_infos[1..tree_account_len - 2]; + let tree_pubkeys = tree_account_infos + .iter() + .map(|x| x.pubkey()) + .collect::>(); + let cpi_context_pubkey = *cpi_accounts.cpi_context().unwrap().key; + // msg!("cpi_context_pubkey {:?}", cpi_context_pubkey); + let transfer_inputs = TransferInputs { + fee_payer: *cpi_accounts.fee_payer().key, + sender_account, + // No validity proof necessary we are just storing state in the cpi context. + validity_proof: None.into(), + recipient, + tree_pubkeys, + config: Some(TransferConfig { + cpi_context: Some(CompressedCpiContext { + set_context: true, + first_set_context: true, + cpi_context_account_index: 0, // TODO: replace with Pubkey (maybe not because it is in tree pubkeys 1 in this case) + }), + cpi_context_pubkey: Some(cpi_context_pubkey), + ..Default::default() + }), + amount, + }; + let instruction = + light_compressed_token_sdk::instructions::transfer::instruction::transfer(transfer_inputs) + .unwrap(); + // msg!("instruction {:?}", instruction); + // We can use the property that account infos don't have to be in order if you use + // solana program invoke. + sol_log_compute_units(); + + msg!("create_account_infos"); + sol_log_compute_units(); + // TODO: initialize from CpiAccounts, use with_compressed_pda() offchain. + // let account_infos: TransferAccountInfos<'_, 'info, MAX_ACCOUNT_INFOS> = TransferAccountInfos { + // fee_payer: cpi_accounts.fee_payer(), + // authority: cpi_accounts.fee_payer(), + // packed_accounts: tree_account_infos.as_slice(), + // ctoken_accounts: token_account_infos, + // cpi_context: Some(cpi_context), + // }; + // let account_infos = account_infos.into_account_infos(); + // We can remove the address Merkle tree accounts. + let len = remaining_accounts.len() - 2; + // into_account_infos_checked() can be used for debugging but doubles CU cost to 1.5k CU + let account_infos = [ + &[cpi_accounts.fee_payer().clone()][..], + &remaining_accounts[..len], + ] + .concat(); + sol_log_compute_units(); + + sol_log_compute_units(); + msg!("invoke"); + sol_log_compute_units(); + anchor_lang::solana_program::program::invoke(&instruction, account_infos.as_slice())?; + sol_log_compute_units(); + + Ok(()) +} diff --git a/program-tests/sdk-token-test/src/process_create_escrow_pda.rs b/program-tests/sdk-token-test/src/process_create_escrow_pda.rs new file mode 100644 index 0000000000..9bad2d8978 --- /dev/null +++ b/program-tests/sdk-token-test/src/process_create_escrow_pda.rs @@ -0,0 +1,49 @@ +use anchor_lang::prelude::*; +use light_sdk::{ + account::LightAccount, + cpi::{CpiAccounts, CpiInputs}, + instruction::ValidityProof as LightValidityProof, +}; + +use crate::process_update_deposit::CompressedEscrowPda; + +pub fn process_create_escrow_pda<'info>( + ctx: Context<'_, '_, '_, 'info, crate::Generic<'info>>, + proof: LightValidityProof, + output_tree_index: u8, + amount: u64, + address: [u8; 32], + new_address_params: light_sdk::address::PackedNewAddressParams, +) -> Result<()> { + let cpi_accounts = CpiAccounts::new( + ctx.accounts.signer.as_ref(), + ctx.remaining_accounts, + crate::LIGHT_CPI_SIGNER, + ); + + let mut my_compressed_account = LightAccount::<'_, CompressedEscrowPda>::new_init( + &crate::ID, + Some(address), + output_tree_index, + ); + + my_compressed_account.amount = amount; + my_compressed_account.owner = *cpi_accounts.fee_payer().key; + + let cpi_inputs = CpiInputs { + proof, + account_infos: Some(vec![my_compressed_account + .to_account_info() + .map_err(ProgramError::from)?]), + new_addresses: Some(vec![new_address_params]), + cpi_context: None, + ..Default::default() + }; + msg!("invoke"); + + cpi_inputs + .invoke_light_system_program(cpi_accounts) + .map_err(ProgramError::from)?; + + Ok(()) +} diff --git a/program-tests/sdk-token-test/src/process_decompress_tokens.rs b/program-tests/sdk-token-test/src/process_decompress_tokens.rs new file mode 100644 index 0000000000..24aa94a0b8 --- /dev/null +++ b/program-tests/sdk-token-test/src/process_decompress_tokens.rs @@ -0,0 +1,50 @@ +use anchor_lang::{prelude::*, solana_program::program::invoke}; +use light_compressed_token_sdk::{ + instructions::transfer::{ + instruction::{decompress, DecompressInputs}, + TransferAccountInfos, + }, + TokenAccountMeta, ValidityProof, +}; + +use crate::Generic; + +pub fn process_decompress_tokens<'info>( + ctx: Context<'_, '_, '_, 'info, Generic<'info>>, + validity_proof: ValidityProof, + token_data: Vec, + output_tree_index: u8, + mint: Pubkey, +) -> Result<()> { + let sender_account = light_compressed_token_sdk::account::CTokenAccount::new( + mint, + ctx.accounts.signer.key(), + token_data, + output_tree_index, + ); + + let light_cpi_accounts = TransferAccountInfos::new_decompress( + ctx.accounts.signer.as_ref(), + ctx.accounts.signer.as_ref(), + ctx.remaining_accounts, + ); + + let inputs = DecompressInputs { + fee_payer: *ctx.accounts.signer.key, + validity_proof, + sender_account, + amount: 10, + tree_pubkeys: light_cpi_accounts.tree_pubkeys().unwrap(), + token_pool_pda: *light_cpi_accounts.token_pool_pda().unwrap().key, + recipient_token_account: *light_cpi_accounts.decompression_recipient().unwrap().key, + spl_token_program: *light_cpi_accounts.spl_token_program().unwrap().key, + config: None, + }; + + let instruction = decompress(inputs).unwrap(); + let account_infos = light_cpi_accounts.to_account_infos(); + + invoke(&instruction, account_infos.as_slice())?; + + Ok(()) +} diff --git a/program-tests/sdk-token-test/src/process_four_invokes.rs b/program-tests/sdk-token-test/src/process_four_invokes.rs new file mode 100644 index 0000000000..9caf319c4c --- /dev/null +++ b/program-tests/sdk-token-test/src/process_four_invokes.rs @@ -0,0 +1,196 @@ +use anchor_lang::{prelude::*, solana_program::program::invoke}; +use light_compressed_account::instruction_data::cpi_context::CompressedCpiContext; +use light_compressed_token_sdk::{ + account::CTokenAccount, + instructions::transfer::instruction::{ + compress, transfer, CompressInputs, TransferConfig, TransferInputs, + }, + TokenAccountMeta, +}; +use light_sdk::{ + cpi::CpiAccounts, instruction::ValidityProof as LightValidityProof, + light_account_checks::AccountInfoTrait, +}; +use light_sdk_types::CpiAccountsConfig; + +use crate::{process_update_deposit::process_update_escrow_pda, PdaParams}; + +#[derive(Clone, AnchorSerialize, AnchorDeserialize)] +pub struct TransferParams { + pub mint: Pubkey, + pub transfer_amount: u64, + pub token_metas: Vec, + pub recipient: Pubkey, + pub recipient_bump: u8, +} + +#[derive(Clone, AnchorSerialize, AnchorDeserialize)] +pub struct CompressParams { + pub mint: Pubkey, + pub amount: u64, + pub recipient: Pubkey, + pub recipient_bump: u8, + pub token_account: Pubkey, +} + +#[derive(Clone, AnchorSerialize, AnchorDeserialize)] +pub struct FourInvokesParams { + pub compress_1: CompressParams, + pub transfer_2: TransferParams, + pub transfer_3: TransferParams, +} + +pub fn process_four_invokes<'info>( + ctx: Context<'_, '_, '_, 'info, crate::Generic<'info>>, + output_tree_index: u8, + proof: LightValidityProof, + system_accounts_start_offset: u8, + four_invokes_params: FourInvokesParams, + pda_params: PdaParams, +) -> Result<()> { + // Parse CPI accounts once for the final system program invocation + let config = CpiAccountsConfig { + cpi_signer: crate::LIGHT_CPI_SIGNER, + cpi_context: true, + sol_pool_pda: false, + sol_compression_recipient: false, + }; + let (_token_account_infos, system_account_infos) = ctx + .remaining_accounts + .split_at(system_accounts_start_offset as usize); + + let cpi_accounts = + CpiAccounts::new_with_config(ctx.accounts.signer.as_ref(), system_account_infos, config); + + // Invocation 1: Compress mint 1 (writes to CPI context) + compress_tokens_with_cpi_context( + &cpi_accounts, + ctx.remaining_accounts, + four_invokes_params.compress_1.mint, + four_invokes_params.compress_1.recipient, + four_invokes_params.compress_1.amount, + output_tree_index, + )?; + + // Invocation 2: Transfer mint 2 (writes to CPI context) + transfer_tokens_with_cpi_context( + &cpi_accounts, + ctx.remaining_accounts, + four_invokes_params.transfer_2.mint, + four_invokes_params.transfer_2.transfer_amount, + four_invokes_params.transfer_2.recipient, + output_tree_index, + four_invokes_params.transfer_2.token_metas, + )?; + + // Invocation 3: Transfer mint 3 (writes to CPI context) + transfer_tokens_with_cpi_context( + &cpi_accounts, + ctx.remaining_accounts, + four_invokes_params.transfer_3.mint, + four_invokes_params.transfer_3.transfer_amount, + four_invokes_params.transfer_3.recipient, + output_tree_index, + four_invokes_params.transfer_3.token_metas, + )?; + + // Invocation 4: Execute CPI context with system program + process_update_escrow_pda(cpi_accounts, pda_params, proof, 0, false)?; + + Ok(()) +} + +fn transfer_tokens_with_cpi_context<'info>( + cpi_accounts: &CpiAccounts<'_, 'info>, + remaining_accounts: &[AccountInfo<'info>], + mint: Pubkey, + amount: u64, + recipient: Pubkey, + output_tree_index: u8, + token_metas: Vec, +) -> Result<()> { + let cpi_context_pubkey = *cpi_accounts.cpi_context().unwrap().key; + + // Create sender account from token metas using CTokenAccount::new + let sender_account = CTokenAccount::new( + mint, + *cpi_accounts.fee_payer().key, + token_metas, + output_tree_index, + ); + + // Get tree pubkeys excluding the CPI context account (first account) + // We already pass the cpi context pubkey separately. + let tree_account_infos = cpi_accounts.tree_accounts().unwrap(); + let tree_account_infos = &tree_account_infos[1..]; + let tree_pubkeys = tree_account_infos + .iter() + .map(|x| x.pubkey()) + .collect::>(); + + let transfer_inputs = TransferInputs { + fee_payer: *cpi_accounts.fee_payer().key, + validity_proof: None.into(), + sender_account, + amount, + recipient, + tree_pubkeys, + config: Some(TransferConfig { + cpi_context: Some(CompressedCpiContext { + set_context: true, + first_set_context: false, + cpi_context_account_index: 0, + }), + cpi_context_pubkey: Some(cpi_context_pubkey), + ..Default::default() + }), + }; + + let instruction = transfer(transfer_inputs).map_err(ProgramError::from)?; + + let account_infos = [&[cpi_accounts.fee_payer().clone()][..], remaining_accounts].concat(); + invoke(&instruction, account_infos.as_slice())?; + + Ok(()) +} + +fn compress_tokens_with_cpi_context<'info>( + cpi_accounts: &CpiAccounts<'_, 'info>, + remaining_accounts: &[AccountInfo<'info>], + mint: Pubkey, + recipient: Pubkey, + amount: u64, + output_tree_index: u8, +) -> Result<()> { + let cpi_context_pubkey = *cpi_accounts.cpi_context().unwrap().key; + let compress_inputs = CompressInputs { + fee_payer: *cpi_accounts.fee_payer().key, + authority: *cpi_accounts.fee_payer().key, + mint, + recipient, + sender_token_account: *remaining_accounts[0].key, + amount, + output_tree_index, + // output_queue_pubkey: *cpi_accounts.tree_accounts().unwrap()[0].key, + token_pool_pda: *remaining_accounts[1].key, + transfer_config: Some(TransferConfig { + cpi_context: Some(CompressedCpiContext { + set_context: true, + first_set_context: true, + cpi_context_account_index: 0, + }), + cpi_context_pubkey: Some(cpi_context_pubkey), + ..Default::default() + }), + spl_token_program: *remaining_accounts[2].key, + tree_accounts: cpi_accounts.tree_pubkeys().unwrap(), + }; + + let instruction = compress(compress_inputs).map_err(ProgramError::from)?; + + // order doesn't matter in account infos with solana program only with pinocchio it matters. + let account_infos = [&[cpi_accounts.fee_payer().clone()][..], remaining_accounts].concat(); + invoke(&instruction, account_infos.as_slice())?; + + Ok(()) +} diff --git a/program-tests/sdk-token-test/src/process_four_transfer2.rs b/program-tests/sdk-token-test/src/process_four_transfer2.rs new file mode 100644 index 0000000000..21ddb0814c --- /dev/null +++ b/program-tests/sdk-token-test/src/process_four_transfer2.rs @@ -0,0 +1,252 @@ +use anchor_lang::{prelude::*, solana_program::program::invoke}; +use light_compressed_account::instruction_data::cpi_context::CompressedCpiContext; +use light_compressed_token_sdk::{ + account2::CTokenAccount2, + instructions::transfer2::{ + account_metas::Transfer2AccountsMetaConfig, create_transfer2_instruction, Transfer2Config, + Transfer2Inputs, + }, +}; +use light_ctoken_types::instructions::transfer2::MultiInputTokenDataWithContext; +use light_sdk::{cpi::CpiAccounts, instruction::ValidityProof as LightValidityProof}; +use light_sdk_types::CpiAccountsConfig; + +use crate::{process_update_deposit::process_update_escrow_pda, PdaParams}; + +#[derive(Clone, AnchorSerialize, AnchorDeserialize)] +pub struct TransferParams { + pub transfer_amount: u64, + pub token_metas: Vec, + pub recipient: u8, +} + +#[derive(Clone, AnchorSerialize, AnchorDeserialize)] +pub struct CompressParams { + pub mint: u8, + pub amount: u64, + pub recipient: u8, + pub solana_token_account: u8, + pub authority: u8, +} + +#[derive(Clone, AnchorSerialize, AnchorDeserialize)] +pub struct FourTransfer2Params { + pub compress_1: CompressParams, + pub transfer_2: TransferParams, + pub transfer_3: TransferParams, +} + +pub fn process_four_transfer2<'info>( + ctx: Context<'_, '_, '_, 'info, crate::Generic<'info>>, + output_tree_index: u8, + proof: LightValidityProof, + system_accounts_start_offset: u8, + packed_accounts_start_offset: u8, + four_invokes_params: FourTransfer2Params, + pda_params: PdaParams, +) -> Result<()> { + { + // Debug prints for CPI struct values + msg!("=== PROGRAM DEBUG - CPI STRUCT VALUES ==="); + msg!("output_tree_index: {}", output_tree_index); + msg!( + "system_accounts_start_offset: {}", + system_accounts_start_offset + ); + msg!( + "packed_accounts_start_offset: {}", + packed_accounts_start_offset + ); + msg!("signer: {}", ctx.accounts.signer.key()); + + msg!("compress_1.mint: {}", four_invokes_params.compress_1.mint); + msg!( + "compress_1.amount: {}", + four_invokes_params.compress_1.amount + ); + msg!( + "compress_1.recipient: {}", + four_invokes_params.compress_1.recipient + ); + msg!( + "compress_1.solana_token_account: {}", + four_invokes_params.compress_1.solana_token_account + ); + + msg!( + "transfer_2.transfer_amount: {}", + four_invokes_params.transfer_2.transfer_amount + ); + msg!( + "transfer_2.recipient: {}", + four_invokes_params.transfer_2.recipient + ); + msg!( + "transfer_2.token_metas len: {}", + four_invokes_params.transfer_2.token_metas.len() + ); + for (i, meta) in four_invokes_params + .transfer_2 + .token_metas + .iter() + .enumerate() + { + msg!(" transfer_2.token_metas[{}].amount: {}", i, meta.amount); + msg!( + " transfer_2.token_metas[{}].merkle_context.merkle_tree_pubkey_index: {}", + i, + meta.merkle_context.merkle_tree_pubkey_index + ); + msg!(" transfer_2.token_metas[{}].mint: {}", i, meta.mint); + msg!(" transfer_2.token_metas[{}].owner: {}", i, meta.owner); + } + + msg!( + "transfer_3.transfer_amount: {}", + four_invokes_params.transfer_3.transfer_amount + ); + msg!( + "transfer_3.recipient: {}", + four_invokes_params.transfer_3.recipient + ); + msg!( + "transfer_3.token_metas len: {}", + four_invokes_params.transfer_3.token_metas.len() + ); + for (i, meta) in four_invokes_params + .transfer_3 + .token_metas + .iter() + .enumerate() + { + msg!(" transfer_3.token_metas[{}].amount: {}", i, meta.amount); + msg!( + " transfer_3.token_metas[{}].merkle_context.merkle_tree_pubkey_index: {}", + i, + meta.merkle_context.merkle_tree_pubkey_index + ); + msg!(" transfer_3.token_metas[{}].mint: {}", i, meta.mint); + msg!(" transfer_3.token_metas[{}].owner: {}", i, meta.owner); + } + + msg!("pda_params.account_meta: {:?}", pda_params.account_meta); + msg!("pda_params.existing_amount: {}", pda_params.existing_amount); + + // Debug remaining accounts + msg!("=== REMAINING ACCOUNTS ==="); + for (i, account) in ctx.remaining_accounts.iter().enumerate() { + msg!(" {}: {}", i, anchor_lang::Key::key(account)); + } + } + // Parse CPI accounts once for the final system program invocation + let config = CpiAccountsConfig { + cpi_signer: crate::LIGHT_CPI_SIGNER, + cpi_context: true, + sol_pool_pda: false, + sol_compression_recipient: false, + }; + let (_token_account_infos, system_account_infos) = ctx + .remaining_accounts + .split_at(system_accounts_start_offset as usize); + + let cpi_accounts = + CpiAccounts::new_with_config(ctx.accounts.signer.as_ref(), system_account_infos, config); + + // Invocation 4: Execute CPI context with system program + process_update_escrow_pda(cpi_accounts.clone(), pda_params, proof, 0, true)?; + + { + let mut token_account_compress = CTokenAccount2::new_empty( + four_invokes_params.compress_1.recipient, + four_invokes_params.compress_1.mint, + output_tree_index, + ); + token_account_compress + .compress( + four_invokes_params.compress_1.amount, + four_invokes_params.compress_1.solana_token_account, + four_invokes_params.compress_1.authority, + ) + .map_err(ProgramError::from)?; + + let mut token_account_transfer_2 = CTokenAccount2::new( + four_invokes_params.transfer_2.token_metas, + output_tree_index, + ) + .map_err(ProgramError::from)?; + let transfer_recipient2 = token_account_transfer_2 + .transfer( + four_invokes_params.transfer_2.recipient, + four_invokes_params.transfer_2.transfer_amount, + None, + ) + .map_err(ProgramError::from)?; + + let mut token_account_transfer_3 = CTokenAccount2::new( + four_invokes_params.transfer_3.token_metas, + output_tree_index, + ) + .map_err(ProgramError::from)?; + let transfer_recipient3 = token_account_transfer_3 + .transfer( + four_invokes_params.transfer_3.recipient, + four_invokes_params.transfer_3.transfer_amount, + None, + ) + .map_err(ProgramError::from)?; + + msg!("tree_pubkeys {:?}", cpi_accounts.tree_pubkeys()); + let tree_accounts = cpi_accounts.tree_accounts().unwrap(); + let mut packed_accounts = Vec::with_capacity(tree_accounts.len()); + for account_info in tree_accounts { + packed_accounts.push(account_meta_from_account_info(account_info)); + } + msg!("packed_accounts {:?}", packed_accounts); + + let inputs = Transfer2Inputs { + validity_proof: proof, + transfer_config: Transfer2Config { + cpi_context: Some(CompressedCpiContext { + set_context: false, + first_set_context: false, + cpi_context_account_index: 0, + }), + ..Default::default() + }, + meta_config: Transfer2AccountsMetaConfig { + fee_payer: Some(*ctx.accounts.signer.key), + packed_accounts: Some(packed_accounts), // TODO: test that if we were to set the cpi context we don't have to pass packed accounts. (only works with transfers) + cpi_context: Some(*cpi_accounts.cpi_context().unwrap().key), + ..Default::default() + }, + in_lamports: None, + out_lamports: None, + token_accounts: vec![ + token_account_compress, + token_account_transfer_2, + token_account_transfer_3, + transfer_recipient2, + transfer_recipient3, + ], + }; + let instruction = create_transfer2_instruction(inputs).map_err(ProgramError::from)?; + + let account_infos = [ + &[cpi_accounts.fee_payer().clone()][..], + ctx.remaining_accounts, + ] + .concat(); + invoke(&instruction, account_infos.as_slice())?; + } + + Ok(()) +} + +#[inline] +pub fn account_meta_from_account_info(account_info: &AccountInfo) -> AccountMeta { + AccountMeta { + pubkey: *account_info.key, + is_signer: account_info.is_signer, + is_writable: account_info.is_writable, + } +} diff --git a/program-tests/sdk-token-test/src/process_transfer_tokens.rs b/program-tests/sdk-token-test/src/process_transfer_tokens.rs new file mode 100644 index 0000000000..0f51dc2948 --- /dev/null +++ b/program-tests/sdk-token-test/src/process_transfer_tokens.rs @@ -0,0 +1,48 @@ +use anchor_lang::{prelude::*, solana_program::program::invoke}; +use light_compressed_token_sdk::{ + account::CTokenAccount, + instructions::transfer::{ + instruction::{transfer, TransferInputs}, + TransferAccountInfos, + }, + TokenAccountMeta, ValidityProof, +}; + +use crate::Generic; + +pub fn process_transfer_tokens<'info>( + ctx: Context<'_, '_, '_, 'info, Generic<'info>>, + validity_proof: ValidityProof, + token_metas: Vec, + output_tree_index: u8, + mint: Pubkey, + recipient: Pubkey, +) -> Result<()> { + let light_cpi_accounts = TransferAccountInfos::new( + ctx.accounts.signer.as_ref(), + ctx.accounts.signer.as_ref(), + ctx.remaining_accounts, + ); + let sender_account = CTokenAccount::new( + mint, + ctx.accounts.signer.key(), + token_metas, + output_tree_index, + ); + let transfer_inputs = TransferInputs { + fee_payer: ctx.accounts.signer.key(), + sender_account, + validity_proof, + recipient, + tree_pubkeys: light_cpi_accounts.tree_pubkeys().unwrap(), + config: None, + amount: 10, + }; + let instruction = transfer(transfer_inputs).unwrap(); + + let account_infos = light_cpi_accounts.to_account_infos(); + + invoke(&instruction, account_infos.as_slice())?; + + Ok(()) +} diff --git a/program-tests/sdk-token-test/src/process_update_deposit.rs b/program-tests/sdk-token-test/src/process_update_deposit.rs new file mode 100644 index 0000000000..1983ec3538 --- /dev/null +++ b/program-tests/sdk-token-test/src/process_update_deposit.rs @@ -0,0 +1,306 @@ +use anchor_lang::prelude::*; +use light_batched_merkle_tree::queue::BatchedQueueAccount; +use light_compressed_account::instruction_data::cpi_context::CompressedCpiContext; +use light_compressed_token_sdk::{ + account::CTokenAccount, + instructions::transfer::instruction::{TransferConfig, TransferInputs}, + TokenAccountMeta, +}; +use light_sdk::{ + account::LightAccount, + cpi::{CpiAccounts, CpiInputs}, + instruction::{PackedStateTreeInfo, ValidityProof}, + light_account_checks::AccountInfoTrait, + LightDiscriminator, LightHasher, +}; +use light_sdk_types::CpiAccountsConfig; + +use crate::{PdaParams, TokenParams}; + +#[event] +#[derive(Clone, Debug, Default, LightHasher, LightDiscriminator)] +pub struct CompressedEscrowPda { + pub amount: u64, + #[hash] + pub owner: Pubkey, +} + +pub fn process_update_escrow_pda( + cpi_accounts: CpiAccounts, + pda_params: PdaParams, + proof: ValidityProof, + deposit_amount: u64, + set_context: bool, +) -> Result<()> { + let mut my_compressed_account = LightAccount::<'_, CompressedEscrowPda>::new_mut( + &crate::ID, + &pda_params.account_meta, + CompressedEscrowPda { + owner: *cpi_accounts.fee_payer().key, + amount: pda_params.existing_amount, + }, + ) + .unwrap(); + + my_compressed_account.amount += deposit_amount; + + let cpi_inputs = CpiInputs { + proof, + account_infos: Some(vec![my_compressed_account + .to_account_info() + .map_err(ProgramError::from)?]), + new_addresses: None, + cpi_context: Some(CompressedCpiContext { + set_context, + first_set_context: set_context, + // change to bool works well. + cpi_context_account_index: 0, // seems to be useless. Seems to be unused. + // TODO: unify the account meta generation on and offchain. + }), + ..Default::default() + }; + cpi_inputs + .invoke_light_system_program(cpi_accounts) + .map_err(ProgramError::from)?; + + Ok(()) +} + +fn adjust_token_meta_indices(mut meta: TokenAccountMeta) -> TokenAccountMeta { + meta.packed_tree_info.merkle_tree_pubkey_index -= 1; + meta.packed_tree_info.queue_pubkey_index -= 1; + meta +} + +fn merge_escrow_token_accounts<'info>( + tree_account_infos: Vec>, + fee_payer: AccountInfo<'info>, + authority: AccountInfo<'info>, + remaining_accounts: &[AccountInfo<'info>], + mint: Pubkey, + recipient: Pubkey, + output_tree_queue_index: u8, + escrowed_token_meta: TokenAccountMeta, + escrow_token_account_meta_2: TokenAccountMeta, + address: [u8; 32], + recipient_bump: u8, +) -> Result<()> { + // 3. Merge the newly escrowed tokens into the existing escrow account. + // We remove the cpi context account -> we decrement all packed account indices by 1. + let adjusted_queue_index = output_tree_queue_index - 1; + let adjusted_escrowed_meta = adjust_token_meta_indices(escrowed_token_meta); + let adjusted_escrow_meta_2 = adjust_token_meta_indices(escrow_token_account_meta_2); + + let escrow_account = CTokenAccount::new( + mint, + recipient, + vec![adjusted_escrowed_meta, adjusted_escrow_meta_2], + adjusted_queue_index, + ); + + let total_escrowed_amount = escrow_account.amount; + + let tree_pubkeys = tree_account_infos + .iter() + .map(|x| x.pubkey()) + .collect::>(); + let transfer_inputs = TransferInputs { + fee_payer: *fee_payer.key, + sender_account: escrow_account, + // No validity proof necessary we are just storing state in the cpi context. + validity_proof: None.into(), + recipient, + tree_pubkeys, + config: Some(TransferConfig { + cpi_context: None, + cpi_context_pubkey: None, + ..Default::default() + }), + amount: total_escrowed_amount, + }; + let instruction = + light_compressed_token_sdk::instructions::transfer::instruction::transfer(transfer_inputs) + .unwrap(); + + let account_infos = [&[fee_payer, authority][..], remaining_accounts].concat(); + + let seeds = [&b"escrow"[..], &address, &[recipient_bump]]; + anchor_lang::solana_program::program::invoke_signed( + &instruction, + account_infos.as_slice(), + &[&seeds], + )?; + Ok(()) +} + +#[allow(clippy::too_many_arguments)] +pub fn transfer_tokens_to_escrow_pda<'info>( + cpi_accounts: &CpiAccounts<'_, 'info>, + remaining_accounts: &[AccountInfo<'info>], + mint: Pubkey, + amount: u64, + recipient: &Pubkey, + output_tree_index: u8, + output_tree_queue_index: u8, + address: [u8; 32], + recipient_bump: u8, + depositing_token_metas: Vec, +) -> Result { + // 1.transfer depositing token to recipient pda -> escrow token account 2 + let sender_account = CTokenAccount::new( + mint, + *cpi_accounts.fee_payer().key, + depositing_token_metas, + output_tree_queue_index, + ); + // leaf index is the next index in the output queue, + let output_queue = BatchedQueueAccount::output_from_account_info( + cpi_accounts + .get_tree_account_info(output_tree_queue_index as usize) + .unwrap(), + ) + .unwrap(); + // SAFETY: state trees are height 32 -> as u32 will always succeed + let leaf_index = output_queue.batch_metadata.next_index as u32 + 1; + + let escrow_token_account_meta_2 = TokenAccountMeta { + amount, + delegate_index: None, + lamports: None, + tlv: None, + packed_tree_info: PackedStateTreeInfo { + root_index: 0, // not used proof by index + prove_by_index: true, + merkle_tree_pubkey_index: output_tree_index, + queue_pubkey_index: output_tree_queue_index, + leaf_index, + }, + }; + + // TODO: remove cpi context pda from tree accounts. + // The confusing thing is that cpi context pda is the first packed account so it should be in the tree accounts. + // because the tree accounts are packed accounts. + // - rename tree_accounts to packed accounts + // - omit cpi context in tree_pubkeys + let tree_account_infos = cpi_accounts.tree_accounts().unwrap(); + let tree_account_infos = &tree_account_infos[1..]; + let tree_pubkeys = tree_account_infos + .iter() + .map(|x| x.pubkey()) + .collect::>(); + let cpi_context_pubkey = *cpi_accounts.cpi_context().unwrap().key; + let transfer_inputs = TransferInputs { + fee_payer: *cpi_accounts.fee_payer().key, + sender_account, + // No validity proof necessary we are just storing state in the cpi context. + validity_proof: None.into(), + recipient: *recipient, + tree_pubkeys, + config: Some(TransferConfig { + cpi_context: Some(CompressedCpiContext { + set_context: true, + first_set_context: true, + // TODO: change to bool and add sanity check that if true account in index 0 is a cpi context pubkey + cpi_context_account_index: 0, // TODO: replace with Pubkey (maybe not because it is in tree pubkeys 1 in this case) + }), + cpi_context_pubkey: Some(cpi_context_pubkey), // cpi context pubkey is in index 0. + ..Default::default() + }), + amount, + }; + let instruction = + light_compressed_token_sdk::instructions::transfer::instruction::transfer(transfer_inputs) + .unwrap(); + + let account_infos = [&[cpi_accounts.fee_payer().clone()][..], remaining_accounts].concat(); + + let seeds = [&b"escrow"[..], &address, &[recipient_bump]]; + anchor_lang::solana_program::program::invoke_signed( + &instruction, + account_infos.as_slice(), + &[&seeds], + )?; + + Ok(escrow_token_account_meta_2) +} + +pub fn process_update_deposit<'info>( + ctx: Context<'_, '_, '_, 'info, crate::GenericWithAuthority<'info>>, + output_tree_index: u8, + output_tree_queue_index: u8, + proof: ValidityProof, + system_accounts_start_offset: u8, + token_params: TokenParams, + pda_params: PdaParams, +) -> Result<()> { + // It makes sense to parse accounts once. + let config = CpiAccountsConfig { + cpi_signer: crate::LIGHT_CPI_SIGNER, + cpi_context: true, + sol_pool_pda: false, + sol_compression_recipient: false, + }; + + let (_token_account_infos, system_account_infos) = ctx + .remaining_accounts + .split_at(system_accounts_start_offset as usize); + // TODO: figure out why the offsets are wrong. + // Could add with pre account infos Option + let cpi_accounts = + CpiAccounts::new_with_config(ctx.accounts.signer.as_ref(), system_account_infos, config); + + let recipient = *ctx.accounts.authority.key; + // We want to keep only one escrow compressed token account + // But ctoken transfers can only have one signer -> we cannot from 2 signers at the same time + // 1. transfer depositing token to recipient pda -> escrow token account 2 + // 2. update escrow pda balance + // 3. merge escrow token account 2 into escrow token account + // Note: + // - if the escrow pda only stores the amount and the owner we can omit the escrow pda. + // - the escrowed token accounts are owned by a pda derived from the owner + // that is sufficient to verify ownership. + // - no escrow pda will simplify the transaction, for no cpi context account is required + let address = pda_params.account_meta.address; + + // 1.transfer depositing token to recipient pda -> escrow token account 2 + let escrow_token_account_meta_2 = transfer_tokens_to_escrow_pda( + &cpi_accounts, + ctx.remaining_accounts, + token_params.mint, + token_params.deposit_amount, + &recipient, + output_tree_index, + output_tree_queue_index, + address, + token_params.recipient_bump, + token_params.depositing_token_metas, + )?; + let tree_account_infos = cpi_accounts.tree_accounts().unwrap()[1..].to_vec(); + let fee_payer = cpi_accounts.fee_payer().clone(); + + // 2. Update escrow pda balance + // - settle tx 1 in the same instruction with the cpi context account + process_update_escrow_pda( + cpi_accounts, + pda_params, + proof, + token_params.deposit_amount, + false, + )?; + + // 3. Merge the newly escrowed tokens into the existing escrow account. + merge_escrow_token_accounts( + tree_account_infos, + fee_payer, + ctx.accounts.authority.to_account_info(), + ctx.remaining_accounts, + token_params.mint, + recipient, + output_tree_queue_index, + token_params.escrowed_token_meta, + escrow_token_account_meta_2, + address, + token_params.recipient_bump, + )?; + Ok(()) +} diff --git a/program-tests/sdk-token-test/tests/test.rs b/program-tests/sdk-token-test/tests/test.rs new file mode 100644 index 0000000000..bbef7d1816 --- /dev/null +++ b/program-tests/sdk-token-test/tests/test.rs @@ -0,0 +1,614 @@ +// #![cfg(feature = "test-sbf")] + +use anchor_lang::{AccountDeserialize, InstructionData}; +use anchor_spl::token::TokenAccount; +use light_client::indexer::CompressedTokenAccount; +use light_compressed_token_sdk::{ + instructions::{ + batch_compress::{ + get_batch_compress_instruction_account_metas, BatchCompressMetaConfig, Recipient, + }, + transfer::account_metas::{ + get_transfer_instruction_account_metas, TokenAccountsMetaConfig, + }, + }, + token_pool::{find_token_pool_pda_with_index, get_token_pool_pda}, + TokenAccountMeta, SPL_TOKEN_PROGRAM_ID, +}; +use light_program_test::{Indexer, LightProgramTest, ProgramTestConfig, Rpc}; +use light_sdk::instruction::PackedAccounts; +use light_test_utils::{ + spl::{create_mint_helper, create_token_account, mint_spl_tokens}, + RpcError, +}; +use solana_sdk::{ + instruction::Instruction, + pubkey::Pubkey, + signature::{Keypair, Signature, Signer}, +}; + +#[tokio::test] +async fn test() { + // Initialize the test environment + let mut rpc = LightProgramTest::new(ProgramTestConfig::new_v2( + false, + Some(vec![("sdk_token_test", sdk_token_test::ID)]), + )) + .await + .unwrap(); + + let payer = rpc.get_payer().insecure_clone(); + + // Create a mint + let mint_pubkey = create_mint_helper(&mut rpc, &payer).await; + println!("Created mint: {}", mint_pubkey); + + // Create a token account + let token_account_keypair = Keypair::new(); + + create_token_account(&mut rpc, &mint_pubkey, &token_account_keypair, &payer) + .await + .unwrap(); + + println!("Created token account: {}", token_account_keypair.pubkey()); + + // Mint some tokens to the account + let mint_amount = 1_000_000; // 1000 tokens with 6 decimals + + mint_spl_tokens( + &mut rpc, + &mint_pubkey, + &token_account_keypair.pubkey(), + &payer.pubkey(), // owner + &payer, // mint authority + mint_amount, + false, // not token22 + ) + .await + .unwrap(); + + println!("Minted {} tokens to account", mint_amount); + + // Verify the token account has the correct balance before compression + let token_account_data = rpc + .get_account(token_account_keypair.pubkey()) + .await + .unwrap() + .unwrap(); + + let token_account = + TokenAccount::try_deserialize(&mut token_account_data.data.as_slice()).unwrap(); + + assert_eq!(token_account.amount, mint_amount); + assert_eq!(token_account.mint, mint_pubkey); + assert_eq!(token_account.owner, payer.pubkey()); + + println!("Verified token account balance before compression"); + + // Now compress the SPL tokens + let compress_amount = 500_000; // Compress half of the tokens + let compression_recipient = payer.pubkey(); // Compress to the same owner + + // Declare transfer parameters early + let transfer_recipient = Keypair::new(); + let transfer_amount = 10; + + compress_spl_tokens( + &mut rpc, + &payer, + compression_recipient, + mint_pubkey, + compress_amount, + token_account_keypair.pubkey(), + ) + .await + .unwrap(); + + println!("Compressed {} tokens successfully", compress_amount); + + // Get the compressed token account from indexer + let compressed_accounts = rpc + .indexer() + .unwrap() + .get_compressed_token_accounts_by_owner(&payer.pubkey(), None, None) + .await + .unwrap() + .value + .items; + + let compressed_account = &compressed_accounts[0]; + + // Assert the compressed token account properties + assert_eq!(compressed_account.token.owner, payer.pubkey()); + assert_eq!(compressed_account.token.mint, mint_pubkey); + + // Verify the token amount (should match the compressed amount) + let amount = compressed_account.token.amount; + assert_eq!(amount, compress_amount); + + println!( + "Verified compressed token account: owner={}, mint={}, amount={}", + payer.pubkey(), + mint_pubkey, + amount + ); + println!("compressed_account {:?}", compressed_account); + // Now transfer some compressed tokens to a recipient + transfer_compressed_tokens( + &mut rpc, + &payer, + transfer_recipient.pubkey(), + compressed_account, + ) + .await + .unwrap(); + + println!( + "Transferred {} compressed tokens to recipient successfully", + transfer_amount + ); + + // Verify the transfer by checking both sender and recipient accounts + let updated_accounts = rpc + .indexer() + .unwrap() + .get_compressed_token_accounts_by_owner(&payer.pubkey(), None, None) + .await + .unwrap() + .value + .items; + + let recipient_accounts = rpc + .indexer() + .unwrap() + .get_compressed_token_accounts_by_owner(&transfer_recipient.pubkey(), None, None) + .await + .unwrap() + .value + .items; + + // Sender should have (compress_amount - transfer_amount) remaining + if !updated_accounts.is_empty() { + let sender_account = &updated_accounts[0]; + let sender_amount = sender_account.token.amount; + assert_eq!(sender_amount, compress_amount - transfer_amount); + println!("Verified sender remaining balance: {}", sender_amount); + } + + // Recipient should have transfer_amount + assert!( + !recipient_accounts.is_empty(), + "Recipient should have compressed token account" + ); + let recipient_account = &recipient_accounts[0]; + assert_eq!(recipient_account.token.owner, transfer_recipient.pubkey()); + let recipient_amount = recipient_account.token.amount; + assert_eq!(recipient_amount, transfer_amount); + println!("Verified recipient balance: {}", recipient_amount); + + // Now decompress some tokens from the recipient back to SPL token account + let decompress_token_account_keypair = Keypair::new(); + let decompress_amount = 10; // Decompress a small amount + rpc.airdrop_lamports(&transfer_recipient.pubkey(), 10_000_000_000) + .await + .unwrap(); + // Create a new SPL token account for decompression + create_token_account( + &mut rpc, + &mint_pubkey, + &decompress_token_account_keypair, + &transfer_recipient, + ) + .await + .unwrap(); + + println!( + "Created decompress token account: {}", + decompress_token_account_keypair.pubkey() + ); + + // Get the recipient's compressed token account after transfer + let recipient_compressed_accounts = rpc + .indexer() + .unwrap() + .get_compressed_token_accounts_by_owner(&transfer_recipient.pubkey(), None, None) + .await + .unwrap() + .value + .items; + + let recipient_compressed_account = &recipient_compressed_accounts[0]; + + // Decompress tokens from recipient's compressed account to SPL token account + decompress_compressed_tokens( + &mut rpc, + &transfer_recipient, + recipient_compressed_account, + decompress_token_account_keypair.pubkey(), + ) + .await + .unwrap(); + + println!( + "Decompressed {} tokens from recipient successfully", + decompress_amount + ); + + // Verify the decompression worked + let decompress_token_account_data = rpc + .get_account(decompress_token_account_keypair.pubkey()) + .await + .unwrap() + .unwrap(); + + let decompress_token_account = + TokenAccount::try_deserialize(&mut decompress_token_account_data.data.as_slice()).unwrap(); + + // Assert the SPL token account has the decompressed amount + assert_eq!(decompress_token_account.amount, decompress_amount); + assert_eq!(decompress_token_account.mint, mint_pubkey); + assert_eq!(decompress_token_account.owner, transfer_recipient.pubkey()); + + println!( + "Verified SPL token account after decompression: amount={}", + decompress_token_account.amount + ); + + // Verify the compressed account balance was reduced + let updated_recipient_accounts = rpc + .indexer() + .unwrap() + .get_compressed_token_accounts_by_owner(&transfer_recipient.pubkey(), None, None) + .await + .unwrap() + .value + .items; + + if !updated_recipient_accounts.is_empty() { + let updated_recipient_account = &updated_recipient_accounts[0]; + let remaining_compressed_amount = updated_recipient_account.token.amount; + assert_eq!( + remaining_compressed_amount, + transfer_amount - decompress_amount + ); + println!( + "Verified remaining compressed balance: {}", + remaining_compressed_amount + ); + } + + println!("Compression, transfer, and decompress test completed successfully!"); +} + +async fn compress_spl_tokens( + rpc: &mut LightProgramTest, + payer: &Keypair, + recipient: Pubkey, + mint: Pubkey, + amount: u64, + token_account: Pubkey, +) -> Result { + let mut remaining_accounts = PackedAccounts::default(); + let token_pool_pda = get_token_pool_pda(&mint); + let config = TokenAccountsMetaConfig::compress_client( + token_pool_pda, + token_account, + SPL_TOKEN_PROGRAM_ID.into(), + ); + remaining_accounts.add_pre_accounts_signer_mut(payer.pubkey()); + let metas = get_transfer_instruction_account_metas(config); + println!("metas {:?}", metas.to_vec()); + // Add the token account to pre_accounts for the compressiospl_token_programn + remaining_accounts.add_pre_accounts_metas(metas.as_slice()); + + let output_tree_index = rpc + .get_random_state_tree_info() + .unwrap() + .pack_output_tree_index(&mut remaining_accounts) + .unwrap(); + + let (remaining_accounts, _, _) = remaining_accounts.to_account_metas(); + println!("remaining_accounts {:?}", remaining_accounts.to_vec()); + + let instruction = Instruction { + program_id: sdk_token_test::ID, + accounts: [remaining_accounts].concat(), + data: sdk_token_test::instruction::CompressTokens { + output_tree_index, + recipient, + mint, + amount, + } + .data(), + }; + + rpc.create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await +} + +async fn transfer_compressed_tokens( + rpc: &mut LightProgramTest, + payer: &Keypair, + recipient: Pubkey, + compressed_account: &CompressedTokenAccount, +) -> Result { + let mut remaining_accounts = PackedAccounts::default(); + let config = TokenAccountsMetaConfig::new_client(); + remaining_accounts.add_pre_accounts_signer_mut(payer.pubkey()); + let metas = get_transfer_instruction_account_metas(config); + remaining_accounts.add_pre_accounts_metas(metas.as_slice()); + + // Get validity proof from RPC + let rpc_result = rpc + .get_validity_proof(vec![compressed_account.account.hash], vec![], None) + .await? + .value; + + let packed_tree_info = rpc_result.pack_tree_infos(&mut remaining_accounts); + let output_tree_index = packed_tree_info + .state_trees + .as_ref() + .unwrap() + .output_tree_index; + + // Use the tree info from the validity proof result + let tree_info = packed_tree_info + .state_trees + .as_ref() + .unwrap() + .packed_tree_infos[0]; + println!("Transfer tree_info: {:?}", tree_info); + + // Create input token data + let token_metas = vec![TokenAccountMeta { + amount: compressed_account.token.amount, + delegate_index: None, + packed_tree_info: tree_info, + lamports: None, + tlv: None, + }]; + + let (accounts, _, _) = remaining_accounts.to_account_metas(); + + let instruction = Instruction { + program_id: sdk_token_test::ID, + accounts, + data: sdk_token_test::instruction::TransferTokens { + validity_proof: rpc_result.proof, + token_metas, + output_tree_index, + mint: compressed_account.token.mint, + recipient, + } + .data(), + }; + + rpc.create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await +} + +async fn decompress_compressed_tokens( + rpc: &mut LightProgramTest, + payer: &Keypair, + compressed_account: &CompressedTokenAccount, + decompress_token_account: Pubkey, +) -> Result { + let mut remaining_accounts = PackedAccounts::default(); + let token_pool_pda = get_token_pool_pda(&compressed_account.token.mint); + let config = TokenAccountsMetaConfig::decompress_client( + token_pool_pda, + decompress_token_account, + SPL_TOKEN_PROGRAM_ID.into(), + ); + remaining_accounts.add_pre_accounts_signer_mut(payer.pubkey()); + let metas = get_transfer_instruction_account_metas(config); + remaining_accounts.add_pre_accounts_metas(metas.as_slice()); + + // Get validity proof from RPC + let rpc_result = rpc + .get_validity_proof(vec![compressed_account.account.hash], vec![], None) + .await? + .value; + + let packed_tree_info = rpc_result.pack_tree_infos(&mut remaining_accounts); + let output_tree_index = packed_tree_info + .state_trees + .as_ref() + .unwrap() + .output_tree_index; + + // Use the tree info from the validity proof result + let tree_info = packed_tree_info + .state_trees + .as_ref() + .unwrap() + .packed_tree_infos[0]; + + // Create input token data + let token_data = vec![TokenAccountMeta { + amount: compressed_account.token.amount, + delegate_index: None, + packed_tree_info: tree_info, + lamports: None, + tlv: None, + }]; + + let (remaining_accounts, _, _) = remaining_accounts.to_account_metas(); + println!(" remaining_accounts: {:?}", remaining_accounts); + + let instruction = Instruction { + program_id: sdk_token_test::ID, + accounts: [remaining_accounts].concat(), + data: sdk_token_test::instruction::DecompressTokens { + validity_proof: rpc_result.proof, + token_data, + output_tree_index, + mint: compressed_account.token.mint, + } + .data(), + }; + + rpc.create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await +} + +#[tokio::test] +async fn test_batch_compress() { + // Initialize the test environment + let mut rpc = LightProgramTest::new(ProgramTestConfig::new_v2( + false, + Some(vec![("sdk_token_test", sdk_token_test::ID)]), + )) + .await + .unwrap(); + + let payer = rpc.get_payer().insecure_clone(); + + // Create a mint + let mint_pubkey = create_mint_helper(&mut rpc, &payer).await; + println!("Created mint: {}", mint_pubkey); + + // Create a token account + let token_account_keypair = Keypair::new(); + + create_token_account(&mut rpc, &mint_pubkey, &token_account_keypair, &payer) + .await + .unwrap(); + + println!("Created token account: {}", token_account_keypair.pubkey()); + + // Mint some tokens to the account + let mint_amount = 2_000_000; // 2000 tokens with 6 decimals + + mint_spl_tokens( + &mut rpc, + &mint_pubkey, + &token_account_keypair.pubkey(), + &payer.pubkey(), // owner + &payer, // mint authority + mint_amount, + false, // not token22 + ) + .await + .unwrap(); + + println!("Minted {} tokens to account", mint_amount); + + // Create multiple recipients for batch compression + let recipient1 = Keypair::new().pubkey(); + let recipient2 = Keypair::new().pubkey(); + let recipient3 = Keypair::new().pubkey(); + + let recipients = vec![ + Recipient { + pubkey: recipient1, + amount: 100_000, + }, + Recipient { + pubkey: recipient2, + amount: 200_000, + }, + Recipient { + pubkey: recipient3, + amount: 300_000, + }, + ]; + + let total_batch_amount: u64 = recipients.iter().map(|r| r.amount).sum(); + + // Perform batch compression + batch_compress_spl_tokens( + &mut rpc, + &payer, + recipients, + mint_pubkey, + token_account_keypair.pubkey(), + ) + .await + .unwrap(); + + println!( + "Batch compressed {} tokens to {} recipients successfully", + total_batch_amount, 3 + ); + + // Verify each recipient received their compressed tokens + for (i, recipient) in [recipient1, recipient2, recipient3].iter().enumerate() { + let compressed_accounts = rpc + .indexer() + .unwrap() + .get_compressed_token_accounts_by_owner(recipient, None, None) + .await + .unwrap() + .value + .items; + + assert!( + !compressed_accounts.is_empty(), + "Recipient {} should have compressed tokens", + i + 1 + ); + + let compressed_account = &compressed_accounts[0]; + assert_eq!(compressed_account.token.owner, *recipient); + assert_eq!(compressed_account.token.mint, mint_pubkey); + + let expected_amount = match i { + 0 => 100_000, + 1 => 200_000, + 2 => 300_000, + _ => unreachable!(), + }; + assert_eq!(compressed_account.token.amount, expected_amount); + + println!( + "Verified recipient {} received {} compressed tokens", + i + 1, + compressed_account.token.amount + ); + } + + println!("Batch compression test completed successfully!"); +} + +async fn batch_compress_spl_tokens( + rpc: &mut LightProgramTest, + payer: &Keypair, + recipients: Vec, + mint: Pubkey, + token_account: Pubkey, +) -> Result { + let mut remaining_accounts = PackedAccounts::default(); + remaining_accounts.add_pre_accounts_signer_mut(payer.pubkey()); + let token_pool_index = 0; + let (token_pool_pda, token_pool_bump) = find_token_pool_pda_with_index(&mint, token_pool_index); + println!("token_pool_pda {:?}", token_pool_pda); + // Use batch compress account metas + let config = BatchCompressMetaConfig::new_client( + token_pool_pda, + token_account, + SPL_TOKEN_PROGRAM_ID.into(), + rpc.get_random_state_tree_info().unwrap().queue, + false, // with_lamports + ); + let metas = get_batch_compress_instruction_account_metas(config); + println!("metas {:?}", metas); + remaining_accounts.add_pre_accounts_metas(metas.as_slice()); + + let (accounts, _, _) = remaining_accounts.to_account_metas(); + println!("accounts {:?}", accounts); + + let instruction = Instruction { + program_id: sdk_token_test::ID, + accounts, + data: sdk_token_test::instruction::BatchCompressTokens { + recipients, + token_pool_index, + token_pool_bump, + } + .data(), + }; + + rpc.create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await +} diff --git a/program-tests/sdk-token-test/tests/test_4_invocations.rs b/program-tests/sdk-token-test/tests/test_4_invocations.rs new file mode 100644 index 0000000000..52343e7900 --- /dev/null +++ b/program-tests/sdk-token-test/tests/test_4_invocations.rs @@ -0,0 +1,597 @@ +use anchor_lang::{prelude::AccountMeta, AccountDeserialize, InstructionData}; +use light_compressed_token_sdk::{ + instructions::{ + transfer::account_metas::{ + get_transfer_instruction_account_metas, TokenAccountsMetaConfig, + }, + CTokenDefaultAccounts, + }, + token_pool::get_token_pool_pda, + SPL_TOKEN_PROGRAM_ID, +}; +use light_program_test::{AddressWithTree, Indexer, LightProgramTest, ProgramTestConfig, Rpc}; +use light_sdk::{ + address::v1::derive_address, + instruction::{PackedAccounts, SystemAccountMetaConfig}, +}; +use light_test_utils::{ + spl::{create_mint_helper, create_token_account, mint_spl_tokens}, + RpcError, +}; +use solana_sdk::{ + instruction::Instruction, + pubkey::Pubkey, + signature::{Keypair, Signature, Signer}, +}; + +#[ignore = "fix cpi context usage"] +#[tokio::test] +async fn test_4_invocations() { + // Initialize the test environment + let mut rpc = LightProgramTest::new(ProgramTestConfig::new_v2( + false, + Some(vec![("sdk_token_test", sdk_token_test::ID)]), + )) + .await + .unwrap(); + + let payer = rpc.get_payer().insecure_clone(); + + let (mint1, mint2, mint3, token_account_1, token_account_2, token_account_3) = + create_mints_and_tokens(&mut rpc, &payer).await; + + println!("✅ Test setup complete: 3 mints created and minted to 3 token accounts"); + + // Compress tokens + let compress_amount = 1000; // Compress 1000 tokens + + compress_tokens_bundled( + &mut rpc, + &payer, + vec![ + (token_account_2, compress_amount, Some(mint2)), + (token_account_3, compress_amount, Some(mint3)), + ], + ) + .await + .unwrap(); + + println!( + "✅ Completed compression of {} tokens from mint 2 and mint 3", + compress_amount + ); + + // Create compressed escrow PDA + let initial_amount = 100; // Initial escrow amount + let escrow_address = create_compressed_escrow_pda(&mut rpc, &payer, initial_amount) + .await + .unwrap(); + + println!( + "✅ Created compressed escrow PDA with address: {:?}", + escrow_address + ); + + // Test the four_invokes instruction + test_four_invokes_instruction( + &mut rpc, + &payer, + mint1, + mint2, + mint3, + escrow_address, + initial_amount, + token_account_1, + ) + .await + .unwrap(); + + println!("✅ Successfully executed four_invokes instruction"); +} + +async fn create_mints_and_tokens( + rpc: &mut impl Rpc, + payer: &Keypair, +) -> ( + solana_sdk::pubkey::Pubkey, // mint1 + solana_sdk::pubkey::Pubkey, // mint2 + solana_sdk::pubkey::Pubkey, // mint3 + solana_sdk::pubkey::Pubkey, // token1 + solana_sdk::pubkey::Pubkey, // token2 + solana_sdk::pubkey::Pubkey, // token3 +) { + // Create 3 SPL mints + let mint1_pubkey = create_mint_helper(rpc, payer).await; + let mint2_pubkey = create_mint_helper(rpc, payer).await; + let mint3_pubkey = create_mint_helper(rpc, payer).await; + + println!("Created mint 1: {}", mint1_pubkey); + println!("Created mint 2: {}", mint2_pubkey); + println!("Created mint 3: {}", mint3_pubkey); + + // Create 3 SPL token accounts (one for each mint) + let token_account1_keypair = Keypair::new(); + let token_account2_keypair = Keypair::new(); + let token_account3_keypair = Keypair::new(); + + // Create token account for mint 1 + create_token_account(rpc, &mint1_pubkey, &token_account1_keypair, payer) + .await + .unwrap(); + + // Create token account for mint 2 + create_token_account(rpc, &mint2_pubkey, &token_account2_keypair, payer) + .await + .unwrap(); + + // Create token account for mint 3 + create_token_account(rpc, &mint3_pubkey, &token_account3_keypair, payer) + .await + .unwrap(); + + println!( + "Created token account 1: {}", + token_account1_keypair.pubkey() + ); + println!( + "Created token account 2: {}", + token_account2_keypair.pubkey() + ); + println!( + "Created token account 3: {}", + token_account3_keypair.pubkey() + ); + + // Mint tokens to each account + let mint_amount = 1_000_000; // 1000 tokens with 6 decimals + + // Mint to token account 1 + mint_spl_tokens( + rpc, + &mint1_pubkey, + &token_account1_keypair.pubkey(), + &payer.pubkey(), // owner + payer, // mint authority + mint_amount, + false, // not token22 + ) + .await + .unwrap(); + + // Mint to token account 2 + mint_spl_tokens( + rpc, + &mint2_pubkey, + &token_account2_keypair.pubkey(), + &payer.pubkey(), // owner + payer, // mint authority + mint_amount, + false, // not token22 + ) + .await + .unwrap(); + + // Mint to token account 3 + mint_spl_tokens( + rpc, + &mint3_pubkey, + &token_account3_keypair.pubkey(), + &payer.pubkey(), // owner + payer, // mint authority + mint_amount, + false, // not token22 + ) + .await + .unwrap(); + + println!("Minted {} tokens to each account", mint_amount); + + // Verify all token accounts have the correct balances + verify_token_account_balance( + rpc, + &token_account1_keypair.pubkey(), + &mint1_pubkey, + &payer.pubkey(), + mint_amount, + ) + .await; + verify_token_account_balance( + rpc, + &token_account2_keypair.pubkey(), + &mint2_pubkey, + &payer.pubkey(), + mint_amount, + ) + .await; + verify_token_account_balance( + rpc, + &token_account3_keypair.pubkey(), + &mint3_pubkey, + &payer.pubkey(), + mint_amount, + ) + .await; + + ( + mint1_pubkey, + mint2_pubkey, + mint3_pubkey, + token_account1_keypair.pubkey(), + token_account2_keypair.pubkey(), + token_account3_keypair.pubkey(), + ) +} + +async fn verify_token_account_balance( + rpc: &mut impl Rpc, + token_account_pubkey: &solana_sdk::pubkey::Pubkey, + expected_mint: &solana_sdk::pubkey::Pubkey, + expected_owner: &solana_sdk::pubkey::Pubkey, + expected_amount: u64, +) { + use anchor_lang::AccountDeserialize; + use anchor_spl::token::TokenAccount; + + let token_account_data = rpc + .get_account(*token_account_pubkey) + .await + .unwrap() + .unwrap(); + + let token_account = + TokenAccount::try_deserialize(&mut token_account_data.data.as_slice()).unwrap(); + + assert_eq!(token_account.amount, expected_amount); + assert_eq!(token_account.mint, *expected_mint); + assert_eq!(token_account.owner, *expected_owner); + + println!( + "✅ Verified token account {} has correct balance and properties", + token_account_pubkey + ); +} + +// Copy the working compress function from test.rs +async fn compress_spl_tokens( + rpc: &mut impl Rpc, + payer: &Keypair, + recipient: Pubkey, + mint: Pubkey, + amount: u64, + token_account: Pubkey, +) -> Result { + let mut remaining_accounts = PackedAccounts::default(); + let token_pool_pda = get_token_pool_pda(&mint); + let config = TokenAccountsMetaConfig::compress_client( + token_pool_pda, + token_account, + SPL_TOKEN_PROGRAM_ID.into(), + ); + remaining_accounts.add_pre_accounts_signer_mut(payer.pubkey()); + let metas = get_transfer_instruction_account_metas(config); + remaining_accounts.add_pre_accounts_metas(metas.as_slice()); + + let output_tree_index = rpc + .get_random_state_tree_info() + .unwrap() + .pack_output_tree_index(&mut remaining_accounts) + .unwrap(); + + let (remaining_accounts, _, _) = remaining_accounts.to_account_metas(); + + let instruction = Instruction { + program_id: sdk_token_test::ID, + accounts: remaining_accounts, + data: sdk_token_test::instruction::CompressTokens { + output_tree_index, + recipient, + mint, + amount, + } + .data(), + }; + + rpc.create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await +} + +async fn compress_tokens( + rpc: &mut impl Rpc, + payer: &Keypair, + sender_token_account: Pubkey, + amount: u64, + mint: Option, +) -> Result { + // Get mint from token account if not provided + let mint = match mint { + Some(mint) => mint, + None => { + let token_account_data = rpc + .get_account(sender_token_account) + .await? + .ok_or_else(|| RpcError::CustomError("Token account not found".to_string()))?; + + let token_account = anchor_spl::token::TokenAccount::try_deserialize( + &mut token_account_data.data.as_slice(), + ) + .map_err(|e| { + RpcError::CustomError(format!("Failed to deserialize token account: {}", e)) + })?; + + token_account.mint + } + }; + + // Use the working compress function + compress_spl_tokens( + rpc, + payer, + payer.pubkey(), // recipient + mint, + amount, + sender_token_account, + ) + .await +} + +async fn compress_tokens_bundled( + rpc: &mut impl Rpc, + payer: &Keypair, + compressions: Vec<(Pubkey, u64, Option)>, // (token_account, amount, optional_mint) +) -> Result, RpcError> { + let mut signatures = Vec::new(); + + for (token_account, amount, mint) in compressions { + let sig = compress_tokens(rpc, payer, token_account, amount, mint).await?; + signatures.push(sig); + println!( + "✅ Compressed {} tokens from token account {}", + amount, token_account + ); + } + + Ok(signatures) +} + +async fn create_compressed_escrow_pda( + rpc: &mut (impl Rpc + Indexer), + payer: &Keypair, + initial_amount: u64, +) -> Result<[u8; 32], RpcError> { + let tree_info = rpc.get_random_state_tree_info().unwrap(); + let mut remaining_accounts = PackedAccounts::default(); + remaining_accounts.add_pre_accounts_signer_mut(payer.pubkey()); + + // Add system accounts configuration + let config = SystemAccountMetaConfig::new(sdk_token_test::ID); + remaining_accounts.add_system_accounts(config).unwrap(); + + // Get address tree info and derive the PDA address + let address_tree_info = rpc.get_address_tree_v1(); + let (address, address_seed) = derive_address( + &[b"escrow", payer.pubkey().to_bytes().as_ref()], + &address_tree_info.tree, + &sdk_token_test::ID, + ); + + let output_tree_index = tree_info + .pack_output_tree_index(&mut remaining_accounts) + .unwrap(); + + // Get validity proof with address + let rpc_result = rpc + .get_validity_proof( + vec![], // No compressed accounts to prove + vec![AddressWithTree { + address, + tree: address_tree_info.tree, + }], + None, + ) + .await? + .value; + + let packed_tree_info = rpc_result.pack_tree_infos(&mut remaining_accounts); + let new_address_params = + packed_tree_info.address_trees[0].into_new_address_params_packed(address_seed); + + let (accounts, _, _) = remaining_accounts.to_account_metas(); + + let instruction = Instruction { + program_id: sdk_token_test::ID, + accounts, + data: sdk_token_test::instruction::CreateEscrowPda { + proof: rpc_result.proof, + output_tree_index, + amount: initial_amount, + address, + new_address_params, + } + .data(), + }; + + rpc.create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await?; + + Ok(address) +} + +#[allow(clippy::too_many_arguments)] +async fn test_four_invokes_instruction( + rpc: &mut (impl Rpc + Indexer), + payer: &Keypair, + mint1: Pubkey, + mint2: Pubkey, + mint3: Pubkey, + escrow_address: [u8; 32], + initial_escrow_amount: u64, + compression_token_account: Pubkey, +) -> Result<(), RpcError> { + let default_pubkeys = CTokenDefaultAccounts::default(); + let mut remaining_accounts = PackedAccounts::default(); + let token_pool_pda1 = get_token_pool_pda(&mint1); + // Remaining accounts 0 + remaining_accounts.add_pre_accounts_meta(AccountMeta::new(compression_token_account, false)); + // Remaining accounts 1 + remaining_accounts.add_pre_accounts_meta(AccountMeta::new(token_pool_pda1, false)); + // Remaining accounts 2 + remaining_accounts.add_pre_accounts_meta(AccountMeta::new(SPL_TOKEN_PROGRAM_ID.into(), false)); + // Remaining accounts 3 + remaining_accounts.add_pre_accounts_meta(AccountMeta::new( + default_pubkeys.compressed_token_program, + false, + )); + // Remaining accounts 4 + remaining_accounts + .add_pre_accounts_meta(AccountMeta::new(default_pubkeys.cpi_authority_pda, false)); + + // Add system accounts configuration with CPI context + let tree_info = rpc.get_random_state_tree_info().unwrap(); + + // Check if CPI context is available, otherwise this instruction can't work + if tree_info.cpi_context.is_none() { + panic!("CPI context account is required for four_invokes instruction but not available in tree_info"); + } + + let config = SystemAccountMetaConfig::new_with_cpi_context( + sdk_token_test::ID, + tree_info.cpi_context.unwrap(), + ); + remaining_accounts.add_system_accounts(config).unwrap(); + + // Get validity proof - need to prove the escrow PDA and compressed token accounts + let escrow_account = rpc + .get_compressed_account(escrow_address, None) + .await? + .value; + + // Get compressed token accounts for mint2 and mint3 + let compressed_token_accounts = rpc + .indexer() + .unwrap() + .get_compressed_token_accounts_by_owner(&payer.pubkey(), None, None) + .await? + .value + .items; + + let mint2_token_account = compressed_token_accounts + .iter() + .find(|acc| acc.token.mint == mint2) + .expect("Compressed token account for mint2 should exist"); + + let mint3_token_account = compressed_token_accounts + .iter() + .find(|acc| acc.token.mint == mint3) + .expect("Compressed token account for mint3 should exist"); + + let rpc_result = rpc + .get_validity_proof( + vec![ + escrow_account.hash, + mint2_token_account.account.hash, + mint3_token_account.account.hash, + ], + vec![], + None, + ) + .await? + .value; + // We need to pack the tree after the cpi context. + remaining_accounts.insert_or_get(rpc_result.accounts[0].tree_info.tree); + + let packed_tree_info = rpc_result.pack_tree_infos(&mut remaining_accounts); + let output_tree_index = packed_tree_info + .state_trees + .as_ref() + .unwrap() + .output_tree_index; + + // Create token metas from compressed accounts - each uses its respective tree info index + // Index 0: escrow PDA, Index 1: mint2 token account, Index 2: mint3 token account + let mint2_tree_info = packed_tree_info + .state_trees + .as_ref() + .unwrap() + .packed_tree_infos[1]; + + let mint3_tree_info = packed_tree_info + .state_trees + .as_ref() + .unwrap() + .packed_tree_infos[2]; + + // Create FourInvokesParams + let four_invokes_params = sdk_token_test::FourInvokesParams { + compress_1: sdk_token_test::CompressParams { + mint: mint1, + amount: 500, + recipient: payer.pubkey(), + recipient_bump: 0, + token_account: compression_token_account, + }, + transfer_2: sdk_token_test::TransferParams { + mint: mint2, + transfer_amount: 300, + token_metas: vec![light_compressed_token_sdk::TokenAccountMeta { + amount: mint2_token_account.token.amount, + delegate_index: None, + packed_tree_info: mint2_tree_info, + lamports: None, + tlv: None, + }], + recipient: payer.pubkey(), + recipient_bump: 0, + }, + transfer_3: sdk_token_test::TransferParams { + mint: mint3, + transfer_amount: 200, + token_metas: vec![light_compressed_token_sdk::TokenAccountMeta { + amount: mint3_token_account.token.amount, + delegate_index: None, + packed_tree_info: mint3_tree_info, + lamports: None, + tlv: None, + }], + recipient: payer.pubkey(), + recipient_bump: 0, + }, + }; + + // Create PdaParams - escrow PDA uses tree info index 0 + let escrow_tree_info = packed_tree_info + .state_trees + .as_ref() + .unwrap() + .packed_tree_infos[0]; + + let pda_params = sdk_token_test::PdaParams { + account_meta: light_sdk::instruction::account_meta::CompressedAccountMeta { + address: escrow_address, + tree_info: escrow_tree_info, + output_state_tree_index: output_tree_index, + }, + existing_amount: initial_escrow_amount, + }; + + let (accounts, system_accounts_start_offset, _) = remaining_accounts.to_account_metas(); + + // We need to concat here to separate remaining accounts from the payer account. + let accounts = [vec![AccountMeta::new(payer.pubkey(), true)], accounts].concat(); + let instruction = Instruction { + program_id: sdk_token_test::ID, + accounts, + data: sdk_token_test::instruction::FourInvokes { + output_tree_index, + proof: rpc_result.proof, + system_accounts_start_offset: system_accounts_start_offset as u8, + four_invokes_params, + pda_params, + } + .data(), + }; + + rpc.create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await?; + + Ok(()) +} diff --git a/program-tests/sdk-token-test/tests/test_4_transfer2.rs b/program-tests/sdk-token-test/tests/test_4_transfer2.rs new file mode 100644 index 0000000000..a01261faf2 --- /dev/null +++ b/program-tests/sdk-token-test/tests/test_4_transfer2.rs @@ -0,0 +1,578 @@ +use anchor_lang::{prelude::AccountMeta, InstructionData}; +use light_compressed_token_sdk::{ + instructions::{ + create_compressed_mint, create_mint_to_compressed_instruction, CTokenDefaultAccounts, + CreateCompressedMintInputs, MintToCompressedInputs, + }, + token_pool::get_token_pool_pda, +}; +use light_ctoken_types::{ + instructions::{ + mint_to_compressed::{CompressedMintInputs, Recipient}, + transfer2::MultiInputTokenDataWithContext, + }, + COMPRESSED_MINT_SEED, +}; +use light_program_test::{AddressWithTree, Indexer, LightProgramTest, ProgramTestConfig, Rpc}; +use light_sdk::{ + address::v1::derive_address, + instruction::{PackedAccounts, PackedStateTreeInfo, SystemAccountMetaConfig}, +}; +use light_test_utils::RpcError; +use solana_sdk::{ + instruction::Instruction, + pubkey::Pubkey, + signature::{Keypair, Signer}, +}; + +#[tokio::test] +async fn test_4_transfer2() { + // Initialize the test environment + let mut rpc = LightProgramTest::new(ProgramTestConfig::new_v2( + false, + Some(vec![("sdk_token_test", sdk_token_test::ID)]), + )) + .await + .unwrap(); + + let payer = rpc.get_payer().insecure_clone(); + + let (mint1_pda, mint2_pda, mint3_pda, token_account_1) = + create_compressed_mints_and_tokens(&mut rpc, &payer).await; + + println!("✅ Test setup complete: 3 compressed mints created with compressed tokens"); + + // Create compressed escrow PDA + let initial_amount = 100; // Initial escrow amount + let escrow_address = create_compressed_escrow_pda(&mut rpc, &payer, initial_amount) + .await + .unwrap(); + + println!( + "✅ Created compressed escrow PDA with address: {:?}", + escrow_address + ); + + // Test the four_transfer2 instruction + test_four_transfer2_instruction( + &mut rpc, + &payer, + mint1_pda, + mint2_pda, + mint3_pda, + escrow_address, + initial_amount, + token_account_1, + ) + .await + .unwrap(); + + println!("✅ Successfully executed four_transfer2 instruction"); +} + +async fn create_compressed_mints_and_tokens( + rpc: &mut LightProgramTest, + payer: &Keypair, +) -> (Pubkey, Pubkey, Pubkey, Pubkey) { + let decimals = 6u8; + let compress_amount = 1000; // Amount to mint as compressed tokens + + // Create 3 compressed mints + let (mint1_pda, mint1_pubkey) = create_compressed_mint_helper(rpc, payer, decimals).await; + let (mint2_pda, mint2_pubkey) = create_compressed_mint_helper(rpc, payer, decimals).await; + let (mint3_pda, mint3_pubkey) = create_compressed_mint_helper(rpc, payer, decimals).await; + + println!("Created compressed mint 1: {}", mint1_pubkey); + println!("Created compressed mint 2: {}", mint2_pubkey); + println!("Created compressed mint 3: {}", mint3_pubkey); + + // Mint compressed tokens for all three mints + mint_compressed_tokens(rpc, payer, &mint1_pda, mint1_pubkey, compress_amount).await; + mint_compressed_tokens(rpc, payer, &mint2_pda, mint2_pubkey, compress_amount).await; + mint_compressed_tokens(rpc, payer, &mint3_pda, mint3_pubkey, compress_amount).await; + + // Create associated token account for mint1 decompression + let (token_account1_pubkey, _bump) = + light_compressed_token_sdk::instructions::derive_ctoken_ata(&payer.pubkey(), &mint1_pda); + let create_ata_instruction = + light_compressed_token_sdk::instructions::create_associated_token_account( + payer.pubkey(), + payer.pubkey(), + mint1_pda, + ) + .unwrap(); + rpc.create_and_send_transaction(&[create_ata_instruction], &payer.pubkey(), &[payer]) + .await + .unwrap(); + + // Decompress some compressed tokens for mint1 into the associated token account + let decompress_amount = 500u64; + let compressed_token_accounts = rpc + .indexer() + .unwrap() + .get_compressed_token_accounts_by_owner(&payer.pubkey(), None, None) + .await + .unwrap() + .value + .items; + + let mint1_token_account = compressed_token_accounts + .iter() + .find(|acc| acc.token.mint == mint1_pda) + .expect("Compressed token account for mint1 should exist"); + + let decompress_instruction = + light_token_client::instructions::transfer2::create_decompress_instruction( + rpc, + std::slice::from_ref(mint1_token_account), + decompress_amount, + token_account1_pubkey, + payer.pubkey(), + ) + .await + .unwrap(); + + rpc.create_and_send_transaction(&[decompress_instruction], &payer.pubkey(), &[payer]) + .await + .unwrap(); + + println!( + "✅ Minted {} compressed tokens for all three mints and decompressed {} tokens for mint1", + compress_amount, decompress_amount + ); + + (mint1_pda, mint2_pda, mint3_pda, token_account1_pubkey) +} + +async fn create_compressed_mint_helper( + rpc: &mut LightProgramTest, + payer: &Keypair, + decimals: u8, +) -> (Pubkey, Pubkey) { + let mint_authority = payer.pubkey(); + let mint_signer = Keypair::new(); + let address_tree_pubkey = rpc.get_address_tree_v2().tree; + let output_queue = rpc.get_random_state_tree_info().unwrap().queue; + + // Find mint PDA + let compressed_token_program_id = + Pubkey::new_from_array(light_ctoken_types::COMPRESSED_TOKEN_PROGRAM_ID); + let (mint_pda, mint_bump) = Pubkey::find_program_address( + &[COMPRESSED_MINT_SEED, mint_signer.pubkey().as_ref()], + &compressed_token_program_id, + ); + + // Derive compressed mint address + let address_seed = mint_pda.to_bytes(); + let compressed_mint_address = light_compressed_account::address::derive_address( + &address_seed, + &address_tree_pubkey.to_bytes(), + &compressed_token_program_id.to_bytes(), + ); + + // Get validity proof + let rpc_result = rpc + .get_validity_proof( + vec![], + vec![AddressWithTree { + address: compressed_mint_address, + tree: address_tree_pubkey, + }], + None, + ) + .await + .unwrap() + .value; + + // Create compressed mint + let instruction = create_compressed_mint(CreateCompressedMintInputs { + version: 0, + decimals, + mint_authority, + freeze_authority: None, + proof: rpc_result.proof.0.unwrap(), + mint_bump, + address_merkle_tree_root_index: rpc_result.addresses[0].root_index, + mint_signer: mint_signer.pubkey(), + payer: payer.pubkey(), + address_tree_pubkey, + output_queue, + extensions: None, + }) + .unwrap(); + + rpc.create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer, &mint_signer]) + .await + .unwrap(); + + (mint_pda, compressed_mint_address.into()) +} + +async fn mint_compressed_tokens( + rpc: &mut LightProgramTest, + payer: &Keypair, + mint_pda: &Pubkey, + mint_pubkey: Pubkey, + amount: u64, +) { + let state_merkle_tree = rpc.get_random_state_tree_info().unwrap().tree; + let output_queue = rpc.get_random_state_tree_info().unwrap().queue; + + // Get the compressed mint account to use in the inputs + let compressed_mint_account = rpc + .indexer() + .unwrap() + .get_compressed_account(mint_pubkey.to_bytes(), None) + .await + .unwrap() + .value; + + // Create expected compressed mint for the input + let expected_compressed_mint = light_ctoken_types::state::CompressedMint { + spl_mint: mint_pda.into(), + supply: 0, + decimals: 6, + is_decompressed: false, + mint_authority: Some(payer.pubkey().into()), + freeze_authority: None, + version: 0, + extensions: None, + }; + + let mint_to_instruction = create_mint_to_compressed_instruction(MintToCompressedInputs { + compressed_mint_inputs: CompressedMintInputs { + prove_by_index: true, + leaf_index: compressed_mint_account.leaf_index, + root_index: 0, + address: compressed_mint_account.address.unwrap(), + compressed_mint_input: expected_compressed_mint, + }, + recipients: vec![Recipient { + recipient: payer.pubkey().into(), + amount, + }], + mint_authority: payer.pubkey(), + payer: payer.pubkey(), + state_merkle_tree, + output_queue, + state_tree_pubkey: state_merkle_tree, + decompressed_mint_config: None, + lamports: None, + }) + .unwrap(); + + rpc.create_and_send_transaction(&[mint_to_instruction], &payer.pubkey(), &[payer]) + .await + .unwrap(); +} + +async fn create_compressed_escrow_pda( + rpc: &mut LightProgramTest, + payer: &Keypair, + initial_amount: u64, +) -> Result<[u8; 32], RpcError> { + let tree_info = rpc.get_random_state_tree_info().unwrap(); + let mut remaining_accounts = PackedAccounts::default(); + remaining_accounts.add_pre_accounts_signer_mut(payer.pubkey()); + + // Add system accounts configuration + let config = SystemAccountMetaConfig::new(sdk_token_test::ID); + remaining_accounts.add_system_accounts(config).unwrap(); + + // Get address tree info and derive the PDA address + let address_tree_info = rpc.get_address_tree_v1(); + let (address, address_seed) = derive_address( + &[b"escrow", payer.pubkey().to_bytes().as_ref()], + &address_tree_info.tree, + &sdk_token_test::ID, + ); + + let output_tree_index = tree_info + .pack_output_tree_index(&mut remaining_accounts) + .unwrap(); + + // Get validity proof with address + let rpc_result = rpc + .get_validity_proof( + vec![], // No compressed accounts to prove + vec![AddressWithTree { + address, + tree: address_tree_info.tree, + }], + None, + ) + .await? + .value; + + let packed_tree_info = rpc_result.pack_tree_infos(&mut remaining_accounts); + let new_address_params = + packed_tree_info.address_trees[0].into_new_address_params_packed(address_seed); + + let (accounts, _, _) = remaining_accounts.to_account_metas(); + + let instruction = Instruction { + program_id: sdk_token_test::ID, + accounts, + data: sdk_token_test::instruction::CreateEscrowPda { + proof: rpc_result.proof, + output_tree_index, + amount: initial_amount, + address, + new_address_params, + } + .data(), + }; + + rpc.create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await?; + + Ok(address) +} + +#[allow(clippy::too_many_arguments)] +async fn test_four_transfer2_instruction( + rpc: &mut LightProgramTest, + payer: &Keypair, + mint1: Pubkey, + mint2: Pubkey, + mint3: Pubkey, + escrow_address: [u8; 32], + initial_escrow_amount: u64, + token_account_1: Pubkey, +) -> Result<(), RpcError> { + let default_pubkeys = CTokenDefaultAccounts::default(); + let mut remaining_accounts = PackedAccounts::default(); + let _token_pool_pda1 = get_token_pool_pda(&mint1); + // We don't need SPL token accounts for this test since we're using compressed tokens + // Just add the compressed token program and CPI authority PDA + // Remaining accounts 0 + remaining_accounts.add_pre_accounts_meta(AccountMeta::new( + default_pubkeys.compressed_token_program, + false, + )); + // Remaining accounts 1 + remaining_accounts + .add_pre_accounts_meta(AccountMeta::new(default_pubkeys.cpi_authority_pda, false)); + + // Add system accounts configuration with CPI context + let tree_info = rpc.get_random_state_tree_info().unwrap(); + + // Check if CPI context is available, otherwise this instruction can't work + if tree_info.cpi_context.is_none() { + panic!("CPI context account is required for four_transfer2 instruction but not available in tree_info"); + } + + let config = SystemAccountMetaConfig::new_with_cpi_context( + sdk_token_test::ID, + tree_info.cpi_context.unwrap(), + ); + remaining_accounts.add_system_accounts(config).unwrap(); + println!("next index {}", remaining_accounts.packed_pubkeys().len()); + + // Get validity proof - need to prove the escrow PDA and compressed token accounts + let escrow_account = rpc + .get_compressed_account(escrow_address, None) + .await? + .value; + + // Get compressed token accounts for mint2 and mint3 + let compressed_token_accounts = rpc + .indexer() + .unwrap() + .get_compressed_token_accounts_by_owner(&payer.pubkey(), None, None) + .await? + .value + .items; + + let mint2_token_account = compressed_token_accounts + .iter() + .find(|acc| acc.token.mint == mint2) + .expect("Compressed token account for mint2 should exist"); + + let mint3_token_account = compressed_token_accounts + .iter() + .find(|acc| acc.token.mint == mint3) + .expect("Compressed token account for mint3 should exist"); + + let rpc_result = rpc + .get_validity_proof( + vec![ + escrow_account.hash, + mint2_token_account.account.hash, + mint3_token_account.account.hash, + ], + vec![], + None, + ) + .await? + .value; + // We need to pack the tree after the cpi context. + remaining_accounts.insert_or_get(rpc_result.accounts[0].tree_info.tree); + + let packed_tree_info = rpc_result.pack_tree_infos(&mut remaining_accounts); + let output_tree_index = packed_tree_info + .state_trees + .as_ref() + .unwrap() + .output_tree_index; + + // Create token metas from compressed accounts - each uses its respective tree info index + // Index 0: escrow PDA, Index 1: mint2 token account, Index 2: mint3 token account + let mint2_tree_info = packed_tree_info + .state_trees + .as_ref() + .unwrap() + .packed_tree_infos[1]; + + let mint3_tree_info = packed_tree_info + .state_trees + .as_ref() + .unwrap() + .packed_tree_infos[2]; + + // Create FourTransfer2Params + let four_transfer2_params = sdk_token_test::process_four_transfer2::FourTransfer2Params { + compress_1: sdk_token_test::process_four_transfer2::CompressParams { + mint: remaining_accounts.insert_or_get(mint1), + amount: 500, + recipient: remaining_accounts.insert_or_get(payer.pubkey()), + solana_token_account: remaining_accounts.insert_or_get(token_account_1), + authority: remaining_accounts.insert_or_get(payer.pubkey()), // Payer is the authority for compression + }, + transfer_2: sdk_token_test::process_four_transfer2::TransferParams { + transfer_amount: 300, + token_metas: vec![pack_input_token_account( + mint2_token_account, + &mint2_tree_info, + &mut remaining_accounts, + &mut Vec::new(), + )], + recipient: remaining_accounts.insert_or_get(payer.pubkey()), + }, + transfer_3: sdk_token_test::process_four_transfer2::TransferParams { + transfer_amount: 200, + token_metas: vec![pack_input_token_account( + mint3_token_account, + &mint3_tree_info, + &mut remaining_accounts, + &mut Vec::new(), + )], + recipient: remaining_accounts.insert_or_get(payer.pubkey()), + }, + }; + + // Create PdaParams - escrow PDA uses tree info index 0 + let escrow_tree_info = packed_tree_info + .state_trees + .as_ref() + .unwrap() + .packed_tree_infos[0]; + + let pda_params = sdk_token_test::PdaParams { + account_meta: light_sdk::instruction::account_meta::CompressedAccountMeta { + address: escrow_address, + tree_info: escrow_tree_info, + output_state_tree_index: output_tree_index, + }, + existing_amount: initial_escrow_amount, + }; + + let (accounts, system_accounts_start_offset, tree_accounts_start_offset) = + remaining_accounts.to_account_metas(); + let packed_accounts_start_offset = tree_accounts_start_offset; + println!("accounts {:?}", accounts); + println!( + "system_accounts_start_offset {}", + system_accounts_start_offset + ); + println!( + "packed_accounts_start_offset {}", + packed_accounts_start_offset + ); + println!( + "accounts packed_accounts_start_offset {:?}", + accounts[packed_accounts_start_offset..].to_vec() + ); + + // We need to concat here to separate remaining accounts from the payer account. + let accounts = [vec![AccountMeta::new(payer.pubkey(), true)], accounts].concat(); + let instruction = Instruction { + program_id: sdk_token_test::ID, + accounts, + data: sdk_token_test::instruction::FourTransfer2 { + output_tree_index, + proof: rpc_result.proof, + system_accounts_start_offset: system_accounts_start_offset as u8, + packed_accounts_start_offset: tree_accounts_start_offset as u8, + four_transfer2_params, + pda_params, + } + .data(), + }; + // Print test setup values + println!("=== TEST SETUP VALUES ==="); + println!(" mint1_pda: {}", mint1); + println!(" mint2_pda: {}", mint2); + println!(" mint3_pda: {}", mint3); + println!(" token_account_1: {}", token_account_1); + println!(" escrow_address: {:?}", escrow_address); + println!(" initial_escrow_amount: {}", initial_escrow_amount); + println!(" payer: {}", payer.pubkey()); + + // Print all instruction accounts with names + println!("=== INSTRUCTION ACCOUNTS ==="); + for (i, account) in instruction.accounts.iter().enumerate() { + let name = match i { + 0 => "payer", + 1 => "compressed_token_program", + 2 => "cpi_authority_pda", + 3 => "system_program", + 4 => "light_system_program", + 5 => "account_compression_authority", + 6 => "noop_program", + 7 => "registered_program_pda", + 8 => "account_compression_program", + 9 => "self_program", + 10 => "sol_pool_pda", + i if i >= 11 && i < 11 + system_accounts_start_offset => &format!("tree_{}", i - 11), + _ => "remaining_account", + }; + println!(" {}: {} - {}", i, name, account.pubkey); + } + rpc.create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await?; + + Ok(()) +} + +fn pack_input_token_account( + account: &light_client::indexer::CompressedTokenAccount, + tree_info: &PackedStateTreeInfo, + packed_accounts: &mut PackedAccounts, + in_lamports: &mut Vec, +) -> MultiInputTokenDataWithContext { + let delegate_index = if let Some(delegate) = account.token.delegate { + packed_accounts.insert_or_get_read_only(delegate) // TODO: cover delegated transfer + } else { + 0 + }; + println!("account {:?}", account); + if account.account.lamports != 0 { + in_lamports.push(account.account.lamports); + } + MultiInputTokenDataWithContext { + amount: account.token.amount, + merkle_context: light_compressed_account::compressed_account::PackedMerkleContext { + merkle_tree_pubkey_index: tree_info.merkle_tree_pubkey_index, + queue_pubkey_index: tree_info.queue_pubkey_index, + leaf_index: tree_info.leaf_index, + prove_by_index: tree_info.prove_by_index, + }, + root_index: tree_info.root_index, + mint: packed_accounts.insert_or_get_read_only(account.token.mint), + owner: packed_accounts.insert_or_get_config(account.token.owner, true, false), + with_delegate: account.token.delegate.is_some(), + delegate: delegate_index, + version: 2, + } +} diff --git a/program-tests/sdk-token-test/tests/test_compress_full_and_close.rs b/program-tests/sdk-token-test/tests/test_compress_full_and_close.rs new file mode 100644 index 0000000000..9b67c11ae8 --- /dev/null +++ b/program-tests/sdk-token-test/tests/test_compress_full_and_close.rs @@ -0,0 +1,361 @@ +use anchor_lang::{ + prelude::{AccountMeta, Pubkey}, + InstructionData, +}; +use light_compressed_token_sdk::instructions::{ + create_associated_token_account, create_compressed_mint, create_mint_to_compressed_instruction, + derive_ctoken_ata, CreateCompressedMintInputs, MintToCompressedInputs, +}; +use light_ctoken_types::{ + instructions::mint_to_compressed::{CompressedMintInputs, Recipient}, + state::CompressedMint, + COMPRESSED_MINT_SEED, COMPRESSED_TOKEN_PROGRAM_ID, +}; +use light_program_test::{Indexer, LightProgramTest, ProgramTestConfig, Rpc}; +use light_sdk::instruction::{PackedAccounts, SystemAccountMetaConfig}; +use light_token_client::instructions::transfer2::create_decompress_instruction; +use sdk_token_test::instruction; +use serial_test::serial; +use solana_sdk::{ + instruction::Instruction, signature::Keypair, signer::Signer, transaction::Transaction, +}; + +#[tokio::test] +#[serial] +async fn test_compress_full_and_close() { + let mut rpc = LightProgramTest::new(ProgramTestConfig::new_v2( + false, + Some(vec![("sdk_token_test", sdk_token_test::ID)]), + )) + .await + .unwrap(); + let payer = rpc.get_payer().insecure_clone(); + + println!("🔧 Setting up compressed mint and tokens..."); + + // Step 1: Create a compressed mint + let decimals = 6u8; + let mint_authority_keypair = Keypair::new(); + let mint_authority = mint_authority_keypair.pubkey(); + let freeze_authority = Pubkey::new_unique(); + let mint_signer = Keypair::new(); + + let address_tree_pubkey = rpc.get_address_tree_v2().tree; + let output_queue = rpc.get_random_state_tree_info().unwrap().queue; + + let compressed_token_program_id = + Pubkey::new_from_array(light_ctoken_types::COMPRESSED_TOKEN_PROGRAM_ID); + let (mint_pda, mint_bump) = Pubkey::find_program_address( + &[COMPRESSED_MINT_SEED, mint_signer.pubkey().as_ref()], + &compressed_token_program_id, + ); + + let address_seed = mint_pda.to_bytes(); + let compressed_mint_address = light_compressed_account::address::derive_address( + &address_seed, + &address_tree_pubkey.to_bytes(), + &compressed_token_program_id.to_bytes(), + ); + + let rpc_result = rpc + .get_validity_proof( + vec![], + vec![light_program_test::AddressWithTree { + address: compressed_mint_address, + tree: address_tree_pubkey, + }], + None, + ) + .await + .unwrap() + .value; + + let address_merkle_tree_root_index = rpc_result.addresses[0].root_index; + + let instruction = create_compressed_mint(CreateCompressedMintInputs { + version: 0, + decimals, + mint_authority, + freeze_authority: Some(freeze_authority), + proof: rpc_result.proof.0.unwrap(), + mint_bump, + address_merkle_tree_root_index, + mint_signer: mint_signer.pubkey(), + payer: payer.pubkey(), + address_tree_pubkey, + output_queue, + extensions: None, + }) + .unwrap(); + + rpc.create_and_send_transaction(&[instruction], &payer.pubkey(), &[&payer, &mint_signer]) + .await + .unwrap(); + + println!("✅ Created compressed mint: {}", mint_pda); + + // Step 2: Mint compressed tokens + let mint_amount = 1000u64; + let recipient_keypair = Keypair::new(); + let recipient = recipient_keypair.pubkey(); + + let compressed_mint_account = rpc + .indexer() + .unwrap() + .get_compressed_account(compressed_mint_address, None) + .await + .unwrap() + .value; + + let expected_compressed_mint = CompressedMint { + spl_mint: mint_pda.into(), + supply: 0, + decimals, + is_decompressed: false, + mint_authority: Some(mint_authority.into()), + freeze_authority: Some(freeze_authority.into()), + version: 0, + extensions: None, + }; + + let state_tree_info = rpc.get_random_state_tree_info().unwrap(); + let state_tree_pubkey = state_tree_info.tree; + let state_output_queue = state_tree_info.queue; + + let compressed_mint_inputs = CompressedMintInputs { + prove_by_index: true, + leaf_index: compressed_mint_account.leaf_index, + root_index: 0, + address: compressed_mint_address, + compressed_mint_input: expected_compressed_mint, + }; + + let mint_instruction = create_mint_to_compressed_instruction(MintToCompressedInputs { + compressed_mint_inputs, + lamports: Some(10000u64), + recipients: vec![Recipient { + recipient: recipient.into(), + amount: mint_amount, + }], + mint_authority, + payer: payer.pubkey(), + state_merkle_tree: state_tree_pubkey, + output_queue: state_output_queue, + state_tree_pubkey, + decompressed_mint_config: None, + }) + .unwrap(); + + rpc.create_and_send_transaction( + &[mint_instruction], + &payer.pubkey(), + &[&payer, &mint_authority_keypair], + ) + .await + .unwrap(); + + println!("✅ Minted {} compressed tokens to recipient", mint_amount); + + // Step 4: Create associated token account for decompression + let (ctoken_ata_pubkey, _bump) = derive_ctoken_ata(&recipient, &mint_pda); + let create_ata_instruction = + create_associated_token_account(payer.pubkey(), recipient, mint_pda).unwrap(); + + rpc.create_and_send_transaction(&[create_ata_instruction], &payer.pubkey(), &[&payer]) + .await + .unwrap(); + + println!("✅ Created associated token account: {}", ctoken_ata_pubkey); + + // Step 5: Decompress compressed tokens to the token account + let decompress_amount = mint_amount; // Decompress all tokens + + let compressed_token_accounts = rpc + .indexer() + .unwrap() + .get_compressed_token_accounts_by_owner(&recipient, None, None) + .await + .unwrap() + .value + .items; + + assert_eq!( + compressed_token_accounts.len(), + 1, + "Should have one compressed token account" + ); + + let decompress_instruction = create_decompress_instruction( + &mut rpc, + std::slice::from_ref(&compressed_token_accounts[0]), + decompress_amount, + ctoken_ata_pubkey, + payer.pubkey(), + ) + .await + .unwrap(); + + rpc.create_and_send_transaction( + &[decompress_instruction], + &payer.pubkey(), + &[&payer, &recipient_keypair], + ) + .await + .unwrap(); + + println!( + "✅ Decompressed {} tokens to SPL token account", + decompress_amount + ); + + // Verify the token account has the expected balance by checking it exists and has data + let token_account_info = rpc.get_account(ctoken_ata_pubkey).await.unwrap().unwrap(); + assert!( + token_account_info.lamports > 0, + "Token account should exist with lamports" + ); + assert!( + !token_account_info.data.is_empty(), + "Token account should have data" + ); + + // Step 6: Now test our compress_full_and_close instruction + println!("🧪 Testing compress_full_and_close instruction..."); + + let final_recipient = Keypair::new(); + let final_recipient_pubkey = final_recipient.pubkey(); + let close_recipient = Keypair::new(); + let close_recipient_pubkey = close_recipient.pubkey(); + + // Airdrop lamports to close recipient + rpc.context + .airdrop(&close_recipient_pubkey, 1_000_000) + .unwrap(); + + // Create remaining accounts following four_multi_transfer pattern + let mut remaining_accounts = PackedAccounts::default(); + remaining_accounts.add_pre_accounts_meta(AccountMeta::new_readonly( + Pubkey::new_from_array(COMPRESSED_TOKEN_PROGRAM_ID), + false, + )); + remaining_accounts + .add_system_accounts(SystemAccountMetaConfig::new(Pubkey::new_from_array( + COMPRESSED_TOKEN_PROGRAM_ID, + ))) + .unwrap(); + let output_tree_index = + remaining_accounts.insert_or_get(rpc.get_random_state_tree_info().unwrap().queue); + // Pack accounts using insert_or_get (following four_multi_transfer pattern) + let recipient_index = remaining_accounts.insert_or_get(final_recipient_pubkey); + let mint_index = remaining_accounts.insert_or_get(mint_pda); + let source_index = remaining_accounts.insert_or_get(ctoken_ata_pubkey); // Token account to compress + let authority_index = remaining_accounts.insert_or_get(recipient_keypair.pubkey()); // Authority + let close_recipient_index = remaining_accounts.insert_or_get(close_recipient_pubkey); // Close recipient + + // Get remaining accounts and create instruction + let (account_metas, system_accounts_offset, _packed_accounts_offset) = + remaining_accounts.to_account_metas(); + + let instruction_data = instruction::CompressFullAndClose { + output_tree_index, + recipient_index, + mint_index, + source_index, + authority_index, + close_recipient_index, + system_accounts_offset: system_accounts_offset as u8, + }; + rpc.airdrop_lamports(&recipient_keypair.pubkey(), 1_000_000_000) + .await + .unwrap(); + // Prepend signer as first account (for Generic<'info> struct) + let accounts = [ + vec![solana_sdk::instruction::AccountMeta::new( + recipient_keypair.pubkey(), + true, + )], + account_metas, + ] + .concat(); + + let instruction = Instruction { + program_id: sdk_token_test::ID, + accounts, + data: instruction_data.data(), + }; + + println!("📤 Executing compress_full_and_close instruction..."); + + // Execute the instruction + let (blockhash, _) = rpc.get_latest_blockhash().await.unwrap(); + let transaction = Transaction::new_signed_with_payer( + &[instruction], + Some(&payer.pubkey()), + &[&payer, &recipient_keypair], + blockhash, + ); + + let result = rpc.process_transaction(transaction).await; + + match result { + Ok(_) => { + println!("✅ compress_full_and_close instruction executed successfully!"); + + // Verify the token account was closed + let closed_account = rpc.get_account(ctoken_ata_pubkey).await.unwrap(); + if let Some(account) = closed_account { + assert_eq!( + account.lamports, 0, + "Token account should have 0 lamports after closing" + ); + assert!( + account.data.iter().all(|&b| b == 0), + "Token account data should be cleared" + ); + } + + // Verify compressed tokens were created for the final recipient + let final_compressed_tokens = rpc + .indexer() + .unwrap() + .get_compressed_token_accounts_by_owner(&final_recipient_pubkey, None, None) + .await + .unwrap() + .value + .items; + + assert_eq!( + final_compressed_tokens.len(), + 1, + "Should have exactly one compressed token account for final recipient" + ); + + let final_compressed_token = &final_compressed_tokens[0].token; + assert_eq!( + final_compressed_token.amount, decompress_amount, + "Final compressed token should have the full original amount" + ); + assert_eq!( + final_compressed_token.owner, final_recipient_pubkey, + "Final compressed token should have correct owner" + ); + assert_eq!( + final_compressed_token.mint, mint_pda, + "Final compressed token should have correct mint" + ); + + println!("✅ All verifications passed!"); + println!(" - Original amount: {} tokens", mint_amount); + println!(" - Decompressed: {} tokens", decompress_amount); + println!( + " - Compressed full and closed: {} tokens", + final_compressed_token.amount + ); + println!(" - Token account closed successfully"); + println!(" - Lamports transferred to close recipient"); + } + Err(e) => { + panic!("❌ compress_full_and_close instruction failed: {:?}", e); + } + } +} diff --git a/program-tests/sdk-token-test/tests/test_deposit.rs b/program-tests/sdk-token-test/tests/test_deposit.rs new file mode 100644 index 0000000000..c594b625a4 --- /dev/null +++ b/program-tests/sdk-token-test/tests/test_deposit.rs @@ -0,0 +1,483 @@ +use anchor_lang::InstructionData; +use light_client::indexer::{CompressedAccount, CompressedTokenAccount, IndexerRpcConfig}; +use light_compressed_token_sdk::{ + instructions::{ + batch_compress::{ + get_batch_compress_instruction_account_metas, BatchCompressMetaConfig, Recipient, + }, + CTokenDefaultAccounts, + }, + token_pool::find_token_pool_pda_with_index, + TokenAccountMeta, SPL_TOKEN_PROGRAM_ID, +}; +use light_program_test::{AddressWithTree, Indexer, LightProgramTest, ProgramTestConfig, Rpc}; +use light_sdk::{ + address::v1::derive_address, + instruction::{account_meta::CompressedAccountMeta, PackedAccounts, SystemAccountMetaConfig}, +}; +use light_test_utils::{ + spl::{create_mint_helper, create_token_account, mint_spl_tokens}, + RpcError, +}; +use solana_sdk::{ + instruction::{AccountMeta, Instruction}, + pubkey::Pubkey, + signature::{Keypair, Signature, Signer}, +}; + +#[ignore = "fix cpi context usage"] +#[tokio::test] +async fn test_deposit_compressed_account() { + // Initialize the test environment + let mut rpc = LightProgramTest::new(ProgramTestConfig::new_v2( + false, + Some(vec![("sdk_token_test", sdk_token_test::ID)]), + )) + .await + .unwrap(); + + let payer = rpc.get_payer().insecure_clone(); + let deposit_amount = 1000u64; + + let recipients = vec![Recipient { + pubkey: payer.pubkey(), + amount: 100_000_000, + }]; + + // Execute batch compress (this will create mint, token account, and compress) + batch_compress_spl_tokens(&mut rpc, &payer, recipients.clone()) + .await + .unwrap(); + + println!("Batch compressed tokens successfully"); + + // Fetch the compressed token accounts created by batch compress + let recipient1 = recipients[0].pubkey; + let compressed_accounts = rpc + .indexer() + .unwrap() + .get_compressed_token_accounts_by_owner(&recipient1, None, None) + .await + .unwrap() + .value + .items; + + assert!( + !compressed_accounts.is_empty(), + "Should have compressed token accounts" + ); + let ctoken_account = &compressed_accounts[0]; + + println!( + "Found compressed token account: amount={}, owner={}", + ctoken_account.token.amount, ctoken_account.token.owner + ); + + // Derive the address that will be created for deposit + let address_tree_info = rpc.get_address_tree_v1(); + let (deposit_address, _) = derive_address( + &[b"escrow", payer.pubkey().to_bytes().as_ref()], + &address_tree_info.tree, + &sdk_token_test::ID, + ); + + // Derive recipient PDA from the deposit address + let (recipient_pda, recipient_bump) = + Pubkey::find_program_address(&[b"escrow", deposit_address.as_ref()], &sdk_token_test::ID); + println!("seeds: {:?}", b"escrow"); + println!("seeds: {:?}", deposit_address); + println!("recipient_bump: {:?}", recipient_bump); + // Create deposit instruction with the compressed token account + create_deposit_compressed_account( + &mut rpc, + &payer, + ctoken_account, + recipient_bump, + deposit_amount, + ) + .await + .unwrap(); + + println!("Created compressed account deposit successfully"); + + // Verify the compressed account was created at the expected address + let compressed_account = rpc + .get_compressed_account(deposit_address, None) + .await + .unwrap() + .value; + + println!("Created compressed account: {:?}", compressed_account); + + println!("Deposit compressed account test completed successfully!"); + + let slot = rpc.get_slot().await.unwrap(); + + let deposit_account = rpc + .get_compressed_token_accounts_by_owner( + &payer.pubkey(), + None, + Some(IndexerRpcConfig { + slot, + ..Default::default() + }), + ) + .await + .unwrap() + .value + .items[0] + .clone(); + let escrow_token_account = rpc + .get_compressed_token_accounts_by_owner(&recipient_pda, None, None) + .await + .unwrap() + .value + .items[0] + .clone(); + + update_deposit_compressed_account( + &mut rpc, + &payer, + &deposit_account, + &escrow_token_account, + compressed_account, + recipient_bump, + deposit_amount, + ) + .await + .unwrap(); +} + +async fn create_deposit_compressed_account( + rpc: &mut LightProgramTest, + payer: &Keypair, + ctoken_account: &CompressedTokenAccount, + recipient_bump: u8, + amount: u64, +) -> Result { + let tree_info = rpc.get_random_state_tree_info().unwrap(); + println!("tree_info {:?}", tree_info); + + let mut remaining_accounts = PackedAccounts::default(); + // new_with_anchor_none is only recommended for pinocchio else additional account infos cost approx 1k CU + // used here for consistentcy with into_account_infos_checked + // let config = TokenAccountsMetaConfig::new_client(); + // let metas = get_transfer_instruction_account_metas(config); + // remaining_accounts.add_pre_accounts_metas(metas); + // Alternative even though we pass fewer account infos this is minimally more efficient. + let default_pubkeys = CTokenDefaultAccounts::default(); + remaining_accounts.add_pre_accounts_meta(AccountMeta::new( + default_pubkeys.compressed_token_program, + false, + )); + remaining_accounts + .add_pre_accounts_meta(AccountMeta::new(default_pubkeys.cpi_authority_pda, false)); + + let config = SystemAccountMetaConfig::new_with_cpi_context( + sdk_token_test::ID, + tree_info.cpi_context.unwrap(), + ); + println!("cpi_context {:?}", config); + remaining_accounts.add_system_accounts(config).unwrap(); + let address_tree_info = rpc.get_address_tree_v1(); + + let (address, _) = derive_address( + &[b"escrow", payer.pubkey().to_bytes().as_ref()], + &address_tree_info.tree, + &sdk_token_test::ID, + ); + + // Get mint from the compressed token account + let mint = ctoken_account.token.mint; + println!( + "ctoken_account.account.hash {:?}", + ctoken_account.account.hash + ); + println!("ctoken_account.account {:?}", ctoken_account.account); + // Get validity proof for the compressed token account and new address + let rpc_result = rpc + .get_validity_proof( + vec![ctoken_account.account.hash], + vec![AddressWithTree { + address, + tree: address_tree_info.tree, + }], + None, + ) + .await? + .value; + let packed_accounts = rpc_result.pack_tree_infos(&mut remaining_accounts); + println!("packed_accounts {:?}", packed_accounts.state_trees); + + // Create token meta from compressed account + let tree_info = packed_accounts + .state_trees + .as_ref() + .unwrap() + .packed_tree_infos[0]; + + let token_metas = vec![TokenAccountMeta { + amount: ctoken_account.token.amount, + delegate_index: None, + packed_tree_info: tree_info, + lamports: None, + tlv: None, + }]; + + let (remaining_accounts, system_accounts_start_offset, _packed_accounts_start_offset) = + remaining_accounts.to_account_metas(); + let system_accounts_start_offset = system_accounts_start_offset as u8; + println!("remaining_accounts {:?}", remaining_accounts); + let instruction = Instruction { + program_id: sdk_token_test::ID, + accounts: [ + vec![AccountMeta::new(payer.pubkey(), true)], + remaining_accounts, + ] + .concat(), + data: sdk_token_test::instruction::Deposit { + proof: rpc_result.proof, + address_tree_info: packed_accounts.address_trees[0], + output_tree_index: packed_accounts.state_trees.unwrap().output_tree_index, + deposit_amount: amount, + token_metas, + mint, + recipient_bump, + system_accounts_start_offset, + } + .data(), + }; + + rpc.create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await +} + +async fn update_deposit_compressed_account( + rpc: &mut LightProgramTest, + payer: &Keypair, + deposit_ctoken_account: &CompressedTokenAccount, + escrow_ctoken_account: &CompressedTokenAccount, + escrow_pda: CompressedAccount, + recipient_bump: u8, + amount: u64, +) -> Result { + println!("deposit_ctoken_account {:?}", deposit_ctoken_account); + println!("escrow_ctoken_account {:?}", escrow_ctoken_account); + println!("escrow_pda {:?}", escrow_pda); + let rpc_result = rpc + .get_validity_proof( + vec![ + escrow_pda.hash, + deposit_ctoken_account.account.hash, + escrow_ctoken_account.account.hash, + ], + vec![], + None, + ) + .await? + .value; + let mut remaining_accounts = PackedAccounts::default(); + + let default_pubkeys = CTokenDefaultAccounts::default(); + remaining_accounts.add_pre_accounts_meta(AccountMeta::new( + default_pubkeys.compressed_token_program, + false, + )); + remaining_accounts + .add_pre_accounts_meta(AccountMeta::new(default_pubkeys.cpi_authority_pda, false)); + + let config = SystemAccountMetaConfig::new_with_cpi_context( + sdk_token_test::ID, + rpc_result.accounts[0].tree_info.cpi_context.unwrap(), + ); + println!("pre accounts {:?}", remaining_accounts.pre_accounts); + + println!("cpi_context {:?}", config); + remaining_accounts.add_system_accounts(config).unwrap(); + println!( + "rpc_result.accounts[0].tree_info.tree {:?}", + rpc_result.accounts[0].tree_info.tree.to_bytes() + ); + println!( + "rpc_result.accounts[0].tree_info.queue {:?}", + rpc_result.accounts[0].tree_info.queue.to_bytes() + ); + // We need to pack the tree after the cpi context. + let index = remaining_accounts.insert_or_get(rpc_result.accounts[0].tree_info.tree); + println!("index {}", index); + // Get mint from the compressed token account + let mint = deposit_ctoken_account.token.mint; + println!( + "ctoken_account.account.hash {:?}", + deposit_ctoken_account.account.hash + ); + println!( + "deposit_ctoken_account.account {:?}", + deposit_ctoken_account.account + ); + // Get validity proof for the compressed token account and new address + println!("rpc_result {:?}", rpc_result); + + let packed_accounts = rpc_result.pack_tree_infos(&mut remaining_accounts); + println!("packed_accounts {:?}", packed_accounts.state_trees); + // TODO: investigate why packed_tree_infos seem to be out of order + // Create token meta from compressed account + let tree_info = packed_accounts + .state_trees + .as_ref() + .unwrap() + .packed_tree_infos[1]; + let depositing_token_metas = vec![TokenAccountMeta { + amount: deposit_ctoken_account.token.amount, + delegate_index: None, + packed_tree_info: tree_info, + lamports: None, + tlv: None, + }]; + println!("depositing_token_metas {:?}", depositing_token_metas); + let tree_info = packed_accounts + .state_trees + .as_ref() + .unwrap() + .packed_tree_infos[2]; + let escrowed_token_meta = TokenAccountMeta { + amount: escrow_ctoken_account.token.amount, + delegate_index: None, + packed_tree_info: tree_info, + lamports: None, + tlv: None, + }; + println!("escrowed_token_meta {:?}", escrowed_token_meta); + + let (remaining_accounts, system_accounts_start_offset, _packed_accounts_start_offset) = + remaining_accounts.to_account_metas(); + let system_accounts_start_offset = system_accounts_start_offset as u8; + println!("remaining_accounts {:?}", remaining_accounts); + + let tree_info = packed_accounts + .state_trees + .as_ref() + .unwrap() + .packed_tree_infos[0]; + let account_meta = CompressedAccountMeta { + tree_info, + address: escrow_pda.address.unwrap(), + output_state_tree_index: packed_accounts + .state_trees + .as_ref() + .unwrap() + .output_tree_index, + }; + + let instruction = Instruction { + program_id: sdk_token_test::ID, + accounts: [ + vec![ + AccountMeta::new(payer.pubkey(), true), + AccountMeta::new_readonly(escrow_ctoken_account.token.owner, false), + ], + remaining_accounts, + ] + .concat(), + data: sdk_token_test::instruction::UpdateDeposit { + proof: rpc_result.proof, + output_tree_index: packed_accounts + .state_trees + .as_ref() + .unwrap() + .packed_tree_infos[0] + .merkle_tree_pubkey_index, + output_tree_queue_index: packed_accounts.state_trees.unwrap().packed_tree_infos[0] + .queue_pubkey_index, + system_accounts_start_offset, + token_params: sdk_token_test::TokenParams { + deposit_amount: amount, + depositing_token_metas, + mint, + escrowed_token_meta, + recipient_bump, + }, + pda_params: sdk_token_test::PdaParams { + account_meta, + existing_amount: amount, + }, + } + .data(), + }; + + rpc.create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await +} + +async fn batch_compress_spl_tokens( + rpc: &mut LightProgramTest, + payer: &Keypair, + recipients: Vec, +) -> Result { + // Create mint and token account + let mint = create_mint_helper(rpc, payer).await; + println!("Created mint: {}", mint); + + let token_account_keypair = Keypair::new(); + create_token_account(rpc, &mint, &token_account_keypair, payer) + .await + .unwrap(); + + println!("Created token account: {}", token_account_keypair.pubkey()); + + // Calculate total amount needed and mint tokens + let total_amount: u64 = recipients.iter().map(|r| r.amount).sum(); + let mint_amount = total_amount + 100_000; // Add some buffer + + mint_spl_tokens( + rpc, + &mint, + &token_account_keypair.pubkey(), + &payer.pubkey(), + payer, + mint_amount, + false, + ) + .await + .unwrap(); + + println!("Minted {} tokens to account", mint_amount); + + let token_account = token_account_keypair.pubkey(); + let mut remaining_accounts = PackedAccounts::default(); + remaining_accounts.add_pre_accounts_signer_mut(payer.pubkey()); + let token_pool_index = 0; + let (token_pool_pda, token_pool_bump) = find_token_pool_pda_with_index(&mint, token_pool_index); + println!("token_pool_pda {:?}", token_pool_pda); + + // Use batch compress account metas + let config = BatchCompressMetaConfig::new_client( + token_pool_pda, + token_account, + SPL_TOKEN_PROGRAM_ID.into(), + rpc.get_random_state_tree_info().unwrap().queue, + false, // with_lamports + ); + let metas = get_batch_compress_instruction_account_metas(config); + println!("metas {:?}", metas); + remaining_accounts.add_pre_accounts_metas(metas.as_slice()); + + let (accounts, _, _) = remaining_accounts.to_account_metas(); + println!("accounts {:?}", accounts); + + let instruction = Instruction { + program_id: sdk_token_test::ID, + accounts, + data: sdk_token_test::instruction::BatchCompressTokens { + recipients, + token_pool_index, + token_pool_bump, + } + .data(), + }; + + rpc.create_and_send_transaction(&[instruction], &payer.pubkey(), &[payer]) + .await?; + + Ok(mint) +} diff --git a/program-tests/utils/Cargo.toml b/program-tests/utils/Cargo.toml index 03e32cd812..bc32a73527 100644 --- a/program-tests/utils/Cargo.toml +++ b/program-tests/utils/Cargo.toml @@ -44,3 +44,7 @@ reqwest = { workspace = true } light-account-checks = { workspace = true } light-sparse-merkle-tree = { workspace = true } solana-banks-client = { workspace = true } +light-ctoken-types = { workspace = true } +light-compressed-token-sdk = { workspace = true } +light-token-client = { workspace = true } +light-zero-copy = { workspace = true } diff --git a/program-tests/utils/src/assert_close_token_account.rs b/program-tests/utils/src/assert_close_token_account.rs new file mode 100644 index 0000000000..d578927a56 --- /dev/null +++ b/program-tests/utils/src/assert_close_token_account.rs @@ -0,0 +1,115 @@ +use light_client::rpc::Rpc; +use light_ctoken_types::state::solana_ctoken::CompressedToken; +use light_zero_copy::borsh::Deserialize; +use solana_sdk::pubkey::Pubkey; + +/// Assert that a token account was closed correctly. +/// Verifies that the account has 0 lamports, cleared data, and lamports were transferred correctly. +/// If account_data_before_close is provided, validates compressible account closure. +pub async fn assert_close_token_account( + rpc: &mut R, + token_account_pubkey: Pubkey, + account_data_before_close: Option<&[u8]>, + destination_pubkey: Pubkey, + initial_destination_lamports: u64, +) { + // Verify the account was closed (data should be cleared, lamports should be 0) + let closed_account = rpc + .get_account(token_account_pubkey) + .await + .expect("Failed to get closed token account"); + + if let Some(account) = closed_account { + // Account still exists, but should have 0 lamports and cleared data + assert_eq!(account.lamports, 0, "Closed account should have 0 lamports"); + assert!( + account.data.iter().all(|&b| b == 0), + "Closed account data should be cleared" + ); + } + + // If account data is provided, validate compressible account closure + if let Some(account_data) = account_data_before_close { + // Try to deserialize as compressible token account + let (compressed_token, _) = CompressedToken::zero_copy_at(account_data) + .expect("Failed to deserialize compressible token account"); + + // Extract the compressible extension + let compressible_extension = compressed_token + .extensions + .as_ref() + .expect("Compressible account should have extensions") + .iter() + .find_map(|ext| match ext { + light_ctoken_types::state::extensions::ZExtensionStruct::Compressible(comp) => { + Some(comp) + } + _ => None, + }) + .expect("Should have compressible extension"); + + // Calculate rent exemption based on account data length + let rent_exemption = rpc + .get_minimum_balance_for_rent_exemption(account_data.len()) + .await + .expect("Failed to get rent exemption"); + + // Verify the destination matches the rent recipient from the extension + let expected_destination = Pubkey::from(compressible_extension.rent_recipient.to_bytes()); + assert_eq!( + destination_pubkey, expected_destination, + "Destination should match rent recipient from compressible extension" + ); + + // Verify compressible extension fields are valid + let current_slot = rpc.get_slot().await.expect("Failed to get current slot"); + assert!( + compressible_extension.last_written_slot <= current_slot, + "Last written slot ({}) should not be greater than current slot ({})", + compressible_extension.last_written_slot, + current_slot + ); + + // Verify slots_until_compression is a valid value (should be >= 0) + // Note: This is a u64 so it's always >= 0, but we can check it's reasonable + assert!( + compressible_extension.slots_until_compression < 1_000_000, // Reasonable upper bound + "Slots until compression ({}) should be a reasonable value", + compressible_extension.slots_until_compression + ); + + // Verify lamports were transferred to destination + let final_destination_lamports = rpc + .get_account(destination_pubkey) + .await + .expect("Failed to get destination account") + .expect("Destination account should exist") + .lamports; + + assert_eq!( + final_destination_lamports, + initial_destination_lamports + rent_exemption, + "Destination should receive rent exemption lamports from closed account" + ); + } else { + // Basic account closure - verify lamports were transferred to destination + let final_destination_lamports = rpc + .get_account(destination_pubkey) + .await + .expect("Failed to get destination account") + .expect("Destination account should exist") + .lamports; + + // Calculate rent exemption based on basic account size + let rent_exemption = rpc + .get_minimum_balance_for_rent_exemption(165) // Basic SPL token account size + .await + .expect("Failed to get rent exemption"); + + assert_eq!( + final_destination_lamports, + initial_destination_lamports + rent_exemption, + "Destination should receive rent exemption lamports from closed account" + ); + } +} diff --git a/program-tests/utils/src/assert_create_token_account.rs b/program-tests/utils/src/assert_create_token_account.rs new file mode 100644 index 0000000000..3c605c9aa5 --- /dev/null +++ b/program-tests/utils/src/assert_create_token_account.rs @@ -0,0 +1,127 @@ +use anchor_spl::token_2022::spl_token_2022; +use light_client::rpc::Rpc; +use light_compressed_token_sdk::instructions::create_associated_token_account::derive_ctoken_ata; +use light_ctoken_types::{ + state::{extensions::CompressibleExtension, solana_ctoken::CompressedToken}, + COMPRESSIBLE_TOKEN_ACCOUNT_SIZE, +}; +use light_zero_copy::borsh::Deserialize; +use solana_sdk::{program_pack::Pack, pubkey::Pubkey}; + +#[derive(Debug, Clone)] +pub struct CompressibleData { + pub rent_authority: Pubkey, + pub rent_recipient: Pubkey, + pub slots_until_compression: u64, +} + +/// Assert that a token account was created correctly. +/// If compressible_data is provided, validates compressible token account with extensions. +/// If compressible_data is None, validates basic SPL token account. +pub async fn assert_create_token_account( + rpc: &mut R, + token_account_pubkey: Pubkey, + mint_pubkey: Pubkey, + owner_pubkey: Pubkey, + compressible_data: Option, +) { + // Get the token account data + let account_info = rpc + .get_account(token_account_pubkey) + .await + .expect("Failed to get token account") + .expect("Token account should exist"); + + // Verify basic account properties + assert_eq!(account_info.owner, light_compressed_token::ID); + assert!(account_info.lamports > 0); + assert!(!account_info.executable); + + match compressible_data { + Some(compressible_info) => { + // Validate compressible token account + assert_eq!( + account_info.data.len(), + COMPRESSIBLE_TOKEN_ACCOUNT_SIZE as usize + ); + + // Use zero-copy deserialization for compressible account + let (actual_token_account, _) = CompressedToken::zero_copy_at(&account_info.data) + .expect("Failed to deserialize compressible token account with zero-copy"); + + // Get current slot for validation (program sets this to current slot) + let current_slot = rpc.get_slot().await.expect("Failed to get current slot"); + + // Create expected compressible token account + let expected_token_account = CompressedToken { + mint: mint_pubkey.into(), + owner: owner_pubkey.into(), + amount: 0, + delegate: None, + state: 1, // Initialized + is_native: None, + delegated_amount: 0, + close_authority: None, + extensions: Some(vec![ + light_ctoken_types::state::extensions::ExtensionStruct::Compressible( + CompressibleExtension { + last_written_slot: current_slot, + slots_until_compression: compressible_info.slots_until_compression, + rent_authority: compressible_info.rent_authority.into(), + rent_recipient: compressible_info.rent_recipient.into(), + }, + ), + ]), + }; + + assert_eq!(actual_token_account, expected_token_account); + } + None => { + // Validate basic SPL token account + assert_eq!(account_info.data.len(), 165); // SPL token account size + + // Use SPL token Pack trait for basic account + let actual_spl_token_account = + spl_token_2022::state::Account::unpack(&account_info.data) + .expect("Failed to unpack basic token account data"); + + // Create expected SPL token account + let expected_spl_token_account = spl_token_2022::state::Account { + mint: mint_pubkey, + owner: owner_pubkey, + amount: 0, + delegate: actual_spl_token_account.delegate, // Copy the actual COption value + state: spl_token_2022::state::AccountState::Initialized, + is_native: actual_spl_token_account.is_native, // Copy the actual COption value + delegated_amount: 0, + close_authority: actual_spl_token_account.close_authority, // Copy the actual COption value + }; + + assert_eq!(actual_spl_token_account, expected_spl_token_account); + } + } +} + +/// Assert that an associated token account was created correctly. +/// Automatically derives the ATA address from owner and mint. +/// If compressible_data is provided, validates compressible ATA with extensions. +/// If compressible_data is None, validates basic SPL ATA. +pub async fn assert_create_associated_token_account( + rpc: &mut R, + owner_pubkey: Pubkey, + mint_pubkey: Pubkey, + compressible_data: Option, +) { + // Derive the associated token account address + let (ata_pubkey, _bump) = derive_ctoken_ata(&owner_pubkey, &mint_pubkey); + + // Use the main assertion function + assert_create_token_account( + rpc, + ata_pubkey, + mint_pubkey, + owner_pubkey, + compressible_data, + ) + .await; +} diff --git a/program-tests/utils/src/assert_mint_to_compressed.rs b/program-tests/utils/src/assert_mint_to_compressed.rs new file mode 100644 index 0000000000..5c84cf0c7e --- /dev/null +++ b/program-tests/utils/src/assert_mint_to_compressed.rs @@ -0,0 +1,192 @@ +use anchor_lang::prelude::borsh::BorshDeserialize; +use anchor_spl::token_2022::spl_token_2022; +use light_client::{ + indexer::{CompressedTokenAccount, Indexer}, + rpc::Rpc, +}; +use light_compressed_token::instructions::create_token_pool::find_token_pool_pda_with_index; +use light_compressed_token_sdk::instructions::derive_compressed_mint_from_spl_mint; +use light_ctoken_types::{ + instructions::mint_to_compressed::Recipient, state::CompressedMint, COMPRESSED_TOKEN_PROGRAM_ID, +}; +use solana_sdk::{program_pack::Pack, pubkey::Pubkey}; + +pub async fn assert_mint_to_compressed( + rpc: &mut R, + spl_mint_pda: Pubkey, + recipients: &[Recipient], + expected_total_supply: u64, + pre_token_pool_account: Option, + pre_compressed_mint: CompressedMint, + pre_spl_mint: Option, +) -> Vec { + // Derive compressed mint address from SPL mint PDA (same as instruction) + let address_tree_pubkey = rpc.get_address_tree_v2().tree; + let compressed_mint_address = + derive_compressed_mint_from_spl_mint(&spl_mint_pda, &address_tree_pubkey); + // Verify each recipient received their tokens + let mut all_token_accounts = Vec::new(); + let mut total_minted = 0u64; + + for recipient in recipients { + let recipient_pubkey = Pubkey::from(recipient.recipient); + + // Get compressed token accounts for this recipient + let token_accounts = rpc + .get_compressed_token_accounts_by_owner(&recipient_pubkey, None, None) + .await + .expect("Failed to get compressed token accounts") + .value + .items; + + // Find the token account for this specific mint + let matching_account = token_accounts + .iter() + .find(|account| { + account.token.mint == spl_mint_pda && account.token.amount == recipient.amount + }) + .expect(&format!( + "Recipient {} should have a token account with {} tokens for mint {}", + recipient_pubkey, recipient.amount, spl_mint_pda + )); + + // Create expected token data + let expected_token_data = light_sdk::token::TokenData { + mint: spl_mint_pda, + owner: recipient_pubkey, + amount: recipient.amount, + delegate: None, + state: light_sdk::token::AccountState::Initialized, + tlv: None, + }; + + // Assert complete token account matches expected + assert_eq!( + matching_account.token, expected_token_data, + "Recipient token account should match expected" + ); + assert_eq!( + matching_account.account.owner.to_bytes(), + COMPRESSED_TOKEN_PROGRAM_ID, + "Recipient token account should have correct program owner" + ); + + // Add to total minted amount + total_minted += recipient.amount; + + // Collect all token accounts for return + all_token_accounts.extend(token_accounts); + } + + // Verify the compressed mint supply was updated correctly + let updated_compressed_mint_account = rpc + .get_compressed_account(compressed_mint_address, None) + .await + .expect("Failed to get compressed mint account") + .value; + + let actual_compressed_mint: CompressedMint = BorshDeserialize::deserialize( + &mut updated_compressed_mint_account + .data + .unwrap() + .data + .as_slice(), + ) + .expect("Failed to deserialize compressed mint"); + + // Create expected compressed mint by mutating the pre-mint + let mut expected_compressed_mint = pre_compressed_mint; + expected_compressed_mint.supply = expected_total_supply; + + assert_eq!( + actual_compressed_mint, expected_compressed_mint, + "Compressed mint should match expected state after mint" + ); + + // If mint is decompressed and pre_token_pool_account is provided, validate SPL mint and token pool + if actual_compressed_mint.is_decompressed { + if let Some(pre_pool_account) = pre_token_pool_account { + // Validate SPL mint supply + let spl_mint_data = rpc + .get_account(spl_mint_pda) + .await + .expect("Failed to get SPL mint account") + .expect("SPL mint should exist when decompressed"); + + let actual_spl_mint = spl_token_2022::state::Mint::unpack(&spl_mint_data.data) + .expect("Failed to unpack SPL mint data"); + + // Validate SPL mint using mutation pattern if pre_spl_mint is provided + if let Some(pre_spl_mint_account) = pre_spl_mint { + let mut expected_spl_mint = pre_spl_mint_account; + expected_spl_mint.supply = expected_total_supply; + + assert_eq!( + actual_spl_mint, expected_spl_mint, + "SPL mint should match expected state after mint" + ); + } else { + // Fallback validation if no pre_spl_mint provided + assert_eq!( + actual_spl_mint.supply, expected_total_supply, + "SPL mint supply should be updated to expected total supply when decompressed" + ); + } + + // Validate token pool balance increase + let (token_pool_pda, _) = find_token_pool_pda_with_index(&spl_mint_pda, 0); + let token_pool_data = rpc + .get_account(token_pool_pda) + .await + .expect("Failed to get token pool account") + .expect("Token pool should exist when decompressed"); + + let actual_token_pool = spl_token_2022::state::Account::unpack(&token_pool_data.data) + .expect("Failed to unpack token pool data"); + + // Create expected token pool account by mutating the pre-account + let mut expected_token_pool = pre_pool_account; + expected_token_pool.amount += total_minted; + + assert_eq!( + actual_token_pool, expected_token_pool, + "Token pool should match expected state after mint" + ); + } + } + + all_token_accounts +} + +pub async fn assert_mint_to_compressed_one( + rpc: &mut R, + spl_mint_pda: Pubkey, + recipient: Pubkey, + expected_amount: u64, + expected_total_supply: u64, + pre_token_pool_account: Option, + pre_compressed_mint: CompressedMint, + pre_spl_mint: Option, +) -> light_client::indexer::CompressedTokenAccount { + let recipients = vec![Recipient { + recipient: recipient.into(), + amount: expected_amount, + }]; + + let token_accounts = assert_mint_to_compressed( + rpc, + spl_mint_pda, + &recipients, + expected_total_supply, + pre_token_pool_account, + pre_compressed_mint, + pre_spl_mint, + ) + .await; + + // Return the first token account for the recipient + token_accounts + .into_iter() + .find(|account| account.token.owner == recipient && account.token.mint == spl_mint_pda) + .expect("Should find exactly one matching token account for the recipient") +} diff --git a/program-tests/utils/src/assert_rollover.rs b/program-tests/utils/src/assert_rollover.rs index 7fa91145bb..0043256a35 100644 --- a/program-tests/utils/src/assert_rollover.rs +++ b/program-tests/utils/src/assert_rollover.rs @@ -3,6 +3,7 @@ use light_concurrent_merkle_tree::ConcurrentMerkleTree; use light_hasher::Hasher; use light_merkle_tree_metadata::{merkle_tree::MerkleTreeMetadata, queue::QueueMetadata}; +#[track_caller] pub fn assert_rolledover_merkle_trees( old_merkle_tree: &ConcurrentMerkleTree, new_merkle_tree: &ConcurrentMerkleTree, diff --git a/program-tests/utils/src/assert_spl_mint.rs b/program-tests/utils/src/assert_spl_mint.rs new file mode 100644 index 0000000000..ddcbec355f --- /dev/null +++ b/program-tests/utils/src/assert_spl_mint.rs @@ -0,0 +1,97 @@ +use anchor_lang::prelude::borsh::BorshDeserialize; +use anchor_spl::token_2022::spl_token_2022; +use light_client::{indexer::Indexer, rpc::Rpc}; +use light_compressed_token::{ + instructions::create_token_pool::find_token_pool_pda_with_index, LIGHT_CPI_SIGNER, +}; +use light_compressed_token_sdk::instructions::{ + derive_compressed_mint_address, find_spl_mint_address, +}; +use light_ctoken_types::state::CompressedMint; +use solana_sdk::{program_pack::Pack, pubkey::Pubkey}; + +/// Assert that: +/// 1. compressed mint is marked as decompressed and didn't change otherwise +/// 2. spl mint is initialized and equivalent with the compressed mint +/// 3. if supply exists has been minted to the token pool +pub async fn assert_spl_mint( + rpc: &mut R, + seed: Pubkey, + pre_compressed_mint: &CompressedMint, +) { + // Derive all necessary addresses from the seed + let address_tree_pubkey = rpc.get_address_tree_v2().tree; + let compressed_mint_address = derive_compressed_mint_address(&seed, &address_tree_pubkey); + let (spl_mint_pda, _) = find_spl_mint_address(&seed); + + // Get the compressed mint data + let compressed_mint_account = rpc + .get_compressed_account(compressed_mint_address, None) + .await + .expect("Failed to get compressed mint account") + .value; + + let compressed_mint: CompressedMint = BorshDeserialize::deserialize( + &mut compressed_mint_account + .data + .as_ref() + .expect("Compressed mint should have data") + .data + .as_slice(), + ) + .expect("Failed to deserialize compressed mint"); + + let mut expected_compressed_mint = (*pre_compressed_mint).clone(); + expected_compressed_mint.is_decompressed = true; + assert_eq!(compressed_mint, expected_compressed_mint); + + // 2. Assert SPL mint is initialized and equivalent with compressed mint + { + let mint_account_data = rpc + .get_account(spl_mint_pda) + .await + .expect("Failed to get SPL mint account") + .expect("SPL mint account should exist"); + + let actual_spl_mint = spl_token_2022::state::Mint::unpack(&mint_account_data.data) + .expect("Failed to unpack SPL mint data"); + + // Create expected SPL mint struct + let expected_spl_mint = spl_token_2022::state::Mint { + mint_authority: actual_spl_mint.mint_authority, // Copy the actual COption value + supply: compressed_mint.supply, + decimals: compressed_mint.decimals, + is_initialized: true, + freeze_authority: actual_spl_mint.freeze_authority, // Copy the actual COption value + }; + + assert_eq!(actual_spl_mint, expected_spl_mint); + } + // 3. If supply > 0, assert token pool has the supply + if compressed_mint.supply > 0 { + let (token_pool_pda, _) = find_token_pool_pda_with_index(&spl_mint_pda, 0); + let token_pool_account_data = rpc + .get_account(token_pool_pda) + .await + .expect("Failed to get token pool account") + .expect("Token pool account should exist"); + + let actual_token_pool = + spl_token_2022::state::Account::unpack(&token_pool_account_data.data) + .expect("Failed to unpack token pool data"); + + // Create expected token pool struct + let expected_token_pool = spl_token_2022::state::Account { + mint: spl_mint_pda, + owner: LIGHT_CPI_SIGNER.cpi_signer.into(), + amount: compressed_mint.supply, + delegate: actual_token_pool.delegate, // Copy the actual COption value + state: spl_token_2022::state::AccountState::Initialized, + is_native: actual_token_pool.is_native, // Copy the actual COption value + delegated_amount: 0, + close_authority: actual_token_pool.close_authority, // Copy the actual COption value + }; + + assert_eq!(actual_token_pool, expected_token_pool); + } +} diff --git a/program-tests/utils/src/assert_transfer2.rs b/program-tests/utils/src/assert_transfer2.rs new file mode 100644 index 0000000000..a005f6c655 --- /dev/null +++ b/program-tests/utils/src/assert_transfer2.rs @@ -0,0 +1,265 @@ +use anchor_spl::token_2022::spl_token_2022; +use light_client::{indexer::Indexer, rpc::Rpc}; +use light_ctoken_types::COMPRESSED_TOKEN_PROGRAM_ID; +use light_token_client::instructions::transfer2::{ + CompressInput, DecompressInput, Transfer2InstructionType, TransferInput, +}; +use solana_sdk::program_pack::Pack; + +/// Comprehensive assertion for transfer2 operations that verifies all expected outcomes +/// based on the actions performed. This validates: +/// - Transfer recipients receive correct compressed token amounts +/// - Decompression creates correct SPL token amounts in target accounts +/// - Compression creates correct compressed tokens from SPL sources +pub async fn assert_transfer2( + rpc: &mut R, + actions: Vec>, + pre_token_accounts: Vec>, +) { + assert_eq!( + actions.len(), + pre_token_accounts.len(), + "Actions and pre_token_accounts must have same length" + ); + + for (action, pre_account) in actions.iter().zip(pre_token_accounts.iter()) { + match action { + Transfer2InstructionType::Transfer(transfer_input) => { + assert!( + pre_account.is_none(), + "Transfer actions should have None for pre_token_account" + ); + // Get recipient's compressed token accounts + let recipient_accounts = rpc + .indexer() + .unwrap() + .get_compressed_token_accounts_by_owner(&transfer_input.to, None, None) + .await + .unwrap() + .value + .items; + + // Get mint from the source compressed token account + let source_mint = transfer_input.compressed_token_account[0].token.mint; + let expected_recipient_token_data = light_sdk::token::TokenData { + mint: source_mint, + owner: transfer_input.to, + amount: transfer_input.amount, + delegate: None, + state: light_sdk::token::AccountState::Initialized, + tlv: None, + }; + + // Assert complete recipient token account + assert_eq!( + recipient_accounts[0].token, expected_recipient_token_data, + "Transfer recipient token account should match expected" + ); + assert_eq!( + recipient_accounts[0].account.owner.to_bytes(), + COMPRESSED_TOKEN_PROGRAM_ID, + "Transfer change token account should match expected" + ); + // Get change account owner from source account and calculate change amount + let source_owner = transfer_input.compressed_token_account[0].token.owner; + let source_amount = transfer_input.compressed_token_account[0].token.amount; + let change_amount = source_amount - transfer_input.amount; + + // Assert change account if there should be change + if change_amount > 0 { + let change_accounts = rpc + .indexer() + .unwrap() + .get_compressed_token_accounts_by_owner(&source_owner, None, None) + .await + .unwrap() + .value + .items; + + let expected_change_token = light_sdk::token::TokenData { + mint: source_mint, + owner: source_owner, + amount: change_amount, + delegate: None, + state: light_sdk::token::AccountState::Initialized, + tlv: None, + }; + + // Assert complete change token account + assert_eq!( + change_accounts[0].token, expected_change_token, + "Transfer change token account should match expected" + ); + assert_eq!( + change_accounts[0].account.owner.to_bytes(), + COMPRESSED_TOKEN_PROGRAM_ID, + "Transfer change token account should match expected" + ); + } + } + Transfer2InstructionType::Decompress(decompress_input) => { + let pre_spl_account = pre_account + .as_ref() + .ok_or("Decompress actions require pre_token_account") + .unwrap(); + // Verify SPL token account received tokens + let spl_account_data = rpc + .get_account(decompress_input.solana_token_account) + .await + .expect("Failed to get SPL token account") + .expect("SPL token account should exist"); + + let actual_spl_token_account = + spl_token_2022::state::Account::unpack(&spl_account_data.data) + .expect("Failed to unpack SPL token account"); + + // Get mint from the source compressed token account + let source_mint = decompress_input.compressed_token_account[0].token.mint; + let source_owner = decompress_input.compressed_token_account[0].token.owner; + + // Create expected SPL token account state + let mut expected_spl_token_account = *pre_spl_account; + expected_spl_token_account.amount += decompress_input.amount; + + // Assert complete SPL token account + assert_eq!( + actual_spl_token_account, expected_spl_token_account, + "Decompressed SPL token account should match expected state" + ); + + // Assert change compressed token account if there should be change + let source_amount = decompress_input.compressed_token_account[0].token.amount; + let change_amount = source_amount - decompress_input.amount; + + if change_amount > 0 { + let change_accounts = rpc + .indexer() + .unwrap() + .get_compressed_token_accounts_by_owner(&source_owner, None, None) + .await + .unwrap() + .value + .items; + + let expected_change_token = light_sdk::token::TokenData { + mint: source_mint, + owner: source_owner, + amount: change_amount, + delegate: None, + state: light_sdk::token::AccountState::Initialized, + tlv: None, + }; + + // Assert complete change token account + assert_eq!( + change_accounts[0].token, expected_change_token, + "Decompress change token account should match expected" + ); + assert_eq!( + change_accounts[0].account.owner.to_bytes(), + COMPRESSED_TOKEN_PROGRAM_ID, + "Decompress change token account should match expected" + ); + } + } + + Transfer2InstructionType::Compress(compress_input) => { + let pre_spl_account = pre_account + .as_ref() + .ok_or("Compress actions require pre_token_account") + .unwrap(); + // Verify recipient received compressed tokens + let recipient_accounts = rpc + .indexer() + .unwrap() + .get_compressed_token_accounts_by_owner(&compress_input.to, None, None) + .await + .unwrap() + .value + .items; + + let expected_recipient_token_data = light_sdk::token::TokenData { + mint: compress_input.mint, + owner: compress_input.to, + amount: compress_input.amount, + delegate: None, + state: light_sdk::token::AccountState::Initialized, + tlv: None, + }; + + // Assert complete recipient compressed token account + assert_eq!( + recipient_accounts[0].token, expected_recipient_token_data, + "Compress recipient token account should match expected" + ); + assert_eq!( + recipient_accounts[0].account.owner.to_bytes(), + COMPRESSED_TOKEN_PROGRAM_ID, + "Compress recipient token account should match expected" + ); + + // Verify SPL source account was reduced + let spl_account_data = rpc + .get_account(compress_input.solana_token_account) + .await + .expect("Failed to get SPL source account") + .expect("SPL source account should exist"); + + let actual_spl_token_account = + spl_token_2022::state::Account::unpack(&spl_account_data.data) + .expect("Failed to unpack SPL source account"); + + // Create expected SPL token account state (amount reduced by compression) + let mut expected_spl_token_account = *pre_spl_account; + expected_spl_token_account.amount -= compress_input.amount; + + // Assert complete SPL source account + assert_eq!( + actual_spl_token_account, expected_spl_token_account, + "Compress SPL source account should match expected state" + ); + } + } + } +} + +/// Assert transfer operation that transfers compressed tokens to a new recipient +pub async fn assert_transfer2_transfer( + rpc: &mut R, + transfer_input: TransferInput<'_>, +) { + assert_transfer2( + rpc, + vec![Transfer2InstructionType::Transfer(transfer_input)], + vec![None], + ) + .await; +} + +/// Assert decompress operation that converts compressed tokens to SPL tokens +pub async fn assert_transfer2_decompress( + rpc: &mut R, + decompress_input: DecompressInput<'_>, + pre_spl_token_account: spl_token_2022::state::Account, +) { + assert_transfer2( + rpc, + vec![Transfer2InstructionType::Decompress(decompress_input)], + vec![Some(pre_spl_token_account)], + ) + .await; +} + +/// Assert compress operation that converts SPL tokens to compressed tokens +pub async fn assert_transfer2_compress( + rpc: &mut R, + compress_input: CompressInput<'_>, + pre_spl_token_account: spl_token_2022::state::Account, +) { + assert_transfer2( + rpc, + vec![Transfer2InstructionType::Compress(compress_input)], + vec![Some(pre_spl_token_account)], + ) + .await; +} diff --git a/program-tests/utils/src/conversions.rs b/program-tests/utils/src/conversions.rs index 2891fd6713..4a606929ec 100644 --- a/program-tests/utils/src/conversions.rs +++ b/program-tests/utils/src/conversions.rs @@ -1,6 +1,5 @@ -use light_compressed_token::{ - token_data::AccountState as ProgramAccountState, TokenData as ProgramTokenData, -}; +use light_compressed_token::TokenData as ProgramTokenData; +use light_ctoken_types::state::AccountState as ProgramAccountState; use light_sdk::{self as sdk}; // pub fn sdk_to_program_merkle_context( @@ -104,10 +103,10 @@ pub fn program_to_sdk_account_state( pub fn sdk_to_program_token_data(sdk_token: sdk::token::TokenData) -> ProgramTokenData { ProgramTokenData { - mint: sdk_token.mint, - owner: sdk_token.owner, + mint: sdk_token.mint.into(), + owner: sdk_token.owner.into(), amount: sdk_token.amount, - delegate: sdk_token.delegate, + delegate: sdk_token.delegate.map(|d| d.into()), state: sdk_to_program_account_state(sdk_token.state), tlv: sdk_token.tlv, } @@ -115,10 +114,10 @@ pub fn sdk_to_program_token_data(sdk_token: sdk::token::TokenData) -> ProgramTok pub fn program_to_sdk_token_data(program_token: ProgramTokenData) -> sdk::token::TokenData { sdk::token::TokenData { - mint: program_token.mint, - owner: program_token.owner, + mint: program_token.mint.into(), + owner: program_token.owner.into(), amount: program_token.amount, - delegate: program_token.delegate, + delegate: program_token.delegate.map(|d| d.into()), state: program_to_sdk_account_state(program_token.state), tlv: program_token.tlv, } diff --git a/program-tests/utils/src/lib.rs b/program-tests/utils/src/lib.rs index 5a581bbb4b..fd5aafa8f0 100644 --- a/program-tests/utils/src/lib.rs +++ b/program-tests/utils/src/lib.rs @@ -18,16 +18,22 @@ use solana_sdk::{ }; pub mod address; pub mod address_tree_rollover; +pub mod assert_close_token_account; pub mod assert_compressed_tx; +pub mod assert_create_token_account; pub mod assert_epoch; pub mod assert_merkle_tree; +pub mod assert_mint_to_compressed; pub mod assert_queue; pub mod assert_rollover; +pub mod assert_spl_mint; pub mod assert_token_tx; +pub mod assert_transfer2; pub mod batched_address_tree; pub mod conversions; pub mod create_address_test_program_sdk; pub mod e2e_test_env; +pub mod mint_assert; pub mod mock_batched_forester; pub mod pack; pub mod registered_program_accounts_v1; diff --git a/program-tests/utils/src/mint_assert.rs b/program-tests/utils/src/mint_assert.rs new file mode 100644 index 0000000000..1cd4f9d294 --- /dev/null +++ b/program-tests/utils/src/mint_assert.rs @@ -0,0 +1,71 @@ +use anchor_lang::prelude::borsh::BorshDeserialize; +use light_ctoken_types::{ + instructions::extensions::TokenMetadataInstructionData, + state::{CompressedMint, ExtensionStruct}, +}; +use light_hasher::Poseidon; +use solana_sdk::pubkey::Pubkey; + +#[track_caller] +pub fn assert_compressed_mint_account( + compressed_mint_account: &light_client::indexer::CompressedAccount, + compressed_mint_address: [u8; 32], + spl_mint_pda: Pubkey, + decimals: u8, + mint_authority: Pubkey, + freeze_authority: Pubkey, + metadata: Option, +) -> CompressedMint { + // Create expected extensions if metadata is provided + let expected_extensions = metadata.map(|meta| { + vec![ExtensionStruct::TokenMetadata( + light_ctoken_types::state::extensions::TokenMetadata { + update_authority: meta.update_authority, + mint: spl_mint_pda.into(), + metadata: meta.metadata, + additional_metadata: meta.additional_metadata.unwrap_or_default(), + version: meta.version, + }, + )] + }); + + // Create expected compressed mint for comparison + let expected_compressed_mint = CompressedMint { + spl_mint: spl_mint_pda.into(), + supply: 0, + decimals, + is_decompressed: false, + mint_authority: Some(mint_authority.into()), + freeze_authority: Some(freeze_authority.into()), + version: 0, + extensions: expected_extensions, + }; + + // Verify the account exists and has correct properties + assert_eq!( + compressed_mint_account.address.unwrap(), + compressed_mint_address + ); + assert_eq!(compressed_mint_account.owner, light_compressed_token::ID); + assert_eq!(compressed_mint_account.lamports, 0); + + // Verify the compressed mint data + let compressed_account_data = compressed_mint_account.data.clone().unwrap(); + assert_eq!( + compressed_account_data.discriminator, + light_compressed_token::constants::COMPRESSED_MINT_DISCRIMINATOR + ); + + // Deserialize and verify the CompressedMint struct matches expected + let compressed_mint: CompressedMint = + BorshDeserialize::deserialize(&mut compressed_account_data.data.as_slice()).unwrap(); + println!("Compressed Mint: {:?}", compressed_mint); + assert_eq!(compressed_mint, expected_compressed_mint); + if let Some(extensions) = compressed_mint.extensions { + println!( + "Compressed Mint extension hash: {:?}", + extensions[0].hash::() + ); + } + expected_compressed_mint +} diff --git a/program-tests/utils/src/spl.rs b/program-tests/utils/src/spl.rs index 163f96757e..c1d80fd25f 100644 --- a/program-tests/utils/src/spl.rs +++ b/program-tests/utils/src/spl.rs @@ -24,9 +24,9 @@ use light_compressed_token::{ }, process_compress_spl_token_account::sdk::create_compress_spl_token_account_instruction, process_transfer::{transfer_sdk::create_transfer_instruction, TokenTransferOutputData}, - token_data::AccountState, TokenData, }; +use light_ctoken_types::state::AccountState; use light_hasher::Poseidon; use light_program_test::{indexer::TestIndexerExtensions, program_test::TestRpc}; use light_sdk::token::TokenDataWithMerkleContext; @@ -992,8 +992,8 @@ pub async fn perform_compress_spl_token_account(); let expected_token_data = TokenData { - mint, - owner: authority.pubkey(), + mint: mint.into(), + owner: authority.pubkey().into(), amount: input_amount, delegate: None, state: AccountState::Initialized, @@ -1556,10 +1556,10 @@ pub async fn freeze_or_thaw_test< AccountState::Initialized }; let expected_token_data = TokenData { - mint, - owner: input_compressed_accounts[0].token_data.owner, + mint: mint.into(), + owner: input_compressed_accounts[0].token_data.owner.into(), amount: account.token_data.amount, - delegate: account.token_data.delegate, + delegate: account.token_data.delegate.map(|d| d.into()), state, tlv: None, }; @@ -1689,15 +1689,15 @@ pub async fn burn_test 0 { let expected_token_data = TokenData { - mint, - owner: input_compressed_accounts[0].token_data.owner, + mint: mint.into(), + owner: input_compressed_accounts[0].token_data.owner.into(), amount: output_amount, - delegate, + delegate: delegate.map(|d| d.into()), state: AccountState::Initialized, tlv: None, }; if let Some(delegate) = expected_token_data.delegate { - delegates.push(Some(delegate)); + delegates.push(Some(delegate.into())); } else { delegates.push(None); } @@ -1854,7 +1854,7 @@ pub fn create_expected_token_output_data( expected_token_data.iter().zip(merkle_tree_pubkeys.iter()) { expected_compressed_output_accounts.push(TokenTransferOutputData { - owner: token_data.owner, + owner: token_data.owner.into(), amount: token_data.amount, merkle_tree: *merkle_tree_pubkey, lamports: None, diff --git a/programs/compressed-token/README.md b/programs/compressed-token/README.md index 764e509cdc..227bd71394 100644 --- a/programs/compressed-token/README.md +++ b/programs/compressed-token/README.md @@ -1,13 +1,2 @@ # Compressed Token Program - -A token program on the Solana blockchain using ZK Compression. - -This program provides an interface and implementation that third parties can utilize to create and use compressed tokens on Solana. - -Documentation is available at https://zkcompression.com - -Source code: https://github.com/Lightprotocol/light-protocol/tree/main/programs/compressed-token - -## Audit - -This code is unaudited. Use at your own risk. +- program wraps the anchor program and new optimized instructions diff --git a/programs/compressed-token/Cargo.toml b/programs/compressed-token/anchor/Cargo.toml similarity index 86% rename from programs/compressed-token/Cargo.toml rename to programs/compressed-token/anchor/Cargo.toml index 4c1604dcdf..9def469e8c 100644 --- a/programs/compressed-token/Cargo.toml +++ b/programs/compressed-token/anchor/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "light-compressed-token" +name = "anchor-compressed-token" version = "2.0.0" description = "Generalized token compression on Solana" repository = "https://github.com/Lightprotocol/light-protocol" @@ -8,7 +8,7 @@ edition = "2021" [lib] crate-type = ["cdylib", "lib"] -name = "light_compressed_token" +name = "anchor_compressed_token" [features] no-entrypoint = [] @@ -36,6 +36,7 @@ light-compressed-account = { workspace = true, features = ["anchor"] } spl-token-2022 = { workspace = true } light-zero-copy = { workspace = true } zerocopy = { workspace = true } +light-ctoken-types = { workspace = true, features = ["anchor"] } [target.'cfg(not(target_os = "solana"))'.dependencies] solana-sdk = { workspace = true } @@ -44,6 +45,10 @@ solana-sdk = { workspace = true } [dev-dependencies] rand = { workspace = true } num-bigint = { workspace = true } +light-compressed-account = { workspace = true, features = [ + "anchor", + "new-unique", +] } [lints.rust.unexpected_cfgs] level = "allow" diff --git a/programs/compressed-token/anchor/README.md b/programs/compressed-token/anchor/README.md new file mode 100644 index 0000000000..764e509cdc --- /dev/null +++ b/programs/compressed-token/anchor/README.md @@ -0,0 +1,13 @@ +# Compressed Token Program + +A token program on the Solana blockchain using ZK Compression. + +This program provides an interface and implementation that third parties can utilize to create and use compressed tokens on Solana. + +Documentation is available at https://zkcompression.com + +Source code: https://github.com/Lightprotocol/light-protocol/tree/main/programs/compressed-token + +## Audit + +This code is unaudited. Use at your own risk. diff --git a/programs/compressed-token/Xargo.toml b/programs/compressed-token/anchor/Xargo.toml similarity index 100% rename from programs/compressed-token/Xargo.toml rename to programs/compressed-token/anchor/Xargo.toml diff --git a/programs/compressed-token/src/batch_compress.rs b/programs/compressed-token/anchor/src/batch_compress.rs similarity index 100% rename from programs/compressed-token/src/batch_compress.rs rename to programs/compressed-token/anchor/src/batch_compress.rs diff --git a/programs/compressed-token/src/burn.rs b/programs/compressed-token/anchor/src/burn.rs similarity index 98% rename from programs/compressed-token/src/burn.rs rename to programs/compressed-token/anchor/src/burn.rs index 95a42ade34..ac02325bdf 100644 --- a/programs/compressed-token/src/burn.rs +++ b/programs/compressed-token/anchor/src/burn.rs @@ -206,7 +206,7 @@ pub mod sdk { }, DelegatedTransfer, }, - token_data::TokenData, + TokenData, }; pub struct CreateBurnInstructionInputs { @@ -249,7 +249,7 @@ pub mod sdk { }; let delegated_transfer = if inputs.signer_is_delegate { let delegated_transfer = DelegatedTransfer { - owner: inputs.input_token_data[0].owner, + owner: inputs.input_token_data[0].owner.into(), delegate_change_account_index: Some(0), }; Some(delegated_transfer) @@ -318,6 +318,7 @@ mod test { use account_compression::StateMerkleTreeAccount; use anchor_lang::{solana_program::account_info::AccountInfo, Discriminator}; use light_compressed_account::compressed_account::PackedMerkleContext; + use light_ctoken_types::state::AccountState; use rand::Rng; use super::*; @@ -326,7 +327,6 @@ mod test { create_expected_input_accounts, create_expected_token_output_accounts, get_rnd_input_token_data_with_contexts, }, - token_data::AccountState, TokenData, }; @@ -415,8 +415,8 @@ mod test { ); if change_amount != 0 { let expected_change_token_data = TokenData { - mint, - owner: authority, + mint: mint.into(), + owner: authority.into(), amount: change_amount, delegate: None, state: AccountState::Initialized, @@ -514,17 +514,16 @@ mod test { &authority, remaining_accounts .iter() - .map(|x| x.key) - .cloned() - .collect::>() + .map(|x| *x.key) + .collect::>() .as_slice(), ); assert_eq!(compressed_input_accounts, expected_input_accounts); assert_eq!(compressed_input_accounts.len(), num_inputs); assert_eq!(output_compressed_accounts.len(), 1); let expected_change_token_data = TokenData { - mint, - owner: authority, + mint: mint.into(), + owner: authority.into(), amount: sum_inputs - burn_amount, delegate: None, state: AccountState::Initialized, @@ -655,8 +654,8 @@ mod test { ); assert_eq!(compressed_input_accounts, expected_input_accounts); let expected_change_token_data = TokenData { - mint, - owner: invalid_authority, + mint: mint.into(), + owner: invalid_authority.into(), amount: 50, delegate: None, state: AccountState::Initialized, @@ -706,8 +705,8 @@ mod test { ); assert_eq!(compressed_input_accounts, expected_input_accounts); let expected_change_token_data = TokenData { - mint: invalid_mint, - owner: authority, + mint: invalid_mint.into(), + owner: authority.into(), amount: 50, delegate: None, state: AccountState::Initialized, diff --git a/programs/compressed-token/src/constants.rs b/programs/compressed-token/anchor/src/constants.rs similarity index 54% rename from programs/compressed-token/src/constants.rs rename to programs/compressed-token/anchor/src/constants.rs index 67b9ab70f8..68b25b41ae 100644 --- a/programs/compressed-token/src/constants.rs +++ b/programs/compressed-token/anchor/src/constants.rs @@ -1,5 +1,9 @@ +// 1 in little endian (for compressed mint accounts) +pub const COMPRESSED_MINT_DISCRIMINATOR: [u8; 8] = [1, 0, 0, 0, 0, 0, 0, 0]; // 2 in little endian pub const TOKEN_COMPRESSED_ACCOUNT_DISCRIMINATOR: [u8; 8] = [2, 0, 0, 0, 0, 0, 0, 0]; +// 3 in big endian (for V2 token accounts in batched trees) +pub const TOKEN_COMPRESSED_ACCOUNT_V2_DISCRIMINATOR: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 3]; pub const BUMP_CPI_AUTHORITY: u8 = 254; pub const NOT_FROZEN: bool = false; pub const POOL_SEED: &[u8] = b"pool"; diff --git a/programs/compressed-token/anchor/src/create_mint.rs b/programs/compressed-token/anchor/src/create_mint.rs new file mode 100644 index 0000000000..1e0675a6d3 --- /dev/null +++ b/programs/compressed-token/anchor/src/create_mint.rs @@ -0,0 +1,422 @@ +use anchor_lang::{ + prelude::{borsh, Pubkey}, + AnchorDeserialize, AnchorSerialize, +}; +use light_compressed_account::hash_to_bn254_field_size_be; +use light_hasher::{errors::HasherError, Hasher, Poseidon}; + +// TODO: add is native_compressed, this means that the compressed mint is always synced with the spl mint +// compressed mint accounts which are not native_compressed can be not in sync the spl mint account is the source of truth +// Order is optimized for hashing. +// freeze_authority option is skipped if None. +#[derive(Debug, PartialEq, Eq, AnchorSerialize, AnchorDeserialize, Clone)] +pub struct CompressedMint { + /// Pda with seed address of compressed mint + pub spl_mint: Pubkey, + /// Total supply of tokens. + pub supply: u64, + /// Number of base 10 digits to the right of the decimal place. + pub decimals: u8, + /// Extension, necessary for mint to. + pub is_decompressed: bool, + /// Optional authority used to mint new tokens. The mint authority may only + /// be provided during mint creation. If no mint authority is present + /// then the mint has a fixed supply and no further tokens may be + /// minted. + pub mint_authority: Option, + /// Optional authority to freeze token accounts. + pub freeze_authority: Option, + // Not necessary. + // /// Is `true` if this structure has been initialized + // pub is_initialized: bool, + pub num_extensions: u8, // TODO: check again how token22 does it +} + +impl CompressedMint { + pub fn hash(&self) -> std::result::Result<[u8; 32], HasherError> { + let hashed_spl_mint = hash_to_bn254_field_size_be(self.spl_mint.to_bytes().as_slice()); + let mut supply_bytes = [0u8; 32]; + supply_bytes[24..].copy_from_slice(self.supply.to_be_bytes().as_slice()); + + let hashed_mint_authority; + let hashed_mint_authority_option = if let Some(mint_authority) = self.mint_authority { + hashed_mint_authority = + hash_to_bn254_field_size_be(mint_authority.to_bytes().as_slice()); + Some(&hashed_mint_authority) + } else { + None + }; + + let hashed_freeze_authority; + let hashed_freeze_authority_option = if let Some(freeze_authority) = self.freeze_authority { + hashed_freeze_authority = + hash_to_bn254_field_size_be(freeze_authority.to_bytes().as_slice()); + Some(&hashed_freeze_authority) + } else { + None + }; + + Self::hash_with_hashed_values( + &hashed_spl_mint, + &supply_bytes, + self.decimals, + self.is_decompressed, + &hashed_mint_authority_option, + &hashed_freeze_authority_option, + self.num_extensions, + ) + } + + pub fn hash_with_hashed_values( + hashed_spl_mint: &[u8; 32], + supply_bytes: &[u8; 32], + decimals: u8, + is_decompressed: bool, + hashed_mint_authority: &Option<&[u8; 32]>, + hashed_freeze_authority: &Option<&[u8; 32]>, + num_extensions: u8, + ) -> std::result::Result<[u8; 32], HasherError> { + let mut hash_inputs = vec![hashed_spl_mint.as_slice(), supply_bytes.as_slice()]; + + // Add decimals with prefix if not 0 + let mut decimals_bytes = [0u8; 32]; + if decimals != 0 { + decimals_bytes[30] = 1; // decimals prefix + decimals_bytes[31] = decimals; + hash_inputs.push(&decimals_bytes[..]); + } + + // Add is_decompressed with prefix if true + let mut is_decompressed_bytes = [0u8; 32]; + if is_decompressed { + is_decompressed_bytes[30] = 2; // is_decompressed prefix + is_decompressed_bytes[31] = 1; // true as 1 + hash_inputs.push(&is_decompressed_bytes[..]); + } + + // Add mint authority if present + if let Some(hashed_mint_authority) = hashed_mint_authority { + hash_inputs.push(hashed_mint_authority.as_slice()); + } + + // Add freeze authority if present + let empty_authority = [0u8; 32]; + if let Some(hashed_freeze_authority) = hashed_freeze_authority { + // If there is freeze authority but no mint authority, add empty mint authority + if hashed_mint_authority.is_none() { + hash_inputs.push(&empty_authority[..]); + } + hash_inputs.push(hashed_freeze_authority.as_slice()); + } + + // Add num_extensions with prefix if not 0 + let mut num_extensions_bytes = [0u8; 32]; + if num_extensions != 0 { + num_extensions_bytes[30] = 3; // num_extensions prefix + num_extensions_bytes[31] = num_extensions; + hash_inputs.push(&num_extensions_bytes[..]); + } + + Poseidon::hashv(hash_inputs.as_slice()) + } +} + +#[cfg(test)] +pub mod test { + use rand::Rng; + + use super::*; + + #[test] + fn test_equivalency_of_hash_functions() { + let compressed_mint = CompressedMint { + spl_mint: Pubkey::new_unique(), + supply: 1000000, + decimals: 6, + is_decompressed: false, + mint_authority: Some(Pubkey::new_unique()), + freeze_authority: Some(Pubkey::new_unique()), + num_extensions: 2, + }; + + let hash_result = compressed_mint.hash().unwrap(); + + // Test with hashed values + let hashed_spl_mint = + hash_to_bn254_field_size_be(compressed_mint.spl_mint.to_bytes().as_slice()); + let mut supply_bytes = [0u8; 32]; + supply_bytes[24..].copy_from_slice(compressed_mint.supply.to_be_bytes().as_slice()); + + let hashed_mint_authority = hash_to_bn254_field_size_be( + compressed_mint + .mint_authority + .unwrap() + .to_bytes() + .as_slice(), + ); + let hashed_freeze_authority = hash_to_bn254_field_size_be( + compressed_mint + .freeze_authority + .unwrap() + .to_bytes() + .as_slice(), + ); + + let hash_with_hashed_values = CompressedMint::hash_with_hashed_values( + &hashed_spl_mint, + &supply_bytes, + compressed_mint.decimals, + compressed_mint.is_decompressed, + &Some(&hashed_mint_authority), + &Some(&hashed_freeze_authority), + compressed_mint.num_extensions, + ) + .unwrap(); + + assert_eq!(hash_result, hash_with_hashed_values); + } + + #[test] + fn test_equivalency_without_optional_fields() { + let compressed_mint = CompressedMint { + spl_mint: Pubkey::new_unique(), + supply: 500000, + decimals: 0, + is_decompressed: false, + mint_authority: None, + freeze_authority: None, + num_extensions: 0, + }; + + let hash_result = compressed_mint.hash().unwrap(); + + let hashed_spl_mint = + hash_to_bn254_field_size_be(compressed_mint.spl_mint.to_bytes().as_slice()); + let mut supply_bytes = [0u8; 32]; + supply_bytes[24..].copy_from_slice(compressed_mint.supply.to_be_bytes().as_slice()); + + let hash_with_hashed_values = CompressedMint::hash_with_hashed_values( + &hashed_spl_mint, + &supply_bytes, + compressed_mint.decimals, + compressed_mint.is_decompressed, + &None, + &None, + compressed_mint.num_extensions, + ) + .unwrap(); + + assert_eq!(hash_result, hash_with_hashed_values); + } + + fn equivalency_of_hash_functions_rnd_iters() { + let mut rng = rand::thread_rng(); + + for _ in 0..ITERS { + let compressed_mint = CompressedMint { + spl_mint: Pubkey::new_unique(), + supply: rng.gen(), + decimals: rng.gen_range(0..=18), + is_decompressed: rng.gen_bool(0.5), + mint_authority: if rng.gen_bool(0.5) { + Some(Pubkey::new_unique()) + } else { + None + }, + freeze_authority: if rng.gen_bool(0.5) { + Some(Pubkey::new_unique()) + } else { + None + }, + num_extensions: rng.gen_range(0..=10), + }; + + let hash_result = compressed_mint.hash().unwrap(); + + let hashed_spl_mint = + hash_to_bn254_field_size_be(compressed_mint.spl_mint.to_bytes().as_slice()); + let mut supply_bytes = [0u8; 32]; + supply_bytes[24..].copy_from_slice(compressed_mint.supply.to_be_bytes().as_slice()); + + let hashed_mint_authority; + let hashed_mint_authority_option = + if let Some(mint_authority) = compressed_mint.mint_authority { + hashed_mint_authority = + hash_to_bn254_field_size_be(mint_authority.to_bytes().as_slice()); + Some(&hashed_mint_authority) + } else { + None + }; + + let hashed_freeze_authority; + let hashed_freeze_authority_option = + if let Some(freeze_authority) = compressed_mint.freeze_authority { + hashed_freeze_authority = + hash_to_bn254_field_size_be(freeze_authority.to_bytes().as_slice()); + Some(&hashed_freeze_authority) + } else { + None + }; + + let hash_with_hashed_values = CompressedMint::hash_with_hashed_values( + &hashed_spl_mint, + &supply_bytes, + compressed_mint.decimals, + compressed_mint.is_decompressed, + &hashed_mint_authority_option, + &hashed_freeze_authority_option, + compressed_mint.num_extensions, + ) + .unwrap(); + + assert_eq!(hash_result, hash_with_hashed_values); + } + } + + #[test] + fn test_equivalency_random_iterations() { + equivalency_of_hash_functions_rnd_iters::<1000>(); + } + + #[test] + fn test_hash_collision_detection() { + let mut vec_previous_hashes = Vec::new(); + + // Base compressed mint + let base_mint = CompressedMint { + spl_mint: Pubkey::new_unique(), + supply: 1000000, + decimals: 6, + is_decompressed: false, + mint_authority: None, + freeze_authority: None, + num_extensions: 0, + }; + + let base_hash = base_mint.hash().unwrap(); + vec_previous_hashes.push(base_hash); + + // Different spl_mint + let mut mint1 = base_mint.clone(); + mint1.spl_mint = Pubkey::new_unique(); + let hash1 = mint1.hash().unwrap(); + assert_to_previous_hashes(hash1, &mut vec_previous_hashes); + + // Different supply + let mut mint2 = base_mint.clone(); + mint2.supply = 2000000; + let hash2 = mint2.hash().unwrap(); + assert_to_previous_hashes(hash2, &mut vec_previous_hashes); + + // Different decimals + let mut mint3 = base_mint.clone(); + mint3.decimals = 9; + let hash3 = mint3.hash().unwrap(); + assert_to_previous_hashes(hash3, &mut vec_previous_hashes); + + // Different is_decompressed + let mut mint4 = base_mint.clone(); + mint4.is_decompressed = true; + let hash4 = mint4.hash().unwrap(); + assert_to_previous_hashes(hash4, &mut vec_previous_hashes); + + // Different mint_authority + let mut mint5 = base_mint.clone(); + mint5.mint_authority = Some(Pubkey::new_unique()); + let hash5 = mint5.hash().unwrap(); + assert_to_previous_hashes(hash5, &mut vec_previous_hashes); + + // Different freeze_authority + let mut mint6 = base_mint.clone(); + mint6.freeze_authority = Some(Pubkey::new_unique()); + let hash6 = mint6.hash().unwrap(); + assert_to_previous_hashes(hash6, &mut vec_previous_hashes); + + // Different num_extensions + let mut mint7 = base_mint.clone(); + mint7.num_extensions = 5; + let hash7 = mint7.hash().unwrap(); + assert_to_previous_hashes(hash7, &mut vec_previous_hashes); + + // Multiple fields different + let mut mint8 = base_mint.clone(); + mint8.decimals = 18; + mint8.is_decompressed = true; + mint8.mint_authority = Some(Pubkey::new_unique()); + mint8.freeze_authority = Some(Pubkey::new_unique()); + mint8.num_extensions = 3; + let hash8 = mint8.hash().unwrap(); + assert_to_previous_hashes(hash8, &mut vec_previous_hashes); + } + + #[test] + fn test_authority_hash_collision_prevention() { + // This is a critical security test: ensuring that different authority combinations + // with the same pubkey don't produce the same hash + let same_pubkey = Pubkey::new_unique(); + + let base_mint = CompressedMint { + spl_mint: Pubkey::new_unique(), + supply: 1000000, + decimals: 6, + is_decompressed: false, + mint_authority: None, + freeze_authority: None, + num_extensions: 0, + }; + + // Case 1: None mint_authority, Some freeze_authority + let mut mint1 = base_mint.clone(); + mint1.mint_authority = None; + mint1.freeze_authority = Some(same_pubkey); + let hash1 = mint1.hash().unwrap(); + + // Case 2: Some mint_authority, None freeze_authority (using same pubkey) + let mut mint2 = base_mint.clone(); + mint2.mint_authority = Some(same_pubkey); + mint2.freeze_authority = None; + let hash2 = mint2.hash().unwrap(); + + // These must be different hashes to prevent authority confusion + assert_ne!( + hash1, hash2, + "CRITICAL: Hash collision between different authority configurations!" + ); + + // Case 3: Both authorities present (should also be different) + let mut mint3 = base_mint.clone(); + mint3.mint_authority = Some(same_pubkey); + mint3.freeze_authority = Some(same_pubkey); + let hash3 = mint3.hash().unwrap(); + + assert_ne!( + hash1, hash3, + "Hash collision between freeze-only and both authorities!" + ); + assert_ne!( + hash2, hash3, + "Hash collision between mint-only and both authorities!" + ); + + // Test with different pubkeys for good measure + let different_pubkey = Pubkey::new_unique(); + let mut mint4 = base_mint.clone(); + mint4.mint_authority = Some(same_pubkey); + mint4.freeze_authority = Some(different_pubkey); + let hash4 = mint4.hash().unwrap(); + + assert_ne!( + hash1, hash4, + "Hash collision with different freeze authority!" + ); + assert_ne!(hash2, hash4, "Hash collision with different authorities!"); + assert_ne!(hash3, hash4, "Hash collision with mixed authorities!"); + } + + fn assert_to_previous_hashes(hash: [u8; 32], previous_hashes: &mut Vec<[u8; 32]>) { + for previous_hash in previous_hashes.iter() { + assert_ne!(hash, *previous_hash, "Hash collision detected!"); + } + previous_hashes.push(hash); + } +} diff --git a/programs/compressed-token/src/delegation.rs b/programs/compressed-token/anchor/src/delegation.rs similarity index 98% rename from programs/compressed-token/src/delegation.rs rename to programs/compressed-token/anchor/src/delegation.rs index 99eea8eec4..7de055dc96 100644 --- a/programs/compressed-token/src/delegation.rs +++ b/programs/compressed-token/anchor/src/delegation.rs @@ -278,7 +278,7 @@ pub mod sdk { create_input_output_and_remaining_accounts, to_account_metas, TransferSdkError, }, }, - token_data::TokenData, + TokenData, }; pub struct CreateApproveInstructionInputs { @@ -450,12 +450,10 @@ mod test { use account_compression::StateMerkleTreeAccount; use anchor_lang::{solana_program::account_info::AccountInfo, Discriminator}; use light_compressed_account::compressed_account::PackedMerkleContext; + use light_ctoken_types::state::AccountState; use super::*; - use crate::{ - freeze::test_freeze::create_expected_token_output_accounts, token_data::AccountState, - TokenData, - }; + use crate::{freeze::test_freeze::create_expected_token_output_accounts, TokenData}; // TODO: add randomized and edge case tests #[test] @@ -549,18 +547,18 @@ mod test { assert_eq!(compressed_input_accounts.len(), 2); assert_eq!(output_compressed_accounts.len(), 2); let expected_change_token_data = TokenData { - mint, - owner: authority, + mint: mint.into(), + owner: authority.into(), amount: 151, delegate: None, state: AccountState::Initialized, tlv: None, }; let expected_delegated_token_data = TokenData { - mint, - owner: authority, + mint: mint.into(), + owner: authority.into(), amount: 50, - delegate: Some(delegate), + delegate: Some(delegate.into()), state: AccountState::Initialized, tlv: None, }; @@ -664,8 +662,8 @@ mod test { assert_eq!(compressed_input_accounts.len(), 2); assert_eq!(output_compressed_accounts.len(), 1); let expected_change_token_data = TokenData { - mint, - owner: authority, + mint: mint.into(), + owner: authority.into(), amount: 201, delegate: None, state: AccountState::Initialized, @@ -723,8 +721,8 @@ mod test { assert_eq!(compressed_input_accounts.len(), 2); assert_eq!(output_compressed_accounts.len(), 1); let expected_change_token_data = TokenData { - mint, - owner: authority, + mint: mint.into(), + owner: authority.into(), amount: 201, delegate: None, state: AccountState::Initialized, diff --git a/programs/compressed-token/src/freeze.rs b/programs/compressed-token/anchor/src/freeze.rs similarity index 92% rename from programs/compressed-token/src/freeze.rs rename to programs/compressed-token/anchor/src/freeze.rs index 68163fd6e9..65739f53c7 100644 --- a/programs/compressed-token/src/freeze.rs +++ b/programs/compressed-token/anchor/src/freeze.rs @@ -8,16 +8,15 @@ use light_compressed_account::{ data::OutputCompressedAccountWithPackedContext, with_readonly::InAccount, }, }; +use light_ctoken_types::state::AccountState; use crate::{ - constants::TOKEN_COMPRESSED_ACCOUNT_DISCRIMINATOR, process_transfer::{ add_data_hash_to_input_compressed_accounts, cpi_execute_compressed_transaction_transfer, get_input_compressed_accounts_with_merkle_context_and_check_signer, - InputTokenDataWithContext, BATCHED_DISCRIMINATOR, + get_token_account_discriminator, InputTokenDataWithContext, BATCHED_DISCRIMINATOR, }, - token_data::{AccountState, TokenData}, - FreezeInstruction, + FreezeInstruction, TokenData, }; #[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize)] @@ -156,10 +155,10 @@ fn create_token_output_accounts( }; // 1,000 CU token data and serialize let token_data = TokenData { - mint: *mint, - owner: *owner, + mint: (*mint).into(), + owner: (*owner).into(), amount: token_data_with_context.amount, - delegate, + delegate: delegate.map(|k| k.into()), state, tlv: None, }; @@ -174,12 +173,14 @@ fn create_token_output_accounts( let data_hash = match discriminator_bytes { StateMerkleTreeAccount::DISCRIMINATOR => token_data.hash_legacy(), BATCHED_DISCRIMINATOR => token_data.hash(), - _ => panic!(), + _ => panic!(), // TODO: throw error } .map_err(ProgramError::from)?; + let discriminator = get_token_account_discriminator(discriminator_bytes)?; + let data: CompressedAccountData = CompressedAccountData { - discriminator: TOKEN_COMPRESSED_ACCOUNT_DISCRIMINATOR, + discriminator, data: token_data_bytes, data_hash, }; @@ -220,7 +221,7 @@ pub mod sdk { process_transfer::transfer_sdk::{ create_input_output_and_remaining_accounts, to_account_metas, TransferSdkError, }, - token_data::TokenData, + TokenData, }; pub struct CreateInstructionInputs { @@ -254,7 +255,7 @@ pub mod sdk { input_token_data_with_context, cpi_context: None, outputs_merkle_tree_index: *outputs_merkle_tree_index as u8, - owner: inputs.input_token_data[0].owner, + owner: inputs.input_token_data[0].owner.into(), }; let remaining_accounts = to_account_metas(remaining_accounts); let mut serialized_ix_data = Vec::new(); @@ -291,7 +292,7 @@ pub mod sdk { account_compression_program: account_compression::ID, self_program: crate::ID, system_program: solana_sdk::system_program::ID, - mint: inputs.input_token_data[0].mint, + mint: inputs.input_token_data[0].mint.into(), }; Ok(Instruction { @@ -319,27 +320,26 @@ pub mod sdk { pub mod test_freeze { use account_compression::StateMerkleTreeAccount; use anchor_lang::{solana_program::account_info::AccountInfo, Discriminator}; - use light_compressed_account::compressed_account::PackedMerkleContext; + use light_compressed_account::{compressed_account::PackedMerkleContext, Pubkey}; + use light_ctoken_types::state::AccountState; use rand::Rng; use super::*; - use crate::{ - constants::TOKEN_COMPRESSED_ACCOUNT_DISCRIMINATOR, token_data::AccountState, TokenData, - }; + use crate::{constants::TOKEN_COMPRESSED_ACCOUNT_DISCRIMINATOR, TokenData}; // TODO: add randomized and edge case tests #[test] fn test_freeze() { - let merkle_tree_pubkey = Pubkey::new_unique(); + let merkle_tree_pubkey = anchor_lang::prelude::Pubkey::new_unique(); let mut merkle_tree_account_lamports = 0; let mut merkle_tree_account_data = StateMerkleTreeAccount::DISCRIMINATOR.to_vec(); - let nullifier_queue_pubkey = Pubkey::new_unique(); + let nullifier_queue_pubkey = anchor_lang::prelude::Pubkey::new_unique(); let mut nullifier_queue_account_lamports = 0; let mut nullifier_queue_account_data = Vec::new(); - let delegate = Pubkey::new_unique(); + let delegate = anchor_lang::prelude::Pubkey::new_unique(); let mut delegate_account_lamports = 0; let mut delegate_account_data = Vec::new(); - let merkle_tree_pubkey_1 = Pubkey::new_unique(); + let merkle_tree_pubkey_1 = anchor_lang::prelude::Pubkey::new_unique(); let mut merkle_tree_account_lamports_1 = 0; let mut merkle_tree_account_data_1 = StateMerkleTreeAccount::DISCRIMINATOR.to_vec(); let remaining_accounts = vec![ @@ -421,7 +421,7 @@ pub mod test_freeze { { let inputs = CompressedTokenInstructionDataFreeze { proof: CompressedProof::default(), - owner, + owner: owner.into(), input_token_data_with_context: input_token_data_with_context.clone(), cpi_context: None, outputs_merkle_tree_index: 3, @@ -429,7 +429,7 @@ pub mod test_freeze { let (compressed_input_accounts, output_compressed_accounts) = create_input_and_output_accounts_freeze_or_thaw::( &inputs, - &mint, + &mint.into(), &remaining_accounts, ) .unwrap(); @@ -447,7 +447,7 @@ pub mod test_freeze { mint, owner, amount: 101, - delegate: Some(delegate), + delegate: Some(delegate.into()), state: AccountState::Frozen, tlv: None, }; @@ -465,7 +465,7 @@ pub mod test_freeze { { let inputs = CompressedTokenInstructionDataFreeze { proof: CompressedProof::default(), - owner, + owner: owner.into(), input_token_data_with_context, cpi_context: None, outputs_merkle_tree_index: 3, @@ -473,7 +473,7 @@ pub mod test_freeze { let (compressed_input_accounts, output_compressed_accounts) = create_input_and_output_accounts_freeze_or_thaw::( &inputs, - &mint, + &mint.into(), &remaining_accounts, ) .unwrap(); @@ -491,7 +491,7 @@ pub mod test_freeze { mint, owner, amount: 101, - delegate: Some(delegate), + delegate: Some(delegate.into()), state: AccountState::Initialized, tlv: None, }; @@ -558,9 +558,9 @@ pub mod test_freeze { } pub fn create_expected_input_accounts( input_token_data_with_context: &[InputTokenDataWithContext], - mint: &Pubkey, - owner: &Pubkey, - remaining_accounts: &[Pubkey], + mint: &anchor_lang::prelude::Pubkey, + owner: &anchor_lang::prelude::Pubkey, + remaining_accounts: &[anchor_lang::prelude::Pubkey], ) -> Vec { input_token_data_with_context .iter() @@ -569,10 +569,10 @@ pub mod test_freeze { .delegate_index .map(|index| remaining_accounts[index as usize]); let token_data = TokenData { - mint: *mint, - owner: *owner, + mint: mint.into(), + owner: owner.into(), amount: x.amount, - delegate, + delegate: delegate.map(|d| d.into()), state: AccountState::Initialized, tlv: None, }; diff --git a/programs/compressed-token/src/instructions/burn.rs b/programs/compressed-token/anchor/src/instructions/burn.rs similarity index 100% rename from programs/compressed-token/src/instructions/burn.rs rename to programs/compressed-token/anchor/src/instructions/burn.rs diff --git a/programs/compressed-token/anchor/src/instructions/create_compressed_mint.rs b/programs/compressed-token/anchor/src/instructions/create_compressed_mint.rs new file mode 100644 index 0000000000..582ac1905c --- /dev/null +++ b/programs/compressed-token/anchor/src/instructions/create_compressed_mint.rs @@ -0,0 +1,48 @@ +use account_compression::program::AccountCompression; +use anchor_lang::prelude::*; +use light_system_program::program::LightSystemProgram; + +use crate::program::LightCompressedToken; + +/// Creates a compressed mint stored as a compressed account +#[derive(Accounts)] +pub struct CreateCompressedMintInstruction<'info> { + #[account(mut)] + pub fee_payer: Signer<'info>, + + /// CPI authority for compressed account creation + pub cpi_authority_pda: AccountInfo<'info>, + + /// Light system program for compressed account creation + pub light_system_program: Program<'info, LightSystemProgram>, + + /// Account compression program + pub account_compression_program: Program<'info, AccountCompression>, + + /// Registered program PDA for light system program + pub registered_program_pda: AccountInfo<'info>, + + /// NoOp program for event emission + pub noop_program: AccountInfo<'info>, + + /// Authority for account compression + pub account_compression_authority: AccountInfo<'info>, + + /// Self program reference + pub self_program: Program<'info, LightCompressedToken>, + + pub system_program: Program<'info, System>, + + /// Address merkle tree for compressed account creation + /// CHECK: Validated by light-system-program + #[account(mut)] + pub address_merkle_tree: AccountInfo<'info>, + + /// Output queue account where compressed mint will be stored + /// CHECK: Validated by light-system-program + #[account(mut)] + pub output_queue: AccountInfo<'info>, + + /// Signer used as seed for PDA derivation (ensures uniqueness) + pub mint_signer: Signer<'info>, +} diff --git a/programs/compressed-token/src/instructions/create_token_pool.rs b/programs/compressed-token/anchor/src/instructions/create_token_pool.rs similarity index 100% rename from programs/compressed-token/src/instructions/create_token_pool.rs rename to programs/compressed-token/anchor/src/instructions/create_token_pool.rs diff --git a/programs/compressed-token/src/instructions/freeze.rs b/programs/compressed-token/anchor/src/instructions/freeze.rs similarity index 100% rename from programs/compressed-token/src/instructions/freeze.rs rename to programs/compressed-token/anchor/src/instructions/freeze.rs diff --git a/programs/compressed-token/src/instructions/generic.rs b/programs/compressed-token/anchor/src/instructions/generic.rs similarity index 100% rename from programs/compressed-token/src/instructions/generic.rs rename to programs/compressed-token/anchor/src/instructions/generic.rs diff --git a/programs/compressed-token/src/instructions/mod.rs b/programs/compressed-token/anchor/src/instructions/mod.rs similarity index 74% rename from programs/compressed-token/src/instructions/mod.rs rename to programs/compressed-token/anchor/src/instructions/mod.rs index c934aac35a..b27b424afa 100644 --- a/programs/compressed-token/src/instructions/mod.rs +++ b/programs/compressed-token/anchor/src/instructions/mod.rs @@ -1,10 +1,12 @@ pub mod burn; +pub mod create_compressed_mint; pub mod create_token_pool; pub mod freeze; pub mod generic; pub mod transfer; pub use burn::*; +pub use create_compressed_mint::*; pub use create_token_pool::*; pub use freeze::*; pub use generic::*; diff --git a/programs/compressed-token/src/instructions/transfer.rs b/programs/compressed-token/anchor/src/instructions/transfer.rs similarity index 100% rename from programs/compressed-token/src/instructions/transfer.rs rename to programs/compressed-token/anchor/src/instructions/transfer.rs diff --git a/programs/compressed-token/src/lib.rs b/programs/compressed-token/anchor/src/lib.rs similarity index 96% rename from programs/compressed-token/src/lib.rs rename to programs/compressed-token/anchor/src/lib.rs index 50dc3c1301..99c17e17fd 100644 --- a/programs/compressed-token/src/lib.rs +++ b/programs/compressed-token/anchor/src/lib.rs @@ -6,9 +6,8 @@ pub mod process_mint; pub mod process_transfer; use process_compress_spl_token_account::process_compress_spl_token_account; pub mod spl_compression; +pub use light_ctoken_types::state::TokenData; pub use process_mint::*; -pub mod token_data; -pub use token_data::TokenData; pub mod delegation; pub mod freeze; pub mod instructions; @@ -16,6 +15,7 @@ pub use instructions::*; pub mod burn; pub use burn::*; pub mod batch_compress; +pub mod create_mint; use light_compressed_account::instruction_data::cpi_context::CompressedCpiContext; use crate::process_transfer::CompressedTokenInstructionDataTransfer; @@ -46,7 +46,7 @@ pub mod light_compressed_token { pub fn create_token_pool<'info>( ctx: Context<'_, '_, '_, 'info, CreateTokenPoolInstruction<'info>>, ) -> Result<()> { - create_token_pool::assert_mint_extensions( + instructions::create_token_pool::assert_mint_extensions( &ctx.accounts.mint.to_account_info().try_borrow_data()?, ) } @@ -280,13 +280,21 @@ pub enum ErrorCode { NoMatchingBumpFound, NoAmount, AmountsAndAmountProvided, + MintIsNone, + InvalidMintPda, + InputsOutOfOrder, + TooManyMints, + InvalidExtensionType, #[msg("Cpi context set and set first is not usable with burn, compression(transfer ix) or decompress(transfer).")] CpiContextSetNotUsable, + InstructionDataExpectedDelegate, + ZeroCopyExpectedDelegate, + TokenDataTlvUnimplemented, } /// Checks if CPI context usage is valid for the current instruction /// Throws an error if cpi_context is Some and (set_context OR first_set_context is true) -fn check_cpi_context(cpi_context: &Option) -> Result<()> { +pub fn check_cpi_context(cpi_context: &Option) -> Result<()> { if let Some(ctx) = cpi_context { if ctx.set_context || ctx.first_set_context { return Err(ErrorCode::CpiContextSetNotUsable.into()); diff --git a/programs/compressed-token/src/process_compress_spl_token_account.rs b/programs/compressed-token/anchor/src/process_compress_spl_token_account.rs similarity index 100% rename from programs/compressed-token/src/process_compress_spl_token_account.rs rename to programs/compressed-token/anchor/src/process_compress_spl_token_account.rs diff --git a/programs/compressed-token/src/process_mint.rs b/programs/compressed-token/anchor/src/process_mint.rs similarity index 58% rename from programs/compressed-token/src/process_mint.rs rename to programs/compressed-token/anchor/src/process_mint.rs index 719eeda736..0c7384f0b5 100644 --- a/programs/compressed-token/src/process_mint.rs +++ b/programs/compressed-token/anchor/src/process_mint.rs @@ -2,7 +2,11 @@ use account_compression::program::AccountCompression; use anchor_lang::prelude::*; use anchor_spl::token_interface::{TokenAccount, TokenInterface}; use light_compressed_account::{ - instruction_data::data::OutputCompressedAccountWithPackedContext, pubkey::AsPubkey, + compressed_account::PackedCompressedAccountWithMerkleContext, + instruction_data::{ + compressed_proof::CompressedProof, data::OutputCompressedAccountWithPackedContext, + }, + pubkey::AsPubkey, }; use light_system_program::program::LightSystemProgram; use light_zero_copy::num_trait::ZeroCopyNumTrait; @@ -10,8 +14,8 @@ use light_zero_copy::num_trait::ZeroCopyNumTrait; use { crate::{ check_spl_token_pool_derivation_with_index, - process_transfer::create_output_compressed_accounts, - process_transfer::get_cpi_signer_seeds, spl_compression::spl_token_transfer, + process_transfer::{create_output_compressed_accounts, get_cpi_signer_seeds}, + spl_compression::spl_token_transfer, }, light_compressed_account::hash_to_bn254_field_size_be, light_heap::{bench_sbf_end, bench_sbf_start, GLOBAL_ALLOCATOR}, @@ -58,6 +62,7 @@ pub fn process_mint_to_or_compress<'info, const IS_MINT_TO: bool>( #[cfg(target_os = "solana")] { let option_compression_lamports = if lamports.unwrap_or(0) == 0 { 0 } else { 8 }; + let inputs_len = 1 + 4 + 4 + 4 + amounts.len() * 162 + 1 + 1 + 1 + 1 + option_compression_lamports; // inputs_len = @@ -75,11 +80,15 @@ pub fn process_mint_to_or_compress<'info, const IS_MINT_TO: bool>( let pre_compressed_acounts_pos = GLOBAL_ALLOCATOR.get_heap_pos(); bench_sbf_start!("tm_mint_spl_to_pool_pda"); - let mint = if IS_MINT_TO { - // 7,978 CU + let (mint, compressed_mint_update_data) = if IS_MINT_TO { + // EXISTING SPL MINT PATH mint_spl_to_pool_pda(&ctx, &amounts)?; - ctx.accounts.mint.as_ref().unwrap().key() + ( + ctx.accounts.mint.as_ref().unwrap().key(), + None::, + ) } else { + // EXISTING BATCH COMPRESS PATH let mut amount = 0u64; for a in amounts { amount += (*a).into(); @@ -103,7 +112,7 @@ pub fn process_mint_to_or_compress<'info, const IS_MINT_TO: bool>( ctx.accounts.token_program.to_account_info(), amount, )?; - mint + (mint, None) }; let hashed_mint = hash_to_bn254_field_size_be(mint.as_ref()); @@ -126,10 +135,15 @@ pub fn process_mint_to_or_compress<'info, const IS_MINT_TO: bool>( )?; bench_sbf_end!("tm_output_compressed_accounts"); - cpi_execute_compressed_transaction_mint_to( + // Create compressed mint update data if needed + let (input_compressed_accounts, proof) = (vec![], None); + // Execute single CPI call with updated serialization + cpi_execute_compressed_transaction_mint_to::( &ctx, + input_compressed_accounts.as_slice(), output_compressed_accounts, &mut inputs, + proof, pre_compressed_acounts_pos, )?; @@ -147,12 +161,123 @@ pub fn process_mint_to_or_compress<'info, const IS_MINT_TO: bool>( Ok(()) } +// #[cfg(target_os = "solana")] +// fn mint_with_compressed_mint<'info>( +// ctx: &Context<'_, '_, '_, 'info, MintToInstruction<'info>>, +// amounts: &[impl ZeroCopyNumTrait], +// compressed_inputs: &CompressedMintInputs, +// ) -> Result<( +// Pubkey, +// Option<( +// PackedCompressedAccountWithMerkleContext, +// OutputCompressedAccountWithPackedContext, +// )>, +// )> { +// let mint_pubkey = ctx +// .accounts +// .mint +// .as_ref() +// .ok_or(crate::ErrorCode::MintIsNone)? +// .key(); +// let compressed_mint: CompressedMint = CompressedMint { +// mint_authority: Some(ctx.accounts.authority.key()), +// freeze_authority: if compressed_inputs +// .compressed_mint_input +// .freeze_authority_is_set +// { +// Some(compressed_inputs.compressed_mint_input.freeze_authority) +// } else { +// None +// }, +// spl_mint: mint_pubkey, +// supply: compressed_inputs.compressed_mint_input.supply, +// decimals: compressed_inputs.compressed_mint_input.decimals, +// is_decompressed: compressed_inputs.compressed_mint_input.is_decompressed, +// num_extensions: compressed_inputs.compressed_mint_input.num_extensions, +// }; +// // Create input compressed account for existing mint +// let input_compressed_account = PackedCompressedAccountWithMerkleContext { +// compressed_account: CompressedAccount { +// owner: crate::ID.into(), +// lamports: 0, +// address: Some(compressed_inputs.address), +// data: Some(CompressedAccountData { +// discriminator: COMPRESSED_MINT_DISCRIMINATOR, +// data: Vec::new(), +// // TODO: hash with hashed inputs +// data_hash: compressed_mint.hash().map_err(ProgramError::from)?, +// }), +// }, +// merkle_context: compressed_inputs.merkle_context, +// root_index: compressed_inputs.root_index, +// read_only: false, +// }; +// let total_mint_amount: u64 = amounts.iter().map(|a| (*a).into()).sum(); +// let updated_compressed_mint = if compressed_mint.is_decompressed { +// // SYNC WITH SPL MINT (SPL is source of truth) + +// // Mint to SPL token pool as normal +// mint_spl_to_pool_pda(ctx, amounts)?; + +// // Read updated SPL mint state for sync +// let spl_mint_info = ctx +// .accounts +// .mint +// .as_ref() +// .ok_or(crate::ErrorCode::MintIsNone)?; +// let spl_mint_data = spl_mint_info.data.borrow(); +// let spl_mint = anchor_spl::token::Mint::try_deserialize(&mut &spl_mint_data[..])?; + +// // Create updated compressed mint with synced state +// let mut updated_compressed_mint = compressed_mint; +// updated_compressed_mint.supply = spl_mint.supply; +// updated_compressed_mint +// } else { +// // PURE COMPRESSED MINT - no SPL backing +// let mut updated_compressed_mint = compressed_mint; +// updated_compressed_mint.supply = updated_compressed_mint +// .supply +// .checked_add(total_mint_amount) +// .ok_or(crate::ErrorCode::MintTooLarge)?; +// updated_compressed_mint +// }; +// let updated_data_hash = updated_compressed_mint +// .hash() +// .map_err(|_| crate::ErrorCode::HashToFieldError)?; + +// let mut updated_mint_bytes = Vec::new(); +// updated_compressed_mint.serialize(&mut updated_mint_bytes)?; + +// let updated_compressed_account_data = CompressedAccountData { +// discriminator: COMPRESSED_MINT_DISCRIMINATOR, +// data: updated_mint_bytes, +// data_hash: updated_data_hash, +// }; + +// let output_compressed_mint_account = OutputCompressedAccountWithPackedContext { +// compressed_account: CompressedAccount { +// owner: crate::ID.into(), +// lamports: 0, +// address: Some(compressed_inputs.address), +// data: Some(updated_compressed_account_data), +// }, +// merkle_tree_index: compressed_inputs.output_merkle_tree_index, +// }; + +// Ok(( +// mint_pubkey, +// Some((input_compressed_account, output_compressed_mint_account)), +// )) +// } + #[cfg(target_os = "solana")] #[inline(never)] -pub fn cpi_execute_compressed_transaction_mint_to<'info>( - ctx: &Context<'_, '_, '_, 'info, MintToInstruction>, +pub fn cpi_execute_compressed_transaction_mint_to<'info, const IS_MINT_TO: bool>( + ctx: &Context<'_, '_, '_, 'info, MintToInstruction<'info>>, + mint_to_compressed_account: &[PackedCompressedAccountWithMerkleContext], output_compressed_accounts: Vec, inputs: &mut Vec, + proof: Option, pre_compressed_acounts_pos: usize, ) -> Result<()> { bench_sbf_start!("tm_cpi"); @@ -162,7 +287,12 @@ pub fn cpi_execute_compressed_transaction_mint_to<'info>( // 4300 CU for 10 accounts // 6700 CU for 20 accounts // 7,978 CU for 25 accounts - serialize_mint_to_cpi_instruction_data(inputs, &output_compressed_accounts); + serialize_mint_to_cpi_instruction_data_with_inputs( + inputs, + mint_to_compressed_account, + &output_compressed_accounts, + proof, + ); GLOBAL_ALLOCATOR.free_heap(pre_compressed_acounts_pos)?; @@ -181,7 +311,7 @@ pub fn cpi_execute_compressed_transaction_mint_to<'info>( }; // 1300 CU - let account_infos = vec![ + let mut account_infos = vec![ ctx.accounts.fee_payer.to_account_info(), ctx.accounts.cpi_authority_pda.to_account_info(), ctx.accounts.registered_program_pda.to_account_info(), @@ -195,9 +325,16 @@ pub fn cpi_execute_compressed_transaction_mint_to<'info>( ctx.accounts.light_system_program.to_account_info(), // none cpi_context_account ctx.accounts.merkle_tree.to_account_info(), // first remaining account ]; + // Don't add for batch compress + if IS_MINT_TO { + // Add remaining account metas (compressed mint merkle tree should be writable) + for remaining in ctx.remaining_accounts { + account_infos.push(remaining.to_account_info()); + } + } // account_metas take 1k cu - let accounts = vec![ + let mut accounts = vec![ AccountMeta { pubkey: account_infos[0].key(), is_signer: true, @@ -255,7 +392,18 @@ pub fn cpi_execute_compressed_transaction_mint_to<'info>( is_writable: true, }, ]; - + // Don't add for batch compress + if IS_MINT_TO { + // Add remaining account metas (compressed mint merkle tree should be writable) + for remaining in &account_infos[12..] { + msg!(" remaining.key() {:?}", remaining.key()); + accounts.push(AccountMeta { + pubkey: remaining.key(), + is_signer: false, + is_writable: remaining.is_writable, + }); + } + } let instruction = anchor_lang::solana_program::instruction::Instruction { program_id: light_system_program::ID, accounts, @@ -274,26 +422,41 @@ pub fn cpi_execute_compressed_transaction_mint_to<'info>( } #[inline(never)] -pub fn serialize_mint_to_cpi_instruction_data( +pub fn serialize_mint_to_cpi_instruction_data_with_inputs( inputs: &mut Vec, + input_compressed_accounts: &[PackedCompressedAccountWithMerkleContext], output_compressed_accounts: &[OutputCompressedAccountWithPackedContext], + proof: Option, ) { - let len = output_compressed_accounts.len(); - // proof (option None) - inputs.extend_from_slice(&[0u8]); - // two empty vecs 4 bytes of zeroes each: address_params, + // proof (option) + if let Some(proof) = proof { + inputs.extend_from_slice(&[1u8]); // Some + proof.serialize(inputs).unwrap(); + } else { + inputs.extend_from_slice(&[0u8]); // None + } + + // new_address_params (empty for mint operations) + inputs.extend_from_slice(&[0u8; 4]); + // input_compressed_accounts_with_merkle_context - inputs.extend_from_slice(&[0u8; 8]); - // lenght of output_compressed_accounts vec as u32 - inputs.extend_from_slice(&[(len as u8), 0, 0, 0]); - let mut sum_lamports = 0u64; + let input_len = input_compressed_accounts.len(); + inputs.extend_from_slice(&[(input_len as u8), 0, 0, 0]); + for input_account in input_compressed_accounts.iter() { + input_account.serialize(inputs).unwrap(); + } + // output_compressed_accounts + let output_len = output_compressed_accounts.len(); + inputs.extend_from_slice(&[(output_len as u8), 0, 0, 0]); + let mut sum_lamports = 0u64; for compressed_account in output_compressed_accounts.iter() { compressed_account.serialize(inputs).unwrap(); sum_lamports = sum_lamports .checked_add(compressed_account.compressed_account.lamports) .unwrap(); } + // None relay_fee inputs.extend_from_slice(&[0u8; 1]); @@ -309,6 +472,158 @@ pub fn serialize_mint_to_cpi_instruction_data( inputs.extend_from_slice(&[0u8]); } +// #[cfg(target_os = "solana")] +// fn create_compressed_mint_update_accounts( +// updated_compressed_mint: CompressedMint, +// compressed_inputs: CompressedMintInputs, +// ) -> Result<( +// PackedCompressedAccountWithMerkleContext, +// OutputCompressedAccountWithPackedContext, +// )> { +// // Create input compressed account for existing mint +// let input_compressed_account = PackedCompressedAccountWithMerkleContext { +// compressed_account: CompressedAccount { +// owner: crate::ID.into(), +// lamports: 0, +// address: Some(compressed_inputs.address), +// data: Some(CompressedAccountData { +// discriminator: COMPRESSED_MINT_DISCRIMINATOR, +// data: Vec::new(), +// data_hash: updated_compressed_mint.hash().map_err(ProgramError::from)?, +// }), +// }, +// merkle_context: compressed_inputs.merkle_context, +// root_index: compressed_inputs.root_index, +// read_only: false, +// }; +// msg!( +// "compressed_inputs.merkle_context: {:?}", +// compressed_inputs.merkle_context +// ); + +// // Create output compressed account for updated mint +// let mut updated_mint_bytes = Vec::new(); +// updated_compressed_mint.serialize(&mut updated_mint_bytes)?; +// let updated_data_hash = updated_compressed_mint +// .hash() +// .map_err(|_| crate::ErrorCode::HashToFieldError)?; + +// let updated_compressed_account_data = CompressedAccountData { +// discriminator: COMPRESSED_MINT_DISCRIMINATOR, +// data: updated_mint_bytes, +// data_hash: updated_data_hash, +// }; + +// let output_compressed_mint_account = OutputCompressedAccountWithPackedContext { +// compressed_account: CompressedAccount { +// owner: crate::ID.into(), +// lamports: 0, +// address: Some(compressed_inputs.address), +// data: Some(updated_compressed_account_data), +// }, +// merkle_tree_index: compressed_inputs.output_merkle_tree_index, +// }; +// msg!( +// "compressed_inputs.output_merkle_tree_index {}", +// compressed_inputs.output_merkle_tree_index +// ); + +// Ok((input_compressed_account, output_compressed_mint_account)) +// } + +// #[cfg(target_os = "solana")] +// #[inline(never)] +// pub fn cpi_execute_compressed_transaction_mint_to_with_inputs<'info>( +// ctx: &Context<'_, '_, '_, 'info, MintToInstruction<'info>>, +// input_compressed_accounts: Vec, +// output_compressed_accounts: Vec, +// proof: Option, +// inputs: &mut Vec, +// pre_compressed_accounts_pos: usize, +// ) -> Result<()> { +// bench_sbf_start!("tm_cpi_mint_update"); + +// let signer_seeds = get_cpi_signer_seeds(); + +// // Serialize CPI instruction data with inputs +// serialize_mint_to_cpi_instruction_data_with_inputs( +// inputs, +// &input_compressed_accounts, +// &output_compressed_accounts, +// proof, +// ); + +// GLOBAL_ALLOCATOR.free_heap(pre_compressed_accounts_pos)?; + +// use anchor_lang::InstructionData; + +// let instructiondata = light_system_program::instruction::InvokeCpi { +// inputs: inputs.to_owned(), +// }; + +// let (sol_pool_pda, is_writable) = if let Some(pool_pda) = ctx.accounts.sol_pool_pda.as_ref() { +// (pool_pda.to_account_info(), true) +// } else { +// (ctx.accounts.light_system_program.to_account_info(), false) +// }; + +// // Build account infos including both output merkle tree and remaining accounts (compressed mint merkle tree) +// let mut account_infos = vec![ +// ctx.accounts.fee_payer.to_account_info(), +// ctx.accounts.cpi_authority_pda.to_account_info(), +// ctx.accounts.registered_program_pda.to_account_info(), +// ctx.accounts.noop_program.to_account_info(), +// ctx.accounts.account_compression_authority.to_account_info(), +// ctx.accounts.account_compression_program.to_account_info(), +// ctx.accounts.self_program.to_account_info(), +// sol_pool_pda, +// ctx.accounts.light_system_program.to_account_info(), +// ctx.accounts.system_program.to_account_info(), +// ctx.accounts.light_system_program.to_account_info(), // cpi_context_account placeholder +// ctx.accounts.merkle_tree.to_account_info(), // output merkle tree +// ]; + +// // Add remaining accounts (compressed mint merkle tree, etc.) +// account_infos.extend_from_slice(ctx.remaining_accounts); + +// // Build account metas +// let mut accounts = vec![ +// AccountMeta::new(account_infos[0].key(), true), // fee_payer +// AccountMeta::new_readonly(account_infos[1].key(), true), // cpi_authority_pda (signer) +// AccountMeta::new_readonly(account_infos[2].key(), false), // registered_program_pda +// AccountMeta::new_readonly(account_infos[3].key(), false), // noop_program +// AccountMeta::new_readonly(account_infos[4].key(), false), // account_compression_authority +// AccountMeta::new_readonly(account_infos[5].key(), false), // account_compression_program +// AccountMeta::new_readonly(account_infos[6].key(), false), // self_program +// AccountMeta::new(account_infos[7].key(), is_writable), // sol_pool_pda +// AccountMeta::new_readonly(account_infos[8].key(), false), // decompression_recipient placeholder +// AccountMeta::new_readonly(account_infos[9].key(), false), // system_program +// AccountMeta::new_readonly(account_infos[10].key(), false), // cpi_context_account placeholder +// AccountMeta::new(account_infos[11].key(), false), // output merkle tree (writable) +// ]; + +// // Add remaining account metas (compressed mint merkle tree should be writable) +// for remaining in &account_infos[12..] { +// accounts.push(AccountMeta::new(remaining.key(), false)); +// } + +// let instruction = anchor_lang::solana_program::instruction::Instruction { +// program_id: light_system_program::ID, +// accounts, +// data: instructiondata.data(), +// }; + +// bench_sbf_end!("tm_cpi_mint_update"); +// bench_sbf_start!("tm_invoke_mint_update"); +// anchor_lang::solana_program::program::invoke_signed( +// &instruction, +// account_infos.as_slice(), +// &[&signer_seeds[..]], +// )?; +// bench_sbf_end!("tm_invoke_mint_update"); +// Ok(()) +// } + #[inline(never)] pub fn mint_spl_to_pool_pda( ctx: &Context, @@ -529,18 +844,16 @@ pub mod mint_sdk { #[cfg(test)] mod test { use light_compressed_account::{ - compressed_account::{CompressedAccount, CompressedAccountData}, + compressed_account::{CompressedAccount, CompressedAccountData, PackedMerkleContext}, instruction_data::{ data::OutputCompressedAccountWithPackedContext, invoke_cpi::InstructionDataInvokeCpi, }, + Pubkey, }; + use light_ctoken_types::state::{AccountState, TokenData}; use super::*; - use crate::{ - constants::TOKEN_COMPRESSED_ACCOUNT_DISCRIMINATOR, - token_data::{AccountState, TokenData}, - }; - + use crate::constants::TOKEN_COMPRESSED_ACCOUNT_DISCRIMINATOR; #[test] fn test_manual_ix_data_serialization_borsh_compat() { let pubkeys = [Pubkey::new_unique(), Pubkey::new_unique()]; @@ -580,7 +893,12 @@ mod test { } let mut inputs = Vec::::new(); - serialize_mint_to_cpi_instruction_data(&mut inputs, &output_compressed_accounts); + serialize_mint_to_cpi_instruction_data_with_inputs( + &mut inputs, + &[], + &output_compressed_accounts, + None, + ); let inputs_struct = InstructionDataInvokeCpi { relay_fee: None, input_compressed_accounts_with_merkle_context: Vec::with_capacity(0), @@ -643,17 +961,67 @@ mod test { merkle_tree_index: 0, }; } + + // Randomly test with or without compressed mint inputs + let (input_compressed_accounts, expected_inputs, proof) = if rng.gen_bool(0.5) { + // Test with compressed mint inputs (50% chance) + let input_mint_account = PackedCompressedAccountWithMerkleContext { + compressed_account: CompressedAccount { + owner: crate::ID.into(), + lamports: 0, + address: Some([rng.gen::(); 32]), + data: Some(CompressedAccountData { + discriminator: crate::constants::COMPRESSED_MINT_DISCRIMINATOR, + data: vec![rng.gen::(); 32], + data_hash: [rng.gen::(); 32], + }), + }, + merkle_context: PackedMerkleContext { + merkle_tree_pubkey_index: rng.gen_range(0..10), + queue_pubkey_index: rng.gen_range(0..10), + leaf_index: rng.gen_range(0..1000), + prove_by_index: rng.gen_bool(0.5), + }, + root_index: rng.gen_range(0..100), + read_only: false, + }; + + let proof = if rng.gen_bool(0.3) { + Some(CompressedProof { + a: [rng.gen::(); 32], + b: [rng.gen::(); 64], + c: [rng.gen::(); 32], + }) + } else { + None + }; + + ( + vec![input_mint_account.clone()], + vec![input_mint_account], + proof, + ) + } else { + // Test without compressed mint inputs (50% chance) + (Vec::new(), Vec::new(), None) + }; + let mut inputs = Vec::::new(); - serialize_mint_to_cpi_instruction_data(&mut inputs, &output_compressed_accounts); + serialize_mint_to_cpi_instruction_data_with_inputs( + &mut inputs, + &input_compressed_accounts, + &output_compressed_accounts, + proof, + ); let sum = output_compressed_accounts .iter() .map(|x| x.compressed_account.lamports) .sum::(); let inputs_struct = InstructionDataInvokeCpi { relay_fee: None, - input_compressed_accounts_with_merkle_context: Vec::with_capacity(0), + input_compressed_accounts_with_merkle_context: expected_inputs, output_compressed_accounts: output_compressed_accounts.clone(), - proof: None, + proof, new_address_params: Vec::with_capacity(0), compress_or_decompress_lamports: Some(sum), is_compress: true, diff --git a/programs/compressed-token/src/process_transfer.rs b/programs/compressed-token/anchor/src/process_transfer.rs similarity index 95% rename from programs/compressed-token/src/process_transfer.rs rename to programs/compressed-token/anchor/src/process_transfer.rs index 7f3d67b7bc..d8f0fedeba 100644 --- a/programs/compressed-token/src/process_transfer.rs +++ b/programs/compressed-token/anchor/src/process_transfer.rs @@ -13,14 +13,17 @@ use light_compressed_account::{ }, pubkey::AsPubkey, }; +use light_ctoken_types::state::{AccountState, TokenData}; use light_heap::{bench_sbf_end, bench_sbf_start}; use light_system_program::account_traits::{InvokeAccounts, SignerAccounts}; use light_zero_copy::num_trait::ZeroCopyNumTrait; use crate::{ - constants::{BUMP_CPI_AUTHORITY, NOT_FROZEN, TOKEN_COMPRESSED_ACCOUNT_DISCRIMINATOR}, + constants::{ + BUMP_CPI_AUTHORITY, NOT_FROZEN, TOKEN_COMPRESSED_ACCOUNT_DISCRIMINATOR, + TOKEN_COMPRESSED_ACCOUNT_V2_DISCRIMINATOR, + }, spl_compression::process_compression_or_decompression, - token_data::{AccountState, TokenData}, ErrorCode, TransferInstruction, }; @@ -180,6 +183,17 @@ pub fn process_transfer<'a, 'b, 'c, 'info: 'b + 'c>( pub const BATCHED_DISCRIMINATOR: &[u8] = b"BatchMta"; pub const OUTPUT_QUEUE_DISCRIMINATOR: &[u8] = b"queueacc"; +/// Helper function to determine the appropriate token account discriminator based on tree type +pub fn get_token_account_discriminator(tree_discriminator: &[u8]) -> Result<[u8; 8]> { + match tree_discriminator { + StateMerkleTreeAccount::DISCRIMINATOR => Ok(TOKEN_COMPRESSED_ACCOUNT_DISCRIMINATOR), + BATCHED_DISCRIMINATOR | OUTPUT_QUEUE_DISCRIMINATOR => { + Ok(TOKEN_COMPRESSED_ACCOUNT_V2_DISCRIMINATOR) + } + _ => err!(anchor_lang::error::ErrorCode::AccountDiscriminatorMismatch), + } +} + /// Creates output compressed accounts. /// Steps: /// 1. Allocate memory for token data. @@ -229,10 +243,10 @@ pub fn create_output_compressed_accounts( let mut token_data_bytes = Vec::with_capacity(capacity); // 1,000 CU token data and serialize let token_data = TokenData { - mint: (mint_pubkey).to_anchor_pubkey(), - owner: (*owner).to_anchor_pubkey(), + mint: (mint_pubkey).to_anchor_pubkey().into(), + owner: (*owner).to_anchor_pubkey().into(), amount: (*amount).into(), - delegate, + delegate: delegate.map(|delegate_pubkey| delegate_pubkey.into()), state: AccountState::Initialized, tlv: None, }; @@ -273,8 +287,11 @@ pub fn create_output_compressed_accounts( &hashed_delegate, ) .map_err(ProgramError::from)?; + + let discriminator = get_token_account_discriminator(discriminator_bytes)?; + let data = CompressedAccountData { - discriminator: TOKEN_COMPRESSED_ACCOUNT_DISCRIMINATOR, + discriminator, data: token_data_bytes, data_hash, }; @@ -660,9 +677,15 @@ pub fn get_input_compressed_accounts_with_merkle_context_and_check_signer match remaining_accounts.get(&delegate) { + Some(delegate) => match remaining_accounts.get(&delegate.into()) { Some(delegate_index) => Some(*delegate_index as u8), None => { - remaining_accounts.insert(delegate, index); + remaining_accounts.insert(delegate.into(), index); index += 1; Some((index - 1) as u8) } @@ -1109,8 +1134,9 @@ pub mod transfer_sdk { #[cfg(test)] mod test { + use light_ctoken_types::state::AccountState; + use super::*; - use crate::token_data::AccountState; #[test] fn test_sum_check() { @@ -1152,6 +1178,7 @@ mod test { compress_or_decompress_amount: Option, is_compress: bool, ) -> Result<()> { + use light_compressed_account::Pubkey; let mut inputs = Vec::new(); for i in input_amounts.iter() { inputs.push(TokenData { diff --git a/programs/compressed-token/src/spl_compression.rs b/programs/compressed-token/anchor/src/spl_compression.rs similarity index 100% rename from programs/compressed-token/src/spl_compression.rs rename to programs/compressed-token/anchor/src/spl_compression.rs diff --git a/programs/compressed-token/program/Cargo.toml b/programs/compressed-token/program/Cargo.toml new file mode 100644 index 0000000000..a2fb752487 --- /dev/null +++ b/programs/compressed-token/program/Cargo.toml @@ -0,0 +1,63 @@ +[package] +name = "light-compressed-token" +version = "2.0.0" +description = "Generalized token compression on Solana" +repository = "https://github.com/Lightprotocol/light-protocol" +license = "Apache-2.0" +edition = "2021" + +[lib] +crate-type = ["cdylib", "lib"] +name = "light_compressed_token" + +[features] +no-entrypoint = [] +no-log-ix-name = [] +cpi = ["no-entrypoint"] +custom-heap = ["light-heap"] +mem-profiling = [] +default = ["custom-heap"] +test-sbf = [] +bench-sbf = [] +cpi-context = [] +cpi-without-program-ids = [] + +[dependencies] +anchor-lang = { workspace = true } +spl-token = { workspace = true, features = ["no-entrypoint"] } +account-compression = { workspace = true, features = ["cpi", "no-idl"] } +light-system-program-anchor = { workspace = true, features = ["cpi"] } +solana-security-txt = "1.1.0" +light-hasher = { workspace = true } +light-heap = { workspace = true, optional = true } +light-compressed-account = { workspace = true, features = ["anchor"] } +spl-token-2022 = { workspace = true } +spl-pod = { workspace = true } +light-zero-copy = { workspace = true, features = ["mut", "std", "derive"] } +zerocopy = { workspace = true } +anchor-compressed-token = { path = "../anchor", features = ["cpi"] } +light-account-checks = { workspace = true, features = ["solana", "pinocchio"] } +light-sdk = { workspace = true } +borsh = { workspace = true } +light-sdk-types = { workspace = true } +solana-pubkey = { workspace = true } +arrayvec = { workspace = true } +pinocchio = { workspace = true, features = ["std"] } +light-sdk-pinocchio = { workspace = true } +light-ctoken-types = { workspace = true, features = ["anchor"] } + +[dev-dependencies] +rand = { workspace = true } +num-bigint = { workspace = true } +light-account-checks = { workspace = true, features = [ + "solana", + "pinocchio", + "test-only", +] } + +[lints.rust.unexpected_cfgs] +level = "allow" +check-cfg = [ + 'cfg(target_os, values("solana"))', + 'cfg(feature, values("frozen-abi", "no-entrypoint"))', +] diff --git a/programs/compressed-token/program/README.md b/programs/compressed-token/program/README.md new file mode 100644 index 0000000000..764e509cdc --- /dev/null +++ b/programs/compressed-token/program/README.md @@ -0,0 +1,13 @@ +# Compressed Token Program + +A token program on the Solana blockchain using ZK Compression. + +This program provides an interface and implementation that third parties can utilize to create and use compressed tokens on Solana. + +Documentation is available at https://zkcompression.com + +Source code: https://github.com/Lightprotocol/light-protocol/tree/main/programs/compressed-token + +## Audit + +This code is unaudited. Use at your own risk. diff --git a/programs/compressed-token/program/Xargo.toml b/programs/compressed-token/program/Xargo.toml new file mode 100644 index 0000000000..475fb71ed1 --- /dev/null +++ b/programs/compressed-token/program/Xargo.toml @@ -0,0 +1,2 @@ +[target.bpfel-unknown-unknown.dependencies.std] +features = [] diff --git a/programs/compressed-token/program/src/close_token_account/accounts.rs b/programs/compressed-token/program/src/close_token_account/accounts.rs new file mode 100644 index 0000000000..267a4285ce --- /dev/null +++ b/programs/compressed-token/program/src/close_token_account/accounts.rs @@ -0,0 +1,32 @@ +use anchor_lang::solana_program::program_error::ProgramError; +use light_account_checks::checks::{check_mut, check_signer}; +use pinocchio::account_info::AccountInfo; + +use crate::shared::AccountIterator; + +pub struct CloseTokenAccountAccounts<'info> { + pub token_account: &'info AccountInfo, + pub destination: &'info AccountInfo, + pub authority: &'info AccountInfo, +} + +impl<'info> CloseTokenAccountAccounts<'info> { + pub fn validate_and_parse(accounts: &'info [AccountInfo]) -> Result { + let mut iter = AccountIterator::new(accounts); + + let token_account = iter.next_account("token_account")?; + let destination = iter.next_account("destination")?; + let authority = iter.next_account("authority")?; + + // Basic validations using light_account_checks + check_mut(token_account)?; + check_mut(destination)?; + check_signer(authority)?; + + Ok(CloseTokenAccountAccounts { + token_account, + destination, + authority, + }) + } +} diff --git a/programs/compressed-token/program/src/close_token_account/mod.rs b/programs/compressed-token/program/src/close_token_account/mod.rs new file mode 100644 index 0000000000..2e42d63ac6 --- /dev/null +++ b/programs/compressed-token/program/src/close_token_account/mod.rs @@ -0,0 +1,2 @@ +pub mod accounts; +pub mod processor; diff --git a/programs/compressed-token/program/src/close_token_account/processor.rs b/programs/compressed-token/program/src/close_token_account/processor.rs new file mode 100644 index 0000000000..494fb5eade --- /dev/null +++ b/programs/compressed-token/program/src/close_token_account/processor.rs @@ -0,0 +1,100 @@ +use anchor_lang::prelude::ProgramError; +use light_account_checks::AccountInfoTrait; +use light_ctoken_types::state::{CompressedToken, ZExtensionStruct}; +use light_zero_copy::borsh::Deserialize; +use pinocchio::account_info::AccountInfo; +use spl_token_2022::state::AccountState; + +use super::accounts::CloseTokenAccountAccounts; + +/// Process the close token account instruction +pub fn process_close_token_account( + account_infos: &[AccountInfo], + _instruction_data: &[u8], +) -> Result<(), ProgramError> { + // Validate and get accounts + let accounts = CloseTokenAccountAccounts::validate_and_parse(account_infos)?; + + validate_and_close_token_account(&accounts)?; + + Ok(()) +} + +pub fn validate_and_close_token_account( + accounts: &CloseTokenAccountAccounts, +) -> Result<(), ProgramError> { + validate_token_account(accounts)?; + close_token_account(accounts)?; + + Ok(()) +} + +pub fn validate_token_account(accounts: &CloseTokenAccountAccounts) -> Result<(), ProgramError> { + let token_account_data = AccountInfoTrait::try_borrow_data(accounts.token_account) + .map_err(|_| ProgramError::InvalidAccountData)?; + + // Try to parse as CompressedToken using zero-copy deserialization + let (compressed_token, _) = CompressedToken::zero_copy_at(&token_account_data) + .map_err(|_| ProgramError::InvalidAccountData)?; + + // Check that the account is initialized + if compressed_token.state != AccountState::Initialized as u8 { + return Err(ProgramError::UninitializedAccount); + } + + // Check that the account has zero balance + if u64::from(*compressed_token.amount) != 0 { + return Err(ProgramError::InvalidAccountData); + } + + // Verify the authority matches the account owner or rent authority (if compressible) + let authority_key = solana_pubkey::Pubkey::new_from_array(*accounts.authority.key()); + let mut is_valid_authority = compressed_token.owner.to_bytes() == authority_key.to_bytes(); + + // Check if account has compressible extension and if authority is rent authority + if !is_valid_authority { + if let Some(extensions) = compressed_token.extensions.as_ref() { + // Look for compressible extension + for extension in extensions { + if let ZExtensionStruct::Compressible(compressible_ext) = extension { + // Check if authority is the rent authority && rent_recipient is the destination account + if compressible_ext.rent_authority.to_bytes() == authority_key.to_bytes() + && compressible_ext.rent_recipient.to_bytes() == *accounts.destination.key() + { + is_valid_authority = true; + + // For rent authority, check timing constraints + #[cfg(target_os = "solana")] + if !compressible_ext.is_compressible()? { + return Err(ProgramError::InvalidAccountData); + } + break; + } + } + } + } + } + + if !is_valid_authority { + return Err(ProgramError::InvalidAccountOwner); + } + Ok(()) +} + +pub fn close_token_account(accounts: &CloseTokenAccountAccounts<'_>) -> Result<(), ProgramError> { + let token_account_lamports = AccountInfoTrait::lamports(accounts.token_account); + unsafe { + *accounts.token_account.borrow_mut_lamports_unchecked() = 0; + } + let destination_lamports = AccountInfoTrait::lamports(accounts.destination); + let new_destination_lamports = destination_lamports + .checked_add(token_account_lamports) + .ok_or(ProgramError::ArithmeticOverflow)?; + unsafe { + *accounts.destination.borrow_mut_lamports_unchecked() = new_destination_lamports; + } + let mut token_account_data = AccountInfoTrait::try_borrow_mut_data(accounts.token_account) + .map_err(|_| ProgramError::InvalidAccountData)?; + token_account_data.fill(0); + Ok(()) +} diff --git a/programs/compressed-token/program/src/constants.rs b/programs/compressed-token/program/src/constants.rs new file mode 100644 index 0000000000..2309eb6902 --- /dev/null +++ b/programs/compressed-token/program/src/constants.rs @@ -0,0 +1,5 @@ +// Compressed mint discriminator +pub const COMPRESSED_MINT_DISCRIMINATOR: [u8; 8] = [1, 0, 0, 0, 0, 0, 0, 0]; + +// CPI authority bump +pub const BUMP_CPI_AUTHORITY: u8 = 254; \ No newline at end of file diff --git a/programs/compressed-token/program/src/convert_account_infos.rs b/programs/compressed-token/program/src/convert_account_infos.rs new file mode 100644 index 0000000000..a9017f9e9b --- /dev/null +++ b/programs/compressed-token/program/src/convert_account_infos.rs @@ -0,0 +1,62 @@ +use anchor_lang::prelude::ProgramError; +use pinocchio::account_info::AccountInfo; + +/// Convert Pinocchio AccountInfo to Solana AccountInfo with minimal safety overhead +/// +/// # SAFETY +/// - `pinocchio_accounts` must remain valid for lifetime 'a +/// - No other code may mutably borrow these accounts during 'a +/// - Pinocchio runtime must have properly deserialized the accounts +/// - Caller must ensure no concurrent access to returned AccountInfo +#[inline(always)] +pub unsafe fn convert_account_infos<'a, const N: usize>( + pinocchio_accounts: &'a [AccountInfo], +) -> Result, N>, ProgramError> { + if pinocchio_accounts.len() > N { + return Err(ProgramError::MaxAccountsDataAllocationsExceeded); + } + + use std::{cell::RefCell, rc::Rc}; + + // Compile-time type safety: Ensure Pubkey types are layout-compatible + const _: () = { + assert!( + std::mem::size_of::() + == std::mem::size_of::() + ); + assert!( + std::mem::align_of::() + == std::mem::align_of::() + ); + }; + + let mut solana_accounts = arrayvec::ArrayVec::, N>::new(); + for pinocchio_account in pinocchio_accounts { + let key: &'a solana_pubkey::Pubkey = + &*(pinocchio_account.key() as *const _ as *const solana_pubkey::Pubkey); + + let owner: &'a solana_pubkey::Pubkey = + &*(pinocchio_account.owner() as *const _ as *const solana_pubkey::Pubkey); + + let lamports = Rc::new(RefCell::new( + pinocchio_account.borrow_mut_lamports_unchecked(), + )); + + let data = Rc::new(RefCell::new(pinocchio_account.borrow_mut_data_unchecked())); + + let account_info = anchor_lang::prelude::AccountInfo { + key, + lamports, + data, + owner, + rent_epoch: 0, // Pinocchio doesn't track rent epoch + is_signer: pinocchio_account.is_signer(), + is_writable: pinocchio_account.is_writable(), + executable: pinocchio_account.executable(), + }; + + solana_accounts.push(account_info); + } + + Ok(solana_accounts) +} diff --git a/programs/compressed-token/program/src/create_associated_token_account/accounts.rs b/programs/compressed-token/program/src/create_associated_token_account/accounts.rs new file mode 100644 index 0000000000..420ea76564 --- /dev/null +++ b/programs/compressed-token/program/src/create_associated_token_account/accounts.rs @@ -0,0 +1,70 @@ +use anchor_lang::solana_program::{program_error::ProgramError, program_pack::IsInitialized}; +use light_account_checks::{ + checks::{check_mut, check_non_mut, check_signer}, + AccountInfoTrait, +}; +use pinocchio::account_info::AccountInfo; +use spl_pod::bytemuck::pod_from_bytes; +use spl_token_2022::pod::PodMint; + +use crate::shared::AccountIterator; + +pub struct CreateAssociatedTokenAccountAccounts<'info> { + pub fee_payer: &'info AccountInfo, + pub associated_token_account: &'info AccountInfo, + pub mint: Option<&'info AccountInfo>, + pub system_program: &'info AccountInfo, +} + +impl<'info> CreateAssociatedTokenAccountAccounts<'info> { + pub fn validate_and_parse( + accounts: &'info [AccountInfo], + mint: &[u8; 32], + mint_is_decompressed: bool, + ) -> Result { + let mut iter = AccountIterator::new(accounts); + + let fee_payer = iter.next_account("fee_payer")?; + let associated_token_account = iter.next_account("associated_token_account")?; + let mint_account = if mint_is_decompressed { + let mint_account_info = iter.next_account("mint_account")?; + if AccountInfoTrait::key(mint_account_info) != *mint { + return Err(ProgramError::InvalidAccountData); + } + + // Check if owned by either spl-token or spl-token-2022 program + let spl_token_id = spl_token::id().to_bytes(); + let spl_token_2022_id = spl_token_2022::id().to_bytes(); + let owner = unsafe { *mint_account_info.owner() }; + if owner != spl_token_id && owner != spl_token_2022_id { + return Err(ProgramError::IncorrectProgramId); + } + + let mint_data = AccountInfoTrait::try_borrow_data(mint_account_info) + .map_err(|_| ProgramError::InvalidAccountData)?; + let pod_mint = pod_from_bytes::(&mint_data) + .map_err(|_| ProgramError::InvalidAccountData)?; + + if !pod_mint.is_initialized() { + return Err(ProgramError::UninitializedAccount); + } + Some(mint_account_info) + } else { + None + }; + let system_program = iter.next_account("system_program")?; + + // Basic validations using light_account_checks + check_signer(fee_payer)?; + check_mut(fee_payer)?; + check_mut(associated_token_account)?; + check_non_mut(system_program)?; + + Ok(CreateAssociatedTokenAccountAccounts { + fee_payer, + associated_token_account, + mint: mint_account, + system_program, + }) + } +} diff --git a/programs/compressed-token/program/src/create_associated_token_account/mod.rs b/programs/compressed-token/program/src/create_associated_token_account/mod.rs new file mode 100644 index 0000000000..52d50fbef5 --- /dev/null +++ b/programs/compressed-token/program/src/create_associated_token_account/mod.rs @@ -0,0 +1,4 @@ +pub mod accounts; +pub mod processor; + +pub use processor::process_create_associated_token_account; diff --git a/programs/compressed-token/program/src/create_associated_token_account/processor.rs b/programs/compressed-token/program/src/create_associated_token_account/processor.rs new file mode 100644 index 0000000000..273a501813 --- /dev/null +++ b/programs/compressed-token/program/src/create_associated_token_account/processor.rs @@ -0,0 +1,120 @@ +use anchor_lang::{ + prelude::{ProgramError, SolanaSysvar}, + solana_program::{rent::Rent, system_instruction}, +}; +use light_account_checks::AccountInfoTrait; +use light_ctoken_types::instructions::create_associated_token_account::CreateAssociatedTokenAccountInstructionData; +use light_zero_copy::borsh::Deserialize; +use pinocchio::account_info::AccountInfo; + +use super::accounts::CreateAssociatedTokenAccountAccounts; +use crate::shared::initialize_token_account::initialize_token_account; + +/// Note: +/// - we don't validate the mint because it would be very expensive with compressed mints +/// - it is possible to create an associated token account for non existing mints +/// - accounts with non existing mints can never have a balance +/// Process the create associated token account instruction +pub fn process_create_associated_token_account( + account_infos: &[AccountInfo], + instruction_data: &[u8], +) -> Result<(), ProgramError> { + // Parse instruction data using zero-copy + let (inputs, _) = CreateAssociatedTokenAccountInstructionData::zero_copy_at(instruction_data) + .map_err(ProgramError::from)?; + + // Validate and get accounts + let accounts = CreateAssociatedTokenAccountAccounts::validate_and_parse( + account_infos, + &inputs.mint.to_bytes(), + false, + )?; + + { + let owner = inputs.owner.to_bytes(); + let mint = inputs.mint.to_bytes(); + // Define the PDA seeds for signing + use pinocchio::instruction::{Seed, Signer}; + let bump_bytes = [inputs.bump]; + let seed_array = [ + Seed::from(owner.as_ref()), + Seed::from(crate::ID.as_ref()), + Seed::from(mint.as_ref()), + Seed::from(bump_bytes.as_ref()), + ]; + let signer = Signer::from(&seed_array); + + // Calculate rent based on whether compressible extension is needed + let token_account_size = if inputs.compressible_config.is_some() { + light_ctoken_types::COMPRESSIBLE_TOKEN_ACCOUNT_SIZE as usize + } else { + light_ctoken_types::BASIC_TOKEN_ACCOUNT_SIZE as usize + }; + let rent = Rent::get()?; + let rent_lamports = rent.minimum_balance(token_account_size); + + // Create the associated token account + let fee_payer_key = + solana_pubkey::Pubkey::new_from_array(AccountInfoTrait::key(accounts.fee_payer)); + let ata_key = solana_pubkey::Pubkey::new_from_array(AccountInfoTrait::key( + accounts.associated_token_account, + )); + let create_account_instruction = system_instruction::create_account( + &fee_payer_key, + &ata_key, + rent_lamports, + token_account_size as u64, + &crate::ID, + ); + + // Execute the create account instruction with PDA signing + let instruction_data = create_account_instruction.data; + let pinocchio_instruction = pinocchio::instruction::Instruction { + program_id: &create_account_instruction.program_id.to_bytes(), + accounts: &[ + pinocchio::instruction::AccountMeta { + pubkey: accounts.fee_payer.key(), + is_signer: true, + is_writable: true, + }, + pinocchio::instruction::AccountMeta { + pubkey: accounts.associated_token_account.key(), + is_signer: true, + is_writable: true, + }, + pinocchio::instruction::AccountMeta { + pubkey: accounts.system_program.key(), + is_signer: false, + is_writable: false, + }, + ], + data: &instruction_data, + }; + + match pinocchio::program::invoke_signed( + &pinocchio_instruction, + &[ + accounts.fee_payer, + accounts.associated_token_account, + accounts.system_program, + ], + &[signer], + ) { + Ok(()) => {} + Err(e) => { + anchor_lang::solana_program::msg!("invoke_signed failed: {:?}", e); + return Err(ProgramError::Custom(u64::from(e) as u32)); + } + } + } + + // Initialize the token account using shared utility + initialize_token_account( + accounts.associated_token_account, + &inputs.mint.to_bytes(), + &inputs.owner.to_bytes(), + inputs.compressible_config, + )?; + + Ok(()) +} diff --git a/programs/compressed-token/program/src/create_spl_mint/accounts.rs b/programs/compressed-token/program/src/create_spl_mint/accounts.rs new file mode 100644 index 0000000000..be0f1630b4 --- /dev/null +++ b/programs/compressed-token/program/src/create_spl_mint/accounts.rs @@ -0,0 +1,73 @@ +use std::ops::Deref; + +use anchor_lang::solana_program::program_error::ProgramError; +use light_account_checks::checks::{check_program, check_signer}; +use pinocchio::{account_info::AccountInfo, pubkey::Pubkey}; + +use crate::shared::{ + accounts::{LightSystemAccounts, UpdateOneCompressedAccountTreeAccounts}, + AccountIterator, +}; + +pub struct CreateSplMintAccounts<'info> { + pub authority: &'info AccountInfo, + pub mint: &'info AccountInfo, + pub mint_signer: &'info AccountInfo, + pub token_pool_pda: &'info AccountInfo, + pub token_program: &'info AccountInfo, + pub light_system_program: &'info AccountInfo, + pub system: LightSystemAccounts<'info>, + pub trees: UpdateOneCompressedAccountTreeAccounts<'info>, +} + +impl CreateSplMintAccounts<'_> { + pub const SYSTEM_ACCOUNTS_OFFSET: usize = 6; +} + +impl<'info> CreateSplMintAccounts<'info> { + #[inline(always)] + pub fn tree_pubkeys(&self) -> [&'info Pubkey; 3] { + self.trees.pubkeys() + } +} + +impl<'info> Deref for CreateSplMintAccounts<'info> { + type Target = LightSystemAccounts<'info>; + + fn deref(&self) -> &Self::Target { + &self.system + } +} + +impl<'info> CreateSplMintAccounts<'info> { + pub fn validate_and_parse(accounts: &'info [AccountInfo]) -> Result { + let mut iter = AccountIterator::new(accounts); + + // Static non-CPI accounts first + let authority = iter.next_account("authority")?; + let mint = iter.next_account("mint")?; + let mint_signer = iter.next_account("mint_signer")?; + let token_pool_pda = iter.next_account("token_pool_pda")?; + let token_program = iter.next_account("token_program")?; + let light_system_program = iter.next_account("light_system_program")?; + + let system = LightSystemAccounts::validate_and_parse(&mut iter)?; + let trees = UpdateOneCompressedAccountTreeAccounts::validate_and_parse(&mut iter)?; + + // Validate authority: must be signer + check_signer(authority)?; + + check_program(&spl_token_2022::ID.to_bytes(), token_program)?; + + Ok(CreateSplMintAccounts { + authority, + mint, + mint_signer, + token_pool_pda, + token_program, + light_system_program, + system, + trees, + }) + } +} diff --git a/programs/compressed-token/program/src/create_spl_mint/mod.rs b/programs/compressed-token/program/src/create_spl_mint/mod.rs new file mode 100644 index 0000000000..2e42d63ac6 --- /dev/null +++ b/programs/compressed-token/program/src/create_spl_mint/mod.rs @@ -0,0 +1,2 @@ +pub mod accounts; +pub mod processor; diff --git a/programs/compressed-token/program/src/create_spl_mint/processor.rs b/programs/compressed-token/program/src/create_spl_mint/processor.rs new file mode 100644 index 0000000000..5315e8a024 --- /dev/null +++ b/programs/compressed-token/program/src/create_spl_mint/processor.rs @@ -0,0 +1,436 @@ +use anchor_lang::solana_program::{ + program_error::ProgramError, rent::Rent, system_instruction, sysvar::Sysvar, +}; +use arrayvec::ArrayVec; +use light_compressed_account::pubkey::AsPubkey; +use light_ctoken_types::{ + context::TokenContext, + instructions::create_spl_mint::{CreateSplMintInstructionData, ZCreateSplMintInstructionData}, + state::{CompressedMint, CompressedMintConfig}, + COMPRESSED_MINT_SEED, +}; +use light_sdk::instruction::PackedMerkleContext; +use light_zero_copy::{borsh::Deserialize, borsh_mut::DeserializeMut, ZeroCopyNew}; +use pinocchio::account_info::AccountInfo; +use spl_token::solana_program::log::sol_log_compute_units; + +use crate::{ + constants::POOL_SEED, + create_spl_mint::accounts::CreateSplMintAccounts, + shared::{cpi::execute_cpi_invoke, mint_to_token_pool}, + LIGHT_CPI_SIGNER, +}; + +// TODO: add test which asserts spl mint and compressed mint equivalence. +// TODO: check and handle extensions +pub fn process_create_spl_mint( + accounts: &[AccountInfo], + instruction_data: &[u8], +) -> Result<(), ProgramError> { + sol_log_compute_units(); + + // Parse instruction data using zero-copy + let (parsed_instruction_data, _) = CreateSplMintInstructionData::zero_copy_at(instruction_data) + .map_err(|_| ProgramError::InvalidInstructionData)?; + + sol_log_compute_units(); + + // Validate and parse accounts + let validated_accounts = CreateSplMintAccounts::validate_and_parse(accounts)?; + + // Verify mint PDA matches the spl_mint field in compressed mint inputs + // TODO: set it instead of passing it, to eliminate duplicate ix data. + let expected_mint: [u8; 32] = parsed_instruction_data.mint.mint.spl_mint.to_bytes(); + if validated_accounts.mint.key() != &expected_mint { + return Err(ProgramError::InvalidAccountData); + } + + // Create the mint account manually (PDA derived from our program, owned by token program) + create_mint_account( + &validated_accounts, + &crate::LIGHT_CPI_SIGNER.program_id, + parsed_instruction_data.mint_bump, + )?; + + // Initialize the mint account using Token-2022's initialize_mint2 instruction + initialize_mint_account(&validated_accounts, &parsed_instruction_data)?; + + // Create the token pool account manually (PDA derived from our program, owned by token program) + create_token_pool_account_manual(&validated_accounts, &crate::LIGHT_CPI_SIGNER.program_id)?; + + // Initialize the token pool account + initialize_token_pool_account(&validated_accounts)?; + + // Mint the existing supply to the token pool if there's any supply + if parsed_instruction_data.mint.mint.supply > 0 { + mint_to_token_pool( + validated_accounts.mint, + validated_accounts.token_pool_pda, + validated_accounts.token_program, + validated_accounts.cpi_authority_pda, + parsed_instruction_data.mint.mint.supply.into(), + )?; + } + if parsed_instruction_data.mint_authority_is_none() { + // TODO: remove mint authority from spl mint. + } + + // Update the compressed mint to mark it as is_decompressed = true + update_compressed_mint_to_decompressed( + accounts, + &validated_accounts, + &parsed_instruction_data, + )?; + + sol_log_compute_units(); + Ok(()) +} + +const IN_TREE: u8 = 0; +const IN_OUTPUT_QUEUE: u8 = 1; + +const OUT_OUTPUT_QUEUE: u8 = 2; + +fn update_compressed_mint_to_decompressed<'info>( + all_accounts: &'info [AccountInfo], + accounts: &CreateSplMintAccounts<'info>, + instruction_data: &ZCreateSplMintInstructionData, +) -> Result<(), ProgramError> { + use light_compressed_account::instruction_data::with_readonly::InstructionDataInvokeCpiWithReadOnly; + + use crate::{ + mint::{ + mint_input::create_input_compressed_mint_account, + mint_output::create_output_compressed_mint_account, + }, + shared::cpi_bytes_size::{ + allocate_invoke_with_read_only_cpi_bytes, cpi_bytes_config, CpiConfigInput, + }, + }; + + // Process extensions from input mint + let mint_inputs = &instruction_data.mint.mint; + let (_, extensions_config, _) = + crate::extensions::process_extensions_config(mint_inputs.extensions.as_ref())?; + + // Build configuration for CPI instruction data - 1 input, 1 output, with optional proof + let config_input = CpiConfigInput { + input_accounts: ArrayVec::new(), + output_accounts: ArrayVec::new(), + has_proof: instruction_data.mint.proof.is_some(), + compressed_mint: true, + compressed_mint_with_freeze_authority: mint_inputs.freeze_authority.is_some(), + extensions_config, + }; + + let config = cpi_bytes_config(config_input); + let mut cpi_bytes = allocate_invoke_with_read_only_cpi_bytes(&config); + + { + let (mut cpi_instruction_struct, _) = + InstructionDataInvokeCpiWithReadOnly::new_zero_copy(&mut cpi_bytes[8..], config) + .map_err(ProgramError::from)?; + cpi_instruction_struct.initialize( + crate::LIGHT_CPI_SIGNER.bump, + &crate::LIGHT_CPI_SIGNER.program_id.into(), + instruction_data.mint.proof, + None, + )?; + + let mut context = TokenContext::new(); + let hashed_mint_authority = context.get_or_hash_pubkey(accounts.authority.key()); + + // Process input compressed mint account (before is_decompressed = true) + create_input_compressed_mint_account( + &mut cpi_instruction_struct.input_compressed_accounts[0], + &mut context, + &instruction_data.mint, + &hashed_mint_authority, + PackedMerkleContext { + leaf_index: instruction_data.mint.leaf_index.into(), + prove_by_index: instruction_data.mint.prove_by_index(), + merkle_tree_pubkey_index: IN_TREE, + queue_pubkey_index: IN_OUTPUT_QUEUE, + }, + )?; + + // Process output compressed mint account (with is_decompressed = true) + let mint_inputs = &instruction_data.mint.mint; + let mint_pda = mint_inputs.spl_mint; + let decimals = mint_inputs.decimals; + let freeze_authority = mint_inputs + .freeze_authority + .as_ref() + .map(|fa| fa.to_bytes().into()); + let mint_authority = if instruction_data.mint_authority_is_none() { + None + } else { + Some(accounts.authority.key().to_pubkey_bytes().into()) + }; + + // Reuse the extensions config we already processed + let (has_extensions_output, extensions_config_output, _) = + crate::extensions::process_extensions_config(mint_inputs.extensions.as_ref())?; + + let mint_config = CompressedMintConfig { + mint_authority: (true, ()), + freeze_authority: (mint_inputs.freeze_authority.is_some(), ()), + extensions: (has_extensions_output, extensions_config_output), + }; + let mut token_context = TokenContext::new(); + + create_output_compressed_mint_account( + &mut cpi_instruction_struct.output_compressed_accounts[0], + mint_pda, + decimals, + freeze_authority, + mint_authority, + mint_inputs.supply, + mint_config, + instruction_data.mint.address, + OUT_OUTPUT_QUEUE, + instruction_data.mint.mint.version, + true, // Set is_decompressed = true for create_spl_mint + mint_inputs.extensions.as_deref(), + &mut token_context, + )?; + + // Override the output compressed mint to set is_decompressed = true + // The create_output_compressed_mint_account function sets is_decompressed = false by default + { + let output_account = &mut cpi_instruction_struct.output_compressed_accounts[0]; + if let Some(data) = output_account.compressed_account.data.as_mut() { + let (mut compressed_mint, _) = + CompressedMint::zero_copy_at_mut(data.data).map_err(ProgramError::from)?; + compressed_mint.is_decompressed = 1; // Override to mark as decompressed (1 = true) + } + } + } + // Execute CPI to light system program to update the compressed mint + execute_cpi_invoke( + &all_accounts[CreateSplMintAccounts::SYSTEM_ACCOUNTS_OFFSET..], + cpi_bytes, + accounts.tree_pubkeys().as_slice(), + false, // no sol_pool_pda + None, // no cpi_context_account + )?; + + Ok(()) +} + +/// Creates the mint account manually as a PDA derived from our program but owned by the token program +fn create_mint_account( + accounts: &CreateSplMintAccounts<'_>, + program_id: &pinocchio::pubkey::Pubkey, + mint_bump: u8, +) -> Result<(), ProgramError> { + let mint_account_size = 82; // Size of Token-2022 Mint account + let rent = Rent::get()?; + let lamports = rent.minimum_balance(mint_account_size); + + // Derive the mint PDA seeds using provided bump + let program_id_pubkey = solana_pubkey::Pubkey::new_from_array(*program_id); + let expected_mint = solana_pubkey::Pubkey::create_program_address( + &[ + COMPRESSED_MINT_SEED, + accounts.mint_signer.key().as_ref(), + &[mint_bump], + ], + &program_id_pubkey, + ) + .map_err(|_| ProgramError::InvalidAccountData)?; + + // Verify the provided mint account matches the expected PDA + if accounts.mint.key() != &expected_mint.to_bytes() { + return Err(ProgramError::InvalidAccountData); + } + + use pinocchio::instruction::{Seed, Signer}; + let mint_signer_key = accounts.mint_signer.key(); + let bump_bytes = [mint_bump]; + let seed_array = [ + Seed::from(COMPRESSED_MINT_SEED), + Seed::from(mint_signer_key.as_ref()), + Seed::from(bump_bytes.as_ref()), + ]; + let signer = Signer::from(&seed_array); + + // Create account owned by token program but derived from our program + let fee_payer_pubkey = solana_pubkey::Pubkey::new_from_array(*accounts.fee_payer.key()); + let mint_pubkey = solana_pubkey::Pubkey::new_from_array(*accounts.mint.key()); + let token_program_pubkey = solana_pubkey::Pubkey::new_from_array(*accounts.token_program.key()); + let create_account_ix = system_instruction::create_account( + &fee_payer_pubkey, + &mint_pubkey, + lamports, + mint_account_size as u64, + &token_program_pubkey, // Owned by token program + ); + + let pinocchio_instruction = pinocchio::instruction::Instruction { + program_id: &create_account_ix.program_id.to_bytes(), + accounts: &[ + pinocchio::instruction::AccountMeta::new(accounts.fee_payer.key(), true, true), + pinocchio::instruction::AccountMeta::new(accounts.mint.key(), true, true), + pinocchio::instruction::AccountMeta::readonly(accounts.system_program.key()), + ], + data: &create_account_ix.data, + }; + + match pinocchio::program::invoke_signed( + &pinocchio_instruction, + &[ + accounts.system.fee_payer, + accounts.mint, + accounts.system_program, + ], + &[signer], // Signed with our program's PDA seeds + ) { + Ok(()) => {} + Err(e) => { + return Err(ProgramError::Custom(u64::from(e) as u32)); + } + } + + Ok(()) +} + +/// Initializes the mint account using Token-2022's initialize_mint2 instruction +fn initialize_mint_account( + accounts: &CreateSplMintAccounts<'_>, + instruction_data: &ZCreateSplMintInstructionData, +) -> Result<(), ProgramError> { + let spl_ix = spl_token_2022::instruction::initialize_mint2( + &solana_pubkey::Pubkey::new_from_array(*accounts.token_program.key()), + &solana_pubkey::Pubkey::new_from_array(*accounts.mint.key()), + // cpi_signer is spl mint authority for compressed mints. + &solana_pubkey::Pubkey::new_from_array(LIGHT_CPI_SIGNER.cpi_signer), + instruction_data + .mint + .mint + .freeze_authority + .as_ref() + .map(|f| solana_pubkey::Pubkey::new_from_array(f.to_bytes())) + .as_ref(), + instruction_data.mint.mint.decimals, + )?; + + let initialize_mint_ix = pinocchio::instruction::Instruction { + program_id: accounts.token_program.key(), + accounts: &[pinocchio::instruction::AccountMeta::new( + accounts.mint.key(), + true, // is_writable: true (we're initializing the mint) + false, + )], + data: &spl_ix.data, + }; + + match pinocchio::program::invoke(&initialize_mint_ix, &[accounts.mint]) { + Ok(()) => {} + Err(e) => { + return Err(ProgramError::Custom(u64::from(e) as u32)); + } + } + + Ok(()) +} + +/// Creates the token pool account manually as a PDA derived from our program but owned by the token program +fn create_token_pool_account_manual( + accounts: &CreateSplMintAccounts<'_>, + program_id: &pinocchio::pubkey::Pubkey, +) -> Result<(), ProgramError> { + let token_account_size = 165; // Size of Token account + let rent = Rent::get()?; + let lamports = rent.minimum_balance(token_account_size); + + // Derive the token pool PDA seeds and bump + let mint_key = accounts.mint.key(); + let program_id_pubkey = solana_pubkey::Pubkey::new_from_array(*program_id); + let (expected_token_pool, bump) = solana_pubkey::Pubkey::find_program_address( + &[POOL_SEED, mint_key.as_ref()], + &program_id_pubkey, + ); + + // Verify the provided token pool account matches the expected PDA + if accounts.token_pool_pda.key() != &expected_token_pool.to_bytes() { + return Err(ProgramError::InvalidAccountData); + } + + use pinocchio::instruction::{Seed, Signer}; + let bump_bytes = [bump]; + let seed_array = [ + Seed::from(POOL_SEED), + Seed::from(mint_key.as_ref()), + Seed::from(bump_bytes.as_ref()), + ]; + let signer = Signer::from(&seed_array); + + // Create account owned by token program but derived from our program + let fee_payer_pubkey = solana_pubkey::Pubkey::new_from_array(*accounts.fee_payer.key()); + let token_pool_pubkey = solana_pubkey::Pubkey::new_from_array(*accounts.token_pool_pda.key()); + let token_program_pubkey = solana_pubkey::Pubkey::new_from_array(*accounts.token_program.key()); + let create_account_ix = system_instruction::create_account( + &fee_payer_pubkey, + &token_pool_pubkey, + lamports, + token_account_size as u64, + &token_program_pubkey, // Owned by token program + ); + + let pinocchio_instruction = pinocchio::instruction::Instruction { + program_id: &create_account_ix.program_id.to_bytes(), + accounts: &[ + pinocchio::instruction::AccountMeta::new(accounts.fee_payer.key(), true, true), + pinocchio::instruction::AccountMeta::new(accounts.token_pool_pda.key(), true, true), + pinocchio::instruction::AccountMeta::readonly(accounts.system_program.key()), + ], + data: &create_account_ix.data, + }; + + match pinocchio::program::invoke_signed( + &pinocchio_instruction, + &[ + accounts.fee_payer, + accounts.token_pool_pda, + accounts.system_program, + ], + &[signer], // Signed with our program's PDA seeds + ) { + Ok(()) => {} + Err(e) => { + return Err(ProgramError::Custom(u64::from(e) as u32)); + } + } + + Ok(()) +} + +/// Initializes the token pool account (assumes account already exists) +fn initialize_token_pool_account(accounts: &CreateSplMintAccounts<'_>) -> Result<(), ProgramError> { + let initialize_account_ix = pinocchio::instruction::Instruction { + program_id: accounts.token_program.key(), + accounts: &[ + pinocchio::instruction::AccountMeta::new(accounts.token_pool_pda.key(), true, false), // writable=true for initialization + pinocchio::instruction::AccountMeta::readonly(accounts.mint.key()), + ], + data: &spl_token_2022::instruction::initialize_account3( + &solana_pubkey::Pubkey::new_from_array(*accounts.token_program.key()), + &solana_pubkey::Pubkey::new_from_array(*accounts.token_pool_pda.key()), + &solana_pubkey::Pubkey::new_from_array(*accounts.mint.key()), + &solana_pubkey::Pubkey::new_from_array(*accounts.cpi_authority_pda.key()), + )? + .data, + }; + + match pinocchio::program::invoke( + &initialize_account_ix, + &[accounts.token_pool_pda, accounts.mint], + ) { + Ok(()) => {} + Err(e) => { + return Err(ProgramError::Custom(u64::from(e) as u32)); + } + } + Ok(()) +} diff --git a/programs/compressed-token/program/src/create_token_account/accounts.rs b/programs/compressed-token/program/src/create_token_account/accounts.rs new file mode 100644 index 0000000000..6cc802bd2d --- /dev/null +++ b/programs/compressed-token/program/src/create_token_account/accounts.rs @@ -0,0 +1,27 @@ +use anchor_lang::solana_program::program_error::ProgramError; +use light_account_checks::checks::{check_mut, check_non_mut}; +use pinocchio::account_info::AccountInfo; + +use crate::shared::AccountIterator; + +pub struct CreateTokenAccountAccounts<'info> { + pub token_account: &'info AccountInfo, + pub mint: &'info AccountInfo, +} + +impl<'info> CreateTokenAccountAccounts<'info> { + pub fn validate_and_parse(accounts: &'info [AccountInfo]) -> Result { + let mut iter = AccountIterator::new(accounts); + + let token_account = iter.next_account("token_account")?; + let mint = iter.next_account("mint")?; + + check_mut(token_account)?; + check_non_mut(mint)?; + + Ok(CreateTokenAccountAccounts { + token_account, + mint, + }) + } +} diff --git a/programs/compressed-token/program/src/create_token_account/instruction_data.rs b/programs/compressed-token/program/src/create_token_account/instruction_data.rs new file mode 100644 index 0000000000..b76ea034c6 --- /dev/null +++ b/programs/compressed-token/program/src/create_token_account/instruction_data.rs @@ -0,0 +1,12 @@ +use borsh::{BorshDeserialize, BorshSerialize}; +use light_compressed_account::Pubkey; +use light_ctoken_types::instructions::extensions::compressible::CompressibleExtensionInstructionData; +use light_zero_copy::ZeroCopy; + +#[derive(Debug, Clone, BorshSerialize, BorshDeserialize, ZeroCopy)] +pub struct CreateTokenAccountInstructionData { + /// The owner of the token account + pub owner: Pubkey, + /// Optional compressible configuration for the token account + pub compressible_config: Option, +} diff --git a/programs/compressed-token/program/src/create_token_account/mod.rs b/programs/compressed-token/program/src/create_token_account/mod.rs new file mode 100644 index 0000000000..9b7ff98bd6 --- /dev/null +++ b/programs/compressed-token/program/src/create_token_account/mod.rs @@ -0,0 +1,5 @@ +pub mod accounts; +pub mod instruction_data; +pub mod processor; + +pub use processor::process_create_token_account; diff --git a/programs/compressed-token/program/src/create_token_account/processor.rs b/programs/compressed-token/program/src/create_token_account/processor.rs new file mode 100644 index 0000000000..494f08b929 --- /dev/null +++ b/programs/compressed-token/program/src/create_token_account/processor.rs @@ -0,0 +1,38 @@ +use anchor_lang::prelude::ProgramError; +use light_zero_copy::borsh::Deserialize; +use pinocchio::account_info::AccountInfo; + +use super::{ + accounts::CreateTokenAccountAccounts, instruction_data::CreateTokenAccountInstructionData, +}; +use crate::shared::initialize_token_account::initialize_token_account; + +/// Process the create token account instruction +pub fn process_create_token_account( + account_infos: &[AccountInfo], + instruction_data: &[u8], +) -> Result<(), ProgramError> { + let mut backup_instruction_data = [0u8; 33]; + let (inputs, _) = if instruction_data.len() == 32 { + // Extend instruction data with a zero option byte for initialize_3 spl_token instruction compatibility + backup_instruction_data[0..32].copy_from_slice(instruction_data); + CreateTokenAccountInstructionData::zero_copy_at(backup_instruction_data.as_slice()) + .map_err(ProgramError::from)? + } else { + CreateTokenAccountInstructionData::zero_copy_at(instruction_data) + .map_err(ProgramError::from)? + }; + + // Validate and get accounts + let accounts = CreateTokenAccountAccounts::validate_and_parse(account_infos)?; + + // Initialize the token account (assumes account already exists and is owned by our program) + initialize_token_account( + accounts.token_account, + accounts.mint.key(), + &inputs.owner.to_bytes(), + inputs.compressible_config, + )?; + + Ok(()) +} diff --git a/programs/compressed-token/program/src/extensions/metadata_pointer.rs b/programs/compressed-token/program/src/extensions/metadata_pointer.rs new file mode 100644 index 0000000000..18ae1c2c6a --- /dev/null +++ b/programs/compressed-token/program/src/extensions/metadata_pointer.rs @@ -0,0 +1,63 @@ +use anchor_lang::prelude::ProgramError; +use light_compressed_account::instruction_data::data::ZOutputCompressedAccountWithPackedContextMut; +use light_ctoken_types::instructions::extensions::metadata_pointer::{ + MetadataPointer, MetadataPointerConfig, ZInitMetadataPointer, +}; +use light_hasher::DataHasher; +use light_zero_copy::ZeroCopyNew; + +pub fn create_output_metadata_pointer<'a>( + metadata_pointer_data: &ZInitMetadataPointer<'a>, + output_compressed_account: &mut ZOutputCompressedAccountWithPackedContextMut<'a>, + start_offset: usize, +) -> Result<([u8; 32], usize), ProgramError> { + if metadata_pointer_data.authority.is_none() && metadata_pointer_data.metadata_address.is_none() + { + return Err(anchor_lang::prelude::ProgramError::InvalidInstructionData); + } + + let cpi_data = output_compressed_account + .compressed_account + .data + .as_mut() + .ok_or(ProgramError::InvalidInstructionData)?; + + let config = MetadataPointerConfig { + authority: (metadata_pointer_data.authority.is_some(), ()), + metadata_address: (metadata_pointer_data.metadata_address.is_some(), ()), + }; + let byte_len = MetadataPointer::byte_len(&config); + let end_offset = start_offset + byte_len; + + println!("MetadataPointer::new_zero_copy - start_offset: {}, end_offset: {}, total_data_len: {}, slice_len: {}", + start_offset, end_offset, cpi_data.data.len(), end_offset - start_offset); + println!( + "Data slice at offset: {:?}", + &cpi_data.data[start_offset..std::cmp::min(start_offset + 32, cpi_data.data.len())] + ); + let (metadata_pointer, _) = + MetadataPointer::new_zero_copy(&mut cpi_data.data[start_offset..end_offset], config)?; + if let Some(mut authority) = metadata_pointer.authority { + *authority = *metadata_pointer_data + .authority + .ok_or(ProgramError::InvalidInstructionData)?; + } + if let Some(mut metadata_address) = metadata_pointer.metadata_address { + *metadata_address = *metadata_pointer_data + .metadata_address + .ok_or(ProgramError::InvalidInstructionData)?; + } + + // Create the actual MetadataPointer struct for hashing + let metadata_pointer_for_hash = MetadataPointer { + authority: metadata_pointer_data.authority.map(|a| *a), + metadata_address: metadata_pointer_data.metadata_address.map(|a| *a), + }; + + let hash = metadata_pointer_for_hash + .hash::() + .map_err(|_| ProgramError::InvalidAccountData)?; + + Ok((hash, end_offset)) +} +// TODO: add update diff --git a/programs/compressed-token/program/src/extensions/mod.rs b/programs/compressed-token/program/src/extensions/mod.rs new file mode 100644 index 0000000000..7284eb32e7 --- /dev/null +++ b/programs/compressed-token/program/src/extensions/mod.rs @@ -0,0 +1,83 @@ +// pub mod metadata_pointer; +pub mod processor; +pub mod token_metadata; +pub mod token_metadata_ui; + +// Import from ctoken-types instead of local modules +use light_ctoken_types::{ + instructions::extensions::ZExtensionInstructionData, + state::{ + AdditionalMetadataConfig, ExtensionStructConfig, MetadataConfig, TokenMetadata, + TokenMetadataConfig, + }, + CTokenError, +}; +use light_zero_copy::ZeroCopyNew; + +/// Processes extension instruction data and returns the configuration tuple and additional data length +/// Returns: (has_extensions, extension_configs, additional_data_len) +pub fn process_extensions_config( + extensions: Option<&Vec>, +) -> Result<(bool, Vec, usize), CTokenError> { + if let Some(extensions) = extensions { + let mut additional_mint_data_len = 0; + let mut config_vec = Vec::new(); + + for extension in extensions.iter() { + match extension { + /* ZExtensionInstructionData::MetadataPointer(extension) => { + let config = MetadataPointerConfig { + authority: (extension.authority.is_some(), ()), + metadata_address: (extension.metadata_address.is_some(), ()), + }; + let byte_len = MetadataPointer::byte_len(&config); + additional_mint_data_len += byte_len; + config_vec.push(ExtensionStructConfig::MetadataPointer(config)); + }*/ + ZExtensionInstructionData::TokenMetadata(token_metadata_data) => { + process_token_metadata_config( + &mut additional_mint_data_len, + &mut config_vec, + token_metadata_data, + ) + } + _ => return Err(CTokenError::UnsupportedExtension), + } + } + Ok((true, config_vec, additional_mint_data_len)) + } else { + Ok((false, Vec::new(), 0)) + } +} + +fn process_token_metadata_config( + additional_mint_data_len: &mut usize, + config_vec: &mut Vec, + token_metadata_data: &light_ctoken_types::instructions::extensions::ZTokenMetadataInstructionData<'_>, +) { + let additional_metadata_configs = + if let Some(ref additional_metadata) = token_metadata_data.additional_metadata { + additional_metadata + .iter() + .map(|item| AdditionalMetadataConfig { + key: item.key.len() as u32, + value: item.value.len() as u32, + }) + .collect() + } else { + vec![] + }; + + let config = TokenMetadataConfig { + update_authority: (token_metadata_data.update_authority.is_some(), ()), + metadata: MetadataConfig { + name: token_metadata_data.metadata.name.len() as u32, + symbol: token_metadata_data.metadata.symbol.len() as u32, + uri: token_metadata_data.metadata.uri.len() as u32, + }, + additional_metadata: additional_metadata_configs, + }; + let byte_len = TokenMetadata::byte_len(&config); + *additional_mint_data_len += byte_len; + config_vec.push(ExtensionStructConfig::TokenMetadata(config)); +} diff --git a/programs/compressed-token/program/src/extensions/processor.rs b/programs/compressed-token/program/src/extensions/processor.rs new file mode 100644 index 0000000000..8485e9116b --- /dev/null +++ b/programs/compressed-token/program/src/extensions/processor.rs @@ -0,0 +1,54 @@ +use anchor_lang::prelude::ProgramError; +use light_ctoken_types::{context::TokenContext, state::ZExtensionStructMut}; +use light_hasher::Hasher; +use pinocchio::pubkey::Pubkey; + +use crate::extensions::{token_metadata::create_output_token_metadata, ZExtensionInstructionData}; + +/// Set extensions state in output compressed account. +/// Compute extensions hash chain. +pub fn extensions_state_in_output_compressed_account( + extensions: &[ZExtensionInstructionData<'_>], + extension_in_output_compressed_account: &mut [ZExtensionStructMut<'_>], + mint: light_compressed_account::Pubkey, +) -> Result<(), ProgramError> { + if extension_in_output_compressed_account.len() != extensions.len() { + return Err(ProgramError::InvalidInstructionData); + } + for (extension, output_extension) in extensions + .iter() + .zip(extension_in_output_compressed_account.iter_mut()) + { + match (extension, output_extension) { + /*( + ZExtensionInstructionData::MetadataPointer(_extension), + ZExtensionStructMut::MetadataPointer(_output_extension), + ) => { + create_output_metadata_pointer(extension, output_extension, start_offset)?; + }*/ + ( + ZExtensionInstructionData::TokenMetadata(extension), + ZExtensionStructMut::TokenMetadata(output_extension), + ) => create_output_token_metadata(extension, output_extension, mint)?, + _ => { + return Err(ProgramError::InvalidInstructionData); + } + }; + } + Ok(()) +} + +/// Creates extension hash chain for +pub fn create_extension_hash_chain( + extensions: &[ZExtensionInstructionData<'_>], + hashed_spl_mint: &Pubkey, + context: &mut TokenContext, +) -> Result<[u8; 32], ProgramError> { + let mut extension_hashchain = [0u8; 32]; + for extension in extensions { + let extension_hash = extension.hash::(hashed_spl_mint, context)?; + extension_hashchain = + H::hashv(&[extension_hashchain.as_slice(), extension_hash.as_slice()])?; + } + Ok(extension_hashchain) +} diff --git a/programs/compressed-token/program/src/extensions/token_metadata.rs b/programs/compressed-token/program/src/extensions/token_metadata.rs new file mode 100644 index 0000000000..7fa7aab48c --- /dev/null +++ b/programs/compressed-token/program/src/extensions/token_metadata.rs @@ -0,0 +1,54 @@ +use anchor_lang::prelude::ProgramError; +use light_compressed_account::Pubkey; +use light_ctoken_types::{ + instructions::extensions::token_metadata::ZTokenMetadataInstructionData, + state::ZTokenMetadataMut, +}; + +pub fn create_output_token_metadata( + token_metadata_data: &ZTokenMetadataInstructionData<'_>, + token_metadata: &mut ZTokenMetadataMut<'_>, + mint: Pubkey, +) -> Result<(), ProgramError> { + if let Some(ref mut authority) = token_metadata.update_authority { + **authority = *token_metadata_data + .update_authority + .ok_or(ProgramError::InvalidInstructionData)?; + } + token_metadata + .metadata + .name + .copy_from_slice(token_metadata_data.metadata.name); + token_metadata + .metadata + .symbol + .copy_from_slice(token_metadata_data.metadata.symbol); + token_metadata + .metadata + .uri + .copy_from_slice(token_metadata_data.metadata.uri); + + // Set mint + *token_metadata.mint = mint; + + // Set version + *token_metadata.version = token_metadata_data.version; + + // Set additional metadata if provided + if let Some(ref additional_metadata) = token_metadata_data.additional_metadata { + for (i, item) in additional_metadata.iter().enumerate() { + token_metadata.additional_metadata[i] + .key + .copy_from_slice(item.key); + token_metadata.additional_metadata[i] + .value + .copy_from_slice(item.value); + } + } + + // Use the zero-copy mut struct for hashing + // let hash = token_metadata + // .hash::() + // .map_err(|_| ProgramError::InvalidAccountData)?; + Ok(()) +} diff --git a/programs/compressed-token/program/src/extensions/token_metadata_ui.rs b/programs/compressed-token/program/src/extensions/token_metadata_ui.rs new file mode 100644 index 0000000000..51e717d3c7 --- /dev/null +++ b/programs/compressed-token/program/src/extensions/token_metadata_ui.rs @@ -0,0 +1,41 @@ +use borsh::{BorshDeserialize, BorshSerialize}; +use light_sdk::LightHasher; +use solana_pubkey::Pubkey; + +// TODO: add borsh compat test TokenMetadataUi TokenMetadata +/// Ui Token metadata with Strings instead of bytes. +#[derive(Debug, Clone, PartialEq, Eq, BorshSerialize, BorshDeserialize)] +pub struct TokenMetadataUi { + // TODO: decide whether to move down for more efficient zero copy. Or impl manual zero copy. + /// The authority that can sign to update the metadata + pub update_authority: Option, + // TODO: decide whether to keep this. + /// The associated mint, used to counter spoofing to be sure that metadata + /// belongs to a particular mint + pub mint: Pubkey, + pub metadata: MetadataUi, + /// Any additional metadata about the token as key-value pairs. The program + /// must avoid storing the same key twice. + pub additional_metadata: Vec, + // TODO: decide whether to do this on this or MintAccount level + /// 0: Poseidon, 1: Sha256, 2: Keccak256, 3: Sha256Flat + pub version: u8, +} + +#[derive(Debug, LightHasher, Clone, PartialEq, Eq, BorshSerialize, BorshDeserialize)] +pub struct MetadataUi { + /// The longer name of the token + pub name: String, + /// The shortened symbol for the token + pub symbol: String, + /// The URI pointing to richer metadata + pub uri: String, +} + +#[derive(Debug, Clone, PartialEq, Eq, BorshSerialize, BorshDeserialize)] +pub struct AdditionalMetadataUi { + /// The key of the metadata + pub key: String, + /// The value of the metadata + pub value: String, +} diff --git a/programs/compressed-token/program/src/lib.rs b/programs/compressed-token/program/src/lib.rs new file mode 100644 index 0000000000..369dcd6973 --- /dev/null +++ b/programs/compressed-token/program/src/lib.rs @@ -0,0 +1,139 @@ +use std::mem::ManuallyDrop; + +use anchor_lang::solana_program::program_error::ProgramError; +use light_sdk::{cpi::CpiSigner, derive_light_cpi_signer}; +use pinocchio::account_info::AccountInfo; +use spl_token::instruction::TokenInstruction; + +pub mod close_token_account; +pub mod convert_account_infos; +pub mod create_associated_token_account; +pub mod create_spl_mint; +pub mod create_token_account; +pub mod extensions; +pub mod mint; +pub mod mint_to_compressed; +pub mod shared; +pub mod transfer2; + +// Reexport the wrapped anchor program. +pub use ::anchor_compressed_token::*; +use close_token_account::processor::process_close_token_account; +use create_associated_token_account::processor::process_create_associated_token_account; +use create_spl_mint::processor::process_create_spl_mint; +use create_token_account::processor::process_create_token_account; +use mint::processor::process_create_compressed_mint; +use mint_to_compressed::processor::process_mint_to_compressed; + +pub const LIGHT_CPI_SIGNER: CpiSigner = + derive_light_cpi_signer!("cTokenmWW8bLPjZEBAUgYy3zKxQZW6VKi7bqNFEVv3m"); + +pub const MAX_ACCOUNTS: usize = 30; + +// Start light token instructions at 100 to skip spl-token program instrutions. +// When adding new instructions check anchor discriminators for collisions! +#[repr(u8)] +pub enum InstructionType { + DecompressedTransfer = 3, // SPL Token transfer + CloseTokenAccount = 9, // SPL Token CloseAccount + CreateCompressedMint = 100, + MintToCompressed = 101, + CreateSplMint = 102, + CreateAssociatedTokenAccount = 103, + Transfer2 = 104, + CreateTokenAccount = 18, // equivalen to SPL Token InitializeAccount3 + Other, +} + +impl From for InstructionType { + fn from(value: u8) -> Self { + match value { + 3 => InstructionType::DecompressedTransfer, + 9 => InstructionType::CloseTokenAccount, + 100 => InstructionType::CreateCompressedMint, + 101 => InstructionType::MintToCompressed, + 102 => InstructionType::CreateSplMint, + 103 => InstructionType::CreateAssociatedTokenAccount, // TODO: double check compatibility + 104 => InstructionType::Transfer2, + 18 => InstructionType::CreateTokenAccount, + _ => InstructionType::Other, + } + } +} + +#[cfg(not(feature = "cpi"))] +use pinocchio::program_entrypoint; + +use crate::{ + convert_account_infos::convert_account_infos, transfer2::processor::process_transfer2, +}; + +#[cfg(not(feature = "cpi"))] +program_entrypoint!(process_instruction); + +pub fn process_instruction( + program_id: &pinocchio::pubkey::Pubkey, + accounts: &[AccountInfo], + instruction_data: &[u8], +) -> Result<(), ProgramError> { + let discriminator = InstructionType::from(instruction_data[0]); + match discriminator { + InstructionType::DecompressedTransfer => { + let instruction = TokenInstruction::unpack(instruction_data)?; + match instruction { + TokenInstruction::Transfer { amount } => { + let account_infos = unsafe { convert_account_infos::(accounts)? }; + let program_id_pubkey = solana_pubkey::Pubkey::new_from_array(*program_id); + spl_token::processor::Processor::process_transfer( + &program_id_pubkey, + &account_infos, + amount, + None, + )?; + } + _ => return Err(ProgramError::InvalidInstructionData), + } + } + InstructionType::CreateCompressedMint => { + anchor_lang::solana_program::msg!("CreateCompressedMint"); + process_create_compressed_mint(accounts, &instruction_data[1..])?; + } + InstructionType::MintToCompressed => { + anchor_lang::solana_program::msg!("MintToCompressed"); + process_mint_to_compressed(accounts, &instruction_data[1..])?; + } + InstructionType::CreateSplMint => { + anchor_lang::solana_program::msg!("CreateSplMint"); + process_create_spl_mint(accounts, &instruction_data[1..])?; + } + InstructionType::CreateAssociatedTokenAccount => { + anchor_lang::solana_program::msg!("CreateAssociatedTokenAccount"); + process_create_associated_token_account(accounts, &instruction_data[1..])?; + } + InstructionType::CreateTokenAccount => { + anchor_lang::solana_program::msg!("CreateTokenAccount"); + process_create_token_account(accounts, &instruction_data[1..])?; + } + InstructionType::CloseTokenAccount => { + anchor_lang::solana_program::msg!("CloseTokenAccount"); + process_close_token_account(accounts, &instruction_data[1..])?; + } + InstructionType::Transfer2 => { + anchor_lang::solana_program::msg!("Transfer2"); + process_transfer2(accounts, &instruction_data[1..])?; + } + // anchor instructions have no discriminator conflicts with InstructionType + _ => { + let account_infos = unsafe { convert_account_infos::(accounts)? }; + let account_infos = ManuallyDrop::new(account_infos); + let solana_program_id = solana_pubkey::Pubkey::new_from_array(*program_id); + + entry( + &solana_program_id, + account_infos.as_slice(), + instruction_data, + )?; + } + } + Ok(()) +} diff --git a/programs/compressed-token/program/src/mint/accounts.rs b/programs/compressed-token/program/src/mint/accounts.rs new file mode 100644 index 0000000000..e84ea4b073 --- /dev/null +++ b/programs/compressed-token/program/src/mint/accounts.rs @@ -0,0 +1,58 @@ +use std::ops::Deref; + +use anchor_lang::solana_program::program_error::ProgramError; +use light_account_checks::checks::check_signer; +use pinocchio::{account_info::AccountInfo, pubkey::Pubkey}; + +use crate::shared::{ + accounts::{CreateCompressedAccountTreeAccounts, LightSystemAccounts}, + AccountIterator, +}; + +pub struct CreateCompressedMintAccounts<'info> { + pub mint_signer: &'info AccountInfo, + pub light_system_program: &'info AccountInfo, + pub system: LightSystemAccounts<'info>, + pub trees: CreateCompressedAccountTreeAccounts<'info>, +} + +impl<'info> Deref for CreateCompressedMintAccounts<'info> { + type Target = LightSystemAccounts<'info>; + + fn deref(&self) -> &Self::Target { + &self.system + } +} + +impl CreateCompressedMintAccounts<'_> { + pub const CPI_ACCOUNTS_OFFSET: usize = 2; +} + +impl<'info> CreateCompressedMintAccounts<'info> { + pub fn validate_and_parse(accounts: &'info [AccountInfo]) -> Result { + let mut iter = AccountIterator::new(accounts); + + // Static non-CPI accounts first + let mint_signer = iter.next_account("mint_signer")?; + let light_system_program = iter.next_account("light_system_program")?; + + let system = LightSystemAccounts::validate_and_parse(&mut iter)?; + + let trees = CreateCompressedAccountTreeAccounts::validate_and_parse(&mut iter)?; + + // Validate mint_signer: must be signer + check_signer(mint_signer)?; + + Ok(CreateCompressedMintAccounts { + mint_signer, + light_system_program, + system, + trees, + }) + } + + #[inline(always)] + pub fn tree_pubkeys(&self) -> [&'info Pubkey; 2] { + self.trees.pubkeys() + } +} diff --git a/programs/compressed-token/program/src/mint/mint_input.rs b/programs/compressed-token/program/src/mint/mint_input.rs new file mode 100644 index 0000000000..dd1bfe0fae --- /dev/null +++ b/programs/compressed-token/program/src/mint/mint_input.rs @@ -0,0 +1,87 @@ +use anchor_lang::solana_program::program_error::ProgramError; +use light_compressed_account::instruction_data::with_readonly::ZInAccountMut; +use light_ctoken_types::{ + context::TokenContext, + instructions::create_compressed_mint::ZUpdateCompressedMintInstructionData, + state::CompressedMint, +}; +use light_hasher::{Hasher, Poseidon}; +use light_sdk::instruction::PackedMerkleContext; + +use crate::{ + constants::COMPRESSED_MINT_DISCRIMINATOR, extensions::processor::create_extension_hash_chain, +}; + +/// Creates and validates an input compressed mint account. +/// This function follows the same pattern as create_output_compressed_mint_account +/// but processes existing compressed mint accounts as inputs. +/// +/// Steps: +/// 1. Set InAccount fields (discriminator, merkle context, address) +/// 2. Validate the compressed mint data matches expected values +/// 3. Compute data hash using TokenContext for caching +/// 4. Return validated CompressedMint data for output processing +pub fn create_input_compressed_mint_account( + input_compressed_account: &mut ZInAccountMut, + context: &mut TokenContext, + compressed_mint_inputs: &ZUpdateCompressedMintInstructionData, + hashed_mint_authority: &[u8; 32], + merkle_context: PackedMerkleContext, +) -> Result<(), ProgramError> { + // 2. Extract and validate compressed mint data + let compressed_mint_input = &compressed_mint_inputs.mint; + //TODO: extract into function and test vs output hash creation + // 1. Compute data hash using TokenContext for caching + let data_hash = { + let hashed_spl_mint = context + .get_or_hash_mint(&compressed_mint_input.spl_mint.into()) + .map_err(ProgramError::from)?; + let mut supply_bytes = [0u8; 32]; + supply_bytes[24..] + .copy_from_slice(compressed_mint_input.supply.get().to_be_bytes().as_slice()); + + let hashed_freeze_authority = compressed_mint_input + .freeze_authority + .as_ref() + .map(|freeze_authority| context.get_or_hash_pubkey(&(**freeze_authority).to_bytes())); + + // Compute the data hash using the CompressedMint hash function + let data_hash = CompressedMint::hash_with_hashed_values( + &hashed_spl_mint, + &supply_bytes, + compressed_mint_input.decimals, + compressed_mint_input.is_decompressed(), + &Some(hashed_mint_authority), // pre-hashed mint_authority from signer + &hashed_freeze_authority.as_ref(), + compressed_mint_input.version, + ) + .map_err(|_| ProgramError::InvalidAccountData)?; + + let extension_hashchain = + compressed_mint_inputs + .mint + .extensions + .as_ref() + .map(|extensions| { + create_extension_hash_chain::(extensions, &hashed_spl_mint, context) + }); + if let Some(extension_hashchain) = extension_hashchain { + Poseidon::hashv(&[data_hash.as_slice(), extension_hashchain?.as_slice()])? + } else { + data_hash + } + }; + + // 2. Set InAccount fields + + input_compressed_account.set( + COMPRESSED_MINT_DISCRIMINATOR, + data_hash, + &merkle_context, + compressed_mint_inputs.root_index, + 0, + Some(compressed_mint_inputs.address.as_ref()), + )?; + + Ok(()) +} diff --git a/programs/compressed-token/program/src/mint/mint_output.rs b/programs/compressed-token/program/src/mint/mint_output.rs new file mode 100644 index 0000000000..d8b909744f --- /dev/null +++ b/programs/compressed-token/program/src/mint/mint_output.rs @@ -0,0 +1,128 @@ +use anchor_lang::solana_program::program_error::ProgramError; +use light_compressed_account::{ + instruction_data::data::ZOutputCompressedAccountWithPackedContextMut, Pubkey, +}; +use light_ctoken_types::{ + context::TokenContext, + instructions::{ + extensions::ZExtensionInstructionData, mint_to_compressed::ZCompressedMintInputs, + }, + state::{CompressedMint, CompressedMintConfig}, +}; +use light_hasher::Poseidon; +use light_zero_copy::ZeroCopyNew; +use zerocopy::little_endian::U64; + +use crate::{ + constants::COMPRESSED_MINT_DISCRIMINATOR, + extensions::processor::{ + create_extension_hash_chain, extensions_state_in_output_compressed_account, + }, +}; + +/// Input struct for create_output_compressed_mint_account function +/// Consolidates all parameters needed to create an output compressed mint account +pub struct CreateOutputCompressedMintAccountInputs<'a, 'b> { + /// The mint PDA address + pub mint_pda: Pubkey, + /// Number of decimal places + pub decimals: u8, + /// Optional freeze authority + pub freeze_authority: Option, + /// Optional mint authority + pub mint_authority: Option, + /// Token supply + pub supply: U64, + /// Mint configuration for zero-copy + pub mint_config: CompressedMintConfig, + /// Compressed account address + pub compressed_account_address: [u8; 32], + /// Merkle tree index + pub merkle_tree_index: u8, + /// Version for upgradability + pub version: u8, + /// Whether the mint is decompressed + pub is_decompressed: bool, + pub compressed_mint_input: ZCompressedMintInputs<'a>, + /// Optional extensions + pub extensions: Option<&'a [ZExtensionInstructionData<'b>]>, +} + +// TODO: pass in struct +#[allow(clippy::too_many_arguments)] +pub fn create_output_compressed_mint_account( + output_compressed_account: &mut ZOutputCompressedAccountWithPackedContextMut<'_>, + mint_pda: Pubkey, + decimals: u8, + freeze_authority: Option, + mint_authority: Option, + supply: U64, + mint_config: CompressedMintConfig, + compressed_account_address: [u8; 32], + merkle_tree_index: u8, + version: u8, + is_decompressed: bool, + extensions: Option<&[ZExtensionInstructionData<'_>]>, + context: &mut TokenContext, +) -> Result<(), ProgramError> { + // 1. Set CompressedMint account data & compute hash + let data_hash = { + let compressed_account_data = output_compressed_account + .compressed_account + .data + .as_mut() + .ok_or(ProgramError::InvalidAccountData)?; + + let (mut compressed_mint, _) = + CompressedMint::new_zero_copy(compressed_account_data.data, mint_config) + .map_err(ProgramError::from)?; + compressed_mint.set( + version, + mint_pda, + supply, + decimals, + is_decompressed, + mint_authority, + freeze_authority, + )?; + + // Process extensions if provided and populate the zero-copy extension data + let extension_hash = if let Some(extensions) = extensions.as_ref() { + let z_extensions = compressed_mint + .extensions + .as_mut() + .ok_or(ProgramError::AccountAlreadyInitialized)?; + + extensions_state_in_output_compressed_account( + extensions, + z_extensions.as_mut_slice(), + mint_pda, + )?; + let hashed_spl_mint = context.get_or_hash_mint(&mint_pda.into())?; + + Some(create_extension_hash_chain::( + extensions, + &hashed_spl_mint, + context, + )?) + } else { + None + }; + // Compute final hash with extensions + compressed_mint + .hash(extension_hash, context) + .map_err(|_| ProgramError::InvalidAccountData)? + }; + + // 2. Set output compressed account + output_compressed_account.set( + crate::LIGHT_CPI_SIGNER.program_id.into(), + 0, + Some(compressed_account_address), + merkle_tree_index, + COMPRESSED_MINT_DISCRIMINATOR, + data_hash, + )?; + + Ok(()) +} diff --git a/programs/compressed-token/program/src/mint/mod.rs b/programs/compressed-token/program/src/mint/mod.rs new file mode 100644 index 0000000000..adf1b12364 --- /dev/null +++ b/programs/compressed-token/program/src/mint/mod.rs @@ -0,0 +1,5 @@ +pub mod accounts; +pub mod mint_input; +pub mod mint_output; +pub mod processor; +pub mod zero_copy_config; diff --git a/programs/compressed-token/program/src/mint/processor.rs b/programs/compressed-token/program/src/mint/processor.rs new file mode 100644 index 0000000000..8854bad3ac --- /dev/null +++ b/programs/compressed-token/program/src/mint/processor.rs @@ -0,0 +1,105 @@ +use anchor_lang::solana_program::program_error::ProgramError; +use light_compressed_account::{ + instruction_data::with_readonly::InstructionDataInvokeCpiWithReadOnly, Pubkey, +}; +use light_ctoken_types::{ + context::TokenContext, + instructions::create_compressed_mint::CreateCompressedMintInstructionData, + COMPRESSED_MINT_SEED, +}; +use light_zero_copy::{borsh::Deserialize, ZeroCopyNew}; +use pinocchio::account_info::AccountInfo; +use spl_token::solana_program::log::sol_log_compute_units; + +use crate::{ + mint::{ + accounts::CreateCompressedMintAccounts, mint_output::create_output_compressed_mint_account, + zero_copy_config::get_zero_copy_configs, + }, + shared::{cpi::execute_cpi_invoke, cpi_bytes_size::allocate_invoke_with_read_only_cpi_bytes}, +}; + +/// Checks: +/// 1. check mint_signer (compressed mint randomness) is signer +/// 2. +pub fn process_create_compressed_mint( + accounts: &[AccountInfo], + instruction_data: &[u8], +) -> Result<(), ProgramError> { + sol_log_compute_units(); + let (parsed_instruction_data, _) = + CreateCompressedMintInstructionData::zero_copy_at(instruction_data) + .map_err(|_| ProgramError::InvalidInstructionData)?; + sol_log_compute_units(); + + // Validate and parse accounts + let validated_accounts = CreateCompressedMintAccounts::validate_and_parse(accounts)?; + + // 1. Create spl mint PDA using provided bump + // - The compressed address is derived from the spl_mint_pda. + // - The spl mint pda is used as mint in compressed token accounts. + let spl_mint_pda: Pubkey = solana_pubkey::Pubkey::create_program_address( + &[ + COMPRESSED_MINT_SEED, + validated_accounts.mint_signer.key().as_slice(), + &[parsed_instruction_data.mint_bump], + ], + &crate::ID, + )? + .into(); + + let (mint_size_config, config) = get_zero_copy_configs(&parsed_instruction_data)?; + + // + discriminator len + vector len + let mut cpi_bytes = allocate_invoke_with_read_only_cpi_bytes(&config); + + sol_log_compute_units(); + let (mut cpi_instruction_struct, _) = + InstructionDataInvokeCpiWithReadOnly::new_zero_copy(&mut cpi_bytes[8..], config) + .map_err(ProgramError::from)?; + cpi_instruction_struct.initialize( + crate::LIGHT_CPI_SIGNER.bump, + &crate::LIGHT_CPI_SIGNER.program_id.into(), + Some(parsed_instruction_data.proof), + None, + )?; + + sol_log_compute_units(); + // 2. Create NewAddressParams + let address_merkle_tree_account_index = 0; + let assigned_account_index = 0; + cpi_instruction_struct.new_address_params[0].set( + spl_mint_pda.to_bytes(), + *parsed_instruction_data.address_merkle_tree_root_index, + Some(assigned_account_index), + address_merkle_tree_account_index, + ); + // 3. Create compressed mint account data + // TODO: add input struct, try to use CompressedMintInput + // TODO: bench performance input struct vs direct inputs. + let mut token_context = TokenContext::new(); + create_output_compressed_mint_account( + &mut cpi_instruction_struct.output_compressed_accounts[0], + spl_mint_pda, + parsed_instruction_data.decimals, + parsed_instruction_data.freeze_authority.map(|fa| *fa), + Some(parsed_instruction_data.mint_authority), + 0.into(), + mint_size_config, + *parsed_instruction_data.mint_address, + 1, + parsed_instruction_data.version, + false, // Set is_decompressed = false for new mint creation + parsed_instruction_data.extensions.as_deref(), + &mut token_context, + )?; + sol_log_compute_units(); + // 4. Execute CPI to light-system-program + execute_cpi_invoke( + &accounts[CreateCompressedMintAccounts::CPI_ACCOUNTS_OFFSET..], + cpi_bytes, + validated_accounts.tree_pubkeys().as_slice(), + false, // no sol_pool_pda for create_compressed_mint + None, // no cpi_context_account for create_compressed_mint + ) +} diff --git a/programs/compressed-token/program/src/mint/zero_copy_config.rs b/programs/compressed-token/program/src/mint/zero_copy_config.rs new file mode 100644 index 0000000000..13e559dcf6 --- /dev/null +++ b/programs/compressed-token/program/src/mint/zero_copy_config.rs @@ -0,0 +1,63 @@ +use anchor_lang::solana_program::program_error::ProgramError; +use light_compressed_account::{ + compressed_account::{CompressedAccountConfig, CompressedAccountDataConfig}, + instruction_data::{ + compressed_proof::CompressedProofConfig, cpi_context::CompressedCpiContextConfig, + data::OutputCompressedAccountWithPackedContextConfig, + with_readonly::InstructionDataInvokeCpiWithReadOnlyConfig, + }, +}; +use light_ctoken_types::state::{CompressedMint, CompressedMintConfig}; +use light_sdk_pinocchio::NewAddressParamsAssignedPackedConfig; +use light_zero_copy::ZeroCopyNew; + +// TODO: unit test. +pub fn get_zero_copy_configs( + parsed_instruction_data: &light_ctoken_types::instructions::create_compressed_mint::ZCreateCompressedMintInstructionData<'_>, +) -> Result< + ( + CompressedMintConfig, + InstructionDataInvokeCpiWithReadOnlyConfig, + ), + ProgramError, +> { + let (compressed_mint_len, mint_size_config) = { + let (has_extensions, extensions_config, additional_mint_data_len) = + crate::extensions::process_extensions_config( + parsed_instruction_data.extensions.as_ref(), + )?; + let mint_size_config: ::ZeroCopyConfig = + CompressedMintConfig { + mint_authority: (true, ()), + freeze_authority: (parsed_instruction_data.freeze_authority.is_some(), ()), + extensions: (has_extensions, extensions_config), + }; + ( + (CompressedMint::byte_len(&mint_size_config) + additional_mint_data_len) as u32, + mint_size_config, + ) + }; + let output_compressed_accounts = vec![OutputCompressedAccountWithPackedContextConfig { + compressed_account: CompressedAccountConfig { + address: (true, ()), + data: ( + true, + CompressedAccountDataConfig { + data: compressed_mint_len, + }, + ), + }, + }]; + let new_address_params = vec![NewAddressParamsAssignedPackedConfig {}]; + let config = InstructionDataInvokeCpiWithReadOnlyConfig { + cpi_context: CompressedCpiContextConfig {}, + input_compressed_accounts: vec![], + // We always need a proof to create the compressed address. + proof: (true, CompressedProofConfig {}), + read_only_accounts: vec![], + read_only_addresses: vec![], + new_address_params, + output_compressed_accounts, + }; + Ok((mint_size_config, config)) +} diff --git a/programs/compressed-token/program/src/mint_to_compressed/accounts.rs b/programs/compressed-token/program/src/mint_to_compressed/accounts.rs new file mode 100644 index 0000000000..67bd1afd88 --- /dev/null +++ b/programs/compressed-token/program/src/mint_to_compressed/accounts.rs @@ -0,0 +1,81 @@ +use std::ops::Deref; + +use anchor_lang::solana_program::program_error::ProgramError; +use light_account_checks::checks::check_signer; +use pinocchio::account_info::AccountInfo; + +use crate::shared::{ + accounts::{LightSystemAccounts, UpdateOneCompressedAccountTreeAccounts}, + AccountIterator, +}; + +pub struct MintToCompressedAccounts<'info> { + pub authority: &'info AccountInfo, + pub mint: Option<&'info AccountInfo>, + pub token_pool_pda: Option<&'info AccountInfo>, + pub token_program: Option<&'info AccountInfo>, + pub light_system_program: &'info AccountInfo, + pub system: LightSystemAccounts<'info>, + pub sol_pool_pda: Option<&'info AccountInfo>, + pub tree_accounts: UpdateOneCompressedAccountTreeAccounts<'info>, + pub tokens_out_queue: &'info AccountInfo, +} + +impl<'info> Deref for MintToCompressedAccounts<'info> { + type Target = LightSystemAccounts<'info>; + + fn deref(&self) -> &Self::Target { + &self.system + } +} + +impl<'info> MintToCompressedAccounts<'info> { + pub fn validate_and_parse( + accounts: &'info [AccountInfo], + with_lamports: bool, + is_decompressed: bool, + ) -> Result { + let mut iter = AccountIterator::new(accounts); + + // Static non-CPI accounts first + let authority = iter.next_account("authority")?; + + let (mint, token_pool_pda, token_program) = if is_decompressed { + ( + Some(iter.next_account("mint")?), + Some(iter.next_account("token_pool_pda")?), + Some(iter.next_account("token_program")?), + ) + } else { + (None, None, None) + }; + + let light_system_program = iter.next_account("light_system_program")?; + + let system = LightSystemAccounts::validate_and_parse(&mut iter)?; + + let sol_pool_pda = if with_lamports { + Some(iter.next_account("sol_pool_pda")?) + } else { + None + }; + + let tree_accounts = UpdateOneCompressedAccountTreeAccounts::validate_and_parse(&mut iter)?; + let tokens_out_queue = iter.next_account("tokens_out_queue")?; + + // Validate authority: must be signer + check_signer(authority)?; + + Ok(MintToCompressedAccounts { + authority, + mint, + token_pool_pda, + token_program, + light_system_program, + system, + sol_pool_pda, + tree_accounts, + tokens_out_queue, + }) + } +} diff --git a/programs/compressed-token/program/src/mint_to_compressed/mod.rs b/programs/compressed-token/program/src/mint_to_compressed/mod.rs new file mode 100644 index 0000000000..2e42d63ac6 --- /dev/null +++ b/programs/compressed-token/program/src/mint_to_compressed/mod.rs @@ -0,0 +1,2 @@ +pub mod accounts; +pub mod processor; diff --git a/programs/compressed-token/program/src/mint_to_compressed/processor.rs b/programs/compressed-token/program/src/mint_to_compressed/processor.rs new file mode 100644 index 0000000000..2ccd320ecf --- /dev/null +++ b/programs/compressed-token/program/src/mint_to_compressed/processor.rs @@ -0,0 +1,262 @@ +use anchor_lang::solana_program::program_error::ProgramError; +use light_compressed_account::{ + instruction_data::with_readonly::InstructionDataInvokeCpiWithReadOnly, Pubkey, +}; +use light_ctoken_types::{ + context::TokenContext, instructions::mint_to_compressed::MintToCompressedInstructionData, + state::CompressedMintConfig, +}; +use light_sdk::instruction::PackedMerkleContext; +use light_zero_copy::{borsh::Deserialize, ZeroCopyNew}; +use pinocchio::account_info::AccountInfo; +use spl_token::solana_program::log::sol_log_compute_units; +use zerocopy::little_endian::U64; + +use crate::{ + mint::{ + mint_input::create_input_compressed_mint_account, + mint_output::create_output_compressed_mint_account, + }, + mint_to_compressed::accounts::MintToCompressedAccounts, + shared::{ + cpi::execute_cpi_invoke, + cpi_bytes_size::{ + allocate_invoke_with_read_only_cpi_bytes, cpi_bytes_config, CpiConfigInput, + }, + mint_to_token_pool, + token_output::set_output_compressed_account, + }, + LIGHT_CPI_SIGNER, +}; + +pub fn process_mint_to_compressed( + accounts: &[AccountInfo], + instruction_data: &[u8], +) -> Result<(), ProgramError> { + sol_log_compute_units(); + + // Parse instruction data using zero-copy + let (parsed_instruction_data, _) = + MintToCompressedInstructionData::zero_copy_at(instruction_data) + .map_err(|_| ProgramError::InvalidInstructionData)?; + + sol_log_compute_units(); + + // Validate and parse accounts + let validated_accounts = MintToCompressedAccounts::validate_and_parse( + accounts, + parsed_instruction_data.lamports.is_some(), + parsed_instruction_data + .compressed_mint_inputs + .mint + .is_decompressed(), + )?; + let (config, mut cpi_bytes) = get_zero_copy_configs(&parsed_instruction_data)?; + + sol_log_compute_units(); + let (mut cpi_instruction_struct, _) = + InstructionDataInvokeCpiWithReadOnly::new_zero_copy(&mut cpi_bytes[8..], config) + .map_err(ProgramError::from)?; + cpi_instruction_struct.bump = LIGHT_CPI_SIGNER.bump; + cpi_instruction_struct.invoking_program_id = LIGHT_CPI_SIGNER.program_id.into(); + if let Some(lamports) = parsed_instruction_data.lamports { + cpi_instruction_struct.compress_or_decompress_lamports = + U64::from(parsed_instruction_data.recipients.len() as u64) * *lamports; + cpi_instruction_struct.is_compress = 1; + } + + let mut context = TokenContext::new(); + let mint_pda = parsed_instruction_data.compressed_mint_inputs.mint.spl_mint; + + let hashed_mint_authority = context.get_or_hash_pubkey(validated_accounts.authority.key()); + + { + // Process input compressed mint account + create_input_compressed_mint_account( + &mut cpi_instruction_struct.input_compressed_accounts[0], + &mut context, + &parsed_instruction_data.compressed_mint_inputs, + &hashed_mint_authority, + PackedMerkleContext { + merkle_tree_pubkey_index: 0, + queue_pubkey_index: 1, + leaf_index: parsed_instruction_data + .compressed_mint_inputs + .leaf_index + .into(), + prove_by_index: parsed_instruction_data + .compressed_mint_inputs + .prove_by_index(), + }, + )?; + + let mint_inputs = &parsed_instruction_data.compressed_mint_inputs.mint; + let decimals = mint_inputs.decimals; + let freeze_authority = mint_inputs + .freeze_authority + .as_ref() + .map(|freeze_authority| (**freeze_authority)); + + // Process extensions from input mint + let (has_extensions, extensions_config, _) = + crate::extensions::process_extensions_config(mint_inputs.extensions.as_ref())?; + // TODO: get from get_zero_copy_configs + let mint_config = CompressedMintConfig { + mint_authority: (true, ()), + freeze_authority: (mint_inputs.freeze_authority.is_some(), ()), + extensions: (has_extensions, extensions_config), + }; + let sum_amounts: U64 = parsed_instruction_data + .recipients + .iter() + .map(|x| u64::from(x.amount)) + .sum::() + .into(); + let supply = mint_inputs.supply + sum_amounts; + + // Compressed mint account is the last output + create_output_compressed_mint_account( + &mut cpi_instruction_struct.output_compressed_accounts + [parsed_instruction_data.recipients.len()], + mint_pda, + decimals, + freeze_authority, + Some(Pubkey::from(*validated_accounts.authority.key())), + supply, + mint_config, + parsed_instruction_data.compressed_mint_inputs.address, + 2, + parsed_instruction_data.compressed_mint_inputs.mint.version, + parsed_instruction_data + .compressed_mint_inputs + .mint + .is_decompressed(), + mint_inputs.extensions.as_deref(), + &mut context, + )?; + } + + let is_decompressed = parsed_instruction_data + .compressed_mint_inputs + .mint + .is_decompressed(); + + // If mint is decompressed, mint tokens to the token pool to maintain SPL mint supply consistency + if is_decompressed { + let sum_amounts: u64 = parsed_instruction_data + .recipients + .iter() + .map(|x| u64::from(x.amount)) + .sum(); + + let mint_account = validated_accounts + .mint + .ok_or(ProgramError::InvalidAccountData)?; + let token_pool_account = validated_accounts + .token_pool_pda + .ok_or(ProgramError::InvalidAccountData)?; + let token_program = validated_accounts + .token_program + .ok_or(ProgramError::InvalidAccountData)?; + + mint_to_token_pool( + mint_account, + token_pool_account, + token_program, + validated_accounts.cpi_authority_pda, + sum_amounts, + )?; + } + + // Create output token accounts + create_output_compressed_token_accounts( + parsed_instruction_data, + cpi_instruction_struct, + &mut context, + mint_pda, + )?; + + // Extract tree accounts for the generalized CPI call + let tree_accounts = [ + validated_accounts.tree_accounts.in_merkle_tree.key(), + validated_accounts.tree_accounts.in_output_queue.key(), + validated_accounts.tree_accounts.out_output_queue.key(), + validated_accounts.tokens_out_queue.key(), + ]; + let start_index = if is_decompressed { 5 } else { 2 }; + + execute_cpi_invoke( + &accounts[start_index..], // Skip first 5 non-CPI accounts (authority, mint, token_pool_pda, token_program, light_system_program) + cpi_bytes, + tree_accounts.as_slice(), + validated_accounts.sol_pool_pda.is_some(), + None, // no cpi_context_account for mint_to_compressed + )?; + Ok(()) +} + + +fn get_zero_copy_configs(parsed_instruction_data: &light_ctoken_types::instructions::mint_to_compressed::ZMintToCompressedInstructionData<'_>) -> Result<(light_compressed_account::instruction_data::with_readonly::InstructionDataInvokeCpiWithReadOnlyConfig, Vec), ProgramError>{ + // Build configuration for CPI instruction data using the generalized function + let compressed_mint_with_freeze_authority = parsed_instruction_data + .compressed_mint_inputs + .mint + .freeze_authority + .is_some(); + + // Process extensions to get the proper config for CPI bytes allocation + // The mint contains ZExtensionInstructionData, so we can use process_extensions_config directly + let (_, extensions_config, _) = crate::extensions::process_extensions_config( + parsed_instruction_data + .compressed_mint_inputs + .mint + .extensions + .as_ref(), + )?; + + let mut config_input = CpiConfigInput::mint_to_compressed( + parsed_instruction_data.recipients.len(), + parsed_instruction_data.proof.is_some(), + compressed_mint_with_freeze_authority, + ); + // Override the empty extensions_config with the actual one + config_input.extensions_config = extensions_config; + + let config = cpi_bytes_config(config_input); + let cpi_bytes = allocate_invoke_with_read_only_cpi_bytes(&config); + + Ok((config, cpi_bytes)) +} + +fn create_output_compressed_token_accounts( + parsed_instruction_data: light_ctoken_types::instructions::mint_to_compressed::ZMintToCompressedInstructionData<'_>, + mut cpi_instruction_struct: light_compressed_account::instruction_data::with_readonly::ZInstructionDataInvokeCpiWithReadOnlyMut<'_>, + context: &mut TokenContext, + mint: Pubkey, +) -> Result<(), ProgramError> { + let hashed_mint = context.get_or_hash_mint(&mint.to_bytes())?; + + let lamports = parsed_instruction_data + .lamports + .map(|lamports| u64::from(*lamports)); + for (recipient, output_account) in parsed_instruction_data + .recipients + .iter() + .zip(cpi_instruction_struct.output_compressed_accounts.iter_mut()) + { + let output_delegate = None; + set_output_compressed_account::( + output_account, + context, + recipient.recipient, + output_delegate, + recipient.amount, + lamports, + mint, + &hashed_mint, + 2, + parsed_instruction_data.token_account_version, + )?; + } + Ok(()) +} diff --git a/programs/compressed-token/program/src/shared/accounts.rs b/programs/compressed-token/program/src/shared/accounts.rs new file mode 100644 index 0000000000..95d61a0519 --- /dev/null +++ b/programs/compressed-token/program/src/shared/accounts.rs @@ -0,0 +1,101 @@ +use anchor_lang::solana_program::program_error::ProgramError; +use light_account_checks::checks::{check_mut, check_signer}; +use pinocchio::{account_info::AccountInfo, pubkey::Pubkey}; + +use crate::shared::AccountIterator; + +pub struct LightSystemAccounts<'info> { + pub fee_payer: &'info AccountInfo, + pub cpi_authority_pda: &'info AccountInfo, + pub registered_program_pda: &'info AccountInfo, + pub noop_program: &'info AccountInfo, + pub account_compression_authority: &'info AccountInfo, + pub account_compression_program: &'info AccountInfo, + pub system_program: &'info AccountInfo, + pub self_program: &'info AccountInfo, +} + +impl<'info> LightSystemAccounts<'info> { + #[track_caller] + pub fn validate_and_parse( + iter: &mut AccountIterator<'info, AccountInfo>, + ) -> Result { + let fee_payer: &AccountInfo = iter.next_account("fee_payer")?; + // Validate fee_payer: must be signer and mutable + check_signer(fee_payer)?; + check_mut(fee_payer)?; + + Ok(Self { + fee_payer, + cpi_authority_pda: iter.next_account("cpi_authority_pda")?, + registered_program_pda: iter.next_account("registered_program_pda")?, + noop_program: iter.next_account("noop_program")?, + account_compression_authority: iter.next_account("account_compression_authority")?, + account_compression_program: iter.next_account("account_compression_program")?, + system_program: iter.next_account("system_program")?, + self_program: iter.next_account("self_program")?, + }) + } +} + +pub struct UpdateOneCompressedAccountTreeAccounts<'info> { + pub in_merkle_tree: &'info AccountInfo, + pub in_output_queue: &'info AccountInfo, + pub out_output_queue: &'info AccountInfo, +} + +impl<'info> UpdateOneCompressedAccountTreeAccounts<'info> { + #[track_caller] + pub fn validate_and_parse( + iter: &mut AccountIterator<'info, AccountInfo>, + ) -> Result { + let in_merkle_tree = iter.next_account("in_merkle_tree")?; + let in_output_queue = iter.next_account("in_output_queue")?; + let out_output_queue = iter.next_account("out_output_queue")?; + check_mut(in_merkle_tree)?; + check_mut(in_output_queue)?; + check_mut(out_output_queue)?; + + Ok(Self { + in_merkle_tree, + in_output_queue, + out_output_queue, + }) + } + + #[inline(always)] + pub fn pubkeys(&self) -> [&'info Pubkey; 3] { + [ + self.in_merkle_tree.key(), + self.in_output_queue.key(), + self.out_output_queue.key(), + ] + } +} + +pub struct CreateCompressedAccountTreeAccounts<'info> { + pub address_merkle_tree: &'info AccountInfo, + pub out_output_queue: &'info AccountInfo, +} + +impl<'info> CreateCompressedAccountTreeAccounts<'info> { + #[track_caller] + pub fn validate_and_parse( + iter: &mut AccountIterator<'info, AccountInfo>, + ) -> Result { + let address_merkle_tree = iter.next_account("address_merkle_tree")?; + let out_output_queue = iter.next_account("out_output_queue")?; + check_mut(address_merkle_tree)?; + check_mut(out_output_queue)?; + + Ok(Self { + address_merkle_tree, + out_output_queue, + }) + } + + #[inline(always)] + pub fn pubkeys(&self) -> [&'info Pubkey; 2] { + [self.address_merkle_tree.key(), self.out_output_queue.key()] + } +} diff --git a/programs/compressed-token/program/src/shared/cpi.rs b/programs/compressed-token/program/src/shared/cpi.rs new file mode 100644 index 0000000000..0530fd1d98 --- /dev/null +++ b/programs/compressed-token/program/src/shared/cpi.rs @@ -0,0 +1,188 @@ +use std::mem::MaybeUninit; + +use account_compression::utils::constants::NOOP_PUBKEY; +use anchor_lang::solana_program::program_error::ProgramError; +use light_sdk_types::{ + ACCOUNT_COMPRESSION_AUTHORITY_PDA, ACCOUNT_COMPRESSION_PROGRAM_ID, CPI_AUTHORITY_PDA_SEED, + LIGHT_SYSTEM_PROGRAM_ID, REGISTERED_PROGRAM_PDA, +}; +use pinocchio::{ + account_info::{AccountInfo, BorrowState}, + cpi::{invoke_signed_unchecked, MAX_CPI_ACCOUNTS}, + instruction::{Account, AccountMeta, Instruction, Seed, Signer}, + msg, + pubkey::Pubkey, +}; + +use crate::LIGHT_CPI_SIGNER; + +/// Generalized CPI function for invoking light-system-program +/// +/// This function builds the standard account meta structure for light-system-program CPI +/// and appends dynamic tree accounts (merkle trees, queues, etc.) to the account metas. +/// +/// # Arguments +/// * `accounts` - All account infos passed to the instruction +/// * `cpi_bytes` - The CPI instruction data bytes +/// * `tree_accounts` - Slice of tree account pubkeys to append (will be marked as mutable) +/// * `sol_pool_pda` - Optional sol pool PDA pubkey +/// * `cpi_context_account` - Optional CPI context account pubkey +/// +/// # Returns +/// * `Result<(), ProgramError>` - Success or error from the CPI call +pub fn execute_cpi_invoke( + accounts: &[AccountInfo], + cpi_bytes: Vec, + tree_accounts: &[&Pubkey], + with_sol_pool: bool, + cpi_context_account: Option, +) -> Result<(), ProgramError> { + if cpi_bytes[9] == 0 { + msg!("Bump not set in cpi struct."); + return Err(ProgramError::InvalidInstructionData); + } + // Build account metas with capacity for standard accounts + dynamic tree accounts + let capacity = 11 + tree_accounts.len(); // 11 standard accounts + dynamic tree accounts + // TODO: investigate why array vec is not working + // let mut account_metas = ArrayVec::::new(); + let mut account_metas = Vec::with_capacity(capacity); + + // Standard account metas for light-system-program CPI + // Account order must match light-system program's InvokeCpiInstruction expectation: + // 0: fee_payer, 1: authority, 2: registered_program_pda, 3: noop_program, + // 4: account_compression_authority, 5: account_compression_program, 6: invoking_program, + // 7: sol_pool_pda (optional), 8: decompression_recipient (optional), 9: system_program, + // 10: cpi_context_account (optional), then remaining accounts (merkle trees, etc.) + const INNER_POOL: [u8; 32] = + solana_pubkey::pubkey!("CHK57ywWSDncAoRu1F8QgwYJeXuAJyyBYT4LixLXvMZ1").to_bytes(); + let sol_pool_pda = if with_sol_pool { + AccountMeta::new(&INNER_POOL, true, false) + } else { + AccountMeta::new(&LIGHT_SYSTEM_PROGRAM_ID, false, false) + }; + // Add accounts one by one since extend_from_slice is private + account_metas.push(AccountMeta::new(accounts[0].key(), true, true)); // 0 fee_payer (signer, mutable) + account_metas.push(AccountMeta::new(&LIGHT_CPI_SIGNER.cpi_signer, false, true)); // 1 authority (cpi_authority_pda) + account_metas.push(AccountMeta::new(®ISTERED_PROGRAM_PDA, false, false)); // 2 registered_program_pda + account_metas.push(AccountMeta::new(&NOOP_PUBKEY, false, false)); // 3 noop_program + account_metas.push(AccountMeta::new( + &ACCOUNT_COMPRESSION_AUTHORITY_PDA, + false, + false, + )); // 4 account_compression_authority + account_metas.push(AccountMeta::new( + &ACCOUNT_COMPRESSION_PROGRAM_ID, + false, + false, + )); // 5 account_compression_program + account_metas.push(AccountMeta::new(&LIGHT_CPI_SIGNER.program_id, false, false)); // 6 invoking_program (self_program) + account_metas.push(sol_pool_pda); // 7 sol_pool_pda + account_metas.push(AccountMeta::new(&LIGHT_SYSTEM_PROGRAM_ID, false, false)); // 8 decompression_recipient (None, using default) + account_metas.push(AccountMeta::new(&[0u8; 32], false, false)); // system_program + account_metas.push(if let Some(cpi_context) = cpi_context_account.as_ref() { + AccountMeta::new(cpi_context, true, false) + } else { + AccountMeta::new(&LIGHT_SYSTEM_PROGRAM_ID, false, false) + }); // cpi_context_account + + // Append dynamic tree accounts (merkle trees, queues, etc.) as mutable accounts + for tree_account in tree_accounts { + account_metas.push(AccountMeta::new(tree_account, true, false)); + } + let instruction = Instruction { + program_id: &LIGHT_SYSTEM_PROGRAM_ID, + accounts: account_metas.as_slice(), + data: cpi_bytes.as_slice(), + }; + + // Use the precomputed CPI signer and bump from the config + let bump_seed = [LIGHT_CPI_SIGNER.bump]; + let seed_array = [ + Seed::from(CPI_AUTHORITY_PDA_SEED), + Seed::from(bump_seed.as_slice()), + ]; + let signer = Signer::from(&seed_array); + + match slice_invoke_signed(&instruction, accounts, &[signer]) { + Ok(()) => {} + Err(e) => { + msg!(format!("slice_invoke_signed failed: {:?}", e).as_str()); + return Err(ProgramError::InvalidArgument); + } + } + + Ok(()) +} + +#[inline] +pub fn slice_invoke_signed( + instruction: &Instruction, + account_infos: &[AccountInfo], + signers_seeds: &[Signer], +) -> pinocchio::ProgramResult { + use pinocchio::program_error::ProgramError; + if instruction.accounts.len() < account_infos.len() { + return Err(ProgramError::NotEnoughAccountKeys); + } + + if account_infos.len() > MAX_CPI_ACCOUNTS { + return Err(ProgramError::InvalidArgument); + } + + const UNINIT: MaybeUninit = MaybeUninit::::uninit(); + let mut accounts = [UNINIT; MAX_CPI_ACCOUNTS]; + let mut len = 0; + + for (account_info, account_meta) in account_infos.iter().zip( + instruction + .accounts + .iter() + .filter(|x| x.pubkey != instruction.program_id), + ) { + if account_info.key() != account_meta.pubkey { + use std::format; + msg!(format!( + "Received account key: {:?}", + solana_pubkey::Pubkey::new_from_array(*account_info.key()) + ) + .as_str()); + msg!(format!( + "Expected account key: {:?}", + solana_pubkey::Pubkey::new_from_array(*account_meta.pubkey) + ) + .as_str()); + + return Err(ProgramError::InvalidArgument); + } + + let state = if account_meta.is_writable { + BorrowState::Borrowed + } else { + BorrowState::MutablyBorrowed + }; + + if account_info.is_borrowed(state) { + return Err(ProgramError::AccountBorrowFailed); + } + + // SAFETY: The number of accounts has been validated to be less than + // `MAX_CPI_ACCOUNTS`. + unsafe { + accounts + .get_unchecked_mut(len) + .write(Account::from(account_info)); + } + + len += 1; + } + // SAFETY: The accounts have been validated. + unsafe { + invoke_signed_unchecked( + instruction, + core::slice::from_raw_parts(accounts.as_ptr() as _, len), + signers_seeds, + ); + } + + Ok(()) +} diff --git a/programs/compressed-token/program/src/shared/cpi_bytes_size.rs b/programs/compressed-token/program/src/shared/cpi_bytes_size.rs new file mode 100644 index 0000000000..e12606cfca --- /dev/null +++ b/programs/compressed-token/program/src/shared/cpi_bytes_size.rs @@ -0,0 +1,146 @@ +use anchor_lang::Discriminator; +use arrayvec::ArrayVec; +use light_compressed_account::{ + compressed_account::{ + CompressedAccountConfig, CompressedAccountDataConfig, PackedMerkleContextConfig, + }, + instruction_data::{ + compressed_proof::CompressedProofConfig, + cpi_context::CompressedCpiContextConfig, + data::OutputCompressedAccountWithPackedContextConfig, + with_readonly::{ + InAccountConfig, InstructionDataInvokeCpiWithReadOnly, + InstructionDataInvokeCpiWithReadOnlyConfig, + }, + }, +}; +use light_zero_copy::ZeroCopyNew; + +const MAX_INPUT_ACCOUNTS: usize = 8; +const MAX_OUTPUT_ACCOUNTS: usize = 35; + +#[derive(Debug, Clone)] +pub struct CpiConfigInput { + pub input_accounts: ArrayVec, // Per-input account delegate flag + pub output_accounts: ArrayVec, // Per-output account delegate flag + pub has_proof: bool, + pub compressed_mint: bool, + pub compressed_mint_with_freeze_authority: bool, + pub extensions_config: Vec, +} + +impl CpiConfigInput { + /// Helper to create config for mint_to_compressed with no delegates + pub fn mint_to_compressed( + num_recipients: usize, + has_proof: bool, + compressed_mint_with_freeze_authority: bool, + ) -> Self { + let mut output_delegates = ArrayVec::new(); + for _ in 0..num_recipients { + output_delegates.push(false); // No delegates for simple mint + } + + Self { + input_accounts: ArrayVec::new(), // No input accounts for mint_to_compressed + output_accounts: output_delegates, + has_proof, + compressed_mint: true, + compressed_mint_with_freeze_authority, + extensions_config: vec![], + } + } +} + +// TODO: add version of this function with hardcoded values that just calculates the cpi_byte_size, with a randomized test vs this function +pub fn cpi_bytes_config(input: CpiConfigInput) -> InstructionDataInvokeCpiWithReadOnlyConfig { + let input_compressed_accounts = { + let mut inputs_capacity = input.input_accounts.len(); + if input.compressed_mint { + inputs_capacity += 1; + } + let mut input_compressed_accounts = Vec::with_capacity(inputs_capacity); + + // Add regular input accounts (token accounts) + for _ in input.input_accounts { + input_compressed_accounts.push(InAccountConfig { + merkle_context: PackedMerkleContextConfig {}, // Default merkle context + address: (false, ()), // Token accounts don't have addresses + }); + } + + // Add compressed mint input account if needed + if input.compressed_mint { + input_compressed_accounts.push(InAccountConfig { + merkle_context: PackedMerkleContextConfig {}, // Default merkle context + address: (true, ()), + }); + } + + input_compressed_accounts + }; + + let output_compressed_accounts = { + { + let total_outputs = input.output_accounts.len() + if input.has_proof { 1 } else { 0 }; + let mut outputs = Vec::with_capacity(total_outputs); + for has_delegate in input.output_accounts { + let token_data_size = if has_delegate { 107 } else { 75 }; // 75 + 32 (delegate) = 107 + + outputs.push(OutputCompressedAccountWithPackedContextConfig { + compressed_account: CompressedAccountConfig { + address: (false, ()), // Token accounts don't have addresses + data: ( + true, + CompressedAccountDataConfig { + data: token_data_size, // Size depends on delegate: 75 without, 107 with + }, + ), + }, + }); + } + + // Add compressed mint update if needed (last output account) + if input.compressed_mint { + use light_ctoken_types::state::{CompressedMint, CompressedMintConfig}; + let mint_size_config = CompressedMintConfig { + mint_authority: (input.compressed_mint, ()), + freeze_authority: (input.compressed_mint_with_freeze_authority, ()), + extensions: (!input.extensions_config.is_empty(), input.extensions_config), + }; + outputs.push(OutputCompressedAccountWithPackedContextConfig { + compressed_account: CompressedAccountConfig { + address: (true, ()), // Compressed mint has an address + data: ( + true, + CompressedAccountDataConfig { + data: CompressedMint::byte_len(&mint_size_config) as u32, + }, + ), + }, + }); + } + outputs + } + }; + InstructionDataInvokeCpiWithReadOnlyConfig { + cpi_context: CompressedCpiContextConfig {}, + proof: (input.has_proof, CompressedProofConfig {}), + new_address_params: vec![], // No new addresses for mint_to_compressed + input_compressed_accounts, + output_compressed_accounts, + read_only_addresses: vec![], + read_only_accounts: vec![], + } +} + +/// Allocate CPI instruction bytes with discriminator and length prefix +pub fn allocate_invoke_with_read_only_cpi_bytes( + config: &InstructionDataInvokeCpiWithReadOnlyConfig, +) -> Vec { + let vec_len = InstructionDataInvokeCpiWithReadOnly::byte_len(config); + let mut cpi_bytes = vec![0u8; vec_len + 8]; + cpi_bytes[0..8] + .copy_from_slice(light_system_program::instruction::InvokeCpiWithReadOnly::DISCRIMINATOR); + cpi_bytes +} diff --git a/programs/compressed-token/program/src/shared/initialize_token_account.rs b/programs/compressed-token/program/src/shared/initialize_token_account.rs new file mode 100644 index 0000000000..474a2a0312 --- /dev/null +++ b/programs/compressed-token/program/src/shared/initialize_token_account.rs @@ -0,0 +1,80 @@ +use anchor_lang::prelude::ProgramError; +use light_account_checks::AccountInfoTrait; +use light_ctoken_types::{ + instructions::extensions::compressible::ZCompressibleExtensionInstructionData, + state::{CompressedToken, CompressedTokenConfig, ExtensionStructConfig, ZExtensionStructMut}, +}; +use light_zero_copy::init_mut::ZeroCopyNew; +use pinocchio::{account_info::AccountInfo, msg, sysvars::clock::Clock}; + +/// Initialize a token account using spl-pod with zero balance and default settings +pub fn initialize_token_account( + token_account_info: &AccountInfo, + mint_pubkey: &[u8; 32], + owner_pubkey: &[u8; 32], + compressible_config: Option, +) -> Result<(), ProgramError> { + // Access the token account data as mutable bytes + let mut token_account_data = AccountInfoTrait::try_borrow_mut_data(token_account_info) + .map_err(|_| ProgramError::InvalidAccountData)?; + + // Create configuration for the compressed token + let extensions = if compressible_config.is_some() { + vec![ExtensionStructConfig::Compressible] + } else { + vec![] + }; + + let config = CompressedTokenConfig { + // Start with zero balance + delegate: false, // No delegate + is_native: false, // Not a native token + close_authority: false, // No close authority + extensions, + }; + let required_size = CompressedToken::byte_len(&config); + let actual_size = token_account_data.len(); + + // Check account size before attempting to initialize + if actual_size < required_size { + msg!( + "Account too small: required {} bytes, got {} bytes", + required_size, + actual_size + ); + return Err(ProgramError::InvalidAccountData); + } + + // Use zero-copy new to initialize the token account + let (mut compressed_token, _) = CompressedToken::new_zero_copy(&mut token_account_data, config) + .map_err(|e| { + msg!("Failed to create CompressedToken: {:?}", e); + ProgramError::InvalidAccountData + })?; + *compressed_token.mint = mint_pubkey.into(); + *compressed_token.owner = owner_pubkey.into(); + *compressed_token.state = 1; // Set state to Initialized + if let Some(deref_compressible_config) = compressed_token.extensions.as_deref_mut() { + msg!("compressible_config {:?}", compressible_config); + let compressible_config = + compressible_config.ok_or(ProgramError::InvalidInstructionData)?; + msg!("deref_compressible_config {:?}", deref_compressible_config); + match deref_compressible_config.get_mut(0) { + Some(ZExtensionStructMut::Compressible(compressible_extension)) => { + msg!("Compressible {:?}", compressible_extension); + + use pinocchio::sysvars::Sysvar; + let current_slot = Clock::get().unwrap().slot; + compressible_extension.last_written_slot = current_slot.into(); + compressible_extension.rent_authority = compressible_config.rent_authority; + compressible_extension.rent_recipient = compressible_config.rent_recipient; + compressible_extension.slots_until_compression = + compressible_config.slots_until_compression; + } + _ => { + return Err(ProgramError::InvalidInstructionData); + } + } + } + Ok(()) +} diff --git a/programs/compressed-token/program/src/shared/mint_to_token_pool.rs b/programs/compressed-token/program/src/shared/mint_to_token_pool.rs new file mode 100644 index 0000000000..25adc1412c --- /dev/null +++ b/programs/compressed-token/program/src/shared/mint_to_token_pool.rs @@ -0,0 +1,59 @@ +use anchor_lang::solana_program::program_error::ProgramError; +use light_sdk_types::CPI_AUTHORITY_PDA_SEED; +use pinocchio::{ + account_info::AccountInfo, + instruction::{AccountMeta, Instruction, Seed, Signer}, + program::invoke_signed, +}; + +use crate::LIGHT_CPI_SIGNER; + +/// Mint tokens to the token pool using SPL token mint_to instruction. +/// This function is shared between create_spl_mint and mint_to_compressed processors +/// to ensure consistent token pool management. +pub fn mint_to_token_pool( + mint_account: &AccountInfo, + token_pool_account: &AccountInfo, + token_program: &AccountInfo, + cpi_authority_pda: &AccountInfo, + amount: u64, +) -> Result<(), ProgramError> { + // Create SPL mint_to instruction + let spl_mint_to_ix = spl_token_2022::instruction::mint_to( + &solana_pubkey::Pubkey::new_from_array(*token_program.key()), + &solana_pubkey::Pubkey::new_from_array(*mint_account.key()), + &solana_pubkey::Pubkey::new_from_array(*token_pool_account.key()), + &solana_pubkey::Pubkey::new_from_array(LIGHT_CPI_SIGNER.cpi_signer), + &[], + amount, + )?; + + // Create instruction for CPI call + let mint_to_ix = Instruction { + program_id: token_program.key(), + accounts: &[ + AccountMeta::new(mint_account.key(), true, false), // mint (writable) + AccountMeta::new(token_pool_account.key(), true, false), // token_pool (writable) + AccountMeta::new(&LIGHT_CPI_SIGNER.cpi_signer, false, true), // authority (signer) + ], + data: &spl_mint_to_ix.data, + }; + + // Create signer seeds for CPI + let bump_seed = [LIGHT_CPI_SIGNER.bump]; + let seed_array = [ + Seed::from(CPI_AUTHORITY_PDA_SEED), + Seed::from(bump_seed.as_slice()), + ]; + let signer = Signer::from(&seed_array); + + // Execute the mint_to CPI call + match invoke_signed( + &mint_to_ix, + &[mint_account, token_pool_account, cpi_authority_pda], + &[signer], + ) { + Ok(()) => Ok(()), + Err(e) => Err(ProgramError::Custom(u64::from(e) as u32)), + } +} diff --git a/programs/compressed-token/program/src/shared/mod.rs b/programs/compressed-token/program/src/shared/mod.rs new file mode 100644 index 0000000000..ef0cffd32d --- /dev/null +++ b/programs/compressed-token/program/src/shared/mod.rs @@ -0,0 +1,12 @@ +pub mod accounts; +pub mod cpi; +pub mod cpi_bytes_size; +pub mod initialize_token_account; +mod mint_to_token_pool; +pub mod owner_validation; +pub mod token_input; +pub mod token_output; + +// Re-export AccountIterator from light-account-checks +pub use light_account_checks::AccountIterator; +pub use mint_to_token_pool::mint_to_token_pool; diff --git a/programs/compressed-token/program/src/shared/owner_validation.rs b/programs/compressed-token/program/src/shared/owner_validation.rs new file mode 100644 index 0000000000..04799c1daf --- /dev/null +++ b/programs/compressed-token/program/src/shared/owner_validation.rs @@ -0,0 +1,95 @@ +use anchor_lang::solana_program::program_error::ProgramError; +use light_account_checks::checks::check_signer; +use pinocchio::account_info::AccountInfo; +use spl_token_2022::pod::PodAccount; + +/// Verify owner or delegate signer authorization for token operations +/// Returns the delegate account info if delegate is used, None otherwise +pub fn verify_owner_or_delegate_signer<'a>( + owner_account: &'a AccountInfo, + delegate_account: Option<&'a AccountInfo>, +) -> Result, ProgramError> { + if let Some(delegate_account) = delegate_account { + // If delegate is used, delegate must be signer + check_signer(delegate_account).map_err(|e| { + anchor_lang::solana_program::msg!( + "Delegate signer: {:?}", + solana_pubkey::Pubkey::new_from_array(*delegate_account.key()) + ); + anchor_lang::solana_program::msg!("Delegate signer check failed: {:?}", e); + ProgramError::from(e) + })?; + Ok(Some(delegate_account)) + } else { + // If no delegate, owner must be signer + check_signer(owner_account).map_err(|e| { + anchor_lang::solana_program::msg!( + "Checking owner signer: {:?}", + solana_pubkey::Pubkey::new_from_array(*owner_account.key()) + ); + anchor_lang::solana_program::msg!("Owner signer check failed: {:?}", e); + ProgramError::from(e) + })?; + Ok(None) + } +} + +/// Verify authority for token account compression operations using existing pod_account +/// Checks if authority is owner or valid delegate with sufficient delegated amount +/// If delegate, decreases the delegated amount by the compression amount +pub fn verify_and_update_token_account_authority_with_pod( + pod_account: &mut PodAccount, + authority_account: &AccountInfo, + compression_amount: u64, +) -> Result<(), ProgramError> { + // Verify authority is signer + check_signer(authority_account).map_err(|e| { + anchor_lang::solana_program::msg!("Authority signer check failed: {:?}", e); + ProgramError::from(e) + })?; + + let authority_key = authority_account.key(); + let owner_key = &pod_account.owner; + + // Check if authority is the owner + if *authority_key == owner_key.to_bytes() { + return Ok(()); // Owner can always compress, no delegation update needed + } + + // Check if authority is a valid delegate + if pod_account.delegate.is_some() { + let delegate_key = pod_account + .delegate + .ok_or(ProgramError::InvalidAccountData)?; + if *authority_key == delegate_key.to_bytes() { + // Verify delegated amount is sufficient + let delegated_amount: u64 = pod_account.delegated_amount.into(); + if delegated_amount >= compression_amount { + // Decrease delegated amount by compression amount + let new_delegated_amount = delegated_amount - compression_amount; + pod_account.delegated_amount = new_delegated_amount.into(); + + anchor_lang::solana_program::msg!( + "Delegate compression: decreased delegated amount from {} to {}", + delegated_amount, + new_delegated_amount + ); + return Ok(()); + } else { + anchor_lang::solana_program::msg!( + "Insufficient delegated amount: {} < {}", + delegated_amount, + compression_amount + ); + return Err(ProgramError::InsufficientFunds); + } + } + } + + // Authority is neither owner nor valid delegate + anchor_lang::solana_program::msg!( + "Authority {:?} is not owner or valid delegate of token account", + solana_pubkey::Pubkey::new_from_array(*authority_key) + ); + Err(ProgramError::InvalidAccountData) +} diff --git a/programs/compressed-token/program/src/shared/token_input.rs b/programs/compressed-token/program/src/shared/token_input.rs new file mode 100644 index 0000000000..c18852c5c3 --- /dev/null +++ b/programs/compressed-token/program/src/shared/token_input.rs @@ -0,0 +1,77 @@ +use anchor_compressed_token::TokenData; +use anchor_lang::solana_program::program_error::ProgramError; +use light_compressed_account::instruction_data::with_readonly::ZInAccountMut; +use light_ctoken_types::{ + context::TokenContext, + instructions::transfer2::{TokenAccountVersion, ZMultiInputTokenDataWithContext}, +}; +use pinocchio::account_info::AccountInfo; + +use crate::shared::owner_validation::verify_owner_or_delegate_signer; + +/// Creates an input compressed account using zero-copy patterns and index-based account lookup. +/// +/// Validates signer authorization (owner or delegate), populates the zero-copy account structure, +/// and computes the appropriate token data hash based on frozen state. +pub fn set_input_compressed_account( + input_compressed_account: &mut ZInAccountMut, + context: &mut TokenContext, + input_token_data: &ZMultiInputTokenDataWithContext, + accounts: &[AccountInfo], + lamports: u64, +) -> std::result::Result<(), ProgramError> { + // Get owner from remaining accounts using the owner index + let owner_account = &accounts[input_token_data.owner as usize]; + + // Verify signer authorization using shared function + let delegate_account = if input_token_data.with_delegate() { + Some(&accounts[input_token_data.delegate as usize]) + } else { + None + }; + + let verified_delegate = verify_owner_or_delegate_signer(owner_account, delegate_account)?; + let hashed_delegate = + verified_delegate.map(|delegate| context.get_or_hash_pubkey(delegate.key())); + + // Compute data hash using TokenContext for caching + let hashed_owner = context.get_or_hash_pubkey(owner_account.key()); + + // Get mint hash from context + let mint_account = &accounts[input_token_data.mint as usize]; + let hashed_mint = context.get_or_hash_mint(mint_account.key())?; + + let version = TokenAccountVersion::try_from(input_token_data.version)?; + let amount_bytes = version.serialize_amount_bytes(input_token_data.amount.get()); + + let data_hash = if !IS_FROZEN { + TokenData::hash_with_hashed_values( + &hashed_mint, + &hashed_owner, + &amount_bytes, + &hashed_delegate.as_ref(), + ) + .map_err(ProgramError::from)? + } else { + TokenData::hash_frozen_with_hashed_values( + &hashed_mint, + &hashed_owner, + &amount_bytes, + &hashed_delegate.as_ref(), + ) + .map_err(ProgramError::from)? + }; + + input_compressed_account + .set_z( + version.discriminator(), + data_hash, + &input_token_data.merkle_context, + *input_token_data.root_index, + lamports, + None, // Token accounts don't have addresses + ) + .map_err(ProgramError::from)?; + + Ok(()) +} diff --git a/programs/compressed-token/program/src/shared/token_output.rs b/programs/compressed-token/program/src/shared/token_output.rs new file mode 100644 index 0000000000..9e381f3c50 --- /dev/null +++ b/programs/compressed-token/program/src/shared/token_output.rs @@ -0,0 +1,153 @@ +// Import the anchor TokenData for hash computation +use anchor_compressed_token::{ErrorCode, TokenData as AnchorTokenData}; +use anchor_lang::{ + prelude::{borsh, ProgramError}, + AnchorDeserialize, AnchorSerialize, +}; +use light_compressed_account::{ + instruction_data::data::ZOutputCompressedAccountWithPackedContextMut, Pubkey, +}; +use light_ctoken_types::{context::TokenContext, instructions::transfer2::TokenAccountVersion}; +use light_zero_copy::{num_trait::ZeroCopyNumTrait, ZeroCopyMut, ZeroCopyNew}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, AnchorSerialize, AnchorDeserialize)] +#[repr(u8)] +pub enum AccountState { + Initialized, + Frozen, +} + +#[derive(Debug, PartialEq, Eq, AnchorSerialize, AnchorDeserialize, Clone, ZeroCopyMut)] +pub struct TokenData { + /// The mint associated with this account + pub mint: Pubkey, + /// The owner of this account. + pub owner: Pubkey, + /// The amount of tokens this account holds. + pub amount: u64, + /// If `delegate` is `Some` then `delegated_amount` represents + /// the amount authorized by the delegate + pub delegate: Option, + /// The account's state (u8: 0 = Initialized, 1 = Frozen) + pub state: u8, + /// Placeholder for TokenExtension tlv data (unimplemented) + pub tlv: Option>, +} + +// Implementation for zero-copy mutable TokenData +impl ZTokenDataMut<'_> { + /// Set all fields of the TokenData struct at once + #[inline] + pub fn set( + &mut self, + mint: Pubkey, + owner: Pubkey, + amount: impl ZeroCopyNumTrait, + delegate: Option, + state: AccountState, + ) -> Result<(), ErrorCode> { + self.mint = mint; + self.owner = owner; + self.amount.set(amount.into()); + if let Some(z_delegate) = self.delegate.as_deref_mut() { + *z_delegate = delegate.ok_or(ErrorCode::InstructionDataExpectedDelegate)?; + } + if self.delegate.is_none() && delegate.is_some() { + return Err(ErrorCode::ZeroCopyExpectedDelegate); + } + *self.state = state as u8; + + if self.tlv.is_some() { + return Err(ErrorCode::TokenDataTlvUnimplemented); + } + Ok(()) + } +} + +/// 1. Set token account data +/// 2. Create token account data hash +/// 3. Set output compressed account +#[allow(clippy::too_many_arguments)] +pub fn set_output_compressed_account( + output_compressed_account: &mut ZOutputCompressedAccountWithPackedContextMut<'_>, + context: &mut TokenContext, + owner: Pubkey, + delegate: Option, + amount: impl ZeroCopyNumTrait, + lamports: Option, + mint_pubkey: Pubkey, + hashed_mint: &[u8; 32], + merkle_tree_index: u8, + version: u8, +) -> Result<(), ProgramError> { + // 1. Set token account data + { + // Get compressed account data from CPI struct to temporarily create TokenData + let compressed_account_data = output_compressed_account + .compressed_account + .data + .as_mut() + .ok_or(ProgramError::InvalidAccountData)?; + + // Create token data config based on delegate presence + let token_config: ::ZeroCopyConfig = TokenDataConfig { + delegate: (delegate.is_some(), ()), + tlv: (false, vec![]), + }; + + let (mut token_data, _) = + TokenData::new_zero_copy(compressed_account_data.data, token_config) + .map_err(ProgramError::from)?; + + token_data + .set( + mint_pubkey, + owner, + amount, + delegate, + AccountState::Initialized, + ) + .map_err(|e| ProgramError::Custom(e.into()))?; + } + let token_version = TokenAccountVersion::try_from(version)?; + // 2. Create TokenData using zero-copy to compute the data hash + let data_hash = { + let hashed_owner = context.get_or_hash_pubkey(&owner.into()); + let amount_bytes = token_version.serialize_amount_bytes(amount.into()); + + let hashed_delegate = + delegate.map(|delegate_pubkey| context.get_or_hash_pubkey(&delegate_pubkey.into())); + + if !IS_FROZEN { + AnchorTokenData::hash_with_hashed_values( + hashed_mint, + &hashed_owner, + &amount_bytes, + &hashed_delegate.as_ref(), + ) + .map_err(ProgramError::from)? + } else { + AnchorTokenData::hash_frozen_with_hashed_values( + hashed_mint, + &hashed_owner, + &amount_bytes, + &hashed_delegate.as_ref(), + ) + .map_err(ProgramError::from)? + } + }; + // 3. Set output compressed account + let lamports_value = lamports.unwrap_or(0u64.into()).into(); + output_compressed_account + .set( + crate::ID.into(), + lamports_value, + None, // Token accounts don't have addresses + merkle_tree_index, + token_version.discriminator(), + data_hash, + ) + .map_err(ProgramError::from)?; + + Ok(()) +} diff --git a/programs/compressed-token/program/src/transfer2/accounts.rs b/programs/compressed-token/program/src/transfer2/accounts.rs new file mode 100644 index 0000000000..0afc9342a1 --- /dev/null +++ b/programs/compressed-token/program/src/transfer2/accounts.rs @@ -0,0 +1,188 @@ +use anchor_lang::solana_program::program_error::ProgramError; +use light_account_checks::checks::{check_mut, check_signer}; +use light_ctoken_types::instructions::transfer2::ZCompressedTokenInstructionDataTransfer2; +use pinocchio::{account_info::AccountInfo, pubkey::Pubkey}; + +use crate::shared::AccountIterator; + +/// Validated system accounts for multi-transfer instruction +/// Accounts are ordered to match light-system-program CPI expectation +pub struct Transfer2ValidatedAccounts<'info> { + /// Fee payer account (index 0) - signer, mutable + pub fee_payer: &'info AccountInfo, + /// CPI authority PDA (index 1) - signer (via CPI) + pub authority: &'info AccountInfo, + /// Registered program PDA (index 2) - non-mutable + pub registered_program_pda: &'info AccountInfo, + /// Noop program (index 3) - non-mutable + pub noop_program: &'info AccountInfo, + /// Account compression authority (index 4) - non-mutable + pub account_compression_authority: &'info AccountInfo, + /// Account compression program (index 5) - non-mutable + pub account_compression_program: &'info AccountInfo, + /// Invoking program (index 6) - self program, non-mutable + pub invoking_program: &'info AccountInfo, + /// Sol pool PDA (index 7) - optional, mutable if present + pub sol_pool_pda: Option<&'info AccountInfo>, + /// SOL decompression recipient (index 8) - optional, mutable, for SOL decompression + pub sol_decompression_recipient: Option<&'info AccountInfo>, + /// System program (index 9) - non-mutable + pub system_program: &'info AccountInfo, + /// CPI context account (index 10) - optional, non-mutable + pub cpi_context_account: Option<&'info AccountInfo>, +} + +/// Dynamic accounts slice for index-based access +/// Contains mint, owner, delegate, merkle tree, and queue accounts +pub struct Transfer2PackedAccounts<'info> { + /// Packed accounts slice starting at index 11 + pub accounts: &'info [AccountInfo], +} + +impl Transfer2PackedAccounts<'_> { + /// Get account by index with bounds checking + pub fn get(&self, index: usize) -> Result<&AccountInfo, ProgramError> { + self.accounts + .get(index) + .ok_or(ProgramError::NotEnoughAccountKeys) + } + + /// Get account by u8 index with bounds checking + pub fn get_u8(&self, index: u8) -> Result<&AccountInfo, ProgramError> { + self.get(index as usize) + } +} + +impl Transfer2ValidatedAccounts<'_> { + // The offset of 1 skips the light-system-program account (index 0) + pub const CPI_ACCOUNTS_OFFSET: usize = 1; +} + +impl<'info> Transfer2ValidatedAccounts<'info> { + /// Validate and parse accounts from the instruction accounts slice + pub fn validate_and_parse( + accounts: &'info [AccountInfo], + with_sol_pool: bool, + with_cpi_context: bool, + ) -> Result<(Self, Transfer2PackedAccounts<'info>), ProgramError> { + // Parse system accounts from fixed positions + let mut iter = AccountIterator::new(accounts); + let fee_payer = iter.next_account("fee_payer")?; + let authority = iter.next_account("authority")?; + let registered_program_pda = iter.next_account("registered_program_pda")?; + let noop_program = iter.next_account("noop_program")?; + let account_compression_authority = iter.next_account("account_compression_authority")?; + let account_compression_program = iter.next_account("account_compression_program")?; + let invoking_program = iter.next_account("invoking_program")?; + let sol_pool_pda = if with_sol_pool { + Some(iter.next_account("sol_pool_pda")?) + } else { + None + }; + + let sol_decompression_recipient = if with_sol_pool { + Some(iter.next_account("sol_decompression_recipient")?) + } else { + None + }; + + let system_program = iter.next_account("system_program")?; + let cpi_context_account = if with_cpi_context { + let cpi_context_account = iter.next_account("cpi_context_account")?; + check_mut(cpi_context_account)?; + Some(cpi_context_account) + } else { + None + }; + + // Validate fee_payer: must be signer and mutable + check_signer(fee_payer)?; + check_mut(fee_payer)?; + // Extract remaining accounts slice for dynamic indexing + let remaining_accounts = iter.remaining()?; + + let validated_accounts = Transfer2ValidatedAccounts { + fee_payer, + authority, + registered_program_pda, + noop_program, + account_compression_authority, + account_compression_program, + invoking_program, + sol_pool_pda, + sol_decompression_recipient, + system_program, + cpi_context_account, + }; + + let packed_accounts = Transfer2PackedAccounts { + accounts: remaining_accounts, + }; + + Ok((validated_accounts, packed_accounts)) + } + + /// Calculate static accounts count after skipping index 0 (system accounts only) + /// Returns the count of fixed accounts based on optional features + #[inline(always)] + pub fn static_accounts_count(&self) -> usize { + let with_sol_pool = self.sol_pool_pda.is_some(); + let with_cpi_context = self.cpi_context_account.is_some(); + 8 + if with_sol_pool { 2 } else { 0 } + if with_cpi_context { 1 } else { 0 } + } + + /// Extract CPI accounts slice for light-system-program invocation + /// Includes static accounts + tree accounts based on highest tree index + /// Returns (cpi_accounts_slice, tree_accounts) + #[inline(always)] + pub fn cpi_accounts( + &self, + all_accounts: &'info [AccountInfo], + inputs: &ZCompressedTokenInstructionDataTransfer2, + packed_accounts: &'info Transfer2PackedAccounts<'info>, + ) -> (&'info [AccountInfo], Vec<&'info Pubkey>) { + // Extract tree accounts using highest index approach + let (tree_accounts, tree_accounts_count) = extract_tree_accounts(inputs, packed_accounts); + + // Calculate static accounts count after skipping index 0 (system accounts only) + let static_accounts_count = self.static_accounts_count(); + + // Include static CPI accounts + tree accounts based on highest tree index + let cpi_accounts_end = + Self::CPI_ACCOUNTS_OFFSET + static_accounts_count + tree_accounts_count; + let cpi_accounts_slice = &all_accounts[Self::CPI_ACCOUNTS_OFFSET..cpi_accounts_end]; + + (cpi_accounts_slice, tree_accounts) + } +} + +// TODO: unit test. +/// Extract tree accounts by finding the highest tree index and using it as closing offset +pub fn extract_tree_accounts<'info>( + inputs: &ZCompressedTokenInstructionDataTransfer2, + packed_accounts: &'info Transfer2PackedAccounts<'info>, +) -> (Vec<&'info Pubkey>, usize) { + // Find highest tree index from input and output data to determine tree accounts range + let mut highest_tree_index = 0u8; + for input_data in inputs.in_token_data.iter() { + highest_tree_index = + highest_tree_index.max(input_data.merkle_context.merkle_tree_pubkey_index); + highest_tree_index = highest_tree_index.max(input_data.merkle_context.queue_pubkey_index); + } + for output_data in inputs.out_token_data.iter() { + highest_tree_index = highest_tree_index.max(output_data.merkle_tree); + } + + // Tree accounts span from index 0 to highest_tree_index in remaining accounts + let tree_accounts_count = highest_tree_index as usize + 1; + + // Extract tree account pubkeys from the determined range + let mut tree_accounts = Vec::new(); + for i in 0..tree_accounts_count { + if let Some(account) = packed_accounts.accounts.get(i) { + tree_accounts.push(account.key()); + } + } + + (tree_accounts, tree_accounts_count) +} diff --git a/programs/compressed-token/program/src/transfer2/change_account.rs b/programs/compressed-token/program/src/transfer2/change_account.rs new file mode 100644 index 0000000000..77dbc10ef4 --- /dev/null +++ b/programs/compressed-token/program/src/transfer2/change_account.rs @@ -0,0 +1,90 @@ +use anchor_lang::prelude::ProgramError; +use light_compressed_account::instruction_data::with_readonly::ZInstructionDataInvokeCpiWithReadOnlyMut; +use light_ctoken_types::instructions::transfer2::ZCompressedTokenInstructionDataTransfer2; + +use crate::transfer2::accounts::Transfer2PackedAccounts; + +/// Create a change account for excess lamports (following anchor program pattern) +pub fn assign_change_account( + cpi_instruction_struct: &mut ZInstructionDataInvokeCpiWithReadOnlyMut, + inputs: &ZCompressedTokenInstructionDataTransfer2, + packed_accounts: &Transfer2PackedAccounts, + change_lamports: u64, +) -> Result<(), ProgramError> { + // Find the next available output account slot + let current_output_count = inputs.out_token_data.len(); + + // Get the change account slot (should be pre-allocated by CPI config) + let change_account = cpi_instruction_struct + .output_compressed_accounts + .get_mut(current_output_count) + .ok_or(ProgramError::InvalidAccountData)?; + anchor_lang::solana_program::log::msg!("inputs {:?}", inputs); + + // Get merkle tree index - use specified index + let merkle_tree_index = if inputs.with_lamports_change_account_merkle_tree_index != 0 { + inputs.lamports_change_account_merkle_tree_index + } else { + return Err(ProgramError::InvalidInstructionData); + }; + + // Get the owner account using the specified index + let owner_account = packed_accounts.get_u8(inputs.lamports_change_account_owner_index)?; + let owner_pubkey = *owner_account.key(); + + // Set up the change account as a lamports-only account (no token data) + let compressed_account = &mut change_account.compressed_account; + + // Set owner from the specified account index + compressed_account.owner = owner_pubkey.into(); + + // Set lamports amount + compressed_account.lamports.set(change_lamports); + + // No token data for change account + + if compressed_account.data.is_some() { + unimplemented!("lamports change account shouldn't have data.") + } + + // Set merkle tree index + *change_account.merkle_tree_index = merkle_tree_index; + + Ok(()) +} + +pub fn process_change_lamports( + inputs: &ZCompressedTokenInstructionDataTransfer2<'_>, + packed_accounts: &Transfer2PackedAccounts<'_>, + mut cpi_instruction_struct: ZInstructionDataInvokeCpiWithReadOnlyMut<'_>, + total_input_lamports: u64, + total_output_lamports: u64, +) -> Result<(), ProgramError> { + if total_input_lamports != total_output_lamports { + let (change_lamports, is_compress) = if total_input_lamports > total_output_lamports { + ( + total_input_lamports.saturating_sub(total_output_lamports), + 0, + ) + } else { + ( + total_output_lamports.saturating_sub(total_input_lamports), + 1, + ) + }; + // Set CPI instruction fields for compression/decompression + cpi_instruction_struct + .compress_or_decompress_lamports + .set(change_lamports); + cpi_instruction_struct.is_compress = is_compress; + // Create change account with the lamports difference + assign_change_account( + &mut cpi_instruction_struct, + inputs, + packed_accounts, + change_lamports, + )?; + } + + Ok(()) +} diff --git a/programs/compressed-token/program/src/transfer2/cpi.rs b/programs/compressed-token/program/src/transfer2/cpi.rs new file mode 100644 index 0000000000..80c1b8c502 --- /dev/null +++ b/programs/compressed-token/program/src/transfer2/cpi.rs @@ -0,0 +1,40 @@ +use arrayvec::ArrayVec; +use light_compressed_account::instruction_data::with_readonly::InstructionDataInvokeCpiWithReadOnlyConfig; +use light_ctoken_types::instructions::transfer2::ZCompressedTokenInstructionDataTransfer2; + +use crate::shared::cpi_bytes_size::{ + allocate_invoke_with_read_only_cpi_bytes, cpi_bytes_config, CpiConfigInput, +}; + +/// Build CPI configuration from instruction data +pub fn allocate_cpi_bytes( + inputs: &ZCompressedTokenInstructionDataTransfer2, +) -> (Vec, InstructionDataInvokeCpiWithReadOnlyConfig) { + // Build CPI configuration based on delegate flags + let mut input_delegate_flags = ArrayVec::new(); + for input_data in inputs.in_token_data.iter() { + input_delegate_flags.push(input_data.with_delegate != 0); + } + + let mut output_delegate_flags = ArrayVec::new(); + for output_data in inputs.out_token_data.iter() { + // Check if output has delegate (delegate index != 0 means delegate is present) + output_delegate_flags.push(output_data.delegate != 0); + } + + // Add extra output account for change account if needed (no delegate, no token data) + if inputs.with_lamports_change_account_merkle_tree_index != 0 { + output_delegate_flags.push(false); + } + + let config_input = CpiConfigInput { + input_accounts: input_delegate_flags, + output_accounts: output_delegate_flags, + has_proof: inputs.proof.is_some(), + compressed_mint: false, + compressed_mint_with_freeze_authority: false, + extensions_config: vec![], // TODO: Add extensions support for transfer2 + }; + let config = cpi_bytes_config(config_input); + (allocate_invoke_with_read_only_cpi_bytes(&config), config) +} diff --git a/programs/compressed-token/program/src/transfer2/mod.rs b/programs/compressed-token/program/src/transfer2/mod.rs new file mode 100644 index 0000000000..fbf4de2751 --- /dev/null +++ b/programs/compressed-token/program/src/transfer2/mod.rs @@ -0,0 +1,8 @@ +pub mod accounts; +pub mod change_account; +pub mod cpi; +pub mod native_compression; +pub mod processor; +pub mod sum_check; +pub mod token_inputs; +pub mod token_outputs; diff --git a/programs/compressed-token/program/src/transfer2/native_compression.rs b/programs/compressed-token/program/src/transfer2/native_compression.rs new file mode 100644 index 0000000000..d3562e1cf0 --- /dev/null +++ b/programs/compressed-token/program/src/transfer2/native_compression.rs @@ -0,0 +1,108 @@ +use anchor_lang::prelude::ProgramError; +use light_ctoken_types::instructions::transfer2::{ + CompressionMode, ZCompressedTokenInstructionDataTransfer2, ZCompression, +}; +use pinocchio::{account_info::AccountInfo, msg}; +use spl_pod::bytemuck::pod_from_bytes_mut; +use spl_token_2022::pod::PodAccount; + +use crate::{ + shared::owner_validation::verify_and_update_token_account_authority_with_pod, + transfer2::accounts::Transfer2PackedAccounts, LIGHT_CPI_SIGNER, +}; +const ID: &[u8; 32] = &LIGHT_CPI_SIGNER.program_id; +/// Process native compressions/decompressions with token accounts +pub fn process_token_compression( + inputs: &ZCompressedTokenInstructionDataTransfer2, + packed_accounts: &Transfer2PackedAccounts, +) -> Result<(), ProgramError> { + if let Some(compressions) = inputs.compressions.as_ref() { + for compression in compressions { + let source_or_recipient = packed_accounts.get_u8(compression.source_or_recipient)?; + + match unsafe { source_or_recipient.owner() } { + ID => { + process_native_compressions(compression, source_or_recipient, packed_accounts)?; + } + _ => return Err(ProgramError::InvalidInstructionData), + } + } + } + Ok(()) +} + +/// Validate compression fields based on compression mode +fn validate_compression_mode_fields(compression: &ZCompression) -> Result<(), ProgramError> { + let mode = compression.mode; + + match mode { + CompressionMode::Decompress => { + // Decompress must have authority = 0 + if compression.authority != 0 { + msg!("authority must be 0 for Decompress mode"); + return Err(ProgramError::InvalidInstructionData); + } + } + CompressionMode::Compress => { + // No additional validation needed for regular compress + } + } + + Ok(()) +} + +/// Process compression/decompression for token accounts using zero-copy PodAccount +fn process_native_compressions( + compression: &ZCompression, + token_account_info: &AccountInfo, + packed_accounts: &Transfer2PackedAccounts, +) -> Result<(), ProgramError> { + let mode = compression.mode; + + // Validate compression fields for the given mode + validate_compression_mode_fields(compression)?; + + // Get authority account and effective compression amount + let authority_account = packed_accounts.get_u8(compression.authority)?; + let effective_amount = u64::from(*compression.amount); + + // Access token account data as mutable bytes + let mut token_account_data = token_account_info + .try_borrow_mut_data() + .map_err(|_| ProgramError::AccountBorrowFailed)?; + + // Use zero-copy PodAccount to access the token account + let pod_account = pod_from_bytes_mut::(&mut token_account_data) + .map_err(|e| ProgramError::Custom(u64::from(e) as u32))?; + + // Get current balance + let current_balance: u64 = pod_account.amount.into(); + + // Calculate new balance using effective amount + let new_balance = match mode { + CompressionMode::Compress => { + // Verify authority for compression operations and update delegated amount if needed + verify_and_update_token_account_authority_with_pod( + pod_account, + authority_account, + effective_amount, + )?; + + // Compress: subtract from solana account + current_balance + .checked_sub(effective_amount) + .ok_or(ProgramError::ArithmeticOverflow)? + } + CompressionMode::Decompress => { + // Decompress: add to solana account + current_balance + .checked_add(effective_amount) + .ok_or(ProgramError::ArithmeticOverflow)? + } + }; + + // Update the balance in the pod account + pod_account.amount = new_balance.into(); + + Ok(()) +} diff --git a/programs/compressed-token/program/src/transfer2/processor.rs b/programs/compressed-token/program/src/transfer2/processor.rs new file mode 100644 index 0000000000..7bc7885f0a --- /dev/null +++ b/programs/compressed-token/program/src/transfer2/processor.rs @@ -0,0 +1,149 @@ +use anchor_compressed_token::check_cpi_context; +use anchor_lang::prelude::ProgramError; +use light_compressed_account::instruction_data::with_readonly::InstructionDataInvokeCpiWithReadOnly; +use light_ctoken_types::{ + context::TokenContext, + instructions::transfer2::{validate_instruction_data, CompressedTokenInstructionDataTransfer2}, +}; +use light_heap::{bench_sbf_end, bench_sbf_start}; +use light_zero_copy::{borsh::Deserialize, ZeroCopyNew}; +use pinocchio::account_info::AccountInfo; + +use crate::{ + shared::cpi::execute_cpi_invoke, + transfer2::{ + accounts::Transfer2ValidatedAccounts, change_account::process_change_lamports, + cpi::allocate_cpi_bytes, native_compression::process_token_compression, + sum_check::sum_check_multi_mint, token_inputs::set_input_compressed_accounts, + token_outputs::set_output_compressed_accounts, + }, +}; + +/// Process a token transfer instruction +/// build inputs -> sum check -> build outputs -> add token data to inputs -> invoke cpi +/// 1. Unpack compressed input accounts and input token data, this uses +/// standardized signer / delegate and will fail in proof verification in +/// case either is invalid. +/// 2. Check that compressed accounts are of same mint. +/// 3. Check that sum of input compressed accounts is equal to sum of output +/// compressed accounts +/// 4. create_output_compressed_accounts +/// 5. Serialize and add token_data data to in compressed_accounts. +/// 6. Invoke light_system_program::execute_compressed_transaction. +#[inline(always)] +pub fn process_transfer2( + accounts: &[AccountInfo], + instruction_data: &[u8], +) -> Result<(), ProgramError> { + // Parse instruction data first to determine optional accounts + let (inputs, _) = CompressedTokenInstructionDataTransfer2::zero_copy_at(instruction_data) + .map_err(ProgramError::from)?; + + // Check CPI context validity (multi-transfer modifies Solana account state) + check_cpi_context(&inputs.cpi_context).map_err(ProgramError::from)?; + + let total_input_lamports = if let Some(inputs) = inputs.in_lamports.as_ref() { + inputs.iter().map(|input| u64::from(**input)).sum() + } else { + 0 + }; + let total_output_lamports = if let Some(inputs) = inputs.out_lamports.as_ref() { + inputs.iter().map(|input| u64::from(**input)).sum() + } else { + 0 + }; + + // Determine optional account flags from instruction data + let with_sol_pool = total_input_lamports != total_output_lamports; + let with_cpi_context = inputs.cpi_context.is_some(); + + // Skip first account (light-system-program) and validate remaining accounts + let (validated_accounts, packed_accounts) = Transfer2ValidatedAccounts::validate_and_parse( + &accounts[Transfer2ValidatedAccounts::CPI_ACCOUNTS_OFFSET..], + with_sol_pool, + with_cpi_context, + )?; + // Validate instruction data consistency + validate_instruction_data(&inputs)?; + bench_sbf_start!("t_context_and_check_sig"); + // anchor_lang::solana_program::log::msg!("inputs {:?}", inputs); + + // Create TokenContext for hash caching + let mut context = TokenContext::new(); + + // Allocate CPI bytes and create zero-copy structure + let (mut cpi_bytes, config) = allocate_cpi_bytes(&inputs); + + let (mut cpi_instruction_struct, _) = + InstructionDataInvokeCpiWithReadOnly::new_zero_copy(&mut cpi_bytes[8..], config) + .map_err(ProgramError::from)?; + cpi_instruction_struct.initialize( + crate::LIGHT_CPI_SIGNER.bump, + &crate::LIGHT_CPI_SIGNER.program_id.into(), + inputs.proof, + inputs.cpi_context, + )?; + + // Process input compressed accounts + set_input_compressed_accounts( + &mut cpi_instruction_struct, + &mut context, + &inputs, + &packed_accounts, + )?; + + // Process output compressed accounts + set_output_compressed_accounts( + &mut cpi_instruction_struct, + &mut context, + &inputs, + &packed_accounts, + )?; + bench_sbf_end!("t_create_output_compressed_accounts"); + //msg!("cpi_instruction_struct {:?}", cpi_instruction_struct); + + process_change_lamports( + &inputs, + &packed_accounts, + cpi_instruction_struct, + total_input_lamports, + total_output_lamports, + )?; + // Process token compressions/decompressions + // TODO: support spl + process_token_compression(&inputs, &packed_accounts)?; + bench_sbf_end!("t_context_and_check_sig"); + bench_sbf_start!("t_sum_check"); + sum_check_multi_mint( + &inputs.in_token_data, + &inputs.out_token_data, + inputs.compressions.as_deref(), + ) + .map_err(|e| ProgramError::Custom(e as u32))?; + bench_sbf_end!("t_sum_check"); + + // Get CPI accounts slice and tree accounts for light-system-program invocation + let (cpi_accounts, tree_pubkeys) = + validated_accounts.cpi_accounts(accounts, &inputs, &packed_accounts); + // Debug prints keep for now. + { + let _solana_tree_accounts = tree_pubkeys + .iter() + .map(|&x| solana_pubkey::Pubkey::new_from_array(*x)) + .collect::>(); + let _cpi_accounts = cpi_accounts + .iter() + .map(|x| solana_pubkey::Pubkey::new_from_array(*x.key())) + .collect::>(); + } + // Execute CPI call to light-system-program + execute_cpi_invoke( + cpi_accounts, + cpi_bytes, + tree_pubkeys.as_slice(), + with_sol_pool, + validated_accounts.cpi_context_account.map(|x| *x.key()), + )?; + + Ok(()) +} diff --git a/programs/compressed-token/program/src/transfer2/sum_check.rs b/programs/compressed-token/program/src/transfer2/sum_check.rs new file mode 100644 index 0000000000..6bc7512a5f --- /dev/null +++ b/programs/compressed-token/program/src/transfer2/sum_check.rs @@ -0,0 +1,121 @@ +use anchor_compressed_token::ErrorCode; +use arrayvec::ArrayVec; +use light_ctoken_types::instructions::transfer2::{ + CompressionMode, ZCompression, ZMultiInputTokenDataWithContext, ZMultiTokenTransferOutputData, +}; + +/// Process inputs and add amounts to mint sums with order validation +#[inline(always)] +fn sum_inputs( + inputs: &[ZMultiInputTokenDataWithContext], + mint_sums: &mut ArrayVec<(u8, u64), 5>, +) -> Result<(), ErrorCode> { + let mut prev_mint_index = 0u8; + for (i, input) in inputs.iter().enumerate() { + let mint_index = input.mint; + + // Validate incremental order + if i > 0 && mint_index < prev_mint_index { + return Err(ErrorCode::InputsOutOfOrder); + } + + // Find or create mint entry + if let Some(entry) = mint_sums.iter_mut().find(|(idx, _)| *idx == mint_index) { + entry.1 = entry + .1 + .checked_add(input.amount.into()) + .ok_or(ErrorCode::ComputeInputSumFailed)?; + } else { + if mint_sums.is_full() { + return Err(ErrorCode::TooManyMints); + } + mint_sums.push((mint_index, input.amount.into())); + } + + prev_mint_index = mint_index; + } + Ok(()) +} + +/// Process compressions and adjust mint sums (add for compress, subtract for decompress) +#[inline(always)] +fn sum_compressions( + compressions: &[ZCompression], + mint_sums: &mut ArrayVec<(u8, u64), 5>, +) -> Result<(), ErrorCode> { + for compression in compressions.iter() { + let mint_index = compression.mint; + + // Find mint entry (create if doesn't exist for compression) + if let Some(entry) = mint_sums.iter_mut().find(|(idx, _)| *idx == mint_index) { + entry.1 = compression + .new_balance_compressed_account(entry.1) + .map_err(|_| ErrorCode::SumCheckFailed)?; // TODO propagate error + } else { + // Create new entry if compressing + if compression.mode == CompressionMode::Compress { + if mint_sums.is_full() { + return Err(ErrorCode::TooManyMints); + } + mint_sums.push((mint_index, (*compression.amount).into())); + } else { + // Cannot decompress if no balance exists + return Err(ErrorCode::SumCheckFailed); + } + } + } + Ok(()) +} + +/// Process outputs and subtract amounts from mint sums +#[inline(always)] +fn sum_outputs( + outputs: &[ZMultiTokenTransferOutputData], + mint_sums: &mut ArrayVec<(u8, u64), 5>, +) -> Result<(), ErrorCode> { + for output in outputs.iter() { + let mint_index = output.mint; + + // Find mint entry (create if doesn't exist for output-only mints) + if let Some(entry) = mint_sums.iter_mut().find(|(idx, _)| *idx == mint_index) { + entry.1 = entry + .1 + .checked_sub(output.amount.into()) + .ok_or(ErrorCode::ComputeOutputSumFailed)?; + } else { + // Output mint not in inputs or compressions - invalid + return Err(ErrorCode::ComputeOutputSumFailed); + } + } + Ok(()) +} + +/// Sum check for multi-mint transfers with ordered mint validation and compression support +pub fn sum_check_multi_mint( + inputs: &[ZMultiInputTokenDataWithContext], + outputs: &[ZMultiTokenTransferOutputData], + compressions: Option<&[ZCompression]>, +) -> Result<(), ErrorCode> { + // ArrayVec with 5 entries: (mint_index, sum) + let mut mint_sums: ArrayVec<(u8, u64), 5> = ArrayVec::new(); + + // Process inputs - increase sums + sum_inputs(inputs, &mut mint_sums)?; + + // Process compressions if present + if let Some(compressions) = compressions { + sum_compressions(compressions, &mut mint_sums)?; + } + + // Process outputs - decrease sums + sum_outputs(outputs, &mut mint_sums)?; + + // Verify all sums are zero + for (_, sum) in mint_sums.iter() { + if *sum != 0 { + return Err(ErrorCode::SumCheckFailed); + } + } + + Ok(()) +} diff --git a/programs/compressed-token/program/src/transfer2/token_inputs.rs b/programs/compressed-token/program/src/transfer2/token_inputs.rs new file mode 100644 index 0000000000..7a9d966183 --- /dev/null +++ b/programs/compressed-token/program/src/transfer2/token_inputs.rs @@ -0,0 +1,46 @@ +use anchor_lang::prelude::ProgramError; +use light_compressed_account::instruction_data::with_readonly::ZInstructionDataInvokeCpiWithReadOnlyMut; +use light_ctoken_types::{ + context::TokenContext, instructions::transfer2::ZCompressedTokenInstructionDataTransfer2, +}; + +use crate::{ + shared::token_input::set_input_compressed_account, transfer2::accounts::Transfer2PackedAccounts, +}; + +/// Process input compressed accounts and return total input lamports +pub fn set_input_compressed_accounts( + cpi_instruction_struct: &mut ZInstructionDataInvokeCpiWithReadOnlyMut, + context: &mut TokenContext, + inputs: &ZCompressedTokenInstructionDataTransfer2, + packed_accounts: &Transfer2PackedAccounts, +) -> Result { + let mut total_input_lamports = 0u64; + + for (i, input_data) in inputs.in_token_data.iter().enumerate() { + let input_lamports = if let Some(lamports) = inputs.in_lamports.as_ref() { + if let Some(input_lamports) = lamports.get(i) { + input_lamports.get() + } else { + 0 + } + } else { + 0 + }; + + total_input_lamports += input_lamports; + + set_input_compressed_account::( + cpi_instruction_struct + .input_compressed_accounts + .get_mut(i) + .ok_or(ProgramError::InvalidAccountData)?, + context, + input_data, + packed_accounts.accounts, + input_lamports, + )?; + } + + Ok(total_input_lamports) +} diff --git a/programs/compressed-token/program/src/transfer2/token_outputs.rs b/programs/compressed-token/program/src/transfer2/token_outputs.rs new file mode 100644 index 0000000000..8d7f3ed452 --- /dev/null +++ b/programs/compressed-token/program/src/transfer2/token_outputs.rs @@ -0,0 +1,72 @@ +use anchor_lang::prelude::ProgramError; +use light_compressed_account::instruction_data::with_readonly::ZInstructionDataInvokeCpiWithReadOnlyMut; +use light_ctoken_types::{ + context::TokenContext, instructions::transfer2::ZCompressedTokenInstructionDataTransfer2, +}; + +use crate::{ + shared::token_output::set_output_compressed_account, + transfer2::accounts::Transfer2PackedAccounts, +}; + +/// Process output compressed accounts and return total output lamports +pub fn set_output_compressed_accounts( + cpi_instruction_struct: &mut ZInstructionDataInvokeCpiWithReadOnlyMut, + context: &mut TokenContext, + inputs: &ZCompressedTokenInstructionDataTransfer2, + packed_accounts: &Transfer2PackedAccounts, +) -> Result { + let mut total_output_lamports = 0u64; + + for (i, output_data) in inputs.out_token_data.iter().enumerate() { + let output_lamports = if let Some(lamports) = inputs.out_lamports.as_ref() { + if let Some(lamports) = lamports.get(i) { + lamports.get() + } else { + 0 + } + } else { + 0 + }; + + total_output_lamports += output_lamports; + + let mint_index = output_data.mint; + let mint_account = packed_accounts.get_u8(mint_index)?; + let hashed_mint = context.get_or_hash_pubkey(mint_account.key()); + + // Get owner account using owner index + let owner_account = packed_accounts.get_u8(output_data.owner)?; + let owner_pubkey = *owner_account.key(); + + // Get delegate if present + let delegate_pubkey = if output_data.delegate != 0 { + let delegate_account = packed_accounts.get_u8(output_data.delegate)?; + Some(*delegate_account.key()) + } else { + None + }; + let output_lamports = if output_lamports > 0 { + Some(output_lamports) + } else { + None + }; + set_output_compressed_account::( + cpi_instruction_struct + .output_compressed_accounts + .get_mut(i) + .ok_or(ProgramError::InvalidAccountData)?, + context, + owner_pubkey.into(), + delegate_pubkey.map(|d| d.into()), + output_data.amount, + output_lamports, + mint_account.key().into(), + &hashed_mint, + output_data.merkle_tree, + output_data.version, + )?; + } + + Ok(total_output_lamports) +} diff --git a/programs/compressed-token/program/tests/allocation_test.rs b/programs/compressed-token/program/tests/allocation_test.rs new file mode 100644 index 0000000000..3b4f29c2b7 --- /dev/null +++ b/programs/compressed-token/program/tests/allocation_test.rs @@ -0,0 +1,143 @@ +use light_compressed_account::instruction_data::with_readonly::InstructionDataInvokeCpiWithReadOnly; +use light_compressed_token::shared::cpi_bytes_size::{ + allocate_invoke_with_read_only_cpi_bytes, cpi_bytes_config, CpiConfigInput, +}; +use light_ctoken_types::state::{ + extensions::{MetadataConfig, TokenMetadataConfig}, + CompressedMint, CompressedMintConfig, ExtensionStructConfig, +}; +use light_zero_copy::ZeroCopyNew; + +#[test] +fn test_extension_allocation_only() { + // Test 1: No extensions - should work + let config_input_no_ext = CpiConfigInput { + input_accounts: arrayvec::ArrayVec::new(), + output_accounts: arrayvec::ArrayVec::new(), + has_proof: false, + compressed_mint: true, + compressed_mint_with_freeze_authority: false, + extensions_config: vec![], + }; + + let config_no_ext = cpi_bytes_config(config_input_no_ext); + let cpi_bytes_no_ext = allocate_invoke_with_read_only_cpi_bytes(&config_no_ext); + + println!( + "No extensions - CPI bytes length: {}", + cpi_bytes_no_ext.len() + ); + + // Test 2: With minimal token metadata extension + let extensions_config = vec![ExtensionStructConfig::TokenMetadata(TokenMetadataConfig { + update_authority: (true, ()), + metadata: MetadataConfig { + name: 5, // 5 bytes + symbol: 3, // 3 bytes + uri: 10, // 10 bytes + }, + additional_metadata: vec![], // No additional metadata + })]; + + let config_input_with_ext = CpiConfigInput { + input_accounts: arrayvec::ArrayVec::new(), + output_accounts: arrayvec::ArrayVec::new(), + has_proof: false, + compressed_mint: true, + compressed_mint_with_freeze_authority: false, + extensions_config: extensions_config.clone(), + }; + + let config_with_ext = cpi_bytes_config(config_input_with_ext); + let cpi_bytes_with_ext = allocate_invoke_with_read_only_cpi_bytes(&config_with_ext); + + println!( + "With extensions - CPI bytes length: {}", + cpi_bytes_with_ext.len() + ); + println!( + "Difference: {}", + cpi_bytes_with_ext.len() - cpi_bytes_no_ext.len() + ); + + // Test 3: Calculate expected mint size with extensions + let mint_config = CompressedMintConfig { + mint_authority: (true, ()), + freeze_authority: (false, ()), + extensions: (true, extensions_config), + }; + + let expected_mint_size = CompressedMint::byte_len(&mint_config); + println!("Expected mint size with extensions: {}", expected_mint_size); + + // Test 4: Try to create the CPI instruction structure to see if allocation is sufficient + let mut cpi_bytes_copy = cpi_bytes_with_ext.clone(); + let result = InstructionDataInvokeCpiWithReadOnly::new_zero_copy( + &mut cpi_bytes_copy[8..], + config_with_ext, + ); + + match result { + Ok(_) => println!("✅ CPI instruction creation succeeded"), + Err(e) => println!("❌ CPI instruction creation failed: {:?}", e), + } +} + +#[test] +fn test_progressive_extension_sizes() { + // Test progressively larger extensions to find the breaking point + let base_sizes = [ + (1, 1, 1), // Minimal + (5, 3, 10), // Small + (10, 5, 20), // Medium + (20, 8, 40), // Large + ]; + + for (name_len, symbol_len, uri_len) in base_sizes { + println!( + "\n--- Testing sizes: name={}, symbol={}, uri={} ---", + name_len, symbol_len, uri_len + ); + + let extensions_config = vec![ExtensionStructConfig::TokenMetadata(TokenMetadataConfig { + update_authority: (true, ()), + metadata: MetadataConfig { + name: name_len, + symbol: symbol_len, + uri: uri_len, + }, + additional_metadata: vec![], + })]; + + let config_input = CpiConfigInput { + input_accounts: arrayvec::ArrayVec::new(), + output_accounts: arrayvec::ArrayVec::new(), + has_proof: false, + compressed_mint: true, + compressed_mint_with_freeze_authority: false, + extensions_config: extensions_config.clone(), + }; + + let config = cpi_bytes_config(config_input); + let mut cpi_bytes = allocate_invoke_with_read_only_cpi_bytes(&config); + + println!("CPI bytes allocated: {}", cpi_bytes.len()); + + let mint_config = CompressedMintConfig { + mint_authority: (true, ()), + freeze_authority: (false, ()), + extensions: (true, extensions_config), + }; + + let expected_mint_size = CompressedMint::byte_len(&mint_config); + println!("Expected mint size: {}", expected_mint_size); + + let result = + InstructionDataInvokeCpiWithReadOnly::new_zero_copy(&mut cpi_bytes[8..], config); + + match result { + Ok(_) => println!("✅ Success"), + Err(e) => println!("❌ Failed: {:?}", e), + } + } +} diff --git a/programs/compressed-token/program/tests/exact_allocation_test.rs b/programs/compressed-token/program/tests/exact_allocation_test.rs new file mode 100644 index 0000000000..a58a89c3b4 --- /dev/null +++ b/programs/compressed-token/program/tests/exact_allocation_test.rs @@ -0,0 +1,311 @@ +use light_compressed_account::instruction_data::with_readonly::InstructionDataInvokeCpiWithReadOnly; +use light_compressed_token::shared::cpi_bytes_size::{ + allocate_invoke_with_read_only_cpi_bytes, cpi_bytes_config, CpiConfigInput, +}; +use light_ctoken_types::state::{ + extensions::{AdditionalMetadataConfig, MetadataConfig, TokenMetadataConfig}, + CompressedMint, CompressedMintConfig, ExtensionStructConfig, +}; +use light_zero_copy::ZeroCopyNew; + +#[test] +fn test_exact_allocation_assertion() { + println!("\n=== EXACT ALLOCATION TEST ==="); + + // Test case: specific token metadata configuration + let name_len = 10u32; + let symbol_len = 5u32; + let uri_len = 20u32; + + // Add some additional metadata + let additional_metadata_configs = vec![ + AdditionalMetadataConfig { key: 8, value: 15 }, + AdditionalMetadataConfig { key: 12, value: 25 }, + ]; + + let extensions_config = vec![ExtensionStructConfig::TokenMetadata(TokenMetadataConfig { + update_authority: (true, ()), + metadata: MetadataConfig { + name: name_len, + symbol: symbol_len, + uri: uri_len, + }, + additional_metadata: additional_metadata_configs.clone(), + })]; + + println!("Extension config: {:?}", extensions_config); + + // Step 1: Calculate expected mint size + let mint_config = CompressedMintConfig { + mint_authority: (true, ()), + freeze_authority: (false, ()), + extensions: (true, extensions_config.clone()), + }; + + let expected_mint_size = CompressedMint::byte_len(&mint_config); + println!("Expected mint size: {} bytes", expected_mint_size); + + // Step 2: Calculate CPI allocation + let config_input = CpiConfigInput { + input_accounts: arrayvec::ArrayVec::new(), + output_accounts: arrayvec::ArrayVec::new(), + has_proof: false, + compressed_mint: true, + compressed_mint_with_freeze_authority: false, + extensions_config: extensions_config.clone(), + }; + + let config = cpi_bytes_config(config_input); + let mut cpi_bytes = allocate_invoke_with_read_only_cpi_bytes(&config); + + println!("Total CPI bytes allocated: {} bytes", cpi_bytes.len()); + println!("CPI instruction header: 8 bytes"); + println!( + "Available for instruction data: {} bytes", + cpi_bytes.len() - 8 + ); + + // Step 3: Create the CPI instruction and examine allocation + let (cpi_instruction_struct, _) = + InstructionDataInvokeCpiWithReadOnly::new_zero_copy(&mut cpi_bytes[8..], config) + .expect("Should create CPI instruction successfully"); + + // Step 4: Get the output compressed account data buffer + let output_account = &cpi_instruction_struct.output_compressed_accounts[0]; + let compressed_account_data = output_account + .compressed_account + .data + .as_ref() + .expect("Should have compressed account data"); + + let available_data_space = compressed_account_data.data.len(); + println!( + "Available data space in output account: {} bytes", + available_data_space + ); + + // Step 5: Calculate exact space needed + let base_mint_size_no_ext = { + let no_ext_config = CompressedMintConfig { + mint_authority: (true, ()), + freeze_authority: (false, ()), + extensions: (false, vec![]), + }; + CompressedMint::byte_len(&no_ext_config) + }; + + let extension_space_needed = expected_mint_size - base_mint_size_no_ext; + + println!("\n=== BREAKDOWN ==="); + println!( + "Base mint size (no extensions): {} bytes", + base_mint_size_no_ext + ); + println!("Extension space needed: {} bytes", extension_space_needed); + println!("Total mint size needed: {} bytes", expected_mint_size); + println!("Allocated data space: {} bytes", available_data_space); + println!( + "Margin: {} bytes", + available_data_space as i32 - expected_mint_size as i32 + ); + + // Step 6: Exact assertions + assert!( + available_data_space >= expected_mint_size, + "Allocated space ({}) must be >= expected mint size ({})", + available_data_space, + expected_mint_size + ); + + // Step 7: Calculate exact dynamic token metadata length + println!("\n=== EXACT LENGTH CALCULATION ==="); + + // Sum all the dynamic lengths + let total_metadata_dynamic_len = name_len + symbol_len + uri_len; + let total_additional_metadata_len: u32 = additional_metadata_configs + .iter() + .map(|config| config.key + config.value) + .sum(); + + let total_dynamic_len = total_metadata_dynamic_len + total_additional_metadata_len; + + println!("Metadata dynamic lengths:"); + println!(" name: {} bytes", name_len); + println!(" symbol: {} bytes", symbol_len); + println!(" uri: {} bytes", uri_len); + println!(" metadata total: {} bytes", total_metadata_dynamic_len); + + println!("Additional metadata dynamic lengths:"); + for (i, config) in additional_metadata_configs.iter().enumerate() { + println!( + " item {}: key={}, value={}, total={}", + i, + config.key, + config.value, + config.key + config.value + ); + } + println!( + " additional metadata total: {} bytes", + total_additional_metadata_len + ); + + println!("TOTAL dynamic length: {} bytes", total_dynamic_len); + + // Calculate expected TokenMetadata size with exact breakdown + let token_metadata_size = { + let mut size = 0u32; + + // Fixed overhead for TokenMetadata struct: + size += 1; // update_authority discriminator + size += 32; // update_authority pubkey + size += 32; // mint pubkey + size += 4; // name vec length + size += 4; // symbol vec length + size += 4; // uri vec length + size += 4; // additional_metadata vec length + size += 1; // version byte + + // Additional metadata items overhead + for _ in &additional_metadata_configs { + size += 4; // key vec length + size += 4; // value vec length + } + + let fixed_overhead = size; + println!("Fixed TokenMetadata overhead: {} bytes", fixed_overhead); + + // Add dynamic content + size += total_dynamic_len; + + println!( + "Total TokenMetadata size: {} + {} = {} bytes", + fixed_overhead, total_dynamic_len, size + ); + size + }; + + // Step 8: Assert exact allocation + println!("\n=== EXACT ALLOCATION ASSERTION ==="); + + let expected_total_size = base_mint_size_no_ext as u32 + token_metadata_size; + + println!("Base mint size: {} bytes", base_mint_size_no_ext); + println!( + "Dynamic token metadata length: {} bytes", + token_metadata_size + ); + println!( + "Expected total size: {} + {} = {} bytes", + base_mint_size_no_ext, token_metadata_size, expected_total_size + ); + println!("Allocated data space: {} bytes", available_data_space); + + // The critical assertion: allocated space should exactly match CompressedMint::byte_len() + assert_eq!( + available_data_space, expected_mint_size, + "Allocated bytes ({}) must exactly equal CompressedMint::byte_len() ({})", + available_data_space, expected_mint_size + ); + + println!("✅ SUCCESS: Perfect allocation match!"); + println!(" allocated_bytes = CompressedMint::byte_len()"); + println!(" {} = {}", available_data_space, expected_mint_size); + + // Note: The difference between our manual calculation and actual struct size + // is due to struct padding/alignment which is normal for zero-copy structs + let manual_vs_actual = expected_mint_size as i32 - expected_total_size as i32; + if manual_vs_actual != 0 { + println!( + "📝 Note: {} bytes difference between manual calculation and actual struct size", + manual_vs_actual + ); + println!(" This is normal padding/alignment overhead in zero-copy structs"); + } +} + +#[test] +fn test_allocation_with_various_metadata_sizes() { + println!("\n=== VARIOUS METADATA SIZES TEST ==="); + + let test_cases = [ + // (name, symbol, uri, additional_metadata_count) + (5, 3, 10, 0), + (10, 5, 20, 1), + (15, 8, 30, 2), + (20, 10, 40, 3), + ]; + + for (i, (name_len, symbol_len, uri_len, additional_count)) in test_cases.iter().enumerate() { + println!("\n--- Test case {} ---", i + 1); + println!( + "Metadata: name={}, symbol={}, uri={}, additional={}", + name_len, symbol_len, uri_len, additional_count + ); + + let additional_metadata_configs: Vec<_> = (0..*additional_count) + .map(|j| AdditionalMetadataConfig { + key: 5 + j * 2, + value: 10 + j * 3, + }) + .collect(); + + let extensions_config = vec![ExtensionStructConfig::TokenMetadata(TokenMetadataConfig { + update_authority: (true, ()), + metadata: MetadataConfig { + name: *name_len, + symbol: *symbol_len, + uri: *uri_len, + }, + additional_metadata: additional_metadata_configs, + })]; + + let mint_config = CompressedMintConfig { + mint_authority: (true, ()), + freeze_authority: (false, ()), + extensions: (true, extensions_config.clone()), + }; + + let expected_mint_size = CompressedMint::byte_len(&mint_config); + + let config_input = CpiConfigInput { + input_accounts: arrayvec::ArrayVec::new(), + output_accounts: arrayvec::ArrayVec::new(), + has_proof: false, + compressed_mint: true, + compressed_mint_with_freeze_authority: false, + extensions_config, + }; + + let config = cpi_bytes_config(config_input); + let mut cpi_bytes = allocate_invoke_with_read_only_cpi_bytes(&config); + + let (cpi_instruction_struct, _) = + InstructionDataInvokeCpiWithReadOnly::new_zero_copy(&mut cpi_bytes[8..], config) + .expect("Should create CPI instruction successfully"); + + let output_account = &cpi_instruction_struct.output_compressed_accounts[0]; + let compressed_account_data = output_account + .compressed_account + .data + .as_ref() + .expect("Should have compressed account data"); + + let available_space = compressed_account_data.data.len(); + + println!( + "Required: {} bytes, Allocated: {} bytes, Margin: {} bytes", + expected_mint_size, + available_space, + available_space as i32 - expected_mint_size as i32 + ); + + assert!( + available_space >= expected_mint_size, + "Test case {}: insufficient allocation", + i + 1 + ); + + println!("✅ Test case {} passed", i + 1); + } +} diff --git a/programs/compressed-token/program/tests/metadata_hash.rs b/programs/compressed-token/program/tests/metadata_hash.rs new file mode 100644 index 0000000000..496149edc3 --- /dev/null +++ b/programs/compressed-token/program/tests/metadata_hash.rs @@ -0,0 +1,51 @@ +use borsh::BorshSerialize; +use light_ctoken_types::state::Metadata; +use light_hasher::{to_byte_array::ToByteArray, DataHasher}; +use light_zero_copy::borsh_mut::DeserializeMut; +// TODO: add random test +#[test] +fn test_metadata_hash_consistency() { + // Create test data + let metadata = Metadata { + name: b"MyToken".to_vec(), + symbol: b"MTK".to_vec(), + uri: b"https://example.com/metadata.json".to_vec(), + }; + + // Deserialize to ZStruct + let mut serialized = metadata.try_to_vec().unwrap(); + let (z_metadata, _) = Metadata::zero_copy_at_mut(&mut serialized).unwrap(); + + // Hash both structs + let original_hash = metadata.hash::().unwrap(); + let z_struct_hash = z_metadata.hash::().unwrap(); + + // They should now produce the same hash + assert_eq!( + original_hash, z_struct_hash, + "Hashes should match between original struct and ZStruct" + ); + + println!("Original hash: {:?}", original_hash); + println!("ZStruct hash: {:?}", z_struct_hash); +} + +#[test] +fn test_metadata_to_byte_array_consistency() { + let metadata = Metadata { + name: b"MyToken".to_vec(), + symbol: b"MTK".to_vec(), + uri: b"https://example.com/metadata.json".to_vec(), + }; + + let mut serialized = metadata.try_to_vec().unwrap(); + let (z_metadata, _) = Metadata::zero_copy_at_mut(&mut serialized).unwrap(); + + let original_bytes = metadata.to_byte_array().unwrap(); + let z_struct_bytes = z_metadata.to_byte_array().unwrap(); + + assert_eq!( + original_bytes, z_struct_bytes, + "to_byte_array should produce same result" + ); +} diff --git a/programs/compressed-token/program/tests/metadata_pointer.rs b/programs/compressed-token/program/tests/metadata_pointer.rs new file mode 100644 index 0000000000..d658c57f9b --- /dev/null +++ b/programs/compressed-token/program/tests/metadata_pointer.rs @@ -0,0 +1,194 @@ +/*use borsh::BorshSerialize; +use light_compressed_account::Pubkey; +use light_ctoken_types::{ + instructions::extensions::{ + metadata_pointer::{InitMetadataPointer, MetadataPointer, MetadataPointerConfig}, + ExtensionInstructionData, ZExtensionInstructionData, + }, + state::{ExtensionStruct, ExtensionStructConfig, ZExtensionStruct, ZExtensionStructMut}, +}; +use light_zero_copy::{borsh::Deserialize, borsh_mut::DeserializeMut, ZeroCopyNew}; + +#[test] +fn test_borsh_zero_copy_compatibility() { + let config = ExtensionStructConfig::MetadataPointer(MetadataPointerConfig { + authority: (true, ()), + metadata_address: (true, ()), + }); + let byte_len = ExtensionStruct::byte_len(&config); + let mut bytes = vec![0u8; byte_len]; + // Assert zero init + { + let (zero_copy_new_result, _) = + ExtensionStruct::new_zero_copy(&mut bytes, config.clone()).unwrap(); + if let ZExtensionStructMut::MetadataPointer(metadata) = zero_copy_new_result { + assert!(metadata.authority.is_some()); + assert!(metadata.metadata_address.is_some()); + + let expected = ExtensionStruct::MetadataPointer(MetadataPointer { + authority: Some(Pubkey::new_from_array([0; 32])), + metadata_address: Some(Pubkey::new_from_array([0; 32])), + }); + assert_eq!(bytes, expected.try_to_vec().unwrap()); + } else { + panic!("Unexpected extension type"); + } + } + // Assert zero copy mut + { + let (mut zero_copy_new_result, _) = ExtensionStruct::zero_copy_at_mut(&mut bytes).unwrap(); + + let new_authority = Pubkey::new_from_array([1; 32]); + let new_metadata_address = Pubkey::new_from_array([1; 32]); + if let ZExtensionStructMut::MetadataPointer(metadata) = &mut zero_copy_new_result { + **metadata.authority.as_mut().unwrap() = new_authority; + **metadata.metadata_address.as_mut().unwrap() = new_metadata_address; + } + let expected = ExtensionStruct::MetadataPointer(MetadataPointer { + authority: Some(new_authority), + metadata_address: Some(new_metadata_address), + }); + assert_eq!(bytes, expected.try_to_vec().unwrap()); + } + + // Test zero_copy_at (immutable deserialization) + { + let original_metadata = MetadataPointer { + authority: Some(Pubkey::new_from_array([5; 32])), + metadata_address: Some(Pubkey::new_from_array([6; 32])), + }; + let original_struct = ExtensionStruct::MetadataPointer(original_metadata.clone()); + let serialized_bytes = original_struct.try_to_vec().unwrap(); + + // Test zero_copy_at immutable deserialization + let (zero_copy_result, remaining_bytes) = + ExtensionStruct::zero_copy_at(&serialized_bytes).unwrap(); + assert!(remaining_bytes.is_empty()); + + // Verify the deserialized data matches + if let ZExtensionStruct::MetadataPointer(metadata) = zero_copy_result { + assert_eq!( + *metadata.authority.unwrap(), + Pubkey::new_from_array([5; 32]) + ); + assert_eq!( + *metadata.metadata_address.unwrap(), + Pubkey::new_from_array([6; 32]) + ); + } else { + panic!("deserialization failed ") + } + } +} + +#[test] +fn test_borsh_zero_copy_compatibility_none_fields() { + let original_metadata = MetadataPointer { + authority: None, + metadata_address: None, + }; + let original_struct = ExtensionStruct::MetadataPointer(original_metadata.clone()); + let serialized_bytes = original_struct.try_to_vec().unwrap(); + + let config = ExtensionStructConfig::MetadataPointer(MetadataPointerConfig { + authority: (false, ()), + metadata_address: (false, ()), + }); + let byte_len = ExtensionStruct::byte_len(&config); + let mut bytes = vec![0u8; byte_len]; + + // Assert zero init with None fields + { + let (zero_copy_new_result, _) = + ExtensionStruct::new_zero_copy(&mut bytes, config.clone()).unwrap(); + if let ZExtensionStructMut::MetadataPointer(metadata) = zero_copy_new_result { + assert!(metadata.authority.is_none()); + assert!(metadata.metadata_address.is_none()); + assert_eq!(bytes, serialized_bytes); + } else { + panic!("Unexpected deserialization result"); + } + } + + // Assert zero copy mut with None fields (no mutation needed) + { + let (zero_copy_new_result, _) = ExtensionStruct::zero_copy_at_mut(&mut bytes).unwrap(); + + if let ZExtensionStructMut::MetadataPointer(metadata) = zero_copy_new_result { + assert!(metadata.authority.is_none()); + assert!(metadata.metadata_address.is_none()); + assert_eq!(bytes, serialized_bytes); + } else { + panic!("Unexpected deserialization result"); + } + } + + // Test zero_copy_at (immutable deserialization) with None fields + { + // Test zero_copy_at immutable deserialization + let (zero_copy_result, remaining_bytes) = + ExtensionStruct::zero_copy_at(&serialized_bytes).unwrap(); + assert!(remaining_bytes.is_empty()); + + // Verify the deserialized data matches (None fields) + if let ZExtensionStruct::MetadataPointer(metadata) = zero_copy_result { + assert!(metadata.authority.is_none()); + assert!(metadata.metadata_address.is_none()); + assert_eq!(bytes, serialized_bytes); + } else { + panic!("Unexpected deserialization result"); + } + } +} + +#[test] +fn test_extension_instruction_data_borsh_zero_copy_compatibility() { + // Test with Some values + let init_metadata_pointer = InitMetadataPointer { + authority: Some(Pubkey::new_from_array([1; 32])), + metadata_address: Some(Pubkey::new_from_array([2; 32])), + }; + let instruction_data = ExtensionInstructionData::MetadataPointer(init_metadata_pointer); + let serialized_bytes = instruction_data.try_to_vec().unwrap(); + + // Test zero_copy_at deserialization + let (zero_copy_result, remaining_bytes) = + ExtensionInstructionData::zero_copy_at(&serialized_bytes).unwrap(); + assert!(remaining_bytes.is_empty()); + + // Verify the deserialized data matches + if let ZExtensionInstructionData::MetadataPointer(metadata) = zero_copy_result { + assert_eq!( + *metadata.authority.unwrap(), + Pubkey::new_from_array([1; 32]) + ); + let address = metadata.metadata_address.unwrap(); + assert_eq!(*address, Pubkey::new_from_array([2; 32])); + } else { + panic!("Unexpected deserialization result"); + } +} + +#[test] +fn test_extension_instruction_data_borsh_zero_copy_compatibility_none_fields() { + // Test with None values + let init_metadata_pointer = InitMetadataPointer { + authority: None, + metadata_address: None, + }; + let instruction_data = ExtensionInstructionData::MetadataPointer(init_metadata_pointer); + let serialized_bytes = instruction_data.try_to_vec().unwrap(); + + // Test zero_copy_at deserialization + let (zero_copy_result, remaining_bytes) = + ExtensionInstructionData::zero_copy_at(&serialized_bytes).unwrap(); + assert!(remaining_bytes.is_empty()); + + if let ZExtensionInstructionData::MetadataPointer(metadata) = zero_copy_result { + assert!(metadata.authority.is_none()); + assert!(metadata.metadata_address.is_none()); + } else { + panic!("Unexpected deserialization result"); + } +} +*/ diff --git a/programs/compressed-token/program/tests/mint.rs b/programs/compressed-token/program/tests/mint.rs new file mode 100644 index 0000000000..c45e35b958 --- /dev/null +++ b/programs/compressed-token/program/tests/mint.rs @@ -0,0 +1,556 @@ +use borsh::{BorshDeserialize, BorshSerialize}; +use light_compressed_account::{ + address::derive_address, + compressed_account::{CompressedAccount, CompressedAccountData}, + instruction_data::{ + data::OutputCompressedAccountWithPackedContext, + with_readonly::InstructionDataInvokeCpiWithReadOnly, + }, + Pubkey, +}; +use light_compressed_token::{ + constants::COMPRESSED_MINT_DISCRIMINATOR, + mint::mint_output::create_output_compressed_mint_account, + shared::cpi_bytes_size::{ + allocate_invoke_with_read_only_cpi_bytes, cpi_bytes_config, CpiConfigInput, + }, +}; +use light_ctoken_types::{ + context::TokenContext, + instructions::{ + extensions::{ExtensionInstructionData, TokenMetadataInstructionData}, + mint_to_compressed::CompressedMintInputs, + }, + state::{ + AdditionalMetadata, AdditionalMetadataConfig, CompressedMint, CompressedMintConfig, + ExtensionStruct, ExtensionStructConfig, Metadata, MetadataConfig, TokenMetadata, + TokenMetadataConfig, ZCompressedMint, ZExtensionStruct, + }, +}; +use light_zero_copy::ZeroCopyNew; +use rand::Rng; + +// Function to create expected input account +#[allow(clippy::too_many_arguments)] +fn create_expected_input_account( + mint_pda: Pubkey, + input_supply: u64, + decimals: u8, + is_decompressed: bool, + mint_authority: Option, + freeze_authority: Option, + version: u8, + extensions: Option>, + compressed_account_address: [u8; 32], + merkle_tree_pubkey_index: u8, + queue_pubkey_index: u8, + leaf_index: u32, + prove_by_index: bool, + root_index: u16, +) -> light_compressed_account::instruction_data::with_readonly::InAccount { + let expected_input_compressed_mint = CompressedMint { + spl_mint: mint_pda, + supply: input_supply, + decimals, + is_decompressed, + mint_authority, + freeze_authority, + version, + extensions, + }; + let expected_input_data_hash = expected_input_compressed_mint.hash().unwrap(); + + light_compressed_account::instruction_data::with_readonly::InAccount { + discriminator: COMPRESSED_MINT_DISCRIMINATOR, + data_hash: expected_input_data_hash, + merkle_context: light_compressed_account::compressed_account::PackedMerkleContext { + merkle_tree_pubkey_index, + queue_pubkey_index, + leaf_index, + prove_by_index, + }, + root_index, + lamports: 0, + address: Some(compressed_account_address), + } +} + +// Function to create expected output account +#[allow(clippy::too_many_arguments)] +fn create_expected_output_account( + mint_pda: Pubkey, + output_supply: u64, + decimals: u8, + is_decompressed: bool, + mint_authority: Option, + freeze_authority: Option, + version: u8, + extensions: Option>, + compressed_account_address: [u8; 32], + program_id: Pubkey, + output_merkle_tree_index: u8, +) -> OutputCompressedAccountWithPackedContext { + let expected_compressed_mint = CompressedMint { + spl_mint: mint_pda, + supply: output_supply, + decimals, + is_decompressed, + mint_authority, + freeze_authority, + version, + extensions, + }; + let expected_data_hash = expected_compressed_mint.hash().unwrap(); + + OutputCompressedAccountWithPackedContext { + compressed_account: CompressedAccount { + address: Some(compressed_account_address), + owner: program_id, + lamports: 0, + data: Some(CompressedAccountData { + data: borsh::to_vec(&expected_compressed_mint).unwrap(), + discriminator: COMPRESSED_MINT_DISCRIMINATOR, + data_hash: expected_data_hash, + }), + }, + merkle_tree_index: output_merkle_tree_index, + } +} + +// Function to convert expected accounts to instruction data +fn create_instruction_data_from_expected( + expected_extensions: Option>, +) -> ( + Option>, + Vec, +) { + if let Some(extension_structs) = expected_extensions { + let mut instruction_extensions = Vec::new(); + let mut extension_configs = Vec::new(); + + for extension_struct in extension_structs { + match extension_struct { + ExtensionStruct::TokenMetadata(token_metadata) => { + let instruction_data = TokenMetadataInstructionData { + update_authority: token_metadata.update_authority, + metadata: token_metadata.metadata.clone(), + additional_metadata: if token_metadata.additional_metadata.is_empty() { + None + } else { + Some(token_metadata.additional_metadata.clone()) + }, + version: token_metadata.version, + }; + instruction_extensions + .push(ExtensionInstructionData::TokenMetadata(instruction_data)); + + let additional_metadata_configs = token_metadata + .additional_metadata + .iter() + .map(|item| AdditionalMetadataConfig { + key: item.key.len() as u32, + value: item.value.len() as u32, + }) + .collect(); + + let config = ExtensionStructConfig::TokenMetadata(TokenMetadataConfig { + update_authority: (token_metadata.update_authority.is_some(), ()), + metadata: MetadataConfig { + name: token_metadata.metadata.name.len() as u32, + symbol: token_metadata.metadata.symbol.len() as u32, + uri: token_metadata.metadata.uri.len() as u32, + }, + additional_metadata: additional_metadata_configs, + }); + extension_configs.push(config); + } + /* ExtensionStruct::MetadataPointer(metadata_pointer) => { + let instruction_data = InitMetadataPointer { + authority: metadata_pointer.authority, + metadata_address: metadata_pointer.metadata_address, + }; + instruction_extensions + .push(ExtensionInstructionData::MetadataPointer(instruction_data)); + + let config = ExtensionStructConfig::MetadataPointer(MetadataPointerConfig { + authority: (metadata_pointer.authority.is_some(), ()), + metadata_address: (metadata_pointer.metadata_address.is_some(), ()), + }); + extension_configs.push(config); + }*/ + ExtensionStruct::Compressible(_compressible) => { + // Compressible extension doesn't need special handling in mint tests + let config = ExtensionStructConfig::Compressible; + extension_configs.push(config); + } + _ => {} + } + } + + (Some(instruction_extensions), extension_configs) + } else { + (None, vec![]) + } +} + +// Function to create random extension data +fn create_random_extension_data( + rng: &mut R, + mint_pda: Pubkey, +) -> Option> { + if rng.gen_bool(0.3) { + let update_authority = if rng.gen_bool(0.7) { + Some(Pubkey::new_from_array(rng.gen::<[u8; 32]>())) + } else { + None + }; + + // Generate smaller random metadata for testing + let name_len = rng.gen_range(1..=10); + let symbol_len = rng.gen_range(1..=3); + let uri_len = rng.gen_range(5..=20); + + let name: Vec = (0..name_len).map(|_| rng.gen_range(b'A'..=b'Z')).collect(); + let symbol: Vec = (0..symbol_len) + .map(|_| rng.gen_range(b'A'..=b'Z')) + .collect(); + let uri: Vec = (0..uri_len).map(|_| rng.gen_range(b'a'..=b'z')).collect(); + + // Random additional metadata (50% chance) + let additional_metadata = if rng.gen_bool(0.5) { + let num_items = rng.gen_range(1..=3); + (0..num_items) + .map(|_| { + let key_len = rng.gen_range(3..=16); + let value_len = rng.gen_range(5..=31); + AdditionalMetadata { + key: (0..key_len).map(|_| rng.gen_range(b'a'..=b'z')).collect(), + value: (0..value_len).map(|_| rng.gen_range(b'a'..=b'z')).collect(), + } + }) + .collect() + } else { + vec![] + }; + + let expected_token_metadata = TokenMetadata { + update_authority, + mint: mint_pda, + metadata: Metadata { + name: name.clone(), + symbol: symbol.clone(), + uri: uri.clone(), + }, + additional_metadata, + version: 0, // Hardcode to version 0 (Poseidon) + }; + + Some(vec![ExtensionStruct::TokenMetadata( + expected_token_metadata, + )]) + } else { + None + } +} + +#[test] +fn test_rnd_create_compressed_mint_account() { + let mut rng = rand::thread_rng(); + let iter = 100; + + for _ in 0..iter { + // Generate random mint parameters + let mint_pda = Pubkey::new_from_array(rng.gen::<[u8; 32]>()); + let decimals = rng.gen_range(0..=18u8); + let program_id = Pubkey::new_from_array(rng.gen::<[u8; 32]>()); + let address_merkle_tree = Pubkey::new_from_array(rng.gen::<[u8; 32]>()); + + // Random freeze authority (50% chance) + let freeze_authority = if rng.gen_bool(0.5) { + Some(Pubkey::new_from_array(rng.gen::<[u8; 32]>())) + } else { + None + }; + + let mint_authority = Pubkey::new_from_array(rng.gen::<[u8; 32]>()); + + // Generate version for use in extensions + let version = 0; // rng.gen_range(0..=255u8); + + // Generate random supplies + let input_supply = rng.gen_range(0..=u64::MAX); + let output_supply = rng.gen_range(0..=u64::MAX); + let is_decompressed = rng.gen_bool(0.1); + + // Generate random merkle context + let merkle_tree_pubkey_index = rng.gen_range(0..=255u8); + let queue_pubkey_index = rng.gen_range(0..=255u8); + let leaf_index = rng.gen::(); + let prove_by_index = rng.gen_bool(0.5); + let root_index = rng.gen::(); + let output_merkle_tree_index = rng.gen_range(0..=255u8); + + // Derive compressed account address + let compressed_account_address = derive_address( + &mint_pda.to_bytes(), + &address_merkle_tree.to_bytes(), + &program_id.to_bytes(), + ); + + // Step 1: Create expected extensions + let expected_extensions = create_random_extension_data(&mut rng, mint_pda); + + // Step 2: Create expected input and output accounts + let expected_input_account = create_expected_input_account( + mint_pda, + input_supply, + decimals, + is_decompressed, + Some(mint_authority), + freeze_authority, + version, + expected_extensions.clone(), + compressed_account_address, + merkle_tree_pubkey_index, + queue_pubkey_index, + leaf_index, + prove_by_index, + root_index, + ); + + let expected_output_account = create_expected_output_account( + mint_pda, + output_supply, + decimals, + is_decompressed, + Some(mint_authority), + freeze_authority, + version, + expected_extensions.clone(), + compressed_account_address, + program_id, + output_merkle_tree_index, + ); + + // Step 3: Convert expected accounts to instruction data + let (extensions, extensions_config) = + create_instruction_data_from_expected(expected_extensions.clone()); + + // Step 4: Create allocations and mint config + let mint_config = CompressedMintConfig { + mint_authority: (true, ()), // Always true like in cpi_bytes_config and mint_to_compressed + freeze_authority: (freeze_authority.is_some(), ()), + extensions: (extensions.is_some(), extensions_config.clone()), + }; + + let config_input = CpiConfigInput { + input_accounts: arrayvec::ArrayVec::new(), + output_accounts: arrayvec::ArrayVec::new(), + has_proof: false, + compressed_mint: true, + compressed_mint_with_freeze_authority: freeze_authority.is_some(), + extensions_config: extensions_config.clone(), + }; + + let config = cpi_bytes_config(config_input); + let mut cpi_bytes = allocate_invoke_with_read_only_cpi_bytes(&config); + let (mut cpi_instruction_struct, _) = + light_compressed_account::instruction_data::with_readonly::InstructionDataInvokeCpiWithReadOnly::new_zero_copy( + &mut cpi_bytes[8..], + config, + ) + .unwrap(); + + // Step 5: Create actual input and output data + let input_account = &mut cpi_instruction_struct.input_compressed_accounts[0]; + let output_account = &mut cpi_instruction_struct.output_compressed_accounts[0]; + + // Create input data + use light_compressed_account::compressed_account::PackedMerkleContext; + use light_zero_copy::borsh::Deserialize; + + let input_compressed_mint = CompressedMintInputs { + compressed_mint_input: CompressedMint { + spl_mint: mint_pda, + supply: input_supply, + decimals, + is_decompressed, + mint_authority: Some(mint_authority), + freeze_authority, + version, + extensions: expected_extensions.clone(), + }, + leaf_index, + prove_by_index, + root_index, + address: compressed_account_address, + }; + + let update_instruction_data = light_ctoken_types::instructions::create_compressed_mint::UpdateCompressedMintInstructionData { + leaf_index: input_compressed_mint.leaf_index, + prove_by_index: input_compressed_mint.prove_by_index, + root_index: input_compressed_mint.root_index, + address: input_compressed_mint.address, + proof: None, + mint: light_ctoken_types::instructions::create_compressed_mint::CompressedMintInstructionData { + version: input_compressed_mint.compressed_mint_input.version, + spl_mint: input_compressed_mint.compressed_mint_input.spl_mint, + supply: input_compressed_mint.compressed_mint_input.supply, + decimals: input_compressed_mint.compressed_mint_input.decimals, + is_decompressed: input_compressed_mint.compressed_mint_input.is_decompressed, + freeze_authority: input_compressed_mint.compressed_mint_input.freeze_authority, + extensions: extensions.clone(), + }, + }; + + let input_data = update_instruction_data.try_to_vec().unwrap(); + let (z_update_instruction_data, _) = + light_ctoken_types::instructions::create_compressed_mint::UpdateCompressedMintInstructionData::zero_copy_at(&input_data).unwrap(); + + let mut context = TokenContext::new(); + let hashed_mint_authority = context.get_or_hash_pubkey(&mint_authority.into()); + light_compressed_token::mint::mint_input::create_input_compressed_mint_account( + input_account, + &mut context, + &z_update_instruction_data, + &hashed_mint_authority, + PackedMerkleContext { + merkle_tree_pubkey_index: input_account.merkle_context.merkle_tree_pubkey_index, + queue_pubkey_index: input_account.merkle_context.queue_pubkey_index, + leaf_index: input_account.merkle_context.leaf_index.into(), + prove_by_index: input_account.merkle_context.prove_by_index(), + }, + ) + .unwrap(); + + // Prepare extensions for zero-copy usage + let extensions_data = if let Some(ref extensions) = extensions { + use borsh::BorshSerialize; + let mut extensions_data = Vec::new(); + for extension in extensions { + extension.serialize(&mut extensions_data).unwrap(); + } + Some(extensions_data) + } else { + None + }; + + let z_extensions = if let Some(ref extensions_data) = extensions_data { + let mut z_extensions = Vec::new(); + let mut offset = 0; + for _ in extensions.as_ref().unwrap() { + let (z_ext, remaining) = + ExtensionInstructionData::zero_copy_at(&extensions_data[offset..]).unwrap(); + z_extensions.push(z_ext); + offset = extensions_data.len() - remaining.len(); + } + Some(z_extensions) + } else { + None + }; + + // Create output data + let mut context = TokenContext::new(); + create_output_compressed_mint_account( + output_account, + mint_pda, + decimals, + freeze_authority, + Some(mint_authority), + output_supply.into(), + mint_config, + compressed_account_address, + output_merkle_tree_index, + version, + is_decompressed, + z_extensions.as_deref(), + &mut context, + ) + .unwrap(); + + // Step 6: Assert created data vs expected + let cpi_borsh = + InstructionDataInvokeCpiWithReadOnly::deserialize(&mut &cpi_bytes[8..]).unwrap(); + + let expected = InstructionDataInvokeCpiWithReadOnly { + input_compressed_accounts: vec![expected_input_account], + output_compressed_accounts: vec![expected_output_account], + ..Default::default() + }; + + assert_eq!(cpi_borsh, expected); + } +} + +#[test] +fn test_compressed_mint_borsh_zero_copy_compatibility() { + use light_zero_copy::borsh::Deserialize; + + // Create CompressedMint with token metadata extension + let token_metadata = TokenMetadata { + update_authority: Some(Pubkey::new_from_array([1; 32])), + mint: Pubkey::new_from_array([2; 32]), + metadata: Metadata { + name: b"TestToken".to_vec(), + symbol: b"TT".to_vec(), + uri: b"https://test.com".to_vec(), + }, + additional_metadata: vec![], + version: 0, + }; + + let compressed_mint = CompressedMint { + spl_mint: Pubkey::new_from_array([3; 32]), + supply: 1000u64, + decimals: 6u8, + is_decompressed: false, + mint_authority: Some(Pubkey::new_from_array([4; 32])), + freeze_authority: None, + version: 1u8, + extensions: Some(vec![ExtensionStruct::TokenMetadata(token_metadata)]), + }; + + // Serialize with Borsh + let borsh_bytes = borsh::to_vec(&compressed_mint).unwrap(); + + // Deserialize with zero_copy_at + let (zc_mint, remaining): (ZCompressedMint<'_>, &[u8]) = + CompressedMint::zero_copy_at(&borsh_bytes).unwrap(); + assert!(remaining.is_empty()); + + // Verify data matches - zero-copy fields vs original fields + assert_eq!(zc_mint.spl_mint, compressed_mint.spl_mint); + assert_eq!(u64::from(zc_mint.supply), compressed_mint.supply); + assert_eq!(zc_mint.decimals, compressed_mint.decimals); + assert_eq!(zc_mint.version, compressed_mint.version); + + // Check extensions match + if let Some(ref zc_extensions) = zc_mint.extensions { + if let Some(ref orig_extensions) = compressed_mint.extensions { + for (z_extension, extension) in zc_extensions.iter().zip(orig_extensions.iter()) { + match (z_extension, extension) { + ( + ZExtensionStruct::TokenMetadata(z_metadata), + ExtensionStruct::TokenMetadata(metadata), + ) => { + assert_eq!(z_metadata.metadata.name, metadata.metadata.name.as_slice()); + assert_eq!( + z_metadata.metadata.symbol, + metadata.metadata.symbol.as_slice() + ); + assert_eq!(z_metadata.metadata.uri, metadata.metadata.uri.as_slice()); + assert_eq!(*z_metadata.mint, metadata.mint); + assert_eq!( + z_metadata.update_authority.map(|x| *x), + metadata.update_authority + ); + assert_eq!(z_metadata.version, metadata.version); + } + _ => panic!("Mismatched extension types"), + } + } + } + } + + println!("Borsh/zero-copy compatibility test passed"); +} diff --git a/programs/compressed-token/program/tests/multi_sum_check.rs b/programs/compressed-token/program/tests/multi_sum_check.rs new file mode 100644 index 0000000000..d728efb1f0 --- /dev/null +++ b/programs/compressed-token/program/tests/multi_sum_check.rs @@ -0,0 +1,378 @@ +use std::collections::HashMap; + +use anchor_compressed_token::ErrorCode; +use anchor_lang::AnchorSerialize; +use light_compressed_token::transfer2::sum_check::sum_check_multi_mint; +use light_ctoken_types::instructions::transfer2::{ + Compression, CompressionMode, MultiInputTokenDataWithContext, MultiTokenTransferOutputData, +}; +use light_zero_copy::borsh::Deserialize; + +type Result = std::result::Result; +// TODO: check test coverage +#[test] +fn test_multi_sum_check() { + // SUCCEED: no relay fee, compression + multi_sum_check_test(&[100, 50], &[150], None, CompressionMode::Decompress).unwrap(); + multi_sum_check_test( + &[75, 25, 25], + &[25, 25, 25, 25, 12, 13], + None, + CompressionMode::Decompress, + ) + .unwrap(); + + // FAIL: no relay fee, compression + multi_sum_check_test(&[100, 50], &[150 + 1], None, CompressionMode::Decompress).unwrap_err(); + multi_sum_check_test(&[100, 50], &[150 - 1], None, CompressionMode::Decompress).unwrap_err(); + multi_sum_check_test(&[100, 50], &[], None, CompressionMode::Decompress).unwrap_err(); + multi_sum_check_test(&[], &[100, 50], None, CompressionMode::Decompress).unwrap_err(); + + // SUCCEED: empty + multi_sum_check_test(&[], &[], None, CompressionMode::Compress).unwrap(); + multi_sum_check_test(&[], &[], None, CompressionMode::Decompress).unwrap(); + // FAIL: empty + multi_sum_check_test(&[], &[], Some(1), CompressionMode::Decompress).unwrap_err(); + multi_sum_check_test(&[], &[], Some(1), CompressionMode::Compress).unwrap_err(); + + // SUCCEED: with compress + multi_sum_check_test(&[100], &[123], Some(23), CompressionMode::Compress).unwrap(); + multi_sum_check_test(&[], &[150], Some(150), CompressionMode::Compress).unwrap(); + // FAIL: compress + multi_sum_check_test(&[], &[150], Some(150 - 1), CompressionMode::Compress).unwrap_err(); + multi_sum_check_test(&[], &[150], Some(150 + 1), CompressionMode::Compress).unwrap_err(); + + // SUCCEED: with decompress + multi_sum_check_test(&[100, 50], &[100], Some(50), CompressionMode::Decompress).unwrap(); + multi_sum_check_test(&[100, 50], &[], Some(150), CompressionMode::Decompress).unwrap(); + // FAIL: decompress + multi_sum_check_test(&[100, 50], &[], Some(150 - 1), CompressionMode::Decompress).unwrap_err(); + multi_sum_check_test(&[100, 50], &[], Some(150 + 1), CompressionMode::Decompress).unwrap_err(); +} + +fn multi_sum_check_test( + input_amounts: &[u64], + output_amounts: &[u64], + compress_or_decompress_amount: Option, + compression_mode: CompressionMode, +) -> Result<()> { + // Create normal types + let inputs: Vec<_> = input_amounts + .iter() + .map(|&amount| MultiInputTokenDataWithContext { + amount, + ..Default::default() + }) + .collect(); + + let outputs: Vec<_> = output_amounts + .iter() + .map(|&amount| MultiTokenTransferOutputData { + amount, + ..Default::default() + }) + .collect(); + + let compressions = compress_or_decompress_amount.map(|amount| { + vec![Compression { + amount, + mode: compression_mode, + mint: 0, // Same mint + source_or_recipient: 0, + authority: 0, + }] + }); + + // Serialize to bytes using borsh + let input_bytes = inputs.try_to_vec().unwrap(); + let output_bytes = outputs.try_to_vec().unwrap(); + let compression_bytes = compressions.as_ref().map(|c| c.try_to_vec().unwrap()); + + // Deserialize as zero-copy + let (inputs_zc, _) = Vec::::zero_copy_at(&input_bytes).unwrap(); + let (outputs_zc, _) = Vec::::zero_copy_at(&output_bytes).unwrap(); + let compressions_zc = if let Some(ref bytes) = compression_bytes { + let (comp, _) = Vec::::zero_copy_at(bytes).unwrap(); + Some(comp) + } else { + None + }; + + // Call our sum check function + sum_check_multi_mint(&inputs_zc, &outputs_zc, compressions_zc.as_deref()) +} + +#[test] +fn test_simple_multi_mint_cases() { + // First test a simple known case + test_simple_multi_mint().unwrap(); +} + +#[test] +fn test_multi_mint_randomized() { + // Test multiple scenarios with different mint combinations + for scenario in 0..3000 { + println!("Testing scenario {}", scenario); + + // Create test case with multiple mints + let seed = scenario as u64; + test_randomized_scenario(seed).unwrap(); + } +} +#[test] +fn test_failing_multi_mint_cases() { + // Test specific failure cases + test_failing_cases().unwrap(); +} +fn test_simple_multi_mint() -> Result<()> { + // Simple test: mint 0: input 100, output 100; mint 1: input 200, output 200 + let inputs = vec![(0, 100), (1, 200)]; + let outputs = vec![(0, 100), (1, 200)]; + let compressions = vec![]; + + test_multi_mint_scenario(&inputs, &outputs, &compressions)?; + + // Test with compression: mint 0: input 100 + compress 50 = output 150 + let inputs = vec![(0, 100)]; + let outputs = vec![(0, 150)]; + let compressions = vec![(0, 50, CompressionMode::Compress)]; + + test_multi_mint_scenario(&inputs, &outputs, &compressions)?; + + // Test with decompression: mint 0: input 200 - decompress 50 = output 150 + let inputs = vec![(0, 200)]; + let outputs = vec![(0, 150)]; + let compressions = vec![(0, 50, CompressionMode::Decompress)]; + + test_multi_mint_scenario(&inputs, &outputs, &compressions) +} + +fn test_randomized_scenario(seed: u64) -> Result<()> { + let mut rng_state = seed; + + // Simple LCG for deterministic randomness + let mut next_rand = || { + rng_state = rng_state.wrapping_mul(1103515245).wrapping_add(12345); + rng_state + }; + + // Generate 2-4 mints + let num_mints = 2 + (next_rand() % 3) as usize; + let mint_ids: Vec = (0..num_mints as u8).collect(); + + // Track balances per mint + let mut mint_balances: HashMap = HashMap::new(); + + // Generate inputs (1-6 inputs) + let num_inputs = 1 + (next_rand() % 6) as usize; + let mut inputs = Vec::new(); + + for _ in 0..num_inputs { + let mint = mint_ids[(next_rand() % num_mints as u64) as usize]; + let amount = 100 + (next_rand() % 1000); + + inputs.push((mint, amount)); + *mint_balances.entry(mint).or_insert(0) += amount as i128; + } + + // Generate compressions (0-3 compressions) + let num_compressions = (next_rand() % 4) as usize; + let mut compressions = Vec::new(); + + for _ in 0..num_compressions { + let mint = mint_ids[(next_rand() % num_mints as u64) as usize]; + let amount = 50 + (next_rand() % 500); + let compression_mode = if (next_rand() % 2) == 0 { + CompressionMode::Compress + } else { + CompressionMode::Decompress + }; + + compressions.push((mint, amount, compression_mode)); + + if matches!(compression_mode, CompressionMode::Compress) { + *mint_balances.entry(mint).or_insert(0) += amount as i128; + } else { + // Only allow decompress if the mint has sufficient balance + let current_balance = *mint_balances.entry(mint).or_insert(0); + if current_balance >= amount as i128 { + *mint_balances.entry(mint).or_insert(0) -= amount as i128; + } else { + // Convert to compress instead to avoid negative balance + compressions.last_mut().unwrap().2 = CompressionMode::Compress; + *mint_balances.entry(mint).or_insert(0) += amount as i128; + } + } + } + + // Ensure all balances are non-negative (adjust decompressions if needed) + for (&mint, balance) in mint_balances.iter_mut() { + if *balance < 0 { + // Add compression to make balance positive + let needed = (-*balance) as u64; + compressions.push((mint, needed, CompressionMode::Compress)); + *balance += needed as i128; + } + } + + // Generate outputs that exactly match the remaining balances + let mut outputs = Vec::new(); + for (&mint, &balance) in mint_balances.iter() { + if balance > 0 { + // Split the balance into 1-3 outputs + let num_outputs = 1 + (next_rand() % 3) as usize; + let mut remaining = balance as u64; + + for i in 0..num_outputs { + let amount = if i == num_outputs - 1 { + // Last output gets the remainder + remaining + } else if remaining <= 1 { + break; // Don't create zero-amount outputs + } else { + let max_amount = remaining / (num_outputs - i) as u64; + if max_amount == 0 { + break; + } else { + 1 + (next_rand() % max_amount.max(1)) + } + }; + + if amount > 0 && remaining >= amount { + outputs.push((mint, amount)); + remaining -= amount; + } else { + break; + } + } + + // Add any remaining amount as final output + if remaining > 0 { + outputs.push((mint, remaining)); + } + } + } + + // Debug print for first scenario only + if seed == 0 { + println!( + "Debug scenario {}: inputs={:?}, compressions={:?}, outputs={:?}", + seed, inputs, compressions, outputs + ); + println!("Balances: {:?}", mint_balances); + } + + // Sort inputs by mint for order validation + inputs.sort_by_key(|(mint, _)| *mint); + // Sort outputs by mint for order validation + outputs.sort_by_key(|(mint, _)| *mint); + + // Test the sum check + test_multi_mint_scenario(&inputs, &outputs, &compressions) +} + +fn test_failing_cases() -> Result<()> { + // Test case 1: Wrong output amount + let inputs = vec![(0, 100), (1, 200)]; + let outputs = vec![(0, 100), (1, 201)]; // Wrong amount + let compressions = vec![]; + + match test_multi_mint_scenario(&inputs, &outputs, &compressions) { + Err(ErrorCode::ComputeOutputSumFailed) => {} // Expected + Err(e) => panic!("Expected ComputeOutputSumFailed, got: {:?}", e), + Ok(_) => panic!("Expected ComputeOutputSumFailed, but transaction succeeded"), + } + + // Test case 2: Output for non-existent mint + let inputs = vec![(0, 100)]; + let outputs = vec![(0, 50), (1, 50)]; // Mint 1 not in inputs + let compressions = vec![]; + + match test_multi_mint_scenario(&inputs, &outputs, &compressions) { + Err(ErrorCode::ComputeOutputSumFailed) => {} // Expected + _ => panic!("Should have failed with SumCheckFailed"), + } + + // Test case 3: Too many mints (>5) + let inputs = vec![(0, 10), (1, 10), (2, 10), (3, 10), (4, 10), (5, 10)]; + let outputs = vec![(0, 10), (1, 10), (2, 10), (3, 10), (4, 10), (5, 10)]; + let compressions = vec![]; + + match test_multi_mint_scenario(&inputs, &outputs, &compressions) { + Err(ErrorCode::TooManyMints) => {} // Expected + _ => panic!("Should have failed with TooManyMints"), + } + + // Test case 4: Inputs out of order + let inputs = vec![(1, 100), (0, 200)]; // Wrong order + let outputs = vec![(0, 200), (1, 100)]; + let compressions = vec![]; + + match test_multi_mint_scenario(&inputs, &outputs, &compressions) { + Err(ErrorCode::InputsOutOfOrder) => {} // Expected + _ => panic!("Should have failed with InputsOutOfOrder"), + } + + Ok(()) +} + +fn test_multi_mint_scenario( + inputs: &[(u8, u64)], // (mint, amount) + outputs: &[(u8, u64)], // (mint, amount) + compressions: &[(u8, u64, CompressionMode)], // (mint, amount, compression_mode) +) -> Result<()> { + // Create input structures + let input_structs: Vec<_> = inputs + .iter() + .map(|&(mint, amount)| MultiInputTokenDataWithContext { + amount, + mint, + ..Default::default() + }) + .collect(); + + // Create output structures + let output_structs: Vec<_> = outputs + .iter() + .map(|&(mint, amount)| MultiTokenTransferOutputData { + amount, + mint, + ..Default::default() + }) + .collect(); + + // Create compression structures + + let compression_structs: Vec<_> = compressions + .iter() + .map(|&(mint, amount, mode)| Compression { + amount, + mode, + mint, + source_or_recipient: 0, + authority: 0, + }) + .collect(); + + // Serialize to bytes + let input_bytes = input_structs.try_to_vec().unwrap(); + let output_bytes = output_structs.try_to_vec().unwrap(); + let compression_bytes = if compression_structs.is_empty() { + None + } else { + Some(compression_structs.try_to_vec().unwrap()) + }; + + // Deserialize as zero-copy + let (inputs_zc, _) = Vec::::zero_copy_at(&input_bytes).unwrap(); + let (outputs_zc, _) = Vec::::zero_copy_at(&output_bytes).unwrap(); + let compressions_zc = if let Some(ref bytes) = compression_bytes { + let (comp, _) = Vec::::zero_copy_at(bytes).unwrap(); + Some(comp) + } else { + None + }; + + // Call sum check + sum_check_multi_mint(&inputs_zc, &outputs_zc, compressions_zc.as_deref()) +} diff --git a/programs/compressed-token/program/tests/token_input.rs b/programs/compressed-token/program/tests/token_input.rs new file mode 100644 index 0000000000..37480cf25c --- /dev/null +++ b/programs/compressed-token/program/tests/token_input.rs @@ -0,0 +1,196 @@ +use anchor_compressed_token::TokenData as AnchorTokenData; +use anchor_lang::prelude::*; +use arrayvec::ArrayVec; +use borsh::{BorshDeserialize, BorshSerialize}; +use light_account_checks::account_info::test_account_info::pinocchio::get_account_info; +use light_compressed_account::instruction_data::with_readonly::{ + InAccount, InstructionDataInvokeCpiWithReadOnly, +}; +use light_compressed_token::{ + constants::TOKEN_COMPRESSED_ACCOUNT_DISCRIMINATOR, + shared::{ + cpi_bytes_size::{ + allocate_invoke_with_read_only_cpi_bytes, cpi_bytes_config, CpiConfigInput, + }, + token_input::set_input_compressed_account, + }, +}; +use light_ctoken_types::{ + context::TokenContext, instructions::transfer2::MultiInputTokenDataWithContext, + state::AccountState, +}; +use light_sdk::instruction::PackedMerkleContext; +use light_zero_copy::{borsh::Deserialize, ZeroCopyNew}; +use pinocchio::account_info::AccountInfo; +use rand::Rng; + +#[test] +fn test_rnd_create_input_compressed_account() { + let mut rng = rand::thread_rng(); + let iter = 1000; + + for _ in 0..iter { + // Generate random parameters + let mint_pubkey = Pubkey::new_from_array(rng.gen::<[u8; 32]>()); + let owner_pubkey = Pubkey::new_from_array(rng.gen::<[u8; 32]>()); + let delegate_pubkey = Pubkey::new_from_array(rng.gen::<[u8; 32]>()); + + // Random amount from 0 to u64::MAX + let amount = rng.gen::(); + let lamports = rng.gen_range(0..=1000000u64); + + // Random delegate flag (30% chance) + let with_delegate = rng.gen_bool(0.3); + + // Random merkle context fields + let merkle_tree_pubkey_index = rng.gen_range(0..=255u8); + let queue_pubkey_index = rng.gen_range(0..=255u8); + let leaf_index = rng.gen::(); + let prove_by_index = rng.gen_bool(0.5); + let root_index = rng.gen::(); + + // Create input token data + let input_token_data = MultiInputTokenDataWithContext { + amount, + merkle_context: PackedMerkleContext { + merkle_tree_pubkey_index, + queue_pubkey_index, + leaf_index, + prove_by_index, + }, + root_index, + mint: 0, // mint is at index 0 in remaining_accounts + owner: 1, // owner is at index 1 in remaining_accounts + with_delegate, + delegate: if with_delegate { 2 } else { 0 }, // delegate at index 2 if present + version: 2, + }; + + // Serialize and get zero-copy reference + let input_data = input_token_data.try_to_vec().unwrap(); + let (z_input_data, _) = MultiInputTokenDataWithContext::zero_copy_at(&input_data).unwrap(); + + // Create mock remaining accounts + let mut mock_accounts = vec![ + create_mock_account(mint_pubkey, false), // mint at index 0 + create_mock_account(owner_pubkey, !with_delegate), // owner at index 1, signer if no delegate + ]; + + if with_delegate { + mock_accounts.push(create_mock_account(delegate_pubkey, true)); // delegate at index 2, signer + } + + let remaining_accounts: Vec = mock_accounts; + + // Test both frozen and unfrozen states + for is_frozen in [false, true] { + // Allocate CPI bytes structure like in other tests + let config_input = CpiConfigInput { + input_accounts: { + let mut arr = ArrayVec::new(); + arr.push(false); // Basic input account + arr + }, + output_accounts: ArrayVec::new(), + has_proof: false, + compressed_mint: false, + compressed_mint_with_freeze_authority: false, + extensions_config: vec![], + }; + + let config = cpi_bytes_config(config_input); + let mut cpi_bytes = allocate_invoke_with_read_only_cpi_bytes(&config); + let (mut cpi_instruction_struct, _) = + InstructionDataInvokeCpiWithReadOnly::new_zero_copy(&mut cpi_bytes[8..], config) + .unwrap(); + + // Get the input account reference + let input_account = &mut cpi_instruction_struct.input_compressed_accounts[0]; + + let mut context = TokenContext::new(); + + // Call the function under test + let result = if is_frozen { + set_input_compressed_account::( + input_account, + &mut context, + &z_input_data, + remaining_accounts.as_slice(), + lamports, + ) + } else { + set_input_compressed_account::( + input_account, + &mut context, + &z_input_data, + remaining_accounts.as_slice(), + lamports, + ) + }; + + assert!(result.is_ok(), "Function failed: {:?}", result.err()); + + // Deserialize for validation using borsh pattern like other tests + let cpi_borsh = + InstructionDataInvokeCpiWithReadOnly::deserialize(&mut &cpi_bytes[8..]).unwrap(); + + // Create expected token data for validation + let expected_owner = owner_pubkey; + let expected_delegate = if with_delegate { + Some(delegate_pubkey) + } else { + None + }; + + let expected_token_data = AnchorTokenData { + mint: mint_pubkey.into(), + owner: expected_owner.into(), + amount, + delegate: expected_delegate.map(|d| d.into()), + state: if is_frozen { + AccountState::Frozen + } else { + AccountState::Initialized + }, + tlv: None, + }; + + // Calculate expected data hash + let expected_hash = expected_token_data.hash().unwrap(); + + // Build expected input account + let expected_input_account = InAccount { + discriminator: TOKEN_COMPRESSED_ACCOUNT_DISCRIMINATOR, + data_hash: expected_hash, + merkle_context: PackedMerkleContext { + merkle_tree_pubkey_index, + queue_pubkey_index, + leaf_index, + prove_by_index, + }, + root_index, + lamports, + address: None, + }; + + let expected = InstructionDataInvokeCpiWithReadOnly { + input_compressed_accounts: vec![expected_input_account], + ..Default::default() + }; + + assert_eq!(cpi_borsh, expected); + } + } +} + +// Helper function to create mock AccountInfo +fn create_mock_account(pubkey: Pubkey, is_signer: bool) -> AccountInfo { + get_account_info( + pubkey.to_bytes(), + Pubkey::default().to_bytes(), // owner is not checked, + is_signer, + false, + false, + vec![], + ) +} diff --git a/programs/compressed-token/program/tests/token_output.rs b/programs/compressed-token/program/tests/token_output.rs new file mode 100644 index 0000000000..b3c0761cb3 --- /dev/null +++ b/programs/compressed-token/program/tests/token_output.rs @@ -0,0 +1,161 @@ +use anchor_compressed_token::TokenData as AnchorTokenData; +use arrayvec::ArrayVec; +use borsh::{BorshDeserialize, BorshSerialize}; +use light_compressed_account::{ + compressed_account::{CompressedAccount, CompressedAccountData}, + hash_to_bn254_field_size_be, + instruction_data::{ + data::OutputCompressedAccountWithPackedContext, + with_readonly::InstructionDataInvokeCpiWithReadOnly, + }, + Pubkey, +}; +use light_compressed_token::{ + constants::TOKEN_COMPRESSED_ACCOUNT_DISCRIMINATOR, + shared::{ + cpi_bytes_size::{ + allocate_invoke_with_read_only_cpi_bytes, cpi_bytes_config, CpiConfigInput, + }, + token_output::set_output_compressed_account, + }, +}; +use light_ctoken_types::{context::TokenContext, state::AccountState}; +use light_zero_copy::ZeroCopyNew; + +#[test] +fn test_rnd_create_output_compressed_accounts() { + use rand::Rng; + let mut rng = rand::rngs::ThreadRng::default(); + + let iter = 1000; + for _ in 0..iter { + let mint_pubkey = Pubkey::new_from_array(rng.gen::<[u8; 32]>()); + let hashed_mint = hash_to_bn254_field_size_be(mint_pubkey.to_bytes().as_slice()); + + // Random number of output accounts (0-35 max) + let num_outputs = rng.gen_range(0..=35); + + // Generate random owners and amounts + let mut owner_pubkeys = Vec::new(); + let mut amounts = Vec::new(); + let mut delegate_flags = Vec::new(); + let mut lamports_vec = Vec::new(); + let mut merkle_tree_indices = Vec::new(); + + for _ in 0..num_outputs { + owner_pubkeys.push(Pubkey::new_from_array(rng.gen::<[u8; 32]>())); + amounts.push(rng.gen_range(1..=u64::MAX)); + delegate_flags.push(rng.gen_bool(0.3)); // 30% chance of having delegate + lamports_vec.push(if rng.gen_bool(0.2) { + Some(rng.gen_range(1..=1000000)) + } else { + None + }); + merkle_tree_indices.push(rng.gen_range(0..=255u8)); + } + + // Random delegate + let delegate = if delegate_flags.iter().any(|&has_delegate| has_delegate) { + Some(Pubkey::new_from_array(rng.gen::<[u8; 32]>())) + } else { + None + }; + + let lamports = if lamports_vec.iter().any(|l| l.is_some()) { + Some(lamports_vec.clone()) + } else { + None + }; + + // Create output config + let mut outputs = ArrayVec::new(); + for &has_delegate in &delegate_flags { + outputs.push(has_delegate); + } + + let config_input = CpiConfigInput { + input_accounts: ArrayVec::new(), + output_accounts: outputs, + has_proof: false, + compressed_mint: false, + compressed_mint_with_freeze_authority: false, + extensions_config: vec![], // TODO: Add extensions support for outputs test + }; + + let config = cpi_bytes_config(config_input.clone()); + let mut cpi_bytes = allocate_invoke_with_read_only_cpi_bytes(&config); + let (mut cpi_instruction_struct, _) = InstructionDataInvokeCpiWithReadOnly::new_zero_copy( + &mut cpi_bytes[8..], + config.clone(), + ) + .unwrap(); + + let mut context = TokenContext::new(); + for (index, output_account) in cpi_instruction_struct + .output_compressed_accounts + .iter_mut() + .enumerate() + { + let output_delegate = if delegate_flags[index] { + delegate + } else { + None + }; + + set_output_compressed_account::( + output_account, + &mut context, + owner_pubkeys[index], + output_delegate, + amounts[index], + lamports.as_ref().and_then(|l| l[index]), + mint_pubkey, + &hashed_mint, + merkle_tree_indices[index], + 2, + ) + .unwrap(); + } + + let cpi_borsh = + InstructionDataInvokeCpiWithReadOnly::deserialize(&mut &cpi_bytes[8..]).unwrap(); + + // Build expected output + let mut expected_accounts = Vec::new(); + + for i in 0..num_outputs { + let token_delegate = if delegate_flags[i] { delegate } else { None }; + let account_lamports = lamports_vec[i].unwrap_or(0); + + let token_data = AnchorTokenData { + mint: mint_pubkey, + owner: owner_pubkeys[i], + amount: amounts[i], + delegate: token_delegate, + state: AccountState::Initialized, + tlv: None, + }; + let data_hash = token_data.hash().unwrap(); + + expected_accounts.push(OutputCompressedAccountWithPackedContext { + compressed_account: CompressedAccount { + address: None, + owner: light_compressed_token::ID.into(), + lamports: account_lamports, + data: Some(CompressedAccountData { + data: token_data.try_to_vec().unwrap(), + discriminator: TOKEN_COMPRESSED_ACCOUNT_DISCRIMINATOR, + data_hash, + }), + }, + merkle_tree_index: merkle_tree_indices[i], + }); + } + + let expected = InstructionDataInvokeCpiWithReadOnly { + output_compressed_accounts: expected_accounts, + ..Default::default() + }; + assert_eq!(cpi_borsh, expected); + } +} diff --git a/programs/compressed-token/src/token_data.rs b/programs/compressed-token/src/token_data.rs deleted file mode 100644 index b1cbdfe78e..0000000000 --- a/programs/compressed-token/src/token_data.rs +++ /dev/null @@ -1,445 +0,0 @@ -use std::vec; - -use anchor_lang::{ - prelude::borsh, solana_program::pubkey::Pubkey, AnchorDeserialize, AnchorSerialize, -}; -use light_compressed_account::hash_to_bn254_field_size_be; -use light_hasher::{errors::HasherError, Hasher, Poseidon}; - -#[derive(Clone, Copy, Debug, PartialEq, Eq, AnchorSerialize, AnchorDeserialize)] -#[repr(u8)] -pub enum AccountState { - Initialized, - Frozen, -} - -#[derive(Debug, PartialEq, Eq, AnchorSerialize, AnchorDeserialize, Clone)] -pub struct TokenData { - /// The mint associated with this account - pub mint: Pubkey, - /// The owner of this account. - pub owner: Pubkey, - /// The amount of tokens this account holds. - pub amount: u64, - /// If `delegate` is `Some` then `delegated_amount` represents - /// the amount authorized by the delegate - pub delegate: Option, - /// The account's state - pub state: AccountState, - /// Placeholder for TokenExtension tlv data (unimplemented) - pub tlv: Option>, -} - -/// Hashing schema: H(mint, owner, amount, delegate, delegated_amount, -/// is_native, state) -/// -/// delegate, delegated_amount, is_native and state have dynamic positions. -/// Always hash mint, owner and amount If delegate hash delegate and -/// delegated_amount together. If is native hash is_native else is omitted. -/// If frozen hash AccountState::Frozen else is omitted. -/// -/// Security: to prevent the possibility that different fields with the same -/// value to result in the same hash we add a prefix to the delegated amount, is -/// native and state fields. This way we can have a dynamic hashing schema and -/// hash only used values. -impl TokenData { - /// Only the spl representation of native tokens (wrapped SOL) is - /// compressed. - /// The sol value is stored in the token pool account. - /// The sol value in the compressed account is independent from - /// the wrapped sol amount. - pub fn is_native(&self) -> bool { - self.mint == spl_token::native_mint::id() - } - pub fn hash_with_hashed_values( - hashed_mint: &[u8; 32], - hashed_owner: &[u8; 32], - amount_bytes: &[u8; 32], - hashed_delegate: &Option<&[u8; 32]>, - ) -> std::result::Result<[u8; 32], HasherError> { - Self::hash_inputs_with_hashed_values::( - hashed_mint, - hashed_owner, - amount_bytes, - hashed_delegate, - ) - } - - pub fn hash_frozen_with_hashed_values( - hashed_mint: &[u8; 32], - hashed_owner: &[u8; 32], - amount_bytes: &[u8; 32], - hashed_delegate: &Option<&[u8; 32]>, - ) -> std::result::Result<[u8; 32], HasherError> { - Self::hash_inputs_with_hashed_values::( - hashed_mint, - hashed_owner, - amount_bytes, - hashed_delegate, - ) - } - - /// We should not hash pubkeys multiple times. For all we can assume mints - /// are equal. For all input compressed accounts we assume owners are - /// equal. - pub fn hash_inputs_with_hashed_values( - mint: &[u8; 32], - owner: &[u8; 32], - amount_bytes: &[u8], - hashed_delegate: &Option<&[u8; 32]>, - ) -> std::result::Result<[u8; 32], HasherError> { - let mut hash_inputs = vec![mint.as_slice(), owner.as_slice(), amount_bytes]; - if let Some(hashed_delegate) = hashed_delegate { - hash_inputs.push(hashed_delegate.as_slice()); - } - let mut state_bytes = [0u8; 32]; - if FROZEN_INPUTS { - state_bytes[31] = AccountState::Frozen as u8; - hash_inputs.push(&state_bytes[..]); - } - Poseidon::hashv(hash_inputs.as_slice()) - } -} - -impl TokenData { - /// Hashes token data of token accounts. - /// - /// Note, hashing changed for token account data in batched Merkle trees. - /// For hashing of token account data stored in concurrent Merkle trees use hash_legacy(). - pub fn hash(&self) -> std::result::Result<[u8; 32], HasherError> { - self._hash::() - } - - /// Hashes token data of token accounts stored in concurrent Merkle trees. - pub fn hash_legacy(&self) -> std::result::Result<[u8; 32], HasherError> { - self._hash::() - } - - fn _hash(&self) -> std::result::Result<[u8; 32], HasherError> { - let hashed_mint = hash_to_bn254_field_size_be(self.mint.to_bytes().as_slice()); - let hashed_owner = hash_to_bn254_field_size_be(self.owner.to_bytes().as_slice()); - let mut amount_bytes = [0u8; 32]; - if BATCHED { - amount_bytes[24..].copy_from_slice(self.amount.to_be_bytes().as_slice()); - } else { - amount_bytes[24..].copy_from_slice(self.amount.to_le_bytes().as_slice()); - } - let hashed_delegate; - let hashed_delegate_option = if let Some(delegate) = self.delegate { - hashed_delegate = hash_to_bn254_field_size_be(delegate.to_bytes().as_slice()); - Some(&hashed_delegate) - } else { - None - }; - if self.state != AccountState::Initialized { - Self::hash_inputs_with_hashed_values::( - &hashed_mint, - &hashed_owner, - &amount_bytes, - &hashed_delegate_option, - ) - } else { - Self::hash_inputs_with_hashed_values::( - &hashed_mint, - &hashed_owner, - &amount_bytes, - &hashed_delegate_option, - ) - } - } -} - -#[cfg(test)] -pub mod test { - - use num_bigint::BigUint; - use rand::Rng; - - use super::*; - - #[test] - fn equivalency_of_hash_functions() { - let token_data = TokenData { - mint: Pubkey::new_unique(), - owner: Pubkey::new_unique(), - amount: 100, - delegate: Some(Pubkey::new_unique()), - state: AccountState::Initialized, - tlv: None, - }; - let hashed_token_data = token_data.hash_legacy().unwrap(); - let hashed_mint = hash_to_bn254_field_size_be(token_data.mint.to_bytes().as_slice()); - let hashed_owner = hash_to_bn254_field_size_be(token_data.owner.to_bytes().as_slice()); - let hashed_delegate = - hash_to_bn254_field_size_be(token_data.delegate.unwrap().to_bytes().as_slice()); - let mut amount_bytes = [0u8; 32]; - amount_bytes[24..].copy_from_slice(token_data.amount.to_le_bytes().as_slice()); - let hashed_token_data_with_hashed_values = - TokenData::hash_inputs_with_hashed_values::( - &hashed_mint, - &hashed_owner, - &amount_bytes, - &Some(&hashed_delegate), - ) - .unwrap(); - assert_eq!(hashed_token_data, hashed_token_data_with_hashed_values); - - let token_data = TokenData { - mint: Pubkey::new_unique(), - owner: Pubkey::new_unique(), - amount: 101, - delegate: None, - state: AccountState::Initialized, - tlv: None, - }; - let hashed_token_data = token_data.hash_legacy().unwrap(); - let hashed_mint = hash_to_bn254_field_size_be(token_data.mint.to_bytes().as_slice()); - let hashed_owner = hash_to_bn254_field_size_be(token_data.owner.to_bytes().as_slice()); - let mut amount_bytes = [0u8; 32]; - amount_bytes[24..].copy_from_slice(token_data.amount.to_le_bytes().as_slice()); - let hashed_token_data_with_hashed_values = - TokenData::hash_with_hashed_values(&hashed_mint, &hashed_owner, &amount_bytes, &None) - .unwrap(); - assert_eq!(hashed_token_data, hashed_token_data_with_hashed_values); - } - - impl TokenData { - fn legacy_hash(&self) -> std::result::Result<[u8; 32], HasherError> { - let hashed_mint = hash_to_bn254_field_size_be(self.mint.to_bytes().as_slice()); - let hashed_owner = hash_to_bn254_field_size_be(self.owner.to_bytes().as_slice()); - let amount_bytes = self.amount.to_le_bytes(); - let hashed_delegate; - let hashed_delegate_option = if let Some(delegate) = self.delegate { - hashed_delegate = hash_to_bn254_field_size_be(delegate.to_bytes().as_slice()); - Some(&hashed_delegate) - } else { - None - }; - if self.state != AccountState::Initialized { - Self::hash_inputs_with_hashed_values::( - &hashed_mint, - &hashed_owner, - &amount_bytes, - &hashed_delegate_option, - ) - } else { - Self::hash_inputs_with_hashed_values::( - &hashed_mint, - &hashed_owner, - &amount_bytes, - &hashed_delegate_option, - ) - } - } - } - fn equivalency_of_hash_functions_rnd_iters() { - let mut rng = rand::thread_rng(); - - for _ in 0..ITERS { - let token_data = TokenData { - mint: Pubkey::new_unique(), - owner: Pubkey::new_unique(), - amount: rng.gen(), - delegate: Some(Pubkey::new_unique()), - state: AccountState::Initialized, - tlv: None, - }; - let hashed_token_data = token_data.hash_legacy().unwrap(); - let hashed_mint = hash_to_bn254_field_size_be(token_data.mint.to_bytes().as_slice()); - let hashed_owner = hash_to_bn254_field_size_be(token_data.owner.to_bytes().as_slice()); - let hashed_delegate = - hash_to_bn254_field_size_be(token_data.delegate.unwrap().to_bytes().as_slice()); - let mut amount_bytes = [0u8; 32]; - amount_bytes[24..].copy_from_slice(token_data.amount.to_le_bytes().as_slice()); - let hashed_token_data_with_hashed_values = TokenData::hash_with_hashed_values( - &hashed_mint, - &hashed_owner, - &amount_bytes, - &Some(&hashed_delegate), - ) - .unwrap(); - assert_eq!(hashed_token_data, hashed_token_data_with_hashed_values); - let legacy_hash = token_data.legacy_hash().unwrap(); - assert_eq!(hashed_token_data, legacy_hash); - - let token_data = TokenData { - mint: Pubkey::new_unique(), - owner: Pubkey::new_unique(), - amount: rng.gen(), - delegate: None, - state: AccountState::Initialized, - tlv: None, - }; - let hashed_token_data = token_data.hash_legacy().unwrap(); - let hashed_mint = hash_to_bn254_field_size_be(token_data.mint.to_bytes().as_slice()); - let hashed_owner = hash_to_bn254_field_size_be(token_data.owner.to_bytes().as_slice()); - let mut amount_bytes = [0u8; 32]; - amount_bytes[24..].copy_from_slice(token_data.amount.to_le_bytes().as_slice()); - let hashed_token_data_with_hashed_values: [u8; 32] = - TokenData::hash_with_hashed_values( - &hashed_mint, - &hashed_owner, - &amount_bytes, - &None, - ) - .unwrap(); - assert_eq!(hashed_token_data, hashed_token_data_with_hashed_values); - let legacy_hash = token_data.legacy_hash().unwrap(); - assert_eq!(hashed_token_data, legacy_hash); - } - } - - #[test] - fn equivalency_of_hash_functions_iters_poseidon() { - equivalency_of_hash_functions_rnd_iters::<10_000>(); - } - - #[test] - fn test_circuit_equivalence() { - // Convert hex strings to Pubkeys - let mint_pubkey = Pubkey::new_from_array([ - 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, - ]); - let owner_pubkey = Pubkey::new_from_array([ - 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, - ]); - let delegate_pubkey = Pubkey::new_from_array([ - 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, - ]); - - let token_data = TokenData { - mint: mint_pubkey, - owner: owner_pubkey, - amount: 1000000u64, - delegate: Some(delegate_pubkey), - state: AccountState::Initialized, // Using Frozen state to match our circuit test - tlv: None, - }; - - // Calculate the hash with the Rust code - let rust_hash = token_data.hash().unwrap(); - - let circuit_hash_str = - "12698830169693734517877055378728747723888091986541703429186543307137690361131"; - use std::str::FromStr; - let circuit_hash = BigUint::from_str(circuit_hash_str).unwrap().to_bytes_be(); - let rust_hash_string = BigUint::from_bytes_be(rust_hash.as_slice()).to_string(); - println!("Circuit hash string: {}", circuit_hash_str); - println!("rust_hash_string {}", rust_hash_string); - assert_eq!(rust_hash.to_vec(), circuit_hash); - } - - #[test] - fn test_frozen_equivalence() { - let token_data = TokenData { - mint: Pubkey::new_unique(), - owner: Pubkey::new_unique(), - amount: 100, - delegate: Some(Pubkey::new_unique()), - state: AccountState::Initialized, - tlv: None, - }; - let hashed_mint = hash_to_bn254_field_size_be(token_data.mint.to_bytes().as_slice()); - let hashed_owner = hash_to_bn254_field_size_be(token_data.owner.to_bytes().as_slice()); - let hashed_delegate = - hash_to_bn254_field_size_be(token_data.delegate.unwrap().to_bytes().as_slice()); - let mut amount_bytes = [0u8; 32]; - amount_bytes[24..].copy_from_slice(token_data.amount.to_le_bytes().as_slice()); - let hash = TokenData::hash_with_hashed_values( - &hashed_mint, - &hashed_owner, - &amount_bytes, - &Some(&hashed_delegate), - ) - .unwrap(); - let other_hash = token_data.hash_legacy().unwrap(); - assert_eq!(hash, other_hash); - } - - #[test] - fn failing_tests_hashing() { - let mut vec_previous_hashes = Vec::new(); - let token_data = TokenData { - mint: Pubkey::new_unique(), - owner: Pubkey::new_unique(), - amount: 100, - delegate: None, - state: AccountState::Initialized, - tlv: None, - }; - let hashed_mint = hash_to_bn254_field_size_be(token_data.mint.to_bytes().as_slice()); - let hashed_owner = hash_to_bn254_field_size_be(token_data.owner.to_bytes().as_slice()); - let mut amount_bytes = [0u8; 32]; - amount_bytes[24..].copy_from_slice(token_data.amount.to_le_bytes().as_slice()); - let hash = - TokenData::hash_with_hashed_values(&hashed_mint, &hashed_owner, &amount_bytes, &None) - .unwrap(); - vec_previous_hashes.push(hash); - // different mint - let hashed_mint_2 = hash_to_bn254_field_size_be(Pubkey::new_unique().to_bytes().as_slice()); - let mut amount_bytes = [0u8; 32]; - amount_bytes[24..].copy_from_slice(token_data.amount.to_le_bytes().as_slice()); - let hash2 = - TokenData::hash_with_hashed_values(&hashed_mint_2, &hashed_owner, &amount_bytes, &None) - .unwrap(); - assert_to_previous_hashes(hash2, &mut vec_previous_hashes); - - // different owner - let hashed_owner_2 = - hash_to_bn254_field_size_be(Pubkey::new_unique().to_bytes().as_slice()); - let mut amount_bytes = [0u8; 32]; - amount_bytes[24..].copy_from_slice(token_data.amount.to_le_bytes().as_slice()); - let hash3 = - TokenData::hash_with_hashed_values(&hashed_mint, &hashed_owner_2, &amount_bytes, &None) - .unwrap(); - assert_to_previous_hashes(hash3, &mut vec_previous_hashes); - - // different amount - let different_amount: u64 = 101; - let mut different_amount_bytes = [0u8; 32]; - different_amount_bytes[24..].copy_from_slice(different_amount.to_le_bytes().as_slice()); - let hash4 = TokenData::hash_with_hashed_values( - &hashed_mint, - &hashed_owner, - &different_amount_bytes, - &None, - ) - .unwrap(); - assert_to_previous_hashes(hash4, &mut vec_previous_hashes); - - // different delegate - let delegate = Pubkey::new_unique(); - let hashed_delegate = hash_to_bn254_field_size_be(delegate.to_bytes().as_slice()); - let mut amount_bytes = [0u8; 32]; - amount_bytes[24..].copy_from_slice(token_data.amount.to_le_bytes().as_slice()); - let hash7 = TokenData::hash_with_hashed_values( - &hashed_mint, - &hashed_owner, - &amount_bytes, - &Some(&hashed_delegate), - ) - .unwrap(); - - assert_to_previous_hashes(hash7, &mut vec_previous_hashes); - // different account state - let mut token_data = token_data; - token_data.state = AccountState::Frozen; - let hash9 = token_data.hash_legacy().unwrap(); - assert_to_previous_hashes(hash9, &mut vec_previous_hashes); - // different account state with delegate - token_data.delegate = Some(delegate); - let hash10 = token_data.hash_legacy().unwrap(); - assert_to_previous_hashes(hash10, &mut vec_previous_hashes); - } - - fn assert_to_previous_hashes(hash: [u8; 32], previous_hashes: &mut Vec<[u8; 32]>) { - for previous_hash in previous_hashes.iter() { - assert_ne!(hash, *previous_hash); - } - println!("len previous hashes: {}", previous_hashes.len()); - previous_hashes.push(hash); - } -} diff --git a/programs/package.json b/programs/package.json index 0ce4a5faef..0a977902fe 100644 --- a/programs/package.json +++ b/programs/package.json @@ -3,7 +3,7 @@ "version": "0.3.0", "license": "Apache-2.0", "scripts": { - "build": "cd system/ && cargo build-sbf && cd .. && cd account-compression/ && cargo build-sbf && cd .. && cd registry/ && cargo build-sbf && cd .. && cd compressed-token/ && cargo build-sbf && cd ..", + "build": "cd system/ && cargo build-sbf && cd .. && cd account-compression/ && cargo build-sbf && cd .. && cd registry/ && cargo build-sbf && cd .. && cd compressed-token/program && cargo build-sbf && cd ../..", "build-compressed-token-small": "cd compressed-token/ && cargo build-sbf --features cpi-without-program-ids && cd ..", "build-system": "anchor build --program-name light_system_program -- --features idl-build custom-heap", "build-compressed-token": "anchor build --program-name light_compressed_token -- --features idl-build custom-heap", diff --git a/programs/system/src/accounts/account_checks.rs b/programs/system/src/accounts/account_checks.rs index c98de1a228..e531acce52 100644 --- a/programs/system/src/accounts/account_checks.rs +++ b/programs/system/src/accounts/account_checks.rs @@ -90,6 +90,13 @@ pub fn check_anchor_option_cpi_context_account( } else { { check_owner(&crate::ID, option_cpi_context_account)?; + /* .inspect_err(|_| { + msg!(format!( + "Invalid CPI context account {:?}", + solana_pubkey::Pubkey::new_from_array(*option_cpi_context_account.key()) + ) + .as_str()) + })?;*/ check_discriminator::( option_cpi_context_account.try_borrow_data()?.as_ref(), )?; diff --git a/programs/system/src/invoke_cpi/instruction.rs b/programs/system/src/invoke_cpi/instruction.rs index 1a69148ff9..16b8ea17a8 100644 --- a/programs/system/src/invoke_cpi/instruction.rs +++ b/programs/system/src/invoke_cpi/instruction.rs @@ -46,12 +46,8 @@ impl<'info> InvokeCpiInstruction<'info> { let fee_payer = check_fee_payer(accounts.next())?; let authority = check_authority(accounts.next())?; - let registered_program_pda = check_non_mut_account_info(accounts.next())?; - - // Unchecked since unused. let _noop_program = accounts.next().ok_or(ProgramError::NotEnoughAccountKeys)?; - let account_compression_authority = check_non_mut_account_info(accounts.next())?; let account_compression_program = check_account_compression_program(accounts.next())?; diff --git a/programs/system/src/invoke_cpi/process_cpi_context.rs b/programs/system/src/invoke_cpi/process_cpi_context.rs index 8471997971..63ba0a7688 100644 --- a/programs/system/src/invoke_cpi/process_cpi_context.rs +++ b/programs/system/src/invoke_cpi/process_cpi_context.rs @@ -53,7 +53,7 @@ pub fn process_cpi_context<'a, 'info, T: InstructionData<'a>>( }; let (mut cpi_context_account, outputs_offsets) = deserialize_cpi_context_account(cpi_context_account_info)?; - + msg!(format!("cpi_context_account: {:?}", cpi_context_account).as_str()); validate_cpi_context_associated_with_merkle_tree( &instruction_data, &cpi_context_account, @@ -61,7 +61,9 @@ pub fn process_cpi_context<'a, 'info, T: InstructionData<'a>>( )?; if cpi_context.set_context || cpi_context.first_set_context { + msg!("set_cpi_context"); set_cpi_context(fee_payer, cpi_context_account_info, instruction_data)?; + msg!("post set_cpi_context"); return Ok(None); } else { if cpi_context_account.context.is_empty() { diff --git a/programs/system/src/invoke_cpi/processor.rs b/programs/system/src/invoke_cpi/processor.rs index 3da61e61d1..76d4a63006 100644 --- a/programs/system/src/invoke_cpi/processor.rs +++ b/programs/system/src/invoke_cpi/processor.rs @@ -46,7 +46,6 @@ pub fn process_invoke_cpi< Ok(None) => return Ok(()), Err(err) => return Err(err), }; - // 3. Process input data and cpi the account compression program. process::( instruction_data, diff --git a/scripts/devenv.sh b/scripts/devenv.sh index 0349400d9c..35f10067c3 100755 --- a/scripts/devenv.sh +++ b/scripts/devenv.sh @@ -87,6 +87,7 @@ export CARGO_HOME export NPM_CONFIG_PREFIX export LIGHT_PROTOCOL_TOPLEVEL export LIGHT_PROTOCOL_DEVENV +export SBF_OUT_DIR=./target/deploy # Set Redis URL if not already set export REDIS_URL="${REDIS_URL:-redis://localhost:6379}" diff --git a/sdk-libs/client/src/indexer/indexer_trait.rs b/sdk-libs/client/src/indexer/indexer_trait.rs index 577372b6ed..d2686d640d 100644 --- a/sdk-libs/client/src/indexer/indexer_trait.rs +++ b/sdk-libs/client/src/indexer/indexer_trait.rs @@ -5,8 +5,8 @@ use solana_pubkey::Pubkey; use super::{ response::{Items, ItemsWithCursor, Response}, types::{ - CompressedAccount, OwnerBalance, SignatureWithMetadata, TokenAccount, TokenBalance, - ValidityProofWithContext, + CompressedAccount, CompressedTokenAccount, OwnerBalance, SignatureWithMetadata, + TokenBalance, ValidityProofWithContext, }, Address, AddressWithTree, BatchAddressUpdateIndexerResponse, GetCompressedAccountsByOwnerConfig, GetCompressedTokenAccountsByOwnerOrDelegateOptions, Hash, @@ -75,14 +75,14 @@ pub trait Indexer: std::marker::Send + std::marker::Sync { delegate: &Pubkey, options: Option, config: Option, - ) -> Result>, IndexerError>; + ) -> Result>, IndexerError>; async fn get_compressed_token_accounts_by_owner( &self, owner: &Pubkey, options: Option, config: Option, - ) -> Result>, IndexerError>; + ) -> Result>, IndexerError>; /// Returns the token balances for a given owner. async fn get_compressed_token_balances_by_owner_v2( diff --git a/sdk-libs/client/src/indexer/mod.rs b/sdk-libs/client/src/indexer/mod.rs index c66baf2d0b..745b512beb 100644 --- a/sdk-libs/client/src/indexer/mod.rs +++ b/sdk-libs/client/src/indexer/mod.rs @@ -15,10 +15,10 @@ pub use indexer_trait::Indexer; pub use response::{Context, Items, ItemsWithCursor, Response}; pub use types::{ AccountProofInputs, Address, AddressMerkleTreeAccounts, AddressProofInputs, AddressQueueIndex, - AddressWithTree, BatchAddressUpdateIndexerResponse, CompressedAccount, Hash, MerkleProof, - MerkleProofWithContext, NewAddressProofWithContext, NextTreeInfo, OwnerBalance, ProofOfLeaf, - RootIndex, SignatureWithMetadata, StateMerkleTreeAccounts, TokenAccount, TokenBalance, - TreeInfo, ValidityProofWithContext, + AddressWithTree, BatchAddressUpdateIndexerResponse, CompressedAccount, CompressedTokenAccount, + Hash, MerkleProof, MerkleProofWithContext, NewAddressProofWithContext, NextTreeInfo, + OwnerBalance, ProofOfLeaf, RootIndex, SignatureWithMetadata, StateMerkleTreeAccounts, + TokenBalance, TreeInfo, ValidityProofWithContext, }; mod options; pub use options::*; diff --git a/sdk-libs/client/src/indexer/photon_indexer.rs b/sdk-libs/client/src/indexer/photon_indexer.rs index 3409a5df7d..644e8a0018 100644 --- a/sdk-libs/client/src/indexer/photon_indexer.rs +++ b/sdk-libs/client/src/indexer/photon_indexer.rs @@ -11,7 +11,10 @@ use solana_pubkey::Pubkey; use tracing::{debug, error, warn}; use super::{ - types::{CompressedAccount, OwnerBalance, SignatureWithMetadata, TokenAccount, TokenBalance}, + types::{ + CompressedAccount, CompressedTokenAccount, OwnerBalance, SignatureWithMetadata, + TokenBalance, + }, BatchAddressUpdateIndexerResponse, MerkleProofWithContext, }; use crate::indexer::{ @@ -544,7 +547,7 @@ impl Indexer for PhotonIndexer { delegate: &Pubkey, options: Option, config: Option, - ) -> Result>, IndexerError> { + ) -> Result>, IndexerError> { let config = config.unwrap_or_default(); self.retry(config.retry_config, || async { #[cfg(feature = "v2")] @@ -576,7 +579,7 @@ impl Indexer for PhotonIndexer { .value .items .iter() - .map(TokenAccount::try_from) + .map(CompressedTokenAccount::try_from) .collect(); let cursor = response.value.cursor; @@ -620,7 +623,7 @@ impl Indexer for PhotonIndexer { .value .items .iter() - .map(TokenAccount::try_from) + .map(CompressedTokenAccount::try_from) .collect(); let cursor = response.value.cursor; @@ -644,7 +647,7 @@ impl Indexer for PhotonIndexer { owner: &Pubkey, options: Option, config: Option, - ) -> Result>, IndexerError> { + ) -> Result>, IndexerError> { let config = config.unwrap_or_default(); self.retry(config.retry_config, || async { #[cfg(feature = "v2")] @@ -677,7 +680,7 @@ impl Indexer for PhotonIndexer { .value .items .iter() - .map(TokenAccount::try_from) + .map(CompressedTokenAccount::try_from) .collect(); let cursor = response.value.cursor; @@ -728,7 +731,7 @@ impl Indexer for PhotonIndexer { .value .items .iter() - .map(TokenAccount::try_from) + .map(CompressedTokenAccount::try_from) .collect(); let cursor = response.value.cursor; diff --git a/sdk-libs/client/src/indexer/types.rs b/sdk-libs/client/src/indexer/types.rs index 51d804e40b..98c1a4d125 100644 --- a/sdk-libs/client/src/indexer/types.rs +++ b/sdk-libs/client/src/indexer/types.rs @@ -458,6 +458,14 @@ impl TreeInfo { } } + pub fn get_output_pubkey(&self) -> Result { + match self.tree_type { + TreeType::StateV1 => Ok(self.tree), + TreeType::StateV2 => Ok(self.queue), + _ => Err(IndexerError::InvalidPackTreeType), + } + } + pub fn from_api_model( value: &photon_api::models::MerkleContextV2, ) -> Result { @@ -707,14 +715,14 @@ pub struct AddressMerkleTreeAccounts { } #[derive(Clone, Default, Debug, PartialEq)] -pub struct TokenAccount { +pub struct CompressedTokenAccount { /// Token-specific data (mint, owner, amount, delegate, state, tlv) pub token: TokenData, /// General account information (address, hash, lamports, merkle context, etc.) pub account: CompressedAccount, } -impl TryFrom<&photon_api::models::TokenAccount> for TokenAccount { +impl TryFrom<&photon_api::models::TokenAccount> for CompressedTokenAccount { type Error = IndexerError; fn try_from(token_account: &photon_api::models::TokenAccount) -> Result { @@ -747,11 +755,11 @@ impl TryFrom<&photon_api::models::TokenAccount> for TokenAccount { .map_err(|_| IndexerError::InvalidResponseData)?, }; - Ok(TokenAccount { token, account }) + Ok(CompressedTokenAccount { token, account }) } } -impl TryFrom<&photon_api::models::TokenAccountV2> for TokenAccount { +impl TryFrom<&photon_api::models::TokenAccountV2> for CompressedTokenAccount { type Error = IndexerError; fn try_from(token_account: &photon_api::models::TokenAccountV2) -> Result { @@ -784,12 +792,12 @@ impl TryFrom<&photon_api::models::TokenAccountV2> for TokenAccount { .map_err(|_| IndexerError::InvalidResponseData)?, }; - Ok(TokenAccount { token, account }) + Ok(CompressedTokenAccount { token, account }) } } #[allow(clippy::from_over_into)] -impl Into for TokenAccount { +impl Into for CompressedTokenAccount { fn into(self) -> light_sdk::token::TokenDataWithMerkleContext { let compressed_account = CompressedAccountWithMerkleContext::from(self.account); @@ -802,7 +810,7 @@ impl Into for TokenAccount { #[allow(clippy::from_over_into)] impl Into> - for super::response::Response> + for super::response::Response> { fn into(self) -> Vec { self.value @@ -820,7 +828,7 @@ impl Into> } } -impl TryFrom for TokenAccount { +impl TryFrom for CompressedTokenAccount { type Error = IndexerError; fn try_from( @@ -828,7 +836,7 @@ impl TryFrom for TokenAccount { ) -> Result { let account = CompressedAccount::try_from(token_data_with_context.compressed_account)?; - Ok(TokenAccount { + Ok(CompressedTokenAccount { token: token_data_with_context.token_data, account, }) diff --git a/sdk-libs/client/src/rpc/client.rs b/sdk-libs/client/src/rpc/client.rs index ef2cded5fa..25d2de1692 100644 --- a/sdk-libs/client/src/rpc/client.rs +++ b/sdk-libs/client/src/rpc/client.rs @@ -759,6 +759,16 @@ impl Rpc for LightClient { tree_type: TreeType::AddressV1, } } + + fn get_address_tree_v2(&self) -> TreeInfo { + TreeInfo { + tree: pubkey!("EzKE84aVTkCUhDHLELqyJaq1Y7UVVmqxXqZjVHwHY3rK"), + queue: pubkey!("EzKE84aVTkCUhDHLELqyJaq1Y7UVVmqxXqZjVHwHY3rK"), + cpi_context: None, + next_tree_info: None, + tree_type: TreeType::AddressV2, + } + } } impl MerkleTreeExt for LightClient {} diff --git a/sdk-libs/client/src/rpc/indexer.rs b/sdk-libs/client/src/rpc/indexer.rs index 56963ed64c..1a9c764e68 100644 --- a/sdk-libs/client/src/rpc/indexer.rs +++ b/sdk-libs/client/src/rpc/indexer.rs @@ -5,10 +5,11 @@ use solana_pubkey::Pubkey; use super::LightClient; use crate::indexer::{ Address, AddressWithTree, BatchAddressUpdateIndexerResponse, CompressedAccount, - GetCompressedAccountsByOwnerConfig, GetCompressedTokenAccountsByOwnerOrDelegateOptions, Hash, - Indexer, IndexerError, IndexerRpcConfig, Items, ItemsWithCursor, MerkleProof, - MerkleProofWithContext, NewAddressProofWithContext, OwnerBalance, PaginatedOptions, Response, - RetryConfig, SignatureWithMetadata, TokenAccount, TokenBalance, ValidityProofWithContext, + CompressedTokenAccount, GetCompressedAccountsByOwnerConfig, + GetCompressedTokenAccountsByOwnerOrDelegateOptions, Hash, Indexer, IndexerError, + IndexerRpcConfig, Items, ItemsWithCursor, MerkleProof, MerkleProofWithContext, + NewAddressProofWithContext, OwnerBalance, PaginatedOptions, Response, RetryConfig, + SignatureWithMetadata, TokenBalance, ValidityProofWithContext, }; #[async_trait] @@ -94,7 +95,7 @@ impl Indexer for LightClient { owner: &Pubkey, options: Option, config: Option, - ) -> Result>, IndexerError> { + ) -> Result>, IndexerError> { Ok(self .indexer .as_ref() @@ -268,7 +269,7 @@ impl Indexer for LightClient { delegate: &Pubkey, options: Option, config: Option, - ) -> Result>, IndexerError> { + ) -> Result>, IndexerError> { Ok(self .indexer .as_ref() diff --git a/sdk-libs/client/src/rpc/rpc_trait.rs b/sdk-libs/client/src/rpc/rpc_trait.rs index 0cb349e368..b99f4e9b8c 100644 --- a/sdk-libs/client/src/rpc/rpc_trait.rs +++ b/sdk-libs/client/src/rpc/rpc_trait.rs @@ -204,6 +204,5 @@ pub trait Rpc: Send + Sync + Debug + 'static { fn get_address_tree_v1(&self) -> TreeInfo; - // TODO: add with v2 release - // fn get_address_tree_v2(&self) -> Result, RpcError>; + fn get_address_tree_v2(&self) -> TreeInfo; } diff --git a/sdk-libs/compressed-token-sdk/Cargo.toml b/sdk-libs/compressed-token-sdk/Cargo.toml new file mode 100644 index 0000000000..919e8d8ae2 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/Cargo.toml @@ -0,0 +1,36 @@ +[package] +name = "light-compressed-token-sdk" +version = { workspace = true } +edition = { workspace = true } + +[features] + +anchor = ["anchor-lang", "light-compressed-token-types/anchor"] + +[dependencies] +# Light Protocol dependencies +light-compressed-token-types = { workspace = true } +light-compressed-account = { workspace = true } +light-ctoken-types = { workspace = true } +light-sdk = { workspace = true } +light-macros = { workspace = true } +thiserror = { workspace = true } +# Serialization +borsh = { workspace = true } +solana-msg = { workspace = true } +# Solana dependencies +solana-pubkey = { workspace = true, features = ["sha2", "curve25519"] } +solana-instruction = { workspace = true } +solana-account-info = { workspace = true } +solana-cpi = { workspace = true } +solana-program-error = { workspace = true } +arrayvec = { workspace = true } +spl-token-2022 = { workspace = true } +spl-pod = { workspace = true } +# Optional Anchor dependency +anchor-lang = { workspace = true, optional = true } + +[dev-dependencies] +light-account-checks = { workspace = true, features = ["test-only", "solana"] } +anchor-lang = { workspace = true } +light-compressed-token = { workspace = true } diff --git a/sdk-libs/compressed-token-sdk/src/account.rs b/sdk-libs/compressed-token-sdk/src/account.rs new file mode 100644 index 0000000000..11c871d150 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/account.rs @@ -0,0 +1,209 @@ +use std::ops::Deref; + +use light_compressed_token_types::{PackedTokenTransferOutputData, TokenAccountMeta}; +use solana_pubkey::Pubkey; + +use crate::error::TokenSdkError; + +#[derive(Debug, PartialEq, Clone)] +pub struct CTokenAccount { + inputs: Vec, + output: PackedTokenTransferOutputData, + compression_amount: Option, + is_compress: bool, + is_decompress: bool, + mint: Pubkey, + pub(crate) method_used: bool, +} + +impl CTokenAccount { + pub fn new( + mint: Pubkey, + owner: Pubkey, + token_data: Vec, + output_merkle_tree_index: u8, + ) -> Self { + let amount = token_data.iter().map(|data| data.amount).sum(); + let lamports = token_data.iter().map(|data| data.lamports).sum(); + let output = PackedTokenTransferOutputData { + owner: owner.to_bytes(), + amount, + lamports, + tlv: None, + merkle_tree_index: output_merkle_tree_index, + }; + Self { + inputs: token_data, + output, + compression_amount: None, + is_compress: false, + is_decompress: false, + mint, + method_used: false, + } + } + + pub fn new_empty(mint: Pubkey, owner: Pubkey, output_merkle_tree_index: u8) -> Self { + Self { + inputs: vec![], + output: PackedTokenTransferOutputData { + owner: owner.to_bytes(), + amount: 0, + lamports: None, + tlv: None, + merkle_tree_index: output_merkle_tree_index, + }, + compression_amount: None, + is_compress: false, + is_decompress: false, + mint, + method_used: false, + } + } + + // TODO: consider this might be confusing because it must not be used in combination with fn transfer() + // could mark the struct as transferred and throw in fn transfer + pub fn transfer( + &mut self, + recipient: &Pubkey, + amount: u64, + output_merkle_tree_index: Option, + ) -> Result { + if amount > self.output.amount { + return Err(TokenSdkError::InsufficientBalance); + } + // TODO: skip outputs with zero amount when creating the instruction data. + self.output.amount -= amount; + let merkle_tree_index = output_merkle_tree_index.unwrap_or(self.output.merkle_tree_index); + + self.method_used = true; + Ok(Self { + compression_amount: None, + is_compress: false, + is_decompress: false, + inputs: vec![], + output: PackedTokenTransferOutputData { + owner: recipient.to_bytes(), + amount, + lamports: None, + tlv: None, + merkle_tree_index, + }, + mint: self.mint, + method_used: true, + }) + } + + /// Approves a delegate for a specified amount of tokens. + /// Similar to transfer, this deducts the amount from the current account + /// and returns a new CTokenAccount that represents the delegated portion. + /// The original account balance is reduced by the delegated amount. + pub fn approve( + &mut self, + _delegate: &Pubkey, + amount: u64, + output_merkle_tree_index: Option, + ) -> Result { + if amount > self.output.amount { + return Err(TokenSdkError::InsufficientBalance); + } + + // Deduct the delegated amount from current account + self.output.amount -= amount; + let merkle_tree_index = output_merkle_tree_index.unwrap_or(self.output.merkle_tree_index); + + self.method_used = true; + + // Create a new delegated account with the specified delegate + // Note: In the actual instruction, this will create the proper delegation structure + Ok(Self { + compression_amount: None, + is_compress: false, + is_decompress: false, + inputs: vec![], + output: PackedTokenTransferOutputData { + owner: self.output.owner, // Owner remains the same, but delegate is set + amount, + lamports: None, + tlv: None, + merkle_tree_index, + }, + mint: self.mint, + method_used: true, + }) + } + + // TODO: consider this might be confusing because it must not be used in combination with fn compress() + pub fn compress(&mut self, amount: u64) -> Result<(), TokenSdkError> { + self.output.amount += amount; + self.is_compress = true; + if self.is_decompress { + return Err(TokenSdkError::CannotCompressAndDecompress); + } + + match self.compression_amount.as_mut() { + Some(amount_ref) => *amount_ref += amount, + None => self.compression_amount = Some(amount), + } + self.method_used = true; + + Ok(()) + } + + // TODO: consider this might be confusing because it must not be used in combination with fn decompress() + pub fn decompress(&mut self, amount: u64) -> Result<(), TokenSdkError> { + if self.is_compress { + return Err(TokenSdkError::CannotCompressAndDecompress); + } + if self.output.amount < amount { + return Err(TokenSdkError::InsufficientBalance); + } + self.output.amount -= amount; + + self.is_decompress = true; + + match self.compression_amount.as_mut() { + Some(amount_ref) => *amount_ref += amount, + None => self.compression_amount = Some(amount), + } + self.method_used = true; + + Ok(()) + } + + pub fn is_compress(&self) -> bool { + self.is_compress + } + + pub fn is_decompress(&self) -> bool { + self.is_decompress + } + + pub fn mint(&self) -> &Pubkey { + &self.mint + } + + pub fn compression_amount(&self) -> Option { + self.compression_amount + } + + pub fn owner(&self) -> Pubkey { + Pubkey::new_from_array(self.owner) + } + pub fn input_metas(&self) -> &[TokenAccountMeta] { + self.inputs.as_slice() + } + + /// Consumes token account for instruction creation. + pub fn into_inputs_and_outputs(self) -> (Vec, PackedTokenTransferOutputData) { + (self.inputs, self.output) + } +} + +impl Deref for CTokenAccount { + type Target = PackedTokenTransferOutputData; + + fn deref(&self) -> &Self::Target { + &self.output + } +} diff --git a/sdk-libs/compressed-token-sdk/src/account2.rs b/sdk-libs/compressed-token-sdk/src/account2.rs new file mode 100644 index 0000000000..af9920f53c --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/account2.rs @@ -0,0 +1,271 @@ +use std::ops::Deref; + +use light_ctoken_types::instructions::transfer2::{ + Compression, CompressionMode, MultiInputTokenDataWithContext, MultiTokenTransferOutputData, +}; +use solana_account_info::AccountInfo; +use solana_pubkey::Pubkey; + +use crate::{error::TokenSdkError, utils::get_token_account_balance}; + +#[derive(Debug, PartialEq, Clone)] +pub struct CTokenAccount2 { + inputs: Vec, + output: MultiTokenTransferOutputData, + compression: Option, + delegate_is_set: bool, + pub(crate) method_used: bool, +} + +impl CTokenAccount2 { + pub fn new( + token_data: Vec, + output_merkle_tree_index: u8, + ) -> Result { + // all mint indices must be the same + // all owners must be the same + let amount = token_data.iter().map(|data| data.amount).sum(); + // Check if token_data is empty + if token_data.is_empty() { + return Err(TokenSdkError::InsufficientBalance); // TODO: Add proper error variant + } + + // Use the indices from the first token data (assuming they're all the same mint/owner) + let mint_index = token_data[0].mint; + let owner_index = token_data[0].owner; + let output = MultiTokenTransferOutputData { + owner: owner_index, + amount, + merkle_tree: output_merkle_tree_index, + delegate: 0, // Default delegate index + mint: mint_index, + version: 2, // V2 for batched Merkle trees + }; + Ok(Self { + inputs: token_data, + output, + delegate_is_set: false, + compression: None, + method_used: false, + }) + } + + pub fn new_empty(owner_index: u8, mint_index: u8, output_merkle_tree_index: u8) -> Self { + Self { + inputs: vec![], + output: MultiTokenTransferOutputData { + owner: owner_index, + amount: 0, + merkle_tree: output_merkle_tree_index, + delegate: 0, // Default delegate index + mint: mint_index, + version: 2, // V2 for batched Merkle trees + }, + compression: None, + delegate_is_set: false, + method_used: false, + } + } + + // TODO: consider this might be confusing because it must not be used in combination with fn transfer() + // could mark the struct as transferred and throw in fn transfer + pub fn transfer( + &mut self, + recipient_index: u8, + amount: u64, + output_merkle_tree_index: Option, + ) -> Result { + if amount > self.output.amount { + return Err(TokenSdkError::InsufficientBalance); + } + // TODO: skip outputs with zero amount when creating the instruction data. + self.output.amount -= amount; + let merkle_tree_index = output_merkle_tree_index.unwrap_or(self.output.merkle_tree); + + self.method_used = true; + Ok(Self { + compression: None, + inputs: vec![], + output: MultiTokenTransferOutputData { + owner: recipient_index, + amount, + merkle_tree: merkle_tree_index, + delegate: 0, + mint: self.output.mint, + version: self.output.version, + }, + delegate_is_set: false, + method_used: false, + }) + } + + /// Approves a delegate for a specified amount of tokens. + /// Similar to transfer, this deducts the amount from the current account + /// and returns a new CTokenAccount that represents the delegated portion. + /// The original account balance is reduced by the delegated amount. + pub fn approve( + &mut self, + delegate_index: u8, + amount: u64, + output_merkle_tree_index: Option, + ) -> Result { + if amount > self.output.amount { + return Err(TokenSdkError::InsufficientBalance); + } + + // Deduct the delegated amount from current account + self.output.amount -= amount; + let merkle_tree_index = output_merkle_tree_index.unwrap_or(self.output.merkle_tree); + + self.method_used = true; + + // Create a new delegated account with the specified delegate + // Note: In the actual instruction, this will create the proper delegation structure + Ok(Self { + compression: None, + inputs: vec![], + output: MultiTokenTransferOutputData { + owner: self.output.owner, // Owner remains the same + amount, + merkle_tree: merkle_tree_index, + delegate: delegate_index, + mint: self.output.mint, + version: self.output.version, + }, + delegate_is_set: true, + method_used: false, + }) + } + + // TODO: consider this might be confusing because it must not be used in combination with fn compress() + pub fn compress( + &mut self, + amount: u64, + source_or_recipient_index: u8, + authority: u8, + ) -> Result<(), TokenSdkError> { + // Check if there's already a compression set + if self.compression.is_some() { + return Err(TokenSdkError::CompressionCannotBeSetTwice); + } + + self.output.amount += amount; + self.compression = Some(Compression::compress( + amount, + self.output.mint, + source_or_recipient_index, + authority, + )); + self.method_used = true; + + Ok(()) + } + + // TODO: consider this might be confusing because it must not be used in combination with fn decompress() + pub fn decompress(&mut self, amount: u64, source_index: u8) -> Result<(), TokenSdkError> { + // Check if there's already a compression set + if self.compression.is_some() { + return Err(TokenSdkError::CompressionCannotBeSetTwice); + } + + if self.output.amount < amount { + return Err(TokenSdkError::InsufficientBalance); + } + self.output.amount -= amount; + + self.compression = Some(Compression::decompress( + amount, + self.output.mint, + source_index, + )); + self.method_used = true; + + Ok(()) + } + + pub fn compress_full( + &mut self, + source_or_recipient_index: u8, + authority: u8, + token_account_info: &AccountInfo, + ) -> Result<(), TokenSdkError> { + // Check if there's already a compression set + if self.compression.is_some() { + return Err(TokenSdkError::CompressionCannotBeSetTwice); + } + + // Get the actual token account balance to add to output + let token_balance = get_token_account_balance(token_account_info)?; + + // Add the full token balance to the output amount + self.output.amount += token_balance; + + // For compress_full, set amount to the actual balance for instruction data + self.compression = Some(Compression { + amount: token_balance, + mode: CompressionMode::Compress, // Use regular compress mode with actual amount + mint: self.output.mint, + source_or_recipient: source_or_recipient_index, + authority, + }); + self.method_used = true; + + Ok(()) + } + + pub fn is_compress(&self) -> bool { + self.compression + .as_ref() + .map(|c| c.mode == CompressionMode::Compress) + .unwrap_or(false) + } + + pub fn is_decompress(&self) -> bool { + self.compression + .as_ref() + .map(|c| c.mode == CompressionMode::Decompress) + .unwrap_or(false) + } + + pub fn mint(&self, account_infos: &[AccountInfo]) -> Pubkey { + *account_infos[self.mint as usize].key + } + + pub fn compression_amount(&self) -> Option { + self.compression.as_ref().map(|c| c.amount) + } + + pub fn compression(&self) -> Option<&Compression> { + self.compression.as_ref() + } + + pub fn owner(&self, account_infos: &[AccountInfo]) -> Pubkey { + *account_infos[self.owner as usize].key + } + // TODO: make option and take from self + //pub fn delegate_account<'b>(&self, account_infos: &'b [&'b AccountInfo]) -> &'b Pubkey { + // account_infos[self.output.delegate as usize].key + // } + + pub fn input_metas(&self) -> &[MultiInputTokenDataWithContext] { + self.inputs.as_slice() + } + + /// Consumes token account for instruction creation. + pub fn into_inputs_and_outputs( + self, + ) -> ( + Vec, + MultiTokenTransferOutputData, + ) { + (self.inputs, self.output) + } +} + +impl Deref for CTokenAccount2 { + type Target = MultiTokenTransferOutputData; + + fn deref(&self) -> &Self::Target { + &self.output + } +} diff --git a/sdk-libs/compressed-token-sdk/src/error.rs b/sdk-libs/compressed-token-sdk/src/error.rs new file mode 100644 index 0000000000..8086b9d995 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/error.rs @@ -0,0 +1,68 @@ +use light_compressed_token_types::error::LightTokenSdkTypeError; +use light_ctoken_types::CTokenError; +use solana_program_error::ProgramError; +use thiserror::Error; + +pub type Result = std::result::Result; + +#[derive(Debug, Error)] +pub enum TokenSdkError { + #[error("Insufficient balance")] + InsufficientBalance, + #[error("Serialization error")] + SerializationError, + #[error("CPI error: {0}")] + CpiError(String), + #[error("Cannot compress and decompress")] + CannotCompressAndDecompress, + #[error("Compression cannot be set twice")] + CompressionCannotBeSetTwice, + #[error("Inconsistent compress/decompress state")] + InconsistentCompressDecompressState, + #[error("Both compress and decompress specified")] + BothCompressAndDecompress, + #[error("Invalid compress/decompress amount")] + InvalidCompressDecompressAmount, + #[error("Ctoken::transfer, compress, or decompress cannot be used with fn transfer(), fn compress(), fn decompress()")] + MethodUsed, + #[error("DecompressedMintConfig is required for decompressed mints")] + DecompressedMintConfigRequired, + #[error("Invalid compress input owner")] + InvalidCompressInputOwner, + #[error("Account borrow failed")] + AccountBorrowFailed, + #[error("Invalid account data")] + InvalidAccountData, + #[error(transparent)] + CompressedTokenTypes(#[from] LightTokenSdkTypeError), + #[error(transparent)] + CTokenError(#[from] CTokenError), +} + +impl From for ProgramError { + fn from(e: TokenSdkError) -> Self { + ProgramError::Custom(e.into()) + } +} + +impl From for u32 { + fn from(e: TokenSdkError) -> Self { + match e { + TokenSdkError::InsufficientBalance => 17001, + TokenSdkError::SerializationError => 17002, + TokenSdkError::CpiError(_) => 17003, + TokenSdkError::CannotCompressAndDecompress => 17004, + TokenSdkError::CompressionCannotBeSetTwice => 17005, + TokenSdkError::InconsistentCompressDecompressState => 17006, + TokenSdkError::BothCompressAndDecompress => 17007, + TokenSdkError::InvalidCompressDecompressAmount => 17008, + TokenSdkError::MethodUsed => 17009, + TokenSdkError::DecompressedMintConfigRequired => 17010, + TokenSdkError::InvalidCompressInputOwner => 17011, + TokenSdkError::AccountBorrowFailed => 17012, + TokenSdkError::InvalidAccountData => 17013, + TokenSdkError::CompressedTokenTypes(e) => e.into(), + TokenSdkError::CTokenError(e) => e.into(), + } + } +} diff --git a/sdk-libs/compressed-token-sdk/src/instructions/approve/account_metas.rs b/sdk-libs/compressed-token-sdk/src/instructions/approve/account_metas.rs new file mode 100644 index 0000000000..82ff51ffcc --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/approve/account_metas.rs @@ -0,0 +1,136 @@ +use solana_instruction::AccountMeta; +use solana_pubkey::Pubkey; + +use crate::instructions::CTokenDefaultAccounts; + +/// Account metadata configuration for approve instruction +#[derive(Debug, Copy, Clone)] +pub struct ApproveMetaConfig { + pub fee_payer: Option, + pub authority: Option, + pub delegated_compressed_account_merkle_tree: Pubkey, + pub change_compressed_account_merkle_tree: Pubkey, +} + +impl ApproveMetaConfig { + /// Create a new ApproveMetaConfig for direct invocation + pub fn new( + fee_payer: Pubkey, + authority: Pubkey, + delegated_compressed_account_merkle_tree: Pubkey, + change_compressed_account_merkle_tree: Pubkey, + ) -> Self { + Self { + fee_payer: Some(fee_payer), + authority: Some(authority), + delegated_compressed_account_merkle_tree, + change_compressed_account_merkle_tree, + } + } + + /// Create a new ApproveMetaConfig for client-side (CPI) usage + pub fn new_client( + delegated_compressed_account_merkle_tree: Pubkey, + change_compressed_account_merkle_tree: Pubkey, + ) -> Self { + Self { + fee_payer: None, + authority: None, + delegated_compressed_account_merkle_tree, + change_compressed_account_merkle_tree, + } + } +} + +/// Get the standard account metas for an approve instruction +/// Uses the GenericInstruction account structure for delegation operations +pub fn get_approve_instruction_account_metas(config: ApproveMetaConfig) -> Vec { + let default_pubkeys = CTokenDefaultAccounts::default(); + + // Calculate capacity based on whether fee_payer is provided + // Base accounts: cpi_authority_pda + light_system_program + registered_program_pda + + // noop_program + account_compression_authority + account_compression_program + + // self_program + system_program + delegated_merkle_tree + change_merkle_tree + let base_capacity = 10; + + // Direct invoke accounts: fee_payer + authority + let fee_payer_capacity = if config.fee_payer.is_some() { 2 } else { 0 }; + + let total_capacity = base_capacity + fee_payer_capacity; + + // Start building the account metas to match GenericInstruction structure + let mut metas = Vec::with_capacity(total_capacity); + + // Add fee_payer and authority if provided (for direct invoke) + if let Some(fee_payer) = config.fee_payer { + let authority = config.authority.expect("Missing authority"); + metas.extend_from_slice(&[ + // fee_payer (mut, signer) + AccountMeta::new(fee_payer, true), + // authority (signer) + AccountMeta::new_readonly(authority, true), + ]); + } + + // cpi_authority_pda + metas.push(AccountMeta::new_readonly( + default_pubkeys.cpi_authority_pda, + false, + )); + + // light_system_program + metas.push(AccountMeta::new_readonly( + default_pubkeys.light_system_program, + false, + )); + + // registered_program_pda + metas.push(AccountMeta::new_readonly( + default_pubkeys.registered_program_pda, + false, + )); + + // noop_program + metas.push(AccountMeta::new_readonly( + default_pubkeys.noop_program, + false, + )); + + // account_compression_authority + metas.push(AccountMeta::new_readonly( + default_pubkeys.account_compression_authority, + false, + )); + + // account_compression_program + metas.push(AccountMeta::new_readonly( + default_pubkeys.account_compression_program, + false, + )); + + // self_program (compressed token program) + metas.push(AccountMeta::new_readonly( + default_pubkeys.self_program, + false, + )); + + // system_program + metas.push(AccountMeta::new_readonly( + default_pubkeys.system_program, + false, + )); + + // delegated_compressed_account_merkle_tree (mut) - for the delegated output account + metas.push(AccountMeta::new( + config.delegated_compressed_account_merkle_tree, + false, + )); + + // change_compressed_account_merkle_tree (mut) - for the change output account + metas.push(AccountMeta::new( + config.change_compressed_account_merkle_tree, + false, + )); + + metas +} diff --git a/sdk-libs/compressed-token-sdk/src/instructions/approve/instruction.rs b/sdk-libs/compressed-token-sdk/src/instructions/approve/instruction.rs new file mode 100644 index 0000000000..ab2542adb1 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/approve/instruction.rs @@ -0,0 +1,91 @@ +use borsh::BorshSerialize; +use light_compressed_token_types::{ + instruction::delegation::CompressedTokenInstructionDataApprove, ValidityProof, +}; +use light_ctoken_types::COMPRESSED_TOKEN_PROGRAM_ID; +use solana_instruction::Instruction; +use solana_pubkey::Pubkey; + +use crate::{ + account::CTokenAccount, + error::{Result, TokenSdkError}, + instructions::approve::account_metas::{ + get_approve_instruction_account_metas, ApproveMetaConfig, + }, +}; + +#[derive(Debug, Clone)] +pub struct ApproveInputs { + pub fee_payer: Pubkey, + pub authority: Pubkey, + pub sender_account: CTokenAccount, + pub validity_proof: ValidityProof, + pub delegate: Pubkey, + pub delegated_amount: u64, + pub delegate_lamports: Option, + pub delegated_compressed_account_merkle_tree: Pubkey, + pub change_compressed_account_merkle_tree: Pubkey, +} + +/// Create a compressed token approve instruction +/// This creates two output accounts: +/// 1. A delegated account with the specified amount and delegate +/// 2. A change account with the remaining balance (if any) +pub fn create_approve_instruction(inputs: ApproveInputs) -> Result { + // Store mint before consuming sender_account + let mint = *inputs.sender_account.mint(); + let (input_token_data, _) = inputs.sender_account.into_inputs_and_outputs(); + + if input_token_data.is_empty() { + return Err(TokenSdkError::InsufficientBalance); + } + + // Calculate total input amount + let total_input_amount: u64 = input_token_data.iter().map(|data| data.amount).sum(); + if total_input_amount < inputs.delegated_amount { + return Err(TokenSdkError::InsufficientBalance); + } + + // Use the input token data directly since it's already in the correct format + let input_token_data_with_context = input_token_data; + + // Create instruction data + let instruction_data = CompressedTokenInstructionDataApprove { + proof: inputs.validity_proof.0.unwrap(), + mint: mint.to_bytes(), + input_token_data_with_context, + cpi_context: None, + delegate: inputs.delegate.to_bytes(), + delegated_amount: inputs.delegated_amount, + delegate_merkle_tree_index: 0, // Will be set based on remaining accounts + change_account_merkle_tree_index: 1, // Will be set based on remaining accounts + delegate_lamports: inputs.delegate_lamports, + }; + + // Serialize instruction data + let serialized_data = instruction_data + .try_to_vec() + .map_err(|_| TokenSdkError::SerializationError)?; + + // Create account meta config + let meta_config = ApproveMetaConfig::new( + inputs.fee_payer, + inputs.authority, + inputs.delegated_compressed_account_merkle_tree, + inputs.change_compressed_account_merkle_tree, + ); + + // Get account metas using the dedicated function + let account_metas = get_approve_instruction_account_metas(meta_config); + + Ok(Instruction { + program_id: Pubkey::new_from_array(COMPRESSED_TOKEN_PROGRAM_ID), + accounts: account_metas, + data: serialized_data, + }) +} + +/// Simplified approve function similar to transfer +pub fn approve(inputs: ApproveInputs) -> Result { + create_approve_instruction(inputs) +} diff --git a/sdk-libs/compressed-token-sdk/src/instructions/approve/mod.rs b/sdk-libs/compressed-token-sdk/src/instructions/approve/mod.rs new file mode 100644 index 0000000000..6b8ac4a1af --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/approve/mod.rs @@ -0,0 +1,5 @@ +pub mod account_metas; +pub mod instruction; + +pub use account_metas::*; +pub use instruction::*; diff --git a/sdk-libs/compressed-token-sdk/src/instructions/batch_compress/account_metas.rs b/sdk-libs/compressed-token-sdk/src/instructions/batch_compress/account_metas.rs new file mode 100644 index 0000000000..f0812f5f4b --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/batch_compress/account_metas.rs @@ -0,0 +1,183 @@ +use solana_instruction::AccountMeta; +use solana_pubkey::Pubkey; + +use crate::instructions::CTokenDefaultAccounts; + +/// Account metadata configuration for batch compress instruction +#[derive(Debug, Copy, Clone)] +pub struct BatchCompressMetaConfig { + pub fee_payer: Option, + pub authority: Option, + pub token_pool_pda: Pubkey, + pub sender_token_account: Pubkey, + pub token_program: Pubkey, + pub merkle_tree: Pubkey, + pub sol_pool_pda: Option, +} + +impl BatchCompressMetaConfig { + /// Create a new BatchCompressMetaConfig for direct invocation + pub fn new( + fee_payer: Pubkey, + authority: Pubkey, + token_pool_pda: Pubkey, + sender_token_account: Pubkey, + token_program: Pubkey, + merkle_tree: Pubkey, + with_lamports: bool, + ) -> Self { + let sol_pool_pda = if with_lamports { + unimplemented!("TODO hardcode sol pool pda") + } else { + None + }; + Self { + fee_payer: Some(fee_payer), + authority: Some(authority), + token_pool_pda, + sender_token_account, + token_program, + merkle_tree, + sol_pool_pda, + } + } + + /// Create a new BatchCompressMetaConfig for client-side (CPI) usage + pub fn new_client( + token_pool_pda: Pubkey, + sender_token_account: Pubkey, + token_program: Pubkey, + merkle_tree: Pubkey, + with_lamports: bool, + ) -> Self { + let sol_pool_pda = if with_lamports { + unimplemented!("TODO hardcode sol pool pda") + } else { + None + }; + Self { + fee_payer: None, + authority: None, + token_pool_pda, + sender_token_account, + token_program, + merkle_tree, + sol_pool_pda, + } + } +} + +/// Get the standard account metas for a batch compress instruction +/// Matches the MintToInstruction account structure used by batch_compress +pub fn get_batch_compress_instruction_account_metas( + config: BatchCompressMetaConfig, +) -> Vec { + let default_pubkeys = CTokenDefaultAccounts::default(); + + // Calculate capacity based on whether fee_payer is provided + // Base accounts: cpi_authority_pda + token_pool_pda + token_program + light_system_program + + // registered_program_pda + noop_program + account_compression_authority + + // account_compression_program + merkle_tree + + // self_program + system_program + sender_token_account + let base_capacity = 11; + + // Direct invoke accounts: fee_payer + authority + mint_placeholder + sol_pool_pda_or_placeholder + let fee_payer_capacity = if config.fee_payer.is_some() { 4 } else { 0 }; + + let total_capacity = base_capacity + fee_payer_capacity; + + // Start building the account metas to match MintToInstruction structure + let mut metas = Vec::with_capacity(total_capacity); + + // Add fee_payer and authority if provided (for direct invoke) + if let Some(fee_payer) = config.fee_payer { + let authority = config.authority.expect("Missing authority"); + metas.extend_from_slice(&[ + // fee_payer (mut, signer) + AccountMeta::new(fee_payer, true), + // authority (signer) + AccountMeta::new_readonly(authority, true), + ]); + } + + // cpi_authority_pda + metas.push(AccountMeta::new_readonly( + default_pubkeys.cpi_authority_pda, + false, + )); + + // mint: Option - Always None for batch_compress, so we add a placeholder + if config.fee_payer.is_some() { + metas.push(AccountMeta::new_readonly( + default_pubkeys.compressed_token_program, + false, + )); + } + println!("config {:?}", config); + println!("default_pubkeys {:?}", default_pubkeys); + // token_pool_pda (mut) + metas.push(AccountMeta::new(config.token_pool_pda, false)); + + // token_program + metas.push(AccountMeta::new_readonly(config.token_program, false)); + + // light_system_program + metas.push(AccountMeta::new_readonly( + default_pubkeys.light_system_program, + false, + )); + + // registered_program_pda + metas.push(AccountMeta::new_readonly( + default_pubkeys.registered_program_pda, + false, + )); + + // noop_program + metas.push(AccountMeta::new_readonly( + default_pubkeys.noop_program, + false, + )); + + // account_compression_authority + metas.push(AccountMeta::new_readonly( + default_pubkeys.account_compression_authority, + false, + )); + + // account_compression_program + metas.push(AccountMeta::new_readonly( + default_pubkeys.account_compression_program, + false, + )); + + // merkle_tree (mut) + metas.push(AccountMeta::new(config.merkle_tree, false)); + + // self_program (compressed token program) + metas.push(AccountMeta::new_readonly( + default_pubkeys.self_program, + false, + )); + + // system_program + metas.push(AccountMeta::new_readonly( + default_pubkeys.system_program, + false, + )); + + // sol_pool_pda (optional, mut) - add placeholder if None but fee_payer is present + if let Some(sol_pool_pda) = config.sol_pool_pda { + metas.push(AccountMeta::new(sol_pool_pda, false)); + } else if config.fee_payer.is_some() { + metas.push(AccountMeta::new_readonly( + default_pubkeys.compressed_token_program, + false, + )); + } + + // sender_token_account (mut) - last account + metas.push(AccountMeta::new(config.sender_token_account, false)); + + metas +} diff --git a/sdk-libs/compressed-token-sdk/src/instructions/batch_compress/instruction.rs b/sdk-libs/compressed-token-sdk/src/instructions/batch_compress/instruction.rs new file mode 100644 index 0000000000..e284424286 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/batch_compress/instruction.rs @@ -0,0 +1,88 @@ +use light_compressed_token_types::{ + instruction::batch_compress::BatchCompressInstructionData, BATCH_COMPRESS, +}; +use light_ctoken_types; +use solana_instruction::Instruction; +use solana_pubkey::Pubkey; + +use crate::{ + error::{Result, TokenSdkError}, + instructions::batch_compress::account_metas::{ + get_batch_compress_instruction_account_metas, BatchCompressMetaConfig, + }, + AnchorDeserialize, AnchorSerialize, +}; + +#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize)] +pub struct Recipient { + pub pubkey: Pubkey, + pub amount: u64, +} + +#[derive(Debug, Clone)] +pub struct BatchCompressInputs { + pub fee_payer: Pubkey, + pub authority: Pubkey, + pub token_pool_pda: Pubkey, + pub sender_token_account: Pubkey, + pub token_program: Pubkey, + pub merkle_tree: Pubkey, + pub recipients: Vec, + pub lamports: Option, + pub token_pool_index: u8, + pub token_pool_bump: u8, + pub sol_pool_pda: Option, +} + +pub fn create_batch_compress_instruction(inputs: BatchCompressInputs) -> Result { + let mut pubkeys = Vec::with_capacity(inputs.recipients.len()); + let mut amounts = Vec::with_capacity(inputs.recipients.len()); + + inputs.recipients.iter().for_each(|recipient| { + pubkeys.push(recipient.pubkey.to_bytes()); + amounts.push(recipient.amount); + }); + + // Create instruction data + let instruction_data = BatchCompressInstructionData { + pubkeys, + amounts: Some(amounts), + amount: None, + index: inputs.token_pool_index, + lamports: inputs.lamports, + bump: inputs.token_pool_bump, + }; + + // Serialize instruction data + let data_vec = instruction_data + .try_to_vec() + .map_err(|_| TokenSdkError::SerializationError)?; + let mut data = Vec::with_capacity(data_vec.len() + 8 + 4); + data.extend_from_slice(BATCH_COMPRESS.as_slice()); + data.extend_from_slice( + u32::try_from(data_vec.len()) + .unwrap() + .to_le_bytes() + .as_slice(), + ); + data.extend(&data_vec); + // Create account meta config for batch_compress (uses MintToInstruction accounts) + let meta_config = BatchCompressMetaConfig { + fee_payer: Some(inputs.fee_payer), + authority: Some(inputs.authority), + token_pool_pda: inputs.token_pool_pda, + sender_token_account: inputs.sender_token_account, + token_program: inputs.token_program, + merkle_tree: inputs.merkle_tree, + sol_pool_pda: inputs.sol_pool_pda, + }; + + // Get account metas that match MintToInstruction structure + let account_metas = get_batch_compress_instruction_account_metas(meta_config); + + Ok(Instruction { + program_id: Pubkey::new_from_array(light_ctoken_types::COMPRESSED_TOKEN_PROGRAM_ID), + accounts: account_metas, + data, + }) +} diff --git a/sdk-libs/compressed-token-sdk/src/instructions/batch_compress/mod.rs b/sdk-libs/compressed-token-sdk/src/instructions/batch_compress/mod.rs new file mode 100644 index 0000000000..5f207527b1 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/batch_compress/mod.rs @@ -0,0 +1,5 @@ +pub mod account_metas; +pub mod instruction; + +pub use account_metas::{get_batch_compress_instruction_account_metas, BatchCompressMetaConfig}; +pub use instruction::{create_batch_compress_instruction, BatchCompressInputs, Recipient}; diff --git a/sdk-libs/compressed-token-sdk/src/instructions/burn.rs b/sdk-libs/compressed-token-sdk/src/instructions/burn.rs new file mode 100644 index 0000000000..f652eab803 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/burn.rs @@ -0,0 +1,40 @@ +// /// Get account metas for burn instruction +// pub fn get_burn_instruction_account_metas( +// fee_payer: Pubkey, +// authority: Pubkey, +// mint: Pubkey, +// token_pool_pda: Pubkey, +// token_program: Option, +// ) -> Vec { +// let default_pubkeys = CTokenDefaultAccounts::default(); +// let token_program = token_program.unwrap_or(Pubkey::from(SPL_TOKEN_PROGRAM_ID)); + +// vec![ +// // fee_payer (mut, signer) +// AccountMeta::new(fee_payer, true), +// // authority (signer) +// AccountMeta::new_readonly(authority, true), +// // cpi_authority_pda +// AccountMeta::new_readonly(default_pubkeys.cpi_authority_pda, false), +// // mint (mut) +// AccountMeta::new(mint, false), +// // token_pool_pda (mut) +// AccountMeta::new(token_pool_pda, false), +// // token_program +// AccountMeta::new_readonly(token_program, false), +// // light_system_program +// AccountMeta::new_readonly(default_pubkeys.light_system_program, false), +// // registered_program_pda +// AccountMeta::new_readonly(default_pubkeys.registered_program_pda, false), +// // noop_program +// AccountMeta::new_readonly(default_pubkeys.noop_program, false), +// // account_compression_authority +// AccountMeta::new_readonly(default_pubkeys.account_compression_authority, false), +// // account_compression_program +// AccountMeta::new_readonly(default_pubkeys.account_compression_program, false), +// // self_program +// AccountMeta::new_readonly(default_pubkeys.self_program, false), +// // system_program +// AccountMeta::new_readonly(default_pubkeys.system_program, false), +// ] +// } diff --git a/sdk-libs/compressed-token-sdk/src/instructions/close.rs b/sdk-libs/compressed-token-sdk/src/instructions/close.rs new file mode 100644 index 0000000000..6da8d6a73e --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/close.rs @@ -0,0 +1,25 @@ +use solana_instruction::{AccountMeta, Instruction}; +use solana_pubkey::Pubkey; + +/// Creates a `CloseAccount` instruction. +pub fn close_account( + token_program_id: &Pubkey, + account_pubkey: &Pubkey, + destination_pubkey: &Pubkey, + owner_pubkey: &Pubkey, +) -> Instruction { + // TODO: do manual serialization + let data = spl_token_2022::instruction::TokenInstruction::CloseAccount.pack(); + + let accounts = vec![ + AccountMeta::new(*account_pubkey, false), + AccountMeta::new(*destination_pubkey, false), + AccountMeta::new_readonly(*owner_pubkey, true), // signer + ]; + + Instruction { + program_id: *token_program_id, + accounts, + data, + } +} diff --git a/sdk-libs/compressed-token-sdk/src/instructions/create_associated_token_account.rs b/sdk-libs/compressed-token-sdk/src/instructions/create_associated_token_account.rs new file mode 100644 index 0000000000..4ea43e4c57 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/create_associated_token_account.rs @@ -0,0 +1,108 @@ +use solana_instruction::Instruction; +use solana_pubkey::Pubkey; + +use crate::error::Result; + +/// Input parameters for creating an associated token account with compressible extension +#[derive(Debug, Clone)] +pub struct CreateCompressibleAssociatedTokenAccountInputs { + /// The payer for the account creation + pub payer: Pubkey, + /// The owner of the associated token account + pub owner: Pubkey, + /// The mint for the associated token account + pub mint: Pubkey, + /// The authority that can close this account (in addition to owner) + pub rent_authority: Pubkey, + /// The recipient of lamports when the account is closed by rent authority + pub rent_recipient: Pubkey, + /// Number of slots that must pass before compression is allowed + pub slots_until_compression: u64, +} + +/// Creates a compressible associated token account instruction +pub fn create_compressible_associated_token_account( + inputs: CreateCompressibleAssociatedTokenAccountInputs, +) -> Result { + let (ata_pubkey, bump) = derive_ctoken_ata(&inputs.owner, &inputs.mint); + create_compressible_associated_token_account_with_bump(inputs, ata_pubkey, bump) +} + +/// Creates a compressible associated token account instruction with a specified bump +pub fn create_compressible_associated_token_account_with_bump( + inputs: CreateCompressibleAssociatedTokenAccountInputs, + ata_pubkey: Pubkey, + bump: u8, +) -> Result { + // Manual serialization: [discriminator, owner, mint, bump, compressible_config] + let mut data = Vec::with_capacity(103 + 32 + 32 + 1 + 1 + 8 + 32 + 32); + data.push(103u8); // CreateAssociatedTokenAccount discriminator + data.extend_from_slice(&inputs.owner.to_bytes()); // owner: 32 bytes + data.extend_from_slice(&inputs.mint.to_bytes()); // mint: 32 bytes + data.push(bump); // bump: 1 byte + data.push(1u8); // Some option byte for compressible_config + data.extend_from_slice(&inputs.slots_until_compression.to_le_bytes()); // slots_until_compression: 8 bytes + data.extend_from_slice(&inputs.rent_authority.to_bytes()); // rent_authority: 32 bytes + data.extend_from_slice(&inputs.rent_recipient.to_bytes()); // rent_recipient: 32 bytes + + Ok(Instruction { + program_id: Pubkey::from(light_ctoken_types::COMPRESSED_TOKEN_PROGRAM_ID), + accounts: vec![ + solana_instruction::AccountMeta::new(inputs.payer, true), // fee_payer (signer) + solana_instruction::AccountMeta::new(ata_pubkey, false), // associated_token_account + solana_instruction::AccountMeta::new_readonly(inputs.mint, false), // mint + solana_instruction::AccountMeta::new_readonly(inputs.owner, false), // owner + solana_instruction::AccountMeta::new_readonly(Pubkey::new_from_array([0; 32]), false), // system_program + ], + data, + }) +} + +/// Creates a basic associated token account instruction +pub fn create_associated_token_account( + payer: Pubkey, + owner: Pubkey, + mint: Pubkey, +) -> Result { + let (ata_pubkey, bump) = derive_ctoken_ata(&owner, &mint); + create_associated_token_account_with_bump(payer, owner, mint, ata_pubkey, bump) +} + +pub fn create_associated_token_account_with_bump( + payer: Pubkey, + owner: Pubkey, + mint: Pubkey, + ata_pubkey: Pubkey, + bump: u8, +) -> Result { + // Manual serialization: [discriminator, owner, mint, bump, compressible_config] + let mut data = Vec::with_capacity(1 + 32 + 32 + 1 + 1); + data.push(103u8); // CreateAssociatedTokenAccount discriminator + data.extend_from_slice(&owner.to_bytes()); // owner: 32 bytes + data.extend_from_slice(&mint.to_bytes()); // mint: 32 bytes + data.push(bump); // bump: 1 byte + data.push(0u8); // None option byte for compressible_config + + Ok(Instruction { + program_id: Pubkey::from(light_ctoken_types::COMPRESSED_TOKEN_PROGRAM_ID), + accounts: vec![ + solana_instruction::AccountMeta::new(payer, true), // fee_payer (signer) + solana_instruction::AccountMeta::new(ata_pubkey, false), // associated_token_account + solana_instruction::AccountMeta::new_readonly(mint, false), // mint + solana_instruction::AccountMeta::new_readonly(owner, false), // owner + solana_instruction::AccountMeta::new_readonly(Pubkey::new_from_array([0; 32]), false), // system_program + ], + data, + }) +} + +pub fn derive_ctoken_ata(owner: &Pubkey, mint: &Pubkey) -> (Pubkey, u8) { + Pubkey::find_program_address( + &[ + owner.as_ref(), + light_ctoken_types::COMPRESSED_TOKEN_PROGRAM_ID.as_ref(), + mint.as_ref(), + ], + &Pubkey::from(light_ctoken_types::COMPRESSED_TOKEN_PROGRAM_ID), + ) +} diff --git a/sdk-libs/compressed-token-sdk/src/instructions/create_compressed_mint/account_metas.rs b/sdk-libs/compressed-token-sdk/src/instructions/create_compressed_mint/account_metas.rs new file mode 100644 index 0000000000..355744c162 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/create_compressed_mint/account_metas.rs @@ -0,0 +1,130 @@ +use solana_instruction::AccountMeta; +use solana_pubkey::Pubkey; + +use crate::instructions::CTokenDefaultAccounts; + +/// Account metadata configuration for create compressed mint instruction +#[derive(Debug, Copy, Clone)] +pub struct CreateCompressedMintMetaConfig { + pub fee_payer: Option, + pub mint_signer: Option, + pub address_tree_pubkey: Pubkey, + pub output_queue: Pubkey, +} + +impl CreateCompressedMintMetaConfig { + /// Create a new CreateCompressedMintMetaConfig for direct invocation + pub fn new( + fee_payer: Pubkey, + mint_signer: Pubkey, + address_tree_pubkey: Pubkey, + output_queue: Pubkey, + ) -> Self { + Self { + fee_payer: Some(fee_payer), + mint_signer: Some(mint_signer), + address_tree_pubkey, + output_queue, + } + } + + /// Create a new CreateCompressedMintMetaConfig for client-side (CPI) usage + pub fn new_client( + mint_seed: Pubkey, + address_tree_pubkey: Pubkey, + output_queue: Pubkey, + ) -> Self { + Self { + fee_payer: None, + mint_signer: Some(mint_seed), + address_tree_pubkey, + output_queue, + } + } +} + +/// Get the standard account metas for a create compressed mint instruction +pub fn get_create_compressed_mint_instruction_account_metas( + config: CreateCompressedMintMetaConfig, +) -> Vec { + let default_pubkeys = CTokenDefaultAccounts::default(); + + // Calculate capacity based on whether fee_payer is provided + // Base accounts: light_system_program + cpi_authority_pda + registered_program_pda + + // noop_program + account_compression_authority + account_compression_program + + // self_program + system_program + address_merkle_tree + output_queue + let base_capacity = 10; + + // Direct invoke accounts: mint_signer + fee_payer + let direct_invoke_capacity = if config.fee_payer.is_some() { 2 } else { 0 }; + + let total_capacity = base_capacity + direct_invoke_capacity; + + let mut metas = Vec::with_capacity(total_capacity); + + // Add mint_signer and fee_payer if provided (for direct invoke) + if let Some(mint_signer) = config.mint_signer { + metas.push(AccountMeta::new_readonly(mint_signer, true)); + } + + // light_system_program + metas.push(AccountMeta::new_readonly( + default_pubkeys.light_system_program, + false, + )); + + // Add fee_payer if provided (for direct invoke) + if let Some(fee_payer) = config.fee_payer { + metas.push(AccountMeta::new(fee_payer, true)); + } + + // cpi_authority_pda + metas.push(AccountMeta::new_readonly( + default_pubkeys.cpi_authority_pda, + false, + )); + + // registered_program_pda + metas.push(AccountMeta::new_readonly( + default_pubkeys.registered_program_pda, + false, + )); + + // noop_program + metas.push(AccountMeta::new_readonly( + default_pubkeys.noop_program, + false, + )); + + // account_compression_authority + metas.push(AccountMeta::new_readonly( + default_pubkeys.account_compression_authority, + false, + )); + + // account_compression_program + metas.push(AccountMeta::new_readonly( + default_pubkeys.account_compression_program, + false, + )); + + // self_program (compressed token program) + metas.push(AccountMeta::new_readonly( + default_pubkeys.self_program, + false, + )); + + // system_program + metas.push(AccountMeta::new_readonly( + default_pubkeys.system_program, + false, + )); + + // address_merkle_tree (mutable) + metas.push(AccountMeta::new(config.address_tree_pubkey, false)); + + // output_queue (mutable) + metas.push(AccountMeta::new(config.output_queue, false)); + + metas +} diff --git a/sdk-libs/compressed-token-sdk/src/instructions/create_compressed_mint/instruction.rs b/sdk-libs/compressed-token-sdk/src/instructions/create_compressed_mint/instruction.rs new file mode 100644 index 0000000000..1ae0ef6838 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/create_compressed_mint/instruction.rs @@ -0,0 +1,112 @@ +use light_compressed_account::instruction_data::compressed_proof::CompressedProof; +use light_ctoken_types::{ + self, instructions::extensions::ExtensionInstructionData, COMPRESSED_MINT_SEED, +}; +use solana_instruction::Instruction; +use solana_pubkey::Pubkey; + +use crate::{ + error::{Result, TokenSdkError}, + instructions::create_compressed_mint::account_metas::{ + get_create_compressed_mint_instruction_account_metas, CreateCompressedMintMetaConfig, + }, + AnchorDeserialize, AnchorSerialize, +}; + +pub const CREATE_COMPRESSED_MINT_DISCRIMINATOR: u8 = 100; + +/// Input struct for creating a compressed mint instruction +#[derive(Debug, Clone, AnchorDeserialize, AnchorSerialize)] +pub struct CreateCompressedMintInputs { + pub decimals: u8, + pub mint_authority: Pubkey, + pub freeze_authority: Option, + pub proof: CompressedProof, + pub mint_bump: u8, + pub address_merkle_tree_root_index: u16, + pub mint_signer: Pubkey, + pub payer: Pubkey, + pub address_tree_pubkey: Pubkey, + pub output_queue: Pubkey, + pub extensions: Option>, + pub version: u8, +} + +/// Creates a compressed mint instruction with a pre-computed mint address +pub fn create_compressed_mint_cpi( + input: CreateCompressedMintInputs, + mint_address: [u8; 32], +) -> Result { + use light_ctoken_types::instructions::create_compressed_mint::CreateCompressedMintInstructionData; + + let instruction_data = CreateCompressedMintInstructionData { + decimals: input.decimals, + mint_authority: input.mint_authority.to_bytes().into(), + freeze_authority: input.freeze_authority.map(|auth| auth.to_bytes().into()), + proof: input.proof, + mint_bump: input.mint_bump, + address_merkle_tree_root_index: input.address_merkle_tree_root_index, + extensions: input.extensions, + mint_address, + version: input.version, + }; + + // Create account meta config for create_compressed_mint + let meta_config = CreateCompressedMintMetaConfig { + fee_payer: Some(input.payer), + mint_signer: Some(input.mint_signer), + address_tree_pubkey: input.address_tree_pubkey, + output_queue: input.output_queue, + }; + + // Get account metas + let accounts = get_create_compressed_mint_instruction_account_metas(meta_config); + + // Serialize instruction data + let data_vec = instruction_data + .try_to_vec() + .map_err(|_| TokenSdkError::SerializationError)?; + + Ok(Instruction { + program_id: Pubkey::new_from_array(light_ctoken_types::COMPRESSED_TOKEN_PROGRAM_ID), + accounts, + data: [vec![CREATE_COMPRESSED_MINT_DISCRIMINATOR], data_vec].concat(), + }) +} + +/// Creates a compressed mint instruction with automatic mint address derivation +pub fn create_compressed_mint(input: CreateCompressedMintInputs) -> Result { + let mint_address = + derive_compressed_mint_address(&input.mint_signer, &input.address_tree_pubkey); + create_compressed_mint_cpi(input, mint_address) +} + +/// Derives the compressed mint address from the mint seed and address tree +pub fn derive_compressed_mint_address( + mint_seed: &Pubkey, + address_tree_pubkey: &Pubkey, +) -> [u8; 32] { + light_compressed_account::address::derive_address( + &find_spl_mint_address(mint_seed).0.to_bytes(), + &address_tree_pubkey.to_bytes(), + &light_ctoken_types::COMPRESSED_TOKEN_PROGRAM_ID, + ) +} + +pub fn derive_compressed_mint_from_spl_mint( + spl_mint: &Pubkey, + address_tree_pubkey: &Pubkey, +) -> [u8; 32] { + light_compressed_account::address::derive_address( + &spl_mint.to_bytes(), + &address_tree_pubkey.to_bytes(), + &light_ctoken_types::COMPRESSED_TOKEN_PROGRAM_ID, + ) +} + +pub fn find_spl_mint_address(mint_seed: &Pubkey) -> (Pubkey, u8) { + Pubkey::find_program_address( + &[COMPRESSED_MINT_SEED, mint_seed.as_ref()], + &Pubkey::new_from_array(light_ctoken_types::COMPRESSED_TOKEN_PROGRAM_ID), + ) +} diff --git a/sdk-libs/compressed-token-sdk/src/instructions/create_compressed_mint/mod.rs b/sdk-libs/compressed-token-sdk/src/instructions/create_compressed_mint/mod.rs new file mode 100644 index 0000000000..148bfa9b91 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/create_compressed_mint/mod.rs @@ -0,0 +1,11 @@ +pub mod account_metas; +pub mod instruction; + +pub use account_metas::{ + get_create_compressed_mint_instruction_account_metas, CreateCompressedMintMetaConfig, +}; +pub use instruction::{ + create_compressed_mint, create_compressed_mint_cpi, derive_compressed_mint_address, + derive_compressed_mint_from_spl_mint, find_spl_mint_address, CreateCompressedMintInputs, + CREATE_COMPRESSED_MINT_DISCRIMINATOR, +}; diff --git a/sdk-libs/compressed-token-sdk/src/instructions/create_spl_mint.rs b/sdk-libs/compressed-token-sdk/src/instructions/create_spl_mint.rs new file mode 100644 index 0000000000..5b34c38b39 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/create_spl_mint.rs @@ -0,0 +1,128 @@ +use light_compressed_token_types::{ValidityProof, CPI_AUTHORITY_PDA}; +use light_ctoken_types::{ + instructions::{ + create_compressed_mint::UpdateCompressedMintInstructionData, + create_spl_mint::CreateSplMintInstructionData, mint_to_compressed::CompressedMintInputs, + }, + COMPRESSED_TOKEN_PROGRAM_ID, +}; +use light_sdk::constants::{ + ACCOUNT_COMPRESSION_AUTHORITY_PDA, ACCOUNT_COMPRESSION_PROGRAM_ID, LIGHT_SYSTEM_PROGRAM_ID, + NOOP_PROGRAM_ID, REGISTERED_PROGRAM_PDA, +}; +use solana_instruction::{AccountMeta, Instruction}; +use solana_pubkey::Pubkey; + +use crate::{error::Result, AnchorSerialize}; + +pub const POOL_SEED: &[u8] = b"pool"; + +pub struct CreateSplMintInputs { + pub mint_signer: Pubkey, + pub mint_bump: u8, + pub compressed_mint_inputs: CompressedMintInputs, + pub proof: ValidityProof, + pub payer: Pubkey, + pub input_merkle_tree: Pubkey, + pub input_output_queue: Pubkey, + pub output_queue: Pubkey, + pub mint_authority: Pubkey, +} + +pub fn create_spl_mint_instruction(inputs: CreateSplMintInputs) -> Result { + // Extract values from compressed_mint_inputs + let mint_pda: Pubkey = inputs + .compressed_mint_inputs + .compressed_mint_input + .spl_mint + .to_bytes() + .into(); + // Find token pool PDA index 0 + let (token_pool_pda, _token_pool_bump) = Pubkey::find_program_address( + &[POOL_SEED, &mint_pda.to_bytes()], + &Pubkey::new_from_array(COMPRESSED_TOKEN_PROGRAM_ID), + ); + create_spl_mint_instruction_with_bump(inputs, token_pool_pda) +} + +pub fn create_spl_mint_instruction_with_bump( + inputs: CreateSplMintInputs, + token_pool_pda: Pubkey, +) -> Result { + let CreateSplMintInputs { + mint_signer, + mint_bump, + compressed_mint_inputs, + proof, + payer, + input_merkle_tree, + input_output_queue, + output_queue, + mint_authority, + } = inputs; + // Extract values from compressed_mint_inputs + let mint_pda: Pubkey = compressed_mint_inputs + .compressed_mint_input + .spl_mint + .to_bytes() + .into(); + let mint_authority_is_none = compressed_mint_inputs + .compressed_mint_input + .mint_authority + .is_none(); + // Create UpdateCompressedMintInstructionData from the compressed mint inputs + let update_mint_data = UpdateCompressedMintInstructionData { + leaf_index: compressed_mint_inputs.leaf_index.into(), + prove_by_index: compressed_mint_inputs.prove_by_index, + root_index: compressed_mint_inputs.root_index, + address: compressed_mint_inputs.address, + proof: proof.into(), + mint: compressed_mint_inputs.compressed_mint_input.try_into()?, + }; + + // Create the create_spl_mint instruction data + let create_spl_mint_instruction_data = CreateSplMintInstructionData { + mint_bump, + mint: update_mint_data, + mint_authority_is_none, + }; + + // Create create_spl_mint accounts in the exact order expected by accounts.rs + let create_spl_mint_accounts = vec![ + // Static non-CPI accounts first (in order from accounts.rs) + AccountMeta::new(mint_authority, true), // authority (signer) + AccountMeta::new(mint_pda, false), // mint + AccountMeta::new_readonly(mint_signer, false), // mint_signer + AccountMeta::new(token_pool_pda, false), // token_pool_pda + AccountMeta::new_readonly(spl_token_2022::ID, false), // token_program TODO: add constant + AccountMeta::new_readonly(Pubkey::new_from_array(LIGHT_SYSTEM_PROGRAM_ID), false), // light_system_program + // CPI accounts in exact order expected by light-system-program + AccountMeta::new(payer, true), // fee_payer (signer, mutable) + AccountMeta::new_readonly(Pubkey::new_from_array(CPI_AUTHORITY_PDA), false), // cpi_authority_pda + AccountMeta::new_readonly(Pubkey::new_from_array(REGISTERED_PROGRAM_PDA), false), // registered_program_pda + AccountMeta::new_readonly(Pubkey::new_from_array(NOOP_PROGRAM_ID), false), // noop_program + AccountMeta::new_readonly( + Pubkey::new_from_array(ACCOUNT_COMPRESSION_AUTHORITY_PDA), + false, + ), // account_compression_authority + AccountMeta::new_readonly( + Pubkey::new_from_array(ACCOUNT_COMPRESSION_PROGRAM_ID), + false, + ), // account_compression_program + AccountMeta::new_readonly(Pubkey::new_from_array(COMPRESSED_TOKEN_PROGRAM_ID), false), // self_program + AccountMeta::new_readonly(Pubkey::default(), false), // system_program + AccountMeta::new(input_merkle_tree, false), // in_merkle_tree + AccountMeta::new(input_output_queue, false), // in_output_queue + AccountMeta::new(output_queue, false), // out_output_queue + ]; + + Ok(Instruction { + program_id: Pubkey::new_from_array(COMPRESSED_TOKEN_PROGRAM_ID), + accounts: create_spl_mint_accounts, + data: [ + vec![102], // CreateSplMint discriminator + create_spl_mint_instruction_data.try_to_vec().unwrap(), // TODO: use manual serialization + ] + .concat(), + }) +} diff --git a/sdk-libs/compressed-token-sdk/src/instructions/create_token_account/instruction.rs b/sdk-libs/compressed-token-sdk/src/instructions/create_token_account/instruction.rs new file mode 100644 index 0000000000..3c26db0efd --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/create_token_account/instruction.rs @@ -0,0 +1,66 @@ +use solana_instruction::Instruction; +use solana_pubkey::Pubkey; + +use crate::error::Result; + +/// Input parameters for creating a token account with compressible extension +#[derive(Debug, Clone)] +pub struct CreateCompressibleTokenAccount { + /// The account to be created + pub account_pubkey: Pubkey, + /// The mint for the token account + pub mint_pubkey: Pubkey, + /// The owner of the token account + pub owner_pubkey: Pubkey, + /// The authority that can close this account (in addition to owner) + pub rent_authority: Pubkey, + /// The recipient of lamports when the account is closed by rent authority + pub rent_recipient: Pubkey, + /// Number of slots that must pass before compression is allowed + pub slots_until_compression: u64, +} + +pub fn create_compressible_token_account( + inputs: CreateCompressibleTokenAccount, +) -> Result { + // Format: [18, owner_pubkey_32_bytes, 0] + // Create compressible extension data manually + // Layout: [slots_until_compression: u64, rent_authority: 32 bytes, rent_recipient: 32 bytes] + let mut data = Vec::with_capacity(1 + 32 + 1 + 8 + 32 + 32); + data.push(18u8); // InitializeAccount3 opcode + data.extend_from_slice(&inputs.owner_pubkey.to_bytes()); + data.push(1); // Some option byte extension + data.extend_from_slice(&inputs.slots_until_compression.to_le_bytes()); + data.extend_from_slice(&inputs.rent_authority.to_bytes()); + data.extend_from_slice(&inputs.rent_recipient.to_bytes()); + + Ok(Instruction { + program_id: Pubkey::from(light_ctoken_types::COMPRESSED_TOKEN_PROGRAM_ID), + accounts: vec![ + solana_instruction::AccountMeta::new(inputs.account_pubkey, false), + solana_instruction::AccountMeta::new_readonly(inputs.mint_pubkey, false), + ], + data, + }) +} + +pub fn create_token_account( + account_pubkey: Pubkey, + mint_pubkey: Pubkey, + owner_pubkey: Pubkey, +) -> Result { + // Create InitializeAccount3 instruction data manually + // Format: [18, owner_pubkey_32_bytes, 0] + let mut data = Vec::with_capacity(1 + 32); + data.push(18u8); // InitializeAccount3 opcode + data.extend_from_slice(&owner_pubkey.to_bytes()); + + Ok(Instruction { + program_id: Pubkey::from(light_ctoken_types::COMPRESSED_TOKEN_PROGRAM_ID), + accounts: vec![ + solana_instruction::AccountMeta::new(account_pubkey, false), + solana_instruction::AccountMeta::new_readonly(mint_pubkey, false), + ], + data, + }) +} diff --git a/sdk-libs/compressed-token-sdk/src/instructions/create_token_account/mod.rs b/sdk-libs/compressed-token-sdk/src/instructions/create_token_account/mod.rs new file mode 100644 index 0000000000..695c46be13 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/create_token_account/mod.rs @@ -0,0 +1,3 @@ +pub mod instruction; + +pub use instruction::*; diff --git a/sdk-libs/compressed-token-sdk/src/instructions/ctoken_accounts.rs b/sdk-libs/compressed-token-sdk/src/instructions/ctoken_accounts.rs new file mode 100644 index 0000000000..8651634066 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/ctoken_accounts.rs @@ -0,0 +1,36 @@ +use light_compressed_token_types::{ + ACCOUNT_COMPRESSION_AUTHORITY_PDA, ACCOUNT_COMPRESSION_PROGRAM_ID, CPI_AUTHORITY_PDA, + LIGHT_SYSTEM_PROGRAM_ID, NOOP_PROGRAM_ID, PROGRAM_ID as LIGHT_COMPRESSED_TOKEN_PROGRAM_ID, +}; +use light_sdk::constants::{C_TOKEN_PROGRAM_ID, REGISTERED_PROGRAM_PDA}; +use solana_pubkey::Pubkey; + +/// Standard pubkeys for compressed token instructions +#[derive(Debug, Copy, Clone)] +pub struct CTokenDefaultAccounts { + pub light_system_program: Pubkey, + pub registered_program_pda: Pubkey, + pub noop_program: Pubkey, + pub account_compression_authority: Pubkey, + pub account_compression_program: Pubkey, + pub self_program: Pubkey, + pub cpi_authority_pda: Pubkey, + pub system_program: Pubkey, + pub compressed_token_program: Pubkey, +} + +impl Default for CTokenDefaultAccounts { + fn default() -> Self { + Self { + light_system_program: Pubkey::from(LIGHT_SYSTEM_PROGRAM_ID), + registered_program_pda: Pubkey::from(REGISTERED_PROGRAM_PDA), + noop_program: Pubkey::from(NOOP_PROGRAM_ID), + account_compression_authority: Pubkey::from(ACCOUNT_COMPRESSION_AUTHORITY_PDA), + account_compression_program: Pubkey::from(ACCOUNT_COMPRESSION_PROGRAM_ID), + self_program: Pubkey::from(LIGHT_COMPRESSED_TOKEN_PROGRAM_ID), + cpi_authority_pda: Pubkey::from(CPI_AUTHORITY_PDA), + system_program: Pubkey::default(), + compressed_token_program: Pubkey::from(C_TOKEN_PROGRAM_ID), + } + } +} diff --git a/sdk-libs/compressed-token-sdk/src/instructions/mint_to.rs b/sdk-libs/compressed-token-sdk/src/instructions/mint_to.rs new file mode 100644 index 0000000000..c96bbf3dcd --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/mint_to.rs @@ -0,0 +1,43 @@ +// /// Get account metas for mint_to instruction +// pub fn get_mint_to_instruction_account_metas( +// fee_payer: Pubkey, +// authority: Pubkey, +// mint: Pubkey, +// token_pool_pda: Pubkey, +// merkle_tree: Pubkey, +// token_program: Option, +// ) -> Vec { +// let default_pubkeys = CTokenDefaultAccounts::default(); +// let token_program = token_program.unwrap_or(Pubkey::from(SPL_TOKEN_PROGRAM_ID)); + +// vec![ +// // fee_payer (mut, signer) +// AccountMeta::new(fee_payer, true), +// // authority (signer) +// AccountMeta::new_readonly(authority, true), +// // cpi_authority_pda +// AccountMeta::new_readonly(default_pubkeys.cpi_authority_pda, false), +// // mint (optional, mut) +// AccountMeta::new(mint, false), +// // token_pool_pda (mut) +// AccountMeta::new(token_pool_pda, false), +// // token_program +// AccountMeta::new_readonly(token_program, false), +// // light_system_program +// AccountMeta::new_readonly(default_pubkeys.light_system_program, false), +// // registered_program_pda +// AccountMeta::new_readonly(default_pubkeys.registered_program_pda, false), +// // noop_program +// AccountMeta::new_readonly(default_pubkeys.noop_program, false), +// // account_compression_authority +// AccountMeta::new_readonly(default_pubkeys.account_compression_authority, false), +// // account_compression_program +// AccountMeta::new_readonly(default_pubkeys.account_compression_program, false), +// // merkle_tree (mut) +// AccountMeta::new(merkle_tree, false), +// // self_program +// AccountMeta::new_readonly(default_pubkeys.self_program, false), +// // system_program +// AccountMeta::new_readonly(default_pubkeys.system_program, false), +// ] +// } diff --git a/sdk-libs/compressed-token-sdk/src/instructions/mint_to_compressed/account_metas.rs b/sdk-libs/compressed-token-sdk/src/instructions/mint_to_compressed/account_metas.rs new file mode 100644 index 0000000000..6f576737d0 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/mint_to_compressed/account_metas.rs @@ -0,0 +1,201 @@ +use solana_instruction::AccountMeta; +use solana_pubkey::Pubkey; + +use crate::instructions::CTokenDefaultAccounts; + +/// Account metadata configuration for mint_to_compressed instruction +#[derive(Debug, Copy, Clone)] +pub struct MintToCompressedMetaConfig { + pub mint_authority: Option, + pub payer: Option, + pub state_merkle_tree: Pubkey, + pub output_queue: Pubkey, + pub state_tree_pubkey: Pubkey, + pub compressed_mint_tree: Pubkey, + pub compressed_mint_queue: Pubkey, + pub is_decompressed: bool, + pub mint_pda: Option, + pub token_pool_pda: Option, + pub token_program: Option, + pub with_lamports: bool, +} + +impl MintToCompressedMetaConfig { + /// Create a new MintToCompressedMetaConfig for standard compressed mint operations + pub fn new( + mint_authority: Pubkey, + payer: Pubkey, + state_merkle_tree: Pubkey, + output_queue: Pubkey, + state_tree_pubkey: Pubkey, + compressed_mint_tree: Pubkey, + compressed_mint_queue: Pubkey, + with_lamports: bool, + ) -> Self { + Self { + mint_authority: Some(mint_authority), + payer: Some(payer), + state_merkle_tree, + output_queue, + state_tree_pubkey, + compressed_mint_tree, + compressed_mint_queue, + is_decompressed: false, + mint_pda: None, + token_pool_pda: None, + token_program: None, + with_lamports, + } + } + + /// Create a new MintToCompressedMetaConfig for client use (excludes authority and payer accounts) + pub fn new_client( + state_merkle_tree: Pubkey, + output_queue: Pubkey, + state_tree_pubkey: Pubkey, + compressed_mint_tree: Pubkey, + compressed_mint_queue: Pubkey, + with_lamports: bool, + ) -> Self { + Self { + mint_authority: None, // Client mode - account provided by caller + payer: None, // Client mode - account provided by caller + state_merkle_tree, + output_queue, + state_tree_pubkey, + compressed_mint_tree, + compressed_mint_queue, + is_decompressed: false, + mint_pda: None, + token_pool_pda: None, + token_program: None, + with_lamports, + } + } + + /// Create a new MintToCompressedMetaConfig for decompressed mint operations + pub fn new_decompressed( + mint_authority: Pubkey, + payer: Pubkey, + state_merkle_tree: Pubkey, + output_queue: Pubkey, + state_tree_pubkey: Pubkey, + compressed_mint_tree: Pubkey, + compressed_mint_queue: Pubkey, + mint_pda: Pubkey, + token_pool_pda: Pubkey, + token_program: Pubkey, + with_lamports: bool, + ) -> Self { + Self { + mint_authority: Some(mint_authority), + payer: Some(payer), + state_merkle_tree, + output_queue, + state_tree_pubkey, + compressed_mint_tree, + compressed_mint_queue, + is_decompressed: true, + mint_pda: Some(mint_pda), + token_pool_pda: Some(token_pool_pda), + token_program: Some(token_program), + with_lamports, + } + } +} + +/// Get the standard account metas for a mint_to_compressed instruction +pub fn get_mint_to_compressed_instruction_account_metas( + config: MintToCompressedMetaConfig, +) -> Vec { + let default_pubkeys = CTokenDefaultAccounts::default(); + + // Calculate capacity based on configuration + // Optional accounts: authority + payer + optional decompressed accounts (3) + light_system_program + + // cpi accounts (6 without fee_payer) + optional SOL pool + system_program + merkle tree accounts (5) + let base_capacity = 14; // light_system_program + 6 cpi accounts + system_program + 5 tree accounts + let authority_capacity = if config.mint_authority.is_some() { 1 } else { 0 }; + let payer_capacity = if config.payer.is_some() { 1 } else { 0 }; + let decompressed_capacity = if config.is_decompressed { 3 } else { 0 }; + let sol_pool_capacity = if config.with_lamports { 1 } else { 0 }; + let total_capacity = base_capacity + authority_capacity + payer_capacity + decompressed_capacity + sol_pool_capacity; + + let mut metas = Vec::with_capacity(total_capacity); + + // authority (signer) - only add if provided + if let Some(mint_authority) = config.mint_authority { + metas.push(AccountMeta::new_readonly(mint_authority, true)); + } + + // Optional decompressed mint accounts + if config.is_decompressed { + metas.push(AccountMeta::new(config.mint_pda.unwrap(), false)); // mint + metas.push(AccountMeta::new(config.token_pool_pda.unwrap(), false)); // token_pool_pda + metas.push(AccountMeta::new_readonly( + config.token_program.unwrap(), + false, + )); // token_program + } + + // light_system_program + metas.push(AccountMeta::new_readonly( + default_pubkeys.light_system_program, + false, + )); + + // CPI accounts in exact order expected by InvokeCpiWithReadOnly + if let Some(payer) = config.payer { + metas.push(AccountMeta::new(payer, true)); // fee_payer (signer, mutable) + } + metas.push(AccountMeta::new_readonly( + default_pubkeys.cpi_authority_pda, + false, + )); // cpi_authority_pda + metas.push(AccountMeta::new_readonly( + default_pubkeys.registered_program_pda, + false, + )); // registered_program_pda + metas.push(AccountMeta::new_readonly( + default_pubkeys.noop_program, + false, + )); // noop_program + metas.push(AccountMeta::new_readonly( + default_pubkeys.account_compression_authority, + false, + )); // account_compression_authority + metas.push(AccountMeta::new_readonly( + default_pubkeys.account_compression_program, + false, + )); // account_compression_program + metas.push(AccountMeta::new_readonly( + default_pubkeys.self_program, + false, + )); // self_program + + // Optional SOL pool + if config.with_lamports { + metas.push(AccountMeta::new( + Pubkey::from(light_sdk::constants::SOL_POOL_PDA), + false, + )); // sol_pool_pda (mutable) + } + + // system_program + metas.push(AccountMeta::new_readonly( + default_pubkeys.system_program, + false, + )); + + // Merkle tree accounts - UpdateOneCompressedAccountTreeAccounts (3 accounts) + metas.push(AccountMeta::new(config.state_merkle_tree, false)); // in_merkle_tree (mutable) + metas.push(AccountMeta::new(config.compressed_mint_queue, false)); // in_output_queue (mutable) + metas.push(AccountMeta::new(config.compressed_mint_queue, false)); // out_output_queue (mutable) - same as in_output_queue + + // Additional tokens_out_queue (separate from UpdateOneCompressedAccountTreeAccounts) + metas.push(AccountMeta::new(config.output_queue, false)); // tokens_out_queue (mutable) + + // Compressed mint's address tree + metas.push(AccountMeta::new(config.compressed_mint_tree, false)); + + metas +} diff --git a/sdk-libs/compressed-token-sdk/src/instructions/mint_to_compressed/instruction.rs b/sdk-libs/compressed-token-sdk/src/instructions/mint_to_compressed/instruction.rs new file mode 100644 index 0000000000..62e86b9b48 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/mint_to_compressed/instruction.rs @@ -0,0 +1,125 @@ +use light_ctoken_types::{ + instructions::{ + create_compressed_mint::UpdateCompressedMintInstructionData, + mint_to_compressed::{CompressedMintInputs, MintToCompressedInstructionData, Recipient}, + }, + COMPRESSED_TOKEN_PROGRAM_ID, +}; +use solana_instruction::Instruction; +use solana_pubkey::Pubkey; + +use crate::{ + error::{Result, TokenSdkError}, + instructions::mint_to_compressed::account_metas::{ + get_mint_to_compressed_instruction_account_metas, MintToCompressedMetaConfig, + }, + AnchorSerialize, +}; + +pub use light_compressed_token_types::account_infos::mint_to_compressed::DecompressedMintConfig; + +pub const MINT_TO_COMPRESSED_DISCRIMINATOR: u8 = 101; + +/// Input parameters for creating a mint_to_compressed instruction +#[derive(Debug, Clone)] +pub struct MintToCompressedInputs { + pub compressed_mint_inputs: CompressedMintInputs, + pub lamports: Option, + pub recipients: Vec, + pub mint_authority: Pubkey, + pub payer: Pubkey, + pub state_merkle_tree: Pubkey, + pub output_queue: Pubkey, + pub state_tree_pubkey: Pubkey, + /// Required if the mint is decompressed + pub decompressed_mint_config: Option>, +} + +/// Create a mint_to_compressed instruction +pub fn create_mint_to_compressed_instruction( + inputs: MintToCompressedInputs, +) -> Result { + let MintToCompressedInputs { + compressed_mint_inputs, + lamports, + recipients, + mint_authority, + payer, + state_merkle_tree, + output_queue, + state_tree_pubkey, + decompressed_mint_config, + } = inputs; + + // Store decompressed flag before moving the compressed_mint_input + let is_decompressed = compressed_mint_inputs.compressed_mint_input.is_decompressed; + + // Validate that decompressed_mint_config is provided when the mint is decompressed + if is_decompressed && decompressed_mint_config.is_none() { + return Err(TokenSdkError::DecompressedMintConfigRequired); + } + + // Create UpdateCompressedMintInstructionData from CompressedMintInputs + let update_mint_data = UpdateCompressedMintInstructionData { + leaf_index: compressed_mint_inputs.leaf_index.into(), + prove_by_index: compressed_mint_inputs.prove_by_index.into(), + root_index: compressed_mint_inputs.root_index, + address: compressed_mint_inputs.address, + proof: None, // No proof needed for this test + mint: compressed_mint_inputs.compressed_mint_input.try_into()?, + }; + + // Create mint_to_compressed instruction data + let mint_to_instruction_data = MintToCompressedInstructionData { + token_account_version: 2, // V2 for batched merkle trees + compressed_mint_inputs: update_mint_data, + lamports, + recipients, + proof: None, // No proof needed for this test + }; + + // Create account meta config + let has_sol_pool = lamports.is_some(); + + let meta_config = if is_decompressed { + let decompressed_config = decompressed_mint_config.unwrap(); + MintToCompressedMetaConfig::new_decompressed( + mint_authority, + payer, + state_merkle_tree, + output_queue, + state_tree_pubkey, + state_tree_pubkey, // compressed_mint_tree + output_queue, // compressed_mint_queue + decompressed_config.mint_pda, + decompressed_config.token_pool_pda, + decompressed_config.token_program, + has_sol_pool, + ) + } else { + MintToCompressedMetaConfig::new( + mint_authority, + payer, + state_merkle_tree, + output_queue, + state_tree_pubkey, + state_tree_pubkey, // compressed_mint_tree + output_queue, // compressed_mint_queue + has_sol_pool, + ) + }; + + // Get account metas using the SDK function + let accounts = get_mint_to_compressed_instruction_account_metas(meta_config); + + // Serialize instruction data + let data_vec = mint_to_instruction_data + .try_to_vec() + .map_err(|_| TokenSdkError::SerializationError)?; + + Ok(Instruction { + program_id: Pubkey::from(COMPRESSED_TOKEN_PROGRAM_ID), + accounts, + data: [vec![MINT_TO_COMPRESSED_DISCRIMINATOR], data_vec].concat(), + }) +} \ No newline at end of file diff --git a/sdk-libs/compressed-token-sdk/src/instructions/mint_to_compressed/mod.rs b/sdk-libs/compressed-token-sdk/src/instructions/mint_to_compressed/mod.rs new file mode 100644 index 0000000000..7338acec8b --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/mint_to_compressed/mod.rs @@ -0,0 +1,10 @@ +pub mod account_metas; +pub mod instruction; + +pub use account_metas::{ + get_mint_to_compressed_instruction_account_metas, MintToCompressedMetaConfig, +}; +pub use instruction::{ + create_mint_to_compressed_instruction, DecompressedMintConfig, MintToCompressedInputs, + MINT_TO_COMPRESSED_DISCRIMINATOR, +}; \ No newline at end of file diff --git a/sdk-libs/compressed-token-sdk/src/instructions/mod.rs b/sdk-libs/compressed-token-sdk/src/instructions/mod.rs new file mode 100644 index 0000000000..bb265f9f59 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/mod.rs @@ -0,0 +1,32 @@ +pub mod approve; +pub mod batch_compress; +pub mod close; +pub mod create_associated_token_account; +pub mod create_compressed_mint; +mod create_spl_mint; +pub mod create_token_account; +pub mod ctoken_accounts; +pub mod mint_to_compressed; +pub mod transfer; +pub mod transfer2; + +// Re-export all instruction utilities +pub use approve::{ + approve, create_approve_instruction, get_approve_instruction_account_metas, ApproveInputs, + ApproveMetaConfig, +}; +pub use batch_compress::{ + create_batch_compress_instruction, get_batch_compress_instruction_account_metas, + BatchCompressInputs, BatchCompressMetaConfig, Recipient, +}; +pub use create_associated_token_account::*; +pub use create_compressed_mint::*; +pub use create_spl_mint::*; +pub use create_token_account::{ + create_compressible_token_account, create_token_account, CreateCompressibleTokenAccount, +}; +pub use ctoken_accounts::*; +pub use mint_to_compressed::{ + create_mint_to_compressed_instruction, get_mint_to_compressed_instruction_account_metas, + DecompressedMintConfig, MintToCompressedInputs, MintToCompressedMetaConfig, +}; diff --git a/sdk-libs/compressed-token-sdk/src/instructions/transfer/account_infos.rs b/sdk-libs/compressed-token-sdk/src/instructions/transfer/account_infos.rs new file mode 100644 index 0000000000..c67187d332 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/transfer/account_infos.rs @@ -0,0 +1,112 @@ +use arrayvec::ArrayVec; +use solana_account_info::AccountInfo; +use solana_instruction::Instruction; +use solana_msg::msg; + +use crate::{account::CTokenAccount, error::Result}; + +pub const MAX_ACCOUNT_INFOS: usize = 20; + +// TODO: test with delegate +// For pinocchio we will need to build the accounts in oder +// The easiest is probably just pass the accounts multiple times since deserialization is zero copy. +pub struct TransferAccountInfos<'a, 'info, const N: usize = MAX_ACCOUNT_INFOS> { + pub fee_payer: &'a AccountInfo<'info>, + pub authority: &'a AccountInfo<'info>, + pub ctoken_accounts: &'a [AccountInfo<'info>], + pub cpi_context: Option<&'a AccountInfo<'info>>, + // TODO: rename tree accounts to packed accounts + pub packed_accounts: &'a [AccountInfo<'info>], +} + +impl<'info, const N: usize> TransferAccountInfos<'_, 'info, N> { + // 874 with std::vec + // 722 with array vec + pub fn into_account_infos(self) -> ArrayVec, N> { + let mut capacity = 2 + self.ctoken_accounts.len() + self.packed_accounts.len(); + let ctoken_program_id_index = self.ctoken_accounts.len() - 2; + if self.cpi_context.is_some() { + capacity += 1; + } + + // Check if capacity exceeds ArrayVec limit + if capacity > N { + panic!("Account infos capacity {} exceeds limit {}", capacity, N); + } + + let mut account_infos = ArrayVec::, N>::new(); + account_infos.push(self.fee_payer.clone()); + account_infos.push(self.authority.clone()); + + // Add ctoken accounts + for account in self.ctoken_accounts { + account_infos.push(account.clone()); + } + + if let Some(cpi_context) = self.cpi_context { + account_infos.push(cpi_context.clone()); + } else { + account_infos.push(self.ctoken_accounts[ctoken_program_id_index].clone()); + } + + // Add tree accounts + for account in self.packed_accounts { + account_infos.push(account.clone()); + } + + account_infos + } + + // 1528 + pub fn into_account_infos_checked( + self, + ix: &Instruction, + ) -> Result, N>> { + let account_infos = self.into_account_infos(); + for (account_meta, account_info) in ix.accounts.iter().zip(account_infos.iter()) { + if account_meta.pubkey != *account_info.key { + msg!("account meta {:?}", account_meta); + msg!("account info {:?}", account_info); + + msg!("account metas {:?}", ix.accounts); + msg!("account infos {:?}", account_infos); + panic!("account info and meta don't match."); + } + } + Ok(account_infos) + } +} + +// Note: maybe it is not useful for removing accounts results in loss of order +// other than doing [..end] so let's just do that in the first place. +// TODO: test +/// Filter packed accounts for accounts necessary for token accounts. +/// Note accounts still need to be in the correct order. +pub fn filter_packed_accounts<'info>( + token_accounts: &[&CTokenAccount], + account_infos: &[AccountInfo<'info>], +) -> Vec> { + let mut selected_account_infos = Vec::with_capacity(account_infos.len()); + account_infos + .iter() + .enumerate() + .filter(|(i, _)| { + let i = *i as u8; + token_accounts.iter().any(|y| { + y.merkle_tree_index == i + || y.input_metas().iter().any(|z| { + z.packed_tree_info.merkle_tree_pubkey_index == i + || z.packed_tree_info.queue_pubkey_index == i + || { + if let Some(delegate_index) = z.delegate_index { + delegate_index == i + } else { + false + } + } + }) + }) + }) + .for_each(|x| selected_account_infos.push(x.1.clone())); + selected_account_infos +} diff --git a/sdk-libs/compressed-token-sdk/src/instructions/transfer/account_metas.rs b/sdk-libs/compressed-token-sdk/src/instructions/transfer/account_metas.rs new file mode 100644 index 0000000000..b1749c0fbc --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/transfer/account_metas.rs @@ -0,0 +1,221 @@ +use solana_instruction::AccountMeta; +use solana_pubkey::Pubkey; + +use crate::instructions::CTokenDefaultAccounts; + +/// Account metadata configuration for compressed token instructions +#[derive(Debug, Default, Copy, Clone)] +pub struct TokenAccountsMetaConfig { + pub fee_payer: Option, + pub authority: Option, + pub token_pool_pda: Option, + pub compress_or_decompress_token_account: Option, + pub token_program: Option, + pub is_compress: bool, + pub is_decompress: bool, + pub with_anchor_none: bool, +} + +impl TokenAccountsMetaConfig { + pub fn new(fee_payer: Pubkey, authority: Pubkey) -> Self { + Self { + fee_payer: Some(fee_payer), + authority: Some(authority), + token_pool_pda: None, + compress_or_decompress_token_account: None, + token_program: None, + is_compress: false, + is_decompress: false, + with_anchor_none: false, + } + } + + pub fn new_client() -> Self { + Self { + fee_payer: None, + authority: None, + token_pool_pda: None, + compress_or_decompress_token_account: None, + token_program: None, + is_compress: false, + is_decompress: false, + with_anchor_none: false, + } + } + + pub fn new_with_anchor_none() -> Self { + Self { + fee_payer: None, + authority: None, + token_pool_pda: None, + compress_or_decompress_token_account: None, + token_program: None, + is_compress: false, + is_decompress: false, + with_anchor_none: true, + } + } + + pub fn compress( + fee_payer: Pubkey, + authority: Pubkey, + token_pool_pda: Pubkey, + sender_token_account: Pubkey, + spl_program_id: Pubkey, + ) -> Self { + // TODO: derive token_pool_pda here and pass mint instead. + Self { + fee_payer: Some(fee_payer), + authority: Some(authority), + token_pool_pda: Some(token_pool_pda), + compress_or_decompress_token_account: Some(sender_token_account), + token_program: Some(spl_program_id), + is_compress: true, + is_decompress: false, + with_anchor_none: false, + } + } + + pub fn compress_client( + token_pool_pda: Pubkey, + sender_token_account: Pubkey, + spl_program_id: Pubkey, + ) -> Self { + Self { + fee_payer: None, + authority: None, + token_pool_pda: Some(token_pool_pda), + compress_or_decompress_token_account: Some(sender_token_account), + token_program: Some(spl_program_id), + is_compress: true, + is_decompress: false, + with_anchor_none: false, + } + } + + pub fn decompress( + fee_payer: Pubkey, + authority: Pubkey, + token_pool_pda: Pubkey, + recipient_token_account: Pubkey, + spl_program_id: Pubkey, + ) -> Self { + Self { + fee_payer: Some(fee_payer), + authority: Some(authority), + token_pool_pda: Some(token_pool_pda), + compress_or_decompress_token_account: Some(recipient_token_account), + token_program: Some(spl_program_id), + is_compress: false, + is_decompress: true, + with_anchor_none: false, + } + } + + pub fn decompress_client( + token_pool_pda: Pubkey, + recipient_token_account: Pubkey, + spl_program_id: Pubkey, + ) -> Self { + Self { + fee_payer: None, + authority: None, + token_pool_pda: Some(token_pool_pda), + compress_or_decompress_token_account: Some(recipient_token_account), + token_program: Some(spl_program_id), + is_compress: false, + is_decompress: true, + with_anchor_none: false, + } + } + + pub fn is_compress_or_decompress(&self) -> bool { + self.is_compress || self.is_decompress + } +} + +/// Get the standard account metas for a compressed token transfer instruction +pub fn get_transfer_instruction_account_metas(config: TokenAccountsMetaConfig) -> Vec { + let default_pubkeys = CTokenDefaultAccounts::default(); + // Direct invoke adds fee_payer, and authority + let mut metas = if let Some(fee_payer) = config.fee_payer { + let authority = if let Some(authority) = config.authority { + authority + } else { + panic!("Missing authority"); + }; + vec![ + AccountMeta::new(fee_payer, true), + AccountMeta::new_readonly(authority, true), + // cpi_authority_pda + AccountMeta::new_readonly(default_pubkeys.cpi_authority_pda, false), + // light_system_program + AccountMeta::new_readonly(default_pubkeys.light_system_program, false), + // registered_program_pda + AccountMeta::new_readonly(default_pubkeys.registered_program_pda, false), + // noop_program + AccountMeta::new_readonly(default_pubkeys.noop_program, false), + // account_compression_authority + AccountMeta::new_readonly(default_pubkeys.account_compression_authority, false), + // account_compression_program + AccountMeta::new_readonly(default_pubkeys.account_compression_program, false), + // self_program (compressed token program) + AccountMeta::new_readonly(default_pubkeys.self_program, false), + ] + } else { + vec![ + // cpi_authority_pda + AccountMeta::new_readonly(default_pubkeys.cpi_authority_pda, false), + // light_system_program + AccountMeta::new_readonly(default_pubkeys.light_system_program, false), + // registered_program_pda + AccountMeta::new_readonly(default_pubkeys.registered_program_pda, false), + // noop_program + AccountMeta::new_readonly(default_pubkeys.noop_program, false), + // account_compression_authority + AccountMeta::new_readonly(default_pubkeys.account_compression_authority, false), + // account_compression_program + AccountMeta::new_readonly(default_pubkeys.account_compression_program, false), + // self_program (compressed token program) + AccountMeta::new_readonly(default_pubkeys.self_program, false), + ] + }; + + // Optional token pool PDA (for compression/decompression) + if let Some(token_pool_pda) = config.token_pool_pda { + metas.push(AccountMeta::new(token_pool_pda, false)); + } else if config.fee_payer.is_some() || config.with_anchor_none { + metas.push(AccountMeta::new_readonly( + default_pubkeys.compressed_token_program, + false, + )); + } + println!("config.with_anchor_none {}", config.with_anchor_none); + // Optional compress/decompress token account + if let Some(token_account) = config.compress_or_decompress_token_account { + metas.push(AccountMeta::new(token_account, false)); + } else if config.fee_payer.is_some() || config.with_anchor_none { + metas.push(AccountMeta::new_readonly( + default_pubkeys.compressed_token_program, + false, + )); + } + + // Optional token program + if let Some(token_program) = config.token_program { + metas.push(AccountMeta::new_readonly(token_program, false)); + } else if config.fee_payer.is_some() || config.with_anchor_none { + metas.push(AccountMeta::new_readonly( + default_pubkeys.compressed_token_program, + false, + )); + } + + // system_program (always last) + metas.push(AccountMeta::new_readonly( + default_pubkeys.system_program, + false, + )); + + metas +} diff --git a/sdk-libs/compressed-token-sdk/src/instructions/transfer/instruction.rs b/sdk-libs/compressed-token-sdk/src/instructions/transfer/instruction.rs new file mode 100644 index 0000000000..d84cf9ddd4 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/transfer/instruction.rs @@ -0,0 +1,282 @@ +use light_compressed_token_types::{ + constants::TRANSFER, instruction::transfer::CompressedTokenInstructionDataTransfer, + CompressedCpiContext, ValidityProof, +}; +use light_ctoken_types::COMPRESSED_TOKEN_PROGRAM_ID; +use solana_instruction::{AccountMeta, Instruction}; +use solana_pubkey::Pubkey; + +use crate::{ + account::CTokenAccount, + error::{Result, TokenSdkError}, + instructions::transfer::account_metas::{ + get_transfer_instruction_account_metas, TokenAccountsMetaConfig, + }, + AnchorSerialize, +}; +// CTokenAccount abstraction to bundle inputs and create outputs. +// Users don't really need to interact with this struct directly. +// Counter point for an anchor like TokenAccount we need the CTokenAccount +// +// Rename TokenAccountMeta -> TokenAccountMeta +// + +// We should have a create instruction function that works onchain and offchain. +// - account infos don't belong into the create instruction function. +// One difference between spl and compressed token program is that you don't want to make a separate cpi per transfer. +// -> transfer(from, to, amount) doesn't work well +// - +// -> compress(token_account, Option) could be compressed token account +// -> decompress() +// TODO: +// - test decompress and compress in the same instruction + +#[derive(Debug, Default, PartialEq, Copy, Clone)] +pub struct TransferConfig { + pub cpi_context_pubkey: Option, + pub cpi_context: Option, + pub with_transaction_hash: bool, + pub filter_zero_amount_outputs: bool, +} + +/// Create instruction function should only take Pubkeys as inputs not account infos. +/// +/// Create the instruction for compressed token operations +pub fn create_transfer_instruction_raw( + mint: Pubkey, + token_accounts: Vec, + validity_proof: ValidityProof, + transfer_config: TransferConfig, + meta_config: TokenAccountsMetaConfig, + tree_pubkeys: Vec, +) -> Result { + // Determine if this is a compress operation by checking any token account + let is_compress = token_accounts.iter().any(|acc| acc.is_compress()); + let is_decompress = token_accounts.iter().any(|acc| acc.is_decompress()); + + let mut compress_or_decompress_amount: Option = None; + for acc in token_accounts.iter() { + if let Some(amount) = acc.compression_amount() { + if let Some(compress_or_decompress_amount) = compress_or_decompress_amount.as_mut() { + (*compress_or_decompress_amount) += amount; + } else { + compress_or_decompress_amount = Some(amount); + } + } + } + + // Check 1: cpi accounts must be decompress or compress consistent with accounts + if (is_compress && !meta_config.is_compress) || (is_decompress && !meta_config.is_decompress) { + return Err(TokenSdkError::InconsistentCompressDecompressState); + } + + // Check 2: there can only be compress or decompress not both + if is_compress && is_decompress { + return Err(TokenSdkError::BothCompressAndDecompress); + } + + // Check 3: compress_or_decompress_amount must be Some + if compress_or_decompress_amount.is_none() && meta_config.is_compress_or_decompress() { + return Err(TokenSdkError::InvalidCompressDecompressAmount); + } + + // Extract input and output data from token accounts + let mut input_token_data_with_context = Vec::new(); + let mut output_compressed_accounts = Vec::new(); + + for token_account in token_accounts { + let (inputs, output) = token_account.into_inputs_and_outputs(); + for input in inputs { + input_token_data_with_context.push(input.into()); + } + if output.amount == 0 && transfer_config.filter_zero_amount_outputs { + } else { + output_compressed_accounts.push(output); + } + } + + // Create instruction data + let instruction_data = CompressedTokenInstructionDataTransfer { + proof: validity_proof.into(), + mint: mint.to_bytes(), + input_token_data_with_context, + output_compressed_accounts, + is_compress, + compress_or_decompress_amount, + cpi_context: transfer_config.cpi_context, + with_transaction_hash: transfer_config.with_transaction_hash, + delegated_transfer: None, // TODO: support in separate pr + lamports_change_account_merkle_tree_index: None, // TODO: support in separate pr + }; + + // TODO: calculate exact len. + let serialized = instruction_data + .try_to_vec() + .map_err(|_| TokenSdkError::SerializationError)?; + + // Serialize instruction data + let mut data = Vec::with_capacity(8 + 4 + serialized.len()); // rough estimate + data.extend_from_slice(&TRANSFER); + data.extend(u32::try_from(serialized.len()).unwrap().to_le_bytes()); + data.extend(serialized); + let mut account_metas = get_transfer_instruction_account_metas(meta_config); + if let Some(cpi_context_pubkey) = transfer_config.cpi_context_pubkey { + if transfer_config.cpi_context.is_some() { + account_metas.push(AccountMeta::new(cpi_context_pubkey, false)); + } else { + // TODO: throw error + panic!("cpi_context.is_none() but transfer_config.cpi_context_pubkey is some"); + } + } + + // let account_metas = to_compressed_token_account_metas(cpi_accounts)?; + for tree_pubkey in tree_pubkeys { + account_metas.push(AccountMeta::new(tree_pubkey, false)); + } + Ok(Instruction { + program_id: Pubkey::from(COMPRESSED_TOKEN_PROGRAM_ID), + accounts: account_metas, + data, + }) +} + +pub struct CompressInputs { + pub fee_payer: Pubkey, + pub authority: Pubkey, + pub mint: Pubkey, + pub recipient: Pubkey, + pub output_tree_index: u8, + pub sender_token_account: Pubkey, + pub amount: u64, + // pub output_queue_pubkey: Pubkey, + pub token_pool_pda: Pubkey, + pub transfer_config: Option, + pub spl_token_program: Pubkey, + pub tree_accounts: Vec, +} + +// TODO: consider adding compress to existing token accounts +// (effectively compress and merge) +// TODO: wrap batch compress instead. +pub fn compress(inputs: CompressInputs) -> Result { + let CompressInputs { + fee_payer, + authority, + mint, + recipient, + sender_token_account, + amount, + token_pool_pda, + transfer_config, + spl_token_program, + output_tree_index, + tree_accounts, + } = inputs; + let mut token_account = + crate::account::CTokenAccount::new_empty(mint, recipient, output_tree_index); + token_account.compress(amount).unwrap(); + solana_msg::msg!("spl_token_program {:?}", spl_token_program); + let config = transfer_config.unwrap_or_default(); + let meta_config = TokenAccountsMetaConfig::compress( + fee_payer, + authority, + token_pool_pda, + sender_token_account, + spl_token_program, + ); + create_transfer_instruction_raw( + mint, + vec![token_account], + ValidityProof::default(), + config, + meta_config, + tree_accounts, + ) +} + +#[derive(Debug, Clone, PartialEq)] +pub struct TransferInputs { + pub fee_payer: Pubkey, + pub validity_proof: ValidityProof, + pub sender_account: CTokenAccount, + pub amount: u64, + pub recipient: Pubkey, + pub tree_pubkeys: Vec, + pub config: Option, +} + +pub fn transfer(inputs: TransferInputs) -> Result { + let TransferInputs { + fee_payer, + validity_proof, + amount, + mut sender_account, + recipient, + tree_pubkeys, + config, + } = inputs; + // Sanity check. + if sender_account.method_used { + return Err(TokenSdkError::MethodUsed); + } + let account_meta_config = TokenAccountsMetaConfig::new(fee_payer, sender_account.owner()); + // None is the same output_tree_index as token account + let recipient_token_account = sender_account.transfer(&recipient, amount, None).unwrap(); + + create_transfer_instruction_raw( + *sender_account.mint(), + vec![recipient_token_account, sender_account], + validity_proof, + config.unwrap_or_default(), + account_meta_config, + tree_pubkeys, + ) +} + +#[derive(Debug, Clone, PartialEq)] +pub struct DecompressInputs { + pub fee_payer: Pubkey, + pub validity_proof: ValidityProof, + pub sender_account: CTokenAccount, + pub amount: u64, + pub tree_pubkeys: Vec, + pub config: Option, + pub token_pool_pda: Pubkey, + pub recipient_token_account: Pubkey, + pub spl_token_program: Pubkey, +} + +pub fn decompress(inputs: DecompressInputs) -> Result { + let DecompressInputs { + amount, + fee_payer, + validity_proof, + mut sender_account, + tree_pubkeys, + config, + token_pool_pda, + recipient_token_account, + spl_token_program, + } = inputs; + // Sanity check. + if sender_account.method_used { + return Err(TokenSdkError::MethodUsed); + } + let account_meta_config = TokenAccountsMetaConfig::decompress( + fee_payer, + sender_account.owner(), + token_pool_pda, + recipient_token_account, + spl_token_program, + ); + sender_account.decompress(amount).unwrap(); + + create_transfer_instruction_raw( + *sender_account.mint(), + vec![sender_account], + validity_proof, + config.unwrap_or_default(), + account_meta_config, + tree_pubkeys, + ) +} diff --git a/sdk-libs/compressed-token-sdk/src/instructions/transfer/mod.rs b/sdk-libs/compressed-token-sdk/src/instructions/transfer/mod.rs new file mode 100644 index 0000000000..aa39b7fcd3 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/transfer/mod.rs @@ -0,0 +1,8 @@ +use light_compressed_token_types::account_infos::TransferAccountInfos as TransferAccountInfosTypes; +use solana_account_info::AccountInfo; + +pub mod account_infos; +pub mod account_metas; +pub mod instruction; + +pub type TransferAccountInfos<'a, 'b> = TransferAccountInfosTypes<'a, AccountInfo<'b>>; diff --git a/sdk-libs/compressed-token-sdk/src/instructions/transfer2/account_metas.rs b/sdk-libs/compressed-token-sdk/src/instructions/transfer2/account_metas.rs new file mode 100644 index 0000000000..b3ff352e3b --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/transfer2/account_metas.rs @@ -0,0 +1,94 @@ +use light_compressed_token_types::CPI_AUTHORITY_PDA; +use light_sdk::constants::LIGHT_SYSTEM_PROGRAM_ID; +use solana_instruction::AccountMeta; +use solana_pubkey::Pubkey; + +use crate::instructions::CTokenDefaultAccounts; + +/// Account metadata configuration for compressed token multi-transfer instructions +#[derive(Debug, Default, Clone, PartialEq)] +pub struct Transfer2AccountsMetaConfig { + pub fee_payer: Option, + pub sol_pool_pda: Option, + pub sol_decompression_recipient: Option, + pub cpi_context: Option, + pub with_sol_pool: bool, + pub packed_accounts: Option>, // TODO: check whether this can ever be None +} + +impl Transfer2AccountsMetaConfig { + pub fn new(fee_payer: Pubkey, packed_accounts: Vec) -> Self { + Self { + fee_payer: Some(fee_payer), + sol_pool_pda: None, + sol_decompression_recipient: None, + cpi_context: None, + with_sol_pool: false, + packed_accounts: Some(packed_accounts), + } + } +} + +/// Get the standard account metas for a compressed token multi-transfer instruction +pub fn get_transfer2_instruction_account_metas( + config: Transfer2AccountsMetaConfig, +) -> Vec { + let default_pubkeys = CTokenDefaultAccounts::default(); + let packed_accounts_len = if let Some(packed_accounts) = config.packed_accounts.as_ref() { + packed_accounts.len() + } else { + 0 + }; + + // Build the account metas following the order expected by Transfer2ValidatedAccounts + let mut metas = Vec::with_capacity(10 + packed_accounts_len); + metas.push(AccountMeta::new_readonly( + Pubkey::new_from_array(LIGHT_SYSTEM_PROGRAM_ID), + false, + )); + // Add fee payer and authority if provided (for direct invoke) + if let Some(fee_payer) = config.fee_payer { + metas.push(AccountMeta::new(fee_payer, true)); + } + + // Core system accounts (always present) + metas.extend([ + AccountMeta::new_readonly(Pubkey::new_from_array(CPI_AUTHORITY_PDA), false), + // registered_program_pda + AccountMeta::new_readonly(default_pubkeys.registered_program_pda, false), + // noop_program + AccountMeta::new_readonly(default_pubkeys.noop_program, false), + // account_compression_authority + AccountMeta::new_readonly(default_pubkeys.account_compression_authority, false), + // account_compression_program + AccountMeta::new_readonly(default_pubkeys.account_compression_program, false), + // invoking_program (self program) + AccountMeta::new_readonly(default_pubkeys.self_program, false), + ]); + + // Optional sol pool accounts + if config.with_sol_pool { + if let Some(sol_pool_pda) = config.sol_pool_pda { + metas.push(AccountMeta::new(sol_pool_pda, false)); + } + if let Some(sol_decompression_recipient) = config.sol_decompression_recipient { + metas.push(AccountMeta::new(sol_decompression_recipient, false)); + } + } + + // system_program (always present) + metas.push(AccountMeta::new_readonly( + default_pubkeys.system_program, + false, + )); + if let Some(cpi_context) = config.cpi_context { + metas.push(AccountMeta::new(cpi_context, false)); + } + if let Some(packed_accounts) = config.packed_accounts.as_ref() { + for account in packed_accounts { + metas.push(account.clone()); + } + } + + metas +} diff --git a/sdk-libs/compressed-token-sdk/src/instructions/transfer2/instruction.rs b/sdk-libs/compressed-token-sdk/src/instructions/transfer2/instruction.rs new file mode 100644 index 0000000000..2052c8d896 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/transfer2/instruction.rs @@ -0,0 +1,196 @@ +use light_compressed_token_types::{constants::TRANSFER2, CompressedCpiContext, ValidityProof}; +use light_ctoken_types::{ + instructions::transfer2::CompressedTokenInstructionDataTransfer2, COMPRESSED_TOKEN_PROGRAM_ID, +}; +use solana_instruction::{AccountMeta, Instruction}; +use solana_pubkey::Pubkey; + +use crate::{ + account2::CTokenAccount2, + error::{Result, TokenSdkError}, + instructions::transfer2::account_metas::{ + get_transfer2_instruction_account_metas, Transfer2AccountsMetaConfig, + }, + AnchorSerialize, +}; + +#[derive(Debug, Default, PartialEq, Copy, Clone)] +pub struct Transfer2Config { + pub cpi_context_pubkey: Option, + pub cpi_context: Option, + pub with_transaction_hash: bool, + pub sol_pool_pda: bool, + pub sol_decompression_recipient: Option, + pub filter_zero_amount_outputs: bool, +} + +impl Transfer2Config { + pub fn new() -> Self { + Default::default() + } + + pub fn with_cpi_context( + mut self, + cpi_context_pubkey: Pubkey, + cpi_context: CompressedCpiContext, + ) -> Self { + self.cpi_context_pubkey = Some(cpi_context_pubkey); + self.cpi_context = Some(cpi_context); + self + } + + pub fn with_transaction_hash(mut self) -> Self { + self.with_transaction_hash = true; + self + } + + pub fn with_sol_pool(mut self, sol_decompression_recipient: Pubkey) -> Self { + self.sol_pool_pda = true; + self.sol_decompression_recipient = Some(sol_decompression_recipient); + self + } + + pub fn filter_zero_amount_outputs(mut self) -> Self { + self.filter_zero_amount_outputs = true; + self + } +} + +/// Multi-transfer input parameters +#[derive(Debug, Clone, PartialEq, Default)] +pub struct Transfer2Inputs { + pub token_accounts: Vec, + pub validity_proof: ValidityProof, + pub transfer_config: Transfer2Config, + pub meta_config: Transfer2AccountsMetaConfig, + // pub tree_pubkeys: Vec, + // pub packed_pubkeys: Vec, // Owners, Delegates, Mints + pub in_lamports: Option>, + pub out_lamports: Option>, +} + +/// Create the instruction for compressed token multi-transfer operations +pub fn create_transfer2_instruction(inputs: Transfer2Inputs) -> Result { + let Transfer2Inputs { + token_accounts, + validity_proof, + transfer_config, + meta_config, + in_lamports, + out_lamports, + } = inputs; + let mut input_token_data_with_context = Vec::new(); + let mut output_compressed_accounts = Vec::new(); + let mut collected_compressions = Vec::new(); + + // Process each token account and convert to multi-transfer format + for token_account in token_accounts { + // Collect compression if present + if let Some(compression) = token_account.compression() { + collected_compressions.push(*compression); + } + let (inputs, output) = token_account.into_inputs_and_outputs(); + + // Collect inputs directly (they're already in the right format) + input_token_data_with_context.extend(inputs); + + // Add output if not zero amount (when filtering is enabled) + if !transfer_config.filter_zero_amount_outputs || output.amount > 0 { + output_compressed_accounts.push(output); + } + } + + // Create instruction data + let instruction_data = CompressedTokenInstructionDataTransfer2 { + with_transaction_hash: transfer_config.with_transaction_hash, + with_lamports_change_account_merkle_tree_index: false, // TODO: support in future + lamports_change_account_merkle_tree_index: 0, + lamports_change_account_owner_index: 0, + proof: validity_proof.into(), + in_token_data: input_token_data_with_context, + out_token_data: output_compressed_accounts, + in_lamports, + out_lamports, + in_tlv: None, // TLV is unimplemented + out_tlv: None, // TLV is unimplemented + compressions: if collected_compressions.is_empty() { + None + } else { + Some(collected_compressions) + }, + cpi_context: transfer_config.cpi_context, + }; + + // Serialize instruction data + let serialized = instruction_data + .try_to_vec() + .map_err(|_| TokenSdkError::SerializationError)?; + + // Build instruction data with discriminator + let mut data = Vec::with_capacity(1 + serialized.len()); + data.push(TRANSFER2); + data.extend(serialized); + + // Get account metas + let mut account_metas = get_transfer2_instruction_account_metas(meta_config); + + // Add CPI context account if configured + if let Some(cpi_context_pubkey) = transfer_config.cpi_context_pubkey { + if transfer_config.cpi_context.is_some() { + account_metas.push(AccountMeta::new(cpi_context_pubkey, false)); + } + } + + // Moved assignment to account meta config + // Add tree accounts first + //for tree_pubkey in tree_pubkeys { + // account_metas.push(AccountMeta::new(tree_pubkey, false)); + // } + // Add packed accounts second + // for packed_pubkey in packed_pubkeys { + // account_metas.push(AccountMeta::new(packed_pubkey, false)); + // } + + Ok(Instruction { + program_id: Pubkey::from(COMPRESSED_TOKEN_PROGRAM_ID), + accounts: account_metas, + data, + }) +} + +/* +/// Create a multi-transfer instruction +pub fn transfer2(inputs: create_transfer2_instruction) -> Result { + let create_transfer2_instruction { + fee_payer, + authority, + validity_proof, + token_accounts, + tree_pubkeys, + config, + } = inputs; + + // Validate that no token account has been used + for token_account in &token_accounts { + if token_account.method_used { + return Err(TokenSdkError::MethodUsed); + } + } + + let config = config.unwrap_or_default(); + let meta_config = Transfer2AccountsMetaConfig::new(fee_payer, authority) + .with_sol_pool( + config.sol_pool_pda.unwrap_or_default(), + config.sol_decompression_recipient.unwrap_or_default(), + ) + .with_cpi_context(); + + create_transfer2_instruction( + token_accounts, + validity_proof, + config, + meta_config, + tree_pubkeys, + ) +} +*/ diff --git a/sdk-libs/compressed-token-sdk/src/instructions/transfer2/mod.rs b/sdk-libs/compressed-token-sdk/src/instructions/transfer2/mod.rs new file mode 100644 index 0000000000..5ca887aa04 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/instructions/transfer2/mod.rs @@ -0,0 +1,4 @@ +pub mod account_metas; +pub mod instruction; + +pub use instruction::*; diff --git a/sdk-libs/compressed-token-sdk/src/lib.rs b/sdk-libs/compressed-token-sdk/src/lib.rs new file mode 100644 index 0000000000..7e847b8965 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/lib.rs @@ -0,0 +1,13 @@ +pub mod account; +pub mod account2; +pub mod error; +pub mod instructions; +pub mod token_pool; +pub mod utils; + +// Conditional anchor re-exports +#[cfg(feature = "anchor")] +use anchor_lang::{AnchorDeserialize, AnchorSerialize}; +#[cfg(not(feature = "anchor"))] +use borsh::{BorshDeserialize as AnchorDeserialize, BorshSerialize as AnchorSerialize}; +pub use light_compressed_token_types::*; diff --git a/sdk-libs/compressed-token-sdk/src/token_pool.rs b/sdk-libs/compressed-token-sdk/src/token_pool.rs new file mode 100644 index 0000000000..605706100d --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/token_pool.rs @@ -0,0 +1,21 @@ +use light_compressed_token_types::constants::POOL_SEED; +use light_ctoken_types::COMPRESSED_TOKEN_PROGRAM_ID; +use solana_pubkey::Pubkey; + +pub fn get_token_pool_pda(mint: &Pubkey) -> Pubkey { + get_token_pool_pda_with_index(mint, 0) +} + +pub fn find_token_pool_pda_with_index(mint: &Pubkey, token_pool_index: u8) -> (Pubkey, u8) { + let seeds = &[POOL_SEED, mint.as_ref(), &[token_pool_index]]; + let seeds = if token_pool_index == 0 { + &seeds[..2] + } else { + &seeds[..] + }; + Pubkey::find_program_address(seeds, &Pubkey::from(COMPRESSED_TOKEN_PROGRAM_ID)) +} + +pub fn get_token_pool_pda_with_index(mint: &Pubkey, token_pool_index: u8) -> Pubkey { + find_token_pool_pda_with_index(mint, token_pool_index).0 +} diff --git a/sdk-libs/compressed-token-sdk/src/utils.rs b/sdk-libs/compressed-token-sdk/src/utils.rs new file mode 100644 index 0000000000..b8d3050649 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/src/utils.rs @@ -0,0 +1,18 @@ +use solana_account_info::AccountInfo; +use spl_pod::bytemuck::pod_from_bytes; +use spl_token_2022::pod::PodAccount; + +use crate::error::TokenSdkError; + +/// Get token account balance from account info +pub fn get_token_account_balance(token_account_info: &AccountInfo) -> Result { + let token_account_data = token_account_info + .try_borrow_data() + .map_err(|_| TokenSdkError::AccountBorrowFailed)?; + + // Use zero-copy PodAccount to access the token account + let pod_account = pod_from_bytes::(&token_account_data) + .map_err(|_| TokenSdkError::InvalidAccountData)?; + + Ok(pod_account.amount.into()) +} diff --git a/sdk-libs/compressed-token-sdk/tests/account_metas_test.rs b/sdk-libs/compressed-token-sdk/tests/account_metas_test.rs new file mode 100644 index 0000000000..bc66f88d95 --- /dev/null +++ b/sdk-libs/compressed-token-sdk/tests/account_metas_test.rs @@ -0,0 +1,129 @@ +use anchor_lang::ToAccountMetas; +use light_compressed_token_sdk::instructions::{ + batch_compress::{get_batch_compress_instruction_account_metas, BatchCompressMetaConfig}, + transfer::account_metas::{get_transfer_instruction_account_metas, TokenAccountsMetaConfig}, + CTokenDefaultAccounts, +}; +use light_compressed_token_types::constants::{ + ACCOUNT_COMPRESSION_PROGRAM_ID, CPI_AUTHORITY_PDA, LIGHT_SYSTEM_PROGRAM_ID, NOOP_PROGRAM_ID, + PROGRAM_ID as COMPRESSED_TOKEN_PROGRAM_ID, +}; +use light_sdk::constants::REGISTERED_PROGRAM_PDA; +use solana_pubkey::Pubkey; + +// TODO: Rewrite to use get_transfer_instruction_account_metas +#[test] +fn test_to_compressed_token_account_metas_compress() { + // Create test accounts + let fee_payer = Pubkey::new_unique(); + let authority = Pubkey::new_unique(); + + let default_pubkeys = CTokenDefaultAccounts::default(); + let reference = light_compressed_token::accounts::TransferInstruction { + fee_payer, + authority, + registered_program_pda: default_pubkeys.registered_program_pda, + noop_program: default_pubkeys.noop_program, + account_compression_authority: default_pubkeys.account_compression_authority, + account_compression_program: default_pubkeys.account_compression_program, + self_program: default_pubkeys.self_program, + cpi_authority_pda: default_pubkeys.cpi_authority_pda, + light_system_program: default_pubkeys.light_system_program, + token_pool_pda: None, + compress_or_decompress_token_account: None, + token_program: None, + system_program: default_pubkeys.system_program, + }; + + // Test our function + let meta_config = TokenAccountsMetaConfig::new(fee_payer, authority); + let account_metas = get_transfer_instruction_account_metas(meta_config); + let reference_metas = reference.to_account_metas(Some(true)); + + assert_eq!(account_metas, reference_metas); +} + +#[test] +fn test_to_compressed_token_account_metas_with_optional_accounts() { + // Create test accounts + let fee_payer = Pubkey::new_unique(); + let authority = Pubkey::new_unique(); + + // Optional accounts + let token_pool_pda = Pubkey::new_unique(); + let compress_or_decompress_token_account = Pubkey::new_unique(); + let spl_token_program = Pubkey::new_unique(); + + let default_pubkeys = CTokenDefaultAccounts::default(); + let reference = light_compressed_token::accounts::TransferInstruction { + fee_payer, + authority, + light_system_program: default_pubkeys.light_system_program, + cpi_authority_pda: default_pubkeys.cpi_authority_pda, + registered_program_pda: default_pubkeys.registered_program_pda, + noop_program: default_pubkeys.noop_program, + account_compression_authority: default_pubkeys.account_compression_authority, + account_compression_program: default_pubkeys.account_compression_program, + self_program: default_pubkeys.self_program, + token_pool_pda: Some(token_pool_pda), + compress_or_decompress_token_account: Some(compress_or_decompress_token_account), + token_program: Some(spl_token_program), + system_program: default_pubkeys.system_program, + }; + + let meta_config = TokenAccountsMetaConfig::compress( + fee_payer, + authority, + reference.token_pool_pda.unwrap(), + reference.compress_or_decompress_token_account.unwrap(), + reference.token_program.unwrap(), + ); + let account_metas = get_transfer_instruction_account_metas(meta_config); + let reference_metas = reference.to_account_metas(Some(true)); + + assert_eq!(account_metas, reference_metas); +} + +#[test] +fn test_get_batch_compress_instruction_account_metas() { + let fee_payer = Pubkey::new_unique(); + let authority = Pubkey::new_unique(); + let token_pool_pda = Pubkey::new_unique(); + let sender_token_account = Pubkey::new_unique(); + let token_program = Pubkey::new_unique(); + let merkle_tree = Pubkey::new_unique(); + + let config = BatchCompressMetaConfig::new( + fee_payer, + authority, + token_pool_pda, + sender_token_account, + token_program, + merkle_tree, + false, + ); + let default_pubkeys = CTokenDefaultAccounts::default(); + + let account_metas = get_batch_compress_instruction_account_metas(config); + + let reference = light_compressed_token::accounts::MintToInstruction { + fee_payer, + authority, + cpi_authority_pda: Pubkey::from(CPI_AUTHORITY_PDA), + mint: None, + token_pool_pda, + token_program, + light_system_program: Pubkey::from(LIGHT_SYSTEM_PROGRAM_ID), + registered_program_pda: Pubkey::from(REGISTERED_PROGRAM_PDA), + noop_program: Pubkey::from(NOOP_PROGRAM_ID), + account_compression_authority: default_pubkeys.account_compression_authority, + account_compression_program: Pubkey::from(ACCOUNT_COMPRESSION_PROGRAM_ID), + merkle_tree, + self_program: Pubkey::from(COMPRESSED_TOKEN_PROGRAM_ID), + system_program: Pubkey::default(), + sol_pool_pda: None, + }; + + let reference_metas = reference.to_account_metas(Some(true)); + assert_eq!(account_metas, reference_metas); +} diff --git a/sdk-libs/compressed-token-types/Cargo.toml b/sdk-libs/compressed-token-types/Cargo.toml new file mode 100644 index 0000000000..2a4617ff09 --- /dev/null +++ b/sdk-libs/compressed-token-types/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "light-compressed-token-types" +version = "0.1.0" +edition = "2021" + +[features] +anchor = [ + "anchor-lang", + "light-compressed-account/anchor", + "light-sdk-types/anchor", +] + +[dependencies] +borsh = { workspace = true } +light-macros = { workspace = true } +anchor-lang = { workspace = true, optional = true } +light-sdk-types = { workspace = true } +light-account-checks = { workspace = true } +light-compressed-account = { workspace = true } +thiserror = { workspace = true } +solana-msg = { workspace = true } diff --git a/sdk-libs/compressed-token-types/src/account_infos/batch_compress.rs b/sdk-libs/compressed-token-types/src/account_infos/batch_compress.rs new file mode 100644 index 0000000000..6d39da035a --- /dev/null +++ b/sdk-libs/compressed-token-types/src/account_infos/batch_compress.rs @@ -0,0 +1,192 @@ +use light_account_checks::AccountInfoTrait; + +use crate::{ + account_infos::MintToAccountInfosConfig, + error::{LightTokenSdkTypeError, Result}, +}; + +#[repr(usize)] +pub enum BatchCompressAccountInfosIndex { + // FeePayer, + // Authority, + CpiAuthorityPda, + TokenPoolPda, + TokenProgram, + LightSystemProgram, + RegisteredProgramPda, + NoopProgram, + AccountCompressionAuthority, + AccountCompressionProgram, + MerkleTree, + SelfProgram, + SystemProgram, + SolPoolPda, + SenderTokenAccount, +} + +pub struct BatchCompressAccountInfos<'a, T: AccountInfoTrait + Clone> { + fee_payer: &'a T, + authority: &'a T, + accounts: &'a [T], + config: MintToAccountInfosConfig, +} + +impl<'a, T: AccountInfoTrait + Clone> BatchCompressAccountInfos<'a, T> { + pub fn new(fee_payer: &'a T, authority: &'a T, accounts: &'a [T]) -> Self { + Self { + fee_payer, + authority, + accounts, + config: MintToAccountInfosConfig::new_batch_compress(), + } + } + + pub fn new_with_config( + fee_payer: &'a T, + authority: &'a T, + accounts: &'a [T], + config: MintToAccountInfosConfig, + ) -> Self { + Self { + fee_payer, + authority, + accounts, + config, + } + } + + pub fn fee_payer(&self) -> &'a T { + self.fee_payer + } + + pub fn authority(&self) -> &'a T { + self.authority + } + + pub fn cpi_authority_pda(&self) -> Result<&'a T> { + let index = BatchCompressAccountInfosIndex::CpiAuthorityPda as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn token_pool_pda(&self) -> Result<&'a T> { + let index = BatchCompressAccountInfosIndex::TokenPoolPda as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn token_program(&self) -> Result<&'a T> { + let index = BatchCompressAccountInfosIndex::TokenProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn light_system_program(&self) -> Result<&'a T> { + let index = BatchCompressAccountInfosIndex::LightSystemProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn registered_program_pda(&self) -> Result<&'a T> { + let index = BatchCompressAccountInfosIndex::RegisteredProgramPda as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn noop_program(&self) -> Result<&'a T> { + let index = BatchCompressAccountInfosIndex::NoopProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn account_compression_authority(&self) -> Result<&'a T> { + let index = BatchCompressAccountInfosIndex::AccountCompressionAuthority as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn account_compression_program(&self) -> Result<&'a T> { + let index = BatchCompressAccountInfosIndex::AccountCompressionProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn merkle_tree(&self) -> Result<&'a T> { + let index = BatchCompressAccountInfosIndex::MerkleTree as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn self_program(&self) -> Result<&'a T> { + let index = BatchCompressAccountInfosIndex::SelfProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn system_program(&self) -> Result<&'a T> { + let index = BatchCompressAccountInfosIndex::SystemProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn sol_pool_pda(&self) -> Result<&'a T> { + if !self.config.has_sol_pool_pda { + return Err(LightTokenSdkTypeError::SolPoolPdaUndefined); + } + let index = BatchCompressAccountInfosIndex::SolPoolPda as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn sender_token_account(&self) -> Result<&'a T> { + let mut index = BatchCompressAccountInfosIndex::SenderTokenAccount as usize; + if !self.config.has_sol_pool_pda { + index -= 1; + } + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn get_account_info(&self, index: usize) -> Result<&'a T> { + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + pub fn to_account_infos(&self) -> Vec { + [ + vec![self.fee_payer.clone()], + vec![self.authority.clone()], + self.accounts.to_vec(), + ] + .concat() + } + + pub fn account_infos(&self) -> &'a [T] { + self.accounts + } + + pub fn config(&self) -> &MintToAccountInfosConfig { + &self.config + } + + pub fn system_accounts_len(&self) -> usize { + let mut len = 13; // Base accounts from the enum (including sender_token_account) + if !self.config.has_sol_pool_pda { + len -= 1; // Remove sol_pool_pda if it's None + } + len + } +} diff --git a/sdk-libs/compressed-token-types/src/account_infos/burn.rs b/sdk-libs/compressed-token-types/src/account_infos/burn.rs new file mode 100644 index 0000000000..3f555c5e13 --- /dev/null +++ b/sdk-libs/compressed-token-types/src/account_infos/burn.rs @@ -0,0 +1,174 @@ +use light_account_checks::AccountInfoTrait; + +use crate::{ + error::{LightTokenSdkTypeError, Result}, + AnchorDeserialize, AnchorSerialize, +}; + +#[repr(usize)] +pub enum BurnAccountInfosIndex { + FeePayer, + Authority, + CpiAuthorityPda, + Mint, + TokenPoolPda, + TokenProgram, + LightSystemProgram, + RegisteredProgramPda, + NoopProgram, + AccountCompressionAuthority, + AccountCompressionProgram, + SelfProgram, + SystemProgram, +} + +pub struct BurnAccountInfos<'a, T: AccountInfoTrait + Clone> { + fee_payer: &'a T, + authority: &'a T, + accounts: &'a [T], + config: BurnAccountInfosConfig, +} + +#[derive(Debug, Default, Copy, Clone, AnchorSerialize, AnchorDeserialize)] +pub struct BurnAccountInfosConfig { + pub cpi_context: bool, +} + +impl BurnAccountInfosConfig { + pub const fn new() -> Self { + Self { cpi_context: false } + } + + pub const fn new_with_cpi_context() -> Self { + Self { cpi_context: true } + } +} + +impl<'a, T: AccountInfoTrait + Clone> BurnAccountInfos<'a, T> { + pub fn new(fee_payer: &'a T, authority: &'a T, accounts: &'a [T]) -> Self { + Self { + fee_payer, + authority, + accounts, + config: BurnAccountInfosConfig::new(), + } + } + + pub fn new_with_config( + fee_payer: &'a T, + authority: &'a T, + accounts: &'a [T], + config: BurnAccountInfosConfig, + ) -> Self { + Self { + fee_payer, + authority, + accounts, + config, + } + } + + pub fn fee_payer(&self) -> &'a T { + self.fee_payer + } + + pub fn authority(&self) -> &'a T { + self.authority + } + + pub fn cpi_authority_pda(&self) -> Result<&'a T> { + let index = BurnAccountInfosIndex::CpiAuthorityPda as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn mint(&self) -> Result<&'a T> { + let index = BurnAccountInfosIndex::Mint as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn token_pool_pda(&self) -> Result<&'a T> { + let index = BurnAccountInfosIndex::TokenPoolPda as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn token_program(&self) -> Result<&'a T> { + let index = BurnAccountInfosIndex::TokenProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn light_system_program(&self) -> Result<&'a T> { + let index = BurnAccountInfosIndex::LightSystemProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn registered_program_pda(&self) -> Result<&'a T> { + let index = BurnAccountInfosIndex::RegisteredProgramPda as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn noop_program(&self) -> Result<&'a T> { + let index = BurnAccountInfosIndex::NoopProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn account_compression_authority(&self) -> Result<&'a T> { + let index = BurnAccountInfosIndex::AccountCompressionAuthority as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn account_compression_program(&self) -> Result<&'a T> { + let index = BurnAccountInfosIndex::AccountCompressionProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn self_program(&self) -> Result<&'a T> { + let index = BurnAccountInfosIndex::SelfProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn system_program(&self) -> Result<&'a T> { + let index = BurnAccountInfosIndex::SystemProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn get_account_info(&self, index: usize) -> Result<&'a T> { + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn account_infos(&self) -> &'a [T] { + self.accounts + } + + pub fn config(&self) -> &BurnAccountInfosConfig { + &self.config + } + + pub fn system_accounts_len(&self) -> usize { + // BurnInstruction has a fixed number of accounts + 13 // All accounts from the enum + } +} diff --git a/sdk-libs/compressed-token-types/src/account_infos/config.rs b/sdk-libs/compressed-token-types/src/account_infos/config.rs new file mode 100644 index 0000000000..7a30ca1db2 --- /dev/null +++ b/sdk-libs/compressed-token-types/src/account_infos/config.rs @@ -0,0 +1,16 @@ +use crate::{AnchorDeserialize, AnchorSerialize}; + +#[derive(Debug, Default, Copy, Clone, AnchorSerialize, AnchorDeserialize)] +pub struct AccountInfosConfig { + pub cpi_context: bool, +} + +impl AccountInfosConfig { + pub const fn new() -> Self { + Self { cpi_context: false } + } + + pub const fn new_with_cpi_context() -> Self { + Self { cpi_context: true } + } +} diff --git a/sdk-libs/compressed-token-types/src/account_infos/create_compressed_mint.rs b/sdk-libs/compressed-token-types/src/account_infos/create_compressed_mint.rs new file mode 100644 index 0000000000..12e4125005 --- /dev/null +++ b/sdk-libs/compressed-token-types/src/account_infos/create_compressed_mint.rs @@ -0,0 +1,143 @@ +use light_account_checks::AccountInfoTrait; + +use crate::error::{LightTokenSdkTypeError, Result}; + +#[repr(usize)] +pub enum CreateCompressedMintAccountInfosIndex { + // Static non-CPI accounts first + MintSigner = 0, + LightSystemProgram = 1, + // LightSystemAccounts (7 accounts) + // FeePayer = 2, this is not ideal, if we put the fee payer in this position we don't have to copy account infos at all. + CpiAuthorityPda = 2, + RegisteredProgramPda = 3, + NoopProgram = 4, + AccountCompressionAuthority = 5, + AccountCompressionProgram = 6, + SystemProgram = 7, + SelfProgram = 8, + // CreateCompressedAccountTreeAccounts (2 accounts) + AddressMerkleTree = 9, + OutOutputQueue = 10, +} + +pub struct CreateCompressedMintAccountInfos<'a, T: AccountInfoTrait + Clone> { + fee_payer: &'a T, + accounts: &'a [T], +} + +impl<'a, T: AccountInfoTrait + Clone> CreateCompressedMintAccountInfos<'a, T> { + // Idea new_with_fee_payer and new + pub fn new(fee_payer: &'a T, accounts: &'a [T]) -> Self { + Self { + fee_payer, + accounts, + } + } + + pub fn fee_payer(&self) -> &'a T { + self.fee_payer + } + + pub fn mint_signer(&self) -> Result<&'a T> { + let index = CreateCompressedMintAccountInfosIndex::MintSigner as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn light_system_program(&self) -> Result<&'a T> { + let index = CreateCompressedMintAccountInfosIndex::LightSystemProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn cpi_authority_pda(&self) -> Result<&'a T> { + let index = CreateCompressedMintAccountInfosIndex::CpiAuthorityPda as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn registered_program_pda(&self) -> Result<&'a T> { + let index = CreateCompressedMintAccountInfosIndex::RegisteredProgramPda as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn noop_program(&self) -> Result<&'a T> { + let index = CreateCompressedMintAccountInfosIndex::NoopProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn account_compression_authority(&self) -> Result<&'a T> { + let index = CreateCompressedMintAccountInfosIndex::AccountCompressionAuthority as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn account_compression_program(&self) -> Result<&'a T> { + let index = CreateCompressedMintAccountInfosIndex::AccountCompressionProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn system_program(&self) -> Result<&'a T> { + let index = CreateCompressedMintAccountInfosIndex::SystemProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn self_program(&self) -> Result<&'a T> { + let index = CreateCompressedMintAccountInfosIndex::SelfProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn address_merkle_tree(&self) -> Result<&'a T> { + let index = CreateCompressedMintAccountInfosIndex::AddressMerkleTree as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn out_output_queue(&self) -> Result<&'a T> { + let index = CreateCompressedMintAccountInfosIndex::OutOutputQueue as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn get_account_info(&self, index: usize) -> Result<&'a T> { + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn to_account_infos(&self) -> Vec { + [vec![self.fee_payer.clone()], self.accounts.to_vec()].concat() + } + + pub fn account_infos(&self) -> &'a [T] { + self.accounts + } + + pub fn system_accounts_len(&self) -> usize { + 11 // mint_signer(1) + light_system_program(1) + light_system(7) + tree_accounts(2) + } + + pub fn tree_pubkeys(&self) -> Result<[T; 2]> { + Ok([ + self.address_merkle_tree()?.clone(), + self.out_output_queue()?.clone(), + ]) + } +} diff --git a/sdk-libs/compressed-token-types/src/account_infos/freeze.rs b/sdk-libs/compressed-token-types/src/account_infos/freeze.rs new file mode 100644 index 0000000000..aed018c007 --- /dev/null +++ b/sdk-libs/compressed-token-types/src/account_infos/freeze.rs @@ -0,0 +1,158 @@ +use light_account_checks::AccountInfoTrait; + +use crate::{ + error::{LightTokenSdkTypeError, Result}, + AnchorDeserialize, AnchorSerialize, +}; + +#[repr(usize)] +pub enum FreezeAccountInfosIndex { + FeePayer, + Authority, + CpiAuthorityPda, + LightSystemProgram, + RegisteredProgramPda, + NoopProgram, + AccountCompressionAuthority, + AccountCompressionProgram, + SelfProgram, + SystemProgram, + Mint, +} + +pub struct FreezeAccountInfos<'a, T: AccountInfoTrait + Clone> { + fee_payer: &'a T, + authority: &'a T, + accounts: &'a [T], + config: FreezeAccountInfosConfig, +} + +#[derive(Debug, Default, Copy, Clone, AnchorSerialize, AnchorDeserialize)] +pub struct FreezeAccountInfosConfig { + pub cpi_context: bool, +} + +impl FreezeAccountInfosConfig { + pub const fn new() -> Self { + Self { cpi_context: false } + } + + pub const fn new_with_cpi_context() -> Self { + Self { cpi_context: true } + } +} + +impl<'a, T: AccountInfoTrait + Clone> FreezeAccountInfos<'a, T> { + pub fn new(fee_payer: &'a T, authority: &'a T, accounts: &'a [T]) -> Self { + Self { + fee_payer, + authority, + accounts, + config: FreezeAccountInfosConfig::new(), + } + } + + pub fn new_with_config( + fee_payer: &'a T, + authority: &'a T, + accounts: &'a [T], + config: FreezeAccountInfosConfig, + ) -> Self { + Self { + fee_payer, + authority, + accounts, + config, + } + } + + pub fn fee_payer(&self) -> &'a T { + self.fee_payer + } + + pub fn authority(&self) -> &'a T { + self.authority + } + + pub fn cpi_authority_pda(&self) -> Result<&'a T> { + let index = FreezeAccountInfosIndex::CpiAuthorityPda as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn light_system_program(&self) -> Result<&'a T> { + let index = FreezeAccountInfosIndex::LightSystemProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn registered_program_pda(&self) -> Result<&'a T> { + let index = FreezeAccountInfosIndex::RegisteredProgramPda as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn noop_program(&self) -> Result<&'a T> { + let index = FreezeAccountInfosIndex::NoopProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn account_compression_authority(&self) -> Result<&'a T> { + let index = FreezeAccountInfosIndex::AccountCompressionAuthority as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn account_compression_program(&self) -> Result<&'a T> { + let index = FreezeAccountInfosIndex::AccountCompressionProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn self_program(&self) -> Result<&'a T> { + let index = FreezeAccountInfosIndex::SelfProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn system_program(&self) -> Result<&'a T> { + let index = FreezeAccountInfosIndex::SystemProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn mint(&self) -> Result<&'a T> { + let index = FreezeAccountInfosIndex::Mint as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn get_account_info(&self, index: usize) -> Result<&'a T> { + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn account_infos(&self) -> &'a [T] { + self.accounts + } + + pub fn config(&self) -> &FreezeAccountInfosConfig { + &self.config + } + + pub fn system_accounts_len(&self) -> usize { + // FreezeInstruction has a fixed number of accounts + 11 // All accounts from the enum + } +} diff --git a/sdk-libs/compressed-token-types/src/account_infos/mint_to.rs b/sdk-libs/compressed-token-types/src/account_infos/mint_to.rs new file mode 100644 index 0000000000..ce41296e8c --- /dev/null +++ b/sdk-libs/compressed-token-types/src/account_infos/mint_to.rs @@ -0,0 +1,233 @@ +use light_account_checks::AccountInfoTrait; + +use crate::{ + error::{LightTokenSdkTypeError, Result}, + AnchorDeserialize, AnchorSerialize, +}; + +#[repr(usize)] +pub enum MintToAccountInfosIndex { + FeePayer, + Authority, + CpiAuthorityPda, + Mint, + TokenPoolPda, + TokenProgram, + LightSystemProgram, + RegisteredProgramPda, + NoopProgram, + AccountCompressionAuthority, + AccountCompressionProgram, + MerkleTree, + SelfProgram, + SystemProgram, + SolPoolPda, +} + +pub struct MintToAccountInfos<'a, T: AccountInfoTrait + Clone> { + fee_payer: &'a T, + authority: &'a T, + accounts: &'a [T], + config: MintToAccountInfosConfig, +} + +#[derive(Debug, Default, Copy, Clone, AnchorSerialize, AnchorDeserialize)] +pub struct MintToAccountInfosConfig { + pub cpi_context: bool, + pub has_mint: bool, // false for batch_compress, true for mint_to + pub has_sol_pool_pda: bool, // can be Some or None in both cases +} + +impl MintToAccountInfosConfig { + pub const fn new() -> Self { + Self { + cpi_context: false, + has_mint: true, // default to mint_to behavior + has_sol_pool_pda: false, + } + } + + pub const fn new_batch_compress() -> Self { + Self { + cpi_context: false, + has_mint: false, // batch_compress doesn't use mint account + has_sol_pool_pda: false, + } + } + + pub const fn new_with_cpi_context() -> Self { + Self { + cpi_context: true, + has_mint: true, + has_sol_pool_pda: false, + } + } + + pub const fn new_with_sol_pool_pda() -> Self { + Self { + cpi_context: false, + has_mint: true, + has_sol_pool_pda: true, + } + } + + pub const fn new_batch_compress_with_sol_pool_pda() -> Self { + Self { + cpi_context: false, + has_mint: false, + has_sol_pool_pda: true, + } + } +} + +impl<'a, T: AccountInfoTrait + Clone> MintToAccountInfos<'a, T> { + pub fn new(fee_payer: &'a T, authority: &'a T, accounts: &'a [T]) -> Self { + Self { + fee_payer, + authority, + accounts, + config: MintToAccountInfosConfig::new(), + } + } + + pub fn new_with_config( + fee_payer: &'a T, + authority: &'a T, + accounts: &'a [T], + config: MintToAccountInfosConfig, + ) -> Self { + Self { + fee_payer, + authority, + accounts, + config, + } + } + + pub fn fee_payer(&self) -> &'a T { + self.fee_payer + } + + pub fn authority(&self) -> &'a T { + self.authority + } + + pub fn cpi_authority_pda(&self) -> Result<&'a T> { + let index = MintToAccountInfosIndex::CpiAuthorityPda as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn mint(&self) -> Result<&'a T> { + if !self.config.has_mint { + return Err(LightTokenSdkTypeError::MintUndefinedForBatchCompress); + } + let index = MintToAccountInfosIndex::Mint as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn token_pool_pda(&self) -> Result<&'a T> { + let index = MintToAccountInfosIndex::TokenPoolPda as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn token_program(&self) -> Result<&'a T> { + let index = MintToAccountInfosIndex::TokenProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn light_system_program(&self) -> Result<&'a T> { + let index = MintToAccountInfosIndex::LightSystemProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn registered_program_pda(&self) -> Result<&'a T> { + let index = MintToAccountInfosIndex::RegisteredProgramPda as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn noop_program(&self) -> Result<&'a T> { + let index = MintToAccountInfosIndex::NoopProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn account_compression_authority(&self) -> Result<&'a T> { + let index = MintToAccountInfosIndex::AccountCompressionAuthority as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn account_compression_program(&self) -> Result<&'a T> { + let index = MintToAccountInfosIndex::AccountCompressionProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn merkle_tree(&self) -> Result<&'a T> { + let index = MintToAccountInfosIndex::MerkleTree as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn self_program(&self) -> Result<&'a T> { + let index = MintToAccountInfosIndex::SelfProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn system_program(&self) -> Result<&'a T> { + let index = MintToAccountInfosIndex::SystemProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn sol_pool_pda(&self) -> Result<&'a T> { + if !self.config.has_sol_pool_pda { + return Err(LightTokenSdkTypeError::SolPoolPdaUndefined); + } + let index = MintToAccountInfosIndex::SolPoolPda as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn get_account_info(&self, index: usize) -> Result<&'a T> { + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn account_infos(&self) -> &'a [T] { + self.accounts + } + + pub fn config(&self) -> &MintToAccountInfosConfig { + &self.config + } + + pub fn system_accounts_len(&self) -> usize { + let mut len = 15; // Base accounts from the enum + if !self.config.has_sol_pool_pda { + len -= 1; // Remove sol_pool_pda if it's None + } + len + } +} diff --git a/sdk-libs/compressed-token-types/src/account_infos/mint_to_compressed.rs b/sdk-libs/compressed-token-types/src/account_infos/mint_to_compressed.rs new file mode 100644 index 0000000000..bc2760cc5e --- /dev/null +++ b/sdk-libs/compressed-token-types/src/account_infos/mint_to_compressed.rs @@ -0,0 +1,320 @@ +use light_account_checks::AccountInfoTrait; + +use crate::error::{LightTokenSdkTypeError, Result}; + +/// Configuration for decompressed mint operations +#[derive(Debug, Clone)] +pub struct DecompressedMintConfig { + /// SPL mint account + pub mint_pda: T, + /// Token pool PDA + pub token_pool_pda: T, + /// Token program (typically spl_token_2022::ID) + pub token_program: T, +} + +#[repr(usize)] +pub enum MintToCompressedAccountInfosIndex { + // Static non-CPI accounts first + // Authority = 0, + // Optional decompressed accounts (if is_decompressed = true) + Mint = 0, // Only present if is_decompressed + TokenPoolPda = 1, // Only present if is_decompressed + TokenProgram = 2, // Only present if is_decompressed + LightSystemProgram = 3, // Always present (index adjusted based on decompressed) + // LightSystemAccounts (7 accounts) + // FeePayer = 5, // (index adjusted based on decompressed) + CpiAuthorityPda = 4, + RegisteredProgramPda = 5, + NoopProgram = 6, + AccountCompressionAuthority = 7, + AccountCompressionProgram = 8, + SystemProgram = 9, + SelfProgram = 10, + // Optional sol pool + SolPoolPda = 11, // Only present if with_lamports + // UpdateOneCompressedAccountTreeAccounts (3 accounts) + InMerkleTree = 12, // (index adjusted based on sol_pool_pda) + InOutputQueue = 13, + OutOutputQueue = 14, + // Final account + TokensOutQueue = 15, +} + +pub struct MintToCompressedAccountInfos<'a, T: AccountInfoTrait + Clone> { + fee_payer: &'a T, + authority: &'a T, + accounts: &'a [T], + config: MintToCompressedAccountInfosConfig, +} + +#[derive(Debug, Default, Copy, Clone)] +pub struct MintToCompressedAccountInfosConfig { + pub is_decompressed: bool, // Whether mint, token_pool_pda, token_program are present + pub has_sol_pool_pda: bool, // Whether sol_pool_pda is present +} + +impl MintToCompressedAccountInfosConfig { + pub const fn new(is_decompressed: bool, has_sol_pool_pda: bool) -> Self { + Self { + is_decompressed, + has_sol_pool_pda, + } + } +} + +impl<'a, T: AccountInfoTrait + Clone> MintToCompressedAccountInfos<'a, T> { + pub fn new( + fee_payer: &'a T, + authority: &'a T, + accounts: &'a [T], + config: MintToCompressedAccountInfosConfig, + ) -> Self { + Self { + fee_payer, + authority, + accounts, + config, + } + } + + /// Create MintToCompressedAccountInfos for CPI use where authority and payer are provided separately + /// The accounts slice should not include authority or payer as they're handled by the caller + pub fn new_cpi( + fee_payer: &'a T, + authority: &'a T, + accounts: &'a [T], + config: MintToCompressedAccountInfosConfig, + ) -> Self { + Self { + fee_payer, + authority, + accounts, + config, + } + } + + pub fn fee_payer(&self) -> &'a T { + self.fee_payer + } + + pub fn authority(&self) -> &'a T { + self.authority + } + + fn get_adjusted_index(&self, base_index: usize) -> usize { + let mut adjusted = base_index; + + // Adjust for decompressed accounts (mint, token_pool_pda, token_program are indices 1,2,3) + // If not decompressed, all indices after LightSystemProgram shift down by 3 + if !self.config.is_decompressed + && base_index > MintToCompressedAccountInfosIndex::LightSystemProgram as usize + { + adjusted -= 3; + } + + // Adjust for sol_pool_pda (index 13) + // If no sol_pool_pda, all indices after it shift down by 1 + if !self.config.has_sol_pool_pda + && base_index > MintToCompressedAccountInfosIndex::SolPoolPda as usize + { + adjusted -= 1; + } + + adjusted + } + + pub fn mint(&self) -> Result<&'a T> { + if !self.config.is_decompressed { + return Err(LightTokenSdkTypeError::MintUndefinedForBatchCompress); + } + let index = self.get_adjusted_index(MintToCompressedAccountInfosIndex::Mint as usize); + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn token_pool_pda(&self) -> Result<&'a T> { + if !self.config.is_decompressed { + return Err(LightTokenSdkTypeError::TokenPoolUndefinedForCompressed); + } + let index = + self.get_adjusted_index(MintToCompressedAccountInfosIndex::TokenPoolPda as usize); + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn token_program(&self) -> Result<&'a T> { + if !self.config.is_decompressed { + return Err(LightTokenSdkTypeError::TokenProgramUndefinedForCompressed); + } + let index = + self.get_adjusted_index(MintToCompressedAccountInfosIndex::TokenProgram as usize); + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn light_system_program(&self) -> Result<&'a T> { + let index = + self.get_adjusted_index(MintToCompressedAccountInfosIndex::LightSystemProgram as usize); + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn cpi_authority_pda(&self) -> Result<&'a T> { + let index = + self.get_adjusted_index(MintToCompressedAccountInfosIndex::CpiAuthorityPda as usize); + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn registered_program_pda(&self) -> Result<&'a T> { + let index = self + .get_adjusted_index(MintToCompressedAccountInfosIndex::RegisteredProgramPda as usize); + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn noop_program(&self) -> Result<&'a T> { + let index = + self.get_adjusted_index(MintToCompressedAccountInfosIndex::NoopProgram as usize); + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn account_compression_authority(&self) -> Result<&'a T> { + let index = self.get_adjusted_index( + MintToCompressedAccountInfosIndex::AccountCompressionAuthority as usize, + ); + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn account_compression_program(&self) -> Result<&'a T> { + let index = self.get_adjusted_index( + MintToCompressedAccountInfosIndex::AccountCompressionProgram as usize, + ); + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn system_program(&self) -> Result<&'a T> { + let index = + self.get_adjusted_index(MintToCompressedAccountInfosIndex::SystemProgram as usize); + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn self_program(&self) -> Result<&'a T> { + let index = + self.get_adjusted_index(MintToCompressedAccountInfosIndex::SelfProgram as usize); + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn sol_pool_pda(&self) -> Result<&'a T> { + if !self.config.has_sol_pool_pda { + return Err(LightTokenSdkTypeError::SolPoolPdaUndefined); + } + let index = self.get_adjusted_index(MintToCompressedAccountInfosIndex::SolPoolPda as usize); + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn in_merkle_tree(&self) -> Result<&'a T> { + let index = + self.get_adjusted_index(MintToCompressedAccountInfosIndex::InMerkleTree as usize); + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn in_output_queue(&self) -> Result<&'a T> { + let index = + self.get_adjusted_index(MintToCompressedAccountInfosIndex::InOutputQueue as usize); + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn out_output_queue(&self) -> Result<&'a T> { + let index = + self.get_adjusted_index(MintToCompressedAccountInfosIndex::OutOutputQueue as usize); + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn tokens_out_queue(&self) -> Result<&'a T> { + let index = + self.get_adjusted_index(MintToCompressedAccountInfosIndex::TokensOutQueue as usize); + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn get_account_info(&self, index: usize) -> Result<&'a T> { + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn to_account_infos(&self) -> Vec { + let mut vec = self.accounts.to_vec(); + vec.insert(0, self.authority.clone()); + vec.insert(5, self.fee_payer.clone()); + vec + } + + pub fn account_infos(&self) -> &'a [T] { + self.accounts + } + + pub fn config(&self) -> &MintToCompressedAccountInfosConfig { + &self.config + } + + pub fn system_accounts_len(&self) -> usize { + let mut len = 14; // Base accounts: authority(1) + light_system(7) + tree_accounts(3) + tokens_out_queue(1) + 2 signers + + if self.config.is_decompressed { + len += 3; // mint, token_pool_pda, token_program + } + + if self.config.has_sol_pool_pda { + len += 1; // sol_pool_pda + } + + len + } + + /// Creates a DecompressedMintConfig if the mint is decompressed + pub fn get_decompressed_mint_config( + &self, + ) -> Result>> { + if !self.config.is_decompressed { + return Ok(None); + } + + let mint_pda = self.mint()?.pubkey(); + let token_pool_pda = self.token_pool_pda()?.pubkey(); + let token_program = self.token_program()?.pubkey(); + + Ok(Some(DecompressedMintConfig { + mint_pda, + token_pool_pda, + token_program, + })) + } +} diff --git a/sdk-libs/compressed-token-types/src/account_infos/mod.rs b/sdk-libs/compressed-token-types/src/account_infos/mod.rs new file mode 100644 index 0000000000..97b7711a6a --- /dev/null +++ b/sdk-libs/compressed-token-types/src/account_infos/mod.rs @@ -0,0 +1,16 @@ +mod batch_compress; +mod burn; +mod config; +mod create_compressed_mint; +mod freeze; +mod mint_to; +pub mod mint_to_compressed; +mod transfer; +pub use batch_compress::*; +pub use burn::*; +pub use config::*; +pub use create_compressed_mint::*; +pub use freeze::*; +pub use mint_to::*; +pub use mint_to_compressed::*; +pub use transfer::*; diff --git a/sdk-libs/compressed-token-types/src/account_infos/transfer.rs b/sdk-libs/compressed-token-types/src/account_infos/transfer.rs new file mode 100644 index 0000000000..7fa094cc81 --- /dev/null +++ b/sdk-libs/compressed-token-types/src/account_infos/transfer.rs @@ -0,0 +1,285 @@ +use light_account_checks::AccountInfoTrait; + +use crate::{ + error::{LightTokenSdkTypeError, Result}, + AnchorDeserialize, AnchorSerialize, +}; + +#[repr(usize)] +pub enum TransferAccountInfosIndex { + CpiAuthority, + LightSystemProgram, + RegisteredProgramPda, + NoopProgram, + AccountCompressionAuthority, + AccountCompressionProgram, + CTokenProgram, + TokenPoolPda, + DecompressionRecipient, + SplTokenProgram, + SystemProgram, + CpiContext, +} + +#[derive(Debug, Default, Copy, Clone, AnchorSerialize, AnchorDeserialize)] +pub struct TransferAccountInfosConfig { + pub cpi_context: bool, + pub compress: bool, + pub decompress: bool, +} + +impl TransferAccountInfosConfig { + pub const fn new_with_cpi_context() -> Self { + Self { + cpi_context: true, + compress: false, + decompress: false, + } + } + + pub fn new_compress() -> Self { + Self { + cpi_context: false, + compress: true, + decompress: false, + } + } + + pub fn new_decompress() -> Self { + Self { + cpi_context: false, + compress: false, + decompress: true, + } + } + + pub fn is_compress_or_decompress(&self) -> bool { + self.compress || self.decompress + } +} + +pub struct TransferAccountInfos<'a, T: AccountInfoTrait + Clone> { + fee_payer: &'a T, + authority: &'a T, + accounts: &'a [T], + config: TransferAccountInfosConfig, +} + +impl<'a, T: AccountInfoTrait + Clone> TransferAccountInfos<'a, T> { + pub fn new(fee_payer: &'a T, authority: &'a T, accounts: &'a [T]) -> Self { + Self { + fee_payer, + authority, + accounts, + config: TransferAccountInfosConfig::default(), + } + } + + pub fn new_compress(fee_payer: &'a T, authority: &'a T, accounts: &'a [T]) -> Self { + Self { + fee_payer, + authority, + accounts, + config: TransferAccountInfosConfig::new_compress(), + } + } + + pub fn new_decompress(fee_payer: &'a T, authority: &'a T, accounts: &'a [T]) -> Self { + Self { + fee_payer, + authority, + accounts, + config: TransferAccountInfosConfig::new_decompress(), + } + } + + pub fn new_with_config( + fee_payer: &'a T, + authority: &'a T, + accounts: &'a [T], + config: TransferAccountInfosConfig, + ) -> Self { + Self { + fee_payer, + authority, + accounts, + config, + } + } + + pub fn fee_payer(&self) -> &'a T { + self.fee_payer + } + + pub fn light_system_program(&self) -> Result<&'a T> { + let index = TransferAccountInfosIndex::LightSystemProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn authority(&self) -> &'a T { + self.authority + } + + pub fn ctoken_program(&self) -> Result<&'a T> { + let index = TransferAccountInfosIndex::CTokenProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn spl_token_program(&self) -> Result<&'a T> { + let index = TransferAccountInfosIndex::SplTokenProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn registered_program_pda(&self) -> Result<&'a T> { + let index = TransferAccountInfosIndex::RegisteredProgramPda as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn noop_program(&self) -> Result<&'a T> { + let index = TransferAccountInfosIndex::NoopProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn account_compression_authority(&self) -> Result<&'a T> { + let index = TransferAccountInfosIndex::AccountCompressionAuthority as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn account_compression_program(&self) -> Result<&'a T> { + let index = TransferAccountInfosIndex::AccountCompressionProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn token_pool_pda(&self) -> Result<&'a T> { + let index = TransferAccountInfosIndex::TokenPoolPda as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn decompression_recipient(&self) -> Result<&'a T> { + if !self.config.decompress { + return Err(LightTokenSdkTypeError::DecompressionRecipientTokenAccountDoesOnlyExistInDecompressedMode); + }; + let index = TransferAccountInfosIndex::DecompressionRecipient as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn sender_token_account(&self) -> Result<&'a T> { + if !self.config.compress { + return Err(LightTokenSdkTypeError::SenderTokenAccountDoesOnlyExistInCompressedMode); + }; + let index = TransferAccountInfosIndex::DecompressionRecipient as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn system_program(&self) -> Result<&'a T> { + let index = TransferAccountInfosIndex::SystemProgram as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn cpi_context(&self) -> Result<&'a T> { + let index = TransferAccountInfosIndex::CpiContext as usize; + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn config(&self) -> &TransferAccountInfosConfig { + &self.config + } + + pub fn system_accounts_len(&self) -> usize { + let mut len = 12; // Base system accounts length + if !self.config.is_compress_or_decompress() { + // Token pool pda & compression sender or decompression recipient + len -= 3; + } + if !self.config.cpi_context { + len -= 1; + } + len + } + + pub fn account_infos(&self) -> &'a [T] { + self.accounts + } + + pub fn get_account_info(&self, index: usize) -> Result<&'a T> { + self.accounts + .get(index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(index)) + } + + pub fn tree_accounts(&self) -> Result<&'a [T]> { + let system_len = self.system_accounts_len(); + solana_msg::msg!("Tree accounts length calculation {}", system_len); + self.accounts + .get(system_len..) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds( + system_len, + )) + } + + pub fn tree_pubkeys(&self) -> Result> { + let system_len = self.system_accounts_len(); + Ok(self + .accounts + .get(system_len..) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds( + system_len, + ))? + .iter() + .map(|account| account.pubkey()) + .collect::>()) + } + + pub fn get_tree_account_info(&self, tree_index: usize) -> Result<&'a T> { + let tree_accounts = self.tree_accounts()?; + tree_accounts + .get(tree_index) + .ok_or(LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds( + self.system_accounts_len() + tree_index, + )) + } + + /// Create a vector of account info references + pub fn to_account_info_refs(&self) -> Vec<&'a T> { + let mut account_infos = Vec::with_capacity(1 + self.system_accounts_len()); + account_infos.push(self.fee_payer()); + self.account_infos()[1..] + .iter() + .for_each(|acc| account_infos.push(acc)); + account_infos + } + + /// Create a vector of account info references + pub fn to_account_infos(&self) -> Vec { + let mut account_infos = Vec::with_capacity(1 + self.system_accounts_len()); + account_infos.push(self.fee_payer().clone()); + self.account_infos() + .iter() + .for_each(|acc| account_infos.push(acc.clone())); + account_infos + } +} diff --git a/sdk-libs/compressed-token-types/src/constants.rs b/sdk-libs/compressed-token-types/src/constants.rs new file mode 100644 index 0000000000..020f10346b --- /dev/null +++ b/sdk-libs/compressed-token-types/src/constants.rs @@ -0,0 +1,52 @@ +use light_macros::pubkey_array; + +// Program ID for light-compressed-token +pub const PROGRAM_ID: [u8; 32] = pubkey_array!("cTokenmWW8bLPjZEBAUgYy3zKxQZW6VKi7bqNFEVv3m"); + +// SPL Token Program ID +pub const SPL_TOKEN_PROGRAM_ID: [u8; 32] = + pubkey_array!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"); + +// SPL Token 2022 Program ID +pub const SPL_TOKEN_2022_PROGRAM_ID: [u8; 32] = + pubkey_array!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"); + +// Light System Program ID +pub const LIGHT_SYSTEM_PROGRAM_ID: [u8; 32] = + pubkey_array!("SySTEM1eSU2p4BGQfQpimFEWWSC1XDFeun3Nqzz3rT7"); + +// Account Compression Program ID +pub const ACCOUNT_COMPRESSION_PROGRAM_ID: [u8; 32] = + pubkey_array!("compr6CUsB5m2jS4Y3831ztGSTnDpnKJTKS95d64XVq"); + +// Account Compression Program ID +pub const ACCOUNT_COMPRESSION_AUTHORITY_PDA: [u8; 32] = + pubkey_array!("HwXnGK3tPkkVY6P439H2p68AxpeuWXd5PcrAxFpbmfbA"); + +// Noop Program ID +pub const NOOP_PROGRAM_ID: [u8; 32] = pubkey_array!("noopb9bkMVfRPU8AsbpTUg8AQkHtKwMYZiFUjNRtMmV"); + +// CPI Authority PDA seed +pub const CPI_AUTHORITY_PDA_SEED: &[u8] = b"cpi_authority"; + +pub const CPI_AUTHORITY_PDA: [u8; 32] = + pubkey_array!("GXtd2izAiMJPwMEjfgTRH3d7k9mjn4Jq3JrWFv9gySYy"); + +// 2 in little endian +pub const TOKEN_COMPRESSED_ACCOUNT_DISCRIMINATOR: [u8; 8] = [2, 0, 0, 0, 0, 0, 0, 0]; +pub const BUMP_CPI_AUTHORITY: u8 = 254; +pub const NOT_FROZEN: bool = false; +pub const POOL_SEED: &[u8] = b"pool"; + +/// Maximum number of pool accounts that can be created for each mint. +pub const NUM_MAX_POOL_ACCOUNTS: u8 = 5; +pub const MINT_TO: [u8; 8] = [241, 34, 48, 186, 37, 179, 123, 192]; +pub const TRANSFER: [u8; 8] = [163, 52, 200, 231, 140, 3, 69, 186]; +pub const BATCH_COMPRESS: [u8; 8] = [65, 206, 101, 37, 147, 42, 221, 144]; +pub const APPROVE: [u8; 8] = [69, 74, 217, 36, 115, 117, 97, 76]; +pub const REVOKE: [u8; 8] = [170, 23, 31, 34, 133, 173, 93, 242]; +pub const FREEZE: [u8; 8] = [255, 91, 207, 84, 251, 194, 254, 63]; +pub const THAW: [u8; 8] = [226, 249, 34, 57, 189, 21, 177, 101]; +pub const CREATE_TOKEN_POOL: [u8; 8] = [23, 169, 27, 122, 147, 169, 209, 152]; +pub const CREATE_ADDITIONAL_TOKEN_POOL: [u8; 8] = [114, 143, 210, 73, 96, 115, 1, 228]; +pub const TRANSFER2: u8 = 104; diff --git a/sdk-libs/compressed-token-types/src/error.rs b/sdk-libs/compressed-token-types/src/error.rs new file mode 100644 index 0000000000..0e8fba928d --- /dev/null +++ b/sdk-libs/compressed-token-types/src/error.rs @@ -0,0 +1,35 @@ +use thiserror::Error; + +pub type Result = std::result::Result; + +#[derive(Debug, Error)] +pub enum LightTokenSdkTypeError { + #[error("CPI accounts index out of bounds: {0}")] + CpiAccountsIndexOutOfBounds(usize), + #[error("Sender token account does only exist in compressed mode")] + SenderTokenAccountDoesOnlyExistInCompressedMode, + #[error("Decompression recipient token account does only exist in decompressed mode")] + DecompressionRecipientTokenAccountDoesOnlyExistInDecompressedMode, + #[error("Sol pool PDA is undefined")] + SolPoolPdaUndefined, + #[error("Mint is undefined for batch compress")] + MintUndefinedForBatchCompress, + #[error("Token pool PDA is undefined for compressed")] + TokenPoolUndefinedForCompressed, + #[error("Token program is undefined for compressed")] + TokenProgramUndefinedForCompressed, +} + +impl From for u32 { + fn from(error: LightTokenSdkTypeError) -> Self { + match error { + LightTokenSdkTypeError::CpiAccountsIndexOutOfBounds(_) => 18001, + LightTokenSdkTypeError::SenderTokenAccountDoesOnlyExistInCompressedMode => 18002, + LightTokenSdkTypeError::DecompressionRecipientTokenAccountDoesOnlyExistInDecompressedMode => 18003, + LightTokenSdkTypeError::SolPoolPdaUndefined => 18004, + LightTokenSdkTypeError::MintUndefinedForBatchCompress => 18005, + LightTokenSdkTypeError::TokenPoolUndefinedForCompressed => 18006, + LightTokenSdkTypeError::TokenProgramUndefinedForCompressed => 18007, + } + } +} diff --git a/sdk-libs/compressed-token-types/src/instruction/batch_compress.rs b/sdk-libs/compressed-token-types/src/instruction/batch_compress.rs new file mode 100644 index 0000000000..a9c2fdb719 --- /dev/null +++ b/sdk-libs/compressed-token-types/src/instruction/batch_compress.rs @@ -0,0 +1,13 @@ +use borsh::{BorshDeserialize, BorshSerialize}; + +#[derive(Debug, Default, Clone, PartialEq, BorshSerialize, BorshDeserialize)] +pub struct BatchCompressInstructionData { + pub pubkeys: Vec<[u8; 32]>, + // Some if one amount per pubkey. + pub amounts: Option>, + pub lamports: Option, + // Some if one amount across all pubkeys. + pub amount: Option, + pub index: u8, + pub bump: u8, +} diff --git a/sdk-libs/compressed-token-types/src/instruction/burn.rs b/sdk-libs/compressed-token-types/src/instruction/burn.rs new file mode 100644 index 0000000000..6377c52f9a --- /dev/null +++ b/sdk-libs/compressed-token-types/src/instruction/burn.rs @@ -0,0 +1,15 @@ +use borsh::{BorshDeserialize, BorshSerialize}; + +use crate::instruction::transfer::{ + CompressedCpiContext, CompressedProof, DelegatedTransfer, TokenAccountMeta, +}; + +#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)] +pub struct CompressedTokenInstructionDataBurn { + pub proof: CompressedProof, + pub input_token_data_with_context: Vec, + pub cpi_context: Option, + pub burn_amount: u64, + pub change_account_merkle_tree_index: u8, + pub delegated_transfer: Option, +} diff --git a/sdk-libs/compressed-token-types/src/instruction/delegation.rs b/sdk-libs/compressed-token-types/src/instruction/delegation.rs new file mode 100644 index 0000000000..99df49a594 --- /dev/null +++ b/sdk-libs/compressed-token-types/src/instruction/delegation.rs @@ -0,0 +1,27 @@ +use borsh::{BorshDeserialize, BorshSerialize}; + +use crate::instruction::transfer::{CompressedCpiContext, CompressedProof, TokenAccountMeta}; + +#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)] +pub struct CompressedTokenInstructionDataApprove { + pub proof: CompressedProof, + pub mint: [u8; 32], + pub input_token_data_with_context: Vec, + pub cpi_context: Option, + pub delegate: [u8; 32], + pub delegated_amount: u64, + /// Index in remaining accounts. + pub delegate_merkle_tree_index: u8, + /// Index in remaining accounts. + pub change_account_merkle_tree_index: u8, + pub delegate_lamports: Option, +} + +#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)] +pub struct CompressedTokenInstructionDataRevoke { + pub proof: CompressedProof, + pub mint: [u8; 32], + pub input_token_data_with_context: Vec, + pub cpi_context: Option, + pub output_account_merkle_tree_index: u8, +} diff --git a/sdk-libs/compressed-token-types/src/instruction/freeze.rs b/sdk-libs/compressed-token-types/src/instruction/freeze.rs new file mode 100644 index 0000000000..a8bb88cb4b --- /dev/null +++ b/sdk-libs/compressed-token-types/src/instruction/freeze.rs @@ -0,0 +1,21 @@ +use borsh::{BorshDeserialize, BorshSerialize}; + +use crate::instruction::transfer::{CompressedCpiContext, CompressedProof, TokenAccountMeta}; + +#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)] +pub struct CompressedTokenInstructionDataFreeze { + pub proof: CompressedProof, + pub owner: [u8; 32], + pub input_token_data_with_context: Vec, + pub cpi_context: Option, + pub outputs_merkle_tree_index: u8, +} + +#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)] +pub struct CompressedTokenInstructionDataThaw { + pub proof: CompressedProof, + pub owner: [u8; 32], + pub input_token_data_with_context: Vec, + pub cpi_context: Option, + pub outputs_merkle_tree_index: u8, +} diff --git a/sdk-libs/compressed-token-types/src/instruction/generic.rs b/sdk-libs/compressed-token-types/src/instruction/generic.rs new file mode 100644 index 0000000000..10c9fc0ee8 --- /dev/null +++ b/sdk-libs/compressed-token-types/src/instruction/generic.rs @@ -0,0 +1,10 @@ +use borsh::{BorshDeserialize, BorshSerialize}; + +// Generic instruction data wrapper that can hold any instruction data as bytes +#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)] +pub struct GenericInstructionData { + pub instruction_data: Vec, +} + +// Type alias for the main generic instruction data type +pub type CompressedTokenInstructionData = GenericInstructionData; diff --git a/sdk-libs/compressed-token-types/src/instruction/mint_to.rs b/sdk-libs/compressed-token-types/src/instruction/mint_to.rs new file mode 100644 index 0000000000..e94d755352 --- /dev/null +++ b/sdk-libs/compressed-token-types/src/instruction/mint_to.rs @@ -0,0 +1,12 @@ +use borsh::{BorshDeserialize, BorshSerialize}; + +// Note: MintToInstruction is an Anchor account struct, not an instruction data struct +// This file is for completeness but there's no specific MintToInstructionData type +// The mint_to instruction uses pubkeys and amounts directly as parameters + +#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)] +pub struct MintToParams { + pub public_keys: Vec<[u8; 32]>, + pub amounts: Vec, + pub lamports: Option, +} diff --git a/sdk-libs/compressed-token-types/src/instruction/mod.rs b/sdk-libs/compressed-token-types/src/instruction/mod.rs new file mode 100644 index 0000000000..d7a6a4151e --- /dev/null +++ b/sdk-libs/compressed-token-types/src/instruction/mod.rs @@ -0,0 +1,19 @@ +pub mod batch_compress; +pub mod burn; +pub mod delegation; +pub mod freeze; +pub mod generic; +pub mod mint_to; +pub mod transfer; + +// Re-export ValidityProof same as in light-sdk +pub use batch_compress::*; +pub use burn::*; +pub use delegation::*; +pub use freeze::*; +// Export the generic instruction with an alias as the main type +pub use generic::CompressedTokenInstructionData; +pub use light_compressed_account::instruction_data::compressed_proof::ValidityProof; +pub use mint_to::*; +// Re-export all instruction data types +pub use transfer::*; diff --git a/sdk-libs/compressed-token-types/src/instruction/transfer.rs b/sdk-libs/compressed-token-types/src/instruction/transfer.rs new file mode 100644 index 0000000000..f30979e104 --- /dev/null +++ b/sdk-libs/compressed-token-types/src/instruction/transfer.rs @@ -0,0 +1,99 @@ +pub use light_compressed_account::instruction_data::{ + compressed_proof::CompressedProof, cpi_context::CompressedCpiContext, +}; +use light_sdk_types::instruction::PackedStateTreeInfo; + +use crate::{AnchorDeserialize, AnchorSerialize}; + +#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize, PartialEq)] +pub struct PackedMerkleContext { + pub merkle_tree_pubkey_index: u8, + pub nullifier_queue_pubkey_index: u8, + pub leaf_index: u32, + pub proof_by_index: bool, +} + +#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize, PartialEq)] +pub struct TokenAccountMeta { + pub amount: u64, + pub delegate_index: Option, + pub packed_tree_info: PackedStateTreeInfo, + pub lamports: Option, + /// Placeholder for TokenExtension tlv data (unimplemented) + pub tlv: Option>, +} + +#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize, PartialEq)] +pub struct InputTokenDataWithContextOnchain { + pub amount: u64, + pub delegate_index: Option, + pub merkle_context: PackedMerkleContext, + pub root_index: u16, + pub lamports: Option, + /// Placeholder for TokenExtension tlv data (unimplemented) + pub tlv: Option>, +} + +impl From for InputTokenDataWithContextOnchain { + fn from(input: TokenAccountMeta) -> Self { + Self { + amount: input.amount, + delegate_index: input.delegate_index, + merkle_context: PackedMerkleContext { + merkle_tree_pubkey_index: input.packed_tree_info.merkle_tree_pubkey_index, + nullifier_queue_pubkey_index: input.packed_tree_info.queue_pubkey_index, + leaf_index: input.packed_tree_info.leaf_index, + proof_by_index: input.packed_tree_info.prove_by_index, + }, + root_index: input.packed_tree_info.root_index, + lamports: input.lamports, + tlv: input.tlv, + } + } +} + +/// Struct to provide the owner when the delegate is signer of the transaction. +#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize)] +pub struct DelegatedTransfer { + pub owner: [u8; 32], + /// Index of change compressed account in output compressed accounts. In + /// case that the delegate didn't spend the complete delegated compressed + /// account balance the change compressed account will be delegated to her + /// as well. + pub delegate_change_account_index: Option, +} + +#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize)] +pub struct CompressedTokenInstructionDataTransfer { + pub proof: Option, + pub mint: [u8; 32], + /// Is required if the signer is delegate, + /// -> delegate is authority account, + /// owner = Some(owner) is the owner of the token account. + pub delegated_transfer: Option, + pub input_token_data_with_context: Vec, + pub output_compressed_accounts: Vec, + pub is_compress: bool, + pub compress_or_decompress_amount: Option, + pub cpi_context: Option, + pub lamports_change_account_merkle_tree_index: Option, + pub with_transaction_hash: bool, +} + +#[derive(Clone, Debug, PartialEq, Eq, AnchorSerialize, AnchorDeserialize)] +pub struct PackedTokenTransferOutputData { + pub owner: [u8; 32], + pub amount: u64, + pub lamports: Option, + pub merkle_tree_index: u8, + /// Placeholder for TokenExtension tlv data (unimplemented) + pub tlv: Option>, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, AnchorSerialize, AnchorDeserialize)] +pub struct TokenTransferOutputData { + pub owner: [u8; 32], + pub amount: u64, + pub lamports: Option, + pub merkle_tree: [u8; 32], +} diff --git a/sdk-libs/compressed-token-types/src/lib.rs b/sdk-libs/compressed-token-types/src/lib.rs new file mode 100644 index 0000000000..60967fbff2 --- /dev/null +++ b/sdk-libs/compressed-token-types/src/lib.rs @@ -0,0 +1,16 @@ +pub mod account_infos; +pub mod constants; +pub mod error; +pub mod instruction; +pub mod token_data; + +// Conditional anchor re-exports +#[cfg(feature = "anchor")] +use anchor_lang::{AnchorDeserialize, AnchorSerialize}; +#[cfg(not(feature = "anchor"))] +use borsh::{BorshDeserialize as AnchorDeserialize, BorshSerialize as AnchorSerialize}; +// TODO: remove the reexports +// Re-export everything at the crate root level +pub use constants::*; +pub use instruction::*; +pub use token_data::*; diff --git a/sdk-libs/compressed-token-types/src/token_data.rs b/sdk-libs/compressed-token-types/src/token_data.rs new file mode 100644 index 0000000000..b126d6582f --- /dev/null +++ b/sdk-libs/compressed-token-types/src/token_data.rs @@ -0,0 +1,25 @@ +use borsh::{BorshDeserialize, BorshSerialize}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, BorshSerialize, BorshDeserialize)] +#[repr(u8)] +pub enum AccountState { + Initialized, + Frozen, +} + +#[derive(Debug, PartialEq, Eq, BorshSerialize, BorshDeserialize, Clone)] +pub struct TokenData { + /// The mint associated with this account + pub mint: [u8; 32], + /// The owner of this account. + pub owner: [u8; 32], + /// The amount of tokens this account holds. + pub amount: u64, + /// If `delegate` is `Some` then `delegated_amount` represents + /// the amount authorized by the delegate + pub delegate: Option<[u8; 32]>, + /// The account's state + pub state: AccountState, + /// Placeholder for TokenExtension tlv data (unimplemented) + pub tlv: Option>, +} diff --git a/sdk-libs/program-test/src/indexer/test_indexer.rs b/sdk-libs/program-test/src/indexer/test_indexer.rs index 43e781318f..f688173e94 100644 --- a/sdk-libs/program-test/src/indexer/test_indexer.rs +++ b/sdk-libs/program-test/src/indexer/test_indexer.rs @@ -16,12 +16,13 @@ use light_client::{ fee::FeeConfig, indexer::{ AccountProofInputs, Address, AddressMerkleTreeAccounts, AddressProofInputs, - AddressWithTree, BatchAddressUpdateIndexerResponse, CompressedAccount, Context, - GetCompressedAccountsByOwnerConfig, GetCompressedTokenAccountsByOwnerOrDelegateOptions, - Indexer, IndexerError, IndexerRpcConfig, Items, ItemsWithCursor, MerkleProof, - MerkleProofWithContext, NewAddressProofWithContext, OwnerBalance, PaginatedOptions, - Response, RetryConfig, RootIndex, SignatureWithMetadata, StateMerkleTreeAccounts, - TokenAccount, TokenBalance, ValidityProofWithContext, + AddressWithTree, BatchAddressUpdateIndexerResponse, CompressedAccount, + CompressedTokenAccount, Context, GetCompressedAccountsByOwnerConfig, + GetCompressedTokenAccountsByOwnerOrDelegateOptions, Indexer, IndexerError, + IndexerRpcConfig, Items, ItemsWithCursor, MerkleProof, MerkleProofWithContext, + NewAddressProofWithContext, OwnerBalance, PaginatedOptions, Response, RetryConfig, + RootIndex, SignatureWithMetadata, StateMerkleTreeAccounts, TokenBalance, + ValidityProofWithContext, }, rpc::{Rpc, RpcError}, }; @@ -247,15 +248,15 @@ impl Indexer for TestIndexer { owner: &Pubkey, options: Option, _config: Option, - ) -> Result>, IndexerError> { + ) -> Result>, IndexerError> { let mint = options.as_ref().and_then(|opts| opts.mint); - let token_accounts: Result, IndexerError> = self + let token_accounts: Result, IndexerError> = self .token_compressed_accounts .iter() .filter(|acc| { acc.token_data.owner == *owner && mint.is_none_or(|m| acc.token_data.mint == m) }) - .map(|acc| TokenAccount::try_from(acc.clone())) + .map(|acc| CompressedTokenAccount::try_from(acc.clone())) .collect(); let token_accounts = token_accounts?; let token_accounts = if let Some(options) = options { @@ -953,7 +954,7 @@ impl Indexer for TestIndexer { _delegate: &Pubkey, _options: Option, _config: Option, - ) -> Result>, IndexerError> { + ) -> Result>, IndexerError> { todo!("get_compressed_token_accounts_by_delegate not implemented") } @@ -1693,8 +1694,13 @@ impl TestIndexer { // new accounts are inserted in front so that the newest accounts are found first match compressed_account.compressed_account.data.as_ref() { Some(data) => { - if compressed_account.compressed_account.owner == light_compressed_token::ID.to_bytes() - && data.discriminator == light_compressed_token::constants::TOKEN_COMPRESSED_ACCOUNT_DISCRIMINATOR + // Check for both V1 and V2 token account discriminators + let is_v1_token = data.discriminator == light_compressed_token::constants::TOKEN_COMPRESSED_ACCOUNT_DISCRIMINATOR; // [2, 0, 0, 0, 0, 0, 0, 0] + let is_v2_token = data.discriminator == [0, 0, 0, 0, 0, 0, 0, 3]; // V2 discriminator + + if compressed_account.compressed_account.owner + == light_compressed_token::ID.to_bytes() + && (is_v1_token || is_v2_token) { if let Ok(token_data) = TokenData::deserialize(&mut data.data.as_slice()) { let token_account = TokenDataWithMerkleContext { @@ -1708,7 +1714,7 @@ impl TestIndexer { merkle_tree_pubkey: merkle_tree_pubkey.into(), queue_pubkey: nullifier_queue_pubkey.into(), prove_by_index: false, - tree_type:merkle_tree.tree_type, + tree_type: merkle_tree.tree_type, }, }, }; @@ -1723,7 +1729,7 @@ impl TestIndexer { merkle_tree_pubkey: merkle_tree_pubkey.into(), queue_pubkey: nullifier_queue_pubkey.into(), prove_by_index: false, - tree_type: merkle_tree.tree_type + tree_type: merkle_tree.tree_type, }, }; compressed_accounts.push(compressed_account.clone()); diff --git a/sdk-libs/program-test/src/program_test/indexer.rs b/sdk-libs/program-test/src/program_test/indexer.rs index 744148bf66..06d218fef1 100644 --- a/sdk-libs/program-test/src/program_test/indexer.rs +++ b/sdk-libs/program-test/src/program_test/indexer.rs @@ -1,10 +1,11 @@ use async_trait::async_trait; use light_client::indexer::{ Address, AddressWithTree, BatchAddressUpdateIndexerResponse, CompressedAccount, - GetCompressedAccountsByOwnerConfig, GetCompressedTokenAccountsByOwnerOrDelegateOptions, Hash, - Indexer, IndexerError, IndexerRpcConfig, Items, ItemsWithCursor, MerkleProof, - MerkleProofWithContext, NewAddressProofWithContext, OwnerBalance, PaginatedOptions, Response, - RetryConfig, SignatureWithMetadata, TokenAccount, TokenBalance, ValidityProofWithContext, + CompressedTokenAccount, GetCompressedAccountsByOwnerConfig, + GetCompressedTokenAccountsByOwnerOrDelegateOptions, Hash, Indexer, IndexerError, + IndexerRpcConfig, Items, ItemsWithCursor, MerkleProof, MerkleProofWithContext, + NewAddressProofWithContext, OwnerBalance, PaginatedOptions, Response, RetryConfig, + SignatureWithMetadata, TokenBalance, ValidityProofWithContext, }; use light_compressed_account::QueueType; use solana_sdk::pubkey::Pubkey; @@ -94,7 +95,7 @@ impl Indexer for LightProgramTest { owner: &Pubkey, options: Option, config: Option, - ) -> Result>, IndexerError> { + ) -> Result>, IndexerError> { Ok(self .indexer .as_ref() @@ -265,7 +266,7 @@ impl Indexer for LightProgramTest { delegate: &Pubkey, options: Option, config: Option, - ) -> Result>, IndexerError> { + ) -> Result>, IndexerError> { Ok(self .indexer .as_ref() diff --git a/sdk-libs/program-test/src/program_test/light_program_test.rs b/sdk-libs/program-test/src/program_test/light_program_test.rs index 938d071573..eccc27f916 100644 --- a/sdk-libs/program-test/src/program_test/light_program_test.rs +++ b/sdk-libs/program-test/src/program_test/light_program_test.rs @@ -152,11 +152,6 @@ impl LightProgramTest { self.test_accounts.v1_address_trees[0] } - #[cfg(feature = "v2")] - pub fn get_address_merkle_tree_v2(&self) -> solana_sdk::pubkey::Pubkey { - self.test_accounts.v2_address_trees[0] - } - pub async fn add_indexer( &mut self, test_accounts: &TestAccounts, diff --git a/sdk-libs/program-test/src/program_test/rpc.rs b/sdk-libs/program-test/src/program_test/rpc.rs index 01821323da..50d953d5c7 100644 --- a/sdk-libs/program-test/src/program_test/rpc.rs +++ b/sdk-libs/program-test/src/program_test/rpc.rs @@ -287,17 +287,8 @@ impl Rpc for LightProgramTest { tree_type: TreeType::AddressV1, } } -} -impl LightProgramTest { - fn maybe_print_logs(&self, logs: impl std::fmt::Display) { - if !self.config.no_logs && cfg!(debug_assertions) && std::env::var("RUST_BACKTRACE").is_ok() - { - println!("{}", logs); - } - } - #[cfg(feature = "v2")] - pub fn get_address_tree_v2(&self) -> TreeInfo { + fn get_address_tree_v2(&self) -> TreeInfo { TreeInfo { tree: pubkey!("EzKE84aVTkCUhDHLELqyJaq1Y7UVVmqxXqZjVHwHY3rK"), queue: pubkey!("EzKE84aVTkCUhDHLELqyJaq1Y7UVVmqxXqZjVHwHY3rK"), @@ -306,6 +297,15 @@ impl LightProgramTest { tree_type: TreeType::AddressV2, } } +} + +impl LightProgramTest { + fn maybe_print_logs(&self, logs: impl std::fmt::Display) { + if !self.config.no_logs && cfg!(debug_assertions) && std::env::var("RUST_BACKTRACE").is_ok() + { + println!("{}", logs); + } + } async fn _send_transaction_with_batched_event( &mut self, diff --git a/sdk-libs/sdk-types/src/cpi_accounts.rs b/sdk-libs/sdk-types/src/cpi_accounts.rs index 7750603a3d..4aed17ab90 100644 --- a/sdk-libs/sdk-types/src/cpi_accounts.rs +++ b/sdk-libs/sdk-types/src/cpi_accounts.rs @@ -9,7 +9,7 @@ use crate::{ CpiSigner, CPI_CONTEXT_ACCOUNT_DISCRIMINATOR, LIGHT_SYSTEM_PROGRAM_ID, SOL_POOL_PDA, }; -#[derive(Debug, Copy, Clone, AnchorSerialize, AnchorDeserialize)] +#[derive(Debug, Copy, Clone, PartialEq, AnchorSerialize, AnchorDeserialize)] pub struct CpiAccountsConfig { pub cpi_context: bool, pub sol_compression_recipient: bool, @@ -61,14 +61,14 @@ pub enum CompressionCpiAccountIndex { } pub const SYSTEM_ACCOUNTS_LEN: usize = 11; - -pub struct CpiAccounts<'a, T: AccountInfoTrait> { +#[derive(Debug, Clone, PartialEq)] +pub struct CpiAccounts<'a, T: AccountInfoTrait + Clone> { fee_payer: &'a T, accounts: &'a [T], - config: CpiAccountsConfig, + pub config: CpiAccountsConfig, } -impl<'a, T: AccountInfoTrait> CpiAccounts<'a, T> { +impl<'a, T: AccountInfoTrait + Clone> CpiAccounts<'a, T> { pub fn new(fee_payer: &'a T, accounts: &'a [T], cpi_signer: CpiSigner) -> Self { Self { fee_payer, @@ -255,6 +255,14 @@ impl<'a, T: AccountInfoTrait> CpiAccounts<'a, T> { .ok_or(LightSdkTypesError::CpiAccountsIndexOutOfBounds(system_len)) } + pub fn tree_pubkeys(&self) -> Result> { + Ok(self + .tree_accounts()? + .iter() + .map(|x| x.pubkey()) + .collect::>()) + } + pub fn get_tree_account_info(&self, tree_index: usize) -> Result<&'a T> { let tree_accounts = self.tree_accounts()?; tree_accounts @@ -265,12 +273,12 @@ impl<'a, T: AccountInfoTrait> CpiAccounts<'a, T> { } /// Create a vector of account info references - pub fn to_account_infos(&self) -> Vec<&'a T> { - let mut account_infos = Vec::with_capacity(1 + SYSTEM_ACCOUNTS_LEN); - account_infos.push(self.fee_payer()); - self.account_infos()[1..] - .iter() - .for_each(|acc| account_infos.push(acc)); + pub fn to_account_infos(&self) -> Vec { + // Skip system light program + let refs = &self.account_infos()[1..]; + let mut account_infos = Vec::with_capacity(1 + refs.len()); + account_infos.push(self.fee_payer().clone()); + account_infos.extend_from_slice(refs); account_infos } } diff --git a/sdk-libs/sdk-types/src/instruction/tree_info.rs b/sdk-libs/sdk-types/src/instruction/tree_info.rs index 8cdcc7fed0..8f0f481507 100644 --- a/sdk-libs/sdk-types/src/instruction/tree_info.rs +++ b/sdk-libs/sdk-types/src/instruction/tree_info.rs @@ -29,7 +29,7 @@ impl PackedAddressTreeInfo { } } - pub fn get_tree_pubkey( + pub fn get_tree_pubkey( &self, cpi_accounts: &CpiAccounts<'_, T>, ) -> Result { diff --git a/sdk-libs/sdk/src/cpi/invoke.rs b/sdk-libs/sdk/src/cpi/invoke.rs index 39796a8da0..5f869923bd 100644 --- a/sdk-libs/sdk/src/cpi/invoke.rs +++ b/sdk-libs/sdk/src/cpi/invoke.rs @@ -52,9 +52,8 @@ impl CpiInputs { pub fn invoke_light_system_program(self, cpi_accounts: CpiAccounts<'_, '_>) -> Result<()> { let bump = cpi_accounts.bump(); - let account_info_refs = cpi_accounts.to_account_infos(); + let account_infos = cpi_accounts.to_account_infos(); let instruction = create_light_system_progam_instruction_invoke_cpi(self, cpi_accounts)?; - let account_infos: Vec = account_info_refs.into_iter().cloned().collect(); invoke_light_system_program(account_infos.as_slice(), instruction, bump) } } @@ -138,8 +137,9 @@ where data.extend_from_slice(&light_compressed_account::discriminators::DISCRIMINATOR_INVOKE_CPI); data.extend_from_slice(&(inputs.len() as u32).to_le_bytes()); data.extend(inputs); - let account_info_refs = cpi_accounts.to_account_infos(); - let account_infos: Vec = account_info_refs.into_iter().cloned().collect(); + // let account_info_refs = cpi_accounts.to_account_infos(); + // let account_infos: Vec = account_info_refs.into_iter().cloned().collect(); + let account_infos = light_system_accounts.to_account_infos(); let bump = cpi_accounts.bump(); let config = CpiInstructionConfig::try_from(&cpi_accounts)?; diff --git a/sdk-libs/sdk/src/error.rs b/sdk-libs/sdk/src/error.rs index c2af21c28d..d8e1c52ed6 100644 --- a/sdk-libs/sdk/src/error.rs +++ b/sdk-libs/sdk/src/error.rs @@ -76,14 +76,16 @@ pub enum LightSdkError { InvalidSolPoolPdaAccount, #[error("CpigAccounts accounts slice starts with an invalid account. It should start with LightSystemProgram SySTEM1eSU2p4BGQfQpimFEWWSC1XDFeun3Nqzz3rT7.")] InvalidCpiAccountsOffset, + #[error("CPI context must be added before any other accounts (next_index must be 0)")] + CpiContextOrderingViolation, + #[error(transparent)] + AccountError(#[from] AccountError), #[error(transparent)] Hasher(#[from] HasherError), #[error(transparent)] ZeroCopy(#[from] ZeroCopyError), #[error("Program error: {0}")] ProgramError(#[from] ProgramError), - #[error(transparent)] - AccountError(#[from] AccountError), } impl From for ProgramError { @@ -167,6 +169,7 @@ impl From for u32 { LightSdkError::InvalidCpiContextAccount => 16032, LightSdkError::InvalidSolPoolPdaAccount => 16033, LightSdkError::InvalidCpiAccountsOffset => 16034, + LightSdkError::CpiContextOrderingViolation => 16035, LightSdkError::AccountError(e) => e.into(), LightSdkError::Hasher(e) => e.into(), LightSdkError::ZeroCopy(e) => e.into(), diff --git a/sdk-libs/sdk/src/instruction/pack_accounts.rs b/sdk-libs/sdk/src/instruction/pack_accounts.rs index 830ebe98a1..d7e7a1ffaa 100644 --- a/sdk-libs/sdk/src/instruction/pack_accounts.rs +++ b/sdk-libs/sdk/src/instruction/pack_accounts.rs @@ -7,17 +7,17 @@ use crate::{ #[derive(Default, Debug)] pub struct PackedAccounts { - pre_accounts: Vec, + pub pre_accounts: Vec, system_accounts: Vec, next_index: u8, map: HashMap, } impl PackedAccounts { - pub fn new_with_system_accounts(config: SystemAccountMetaConfig) -> Self { + pub fn new_with_system_accounts(config: SystemAccountMetaConfig) -> crate::error::Result { let mut remaining_accounts = PackedAccounts::default(); - remaining_accounts.add_system_accounts(config); - remaining_accounts + remaining_accounts.add_system_accounts(config)?; + Ok(remaining_accounts) } pub fn add_pre_accounts_signer(&mut self, pubkey: Pubkey) { @@ -40,9 +40,24 @@ impl PackedAccounts { self.pre_accounts.push(account_meta); } - pub fn add_system_accounts(&mut self, config: SystemAccountMetaConfig) { + pub fn add_pre_accounts_metas(&mut self, account_metas: &[AccountMeta]) { + self.pre_accounts.extend_from_slice(account_metas); + } + + pub fn add_system_accounts( + &mut self, + config: SystemAccountMetaConfig, + ) -> crate::error::Result<()> { self.system_accounts .extend(get_light_system_account_metas(config)); + // note cpi context account is part of the system accounts + /* if let Some(pubkey) = config.cpi_context { + if self.next_index != 0 { + return Err(crate::error::LightSdkError::CpiContextOrderingViolation); + } + self.insert_or_get(pubkey); + }*/ + Ok(()) } /// Returns the index of the provided `pubkey` in the collection. @@ -66,21 +81,33 @@ impl PackedAccounts { is_signer: bool, is_writable: bool, ) -> u8 { - self.map - .entry(pubkey) - .or_insert_with(|| { + match self.map.get_mut(&pubkey) { + Some((index, entry)) => { + if !entry.is_writable { + entry.is_writable = is_writable; + } + if !entry.is_signer { + entry.is_signer = is_signer; + } + *index + } + None => { let index = self.next_index; self.next_index += 1; - ( - index, - AccountMeta { - pubkey, - is_signer, - is_writable, - }, - ) - }) - .0 + self.map.insert( + pubkey, + ( + index, + AccountMeta { + pubkey, + is_signer, + is_writable, + }, + ), + ); + index + } + } } fn hash_set_accounts_to_metas(&self) -> Vec { @@ -118,6 +145,13 @@ impl PackedAccounts { packed_accounts_start_offset, ) } + + pub fn packed_pubkeys(&self) -> Vec { + self.hash_set_accounts_to_metas() + .iter() + .map(|meta| meta.pubkey) + .collect() + } } #[cfg(test)] diff --git a/sdk-libs/token-client/Cargo.toml b/sdk-libs/token-client/Cargo.toml new file mode 100644 index 0000000000..f29ed7c219 --- /dev/null +++ b/sdk-libs/token-client/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "light-token-client" +version = { workspace = true } +edition = { workspace = true } + +[features] + +[dependencies] +# Light Protocol dependencies +light-compressed-token-types = { workspace = true } +light-compressed-account = { workspace = true } +light-ctoken-types = { workspace = true } +light-sdk = { workspace = true } +light-client = { workspace = true, features = ["v2"] } +light-compressed-token-sdk = { workspace = true } + +# Solana dependencies +solana-pubkey = { workspace = true, features = ["sha2", "curve25519"] } +solana-instruction = { workspace = true } +solana-msg = { workspace = true } +solana-keypair = { workspace = true } +solana-signer = { workspace = true } +solana-signature = { workspace = true } +spl-token-2022 = { workspace = true } +spl-pod = { workspace = true } +borsh = { workspace = true } diff --git a/sdk-libs/token-client/src/actions/create_mint.rs b/sdk-libs/token-client/src/actions/create_mint.rs new file mode 100644 index 0000000000..8a03e1dc86 --- /dev/null +++ b/sdk-libs/token-client/src/actions/create_mint.rs @@ -0,0 +1,56 @@ +use light_client::{ + indexer::Indexer, + rpc::{Rpc, RpcError}, +}; +use light_ctoken_types::instructions::extensions::TokenMetadataInstructionData; +use solana_keypair::Keypair; +use solana_pubkey::Pubkey; +use solana_signature::Signature; +use solana_signer::Signer; + +use crate::instructions::create_mint::create_compressed_mint_instruction; + +/// Create a compressed mint and send the transaction. +/// +/// # Arguments +/// * `rpc` - RPC client with indexer capabilities +/// * `mint_seed` - Keypair used to derive the mint PDA (must sign the transaction) +/// * `decimals` - Number of decimal places for the token +/// * `mint_authority` - Authority that can mint tokens +/// * `freeze_authority` - Optional authority that can freeze tokens +/// * `payer` - Transaction fee payer keypair +/// * `metadata` - Optional metadata for the token +/// +/// # Returns +/// `Result` - The transaction signature +pub async fn create_mint( + rpc: &mut R, + mint_seed: &Keypair, + decimals: u8, + mint_authority: Pubkey, + freeze_authority: Option, + metadata: Option, + payer: &Keypair, +) -> Result { + // Create the instruction + let ix = create_compressed_mint_instruction( + rpc, + mint_seed, + decimals, + mint_authority, + freeze_authority, + payer.pubkey(), + metadata, + ) + .await?; + + // Determine signers (deduplicate if mint_signer and payer are the same) + let mut signers = vec![payer]; + if mint_seed.pubkey() != payer.pubkey() { + signers.push(mint_seed); + } + + // Send the transaction + rpc.create_and_send_transaction(&[ix], &payer.pubkey(), &signers) + .await +} diff --git a/sdk-libs/token-client/src/actions/create_spl_mint.rs b/sdk-libs/token-client/src/actions/create_spl_mint.rs new file mode 100644 index 0000000000..598b68a6e3 --- /dev/null +++ b/sdk-libs/token-client/src/actions/create_spl_mint.rs @@ -0,0 +1,67 @@ +use std::collections::HashSet; + +use light_client::{ + indexer::Indexer, + rpc::{Rpc, RpcError}, +}; +use solana_keypair::Keypair; +use solana_signature::Signature; +use solana_signer::Signer; + +use crate::instructions::create_spl_mint::create_spl_mint_instruction; + +/// Creates an SPL mint from a compressed mint and sends the transaction +/// +/// This function: +/// - Creates the create_spl_mint instruction using the instruction helper +/// - Handles signer deduplication (payer and mint_authority may be the same) +/// - Builds and sends the transaction +/// - Returns the transaction signature +/// +/// # Arguments +/// * `rpc` - RPC client with indexer access +/// * `compressed_mint_address` - Address of the compressed mint to convert to SPL mint +/// * `mint_seed` - Keypair used as seed for the SPL mint PDA +/// * `mint_authority` - Keypair that can mint tokens (must be able to sign) +/// * `payer` - Keypair for transaction fees (must be able to sign) +/// +/// # Returns +/// Returns the transaction signature on success +pub async fn create_spl_mint( + rpc: &mut R, + compressed_mint_address: [u8; 32], + mint_seed: &Keypair, + mint_authority: &Keypair, + payer: &Keypair, +) -> Result { + // Create the instruction + let instruction = create_spl_mint_instruction( + rpc, + compressed_mint_address, + mint_seed, + mint_authority.pubkey(), + payer.pubkey(), + ) + .await?; + + // Deduplicate signers (payer and mint_authority might be the same) + let mut unique_signers = HashSet::new(); + let mut signers = Vec::new(); + + // Always include payer + if unique_signers.insert(payer.pubkey()) { + signers.push(payer); + } + + // Include mint_authority if different from payer + if unique_signers.insert(mint_authority.pubkey()) { + signers.push(mint_authority); + } + + // Create and send the transaction + let signature = rpc + .create_and_send_transaction(&[instruction], &payer.pubkey(), &signers) + .await?; + + Ok(signature) +} diff --git a/sdk-libs/token-client/src/actions/mint_to_compressed.rs b/sdk-libs/token-client/src/actions/mint_to_compressed.rs new file mode 100644 index 0000000000..f3f12ad3b7 --- /dev/null +++ b/sdk-libs/token-client/src/actions/mint_to_compressed.rs @@ -0,0 +1,51 @@ +use light_client::{ + indexer::Indexer, + rpc::{Rpc, RpcError}, +}; +use light_ctoken_types::instructions::mint_to_compressed::Recipient; +use solana_keypair::Keypair; +use solana_pubkey::Pubkey; +use solana_signature::Signature; +use solana_signer::Signer; + +use crate::instructions::mint_to_compressed::mint_to_compressed_instruction; + +/// Mints compressed tokens to recipients using a higher-level action +/// +/// # Arguments +/// * `rpc` - RPC client with indexer access +/// * `spl_mint_pda` - The SPL mint PDA for the compressed mint +/// * `recipients` - Vector of Recipient structs containing recipient and amount +/// * `mint_authority` - Authority that can mint tokens +/// * `payer` - Account that pays for the transaction +/// * `lamports` - Optional lamports to add to new token accounts +pub async fn mint_to_compressed( + rpc: &mut R, + spl_mint_pda: Pubkey, + recipients: Vec, + mint_authority: &Keypair, + payer: &Keypair, + lamports: Option, +) -> Result { + // Create the instruction + let instruction = mint_to_compressed_instruction( + rpc, + spl_mint_pda, + recipients, + mint_authority.pubkey(), + payer.pubkey(), + lamports, + ) + .await?; + + // Determine signers (deduplicate if payer and mint_authority are the same) + let signers: Vec<&Keypair> = if payer.pubkey() == mint_authority.pubkey() { + vec![payer] + } else { + vec![payer, mint_authority] + }; + + // Send the transaction + rpc.create_and_send_transaction(&[instruction], &payer.pubkey(), &signers) + .await +} diff --git a/sdk-libs/token-client/src/actions/mod.rs b/sdk-libs/token-client/src/actions/mod.rs new file mode 100644 index 0000000000..ff547ec9ff --- /dev/null +++ b/sdk-libs/token-client/src/actions/mod.rs @@ -0,0 +1,7 @@ +mod create_mint; +mod create_spl_mint; +mod mint_to_compressed; +pub mod transfer2; +pub use create_mint::*; +pub use create_spl_mint::*; +pub use mint_to_compressed::*; diff --git a/sdk-libs/token-client/src/actions/transfer2/compress.rs b/sdk-libs/token-client/src/actions/transfer2/compress.rs new file mode 100644 index 0000000000..19c09c059b --- /dev/null +++ b/sdk-libs/token-client/src/actions/transfer2/compress.rs @@ -0,0 +1,72 @@ +use light_client::{ + indexer::Indexer, + rpc::{Rpc, RpcError}, +}; +use solana_keypair::Keypair; +use solana_pubkey::Pubkey; +use solana_signature::Signature; +use solana_signer::Signer; +use spl_pod::bytemuck::pod_from_bytes; +use spl_token_2022::pod::PodAccount; + +use crate::instructions::transfer2::{ + create_generic_transfer2_instruction, CompressInput, Transfer2InstructionType, +}; + +/// Create a compression instruction to convert SPL tokens to compressed tokens. +/// +/// # Arguments +/// * `rpc` - RPC client with indexer capabilities +/// * `solana_token_account` - The SPL token account to compress from +/// * `amount` - Amount of tokens to compress +/// * `to` - Recipient pubkey for the compressed tokens +/// * `authority` - Authority that can spend from the token account +/// * `payer` - Transaction fee payer +/// +/// # Returns +/// `Result` - The compression instruction +pub async fn compress( + rpc: &mut R, + solana_token_account: Pubkey, + amount: u64, + to: Pubkey, + authority: &Keypair, + payer: &Keypair, +) -> Result { + // Get mint from token account + let token_account_info = rpc + .get_account(solana_token_account) + .await? + .ok_or_else(|| RpcError::CustomError("Token account not found".to_string()))?; + + let pod_account = pod_from_bytes::(&token_account_info.data) + .map_err(|e| RpcError::CustomError(format!("Failed to parse token account: {}", e)))?; + + let output_queue = rpc.get_random_state_tree_info()?.get_output_pubkey()?; + + let mint = pod_account.mint; + + let ix = create_generic_transfer2_instruction( + rpc, + vec![Transfer2InstructionType::Compress(CompressInput { + compressed_token_account: None, + solana_token_account, + to, + mint, + amount, + authority: authority.pubkey(), + output_queue, + })], + payer.pubkey(), + ) + .await + .map_err(|e| RpcError::CustomError(e.to_string()))?; + + let mut signers = vec![payer]; + if authority.pubkey() != payer.pubkey() { + signers.push(authority); + } + + rpc.create_and_send_transaction(&[ix], &payer.pubkey(), &signers) + .await +} diff --git a/sdk-libs/token-client/src/actions/transfer2/decompress.rs b/sdk-libs/token-client/src/actions/transfer2/decompress.rs new file mode 100644 index 0000000000..b8ad8922cd --- /dev/null +++ b/sdk-libs/token-client/src/actions/transfer2/decompress.rs @@ -0,0 +1,54 @@ +use light_client::{ + indexer::{CompressedTokenAccount, Indexer}, + rpc::{Rpc, RpcError}, +}; +use solana_keypair::Keypair; +use solana_pubkey::Pubkey; +use solana_signature::Signature; +use solana_signer::Signer; + +use crate::instructions::transfer2::{ + create_generic_transfer2_instruction, DecompressInput, Transfer2InstructionType, +}; + +/// Decompress compressed tokens to SPL tokens and send the transaction. +/// +/// # Arguments +/// * `rpc` - RPC client with indexer capabilities +/// * `compressed_token_account` - Slice of compressed token accounts to decompress +/// * `decompress_amount` - Amount of tokens to decompress +/// * `solana_token_account` - The SPL token account to receive the decompressed tokens +/// * `authority` - Authority that can spend from the compressed token account +/// * `payer` - Transaction fee payer keypair +/// +/// # Returns +/// `Result` - The transaction signature +pub async fn decompress( + rpc: &mut R, + compressed_token_account: &[CompressedTokenAccount], + decompress_amount: u64, + solana_token_account: Pubkey, + authority: &Keypair, + payer: &Keypair, +) -> Result { + let ix = create_generic_transfer2_instruction( + rpc, + vec![Transfer2InstructionType::Decompress(DecompressInput { + compressed_token_account, + decompress_amount, + solana_token_account, + amount: decompress_amount, + })], + payer.pubkey(), + ) + .await + .map_err(|e| RpcError::CustomError(e.to_string()))?; + + let mut signers = vec![payer]; + if authority.pubkey() != payer.pubkey() { + signers.push(authority); + } + + rpc.create_and_send_transaction(&[ix], &payer.pubkey(), &signers) + .await +} diff --git a/sdk-libs/token-client/src/actions/transfer2/mod.rs b/sdk-libs/token-client/src/actions/transfer2/mod.rs new file mode 100644 index 0000000000..9e8735cac2 --- /dev/null +++ b/sdk-libs/token-client/src/actions/transfer2/mod.rs @@ -0,0 +1,7 @@ +mod compress; +mod decompress; +mod transfer; + +pub use compress::*; +pub use decompress::*; +pub use transfer::*; diff --git a/sdk-libs/token-client/src/actions/transfer2/transfer.rs b/sdk-libs/token-client/src/actions/transfer2/transfer.rs new file mode 100644 index 0000000000..7fa00e3863 --- /dev/null +++ b/sdk-libs/token-client/src/actions/transfer2/transfer.rs @@ -0,0 +1,53 @@ +use light_client::{ + indexer::{CompressedTokenAccount, Indexer}, + rpc::{Rpc, RpcError}, +}; +use solana_keypair::Keypair; +use solana_pubkey::Pubkey; +use solana_signature::Signature; +use solana_signer::Signer; + +use crate::instructions::transfer2::{ + create_generic_transfer2_instruction, Transfer2InstructionType, TransferInput, +}; + +/// Transfer compressed tokens between compressed accounts and send the transaction. +/// +/// # Arguments +/// * `rpc` - RPC client with indexer capabilities +/// * `compressed_token_account` - Slice of compressed token accounts to transfer from +/// * `to` - Recipient pubkey for the compressed tokens +/// * `amount` - Amount of tokens to transfer +/// * `authority` - Authority that can spend from the compressed token account +/// * `payer` - Transaction fee payer keypair +/// +/// # Returns +/// `Result` - The transaction signature +pub async fn transfer( + rpc: &mut R, + compressed_token_account: &[CompressedTokenAccount], + to: Pubkey, + amount: u64, + authority: &Keypair, + payer: &Keypair, +) -> Result { + let ix = create_generic_transfer2_instruction( + rpc, + vec![Transfer2InstructionType::Transfer(TransferInput { + compressed_token_account, + to, + amount, + })], + payer.pubkey(), + ) + .await + .map_err(|e| RpcError::CustomError(e.to_string()))?; + + let mut signers = vec![payer]; + if authority.pubkey() != payer.pubkey() { + signers.push(authority); + } + + rpc.create_and_send_transaction(&[ix], &payer.pubkey(), &signers) + .await +} diff --git a/sdk-libs/token-client/src/instructions/create_mint.rs b/sdk-libs/token-client/src/instructions/create_mint.rs new file mode 100644 index 0000000000..adf8a5aa60 --- /dev/null +++ b/sdk-libs/token-client/src/instructions/create_mint.rs @@ -0,0 +1,92 @@ +use light_client::{ + indexer::Indexer, + rpc::{Rpc, RpcError}, +}; +use light_compressed_token_sdk::instructions::create_compressed_mint::{ + create_compressed_mint, derive_compressed_mint_address, CreateCompressedMintInputs, +}; +use light_ctoken_types::{ + instructions::extensions::{ + token_metadata::TokenMetadataInstructionData, ExtensionInstructionData, + }, + COMPRESSED_MINT_SEED, +}; +use solana_instruction::Instruction; +use solana_keypair::Keypair; +use solana_pubkey::Pubkey; +use solana_signer::Signer; + +/// Create a compressed mint instruction with automatic setup. +/// +/// # Arguments +/// * `rpc` - RPC client with indexer capabilities +/// * `mint_seed` - Keypair used to derive the mint PDA +/// * `decimals` - Number of decimal places for the token +/// * `mint_authority` - Authority that can mint tokens +/// * `freeze_authority` - Optional authority that can freeze tokens +/// * `payer` - Fee payer pubkey +/// * `metadata` - Optional metadata for the token +/// +/// # Returns +/// `Result` - The compressed mint creation instruction +pub async fn create_compressed_mint_instruction( + rpc: &mut R, + mint_seed: &Keypair, + decimals: u8, + mint_authority: Pubkey, + freeze_authority: Option, + payer: Pubkey, + metadata: Option, +) -> Result { + // Get address tree and output queue from RPC + let address_tree_pubkey = rpc.get_address_tree_v2().tree; + + let output_queue = rpc.get_random_state_tree_info()?.queue; + + // Derive compressed mint address using utility function + let compressed_mint_address = + derive_compressed_mint_address(&mint_seed.pubkey(), &address_tree_pubkey); + + // Find mint bump for the instruction + let (_, mint_bump) = Pubkey::find_program_address( + &[COMPRESSED_MINT_SEED, mint_seed.pubkey().as_ref()], + &Pubkey::new_from_array(light_ctoken_types::COMPRESSED_TOKEN_PROGRAM_ID), + ); + + // Create extensions if metadata is provided + let extensions = metadata.map(|meta| vec![ExtensionInstructionData::TokenMetadata(meta)]); + + // Get validity proof for address creation + let rpc_result = rpc + .get_validity_proof( + vec![], + vec![light_client::indexer::AddressWithTree { + address: compressed_mint_address, + tree: address_tree_pubkey, + }], + None, + ) + .await? + .value; + + let address_merkle_tree_root_index = rpc_result.addresses[0].root_index; + + // Create instruction using the existing SDK function + let inputs = CreateCompressedMintInputs { + decimals, + mint_authority, + freeze_authority, + proof: rpc_result.proof.0.unwrap(), + mint_bump, + address_merkle_tree_root_index, + mint_signer: mint_seed.pubkey(), + payer, + address_tree_pubkey, + output_queue, + extensions, + version: 0, + }; + + create_compressed_mint(inputs) + .map_err(|e| RpcError::CustomError(format!("Token SDK error: {:?}", e))) +} diff --git a/sdk-libs/token-client/src/instructions/create_spl_mint.rs b/sdk-libs/token-client/src/instructions/create_spl_mint.rs new file mode 100644 index 0000000000..195235907b --- /dev/null +++ b/sdk-libs/token-client/src/instructions/create_spl_mint.rs @@ -0,0 +1,105 @@ +use borsh::BorshDeserialize; +use light_client::{ + indexer::Indexer, + rpc::{Rpc, RpcError}, +}; +use light_compressed_token_sdk::instructions::{ + create_spl_mint_instruction as sdk_create_spl_mint_instruction, find_spl_mint_address, + CreateSplMintInputs, +}; +use light_ctoken_types::{ + instructions::mint_to_compressed::CompressedMintInputs, state::CompressedMint, +}; +use solana_instruction::Instruction; +use solana_keypair::Keypair; +use solana_pubkey::Pubkey; +use solana_signer::Signer; + +/// Creates a create_spl_mint instruction with automatic RPC integration +/// +/// This function automatically: +/// - Fetches the compressed mint account data +/// - Gets validity proof for the compressed mint +/// - Derives the necessary PDAs and tree information +/// - Constructs the complete instruction +/// +/// # Arguments +/// * `rpc` - RPC client with indexer access +/// * `compressed_mint_address` - Address of the compressed mint to convert to SPL mint +/// * `mint_seed` - Keypair used as seed for the SPL mint PDA +/// * `mint_authority` - Authority that can mint tokens +/// * `payer` - Transaction fee payer +/// +/// # Returns +/// Returns a configured `Instruction` ready for transaction execution +pub async fn create_spl_mint_instruction( + rpc: &mut R, + compressed_mint_address: [u8; 32], + mint_seed: &Keypair, + mint_authority: Pubkey, + payer: Pubkey, +) -> Result { + // Get the compressed mint account + let compressed_mint_account = rpc + .get_compressed_account(compressed_mint_address, None) + .await? + .value; + + // Deserialize the compressed mint data + let compressed_mint: CompressedMint = BorshDeserialize::deserialize( + &mut compressed_mint_account + .data + .as_ref() + .ok_or_else(|| { + RpcError::CustomError("Compressed mint account has no data".to_string()) + })? + .data + .as_slice(), + ) + .map_err(|e| RpcError::CustomError(format!("Failed to deserialize compressed mint: {}", e)))?; + + // Get validity proof for the compressed mint + let proof_result = rpc + .get_validity_proof(vec![compressed_mint_account.hash], vec![], None) + .await? + .value; + + // Derive SPL mint PDA and bump + let (_spl_mint_pda, mint_bump) = find_spl_mint_address(&mint_seed.pubkey()); + + // Get tree and queue information + let input_tree = compressed_mint_account.tree_info.tree; + let input_queue = compressed_mint_account.tree_info.queue; + + // Get a separate output queue for the new compressed mint state + let output_tree_info = rpc.get_random_state_tree_info()?; + let output_queue = output_tree_info.queue; + + // Prepare compressed mint inputs + let compressed_mint_inputs = CompressedMintInputs { + leaf_index: compressed_mint_account.leaf_index, + prove_by_index: true, + root_index: proof_result.accounts[0] + .root_index + .root_index() + .unwrap_or_default(), + address: compressed_mint_address, + compressed_mint_input: compressed_mint, + }; + + // Create the instruction using the SDK function + let instruction = sdk_create_spl_mint_instruction(CreateSplMintInputs { + mint_signer: mint_seed.pubkey(), + mint_bump, + compressed_mint_inputs, + proof: proof_result.proof, + payer, + input_merkle_tree: input_tree, + input_output_queue: input_queue, + output_queue, + mint_authority, + }) + .map_err(|e| RpcError::CustomError(format!("Failed to create SPL mint instruction: {}", e)))?; + + Ok(instruction) +} diff --git a/sdk-libs/token-client/src/instructions/mint_to_compressed.rs b/sdk-libs/token-client/src/instructions/mint_to_compressed.rs new file mode 100644 index 0000000000..d0f1a71641 --- /dev/null +++ b/sdk-libs/token-client/src/instructions/mint_to_compressed.rs @@ -0,0 +1,89 @@ +use borsh::BorshDeserialize; +use light_client::{ + indexer::Indexer, + rpc::{Rpc, RpcError}, +}; +use light_compressed_token_sdk::{ + instructions::{ + create_mint_to_compressed_instruction, derive_compressed_mint_from_spl_mint, + DecompressedMintConfig, MintToCompressedInputs, + }, + token_pool::find_token_pool_pda_with_index, +}; +use light_ctoken_types::{ + instructions::mint_to_compressed::{CompressedMintInputs, Recipient}, + state::CompressedMint, +}; +use solana_instruction::Instruction; +use solana_pubkey::Pubkey; + +/// Creates a mint_to_compressed instruction that mints compressed tokens to recipients +pub async fn mint_to_compressed_instruction( + rpc: &mut R, + spl_mint_pda: Pubkey, + recipients: Vec, + mint_authority: Pubkey, + payer: Pubkey, + lamports: Option, +) -> Result { + // Derive compressed mint address from SPL mint PDA + let address_tree_pubkey = rpc.get_address_tree_v2().tree; + let compressed_mint_address = + derive_compressed_mint_from_spl_mint(&spl_mint_pda, &address_tree_pubkey); + + // Get the compressed mint account + let compressed_mint_account = rpc + .get_compressed_account(compressed_mint_address, None) + .await? + .value; + + // Deserialize the compressed mint + let compressed_mint: CompressedMint = + BorshDeserialize::deserialize(&mut compressed_mint_account.data.unwrap().data.as_slice()) + .map_err(|e| { + RpcError::CustomError(format!("Failed to deserialize compressed mint: {}", e)) + })?; + + // Get state tree info for outputs + let state_tree_info = rpc.get_random_state_tree_info()?; + + // Create decompressed mint config if mint is decompressed + let decompressed_mint_config = if compressed_mint.is_decompressed { + let (token_pool_pda, _) = find_token_pool_pda_with_index(&spl_mint_pda, 0); + Some(DecompressedMintConfig { + mint_pda: spl_mint_pda, + token_pool_pda, + token_program: spl_token_2022::ID, + }) + } else { + None + }; + + // Prepare compressed mint inputs + let compressed_mint_inputs = CompressedMintInputs { + prove_by_index: true, + leaf_index: compressed_mint_account.leaf_index, + root_index: 0, + address: compressed_mint_address, + compressed_mint_input: compressed_mint, + }; + + // Create the instruction + create_mint_to_compressed_instruction(MintToCompressedInputs { + compressed_mint_inputs, + lamports, + recipients, + mint_authority, + payer, + state_merkle_tree: compressed_mint_account.tree_info.tree, + output_queue: compressed_mint_account.tree_info.queue, + state_tree_pubkey: state_tree_info.tree, + decompressed_mint_config, + }) + .map_err(|e| { + RpcError::CustomError(format!( + "Failed to create mint_to_compressed instruction: {:?}", + e + )) + }) +} diff --git a/sdk-libs/token-client/src/instructions/mod.rs b/sdk-libs/token-client/src/instructions/mod.rs new file mode 100644 index 0000000000..c75897ee8f --- /dev/null +++ b/sdk-libs/token-client/src/instructions/mod.rs @@ -0,0 +1,4 @@ +pub mod create_mint; +pub mod create_spl_mint; +pub mod mint_to_compressed; +pub mod transfer2; diff --git a/sdk-libs/token-client/src/instructions/transfer2.rs b/sdk-libs/token-client/src/instructions/transfer2.rs new file mode 100644 index 0000000000..b8157512d8 --- /dev/null +++ b/sdk-libs/token-client/src/instructions/transfer2.rs @@ -0,0 +1,291 @@ +use light_client::{ + indexer::{CompressedTokenAccount, Indexer}, + rpc::Rpc, +}; +use light_compressed_token_sdk::{ + account2::CTokenAccount2, + error::TokenSdkError, + instructions::transfer2::{ + account_metas::Transfer2AccountsMetaConfig, create_transfer2_instruction, Transfer2Config, + Transfer2Inputs, + }, +}; +use light_ctoken_types::instructions::transfer2::MultiInputTokenDataWithContext; +use light_sdk::instruction::{PackedAccounts, PackedStateTreeInfo}; +use solana_instruction::Instruction; +use solana_pubkey::Pubkey; + +pub fn pack_input_token_account( + account: &CompressedTokenAccount, + tree_info: &PackedStateTreeInfo, + packed_accounts: &mut PackedAccounts, + in_lamports: &mut Vec, +) -> MultiInputTokenDataWithContext { + let delegate_index = if let Some(delegate) = account.token.delegate { + packed_accounts.insert_or_get_read_only(delegate) // TODO: cover delegated transfer + } else { + 0 + }; + if account.account.lamports != 0 { + in_lamports.push(account.account.lamports); + } + MultiInputTokenDataWithContext { + amount: account.token.amount, + merkle_context: light_compressed_account::compressed_account::PackedMerkleContext { + merkle_tree_pubkey_index: tree_info.merkle_tree_pubkey_index, + queue_pubkey_index: tree_info.queue_pubkey_index, + leaf_index: tree_info.leaf_index, + prove_by_index: tree_info.prove_by_index, + }, + root_index: tree_info.root_index, + mint: packed_accounts.insert_or_get_read_only(account.token.mint), + owner: packed_accounts.insert_or_get_config(account.token.owner, true, false), + with_delegate: account.token.delegate.is_some(), + delegate: delegate_index, + version: 2, // V2 for batched Merkle trees + } +} + +pub async fn create_decompress_instruction( + rpc: &mut R, + compressed_token_account: &[CompressedTokenAccount], + decompress_amount: u64, + solana_token_account: Pubkey, + payer: Pubkey, +) -> Result { + create_generic_transfer2_instruction( + rpc, + vec![Transfer2InstructionType::Decompress(DecompressInput { + compressed_token_account, + decompress_amount, + solana_token_account, + amount: decompress_amount, + })], + payer, + ) + .await +} +#[derive(Debug, Clone, PartialEq)] +pub struct TransferInput<'a> { + pub compressed_token_account: &'a [CompressedTokenAccount], + pub to: Pubkey, + pub amount: u64, +} + +#[derive(Debug, Clone, PartialEq)] +pub struct DecompressInput<'a> { + pub compressed_token_account: &'a [CompressedTokenAccount], + pub decompress_amount: u64, + pub solana_token_account: Pubkey, + pub amount: u64, +} + +#[derive(Debug, Clone, PartialEq)] +pub struct CompressInput<'a> { + pub compressed_token_account: Option<&'a [CompressedTokenAccount]>, + pub solana_token_account: Pubkey, + pub to: Pubkey, + pub mint: Pubkey, + pub amount: u64, + pub authority: Pubkey, + pub output_queue: Pubkey, +} + +#[derive(Debug, Clone, PartialEq)] +pub enum Transfer2InstructionType<'a> { + Compress(CompressInput<'a>), + Decompress(DecompressInput<'a>), + Transfer(TransferInput<'a>), +} + +// Note doesn't support multiple signers. +pub async fn create_generic_transfer2_instruction( + rpc: &mut R, + actions: Vec>, + payer: Pubkey, +) -> Result { + let mut hashes = Vec::new(); + actions.iter().for_each(|account| match account { + Transfer2InstructionType::Compress(_) => {} + Transfer2InstructionType::Decompress(input) => input + .compressed_token_account + .iter() + .for_each(|account| hashes.push(account.account.hash)), + Transfer2InstructionType::Transfer(input) => input + .compressed_token_account + .iter() + .for_each(|account| hashes.push(account.account.hash)), + }); + let rpc_proof_result = rpc + .get_validity_proof(hashes, vec![], None) + .await + .unwrap() + .value; + + let mut packed_tree_accounts = PackedAccounts::default(); + // tree infos must be packed before packing the token input accounts + let packed_tree_infos = rpc_proof_result.pack_tree_infos(&mut packed_tree_accounts); + let mut inputs_offset = 0; + let mut in_lamports = Vec::new(); + let mut out_lamports = Vec::new(); + let mut token_accounts = Vec::new(); + for action in actions { + match action { + Transfer2InstructionType::Compress(input) => { + let mut token_account = + if let Some(input_token_account) = input.compressed_token_account { + let token_data = input_token_account + .iter() + .zip( + packed_tree_infos + .state_trees + .as_ref() + .unwrap() + .packed_tree_infos[inputs_offset..] + .iter(), + ) + .map(|(account, rpc_account)| { + if input.to != account.token.owner { + return Err(TokenSdkError::InvalidCompressInputOwner); + } + Ok(pack_input_token_account( + account, + rpc_account, + &mut packed_tree_accounts, + &mut in_lamports, + )) + }) + .collect::, _>>()?; + inputs_offset += token_data.len(); + CTokenAccount2::new( + token_data, + packed_tree_accounts.insert_or_get(input.output_queue), + )? + } else { + CTokenAccount2::new_empty( + packed_tree_accounts.insert_or_get(input.to), + packed_tree_accounts.insert_or_get(input.mint), + packed_tree_accounts.insert_or_get(input.output_queue), + ) + }; + + let source_index = packed_tree_accounts.insert_or_get(input.solana_token_account); + let authority_index = + packed_tree_accounts.insert_or_get_config(input.authority, true, false); + token_account.compress(input.amount, source_index, authority_index)?; + token_accounts.push(token_account); + } + Transfer2InstructionType::Decompress(input) => { + let token_data = input + .compressed_token_account + .iter() + .zip( + packed_tree_infos + .state_trees + .as_ref() + .unwrap() + .packed_tree_infos[inputs_offset..] + .iter(), + ) + .map(|(account, rpc_account)| { + pack_input_token_account( + account, + rpc_account, + &mut packed_tree_accounts, + &mut in_lamports, + ) + }) + .collect::>(); + inputs_offset += token_data.len(); + let mut token_account = CTokenAccount2::new( + token_data, + packed_tree_infos + .state_trees + .as_ref() + .unwrap() + .output_tree_index, + )?; + let recipient_index = + packed_tree_accounts.insert_or_get(input.solana_token_account); + token_account.decompress(input.decompress_amount, recipient_index)?; + out_lamports.push( + input + .compressed_token_account + .iter() + .map(|account| account.account.lamports) + .sum::(), + ); + + token_accounts.push(token_account); + } + Transfer2InstructionType::Transfer(input) => { + let token_data = input + .compressed_token_account + .iter() + .zip( + packed_tree_infos + .state_trees + .as_ref() + .unwrap() + .packed_tree_infos[inputs_offset..] + .iter(), + ) + .map(|(account, rpc_account)| { + pack_input_token_account( + account, + rpc_account, + &mut packed_tree_accounts, + &mut in_lamports, + ) + }) + .collect::>(); + inputs_offset += token_data.len(); + let mut token_account = CTokenAccount2::new( + token_data, + packed_tree_infos + .state_trees + .as_ref() + .unwrap() + .output_tree_index, + )?; + let recipient_index = packed_tree_accounts.insert_or_get(input.to); + let recipient_token_account = + token_account.transfer(recipient_index, input.amount, None)?; + // all lamports go to the sender. + out_lamports.push( + input + .compressed_token_account + .iter() + .map(|account| account.account.lamports) + .sum::(), + ); + // For consistency add 0 lamports for the recipient. + out_lamports.push(0); + token_accounts.push(token_account); + token_accounts.push(recipient_token_account); + } + } + } + let packed_accounts = packed_tree_accounts.to_account_metas().0; + let inputs = Transfer2Inputs { + validity_proof: rpc_proof_result.proof, + transfer_config: Transfer2Config::default(), + meta_config: Transfer2AccountsMetaConfig { + fee_payer: Some(payer), + packed_accounts: Some(packed_accounts), + ..Default::default() + }, + in_lamports: if in_lamports.is_empty() { + None + } else { + Some(in_lamports) + }, + out_lamports: if out_lamports.iter().all(|lamports| *lamports == 0) { + None + } else { + Some(out_lamports) + }, + token_accounts, + }; + create_transfer2_instruction(inputs) +} diff --git a/sdk-libs/token-client/src/lib.rs b/sdk-libs/token-client/src/lib.rs new file mode 100644 index 0000000000..8f5d67d6dc --- /dev/null +++ b/sdk-libs/token-client/src/lib.rs @@ -0,0 +1,2 @@ +pub mod actions; +pub mod instructions;