Skip to content

Commit

Permalink
Implement UTXO Return Address RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
coderofstuff committed Dec 6, 2024
1 parent e16582e commit 7fe3f48
Show file tree
Hide file tree
Showing 29 changed files with 440 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions cli/src/modules/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,21 @@ impl Rpc {
let result = rpc.get_current_block_color_call(None, GetCurrentBlockColorRequest { hash }).await?;
self.println(&ctx, result);
}
RpcApiOps::GetUtxoReturnAddress => {
if argv.is_empty() || argv.len() != 2 {
return Err(Error::custom("Please specify a txid and a accepting_block_daa_score"));
}

let txid = argv.remove(0);
let txid = RpcHash::from_hex(txid.as_str())?;

let accepting_block_daa_score = argv.remove(0).parse::<u64>()?;

let result =
rpc.get_utxo_return_address_call(None, GetUtxoReturnAddressRequest { txid, accepting_block_daa_score }).await?;

self.println(&ctx, result);
}
_ => {
tprintln!(ctx, "rpc method exists but is not supported by the cli: '{op_str}'\r\n");
return Ok(());
Expand Down
1 change: 1 addition & 0 deletions components/consensusmanager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ duration-string.workspace = true
futures-util.workspace = true
futures.workspace = true
itertools.workspace = true
kaspa-addresses.workspace=true
kaspa-consensus-core.workspace = true
kaspa-consensus-notify.workspace = true
kaspa-core.workspace = true
Expand Down
10 changes: 10 additions & 0 deletions components/consensusmanager/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//!
//! We use newtypes in order to simplify changing the underlying lock in the future
use kaspa_addresses::Address;
use kaspa_consensus_core::{
acceptance_data::AcceptanceData,
api::{BlockCount, BlockValidationFutures, ConsensusApi, ConsensusStats, DynConsensus},
Expand All @@ -11,6 +12,7 @@ use kaspa_consensus_core::{
errors::consensus::ConsensusResult,
header::Header,
pruning::{PruningPointProof, PruningPointTrustedData, PruningPointsList},
return_address::ReturnAddressError,
trusted::{ExternalGhostdagData, TrustedBlock},
tx::{MutableTransaction, Transaction, TransactionOutpoint, UtxoEntry},
BlockHashSet, BlueWorkType, ChainPath, Hash,
Expand Down Expand Up @@ -313,6 +315,14 @@ impl ConsensusSessionOwned {
self.clone().spawn_blocking(|c| c.get_chain_block_samples()).await
}

pub async fn async_get_utxo_return_script_public_key(
&self,
txid: Hash,
accepting_block_daa_score: u64,
) -> Result<Address, ReturnAddressError> {
self.clone().spawn_blocking(move |c| c.get_utxo_return_address(txid, accepting_block_daa_score)).await
}

/// Returns the antipast of block `hash` from the POV of `context`, i.e. `antipast(hash) ∩ past(context)`.
/// Since this might be an expensive operation for deep blocks, we allow the caller to specify a limit
/// `max_traversal_allowed` on the maximum amount of blocks to traverse for obtaining the answer
Expand Down
1 change: 1 addition & 0 deletions consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ faster-hex.workspace = true
futures-util.workspace = true
indexmap.workspace = true
itertools.workspace = true
kaspa-addresses.workspace = true
kaspa-consensus-core.workspace = true
kaspa-consensus-notify.workspace = true
kaspa-consensusmanager.workspace = true
Expand Down
6 changes: 6 additions & 0 deletions consensus/core/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use futures_util::future::BoxFuture;
use kaspa_addresses::Address;
use kaspa_muhash::MuHash;
use std::sync::Arc;

Expand All @@ -18,6 +19,7 @@ use crate::{
},
header::Header,
pruning::{PruningPointProof, PruningPointTrustedData, PruningPointsList, PruningProofMetadata},
return_address::ReturnAddressError,
trusted::{ExternalGhostdagData, TrustedBlock},
tx::{MutableTransaction, Transaction, TransactionOutpoint, UtxoEntry},
BlockHashSet, BlueWorkType, ChainPath,
Expand Down Expand Up @@ -170,6 +172,10 @@ pub trait ConsensusApi: Send + Sync {
unimplemented!()
}

fn get_utxo_return_address(&self, txid: Hash, daa_score: u64) -> Result<Address, ReturnAddressError> {
unimplemented!()
}

fn get_virtual_parents(&self) -> BlockHashSet {
unimplemented!()
}
Expand Down
1 change: 1 addition & 0 deletions consensus/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub mod merkle;
pub mod muhash;
pub mod network;
pub mod pruning;
pub mod return_address;
pub mod sign;
pub mod subnets;
pub mod trusted;
Expand Down
15 changes: 15 additions & 0 deletions consensus/core/src/return_address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use thiserror::Error;

#[derive(Error, Debug, Clone)]
pub enum ReturnAddressError {
#[error("Transaction is already pruned")]
AlreadyPruned,
#[error("Transaction return address is coinbase")]
TxFromCoinbase,
#[error("Transaction not found at given accepting daa score")]
NoTxAtScore,
#[error("Transaction was found but not standard")]
NonStandard,
#[error("Transaction return address not found: {0}")]
NotFound(String),
}
6 changes: 6 additions & 0 deletions consensus/src/consensus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use crate::{
window::{WindowManager, WindowType},
},
};
use kaspa_addresses::Address;
use kaspa_consensus_core::{
acceptance_data::AcceptanceData,
api::{
Expand All @@ -65,6 +66,7 @@ use kaspa_consensus_core::{
muhash::MuHashExtensions,
network::NetworkType,
pruning::{PruningPointProof, PruningPointTrustedData, PruningPointsList, PruningProofMetadata},
return_address::ReturnAddressError,
trusted::{ExternalGhostdagData, TrustedBlock},
tx::{MutableTransaction, Transaction, TransactionOutpoint, UtxoEntry},
BlockHashSet, BlueWorkType, ChainPath, HashMapCustomHasher,
Expand Down Expand Up @@ -687,6 +689,10 @@ impl ConsensusApi for Consensus {
sample_headers
}

fn get_utxo_return_address(&self, txid: Hash, target_daa_score: u64) -> Result<Address, ReturnAddressError> {
self.virtual_processor.get_utxo_return_address(txid, target_daa_score, self.get_source(), &self.config)
}

fn get_virtual_parents(&self) -> BlockHashSet {
self.lkg_virtual_state.load().parents.iter().copied().collect()
}
Expand Down
Loading

0 comments on commit 7fe3f48

Please sign in to comment.