Skip to content

Conversation

@mojoX911
Copy link

@mojoX911 mojoX911 commented Nov 2, 2024

Resolves #253, #237

Removes unwraps from utill.rs and taker modules.

Note: setup_logger unwraps are kept intact. As this is only used once in cli apps, where unwraps at main.rs it doesn't have any effect. We will later refator this anyway for custom logger. Wasn't worth to add one more error variants just for this.

Added a few more unwraps removal from maker and market.

@mojoX911 mojoX911 requested review from KnowWhoami and Shourya742 and removed request for Shourya742 November 2, 2024 13:19
@mojoX911 mojoX911 linked an issue Nov 7, 2024 that may be closed by this pull request
Copy link
Collaborator

@Shourya742 Shourya742 left a comment

Choose a reason for hiding this comment

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

Ack. What are your thoughts on adding a NotFound error variant for all instances where we're currently using get with expect?

@mojoX911
Copy link
Author

mojoX911 commented Nov 10, 2024

Ack. What are your thoughts on adding a NotFound error variant for all instances where we're currently using get with expect?

I am not entirely opposed. It feels equivalent to me. And more code to add variants just to denote a None, which is already a variant that we can match with an if let. So it's like writing let x = x;

If None needs to be handled we can already do that with if let matching.

Using expect basically says that "we never expect this to panic". And in rust we can somewhat claim such invariants if we architect the code correctly.

It can still panic, but that only means two things:
We screwed up somewhere in our logic/Something very nasty happened at your computer (your memory wiped out).

Both are valid cases for just panics.

expect is a very useful pattern to easily handle "unexpected" Nones. If you need more complex None handling, you can always use if let on it.

@mojoX911 mojoX911 requested a review from Shourya742 November 10, 2024 10:58
Copy link

@KnowWhoami KnowWhoami left a comment

Choose a reason for hiding this comment

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

Ack few nits and small improvements, then it's good to go.

src/utill.rs Outdated
WitnessVersion::V0,
&redeemscript.wscript_hash().to_byte_array(),
)
.unwrap();

Choose a reason for hiding this comment

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

categroize this error in Wallet::Protocol -> we will get an error only when there's something wrong with our redeemscript which is directly related to coinswap protocol.

Copy link
Author

Choose a reason for hiding this comment

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

Damn, this opened up an entire can of worms. This is such a low-level util that changing its signature affects everything. But this needed to be handled or it can be quite problematic. A rouge maker can trigger this unwrap by sending non-segwit reedemscripts.

Stay tuned for another round of refactoring commits.

Copy link
Author

Choose a reason for hiding this comment

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

This also cannot be added to the wallet error as this function is used in the contract module, which is the core module. Adding this to the ProtocolError because that already encapsulates contract error so we can propagate easily.

Choose a reason for hiding this comment

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

This is such a low-level util that changing its signature affects everything. But this needed to be handled or it can be quite problematic.

yeah , this error needs to be propagated and should be properly handled by high level api's .

A rouge maker can trigger this unwrap by sending non-segwit reedemscripts.

Since this api is been used for both maker and taker components so the unwrap here can be a threat to both of them.
For eg: this api is used to get the scriptpubkey from mutliscript_redeemscript of funding tx in order to verify the proof of funding.
so for:

  • Maker creates a seperate thread for handling the clients so having unwrap in this low level api will ultimately create unwrap inside the thread. So it will not cause much damage to it since Maker Server would still run but it would'nt be able to figure out the reason behind it and couldn't take any proper actions.

  • Taker : Since it works in blocking mode -> thus having unwrap can panic the whole coinswap process and it wouldn't be able to figure out the exact reason behind this.

Choose a reason for hiding this comment

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

Stay tuned for another round of refactoring commits.

If it requires lot of refactorings -> then let's just have a followup PR for this issue otherwise it would create this PR a bit mess.

Copy link
Author

Choose a reason for hiding this comment

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

its gonna be a mess later too trying to fix it. Pushed the changes. lets bite the bullet here.

@KnowWhoami
Copy link

  • fn write_default_maker_config(config_path: &PathBuf) {
    let config = MakerConfig::default();
    config.write_to_file(config_path).unwrap();
    }

    We can make this code inline as already been for taker case and propagate the error.

  • input_info.push(self.get_utxo((txid, vout)).unwrap().unwrap());

    Propagate the error.

@mojoX911
Copy link
Author

We can make this code inline as already been for taker case and propagate the error.

Not sure what you mean, but only Taker didn't had propagation. Added that. Maker is fine.

Propagate the error.

There are many unwraps in funding module functions that are not being used. No point in spending time solving these unwraps. The only routine used currently is create_funding_txes_random_amounts which doesn't have unwraps, so we are fine. Commented the call sites of these functions. So they are disabled fully now.

@mojoX911
Copy link
Author

There are weird overlaps between ContractError and ProtocolError. Life will be much simpler if these two are just merged into one single error. Doing that.

@mojoX911
Copy link
Author

Addressed comments and other refactors from 7de0fba

@codecov
Copy link

codecov bot commented Nov 18, 2024

Codecov Report

Attention: Patch coverage is 66.01307% with 52 lines in your changes missing coverage. Please review.

Project coverage is 74.83%. Comparing base (e2ee88f) to head (64fd3ec).
Report is 7 commits behind head on master.

Files with missing lines Patch % Lines
src/protocol/contract.rs 70.00% 9 Missing ⚠️
src/taker/api.rs 71.42% 8 Missing ⚠️
src/wallet/swapcoin.rs 66.66% 6 Missing ⚠️
src/taker/error.rs 0.00% 5 Missing ⚠️
src/wallet/api.rs 44.44% 5 Missing ⚠️
src/wallet/funding.rs 0.00% 5 Missing ⚠️
src/taker/offers.rs 66.66% 4 Missing ⚠️
src/maker/error.rs 0.00% 2 Missing ⚠️
src/protocol/error.rs 0.00% 2 Missing ⚠️
src/wallet/error.rs 50.00% 2 Missing ⚠️
... and 4 more
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #290      +/-   ##
==========================================
- Coverage   78.01%   74.83%   -3.18%     
==========================================
  Files          32       32              
  Lines        4835     3998     -837     
==========================================
- Hits         3772     2992     -780     
+ Misses       1063     1006      -57     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@KnowWhoami
Copy link

There are weird overlaps between ContractError and ProtocolError. Life will be much simpler if these two are just merged into one single error. Doing that.

Yeah ,that will make things a bit simpler.
But still IMO, we should have that because right now, all errors are at same level under ProtocolError enum -> some of which are related to Contract ones -> so having a ContractError enum for those errors would make our error enum more modular.
what's your say?

@mojoX911
Copy link
Author

mojoX911 commented Dec 3, 2024

Contracts are part of the protocol. I don't see any reason why we need a separate error for just contracts. And if we are going by definition, then that should only include script errors, and nothing else. That's what a "contract" is. Its redundant nomenclature. Better to get rid of it.

Copy link
Collaborator

@Shourya742 Shourya742 left a comment

Choose a reason for hiding this comment

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

ACK

@Shourya742 Shourya742 merged commit e0a772a into citadel-tech:master Dec 3, 2024
14 checks passed
bicodrex pushed a commit to bicodrex/coinswap that referenced this pull request Jul 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove unwraps from Taker APIs Remove unwraps from util.rs

3 participants