Skip to content

Commit

Permalink
do not set maxFeeRate for cancels v2 upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
chappjc committed Apr 28, 2021
1 parent d977162 commit 68a6cb3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
12 changes: 11 additions & 1 deletion client/db/bolt/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,16 @@ func decodeOrderBucket(oid []byte, oBkt *bbolt.Bucket) (*dexdb.MetaOrder, error)
var linkedID order.OrderID
copy(linkedID[:], oBkt.Get(linkedKey))

// Old cancel orders may not have a maxFeeRate set since the v2 upgrade
// doesn't set it for cancel orders.
var maxFeeRate uint64
if maxFeeRateB := oBkt.Get(maxFeeRateKey); len(maxFeeRateB) == 8 {
maxFeeRate = intCoder.Uint64(maxFeeRateB)
} else if ord.Type() != order.CancelOrderType {
// Cancel orders should use zero, but trades need a non-zero value.
maxFeeRate = ^uint64(0) // should not happen for trade orders after v2 upgrade
}

return &dexdb.MetaOrder{
MetaData: &dexdb.OrderMetaData{
Proof: *proof,
Expand All @@ -712,7 +722,7 @@ func decodeOrderBucket(oid []byte, oBkt *bbolt.Bucket) (*dexdb.MetaOrder, error)
ChangeCoin: getCopy(oBkt, changeKey),
LinkedOrder: linkedID,
SwapFeesPaid: intCoder.Uint64(oBkt.Get(swapFeesKey)),
MaxFeeRate: intCoder.Uint64(oBkt.Get(maxFeeRateKey)),
MaxFeeRate: maxFeeRate,
RedemptionFeesPaid: intCoder.Uint64(oBkt.Get(redemptionFeesKey)),
},
Order: ord,
Expand Down
16 changes: 14 additions & 2 deletions client/db/bolt/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ func v1Upgrade(dbtx *bbolt.Tx) error {
}

// v2Upgrade adds a MaxFeeRate field to the OrderMetaData. The upgrade sets the
// MaxFeeRate field for all historical orders to the max uint64. This avoids any
// chance of rejecting a pre-existing active match.
// MaxFeeRate field for all historical trade orders to the max uint64. This
// avoids any chance of rejecting a pre-existing active match.
func v2Upgrade(dbtx *bbolt.Tx) error {
const oldVersion = 1

Expand All @@ -164,6 +164,18 @@ func v2Upgrade(dbtx *bbolt.Tx) error {
if oBkt == nil {
return fmt.Errorf("order %x bucket is not a bucket", oid)
}
// Cancel orders should be stored with a zero maxFeeRate, as done in
// (*Core).tryCancelTrade. Besides, the maxFeeRate should not be applied
// to cancel matches, as done in (*dexConnection).parseMatches.
oTypeB := oBkt.Get(typeKey)
if len(oTypeB) != 1 {
return fmt.Errorf("order %x type invalid: %x", oid, oTypeB)
}
if order.OrderType(oTypeB[0]) == order.CancelOrderType {
// Don't bother setting maxFeeRate for cancel orders.
// decodeOrderBucket will default to zero for cancels.
return nil
}
return oBkt.Put(maxFeeRateKey, maxFeeB)
})
}
Expand Down

0 comments on commit 68a6cb3

Please sign in to comment.