Skip to content

Commit

Permalink
Merge pull request #6 from buffrr/spacehash-rpc
Browse files Browse the repository at this point in the history
Simplify some RPC methods
  • Loading branch information
buffrr authored Aug 30, 2024
2 parents 951a2a5 + 4e24281 commit 67af498
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 90 deletions.
25 changes: 10 additions & 15 deletions node/src/bin/space-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ use jsonrpsee::{
};
use protocol::{
bitcoin::{Amount, FeeRate, OutPoint, Txid},
hasher::{KeyHasher, SpaceHash},
opcodes::OP_SETALL,
sname::{NameLike, SName},
Covenant,
Covenant, FullSpaceOut,
};
use serde::{Deserialize, Serialize};
use spaced::{
Expand All @@ -22,7 +20,6 @@ use spaced::{
BidParams, ExecuteParams, OpenParams, RegisterParams, RpcClient, RpcWalletRequest,
RpcWalletTxBuilder, SendCoinsParams, TransferSpacesParams,
},
store::Sha256,
wallets::AddressKind,
};

Expand Down Expand Up @@ -374,12 +371,15 @@ async fn handle_commands(
let hashes = cli.client.get_rollout(target).await?;
let mut spaceouts = Vec::with_capacity(hashes.len());
for (priority, spacehash) in hashes {
let info = cli
let outpoint = cli
.client
.get_space_info(hex::encode(spacehash.as_slice()))
.get_space_owner(hex::encode(spacehash.as_slice()))
.await?;
if let Some(space) = info {
spaceouts.push((priority, space));

if let Some(outpoint) = outpoint {
if let Some(spaceout) = cli.client.get_spaceout(outpoint).await? {
spaceouts.push((priority, FullSpaceOut { outpoint, spaceout }));
}
}
}

Expand All @@ -404,13 +404,8 @@ async fn handle_commands(
println!("{} sat", Amount::from_sat(response).to_string());
}
Commands::GetSpace { space } => {
let space = SName::try_from(normalize_space(&space).as_str())
.map_err(|e| ClientError::Custom(format!("Invalid space name: {}", e)))?;
let spacehash = SpaceHash::from(Sha256::hash(space.to_bytes()));
let response = cli
.client
.get_space_info(hex::encode(spacehash.as_slice()))
.await?;
let space = normalize_space(&space);
let response = cli.client.get_space(space).await?;
println!("{}", serde_json::to_string_pretty(&response)?);
}
Commands::GetSpaceOut { outpoint } => {
Expand Down
109 changes: 42 additions & 67 deletions node/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ use protocol::{
OutPoint,
},
constants::ChainAnchor,
hasher::{BaseHash, SpaceHash},
hasher::{BaseHash, KeyHasher, SpaceHash},
prepare::DataSource,
sname::{NameLike, SName},
FullSpaceOut, SpaceOut,
};
use serde::{Deserialize, Serialize};
Expand All @@ -42,7 +43,7 @@ use crate::{
config::ExtendedNetwork,
node::ValidatedBlock,
source::BitcoinRpc,
store::{ChainState, LiveSnapshot},
store::{ChainState, LiveSnapshot, Sha256},
wallets::{AddressKind, JointBalance, RpcWallet, TxResponse, WalletCommand, WalletResponse},
};

Expand All @@ -58,7 +59,7 @@ pub enum ChainStateCommand {
GetTip {
resp: Responder<anyhow::Result<ChainAnchor>>,
},
GetSpaceInfo {
GetSpace {
hash: SpaceHash,
resp: Responder<anyhow::Result<Option<FullSpaceOut>>>,
},
Expand Down Expand Up @@ -95,27 +96,21 @@ pub trait Rpc {
#[method(name = "getserverinfo")]
async fn get_server_info(&self) -> Result<ServerInfo, ErrorObjectOwned>;

#[method(name = "getspaceinfo")]
async fn get_space_info(
&self,
space_hash: String,
) -> Result<Option<FullSpaceOut>, ErrorObjectOwned>;
#[method(name = "getspace")]
async fn get_space(&self, space: String) -> Result<Option<FullSpaceOut>, ErrorObjectOwned>;

#[method(name = "getspaceowner")]
async fn get_space_owner(&self, space: String) -> Result<Option<OutPoint>, ErrorObjectOwned>;

#[method(name = "getspaceout")]
async fn get_spaceout(&self, outpoint: OutPoint) -> Result<Option<SpaceOut>, ErrorObjectOwned>;

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

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

#[method(name = "getspaceowner")]
async fn get_space_owner(
&self,
space_hash: String,
) -> Result<Option<OutPoint>, ErrorObjectOwned>;

#[method(name = "getspaceout")]
async fn get_spaceout(&self, outpoint: OutPoint) -> Result<Option<SpaceOut>, ErrorObjectOwned>;

#[method(name = "getblockdata")]
async fn get_block_data(
&self,
Expand Down Expand Up @@ -615,71 +610,33 @@ impl RpcServer for RpcServerImpl {
Ok(ServerInfo { chain, tip })
}

async fn get_space_info(
&self,
space_hash_str: String,
) -> Result<Option<FullSpaceOut>, ErrorObjectOwned> {
let mut space_hash = [0u8; 32];
hex::decode_to_slice(space_hash_str, &mut space_hash).map_err(|_| {
ErrorObjectOwned::owned(
-1,
"expected a 32-byte hex encoded space hash a",
None::<String>,
)
})?;
let space_hash = SpaceHash::from_raw(space_hash).map_err(|_| {
async fn get_space(&self, space: String) -> Result<Option<FullSpaceOut>, ErrorObjectOwned> {
let space = SName::from_str(&space).map_err(|_| {
ErrorObjectOwned::owned(
-1,
"expected a 32-byte hex encoded space hash b",
"must be a valid canonical space name (e.g. @bitcoin)",
None::<String>,
)
})?;

let space_hash = SpaceHash::from(Sha256::hash(space.to_bytes()));
let info = self
.store
.get_space_info(space_hash)
.get_space(space_hash)
.await
.map_err(|error| ErrorObjectOwned::owned(-1, error.to_string(), None::<String>))?;
Ok(info)
}

async fn estimate_bid(&self, target: usize) -> Result<u64, ErrorObjectOwned> {
let info = self
.store
.estimate_bid(target)
.await
.map_err(|error| ErrorObjectOwned::owned(-1, error.to_string(), None::<String>))?;
Ok(info)
}

async fn get_rollout(&self, target: usize) -> Result<Vec<(u32, SpaceHash)>, ErrorObjectOwned> {
let rollouts = self
.store
.get_rollout(target)
.await
.map_err(|error| ErrorObjectOwned::owned(-1, error.to_string(), None::<String>))?;
Ok(rollouts)
}

async fn get_space_owner(
&self,
space_hash_str: String,
) -> Result<Option<OutPoint>, ErrorObjectOwned> {
let mut space_hash = [0u8; 32];
hex::decode_to_slice(space_hash_str, &mut space_hash).map_err(|_| {
async fn get_space_owner(&self, space: String) -> Result<Option<OutPoint>, ErrorObjectOwned> {
let space = SName::from_str(&space).map_err(|_| {
ErrorObjectOwned::owned(
-1,
"expected a 32-byte hex encoded space hash",
None::<String>,
)
})?;
let space_hash = SpaceHash::from_raw(space_hash).map_err(|_| {
ErrorObjectOwned::owned(
-1,
"expected a 32-byte hex encoded space hash",
"must be a valid canonical space name (e.g. @bitcoin)",
None::<String>,
)
})?;
let space_hash = SpaceHash::from(Sha256::hash(space.to_bytes()));

let info = self
.store
Expand All @@ -699,6 +656,24 @@ impl RpcServer for RpcServerImpl {
Ok(spaceout)
}

async fn estimate_bid(&self, target: usize) -> Result<u64, ErrorObjectOwned> {
let info = self
.store
.estimate_bid(target)
.await
.map_err(|error| ErrorObjectOwned::owned(-1, error.to_string(), None::<String>))?;
Ok(info)
}

async fn get_rollout(&self, target: usize) -> Result<Vec<(u32, SpaceHash)>, ErrorObjectOwned> {
let rollouts = self
.store
.get_rollout(target)
.await
.map_err(|error| ErrorObjectOwned::owned(-1, error.to_string(), None::<String>))?;
Ok(rollouts)
}

async fn get_block_data(
&self,
block_hash: BlockHash,
Expand Down Expand Up @@ -851,7 +826,7 @@ impl AsyncChainState {
let tip = chain_state.tip.read().expect("read meta").clone();
_ = resp.send(Ok(tip))
}
ChainStateCommand::GetSpaceInfo { hash, resp } => {
ChainStateCommand::GetSpace { hash, resp } => {
let result = chain_state.get_space_info(&hash);
let _ = resp.send(result);
}
Expand Down Expand Up @@ -927,10 +902,10 @@ impl AsyncChainState {
resp_rx.await?
}

pub async fn get_space_info(&self, hash: SpaceHash) -> anyhow::Result<Option<FullSpaceOut>> {
pub async fn get_space(&self, hash: SpaceHash) -> anyhow::Result<Option<FullSpaceOut>> {
let (resp, resp_rx) = oneshot::channel();
self.sender
.send(ChainStateCommand::GetSpaceInfo { hash, resp })
.send(ChainStateCommand::GetSpace { hash, resp })
.await?;
resp_rx.await?
}
Expand Down
3 changes: 2 additions & 1 deletion protocol/src/hasher.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::alloc::string::ToString;
#[cfg(feature = "bincode")]
use bincode::{Decode, Encode};
use bitcoin::{Amount, OutPoint};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::alloc::string::ToString;

pub type Hash = [u8; 32];

pub trait KeyHasher {
Expand Down
3 changes: 1 addition & 2 deletions protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ extern crate core;

pub extern crate bitcoin;

use alloc::vec;
use alloc::vec::Vec;
use alloc::{vec, vec::Vec};

#[cfg(feature = "bincode")]
use bincode::{Decode, Encode};
Expand Down
3 changes: 1 addition & 2 deletions protocol/src/script.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use alloc::collections::btree_map::BTreeMap;
use alloc::vec::Vec;
use alloc::{collections::btree_map::BTreeMap, vec::Vec};

#[cfg(feature = "bincode")]
use bincode::{Decode, Encode};
Expand Down
4 changes: 1 addition & 3 deletions protocol/src/validate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use alloc::collections::btree_map::BTreeMap;
use alloc::vec;
use alloc::vec::Vec;
use alloc::{collections::btree_map::BTreeMap, vec, vec::Vec};

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

0 comments on commit 67af498

Please sign in to comment.