Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement search_ledger endpoint searching of txo id from the ledger db. #862

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.


Expand Down
4 changes: 1 addition & 3 deletions full-service/src/db/txo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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::<MerlinTranscript>(b"txo_data");
Self(hex::encode(digest))
Self(hex::encode(src.hash()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this be a breaking change for existing databases?

}
}

Expand Down
59 changes: 56 additions & 3 deletions full-service/src/service/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -593,6 +598,54 @@ where
}))
}

fn search_ledger_by_txo_hash(
&self,
query_bytes: &[u8],
) -> Result<Option<LedgerSearchResult>, 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],
Expand Down
7 changes: 7 additions & 0 deletions python/mobilecoin/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion tools/build-fs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
1 change: 1 addition & 0 deletions tools/run-fs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down