fix is_key_image_spent

This commit is contained in:
hinto.janai 2025-01-17 14:40:30 -05:00
parent 78b3ff1925
commit 95ab9765e8
No known key found for this signature in database
GPG key ID: D47CE05FA175A499

View file

@ -305,27 +305,49 @@ async fn is_key_image_spent(
let mut spent_status = Vec::with_capacity(key_images.len()); let mut spent_status = Vec::with_capacity(key_images.len());
// Check the blockchain for key image spend status.
blockchain::key_images_spent(&mut state.blockchain_read, key_images.clone()) blockchain::key_images_spent(&mut state.blockchain_read, key_images.clone())
.await? .await?
.into_iter() .into_iter()
.for_each(|ki| { .for_each(|ki| {
if ki { if ki {
spent_status.push(KeyImageSpentStatus::SpentInBlockchain.to_u8()); spent_status.push(KeyImageSpentStatus::SpentInBlockchain);
} else { } else {
spent_status.push(KeyImageSpentStatus::Unspent.to_u8()); spent_status.push(KeyImageSpentStatus::Unspent);
} }
}); });
txpool::key_images_spent(&mut state.txpool_read, key_images, !restricted) assert_eq!(spent_status.len(), key_images.len(), "key_images_spent() should be returning a Vec with an equal length to the input, the below zip() relies on this.");
.await?
// Filter the remaining unspent key images out from the vector.
let key_images = key_images
.into_iter() .into_iter()
.for_each(|ki| { .zip(&spent_status)
if ki { .filter_map(|(ki, status)| match status {
spent_status.push(KeyImageSpentStatus::SpentInPool.to_u8()); KeyImageSpentStatus::Unspent => Some(ki),
} else { KeyImageSpentStatus::SpentInBlockchain => None,
spent_status.push(KeyImageSpentStatus::Unspent.to_u8()); KeyImageSpentStatus::SpentInPool => unreachable!(),
} })
}); .collect();
// Check if the remaining unspent key images exist in the transaction pool.
if !key_images.is_empty() {
txpool::key_images_spent(&mut state.txpool_read, key_images, !restricted)
.await?
.into_iter()
.for_each(|ki| {
if ki {
spent_status.push(KeyImageSpentStatus::SpentInPool);
} else {
spent_status.push(KeyImageSpentStatus::Unspent);
}
});
}
let spent_status = spent_status
.into_iter()
.map(|status| status.to_u8())
.collect();
Ok(IsKeyImageSpentResponse { Ok(IsKeyImageSpentResponse {
base: helper::access_response_base(false), base: helper::access_response_base(false),