Skip to content

Commit 89c450b

Browse files
committed
get_block_meta by block height
1 parent 015fc83 commit 89c450b

File tree

1 file changed

+45
-24
lines changed

1 file changed

+45
-24
lines changed

node/src/rpc.rs

+45-24
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,33 @@ pub struct SignedMessage {
5757
pub signature: secp256k1::schnorr::Signature,
5858
}
5959

60+
#[derive(Debug, Serialize)]
61+
pub enum BlockIdentifier {
62+
Hash(BlockHash),
63+
Height(u32),
64+
}
65+
impl<'de> Deserialize<'de> for BlockIdentifier {
66+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
67+
where
68+
D: serde::Deserializer<'de>
69+
{
70+
#[derive(Deserialize)]
71+
#[serde(untagged)]
72+
enum RawIdentifier {
73+
String(String),
74+
Number(u32),
75+
}
76+
match RawIdentifier::deserialize(deserializer)? {
77+
RawIdentifier::String(s) => {
78+
BlockHash::from_str(&s)
79+
.map(BlockIdentifier::Hash)
80+
.map_err(serde::de::Error::custom)
81+
}
82+
RawIdentifier::Number(n) => Ok(BlockIdentifier::Height(n)),
83+
}
84+
}
85+
}
86+
6087
pub enum ChainStateCommand {
6188
CheckPackage {
6289
txs: Vec<String>,
@@ -83,7 +110,7 @@ pub enum ChainStateCommand {
83110
resp: Responder<anyhow::Result<Option<TxEntry>>>,
84111
},
85112
GetBlockMeta {
86-
block_hash: BlockHash,
113+
block_identifier: BlockIdentifier,
87114
resp: Responder<anyhow::Result<Option<BlockMeta>>>,
88115
},
89116
EstimateBid {
@@ -144,7 +171,7 @@ pub trait Rpc {
144171
#[method(name = "getblockmeta")]
145172
async fn get_block_meta(
146173
&self,
147-
block_hash: BlockHash,
174+
block_identifier: BlockIdentifier,
148175
) -> Result<Option<BlockMeta>, ErrorObjectOwned>;
149176

150177
#[method(name = "gettxmeta")]
@@ -502,19 +529,6 @@ impl WalletManager {
502529
Ok(())
503530
}
504531

505-
pub async fn get_block_hash(
506-
&self,
507-
client: &reqwest::Client,
508-
height: u32,
509-
) -> anyhow::Result<BlockId> {
510-
let hash = self
511-
.rpc
512-
.send_json(&client, &self.rpc.get_block_hash(height))
513-
.await?;
514-
515-
Ok(BlockId { height, hash })
516-
}
517-
518532
async fn get_wallet_start_block(&self, client: &reqwest::Client) -> anyhow::Result<BlockId> {
519533
let count: i32 = self
520534
.rpc
@@ -689,11 +703,11 @@ impl RpcServer for RpcServerImpl {
689703

690704
async fn get_block_meta(
691705
&self,
692-
block_hash: BlockHash,
706+
block_identifier: BlockIdentifier,
693707
) -> Result<Option<BlockMeta>, ErrorObjectOwned> {
694708
let data = self
695709
.store
696-
.get_block_meta(block_hash)
710+
.get_block_meta(block_identifier)
697711
.await
698712
.map_err(|error| ErrorObjectOwned::owned(-1, error.to_string(), None::<String>))?;
699713

@@ -916,7 +930,7 @@ impl AsyncChainState {
916930
BlockHash::from_str(info.get("blockhash").and_then(|t| t.as_str()).ok_or_else(
917931
|| anyhow!("Could not retrieve block hash for tx (is it in the mempool?)"),
918932
)?)?;
919-
let block = Self::get_indexed_block(index, &block_hash, client, rpc, chain_state).await?;
933+
let block = Self::get_indexed_block(index, BlockIdentifier::Hash(block_hash), client, rpc, chain_state).await?;
920934

921935
if let Some(block) = block {
922936
return Ok(block
@@ -929,14 +943,21 @@ impl AsyncChainState {
929943

930944
async fn get_indexed_block(
931945
index: &mut Option<LiveSnapshot>,
932-
block_hash: &BlockHash,
946+
block_identifier: BlockIdentifier,
933947
client: &reqwest::Client,
934948
rpc: &BitcoinRpc,
935949
chain_state: &mut LiveSnapshot,
936950
) -> Result<Option<BlockMeta>, anyhow::Error> {
937951
let index = index
938952
.as_mut()
939953
.ok_or_else(|| anyhow!("block index must be enabled"))?;
954+
let block_hash = match block_identifier {
955+
BlockIdentifier::Hash(hash) => hash,
956+
BlockIdentifier::Height(height) => rpc
957+
.send_json(client, &rpc.get_block_hash(height))
958+
.await
959+
.map_err(|e| anyhow!("Could not retrieve block hash ({})", e))?
960+
};
940961
let hash = BaseHash::from_slice(block_hash.as_ref());
941962
let block: Option<BlockMeta> = index
942963
.get(hash)
@@ -947,7 +968,7 @@ impl AsyncChainState {
947968
}
948969

949970
let info: serde_json::Value = rpc
950-
.send_json(client, &rpc.get_block_header(block_hash))
971+
.send_json(client, &rpc.get_block_header(&block_hash))
951972
.await
952973
.map_err(|e| anyhow!("Could not retrieve block ({})", e))?;
953974

@@ -1011,9 +1032,9 @@ impl AsyncChainState {
10111032
.context("could not fetch spaceout");
10121033
let _ = resp.send(result);
10131034
}
1014-
ChainStateCommand::GetBlockMeta { block_hash, resp } => {
1035+
ChainStateCommand::GetBlockMeta { block_identifier, resp } => {
10151036
let res =
1016-
Self::get_indexed_block(block_index, &block_hash, client, rpc, chain_state)
1037+
Self::get_indexed_block(block_index, block_identifier, client, rpc, chain_state)
10171038
.await;
10181039
let _ = resp.send(res);
10191040
}
@@ -1135,10 +1156,10 @@ impl AsyncChainState {
11351156
resp_rx.await?
11361157
}
11371158

1138-
pub async fn get_block_meta(&self, block_hash: BlockHash) -> anyhow::Result<Option<BlockMeta>> {
1159+
pub async fn get_block_meta(&self, block_identifier: BlockIdentifier) -> anyhow::Result<Option<BlockMeta>> {
11391160
let (resp, resp_rx) = oneshot::channel();
11401161
self.sender
1141-
.send(ChainStateCommand::GetBlockMeta { block_hash, resp })
1162+
.send(ChainStateCommand::GetBlockMeta { block_identifier, resp })
11421163
.await?;
11431164
resp_rx.await?
11441165
}

0 commit comments

Comments
 (0)