Skip to content

Commit 67af498

Browse files
authored
Merge pull request #6 from buffrr/spacehash-rpc
Simplify some RPC methods
2 parents 951a2a5 + 4e24281 commit 67af498

File tree

6 files changed

+57
-90
lines changed

6 files changed

+57
-90
lines changed

node/src/bin/space-cli.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ use jsonrpsee::{
1010
};
1111
use protocol::{
1212
bitcoin::{Amount, FeeRate, OutPoint, Txid},
13-
hasher::{KeyHasher, SpaceHash},
1413
opcodes::OP_SETALL,
15-
sname::{NameLike, SName},
16-
Covenant,
14+
Covenant, FullSpaceOut,
1715
};
1816
use serde::{Deserialize, Serialize};
1917
use spaced::{
@@ -22,7 +20,6 @@ use spaced::{
2220
BidParams, ExecuteParams, OpenParams, RegisterParams, RpcClient, RpcWalletRequest,
2321
RpcWalletTxBuilder, SendCoinsParams, TransferSpacesParams,
2422
},
25-
store::Sha256,
2623
wallets::AddressKind,
2724
};
2825

@@ -374,12 +371,15 @@ async fn handle_commands(
374371
let hashes = cli.client.get_rollout(target).await?;
375372
let mut spaceouts = Vec::with_capacity(hashes.len());
376373
for (priority, spacehash) in hashes {
377-
let info = cli
374+
let outpoint = cli
378375
.client
379-
.get_space_info(hex::encode(spacehash.as_slice()))
376+
.get_space_owner(hex::encode(spacehash.as_slice()))
380377
.await?;
381-
if let Some(space) = info {
382-
spaceouts.push((priority, space));
378+
379+
if let Some(outpoint) = outpoint {
380+
if let Some(spaceout) = cli.client.get_spaceout(outpoint).await? {
381+
spaceouts.push((priority, FullSpaceOut { outpoint, spaceout }));
382+
}
383383
}
384384
}
385385

@@ -404,13 +404,8 @@ async fn handle_commands(
404404
println!("{} sat", Amount::from_sat(response).to_string());
405405
}
406406
Commands::GetSpace { space } => {
407-
let space = SName::try_from(normalize_space(&space).as_str())
408-
.map_err(|e| ClientError::Custom(format!("Invalid space name: {}", e)))?;
409-
let spacehash = SpaceHash::from(Sha256::hash(space.to_bytes()));
410-
let response = cli
411-
.client
412-
.get_space_info(hex::encode(spacehash.as_slice()))
413-
.await?;
407+
let space = normalize_space(&space);
408+
let response = cli.client.get_space(space).await?;
414409
println!("{}", serde_json::to_string_pretty(&response)?);
415410
}
416411
Commands::GetSpaceOut { outpoint } => {

node/src/rpc.rs

Lines changed: 42 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ use protocol::{
2323
OutPoint,
2424
},
2525
constants::ChainAnchor,
26-
hasher::{BaseHash, SpaceHash},
26+
hasher::{BaseHash, KeyHasher, SpaceHash},
2727
prepare::DataSource,
28+
sname::{NameLike, SName},
2829
FullSpaceOut, SpaceOut,
2930
};
3031
use serde::{Deserialize, Serialize};
@@ -42,7 +43,7 @@ use crate::{
4243
config::ExtendedNetwork,
4344
node::ValidatedBlock,
4445
source::BitcoinRpc,
45-
store::{ChainState, LiveSnapshot},
46+
store::{ChainState, LiveSnapshot, Sha256},
4647
wallets::{AddressKind, JointBalance, RpcWallet, TxResponse, WalletCommand, WalletResponse},
4748
};
4849

@@ -58,7 +59,7 @@ pub enum ChainStateCommand {
5859
GetTip {
5960
resp: Responder<anyhow::Result<ChainAnchor>>,
6061
},
61-
GetSpaceInfo {
62+
GetSpace {
6263
hash: SpaceHash,
6364
resp: Responder<anyhow::Result<Option<FullSpaceOut>>>,
6465
},
@@ -95,27 +96,21 @@ pub trait Rpc {
9596
#[method(name = "getserverinfo")]
9697
async fn get_server_info(&self) -> Result<ServerInfo, ErrorObjectOwned>;
9798

98-
#[method(name = "getspaceinfo")]
99-
async fn get_space_info(
100-
&self,
101-
space_hash: String,
102-
) -> Result<Option<FullSpaceOut>, ErrorObjectOwned>;
99+
#[method(name = "getspace")]
100+
async fn get_space(&self, space: String) -> Result<Option<FullSpaceOut>, ErrorObjectOwned>;
101+
102+
#[method(name = "getspaceowner")]
103+
async fn get_space_owner(&self, space: String) -> Result<Option<OutPoint>, ErrorObjectOwned>;
104+
105+
#[method(name = "getspaceout")]
106+
async fn get_spaceout(&self, outpoint: OutPoint) -> Result<Option<SpaceOut>, ErrorObjectOwned>;
103107

104108
#[method(name = "estimatebid")]
105109
async fn estimate_bid(&self, target: usize) -> Result<u64, ErrorObjectOwned>;
106110

107111
#[method(name = "getrollout")]
108112
async fn get_rollout(&self, target: usize) -> Result<Vec<(u32, SpaceHash)>, ErrorObjectOwned>;
109113

110-
#[method(name = "getspaceowner")]
111-
async fn get_space_owner(
112-
&self,
113-
space_hash: String,
114-
) -> Result<Option<OutPoint>, ErrorObjectOwned>;
115-
116-
#[method(name = "getspaceout")]
117-
async fn get_spaceout(&self, outpoint: OutPoint) -> Result<Option<SpaceOut>, ErrorObjectOwned>;
118-
119114
#[method(name = "getblockdata")]
120115
async fn get_block_data(
121116
&self,
@@ -615,71 +610,33 @@ impl RpcServer for RpcServerImpl {
615610
Ok(ServerInfo { chain, tip })
616611
}
617612

618-
async fn get_space_info(
619-
&self,
620-
space_hash_str: String,
621-
) -> Result<Option<FullSpaceOut>, ErrorObjectOwned> {
622-
let mut space_hash = [0u8; 32];
623-
hex::decode_to_slice(space_hash_str, &mut space_hash).map_err(|_| {
624-
ErrorObjectOwned::owned(
625-
-1,
626-
"expected a 32-byte hex encoded space hash a",
627-
None::<String>,
628-
)
629-
})?;
630-
let space_hash = SpaceHash::from_raw(space_hash).map_err(|_| {
613+
async fn get_space(&self, space: String) -> Result<Option<FullSpaceOut>, ErrorObjectOwned> {
614+
let space = SName::from_str(&space).map_err(|_| {
631615
ErrorObjectOwned::owned(
632616
-1,
633-
"expected a 32-byte hex encoded space hash b",
617+
"must be a valid canonical space name (e.g. @bitcoin)",
634618
None::<String>,
635619
)
636620
})?;
637621

622+
let space_hash = SpaceHash::from(Sha256::hash(space.to_bytes()));
638623
let info = self
639624
.store
640-
.get_space_info(space_hash)
625+
.get_space(space_hash)
641626
.await
642627
.map_err(|error| ErrorObjectOwned::owned(-1, error.to_string(), None::<String>))?;
643628
Ok(info)
644629
}
645630

646-
async fn estimate_bid(&self, target: usize) -> Result<u64, ErrorObjectOwned> {
647-
let info = self
648-
.store
649-
.estimate_bid(target)
650-
.await
651-
.map_err(|error| ErrorObjectOwned::owned(-1, error.to_string(), None::<String>))?;
652-
Ok(info)
653-
}
654-
655-
async fn get_rollout(&self, target: usize) -> Result<Vec<(u32, SpaceHash)>, ErrorObjectOwned> {
656-
let rollouts = self
657-
.store
658-
.get_rollout(target)
659-
.await
660-
.map_err(|error| ErrorObjectOwned::owned(-1, error.to_string(), None::<String>))?;
661-
Ok(rollouts)
662-
}
663-
664-
async fn get_space_owner(
665-
&self,
666-
space_hash_str: String,
667-
) -> Result<Option<OutPoint>, ErrorObjectOwned> {
668-
let mut space_hash = [0u8; 32];
669-
hex::decode_to_slice(space_hash_str, &mut space_hash).map_err(|_| {
631+
async fn get_space_owner(&self, space: String) -> Result<Option<OutPoint>, ErrorObjectOwned> {
632+
let space = SName::from_str(&space).map_err(|_| {
670633
ErrorObjectOwned::owned(
671634
-1,
672-
"expected a 32-byte hex encoded space hash",
673-
None::<String>,
674-
)
675-
})?;
676-
let space_hash = SpaceHash::from_raw(space_hash).map_err(|_| {
677-
ErrorObjectOwned::owned(
678-
-1,
679-
"expected a 32-byte hex encoded space hash",
635+
"must be a valid canonical space name (e.g. @bitcoin)",
680636
None::<String>,
681637
)
682638
})?;
639+
let space_hash = SpaceHash::from(Sha256::hash(space.to_bytes()));
683640

684641
let info = self
685642
.store
@@ -699,6 +656,24 @@ impl RpcServer for RpcServerImpl {
699656
Ok(spaceout)
700657
}
701658

659+
async fn estimate_bid(&self, target: usize) -> Result<u64, ErrorObjectOwned> {
660+
let info = self
661+
.store
662+
.estimate_bid(target)
663+
.await
664+
.map_err(|error| ErrorObjectOwned::owned(-1, error.to_string(), None::<String>))?;
665+
Ok(info)
666+
}
667+
668+
async fn get_rollout(&self, target: usize) -> Result<Vec<(u32, SpaceHash)>, ErrorObjectOwned> {
669+
let rollouts = self
670+
.store
671+
.get_rollout(target)
672+
.await
673+
.map_err(|error| ErrorObjectOwned::owned(-1, error.to_string(), None::<String>))?;
674+
Ok(rollouts)
675+
}
676+
702677
async fn get_block_data(
703678
&self,
704679
block_hash: BlockHash,
@@ -851,7 +826,7 @@ impl AsyncChainState {
851826
let tip = chain_state.tip.read().expect("read meta").clone();
852827
_ = resp.send(Ok(tip))
853828
}
854-
ChainStateCommand::GetSpaceInfo { hash, resp } => {
829+
ChainStateCommand::GetSpace { hash, resp } => {
855830
let result = chain_state.get_space_info(&hash);
856831
let _ = resp.send(result);
857832
}
@@ -927,10 +902,10 @@ impl AsyncChainState {
927902
resp_rx.await?
928903
}
929904

930-
pub async fn get_space_info(&self, hash: SpaceHash) -> anyhow::Result<Option<FullSpaceOut>> {
905+
pub async fn get_space(&self, hash: SpaceHash) -> anyhow::Result<Option<FullSpaceOut>> {
931906
let (resp, resp_rx) = oneshot::channel();
932907
self.sender
933-
.send(ChainStateCommand::GetSpaceInfo { hash, resp })
908+
.send(ChainStateCommand::GetSpace { hash, resp })
934909
.await?;
935910
resp_rx.await?
936911
}

protocol/src/hasher.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use crate::alloc::string::ToString;
21
#[cfg(feature = "bincode")]
32
use bincode::{Decode, Encode};
43
use bitcoin::{Amount, OutPoint};
54
#[cfg(feature = "serde")]
65
use serde::{Deserialize, Serialize};
76

7+
use crate::alloc::string::ToString;
8+
89
pub type Hash = [u8; 32];
910

1011
pub trait KeyHasher {

protocol/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ extern crate core;
55

66
pub extern crate bitcoin;
77

8-
use alloc::vec;
9-
use alloc::vec::Vec;
8+
use alloc::{vec, vec::Vec};
109

1110
#[cfg(feature = "bincode")]
1211
use bincode::{Decode, Encode};

protocol/src/script.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use alloc::collections::btree_map::BTreeMap;
2-
use alloc::vec::Vec;
1+
use alloc::{collections::btree_map::BTreeMap, vec::Vec};
32

43
#[cfg(feature = "bincode")]
54
use bincode::{Decode, Encode};

protocol/src/validate.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use alloc::collections::btree_map::BTreeMap;
2-
use alloc::vec;
3-
use alloc::vec::Vec;
1+
use alloc::{collections::btree_map::BTreeMap, vec, vec::Vec};
42

53
#[cfg(feature = "bincode")]
64
use bincode::{Decode, Encode};

0 commit comments

Comments
 (0)