Skip to content

Commit 58f716d

Browse files
committed
Add gettxmeta helper method
- Also rename getblock => getblockmeta
1 parent 5c66df0 commit 58f716d

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

node/src/node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub struct Node {
3939
/// relevant to the Spaces protocol
4040
#[derive(Clone, Serialize, Deserialize, Encode, Decode)]
4141
pub struct BlockMeta {
42-
tx_meta: Vec<TxChangeSet>,
42+
pub tx_meta: Vec<TxChangeSet>,
4343
}
4444

4545
#[derive(Debug)]

node/src/rpc.rs

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use protocol::{
2525
constants::ChainAnchor,
2626
hasher::{BaseHash, SpaceKey},
2727
prepare::DataSource,
28+
validate::TxChangeSet,
2829
FullSpaceOut, SpaceOut,
2930
};
3031
use serde::{Deserialize, Serialize};
@@ -71,6 +72,10 @@ pub enum ChainStateCommand {
7172
hash: SpaceKey,
7273
resp: Responder<anyhow::Result<Option<OutPoint>>>,
7374
},
75+
GetTxMeta {
76+
txid: Txid,
77+
resp: Responder<anyhow::Result<Option<TxChangeSet>>>,
78+
},
7479
GetBlockMeta {
7580
block_hash: BlockHash,
7681
resp: Responder<anyhow::Result<Option<BlockMeta>>>,
@@ -111,9 +116,14 @@ pub trait Rpc {
111116
#[method(name = "getrollout")]
112117
async fn get_rollout(&self, target: usize) -> Result<Vec<(u32, SpaceKey)>, ErrorObjectOwned>;
113118

114-
#[method(name = "getblock")]
115-
async fn get_block(&self, block_hash: BlockHash)
116-
-> Result<Option<BlockMeta>, ErrorObjectOwned>;
119+
#[method(name = "getblockmeta")]
120+
async fn get_block_meta(
121+
&self,
122+
block_hash: BlockHash,
123+
) -> Result<Option<BlockMeta>, ErrorObjectOwned>;
124+
125+
#[method(name = "gettxmeta")]
126+
async fn get_tx_meta(&self, txid: Txid) -> Result<Option<TxChangeSet>, ErrorObjectOwned>;
117127

118128
#[method(name = "walletload")]
119129
async fn wallet_load(&self, name: &str) -> Result<(), ErrorObjectOwned>;
@@ -652,19 +662,28 @@ impl RpcServer for RpcServerImpl {
652662
Ok(rollouts)
653663
}
654664

655-
async fn get_block(
665+
async fn get_block_meta(
656666
&self,
657667
block_hash: BlockHash,
658668
) -> Result<Option<BlockMeta>, ErrorObjectOwned> {
659669
let data = self
660670
.store
661-
.get_block(block_hash)
671+
.get_block_meta(block_hash)
662672
.await
663673
.map_err(|error| ErrorObjectOwned::owned(-1, error.to_string(), None::<String>))?;
664674

665675
Ok(data)
666676
}
667677

678+
async fn get_tx_meta(&self, txid: Txid) -> Result<Option<TxChangeSet>, ErrorObjectOwned> {
679+
let data = self
680+
.store
681+
.get_tx_meta(txid)
682+
.await
683+
.map_err(|error| ErrorObjectOwned::owned(-1, error.to_string(), None::<String>))?;
684+
Ok(data)
685+
}
686+
668687
async fn wallet_load(&self, name: &str) -> Result<(), ErrorObjectOwned> {
669688
self.wallet_manager
670689
.load_wallet(&self.client, name)
@@ -794,6 +813,31 @@ impl AsyncChainState {
794813
Self { sender }
795814
}
796815

816+
async fn get_indexed_tx(
817+
index: &mut Option<LiveSnapshot>,
818+
txid: &Txid,
819+
client: &reqwest::Client,
820+
rpc: &BitcoinRpc,
821+
chain_state: &mut LiveSnapshot,
822+
) -> Result<Option<TxChangeSet>, anyhow::Error> {
823+
let info: serde_json::Value = rpc
824+
.send_json(client, &rpc.get_raw_transaction(&txid, true))
825+
.await
826+
.map_err(|e| anyhow!("Could not retrieve tx ({})", e))?;
827+
828+
let block_hash = BlockHash::from_str(
829+
info.get("blockhash")
830+
.and_then(|t| t.as_str())
831+
.ok_or_else(|| anyhow!("Could not retrieve block hash"))?,
832+
)?;
833+
let block = Self::get_indexed_block(index, &block_hash, client, rpc, chain_state).await?;
834+
835+
if let Some(block) = block {
836+
return Ok(block.tx_meta.into_iter().find(|tx| &tx.txid == txid));
837+
}
838+
Ok(None)
839+
}
840+
797841
async fn get_indexed_block(
798842
index: &mut Option<LiveSnapshot>,
799843
block_hash: &BlockHash,
@@ -868,6 +912,10 @@ impl AsyncChainState {
868912
.await;
869913
let _ = resp.send(res);
870914
}
915+
ChainStateCommand::GetTxMeta { txid, resp } => {
916+
let res = Self::get_indexed_tx(block_index, &txid, client, rpc, chain_state).await;
917+
let _ = resp.send(res);
918+
}
871919
ChainStateCommand::EstimateBid { target, resp } => {
872920
let estimate = chain_state.estimate_bid(target);
873921
_ = resp.send(estimate);
@@ -947,13 +995,21 @@ impl AsyncChainState {
947995
resp_rx.await?
948996
}
949997

950-
pub async fn get_block(&self, block_hash: BlockHash) -> anyhow::Result<Option<BlockMeta>> {
998+
pub async fn get_block_meta(&self, block_hash: BlockHash) -> anyhow::Result<Option<BlockMeta>> {
951999
let (resp, resp_rx) = oneshot::channel();
9521000
self.sender
9531001
.send(ChainStateCommand::GetBlockMeta { block_hash, resp })
9541002
.await?;
9551003
resp_rx.await?
9561004
}
1005+
1006+
pub async fn get_tx_meta(&self, txid: Txid) -> anyhow::Result<Option<TxChangeSet>> {
1007+
let (resp, resp_rx) = oneshot::channel();
1008+
self.sender
1009+
.send(ChainStateCommand::GetTxMeta { txid, resp })
1010+
.await?;
1011+
resp_rx.await?
1012+
}
9571013
}
9581014

9591015
fn space_hash_from_string(space_hash: &str) -> Result<SpaceKey, ErrorObjectOwned> {

node/src/source.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ impl BitcoinRpc {
146146
self.make_request("getblockheader", params)
147147
}
148148

149+
pub fn get_raw_transaction(&self, hash: &Txid, verbose: bool) -> BitcoinRpcRequest {
150+
let params = serde_json::json!([hash, verbose]);
151+
self.make_request("getrawtransaction", params)
152+
}
153+
149154
pub fn get_block_hash(&self, height: u32) -> BitcoinRpcRequest {
150155
let params = serde_json::json!([height]);
151156

0 commit comments

Comments
 (0)