Skip to content

Commit

Permalink
client/db,dex/order: patch archived match status based on enc version
Browse files Browse the repository at this point in the history
  • Loading branch information
chappjc committed Feb 24, 2023
1 parent 1b05383 commit 5fe7bec
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
7 changes: 6 additions & 1 deletion client/db/bolt/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1603,10 +1603,15 @@ func loadMatchBucket(mBkt *bbolt.Bucket, excludeCancels bool) (*dexdb.MetaMatch,
if matchB == nil {
return nil, fmt.Errorf("nil match bytes")
}
match, err := order.DecodeMatch(matchB)
match, matchVer, err := order.DecodeMatch(matchB)
if err != nil {
return nil, fmt.Errorf("error decoding match: %w", err)
}
if matchVer == 0 && match.Status == order.MatchComplete {
// When v0 matches were written, there was no MatchConfirmed, so we will
// "upgrade" this match on the fly to agree with MatchIsActive.
match.Status = order.MatchConfirmed
}
// A cancel match for a maker (trade) order has an empty address.
if excludeCancels && match.Address == "" {
return nil, nil
Expand Down
2 changes: 1 addition & 1 deletion client/db/bolt/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func v6Upgrade(dbtx *bbolt.Tx) error {
if matchB == nil {
return fmt.Errorf("nil match bytes for %x", k)
}
match, err := order.DecodeMatch(matchB)
match, _, err := order.DecodeMatch(matchB)
if err != nil {
return fmt.Errorf("error decoding match %x: %w", k, err)
}
Expand Down
18 changes: 10 additions & 8 deletions dex/order/serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func decodeTrade_v0(pushes [][]byte) (mrkt *Trade, err error) {
// EncodeMatch encodes the UserMatch to bytes suitable for binary storage or
// communications.
func EncodeMatch(match *UserMatch) []byte {
return encode.BuildyBytes{0}.
return encode.BuildyBytes{1}.
AddData(match.OrderID[:]).
AddData(match.MatchID[:]).
AddData(uint64B(match.Quantity)).
Expand All @@ -158,19 +158,21 @@ func EncodeMatch(match *UserMatch) []byte {
}

// DecodeMatch decodes the versioned blob into a UserMatch.
func DecodeMatch(b []byte) (match *UserMatch, err error) {
ver, pushes, err := encode.DecodeBlob(b, 8)
func DecodeMatch(b []byte) (match *UserMatch, ver uint8, err error) {
var pushes [][]byte
ver, pushes, err = encode.DecodeBlob(b, 8)
if err != nil {
return nil, err
return nil, 0, err
}
switch ver {
case 0:
return matchDecoder_v0(pushes)
case 0, 1: // same encoding, just a flag
match, err = matchDecoder_v0(pushes)
return
}
return nil, fmt.Errorf("unknown UserMatch version %d", ver)
return nil, 0, fmt.Errorf("unknown UserMatch version %d", ver)
}

// matchDecoder_v0 decodes the version 0 payload into a *UserMatch.
// matchDecoder_v0 decodes the version 0 (and 1) payload into a *UserMatch.
func matchDecoder_v0(pushes [][]byte) (*UserMatch, error) {
if len(pushes) != 8 {
return nil, fmt.Errorf("matchDecoder_v0: expected 8 pushes, got %d", len(pushes))
Expand Down
2 changes: 1 addition & 1 deletion dex/order/test/serialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func testUserMatch(t *testing.T) {
match := RandomUserMatch()
matchB := order.EncodeMatch(match)

reMatch, err := order.DecodeMatch(matchB)
reMatch, _, err := order.DecodeMatch(matchB)
if err != nil {
t.Fatalf("error decoding UserMatch: %v", err)
}
Expand Down

0 comments on commit 5fe7bec

Please sign in to comment.