blockchain: add KeyImageSpent request

This commit is contained in:
hinto.janai 2024-09-09 18:10:29 -04:00
parent b6e8b3a08e
commit 1aca03d319
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
2 changed files with 29 additions and 1 deletions

View file

@ -34,7 +34,7 @@ use crate::{
free::{compact_history_genesis_not_included, compact_history_index_to_height_offset}, free::{compact_history_genesis_not_included, compact_history_index_to_height_offset},
types::{BlockchainReadHandle, ResponseResult}, types::{BlockchainReadHandle, ResponseResult},
}, },
tables::{BlockHeights, BlockInfos, OpenTables, Tables}, tables::{BlockHeights, BlockInfos, KeyImages, OpenTables, Tables},
types::{Amount, AmountIndex, BlockHash, BlockHeight, KeyImage, PreRctOutputId}, types::{Amount, AmountIndex, BlockHash, BlockHeight, KeyImage, PreRctOutputId},
}; };
@ -100,6 +100,7 @@ fn map_request(
R::GeneratedCoins(height) => generated_coins(env, height), R::GeneratedCoins(height) => generated_coins(env, height),
R::Outputs(map) => outputs(env, map), R::Outputs(map) => outputs(env, map),
R::NumberOutputsWithAmount(vec) => number_outputs_with_amount(env, vec), R::NumberOutputsWithAmount(vec) => number_outputs_with_amount(env, vec),
R::KeyImageSpent(set) => key_image_spent(env, set),
R::KeyImagesSpent(set) => key_images_spent(env, set), R::KeyImagesSpent(set) => key_images_spent(env, set),
R::CompactChainHistory => compact_chain_history(env), R::CompactChainHistory => compact_chain_history(env),
R::FindFirstUnknown(block_ids) => find_first_unknown(env, &block_ids), R::FindFirstUnknown(block_ids) => find_first_unknown(env, &block_ids),
@ -414,6 +415,21 @@ fn number_outputs_with_amount(env: &ConcreteEnv, amounts: Vec<Amount>) -> Respon
Ok(BlockchainResponse::NumberOutputsWithAmount(map)) Ok(BlockchainResponse::NumberOutputsWithAmount(map))
} }
/// [`BlockchainReadRequest::KeyImageSpent`].
#[inline]
fn key_image_spent(env: &ConcreteEnv, key_image: KeyImage) -> ResponseResult {
// Single-threaded, no `ThreadLocal` required.
let env_inner = env.env_inner();
let tx_ro = env_inner.tx_ro()?;
let table_key_images = env_inner.open_db_ro::<KeyImages>(&tx_ro)?;
match key_image_exists(&key_image, &table_key_images) {
Ok(false) => Ok(BlockchainResponse::KeyImagesSpent(false)), // Key image was NOT found.
Ok(true) => Ok(BlockchainResponse::KeyImagesSpent(true)), // Key image was found.
Err(e) => Err(e), // A database error occurred.
}
}
/// [`BlockchainReadRequest::KeyImagesSpent`]. /// [`BlockchainReadRequest::KeyImagesSpent`].
#[inline] #[inline]
fn key_images_spent(env: &ConcreteEnv, key_images: HashSet<KeyImage>) -> ResponseResult { fn key_images_spent(env: &ConcreteEnv, key_images: HashSet<KeyImage>) -> ResponseResult {

View file

@ -98,6 +98,11 @@ pub enum BlockchainReadRequest {
/// The input is a list of output amounts. /// The input is a list of output amounts.
NumberOutputsWithAmount(Vec<u64>), NumberOutputsWithAmount(Vec<u64>),
/// Check that a single key image is not spent.
///
/// Input is a key image hash.
KeyImageSpent([u8; 32]),
/// Check that all key images within a set are not spent. /// Check that all key images within a set are not spent.
/// ///
/// Input is a set of key images. /// Input is a set of key images.
@ -198,6 +203,13 @@ pub enum BlockchainResponse {
/// - Value = count of outputs with the same amount /// - Value = count of outputs with the same amount
NumberOutputsWithAmount(HashMap<u64, usize>), NumberOutputsWithAmount(HashMap<u64, usize>),
/// Response to [`BlockchainReadRequest::KeyImageSpent`].
///
/// The inner value is `true` if the key image
/// was spent (existed in the database already),
/// else `false`.
KeyImageSpent(bool),
/// Response to [`BlockchainReadRequest::KeyImagesSpent`]. /// Response to [`BlockchainReadRequest::KeyImagesSpent`].
/// ///
/// The inner value is `true` if _any_ of the key images /// The inner value is `true` if _any_ of the key images