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

core,eth: handle redemption funding #1314

Merged
merged 2 commits into from
Jan 11, 2022
Merged

Conversation

buck54321
Copy link
Member

@buck54321 buck54321 commented Nov 29, 2021

    client/asset:
    Add AccounterRedeemer interface with methods for locking and unlocking
    funds for redemption. ReserveN is used to reserve funds for an order.
    Only the amount reserved is returned. All accounting from there is
    handled by Core, who will return funds proportionally according to
    match quantities and order size. The amount reserved is persisted in
    the OrderMetaData so that accounting can be picked up exactly as it was on restarts.
    This pattern was chosen over trying to figure out how much to lock
    on restart with calculations based on order info because you would need
    to consider changes in asset configuration and therefore persist more
    data and ... it gets messy.
    
    client/core:
    Add RedeemSig to orders redeeming to and AccountRedeemer.
    Track and return reserved redemption funds. Return funds manually
    for refunds and revocations. Handle dust that can arise from rounding
    error. Log inconsistencies.
    
    client/asset/eth:
    Implement asset.AccountRedeemer. Track redemption reserves separately
    than swap reserves, but add them when calculating locked balance.

@chappjc
Copy link
Member

chappjc commented Nov 29, 2021

I was imagining a FundBuyOrder to mirror the FundOrder method, which is implicitly for a sale of the asset. Then just use the usual ReturnCoins if cancelling or executing unfilled. Does splitting the redemption reserves away from other locked coins solve a major challenge with that approach?

@buck54321
Copy link
Member Author

buck54321 commented Nov 29, 2021

I was imagining a FundBuyOrder to mirror the FundOrder method, which is implicitly for a sale of the asset. Then just use the usual ReturnCoins if cancelling or executing unfilled. Does splitting the redemption reserves away from other locked coins solve a major challenge with that approach?

Not 100% sure I follow, but you should try it out. This is maybe the third different direction I started down, and the others got messy.

I think FundBuyOrder might be confusing though, since it's presumably still used for sell orders (toAsset = sell & quote || buy && base).

@chappjc
Copy link
Member

chappjc commented Nov 30, 2021

I will consider it more. I do appreciate some of the issues you've addressed by tracking amounts instead of coins or lots independently.
Perhaps FundBuyOrder would be better as FundReceivingOrder, but it's probably a moot point.

@buck54321
Copy link
Member Author

Still need match revocation handling, but if nobody objects, I'll work on getting this out of draft.

@chappjc
Copy link
Member

chappjc commented Dec 22, 2021 via email

@buck54321
Copy link
Member Author

Order Completion

Result Action
Revoked before matching return all [1]
Revoked from booked return remaining [1]
Canceled return remaining [2]
nomatch - market order return all [3]
Executed - market buy do nothing. will return when redeemed
Executed - market sell return remaining [2]
Executed - limit nothing

Match Revoked

Step Maker Taker
NewlyMatched return all [4] return all [4]
MakerSwapCast nothing (will refund) [5] return all [4]
TakerSwapCast nothing (will redeem) [6] nothing (will redeem via findRedemption, or refund) [5]
MakerRedeemed nothing nothing (will redeem) [6]

@buck54321 buck54321 force-pushed the redeem-sig branch 5 times, most recently from 85764f3 to bee8fc3 Compare January 6, 2022 12:51
@buck54321 buck54321 marked this pull request as ready for review January 6, 2022 12:52
client/asset/eth/eth.go Outdated Show resolved Hide resolved
Comment on lines +375 to +381
pubKey, err = recoverPubkey(crypto.Keccak256(data), sig)
if err != nil {
return nil, nil, fmt.Errorf("SignMessage: error recovering pubkey %w", err)
}
Copy link
Member

Choose a reason for hiding this comment

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

I suppose we could get the pubkey at start-up and there would not be a need to recover every time? But if we ever have more than one address being used, it will be needed, I guess.

Copy link
Member Author

Choose a reason for hiding this comment

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

I actually had it that way at one point.

@@ -227,6 +227,20 @@ type Wallet interface {
RegFeeConfirmations(ctx context.Context, coinID dex.Bytes) (confs uint32, err error)
}

type AccountRedeemer interface {
Copy link
Member

Choose a reason for hiding this comment

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

Looking at how funding swaps will work, they will also need special care, and I guess will need a similar interface. For example, the entire funding msg is signed rather than just the funding coin ids. Could the naming here be changed to include more than just redeems? Maybe just Accounter or is that wierd.

Also, could use a doc above.

Copy link
Member Author

Choose a reason for hiding this comment

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

I assume you mean the problems you're addressing in #1392? I don't think we'll need any new Wallet methods for swap funding.

client/core/core.go Show resolved Hide resolved
client/core/core.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@martonp martonp left a comment

Choose a reason for hiding this comment

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

LGTM, just minor comments

client/core/trade.go Outdated Show resolved Hide resolved
client/core/core.go Outdated Show resolved Hide resolved
client/asset:
Add AccounterRedeemer interface with methods for locking and unlocking
funds for redemption. ReserveN is used to reserve funds for an order.
Only the amount reserved is returned. All accounting from there is
handled by Core, who will return funds proportionally according to
match quantities and order size. The amount reserved is persisted in
the OrderMetaData so that accounting can be picked up exactly as it was on restarts.
This pattern was chosen over trying to figure out how much to lock
on restart with calculations based on order info because you would need
to consider changes in asset configuration and therefore persist more
data and ... it gets messy.

client/core:
Add RedeemSig to orders redeeming to and AccountRedeemer.
Track and return reserved redemption funds. Return funds manually
for refunds and revocations. Handle dust that can arise from rounding
error. Log inconsistencies.

client/asset/eth:
Implement asset.AccountRedeemer. Track redemption reserves separately
than swap reserves, but add them when calculating locked balance.
@chappjc chappjc merged commit e1218a2 into decred:master Jan 11, 2022
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.

4 participants