Skip to content

Commit 80acd5f

Browse files
committed
Update rpc trait to use &str when possible
1 parent 69182a1 commit 80acd5f

File tree

5 files changed

+68
-83
lines changed

5 files changed

+68
-83
lines changed

node/src/bin/space-cli.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ impl SpaceCli {
264264
let result = self
265265
.client
266266
.wallet_send_request(
267-
self.wallet.clone(),
267+
&self.wallet,
268268
RpcWalletTxBuilder {
269269
auction_outputs,
270270
requests: match req {
@@ -373,7 +373,7 @@ async fn handle_commands(
373373
for (priority, spacehash) in hashes {
374374
let outpoint = cli
375375
.client
376-
.get_space_owner(hex::encode(spacehash.as_slice()))
376+
.get_space_owner(&hex::encode(spacehash.as_slice()))
377377
.await?;
378378

379379
if let Some(outpoint) = outpoint {
@@ -405,30 +405,30 @@ async fn handle_commands(
405405
}
406406
Commands::GetSpace { space } => {
407407
let space = normalize_space(&space);
408-
let response = cli.client.get_space(space).await?;
408+
let response = cli.client.get_space(&space).await?;
409409
println!("{}", serde_json::to_string_pretty(&response)?);
410410
}
411411
Commands::GetSpaceOut { outpoint } => {
412412
let response = cli.client.get_spaceout(outpoint).await?;
413413
println!("{}", serde_json::to_string_pretty(&response)?);
414414
}
415415
Commands::CreateWallet { name } => {
416-
cli.client.wallet_create(name).await?;
416+
cli.client.wallet_create(&name).await?;
417417
}
418418
Commands::LoadWallet { name } => {
419-
cli.client.wallet_load(name).await?;
419+
cli.client.wallet_load(&name).await?;
420420
}
421421
Commands::ImportWallet { path } => {
422422
let content =
423423
fs::read_to_string(path).map_err(|e| ClientError::Custom(e.to_string()))?;
424-
cli.client.wallet_import(content).await?;
424+
cli.client.wallet_import(&content).await?;
425425
}
426426
Commands::ExportWallet { name } => {
427-
let result = cli.client.wallet_export(name).await?;
427+
let result = cli.client.wallet_export(&name).await?;
428428
println!("{}", result);
429429
}
430430
Commands::GetWalletInfo { name } => {
431-
let result = cli.client.wallet_get_info(name).await?;
431+
let result = cli.client.wallet_get_info(&name).await?;
432432
println!("{}", serde_json::to_string_pretty(&result).expect("result"));
433433
}
434434
Commands::GetServerInfo => {
@@ -543,43 +543,40 @@ async fn handle_commands(
543543
.await?;
544544
}
545545
Commands::ListUnspent => {
546-
let spaces = cli.client.wallet_list_unspent(cli.wallet.clone()).await?;
546+
let spaces = cli.client.wallet_list_unspent(&cli.wallet).await?;
547547
println!("{}", serde_json::to_string_pretty(&spaces)?);
548548
}
549549
Commands::ListAuctionOutputs => {
550-
let spaces = cli
551-
.client
552-
.wallet_list_auction_outputs(cli.wallet.clone())
553-
.await?;
550+
let spaces = cli.client.wallet_list_auction_outputs(&cli.wallet).await?;
554551
println!("{}", serde_json::to_string_pretty(&spaces)?);
555552
}
556553
Commands::ListSpaces => {
557-
let spaces = cli.client.wallet_list_spaces(cli.wallet.clone()).await?;
554+
let spaces = cli.client.wallet_list_spaces(&cli.wallet).await?;
558555
println!("{}", serde_json::to_string_pretty(&spaces)?);
559556
}
560557
Commands::Balance => {
561-
let balance = cli.client.wallet_get_balance(cli.wallet.clone()).await?;
558+
let balance = cli.client.wallet_get_balance(&cli.wallet).await?;
562559
println!("{}", serde_json::to_string_pretty(&balance)?);
563560
}
564561
Commands::GetCoinAddress => {
565562
let response = cli
566563
.client
567-
.wallet_get_new_address(cli.wallet.clone(), AddressKind::Coin)
564+
.wallet_get_new_address(&cli.wallet, AddressKind::Coin)
568565
.await?;
569566
println!("{}", response);
570567
}
571568
Commands::GetSpaceAddress => {
572569
let response = cli
573570
.client
574-
.wallet_get_new_address(cli.wallet.clone(), AddressKind::Space)
571+
.wallet_get_new_address(&cli.wallet, AddressKind::Space)
575572
.await?;
576573
println!("{}", response);
577574
}
578575
Commands::BumpFee { txid, fee_rate } => {
579576
let fee_rate = FeeRate::from_sat_per_vb(fee_rate).expect("valid fee rate");
580577
let response = cli
581578
.client
582-
.wallet_bump_fee(cli.wallet.clone(), txid, fee_rate)
579+
.wallet_bump_fee(&cli.wallet, txid, fee_rate)
583580
.await?;
584581
println!("{}", serde_json::to_string_pretty(&response)?);
585582
}

node/src/rpc.rs

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ pub trait Rpc {
9797
async fn get_server_info(&self) -> Result<ServerInfo, ErrorObjectOwned>;
9898

9999
#[method(name = "getspace")]
100-
async fn get_space(&self, space: String) -> Result<Option<FullSpaceOut>, ErrorObjectOwned>;
100+
async fn get_space(&self, space: &str) -> Result<Option<FullSpaceOut>, ErrorObjectOwned>;
101101

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

105105
#[method(name = "getspaceout")]
106106
async fn get_spaceout(&self, outpoint: OutPoint) -> Result<Option<SpaceOut>, ErrorObjectOwned>;
@@ -118,62 +118,58 @@ pub trait Rpc {
118118
) -> Result<Option<ValidatedBlock>, ErrorObjectOwned>;
119119

120120
#[method(name = "walletload")]
121-
async fn wallet_load(&self, name: String) -> Result<(), ErrorObjectOwned>;
121+
async fn wallet_load(&self, name: &str) -> Result<(), ErrorObjectOwned>;
122122

123123
#[method(name = "walletimport")]
124-
async fn wallet_import(&self, content: String) -> Result<(), ErrorObjectOwned>;
124+
async fn wallet_import(&self, content: &str) -> Result<(), ErrorObjectOwned>;
125125

126126
#[method(name = "walletgetinfo")]
127-
async fn wallet_get_info(&self, name: String) -> Result<WalletInfo, ErrorObjectOwned>;
127+
async fn wallet_get_info(&self, name: &str) -> Result<WalletInfo, ErrorObjectOwned>;
128128

129129
#[method(name = "walletexport")]
130-
async fn wallet_export(&self, name: String) -> Result<String, ErrorObjectOwned>;
130+
async fn wallet_export(&self, name: &str) -> Result<String, ErrorObjectOwned>;
131131

132132
#[method(name = "walletcreate")]
133-
async fn wallet_create(&self, name: String) -> Result<(), ErrorObjectOwned>;
133+
async fn wallet_create(&self, name: &str) -> Result<(), ErrorObjectOwned>;
134134

135135
#[method(name = "walletsendrequest")]
136136
async fn wallet_send_request(
137137
&self,
138-
wallet: String,
138+
wallet: &str,
139139
request: RpcWalletTxBuilder,
140140
) -> Result<WalletResponse, ErrorObjectOwned>;
141141

142142
#[method(name = "walletgetnewaddress")]
143143
async fn wallet_get_new_address(
144144
&self,
145-
wallet: String,
145+
wallet: &str,
146146
kind: AddressKind,
147147
) -> Result<String, ErrorObjectOwned>;
148148

149149
#[method(name = "walletbumpfee")]
150150
async fn wallet_bump_fee(
151151
&self,
152-
wallet: String,
152+
wallet: &str,
153153
txid: Txid,
154154
fee_rate: FeeRate,
155155
) -> Result<Vec<TxResponse>, ErrorObjectOwned>;
156156

157157
#[method(name = "walletlistspaces")]
158-
async fn wallet_list_spaces(
159-
&self,
160-
wallet: String,
161-
) -> Result<Vec<FullSpaceOut>, ErrorObjectOwned>;
158+
async fn wallet_list_spaces(&self, wallet: &str)
159+
-> Result<Vec<FullSpaceOut>, ErrorObjectOwned>;
162160

163161
#[method(name = "walletlistunspent")]
164-
async fn wallet_list_unspent(
165-
&self,
166-
wallet: String,
167-
) -> Result<Vec<LocalOutput>, ErrorObjectOwned>;
162+
async fn wallet_list_unspent(&self, wallet: &str)
163+
-> Result<Vec<LocalOutput>, ErrorObjectOwned>;
168164

169165
#[method(name = "walletlistauctionoutputs")]
170166
async fn wallet_list_auction_outputs(
171167
&self,
172-
wallet: String,
168+
wallet: &str,
173169
) -> Result<Vec<DoubleUtxo>, ErrorObjectOwned>;
174170

175171
#[method(name = "walletgetbalance")]
176-
async fn wallet_get_balance(&self, wallet: String) -> Result<JointBalance, ErrorObjectOwned>;
172+
async fn wallet_get_balance(&self, wallet: &str) -> Result<JointBalance, ErrorObjectOwned>;
177173
}
178174

179175
#[derive(Clone, Serialize, Deserialize)]
@@ -296,29 +292,25 @@ impl WalletManager {
296292
let mut file = fs::File::create(wallet_export_path)?;
297293
file.write_all(wallet.to_string().as_bytes())?;
298294

299-
self.load_wallet(client, wallet.label).await?;
295+
self.load_wallet(client, &wallet.label).await?;
300296
Ok(())
301297
}
302298

303-
pub async fn export_wallet(&self, name: String) -> anyhow::Result<String> {
304-
let wallet_dir = self.data_dir.join(&name);
299+
pub async fn export_wallet(&self, name: &str) -> anyhow::Result<String> {
300+
let wallet_dir = self.data_dir.join(name);
305301
if !wallet_dir.exists() {
306302
return Err(anyhow!("Wallet does not exist"));
307303
}
308304
Ok(fs::read_to_string(wallet_dir.join("wallet.json"))?)
309305
}
310306

311-
pub async fn create_wallet(
312-
&self,
313-
client: &reqwest::Client,
314-
name: String,
315-
) -> anyhow::Result<()> {
307+
pub async fn create_wallet(&self, client: &reqwest::Client, name: &str) -> anyhow::Result<()> {
316308
let mnemonic: GeneratedKey<_, Tap> =
317309
Mnemonic::generate((WordCount::Words12, Language::English))
318310
.map_err(|_| anyhow!("Mnemonic generation error"))?;
319311

320312
let start_block = self.get_wallet_start_block(client).await?;
321-
self.setup_new_wallet(name.clone(), mnemonic.to_string(), start_block)?;
313+
self.setup_new_wallet(name.to_string(), mnemonic.to_string(), start_block)?;
322314
self.load_wallet(client, name).await?;
323315
Ok(())
324316
}
@@ -440,11 +432,11 @@ impl WalletManager {
440432
Ok(true)
441433
}
442434

443-
pub async fn load_wallet(&self, client: &reqwest::Client, name: String) -> anyhow::Result<()> {
444-
let wallet_dir = self.data_dir.join(name.clone());
435+
pub async fn load_wallet(&self, client: &reqwest::Client, name: &str) -> anyhow::Result<()> {
436+
let wallet_dir = self.data_dir.join(name);
445437
if !wallet_dir.exists() {
446438
if self
447-
.migrate_legacy_v0_0_1_wallet(name.clone(), wallet_dir.clone())
439+
.migrate_legacy_v0_0_1_wallet(name.to_string(), wallet_dir.clone())
448440
.await?
449441
{
450442
info!("Migrated legacy wallet {}", name);
@@ -461,7 +453,7 @@ impl WalletManager {
461453
let mut wallet = SpacesWallet::new(WalletConfig {
462454
start_block: export.block_height,
463455
data_dir: wallet_dir,
464-
name: name.clone(),
456+
name: name.to_string(),
465457
network,
466458
genesis_hash,
467459
coins_descriptors: export.descriptors(),
@@ -482,7 +474,7 @@ impl WalletManager {
482474

483475
self.wallet_loader.send(loaded_wallet).await?;
484476
let mut wallets = self.wallets.write().await;
485-
wallets.insert(name, rpc_wallet);
477+
wallets.insert(name.to_string(), rpc_wallet);
486478
Ok(())
487479
}
488480

@@ -611,7 +603,7 @@ impl RpcServer for RpcServerImpl {
611603
Ok(ServerInfo { chain, tip })
612604
}
613605

614-
async fn get_space(&self, space: String) -> Result<Option<FullSpaceOut>, ErrorObjectOwned> {
606+
async fn get_space(&self, space: &str) -> Result<Option<FullSpaceOut>, ErrorObjectOwned> {
615607
let space = SName::from_str(&space).map_err(|_| {
616608
ErrorObjectOwned::owned(
617609
-1,
@@ -629,8 +621,8 @@ impl RpcServer for RpcServerImpl {
629621
Ok(info)
630622
}
631623

632-
async fn get_space_owner(&self, space: String) -> Result<Option<OutPoint>, ErrorObjectOwned> {
633-
let space = SName::from_str(&space).map_err(|_| {
624+
async fn get_space_owner(&self, space: &str) -> Result<Option<OutPoint>, ErrorObjectOwned> {
625+
let space = SName::from_str(space).map_err(|_| {
634626
ErrorObjectOwned::owned(
635627
-1,
636628
"must be a valid canonical space name (e.g. @bitcoin)",
@@ -688,7 +680,7 @@ impl RpcServer for RpcServerImpl {
688680
Ok(data)
689681
}
690682

691-
async fn wallet_load(&self, name: String) -> Result<(), ErrorObjectOwned> {
683+
async fn wallet_load(&self, name: &str) -> Result<(), ErrorObjectOwned> {
692684
self.wallet_manager
693685
.load_wallet(&self.client, name)
694686
.await
@@ -697,24 +689,24 @@ impl RpcServer for RpcServerImpl {
697689
})
698690
}
699691

700-
async fn wallet_import(&self, content: String) -> Result<(), ErrorObjectOwned> {
692+
async fn wallet_import(&self, content: &str) -> Result<(), ErrorObjectOwned> {
701693
self.wallet_manager
702-
.import_wallet(&self.client, &content)
694+
.import_wallet(&self.client, content)
703695
.await
704696
.map_err(|error| {
705697
ErrorObjectOwned::owned(RPC_WALLET_NOT_LOADED, error.to_string(), None::<String>)
706698
})
707699
}
708700

709-
async fn wallet_get_info(&self, wallet: String) -> Result<WalletInfo, ErrorObjectOwned> {
701+
async fn wallet_get_info(&self, wallet: &str) -> Result<WalletInfo, ErrorObjectOwned> {
710702
self.wallet(&wallet)
711703
.await?
712704
.send_get_info()
713705
.await
714706
.map_err(|error| ErrorObjectOwned::owned(-1, error.to_string(), None::<String>))
715707
}
716708

717-
async fn wallet_export(&self, name: String) -> Result<String, ErrorObjectOwned> {
709+
async fn wallet_export(&self, name: &str) -> Result<String, ErrorObjectOwned> {
718710
self.wallet_manager
719711
.export_wallet(name)
720712
.await
@@ -723,17 +715,17 @@ impl RpcServer for RpcServerImpl {
723715
})
724716
}
725717

726-
async fn wallet_create(&self, name: String) -> Result<(), ErrorObjectOwned> {
718+
async fn wallet_create(&self, name: &str) -> Result<(), ErrorObjectOwned> {
727719
self.wallet_manager
728-
.create_wallet(&self.client, name.clone())
720+
.create_wallet(&self.client, name)
729721
.await
730722
.map_err(|error| {
731723
ErrorObjectOwned::owned(RPC_WALLET_NOT_LOADED, error.to_string(), None::<String>)
732724
})
733725
}
734726
async fn wallet_send_request(
735727
&self,
736-
wallet: String,
728+
wallet: &str,
737729
request: RpcWalletTxBuilder,
738730
) -> Result<WalletResponse, ErrorObjectOwned> {
739731
let result = self
@@ -747,7 +739,7 @@ impl RpcServer for RpcServerImpl {
747739

748740
async fn wallet_get_new_address(
749741
&self,
750-
wallet: String,
742+
wallet: &str,
751743
kind: AddressKind,
752744
) -> Result<String, ErrorObjectOwned> {
753745
self.wallet(&wallet)
@@ -759,7 +751,7 @@ impl RpcServer for RpcServerImpl {
759751

760752
async fn wallet_bump_fee(
761753
&self,
762-
wallet: String,
754+
wallet: &str,
763755
txid: Txid,
764756
fee_rate: FeeRate,
765757
) -> Result<Vec<TxResponse>, ErrorObjectOwned> {
@@ -772,7 +764,7 @@ impl RpcServer for RpcServerImpl {
772764

773765
async fn wallet_list_spaces(
774766
&self,
775-
wallet: String,
767+
wallet: &str,
776768
) -> Result<Vec<FullSpaceOut>, ErrorObjectOwned> {
777769
self.wallet(&wallet)
778770
.await?
@@ -783,7 +775,7 @@ impl RpcServer for RpcServerImpl {
783775

784776
async fn wallet_list_unspent(
785777
&self,
786-
wallet: String,
778+
wallet: &str,
787779
) -> Result<Vec<LocalOutput>, ErrorObjectOwned> {
788780
self.wallet(&wallet)
789781
.await?
@@ -794,7 +786,7 @@ impl RpcServer for RpcServerImpl {
794786

795787
async fn wallet_list_auction_outputs(
796788
&self,
797-
wallet: String,
789+
wallet: &str,
798790
) -> Result<Vec<DoubleUtxo>, ErrorObjectOwned> {
799791
self.wallet(&wallet)
800792
.await?
@@ -803,7 +795,7 @@ impl RpcServer for RpcServerImpl {
803795
.map_err(|error| ErrorObjectOwned::owned(-1, error.to_string(), None::<String>))
804796
}
805797

806-
async fn wallet_get_balance(&self, wallet: String) -> Result<JointBalance, ErrorObjectOwned> {
798+
async fn wallet_get_balance(&self, wallet: &str) -> Result<JointBalance, ErrorObjectOwned> {
807799
self.wallet(&wallet)
808800
.await?
809801
.send_get_balance()

0 commit comments

Comments
 (0)