diff --git a/.dockerignore b/.dockerignore index 0fbe460c..11f1a350 100644 --- a/.dockerignore +++ b/.dockerignore @@ -26,11 +26,20 @@ Thumbs.db ci_scripts/ # Documentation +docs/ *.md !README.md -# Configs (copy selectively if needed) +# Non-build project files +completions/ configs/ - -# License +Justfile +clippy.toml +rustfmt.toml +flake.nix +flake.lock LICENSE + +# Docker compose files (not needed inside build) +docker-compose*.yml +**/docker-compose*.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eb4cc791..858a43c9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -156,33 +156,35 @@ jobs: RUST_LOG: "info" run: cargo nextest run -p integration_tests -- --skip tps_test --skip indexer - integration-tests-indexer: - runs-on: ubuntu-latest - timeout-minutes: 60 - steps: - - uses: actions/checkout@v5 - with: - ref: ${{ github.head_ref }} + # # TODO: Bring this back once we find the source of the errors. + # # + # integration-tests-indexer: + # runs-on: ubuntu-latest + # timeout-minutes: 60 + # steps: + # - uses: actions/checkout@v5 + # with: + # ref: ${{ github.head_ref }} - - uses: ./.github/actions/install-system-deps + # - uses: ./.github/actions/install-system-deps - - uses: ./.github/actions/install-risc0 + # - uses: ./.github/actions/install-risc0 - - uses: ./.github/actions/install-logos-blockchain-circuits - with: - github-token: ${{ secrets.GITHUB_TOKEN }} + # - uses: ./.github/actions/install-logos-blockchain-circuits + # with: + # github-token: ${{ secrets.GITHUB_TOKEN }} - - name: Install active toolchain - run: rustup install + # - name: Install active toolchain + # run: rustup install - - name: Install nextest - run: cargo install --locked cargo-nextest + # - name: Install nextest + # run: cargo install --locked cargo-nextest - - name: Run tests - env: - RISC0_DEV_MODE: "1" - RUST_LOG: "info" - run: cargo nextest run -p integration_tests indexer -- --skip tps_test + # - name: Run tests + # env: + # RISC0_DEV_MODE: "1" + # RUST_LOG: "info" + # run: cargo nextest run -p integration_tests indexer -- --skip tps_test valid-proof-test: runs-on: ubuntu-latest diff --git a/Cargo.lock b/Cargo.lock index 228ab10c..34962b1d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -727,6 +727,24 @@ dependencies = [ "syn 2.0.117", ] +[[package]] +name = "ata_core" +version = "0.1.0" +dependencies = [ + "nssa_core", + "risc0-zkvm", + "serde", +] + +[[package]] +name = "ata_program" +version = "0.1.0" +dependencies = [ + "ata_core", + "nssa_core", + "token_core", +] + [[package]] name = "atomic-polyfill" version = "1.0.3" @@ -1939,7 +1957,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ab67060fc6b8ef687992d439ca0fa36e7ed17e9a0b16b25b601e8757df720de" dependencies = [ "data-encoding", - "syn 1.0.109", + "syn 2.0.117", ] [[package]] @@ -3442,6 +3460,7 @@ dependencies = [ "serde_json", "storage", "tempfile", + "testnet_initial_state", "tokio", "url", ] @@ -3551,6 +3570,7 @@ name = "integration_tests" version = "0.1.0" dependencies = [ "anyhow", + "ata_core", "bytesize", "common", "env_logger", @@ -3568,6 +3588,7 @@ dependencies = [ "serde_json", "tempfile", "testcontainers", + "testnet_initial_state", "token_core", "tokio", "url", @@ -5882,6 +5903,8 @@ version = "0.1.0" dependencies = [ "amm_core", "amm_program", + "ata_core", + "ata_program", "nssa_core", "risc0-zkvm", "serde", @@ -5930,7 +5953,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a56d757972c98b346a9b766e3f02746cde6dd1cd1d1d563472929fdd74bec4d" dependencies = [ "anyhow", - "itertools 0.10.5", + "itertools 0.11.0", "proc-macro2", "quote", "syn 2.0.117", @@ -5943,7 +5966,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "27c6023962132f4b30eb4c172c91ce92d933da334c59c23cddee82358ddafb0b" dependencies = [ "anyhow", - "itertools 0.10.5", + "itertools 0.11.0", "proc-macro2", "quote", "syn 2.0.117", @@ -6024,7 +6047,7 @@ dependencies = [ "once_cell", "socket2", "tracing", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -7137,7 +7160,6 @@ name = "sequencer_core" version = "0.1.0" dependencies = [ "anyhow", - "base58", "bedrock_client", "borsh", "bytesize", @@ -7157,6 +7179,7 @@ dependencies = [ "serde_json", "storage", "tempfile", + "testnet_initial_state", "tokio", "url", ] @@ -7873,6 +7896,17 @@ dependencies = [ "uuid", ] +[[package]] +name = "testnet_initial_state" +version = "0.1.0" +dependencies = [ + "common", + "key_protocol", + "nssa", + "nssa_core", + "serde", +] + [[package]] name = "thiserror" version = "1.0.69" @@ -8649,6 +8683,7 @@ dependencies = [ "amm_core", "anyhow", "async-stream", + "ata_core", "base58", "clap", "common", @@ -8669,6 +8704,7 @@ dependencies = [ "serde", "serde_json", "sha2", + "testnet_initial_state", "thiserror 2.0.18", "token_core", "tokio", @@ -9038,6 +9074,15 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets 0.52.6", +] + [[package]] name = "windows-sys" version = "0.61.2" diff --git a/Cargo.toml b/Cargo.toml index 829a6539..c2853089 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,8 @@ members = [ "programs/amm", "programs/token/core", "programs/token", + "programs/associated_token_account/core", + "programs/associated_token_account", "sequencer/core", "sequencer/service", "sequencer/service/protocol", @@ -34,6 +36,7 @@ members = [ "examples/program_deployment/methods", "examples/program_deployment/methods/guest", "bedrock_client", + "testnet_initial_state", ] [workspace.dependencies] @@ -57,8 +60,11 @@ token_core = { path = "programs/token/core" } token_program = { path = "programs/token" } amm_core = { path = "programs/amm/core" } amm_program = { path = "programs/amm" } +ata_core = { path = "programs/associated_token_account/core" } +ata_program = { path = "programs/associated_token_account" } test_program_methods = { path = "test_program_methods" } bedrock_client = { path = "bedrock_client" } +testnet_initial_state = { path = "testnet_initial_state" } tokio = { version = "1.50", features = [ "net", diff --git a/artifacts/program_methods/amm.bin b/artifacts/program_methods/amm.bin index 09996a49..030daf3f 100644 Binary files a/artifacts/program_methods/amm.bin and b/artifacts/program_methods/amm.bin differ diff --git a/artifacts/program_methods/associated_token_account.bin b/artifacts/program_methods/associated_token_account.bin new file mode 100644 index 00000000..7b2c40d2 Binary files /dev/null and b/artifacts/program_methods/associated_token_account.bin differ diff --git a/artifacts/program_methods/authenticated_transfer.bin b/artifacts/program_methods/authenticated_transfer.bin index b197bd50..96335c4f 100644 Binary files a/artifacts/program_methods/authenticated_transfer.bin and b/artifacts/program_methods/authenticated_transfer.bin differ diff --git a/artifacts/program_methods/clock.bin b/artifacts/program_methods/clock.bin new file mode 100644 index 00000000..c3f8af15 Binary files /dev/null and b/artifacts/program_methods/clock.bin differ diff --git a/artifacts/program_methods/pinata.bin b/artifacts/program_methods/pinata.bin index ce4fbef3..2f3e006e 100644 Binary files a/artifacts/program_methods/pinata.bin and b/artifacts/program_methods/pinata.bin differ diff --git a/artifacts/program_methods/pinata_token.bin b/artifacts/program_methods/pinata_token.bin index ca76907a..0e4fabab 100644 Binary files a/artifacts/program_methods/pinata_token.bin and b/artifacts/program_methods/pinata_token.bin differ diff --git a/artifacts/program_methods/privacy_preserving_circuit.bin b/artifacts/program_methods/privacy_preserving_circuit.bin index 3222dbac..637de17c 100644 Binary files a/artifacts/program_methods/privacy_preserving_circuit.bin and b/artifacts/program_methods/privacy_preserving_circuit.bin differ diff --git a/artifacts/program_methods/token.bin b/artifacts/program_methods/token.bin index e2558538..e41c5eea 100644 Binary files a/artifacts/program_methods/token.bin and b/artifacts/program_methods/token.bin differ diff --git a/artifacts/test_program_methods/burner.bin b/artifacts/test_program_methods/burner.bin index 4b6a2cb4..b0cb3843 100644 Binary files a/artifacts/test_program_methods/burner.bin and b/artifacts/test_program_methods/burner.bin differ diff --git a/artifacts/test_program_methods/chain_caller.bin b/artifacts/test_program_methods/chain_caller.bin index e8de01e8..18e87c48 100644 Binary files a/artifacts/test_program_methods/chain_caller.bin and b/artifacts/test_program_methods/chain_caller.bin differ diff --git a/artifacts/test_program_methods/changer_claimer.bin b/artifacts/test_program_methods/changer_claimer.bin index fafeb5c3..07acc779 100644 Binary files a/artifacts/test_program_methods/changer_claimer.bin and b/artifacts/test_program_methods/changer_claimer.bin differ diff --git a/artifacts/test_program_methods/claimer.bin b/artifacts/test_program_methods/claimer.bin index ae11aec5..ef76d3a3 100644 Binary files a/artifacts/test_program_methods/claimer.bin and b/artifacts/test_program_methods/claimer.bin differ diff --git a/artifacts/test_program_methods/data_changer.bin b/artifacts/test_program_methods/data_changer.bin index 71acd89a..2e99ab4d 100644 Binary files a/artifacts/test_program_methods/data_changer.bin and b/artifacts/test_program_methods/data_changer.bin differ diff --git a/artifacts/test_program_methods/extra_output.bin b/artifacts/test_program_methods/extra_output.bin index fb59d75d..9cd29505 100644 Binary files a/artifacts/test_program_methods/extra_output.bin and b/artifacts/test_program_methods/extra_output.bin differ diff --git a/artifacts/test_program_methods/malicious_authorization_changer.bin b/artifacts/test_program_methods/malicious_authorization_changer.bin index 36fa9d85..d7ebddfe 100644 Binary files a/artifacts/test_program_methods/malicious_authorization_changer.bin and b/artifacts/test_program_methods/malicious_authorization_changer.bin differ diff --git a/artifacts/test_program_methods/minter.bin b/artifacts/test_program_methods/minter.bin index 8b8386b0..0eadabbf 100644 Binary files a/artifacts/test_program_methods/minter.bin and b/artifacts/test_program_methods/minter.bin differ diff --git a/artifacts/test_program_methods/missing_output.bin b/artifacts/test_program_methods/missing_output.bin index c17d296e..eabf9c10 100644 Binary files a/artifacts/test_program_methods/missing_output.bin and b/artifacts/test_program_methods/missing_output.bin differ diff --git a/artifacts/test_program_methods/modified_transfer.bin b/artifacts/test_program_methods/modified_transfer.bin index e2784277..2b717936 100644 Binary files a/artifacts/test_program_methods/modified_transfer.bin and b/artifacts/test_program_methods/modified_transfer.bin differ diff --git a/artifacts/test_program_methods/nonce_changer.bin b/artifacts/test_program_methods/nonce_changer.bin index c444be55..534ca569 100644 Binary files a/artifacts/test_program_methods/nonce_changer.bin and b/artifacts/test_program_methods/nonce_changer.bin differ diff --git a/artifacts/test_program_methods/noop.bin b/artifacts/test_program_methods/noop.bin index 889624ee..9ed83fbd 100644 Binary files a/artifacts/test_program_methods/noop.bin and b/artifacts/test_program_methods/noop.bin differ diff --git a/artifacts/test_program_methods/program_owner_changer.bin b/artifacts/test_program_methods/program_owner_changer.bin index 51bd8794..b3afbcf7 100644 Binary files a/artifacts/test_program_methods/program_owner_changer.bin and b/artifacts/test_program_methods/program_owner_changer.bin differ diff --git a/artifacts/test_program_methods/simple_balance_transfer.bin b/artifacts/test_program_methods/simple_balance_transfer.bin index 7d902fe2..9ef21ed6 100644 Binary files a/artifacts/test_program_methods/simple_balance_transfer.bin and b/artifacts/test_program_methods/simple_balance_transfer.bin differ diff --git a/artifacts/test_program_methods/validity_window.bin b/artifacts/test_program_methods/validity_window.bin index 1b2ba8e6..6ce96300 100644 Binary files a/artifacts/test_program_methods/validity_window.bin and b/artifacts/test_program_methods/validity_window.bin differ diff --git a/artifacts/test_program_methods/validity_window_chain_caller.bin b/artifacts/test_program_methods/validity_window_chain_caller.bin new file mode 100644 index 00000000..e4e760ba Binary files /dev/null and b/artifacts/test_program_methods/validity_window_chain_caller.bin differ diff --git a/common/src/block.rs b/common/src/block.rs index 11446314..01ba586e 100644 --- a/common/src/block.rs +++ b/common/src/block.rs @@ -1,5 +1,4 @@ use borsh::{BorshDeserialize, BorshSerialize}; -use nssa::AccountId; use serde::{Deserialize, Serialize}; use sha2::{Digest as _, Sha256, digest::FixedOutput as _}; @@ -123,20 +122,6 @@ impl From for HashableBlockData { } } -/// Helper struct for account (de-)serialization. -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct AccountInitialData { - pub account_id: AccountId, - pub balance: u128, -} - -/// Helper struct to (de-)serialize initial commitments. -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct CommitmentsInitialData { - pub npk: nssa_core::NullifierPublicKey, - pub account: nssa_core::account::Account, -} - #[cfg(test)] mod tests { use crate::{HashType, block::HashableBlockData, test_utils}; diff --git a/common/src/sequencer_client.rs b/common/src/sequencer_client.rs deleted file mode 100644 index b75bbe04..00000000 --- a/common/src/sequencer_client.rs +++ /dev/null @@ -1,361 +0,0 @@ -use std::{collections::HashMap, ops::RangeInclusive}; - -use anyhow::Result; -use nssa::AccountId; -use nssa_core::program::ProgramId; -use reqwest::Client; -use serde::Deserialize; -use serde_json::Value; -use url::Url; - -use super::rpc_primitives::requests::{ - GetAccountBalanceRequest, GetAccountBalanceResponse, GetBlockDataRequest, GetBlockDataResponse, - GetGenesisIdRequest, GetGenesisIdResponse, GetInitialTestnetAccountsRequest, -}; -use crate::{ - HashType, - config::BasicAuth, - error::{SequencerClientError, SequencerRpcError}, - rpc_primitives::{ - self, - requests::{ - GetAccountRequest, GetAccountResponse, GetAccountsNoncesRequest, - GetAccountsNoncesResponse, GetBlockRangeDataRequest, GetBlockRangeDataResponse, - GetInitialTestnetAccountsResponse, GetLastBlockRequest, GetLastBlockResponse, - GetProgramIdsRequest, GetProgramIdsResponse, GetProofForCommitmentRequest, - GetProofForCommitmentResponse, GetTransactionByHashRequest, - GetTransactionByHashResponse, SendTxRequest, SendTxResponse, - }, - }, - transaction::NSSATransaction, -}; - -#[derive(Debug, Clone, Deserialize)] -struct SequencerRpcResponse { - #[serde(rename = "jsonrpc")] - _jsonrpc: String, - result: serde_json::Value, - #[serde(rename = "id")] - _id: u64, -} - -#[derive(Clone)] -pub struct SequencerClient { - pub client: reqwest::Client, - pub sequencer_addr: Url, - pub basic_auth: Option, -} - -impl SequencerClient { - pub fn new(sequencer_addr: Url) -> Result { - Self::new_with_auth(sequencer_addr, None) - } - - pub fn new_with_auth(sequencer_addr: Url, basic_auth: Option) -> Result { - Ok(Self { - client: Client::builder() - // Add more fields if needed - .timeout(std::time::Duration::from_mins(1)) - // Should be kept in sync with server keep-alive settings - .pool_idle_timeout(std::time::Duration::from_secs(5)) - .build()?, - sequencer_addr, - basic_auth, - }) - } - - pub async fn call_method_with_payload( - &self, - method: &str, - payload: Value, - ) -> Result { - let request = - rpc_primitives::message::Request::from_payload_version_2_0(method.to_owned(), payload); - - log::debug!( - "Calling method {method} with payload {request:?} to sequencer at {}", - self.sequencer_addr - ); - - let strategy = tokio_retry::strategy::FixedInterval::from_millis(10000).take(60); - - let response_vall = tokio_retry::Retry::spawn(strategy, || async { - let mut call_builder = self.client.post(self.sequencer_addr.clone()); - - if let Some(BasicAuth { username, password }) = &self.basic_auth { - call_builder = call_builder.basic_auth(username, password.as_deref()); - } - - let call_res_res = call_builder.json(&request).send().await; - - match call_res_res { - Err(err) => Err(err), - Ok(call_res) => call_res.json::().await, - } - }) - .await?; - - if let Ok(response) = serde_json::from_value::(response_vall.clone()) - { - Ok(response.result) - } else { - let err_resp = serde_json::from_value::(response_vall)?; - - Err(err_resp.into()) - } - } - - /// Get block data at `block_id` from sequencer. - pub async fn get_block( - &self, - block_id: u64, - ) -> Result { - let block_req = GetBlockDataRequest { block_id }; - - let req = serde_json::to_value(block_req)?; - - let resp = self.call_method_with_payload("get_block", req).await?; - - let resp_deser = serde_json::from_value(resp)?; - - Ok(resp_deser) - } - - pub async fn get_block_range( - &self, - range: RangeInclusive, - ) -> Result { - let block_req = GetBlockRangeDataRequest { - start_block_id: *range.start(), - end_block_id: *range.end(), - }; - - let req = serde_json::to_value(block_req)?; - - let resp = self - .call_method_with_payload("get_block_range", req) - .await?; - - let resp_deser = serde_json::from_value(resp)?; - - Ok(resp_deser) - } - - /// Get last known `blokc_id` from sequencer. - pub async fn get_last_block(&self) -> Result { - let block_req = GetLastBlockRequest {}; - - let req = serde_json::to_value(block_req)?; - - let resp = self.call_method_with_payload("get_last_block", req).await?; - - let resp_deser = serde_json::from_value(resp)?; - - Ok(resp_deser) - } - - /// Get account public balance for `account_id`. `account_id` must be a valid hex-string for 32 - /// bytes. - pub async fn get_account_balance( - &self, - account_id: AccountId, - ) -> Result { - let block_req = GetAccountBalanceRequest { account_id }; - - let req = serde_json::to_value(block_req)?; - - let resp = self - .call_method_with_payload("get_account_balance", req) - .await?; - - let resp_deser = serde_json::from_value(resp)?; - - Ok(resp_deser) - } - - /// Get accounts nonces for `account_ids`. `account_ids` must be a list of valid hex-strings for - /// 32 bytes. - pub async fn get_accounts_nonces( - &self, - account_ids: Vec, - ) -> Result { - let block_req = GetAccountsNoncesRequest { account_ids }; - - let req = serde_json::to_value(block_req)?; - - let resp = self - .call_method_with_payload("get_accounts_nonces", req) - .await?; - - let resp_deser = serde_json::from_value(resp)?; - - Ok(resp_deser) - } - - pub async fn get_account( - &self, - account_id: AccountId, - ) -> Result { - let block_req = GetAccountRequest { account_id }; - - let req = serde_json::to_value(block_req)?; - - let resp = self.call_method_with_payload("get_account", req).await?; - - let resp_deser = serde_json::from_value(resp)?; - - Ok(resp_deser) - } - - /// Get transaction details for `hash`. - pub async fn get_transaction_by_hash( - &self, - hash: HashType, - ) -> Result { - let block_req = GetTransactionByHashRequest { hash }; - - let req = serde_json::to_value(block_req)?; - - let resp = self - .call_method_with_payload("get_transaction_by_hash", req) - .await?; - - let resp_deser = serde_json::from_value(resp)?; - - Ok(resp_deser) - } - - /// Send transaction to sequencer. - pub async fn send_tx_public( - &self, - transaction: nssa::PublicTransaction, - ) -> Result { - let transaction = NSSATransaction::Public(transaction); - - let tx_req = SendTxRequest { - transaction: borsh::to_vec(&transaction).unwrap(), - }; - - let req = serde_json::to_value(tx_req)?; - - let resp = self.call_method_with_payload("send_tx", req).await?; - - let resp_deser = serde_json::from_value(resp)?; - - Ok(resp_deser) - } - - /// Send transaction to sequencer. - pub async fn send_tx_private( - &self, - transaction: nssa::PrivacyPreservingTransaction, - ) -> Result { - let transaction = NSSATransaction::PrivacyPreserving(transaction); - - let tx_req = SendTxRequest { - transaction: borsh::to_vec(&transaction).unwrap(), - }; - - let req = serde_json::to_value(tx_req)?; - - let resp = self.call_method_with_payload("send_tx", req).await?; - - let resp_deser = serde_json::from_value(resp)?; - - Ok(resp_deser) - } - - /// Get genesis id from sequencer. - pub async fn get_genesis_id(&self) -> Result { - let genesis_req = GetGenesisIdRequest {}; - - let req = serde_json::to_value(genesis_req).unwrap(); - - let resp = self - .call_method_with_payload("get_genesis", req) - .await - .unwrap(); - - let resp_deser = serde_json::from_value(resp).unwrap(); - - Ok(resp_deser) - } - - /// Get initial testnet accounts from sequencer. - pub async fn get_initial_testnet_accounts( - &self, - ) -> Result, SequencerClientError> { - let acc_req = GetInitialTestnetAccountsRequest {}; - - let req = serde_json::to_value(acc_req).unwrap(); - - let resp = self - .call_method_with_payload("get_initial_testnet_accounts", req) - .await - .unwrap(); - - let resp_deser = serde_json::from_value(resp).unwrap(); - - Ok(resp_deser) - } - - /// Get proof for commitment. - pub async fn get_proof_for_commitment( - &self, - commitment: nssa_core::Commitment, - ) -> Result, SequencerClientError> { - let acc_req = GetProofForCommitmentRequest { commitment }; - - let req = serde_json::to_value(acc_req).unwrap(); - - let resp = self - .call_method_with_payload("get_proof_for_commitment", req) - .await - .unwrap(); - - let resp_deser = serde_json::from_value::(resp) - .unwrap() - .membership_proof; - - Ok(resp_deser) - } - - pub async fn send_tx_program( - &self, - transaction: nssa::ProgramDeploymentTransaction, - ) -> Result { - let transaction = NSSATransaction::ProgramDeployment(transaction); - - let tx_req = SendTxRequest { - transaction: borsh::to_vec(&transaction).unwrap(), - }; - - let req = serde_json::to_value(tx_req)?; - - let resp = self.call_method_with_payload("send_tx", req).await?; - - let resp_deser = serde_json::from_value(resp)?; - - Ok(resp_deser) - } - - /// Get Ids of the programs used by the node. - pub async fn get_program_ids( - &self, - ) -> Result, SequencerClientError> { - let acc_req = GetProgramIdsRequest {}; - - let req = serde_json::to_value(acc_req).unwrap(); - - let resp = self - .call_method_with_payload("get_program_ids", req) - .await - .unwrap(); - - let resp_deser = serde_json::from_value::(resp) - .unwrap() - .program_ids; - - Ok(resp_deser) - } -} diff --git a/configs/docker-all-in-one/indexer/indexer_config.json b/configs/docker-all-in-one/indexer_config.json similarity index 100% rename from configs/docker-all-in-one/indexer/indexer_config.json rename to configs/docker-all-in-one/indexer_config.json diff --git a/configs/docker-all-in-one/sequencer/sequencer_config.json b/configs/docker-all-in-one/sequencer_config.json similarity index 100% rename from configs/docker-all-in-one/sequencer/sequencer_config.json rename to configs/docker-all-in-one/sequencer_config.json diff --git a/docker-compose.override.yml b/docker-compose.override.yml index af70ddd6..db955b23 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -11,17 +11,17 @@ services: depends_on: - logos-blockchain-node-0 - indexer_service - volumes: !override - - ./configs/docker-all-in-one/sequencer:/etc/sequencer_service + volumes: + - ./configs/docker-all-in-one/sequencer_config.json:/etc/sequencer_service/sequencer_config.json indexer_service: depends_on: - logos-blockchain-node-0 volumes: - - ./configs/docker-all-in-one/indexer/indexer_config.json:/etc/indexer_service/indexer_config.json + - ./configs/docker-all-in-one/indexer_config.json:/etc/indexer_service/indexer_config.json explorer_service: depends_on: - indexer_service environment: - - INDEXER_RPC_URL=http://indexer_service:8779 \ No newline at end of file + - INDEXER_RPC_URL=http://indexer_service:8779 diff --git a/docs/LEZ testnet v0.1 tutorials/associated-token-accounts.md b/docs/LEZ testnet v0.1 tutorials/associated-token-accounts.md new file mode 100644 index 00000000..330ae909 --- /dev/null +++ b/docs/LEZ testnet v0.1 tutorials/associated-token-accounts.md @@ -0,0 +1,369 @@ +# Associated Token Accounts (ATAs) + +This tutorial covers Associated Token Accounts (ATAs). An ATA lets you derive a unique token holding address from an owner account and a token definition — no need to create and track holding accounts manually. Given the same inputs, anyone can compute the same ATA address without a network call. By the end, you will have practiced: + +1. Deriving ATA addresses locally. +2. Creating an ATA. +3. Sending tokens via ATAs. +4. Burning tokens from an ATA. +5. Listing ATAs across multiple token definitions. +6. Creating an ATA with a private owner. +7. Sending tokens from a private owner's ATA. +8. Burning tokens from a private owner's ATA. + +> [!Important] +> This tutorial assumes you have completed the [wallet-setup](wallet-setup.md) and [custom-tokens](custom-tokens.md) tutorials. You need a running wallet with accounts and at least one token definition. + +## Prerequisites + +### Deploy the ATA program + +Unlike the Token program (which is built-in), the ATA program must be deployed before you can use it. The pre-built binary is included in the repository: + +```bash +wallet deploy-program artifacts/program_methods/associated_token_account.bin +``` + +> [!Note] +> Program deployment is idempotent — if the ATA program has already been deployed (e.g. by another user on the same network), the command is a no-op. + +You can verify the deployment succeeded by running any `wallet ata` command. If the program is not deployed, commands that submit transactions will fail. + +The CLI provides commands to work with the ATA program. Run `wallet ata` to see the options: + +```bash +Commands: + address Derive and print the Associated Token Account address (local only, no network) + create Create (or idempotently no-op) the Associated Token Account + send Send tokens from owner's ATA to a recipient + burn Burn tokens from holder's ATA + list List all ATAs for a given owner across multiple token definitions + help Print this message or the help of the given subcommand(s) +``` + +## 1. How ATA addresses work + +An ATA address is deterministically derived from two inputs: + +1. The **owner** account ID. +2. The **token definition** account ID. + +The derivation works as follows: + +``` +seed = SHA256(owner_id || definition_id) +ata_address = AccountId::from((ata_program_id, seed)) +``` + +Because the computation is pure, anyone who knows the owner and definition can reproduce the exact same ATA address — no network call required. + +> [!Note] +> All ATA commands that submit transactions accept a privacy prefix on the owner/holder argument — `Public/` for public accounts and `Private/` for private accounts. Using `Private/` generates a zero-knowledge proof locally and submits only the proof to the sequencer, keeping the owner's identity off-chain. + +## 2. Deriving an ATA address (`wallet ata address`) + +The `address` subcommand computes the ATA address locally without submitting a transaction. + +### a. Set up an owner and token definition + +If you already have a public account and a token definition from the custom-tokens tutorial, you can reuse them. Otherwise, create them now: + +```bash +wallet account new public + +# Output: +Generated new account with account_id Public/5FkBei8HYoSUNqh9rWCrJDnSZE5FJfGiWmTvhgBx3qTB +``` + +```bash +wallet account new public + +# Output: +Generated new account with account_id Public/3YpK8RvVzWm6Q4h2nDAbxJfLmuRqkEkFP9C7UwTdGvE4 +``` + +```bash +wallet token new \ + --name MYTOKEN \ + --total-supply 10000 \ + --definition-account-id Public/3YpK8RvVzWm6Q4h2nDAbxJfLmuRqkEkFP9C7UwTdGvE4 \ + --supply-account-id Public/5FkBei8HYoSUNqh9rWCrJDnSZE5FJfGiWmTvhgBx3qTB +``` + +### b. Derive the ATA address + +```bash +wallet ata address \ + --owner 5FkBei8HYoSUNqh9rWCrJDnSZE5FJfGiWmTvhgBx3qTB \ + --token-definition 3YpK8RvVzWm6Q4h2nDAbxJfLmuRqkEkFP9C7UwTdGvE4 + +# Output: +7a2Bf9cKLm3XpRtH1wDqZs8vYjN4eU6gAoFxW5kMnE2R +``` + +> [!Note] +> This is a pure computation — no transaction is submitted and no network connection is needed. The same inputs will always produce the same output. + +## 3. Creating an ATA (`wallet ata create`) + +Before an ATA can hold tokens it must be created on-chain. The `create` subcommand submits a transaction that initializes the ATA. If it already exists, the operation is a no-op. + +### a. Create the ATA + +```bash +wallet ata create \ + --owner Public/5FkBei8HYoSUNqh9rWCrJDnSZE5FJfGiWmTvhgBx3qTB \ + --token-definition 3YpK8RvVzWm6Q4h2nDAbxJfLmuRqkEkFP9C7UwTdGvE4 +``` + +### b. Inspect the ATA + +Use the ATA address derived in the previous section: + +```bash +wallet account get --account-id Public/7a2Bf9cKLm3XpRtH1wDqZs8vYjN4eU6gAoFxW5kMnE2R + +# Output: +Holding account owned by ata program +{"account_type":"Token holding","definition_id":"3YpK8RvVzWm6Q4h2nDAbxJfLmuRqkEkFP9C7UwTdGvE4","balance":0} +``` + +> [!Tip] +> Creation is idempotent — running the same command again is a no-op. + +## 4. Sending tokens via ATA (`wallet ata send`) + +The `send` subcommand transfers tokens from the owner's ATA to a recipient account. + +### a. Fund the ATA + +First, move tokens into the ATA from the supply account created earlier: + +```bash +wallet token send \ + --from Public/5FkBei8HYoSUNqh9rWCrJDnSZE5FJfGiWmTvhgBx3qTB \ + --to Public/7a2Bf9cKLm3XpRtH1wDqZs8vYjN4eU6gAoFxW5kMnE2R \ + --amount 5000 +``` + +### b. Create a recipient account + +```bash +wallet account new public + +# Output: +Generated new account with account_id Public/9Ht4Kv8pYmW2rXjN6dFcQsA7bEoLf3gUZx1wDnR5eTi +``` + +### c. Send tokens from the ATA to the recipient + +```bash +wallet ata send \ + --from Public/5FkBei8HYoSUNqh9rWCrJDnSZE5FJfGiWmTvhgBx3qTB \ + --token-definition 3YpK8RvVzWm6Q4h2nDAbxJfLmuRqkEkFP9C7UwTdGvE4 \ + --to 9Ht4Kv8pYmW2rXjN6dFcQsA7bEoLf3gUZx1wDnR5eTi \ + --amount 2000 +``` + +### d. Verify balances + +```bash +wallet account get --account-id Public/7a2Bf9cKLm3XpRtH1wDqZs8vYjN4eU6gAoFxW5kMnE2R + +# Output: +Holding account owned by ata program +{"account_type":"Token holding","definition_id":"3YpK8RvVzWm6Q4h2nDAbxJfLmuRqkEkFP9C7UwTdGvE4","balance":3000} +``` + +```bash +wallet account get --account-id Public/9Ht4Kv8pYmW2rXjN6dFcQsA7bEoLf3gUZx1wDnR5eTi + +# Output: +Holding account owned by token program +{"account_type":"Token holding","definition_id":"3YpK8RvVzWm6Q4h2nDAbxJfLmuRqkEkFP9C7UwTdGvE4","balance":2000} +``` + +## 5. Burning tokens from an ATA (`wallet ata burn`) + +The `burn` subcommand destroys tokens held in the owner's ATA, reducing the token's total supply. + +### a. Burn tokens + +```bash +wallet ata burn \ + --holder Public/5FkBei8HYoSUNqh9rWCrJDnSZE5FJfGiWmTvhgBx3qTB \ + --token-definition 3YpK8RvVzWm6Q4h2nDAbxJfLmuRqkEkFP9C7UwTdGvE4 \ + --amount 500 +``` + +### b. Verify the reduced balance + +```bash +wallet account get --account-id Public/7a2Bf9cKLm3XpRtH1wDqZs8vYjN4eU6gAoFxW5kMnE2R + +# Output: +Holding account owned by ata program +{"account_type":"Token holding","definition_id":"3YpK8RvVzWm6Q4h2nDAbxJfLmuRqkEkFP9C7UwTdGvE4","balance":2500} +``` + +## 6. Listing ATAs (`wallet ata list`) + +The `list` subcommand queries ATAs for a given owner across one or more token definitions. + +### a. Create a second token and ATA + +Create a second token definition so there are multiple ATAs to list: + +```bash +wallet account new public + +# Output: +Generated new account with account_id Public/BxR3Lm7YkWp9vNs2hD4qJcTfA8eUoZ6gKn1wXjM5rFi +``` + +```bash +wallet account new public + +# Output: +Generated new account with account_id Public/Ck8mVp4YhWn2rXjD6dFsQtA7bEoLf3gUZx1wDnR9eTs +``` + +```bash +wallet token new \ + --name OTHERTOKEN \ + --total-supply 5000 \ + --definition-account-id Public/BxR3Lm7YkWp9vNs2hD4qJcTfA8eUoZ6gKn1wXjM5rFi \ + --supply-account-id Public/Ck8mVp4YhWn2rXjD6dFsQtA7bEoLf3gUZx1wDnR9eTs +``` + +Create an ATA for the second token: + +```bash +wallet ata create \ + --owner Public/5FkBei8HYoSUNqh9rWCrJDnSZE5FJfGiWmTvhgBx3qTB \ + --token-definition BxR3Lm7YkWp9vNs2hD4qJcTfA8eUoZ6gKn1wXjM5rFi +``` + +### b. List ATAs for both token definitions + +```bash +wallet ata list \ + --owner 5FkBei8HYoSUNqh9rWCrJDnSZE5FJfGiWmTvhgBx3qTB \ + --token-definition \ + 3YpK8RvVzWm6Q4h2nDAbxJfLmuRqkEkFP9C7UwTdGvE4 \ + BxR3Lm7YkWp9vNs2hD4qJcTfA8eUoZ6gKn1wXjM5rFi + +# Output: +ATA 7a2Bf9cKLm3XpRtH1wDqZs8vYjN4eU6gAoFxW5kMnE2R (definition 3YpK8RvVzWm6Q4h2nDAbxJfLmuRqkEkFP9C7UwTdGvE4): balance 2500 +ATA 4nPxKd8YmW7rVsH2jDfQcA9bEoLf6gUZx3wTnR1eMs5 (definition BxR3Lm7YkWp9vNs2hD4qJcTfA8eUoZ6gKn1wXjM5rFi): balance 0 +``` + +> [!Note] +> The `list` command derives each ATA address locally and fetches its on-chain state. If an ATA has not been created for a given definition, it prints "No ATA for definition ..." instead. + +## 7. Private owner operations + +All three ATA operations — `create`, `send`, and `burn` — support private owner accounts. Passing a `Private/` prefix on the owner argument switches the wallet into privacy-preserving mode: + +1. The wallet builds the transaction locally. +2. The ATA program is executed inside the RISC0 ZK VM to generate a proof. +3. The proof, the updated ATA state (in plaintext), and an encrypted update for the owner's private account are submitted to the sequencer. +4. The sequencer verifies the proof, writes the ATA state change to the public chain, and records the owner's new commitment in the nullifier set. + +The result is that the ATA account and its token balance are **fully public** — anyone can see them. What stays private is the link between the ATA and its owner: the proof demonstrates that someone with the correct private key authorized the operation, but reveals nothing about which account that was. + +> [!Note] +> The ATA address is derived from `SHA256(owner_id || definition_id)`. Because SHA256 is one-way, the ATA address does not reveal the owner's identity. However, if the owner's account ID becomes known for any other reason, all of their ATAs across every token definition can be enumerated by anyone. + +### a. Create a private account + +```bash +wallet account new private + +# Output: +Generated new account with account_id Private/HkR7Lm2YnWp4vNs8hD3qJcTfA6eUoZ9gKn5wXjM1rFi +``` + +### b. Create the ATA for the private owner + +Pass `Private/` on `--owner`. The token definition account has no privacy prefix — it is always a public account. + +```bash +wallet ata create \ + --owner Private/HkR7Lm2YnWp4vNs8hD3qJcTfA6eUoZ9gKn5wXjM1rFi \ + --token-definition 3YpK8RvVzWm6Q4h2nDAbxJfLmuRqkEkFP9C7UwTdGvE4 +``` + +> [!Note] +> Proof generation runs locally in the RISC0 ZK VM and can take up to a minute on first run. + +### c. Verify the ATA was created + +Derive the ATA address using the raw account ID (no privacy prefix): + +```bash +wallet ata address \ + --owner HkR7Lm2YnWp4vNs8hD3qJcTfA6eUoZ9gKn5wXjM1rFi \ + --token-definition 3YpK8RvVzWm6Q4h2nDAbxJfLmuRqkEkFP9C7UwTdGvE4 + +# Output: +2pQxNf7YkWm3rVsH8jDcQaA4bEoLf9gUZx6wTnR2eMs1 +``` + +```bash +wallet account get --account-id Public/2pQxNf7YkWm3rVsH8jDcQaA4bEoLf9gUZx6wTnR2eMs1 + +# Output: +Holding account owned by ata program +{"account_type":"Token holding","definition_id":"3YpK8RvVzWm6Q4h2nDAbxJfLmuRqkEkFP9C7UwTdGvE4","balance":0} +``` + +### d. Fund the ATA + +The ATA is a public account. Fund it with a direct token transfer from any public holding account: + +```bash +wallet token send \ + --from Public/5FkBei8HYoSUNqh9rWCrJDnSZE5FJfGiWmTvhgBx3qTB \ + --to Public/2pQxNf7YkWm3rVsH8jDcQaA4bEoLf9gUZx6wTnR2eMs1 \ + --amount 500 +``` + +### e. Send tokens from the private owner's ATA + +```bash +wallet ata send \ + --from Private/HkR7Lm2YnWp4vNs8hD3qJcTfA6eUoZ9gKn5wXjM1rFi \ + --token-definition 3YpK8RvVzWm6Q4h2nDAbxJfLmuRqkEkFP9C7UwTdGvE4 \ + --to 9Ht4Kv8pYmW2rXjN6dFcQsA7bEoLf3gUZx1wDnR5eTi \ + --amount 200 +``` + +Verify the ATA balance decreased: + +```bash +wallet account get --account-id Public/2pQxNf7YkWm3rVsH8jDcQaA4bEoLf9gUZx6wTnR2eMs1 + +# Output: +Holding account owned by ata program +{"account_type":"Token holding","definition_id":"3YpK8RvVzWm6Q4h2nDAbxJfLmuRqkEkFP9C7UwTdGvE4","balance":300} +``` + +### f. Burn tokens from the private owner's ATA + +```bash +wallet ata burn \ + --holder Private/HkR7Lm2YnWp4vNs8hD3qJcTfA6eUoZ9gKn5wXjM1rFi \ + --token-definition 3YpK8RvVzWm6Q4h2nDAbxJfLmuRqkEkFP9C7UwTdGvE4 \ + --amount 100 +``` + +Verify the balance and token supply: + +```bash +wallet account get --account-id Public/2pQxNf7YkWm3rVsH8jDcQaA4bEoLf9gUZx6wTnR2eMs1 + +# Output: +Holding account owned by ata program +{"account_type":"Token holding","definition_id":"3YpK8RvVzWm6Q4h2nDAbxJfLmuRqkEkFP9C7UwTdGvE4","balance":200} +``` diff --git a/examples/program_deployment/methods/guest/src/bin/hello_world.rs b/examples/program_deployment/methods/guest/src/bin/hello_world.rs index 3391eb5d..381b71c3 100644 --- a/examples/program_deployment/methods/guest/src/bin/hello_world.rs +++ b/examples/program_deployment/methods/guest/src/bin/hello_world.rs @@ -1,5 +1,5 @@ use nssa_core::program::{ - AccountPostState, DEFAULT_PROGRAM_ID, ProgramInput, read_nssa_inputs, write_nssa_outputs, + AccountPostState, DEFAULT_PROGRAM_ID, ProgramInput, ProgramOutput, read_nssa_inputs, }; // Hello-world example program. @@ -56,5 +56,7 @@ fn main() { // The output is a proposed state difference. It will only succeed if the pre states coincide // with the previous values of the accounts, and the transition to the post states conforms // with the NSSA program rules. - write_nssa_outputs(instruction_data, vec![pre_state], vec![post_state]); + // WARNING: constructing a `ProgramOutput` has no effect on its own. `.write()` must be + // called to commit the output. + ProgramOutput::new(instruction_data, vec![pre_state], vec![post_state]).write(); } diff --git a/examples/program_deployment/methods/guest/src/bin/hello_world_with_authorization.rs b/examples/program_deployment/methods/guest/src/bin/hello_world_with_authorization.rs index e327ca47..d90c072b 100644 --- a/examples/program_deployment/methods/guest/src/bin/hello_world_with_authorization.rs +++ b/examples/program_deployment/methods/guest/src/bin/hello_world_with_authorization.rs @@ -1,5 +1,5 @@ use nssa_core::program::{ - AccountPostState, DEFAULT_PROGRAM_ID, ProgramInput, read_nssa_inputs, write_nssa_outputs, + AccountPostState, DEFAULT_PROGRAM_ID, ProgramInput, ProgramOutput, read_nssa_inputs, }; // Hello-world with authorization example program. @@ -63,5 +63,7 @@ fn main() { // The output is a proposed state difference. It will only succeed if the pre states coincide // with the previous values of the accounts, and the transition to the post states conforms // with the NSSA program rules. - write_nssa_outputs(instruction_data, vec![pre_state], vec![post_state]); + // WARNING: constructing a `ProgramOutput` has no effect on its own. `.write()` must be + // called to commit the output. + ProgramOutput::new(instruction_data, vec![pre_state], vec![post_state]).write(); } diff --git a/examples/program_deployment/methods/guest/src/bin/hello_world_with_move_function.rs b/examples/program_deployment/methods/guest/src/bin/hello_world_with_move_function.rs index 65f0f9cd..0b2885a8 100644 --- a/examples/program_deployment/methods/guest/src/bin/hello_world_with_move_function.rs +++ b/examples/program_deployment/methods/guest/src/bin/hello_world_with_move_function.rs @@ -1,7 +1,7 @@ use nssa_core::{ account::{Account, AccountWithMetadata, Data}, program::{ - AccountPostState, DEFAULT_PROGRAM_ID, ProgramInput, read_nssa_inputs, write_nssa_outputs, + AccountPostState, DEFAULT_PROGRAM_ID, ProgramInput, ProgramOutput, read_nssa_inputs, }, }; @@ -95,5 +95,7 @@ fn main() { _ => panic!("invalid params"), }; - write_nssa_outputs(instruction_words, pre_states, post_states); + // WARNING: constructing a `ProgramOutput` has no effect on its own. `.write()` must be + // called to commit the output. + ProgramOutput::new(instruction_words, pre_states, post_states).write(); } diff --git a/examples/program_deployment/methods/guest/src/bin/simple_tail_call.rs b/examples/program_deployment/methods/guest/src/bin/simple_tail_call.rs index 01389085..d2c04083 100644 --- a/examples/program_deployment/methods/guest/src/bin/simple_tail_call.rs +++ b/examples/program_deployment/methods/guest/src/bin/simple_tail_call.rs @@ -1,6 +1,5 @@ use nssa_core::program::{ - AccountPostState, ChainedCall, ProgramId, ProgramInput, read_nssa_inputs, - write_nssa_outputs_with_chained_call, + AccountPostState, ChainedCall, ProgramId, ProgramInput, ProgramOutput, read_nssa_inputs, }; // Tail Call example program. @@ -53,11 +52,10 @@ fn main() { pda_seeds: vec![], }; - // Write the outputs - write_nssa_outputs_with_chained_call( - instruction_data, - vec![pre_state], - vec![post_state], - vec![chained_call], - ); + // Write the outputs. + // WARNING: constructing a `ProgramOutput` has no effect on its own. `.write()` must be + // called to commit the output. + ProgramOutput::new(instruction_data, vec![pre_state], vec![post_state]) + .with_chained_calls(vec![chained_call]) + .write(); } diff --git a/examples/program_deployment/methods/guest/src/bin/tail_call_with_pda.rs b/examples/program_deployment/methods/guest/src/bin/tail_call_with_pda.rs index 3ebcabd2..564efc2b 100644 --- a/examples/program_deployment/methods/guest/src/bin/tail_call_with_pda.rs +++ b/examples/program_deployment/methods/guest/src/bin/tail_call_with_pda.rs @@ -1,6 +1,6 @@ use nssa_core::program::{ - AccountPostState, ChainedCall, PdaSeed, ProgramId, ProgramInput, read_nssa_inputs, - write_nssa_outputs_with_chained_call, + AccountPostState, ChainedCall, PdaSeed, ProgramId, ProgramInput, ProgramOutput, + read_nssa_inputs, }; // Tail Call with PDA example program. @@ -65,11 +65,10 @@ fn main() { pda_seeds: vec![PDA_SEED], }; - // Write the outputs - write_nssa_outputs_with_chained_call( - instruction_data, - vec![pre_state], - vec![post_state], - vec![chained_call], - ); + // Write the outputs. + // WARNING: constructing a `ProgramOutput` has no effect on its own. `.write()` must be + // called to commit the output. + ProgramOutput::new(instruction_data, vec![pre_state], vec![post_state]) + .with_chained_calls(vec![chained_call]) + .write(); } diff --git a/explorer_service/Dockerfile b/explorer_service/Dockerfile index 238e77e6..6484619f 100644 --- a/explorer_service/Dockerfile +++ b/explorer_service/Dockerfile @@ -22,7 +22,13 @@ WORKDIR /explorer_service COPY . . # Build the app -RUN cargo leptos build --release -vv +RUN --mount=type=cache,target=/usr/local/cargo/registry/index \ + --mount=type=cache,target=/usr/local/cargo/registry/cache \ + --mount=type=cache,target=/usr/local/cargo/git \ + --mount=type=cache,target=/explorer_service/target \ + cargo leptos build --release -vv \ + && cp /explorer_service/target/release/explorer_service /usr/local/bin/explorer_service \ + && cp -r /explorer_service/target/site /explorer_service/site_output FROM debian:trixie-slim AS runtime WORKDIR /explorer_service @@ -33,10 +39,10 @@ RUN apt-get update -y \ && rm -rf /var/lib/apt/lists/* # Copy the server binary to the /explorer_service directory -COPY --from=builder /explorer_service/target/release/explorer_service /explorer_service/ +COPY --from=builder /usr/local/bin/explorer_service /explorer_service/ # /target/site contains our JS/WASM/CSS, etc. -COPY --from=builder /explorer_service/target/site /explorer_service/site +COPY --from=builder /explorer_service/site_output /explorer_service/site # Copy Cargo.toml as it’s needed at runtime COPY --from=builder /explorer_service/Cargo.toml /explorer_service/ diff --git a/explorer_service/src/pages/transaction_page.rs b/explorer_service/src/pages/transaction_page.rs index a293b840..ed3d8aac 100644 --- a/explorer_service/src/pages/transaction_page.rs +++ b/explorer_service/src/pages/transaction_page.rs @@ -183,25 +183,6 @@ pub fn TransactionPage() -> impl IntoView { signatures_and_public_keys: _, proof, } = witness_set; - let (block_from, block_to, ts_from, ts_to) = validity_window.0; - let block_part = match (block_from, block_to) { - (Some(start), Some(end)) => format!("block {start}..{end}"), - (Some(start), None) => format!("block {start}.."), - (None, Some(end)) => format!("block ..{end}"), - (None, None) => String::new(), - }; - let ts_part = match (ts_from, ts_to) { - (Some(start), Some(end)) => format!("ts {start}..{end}"), - (Some(start), None) => format!("ts {start}.."), - (None, Some(end)) => format!("ts ..{end}"), - (None, None) => String::new(), - }; - let validity_window_formatted = match (block_part.is_empty(), ts_part.is_empty()) { - (true, true) => "unbounded".to_owned(), - (false, true) => block_part, - (true, false) => ts_part, - (false, false) => format!("{block_part}, {ts_part}"), - }; let proof_len = proof.map_or(0, |p| p.0.len()); view! { @@ -234,7 +215,7 @@ pub fn TransactionPage() -> impl IntoView {
"Validity Window:" - {validity_window_formatted} + {validity_window.to_string()}
diff --git a/indexer/core/Cargo.toml b/indexer/core/Cargo.toml index 13e81088..33fe2d9d 100644 --- a/indexer/core/Cargo.toml +++ b/indexer/core/Cargo.toml @@ -13,6 +13,7 @@ bedrock_client.workspace = true nssa.workspace = true nssa_core.workspace = true storage.workspace = true +testnet_initial_state.workspace = true anyhow.workspace = true log.workspace = true diff --git a/indexer/core/src/config.rs b/indexer/core/src/config.rs index a85284cc..291e54f5 100644 --- a/indexer/core/src/config.rs +++ b/indexer/core/src/config.rs @@ -7,13 +7,11 @@ use std::{ use anyhow::{Context as _, Result}; pub use bedrock_client::BackoffConfig; -use common::{ - block::{AccountInitialData, CommitmentsInitialData}, - config::BasicAuth, -}; +use common::config::BasicAuth; use humantime_serde; pub use logos_blockchain_core::mantle::ops::channel::ChannelId; use serde::{Deserialize, Serialize}; +use testnet_initial_state::{PrivateAccountPublicInitialData, PublicAccountPublicInitialData}; use url::Url; #[derive(Debug, Clone, Serialize, Deserialize)] @@ -29,16 +27,16 @@ pub struct ClientConfig { pub struct IndexerConfig { /// Home dir of sequencer storage. pub home: PathBuf, - /// List of initial accounts data. - pub initial_accounts: Vec, - /// List of initial commitments. - pub initial_commitments: Vec, /// Sequencers signing key. pub signing_key: [u8; 32], #[serde(with = "humantime_serde")] pub consensus_info_polling_interval: Duration, pub bedrock_client_config: ClientConfig, pub channel_id: ChannelId, + #[serde(skip_serializing_if = "Option::is_none")] + pub initial_public_accounts: Option>, + #[serde(skip_serializing_if = "Option::is_none")] + pub initial_private_accounts: Option>, } impl IndexerConfig { diff --git a/indexer/core/src/lib.rs b/indexer/core/src/lib.rs index 16cd7ac9..bcd99ad7 100644 --- a/indexer/core/src/lib.rs +++ b/indexer/core/src/lib.rs @@ -2,14 +2,17 @@ use std::collections::VecDeque; use anyhow::Result; use bedrock_client::{BedrockClient, HeaderId}; -use common::block::{Block, HashableBlockData}; -// ToDo: Remove after testnet -use common::{HashType, PINATA_BASE58}; +use common::{ + HashType, PINATA_BASE58, + block::{Block, HashableBlockData}, +}; use log::{debug, error, info}; use logos_blockchain_core::mantle::{ Op, SignedMantleTx, ops::channel::{ChannelId, inscribe::InscriptionOp}, }; +use nssa::V03State; +use testnet_initial_state::initial_state_testnet; use crate::{block_store::IndexerStore, config::IndexerConfig}; @@ -54,36 +57,50 @@ impl IndexerCore { let channel_genesis_msg_id = [0; 32]; let genesis_block = hashable_data.into_pending_block(&signing_key, channel_genesis_msg_id); - // This is a troubling moment, because changes in key protocol can - // affect this. And indexer can not reliably ask this data from sequencer - // because indexer must be independent from it. - // ToDo: move initial state generation into common and use the same method - // for indexer and sequencer. This way both services buit at same version - // could be in sync. - let initial_commitments: Vec = config - .initial_commitments - .iter() - .map(|init_comm_data| { - let npk = &init_comm_data.npk; + let initial_commitments: Option> = config + .initial_private_accounts + .as_ref() + .map(|initial_commitments| { + initial_commitments + .iter() + .map(|init_comm_data| { + let npk = &init_comm_data.npk; - let mut acc = init_comm_data.account.clone(); + let mut acc = init_comm_data.account.clone(); - acc.program_owner = nssa::program::Program::authenticated_transfer_program().id(); + acc.program_owner = + nssa::program::Program::authenticated_transfer_program().id(); - nssa_core::Commitment::new(npk, &acc) - }) - .collect(); + nssa_core::Commitment::new(npk, &acc) + }) + .collect() + }); - let init_accs: Vec<(nssa::AccountId, u128)> = config - .initial_accounts - .iter() - .map(|acc_data| (acc_data.account_id, acc_data.balance)) - .collect(); + let init_accs: Option> = config + .initial_public_accounts + .as_ref() + .map(|initial_accounts| { + initial_accounts + .iter() + .map(|acc_data| (acc_data.account_id, acc_data.balance)) + .collect() + }); - let mut state = nssa::V03State::new_with_genesis_accounts(&init_accs, &initial_commitments); + // If initial commitments or accounts are present in config, need to construct state from + // them + let state = if initial_commitments.is_some() || init_accs.is_some() { + let mut state = V03State::new_with_genesis_accounts( + &init_accs.unwrap_or_default(), + &initial_commitments.unwrap_or_default(), + ); - // ToDo: Remove after testnet - state.add_pinata_program(PINATA_BASE58.parse().unwrap()); + // ToDo: Remove after testnet + state.add_pinata_program(PINATA_BASE58.parse().unwrap()); + + state + } else { + initial_state_testnet() + }; let home = config.home.join("rocksdb"); diff --git a/indexer/service/Dockerfile b/indexer/service/Dockerfile index bb93c2f2..cc7087bb 100644 --- a/indexer/service/Dockerfile +++ b/indexer/service/Dockerfile @@ -51,32 +51,34 @@ RUN cargo chef prepare --bin indexer_service --recipe-path recipe.json FROM chef AS builder COPY --from=planner /indexer_service/recipe.json recipe.json # Build dependencies only (this layer will be cached) -RUN cargo chef cook --bin indexer_service --release --recipe-path recipe.json +RUN --mount=type=cache,target=/usr/local/cargo/registry/index \ + --mount=type=cache,target=/usr/local/cargo/registry/cache \ + --mount=type=cache,target=/usr/local/cargo/git \ + --mount=type=cache,target=/indexer_service/target \ + cargo chef cook --bin indexer_service --release --recipe-path recipe.json # Copy source code COPY . . -# Build the actual application -RUN cargo build --release --bin indexer_service - -# Strip debug symbols to reduce binary size -RUN strip /indexer_service/target/release/indexer_service +# Build the actual application and copy the binary out of the cache mount +RUN --mount=type=cache,target=/usr/local/cargo/registry/index \ + --mount=type=cache,target=/usr/local/cargo/registry/cache \ + --mount=type=cache,target=/usr/local/cargo/git \ + --mount=type=cache,target=/indexer_service/target \ + cargo build --release --bin indexer_service \ + && strip /indexer_service/target/release/indexer_service \ + && cp /indexer_service/target/release/indexer_service /usr/local/bin/indexer_service # Runtime stage - minimal image FROM debian:trixie-slim -# Install runtime dependencies -RUN apt-get update \ - && apt-get install -y gosu jq \ - && rm -rf /var/lib/apt/lists/* - # Create non-root user for security RUN useradd -m -u 1000 -s /bin/bash indexer_service_user && \ - mkdir -p /indexer_service /etc/indexer_service && \ - chown -R indexer_service_user:indexer_service_user /indexer_service /etc/indexer_service + mkdir -p /indexer_service /etc/indexer_service /var/lib/indexer_service && \ + chown -R indexer_service_user:indexer_service_user /indexer_service /etc/indexer_service /var/lib/indexer_service # Copy binary from builder -COPY --from=builder --chown=indexer_service_user:indexer_service_user /indexer_service/target/release/indexer_service /usr/local/bin/indexer_service +COPY --from=builder --chown=indexer_service_user:indexer_service_user /usr/local/bin/indexer_service /usr/local/bin/indexer_service # Copy r0vm binary from builder COPY --from=builder --chown=indexer_service_user:indexer_service_user /usr/local/bin/r0vm /usr/local/bin/r0vm @@ -84,9 +86,7 @@ COPY --from=builder --chown=indexer_service_user:indexer_service_user /usr/local # Copy logos blockchain circuits from builder COPY --from=builder --chown=indexer_service_user:indexer_service_user /root/.logos-blockchain-circuits /home/indexer_service_user/.logos-blockchain-circuits -# Copy entrypoint script -COPY indexer/service/docker-entrypoint.sh /docker-entrypoint.sh -RUN chmod +x /docker-entrypoint.sh +VOLUME /var/lib/indexer_service # Expose default port EXPOSE 8779 @@ -105,9 +105,7 @@ HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ # Run the application ENV RUST_LOG=info -USER root - -ENTRYPOINT ["/docker-entrypoint.sh"] +USER indexer_service_user WORKDIR /indexer_service CMD ["indexer_service", "/etc/indexer_service/indexer_config.json"] diff --git a/indexer/service/docker-compose.yml b/indexer/service/docker-compose.yml index 73ac90ae..b690a180 100644 --- a/indexer/service/docker-compose.yml +++ b/indexer/service/docker-compose.yml @@ -10,5 +10,8 @@ services: volumes: # Mount configuration - ./configs/indexer_config.json:/etc/indexer_service/indexer_config.json - # Mount data folder - - ./data:/var/lib/indexer_service + # Mount data volume + - indexer_data:/var/lib/indexer_service + +volumes: + indexer_data: diff --git a/indexer/service/docker-entrypoint.sh b/indexer/service/docker-entrypoint.sh deleted file mode 100644 index 49a5f891..00000000 --- a/indexer/service/docker-entrypoint.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/sh - -# This is an entrypoint script for the indexer_service Docker container, -# it's not meant to be executed outside of the container. - -set -e - -CONFIG="/etc/indexer_service/indexer_config.json" - -# Check config file exists -if [ ! -f "$CONFIG" ]; then - echo "Config file not found: $CONFIG" >&2 - exit 1 -fi - -# Parse home dir -HOME_DIR=$(jq -r '.home' "$CONFIG") - -if [ -z "$HOME_DIR" ] || [ "$HOME_DIR" = "null" ]; then - echo "'home' key missing in config" >&2 - exit 1 -fi - -# Give permissions to the data directory and switch to non-root user -if [ "$(id -u)" = "0" ]; then - mkdir -p "$HOME_DIR" - chown -R indexer_service_user:indexer_service_user "$HOME_DIR" - exec gosu indexer_service_user "$@" -fi diff --git a/indexer/service/protocol/src/convert.rs b/indexer/service/protocol/src/convert.rs index 7fcd32e2..14117a48 100644 --- a/indexer/service/protocol/src/convert.rs +++ b/indexer/service/protocol/src/convert.rs @@ -302,12 +302,7 @@ impl From for PrivacyPre .into_iter() .map(|(n, d)| (n.into(), d.into())) .collect(), - validity_window: ValidityWindow(( - validity_window.from(), - validity_window.to(), - validity_window.from_timestamp(), - validity_window.to_timestamp(), - )), + validity_window: validity_window.into(), } } } @@ -346,7 +341,6 @@ impl TryFrom for nssa::privacy_preserving_transaction: .map(|(n, d)| (n.into(), d.into())) .collect(), validity_window: validity_window - .0 .try_into() .map_err(|e| nssa::error::NssaError::InvalidInput(format!("{e}")))?, }) @@ -693,3 +687,26 @@ impl From for common::HashType { Self(value.0) } } + +// ============================================================================ +// ValidityWindow conversions +// ============================================================================ + +impl From for ValidityWindow { + fn from(value: nssa_core::program::ValidityWindow) -> Self { + Self(( + value.start(), + value.end(), + value.from_timestamp(), + value.to_timestamp(), + )) + } +} + +impl TryFrom for nssa_core::program::ValidityWindow { + type Error = nssa_core::program::InvalidWindow; + + fn try_from(value: ValidityWindow) -> Result { + value.0.try_into() + } +} diff --git a/indexer/service/protocol/src/lib.rs b/indexer/service/protocol/src/lib.rs index 6186d187..51119260 100644 --- a/indexer/service/protocol/src/lib.rs +++ b/indexer/service/protocol/src/lib.rs @@ -311,6 +311,17 @@ pub struct ValidityWindow( ), ); +impl Display for ValidityWindow { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self.0 { + (Some(start), Some(end), ..) => write!(f, "[{start}, {end})"), + (Some(start), None, ..) => write!(f, "[{start}, \u{221e})"), + (None, Some(end), ..) => write!(f, "(-\u{221e}, {end})"), + (None, None, ..) => write!(f, "(-\u{221e}, \u{221e})"), + } + } +} + #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)] pub struct CommitmentSetDigest( #[serde(with = "base64::arr")] diff --git a/integration_tests/Cargo.toml b/integration_tests/Cargo.toml index c88ec1fd..cb5277d2 100644 --- a/integration_tests/Cargo.toml +++ b/integration_tests/Cargo.toml @@ -18,9 +18,11 @@ key_protocol.workspace = true indexer_service.workspace = true serde_json.workspace = true token_core.workspace = true +ata_core.workspace = true indexer_service_rpc.workspace = true sequencer_service_rpc = { workspace = true, features = ["client"] } wallet-ffi.workspace = true +testnet_initial_state.workspace = true url.workspace = true diff --git a/integration_tests/src/config.rs b/integration_tests/src/config.rs index e7f12fc1..1dd726eb 100644 --- a/integration_tests/src/config.rs +++ b/integration_tests/src/config.rs @@ -2,16 +2,17 @@ use std::{net::SocketAddr, path::PathBuf, time::Duration}; use anyhow::{Context as _, Result}; use bytesize::ByteSize; -use common::block::{AccountInitialData, CommitmentsInitialData}; use indexer_service::{BackoffConfig, ChannelId, ClientConfig, IndexerConfig}; use key_protocol::key_management::KeyChain; use nssa::{Account, AccountId, PrivateKey, PublicKey}; use nssa_core::{account::Data, program::DEFAULT_PROGRAM_ID}; use sequencer_core::config::{BedrockConfig, SequencerConfig}; -use url::Url; -use wallet::config::{ - InitialAccountData, InitialAccountDataPrivate, InitialAccountDataPublic, WalletConfig, +use testnet_initial_state::{ + PrivateAccountPrivateInitialData, PrivateAccountPublicInitialData, + PublicAccountPrivateInitialData, PublicAccountPublicInitialData, }; +use url::Url; +use wallet::config::{InitialAccountData, WalletConfig}; /// Sequencer config options available for custom changes in integration tests. #[derive(Debug, Clone, Copy)] @@ -102,13 +103,13 @@ impl InitialData { } } - fn sequencer_initial_accounts(&self) -> Vec { + fn sequencer_initial_public_accounts(&self) -> Vec { self.public_accounts .iter() .map(|(priv_key, balance)| { let pub_key = PublicKey::new_from_private_key(priv_key); let account_id = AccountId::from(&pub_key); - AccountInitialData { + PublicAccountPublicInitialData { account_id, balance: *balance, } @@ -116,10 +117,10 @@ impl InitialData { .collect() } - fn sequencer_initial_commitments(&self) -> Vec { + fn sequencer_initial_private_accounts(&self) -> Vec { self.private_accounts .iter() - .map(|(key_chain, account)| CommitmentsInitialData { + .map(|(key_chain, account)| PrivateAccountPublicInitialData { npk: key_chain.nullifier_public_key.clone(), account: account.clone(), }) @@ -132,14 +133,14 @@ impl InitialData { .map(|(priv_key, _)| { let pub_key = PublicKey::new_from_private_key(priv_key); let account_id = AccountId::from(&pub_key); - InitialAccountData::Public(InitialAccountDataPublic { + InitialAccountData::Public(PublicAccountPrivateInitialData { account_id, pub_sign_key: priv_key.clone(), }) }) .chain(self.private_accounts.iter().map(|(key_chain, account)| { let account_id = AccountId::from(&key_chain.nullifier_public_key); - InitialAccountData::Private(Box::new(InitialAccountDataPrivate { + InitialAccountData::Private(Box::new(PrivateAccountPrivateInitialData { account_id, account: account.clone(), key_chain: key_chain.clone(), @@ -181,8 +182,8 @@ pub fn indexer_config( max_retries: 10, }, }, - initial_accounts: initial_data.sequencer_initial_accounts(), - initial_commitments: initial_data.sequencer_initial_commitments(), + initial_public_accounts: Some(initial_data.sequencer_initial_public_accounts()), + initial_private_accounts: Some(initial_data.sequencer_initial_private_accounts()), signing_key: [37; 32], channel_id: bedrock_channel_id(), }) @@ -211,8 +212,8 @@ pub fn sequencer_config( mempool_max_size, block_create_timeout, retry_pending_blocks_timeout: Duration::from_mins(2), - initial_accounts: initial_data.sequencer_initial_accounts(), - initial_commitments: initial_data.sequencer_initial_commitments(), + initial_public_accounts: Some(initial_data.sequencer_initial_public_accounts()), + initial_private_accounts: Some(initial_data.sequencer_initial_private_accounts()), signing_key: [37; 32], bedrock_config: BedrockConfig { backoff: BackoffConfig { @@ -240,7 +241,7 @@ pub fn wallet_config( seq_tx_poll_max_blocks: 15, seq_poll_max_retries: 10, seq_block_poll_max_amount: 100, - initial_accounts: initial_data.wallet_initial_accounts(), + initial_accounts: Some(initial_data.wallet_initial_accounts()), basic_auth: None, }) } diff --git a/integration_tests/tests/ata.rs b/integration_tests/tests/ata.rs new file mode 100644 index 00000000..94ba98c9 --- /dev/null +++ b/integration_tests/tests/ata.rs @@ -0,0 +1,656 @@ +#![expect( + clippy::shadow_unrelated, + clippy::tests_outside_test_module, + reason = "We don't care about these in tests" +)] + +use std::time::Duration; + +use anyhow::{Context as _, Result}; +use ata_core::{compute_ata_seed, get_associated_token_account_id}; +use integration_tests::{ + TIME_TO_WAIT_FOR_BLOCK_SECONDS, TestContext, format_private_account_id, + format_public_account_id, verify_commitment_is_in_state, +}; +use log::info; +use nssa::program::Program; +use sequencer_service_rpc::RpcClient as _; +use token_core::{TokenDefinition, TokenHolding}; +use tokio::test; +use wallet::cli::{ + Command, SubcommandReturnValue, + account::{AccountSubcommand, NewSubcommand}, + programs::{ata::AtaSubcommand, token::TokenProgramAgnosticSubcommand}, +}; + +/// Create a public account and return its ID. +async fn new_public_account(ctx: &mut TestContext) -> Result { + let result = wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Account(AccountSubcommand::New(NewSubcommand::Public { + cci: None, + label: None, + })), + ) + .await?; + let SubcommandReturnValue::RegisterAccount { account_id } = result else { + anyhow::bail!("Expected RegisterAccount return value"); + }; + Ok(account_id) +} + +/// Create a private account and return its ID. +async fn new_private_account(ctx: &mut TestContext) -> Result { + let result = wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Account(AccountSubcommand::New(NewSubcommand::Private { + cci: None, + label: None, + })), + ) + .await?; + let SubcommandReturnValue::RegisterAccount { account_id } = result else { + anyhow::bail!("Expected RegisterAccount return value"); + }; + Ok(account_id) +} + +#[test] +async fn create_ata_initializes_holding_account() -> Result<()> { + let mut ctx = TestContext::new().await?; + + let definition_account_id = new_public_account(&mut ctx).await?; + let supply_account_id = new_public_account(&mut ctx).await?; + let owner_account_id = new_public_account(&mut ctx).await?; + + // Create a fungible token + let total_supply = 100_u128; + wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Token(TokenProgramAgnosticSubcommand::New { + definition_account_id: format_public_account_id(definition_account_id), + supply_account_id: format_public_account_id(supply_account_id), + name: "TEST".to_owned(), + total_supply, + }), + ) + .await?; + + info!("Waiting for next block creation"); + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + + // Create the ATA for owner + definition + wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Ata(AtaSubcommand::Create { + owner: format_public_account_id(owner_account_id), + token_definition: definition_account_id.to_string(), + }), + ) + .await?; + + info!("Waiting for next block creation"); + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + + // Derive expected ATA address and check on-chain state + let ata_program_id = Program::ata().id(); + let ata_id = get_associated_token_account_id( + &ata_program_id, + &compute_ata_seed(owner_account_id, definition_account_id), + ); + + let ata_acc = ctx + .sequencer_client() + .get_account(ata_id) + .await + .context("ATA account not found")?; + + assert_eq!(ata_acc.program_owner, Program::token().id()); + let holding = TokenHolding::try_from(&ata_acc.data)?; + assert_eq!( + holding, + TokenHolding::Fungible { + definition_id: definition_account_id, + balance: 0, + } + ); + + Ok(()) +} + +#[test] +async fn create_ata_is_idempotent() -> Result<()> { + let mut ctx = TestContext::new().await?; + + let definition_account_id = new_public_account(&mut ctx).await?; + let supply_account_id = new_public_account(&mut ctx).await?; + let owner_account_id = new_public_account(&mut ctx).await?; + + // Create a fungible token + wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Token(TokenProgramAgnosticSubcommand::New { + definition_account_id: format_public_account_id(definition_account_id), + supply_account_id: format_public_account_id(supply_account_id), + name: "TEST".to_owned(), + total_supply: 100, + }), + ) + .await?; + + info!("Waiting for next block creation"); + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + + // Create the ATA once + wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Ata(AtaSubcommand::Create { + owner: format_public_account_id(owner_account_id), + token_definition: definition_account_id.to_string(), + }), + ) + .await?; + + info!("Waiting for next block creation"); + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + + // Create the ATA a second time — must succeed (idempotent) + wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Ata(AtaSubcommand::Create { + owner: format_public_account_id(owner_account_id), + token_definition: definition_account_id.to_string(), + }), + ) + .await?; + + info!("Waiting for next block creation"); + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + + // State must be unchanged + let ata_program_id = Program::ata().id(); + let ata_id = get_associated_token_account_id( + &ata_program_id, + &compute_ata_seed(owner_account_id, definition_account_id), + ); + + let ata_acc = ctx + .sequencer_client() + .get_account(ata_id) + .await + .context("ATA account not found")?; + + assert_eq!(ata_acc.program_owner, Program::token().id()); + let holding = TokenHolding::try_from(&ata_acc.data)?; + assert_eq!( + holding, + TokenHolding::Fungible { + definition_id: definition_account_id, + balance: 0, + } + ); + + Ok(()) +} + +#[test] +async fn transfer_and_burn_via_ata() -> Result<()> { + let mut ctx = TestContext::new().await?; + + let definition_account_id = new_public_account(&mut ctx).await?; + let supply_account_id = new_public_account(&mut ctx).await?; + let sender_account_id = new_public_account(&mut ctx).await?; + let recipient_account_id = new_public_account(&mut ctx).await?; + + let total_supply = 1000_u128; + + // Create a fungible token, supply goes to supply_account_id + wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Token(TokenProgramAgnosticSubcommand::New { + definition_account_id: format_public_account_id(definition_account_id), + supply_account_id: format_public_account_id(supply_account_id), + name: "TEST".to_owned(), + total_supply, + }), + ) + .await?; + + info!("Waiting for next block creation"); + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + + // Derive ATA addresses + let ata_program_id = Program::ata().id(); + let sender_ata_id = get_associated_token_account_id( + &ata_program_id, + &compute_ata_seed(sender_account_id, definition_account_id), + ); + let recipient_ata_id = get_associated_token_account_id( + &ata_program_id, + &compute_ata_seed(recipient_account_id, definition_account_id), + ); + + // Create ATAs for sender and recipient + wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Ata(AtaSubcommand::Create { + owner: format_public_account_id(sender_account_id), + token_definition: definition_account_id.to_string(), + }), + ) + .await?; + wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Ata(AtaSubcommand::Create { + owner: format_public_account_id(recipient_account_id), + token_definition: definition_account_id.to_string(), + }), + ) + .await?; + + info!("Waiting for next block creation"); + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + + // Fund sender's ATA from the supply account (direct token transfer) + let fund_amount = 200_u128; + wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Token(TokenProgramAgnosticSubcommand::Send { + from: format_public_account_id(supply_account_id), + to: Some(format_public_account_id(sender_ata_id)), + to_npk: None, + to_vpk: None, + amount: fund_amount, + }), + ) + .await?; + + info!("Waiting for next block creation"); + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + + // Transfer from sender's ATA to recipient's ATA via the ATA program + let transfer_amount = 50_u128; + wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Ata(AtaSubcommand::Send { + from: format_public_account_id(sender_account_id), + token_definition: definition_account_id.to_string(), + to: recipient_ata_id.to_string(), + amount: transfer_amount, + }), + ) + .await?; + + info!("Waiting for next block creation"); + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + + // Verify sender ATA balance decreased + let sender_ata_acc = ctx.sequencer_client().get_account(sender_ata_id).await?; + let sender_holding = TokenHolding::try_from(&sender_ata_acc.data)?; + assert_eq!( + sender_holding, + TokenHolding::Fungible { + definition_id: definition_account_id, + balance: fund_amount - transfer_amount, + } + ); + + // Verify recipient ATA balance increased + let recipient_ata_acc = ctx.sequencer_client().get_account(recipient_ata_id).await?; + let recipient_holding = TokenHolding::try_from(&recipient_ata_acc.data)?; + assert_eq!( + recipient_holding, + TokenHolding::Fungible { + definition_id: definition_account_id, + balance: transfer_amount, + } + ); + + // Burn from sender's ATA + let burn_amount = 30_u128; + wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Ata(AtaSubcommand::Burn { + holder: format_public_account_id(sender_account_id), + token_definition: definition_account_id.to_string(), + amount: burn_amount, + }), + ) + .await?; + + info!("Waiting for next block creation"); + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + + // Verify sender ATA balance after burn + let sender_ata_acc = ctx.sequencer_client().get_account(sender_ata_id).await?; + let sender_holding = TokenHolding::try_from(&sender_ata_acc.data)?; + assert_eq!( + sender_holding, + TokenHolding::Fungible { + definition_id: definition_account_id, + balance: fund_amount - transfer_amount - burn_amount, + } + ); + + // Verify the token definition total_supply decreased by burn_amount + let definition_acc = ctx + .sequencer_client() + .get_account(definition_account_id) + .await?; + let token_definition = TokenDefinition::try_from(&definition_acc.data)?; + assert_eq!( + token_definition, + TokenDefinition::Fungible { + name: "TEST".to_owned(), + total_supply: total_supply - burn_amount, + metadata_id: None, + } + ); + + Ok(()) +} + +#[test] +async fn create_ata_with_private_owner() -> Result<()> { + let mut ctx = TestContext::new().await?; + + let definition_account_id = new_public_account(&mut ctx).await?; + let supply_account_id = new_public_account(&mut ctx).await?; + let owner_account_id = new_private_account(&mut ctx).await?; + + // Create a fungible token + wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Token(TokenProgramAgnosticSubcommand::New { + definition_account_id: format_public_account_id(definition_account_id), + supply_account_id: format_public_account_id(supply_account_id), + name: "TEST".to_owned(), + total_supply: 100, + }), + ) + .await?; + + info!("Waiting for next block creation"); + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + + // Create the ATA for the private owner + definition + wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Ata(AtaSubcommand::Create { + owner: format_private_account_id(owner_account_id), + token_definition: definition_account_id.to_string(), + }), + ) + .await?; + + info!("Waiting for next block creation"); + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + + // Derive expected ATA address and check on-chain state + let ata_program_id = Program::ata().id(); + let ata_id = get_associated_token_account_id( + &ata_program_id, + &compute_ata_seed(owner_account_id, definition_account_id), + ); + + let ata_acc = ctx + .sequencer_client() + .get_account(ata_id) + .await + .context("ATA account not found")?; + + assert_eq!(ata_acc.program_owner, Program::token().id()); + let holding = TokenHolding::try_from(&ata_acc.data)?; + assert_eq!( + holding, + TokenHolding::Fungible { + definition_id: definition_account_id, + balance: 0, + } + ); + + // Verify the private owner's commitment is in state + let commitment = ctx + .wallet() + .get_private_account_commitment(owner_account_id) + .context("Private owner commitment not found")?; + assert!(verify_commitment_is_in_state(commitment, ctx.sequencer_client()).await); + + Ok(()) +} + +#[test] +async fn transfer_via_ata_private_owner() -> Result<()> { + let mut ctx = TestContext::new().await?; + + let definition_account_id = new_public_account(&mut ctx).await?; + let supply_account_id = new_public_account(&mut ctx).await?; + let sender_account_id = new_private_account(&mut ctx).await?; + let recipient_account_id = new_public_account(&mut ctx).await?; + + let total_supply = 1000_u128; + + // Create a fungible token + wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Token(TokenProgramAgnosticSubcommand::New { + definition_account_id: format_public_account_id(definition_account_id), + supply_account_id: format_public_account_id(supply_account_id), + name: "TEST".to_owned(), + total_supply, + }), + ) + .await?; + + info!("Waiting for next block creation"); + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + + // Derive ATA addresses + let ata_program_id = Program::ata().id(); + let sender_ata_id = get_associated_token_account_id( + &ata_program_id, + &compute_ata_seed(sender_account_id, definition_account_id), + ); + let recipient_ata_id = get_associated_token_account_id( + &ata_program_id, + &compute_ata_seed(recipient_account_id, definition_account_id), + ); + + // Create ATAs for sender (private owner) and recipient (public owner) + wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Ata(AtaSubcommand::Create { + owner: format_private_account_id(sender_account_id), + token_definition: definition_account_id.to_string(), + }), + ) + .await?; + wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Ata(AtaSubcommand::Create { + owner: format_public_account_id(recipient_account_id), + token_definition: definition_account_id.to_string(), + }), + ) + .await?; + + info!("Waiting for next block creation"); + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + + // Fund sender's ATA from the supply account (direct token transfer) + let fund_amount = 200_u128; + wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Token(TokenProgramAgnosticSubcommand::Send { + from: format_public_account_id(supply_account_id), + to: Some(format_public_account_id(sender_ata_id)), + to_npk: None, + to_vpk: None, + amount: fund_amount, + }), + ) + .await?; + + info!("Waiting for next block creation"); + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + + // Transfer from sender's ATA (private owner) to recipient's ATA + let transfer_amount = 50_u128; + wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Ata(AtaSubcommand::Send { + from: format_private_account_id(sender_account_id), + token_definition: definition_account_id.to_string(), + to: recipient_ata_id.to_string(), + amount: transfer_amount, + }), + ) + .await?; + + info!("Waiting for next block creation"); + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + + // Verify sender ATA balance decreased + let sender_ata_acc = ctx.sequencer_client().get_account(sender_ata_id).await?; + let sender_holding = TokenHolding::try_from(&sender_ata_acc.data)?; + assert_eq!( + sender_holding, + TokenHolding::Fungible { + definition_id: definition_account_id, + balance: fund_amount - transfer_amount, + } + ); + + // Verify recipient ATA balance increased + let recipient_ata_acc = ctx.sequencer_client().get_account(recipient_ata_id).await?; + let recipient_holding = TokenHolding::try_from(&recipient_ata_acc.data)?; + assert_eq!( + recipient_holding, + TokenHolding::Fungible { + definition_id: definition_account_id, + balance: transfer_amount, + } + ); + + // Verify the private sender's commitment is in state + let commitment = ctx + .wallet() + .get_private_account_commitment(sender_account_id) + .context("Private sender commitment not found")?; + assert!(verify_commitment_is_in_state(commitment, ctx.sequencer_client()).await); + + Ok(()) +} + +#[test] +async fn burn_via_ata_private_owner() -> Result<()> { + let mut ctx = TestContext::new().await?; + + let definition_account_id = new_public_account(&mut ctx).await?; + let supply_account_id = new_public_account(&mut ctx).await?; + let holder_account_id = new_private_account(&mut ctx).await?; + + let total_supply = 500_u128; + + // Create a fungible token + wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Token(TokenProgramAgnosticSubcommand::New { + definition_account_id: format_public_account_id(definition_account_id), + supply_account_id: format_public_account_id(supply_account_id), + name: "TEST".to_owned(), + total_supply, + }), + ) + .await?; + + info!("Waiting for next block creation"); + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + + // Derive holder's ATA address + let ata_program_id = Program::ata().id(); + let holder_ata_id = get_associated_token_account_id( + &ata_program_id, + &compute_ata_seed(holder_account_id, definition_account_id), + ); + + // Create ATA for the private holder + wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Ata(AtaSubcommand::Create { + owner: format_private_account_id(holder_account_id), + token_definition: definition_account_id.to_string(), + }), + ) + .await?; + + info!("Waiting for next block creation"); + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + + // Fund holder's ATA from the supply account + let fund_amount = 300_u128; + wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Token(TokenProgramAgnosticSubcommand::Send { + from: format_public_account_id(supply_account_id), + to: Some(format_public_account_id(holder_ata_id)), + to_npk: None, + to_vpk: None, + amount: fund_amount, + }), + ) + .await?; + + info!("Waiting for next block creation"); + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + + // Burn from holder's ATA (private owner) + let burn_amount = 100_u128; + wallet::cli::execute_subcommand( + ctx.wallet_mut(), + Command::Ata(AtaSubcommand::Burn { + holder: format_private_account_id(holder_account_id), + token_definition: definition_account_id.to_string(), + amount: burn_amount, + }), + ) + .await?; + + info!("Waiting for next block creation"); + tokio::time::sleep(Duration::from_secs(TIME_TO_WAIT_FOR_BLOCK_SECONDS)).await; + + // Verify holder ATA balance after burn + let holder_ata_acc = ctx.sequencer_client().get_account(holder_ata_id).await?; + let holder_holding = TokenHolding::try_from(&holder_ata_acc.data)?; + assert_eq!( + holder_holding, + TokenHolding::Fungible { + definition_id: definition_account_id, + balance: fund_amount - burn_amount, + } + ); + + // Verify the token definition total_supply decreased by burn_amount + let definition_acc = ctx + .sequencer_client() + .get_account(definition_account_id) + .await?; + let token_definition = TokenDefinition::try_from(&definition_acc.data)?; + assert_eq!( + token_definition, + TokenDefinition::Fungible { + name: "TEST".to_owned(), + total_supply: total_supply - burn_amount, + metadata_id: None, + } + ); + + // Verify the private holder's commitment is in state + let commitment = ctx + .wallet() + .get_private_account_commitment(holder_account_id) + .context("Private holder commitment not found")?; + assert!(verify_commitment_is_in_state(commitment, ctx.sequencer_client()).await); + + Ok(()) +} diff --git a/key_protocol/src/key_management/secret_holders.rs b/key_protocol/src/key_management/secret_holders.rs index 45e640da..02890631 100644 --- a/key_protocol/src/key_management/secret_holders.rs +++ b/key_protocol/src/key_management/secret_holders.rs @@ -20,17 +20,16 @@ pub struct SeedHolder { /// Secret spending key object. Can produce `PrivateKeyHolder` objects. #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] -pub struct SecretSpendingKey(pub(crate) [u8; 32]); +pub struct SecretSpendingKey(pub [u8; 32]); pub type ViewingSecretKey = Scalar; #[derive(Serialize, Deserialize, Debug, Clone)] /// Private key holder. Produces public keys. Can produce `account_id`. Can produce shared secret /// for recepient. -#[expect(clippy::partial_pub_fields, reason = "TODO: fix later")] pub struct PrivateKeyHolder { pub nullifier_secret_key: NullifierSecretKey, - pub(crate) viewing_secret_key: ViewingSecretKey, + pub viewing_secret_key: ViewingSecretKey, } impl SeedHolder { diff --git a/nssa/core/src/program.rs b/nssa/core/src/program.rs index 334a2962..095d1777 100644 --- a/nssa/core/src/program.rs +++ b/nssa/core/src/program.rs @@ -157,7 +157,7 @@ pub type BlockId = u64; /// Unix timestamp in milliseconds. pub type Timestamp = u64; -#[derive(Serialize, Deserialize, Clone, Copy)] +#[derive(Clone, Copy, Serialize, Deserialize)] #[cfg_attr( any(feature = "host", test), derive(Debug, PartialEq, Eq, BorshSerialize, BorshDeserialize) @@ -170,6 +170,7 @@ pub struct ValidityWindow { } impl ValidityWindow { + /// Creates a window with no bounds, valid for every block ID. #[must_use] pub const fn new_unbounded() -> Self { Self { @@ -197,6 +198,7 @@ impl ValidityWindow { self.from.is_none_or(|start| id >= start) && self.to.is_none_or(|end| id < end) } + /// Returns `Err(InvalidWindow)` if both bounds are set and `from >= to`. const fn check_window(&self) -> Result<(), InvalidWindow> { if let (Some(from_id), Some(until_id)) = (self.from, self.to) && from_id >= until_id @@ -211,13 +213,15 @@ impl ValidityWindow { Ok(()) } + /// Inclusive lower bound. `None` means the window starts at the beginning of the chain. #[must_use] - pub const fn from(&self) -> Option { + pub const fn start(&self) -> Option { self.from } + /// Exclusive upper bound. `None` means the window has no expiry. #[must_use] - pub const fn to(&self) -> Option { + pub const fn end(&self) -> Option { self.to } @@ -276,12 +280,49 @@ impl } } +impl TryFrom> for ValidityWindow { + type Error = InvalidWindow; + + fn try_from(value: std::ops::Range) -> Result { + (Some(value.start), Some(value.end)).try_into() + } +} + +impl From> for ValidityWindow { + fn from(value: std::ops::RangeFrom) -> Self { + Self { + from: Some(value.start), + to: None, + from_timestamp: None, + to_timestamp: None, + } + } +} + +impl From> for ValidityWindow { + fn from(value: std::ops::RangeTo) -> Self { + Self { + from: None, + to: Some(value.end), + from_timestamp: None, + to_timestamp: None, + } + } +} + +impl From for ValidityWindow { + fn from(_: std::ops::RangeFull) -> Self { + Self::new_unbounded() + } +} + #[derive(Debug, thiserror::Error, Clone, Copy, PartialEq, Eq)] #[error("Invalid window")] pub struct InvalidWindow; #[derive(Serialize, Deserialize, Clone)] #[cfg_attr(any(feature = "host", test), derive(Debug, PartialEq, Eq))] +#[must_use = "ProgramOutput does nothing unless written"] pub struct ProgramOutput { /// The instruction data the program received to produce this output. pub instruction_data: InstructionData, @@ -292,13 +333,10 @@ pub struct ProgramOutput { /// The list of chained calls to other programs. pub chained_calls: Vec, /// The window where the program output is valid. - /// Valid for block IDs in the range [from, to), where `from` is included and `to` is excluded. - /// `None` means unbounded on that side. pub validity_window: ValidityWindow, } impl ProgramOutput { - #[must_use] pub const fn new( instruction_data: InstructionData, pre_states: Vec, @@ -317,21 +355,24 @@ impl ProgramOutput { env::commit(&self); } - #[must_use] pub fn with_chained_calls(mut self, chained_calls: Vec) -> Self { self.chained_calls = chained_calls; self } - pub fn valid_from_id(mut self, id: Option) -> Result { - self.validity_window.from = id; - self.validity_window.check_window()?; - Ok(self) + /// Sets the validity window from an infallible range conversion (`1..`, `..5`, `..`). + pub fn with_validity_window>(mut self, window: W) -> Self { + self.validity_window = window.into(); + self } - pub fn valid_until_id(mut self, id: Option) -> Result { - self.validity_window.to = id; - self.validity_window.check_window()?; + /// Sets the validity window from a fallible range conversion (`1..5`). + /// Returns `Err` if the range is empty. + pub fn try_with_validity_window>( + mut self, + window: W, + ) -> Result { + self.validity_window = window.try_into()?; Ok(self) } @@ -405,25 +446,6 @@ pub fn read_nssa_inputs() -> (ProgramInput, InstructionD ) } -pub fn write_nssa_outputs( - instruction_data: InstructionData, - pre_states: Vec, - post_states: Vec, -) { - ProgramOutput::new(instruction_data, pre_states, post_states).write(); -} - -pub fn write_nssa_outputs_with_chained_call( - instruction_data: InstructionData, - pre_states: Vec, - post_states: Vec, - chained_calls: Vec, -) { - ProgramOutput::new(instruction_data, pre_states, post_states) - .with_chained_calls(chained_calls) - .write(); -} - /// Validates well-behaved program execution. /// /// # Parameters @@ -518,6 +540,132 @@ fn validate_uniqueness_of_account_ids(pre_states: &[AccountWithMetadata]) -> boo mod tests { use super::*; + #[test] + fn validity_window_unbounded_accepts_any_block() { + let w = ValidityWindow::new_unbounded(); + assert!(w.is_valid_for_block_id(0)); + assert!(w.is_valid_for_block_id(u64::MAX)); + } + + #[test] + fn validity_window_bounded_range_includes_from_excludes_to() { + let w: ValidityWindow = (Some(5), Some(10)).try_into().unwrap(); + assert!(!w.is_valid_for_block_id(4)); + assert!(w.is_valid_for_block_id(5)); + assert!(w.is_valid_for_block_id(9)); + assert!(!w.is_valid_for_block_id(10)); + } + + #[test] + fn validity_window_only_from_bound() { + let w: ValidityWindow = (Some(5), None).try_into().unwrap(); + assert!(!w.is_valid_for_block_id(4)); + assert!(w.is_valid_for_block_id(5)); + assert!(w.is_valid_for_block_id(u64::MAX)); + } + + #[test] + fn validity_window_only_to_bound() { + let w: ValidityWindow = (None, Some(5)).try_into().unwrap(); + assert!(w.is_valid_for_block_id(0)); + assert!(w.is_valid_for_block_id(4)); + assert!(!w.is_valid_for_block_id(5)); + } + + #[test] + fn validity_window_adjacent_bounds_are_invalid() { + // [5, 5) is an empty range — from == to + assert!(ValidityWindow::try_from((Some(5), Some(5))).is_err()); + } + + #[test] + fn validity_window_inverted_bounds_are_invalid() { + assert!(ValidityWindow::try_from((Some(10), Some(5))).is_err()); + } + + #[test] + fn validity_window_getters_match_construction() { + let w: ValidityWindow = (Some(3), Some(7)).try_into().unwrap(); + assert_eq!(w.start(), Some(3)); + assert_eq!(w.end(), Some(7)); + } + + #[test] + fn validity_window_getters_for_unbounded() { + let w = ValidityWindow::new_unbounded(); + assert_eq!(w.start(), None); + assert_eq!(w.end(), None); + } + + #[test] + fn validity_window_from_range() { + let w = ValidityWindow::try_from(5_u64..10).unwrap(); + assert_eq!(w.start(), Some(5)); + assert_eq!(w.end(), Some(10)); + } + + #[test] + fn validity_window_from_range_empty_is_invalid() { + assert!(ValidityWindow::try_from(5_u64..5).is_err()); + } + + #[test] + fn validity_window_from_range_inverted_is_invalid() { + let from = 10_u64; + let to = 5_u64; + assert!(ValidityWindow::try_from(from..to).is_err()); + } + + #[test] + fn validity_window_from_range_from() { + let w: ValidityWindow = (5_u64..).into(); + assert_eq!(w.start(), Some(5)); + assert_eq!(w.end(), None); + } + + #[test] + fn validity_window_from_range_to() { + let w: ValidityWindow = (..10_u64).into(); + assert_eq!(w.start(), None); + assert_eq!(w.end(), Some(10)); + } + + #[test] + fn validity_window_from_range_full() { + let w: ValidityWindow = (..).into(); + assert_eq!(w.start(), None); + assert_eq!(w.end(), None); + } + + #[test] + fn program_output_try_with_validity_window_range() { + let output = ProgramOutput::new(vec![], vec![], vec![]) + .try_with_validity_window(10_u64..100) + .unwrap(); + assert_eq!(output.validity_window.start(), Some(10)); + assert_eq!(output.validity_window.end(), Some(100)); + } + + #[test] + fn program_output_with_validity_window_range_from() { + let output = ProgramOutput::new(vec![], vec![], vec![]).with_validity_window(10_u64..); + assert_eq!(output.validity_window.start(), Some(10)); + assert_eq!(output.validity_window.end(), None); + } + + #[test] + fn program_output_with_validity_window_range_to() { + let output = ProgramOutput::new(vec![], vec![], vec![]).with_validity_window(..100_u64); + assert_eq!(output.validity_window.start(), None); + assert_eq!(output.validity_window.end(), Some(100)); + } + + #[test] + fn program_output_try_with_validity_window_empty_range_fails() { + let result = ProgramOutput::new(vec![], vec![], vec![]).try_with_validity_window(5_u64..5); + assert!(result.is_err()); + } + #[test] fn post_state_new_with_claim_constructor() { let account = Account { diff --git a/nssa/src/privacy_preserving_transaction/circuit.rs b/nssa/src/privacy_preserving_transaction/circuit.rs index 2ab141a3..0ae7eaac 100644 --- a/nssa/src/privacy_preserving_transaction/circuit.rs +++ b/nssa/src/privacy_preserving_transaction/circuit.rs @@ -174,12 +174,13 @@ mod tests { #![expect(clippy::shadow_unrelated, reason = "We don't care about it in tests")] use nssa_core::{ - Commitment, DUMMY_COMMITMENT_HASH, EncryptionScheme, Nullifier, + Commitment, DUMMY_COMMITMENT_HASH, EncryptionScheme, Nullifier, SharedSecretKey, account::{Account, AccountId, AccountWithMetadata, Nonce, data::Data}, }; use super::*; use crate::{ + error::NssaError, privacy_preserving_transaction::circuit::execute_and_prove, program::Program, state::{ @@ -364,4 +365,46 @@ mod tests { .unwrap(); assert_eq!(recipient_post, expected_private_account_2); } + + #[test] + fn circuit_fails_when_chained_validity_windows_have_empty_intersection() { + let account_keys = test_private_account_keys_1(); + let pre = AccountWithMetadata::new( + Account::default(), + false, + AccountId::from(&account_keys.npk()), + ); + + let validity_window_chain_caller = Program::validity_window_chain_caller(); + let validity_window = Program::validity_window(); + + let instruction = Program::serialize_instruction(( + Some(1_u64), + Some(4_u64), + validity_window.id(), + Some(4_u64), + Some(7_u64), + )) + .unwrap(); + + let esk = [3; 32]; + let shared_secret = SharedSecretKey::new(&esk, &account_keys.vpk()); + + let program_with_deps = ProgramWithDependencies::new( + validity_window_chain_caller, + [(validity_window.id(), validity_window)].into(), + ); + + let result = execute_and_prove( + vec![pre], + instruction, + vec![2], + vec![(account_keys.npk(), shared_secret)], + vec![], + vec![None], + &program_with_deps, + ); + + assert!(matches!(result, Err(NssaError::CircuitProvingError(_)))); + } } diff --git a/nssa/src/program.rs b/nssa/src/program.rs index fa5e7b42..b87fcf35 100644 --- a/nssa/src/program.rs +++ b/nssa/src/program.rs @@ -8,7 +8,9 @@ use serde::Serialize; use crate::{ error::NssaError, - program_methods::{AMM_ELF, AUTHENTICATED_TRANSFER_ELF, PINATA_ELF, TOKEN_ELF}, + program_methods::{ + AMM_ELF, ASSOCIATED_TOKEN_ACCOUNT_ELF, AUTHENTICATED_TRANSFER_ELF, PINATA_ELF, TOKEN_ELF, + }, }; /// Maximum number of cycles for a public execution. @@ -105,6 +107,12 @@ impl Program { pub fn amm() -> Self { Self::new(AMM_ELF.to_vec()).expect("The AMM program must be a valid Risc0 program") } + + #[must_use] + pub fn ata() -> Self { + Self::new(ASSOCIATED_TOKEN_ACCOUNT_ELF.to_vec()) + .expect("The ATA program must be a valid Risc0 program") + } } // TODO: Testnet only. Refactor to prevent compilation on mainnet. @@ -292,6 +300,12 @@ mod tests { // `program_methods` Self::new(VALIDITY_WINDOW_ELF.to_vec()).unwrap() } + + #[must_use] + pub fn validity_window_chain_caller() -> Self { + use test_program_methods::VALIDITY_WINDOW_CHAIN_CALLER_ELF; + Self::new(VALIDITY_WINDOW_CHAIN_CALLER_ELF.to_vec()).unwrap() + } } #[test] diff --git a/nssa/src/state.rs b/nssa/src/state.rs index a0812801..6a2681b8 100644 --- a/nssa/src/state.rs +++ b/nssa/src/state.rs @@ -146,6 +146,7 @@ impl V03State { this.insert_program(Program::authenticated_transfer_program()); this.insert_program(Program::token()); this.insert_program(Program::amm()); + this.insert_program(Program::ata()); this } @@ -343,7 +344,7 @@ pub mod tests { Commitment, Nullifier, NullifierPublicKey, NullifierSecretKey, SharedSecretKey, account::{Account, AccountId, AccountWithMetadata, Nonce, data::Data}, encryption::{EphemeralPublicKey, Scalar, ViewingPublicKey}, - program::{BlockId, PdaSeed, ProgramId, Timestamp}, + program::{BlockId, PdaSeed, ProgramId, ValidityWindow}, }; use crate::{ @@ -507,6 +508,7 @@ pub mod tests { ); this.insert(Program::token().id(), Program::token()); this.insert(Program::amm().id(), Program::amm()); + this.insert(Program::ata().id(), Program::ata()); this }; @@ -3019,6 +3021,7 @@ pub mod tests { validity_window: (Option, Option), block_id: BlockId, ) { + let validity_window: ValidityWindow = validity_window.try_into().unwrap(); let validity_window_program = Program::validity_window(); let account_keys = test_public_account_keys_1(); let pre = AccountWithMetadata::new(Account::default(), false, account_keys.account_id()); @@ -3027,12 +3030,7 @@ pub mod tests { let account_ids = vec![pre.account_id]; let nonces = vec![]; let program_id = validity_window_program.id(); - let instruction = ( - validity_window.0, - validity_window.1, - None::, - None::, - ); + let instruction = validity_window; let message = public_transaction::Message::try_new(program_id, account_ids, nonces, instruction) .unwrap(); @@ -3040,7 +3038,7 @@ pub mod tests { PublicTransaction::new(message, witness_set) }; let result = state.transition_from_public_transaction(&tx, block_id, 0); - let is_inside_validity_window = match validity_window { + let is_inside_validity_window = match (validity_window.start(), validity_window.end()) { (Some(s), Some(e)) => s <= block_id && block_id < e, (Some(s), None) => s <= block_id, (None, Some(e)) => block_id < e, @@ -3070,6 +3068,7 @@ pub mod tests { validity_window: (Option, Option), block_id: BlockId, ) { + let validity_window: ValidityWindow = validity_window.try_into().unwrap(); let validity_window_program = Program::validity_window(); let account_keys = test_private_account_keys_1(); let pre = AccountWithMetadata::new(Account::default(), false, &account_keys.npk()); @@ -3079,12 +3078,7 @@ pub mod tests { let shared_secret = SharedSecretKey::new(&esk, &account_keys.vpk()); let epk = EphemeralPublicKey::from_scalar(esk); - let instruction = ( - validity_window.0, - validity_window.1, - None::, - None::, - ); + let instruction = validity_window; let (output, proof) = circuit::execute_and_prove( vec![pre], Program::serialize_instruction(instruction).unwrap(), @@ -3108,7 +3102,7 @@ pub mod tests { PrivacyPreservingTransaction::new(message, witness_set) }; let result = state.transition_from_privacy_preserving_transaction(&tx, block_id, 0); - let is_inside_validity_window = match validity_window { + let is_inside_validity_window = match (validity_window.start(), validity_window.end()) { (Some(s), Some(e)) => s <= block_id && block_id < e, (Some(s), None) => s <= block_id, (None, Some(e)) => block_id < e, diff --git a/program_methods/guest/Cargo.toml b/program_methods/guest/Cargo.toml index eabcffc8..29ef8304 100644 --- a/program_methods/guest/Cargo.toml +++ b/program_methods/guest/Cargo.toml @@ -13,5 +13,7 @@ token_core.workspace = true token_program.workspace = true amm_core.workspace = true amm_program.workspace = true +ata_core.workspace = true +ata_program.workspace = true risc0-zkvm.workspace = true serde = { workspace = true, default-features = false } diff --git a/program_methods/guest/src/bin/amm.rs b/program_methods/guest/src/bin/amm.rs index 00fd39d3..748630d9 100644 --- a/program_methods/guest/src/bin/amm.rs +++ b/program_methods/guest/src/bin/amm.rs @@ -9,7 +9,7 @@ use std::num::NonZero; use amm_core::Instruction; -use nssa_core::program::{ProgramInput, read_nssa_inputs, write_nssa_outputs_with_chained_call}; +use nssa_core::program::{ProgramInput, ProgramOutput, read_nssa_inputs}; fn main() { let ( @@ -133,10 +133,7 @@ fn main() { } }; - write_nssa_outputs_with_chained_call( - instruction_words, - pre_states_clone, - post_states, - chained_calls, - ); + ProgramOutput::new(instruction_words, pre_states_clone, post_states) + .with_chained_calls(chained_calls) + .write(); } diff --git a/program_methods/guest/src/bin/associated_token_account.rs b/program_methods/guest/src/bin/associated_token_account.rs new file mode 100644 index 00000000..55d5824b --- /dev/null +++ b/program_methods/guest/src/bin/associated_token_account.rs @@ -0,0 +1,62 @@ +use ata_core::Instruction; +use nssa_core::program::{ProgramInput, ProgramOutput, read_nssa_inputs}; + +fn main() { + let ( + ProgramInput { + pre_states, + instruction, + }, + instruction_words, + ) = read_nssa_inputs::(); + + let pre_states_clone = pre_states.clone(); + + let (post_states, chained_calls) = match instruction { + Instruction::Create { ata_program_id } => { + let [owner, token_definition, ata_account] = pre_states + .try_into() + .expect("Create instruction requires exactly three accounts"); + ata_program::create::create_associated_token_account( + owner, + token_definition, + ata_account, + ata_program_id, + ) + } + Instruction::Transfer { + ata_program_id, + amount, + } => { + let [owner, sender_ata, recipient] = pre_states + .try_into() + .expect("Transfer instruction requires exactly three accounts"); + ata_program::transfer::transfer_from_associated_token_account( + owner, + sender_ata, + recipient, + ata_program_id, + amount, + ) + } + Instruction::Burn { + ata_program_id, + amount, + } => { + let [owner, holder_ata, token_definition] = pre_states + .try_into() + .expect("Burn instruction requires exactly three accounts"); + ata_program::burn::burn_from_associated_token_account( + owner, + holder_ata, + token_definition, + ata_program_id, + amount, + ) + } + }; + + ProgramOutput::new(instruction_words, pre_states_clone, post_states) + .with_chained_calls(chained_calls) + .write(); +} diff --git a/program_methods/guest/src/bin/authenticated_transfer.rs b/program_methods/guest/src/bin/authenticated_transfer.rs index 7835f733..20f4dd68 100644 --- a/program_methods/guest/src/bin/authenticated_transfer.rs +++ b/program_methods/guest/src/bin/authenticated_transfer.rs @@ -1,7 +1,7 @@ use nssa_core::{ account::{Account, AccountWithMetadata}, program::{ - AccountPostState, DEFAULT_PROGRAM_ID, ProgramInput, read_nssa_inputs, write_nssa_outputs, + AccountPostState, DEFAULT_PROGRAM_ID, ProgramInput, ProgramOutput, read_nssa_inputs, }, }; @@ -84,5 +84,5 @@ fn main() { _ => panic!("invalid params"), }; - write_nssa_outputs(instruction_words, pre_states, post_states); + ProgramOutput::new(instruction_words, pre_states, post_states).write(); } diff --git a/program_methods/guest/src/bin/pinata.rs b/program_methods/guest/src/bin/pinata.rs index c9fc0735..cfe0a7e4 100644 --- a/program_methods/guest/src/bin/pinata.rs +++ b/program_methods/guest/src/bin/pinata.rs @@ -1,4 +1,4 @@ -use nssa_core::program::{AccountPostState, ProgramInput, read_nssa_inputs, write_nssa_outputs}; +use nssa_core::program::{AccountPostState, ProgramInput, ProgramOutput, read_nssa_inputs}; use risc0_zkvm::sha::{Impl, Sha256 as _}; const PRIZE: u128 = 150; @@ -78,12 +78,13 @@ fn main() { .checked_add(PRIZE) .expect("Overflow when adding prize to winner"); - write_nssa_outputs( + ProgramOutput::new( instruction_words, vec![pinata, winner], vec![ AccountPostState::new_claimed_if_default(pinata_post), AccountPostState::new(winner_post), ], - ); + ) + .write(); } diff --git a/program_methods/guest/src/bin/pinata_token.rs b/program_methods/guest/src/bin/pinata_token.rs index f1bbdc87..3dee05b7 100644 --- a/program_methods/guest/src/bin/pinata_token.rs +++ b/program_methods/guest/src/bin/pinata_token.rs @@ -1,8 +1,7 @@ use nssa_core::{ account::Data, program::{ - AccountPostState, ChainedCall, PdaSeed, ProgramInput, read_nssa_inputs, - write_nssa_outputs_with_chained_call, + AccountPostState, ChainedCall, PdaSeed, ProgramInput, ProgramOutput, read_nssa_inputs, }, }; use risc0_zkvm::sha::{Impl, Sha256 as _}; @@ -97,7 +96,7 @@ fn main() { ) .with_pda_seeds(vec![PdaSeed::new([0; 32])]); - write_nssa_outputs_with_chained_call( + ProgramOutput::new( instruction_words, vec![ pinata_definition, @@ -109,6 +108,7 @@ fn main() { AccountPostState::new(pinata_token_holding_post), AccountPostState::new(winner_token_holding_post), ], - vec![chained_call], - ); + ) + .with_chained_calls(vec![chained_call]) + .write(); } diff --git a/program_methods/guest/src/bin/privacy_preserving_circuit.rs b/program_methods/guest/src/bin/privacy_preserving_circuit.rs index b3e4b129..2a60989d 100644 --- a/program_methods/guest/src/bin/privacy_preserving_circuit.rs +++ b/program_methods/guest/src/bin/privacy_preserving_circuit.rs @@ -28,11 +28,11 @@ impl ExecutionState { pub fn derive_from_outputs(program_id: ProgramId, program_outputs: Vec) -> Self { let valid_from_id = program_outputs .iter() - .filter_map(|output| output.validity_window.from()) + .filter_map(|output| output.validity_window.start()) .max(); let valid_until_id = program_outputs .iter() - .filter_map(|output| output.validity_window.to()) + .filter_map(|output| output.validity_window.end()) .min(); let valid_from_ts = program_outputs .iter() diff --git a/program_methods/guest/src/bin/token.rs b/program_methods/guest/src/bin/token.rs index 0bc3d245..421d43ef 100644 --- a/program_methods/guest/src/bin/token.rs +++ b/program_methods/guest/src/bin/token.rs @@ -6,7 +6,7 @@ //! Token program accepts [`Instruction`] as input, refer to the corresponding documentation //! for more details. -use nssa_core::program::{ProgramInput, read_nssa_inputs, write_nssa_outputs}; +use nssa_core::program::{ProgramInput, ProgramOutput, read_nssa_inputs}; use token_program::core::Instruction; fn main() { @@ -81,5 +81,5 @@ fn main() { } }; - write_nssa_outputs(instruction_words, pre_states_clone, post_states); + ProgramOutput::new(instruction_words, pre_states_clone, post_states).write(); } diff --git a/programs/associated_token_account/Cargo.toml b/programs/associated_token_account/Cargo.toml new file mode 100644 index 00000000..98e0bfd0 --- /dev/null +++ b/programs/associated_token_account/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "ata_program" +version = "0.1.0" +edition = "2024" +license = { workspace = true } + +[dependencies] +nssa_core.workspace = true +token_core.workspace = true +ata_core.workspace = true diff --git a/programs/associated_token_account/core/Cargo.toml b/programs/associated_token_account/core/Cargo.toml new file mode 100644 index 00000000..7ca8d7fa --- /dev/null +++ b/programs/associated_token_account/core/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "ata_core" +version = "0.1.0" +edition = "2024" +license = { workspace = true } + +[dependencies] +nssa_core.workspace = true +serde.workspace = true +risc0-zkvm.workspace = true diff --git a/programs/associated_token_account/core/src/lib.rs b/programs/associated_token_account/core/src/lib.rs new file mode 100644 index 00000000..994c632b --- /dev/null +++ b/programs/associated_token_account/core/src/lib.rs @@ -0,0 +1,82 @@ +pub use nssa_core::program::PdaSeed; +use nssa_core::{ + account::{AccountId, AccountWithMetadata}, + program::ProgramId, +}; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize)] +pub enum Instruction { + /// Create the Associated Token Account for (owner, definition). + /// Idempotent: no-op if the account already exists. + /// + /// Required accounts (3): + /// - Owner account + /// - Token definition account + /// - Associated token account (default/uninitialized, or already initialized) + /// + /// `token_program_id` is derived from `token_definition.account.program_owner`. + Create { ata_program_id: ProgramId }, + + /// Transfer tokens FROM owner's ATA to a recipient holding account. + /// Uses PDA seeds to authorize the ATA in the chained Token::Transfer call. + /// + /// Required accounts (3): + /// - Owner account (authorized) + /// - Sender ATA (owner's token holding) + /// - Recipient token holding (any account; auto-created if default) + /// + /// `token_program_id` is derived from `sender_ata.account.program_owner`. + Transfer { + ata_program_id: ProgramId, + amount: u128, + }, + + /// Burn tokens FROM owner's ATA. + /// Uses PDA seeds to authorize the ATA in the chained Token::Burn call. + /// + /// Required accounts (3): + /// - Owner account (authorized) + /// - Owner's ATA (the holding to burn from) + /// - Token definition account + /// + /// `token_program_id` is derived from `holder_ata.account.program_owner`. + Burn { + ata_program_id: ProgramId, + amount: u128, + }, +} + +pub fn compute_ata_seed(owner_id: AccountId, definition_id: AccountId) -> PdaSeed { + use risc0_zkvm::sha::{Impl, Sha256}; + let mut bytes = [0u8; 64]; + bytes[0..32].copy_from_slice(&owner_id.to_bytes()); + bytes[32..64].copy_from_slice(&definition_id.to_bytes()); + PdaSeed::new( + Impl::hash_bytes(&bytes) + .as_bytes() + .try_into() + .expect("Hash output must be exactly 32 bytes long"), + ) +} + +pub fn get_associated_token_account_id(ata_program_id: &ProgramId, seed: &PdaSeed) -> AccountId { + AccountId::from((ata_program_id, seed)) +} + +/// Verify the ATA's address matches `(ata_program_id, owner, definition)` and return +/// the [`PdaSeed`] for use in chained calls. +pub fn verify_ata_and_get_seed( + ata_account: &AccountWithMetadata, + owner: &AccountWithMetadata, + definition_id: AccountId, + ata_program_id: ProgramId, +) -> PdaSeed { + let seed = compute_ata_seed(owner.account_id, definition_id); + let expected_id = get_associated_token_account_id(&ata_program_id, &seed); + assert_eq!( + ata_account.account_id, expected_id, + "ATA account ID does not match expected derivation" + ); + seed +} diff --git a/programs/associated_token_account/src/burn.rs b/programs/associated_token_account/src/burn.rs new file mode 100644 index 00000000..4940fdeb --- /dev/null +++ b/programs/associated_token_account/src/burn.rs @@ -0,0 +1,39 @@ +use nssa_core::{ + account::AccountWithMetadata, + program::{AccountPostState, ChainedCall, ProgramId}, +}; +use token_core::TokenHolding; + +pub fn burn_from_associated_token_account( + owner: AccountWithMetadata, + holder_ata: AccountWithMetadata, + token_definition: AccountWithMetadata, + ata_program_id: ProgramId, + amount: u128, +) -> (Vec, Vec) { + let token_program_id = holder_ata.account.program_owner; + assert!(owner.is_authorized, "Owner authorization is missing"); + let definition_id = TokenHolding::try_from(&holder_ata.account.data) + .expect("Holder ATA must hold a valid token") + .definition_id(); + let seed = + ata_core::verify_ata_and_get_seed(&holder_ata, &owner, definition_id, ata_program_id); + + let post_states = vec![ + AccountPostState::new(owner.account.clone()), + AccountPostState::new(holder_ata.account.clone()), + AccountPostState::new(token_definition.account.clone()), + ]; + let mut holder_ata_auth = holder_ata.clone(); + holder_ata_auth.is_authorized = true; + + let chained_call = ChainedCall::new( + token_program_id, + vec![token_definition.clone(), holder_ata_auth], + &token_core::Instruction::Burn { + amount_to_burn: amount, + }, + ) + .with_pda_seeds(vec![seed]); + (post_states, vec![chained_call]) +} diff --git a/programs/associated_token_account/src/create.rs b/programs/associated_token_account/src/create.rs new file mode 100644 index 00000000..86109952 --- /dev/null +++ b/programs/associated_token_account/src/create.rs @@ -0,0 +1,44 @@ +use nssa_core::{ + account::{Account, AccountWithMetadata}, + program::{AccountPostState, ChainedCall, ProgramId}, +}; + +pub fn create_associated_token_account( + owner: AccountWithMetadata, + token_definition: AccountWithMetadata, + ata_account: AccountWithMetadata, + ata_program_id: ProgramId, +) -> (Vec, Vec) { + // No authorization check needed: create is idempotent, so anyone can call it safely. + let token_program_id = token_definition.account.program_owner; + ata_core::verify_ata_and_get_seed( + &ata_account, + &owner, + token_definition.account_id, + ata_program_id, + ); + + // Idempotent: already initialized → no-op + if ata_account.account != Account::default() { + return ( + vec![ + AccountPostState::new_claimed_if_default(owner.account.clone()), + AccountPostState::new(token_definition.account.clone()), + AccountPostState::new(ata_account.account.clone()), + ], + vec![], + ); + } + + let post_states = vec![ + AccountPostState::new_claimed_if_default(owner.account.clone()), + AccountPostState::new(token_definition.account.clone()), + AccountPostState::new(ata_account.account.clone()), + ]; + let chained_call = ChainedCall::new( + token_program_id, + vec![token_definition.clone(), ata_account.clone()], + &token_core::Instruction::InitializeAccount, + ); + (post_states, vec![chained_call]) +} diff --git a/programs/associated_token_account/src/lib.rs b/programs/associated_token_account/src/lib.rs new file mode 100644 index 00000000..13740f0a --- /dev/null +++ b/programs/associated_token_account/src/lib.rs @@ -0,0 +1,10 @@ +//! The Associated Token Account Program implementation. + +pub use ata_core as core; + +pub mod burn; +pub mod create; +pub mod transfer; + +#[cfg(test)] +mod tests; diff --git a/programs/associated_token_account/src/tests.rs b/programs/associated_token_account/src/tests.rs new file mode 100644 index 00000000..9835bf37 --- /dev/null +++ b/programs/associated_token_account/src/tests.rs @@ -0,0 +1,153 @@ +#![cfg(test)] + +use ata_core::{compute_ata_seed, get_associated_token_account_id}; +use nssa_core::account::{Account, AccountId, AccountWithMetadata, Data}; +use token_core::{TokenDefinition, TokenHolding}; + +const ATA_PROGRAM_ID: nssa_core::program::ProgramId = [1u32; 8]; +const TOKEN_PROGRAM_ID: nssa_core::program::ProgramId = [2u32; 8]; + +fn owner_id() -> AccountId { + AccountId::new([0x01u8; 32]) +} + +fn definition_id() -> AccountId { + AccountId::new([0x02u8; 32]) +} + +fn ata_id() -> AccountId { + get_associated_token_account_id( + &ATA_PROGRAM_ID, + &compute_ata_seed(owner_id(), definition_id()), + ) +} + +fn owner_account() -> AccountWithMetadata { + AccountWithMetadata { + account: Account::default(), + is_authorized: true, + account_id: owner_id(), + } +} + +fn definition_account() -> AccountWithMetadata { + AccountWithMetadata { + account: Account { + program_owner: TOKEN_PROGRAM_ID, + balance: 0, + data: Data::from(&TokenDefinition::Fungible { + name: "TEST".to_string(), + total_supply: 1000, + metadata_id: None, + }), + nonce: nssa_core::account::Nonce(0), + }, + is_authorized: false, + account_id: definition_id(), + } +} + +fn uninitialized_ata_account() -> AccountWithMetadata { + AccountWithMetadata { + account: Account::default(), + is_authorized: false, + account_id: ata_id(), + } +} + +fn initialized_ata_account() -> AccountWithMetadata { + AccountWithMetadata { + account: Account { + program_owner: TOKEN_PROGRAM_ID, + balance: 0, + data: Data::from(&TokenHolding::Fungible { + definition_id: definition_id(), + balance: 100, + }), + nonce: nssa_core::account::Nonce(0), + }, + is_authorized: false, + account_id: ata_id(), + } +} + +#[test] +fn create_emits_chained_call_for_uninitialized_ata() { + let (post_states, chained_calls) = crate::create::create_associated_token_account( + owner_account(), + definition_account(), + uninitialized_ata_account(), + ATA_PROGRAM_ID, + ); + + assert_eq!(post_states.len(), 3); + assert_eq!(chained_calls.len(), 1); + assert_eq!(chained_calls[0].program_id, TOKEN_PROGRAM_ID); +} + +#[test] +fn create_is_idempotent_for_initialized_ata() { + let (post_states, chained_calls) = crate::create::create_associated_token_account( + owner_account(), + definition_account(), + initialized_ata_account(), + ATA_PROGRAM_ID, + ); + + assert_eq!(post_states.len(), 3); + assert!( + chained_calls.is_empty(), + "Should emit no chained call for already-initialized ATA" + ); +} + +#[test] +#[should_panic(expected = "ATA account ID does not match expected derivation")] +fn create_panics_on_wrong_ata_address() { + let wrong_ata = AccountWithMetadata { + account: Account::default(), + is_authorized: false, + account_id: AccountId::new([0xFFu8; 32]), + }; + + crate::create::create_associated_token_account( + owner_account(), + definition_account(), + wrong_ata, + ATA_PROGRAM_ID, + ); +} + +#[test] +fn get_associated_token_account_id_is_deterministic() { + let seed = compute_ata_seed(owner_id(), definition_id()); + let id1 = get_associated_token_account_id(&ATA_PROGRAM_ID, &seed); + let id2 = get_associated_token_account_id(&ATA_PROGRAM_ID, &seed); + assert_eq!(id1, id2); +} + +#[test] +fn get_associated_token_account_id_differs_by_owner() { + let other_owner = AccountId::new([0x99u8; 32]); + let id1 = get_associated_token_account_id( + &ATA_PROGRAM_ID, + &compute_ata_seed(owner_id(), definition_id()), + ); + let id2 = get_associated_token_account_id( + &ATA_PROGRAM_ID, + &compute_ata_seed(other_owner, definition_id()), + ); + assert_ne!(id1, id2); +} + +#[test] +fn get_associated_token_account_id_differs_by_definition() { + let other_def = AccountId::new([0x99u8; 32]); + let id1 = get_associated_token_account_id( + &ATA_PROGRAM_ID, + &compute_ata_seed(owner_id(), definition_id()), + ); + let id2 = + get_associated_token_account_id(&ATA_PROGRAM_ID, &compute_ata_seed(owner_id(), other_def)); + assert_ne!(id1, id2); +} diff --git a/programs/associated_token_account/src/transfer.rs b/programs/associated_token_account/src/transfer.rs new file mode 100644 index 00000000..89d70135 --- /dev/null +++ b/programs/associated_token_account/src/transfer.rs @@ -0,0 +1,39 @@ +use nssa_core::{ + account::AccountWithMetadata, + program::{AccountPostState, ChainedCall, ProgramId}, +}; +use token_core::TokenHolding; + +pub fn transfer_from_associated_token_account( + owner: AccountWithMetadata, + sender_ata: AccountWithMetadata, + recipient: AccountWithMetadata, + ata_program_id: ProgramId, + amount: u128, +) -> (Vec, Vec) { + let token_program_id = sender_ata.account.program_owner; + assert!(owner.is_authorized, "Owner authorization is missing"); + let definition_id = TokenHolding::try_from(&sender_ata.account.data) + .expect("Sender ATA must hold a valid token") + .definition_id(); + let seed = + ata_core::verify_ata_and_get_seed(&sender_ata, &owner, definition_id, ata_program_id); + + let post_states = vec![ + AccountPostState::new(owner.account.clone()), + AccountPostState::new(sender_ata.account.clone()), + AccountPostState::new(recipient.account.clone()), + ]; + let mut sender_ata_auth = sender_ata.clone(); + sender_ata_auth.is_authorized = true; + + let chained_call = ChainedCall::new( + token_program_id, + vec![sender_ata_auth, recipient.clone()], + &token_core::Instruction::Transfer { + amount_to_transfer: amount, + }, + ) + .with_pda_seeds(vec![seed]); + (post_states, vec![chained_call]) +} diff --git a/sequencer/core/Cargo.toml b/sequencer/core/Cargo.toml index 334f093c..e1ff0895 100644 --- a/sequencer/core/Cargo.toml +++ b/sequencer/core/Cargo.toml @@ -14,8 +14,8 @@ common.workspace = true storage.workspace = true mempool.workspace = true bedrock_client.workspace = true +testnet_initial_state.workspace = true -base58.workspace = true anyhow.workspace = true serde.workspace = true serde_json.workspace = true diff --git a/sequencer/core/src/config.rs b/sequencer/core/src/config.rs index 9dd10680..2fb101aa 100644 --- a/sequencer/core/src/config.rs +++ b/sequencer/core/src/config.rs @@ -8,13 +8,11 @@ use std::{ use anyhow::Result; use bedrock_client::BackoffConfig; use bytesize::ByteSize; -use common::{ - block::{AccountInitialData, CommitmentsInitialData}, - config::BasicAuth, -}; +use common::config::BasicAuth; use humantime_serde; use logos_blockchain_core::mantle::ops::channel::ChannelId; use serde::{Deserialize, Serialize}; +use testnet_initial_state::{PrivateAccountPublicInitialData, PublicAccountPublicInitialData}; use url::Url; // TODO: Provide default values @@ -39,16 +37,16 @@ pub struct SequencerConfig { /// Interval in which pending blocks are retried. #[serde(with = "humantime_serde")] pub retry_pending_blocks_timeout: Duration, - /// List of initial accounts data. - pub initial_accounts: Vec, - /// List of initial commitments. - pub initial_commitments: Vec, /// Sequencer own signing key. pub signing_key: [u8; 32], /// Bedrock configuration options. pub bedrock_config: BedrockConfig, /// Indexer RPC URL. pub indexer_rpc_url: Url, + #[serde(skip_serializing_if = "Option::is_none")] + pub initial_public_accounts: Option>, + #[serde(skip_serializing_if = "Option::is_none")] + pub initial_private_accounts: Option>, } #[derive(Clone, Serialize, Deserialize)] diff --git a/sequencer/core/src/lib.rs b/sequencer/core/src/lib.rs index 272ec5ec..252a059e 100644 --- a/sequencer/core/src/lib.rs +++ b/sequencer/core/src/lib.rs @@ -15,7 +15,9 @@ use logos_blockchain_key_management_system_service::keys::{ED25519_SECRET_KEY_SI use mempool::{MemPool, MemPoolHandle}; #[cfg(feature = "mock")] pub use mock::SequencerCoreWithMockClients; +use nssa::V03State; pub use storage::error::DbError; +use testnet_initial_state::initial_state; use crate::{ block_settlement_client::{BlockSettlementClient, BlockSettlementClientTrait, MsgId}, @@ -98,30 +100,48 @@ impl SequencerCore = config - .initial_commitments - .iter() - .map(|init_comm_data| { - let npk = &init_comm_data.npk; - let mut acc = init_comm_data.account.clone(); + let initial_commitments: Option> = config + .initial_private_accounts + .clone() + .map(|initial_commitments| { + initial_commitments + .iter() + .map(|init_comm_data| { + let npk = &init_comm_data.npk; - acc.program_owner = - nssa::program::Program::authenticated_transfer_program().id(); + let mut acc = init_comm_data.account.clone(); - nssa_core::Commitment::new(npk, &acc) - }) - .collect(); + acc.program_owner = + nssa::program::Program::authenticated_transfer_program().id(); - let init_accs: Vec<(nssa::AccountId, u128)> = config - .initial_accounts - .iter() - .map(|acc_data| (acc_data.account_id, acc_data.balance)) - .collect(); + nssa_core::Commitment::new(npk, &acc) + }) + .collect() + }); - nssa::V03State::new_with_genesis_accounts(&init_accs, &initial_commitments) + let init_accs: Option> = config + .initial_public_accounts + .clone() + .map(|initial_accounts| { + initial_accounts + .iter() + .map(|acc_data| (acc_data.account_id, acc_data.balance)) + .collect() + }); + + // If initial commitments or accounts are present in config, need to construct state + // from them + if initial_commitments.is_some() || init_accs.is_some() { + V03State::new_with_genesis_accounts( + &init_accs.unwrap_or_default(), + &initial_commitments.unwrap_or_default(), + ) + } else { + initial_state() + } }; #[cfg(feature = "testnet")] @@ -375,26 +395,20 @@ fn load_or_create_signing_key(path: &Path) -> Result { mod tests { #![expect(clippy::shadow_unrelated, reason = "We don't care about it in tests")] - use std::{pin::pin, str::FromStr as _, time::Duration}; + use std::{pin::pin, time::Duration}; - use base58::ToBase58 as _; use bedrock_client::BackoffConfig; - use common::{ - block::AccountInitialData, test_utils::sequencer_sign_key_for_testing, - transaction::NSSATransaction, - }; + use common::{test_utils::sequencer_sign_key_for_testing, transaction::NSSATransaction}; use logos_blockchain_core::mantle::ops::channel::ChannelId; use mempool::MemPoolHandle; - use nssa::{AccountId, PrivateKey}; + use testnet_initial_state::{initial_accounts, initial_pub_accounts_private_keys}; use crate::{ config::{BedrockConfig, SequencerConfig}, mock::SequencerCoreWithMockClients, }; - fn setup_sequencer_config_variable_initial_accounts( - initial_accounts: Vec, - ) -> SequencerConfig { + fn setup_sequencer_config() -> SequencerConfig { let tempdir = tempfile::tempdir().unwrap(); let home = tempdir.path().to_path_buf(); @@ -406,8 +420,6 @@ mod tests { max_block_size: bytesize::ByteSize::mib(1), mempool_max_size: 10000, block_create_timeout: Duration::from_secs(1), - initial_accounts, - initial_commitments: vec![], signing_key: *sequencer_sign_key_for_testing().value(), bedrock_config: BedrockConfig { backoff: BackoffConfig { @@ -420,41 +432,17 @@ mod tests { }, retry_pending_blocks_timeout: Duration::from_mins(4), indexer_rpc_url: "ws://localhost:8779".parse().unwrap(), + initial_public_accounts: None, + initial_private_accounts: None, } } - fn setup_sequencer_config() -> SequencerConfig { - let acc1_account_id: Vec = vec![ - 148, 179, 206, 253, 199, 51, 82, 86, 232, 2, 152, 122, 80, 243, 54, 207, 237, 112, 83, - 153, 44, 59, 204, 49, 128, 84, 160, 227, 216, 149, 97, 102, - ]; - - let acc2_account_id: Vec = vec![ - 30, 145, 107, 3, 207, 73, 192, 230, 160, 63, 238, 207, 18, 69, 54, 216, 103, 244, 92, - 94, 124, 248, 42, 16, 141, 19, 119, 18, 14, 226, 140, 204, - ]; - - let initial_acc1 = AccountInitialData { - account_id: AccountId::from_str(&acc1_account_id.to_base58()).unwrap(), - balance: 10000, - }; - - let initial_acc2 = AccountInitialData { - account_id: AccountId::from_str(&acc2_account_id.to_base58()).unwrap(), - balance: 20000, - }; - - let initial_accounts = vec![initial_acc1, initial_acc2]; - - setup_sequencer_config_variable_initial_accounts(initial_accounts) - } - fn create_signing_key_for_account1() -> nssa::PrivateKey { - nssa::PrivateKey::try_new([1; 32]).unwrap() + initial_pub_accounts_private_keys()[0].pub_sign_key.clone() } fn create_signing_key_for_account2() -> nssa::PrivateKey { - nssa::PrivateKey::try_new([2; 32]).unwrap() + initial_pub_accounts_private_keys()[1].pub_sign_key.clone() } async fn common_setup() -> (SequencerCoreWithMockClients, MemPoolHandle) { @@ -487,8 +475,8 @@ mod tests { assert_eq!(sequencer.chain_height, config.genesis_id); assert_eq!(sequencer.sequencer_config.max_num_tx_in_block, 10); - let acc1_account_id = config.initial_accounts[0].account_id; - let acc2_account_id = config.initial_accounts[1].account_id; + let acc1_account_id = initial_accounts()[0].account_id; + let acc2_account_id = initial_accounts()[1].account_id; let balance_acc_1 = sequencer.state.get_account_by_id(acc1_account_id).balance; let balance_acc_2 = sequencer.state.get_account_by_id(acc2_account_id).balance; @@ -497,47 +485,6 @@ mod tests { assert_eq!(20000, balance_acc_2); } - #[tokio::test] - async fn start_different_intial_accounts_balances() { - let acc1_account_id: Vec = vec![ - 27, 132, 197, 86, 123, 18, 100, 64, 153, 93, 62, 213, 170, 186, 5, 101, 215, 30, 24, - 52, 96, 72, 25, 255, 156, 23, 245, 233, 213, 221, 7, 143, - ]; - - let acc2_account_id: Vec = vec![ - 77, 75, 108, 209, 54, 16, 50, 202, 155, 210, 174, 185, 217, 0, 170, 77, 69, 217, 234, - 216, 10, 201, 66, 51, 116, 196, 81, 167, 37, 77, 7, 102, - ]; - - let initial_acc1 = AccountInitialData { - account_id: AccountId::from_str(&acc1_account_id.to_base58()).unwrap(), - balance: 10000, - }; - - let initial_acc2 = AccountInitialData { - account_id: AccountId::from_str(&acc2_account_id.to_base58()).unwrap(), - balance: 20000, - }; - - let initial_accounts = vec![initial_acc1, initial_acc2]; - - let config = setup_sequencer_config_variable_initial_accounts(initial_accounts); - let (sequencer, _mempool_handle) = - SequencerCoreWithMockClients::start_from_config(config.clone()).await; - - let acc1_account_id = config.initial_accounts[0].account_id; - let acc2_account_id = config.initial_accounts[1].account_id; - - assert_eq!( - 10000, - sequencer.state.get_account_by_id(acc1_account_id).balance - ); - assert_eq!( - 20000, - sequencer.state.get_account_by_id(acc2_account_id).balance - ); - } - #[test] fn transaction_pre_check_pass() { let tx = common::test_utils::produce_dummy_empty_transaction(); @@ -548,10 +495,10 @@ mod tests { #[tokio::test] async fn transaction_pre_check_native_transfer_valid() { - let (sequencer, _mempool_handle) = common_setup().await; + let (_sequencer, _mempool_handle) = common_setup().await; - let acc1 = sequencer.sequencer_config.initial_accounts[0].account_id; - let acc2 = sequencer.sequencer_config.initial_accounts[1].account_id; + let acc1 = initial_accounts()[0].account_id; + let acc2 = initial_accounts()[1].account_id; let sign_key1 = create_signing_key_for_account1(); @@ -567,8 +514,8 @@ mod tests { async fn transaction_pre_check_native_transfer_other_signature() { let (mut sequencer, _mempool_handle) = common_setup().await; - let acc1 = sequencer.sequencer_config.initial_accounts[0].account_id; - let acc2 = sequencer.sequencer_config.initial_accounts[1].account_id; + let acc1 = initial_accounts()[0].account_id; + let acc2 = initial_accounts()[1].account_id; let sign_key2 = create_signing_key_for_account2(); @@ -592,8 +539,8 @@ mod tests { async fn transaction_pre_check_native_transfer_sent_too_much() { let (mut sequencer, _mempool_handle) = common_setup().await; - let acc1 = sequencer.sequencer_config.initial_accounts[0].account_id; - let acc2 = sequencer.sequencer_config.initial_accounts[1].account_id; + let acc1 = initial_accounts()[0].account_id; + let acc2 = initial_accounts()[1].account_id; let sign_key1 = create_signing_key_for_account1(); @@ -619,8 +566,8 @@ mod tests { async fn transaction_execute_native_transfer() { let (mut sequencer, _mempool_handle) = common_setup().await; - let acc1 = sequencer.sequencer_config.initial_accounts[0].account_id; - let acc2 = sequencer.sequencer_config.initial_accounts[1].account_id; + let acc1 = initial_accounts()[0].account_id; + let acc2 = initial_accounts()[1].account_id; let sign_key1 = create_signing_key_for_account1(); @@ -681,8 +628,8 @@ mod tests { async fn replay_transactions_are_rejected_in_the_same_block() { let (mut sequencer, mempool_handle) = common_setup().await; - let acc1 = sequencer.sequencer_config.initial_accounts[0].account_id; - let acc2 = sequencer.sequencer_config.initial_accounts[1].account_id; + let acc1 = initial_accounts()[0].account_id; + let acc2 = initial_accounts()[1].account_id; let sign_key1 = create_signing_key_for_account1(); @@ -714,8 +661,8 @@ mod tests { async fn replay_transactions_are_rejected_in_different_blocks() { let (mut sequencer, mempool_handle) = common_setup().await; - let acc1 = sequencer.sequencer_config.initial_accounts[0].account_id; - let acc2 = sequencer.sequencer_config.initial_accounts[1].account_id; + let acc1 = initial_accounts()[0].account_id; + let acc2 = initial_accounts()[1].account_id; let sign_key1 = create_signing_key_for_account1(); @@ -751,8 +698,8 @@ mod tests { #[tokio::test] async fn restart_from_storage() { let config = setup_sequencer_config(); - let acc1_account_id = config.initial_accounts[0].account_id; - let acc2_account_id = config.initial_accounts[1].account_id; + let acc1_account_id = initial_accounts()[0].account_id; + let acc2_account_id = initial_accounts()[1].account_id; let balance_to_move = 13; // In the following code block a transaction will be processed that moves `balance_to_move` @@ -761,7 +708,7 @@ mod tests { { let (mut sequencer, mempool_handle) = SequencerCoreWithMockClients::start_from_config(config.clone()).await; - let signing_key = PrivateKey::try_new([1; 32]).unwrap(); + let signing_key = create_signing_key_for_account1(); let tx = common::test_utils::create_transaction_native_token_transfer( acc1_account_id, @@ -793,11 +740,11 @@ mod tests { // Balances should be consistent with the stored block assert_eq!( balance_acc_1, - config.initial_accounts[0].balance - balance_to_move + initial_accounts()[0].balance - balance_to_move ); assert_eq!( balance_acc_2, - config.initial_accounts[1].balance + balance_to_move + initial_accounts()[1].balance + balance_to_move ); } @@ -844,15 +791,15 @@ mod tests { #[tokio::test] async fn produce_block_with_correct_prev_meta_after_restart() { let config = setup_sequencer_config(); - let acc1_account_id = config.initial_accounts[0].account_id; - let acc2_account_id = config.initial_accounts[1].account_id; + let acc1_account_id = initial_accounts()[0].account_id; + let acc2_account_id = initial_accounts()[1].account_id; // Step 1: Create initial database with some block metadata let expected_prev_meta = { let (mut sequencer, mempool_handle) = SequencerCoreWithMockClients::start_from_config(config.clone()).await; - let signing_key = PrivateKey::try_new([1; 32]).unwrap(); + let signing_key = create_signing_key_for_account1(); // Add a transaction and produce a block to set up block metadata let tx = common::test_utils::create_transaction_native_token_transfer( @@ -877,7 +824,7 @@ mod tests { SequencerCoreWithMockClients::start_from_config(config.clone()).await; // Step 3: Submit a new transaction - let signing_key = PrivateKey::try_new([1; 32]).unwrap(); + let signing_key = create_signing_key_for_account1(); let tx = common::test_utils::create_transaction_native_token_transfer( acc1_account_id, 1, // Next nonce diff --git a/sequencer/service/Dockerfile b/sequencer/service/Dockerfile index 414d4889..10641e9a 100644 --- a/sequencer/service/Dockerfile +++ b/sequencer/service/Dockerfile @@ -26,7 +26,7 @@ RUN ARCH=$(uname -m); \ else \ echo "Using manual build for $ARCH"; \ git clone --depth 1 --branch release-3.0 https://github.com/risc0/risc0.git; \ - git clone --depth 1 --branch r0.1.94.0 https://github.com/risc0/rust.git; \ + git clone --depth 1 --branch r0.1.91.0 https://github.com/risc0/rust.git; \ cd /risc0; \ cargo install --path rzup; \ rzup build --path /rust rust --verbose; \ @@ -55,7 +55,11 @@ FROM chef AS builder ARG STANDALONE COPY --from=planner /sequencer_service/recipe.json recipe.json # Build dependencies only (this layer will be cached) -RUN if [ "$STANDALONE" = "true" ]; then \ +RUN --mount=type=cache,target=/usr/local/cargo/registry/index \ + --mount=type=cache,target=/usr/local/cargo/registry/cache \ + --mount=type=cache,target=/usr/local/cargo/git \ + --mount=type=cache,target=/sequencer_service/target \ + if [ "$STANDALONE" = "true" ]; then \ cargo chef cook --bin sequencer_service --features standalone --release --recipe-path recipe.json; \ else \ cargo chef cook --bin sequencer_service --release --recipe-path recipe.json; \ @@ -64,31 +68,29 @@ RUN if [ "$STANDALONE" = "true" ]; then \ # Copy source code COPY . . -# Build the actual application -RUN if [ "$STANDALONE" = "true" ]; then \ +# Build the actual application and copy the binary out of the cache mount +RUN --mount=type=cache,target=/usr/local/cargo/registry/index \ + --mount=type=cache,target=/usr/local/cargo/registry/cache \ + --mount=type=cache,target=/usr/local/cargo/git \ + --mount=type=cache,target=/sequencer_service/target \ + if [ "$STANDALONE" = "true" ]; then \ cargo build --release --features standalone --bin sequencer_service; \ else \ cargo build --release --bin sequencer_service; \ - fi - -# Strip debug symbols to reduce binary size -RUN strip /sequencer_service/target/release/sequencer_service + fi \ + && strip /sequencer_service/target/release/sequencer_service \ + && cp /sequencer_service/target/release/sequencer_service /usr/local/bin/sequencer_service # Runtime stage - minimal image FROM debian:trixie-slim -# Install runtime dependencies -RUN apt-get update \ - && apt-get install -y gosu jq \ - && rm -rf /var/lib/apt/lists/* - # Create non-root user for security RUN useradd -m -u 1000 -s /bin/bash sequencer_user && \ - mkdir -p /sequencer_service /etc/sequencer_service && \ - chown -R sequencer_user:sequencer_user /sequencer_service /etc/sequencer_service + mkdir -p /sequencer_service /etc/sequencer_service /var/lib/sequencer_service && \ + chown -R sequencer_user:sequencer_user /sequencer_service /etc/sequencer_service /var/lib/sequencer_service # Copy binary from builder -COPY --from=builder --chown=sequencer_user:sequencer_user /sequencer_service/target/release/sequencer_service /usr/local/bin/sequencer_service +COPY --from=builder --chown=sequencer_user:sequencer_user /usr/local/bin/sequencer_service /usr/local/bin/sequencer_service # Copy r0vm binary from builder COPY --from=builder --chown=sequencer_user:sequencer_user /usr/local/bin/r0vm /usr/local/bin/r0vm @@ -96,9 +98,7 @@ COPY --from=builder --chown=sequencer_user:sequencer_user /usr/local/bin/r0vm /u # Copy logos blockchain circuits from builder COPY --from=builder --chown=sequencer_user:sequencer_user /root/.logos-blockchain-circuits /home/sequencer_user/.logos-blockchain-circuits -# Copy entrypoint script -COPY sequencer/service/docker-entrypoint.sh /docker-entrypoint.sh -RUN chmod +x /docker-entrypoint.sh +VOLUME /var/lib/sequencer_service # Expose default port EXPOSE 3040 @@ -120,9 +120,7 @@ ENV RUST_LOG=info # Set explicit location for r0vm binary ENV RISC0_SERVER_PATH=/usr/local/bin/r0vm -USER root - -ENTRYPOINT ["/docker-entrypoint.sh"] +USER sequencer_user WORKDIR /sequencer_service CMD ["sequencer_service", "/etc/sequencer_service/sequencer_config.json"] diff --git a/sequencer/service/docker-compose.yml b/sequencer/service/docker-compose.yml index 81520e7b..cede8143 100644 --- a/sequencer/service/docker-compose.yml +++ b/sequencer/service/docker-compose.yml @@ -10,5 +10,8 @@ services: volumes: # Mount configuration file - ./configs/docker/sequencer_config.json:/etc/sequencer_service/sequencer_config.json - # Mount data folder - - ./data:/var/lib/sequencer_service + # Mount data volume + - sequencer_data:/var/lib/sequencer_service + +volumes: + sequencer_data: diff --git a/sequencer/service/docker-entrypoint.sh b/sequencer/service/docker-entrypoint.sh deleted file mode 100644 index 131c83e6..00000000 --- a/sequencer/service/docker-entrypoint.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/sh - -# This is an entrypoint script for the sequencer_service Docker container, -# it's not meant to be executed outside of the container. - -set -e - -CONFIG="/etc/sequencer/service/sequencer_config.json" - -# Check config file exists -if [ ! -f "$CONFIG" ]; then - echo "Config file not found: $CONFIG" >&2 - exit 1 -fi - -# Parse home dir -HOME_DIR=$(jq -r '.home' "$CONFIG") - -if [ -z "$HOME_DIR" ] || [ "$HOME_DIR" = "null" ]; then - echo "'home' key missing in config" >&2 - exit 1 -fi - -# Give permissions to the data directory and switch to non-root user -if [ "$(id -u)" = "0" ]; then - mkdir -p "$HOME_DIR" - chown -R sequencer_user:sequencer_user "$HOME_DIR" - exec gosu sequencer_user "$@" -fi diff --git a/sequencer_rpc/src/process.rs b/sequencer_rpc/src/process.rs deleted file mode 100644 index 2fe9bed1..00000000 --- a/sequencer_rpc/src/process.rs +++ /dev/null @@ -1,786 +0,0 @@ -use std::collections::HashMap; - -use actix_web::Error as HttpError; -use base64::{Engine as _, engine::general_purpose}; -use common::{ - block::{AccountInitialData, HashableBlockData}, - rpc_primitives::{ - errors::RpcError, - message::{Message, Request}, - parser::RpcRequest as _, - requests::{ - GetAccountBalanceRequest, GetAccountBalanceResponse, GetAccountRequest, - GetAccountResponse, GetAccountsNoncesRequest, GetAccountsNoncesResponse, - GetBlockDataRequest, GetBlockDataResponse, GetBlockRangeDataRequest, - GetBlockRangeDataResponse, GetGenesisIdRequest, GetGenesisIdResponse, - GetInitialTestnetAccountsRequest, GetLastBlockRequest, GetLastBlockResponse, - GetProgramIdsRequest, GetProgramIdsResponse, GetProofForCommitmentRequest, - GetProofForCommitmentResponse, GetTransactionByHashRequest, - GetTransactionByHashResponse, HelloRequest, HelloResponse, SendTxRequest, - SendTxResponse, - }, - }, - transaction::{NSSATransaction, TransactionMalformationError}, -}; -use itertools::Itertools as _; -use log::warn; -use nssa::{self, program::Program}; -use sequencer_core::{ - block_settlement_client::BlockSettlementClientTrait, indexer_client::IndexerClientTrait, -}; -use serde_json::Value; - -use super::{JsonHandler, respond, types::err_rpc::RpcErr}; - -pub const HELLO: &str = "hello"; -pub const SEND_TX: &str = "send_tx"; -pub const GET_BLOCK: &str = "get_block"; -pub const GET_BLOCK_RANGE: &str = "get_block_range"; -pub const GET_GENESIS: &str = "get_genesis"; -pub const GET_LAST_BLOCK: &str = "get_last_block"; -pub const GET_ACCOUNT_BALANCE: &str = "get_account_balance"; -pub const GET_TRANSACTION_BY_HASH: &str = "get_transaction_by_hash"; -pub const GET_ACCOUNTS_NONCES: &str = "get_accounts_nonces"; -pub const GET_ACCOUNT: &str = "get_account"; -pub const GET_PROOF_FOR_COMMITMENT: &str = "get_proof_for_commitment"; -pub const GET_PROGRAM_IDS: &str = "get_program_ids"; - -pub const HELLO_FROM_SEQUENCER: &str = "HELLO_FROM_SEQUENCER"; - -pub const TRANSACTION_SUBMITTED: &str = "Transaction submitted"; - -pub const GET_INITIAL_TESTNET_ACCOUNTS: &str = "get_initial_testnet_accounts"; - -pub trait Process: Send + Sync + 'static { - fn process(&self, message: Message) -> impl Future> + Send; -} - -impl< - BC: BlockSettlementClientTrait + Send + Sync + 'static, - IC: IndexerClientTrait + Send + Sync + 'static, -> Process for JsonHandler -{ - async fn process(&self, message: Message) -> Result { - let id = message.id(); - if let Message::Request(request) = message { - let message_inner = self - .process_request_internal(request) - .await - .map_err(|e| e.0); - Ok(Message::response(id, message_inner)) - } else { - Ok(Message::error(RpcError::parse_error( - "JSON RPC Request format was expected".to_owned(), - ))) - } - } -} - -impl JsonHandler { - /// Example of request processing. - fn process_temp_hello(request: Request) -> Result { - let _hello_request = HelloRequest::parse(Some(request.params))?; - - let response = HelloResponse { - greeting: HELLO_FROM_SEQUENCER.to_owned(), - }; - - respond(response) - } - - async fn process_send_tx(&self, request: Request) -> Result { - // Check transaction size against block size limit - // Reserve ~200 bytes for block header overhead - const BLOCK_HEADER_OVERHEAD: usize = 200; - - let send_tx_req = SendTxRequest::parse(Some(request.params))?; - let tx = borsh::from_slice::(&send_tx_req.transaction).unwrap(); - - let tx_hash = tx.hash(); - - let tx_size = send_tx_req.transaction.len(); - - let max_tx_size = self.max_block_size.saturating_sub(BLOCK_HEADER_OVERHEAD); - - if tx_size > max_tx_size { - return Err(TransactionMalformationError::TransactionTooLarge { - size: tx_size, - max: max_tx_size, - } - .into()); - } - - let authenticated_tx = tx - .transaction_stateless_check() - .inspect_err(|err| warn!("Error at pre_check {err:#?}"))?; - - // TODO: Do we need a timeout here? It will be usable if we have too many transactions to - // process - self.mempool_handle - .push(authenticated_tx) - .await - .expect("Mempool is closed, this is a bug"); - - let response = SendTxResponse { - status: TRANSACTION_SUBMITTED.to_owned(), - tx_hash, - }; - - respond(response) - } - - async fn process_get_block_data(&self, request: Request) -> Result { - let get_block_req = GetBlockDataRequest::parse(Some(request.params))?; - - let block = { - let state = self.sequencer_state.lock().await; - - state - .block_store() - .get_block_at_id(get_block_req.block_id)? - }; - - let response = GetBlockDataResponse { - block: borsh::to_vec(&HashableBlockData::from(block)).unwrap(), - }; - - respond(response) - } - - async fn process_get_block_range_data(&self, request: Request) -> Result { - let get_block_req = GetBlockRangeDataRequest::parse(Some(request.params))?; - - let blocks = { - let state = self.sequencer_state.lock().await; - (get_block_req.start_block_id..=get_block_req.end_block_id) - .map(|block_id| state.block_store().get_block_at_id(block_id)) - .map_ok(|block| { - borsh::to_vec(&HashableBlockData::from(block)) - .expect("derived BorshSerialize should never fail") - }) - .collect::, _>>()? - }; - - let response = GetBlockRangeDataResponse { blocks }; - - respond(response) - } - - async fn process_get_genesis(&self, request: Request) -> Result { - let _get_genesis_req = GetGenesisIdRequest::parse(Some(request.params))?; - - let genesis_id = { - let state = self.sequencer_state.lock().await; - - state.block_store().genesis_id() - }; - - let response = GetGenesisIdResponse { genesis_id }; - - respond(response) - } - - async fn process_get_last_block(&self, request: Request) -> Result { - let _get_last_block_req = GetLastBlockRequest::parse(Some(request.params))?; - - let last_block = { - let state = self.sequencer_state.lock().await; - - state.chain_height() - }; - - let response = GetLastBlockResponse { last_block }; - - respond(response) - } - - /// Returns the initial accounts for testnet. - /// `ToDo`: Useful only for testnet and needs to be removed later. - async fn get_initial_testnet_accounts(&self, request: Request) -> Result { - let _get_initial_testnet_accounts_request = - GetInitialTestnetAccountsRequest::parse(Some(request.params))?; - - let initial_accounts: Vec = { - let state = self.sequencer_state.lock().await; - - state.sequencer_config().initial_accounts.clone() - }; - - respond(initial_accounts) - } - - /// Returns the balance of the account at the given `account_id`. - /// The `account_id` must be a valid hex string of the correct length. - async fn process_get_account_balance(&self, request: Request) -> Result { - let get_account_req = GetAccountBalanceRequest::parse(Some(request.params))?; - let account_id = get_account_req.account_id; - - let balance = { - let state = self.sequencer_state.lock().await; - let account = state.state().get_account_by_id(account_id); - account.balance - }; - - let response = GetAccountBalanceResponse { balance }; - - respond(response) - } - - /// Returns the nonces of the accounts at the given `account_ids`. - /// Each `account_id` must be a valid hex string of the correct length. - async fn process_get_accounts_nonces(&self, request: Request) -> Result { - let get_account_nonces_req = GetAccountsNoncesRequest::parse(Some(request.params))?; - let account_ids = get_account_nonces_req.account_ids; - - let nonces = { - let state = self.sequencer_state.lock().await; - - account_ids - .into_iter() - .map(|account_id| state.state().get_account_by_id(account_id).nonce.0) - .collect() - }; - - let response = GetAccountsNoncesResponse { nonces }; - - respond(response) - } - - /// Returns account struct for given `account_id`. - /// `AccountId` must be a valid hex string of the correct length. - async fn process_get_account(&self, request: Request) -> Result { - let get_account_nonces_req = GetAccountRequest::parse(Some(request.params))?; - - let account_id = get_account_nonces_req.account_id; - - let account = { - let state = self.sequencer_state.lock().await; - - state.state().get_account_by_id(account_id) - }; - - let response = GetAccountResponse { account }; - - respond(response) - } - - /// Returns the transaction corresponding to the given hash, if it exists in the blockchain. - /// The hash must be a valid hex string of the correct length. - async fn process_get_transaction_by_hash(&self, request: Request) -> Result { - let get_transaction_req = GetTransactionByHashRequest::parse(Some(request.params))?; - let hash = get_transaction_req.hash; - - let transaction = { - let state = self.sequencer_state.lock().await; - state - .block_store() - .get_transaction_by_hash(hash) - .map(|tx| borsh::to_vec(&tx).unwrap()) - }; - let base64_encoded = transaction.map(|tx| general_purpose::STANDARD.encode(tx)); - let response = GetTransactionByHashResponse { - transaction: base64_encoded, - }; - respond(response) - } - - /// Returns the commitment proof, corresponding to commitment. - async fn process_get_proof_by_commitment(&self, request: Request) -> Result { - let get_proof_req = GetProofForCommitmentRequest::parse(Some(request.params))?; - - let membership_proof = { - let state = self.sequencer_state.lock().await; - state - .state() - .get_proof_for_commitment(&get_proof_req.commitment) - }; - let response = GetProofForCommitmentResponse { membership_proof }; - respond(response) - } - - fn process_get_program_ids(request: Request) -> Result { - let _get_proof_req = GetProgramIdsRequest::parse(Some(request.params))?; - - let mut program_ids = HashMap::new(); - program_ids.insert( - "authenticated_transfer".to_owned(), - Program::authenticated_transfer_program().id(), - ); - program_ids.insert("token".to_owned(), Program::token().id()); - program_ids.insert("pinata".to_owned(), Program::pinata().id()); - program_ids.insert("amm".to_owned(), Program::amm().id()); - program_ids.insert( - "privacy_preserving_circuit".to_owned(), - nssa::PRIVACY_PRESERVING_CIRCUIT_ID, - ); - let response = GetProgramIdsResponse { program_ids }; - respond(response) - } - - pub async fn process_request_internal(&self, request: Request) -> Result { - match request.method.as_ref() { - HELLO => Self::process_temp_hello(request), - SEND_TX => self.process_send_tx(request).await, - GET_BLOCK => self.process_get_block_data(request).await, - GET_BLOCK_RANGE => self.process_get_block_range_data(request).await, - GET_GENESIS => self.process_get_genesis(request).await, - GET_LAST_BLOCK => self.process_get_last_block(request).await, - GET_INITIAL_TESTNET_ACCOUNTS => self.get_initial_testnet_accounts(request).await, - GET_ACCOUNT_BALANCE => self.process_get_account_balance(request).await, - GET_ACCOUNTS_NONCES => self.process_get_accounts_nonces(request).await, - GET_ACCOUNT => self.process_get_account(request).await, - GET_TRANSACTION_BY_HASH => self.process_get_transaction_by_hash(request).await, - GET_PROOF_FOR_COMMITMENT => self.process_get_proof_by_commitment(request).await, - GET_PROGRAM_IDS => Self::process_get_program_ids(request), - _ => Err(RpcErr(RpcError::method_not_found(request.method))), - } - } -} - -#[cfg(test)] -mod tests { - use std::{str::FromStr as _, sync::Arc, time::Duration}; - - use base58::ToBase58 as _; - use base64::{Engine as _, engine::general_purpose}; - use bedrock_client::BackoffConfig; - use common::{ - block::AccountInitialData, config::BasicAuth, test_utils::sequencer_sign_key_for_testing, - transaction::NSSATransaction, - }; - use nssa::AccountId; - use sequencer_core::{ - config::{BedrockConfig, SequencerConfig}, - mock::{MockBlockSettlementClient, MockIndexerClient, SequencerCoreWithMockClients}, - }; - use serde_json::Value; - use tempfile::tempdir; - use tokio::sync::Mutex; - - use crate::rpc_handler; - - type JsonHandlerWithMockClients = - crate::JsonHandler; - - fn sequencer_config_for_tests() -> SequencerConfig { - let tempdir = tempdir().unwrap(); - let home = tempdir.path().to_path_buf(); - let acc1_id: Vec = vec![ - 148, 179, 206, 253, 199, 51, 82, 86, 232, 2, 152, 122, 80, 243, 54, 207, 237, 112, 83, - 153, 44, 59, 204, 49, 128, 84, 160, 227, 216, 149, 97, 102, - ]; - - let acc2_id: Vec = vec![ - 30, 145, 107, 3, 207, 73, 192, 230, 160, 63, 238, 207, 18, 69, 54, 216, 103, 244, 92, - 94, 124, 248, 42, 16, 141, 19, 119, 18, 14, 226, 140, 204, - ]; - - let initial_acc1 = AccountInitialData { - account_id: AccountId::from_str(&acc1_id.to_base58()).unwrap(), - balance: 10000, - }; - - let initial_acc2 = AccountInitialData { - account_id: AccountId::from_str(&acc2_id.to_base58()).unwrap(), - balance: 20000, - }; - - let initial_accounts = vec![initial_acc1, initial_acc2]; - - SequencerConfig { - home, - override_rust_log: Some("info".to_owned()), - genesis_id: 1, - is_genesis_random: false, - max_num_tx_in_block: 10, - max_block_size: bytesize::ByteSize::mib(1), - mempool_max_size: 1000, - block_create_timeout: Duration::from_secs(1), - port: 8080, - initial_accounts, - initial_commitments: vec![], - signing_key: *sequencer_sign_key_for_testing().value(), - retry_pending_blocks_timeout: Duration::from_mins(4), - bedrock_config: BedrockConfig { - backoff: BackoffConfig { - start_delay: Duration::from_millis(100), - max_retries: 5, - }, - channel_id: [42; 32].into(), - node_url: "http://localhost:8080".parse().unwrap(), - auth: Some(BasicAuth { - username: "user".to_owned(), - password: None, - }), - }, - indexer_rpc_url: "ws://localhost:8779".parse().unwrap(), - } - } - - async fn components_for_tests() -> ( - JsonHandlerWithMockClients, - Vec, - NSSATransaction, - ) { - let config = sequencer_config_for_tests(); - - let (mut sequencer_core, mempool_handle) = - SequencerCoreWithMockClients::start_from_config(config).await; - let initial_accounts = sequencer_core.sequencer_config().initial_accounts.clone(); - - let signing_key = nssa::PrivateKey::try_new([1; 32]).unwrap(); - let balance_to_move = 10; - let tx = common::test_utils::create_transaction_native_token_transfer( - AccountId::from_str( - &[ - 148, 179, 206, 253, 199, 51, 82, 86, 232, 2, 152, 122, 80, 243, 54, 207, 237, - 112, 83, 153, 44, 59, 204, 49, 128, 84, 160, 227, 216, 149, 97, 102, - ] - .to_base58(), - ) - .unwrap(), - 0, - AccountId::from_str(&[2; 32].to_base58()).unwrap(), - balance_to_move, - &signing_key, - ); - - mempool_handle - .push(tx.clone()) - .await - .expect("Mempool is closed, this is a bug"); - - sequencer_core - .produce_new_block_with_mempool_transactions() - .unwrap(); - - let max_block_size = - usize::try_from(sequencer_core.sequencer_config().max_block_size.as_u64()) - .expect("`max_block_size` is expected to fit in usize"); - let sequencer_core = Arc::new(Mutex::new(sequencer_core)); - - ( - JsonHandlerWithMockClients { - sequencer_state: sequencer_core, - mempool_handle, - max_block_size, - }, - initial_accounts, - tx, - ) - } - - async fn call_rpc_handler_with_json( - handler: JsonHandlerWithMockClients, - request_json: Value, - ) -> Value { - use actix_web::{App, test, web}; - - let app = test::init_service(App::new().app_data(web::Data::new(handler)).route( - "/", - web::post().to(rpc_handler::), - )) - .await; - - let req = test::TestRequest::post() - .uri("/") - .set_json(request_json) - .to_request(); - - let resp = test::call_service(&app, req).await; - let body = test::read_body(resp).await; - - serde_json::from_slice(&body).unwrap() - } - - #[actix_web::test] - async fn get_account_balance_for_non_existent_account() { - let (json_handler, _, _) = components_for_tests().await; - let request = serde_json::json!({ - "jsonrpc": "2.0", - "method": "get_account_balance", - "params": { "account_id": "11".repeat(16) }, - "id": 1 - }); - let expected_response = serde_json::json!({ - "id": 1, - "jsonrpc": "2.0", - "result": { - "balance": 0 - } - }); - - let response = call_rpc_handler_with_json(json_handler, request).await; - - assert_eq!(response, expected_response); - } - - #[actix_web::test] - async fn get_account_balance_for_invalid_base58() { - let (json_handler, _, _) = components_for_tests().await; - let request = serde_json::json!({ - "jsonrpc": "2.0", - "method": "get_account_balance", - "params": { "account_id": "not_a_valid_base58" }, - "id": 1 - }); - let expected_response = serde_json::json!({ - "jsonrpc": "2.0", - "id": 1, - "error": { - "cause": { - "info": { - "error_message": "Failed parsing args: invalid base58: InvalidBase58Character('_', 3)" - }, - "name": "PARSE_ERROR" - }, - "code": -32700, - "data": "Failed parsing args: invalid base58: InvalidBase58Character('_', 3)", - "message": "Parse error", - "name": "REQUEST_VALIDATION_ERROR" - }, - }); - let response = call_rpc_handler_with_json(json_handler, request).await; - - assert_eq!(response, expected_response); - } - - #[actix_web::test] - async fn get_account_balance_for_invalid_length() { - let (json_handler, _, _) = components_for_tests().await; - let request = serde_json::json!({ - "jsonrpc": "2.0", - "method": "get_account_balance", - "params": { "account_id": "cafecafe" }, - "id": 1 - }); - let expected_response = serde_json::json!({ - "jsonrpc": "2.0", - "id": 1, - "error": { - "cause": { - "info": { - "error_message": "Failed parsing args: invalid length: expected 32 bytes, got 6" - }, - "name": "PARSE_ERROR" - }, - "code": -32700, - "data": "Failed parsing args: invalid length: expected 32 bytes, got 6", - "message": "Parse error", - "name": "REQUEST_VALIDATION_ERROR" - }, - }); - let response = call_rpc_handler_with_json(json_handler, request).await; - - assert_eq!(response, expected_response); - } - - #[actix_web::test] - async fn get_account_balance_for_existing_account() { - let (json_handler, initial_accounts, _) = components_for_tests().await; - - let acc1_id = initial_accounts[0].account_id; - - let request = serde_json::json!({ - "jsonrpc": "2.0", - "method": "get_account_balance", - "params": { "account_id": acc1_id }, - "id": 1 - }); - let expected_response = serde_json::json!({ - "id": 1, - "jsonrpc": "2.0", - "result": { - "balance": 10000 - 10 - } - }); - - let response = call_rpc_handler_with_json(json_handler, request).await; - - assert_eq!(response, expected_response); - } - - #[actix_web::test] - async fn get_accounts_nonces_for_non_existent_account() { - let (json_handler, _, _) = components_for_tests().await; - let request = serde_json::json!({ - "jsonrpc": "2.0", - "method": "get_accounts_nonces", - "params": { "account_ids": ["11".repeat(16)] }, - "id": 1 - }); - let expected_response = serde_json::json!({ - "id": 1, - "jsonrpc": "2.0", - "result": { - "nonces": [ 0 ] - } - }); - - let response = call_rpc_handler_with_json(json_handler, request).await; - - assert_eq!(response, expected_response); - } - - #[actix_web::test] - async fn get_accounts_nonces_for_existent_account() { - let (json_handler, initial_accounts, _) = components_for_tests().await; - - let acc1_id = initial_accounts[0].account_id; - let acc2_id = initial_accounts[1].account_id; - - let request = serde_json::json!({ - "jsonrpc": "2.0", - "method": "get_accounts_nonces", - "params": { "account_ids": [acc1_id, acc2_id] }, - "id": 1 - }); - let expected_response = serde_json::json!({ - "id": 1, - "jsonrpc": "2.0", - "result": { - "nonces": [ 1, 0 ] - } - }); - - let response = call_rpc_handler_with_json(json_handler, request).await; - - assert_eq!(response, expected_response); - } - - #[actix_web::test] - async fn get_account_data_for_non_existent_account() { - let (json_handler, _, _) = components_for_tests().await; - let request = serde_json::json!({ - "jsonrpc": "2.0", - "method": "get_account", - "params": { "account_id": "11".repeat(16) }, - "id": 1 - }); - let expected_response = serde_json::json!({ - "id": 1, - "jsonrpc": "2.0", - "result": { - "account": { - "balance": 0, - "nonce": 0, - "program_owner": [ 0, 0, 0, 0, 0, 0, 0, 0], - "data": [], - } - } - }); - - let response = call_rpc_handler_with_json(json_handler, request).await; - - assert_eq!(response, expected_response); - } - - #[actix_web::test] - async fn get_transaction_by_hash_for_non_existent_hash() { - let (json_handler, _, _) = components_for_tests().await; - let request = serde_json::json!({ - "jsonrpc": "2.0", - "method": "get_transaction_by_hash", - "params": { "hash": "cafe".repeat(16) }, - "id": 1 - }); - let expected_response = serde_json::json!({ - "id": 1, - "jsonrpc": "2.0", - "result": { - "transaction": null - } - }); - - let response = call_rpc_handler_with_json(json_handler, request).await; - - assert_eq!(response, expected_response); - } - - #[actix_web::test] - async fn get_transaction_by_hash_for_invalid_hex() { - let (json_handler, _, _) = components_for_tests().await; - let request = serde_json::json!({ - "jsonrpc": "2.0", - "method": "get_transaction_by_hash", - "params": { "hash": "not_a_valid_hex" }, - "id": 1 - }); - let expected_response = serde_json::json!({ - "jsonrpc": "2.0", - "id": 1, - "error": { - "cause": { - "info": { - "error_message": "Failed parsing args: Odd number of digits" - }, - "name": "PARSE_ERROR" - }, - "code": -32700, - "data": "Failed parsing args: Odd number of digits", - "message": "Parse error", - "name": "REQUEST_VALIDATION_ERROR" - }, - }); - - let response = call_rpc_handler_with_json(json_handler, request).await; - - assert_eq!(response, expected_response); - } - - #[actix_web::test] - async fn get_transaction_by_hash_for_invalid_length() { - let (json_handler, _, _) = components_for_tests().await; - let request = serde_json::json!({ - "jsonrpc": "2.0", - "method": "get_transaction_by_hash", - "params": { "hash": "cafecafe" }, - "id": 1 - }); - let expected_response = serde_json::json!({ - "jsonrpc": "2.0", - "id": 1, - "error": { - "cause": { - "info": { - "error_message": "Failed parsing args: Invalid string length" - }, - "name": "PARSE_ERROR" - }, - "code": -32700, - "data": "Failed parsing args: Invalid string length", - "message": "Parse error", - "name": "REQUEST_VALIDATION_ERROR" - } - }); - - let response = call_rpc_handler_with_json(json_handler, request).await; - - assert_eq!(response, expected_response); - } - - #[actix_web::test] - async fn get_transaction_by_hash_for_existing_transaction() { - let (json_handler, _, tx) = components_for_tests().await; - let tx_hash_hex = hex::encode(tx.hash()); - let expected_base64_encoded = general_purpose::STANDARD.encode(borsh::to_vec(&tx).unwrap()); - - let request = serde_json::json!({ - "jsonrpc": "2.0", - "method": "get_transaction_by_hash", - "params": { "hash": tx_hash_hex}, - "id": 1 - }); - - let expected_response = serde_json::json!({ - "id": 1, - "jsonrpc": "2.0", - "result": { - "transaction": expected_base64_encoded, - } - }); - let response = call_rpc_handler_with_json(json_handler, request).await; - - assert_eq!(response, expected_response); - } -} diff --git a/storage/src/indexer.rs b/storage/src/indexer.rs deleted file mode 100644 index 94ab6d38..00000000 --- a/storage/src/indexer.rs +++ /dev/null @@ -1,1360 +0,0 @@ -use std::{collections::HashMap, path::Path, sync::Arc}; - -use common::{ - block::{Block, BlockId}, - transaction::NSSATransaction, -}; -use nssa::V03State; -use rocksdb::{ - BoundColumnFamily, ColumnFamilyDescriptor, DBWithThreadMode, MultiThreaded, Options, WriteBatch, -}; - -use crate::error::DbError; - -/// Maximal size of stored blocks in base. -/// -/// Used to control db size. -/// -/// Currently effectively unbounded. -pub const BUFF_SIZE_ROCKSDB: usize = usize::MAX; - -/// Size of stored blocks cache in memory. -/// -/// Keeping small to not run out of memory. -pub const CACHE_SIZE: usize = 1000; - -/// Key base for storing metainformation about id of first block in db. -pub const DB_META_FIRST_BLOCK_IN_DB_KEY: &str = "first_block_in_db"; -/// Key base for storing metainformation about id of last current block in db. -pub const DB_META_LAST_BLOCK_IN_DB_KEY: &str = "last_block_in_db"; -/// Key base for storing metainformation about id of last observed L1 lib header in db. -pub const DB_META_LAST_OBSERVED_L1_LIB_HEADER_ID_IN_DB_KEY: &str = - "last_observed_l1_lib_header_in_db"; -/// Key base for storing metainformation which describe if first block has been set. -pub const DB_META_FIRST_BLOCK_SET_KEY: &str = "first_block_set"; -/// Key base for storing metainformation about the last breakpoint. -pub const DB_META_LAST_BREAKPOINT_ID: &str = "last_breakpoint_id"; - -/// Interval between state breakpoints. -pub const BREAKPOINT_INTERVAL: u8 = 100; - -/// Name of block column family. -pub const CF_BLOCK_NAME: &str = "cf_block"; -/// Name of meta column family. -pub const CF_META_NAME: &str = "cf_meta"; -/// Name of breakpoint column family. -pub const CF_BREAKPOINT_NAME: &str = "cf_breakpoint"; -/// Name of hash to id map column family. -pub const CF_HASH_TO_ID: &str = "cf_hash_to_id"; -/// Name of tx hash to id map column family. -pub const CF_TX_TO_ID: &str = "cf_tx_to_id"; -/// Name of account meta column family. -pub const CF_ACC_META: &str = "cf_acc_meta"; -/// Name of account id to tx hash map column family. -pub const CF_ACC_TO_TX: &str = "cf_acc_to_tx"; - -pub type DbResult = Result; - -pub struct RocksDBIO { - pub db: DBWithThreadMode, -} - -impl RocksDBIO { - pub fn open_or_create( - path: &Path, - genesis_block: &Block, - initial_state: &V03State, - ) -> DbResult { - let mut cf_opts = Options::default(); - cf_opts.set_max_write_buffer_number(16); - // ToDo: Add more column families for different data - let cfb = ColumnFamilyDescriptor::new(CF_BLOCK_NAME, cf_opts.clone()); - let cfmeta = ColumnFamilyDescriptor::new(CF_META_NAME, cf_opts.clone()); - let cfbreakpoint = ColumnFamilyDescriptor::new(CF_BREAKPOINT_NAME, cf_opts.clone()); - let cfhti = ColumnFamilyDescriptor::new(CF_HASH_TO_ID, cf_opts.clone()); - let cftti = ColumnFamilyDescriptor::new(CF_TX_TO_ID, cf_opts.clone()); - let cfameta = ColumnFamilyDescriptor::new(CF_ACC_META, cf_opts.clone()); - let cfatt = ColumnFamilyDescriptor::new(CF_ACC_TO_TX, cf_opts.clone()); - - let mut db_opts = Options::default(); - db_opts.create_missing_column_families(true); - db_opts.create_if_missing(true); - let db = DBWithThreadMode::::open_cf_descriptors( - &db_opts, - path, - vec![cfb, cfmeta, cfbreakpoint, cfhti, cftti, cfameta, cfatt], - ) - .map_err(|err| DbError::RocksDbError { - error: err, - additional_info: Some("Failed to open or create DB".to_owned()), - })?; - - let dbio = Self { db }; - - let is_start_set = dbio.get_meta_is_first_block_set()?; - if !is_start_set { - let block_id = genesis_block.header.block_id; - dbio.put_meta_last_block_in_db(block_id)?; - dbio.put_meta_first_block_in_db(genesis_block)?; - dbio.put_meta_is_first_block_set()?; - - // First breakpoint setup - dbio.put_breakpoint(0, initial_state)?; - dbio.put_meta_last_breakpoint_id(0)?; - } - - Ok(dbio) - } - - pub fn destroy(path: &Path) -> DbResult<()> { - let mut cf_opts = Options::default(); - cf_opts.set_max_write_buffer_number(16); - // ToDo: Add more column families for different data - let _cfb = ColumnFamilyDescriptor::new(CF_BLOCK_NAME, cf_opts.clone()); - let _cfmeta = ColumnFamilyDescriptor::new(CF_META_NAME, cf_opts.clone()); - let _cfsnapshot = ColumnFamilyDescriptor::new(CF_BREAKPOINT_NAME, cf_opts.clone()); - let _cfhti = ColumnFamilyDescriptor::new(CF_HASH_TO_ID, cf_opts.clone()); - let _cftti = ColumnFamilyDescriptor::new(CF_TX_TO_ID, cf_opts.clone()); - let _cfameta = ColumnFamilyDescriptor::new(CF_ACC_META, cf_opts.clone()); - let _cfatt = ColumnFamilyDescriptor::new(CF_ACC_TO_TX, cf_opts.clone()); - - let mut db_opts = Options::default(); - db_opts.create_missing_column_families(true); - db_opts.create_if_missing(true); - DBWithThreadMode::::destroy(&db_opts, path) - .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None)) - } - - // Columns - - pub fn meta_column(&self) -> Arc> { - self.db.cf_handle(CF_META_NAME).unwrap() - } - - pub fn block_column(&self) -> Arc> { - self.db.cf_handle(CF_BLOCK_NAME).unwrap() - } - - pub fn breakpoint_column(&self) -> Arc> { - self.db.cf_handle(CF_BREAKPOINT_NAME).unwrap() - } - - pub fn hash_to_id_column(&self) -> Arc> { - self.db.cf_handle(CF_HASH_TO_ID).unwrap() - } - - pub fn tx_hash_to_id_column(&self) -> Arc> { - self.db.cf_handle(CF_TX_TO_ID).unwrap() - } - - pub fn account_id_to_tx_hash_column(&self) -> Arc> { - self.db.cf_handle(CF_ACC_TO_TX).unwrap() - } - - pub fn account_meta_column(&self) -> Arc> { - self.db.cf_handle(CF_ACC_META).unwrap() - } - - // Meta - - pub fn get_meta_first_block_in_db(&self) -> DbResult { - let cf_meta = self.meta_column(); - let res = self - .db - .get_cf( - &cf_meta, - borsh::to_vec(&DB_META_FIRST_BLOCK_IN_DB_KEY).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize DB_META_FIRST_BLOCK_IN_DB_KEY".to_owned()), - ) - })?, - ) - .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; - - if let Some(data) = res { - Ok(borsh::from_slice::(&data).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to deserialize first block".to_owned()), - ) - })?) - } else { - Err(DbError::db_interaction_error( - "First block not found".to_owned(), - )) - } - } - - pub fn get_meta_last_block_in_db(&self) -> DbResult { - let cf_meta = self.meta_column(); - let res = self - .db - .get_cf( - &cf_meta, - borsh::to_vec(&DB_META_LAST_BLOCK_IN_DB_KEY).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize DB_META_LAST_BLOCK_IN_DB_KEY".to_owned()), - ) - })?, - ) - .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; - - if let Some(data) = res { - Ok(borsh::from_slice::(&data).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to deserialize last block".to_owned()), - ) - })?) - } else { - Err(DbError::db_interaction_error( - "Last block not found".to_owned(), - )) - } - } - - pub fn get_meta_last_observed_l1_lib_header_in_db(&self) -> DbResult> { - let cf_meta = self.meta_column(); - let res = self - .db - .get_cf( - &cf_meta, - borsh::to_vec(&DB_META_LAST_OBSERVED_L1_LIB_HEADER_ID_IN_DB_KEY).map_err( - |err| { - DbError::borsh_cast_message( - err, - Some( - "Failed to serialize DB_META_LAST_OBSERVED_L1_LIB_HEADER_ID_IN_DB_KEY".to_owned(), - ), - ) - }, - )?, - ) - .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; - - res.map(|data| { - borsh::from_slice::<[u8; 32]>(&data).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to deserialize last l1 lib header".to_owned()), - ) - }) - }) - .transpose() - } - - pub fn get_meta_is_first_block_set(&self) -> DbResult { - let cf_meta = self.meta_column(); - let res = self - .db - .get_cf( - &cf_meta, - borsh::to_vec(&DB_META_FIRST_BLOCK_SET_KEY).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize DB_META_FIRST_BLOCK_SET_KEY".to_owned()), - ) - })?, - ) - .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; - - Ok(res.is_some()) - } - - pub fn get_meta_last_breakpoint_id(&self) -> DbResult { - let cf_meta = self.meta_column(); - let res = self - .db - .get_cf( - &cf_meta, - borsh::to_vec(&DB_META_LAST_BREAKPOINT_ID).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize DB_META_LAST_BREAKPOINT_ID".to_owned()), - ) - })?, - ) - .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; - - if let Some(data) = res { - Ok(borsh::from_slice::(&data).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to deserialize last breakpoint id".to_owned()), - ) - })?) - } else { - Err(DbError::db_interaction_error( - "Last breakpoint id not found".to_owned(), - )) - } - } - - pub fn put_meta_first_block_in_db(&self, block: &Block) -> DbResult<()> { - let cf_meta = self.meta_column(); - self.db - .put_cf( - &cf_meta, - borsh::to_vec(&DB_META_FIRST_BLOCK_IN_DB_KEY).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize DB_META_FIRST_BLOCK_IN_DB_KEY".to_owned()), - ) - })?, - borsh::to_vec(&block.header.block_id).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize first block id".to_owned()), - ) - })?, - ) - .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; - - self.put_block(block, [0; 32])?; - Ok(()) - } - - pub fn put_meta_last_block_in_db(&self, block_id: u64) -> DbResult<()> { - let cf_meta = self.meta_column(); - self.db - .put_cf( - &cf_meta, - borsh::to_vec(&DB_META_LAST_BLOCK_IN_DB_KEY).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize DB_META_LAST_BLOCK_IN_DB_KEY".to_owned()), - ) - })?, - borsh::to_vec(&block_id).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize last block id".to_owned()), - ) - })?, - ) - .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; - Ok(()) - } - - pub fn put_meta_last_observed_l1_lib_header_in_db( - &self, - l1_lib_header: [u8; 32], - ) -> DbResult<()> { - let cf_meta = self.meta_column(); - self.db - .put_cf( - &cf_meta, - borsh::to_vec(&DB_META_LAST_OBSERVED_L1_LIB_HEADER_ID_IN_DB_KEY).map_err( - |err| { - DbError::borsh_cast_message( - err, - Some( - "Failed to serialize DB_META_LAST_OBSERVED_L1_LIB_HEADER_ID_IN_DB_KEY".to_owned(), - ), - ) - }, - )?, - borsh::to_vec(&l1_lib_header).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize last l1 block header".to_owned()), - ) - })?, - ) - .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; - Ok(()) - } - - pub fn put_meta_last_breakpoint_id(&self, br_id: u64) -> DbResult<()> { - let cf_meta = self.meta_column(); - self.db - .put_cf( - &cf_meta, - borsh::to_vec(&DB_META_LAST_BREAKPOINT_ID).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize DB_META_LAST_BREAKPOINT_ID".to_owned()), - ) - })?, - borsh::to_vec(&br_id).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize last block id".to_owned()), - ) - })?, - ) - .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; - Ok(()) - } - - pub fn put_meta_is_first_block_set(&self) -> DbResult<()> { - let cf_meta = self.meta_column(); - self.db - .put_cf( - &cf_meta, - borsh::to_vec(&DB_META_FIRST_BLOCK_SET_KEY).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize DB_META_FIRST_BLOCK_SET_KEY".to_owned()), - ) - })?, - [1_u8; 1], - ) - .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; - Ok(()) - } - - // Block - - pub fn put_block(&self, block: &Block, l1_lib_header: [u8; 32]) -> DbResult<()> { - let cf_block = self.block_column(); - let cf_hti = self.hash_to_id_column(); - let cf_tti: Arc> = self.tx_hash_to_id_column(); - - // ToDo: rewrite this with write batching - - self.db - .put_cf( - &cf_block, - borsh::to_vec(&block.header.block_id).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize block id".to_owned()), - ) - })?, - borsh::to_vec(&block).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize block data".to_owned()), - ) - })?, - ) - .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; - - let last_curr_block = self.get_meta_last_block_in_db()?; - - if block.header.block_id > last_curr_block { - self.put_meta_last_block_in_db(block.header.block_id)?; - self.put_meta_last_observed_l1_lib_header_in_db(l1_lib_header)?; - } - - self.db - .put_cf( - &cf_hti, - borsh::to_vec(&block.header.hash).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize block hash".to_owned()), - ) - })?, - borsh::to_vec(&block.header.block_id).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize block id".to_owned()), - ) - })?, - ) - .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; - - let mut acc_to_tx_map: HashMap<[u8; 32], Vec<[u8; 32]>> = HashMap::new(); - - for tx in &block.body.transactions { - let tx_hash = tx.hash(); - - self.db - .put_cf( - &cf_tti, - borsh::to_vec(&tx_hash).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize tx hash".to_owned()), - ) - })?, - borsh::to_vec(&block.header.block_id).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize block id".to_owned()), - ) - })?, - ) - .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; - - let acc_ids = tx - .affected_public_account_ids() - .into_iter() - .map(nssa::AccountId::into_value) - .collect::>(); - - for acc_id in acc_ids { - acc_to_tx_map - .entry(acc_id) - .and_modify(|tx_hashes| tx_hashes.push(tx_hash.into())) - .or_insert_with(|| vec![tx_hash.into()]); - } - } - - #[expect( - clippy::iter_over_hash_type, - reason = "RocksDB will keep ordering persistent" - )] - for (acc_id, tx_hashes) in acc_to_tx_map { - self.put_account_transactions(acc_id, &tx_hashes)?; - } - - if block - .header - .block_id - .is_multiple_of(u64::from(BREAKPOINT_INTERVAL)) - { - self.put_next_breakpoint()?; - } - - Ok(()) - } - - pub fn get_block(&self, block_id: u64) -> DbResult> { - let cf_block = self.block_column(); - let res = self - .db - .get_cf( - &cf_block, - borsh::to_vec(&block_id).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize block id".to_owned()), - ) - })?, - ) - .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; - - if let Some(data) = res { - Ok(Some(borsh::from_slice::(&data).map_err(|serr| { - DbError::borsh_cast_message( - serr, - Some("Failed to deserialize block data".to_owned()), - ) - })?)) - } else { - Ok(None) - } - } - - pub fn get_block_batch(&self, before: Option, limit: u64) -> DbResult> { - let cf_block = self.block_column(); - let mut block_batch = vec![]; - - // Determine the starting block ID - let start_block_id = if let Some(before_id) = before { - before_id.saturating_sub(1) - } else { - // Get the latest block ID - self.get_meta_last_block_in_db()? - }; - - // ToDo: Multi get this - - for i in 0..limit { - let block_id = start_block_id.saturating_sub(i); - if block_id == 0 { - break; - } - - let res = self - .db - .get_cf( - &cf_block, - borsh::to_vec(&block_id).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize block id".to_owned()), - ) - })?, - ) - .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; - - let block = if let Some(data) = res { - Ok(borsh::from_slice::(&data).map_err(|serr| { - DbError::borsh_cast_message( - serr, - Some("Failed to deserialize block data".to_owned()), - ) - })?) - } else { - // Block not found, assuming that previous one was the last - break; - }?; - - block_batch.push(block); - } - - Ok(block_batch) - } - - // State - - pub fn put_breakpoint(&self, br_id: u64, breakpoint: &V03State) -> DbResult<()> { - let cf_br = self.breakpoint_column(); - - self.db - .put_cf( - &cf_br, - borsh::to_vec(&br_id).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize breakpoint id".to_owned()), - ) - })?, - borsh::to_vec(&breakpoint).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize breakpoint data".to_owned()), - ) - })?, - ) - .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None)) - } - - pub fn get_breakpoint(&self, br_id: u64) -> DbResult { - let cf_br = self.breakpoint_column(); - let res = self - .db - .get_cf( - &cf_br, - borsh::to_vec(&br_id).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize breakpoint id".to_owned()), - ) - })?, - ) - .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; - - if let Some(data) = res { - Ok(borsh::from_slice::(&data).map_err(|serr| { - DbError::borsh_cast_message( - serr, - Some("Failed to deserialize breakpoint data".to_owned()), - ) - })?) - } else { - // Note: this is not a `DbError::NotFound` case, because we expect that all searched - // breakpoints will be present in db as this is an internal method. - Err(DbError::db_interaction_error( - "Breakpoint on this id not found".to_owned(), - )) - } - } - - pub fn calculate_state_for_id(&self, block_id: u64) -> DbResult { - let last_block = self.get_meta_last_block_in_db()?; - - if block_id <= last_block { - let br_id = closest_breakpoint_id(block_id); - let mut breakpoint = self.get_breakpoint(br_id)?; - - // ToDo: update it to handle any genesis id - // right now works correctly only if genesis_id < BREAKPOINT_INTERVAL - let start = if br_id != 0 { - u64::from(BREAKPOINT_INTERVAL) - .checked_mul(br_id) - .expect("Reached maximum breakpoint id") - } else { - self.get_meta_first_block_in_db()? - }; - - for id in start..=block_id { - let block = self.get_block(id)?.ok_or_else(|| { - DbError::db_interaction_error(format!("Block with id {id} not found")) - })?; - - for transaction in block.body.transactions { - transaction - .transaction_stateless_check() - .map_err(|err| { - DbError::db_interaction_error(format!( - "transaction pre check failed with err {err:?}" - )) - })? - .execute_check_on_state( - &mut breakpoint, - block.header.block_id, - block.header.timestamp, - ) - .map_err(|err| { - DbError::db_interaction_error(format!( - "transaction execution failed with err {err:?}" - )) - })?; - } - } - - Ok(breakpoint) - } else { - Err(DbError::db_interaction_error(format!( - "Block with id {block_id} not found" - ))) - } - } - - pub fn final_state(&self) -> DbResult { - self.calculate_state_for_id(self.get_meta_last_block_in_db()?) - } - - pub fn put_next_breakpoint(&self) -> DbResult<()> { - let last_block = self.get_meta_last_block_in_db()?; - let next_breakpoint_id = self - .get_meta_last_breakpoint_id()? - .checked_add(1) - .expect("Reached maximum breakpoint id"); - let block_to_break_id = next_breakpoint_id - .checked_mul(u64::from(BREAKPOINT_INTERVAL)) - .expect("Reached maximum breakpoint id"); - - if block_to_break_id <= last_block { - let next_breakpoint = self.calculate_state_for_id(block_to_break_id)?; - - self.put_breakpoint(next_breakpoint_id, &next_breakpoint)?; - self.put_meta_last_breakpoint_id(next_breakpoint_id) - } else { - Err(DbError::db_interaction_error( - "Breakpoint not yet achieved".to_owned(), - )) - } - } - - // Mappings - - pub fn get_block_id_by_hash(&self, hash: [u8; 32]) -> DbResult> { - let cf_hti = self.hash_to_id_column(); - let res = self - .db - .get_cf( - &cf_hti, - borsh::to_vec(&hash).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize block hash".to_owned()), - ) - })?, - ) - .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; - - if let Some(data) = res { - Ok(Some(borsh::from_slice::(&data).map_err(|serr| { - DbError::borsh_cast_message(serr, Some("Failed to deserialize block id".to_owned())) - })?)) - } else { - Ok(None) - } - } - - pub fn get_block_id_by_tx_hash(&self, tx_hash: [u8; 32]) -> DbResult> { - let cf_tti = self.tx_hash_to_id_column(); - let res = self - .db - .get_cf( - &cf_tti, - borsh::to_vec(&tx_hash).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize transaction hash".to_owned()), - ) - })?, - ) - .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; - - if let Some(data) = res { - Ok(Some(borsh::from_slice::(&data).map_err(|serr| { - DbError::borsh_cast_message(serr, Some("Failed to deserialize block id".to_owned())) - })?)) - } else { - Ok(None) - } - } - - // Accounts meta - - fn update_acc_meta_batch( - &self, - acc_id: [u8; 32], - num_tx: u64, - write_batch: &mut WriteBatch, - ) -> DbResult<()> { - let cf_ameta = self.account_meta_column(); - - write_batch.put_cf( - &cf_ameta, - borsh::to_vec(&acc_id).map_err(|err| { - DbError::borsh_cast_message(err, Some("Failed to serialize account id".to_owned())) - })?, - borsh::to_vec(&num_tx).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize acc metadata".to_owned()), - ) - })?, - ); - - Ok(()) - } - - fn get_acc_meta_num_tx(&self, acc_id: [u8; 32]) -> DbResult> { - let cf_ameta = self.account_meta_column(); - let res = self.db.get_cf(&cf_ameta, acc_id).map_err(|rerr| { - DbError::rocksdb_cast_message(rerr, Some("Failed to read from acc meta cf".to_owned())) - })?; - - res.map(|data| { - borsh::from_slice::(&data).map_err(|serr| { - DbError::borsh_cast_message(serr, Some("Failed to deserialize num tx".to_owned())) - }) - }) - .transpose() - } - - // Account - - pub fn put_account_transactions( - &self, - acc_id: [u8; 32], - tx_hashes: &[[u8; 32]], - ) -> DbResult<()> { - let acc_num_tx = self.get_acc_meta_num_tx(acc_id)?.unwrap_or(0); - let cf_att = self.account_id_to_tx_hash_column(); - let mut write_batch = WriteBatch::new(); - - for (tx_id, tx_hash) in tx_hashes.iter().enumerate() { - let put_id = acc_num_tx - .checked_add( - u64::try_from(tx_id) - .expect("Transaction number for account expected to fit in u64"), - ) - .expect("Reached maximum number of transactions for account"); - - let mut prefix = borsh::to_vec(&acc_id).map_err(|berr| { - DbError::borsh_cast_message(berr, Some("Failed to serialize account id".to_owned())) - })?; - let suffix = borsh::to_vec(&put_id).map_err(|berr| { - DbError::borsh_cast_message(berr, Some("Failed to serialize tx id".to_owned())) - })?; - - prefix.extend_from_slice(&suffix); - - write_batch.put_cf( - &cf_att, - prefix, - borsh::to_vec(tx_hash).map_err(|berr| { - DbError::borsh_cast_message( - berr, - Some("Failed to serialize tx hash".to_owned()), - ) - })?, - ); - } - - self.update_acc_meta_batch( - acc_id, - acc_num_tx - .checked_add( - u64::try_from(tx_hashes.len()) - .expect("Number of transactions expected to fit in u64"), - ) - .expect("Reached maximum number of transactions for account"), - &mut write_batch, - )?; - - self.db.write(write_batch).map_err(|rerr| { - DbError::rocksdb_cast_message(rerr, Some("Failed to write batch".to_owned())) - }) - } - - fn get_acc_transaction_hashes( - &self, - acc_id: [u8; 32], - offset: u64, - limit: u64, - ) -> DbResult> { - let cf_att = self.account_id_to_tx_hash_column(); - let mut tx_batch = vec![]; - - // ToDo: Multi get this - - for tx_id in offset..(offset.saturating_add(limit)) { - let mut prefix = borsh::to_vec(&acc_id).map_err(|berr| { - DbError::borsh_cast_message(berr, Some("Failed to serialize account id".to_owned())) - })?; - let suffix = borsh::to_vec(&tx_id).map_err(|berr| { - DbError::borsh_cast_message(berr, Some("Failed to serialize tx id".to_owned())) - })?; - - prefix.extend_from_slice(&suffix); - - let res = self - .db - .get_cf(&cf_att, prefix) - .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; - - let tx_hash = if let Some(data) = res { - Ok(borsh::from_slice::<[u8; 32]>(&data).map_err(|serr| { - DbError::borsh_cast_message( - serr, - Some("Failed to deserialize tx_hash".to_owned()), - ) - })?) - } else { - // Tx hash not found, assuming that previous one was the last - break; - }?; - - tx_batch.push(tx_hash); - } - - Ok(tx_batch) - } - - pub fn get_acc_transactions( - &self, - acc_id: [u8; 32], - offset: u64, - limit: u64, - ) -> DbResult> { - let mut tx_batch = vec![]; - - for tx_hash in self.get_acc_transaction_hashes(acc_id, offset, limit)? { - let block_id = self.get_block_id_by_tx_hash(tx_hash)?.ok_or_else(|| { - DbError::db_interaction_error(format!( - "Block id not found for tx hash {tx_hash:#?}" - )) - })?; - let block = self.get_block(block_id)?.ok_or_else(|| { - DbError::db_interaction_error(format!("Block with id {block_id} not found")) - })?; - - let transaction = block - .body - .transactions - .iter() - .find(|tx| tx.hash().0 == tx_hash) - .ok_or_else(|| { - DbError::db_interaction_error(format!( - "Missing transaction in block {} with hash {:#?}", - block.header.block_id, tx_hash - )) - })?; - - tx_batch.push(transaction.clone()); - } - - Ok(tx_batch) - } -} - -fn closest_breakpoint_id(block_id: u64) -> u64 { - block_id - .saturating_sub(1) - .checked_div(u64::from(BREAKPOINT_INTERVAL)) - .expect("Breakpoint interval is not zero") -} - -#[cfg(test)] -mod tests { - #![expect(clippy::shadow_unrelated, reason = "We don't care about it in tests")] - - use nssa::AccountId; - use tempfile::tempdir; - - use super::*; - - fn genesis_block() -> Block { - common::test_utils::produce_dummy_block(1, None, vec![]) - } - - fn acc1() -> AccountId { - AccountId::new([ - 148, 179, 206, 253, 199, 51, 82, 86, 232, 2, 152, 122, 80, 243, 54, 207, 237, 112, 83, - 153, 44, 59, 204, 49, 128, 84, 160, 227, 216, 149, 97, 102, - ]) - } - - fn acc2() -> AccountId { - AccountId::new([ - 30, 145, 107, 3, 207, 73, 192, 230, 160, 63, 238, 207, 18, 69, 54, 216, 103, 244, 92, - 94, 124, 248, 42, 16, 141, 19, 119, 18, 14, 226, 140, 204, - ]) - } - - fn acc1_sign_key() -> nssa::PrivateKey { - nssa::PrivateKey::try_new([1; 32]).unwrap() - } - - fn acc2_sign_key() -> nssa::PrivateKey { - nssa::PrivateKey::try_new([2; 32]).unwrap() - } - - fn initial_state() -> V03State { - nssa::V03State::new_with_genesis_accounts(&[(acc1(), 10000), (acc2(), 20000)], &[]) - } - - fn transfer(amount: u128, nonce: u128, direction: bool) -> NSSATransaction { - let from; - let to; - let sign_key = if direction { - from = acc1(); - to = acc2(); - acc1_sign_key() - } else { - from = acc2(); - to = acc1(); - acc2_sign_key() - }; - - common::test_utils::create_transaction_native_token_transfer( - from, nonce, to, amount, &sign_key, - ) - } - - #[test] - fn start_db() { - let temp_dir = tempdir().unwrap(); - let temdir_path = temp_dir.path(); - - let dbio = - RocksDBIO::open_or_create(temdir_path, &genesis_block(), &initial_state()).unwrap(); - - let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let first_id = dbio.get_meta_first_block_in_db().unwrap(); - let is_first_set = dbio.get_meta_is_first_block_set().unwrap(); - let last_br_id = dbio.get_meta_last_breakpoint_id().unwrap(); - let last_block = dbio.get_block(1).unwrap().unwrap(); - let breakpoint = dbio.get_breakpoint(0).unwrap(); - let final_state = dbio.final_state().unwrap(); - - assert_eq!(last_id, 1); - assert_eq!(first_id, 1); - assert!(is_first_set); - assert_eq!(last_br_id, 0); - assert_eq!(last_block.header.hash, genesis_block().header.hash); - assert_eq!( - breakpoint.get_account_by_id(acc1()), - final_state.get_account_by_id(acc1()) - ); - assert_eq!( - breakpoint.get_account_by_id(acc2()), - final_state.get_account_by_id(acc2()) - ); - } - - #[test] - fn one_block_insertion() { - let temp_dir = tempdir().unwrap(); - let temdir_path = temp_dir.path(); - - let dbio = - RocksDBIO::open_or_create(temdir_path, &genesis_block(), &initial_state()).unwrap(); - - let prev_hash = genesis_block().header.hash; - let transfer_tx = transfer(1, 0, true); - let block = common::test_utils::produce_dummy_block(2, Some(prev_hash), vec![transfer_tx]); - - dbio.put_block(&block, [1; 32]).unwrap(); - - let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let first_id = dbio.get_meta_first_block_in_db().unwrap(); - let is_first_set = dbio.get_meta_is_first_block_set().unwrap(); - let last_br_id = dbio.get_meta_last_breakpoint_id().unwrap(); - let last_block = dbio.get_block(last_id).unwrap().unwrap(); - let breakpoint = dbio.get_breakpoint(0).unwrap(); - let final_state = dbio.final_state().unwrap(); - - assert_eq!(last_id, 2); - assert_eq!(first_id, 1); - assert!(is_first_set); - assert_eq!(last_br_id, 0); - assert_ne!(last_block.header.hash, genesis_block().header.hash); - assert_eq!( - breakpoint.get_account_by_id(acc1()).balance - - final_state.get_account_by_id(acc1()).balance, - 1 - ); - assert_eq!( - final_state.get_account_by_id(acc2()).balance - - breakpoint.get_account_by_id(acc2()).balance, - 1 - ); - } - - #[test] - fn new_breakpoint() { - let temp_dir = tempdir().unwrap(); - let temdir_path = temp_dir.path(); - - let dbio = - RocksDBIO::open_or_create(temdir_path, &genesis_block(), &initial_state()).unwrap(); - - for i in 1..BREAKPOINT_INTERVAL { - let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap().unwrap(); - - let prev_hash = last_block.header.hash; - let transfer_tx = transfer(1, u128::from(i - 1), true); - let block = common::test_utils::produce_dummy_block( - u64::from(i + 1), - Some(prev_hash), - vec![transfer_tx], - ); - dbio.put_block(&block, [i; 32]).unwrap(); - } - - let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let first_id = dbio.get_meta_first_block_in_db().unwrap(); - let is_first_set = dbio.get_meta_is_first_block_set().unwrap(); - let last_br_id = dbio.get_meta_last_breakpoint_id().unwrap(); - let last_block = dbio.get_block(last_id).unwrap().unwrap(); - let prev_breakpoint = dbio.get_breakpoint(0).unwrap(); - let breakpoint = dbio.get_breakpoint(1).unwrap(); - let final_state = dbio.final_state().unwrap(); - - assert_eq!(last_id, 100); - assert_eq!(first_id, 1); - assert!(is_first_set); - assert_eq!(last_br_id, 1); - assert_ne!(last_block.header.hash, genesis_block().header.hash); - assert_eq!( - prev_breakpoint.get_account_by_id(acc1()).balance - - final_state.get_account_by_id(acc1()).balance, - 99 - ); - assert_eq!( - final_state.get_account_by_id(acc2()).balance - - prev_breakpoint.get_account_by_id(acc2()).balance, - 99 - ); - assert_eq!( - breakpoint.get_account_by_id(acc1()), - final_state.get_account_by_id(acc1()) - ); - assert_eq!( - breakpoint.get_account_by_id(acc2()), - final_state.get_account_by_id(acc2()) - ); - } - - #[test] - fn simple_maps() { - let temp_dir = tempdir().unwrap(); - let temdir_path = temp_dir.path(); - - let dbio = - RocksDBIO::open_or_create(temdir_path, &genesis_block(), &initial_state()).unwrap(); - - let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap().unwrap(); - - let prev_hash = last_block.header.hash; - let transfer_tx = transfer(1, 0, true); - let block = common::test_utils::produce_dummy_block(2, Some(prev_hash), vec![transfer_tx]); - - let control_hash1 = block.header.hash; - - dbio.put_block(&block, [1; 32]).unwrap(); - - let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap().unwrap(); - - let prev_hash = last_block.header.hash; - let transfer_tx = transfer(1, 1, true); - let block = common::test_utils::produce_dummy_block(3, Some(prev_hash), vec![transfer_tx]); - - let control_hash2 = block.header.hash; - - dbio.put_block(&block, [2; 32]).unwrap(); - - let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap().unwrap(); - - let prev_hash = last_block.header.hash; - let transfer_tx = transfer(1, 2, true); - - let control_tx_hash1 = transfer_tx.hash(); - - let block = common::test_utils::produce_dummy_block(4, Some(prev_hash), vec![transfer_tx]); - dbio.put_block(&block, [3; 32]).unwrap(); - - let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap().unwrap(); - - let prev_hash = last_block.header.hash; - let transfer_tx = transfer(1, 3, true); - - let control_tx_hash2 = transfer_tx.hash(); - - let block = common::test_utils::produce_dummy_block(5, Some(prev_hash), vec![transfer_tx]); - dbio.put_block(&block, [4; 32]).unwrap(); - - let control_block_id1 = dbio.get_block_id_by_hash(control_hash1.0).unwrap().unwrap(); - let control_block_id2 = dbio.get_block_id_by_hash(control_hash2.0).unwrap().unwrap(); - let control_block_id3 = dbio - .get_block_id_by_tx_hash(control_tx_hash1.0) - .unwrap() - .unwrap(); - let control_block_id4 = dbio - .get_block_id_by_tx_hash(control_tx_hash2.0) - .unwrap() - .unwrap(); - - assert_eq!(control_block_id1, 2); - assert_eq!(control_block_id2, 3); - assert_eq!(control_block_id3, 4); - assert_eq!(control_block_id4, 5); - } - - #[test] - fn block_batch() { - let temp_dir = tempdir().unwrap(); - let temdir_path = temp_dir.path(); - - let mut block_res = vec![]; - - let dbio = - RocksDBIO::open_or_create(temdir_path, &genesis_block(), &initial_state()).unwrap(); - - let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap().unwrap(); - - let prev_hash = last_block.header.hash; - let transfer_tx = transfer(1, 0, true); - let block = common::test_utils::produce_dummy_block(2, Some(prev_hash), vec![transfer_tx]); - - block_res.push(block.clone()); - dbio.put_block(&block, [1; 32]).unwrap(); - - let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap().unwrap(); - - let prev_hash = last_block.header.hash; - let transfer_tx = transfer(1, 1, true); - let block = common::test_utils::produce_dummy_block(3, Some(prev_hash), vec![transfer_tx]); - - block_res.push(block.clone()); - dbio.put_block(&block, [2; 32]).unwrap(); - - let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap().unwrap(); - - let prev_hash = last_block.header.hash; - let transfer_tx = transfer(1, 2, true); - - let block = common::test_utils::produce_dummy_block(4, Some(prev_hash), vec![transfer_tx]); - block_res.push(block.clone()); - dbio.put_block(&block, [3; 32]).unwrap(); - - let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap().unwrap(); - - let prev_hash = last_block.header.hash; - let transfer_tx = transfer(1, 3, true); - - let block = common::test_utils::produce_dummy_block(5, Some(prev_hash), vec![transfer_tx]); - block_res.push(block.clone()); - dbio.put_block(&block, [4; 32]).unwrap(); - - let block_hashes_mem: Vec<[u8; 32]> = - block_res.into_iter().map(|bl| bl.header.hash.0).collect(); - - // Get blocks before ID 6 (i.e., starting from 5 going backwards), limit 4 - // This should return blocks 5, 4, 3, 2 in descending order - let mut batch_res = dbio.get_block_batch(Some(6), 4).unwrap(); - batch_res.reverse(); // Reverse to match ascending order for comparison - - let block_hashes_db: Vec<[u8; 32]> = - batch_res.into_iter().map(|bl| bl.header.hash.0).collect(); - - assert_eq!(block_hashes_mem, block_hashes_db); - - let block_hashes_mem_limited = &block_hashes_mem[1..]; - - // Get blocks before ID 6, limit 3 - // This should return blocks 5, 4, 3 in descending order - let mut batch_res_limited = dbio.get_block_batch(Some(6), 3).unwrap(); - batch_res_limited.reverse(); // Reverse to match ascending order for comparison - - let block_hashes_db_limited: Vec<[u8; 32]> = batch_res_limited - .into_iter() - .map(|bl| bl.header.hash.0) - .collect(); - - assert_eq!(block_hashes_mem_limited, block_hashes_db_limited.as_slice()); - } - - #[test] - fn account_map() { - let temp_dir = tempdir().unwrap(); - let temdir_path = temp_dir.path(); - - let mut tx_hash_res = vec![]; - - let dbio = - RocksDBIO::open_or_create(temdir_path, &genesis_block(), &initial_state()).unwrap(); - - let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap().unwrap(); - - let prev_hash = last_block.header.hash; - let transfer_tx = transfer(1, 0, true); - - tx_hash_res.push(transfer_tx.hash().0); - - let block = common::test_utils::produce_dummy_block(2, Some(prev_hash), vec![transfer_tx]); - - dbio.put_block(&block, [1; 32]).unwrap(); - - let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap().unwrap(); - - let prev_hash = last_block.header.hash; - let transfer_tx = transfer(1, 1, true); - - tx_hash_res.push(transfer_tx.hash().0); - - let block = common::test_utils::produce_dummy_block(3, Some(prev_hash), vec![transfer_tx]); - - dbio.put_block(&block, [2; 32]).unwrap(); - - let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap().unwrap(); - - let prev_hash = last_block.header.hash; - let transfer_tx = transfer(1, 2, true); - - tx_hash_res.push(transfer_tx.hash().0); - - let block = common::test_utils::produce_dummy_block(4, Some(prev_hash), vec![transfer_tx]); - - dbio.put_block(&block, [3; 32]).unwrap(); - - let last_id = dbio.get_meta_last_block_in_db().unwrap(); - let last_block = dbio.get_block(last_id).unwrap().unwrap(); - - let prev_hash = last_block.header.hash; - let transfer_tx = transfer(1, 3, true); - - tx_hash_res.push(transfer_tx.hash().0); - - let block = common::test_utils::produce_dummy_block(5, Some(prev_hash), vec![transfer_tx]); - - dbio.put_block(&block, [4; 32]).unwrap(); - - let acc1_tx = dbio.get_acc_transactions(*acc1().value(), 0, 4).unwrap(); - let acc1_tx_hashes: Vec<[u8; 32]> = acc1_tx.into_iter().map(|tx| tx.hash().0).collect(); - - assert_eq!(acc1_tx_hashes, tx_hash_res); - - let acc1_tx_limited = dbio.get_acc_transactions(*acc1().value(), 1, 4).unwrap(); - let acc1_tx_limited_hashes: Vec<[u8; 32]> = - acc1_tx_limited.into_iter().map(|tx| tx.hash().0).collect(); - - assert_eq!(acc1_tx_limited_hashes.as_slice(), &tx_hash_res[1..]); - } -} diff --git a/storage/src/indexer/mod.rs b/storage/src/indexer/mod.rs new file mode 100644 index 00000000..23e8aa89 --- /dev/null +++ b/storage/src/indexer/mod.rs @@ -0,0 +1,688 @@ +use std::{path::Path, sync::Arc}; + +use common::block::Block; +use nssa::V03State; +use rocksdb::{ + BoundColumnFamily, ColumnFamilyDescriptor, DBWithThreadMode, MultiThreaded, Options, +}; + +use crate::error::DbError; + +pub mod read_multiple; +pub mod read_once; +pub mod write_atomic; +pub mod write_non_atomic; + +/// Maximal size of stored blocks in base. +/// +/// Used to control db size. +/// +/// Currently effectively unbounded. +pub const BUFF_SIZE_ROCKSDB: usize = usize::MAX; + +/// Size of stored blocks cache in memory. +/// +/// Keeping small to not run out of memory. +pub const CACHE_SIZE: usize = 1000; + +/// Key base for storing metainformation about id of first block in db. +pub const DB_META_FIRST_BLOCK_IN_DB_KEY: &str = "first_block_in_db"; +/// Key base for storing metainformation about id of last current block in db. +pub const DB_META_LAST_BLOCK_IN_DB_KEY: &str = "last_block_in_db"; +/// Key base for storing metainformation about id of last observed L1 lib header in db. +pub const DB_META_LAST_OBSERVED_L1_LIB_HEADER_ID_IN_DB_KEY: &str = + "last_observed_l1_lib_header_in_db"; +/// Key base for storing metainformation which describe if first block has been set. +pub const DB_META_FIRST_BLOCK_SET_KEY: &str = "first_block_set"; +/// Key base for storing metainformation about the last breakpoint. +pub const DB_META_LAST_BREAKPOINT_ID: &str = "last_breakpoint_id"; + +/// Interval between state breakpoints. +pub const BREAKPOINT_INTERVAL: u8 = 100; + +/// Name of block column family. +pub const CF_BLOCK_NAME: &str = "cf_block"; +/// Name of meta column family. +pub const CF_META_NAME: &str = "cf_meta"; +/// Name of breakpoint column family. +pub const CF_BREAKPOINT_NAME: &str = "cf_breakpoint"; +/// Name of hash to id map column family. +pub const CF_HASH_TO_ID: &str = "cf_hash_to_id"; +/// Name of tx hash to id map column family. +pub const CF_TX_TO_ID: &str = "cf_tx_to_id"; +/// Name of account meta column family. +pub const CF_ACC_META: &str = "cf_acc_meta"; +/// Name of account id to tx hash map column family. +pub const CF_ACC_TO_TX: &str = "cf_acc_to_tx"; + +pub type DbResult = Result; + +pub struct RocksDBIO { + pub db: DBWithThreadMode, +} + +impl RocksDBIO { + pub fn open_or_create( + path: &Path, + genesis_block: &Block, + initial_state: &V03State, + ) -> DbResult { + let mut cf_opts = Options::default(); + cf_opts.set_max_write_buffer_number(16); + // ToDo: Add more column families for different data + let cfb = ColumnFamilyDescriptor::new(CF_BLOCK_NAME, cf_opts.clone()); + let cfmeta = ColumnFamilyDescriptor::new(CF_META_NAME, cf_opts.clone()); + let cfbreakpoint = ColumnFamilyDescriptor::new(CF_BREAKPOINT_NAME, cf_opts.clone()); + let cfhti = ColumnFamilyDescriptor::new(CF_HASH_TO_ID, cf_opts.clone()); + let cftti = ColumnFamilyDescriptor::new(CF_TX_TO_ID, cf_opts.clone()); + let cfameta = ColumnFamilyDescriptor::new(CF_ACC_META, cf_opts.clone()); + let cfatt = ColumnFamilyDescriptor::new(CF_ACC_TO_TX, cf_opts.clone()); + + let mut db_opts = Options::default(); + db_opts.create_missing_column_families(true); + db_opts.create_if_missing(true); + let db = DBWithThreadMode::::open_cf_descriptors( + &db_opts, + path, + vec![cfb, cfmeta, cfbreakpoint, cfhti, cftti, cfameta, cfatt], + ) + .map_err(|err| DbError::RocksDbError { + error: err, + additional_info: Some("Failed to open or create DB".to_owned()), + })?; + + let dbio = Self { db }; + + let is_start_set = dbio.get_meta_is_first_block_set()?; + if !is_start_set { + let block_id = genesis_block.header.block_id; + dbio.put_meta_last_block_in_db(block_id)?; + dbio.put_meta_first_block_in_db_batch(genesis_block)?; + dbio.put_meta_is_first_block_set()?; + + // First breakpoint setup + dbio.put_breakpoint(0, initial_state)?; + dbio.put_meta_last_breakpoint_id(0)?; + } + + Ok(dbio) + } + + pub fn destroy(path: &Path) -> DbResult<()> { + let db_opts = Options::default(); + DBWithThreadMode::::destroy(&db_opts, path) + .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None)) + } + + // Columns + + pub fn meta_column(&self) -> Arc> { + self.db + .cf_handle(CF_META_NAME) + .expect("Meta column should exist") + } + + pub fn block_column(&self) -> Arc> { + self.db + .cf_handle(CF_BLOCK_NAME) + .expect("Block column should exist") + } + + pub fn breakpoint_column(&self) -> Arc> { + self.db + .cf_handle(CF_BREAKPOINT_NAME) + .expect("Breakpoint column should exist") + } + + pub fn hash_to_id_column(&self) -> Arc> { + self.db + .cf_handle(CF_HASH_TO_ID) + .expect("Hash to id map column should exist") + } + + pub fn tx_hash_to_id_column(&self) -> Arc> { + self.db + .cf_handle(CF_TX_TO_ID) + .expect("Tx hash to id map column should exist") + } + + pub fn account_id_to_tx_hash_column(&self) -> Arc> { + self.db + .cf_handle(CF_ACC_TO_TX) + .expect("Account id to tx map column should exist") + } + + pub fn account_meta_column(&self) -> Arc> { + self.db + .cf_handle(CF_ACC_META) + .expect("Account meta column should exist") + } + + // State + + pub fn calculate_state_for_id(&self, block_id: u64) -> DbResult { + let last_block = self.get_meta_last_block_in_db()?; + + if block_id <= last_block { + let br_id = closest_breakpoint_id(block_id); + let mut breakpoint = self.get_breakpoint(br_id)?; + + // ToDo: update it to handle any genesis id + // right now works correctly only if genesis_id < BREAKPOINT_INTERVAL + let start = if br_id != 0 { + u64::from(BREAKPOINT_INTERVAL) + .checked_mul(br_id) + .expect("Reached maximum breakpoint id") + } else { + self.get_meta_first_block_in_db()? + }; + + for block in self.get_block_batch_seq( + start.checked_add(1).expect("Will be lesser that u64::MAX")..=block_id, + )? { + for transaction in block.body.transactions { + transaction + .transaction_stateless_check() + .map_err(|err| { + DbError::db_interaction_error(format!( + "transaction pre check failed with err {err:?}" + )) + })? + .execute_check_on_state(&mut breakpoint, block.header.block_id, block.header.timestamp) + .map_err(|err| { + DbError::db_interaction_error(format!( + "transaction execution failed with err {err:?}" + )) + })?; + } + } + + Ok(breakpoint) + } else { + Err(DbError::db_interaction_error( + "Block on this id not found".to_owned(), + )) + } + } + + pub fn final_state(&self) -> DbResult { + self.calculate_state_for_id(self.get_meta_last_block_in_db()?) + } +} + +fn closest_breakpoint_id(block_id: u64) -> u64 { + block_id + .saturating_sub(1) + .checked_div(u64::from(BREAKPOINT_INTERVAL)) + .expect("Breakpoint interval is not zero") +} + +#[expect(clippy::shadow_unrelated, reason = "Fine for tests")] +#[cfg(test)] +mod tests { + use nssa::{AccountId, PublicKey}; + use tempfile::tempdir; + + use super::*; + + fn genesis_block() -> Block { + common::test_utils::produce_dummy_block(1, None, vec![]) + } + + fn acc1_sign_key() -> nssa::PrivateKey { + nssa::PrivateKey::try_new([1; 32]).unwrap() + } + + fn acc2_sign_key() -> nssa::PrivateKey { + nssa::PrivateKey::try_new([2; 32]).unwrap() + } + + fn acc1() -> AccountId { + AccountId::from(&PublicKey::new_from_private_key(&acc1_sign_key())) + } + + fn acc2() -> AccountId { + AccountId::from(&PublicKey::new_from_private_key(&acc2_sign_key())) + } + + #[test] + fn start_db() { + let temp_dir = tempdir().unwrap(); + let temdir_path = temp_dir.path(); + + let dbio = RocksDBIO::open_or_create( + temdir_path, + &genesis_block(), + &nssa::V03State::new_with_genesis_accounts(&[(acc1(), 10000), (acc2(), 20000)], &[]), + ) + .unwrap(); + + let last_id = dbio.get_meta_last_block_in_db().unwrap(); + let first_id = dbio.get_meta_first_block_in_db().unwrap(); + let is_first_set = dbio.get_meta_is_first_block_set().unwrap(); + let last_observed_l1_header = dbio.get_meta_last_observed_l1_lib_header_in_db().unwrap(); + let last_br_id = dbio.get_meta_last_breakpoint_id().unwrap(); + let last_block = dbio.get_block(1).unwrap().unwrap(); + let breakpoint = dbio.get_breakpoint(0).unwrap(); + let final_state = dbio.final_state().unwrap(); + + assert_eq!(last_id, 1); + assert_eq!(first_id, 1); + assert_eq!(last_observed_l1_header, None); + assert!(is_first_set); + assert_eq!(last_br_id, 0); + assert_eq!(last_block.header.hash, genesis_block().header.hash); + assert_eq!( + breakpoint.get_account_by_id(acc1()), + final_state.get_account_by_id(acc1()) + ); + assert_eq!( + breakpoint.get_account_by_id(acc2()), + final_state.get_account_by_id(acc2()) + ); + } + + #[test] + fn one_block_insertion() { + let temp_dir = tempdir().unwrap(); + let temdir_path = temp_dir.path(); + + let dbio = RocksDBIO::open_or_create( + temdir_path, + &genesis_block(), + &nssa::V03State::new_with_genesis_accounts(&[(acc1(), 10000), (acc2(), 20000)], &[]), + ) + .unwrap(); + + let prev_hash = genesis_block().header.hash; + let from = acc1(); + let to = acc2(); + let sign_key = acc1_sign_key(); + + let transfer_tx = + common::test_utils::create_transaction_native_token_transfer(from, 0, to, 1, &sign_key); + let block = common::test_utils::produce_dummy_block(2, Some(prev_hash), vec![transfer_tx]); + + dbio.put_block(&block, [1; 32]).unwrap(); + + let last_id = dbio.get_meta_last_block_in_db().unwrap(); + let first_id = dbio.get_meta_first_block_in_db().unwrap(); + let last_observed_l1_header = dbio + .get_meta_last_observed_l1_lib_header_in_db() + .unwrap() + .unwrap(); + let is_first_set = dbio.get_meta_is_first_block_set().unwrap(); + let last_br_id = dbio.get_meta_last_breakpoint_id().unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); + let breakpoint = dbio.get_breakpoint(0).unwrap(); + let final_state = dbio.final_state().unwrap(); + + assert_eq!(last_id, 2); + assert_eq!(first_id, 1); + assert_eq!(last_observed_l1_header, [1; 32]); + assert!(is_first_set); + assert_eq!(last_br_id, 0); + assert_ne!(last_block.header.hash, genesis_block().header.hash); + assert_eq!( + breakpoint.get_account_by_id(acc1()).balance + - final_state.get_account_by_id(acc1()).balance, + 1 + ); + assert_eq!( + final_state.get_account_by_id(acc2()).balance + - breakpoint.get_account_by_id(acc2()).balance, + 1 + ); + } + + #[test] + fn new_breakpoint() { + let temp_dir = tempdir().unwrap(); + let temdir_path = temp_dir.path(); + + let dbio = RocksDBIO::open_or_create( + temdir_path, + &genesis_block(), + &nssa::V03State::new_with_genesis_accounts(&[(acc1(), 10000), (acc2(), 20000)], &[]), + ) + .unwrap(); + + let from = acc1(); + let to = acc2(); + let sign_key = acc1_sign_key(); + + for i in 1..=BREAKPOINT_INTERVAL { + let last_id = dbio.get_meta_last_block_in_db().unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); + + let prev_hash = last_block.header.hash; + + let transfer_tx = common::test_utils::create_transaction_native_token_transfer( + from, + (i - 1).into(), + to, + 1, + &sign_key, + ); + let block = common::test_utils::produce_dummy_block( + (i + 1).into(), + Some(prev_hash), + vec![transfer_tx], + ); + dbio.put_block(&block, [i; 32]).unwrap(); + } + + let last_id = dbio.get_meta_last_block_in_db().unwrap(); + let first_id = dbio.get_meta_first_block_in_db().unwrap(); + let is_first_set = dbio.get_meta_is_first_block_set().unwrap(); + let last_br_id = dbio.get_meta_last_breakpoint_id().unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); + let prev_breakpoint = dbio.get_breakpoint(0).unwrap(); + let breakpoint = dbio.get_breakpoint(1).unwrap(); + let final_state = dbio.final_state().unwrap(); + + assert_eq!(last_id, 101); + assert_eq!(first_id, 1); + assert!(is_first_set); + assert_eq!(last_br_id, 1); + assert_ne!(last_block.header.hash, genesis_block().header.hash); + assert_eq!( + prev_breakpoint.get_account_by_id(acc1()).balance + - final_state.get_account_by_id(acc1()).balance, + 100 + ); + assert_eq!( + final_state.get_account_by_id(acc2()).balance + - prev_breakpoint.get_account_by_id(acc2()).balance, + 100 + ); + assert_eq!( + breakpoint.get_account_by_id(acc1()).balance + - final_state.get_account_by_id(acc1()).balance, + 1 + ); + assert_eq!( + final_state.get_account_by_id(acc2()).balance + - breakpoint.get_account_by_id(acc2()).balance, + 1 + ); + } + + #[test] + fn simple_maps() { + let temp_dir = tempdir().unwrap(); + let temdir_path = temp_dir.path(); + + let dbio = RocksDBIO::open_or_create( + temdir_path, + &genesis_block(), + &nssa::V03State::new_with_genesis_accounts(&[(acc1(), 10000), (acc2(), 20000)], &[]), + ) + .unwrap(); + + let from = acc1(); + let to = acc2(); + let sign_key = acc1_sign_key(); + + let last_id = dbio.get_meta_last_block_in_db().unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); + + let prev_hash = last_block.header.hash; + let transfer_tx = + common::test_utils::create_transaction_native_token_transfer(from, 0, to, 1, &sign_key); + let block = common::test_utils::produce_dummy_block(2, Some(prev_hash), vec![transfer_tx]); + + let control_hash1 = block.header.hash; + + dbio.put_block(&block, [1; 32]).unwrap(); + + let last_id = dbio.get_meta_last_block_in_db().unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); + + let prev_hash = last_block.header.hash; + let transfer_tx = + common::test_utils::create_transaction_native_token_transfer(from, 1, to, 1, &sign_key); + let block = common::test_utils::produce_dummy_block(3, Some(prev_hash), vec![transfer_tx]); + + let control_hash2 = block.header.hash; + + dbio.put_block(&block, [2; 32]).unwrap(); + + let last_id = dbio.get_meta_last_block_in_db().unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); + + let prev_hash = last_block.header.hash; + let transfer_tx = + common::test_utils::create_transaction_native_token_transfer(from, 2, to, 1, &sign_key); + + let control_tx_hash1 = transfer_tx.hash(); + + let block = common::test_utils::produce_dummy_block(4, Some(prev_hash), vec![transfer_tx]); + dbio.put_block(&block, [3; 32]).unwrap(); + + let last_id = dbio.get_meta_last_block_in_db().unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); + + let prev_hash = last_block.header.hash; + let transfer_tx = + common::test_utils::create_transaction_native_token_transfer(from, 3, to, 1, &sign_key); + + let control_tx_hash2 = transfer_tx.hash(); + + let block = common::test_utils::produce_dummy_block(5, Some(prev_hash), vec![transfer_tx]); + dbio.put_block(&block, [4; 32]).unwrap(); + + let control_block_id1 = dbio.get_block_id_by_hash(control_hash1.0).unwrap().unwrap(); + let control_block_id2 = dbio.get_block_id_by_hash(control_hash2.0).unwrap().unwrap(); + let control_block_id3 = dbio + .get_block_id_by_tx_hash(control_tx_hash1.0) + .unwrap() + .unwrap(); + let control_block_id4 = dbio + .get_block_id_by_tx_hash(control_tx_hash2.0) + .unwrap() + .unwrap(); + + assert_eq!(control_block_id1, 2); + assert_eq!(control_block_id2, 3); + assert_eq!(control_block_id3, 4); + assert_eq!(control_block_id4, 5); + } + + #[test] + fn block_batch() { + let temp_dir = tempdir().unwrap(); + let temdir_path = temp_dir.path(); + + let mut block_res = vec![]; + + let dbio = RocksDBIO::open_or_create( + temdir_path, + &genesis_block(), + &nssa::V03State::new_with_genesis_accounts(&[(acc1(), 10000), (acc2(), 20000)], &[]), + ) + .unwrap(); + + let from = acc1(); + let to = acc2(); + let sign_key = acc1_sign_key(); + + let last_id = dbio.get_meta_last_block_in_db().unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); + + let prev_hash = last_block.header.hash; + let transfer_tx = + common::test_utils::create_transaction_native_token_transfer(from, 0, to, 1, &sign_key); + let block = common::test_utils::produce_dummy_block(2, Some(prev_hash), vec![transfer_tx]); + + block_res.push(block.clone()); + dbio.put_block(&block, [1; 32]).unwrap(); + + let last_id = dbio.get_meta_last_block_in_db().unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); + + let prev_hash = last_block.header.hash; + let transfer_tx = + common::test_utils::create_transaction_native_token_transfer(from, 1, to, 1, &sign_key); + let block = common::test_utils::produce_dummy_block(3, Some(prev_hash), vec![transfer_tx]); + + block_res.push(block.clone()); + dbio.put_block(&block, [2; 32]).unwrap(); + + let last_id = dbio.get_meta_last_block_in_db().unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); + + let prev_hash = last_block.header.hash; + let transfer_tx = + common::test_utils::create_transaction_native_token_transfer(from, 2, to, 1, &sign_key); + + let block = common::test_utils::produce_dummy_block(4, Some(prev_hash), vec![transfer_tx]); + block_res.push(block.clone()); + dbio.put_block(&block, [3; 32]).unwrap(); + + let last_id = dbio.get_meta_last_block_in_db().unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); + + let prev_hash = last_block.header.hash; + let transfer_tx = + common::test_utils::create_transaction_native_token_transfer(from, 3, to, 1, &sign_key); + + let block = common::test_utils::produce_dummy_block(5, Some(prev_hash), vec![transfer_tx]); + block_res.push(block.clone()); + dbio.put_block(&block, [4; 32]).unwrap(); + + let block_hashes_mem: Vec<[u8; 32]> = + block_res.into_iter().map(|bl| bl.header.hash.0).collect(); + + // Get blocks before ID 6 (i.e., starting from 5 going backwards), limit 4 + // This should return blocks 5, 4, 3, 2 in descending order + let mut batch_res = dbio.get_block_batch(Some(6), 4).unwrap(); + batch_res.reverse(); // Reverse to match ascending order for comparison + + let block_hashes_db: Vec<[u8; 32]> = + batch_res.into_iter().map(|bl| bl.header.hash.0).collect(); + + assert_eq!(block_hashes_mem, block_hashes_db); + + let block_hashes_mem_limited = &block_hashes_mem[1..]; + + // Get blocks before ID 6, limit 3 + // This should return blocks 5, 4, 3 in descending order + let mut batch_res_limited = dbio.get_block_batch(Some(6), 3).unwrap(); + batch_res_limited.reverse(); // Reverse to match ascending order for comparison + + let block_hashes_db_limited: Vec<[u8; 32]> = batch_res_limited + .into_iter() + .map(|bl| bl.header.hash.0) + .collect(); + + assert_eq!(block_hashes_mem_limited, block_hashes_db_limited.as_slice()); + + let block_batch_seq = dbio.get_block_batch_seq(1..=5).unwrap(); + let block_batch_ids = block_batch_seq + .into_iter() + .map(|block| block.header.block_id) + .collect::>(); + + assert_eq!(block_batch_ids, vec![1, 2, 3, 4, 5]); + } + + #[test] + fn account_map() { + let temp_dir = tempdir().unwrap(); + let temdir_path = temp_dir.path(); + + let dbio = RocksDBIO::open_or_create( + temdir_path, + &genesis_block(), + &nssa::V03State::new_with_genesis_accounts(&[(acc1(), 10000), (acc2(), 20000)], &[]), + ) + .unwrap(); + + let from = acc1(); + let to = acc2(); + let sign_key = acc1_sign_key(); + + let mut tx_hash_res = vec![]; + + let last_id = dbio.get_meta_last_block_in_db().unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); + + let prev_hash = last_block.header.hash; + let transfer_tx1 = + common::test_utils::create_transaction_native_token_transfer(from, 0, to, 1, &sign_key); + let transfer_tx2 = + common::test_utils::create_transaction_native_token_transfer(from, 1, to, 1, &sign_key); + tx_hash_res.push(transfer_tx1.hash().0); + tx_hash_res.push(transfer_tx2.hash().0); + + let block = common::test_utils::produce_dummy_block( + 2, + Some(prev_hash), + vec![transfer_tx1, transfer_tx2], + ); + + dbio.put_block(&block, [1; 32]).unwrap(); + + let last_id = dbio.get_meta_last_block_in_db().unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); + + let prev_hash = last_block.header.hash; + let transfer_tx1 = + common::test_utils::create_transaction_native_token_transfer(from, 2, to, 1, &sign_key); + let transfer_tx2 = + common::test_utils::create_transaction_native_token_transfer(from, 3, to, 1, &sign_key); + tx_hash_res.push(transfer_tx1.hash().0); + tx_hash_res.push(transfer_tx2.hash().0); + + let block = common::test_utils::produce_dummy_block( + 3, + Some(prev_hash), + vec![transfer_tx1, transfer_tx2], + ); + + dbio.put_block(&block, [2; 32]).unwrap(); + + let last_id = dbio.get_meta_last_block_in_db().unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); + + let prev_hash = last_block.header.hash; + let transfer_tx1 = + common::test_utils::create_transaction_native_token_transfer(from, 4, to, 1, &sign_key); + let transfer_tx2 = + common::test_utils::create_transaction_native_token_transfer(from, 5, to, 1, &sign_key); + tx_hash_res.push(transfer_tx1.hash().0); + tx_hash_res.push(transfer_tx2.hash().0); + + let block = common::test_utils::produce_dummy_block( + 4, + Some(prev_hash), + vec![transfer_tx1, transfer_tx2], + ); + + dbio.put_block(&block, [3; 32]).unwrap(); + + let last_id = dbio.get_meta_last_block_in_db().unwrap(); + let last_block = dbio.get_block(last_id).unwrap().unwrap(); + + let prev_hash = last_block.header.hash; + let transfer_tx = + common::test_utils::create_transaction_native_token_transfer(from, 6, to, 1, &sign_key); + tx_hash_res.push(transfer_tx.hash().0); + + let block = common::test_utils::produce_dummy_block(5, Some(prev_hash), vec![transfer_tx]); + + dbio.put_block(&block, [4; 32]).unwrap(); + + let acc1_tx = dbio.get_acc_transactions(*acc1().value(), 0, 7).unwrap(); + let acc1_tx_hashes: Vec<[u8; 32]> = acc1_tx.into_iter().map(|tx| tx.hash().0).collect(); + + assert_eq!(acc1_tx_hashes, tx_hash_res); + + let acc1_tx_limited = dbio.get_acc_transactions(*acc1().value(), 1, 4).unwrap(); + let acc1_tx_limited_hashes: Vec<[u8; 32]> = + acc1_tx_limited.into_iter().map(|tx| tx.hash().0).collect(); + + assert_eq!(acc1_tx_limited_hashes.as_slice(), &tx_hash_res[1..5]); + } +} diff --git a/storage/src/indexer/read_multiple.rs b/storage/src/indexer/read_multiple.rs new file mode 100644 index 00000000..866fc7b0 --- /dev/null +++ b/storage/src/indexer/read_multiple.rs @@ -0,0 +1,209 @@ +use common::transaction::NSSATransaction; + +use super::{Block, DbError, DbResult, RocksDBIO}; + +#[expect(clippy::multiple_inherent_impl, reason = "Readability")] +impl RocksDBIO { + pub fn get_block_batch(&self, before: Option, limit: u64) -> DbResult> { + let mut seq = vec![]; + + // Determine the starting block ID + let start_block_id = if let Some(before_id) = before { + before_id.saturating_sub(1) + } else { + // Get the latest block ID + self.get_meta_last_block_in_db()? + }; + + for i in 0..limit { + let block_id = start_block_id.saturating_sub(i); + if block_id == 0 { + break; + } + seq.push(block_id); + } + + self.get_block_batch_seq(seq.into_iter()) + } + + /// Get block batch from a sequence. + /// + /// Currently assumes non-decreasing sequence. + /// + /// `ToDo`: Add suport of arbitrary sequences. + pub fn get_block_batch_seq(&self, seq: impl Iterator) -> DbResult> { + let cf_block = self.block_column(); + + // Keys setup + let mut keys = vec![]; + for block_id in seq { + keys.push(( + &cf_block, + borsh::to_vec(&block_id).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize block id".to_owned()), + ) + })?, + )); + } + + let multi_get_res = self.db.multi_get_cf(keys); + + // Keys parsing + let mut block_batch = vec![]; + for res in multi_get_res { + let res = res.map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; + + let block = if let Some(data) = res { + Ok(borsh::from_slice::(&data).map_err(|serr| { + DbError::borsh_cast_message( + serr, + Some("Failed to deserialize block data".to_owned()), + ) + })?) + } else { + // Block not found, assuming that previous one was the last + break; + }?; + + block_batch.push(block); + } + + Ok(block_batch) + } + + /// Get block ids by txs. + /// + /// `ToDo`: There may be multiple transactions in one block + /// so this method can take redundant reads. + /// Need to update signature and implementation. + fn get_block_ids_by_tx_vec(&self, tx_vec: &[[u8; 32]]) -> DbResult> { + let cf_tti = self.tx_hash_to_id_column(); + + // Keys setup + let mut keys = vec![]; + for tx_hash in tx_vec { + keys.push(( + &cf_tti, + borsh::to_vec(tx_hash).map_err(|err| { + DbError::borsh_cast_message(err, Some("Failed to serialize tx_hash".to_owned())) + })?, + )); + } + + let multi_get_res = self.db.multi_get_cf(keys); + + // Keys parsing + let mut block_id_batch = vec![]; + for res in multi_get_res { + let res = res + .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))? + .ok_or_else(|| { + DbError::db_interaction_error( + "Tx to block id mapping do not contain transaction from vec".to_owned(), + ) + })?; + + let block_id = { + Ok(borsh::from_slice::(&res).map_err(|serr| { + DbError::borsh_cast_message( + serr, + Some("Failed to deserialize block id".to_owned()), + ) + })?) + }?; + + block_id_batch.push(block_id); + } + + Ok(block_id_batch) + } + + // Account + + pub(crate) fn get_acc_transaction_hashes( + &self, + acc_id: [u8; 32], + offset: u64, + limit: u64, + ) -> DbResult> { + let cf_att = self.account_id_to_tx_hash_column(); + let mut tx_batch = vec![]; + + // Keys preparation + let mut keys = vec![]; + for tx_id in offset + ..offset + .checked_add(limit) + .expect("Transaction limit should be lesser than u64::MAX") + { + let mut prefix = borsh::to_vec(&acc_id).map_err(|berr| { + DbError::borsh_cast_message(berr, Some("Failed to serialize account id".to_owned())) + })?; + let suffix = borsh::to_vec(&tx_id).map_err(|berr| { + DbError::borsh_cast_message(berr, Some("Failed to serialize tx id".to_owned())) + })?; + + prefix.extend_from_slice(&suffix); + + keys.push((&cf_att, prefix)); + } + + let multi_get_res = self.db.multi_get_cf(keys); + + for res in multi_get_res { + let res = res.map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; + + let tx_hash = if let Some(data) = res { + Ok(borsh::from_slice::<[u8; 32]>(&data).map_err(|serr| { + DbError::borsh_cast_message( + serr, + Some("Failed to deserialize tx_hash".to_owned()), + ) + })?) + } else { + // Tx hash not found, assuming that previous one was the last + break; + }?; + + tx_batch.push(tx_hash); + } + + Ok(tx_batch) + } + + pub fn get_acc_transactions( + &self, + acc_id: [u8; 32], + offset: u64, + limit: u64, + ) -> DbResult> { + let mut tx_batch = vec![]; + + let tx_hashes = self.get_acc_transaction_hashes(acc_id, offset, limit)?; + + let associated_blocks_multi_get = self + .get_block_batch_seq(self.get_block_ids_by_tx_vec(&tx_hashes)?.into_iter())? + .into_iter() + .zip(tx_hashes); + + for (block, tx_hash) in associated_blocks_multi_get { + let transaction = block + .body + .transactions + .iter() + .find(|tx| tx.hash().0 == tx_hash) + .ok_or_else(|| { + DbError::db_interaction_error(format!( + "Missing transaction in block {} with hash {:#?}", + block.header.block_id, tx_hash + )) + })?; + + tx_batch.push(transaction.clone()); + } + + Ok(tx_batch) + } +} diff --git a/storage/src/indexer/read_once.rs b/storage/src/indexer/read_once.rs new file mode 100644 index 00000000..74d1afe9 --- /dev/null +++ b/storage/src/indexer/read_once.rs @@ -0,0 +1,272 @@ +use super::{ + Block, DB_META_FIRST_BLOCK_IN_DB_KEY, DB_META_FIRST_BLOCK_SET_KEY, + DB_META_LAST_BLOCK_IN_DB_KEY, DB_META_LAST_BREAKPOINT_ID, + DB_META_LAST_OBSERVED_L1_LIB_HEADER_ID_IN_DB_KEY, DbError, DbResult, RocksDBIO, V03State, +}; + +#[expect(clippy::multiple_inherent_impl, reason = "Readability")] +impl RocksDBIO { + // Meta + + pub fn get_meta_first_block_in_db(&self) -> DbResult { + let cf_meta = self.meta_column(); + let res = self + .db + .get_cf( + &cf_meta, + borsh::to_vec(&DB_META_FIRST_BLOCK_IN_DB_KEY).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize DB_META_FIRST_BLOCK_IN_DB_KEY".to_owned()), + ) + })?, + ) + .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; + + if let Some(data) = res { + Ok(borsh::from_slice::(&data).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to deserialize first block".to_owned()), + ) + })?) + } else { + Err(DbError::db_interaction_error( + "First block not found".to_owned(), + )) + } + } + + pub fn get_meta_last_block_in_db(&self) -> DbResult { + let cf_meta = self.meta_column(); + let res = self + .db + .get_cf( + &cf_meta, + borsh::to_vec(&DB_META_LAST_BLOCK_IN_DB_KEY).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize DB_META_LAST_BLOCK_IN_DB_KEY".to_owned()), + ) + })?, + ) + .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; + + if let Some(data) = res { + Ok(borsh::from_slice::(&data).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to deserialize last block".to_owned()), + ) + })?) + } else { + Err(DbError::db_interaction_error( + "Last block not found".to_owned(), + )) + } + } + + pub fn get_meta_last_observed_l1_lib_header_in_db(&self) -> DbResult> { + let cf_meta = self.meta_column(); + let res = self + .db + .get_cf( + &cf_meta, + borsh::to_vec(&DB_META_LAST_OBSERVED_L1_LIB_HEADER_ID_IN_DB_KEY).map_err( + |err| { + DbError::borsh_cast_message( + err, + Some( + "Failed to serialize DB_META_LAST_OBSERVED_L1_LIB_HEADER_ID_IN_DB_KEY" + .to_owned(), + ), + ) + }, + )?, + ) + .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; + + res.map(|data| { + borsh::from_slice::<[u8; 32]>(&data).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to deserialize last l1 lib header".to_owned()), + ) + }) + }) + .transpose() + } + + pub fn get_meta_is_first_block_set(&self) -> DbResult { + let cf_meta = self.meta_column(); + let res = self + .db + .get_cf( + &cf_meta, + borsh::to_vec(&DB_META_FIRST_BLOCK_SET_KEY).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize DB_META_FIRST_BLOCK_SET_KEY".to_owned()), + ) + })?, + ) + .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; + + Ok(res.is_some()) + } + + pub fn get_meta_last_breakpoint_id(&self) -> DbResult { + let cf_meta = self.meta_column(); + let res = self + .db + .get_cf( + &cf_meta, + borsh::to_vec(&DB_META_LAST_BREAKPOINT_ID).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize DB_META_LAST_BREAKPOINT_ID".to_owned()), + ) + })?, + ) + .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; + + if let Some(data) = res { + Ok(borsh::from_slice::(&data).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to deserialize last breakpoint id".to_owned()), + ) + })?) + } else { + Err(DbError::db_interaction_error( + "Last breakpoint id not found".to_owned(), + )) + } + } + + // Block + + pub fn get_block(&self, block_id: u64) -> DbResult> { + let cf_block = self.block_column(); + let res = self + .db + .get_cf( + &cf_block, + borsh::to_vec(&block_id).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize block id".to_owned()), + ) + })?, + ) + .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; + + if let Some(data) = res { + Ok(Some(borsh::from_slice::(&data).map_err(|serr| { + DbError::borsh_cast_message( + serr, + Some("Failed to deserialize block data".to_owned()), + ) + })?)) + } else { + Ok(None) + } + } + + // State + + pub fn get_breakpoint(&self, br_id: u64) -> DbResult { + let cf_br = self.breakpoint_column(); + let res = self + .db + .get_cf( + &cf_br, + borsh::to_vec(&br_id).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize breakpoint id".to_owned()), + ) + })?, + ) + .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; + + if let Some(data) = res { + Ok(borsh::from_slice::(&data).map_err(|serr| { + DbError::borsh_cast_message( + serr, + Some("Failed to deserialize breakpoint data".to_owned()), + ) + })?) + } else { + Err(DbError::db_interaction_error( + "Breakpoint on this id not found".to_owned(), + )) + } + } + + // Mappings + + pub fn get_block_id_by_hash(&self, hash: [u8; 32]) -> DbResult> { + let cf_hti = self.hash_to_id_column(); + let res = self + .db + .get_cf( + &cf_hti, + borsh::to_vec(&hash).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize block hash".to_owned()), + ) + })?, + ) + .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; + + if let Some(data) = res { + Ok(Some(borsh::from_slice::(&data).map_err(|serr| { + DbError::borsh_cast_message(serr, Some("Failed to deserialize block id".to_owned())) + })?)) + } else { + Ok(None) + } + } + + pub fn get_block_id_by_tx_hash(&self, tx_hash: [u8; 32]) -> DbResult> { + let cf_tti = self.tx_hash_to_id_column(); + let res = self + .db + .get_cf( + &cf_tti, + borsh::to_vec(&tx_hash).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize transaction hash".to_owned()), + ) + })?, + ) + .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; + + if let Some(data) = res { + Ok(Some(borsh::from_slice::(&data).map_err(|serr| { + DbError::borsh_cast_message(serr, Some("Failed to deserialize block id".to_owned())) + })?)) + } else { + Ok(None) + } + } + + // Accounts meta + + pub(crate) fn get_acc_meta_num_tx(&self, acc_id: [u8; 32]) -> DbResult> { + let cf_ameta = self.account_meta_column(); + let res = self.db.get_cf(&cf_ameta, acc_id).map_err(|rerr| { + DbError::rocksdb_cast_message(rerr, Some("Failed to read from acc meta cf".to_owned())) + })?; + + res.map(|data| { + borsh::from_slice::(&data).map_err(|serr| { + DbError::borsh_cast_message(serr, Some("Failed to deserialize num tx".to_owned())) + }) + }) + .transpose() + } +} diff --git a/storage/src/indexer/write_atomic.rs b/storage/src/indexer/write_atomic.rs new file mode 100644 index 00000000..161d763a --- /dev/null +++ b/storage/src/indexer/write_atomic.rs @@ -0,0 +1,339 @@ +use std::collections::HashMap; + +use rocksdb::WriteBatch; + +use super::{ + Arc, BREAKPOINT_INTERVAL, Block, BoundColumnFamily, DB_META_FIRST_BLOCK_IN_DB_KEY, + DB_META_FIRST_BLOCK_SET_KEY, DB_META_LAST_BLOCK_IN_DB_KEY, DB_META_LAST_BREAKPOINT_ID, + DB_META_LAST_OBSERVED_L1_LIB_HEADER_ID_IN_DB_KEY, DbError, DbResult, RocksDBIO, +}; + +#[expect(clippy::multiple_inherent_impl, reason = "Readability")] +impl RocksDBIO { + // Accounts meta + + pub(crate) fn update_acc_meta_batch( + &self, + acc_id: [u8; 32], + num_tx: u64, + write_batch: &mut WriteBatch, + ) -> DbResult<()> { + let cf_ameta = self.account_meta_column(); + + write_batch.put_cf( + &cf_ameta, + borsh::to_vec(&acc_id).map_err(|err| { + DbError::borsh_cast_message(err, Some("Failed to serialize account id".to_owned())) + })?, + borsh::to_vec(&num_tx).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize acc metadata".to_owned()), + ) + })?, + ); + + Ok(()) + } + + // Account + + pub fn put_account_transactions( + &self, + acc_id: [u8; 32], + tx_hashes: &[[u8; 32]], + ) -> DbResult<()> { + let acc_num_tx = self.get_acc_meta_num_tx(acc_id)?.unwrap_or(0); + let cf_att = self.account_id_to_tx_hash_column(); + let mut write_batch = WriteBatch::new(); + + for (tx_id, tx_hash) in tx_hashes.iter().enumerate() { + let put_id = acc_num_tx + .checked_add(tx_id.try_into().expect("Must fit into u64")) + .expect("Tx count should be lesser that u64::MAX"); + + let mut prefix = borsh::to_vec(&acc_id).map_err(|berr| { + DbError::borsh_cast_message(berr, Some("Failed to serialize account id".to_owned())) + })?; + let suffix = borsh::to_vec(&put_id).map_err(|berr| { + DbError::borsh_cast_message(berr, Some("Failed to serialize tx id".to_owned())) + })?; + + prefix.extend_from_slice(&suffix); + + write_batch.put_cf( + &cf_att, + prefix, + borsh::to_vec(tx_hash).map_err(|berr| { + DbError::borsh_cast_message( + berr, + Some("Failed to serialize tx hash".to_owned()), + ) + })?, + ); + } + + self.update_acc_meta_batch( + acc_id, + acc_num_tx + .checked_add(tx_hashes.len().try_into().expect("Must fit into u64")) + .expect("Tx count should be lesser that u64::MAX"), + &mut write_batch, + )?; + + self.db.write(write_batch).map_err(|rerr| { + DbError::rocksdb_cast_message(rerr, Some("Failed to write batch".to_owned())) + }) + } + + pub fn put_account_transactions_dependant( + &self, + acc_id: [u8; 32], + tx_hashes: &[[u8; 32]], + write_batch: &mut WriteBatch, + ) -> DbResult<()> { + let acc_num_tx = self.get_acc_meta_num_tx(acc_id)?.unwrap_or(0); + let cf_att = self.account_id_to_tx_hash_column(); + + for (tx_id, tx_hash) in tx_hashes.iter().enumerate() { + let put_id = acc_num_tx + .checked_add(tx_id.try_into().expect("Must fit into u64")) + .expect("Tx count should be lesser that u64::MAX"); + + let mut prefix = borsh::to_vec(&acc_id).map_err(|berr| { + DbError::borsh_cast_message(berr, Some("Failed to serialize account id".to_owned())) + })?; + let suffix = borsh::to_vec(&put_id).map_err(|berr| { + DbError::borsh_cast_message(berr, Some("Failed to serialize tx id".to_owned())) + })?; + + prefix.extend_from_slice(&suffix); + + write_batch.put_cf( + &cf_att, + prefix, + borsh::to_vec(tx_hash).map_err(|berr| { + DbError::borsh_cast_message( + berr, + Some("Failed to serialize tx hash".to_owned()), + ) + })?, + ); + } + + self.update_acc_meta_batch( + acc_id, + acc_num_tx + .checked_add(tx_hashes.len().try_into().expect("Must fit into u64")) + .expect("Tx count should be lesser that u64::MAX"), + write_batch, + )?; + + Ok(()) + } + + // Meta + + pub fn put_meta_first_block_in_db_batch(&self, block: &Block) -> DbResult<()> { + let cf_meta = self.meta_column(); + self.db + .put_cf( + &cf_meta, + borsh::to_vec(&DB_META_FIRST_BLOCK_IN_DB_KEY).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize DB_META_FIRST_BLOCK_IN_DB_KEY".to_owned()), + ) + })?, + borsh::to_vec(&block.header.block_id).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize first block id".to_owned()), + ) + })?, + ) + .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; + + self.put_block(block, [0; 32])?; + Ok(()) + } + + pub fn put_meta_last_block_in_db_batch( + &self, + block_id: u64, + write_batch: &mut WriteBatch, + ) -> DbResult<()> { + let cf_meta = self.meta_column(); + write_batch.put_cf( + &cf_meta, + borsh::to_vec(&DB_META_LAST_BLOCK_IN_DB_KEY).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize DB_META_LAST_BLOCK_IN_DB_KEY".to_owned()), + ) + })?, + borsh::to_vec(&block_id).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize last block id".to_owned()), + ) + })?, + ); + Ok(()) + } + + pub fn put_meta_last_observed_l1_lib_header_in_db_batch( + &self, + l1_lib_header: [u8; 32], + write_batch: &mut WriteBatch, + ) -> DbResult<()> { + let cf_meta = self.meta_column(); + write_batch.put_cf( + &cf_meta, + borsh::to_vec(&DB_META_LAST_OBSERVED_L1_LIB_HEADER_ID_IN_DB_KEY).map_err(|err| { + DbError::borsh_cast_message( + err, + Some( + "Failed to serialize DB_META_LAST_OBSERVED_L1_LIB_HEADER_ID_IN_DB_KEY" + .to_owned(), + ), + ) + })?, + borsh::to_vec(&l1_lib_header).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize last l1 block header".to_owned()), + ) + })?, + ); + Ok(()) + } + + pub fn put_meta_last_breakpoint_id_batch( + &self, + br_id: u64, + write_batch: &mut WriteBatch, + ) -> DbResult<()> { + let cf_meta = self.meta_column(); + write_batch.put_cf( + &cf_meta, + borsh::to_vec(&DB_META_LAST_BREAKPOINT_ID).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize DB_META_LAST_BREAKPOINT_ID".to_owned()), + ) + })?, + borsh::to_vec(&br_id).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize last block id".to_owned()), + ) + })?, + ); + Ok(()) + } + + pub fn put_meta_is_first_block_set_batch(&self, write_batch: &mut WriteBatch) -> DbResult<()> { + let cf_meta = self.meta_column(); + write_batch.put_cf( + &cf_meta, + borsh::to_vec(&DB_META_FIRST_BLOCK_SET_KEY).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize DB_META_FIRST_BLOCK_SET_KEY".to_owned()), + ) + })?, + [1_u8; 1], + ); + Ok(()) + } + + // Block + + pub fn put_block(&self, block: &Block, l1_lib_header: [u8; 32]) -> DbResult<()> { + let cf_block = self.block_column(); + let cf_hti = self.hash_to_id_column(); + let cf_tti: Arc> = self.tx_hash_to_id_column(); + let last_curr_block = self.get_meta_last_block_in_db()?; + let mut write_batch = WriteBatch::default(); + + write_batch.put_cf( + &cf_block, + borsh::to_vec(&block.header.block_id).map_err(|err| { + DbError::borsh_cast_message(err, Some("Failed to serialize block id".to_owned())) + })?, + borsh::to_vec(block).map_err(|err| { + DbError::borsh_cast_message(err, Some("Failed to serialize block data".to_owned())) + })?, + ); + + if block.header.block_id > last_curr_block { + self.put_meta_last_block_in_db_batch(block.header.block_id, &mut write_batch)?; + self.put_meta_last_observed_l1_lib_header_in_db_batch(l1_lib_header, &mut write_batch)?; + } + + write_batch.put_cf( + &cf_hti, + borsh::to_vec(&block.header.hash).map_err(|err| { + DbError::borsh_cast_message(err, Some("Failed to serialize block hash".to_owned())) + })?, + borsh::to_vec(&block.header.block_id).map_err(|err| { + DbError::borsh_cast_message(err, Some("Failed to serialize block id".to_owned())) + })?, + ); + + let mut acc_to_tx_map: HashMap<[u8; 32], Vec<[u8; 32]>> = HashMap::new(); + + for tx in &block.body.transactions { + let tx_hash = tx.hash(); + + write_batch.put_cf( + &cf_tti, + borsh::to_vec(&tx_hash).map_err(|err| { + DbError::borsh_cast_message(err, Some("Failed to serialize tx hash".to_owned())) + })?, + borsh::to_vec(&block.header.block_id).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize block id".to_owned()), + ) + })?, + ); + + let acc_ids = tx + .affected_public_account_ids() + .into_iter() + .map(nssa::AccountId::into_value) + .collect::>(); + + for acc_id in acc_ids { + acc_to_tx_map + .entry(acc_id) + .and_modify(|tx_hashes| tx_hashes.push(tx_hash.into())) + .or_insert_with(|| vec![tx_hash.into()]); + } + } + + #[expect( + clippy::iter_over_hash_type, + reason = "RocksDB will keep ordering persistent" + )] + for (acc_id, tx_hashes) in acc_to_tx_map { + self.put_account_transactions_dependant(acc_id, &tx_hashes, &mut write_batch)?; + } + + self.db.write(write_batch).map_err(|rerr| { + DbError::rocksdb_cast_message(rerr, Some("Failed to write batch".to_owned())) + })?; + + if block + .header + .block_id + .is_multiple_of(BREAKPOINT_INTERVAL.into()) + { + self.put_next_breakpoint()?; + } + + Ok(()) + } +} diff --git a/storage/src/indexer/write_non_atomic.rs b/storage/src/indexer/write_non_atomic.rs new file mode 100644 index 00000000..17c1be18 --- /dev/null +++ b/storage/src/indexer/write_non_atomic.rs @@ -0,0 +1,147 @@ +use super::{ + BREAKPOINT_INTERVAL, DB_META_FIRST_BLOCK_SET_KEY, DB_META_LAST_BLOCK_IN_DB_KEY, + DB_META_LAST_BREAKPOINT_ID, DB_META_LAST_OBSERVED_L1_LIB_HEADER_ID_IN_DB_KEY, DbError, + DbResult, RocksDBIO, V03State, +}; + +#[expect(clippy::multiple_inherent_impl, reason = "Readability")] +impl RocksDBIO { + // Meta + + pub fn put_meta_last_block_in_db(&self, block_id: u64) -> DbResult<()> { + let cf_meta = self.meta_column(); + self.db + .put_cf( + &cf_meta, + borsh::to_vec(&DB_META_LAST_BLOCK_IN_DB_KEY).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize DB_META_LAST_BLOCK_IN_DB_KEY".to_owned()), + ) + })?, + borsh::to_vec(&block_id).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize last block id".to_owned()), + ) + })?, + ) + .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; + Ok(()) + } + + pub fn put_meta_last_observed_l1_lib_header_in_db( + &self, + l1_lib_header: [u8; 32], + ) -> DbResult<()> { + let cf_meta = self.meta_column(); + self.db + .put_cf( + &cf_meta, + borsh::to_vec(&DB_META_LAST_OBSERVED_L1_LIB_HEADER_ID_IN_DB_KEY).map_err( + |err| { + DbError::borsh_cast_message( + err, + Some( + "Failed to serialize DB_META_LAST_OBSERVED_L1_LIB_HEADER_ID_IN_DB_KEY" + .to_owned(), + ), + ) + }, + )?, + borsh::to_vec(&l1_lib_header).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize last l1 block header".to_owned()), + ) + })?, + ) + .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; + Ok(()) + } + + pub fn put_meta_last_breakpoint_id(&self, br_id: u64) -> DbResult<()> { + let cf_meta = self.meta_column(); + self.db + .put_cf( + &cf_meta, + borsh::to_vec(&DB_META_LAST_BREAKPOINT_ID).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize DB_META_LAST_BREAKPOINT_ID".to_owned()), + ) + })?, + borsh::to_vec(&br_id).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize last block id".to_owned()), + ) + })?, + ) + .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; + Ok(()) + } + + pub fn put_meta_is_first_block_set(&self) -> DbResult<()> { + let cf_meta = self.meta_column(); + self.db + .put_cf( + &cf_meta, + borsh::to_vec(&DB_META_FIRST_BLOCK_SET_KEY).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize DB_META_FIRST_BLOCK_SET_KEY".to_owned()), + ) + })?, + [1_u8; 1], + ) + .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?; + Ok(()) + } + + // State + + pub fn put_breakpoint(&self, br_id: u64, breakpoint: &V03State) -> DbResult<()> { + let cf_br = self.breakpoint_column(); + + self.db + .put_cf( + &cf_br, + borsh::to_vec(&br_id).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize breakpoint id".to_owned()), + ) + })?, + borsh::to_vec(breakpoint).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize breakpoint data".to_owned()), + ) + })?, + ) + .map_err(|rerr| DbError::rocksdb_cast_message(rerr, None)) + } + + pub fn put_next_breakpoint(&self) -> DbResult<()> { + let last_block = self.get_meta_last_block_in_db()?; + let next_breakpoint_id = self + .get_meta_last_breakpoint_id()? + .checked_add(1) + .expect("Breakpoint Id will be lesser than u64::MAX"); + let block_to_break_id = next_breakpoint_id + .checked_mul(u64::from(BREAKPOINT_INTERVAL)) + .expect("Reached maximum breakpoint id"); + + if block_to_break_id <= last_block { + let next_breakpoint = self.calculate_state_for_id(block_to_break_id)?; + + self.put_breakpoint(next_breakpoint_id, &next_breakpoint)?; + self.put_meta_last_breakpoint_id(next_breakpoint_id) + } else { + Err(DbError::db_interaction_error( + "Breakpoint not yet achieved".to_owned(), + )) + } + } +} diff --git a/test_program_methods/guest/src/bin/burner.rs b/test_program_methods/guest/src/bin/burner.rs index a2256aa3..991091c0 100644 --- a/test_program_methods/guest/src/bin/burner.rs +++ b/test_program_methods/guest/src/bin/burner.rs @@ -1,4 +1,4 @@ -use nssa_core::program::{AccountPostState, ProgramInput, read_nssa_inputs, write_nssa_outputs}; +use nssa_core::program::{AccountPostState, ProgramInput, ProgramOutput, read_nssa_inputs}; type Instruction = u128; @@ -19,9 +19,10 @@ fn main() { let mut account_post = account_pre.clone(); account_post.balance = account_post.balance.saturating_sub(balance_to_burn); - write_nssa_outputs( + ProgramOutput::new( instruction_words, vec![pre], vec![AccountPostState::new(account_post)], - ); + ) + .write(); } diff --git a/test_program_methods/guest/src/bin/chain_caller.rs b/test_program_methods/guest/src/bin/chain_caller.rs index 7e67fa9b..98154258 100644 --- a/test_program_methods/guest/src/bin/chain_caller.rs +++ b/test_program_methods/guest/src/bin/chain_caller.rs @@ -1,6 +1,6 @@ use nssa_core::program::{ - AccountPostState, ChainedCall, PdaSeed, ProgramId, ProgramInput, read_nssa_inputs, - write_nssa_outputs_with_chained_call, + AccountPostState, ChainedCall, PdaSeed, ProgramId, ProgramInput, ProgramOutput, + read_nssa_inputs, }; use risc0_zkvm::serde::to_vec; @@ -54,13 +54,14 @@ fn main() { }; } - write_nssa_outputs_with_chained_call( + ProgramOutput::new( instruction_words, vec![sender_pre.clone(), recipient_pre.clone()], vec![ AccountPostState::new(sender_pre.account), AccountPostState::new(recipient_pre.account), ], - chained_calls, - ); + ) + .with_chained_calls(chained_calls) + .write(); } diff --git a/test_program_methods/guest/src/bin/changer_claimer.rs b/test_program_methods/guest/src/bin/changer_claimer.rs index 37079737..fb5505f7 100644 --- a/test_program_methods/guest/src/bin/changer_claimer.rs +++ b/test_program_methods/guest/src/bin/changer_claimer.rs @@ -1,4 +1,4 @@ -use nssa_core::program::{AccountPostState, ProgramInput, read_nssa_inputs, write_nssa_outputs}; +use nssa_core::program::{AccountPostState, ProgramInput, ProgramOutput, read_nssa_inputs}; type Instruction = (Option>, bool); @@ -33,5 +33,5 @@ fn main() { AccountPostState::new(account_post) }; - write_nssa_outputs(instruction_words, vec![pre], vec![post_state]); + ProgramOutput::new(instruction_words, vec![pre], vec![post_state]).write(); } diff --git a/test_program_methods/guest/src/bin/claimer.rs b/test_program_methods/guest/src/bin/claimer.rs index 897ca6a6..57e7e4e5 100644 --- a/test_program_methods/guest/src/bin/claimer.rs +++ b/test_program_methods/guest/src/bin/claimer.rs @@ -1,4 +1,4 @@ -use nssa_core::program::{AccountPostState, ProgramInput, read_nssa_inputs, write_nssa_outputs}; +use nssa_core::program::{AccountPostState, ProgramInput, ProgramOutput, read_nssa_inputs}; type Instruction = (); @@ -17,5 +17,5 @@ fn main() { let account_post = AccountPostState::new_claimed(pre.account.clone()); - write_nssa_outputs(instruction_words, vec![pre], vec![account_post]); + ProgramOutput::new(instruction_words, vec![pre], vec![account_post]).write(); } diff --git a/test_program_methods/guest/src/bin/data_changer.rs b/test_program_methods/guest/src/bin/data_changer.rs index c689dce5..55f4e2a0 100644 --- a/test_program_methods/guest/src/bin/data_changer.rs +++ b/test_program_methods/guest/src/bin/data_changer.rs @@ -1,4 +1,4 @@ -use nssa_core::program::{AccountPostState, ProgramInput, read_nssa_inputs, write_nssa_outputs}; +use nssa_core::program::{AccountPostState, ProgramInput, ProgramOutput, read_nssa_inputs}; type Instruction = Vec; @@ -22,9 +22,10 @@ fn main() { .try_into() .expect("provided data should fit into data limit"); - write_nssa_outputs( + ProgramOutput::new( instruction_words, vec![pre], vec![AccountPostState::new_claimed(account_post)], - ); + ) + .write(); } diff --git a/test_program_methods/guest/src/bin/extra_output.rs b/test_program_methods/guest/src/bin/extra_output.rs index 4d67df6e..3adc591c 100644 --- a/test_program_methods/guest/src/bin/extra_output.rs +++ b/test_program_methods/guest/src/bin/extra_output.rs @@ -1,6 +1,6 @@ use nssa_core::{ account::Account, - program::{AccountPostState, ProgramInput, read_nssa_inputs, write_nssa_outputs}, + program::{AccountPostState, ProgramInput, ProgramOutput, read_nssa_inputs}, }; type Instruction = (); @@ -14,12 +14,13 @@ fn main() { let account_pre = pre.account.clone(); - write_nssa_outputs( + ProgramOutput::new( instruction_words, vec![pre], vec![ AccountPostState::new(account_pre), AccountPostState::new(Account::default()), ], - ); + ) + .write(); } diff --git a/test_program_methods/guest/src/bin/malicious_authorization_changer.rs b/test_program_methods/guest/src/bin/malicious_authorization_changer.rs index 56ba7e72..7452d337 100644 --- a/test_program_methods/guest/src/bin/malicious_authorization_changer.rs +++ b/test_program_methods/guest/src/bin/malicious_authorization_changer.rs @@ -1,8 +1,7 @@ use nssa_core::{ account::AccountWithMetadata, program::{ - AccountPostState, ChainedCall, ProgramId, ProgramInput, read_nssa_inputs, - write_nssa_outputs_with_chained_call, + AccountPostState, ChainedCall, ProgramId, ProgramInput, ProgramOutput, read_nssa_inputs, }, }; use risc0_zkvm::serde::to_vec; @@ -40,13 +39,14 @@ fn main() { pda_seeds: vec![], }; - write_nssa_outputs_with_chained_call( + ProgramOutput::new( instruction_words, vec![sender.clone(), receiver.clone()], vec![ AccountPostState::new(sender.account), AccountPostState::new(receiver.account), ], - vec![chained_call], - ); + ) + .with_chained_calls(vec![chained_call]) + .write(); } diff --git a/test_program_methods/guest/src/bin/minter.rs b/test_program_methods/guest/src/bin/minter.rs index a602df56..ac29e4d3 100644 --- a/test_program_methods/guest/src/bin/minter.rs +++ b/test_program_methods/guest/src/bin/minter.rs @@ -1,4 +1,4 @@ -use nssa_core::program::{AccountPostState, ProgramInput, read_nssa_inputs, write_nssa_outputs}; +use nssa_core::program::{AccountPostState, ProgramInput, ProgramOutput, read_nssa_inputs}; type Instruction = (); @@ -16,9 +16,10 @@ fn main() { .checked_add(1) .expect("Balance overflow"); - write_nssa_outputs( + ProgramOutput::new( instruction_words, vec![pre], vec![AccountPostState::new(account_post)], - ); + ) + .write(); } diff --git a/test_program_methods/guest/src/bin/missing_output.rs b/test_program_methods/guest/src/bin/missing_output.rs index 52ca6e2f..b485e87a 100644 --- a/test_program_methods/guest/src/bin/missing_output.rs +++ b/test_program_methods/guest/src/bin/missing_output.rs @@ -1,4 +1,4 @@ -use nssa_core::program::{AccountPostState, ProgramInput, read_nssa_inputs, write_nssa_outputs}; +use nssa_core::program::{AccountPostState, ProgramInput, ProgramOutput, read_nssa_inputs}; type Instruction = (); @@ -11,9 +11,10 @@ fn main() { let account_pre1 = pre1.account.clone(); - write_nssa_outputs( + ProgramOutput::new( instruction_words, vec![pre1, pre2], vec![AccountPostState::new(account_pre1)], - ); + ) + .write(); } diff --git a/test_program_methods/guest/src/bin/modified_transfer.rs b/test_program_methods/guest/src/bin/modified_transfer.rs index 3aee3816..a89c72fb 100644 --- a/test_program_methods/guest/src/bin/modified_transfer.rs +++ b/test_program_methods/guest/src/bin/modified_transfer.rs @@ -5,7 +5,7 @@ use nssa_core::{ account::{Account, AccountWithMetadata}, - program::{AccountPostState, ProgramInput, read_nssa_inputs, write_nssa_outputs}, + program::{AccountPostState, ProgramInput, ProgramOutput, read_nssa_inputs}, }; /// Initializes a default account under the ownership of this program. @@ -80,5 +80,5 @@ fn main() { } _ => panic!("invalid params"), }; - write_nssa_outputs(instruction_data, pre_states, post_states); + ProgramOutput::new(instruction_data, pre_states, post_states).write(); } diff --git a/test_program_methods/guest/src/bin/nonce_changer.rs b/test_program_methods/guest/src/bin/nonce_changer.rs index 52d2e392..0cecdc81 100644 --- a/test_program_methods/guest/src/bin/nonce_changer.rs +++ b/test_program_methods/guest/src/bin/nonce_changer.rs @@ -1,4 +1,4 @@ -use nssa_core::program::{AccountPostState, ProgramInput, read_nssa_inputs, write_nssa_outputs}; +use nssa_core::program::{AccountPostState, ProgramInput, ProgramOutput, read_nssa_inputs}; type Instruction = (); @@ -13,9 +13,10 @@ fn main() { let mut account_post = account_pre.clone(); account_post.nonce.public_account_nonce_increment(); - write_nssa_outputs( + ProgramOutput::new( instruction_words, vec![pre], vec![AccountPostState::new(account_post)], - ); + ) + .write(); } diff --git a/test_program_methods/guest/src/bin/noop.rs b/test_program_methods/guest/src/bin/noop.rs index 79dd1dec..35a07765 100644 --- a/test_program_methods/guest/src/bin/noop.rs +++ b/test_program_methods/guest/src/bin/noop.rs @@ -1,4 +1,4 @@ -use nssa_core::program::{AccountPostState, ProgramInput, read_nssa_inputs, write_nssa_outputs}; +use nssa_core::program::{AccountPostState, ProgramInput, ProgramOutput, read_nssa_inputs}; type Instruction = (); @@ -9,5 +9,5 @@ fn main() { .iter() .map(|account| AccountPostState::new(account.account.clone())) .collect(); - write_nssa_outputs(instruction_words, pre_states, post_states); + ProgramOutput::new(instruction_words, pre_states, post_states).write(); } diff --git a/test_program_methods/guest/src/bin/program_owner_changer.rs b/test_program_methods/guest/src/bin/program_owner_changer.rs index 4b7de0f7..7e421351 100644 --- a/test_program_methods/guest/src/bin/program_owner_changer.rs +++ b/test_program_methods/guest/src/bin/program_owner_changer.rs @@ -1,4 +1,4 @@ -use nssa_core::program::{AccountPostState, ProgramInput, read_nssa_inputs, write_nssa_outputs}; +use nssa_core::program::{AccountPostState, ProgramInput, ProgramOutput, read_nssa_inputs}; type Instruction = (); @@ -13,9 +13,10 @@ fn main() { let mut account_post = account_pre.clone(); account_post.program_owner = [0, 1, 2, 3, 4, 5, 6, 7]; - write_nssa_outputs( + ProgramOutput::new( instruction_words, vec![pre], vec![AccountPostState::new(account_post)], - ); + ) + .write(); } diff --git a/test_program_methods/guest/src/bin/simple_balance_transfer.rs b/test_program_methods/guest/src/bin/simple_balance_transfer.rs index 55bbfcef..9ee715e8 100644 --- a/test_program_methods/guest/src/bin/simple_balance_transfer.rs +++ b/test_program_methods/guest/src/bin/simple_balance_transfer.rs @@ -1,4 +1,4 @@ -use nssa_core::program::{AccountPostState, ProgramInput, read_nssa_inputs, write_nssa_outputs}; +use nssa_core::program::{AccountPostState, ProgramInput, ProgramOutput, read_nssa_inputs}; type Instruction = u128; @@ -26,12 +26,13 @@ fn main() { .checked_add(balance) .expect("Overflow when adding balance"); - write_nssa_outputs( + ProgramOutput::new( instruction_words, vec![sender_pre, receiver_pre], vec![ AccountPostState::new(sender_post), AccountPostState::new(receiver_post), ], - ); + ) + .write(); } diff --git a/test_program_methods/guest/src/bin/validity_window.rs b/test_program_methods/guest/src/bin/validity_window.rs index f049e06b..00e8e5e8 100644 --- a/test_program_methods/guest/src/bin/validity_window.rs +++ b/test_program_methods/guest/src/bin/validity_window.rs @@ -1,19 +1,14 @@ use nssa_core::program::{ - AccountPostState, BlockId, ProgramInput, ProgramOutput, Timestamp, read_nssa_inputs, + AccountPostState, ProgramInput, ProgramOutput, ValidityWindow, read_nssa_inputs, }; -type Instruction = ( - Option, - Option, - Option, - Option, -); +type Instruction = ValidityWindow; fn main() { let ( ProgramInput { pre_states, - instruction: (from_id, until_id, from_ts, until_ts), + instruction: validity_window, }, instruction_words, ) = read_nssa_inputs::(); @@ -24,19 +19,11 @@ fn main() { let post = pre.account.clone(); - let output = ProgramOutput::new( + ProgramOutput::new( instruction_words, vec![pre], vec![AccountPostState::new(post)], ) - .valid_from_id(from_id) - .unwrap() - .valid_until_id(until_id) - .unwrap() - .valid_from_timestamp(from_ts) - .unwrap() - .valid_until_timestamp(until_ts) - .unwrap(); - - output.write(); + .with_validity_window(validity_window) + .write(); } diff --git a/test_program_methods/guest/src/bin/validity_window_chain_caller.rs b/test_program_methods/guest/src/bin/validity_window_chain_caller.rs new file mode 100644 index 00000000..cbd110dd --- /dev/null +++ b/test_program_methods/guest/src/bin/validity_window_chain_caller.rs @@ -0,0 +1,42 @@ +use nssa_core::program::{ + AccountPostState, ChainedCall, ProgramId, ProgramInput, ProgramOutput, ValidityWindow, + read_nssa_inputs, +}; +use risc0_zkvm::serde::to_vec; + +/// A program that sets a validity window on its output and chains to another program with a +/// potentially different validity window. +/// +/// Instruction: (`window`, `chained_program_id`, `chained_window`) +/// The initial output uses `window` and chains to `chained_program_id` with `chained_window`. +type Instruction = (ValidityWindow, ProgramId, ValidityWindow); + +fn main() { + let ( + ProgramInput { + pre_states, + instruction: (validity_window, chained_program_id, chained_validity_window), + }, + instruction_words, + ) = read_nssa_inputs::(); + + let [pre] = <[_; 1]>::try_from(pre_states.clone()).expect("Expected exactly one pre state"); + let post = pre.account.clone(); + + let chained_instruction = to_vec(&chained_validity_window).unwrap(); + let chained_call = ChainedCall { + program_id: chained_program_id, + instruction_data: chained_instruction, + pre_states, + pda_seeds: vec![], + }; + + ProgramOutput::new( + instruction_words, + vec![pre], + vec![AccountPostState::new(post)], + ) + .with_validity_window(validity_window) + .with_chained_calls(vec![chained_call]) + .write(); +} diff --git a/testnet_initial_state/Cargo.toml b/testnet_initial_state/Cargo.toml new file mode 100644 index 00000000..2b73f479 --- /dev/null +++ b/testnet_initial_state/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "testnet_initial_state" +version = "0.1.0" +edition = "2024" +license.workspace = true + +[dependencies] +key_protocol.workspace = true +nssa.workspace = true +nssa_core.workspace = true +common.workspace = true + +serde.workspace = true + +[lints] +workspace = true diff --git a/testnet_initial_state/src/lib.rs b/testnet_initial_state/src/lib.rs new file mode 100644 index 00000000..6224d71e --- /dev/null +++ b/testnet_initial_state/src/lib.rs @@ -0,0 +1,396 @@ +use common::PINATA_BASE58; +use key_protocol::key_management::{ + KeyChain, + secret_holders::{PrivateKeyHolder, SecretSpendingKey}, +}; +use nssa::{Account, AccountId, Data, PrivateKey, PublicKey, V03State}; +use nssa_core::{NullifierPublicKey, encryption::shared_key_derivation::Secp256k1Point}; +use serde::{Deserialize, Serialize}; + +const PRIVATE_KEY_PUB_ACC_A: [u8; 32] = [ + 16, 162, 106, 154, 236, 125, 52, 184, 35, 100, 238, 174, 69, 197, 41, 77, 187, 10, 118, 75, 0, + 11, 148, 238, 185, 181, 133, 17, 220, 72, 124, 77, +]; + +const PRIVATE_KEY_PUB_ACC_B: [u8; 32] = [ + 113, 121, 64, 177, 204, 85, 229, 214, 178, 6, 109, 191, 29, 154, 63, 38, 242, 18, 244, 219, 8, + 208, 35, 136, 23, 127, 207, 237, 216, 169, 190, 27, +]; + +const SSK_PRIV_ACC_A: [u8; 32] = [ + 93, 13, 190, 240, 250, 33, 108, 195, 176, 40, 144, 61, 4, 28, 58, 112, 53, 161, 42, 238, 155, + 27, 23, 176, 208, 121, 15, 229, 165, 180, 99, 143, +]; + +const SSK_PRIV_ACC_B: [u8; 32] = [ + 48, 175, 124, 10, 230, 240, 166, 14, 249, 254, 157, 226, 208, 124, 122, 177, 203, 139, 192, + 180, 43, 120, 55, 151, 50, 21, 113, 22, 254, 83, 148, 56, +]; + +const NSK_PRIV_ACC_A: [u8; 32] = [ + 25, 21, 186, 59, 180, 224, 101, 64, 163, 208, 228, 43, 13, 185, 100, 123, 156, 47, 80, 179, 72, + 51, 115, 11, 180, 99, 21, 201, 48, 194, 118, 144, +]; + +const NSK_PRIV_ACC_B: [u8; 32] = [ + 99, 82, 190, 140, 234, 10, 61, 163, 15, 211, 179, 54, 70, 166, 87, 5, 182, 68, 117, 244, 217, + 23, 99, 9, 4, 177, 230, 125, 109, 91, 160, 30, +]; + +const VSK_PRIV_ACC_A: [u8; 32] = [ + 5, 85, 114, 119, 141, 187, 202, 170, 122, 253, 198, 81, 150, 8, 155, 21, 192, 65, 24, 124, 116, + 98, 110, 106, 137, 90, 165, 239, 80, 13, 222, 30, +]; + +const VSK_PRIV_ACC_B: [u8; 32] = [ + 205, 32, 76, 251, 255, 236, 96, 119, 61, 111, 65, 100, 75, 218, 12, 22, 17, 170, 55, 226, 21, + 154, 161, 34, 208, 74, 27, 1, 119, 13, 88, 128, +]; + +const VPK_PRIV_ACC_A: [u8; 33] = [ + 2, 210, 206, 38, 213, 4, 182, 198, 220, 47, 93, 148, 61, 84, 148, 250, 158, 45, 8, 81, 48, 80, + 46, 230, 87, 210, 47, 204, 76, 58, 214, 167, 81, +]; + +const VPK_PRIV_ACC_B: [u8; 33] = [ + 2, 79, 110, 46, 203, 29, 206, 205, 18, 86, 27, 189, 104, 103, 113, 181, 110, 53, 78, 172, 11, + 171, 190, 18, 126, 214, 81, 77, 192, 154, 58, 195, 238, +]; + +const NPK_PRIV_ACC_A: [u8; 32] = [ + 167, 108, 50, 153, 74, 47, 151, 188, 140, 79, 195, 31, 181, 9, 40, 167, 201, 32, 175, 129, 45, + 245, 223, 193, 210, 170, 247, 128, 167, 140, 155, 129, +]; + +const NPK_PRIV_ACC_B: [u8; 32] = [ + 32, 67, 72, 164, 106, 53, 66, 239, 141, 15, 52, 230, 136, 177, 2, 236, 207, 243, 134, 135, 210, + 143, 87, 232, 215, 128, 194, 120, 113, 224, 4, 165, +]; + +const DEFAULT_PROGRAM_OWNER: [u32; 8] = [0, 0, 0, 0, 0, 0, 0, 0]; + +const PUB_ACC_A_INITIAL_BALANCE: u128 = 10000; +const PUB_ACC_B_INITIAL_BALANCE: u128 = 20000; + +const PRIV_ACC_A_INITIAL_BALANCE: u128 = 10000; +const PRIV_ACC_B_INITIAL_BALANCE: u128 = 20000; + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +pub struct PublicAccountPublicInitialData { + pub account_id: AccountId, + pub balance: u128, +} + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +pub struct PrivateAccountPublicInitialData { + pub npk: nssa_core::NullifierPublicKey, + pub account: nssa_core::account::Account, +} + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +pub struct PublicAccountPrivateInitialData { + pub account_id: nssa::AccountId, + pub pub_sign_key: nssa::PrivateKey, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct PrivateAccountPrivateInitialData { + pub account_id: nssa::AccountId, + pub account: nssa_core::account::Account, + pub key_chain: KeyChain, +} + +#[must_use] +pub fn initial_pub_accounts_private_keys() -> Vec { + let acc1_pub_sign_key = PrivateKey::try_new(PRIVATE_KEY_PUB_ACC_A).unwrap(); + + let acc2_pub_sign_key = PrivateKey::try_new(PRIVATE_KEY_PUB_ACC_B).unwrap(); + + vec![ + PublicAccountPrivateInitialData { + account_id: AccountId::from(&PublicKey::new_from_private_key(&acc1_pub_sign_key)), + pub_sign_key: acc1_pub_sign_key, + }, + PublicAccountPrivateInitialData { + account_id: AccountId::from(&PublicKey::new_from_private_key(&acc2_pub_sign_key)), + pub_sign_key: acc2_pub_sign_key, + }, + ] +} + +#[must_use] +pub fn initial_priv_accounts_private_keys() -> Vec { + let key_chain_1 = KeyChain { + secret_spending_key: SecretSpendingKey(SSK_PRIV_ACC_A), + private_key_holder: PrivateKeyHolder { + nullifier_secret_key: NSK_PRIV_ACC_A, + viewing_secret_key: VSK_PRIV_ACC_A, + }, + nullifier_public_key: NullifierPublicKey(NPK_PRIV_ACC_A), + viewing_public_key: Secp256k1Point(VPK_PRIV_ACC_A.to_vec()), + }; + + let key_chain_2 = KeyChain { + secret_spending_key: SecretSpendingKey(SSK_PRIV_ACC_B), + private_key_holder: PrivateKeyHolder { + nullifier_secret_key: NSK_PRIV_ACC_B, + viewing_secret_key: VSK_PRIV_ACC_B, + }, + nullifier_public_key: NullifierPublicKey(NPK_PRIV_ACC_B), + viewing_public_key: Secp256k1Point(VPK_PRIV_ACC_B.to_vec()), + }; + + vec![ + PrivateAccountPrivateInitialData { + account_id: AccountId::from(&key_chain_1.nullifier_public_key), + account: Account { + program_owner: DEFAULT_PROGRAM_OWNER, + balance: PRIV_ACC_A_INITIAL_BALANCE, + data: Data::default(), + nonce: 0.into(), + }, + key_chain: key_chain_1, + }, + PrivateAccountPrivateInitialData { + account_id: AccountId::from(&key_chain_2.nullifier_public_key), + account: Account { + program_owner: DEFAULT_PROGRAM_OWNER, + balance: PRIV_ACC_B_INITIAL_BALANCE, + data: Data::default(), + nonce: 0.into(), + }, + key_chain: key_chain_2, + }, + ] +} + +#[must_use] +pub fn initial_commitments() -> Vec { + initial_priv_accounts_private_keys() + .into_iter() + .map(|data| PrivateAccountPublicInitialData { + npk: data.key_chain.nullifier_public_key.clone(), + account: data.account, + }) + .collect() +} + +#[must_use] +pub fn initial_accounts() -> Vec { + let initial_account_ids = initial_pub_accounts_private_keys() + .into_iter() + .map(|data| data.account_id) + .collect::>(); + + vec![ + PublicAccountPublicInitialData { + account_id: initial_account_ids[0], + balance: PUB_ACC_A_INITIAL_BALANCE, + }, + PublicAccountPublicInitialData { + account_id: initial_account_ids[1], + balance: PUB_ACC_B_INITIAL_BALANCE, + }, + ] +} + +#[must_use] +pub fn initial_state() -> V03State { + let initial_commitments: Vec = initial_commitments() + .iter() + .map(|init_comm_data| { + let npk = &init_comm_data.npk; + + let mut acc = init_comm_data.account.clone(); + + acc.program_owner = nssa::program::Program::authenticated_transfer_program().id(); + + nssa_core::Commitment::new(npk, &acc) + }) + .collect(); + + let init_accs: Vec<(nssa::AccountId, u128)> = initial_accounts() + .iter() + .map(|acc_data| (acc_data.account_id, acc_data.balance)) + .collect(); + + nssa::V03State::new_with_genesis_accounts(&init_accs, &initial_commitments) +} + +#[must_use] +pub fn initial_state_testnet() -> V03State { + let mut state = initial_state(); + + state.add_pinata_program(PINATA_BASE58.parse().unwrap()); + + state +} + +#[cfg(test)] +mod tests { + use std::str::FromStr as _; + + use super::*; + + const PUB_ACC_A_TEXT_ADDR: &str = "6iArKUXxhUJqS7kCaPNhwMWt3ro71PDyBj7jwAyE2VQV"; + const PUB_ACC_B_TEXT_ADDR: &str = "7wHg9sbJwc6h3NP1S9bekfAzB8CHifEcxKswCKUt3YQo"; + + const PRIV_ACC_A_TEXT_ADDR: &str = "5ya25h4Xc9GAmrGB2WrTEnEWtQKJwRwQx3Xfo2tucNcE"; + const PRIV_ACC_B_TEXT_ADDR: &str = "E8HwiTyQe4H9HK7icTvn95HQMnzx49mP9A2ddtMLpNaN"; + + #[test] + fn pub_state_consistency() { + let init_accs_private_data = initial_pub_accounts_private_keys(); + let init_accs_pub_data = initial_accounts(); + + assert_eq!( + init_accs_private_data[0].account_id, + init_accs_pub_data[0].account_id + ); + + assert_eq!( + init_accs_private_data[1].account_id, + init_accs_pub_data[1].account_id + ); + + assert_eq!( + init_accs_pub_data[0], + PublicAccountPublicInitialData { + account_id: AccountId::from_str(PUB_ACC_A_TEXT_ADDR).unwrap(), + balance: PUB_ACC_A_INITIAL_BALANCE, + } + ); + + assert_eq!( + init_accs_pub_data[1], + PublicAccountPublicInitialData { + account_id: AccountId::from_str(PUB_ACC_B_TEXT_ADDR).unwrap(), + balance: PUB_ACC_B_INITIAL_BALANCE, + } + ); + } + + #[test] + fn private_state_consistency() { + let init_private_accs_keys = initial_priv_accounts_private_keys(); + let init_comms = initial_commitments(); + + assert_eq!( + init_private_accs_keys[0] + .key_chain + .secret_spending_key + .produce_private_key_holder(None) + .nullifier_secret_key, + init_private_accs_keys[0] + .key_chain + .private_key_holder + .nullifier_secret_key + ); + assert_eq!( + init_private_accs_keys[0] + .key_chain + .secret_spending_key + .produce_private_key_holder(None) + .viewing_secret_key, + init_private_accs_keys[0] + .key_chain + .private_key_holder + .viewing_secret_key + ); + assert_eq!( + init_private_accs_keys[0] + .key_chain + .private_key_holder + .generate_nullifier_public_key(), + init_private_accs_keys[0].key_chain.nullifier_public_key + ); + assert_eq!( + init_private_accs_keys[0] + .key_chain + .private_key_holder + .generate_viewing_public_key(), + init_private_accs_keys[0].key_chain.viewing_public_key + ); + + assert_eq!( + init_private_accs_keys[1] + .key_chain + .secret_spending_key + .produce_private_key_holder(None) + .nullifier_secret_key, + init_private_accs_keys[1] + .key_chain + .private_key_holder + .nullifier_secret_key + ); + assert_eq!( + init_private_accs_keys[1] + .key_chain + .secret_spending_key + .produce_private_key_holder(None) + .viewing_secret_key, + init_private_accs_keys[1] + .key_chain + .private_key_holder + .viewing_secret_key + ); + assert_eq!( + init_private_accs_keys[1] + .key_chain + .private_key_holder + .generate_nullifier_public_key(), + init_private_accs_keys[1].key_chain.nullifier_public_key + ); + assert_eq!( + init_private_accs_keys[1] + .key_chain + .private_key_holder + .generate_viewing_public_key(), + init_private_accs_keys[1].key_chain.viewing_public_key + ); + + assert_eq!( + init_private_accs_keys[0].account_id.to_string(), + PRIV_ACC_A_TEXT_ADDR + ); + assert_eq!( + init_private_accs_keys[1].account_id.to_string(), + PRIV_ACC_B_TEXT_ADDR + ); + + assert_eq!( + init_private_accs_keys[0].key_chain.nullifier_public_key, + init_comms[0].npk + ); + assert_eq!( + init_private_accs_keys[1].key_chain.nullifier_public_key, + init_comms[1].npk + ); + + assert_eq!( + init_comms[0], + PrivateAccountPublicInitialData { + npk: NullifierPublicKey(NPK_PRIV_ACC_A), + account: Account { + program_owner: DEFAULT_PROGRAM_OWNER, + balance: PRIV_ACC_A_INITIAL_BALANCE, + data: Data::default(), + nonce: 0.into(), + }, + } + ); + + assert_eq!( + init_comms[1], + PrivateAccountPublicInitialData { + npk: NullifierPublicKey(NPK_PRIV_ACC_B), + account: Account { + program_owner: DEFAULT_PROGRAM_OWNER, + balance: PRIV_ACC_B_INITIAL_BALANCE, + data: Data::default(), + nonce: 0.into(), + }, + } + ); + } +} diff --git a/wallet/Cargo.toml b/wallet/Cargo.toml index 2e628790..f77988a0 100644 --- a/wallet/Cargo.toml +++ b/wallet/Cargo.toml @@ -15,6 +15,8 @@ key_protocol.workspace = true sequencer_service_rpc = { workspace = true, features = ["client"] } token_core.workspace = true amm_core.workspace = true +testnet_initial_state.workspace = true +ata_core.workspace = true anyhow.workspace = true thiserror.workspace = true diff --git a/wallet/src/chain_storage.rs b/wallet/src/chain_storage.rs index a88e0b8b..ebfe9896 100644 --- a/wallet/src/chain_storage.rs +++ b/wallet/src/chain_storage.rs @@ -99,16 +99,22 @@ impl WalletChainStore { let mut public_init_acc_map = BTreeMap::new(); let mut private_init_acc_map = BTreeMap::new(); - for init_acc_data in config.initial_accounts.clone() { + let initial_accounts = config + .initial_accounts + .clone() + .unwrap_or_else(InitialAccountData::create_initial_accounts_data); + + for init_acc_data in initial_accounts { match init_acc_data { InitialAccountData::Public(data) => { public_init_acc_map.insert(data.account_id, data.pub_sign_key); } InitialAccountData::Private(data) => { let mut account = data.account; - // TODO: Program owner is only known after code is compiled and can't be set in - // the config. Therefore we overwrite it here on startup. Fix this when program - // id can be fetched from the node and queried from the wallet. + // TODO: Program owner is only known after code is compiled and can't be set + // in the config. Therefore we overwrite it here on + // startup. Fix this when program id can be fetched + // from the node and queried from the wallet. account.program_owner = Program::authenticated_transfer_program().id(); private_init_acc_map.insert(data.account_id, (data.key_chain, account)); } @@ -161,45 +167,12 @@ impl WalletChainStore { #[cfg(test)] mod tests { - use std::str::FromStr as _; - use key_protocol::key_management::key_tree::{ keys_private::ChildKeysPrivate, keys_public::ChildKeysPublic, traits::KeyNode as _, }; - use nssa::PrivateKey; use super::*; - use crate::config::{ - InitialAccountData, InitialAccountDataPublic, PersistentAccountDataPrivate, - PersistentAccountDataPublic, - }; - - fn create_initial_accounts() -> Vec { - vec![ - InitialAccountData::Public(InitialAccountDataPublic { - account_id: nssa::AccountId::from_str( - "CbgR6tj5kWx5oziiFptM7jMvrQeYY3Mzaao6ciuhSr2r", - ) - .unwrap(), - pub_sign_key: PrivateKey::try_new([ - 127, 39, 48, 152, 242, 91, 113, 230, 192, 5, 169, 81, 159, 38, 120, 218, 141, - 28, 127, 1, 246, 162, 119, 120, 226, 217, 148, 138, 189, 249, 1, 251, - ]) - .unwrap(), - }), - InitialAccountData::Public(InitialAccountDataPublic { - account_id: nssa::AccountId::from_str( - "2RHZhw9h534Zr3eq2RGhQete2Hh667foECzXPmSkGni2", - ) - .unwrap(), - pub_sign_key: PrivateKey::try_new([ - 244, 52, 248, 116, 23, 32, 1, 69, 134, 174, 67, 53, 109, 42, 236, 98, 87, 218, - 8, 98, 34, 246, 4, 221, 183, 93, 105, 115, 59, 134, 252, 76, - ]) - .unwrap(), - }), - ] - } + use crate::config::{PersistentAccountDataPrivate, PersistentAccountDataPublic}; fn create_sample_wallet_config() -> WalletConfig { WalletConfig { @@ -208,8 +181,8 @@ mod tests { seq_tx_poll_max_blocks: 5, seq_poll_max_retries: 10, seq_block_poll_max_amount: 100, - initial_accounts: create_initial_accounts(), basic_auth: None, + initial_accounts: None, } } diff --git a/wallet/src/cli/config.rs b/wallet/src/cli/config.rs index c9a5796f..7f0ba952 100644 --- a/wallet/src/cli/config.rs +++ b/wallet/src/cli/config.rs @@ -4,6 +4,7 @@ use clap::Subcommand; use crate::{ WalletCore, cli::{SubcommandReturnValue, WalletSubcommand}, + config::InitialAccountData, }; /// Represents generic config CLI subcommand. @@ -59,7 +60,17 @@ impl WalletSubcommand for ConfigSubcommand { ); } "initial_accounts" => { - println!("{:#?}", wallet_core.storage.wallet_config.initial_accounts); + println!( + "{:#?}", + wallet_core + .storage + .wallet_config + .initial_accounts + .clone() + .unwrap_or_else( + InitialAccountData::create_initial_accounts_data + ) + ); } "basic_auth" => { if let Some(basic_auth) = &wallet_core.storage.wallet_config.basic_auth diff --git a/wallet/src/cli/mod.rs b/wallet/src/cli/mod.rs index 85e792cc..6463dee8 100644 --- a/wallet/src/cli/mod.rs +++ b/wallet/src/cli/mod.rs @@ -14,8 +14,9 @@ use crate::{ chain::ChainSubcommand, config::ConfigSubcommand, programs::{ - amm::AmmProgramAgnosticSubcommand, native_token_transfer::AuthTransferSubcommand, - pinata::PinataProgramAgnosticSubcommand, token::TokenProgramAgnosticSubcommand, + amm::AmmProgramAgnosticSubcommand, ata::AtaSubcommand, + native_token_transfer::AuthTransferSubcommand, pinata::PinataProgramAgnosticSubcommand, + token::TokenProgramAgnosticSubcommand, }, }, }; @@ -52,6 +53,9 @@ pub enum Command { /// AMM program interaction subcommand. #[command(subcommand)] AMM(AmmProgramAgnosticSubcommand), + /// Associated Token Account program interaction subcommand. + #[command(subcommand)] + Ata(AtaSubcommand), /// Check the wallet can connect to the node and builtin local programs /// match the remote versions. CheckHealth, @@ -158,6 +162,7 @@ pub async fn execute_subcommand( } Command::Token(token_subcommand) => token_subcommand.handle_subcommand(wallet_core).await?, Command::AMM(amm_subcommand) => amm_subcommand.handle_subcommand(wallet_core).await?, + Command::Ata(ata_subcommand) => ata_subcommand.handle_subcommand(wallet_core).await?, Command::Config(config_subcommand) => { config_subcommand.handle_subcommand(wallet_core).await? } diff --git a/wallet/src/cli/programs/ata.rs b/wallet/src/cli/programs/ata.rs new file mode 100644 index 00000000..1a63fa67 --- /dev/null +++ b/wallet/src/cli/programs/ata.rs @@ -0,0 +1,240 @@ +use anyhow::Result; +use clap::Subcommand; +use common::transaction::NSSATransaction; +use nssa::{Account, AccountId, program::Program}; +use token_core::TokenHolding; + +use crate::{ + AccDecodeData::Decode, + WalletCore, + cli::{SubcommandReturnValue, WalletSubcommand}, + helperfunctions::{AccountPrivacyKind, parse_addr_with_privacy_prefix}, + program_facades::ata::Ata, +}; + +/// Represents generic CLI subcommand for a wallet working with the ATA program. +#[derive(Subcommand, Debug, Clone)] +pub enum AtaSubcommand { + /// Derive and print the Associated Token Account address (local only, no network). + Address { + /// Owner account - valid 32 byte base58 string (no privacy prefix). + #[arg(long)] + owner: String, + /// Token definition account - valid 32 byte base58 string (no privacy prefix). + #[arg(long)] + token_definition: String, + }, + /// Create (or idempotently no-op) the Associated Token Account. + Create { + /// Owner account - valid 32 byte base58 string with privacy prefix. + #[arg(long)] + owner: String, + /// Token definition account - valid 32 byte base58 string WITHOUT privacy prefix. + #[arg(long)] + token_definition: String, + }, + /// Send tokens from owner's ATA to a recipient token holding account. + Send { + /// Sender account - valid 32 byte base58 string with privacy prefix. + #[arg(long)] + from: String, + /// Token definition account - valid 32 byte base58 string WITHOUT privacy prefix. + #[arg(long)] + token_definition: String, + /// Recipient account - valid 32 byte base58 string WITHOUT privacy prefix. + #[arg(long)] + to: String, + #[arg(long)] + amount: u128, + }, + /// Burn tokens from holder's ATA. + Burn { + /// Holder account - valid 32 byte base58 string with privacy prefix. + #[arg(long)] + holder: String, + /// Token definition account - valid 32 byte base58 string WITHOUT privacy prefix. + #[arg(long)] + token_definition: String, + #[arg(long)] + amount: u128, + }, + /// List all ATAs for a given owner across multiple token definitions. + List { + /// Owner account - valid 32 byte base58 string (no privacy prefix). + #[arg(long)] + owner: String, + /// Token definition accounts - valid 32 byte base58 strings (no privacy prefix). + #[arg(long, num_args = 1..)] + token_definition: Vec, + }, +} + +impl WalletSubcommand for AtaSubcommand { + async fn handle_subcommand( + self, + wallet_core: &mut WalletCore, + ) -> Result { + match self { + Self::Address { + owner, + token_definition, + } => { + let owner_id: AccountId = owner.parse()?; + let definition_id: AccountId = token_definition.parse()?; + let ata_program_id = Program::ata().id(); + let ata_id = ata_core::get_associated_token_account_id( + &ata_program_id, + &ata_core::compute_ata_seed(owner_id, definition_id), + ); + println!("{ata_id}"); + Ok(SubcommandReturnValue::Empty) + } + Self::Create { + owner, + token_definition, + } => { + let (owner_str, owner_privacy) = parse_addr_with_privacy_prefix(&owner)?; + let owner_id: AccountId = owner_str.parse()?; + let definition_id: AccountId = token_definition.parse()?; + + match owner_privacy { + AccountPrivacyKind::Public => { + Ata(wallet_core) + .send_create(owner_id, definition_id) + .await?; + Ok(SubcommandReturnValue::Empty) + } + AccountPrivacyKind::Private => { + let (tx_hash, secret) = Ata(wallet_core) + .send_create_private_owner(owner_id, definition_id) + .await?; + + println!("Transaction hash is {tx_hash}"); + + let tx = wallet_core.poll_native_token_transfer(tx_hash).await?; + if let NSSATransaction::PrivacyPreserving(tx) = tx { + wallet_core.decode_insert_privacy_preserving_transaction_results( + &tx, + &[Decode(secret, owner_id)], + )?; + } + + wallet_core.store_persistent_data().await?; + Ok(SubcommandReturnValue::Empty) + } + } + } + Self::Send { + from, + token_definition, + to, + amount, + } => { + let (from_str, from_privacy) = parse_addr_with_privacy_prefix(&from)?; + let from_id: AccountId = from_str.parse()?; + let definition_id: AccountId = token_definition.parse()?; + let to_id: AccountId = to.parse()?; + + match from_privacy { + AccountPrivacyKind::Public => { + Ata(wallet_core) + .send_transfer(from_id, definition_id, to_id, amount) + .await?; + Ok(SubcommandReturnValue::Empty) + } + AccountPrivacyKind::Private => { + let (tx_hash, secret) = Ata(wallet_core) + .send_transfer_private_owner(from_id, definition_id, to_id, amount) + .await?; + + println!("Transaction hash is {tx_hash}"); + + let tx = wallet_core.poll_native_token_transfer(tx_hash).await?; + if let NSSATransaction::PrivacyPreserving(tx) = tx { + wallet_core.decode_insert_privacy_preserving_transaction_results( + &tx, + &[Decode(secret, from_id)], + )?; + } + + wallet_core.store_persistent_data().await?; + Ok(SubcommandReturnValue::Empty) + } + } + } + Self::Burn { + holder, + token_definition, + amount, + } => { + let (holder_str, holder_privacy) = parse_addr_with_privacy_prefix(&holder)?; + let holder_id: AccountId = holder_str.parse()?; + let definition_id: AccountId = token_definition.parse()?; + + match holder_privacy { + AccountPrivacyKind::Public => { + Ata(wallet_core) + .send_burn(holder_id, definition_id, amount) + .await?; + Ok(SubcommandReturnValue::Empty) + } + AccountPrivacyKind::Private => { + let (tx_hash, secret) = Ata(wallet_core) + .send_burn_private_owner(holder_id, definition_id, amount) + .await?; + + println!("Transaction hash is {tx_hash}"); + + let tx = wallet_core.poll_native_token_transfer(tx_hash).await?; + if let NSSATransaction::PrivacyPreserving(tx) = tx { + wallet_core.decode_insert_privacy_preserving_transaction_results( + &tx, + &[Decode(secret, holder_id)], + )?; + } + + wallet_core.store_persistent_data().await?; + Ok(SubcommandReturnValue::Empty) + } + } + } + Self::List { + owner, + token_definition, + } => { + let owner_id: AccountId = owner.parse()?; + let ata_program_id = Program::ata().id(); + + for def in &token_definition { + let definition_id: AccountId = def.parse()?; + let ata_id = ata_core::get_associated_token_account_id( + &ata_program_id, + &ata_core::compute_ata_seed(owner_id, definition_id), + ); + let account = wallet_core.get_account_public(ata_id).await?; + + if account == Account::default() { + println!("No ATA for definition {definition_id}"); + } else { + let holding = TokenHolding::try_from(&account.data)?; + match holding { + TokenHolding::Fungible { balance, .. } => { + println!( + "ATA {ata_id} (definition {definition_id}): balance {balance}" + ); + } + TokenHolding::NftMaster { .. } + | TokenHolding::NftPrintedCopy { .. } => { + println!( + "ATA {ata_id} (definition {definition_id}): unsupported token type" + ); + } + } + } + } + + Ok(SubcommandReturnValue::Empty) + } + } + } +} diff --git a/wallet/src/cli/programs/mod.rs b/wallet/src/cli/programs/mod.rs index 96a4e766..f6e4b5dc 100644 --- a/wallet/src/cli/programs/mod.rs +++ b/wallet/src/cli/programs/mod.rs @@ -1,4 +1,5 @@ pub mod amm; +pub mod ata; pub mod native_token_transfer; pub mod pinata; pub mod token; diff --git a/wallet/src/config.rs b/wallet/src/config.rs index 60164e86..33527009 100644 --- a/wallet/src/config.rs +++ b/wallet/src/config.rs @@ -8,22 +8,17 @@ use std::{ use anyhow::{Context as _, Result}; use common::config::BasicAuth; use humantime_serde; -use key_protocol::key_management::{ - KeyChain, - key_tree::{ - chain_index::ChainIndex, keys_private::ChildKeysPrivate, keys_public::ChildKeysPublic, - }, +use key_protocol::key_management::key_tree::{ + chain_index::ChainIndex, keys_private::ChildKeysPrivate, keys_public::ChildKeysPublic, }; use log::warn; use serde::{Deserialize, Serialize}; +use testnet_initial_state::{ + PrivateAccountPrivateInitialData, PublicAccountPrivateInitialData, + initial_priv_accounts_private_keys, initial_pub_accounts_private_keys, +}; use url::Url; -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct InitialAccountDataPublic { - pub account_id: nssa::AccountId, - pub pub_sign_key: nssa::PrivateKey, -} - #[derive(Debug, Clone, Serialize, Deserialize)] pub struct PersistentAccountDataPublic { pub account_id: nssa::AccountId, @@ -31,13 +26,6 @@ pub struct PersistentAccountDataPublic { pub data: ChildKeysPublic, } -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct InitialAccountDataPrivate { - pub account_id: nssa::AccountId, - pub account: nssa_core::account::Account, - pub key_chain: KeyChain, -} - #[derive(Debug, Clone, Serialize, Deserialize)] pub struct PersistentAccountDataPrivate { pub account_id: nssa::AccountId, @@ -50,8 +38,29 @@ pub struct PersistentAccountDataPrivate { // memory #[derive(Debug, Clone, Serialize, Deserialize)] pub enum InitialAccountData { - Public(InitialAccountDataPublic), - Private(Box), + Public(PublicAccountPrivateInitialData), + Private(Box), +} + +impl InitialAccountData { + #[must_use] + pub const fn account_id(&self) -> nssa::AccountId { + match &self { + Self::Public(acc) => acc.account_id, + Self::Private(acc) => acc.account_id, + } + } + + pub(crate) fn create_initial_accounts_data() -> Vec { + let pub_data = initial_pub_accounts_private_keys(); + let priv_data = initial_priv_accounts_private_keys(); + + pub_data + .into_iter() + .map(Into::into) + .chain(priv_data.into_iter().map(Into::into)) + .collect() + } } // Big difference in enum variants sizes @@ -114,16 +123,6 @@ impl PersistentStorage { } } -impl InitialAccountData { - #[must_use] - pub fn account_id(&self) -> nssa::AccountId { - match &self { - Self::Public(acc) => acc.account_id, - Self::Private(acc) => acc.account_id, - } - } -} - impl PersistentAccountData { #[must_use] pub fn account_id(&self) -> nssa::AccountId { @@ -135,14 +134,14 @@ impl PersistentAccountData { } } -impl From for InitialAccountData { - fn from(value: InitialAccountDataPublic) -> Self { +impl From for InitialAccountData { + fn from(value: PublicAccountPrivateInitialData) -> Self { Self::Public(value) } } -impl From for InitialAccountData { - fn from(value: InitialAccountDataPrivate) -> Self { +impl From for InitialAccountData { + fn from(value: PrivateAccountPrivateInitialData) -> Self { Self::Private(Box::new(value)) } } @@ -197,37 +196,15 @@ pub struct WalletConfig { pub seq_poll_max_retries: u64, /// Max amount of blocks to poll in one request. pub seq_block_poll_max_amount: u64, - /// Initial accounts for wallet. - pub initial_accounts: Vec, - /// Basic authentication credentials. + /// Basic authentication credentials #[serde(skip_serializing_if = "Option::is_none")] pub basic_auth: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub initial_accounts: Option>, } impl Default for WalletConfig { fn default() -> Self { - let pub_sign_key1 = nssa::PrivateKey::try_new([ - 127, 39, 48, 152, 242, 91, 113, 230, 192, 5, 169, 81, 159, 38, 120, 218, 141, 28, 127, - 1, 246, 162, 119, 120, 226, 217, 148, 138, 189, 249, 1, 251, - ]) - .unwrap(); - let public_key1 = nssa::PublicKey::new_from_private_key(&pub_sign_key1); - let public_account_id1 = nssa::AccountId::from(&public_key1); - - let pub_sign_key2 = nssa::PrivateKey::try_new([ - 244, 52, 248, 116, 23, 32, 1, 69, 134, 174, 67, 53, 109, 42, 236, 98, 87, 218, 8, 98, - 34, 246, 4, 221, 183, 93, 105, 115, 59, 134, 252, 76, - ]) - .unwrap(); - let public_key2 = nssa::PublicKey::new_from_private_key(&pub_sign_key2); - let public_account_id2 = nssa::AccountId::from(&public_key2); - - let key_chain1 = KeyChain::new_mnemonic("default_private_account_1".to_owned()); - let private_account_id1 = nssa::AccountId::from(&key_chain1.nullifier_public_key); - - let key_chain2 = KeyChain::new_mnemonic("default_private_account_2".to_owned()); - let private_account_id2 = nssa::AccountId::from(&key_chain2.nullifier_public_key); - Self { sequencer_addr: "http://127.0.0.1:3040".parse().unwrap(), seq_poll_timeout: Duration::from_secs(12), @@ -235,32 +212,7 @@ impl Default for WalletConfig { seq_poll_max_retries: 5, seq_block_poll_max_amount: 100, basic_auth: None, - initial_accounts: vec![ - InitialAccountData::Public(InitialAccountDataPublic { - account_id: public_account_id1, - pub_sign_key: pub_sign_key1, - }), - InitialAccountData::Public(InitialAccountDataPublic { - account_id: public_account_id2, - pub_sign_key: pub_sign_key2, - }), - InitialAccountData::Private(Box::new(InitialAccountDataPrivate { - account_id: private_account_id1, - account: nssa::Account { - balance: 10_000, - ..Default::default() - }, - key_chain: key_chain1, - })), - InitialAccountData::Private(Box::new(InitialAccountDataPrivate { - account_id: private_account_id2, - account: nssa::Account { - balance: 20_000, - ..Default::default() - }, - key_chain: key_chain2, - })), - ], + initial_accounts: None, } } } @@ -310,8 +262,8 @@ impl WalletConfig { seq_tx_poll_max_blocks, seq_poll_max_retries, seq_block_poll_max_amount, - initial_accounts, basic_auth, + initial_accounts, } = self; let WalletConfigOverrides { @@ -320,8 +272,8 @@ impl WalletConfig { seq_tx_poll_max_blocks: o_seq_tx_poll_max_blocks, seq_poll_max_retries: o_seq_poll_max_retries, seq_block_poll_max_amount: o_seq_block_poll_max_amount, - initial_accounts: o_initial_accounts, basic_auth: o_basic_auth, + initial_accounts: o_initial_accounts, } = overrides; if let Some(v) = o_sequencer_addr { @@ -344,13 +296,13 @@ impl WalletConfig { warn!("Overriding wallet config 'seq_block_poll_max_amount' to {v}"); *seq_block_poll_max_amount = v; } - if let Some(v) = o_initial_accounts { - warn!("Overriding wallet config 'initial_accounts' to {v:#?}"); - *initial_accounts = v; - } if let Some(v) = o_basic_auth { warn!("Overriding wallet config 'basic_auth' to {v:#?}"); *basic_auth = v; } + if let Some(v) = o_initial_accounts { + warn!("Overriding wallet config 'initial_accounts' to {v:#?}"); + *initial_accounts = v; + } } } diff --git a/wallet/src/helperfunctions.rs b/wallet/src/helperfunctions.rs index 74f7bab3..d82dedaf 100644 --- a/wallet/src/helperfunctions.rs +++ b/wallet/src/helperfunctions.rs @@ -7,12 +7,13 @@ use nssa::Account; use nssa_core::account::Nonce; use rand::{RngCore as _, rngs::OsRng}; use serde::Serialize; +use testnet_initial_state::{PrivateAccountPrivateInitialData, PublicAccountPrivateInitialData}; use crate::{ HOME_DIR_ENV_VAR, config::{ - InitialAccountData, InitialAccountDataPrivate, InitialAccountDataPublic, Label, - PersistentAccountDataPrivate, PersistentAccountDataPublic, PersistentStorage, + InitialAccountData, Label, PersistentAccountDataPrivate, PersistentAccountDataPublic, + PersistentStorage, }, }; @@ -119,7 +120,7 @@ pub fn produce_data_for_storage( for (account_id, key) in &user_data.default_pub_account_signing_keys { vec_for_storage.push( - InitialAccountData::Public(InitialAccountDataPublic { + InitialAccountData::Public(PublicAccountPrivateInitialData { account_id: *account_id, pub_sign_key: key.clone(), }) @@ -129,7 +130,7 @@ pub fn produce_data_for_storage( for (account_id, (key_chain, account)) in &user_data.default_user_private_accounts { vec_for_storage.push( - InitialAccountData::Private(Box::new(InitialAccountDataPrivate { + InitialAccountData::Private(Box::new(PrivateAccountPrivateInitialData { account_id: *account_id, account: account.clone(), key_chain: key_chain.clone(), diff --git a/wallet/src/lib.rs b/wallet/src/lib.rs index bf82a1bd..a09d477e 100644 --- a/wallet/src/lib.rs +++ b/wallet/src/lib.rs @@ -64,6 +64,8 @@ pub enum ExecutionFailureKind { InsufficientFundsError, #[error("Account {0} data is invalid")] AccountDataError(AccountId), + #[error("Failed to build transaction: {0}")] + TransactionBuildError(#[from] nssa::error::NssaError), } #[expect(clippy::partial_pub_fields, reason = "TODO: make all fields private")] diff --git a/wallet/src/program_facades/ata.rs b/wallet/src/program_facades/ata.rs new file mode 100644 index 00000000..ac60fb63 --- /dev/null +++ b/wallet/src/program_facades/ata.rs @@ -0,0 +1,280 @@ +use std::collections::HashMap; + +use ata_core::{compute_ata_seed, get_associated_token_account_id}; +use common::{HashType, transaction::NSSATransaction}; +use nssa::{ + AccountId, privacy_preserving_transaction::circuit::ProgramWithDependencies, program::Program, +}; +use nssa_core::SharedSecretKey; +use sequencer_service_rpc::RpcClient as _; + +use crate::{ExecutionFailureKind, PrivacyPreservingAccount, WalletCore}; + +pub struct Ata<'wallet>(pub &'wallet WalletCore); + +impl Ata<'_> { + pub async fn send_create( + &self, + owner_id: AccountId, + definition_id: AccountId, + ) -> Result { + let program = Program::ata(); + let ata_program_id = program.id(); + let ata_id = get_associated_token_account_id( + &ata_program_id, + &compute_ata_seed(owner_id, definition_id), + ); + + let account_ids = vec![owner_id, definition_id, ata_id]; + + let nonces = self + .0 + .get_accounts_nonces(vec![owner_id]) + .await + .map_err(ExecutionFailureKind::SequencerError)?; + + let Some(signing_key) = self + .0 + .storage + .user_data + .get_pub_account_signing_key(owner_id) + else { + return Err(ExecutionFailureKind::KeyNotFoundError); + }; + + let instruction = ata_core::Instruction::Create { ata_program_id }; + + let message = nssa::public_transaction::Message::try_new( + program.id(), + account_ids, + nonces, + instruction, + )?; + + let witness_set = + nssa::public_transaction::WitnessSet::for_message(&message, &[signing_key]); + + let tx = nssa::PublicTransaction::new(message, witness_set); + + Ok(self + .0 + .sequencer_client + .send_transaction(NSSATransaction::Public(tx)) + .await?) + } + + pub async fn send_transfer( + &self, + owner_id: AccountId, + definition_id: AccountId, + recipient_id: AccountId, + amount: u128, + ) -> Result { + let program = Program::ata(); + let ata_program_id = program.id(); + let sender_ata_id = get_associated_token_account_id( + &ata_program_id, + &compute_ata_seed(owner_id, definition_id), + ); + + let account_ids = vec![owner_id, sender_ata_id, recipient_id]; + + let nonces = self + .0 + .get_accounts_nonces(vec![owner_id]) + .await + .map_err(ExecutionFailureKind::SequencerError)?; + + let Some(signing_key) = self + .0 + .storage + .user_data + .get_pub_account_signing_key(owner_id) + else { + return Err(ExecutionFailureKind::KeyNotFoundError); + }; + + let instruction = ata_core::Instruction::Transfer { + ata_program_id, + amount, + }; + + let message = nssa::public_transaction::Message::try_new( + program.id(), + account_ids, + nonces, + instruction, + )?; + + let witness_set = + nssa::public_transaction::WitnessSet::for_message(&message, &[signing_key]); + + let tx = nssa::PublicTransaction::new(message, witness_set); + + Ok(self + .0 + .sequencer_client + .send_transaction(NSSATransaction::Public(tx)) + .await?) + } + + pub async fn send_burn( + &self, + owner_id: AccountId, + definition_id: AccountId, + amount: u128, + ) -> Result { + let program = Program::ata(); + let ata_program_id = program.id(); + let holder_ata_id = get_associated_token_account_id( + &ata_program_id, + &compute_ata_seed(owner_id, definition_id), + ); + + let account_ids = vec![owner_id, holder_ata_id, definition_id]; + + let nonces = self + .0 + .get_accounts_nonces(vec![owner_id]) + .await + .map_err(ExecutionFailureKind::SequencerError)?; + + let Some(signing_key) = self + .0 + .storage + .user_data + .get_pub_account_signing_key(owner_id) + else { + return Err(ExecutionFailureKind::KeyNotFoundError); + }; + + let instruction = ata_core::Instruction::Burn { + ata_program_id, + amount, + }; + + let message = nssa::public_transaction::Message::try_new( + program.id(), + account_ids, + nonces, + instruction, + )?; + + let witness_set = + nssa::public_transaction::WitnessSet::for_message(&message, &[signing_key]); + + let tx = nssa::PublicTransaction::new(message, witness_set); + + Ok(self + .0 + .sequencer_client + .send_transaction(NSSATransaction::Public(tx)) + .await?) + } + + pub async fn send_create_private_owner( + &self, + owner_id: AccountId, + definition_id: AccountId, + ) -> Result<(HashType, SharedSecretKey), ExecutionFailureKind> { + let ata_program_id = Program::ata().id(); + let ata_id = get_associated_token_account_id( + &ata_program_id, + &compute_ata_seed(owner_id, definition_id), + ); + + let instruction = ata_core::Instruction::Create { ata_program_id }; + let instruction_data = + Program::serialize_instruction(instruction).expect("Instruction should serialize"); + + let accounts = vec![ + PrivacyPreservingAccount::PrivateOwned(owner_id), + PrivacyPreservingAccount::Public(definition_id), + PrivacyPreservingAccount::Public(ata_id), + ]; + + self.0 + .send_privacy_preserving_tx(accounts, instruction_data, &ata_with_token_dependency()) + .await + .map(|(hash, mut secrets)| { + let secret = secrets.pop().expect("expected owner's secret"); + (hash, secret) + }) + } + + pub async fn send_transfer_private_owner( + &self, + owner_id: AccountId, + definition_id: AccountId, + recipient_id: AccountId, + amount: u128, + ) -> Result<(HashType, SharedSecretKey), ExecutionFailureKind> { + let ata_program_id = Program::ata().id(); + let sender_ata_id = get_associated_token_account_id( + &ata_program_id, + &compute_ata_seed(owner_id, definition_id), + ); + + let instruction = ata_core::Instruction::Transfer { + ata_program_id, + amount, + }; + let instruction_data = + Program::serialize_instruction(instruction).expect("Instruction should serialize"); + + let accounts = vec![ + PrivacyPreservingAccount::PrivateOwned(owner_id), + PrivacyPreservingAccount::Public(sender_ata_id), + PrivacyPreservingAccount::Public(recipient_id), + ]; + + self.0 + .send_privacy_preserving_tx(accounts, instruction_data, &ata_with_token_dependency()) + .await + .map(|(hash, mut secrets)| { + let secret = secrets.pop().expect("expected owner's secret"); + (hash, secret) + }) + } + + pub async fn send_burn_private_owner( + &self, + owner_id: AccountId, + definition_id: AccountId, + amount: u128, + ) -> Result<(HashType, SharedSecretKey), ExecutionFailureKind> { + let ata_program_id = Program::ata().id(); + let holder_ata_id = get_associated_token_account_id( + &ata_program_id, + &compute_ata_seed(owner_id, definition_id), + ); + + let instruction = ata_core::Instruction::Burn { + ata_program_id, + amount, + }; + let instruction_data = + Program::serialize_instruction(instruction).expect("Instruction should serialize"); + + let accounts = vec![ + PrivacyPreservingAccount::PrivateOwned(owner_id), + PrivacyPreservingAccount::Public(holder_ata_id), + PrivacyPreservingAccount::Public(definition_id), + ]; + + self.0 + .send_privacy_preserving_tx(accounts, instruction_data, &ata_with_token_dependency()) + .await + .map(|(hash, mut secrets)| { + let secret = secrets.pop().expect("expected owner's secret"); + (hash, secret) + }) + } +} + +fn ata_with_token_dependency() -> ProgramWithDependencies { + let token = Program::token(); + let mut deps = HashMap::new(); + deps.insert(token.id(), token); + ProgramWithDependencies::new(Program::ata(), deps) +} diff --git a/wallet/src/program_facades/mod.rs b/wallet/src/program_facades/mod.rs index 5fdcdb39..a0f8189c 100644 --- a/wallet/src/program_facades/mod.rs +++ b/wallet/src/program_facades/mod.rs @@ -2,6 +2,7 @@ //! on-chain programs. pub mod amm; +pub mod ata; pub mod native_token_transfer; pub mod pinata; pub mod token;