Skip to content

Commit

Permalink
Cleanup client/btc based on chappjc review
Browse files Browse the repository at this point in the history
  • Loading branch information
martonp committed May 18, 2022
1 parent e5d895b commit b5fb4fb
Show file tree
Hide file tree
Showing 8 changed files with 612 additions and 396 deletions.
555 changes: 314 additions & 241 deletions client/asset/btc/btc.go

Large diffs are not rendered by default.

386 changes: 262 additions & 124 deletions client/asset/btc/btc_test.go

Large diffs are not rendered by default.

34 changes: 19 additions & 15 deletions client/asset/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (wt WalletTrait) IsFeeRater() bool {
}

// IsAccelerator tests if the WalletTrait has the WalletTraitAccelerator bit set,
// which indicates the presence of a Accelerate method.
// which indicates the presence of an Accelerate method.
func (wt WalletTrait) IsAccelerator() bool {
return wt&WalletTraitAccelerator != 0
}
Expand Down Expand Up @@ -382,20 +382,22 @@ type EarlyAcceleration struct {
// WasAccelerated is true if the action that took place TimePast seconds
// ago was an acceleration. If false, the oldest unmined swap transaction
// in the order was submitted TimePast seconds ago.
WasAcclerated bool `json:"wasAccelerated"`
WasAccelerated bool `json:"wasAccelerated"`
}

