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

feat(connext): lazy collateralization #1916

Merged
merged 2 commits into from
Oct 2, 2020
Merged

feat(connext): lazy collateralization #1916

merged 2 commits into from
Oct 2, 2020

Conversation

sangaman
Copy link
Collaborator

@sangaman sangaman commented Sep 24, 2020

Closes #1896. Branched off of and dependant on #1885. WIP for now but this adds functionality to track reserved outbound & inbound balances each time an own order is added to or removed from the order book. Every time an order is added (and reserved amount increases) we check if the inbound capacity for that currency is sufficient to cover all orders. In Connext's case, if it's not sufficient then we make a collateral request to cover all orders +3% (unless we already have a pending collateral request waiting). We don't do anything on the lnd side since lnd can't dynamically increase its inbound capacity (although we could consider doing something like logging a message).

Related to #1584, which can use the reserved amounts that are being tracked in this PR and expose them over rpc.

Tests still need to be updated to mock the new methods that track reserved amounts and/or add test cases for the reserved amount tracking logic.

@sangaman sangaman requested review from a user, kilrau and raladev September 24, 2020 05:58
@sangaman sangaman self-assigned this Sep 24, 2020
@sangaman sangaman marked this pull request as draft September 24, 2020 05:59
@sangaman sangaman changed the title Lazy collateral WIP: Lazy collateral Sep 24, 2020
@sangaman
Copy link
Collaborator Author

I finished adding comprehensive tests across the OrderBook, SwapClientManager, and ConnextClient classes. This should be ready for testing/review.

@sangaman sangaman marked this pull request as ready for review September 29, 2020 07:49
@sangaman sangaman changed the title WIP: Lazy collateral feat(connext): lazy collateralization Sep 29, 2020
@raladev
Copy link
Contributor

raladev commented Sep 30, 2020

  • we still have order placing collaterization, should it be removed?
simnet > buy 0.5 ETH/BTC 0.1 2
9 FAILED_PRECONDITION: channel collateralization in progress, please try again in ~1 minute

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Looking very good. Left some minor comments + simulation tests are still failing.

// if not start a new request, and when it completes call channelBalance to refresh our inbound capacity
// we don't await this request - instead we allow for "lazy collateralization" to complete since
// we don't expect all orders to be filled at once, we can be patient
const requestCollateralPromise = this.requestCollateralPromises.get(currency) ?? this.sendRequest('/request-collateral', 'POST', {
Copy link

Choose a reason for hiding this comment

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

This logic below seems to be the same as the one defined in public checkInboundCapacity. Can we extract this to a function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

@ghost
Copy link

ghost commented Sep 30, 2020

* we still have order placing collaterization, should it be removed?
simnet > buy 0.5 ETH/BTC 0.1 2
9 FAILED_PRECONDITION: channel collateralization in progress, please try again in ~1 minute

No, this feature is complementary to the order placing collateralization.

For example:

  1. 0 inbound liquidity
  2. buy 0.5 ETH -> placeorder will request collateral and not let the order enter the orderbook
  3. buy 0.5 ETH -> placeorder will succeed immediately and request the additional 0.5 ETH collateral in the background (this feature)

raladev
raladev previously approved these changes Sep 30, 2020
Copy link
Contributor

@raladev raladev left a comment

Choose a reason for hiding this comment

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

Approved but need to fix tests

@sangaman
Copy link
Collaborator Author

sangaman commented Oct 1, 2020

I finally came up with a way to get the simulation instability tests to pass, as connext payments were inexplicably failing for certain cases. I couldn't figure out why the regular collateral calls on PlaceOrder calls weren't causing any issues, until eventually I realized that all simulation tests use nobalancechecks which skips the collateral on order calls. This PR actually triggers collateral requests in the simulation tests, and for a reason I still don't understand it caused payments to fail in certain cases.

To work around this, I made it so the custom-xud patch code removes the lazy collateral calls logic, and tests are passing now.

@sangaman sangaman requested a review from a user October 1, 2020 06:23
});

this.requestCollateralPromises.set(currency, requestCollateralPromise);
return from(requestCollateralPromise);
Copy link

Choose a reason for hiding this comment

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

Maybe we can also remove the above lines and replace it with return from(this.requestCollateralInBackground(currency, minCollateralRequestUnits)) as it seems to be doing the same thing (with extra logging)?

Copy link
Collaborator Author

@sangaman sangaman Oct 1, 2020

Choose a reason for hiding this comment

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

return from(this.requestCollateralInBackground(currency, minCollateralRequestUnits)) doesn't work exactly because requestCollateralInBackground doesn't return a promise, by design the promise is resolved/handled in the background. I could just call the method and return nothing? that should be fine right?

@kilrau
Copy link
Contributor

kilrau commented Oct 1, 2020

This PR actually triggers collateral requests in the simulation tests, and for a reason I still don't understand it caused payments to fail in certain cases.

Shouldn't we find out why these fail though?

@sangaman sangaman requested a review from a user October 1, 2020 15:11
@sangaman
Copy link
Collaborator Author

sangaman commented Oct 1, 2020

This PR actually triggers collateral requests in the simulation tests, and for a reason I still don't understand it caused payments to fail in certain cases.

Shouldn't we find out why these fail though?

Yes, I reached out to the connext team and I've spent a lot of time trying to figure it out myself but it's still a myster. I was thinking it could be that payments to a node that is in the middle of recollateralization would fail, but that's not supposed to be the case according to connext.

This ads functionality to track reserved outboudn & inbound balances
each tmie an own order is added to or removed from the order book. Every
time an order is added (and reserved amount increases) we check if the
inbound capaacity for that currency is sufficient to cover all orders.
In Connext's case, if it's not sufficient then we make a collateral
request to cover all orders +3% (unless we already have a pending
collateral request waiting). We don't do anything on the lnd side since
lnd can't dynamically increase its inbound capacity.

Closes #1896.
@raladev raladev self-requested a review October 2, 2020 11:12
@raladev raladev merged commit 0f2b841 into master Oct 2, 2020
sangaman added a commit that referenced this pull request Oct 7, 2020
This restores the logic to request collateral in the custom xud builds
used for instability tests by adding a one second wait after adding an
order on the maker side (who then requests collateralization) but
before placing a matching order on the taker side.

This appears to work around a brief, transient inability to send a
connext payment to a node immediately after that node requests
collateral.

Related PR #1916 - specifically the persistent simulation test failures
that were eventually avoided by removing the collateral requests from
custom-xud, which is being restored here.
@sangaman sangaman deleted the lazy-collateral branch October 14, 2020 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lazy Collateralization
3 participants