Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to handle invalid coin(s) in privacy-wallet-service. #3002

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion utt/include/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,13 @@ class Transaction {
struct Impl;
Impl* pImpl_;
};
} // namespace libutt::api::operations

class InvalidCoinsInTransfer : public std::exception {
public:
explicit InvalidCoinsInTransfer(const std::string& what) : msg(what){};
virtual const char* what() const noexcept override { return msg.c_str(); }

private:
std::string msg;
};
} // namespace libutt::api::operations
3 changes: 2 additions & 1 deletion utt/privacy-wallet-service/include/PrivacyService.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "Wallet.hpp"
#include <storage/IStorage.hpp>
#include <utt-client-api/ClientApi.hpp>
#include <transaction.hpp>

namespace utt::walletservice {
//@TODO hide on its own file..
Expand Down Expand Up @@ -106,4 +107,4 @@ class PrivacyWalletService {
std::unique_ptr<grpc::Server> grpc_server_;
std::unique_ptr<PrivacyWalletServiceImpl> privacy_wallet_service_;
};
} // namespace utt::walletservice
} // namespace utt::walletservice
3 changes: 2 additions & 1 deletion utt/privacy-wallet-service/proto/api/v1/wallet-api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ message ClaimCoinsRequest {

message ClaimCoinsReponse {
bool succ = 1;
string warning = 2;
}

message GenerateMintTx {
Expand Down Expand Up @@ -154,4 +155,4 @@ message PrivacyWalletResponse {
SetAppDataResponse set_app_data_response = 9;
GetAppDataResponse get_app_data_response = 10;
}
}
}
8 changes: 5 additions & 3 deletions utt/privacy-wallet-service/src/PrivacyService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,16 @@ ::grpc::Status PrivacyWalletServiceImpl::handleUserClaimCoinsRequest(::grpc::Ser
response->set_err(err_msg);
return grpc::Status(grpc::StatusCode::ABORTED, err_msg);
}
} catch (const libutt::api::operations::InvalidCoinsInTransfer& e) {
std::cout << e.what() << std::endl;
response->mutable_claim_coins_response()->set_warning(e.what());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, shouldn't we return false in the succ?
Also, I would suggest returning the invalid nullifiers and the reply.
Finally, we need to expose a way for the user to remove these invalid coins, otherwise it won't be able to proceed (the invalid coins will be chose over and over again)

Copy link
Contributor Author

@snehal-das snehal-das Apr 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, shouldn't we return false in the succ?

Technically, the recipent was able to claim coins received. Reporting back false after claiming all valid coins seems incorrect to me. The more correct approach seems to be to return true with a warning.

Also, I would suggest returning the invalid nullifiers and the reply.

I didn't want the dapp to handle the invalid coins, but this is something that can be implemented. I'll discuss with you in more detail.

} catch (const std::exception& e) {
std::cout << e.what() << std::endl;
response->set_err(e.what());
return grpc::Status(grpc::StatusCode::ABORTED, e.what());
}
if (response) {
auto resp = response->mutable_claim_coins_response();
resp->set_succ(true);
response->mutable_claim_coins_response()->set_succ(true);
}
return grpc::Status::OK;
}
Expand Down Expand Up @@ -428,4 +430,4 @@ ::grpc::Status PrivacyWalletServiceImpl::handleGetAppDataRequest(
return grpc::Status::OK;
}

} // namespace utt::walletservice
} // namespace utt::walletservice
13 changes: 10 additions & 3 deletions utt/utt-client-api/src/User.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,13 @@ void User::updateTransferTx(const Transaction& tx, const TxOutputSigs& sigs) {

// Claim coins
auto claimedCoins = pImpl_->client_->claimCoins(uttTx, pImpl_->params_, sigs);

bool invalidCoinsInTransfer(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the regular convention here
either invalidCoinsInTransfer = false or invalidCoinsInTransfer{false}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll update in the next commit.

for (auto& coin : claimedCoins) {
if (!pImpl_->client_->validate(coin)) throw std::runtime_error("Invalid normal coin in transfer!");
if (!pImpl_->client_->validate(coin)) {
logdbg_user << "Invalid coin found; coin details: " << dbgPrintCoins({coin}) << endl;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not throw the exception here?
(and then you don't need invalidCoinsInTransfer)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to prevent prematurely exiting from the function. The recipient should be able to claim coins received after the invalid coin, so I am letting it process all coins before raising the exception.

invalidCoinsInTransfer = true;
continue;
}
pImpl_->storage_->setCoin(coin);
if (coin.getType() == libutt::api::Coin::Type::Normal) {
logdbg_user << "claimed normal coin: " << dbgPrintCoins({coin}) << endl;
Expand All @@ -464,6 +468,9 @@ void User::updateTransferTx(const Transaction& tx, const TxOutputSigs& sigs) {
}
}
}
if (invalidCoinsInTransfer) {
throw libutt::api::operations::InvalidCoinsInTransfer("Invalid normal coin(s) in transfer!");
}
}
}

Expand Down Expand Up @@ -617,4 +624,4 @@ void User::debugOutput() const {
std::cout << "------ USER DEBUG OUTPUT END -------------\n";
}

} // namespace utt::client
} // namespace utt::client