Skip to content

Commit

Permalink
Apply end-to-end logic for utxo return address
Browse files Browse the repository at this point in the history
  • Loading branch information
coderofstuff committed Mar 22, 2024
1 parent c5e03c3 commit 2fa5bbc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 13 deletions.
41 changes: 32 additions & 9 deletions consensus/src/consensus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ use crate::model::stores::selected_chain::SelectedChainStoreReader;

use std::cmp;

use crate::model::stores::utxo_diffs::UtxoDiffsStoreReader;
use kaspa_consensus_core::utxo::utxo_diff::ImmutableUtxoDiff;

pub struct Consensus {
// DB
db: Arc<DB>,
Expand Down Expand Up @@ -626,7 +629,7 @@ impl ConsensusApi for Consensus {
// let mut locator = Vec::with_capacity((high_index - low_index).ceiling_log_base_2() as usize);
// let mut current_index = high_index;
let mut matching_chain_block_hash: Option<Hash> = None;
while low_index < high_index {
while low_index <= high_index {
let mid = low_index + (high_index - low_index) / 2;

if let Ok(hash) = sc_read.get_by_index(mid) {
Expand Down Expand Up @@ -655,23 +658,43 @@ impl ConsensusApi for Consensus {
return None;
}

if let Ok(acceptance_data) = self.acceptance_data_store.get(matching_chain_block_hash.unwrap()) {
let matching_chain_block_hash = matching_chain_block_hash?;

if let Ok(acceptance_data) = self.acceptance_data_store.get(matching_chain_block_hash) {
let containing_acceptance = acceptance_data
.iter()
.find(|&mbad| mbad.accepted_transactions.iter().find(|&tx| tx.transaction_id == txid).is_some())
.cloned();

if let Some(containing_acceptance) = containing_acceptance {
// I found the merged block containing the TXID
// let txs = self.block_transactions_store.get(containing_acceptance.block_hash).unwrap().iter().find(|tx| => )
} else {
return None;
}
// Now I need to find the txid
let tx = self
.block_transactions_store
.get(containing_acceptance.block_hash)
.unwrap()
.iter()
.find(|&tx| tx.id() == txid)
.cloned()
.unwrap();

if tx.inputs.is_empty() {
return None;
}

return None;
} else {
return None;
let first_input = &tx.inputs[0];

let prev_outpoint = &first_input.previous_outpoint;

let utxo_diff = self.utxo_diffs_store.get(matching_chain_block_hash).unwrap();

let removed_diffs = utxo_diff.removed();

return Some(removed_diffs.get(prev_outpoint)?.script_public_key.clone());
}
};

return None;
}

fn get_virtual_parents(&self) -> BlockHashSet {
Expand Down
29 changes: 25 additions & 4 deletions rpc/service/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,10 +621,31 @@ NOTE: This error usually indicates an RPC conversion error between the node and
}

async fn get_utxo_return_address_call(&self, request: GetUtxoReturnAddressRequest) -> RpcResult<GetUtxoReturnAddressResponse> {
// let session = self.consensus_manager.consensus().session().await;
println!("{} {}", request.txid, request.accepting_block_daa_score);
// let mut maybe_spk = session.async_get_utxo_return_address(request.txid, request.accepting_block_daa_score).await;
Ok(GetUtxoReturnAddressResponse { return_address: None })
let session = self.consensus_manager.consensus().session().await;

let maybe_spk = session.async_get_utxo_return_address(request.txid, request.accepting_block_daa_score).await;

let mut return_address = None;

// Convert a SPK to an Address
if let Some(spk) = maybe_spk {
let script = spk.script();

// Address scripts are only either 34 or 35 in length:
if script.len() == 34 && script[0] == 0x20 && script[33] == 0xac {
return_address = Some(RpcAddress::new(self.config.prefix(), kaspa_addresses::Version::PubKey, &script[1..33]));
} else if script.len() == 35 {
// Could be ECDSA address OR P2SH
if script[0] == 0x21 && script[34] == 0xab {
return_address =
Some(RpcAddress::new(self.config.prefix(), kaspa_addresses::Version::PubKeyECDSA, &script[1..34]));
} else if script[0] == 0xaa && script[1] == 0x20 && script[34] == 0x87 {
return_address = Some(RpcAddress::new(self.config.prefix(), kaspa_addresses::Version::ScriptHash, &script[2..34]));
}
}
};

Ok(GetUtxoReturnAddressResponse { return_address })
}

async fn ping_call(&self, _: PingRequest) -> RpcResult<PingResponse> {
Expand Down

0 comments on commit 2fa5bbc

Please sign in to comment.