Skip to content

Commit

Permalink
rebase 5.3.X [PATCH 116/142] Replace failure with thiserror (mimb…
Browse files Browse the repository at this point in the history
  • Loading branch information
bayk committed Aug 13, 2024
1 parent 0a44698 commit 652e698
Show file tree
Hide file tree
Showing 112 changed files with 2,453 additions and 3,292 deletions.
21 changes: 6 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ exclude = ["integration"]
[dependencies]
clap = { version = "2.33", features = ["yaml"] }
rpassword = "4.0"
failure = "0.1"
failure_derive = "0.1"
thiserror = "1"
prettytable-rs = "0.10.0"
log = "0.4"
linefeed = "0.6"
Expand Down
2 changes: 0 additions & 2 deletions api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ exclude = ["**/*.grin", "**/*.grin2"]
edition = "2018"

[dependencies]
failure = "0.1"
failure_derive = "0.1"
log = "0.4"
uuid = { version = "0.8", features = ["serde", "v4"] }
serde = "1"
Expand Down
94 changes: 35 additions & 59 deletions api/src/foreign_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
use crate::keychain::Keychain;
use crate::libwallet::{
self, BlockFees, CbData, ErrorKind, InitTxArgs, IssueInvoiceTxArgs, NodeClient,
NodeVersionInfo, Slate, SlateVersion, VersionInfo, VersionedCoinbase, VersionedSlate,
WalletLCProvider,
self, BlockFees, CbData, Error, InitTxArgs, IssueInvoiceTxArgs, NodeClient, NodeVersionInfo,
Slate, SlateVersion, VersionInfo, VersionedCoinbase, VersionedSlate, WalletLCProvider,
};
use crate::{Foreign, ForeignCheckMiddlewareFn};
use easy_jsonrpc_mw;
Expand Down Expand Up @@ -69,7 +68,7 @@ pub trait ForeignRpc {
# ,false, 0, false, false, true);
```
*/
fn check_version(&self) -> Result<VersionInfo, ErrorKind>;
fn check_version(&self) -> Result<VersionInfo, Error>;

/**
Networked version of [Foreign::check_version](struct.Foreign.html#method.check_version).
Expand Down Expand Up @@ -99,7 +98,7 @@ pub trait ForeignRpc {
# ,false, 0, false, false, true);
```
*/
fn get_proof_address(&self) -> Result<String, ErrorKind>;
fn get_proof_address(&self) -> Result<String, Error>;

/**
Networked Legacy (non-secure token) version of [Foreign::build_coinbase](struct.Foreign.html#method.build_coinbase).
Expand Down Expand Up @@ -150,7 +149,7 @@ pub trait ForeignRpc {
```
*/

fn build_coinbase(&self, block_fees: &BlockFees) -> Result<VersionedCoinbase, ErrorKind>;
fn build_coinbase(&self, block_fees: &BlockFees) -> Result<VersionedCoinbase, Error>;

/**
Networked version of [Foreign::verify_slate_messages](struct.Foreign.html#method.verify_slate_messages).
Expand Down Expand Up @@ -232,7 +231,7 @@ pub trait ForeignRpc {
# ,false, 1 ,false, false, false);
```
*/
fn verify_slate_messages(&self, slate: VersionedSlate) -> Result<(), ErrorKind>;
fn verify_slate_messages(&self, slate: VersionedSlate) -> Result<(), Error>;

/**
Networked version of [Foreign::receive_tx](struct.Foreign.html#method.receive_tx).
Expand Down Expand Up @@ -548,7 +547,7 @@ pub trait ForeignRpc {
slate: VersionedSlate,
dest_acct_name: Option<String>,
message: Option<String>,
) -> Result<VersionedSlate, ErrorKind>;
) -> Result<VersionedSlate, Error>;

/**
Expand Down Expand Up @@ -906,20 +905,20 @@ pub trait ForeignRpc {
# ,false, 5, false, true, true);
```
*/
fn finalize_invoice_tx(&self, slate: VersionedSlate) -> Result<VersionedSlate, ErrorKind>;
fn finalize_invoice_tx(&self, slate: VersionedSlate) -> Result<VersionedSlate, Error>;

/**
Networked version of [Foreign::receive_swap_message](struct.Foreign.html#method.receive_swap_message).
# Json rpc example
*/
fn receive_swap_message(&self, message: String) -> Result<(), ErrorKind>;
fn receive_swap_message(&self, message: String) -> Result<(), Error>;

/**
Networked version of [Foreign::marketplace_message](struct.Foreign.html#method.marketplace_message).
# Json rpc example
*/
fn marketplace_message(&self, accept_offer_message: &String) -> Result<String, ErrorKind>;
fn marketplace_message(&self, accept_offer_message: &String) -> Result<String, Error>;
}

impl<'a, L, C, K> ForeignRpc for Foreign<'a, L, C, K>
Expand All @@ -928,52 +927,43 @@ where
C: NodeClient + 'a,
K: Keychain + 'a,
{
fn check_version(&self) -> Result<VersionInfo, ErrorKind> {
Foreign::check_version(self).map_err(|e| e.kind())
fn check_version(&self) -> Result<VersionInfo, Error> {
Foreign::check_version(self)
}

fn get_proof_address(&self) -> Result<String, ErrorKind> {
Foreign::get_proof_address(self).map_err(|e| e.kind())
fn get_proof_address(&self) -> Result<String, Error> {
Foreign::get_proof_address(self)
}

fn build_coinbase(&self, block_fees: &BlockFees) -> Result<VersionedCoinbase, ErrorKind> {
let cb: CbData = Foreign::build_coinbase(self, block_fees).map_err(|e| e.kind())?;
fn build_coinbase(&self, block_fees: &BlockFees) -> Result<VersionedCoinbase, Error> {
let cb: CbData = Foreign::build_coinbase(self, block_fees)?;
Ok(VersionedCoinbase::into_version(cb, SlateVersion::V2))
}

// verify_slate_messages doesn't make sense for the slate pack. That is why supporting only plain slates
fn verify_slate_messages(&self, slate: VersionedSlate) -> Result<(), ErrorKind> {
Foreign::verify_slate_messages(
self,
&slate.into_slate_plain(true).map_err(|e| {
ErrorKind::Compatibility(format!("Expected non ecrypted slate only, {}", e))
})?,
)
.map_err(|e| e.kind())
fn verify_slate_messages(&self, slate: VersionedSlate) -> Result<(), Error> {
Foreign::verify_slate_messages(self, &slate.into_slate_plain(true)?)
}

fn receive_tx(
&self,
in_slate: VersionedSlate,
dest_acct_name: Option<String>,
message: Option<String>,
) -> Result<VersionedSlate, ErrorKind> {
) -> Result<VersionedSlate, Error> {
let version = in_slate.version();
let (slate_from, sender) = if in_slate.is_slatepack() {
let (slate_from, content, sender) =
Foreign::decrypt_slate(self, in_slate).map_err(|e| {
ErrorKind::SlatepackDecodeError(format!("Unable to decrypt a slatepack, {}", e))
})?;
let (slate_from, content, sender) = Foreign::decrypt_slate(self, in_slate)?;

if content != SlatePurpose::SendInitial {
return Err(ErrorKind::SlatepackDecodeError(format!(
return Err(Error::SlatepackDecodeError(format!(
"Expecting SendInitial content of the slatepack, get {:?}",
content
)));
}
(slate_from, sender)
} else {
let slate_from = in_slate.into_slate_plain(false).map_err(|e| e.kind())?;
let slate_from = in_slate.into_slate_plain(false)?;
(slate_from, None)
};
let out_slate = Foreign::receive_tx(
Expand All @@ -982,8 +972,7 @@ where
sender.map(|p| ProvableAddress::from_tor_pub_key(&p).public_key), // We don't want to change RPC. New fields required new version
dest_acct_name.as_ref().map(String::as_str),
message,
)
.map_err(|e| e.kind())?;
)?;

let res_slate = Foreign::encrypt_slate(
self,
Expand All @@ -993,36 +982,30 @@ where
sender, // sending back to the sender
None,
self.doctest_mode,
)
.map_err(|e| {
ErrorKind::SlatepackEncodeError(format!("Unable to encode the slatepack, {}", e))
})?;
)?;

Ok(res_slate)
}

fn finalize_invoice_tx(&self, in_slate: VersionedSlate) -> Result<VersionedSlate, ErrorKind> {
fn finalize_invoice_tx(&self, in_slate: VersionedSlate) -> Result<VersionedSlate, Error> {
let version = in_slate.version();
let (in_slate, sender) = if in_slate.is_slatepack() {
let (slate_from, content, sender) =
Foreign::decrypt_slate(self, in_slate).map_err(|e| {
ErrorKind::SlatepackDecodeError(format!("Unable to decrypt a slatepack, {}", e))
})?;
let (slate_from, content, sender) = Foreign::decrypt_slate(self, in_slate)?;

if content != SlatePurpose::InvoiceResponse {
return Err(ErrorKind::SlatepackDecodeError(format!(
return Err(Error::SlatepackDecodeError(format!(
"Expecting InvoiceResponse content of the slatepack, get {:?}",
content
)));
}

(slate_from, sender)
} else {
let slate_from = in_slate.into_slate_plain(false).map_err(|e| e.kind())?;
let slate_from = in_slate.into_slate_plain(false)?;
(slate_from, None)
};

let out_slate = Foreign::finalize_invoice_tx(self, &in_slate).map_err(|e| e.kind())?;
let out_slate = Foreign::finalize_invoice_tx(self, &in_slate)?;

let res_slate = Foreign::encrypt_slate(
self,
Expand All @@ -1032,24 +1015,17 @@ where
sender, // sending back to the sender
None,
self.doctest_mode,
)
.map_err(|e| {
ErrorKind::SlatepackEncodeError(format!("Unable to encode the slatepack, {}", e))
})?;
)?;
Ok(res_slate)
}

fn receive_swap_message(&self, message: String) -> Result<(), ErrorKind> {
Foreign::receive_swap_message(&self, &message).map_err(|e| {
ErrorKind::SwapError(format!("Error encountered receiving swap message, {}", e))
})?;
fn receive_swap_message(&self, message: String) -> Result<(), Error> {
Foreign::receive_swap_message(&self, &message)?;
Ok(())
}

fn marketplace_message(&self, message: &String) -> Result<String, ErrorKind> {
let res = Foreign::marketplace_message(&self, &message).map_err(|e| {
ErrorKind::SwapError(format!("Error encountered receiving swap message, {}", e))
})?;
fn marketplace_message(&self, message: &String) -> Result<String, Error> {
let res = Foreign::marketplace_message(&self, &message)?;
Ok(res)
}
}
Expand All @@ -1060,7 +1036,7 @@ fn test_check_middleware(
_slate: Option<&Slate>,
) -> Result<(), libwallet::Error> {
// TODO: Implement checks
// return Err(ErrorKind::GenericError("Test Rejection".into()))?
// return Err(Error::GenericError("Test Rejection".into()))?
Ok(())
}

Expand Down
1 change: 0 additions & 1 deletion api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use grin_wallet_util::grin_util as util;
extern crate grin_wallet_impls as impls;
extern crate grin_wallet_libwallet as libwallet;

extern crate failure_derive;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
Expand Down
Loading

0 comments on commit 652e698

Please sign in to comment.