From d97b31ff4493264bb83b7f40e0e16be14a607c68 Mon Sep 17 00:00:00 2001 From: Christian Oudard Date: Thu, 18 May 2023 16:23:08 -0600 Subject: [PATCH] Implement search_ledger endpoint searching of txo id from the ledger db. --- README.md | 6 +++ full-service/src/db/txo.rs | 4 +- full-service/src/service/ledger.rs | 59 ++++++++++++++++++++++++++++-- python/mobilecoin/client.py | 7 ++++ tools/build-fs.sh | 3 +- tools/run-fs.sh | 1 + 6 files changed, 73 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7ce253659..bf1c87953 100644 --- a/README.md +++ b/README.md @@ -234,6 +234,12 @@ sudo xcode-select -s /Applications/Xcode_12.5.1.app/Contents/Developer ./tools/run-fs.sh test ``` + You can also run and build simultaneously with + ```sh + ./tools/run-fs.sh test --build + ``` + + See [Parameters](#parameters) for full list of available options. diff --git a/full-service/src/db/txo.rs b/full-service/src/db/txo.rs index 83b9274d1..ed25572fc 100644 --- a/full-service/src/db/txo.rs +++ b/full-service/src/db/txo.rs @@ -8,7 +8,6 @@ use diesel::{ }; use mc_account_keys::AccountKey; use mc_common::{logger::global_log, HashMap}; -use mc_crypto_digestible::{Digestible, MerlinTranscript}; use mc_crypto_keys::{CompressedRistrettoPublic, RistrettoPublic}; use mc_ledger_db::{Ledger, LedgerDB}; use mc_transaction_core::{ @@ -101,8 +100,7 @@ pub struct TxoID(pub String); impl From<&TxOut> for TxoID { fn from(src: &TxOut) -> TxoID { - let digest: [u8; 32] = src.digest32::(b"txo_data"); - Self(hex::encode(digest)) + Self(hex::encode(src.hash())) } } diff --git a/full-service/src/service/ledger.rs b/full-service/src/service/ledger.rs index 99e4524b2..977bff1ae 100644 --- a/full-service/src/service/ledger.rs +++ b/full-service/src/service/ledger.rs @@ -35,7 +35,7 @@ use rand::Rng; use crate::db::WalletDbError; use displaydoc::Display; use rayon::prelude::*; // For par_iter -use std::{convert::TryFrom, ops::DerefMut}; +use std::{convert::{TryFrom, TryInto}, ops::DerefMut}; /// Errors for the Address Service. #[derive(Display, Debug)] @@ -531,12 +531,17 @@ where query_bytes.remove(0); } - // Try and search for a tx out by public key. + // Search for a tx out by public key. if let Some(result) = self.search_ledger_by_tx_out_pub_key(&query_bytes)? { results.push(result); } - // Try and search for a key image. + // Search for a key image. + if let Some(result) = self.search_ledger_by_txo_hash(&query_bytes)? { + results.push(result); + } + + // Search for a key image. if let Some(result) = self.search_ledger_by_key_image(&query_bytes)? { results.push(result); } @@ -593,6 +598,54 @@ where })) } + fn search_ledger_by_txo_hash( + &self, + query_bytes: &[u8], + ) -> Result, LedgerServiceError> { + // // get any txo_id + // let block_contents = self.ledger_db.get_block_contents(1)?; + // let any_hash = block_contents.outputs[0].hash(); + // dbg!(hex::encode(&any_hash)); + // return Ok(None); + + let txo_id: [u8; 32] = match (*query_bytes).try_into() { + Ok(array_ref) => array_ref, + Err(_) => return Ok(None) + }; + dbg!(&txo_id); + + let tx_out_global_index = match self.ledger_db.get_tx_out_index_by_hash(&txo_id) { + Ok(index) => index, + Err(_) => return Ok(None) + }; + dbg!(tx_out_global_index); + + let block_index = self + .ledger_db + .get_block_index_by_tx_out_index(tx_out_global_index)?; + + let block = self.ledger_db.get_block(block_index)?; + + let block_contents = self.ledger_db.get_block_contents(block_index)?; + + let block_contents_tx_out_index = block_contents + .outputs + .iter() + .position(|tx_out| tx_out.hash() == txo_id) + .ok_or(LedgerServiceError::LedgerInconsistent)? + as u64; + + let watcher_info = self.get_watcher_block_info(block_index)?; + + Ok(Some(LedgerSearchResult::TxOut { + block, + block_contents, + block_contents_tx_out_index, + tx_out_global_index, + watcher_info, + })) + } + fn search_ledger_by_key_image( &self, query_bytes: &[u8], diff --git a/python/mobilecoin/client.py b/python/mobilecoin/client.py index 1b2af362d..91c1d92db 100644 --- a/python/mobilecoin/client.py +++ b/python/mobilecoin/client.py @@ -368,6 +368,13 @@ async def check_b58_type(self, b58_code): "params": {"b58_code": b58_code}, }) + async def search_ledger(self, query): + return await self._req({ + "method": "search_ledger", + "params": {"query": query}, + }) + + # Polling utility functions. @staticmethod diff --git a/tools/build-fs.sh b/tools/build-fs.sh index 8e4c51ceb..b571322d2 100755 --- a/tools/build-fs.sh +++ b/tools/build-fs.sh @@ -150,6 +150,7 @@ fi echo "building full service..." # shellcheck disable=SC2086 # split away - Use BUILD_OPTIONS to set additional build options -cargo build --release ${BUILD_OPTIONS} + +cargo build --release ${BUILD_OPTIONS} --bin full-service echo " Binaries are available in ${RELEASE_DIR} and ${WORK_DIR}" diff --git a/tools/run-fs.sh b/tools/run-fs.sh index 603cab4d4..48c8aee1c 100755 --- a/tools/run-fs.sh +++ b/tools/run-fs.sh @@ -164,6 +164,7 @@ echo " MC_LISTEN_HOST: ${MC_LISTEN_HOST}" if [[ -n "${build}" ]] then "${location}/build-fs.sh" "${net}" + WORK_DIR="${location}/../target/release" fi # start validator and unset envs for full-service