Skip to content

Commit 5eeca3a

Browse files
authored
feat: update the api for deleted block info (#7735)
Description --- Add the timestamp of the deleted utxo info
1 parent 98ab290 commit 5eeca3a

File tree

4 files changed

+98
-11
lines changed

4 files changed

+98
-11
lines changed

applications/minotari_node/src/http/handler/get_utxos_deleted_info.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ use tari_core::{
1717
base_node::rpc::{BaseNodeWalletQueryService, query_service},
1818
chain_storage::BlockchainBackend,
1919
};
20-
use tari_transaction_components::rpc::models::{GetUtxosDeletedInfoRequest, GetUtxosDeletedInfoResponse};
20+
use tari_transaction_components::rpc::models::{
21+
GetUtxosDeletedInfoRequest,
22+
GetUtxosDeletedInfoResponse,
23+
GetUtxosDeletedInfoResponseV1,
24+
};
2125
use tari_utilities::hex::Hex;
2226
use tonic::service::AxumBody;
2327

@@ -42,6 +46,8 @@ pub struct GetUtxosDeletedInfoParams {
4246
pub hashes: Vec<Vec<u8>>,
4347
#[serde(deserialize_with = "from_hex")]
4448
pub must_include_header: Vec<u8>,
49+
#[serde(default)]
50+
pub version: u8,
4551
}
4652

4753
impl From<GetUtxosDeletedInfoParams> for GetUtxosDeletedInfoRequest {
@@ -59,7 +65,8 @@ impl From<GetUtxosDeletedInfoParams> for GetUtxosDeletedInfoRequest {
5965
params(GetUtxosDeletedInfoParams),
6066
path = "/get_utxos_deleted_info",
6167
responses(
62-
(status = 200, description = "UTXOs Deleted Info", body = GetUtxosDeletedInfoResponse),
68+
(status = 200, description = "UTXOs Deleted Info (v0)", body = GetUtxosDeletedInfoResponse),
69+
(status = 200, description = "UTXOs Deleted Info (v1, includes spent_timestamp)", body = GetUtxosDeletedInfoResponseV1),
6370
),
6471
)]
6572
pub async fn handle<B: BlockchainBackend + 'static>(
@@ -68,15 +75,25 @@ pub async fn handle<B: BlockchainBackend + 'static>(
6875
Extension(cache_cfg): Extension<Arc<HttpCacheConfig>>,
6976
) -> Result<Response<AxumBody>, (StatusCode, Json<ErrorResponse>)> {
7077
debug!(target: LOG_TARGET, "Received get_utxos_deleted_info request: {params}");
78+
let version = params.version;
7179
let request = params.into();
7280

73-
let response = query_service
74-
.get_utxos_deleted_info(request)
75-
.await
76-
.map_err(error_handler_with_message)?;
77-
78-
let body = Json(response);
79-
let mut response = body.into_response();
81+
let mut response = match version {
82+
0 => {
83+
let result = query_service
84+
.get_utxos_deleted_info(request)
85+
.await
86+
.map_err(error_handler_with_message)?;
87+
Json(result).into_response()
88+
},
89+
_ => {
90+
let result = query_service
91+
.get_utxos_deleted_info_v1(request)
92+
.await
93+
.map_err(error_handler_with_message)?;
94+
Json(result).into_response()
95+
},
96+
};
8097
apply_cache_control(response.headers_mut(), &cache_cfg, RouteKey::GetUtxosDeletedInfo, 0, 0);
8198
Ok(response)
8299
}
@@ -85,14 +102,15 @@ impl Display for GetUtxosDeletedInfoParams {
85102
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86103
write!(
87104
f,
88-
"GetUtxosDeletedInfoParams {{ must_include_header: {}, hashes: {:?} }}",
105+
"GetUtxosDeletedInfoParams {{ must_include_header: {}, hashes: {:?}, version: {} }}",
89106
HashOutput::try_from(self.must_include_header.as_slice())
90107
.unwrap_or_default()
91108
.to_hex(),
92109
self.hashes
93110
.iter()
94111
.map(|h| HashOutput::try_from(h.as_slice()).unwrap_or_default().to_hex())
95-
.collect::<Vec<_>>()
112+
.collect::<Vec<_>>(),
113+
self.version
96114
)
97115
}
98116
}

