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 23, 2023
1 parent 899accb commit 029b257
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 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
21 changes: 12 additions & 9 deletions client/core/trade.go
Expand Up @@ -92,8 +92,9 @@ type matchTracker struct {

// confirmRedemptionNumTries is just used for logging.
confirmRedemptionNumTries int
// redemptionConfs and redemptionConfsReq are set while the redemption
// confirmation process is running.
// redemptionConfs and redemptionConfsReq are updated while the redemption
// confirmation process is running. Their values are not updated after the
// match reaches MatchConfirmed status.
redemptionConfs uint64
redemptionConfsReq uint64

Expand Down Expand Up @@ -2820,7 +2821,11 @@ func (c *Core) sendRedeemAsync(t *trackedTrade, match *matchTracker, coinID, sec
if match.Side == order.Maker && match.Status < order.MatchComplete {
// As maker, this is the end. However, this diverges from server,
// which still needs taker's redeem.
match.Status = order.MatchComplete
if conf := match.redemptionConfs; conf > 0 && conf >= match.redemptionConfsReq {
match.Status = order.MatchConfirmed // redeem tx already confirmed before redeem request accepted by server
} else {
match.Status = order.MatchComplete
}
}
err = t.db.UpdateMatch(&match.MetaMatch)
if err != nil {
Expand Down Expand Up @@ -2914,14 +2919,12 @@ func (t *trackedTrade) confirmRedemption(match *matchTracker) {
}
}

if redemptionStatus.Req <= redemptionStatus.Confs {
match.redemptionConfs, match.redemptionConfsReq = redemptionStatus.Confs, redemptionStatus.Req

if redemptionStatus.Confs >= redemptionStatus.Req &&
(len(match.MetaData.Proof.Auth.RedeemSig) > 0 || t.isSelfGoverned()) {
redemptionConfirmed = true
match.Status = order.MatchConfirmed
match.redemptionConfs = 0
match.redemptionConfsReq = 0
} else {
match.redemptionConfs = redemptionStatus.Confs
match.redemptionConfsReq = redemptionStatus.Req
}

if redemptionResubmitted || redemptionConfirmed {
Expand Down

0 comments on commit 029b257

Please sign in to comment.