// Accelerator is implemented by wallets which support acceleration of the
// mining of swap transactions.
type Accelerator interface {
// AccelerateOrder uses the Child-Pays-For-Parent technique to accelerate a
// chain of swap transactions and previous accelerations. It broadcasts a
// new transaction with a fee high enough so that the average fee of all
// the unconfirmed transactions in the chain and the new transaction will
// have an average fee rate of newFeeRate. requiredForRemainingSwaps is
// passed in to ensure that the new change coin will have enough funds to
// initiate the additional swaps that will be required to complete the
// order.
// chain of swap transactions and previous accelerations. It broadcasts a new
// transaction with a fee high enough so that the average fee of all the
// unconfirmed transactions in the chain and the new transaction will have
// an average fee rate of newFeeRate. The changeCoin argument is the latest
// chhange in the order. It must be the input in the acceleration transaction
// in order for the order to be accelerated. requiredForRemainingSwaps is the
// amount of funds required to complete the rest of the swaps in the order.
// The change output of the acceleration transaction will have at least
// this amount.
//
// The returned change coin may be nil, and should be checked before use.
AccelerateOrder(swapCoins, accelerationCoins []dex.Bytes, changeCoin dex.Bytes, requiredForRemainingSwaps, newFeeRate uint64) (Coin, string, error)
Expand All @@ -405,12 +407,14 @@ type Accelerator interface {
// average fee rate to the desired amount.
AccelerationEstimate(swapCoins, accelerationCoins []dex.Bytes, changeCoin dex.Bytes, requiredForRemainingSwaps, newFeeRate uint64) (uint64, error)
// PreAccelerate returns the current average fee rate of the unmined swap
// initiation and acceleration transactions, a suggested
// range that the fee rate should be increased to in order to expedite
// mining, and also optionally an EarlyAcceleration notification if
// the user's previous acceleration on this order or the earliest
// unmined transaction in this order happened very recently.
PreAccelerate(swapCoins, accelerationCoins []dex.Bytes, changeCoin dex.Bytes, requiredForRemainingSwaps, feeSuggestion uint64) (uint64, XYRange, *EarlyAcceleration, error)
// initiation and acceleration transactions, and also returns a suggested
// range that the fee rate should be increased to in order to expedite mining.
// The feeSuggestion argument is the current prevailing network rate. It is
// used to help determine the suggestedRange, which is a range meant to give
// the user a good amount of flexibility in determining the post acceleration
// effective fee rate, but still not allowing them to pick something
// outrageously high.
PreAccelerate(swapCoins, accelerationCoins []dex.Bytes, changeCoin dex.Bytes, requiredForRemainingSwaps, feeSuggestion uint64) (uint64, *XYRange, *EarlyAcceleration, error)
}

// TokenMaster is implemented by assets which support degenerate tokens.
Expand Down
8 changes: 6 additions & 2 deletions client/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -7136,7 +7136,6 @@ func (c *Core) AccelerateOrder(pw []byte, oidB dex.Bytes, newFeeRate uint64) (st
if err != nil {
return "", err
}

tracker, err := c.findActiveOrder(oid)
if err != nil {
return "", err
Expand Down Expand Up @@ -7226,10 +7225,15 @@ func (c *Core) PreAccelerateOrder(oidB dex.Bytes) (*PreAccelerate, error) {
return nil, err
}

if suggestedRange == nil {
// this should never happen
return nil, fmt.Errorf("suggested range is nil")
}

return &PreAccelerate{
SwapRate: currentRate,
SuggestedRate: feeSuggestion,
SuggestedRange: suggestedRange,
SuggestedRange: *suggestedRange,
EarlyAcceleration: earlyAcceleration,
}, nil
}
Expand Down
6 changes: 3 additions & 3 deletions client/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -904,9 +904,9 @@ func (w *TXCWallet) AccelerateOrder(swapCoins, accelerationCoins []dex.Bytes, ch
return nil, w.newAccelerationTxID, nil
}

func (w *TXCWallet) PreAccelerate(swapCoins, accelerationCoins []dex.Bytes, changeCoin dex.Bytes, requiredForRemainingSwaps, feeSuggestion uint64) (uint64, asset.XYRange, *asset.EarlyAcceleration, error) {
func (w *TXCWallet) PreAccelerate(swapCoins, accelerationCoins []dex.Bytes, changeCoin dex.Bytes, requiredForRemainingSwaps, feeSuggestion uint64) (uint64, *asset.XYRange, *asset.EarlyAcceleration, error) {
if w.accelerateOrderErr != nil {
return 0, asset.XYRange{}, nil, w.accelerateOrderErr
return 0, nil, nil, w.accelerateOrderErr
}

w.accelerationParams = &struct {
Expand All @@ -924,7 +924,7 @@ func (w *TXCWallet) PreAccelerate(swapCoins, accelerationCoins []dex.Bytes, chan
feeSuggestion: feeSuggestion,
}

return w.preAccelerateSwapRate, w.preAccelerateSuggestedRange, nil, nil
return w.preAccelerateSwapRate, &w.preAccelerateSuggestedRange, nil, nil
}

func (w *TXCWallet) AccelerationEstimate(swapCoins, accelerationCoins []dex.Bytes, changeCoin dex.Bytes, requiredForRemainingSwaps, newFeeRate uint64) (uint64, error) {
Expand Down
13 changes: 5 additions & 8 deletions client/core/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -2843,18 +2843,15 @@ func (t *trackedTrade) orderAccelerationParameters() (swapCoins, accelerationCoi

swapCoins = make([]dex.Bytes, 0, len(t.matches))
for _, match := range t.matches {
if match.Status < order.MakerSwapCast {
continue
}
var swapCoinID order.CoinID
if match.Side == order.Maker {
if match.Side == order.Maker && match.Status >= order.MakerSwapCast {
swapCoinID = match.MetaData.Proof.MakerSwap
} else {
if match.Status < order.TakerSwapCast {
continue
}
} else if match.Side == order.Taker && match.Status >= order.TakerSwapCast {
swapCoinID = match.MetaData.Proof.TakerSwap
} else {
continue
}

swapCoins = append(swapCoins, dex.Bytes(swapCoinID))
}

Expand Down
4 changes: 2 additions & 2 deletions client/core/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,10 @@ func (w *xcWallet) accelerationEstimate(swapCoins, accelerationCoins []dex.Bytes

// preAccelerate gives the user information about accelerating an order if the
// wallet is an Accelerator.
func (w *xcWallet) preAccelerate(swapCoins, accelerationCoins []dex.Bytes, changeCoin dex.Bytes, requiredForRemainingSwaps, feeSuggestion uint64) (uint64, asset.XYRange, *asset.EarlyAcceleration, error) {
func (w *xcWallet) preAccelerate(swapCoins, accelerationCoins []dex.Bytes, changeCoin dex.Bytes, requiredForRemainingSwaps, feeSuggestion uint64) (uint64, *asset.XYRange, *asset.EarlyAcceleration, error) {
accelerator, ok := w.Wallet.(asset.Accelerator)
if !ok {
return 0, asset.XYRange{}, nil, errors.New("wallet does not support acceleration")
return 0, &asset.XYRange{}, nil, errors.New("wallet does not support acceleration")
}

return accelerator.PreAccelerate(swapCoins, accelerationCoins, changeCoin, requiredForRemainingSwaps, feeSuggestion)
Expand Down
2 changes: 1 addition & 1 deletion client/db/bolt/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ func decodeOrderBucket(oid []byte, oBkt *bbolt.Bucket) (*dexdb.MetaOrder, error)
}

var accelerationCoinIDs []order.CoinID
accelerationsB := oBkt.Get(accelerationsKey)
accelerationsB := getCopy(oBkt, accelerationsKey)
if len(accelerationsB) > 0 {
_, coinIDs, err := encode.DecodeBlob(accelerationsB)
if err != nil {
Expand Down

0 comments on commit b5fb4fb

Please sign in to comment.