Skip to content

Commit 707d0e6

Browse files
committed
Simplify rpc interface to use space names instead of hashes
For now this is sufficient and easier to use until we provide more tooling and design an interface that can be exposed publicly
1 parent 951a2a5 commit 707d0e6

File tree

2 files changed

+31
-61
lines changed

2 files changed

+31
-61
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: 21 additions & 46 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,24 +96,18 @@ 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>;
103104

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

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

110-
#[method(name = "getspaceowner")]
111-
async fn get_space_owner(
112-
&self,
113-
space_hash: String,
114-
) -> Result<Option<OutPoint>, ErrorObjectOwned>;
115-
116111
#[method(name = "getspaceout")]
117112
async fn get_spaceout(&self, outpoint: OutPoint) -> Result<Option<SpaceOut>, ErrorObjectOwned>;
118113

@@ -615,29 +610,19 @@ 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)
@@ -661,25 +646,15 @@ impl RpcServer for RpcServerImpl {
661646
Ok(rollouts)
662647
}
663648

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(|_| {
670-
ErrorObjectOwned::owned(
671-
-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(|_| {
649+
async fn get_space_owner(&self, space: String) -> Result<Option<OutPoint>, ErrorObjectOwned> {
650+
let space = SName::from_str(&space).map_err(|_| {
677651
ErrorObjectOwned::owned(
678652
-1,
679-
"expected a 32-byte hex encoded space hash",
653+
"must be a valid canonical space name (e.g. @bitcoin)",
680654
None::<String>,
681655
)
682656
})?;
657+
let space_hash = SpaceHash::from(Sha256::hash(space.to_bytes()));
683658

684659
let info = self
685660
.store
@@ -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
}

0 commit comments

Comments
 (0)