Skip to content

Commit

Permalink
Better wallet error messages (#3041)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph authored Jan 23, 2024
1 parent e967a8c commit 841e70c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
37 changes: 22 additions & 15 deletions src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl Wallet {
if response.text()?.parse::<u64>().unwrap() >= chain_block_count {
break;
} else if i == 20 {
bail!("wallet failed to synchronize to index");
bail!("wallet failed to synchronize with ord server");
}

thread::sleep(Duration::from_millis(50));
Expand All @@ -92,10 +92,14 @@ impl Wallet {
.get(self.ord_url.join(&format!("/output/{output}")).unwrap())
.send()?;

if !response.status().is_success() {
bail!("wallet failed get output: {}", response.text()?);
}

let output_json: OutputJson = serde_json::from_str(&response.text()?)?;

if !output_json.indexed {
bail!("output in Bitcoin Core wallet but not in ord index: {output}");
bail!("output in wallet but not in ord server: {output}");
}

Ok(output_json)
Expand Down Expand Up @@ -141,15 +145,15 @@ impl Wallet {
pub(crate) fn get_output_sat_ranges(&self) -> Result<Vec<(OutPoint, Vec<(u64, u64)>)>> {
ensure!(
self.has_sat_index()?,
"index must be built with `--index-sats` to use `--sat`"
"ord index must be built with `--index-sats` to use `--sat`"
);

let mut output_sat_ranges = Vec::new();
for output in self.get_unspent_outputs()?.keys() {
if let Some(sat_ranges) = self.get_output(output)?.sat_ranges {
output_sat_ranges.push((*output, sat_ranges));
} else {
bail!("output {output} in wallet but is spent according to index");
bail!("output {output} in wallet but is spent according to ord server");
}
}

Expand All @@ -163,7 +167,7 @@ impl Wallet {
) -> Result<SatPoint> {
ensure!(
self.has_sat_index()?,
"index must be built with `--index-sats` to use `--sat`"
"ord index must be built with `--index-sats` to use `--sat`"
);

for output in utxos.keys() {
Expand Down Expand Up @@ -215,7 +219,7 @@ impl Wallet {
)
.send()?;

if response.status().is_client_error() {
if !response.status().is_success() {
bail!("inscription {inscription_id} not found");
}

Expand Down Expand Up @@ -251,7 +255,7 @@ impl Wallet {
)
.send()?;

if response.status().is_client_error() {
if !response.status().is_success() {
return Ok(None);
}

Expand Down Expand Up @@ -332,7 +336,7 @@ impl Wallet {
tx_out: self
.bitcoin_client()?
.get_raw_transaction(&satpoint.outpoint.txid, None)
.expect("parent transaction not found in index")
.expect("parent transaction not found in ord server")
.output
.into_iter()
.nth(satpoint.outpoint.vout.try_into().unwrap())
Expand All @@ -354,13 +358,16 @@ impl Wallet {
}

pub(crate) fn get_server_status(&self) -> Result<StatusJson> {
Ok(serde_json::from_str(
&self
.ord_client()?
.get(self.ord_url.join("/status").unwrap())
.send()?
.text()?,
)?)
let response = self
.ord_client()?
.get(self.ord_url.join("/status").unwrap())
.send()?;

if !response.status().is_success() {
bail!("could not get status: {}", response.text()?)
}

Ok(serde_json::from_str(&response.text()?)?)
}

pub(crate) fn has_rune_index(&self) -> Result<bool> {
Expand Down
6 changes: 2 additions & 4 deletions tests/wallet/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,13 @@ fn unsynced_wallet_fails_with_unindexed_output() {
.ord_rpc_server(&no_sync_ord_rpc_server)
.bitcoin_rpc_server(&bitcoin_rpc_server)
.expected_exit_code(1)
.expected_stderr("error: wallet failed to synchronize to index\n")
.expected_stderr("error: wallet failed to synchronize with ord server\n")
.run_and_extract_stdout();

CommandBuilder::new("wallet --no-sync balance")
.ord_rpc_server(&no_sync_ord_rpc_server)
.bitcoin_rpc_server(&bitcoin_rpc_server)
.expected_exit_code(1)
.stderr_regex(
r"error: output in Bitcoin Core wallet but not in ord index: [[:xdigit:]]{64}:\d+.*",
)
.stderr_regex(r"error: output in wallet but not in ord server: [[:xdigit:]]{64}:\d+.*")
.run_and_extract_stdout();
}
2 changes: 1 addition & 1 deletion tests/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1807,7 +1807,7 @@ fn inscribe_with_sat_arg_fails_if_no_index_or_not_found() {
.bitcoin_rpc_server(&bitcoin_rpc_server)
.ord_rpc_server(&ord_rpc_server)
.expected_exit_code(1)
.expected_stderr("error: index must be built with `--index-sats` to use `--sat`\n")
.expected_stderr("error: ord index must be built with `--index-sats` to use `--sat`\n")
.run_and_extract_stdout();

CommandBuilder::new("--index-sats wallet inscribe --sat 5000000000 --file foo.txt --fee-rate 1")
Expand Down

0 comments on commit 841e70c

Please sign in to comment.