Skip to content

Commit

Permalink
Changes based on buck's review
Browse files Browse the repository at this point in the history
  • Loading branch information
martonp committed Apr 8, 2022
1 parent c74a8a1 commit 5cbeb16
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 39 deletions.
16 changes: 9 additions & 7 deletions client/asset/btc/btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1880,9 +1880,10 @@ func (btc *baseWallet) AccelerateOrder(swapCoins, accelerationCoins []dex.Bytes,
if newChange != nil {
newChangeCoin = newChange

// Checking required for remaining swaps > 0 because this ensures if the previous change
// was locked, this one will also be locked. If requiredForRemainingSwaps = 0, but the
// change was locked, signedAccelerationTx would have returned an error since this means
// Checking required for remaining swaps > 0 because this ensures if
// the previous change was locked, this one will also be locked. If
// requiredForRemainingSwaps = 0, but the change was locked,
// signedAccelerationTx would have returned an error since this means
// that the change was locked by another order.
if requiredForRemainingSwaps > 0 {
err = btc.node.lockUnspent(false, []*output{newChange})
Expand Down Expand Up @@ -1920,9 +1921,9 @@ func (btc *baseWallet) AccelerationEstimate(swapCoins, accelerationCoins []dex.B
return fee, nil
}

// PreAccelerate returns the current average fee rate of the unmined swap initiation
// and acceleration transactions, and also returns a suggested range that the
// fee rate should be increased to in order to expedite mining.
// PreAccelerate returns the current average fee rate of the unmined swap
// initiation and acceleration transactions, and also returns a suggested
// range that the fee rate should be increased to in order to expedite mining.
func (btc *baseWallet) PreAccelerate(swapCoins, accelerationCoins []dex.Bytes, changeCoin dex.Bytes, requiredForRemainingSwaps, feeSuggestion uint64) (currentRate uint64, suggestedRange asset.XYRange, err error) {
makeError := func(err error) (uint64, asset.XYRange, error) {
return 0, asset.XYRange{}, err
Expand Down Expand Up @@ -2151,7 +2152,8 @@ func (btc *baseWallet) lookupOutput(txHash *chainhash.Hash, vout uint32) (*outpu
return newOutput(txHash, vout, uint64(value)), nil
}

// changeCanBeAccelerated will return an error if the change cannot be accelerated.
// changeCanBeAccelerated returns whether or not the change output can be
// accelerated.
func (btc *baseWallet) changeCanBeAccelerated(changeTxHash *chainhash.Hash, changeVout uint32, requiredForRemainingSwaps uint64) (bool, error) {
lockedUtxos, err := btc.node.listLockUnspent()
if err != nil {
Expand Down
20 changes: 11 additions & 9 deletions client/asset/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,21 +374,23 @@ type FeeRater interface {
// 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. 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.
AccelerateOrder(swapCoins, accelerationCoins []dex.Bytes, changeCoin dex.Bytes, requiredForRemainingSwaps, newFeeRate uint64) (Coin, string, error)
// AccelerationEstimate takes the same parameters as AccelerateOrder, but
// instead of broadcasting the acceleration transaction, it just returns
// the amount of funds that will need to be spent in order to increase the
// 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, and also returns a suggested range that the
// fee rate should be increased to in order to expedite mining.
// PreAccelerate returns the current average fee rate of the unmined swap
// initiation and acceleration transactions, and also returns a suggested
// range that the fee rate should be increased to in order to expedite
// mining.
PreAccelerate(swapCoins, accelerationCoins []dex.Bytes, changeCoin dex.Bytes, requiredForRemainingSwaps, feeSuggestion uint64) (currentRate uint64, suggestedRange XYRange, err error)
}

Expand Down
11 changes: 6 additions & 5 deletions client/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -2021,7 +2021,7 @@ func (c *Core) RescanWallet(assetID uint32, force bool) error {
}

// Begin potentially asynchronous wallet rescan operation.
if err = wallet.Rescan(c.ctx); err != nil {
if err = wallet.rescan(c.ctx); err != nil {
return err
}

Expand Down Expand Up @@ -6781,7 +6781,7 @@ func (c *Core) WalletLogFilePath(assetID uint32) (string, error) {
strings.ToUpper(unbip(assetID)), assetID)
}

return wallet.LogFilePath()
return wallet.logFilePath()
}

func createFile(fileName string) (*os.File, error) {
Expand Down Expand Up @@ -7062,7 +7062,7 @@ func (c *Core) AccelerateOrder(pw []byte, oidB dex.Bytes, newFeeRate uint64) (st
}

newChangeCoin, txID, err :=
tracker.wallets.fromWallet.AccelerateOrder(swapCoinIDs, accelerationCoins, changeCoinID, requiredForRemainingSwaps, newFeeRate)
tracker.wallets.fromWallet.accelerateOrder(swapCoinIDs, accelerationCoins, changeCoinID, requiredForRemainingSwaps, newFeeRate)
if err != nil {
return "", err
}
Expand All @@ -7086,7 +7086,7 @@ func (c *Core) AccelerationEstimate(oidB dex.Bytes, newFeeRate uint64) (uint64,
return 0, err
}

accelerationFee, err := tracker.wallets.fromWallet.AccelerationEstimate(swapCoins, accelerationCoins, changeCoin, requiredForRemainingSwaps, newFeeRate)
accelerationFee, err := tracker.wallets.fromWallet.accelerationEstimate(swapCoins, accelerationCoins, changeCoin, requiredForRemainingSwaps, newFeeRate)
if err != nil {
return 0, err
}
Expand All @@ -7105,7 +7105,7 @@ func (c *Core) PreAccelerateOrder(oidB dex.Bytes) (*PreAccelerate, error) {
feeSuggestion := c.feeSuggestionAny(tracker.fromAssetID)

currentRate, suggestedRange, err :=
tracker.wallets.fromWallet.PreAccelerate(swapCoinIDs, accelerationCoins, changeCoinID, requiredForRemainingSwaps, feeSuggestion)
tracker.wallets.fromWallet.preAccelerate(swapCoinIDs, accelerationCoins, changeCoinID, requiredForRemainingSwaps, feeSuggestion)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -7146,6 +7146,7 @@ func (c *Core) orderAccelerationParameters(oidB dex.Bytes) (tracker *trackedTrad
swapSize := fromAsset.SwapSize
lotsRemaining := tracker.Trade().Remaining() / lotSize
requiredForRemainingSwaps = lotsRemaining * swapSize * tracker.metaData.MaxFeeRate
break
}
}
if tracker == nil {
Expand Down
8 changes: 4 additions & 4 deletions client/core/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ func (t *trackedTrade) counterPartyConfirms(ctx context.Context, match *matchTra
coin := match.counterSwap.Coin

var err error
have, spent, err = t.wallets.toWallet.SwapConfirmations(ctx, coin.ID(),
have, spent, err = t.wallets.toWallet.swapConfirmations(ctx, coin.ID(),
match.MetaData.Proof.CounterContract, match.MetaData.Stamp)
if err != nil {
if !errors.Is(err, asset.ErrSwapNotInitiated) {
Expand Down Expand Up @@ -1076,7 +1076,7 @@ func (t *trackedTrade) isSwappable(ctx context.Context, match *matchTracker) boo
// If we're the maker, check the confirmations anyway so we can notify.
t.dc.log.Tracef("Checking confirmations on our OWN swap txn %v (%s)...",
coinIDString(wallet.AssetID, match.MetaData.Proof.MakerSwap), unbip(wallet.AssetID))
confs, spent, err := wallet.SwapConfirmations(ctx, match.MetaData.Proof.MakerSwap,
confs, spent, err := wallet.swapConfirmations(ctx, match.MetaData.Proof.MakerSwap,
match.MetaData.Proof.ContractData, match.MetaData.Stamp)
if err != nil && !errors.Is(err, asset.ErrSwapNotInitiated) {
// No need to log an error if swap not initiated as this
Expand Down Expand Up @@ -1136,7 +1136,7 @@ func (t *trackedTrade) isRedeemable(ctx context.Context, match *matchTracker) bo
return ready
}
// If we're the taker, check the confirmations anyway so we can notify.
confs, spent, err := t.wallets.fromWallet.SwapConfirmations(ctx, match.MetaData.Proof.TakerSwap,
confs, spent, err := t.wallets.fromWallet.swapConfirmations(ctx, match.MetaData.Proof.TakerSwap,
match.MetaData.Proof.ContractData, match.MetaData.Stamp)
if err != nil && !errors.Is(err, asset.ErrSwapNotInitiated) {
// No need to log an error if swap not initiated as this
Expand Down Expand Up @@ -1264,7 +1264,7 @@ func (t *trackedTrade) shouldBeginFindRedemption(ctx context.Context, match *mat
return false
}

confs, spent, err := t.wallets.fromWallet.SwapConfirmations(ctx, swapCoinID, proof.ContractData, match.MetaData.Stamp)
confs, spent, err := t.wallets.fromWallet.swapConfirmations(ctx, swapCoinID, proof.ContractData, match.MetaData.Stamp)
if err != nil {
if !errors.Is(err, asset.ErrSwapNotInitiated) {
// No need to log an error if swap not initiated as this
Expand Down
24 changes: 12 additions & 12 deletions client/core/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,39 +280,39 @@ func (w *xcWallet) Disconnect() {
w.mtx.Unlock()
}

// Rescan will initiate a rescan of the wallet if the asset.Wallet
// rescan will initiate a rescan of the wallet if the asset.Wallet
// implementation is a Rescanner.
func (w *xcWallet) Rescan(ctx context.Context) error {
func (w *xcWallet) rescan(ctx context.Context) error {
rescanner, ok := w.Wallet.(asset.Rescanner)
if !ok {
return errors.New("wallet does not support rescanning")
}
return rescanner.Rescan(ctx)
}

// LogFilePath returns the path of the wallet's log file if the
// logFilePath returns the path of the wallet's log file if the
// asset.Wallet implementation is a LogFiler.
func (w *xcWallet) LogFilePath() (string, error) {
func (w *xcWallet) logFilePath() (string, error) {
logFiler, ok := w.Wallet.(asset.LogFiler)
if !ok {
return "", errors.New("wallet does not support getting log file")
}
return logFiler.LogFilePath(), nil
}

// AccelerateOrder uses the Child-Pays-For-Parent technique to accelerate an
// accelerateOrder uses the Child-Pays-For-Parent technique to accelerate an
// order if the wallet is an Accelerator.
func (w *xcWallet) AccelerateOrder(swapCoins, accelerationCoins []dex.Bytes, changeCoin dex.Bytes, requiredForRemainingSwaps, newFeeRate uint64) (asset.Coin, string, error) {
func (w *xcWallet) accelerateOrder(swapCoins, accelerationCoins []dex.Bytes, changeCoin dex.Bytes, requiredForRemainingSwaps, newFeeRate uint64) (asset.Coin, string, error) {
accelerator, ok := w.Wallet.(asset.Accelerator)
if !ok {
return nil, "", errors.New("wallet does not support acceleration")
}
return accelerator.AccelerateOrder(swapCoins, accelerationCoins, changeCoin, requiredForRemainingSwaps, newFeeRate)
}

// AccelerationEstimate estimates the cost to accelerate an order if the wallet
// accelerationEstimate estimates the cost to accelerate an order if the wallet
// is an Accelerator.
func (w *xcWallet) AccelerationEstimate(swapCoins, accelerationCoins []dex.Bytes, changeCoin dex.Bytes, requiredForRemainingSwaps, feeSuggestion uint64) (uint64, error) {
func (w *xcWallet) accelerationEstimate(swapCoins, accelerationCoins []dex.Bytes, changeCoin dex.Bytes, requiredForRemainingSwaps, feeSuggestion uint64) (uint64, error) {
accelerator, ok := w.Wallet.(asset.Accelerator)
if !ok {
return 0, errors.New("wallet does not support acceleration")
Expand All @@ -321,9 +321,9 @@ func (w *xcWallet) AccelerationEstimate(swapCoins, accelerationCoins []dex.Bytes
return accelerator.AccelerationEstimate(swapCoins, accelerationCoins, changeCoin, requiredForRemainingSwaps, feeSuggestion)
}

// PreAccelerate gives the user information about accelerating an order if the
// 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) (currentRate uint64, suggestedRange asset.XYRange, err error) {
func (w *xcWallet) preAccelerate(swapCoins, accelerationCoins []dex.Bytes, changeCoin dex.Bytes, requiredForRemainingSwaps, feeSuggestion uint64) (currentRate uint64, suggestedRange asset.XYRange, err error) {
accelerator, ok := w.Wallet.(asset.Accelerator)
if !ok {
return 0, asset.XYRange{}, errors.New("wallet does not support acceleration")
Expand All @@ -332,11 +332,11 @@ func (w *xcWallet) PreAccelerate(swapCoins, accelerationCoins []dex.Bytes, chang
return accelerator.PreAccelerate(swapCoins, accelerationCoins, changeCoin, requiredForRemainingSwaps, feeSuggestion)
}

// SwapConfirmations calls (asset.Wallet).SwapConfirmations with a timeout
// swapConfirmations calls (asset.Wallet).SwapConfirmations with a timeout
// Context. If the coin cannot be located, an asset.CoinNotFoundError is
// returned. If the coin is located, but recognized as spent, no error is
// returned.
func (w *xcWallet) SwapConfirmations(ctx context.Context, coinID []byte, contract []byte, matchTime uint64) (uint32, bool, error) {
func (w *xcWallet) swapConfirmations(ctx context.Context, coinID []byte, contract []byte, matchTime uint64) (uint32, bool, error) {
ctx, cancel := context.WithTimeout(ctx, confCheckTimeout)
defer cancel()
return w.Wallet.SwapConfirmations(ctx, coinID, contract, encode.UnixTimeMilli(int64(matchTime)))
Expand Down
8 changes: 6 additions & 2 deletions client/webserver/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,13 +601,17 @@ func (s *WebServer) apiAccelerateOrder(w http.ResponseWriter, r *http.Request) {
OrderID dex.Bytes `json:"orderID"`
NewRate uint64 `json:"newRate"`
}{}

defer form.Pass.Clear()
if !readPost(w, r, &form) {
return
}
pass, err := s.resolvePass(form.Pass, r)
if err != nil {
s.writeAPIError(w, fmt.Errorf("password error: %w", err))
return
}

txID, err := s.core.AccelerateOrder(form.Pass, form.OrderID, form.NewRate)
txID, err := s.core.AccelerateOrder(pass, form.OrderID, form.NewRate)
if err != nil {
s.writeAPIError(w, fmt.Errorf("Accelerate Order error: %w", err))
return
Expand Down

0 comments on commit 5cbeb16

Please sign in to comment.