base_layer/core/src/base_node/rpc/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ pub trait BaseNodeWalletQueryService: Send + Sync + 'static {
8989
request: models::GetUtxosDeletedInfoRequest,
9090
) -> Result<models::GetUtxosDeletedInfoResponse, Self::Error>;
9191

92+
async fn get_utxos_deleted_info_v1(
93+
&self,
94+
request: models::GetUtxosDeletedInfoRequest,
95+
) -> Result<models::GetUtxosDeletedInfoResponseV1, Self::Error>;
96+
9297
async fn generate_kernel_merkle_proof(
9398
&self,
9499
excess_sig: CompressedSignature,

base_layer/core/src/base_node/rpc/query_service.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,55 @@ impl<B: BlockchainBackend + 'static> BaseNodeWalletQueryService for Service<B> {
608608
})
609609
}
610610

611+
async fn get_utxos_deleted_info_v1(
612+
&self,
613+
request: models::GetUtxosDeletedInfoRequest,
614+
) -> Result<models::GetUtxosDeletedInfoResponseV1, Self::Error> {
615+
request.validate()?;
616+
617+
let mut utxos = Vec::with_capacity(request.hashes.len());
618+
619+
let must_include_header = request.must_include_header.clone().try_into()?;
620+
if self
621+
.db()
622+
.fetch_header_by_block_hash(must_include_header)
623+
.await?
624+
.is_none()
625+
{
626+
return Err(Error::HeaderHashNotFound);
627+
}
628+
629+
let tip_header = self.db().fetch_tip_header().await?;
630+
for hash in request.hashes {
631+
let hash = hash.try_into()?;
632+
let output = self.db().fetch_output(hash).await?;
633+
634+
let utxo_info = if let Some(output) = output {
635+
let input = self.db().fetch_input(hash).await?;
636+
models::DeletedUtxoInfoV1 {
637+
utxo_hash: hash.to_vec(),
638+
found_in_header: Some((output.mined_height, output.header_hash.to_vec())),
639+
spent_in_header: input.as_ref().map(|i| (i.spent_height, i.header_hash.to_vec())),
640+
spent_timestamp: input.as_ref().map(|i| i.spent_timestamp),
641+
}
642+
} else {
643+
models::DeletedUtxoInfoV1 {
644+
utxo_hash: hash.to_vec(),
645+
found_in_header: None,
646+
spent_in_header: None,
647+
spent_timestamp: None,
648+
}
649+
};
650+
utxos.push(utxo_info);
651+
}
652+
653+
Ok(models::GetUtxosDeletedInfoResponseV1 {
654+
utxos,
655+
best_block_hash: tip_header.hash().to_vec(),
656+
best_block_height: tip_header.height(),
657+
})
658+
}
659+
611660
async fn generate_kernel_merkle_proof(
612661
&self,
613662
excess_sig: types::CompressedSignature,

base_layer/transaction_components/src/rpc/models/get_utxos_deleted_info.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,18 @@ pub struct DeletedUtxoInfo {
2323
pub found_in_header: Option<(u64, Vec<u8>)>,
2424
pub spent_in_header: Option<(u64, Vec<u8>)>,
2525
}
26+
27+
#[derive(Serialize, Deserialize, ToSchema, Debug, Clone)]
28+
pub struct GetUtxosDeletedInfoResponseV1 {
29+
pub utxos: Vec<DeletedUtxoInfoV1>,
30+
pub best_block_hash: Vec<u8>,
31+
pub best_block_height: u64,
32+
}
33+
34+
#[derive(Debug, Clone, Deserialize, Serialize, ToSchema)]
35+
pub struct DeletedUtxoInfoV1 {
36+
pub utxo_hash: Vec<u8>,
37+
pub found_in_header: Option<(u64, Vec<u8>)>,
38+
pub spent_in_header: Option<(u64, Vec<u8>)>,
39+
pub spent_timestamp: Option<u64>,
40+
}

0 commit comments

Comments
 (0)