Skip to content

Commit

Permalink
client/core: ensure redeem is accepted before confirm/complete match
Browse files Browse the repository at this point in the history
  • Loading branch information
chappjc committed Jun 20, 2023
1 parent d30fd91 commit b98a2ef
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
21 changes: 13 additions & 8 deletions client/core/core_test.go
Expand Up @@ -8700,7 +8700,7 @@ func TestConfirmRedemption(t *testing.T) {

tBtcWallet.redeemCoins = []dex.Bytes{tUpdatedCoinID}

setupMatch := func(status order.MatchStatus) {
setupMatch := func(status order.MatchStatus, side order.MatchSide) {
matchID := ordertest.RandomMatchID()
_, auditInfo := tMsgAudit(oid, matchID, addr, 0, secretHash[:])
matchTime := time.Now()
Expand All @@ -8711,21 +8711,27 @@ func TestConfirmRedemption(t *testing.T) {
UserMatch: &order.UserMatch{
MatchID: matchID,
Address: addr,
Side: side,
Status: status,
},
},
}
tracker.matches = map[order.MatchID]*matchTracker{matchID: match}

isMaker := match.Side == order.Maker
match.Status = status
match.MetaData.Proof = db.MatchProof{}
proof := &match.MetaData.Proof
proof.Auth.InitSig = []byte{1, 2, 3, 4}

// Assume our redeem was accepted, if we sent one.
if isMaker {
auditInfo.Expiration = matchTime.Add(tracker.lockTimeTaker)
if status >= order.MakerRedeemed {
match.MetaData.Proof.Auth.RedeemSig = []byte{0}
}
} else {
auditInfo.Expiration = matchTime.Add(tracker.lockTimeMaker)
if status >= order.MatchComplete {
match.MetaData.Proof.Auth.RedeemSig = []byte{0}
}
}

if status >= order.MakerSwapCast {
Expand Down Expand Up @@ -8967,8 +8973,7 @@ func TestConfirmRedemption(t *testing.T) {

for _, test := range tests {
tracker.mtx.Lock()
setupMatch(test.matchStatus)
match.Side = test.matchSide
setupMatch(test.matchStatus, test.matchSide)
tracker.mtx.Unlock()

tBtcWallet.confirmRedemptionResult = test.confirmRedemptionResult
Expand All @@ -8978,8 +8983,8 @@ func TestConfirmRedemption(t *testing.T) {
tCore.tickAsset(dc, tUTXOAssetB.ID)

if tBtcWallet.confirmRedemptionCalled != test.expectConfirmRedemptionCalled {
t.Fatalf("%s: expected confirm redemption to be called %v but got %v",
test.name, tBtcWallet.confirmRedemptionCalled, test.expectConfirmRedemptionCalled)
t.Fatalf("%s: expected confirm redemption to be called=%v but got=%v",
test.name, test.expectConfirmRedemptionCalled, tBtcWallet.confirmRedemptionCalled)
}

for _, expectedNotification := range test.expectedNotifications {
Expand Down
14 changes: 11 additions & 3 deletions client/core/trade.go
Expand Up @@ -1753,7 +1753,7 @@ func (t *trackedTrade) shouldBeginFindRedemption(ctx context.Context, match *mat
//
// This method accesses match fields and MUST be called with the trackedTrade
// mutex lock held for reads.
func shouldConfirmRedemption(match *matchTracker) bool {
func shouldConfirmRedemption(match *matchTracker, log dex.Logger) bool {
if match.Status == order.MatchConfirmed {
return false
}
Expand All @@ -1762,8 +1762,11 @@ func shouldConfirmRedemption(match *matchTracker) bool {
(match.Side == order.Taker && match.Status < order.MatchComplete) {
return false
}

proof := &match.MetaData.Proof
if len(proof.Auth.RedeemSig) == 0 {
log.Warnf("Redeemed match still needs a redeem acknowledgement! (match %v)", match)
return false
}
if match.Side == order.Maker {
return len(proof.MakerRedeem) > 0
}
Expand Down Expand Up @@ -1877,7 +1880,7 @@ func (c *Core) tick(t *trackedTrade) (assetMap, error) {
return nil
}

if shouldConfirmRedemption(match) {
if shouldConfirmRedemption(match, c.log) {
redemptionConfirms = append(redemptionConfirms, match)
return nil
}
Expand Down Expand Up @@ -2853,6 +2856,11 @@ func (t *trackedTrade) redeemFee() uint64 {
// This method accesses match fields and MUST be called with the trackedTrade
// mutex lock held for writes.
func (t *trackedTrade) confirmRedemption(match *matchTracker) {
if len(match.MetaData.Proof.Auth.RedeemSig) == 0 { // shouldConfirmRedemption should prevent this
t.dc.log.Warnf("Not confirming match %v before it is reported to server", match)
return // let resendPendingRequests have it
}

// In some cases the wallet will need to send a new redeem transaction.
toWallet := t.wallets.toWallet

Expand Down

0 comments on commit b98a2ef

Please sign in to comment.