Skip to content

Commit

Permalink
client: Set conf deadline per asset.
Browse files Browse the repository at this point in the history
Ethereum appears to need a little more time to find swap confirmations
sometimes on testnet. Make the confirmation deadline a per-asset
constant and double it for eth.
  • Loading branch information
JoeGruffins authored and chappjc committed Apr 15, 2022
1 parent 91c450a commit 85dcae7
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 7 deletions.
7 changes: 7 additions & 0 deletions client/asset/dcr/dcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ const (

walletTypeDcrwRPC = "dcrwalletRPC"
walletTypeLegacy = "" // dcrwallet RPC prior to wallet types

// confCheckTimeout is the amount of time allowed to check for
// confirmations.
confCheckTimeout = 2 * time.Second
)

var (
Expand Down Expand Up @@ -2545,6 +2549,9 @@ func (dcr *ExchangeWallet) SwapConfirmations(ctx context.Context, coinID, contra
return 0, false, err
}

ctx, cancel := context.WithTimeout(ctx, confCheckTimeout)
defer cancel()

// Check if we can find the contract onchain without using cfilters.
_, confs, spent, err = dcr.lookupTxOutput(ctx, txHash, vout)
if err == nil {
Expand Down
1 change: 0 additions & 1 deletion client/asset/eth/contractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ func (c *contractorV0) redeem(txOpts *bind.TransactOpts, redemptions []*asset.Re

func (c *contractorV0) swap(ctx context.Context, secretHash [32]byte) (*dexeth.SwapState, error) {
callOpts := &bind.CallOpts{
Pending: true,
From: c.acctAddr,
Context: ctx,
}
Expand Down
8 changes: 8 additions & 0 deletions client/asset/eth/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ const (
defaultSendGasLimit = 21_000

walletTypeGeth = "geth"

// confCheckTimeout is the amount of time allowed to check for
// confirmations. Testing on testnet has shown spikes up to 2.5
// seconds. This value may need to be adjusted in the future.
confCheckTimeout = 4 * time.Second
)

var (
Expand Down Expand Up @@ -1269,6 +1274,9 @@ func (eth *ExchangeWallet) SwapConfirmations(ctx context.Context, _ dex.Bytes, c
return 0, false, err
}

ctx, cancel := context.WithTimeout(ctx, confCheckTimeout)
defer cancel()

hdr, err := eth.node.bestHeader(ctx)
if err != nil {
return 0, false, fmt.Errorf("error fetching best header: %w", err)
Expand Down
7 changes: 5 additions & 2 deletions client/asset/eth/eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2043,8 +2043,11 @@ func TestSwapConfirmation(t *testing.T) {

ver := uint32(0)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

checkResult := func(expErr bool, expConfs uint32, expSpent bool) {
confs, spent, err := eth.SwapConfirmations(nil, nil, dexeth.EncodeContractData(ver, secretHash), time.Time{})
confs, spent, err := eth.SwapConfirmations(ctx, nil, dexeth.EncodeContractData(ver, secretHash), time.Time{})
if err != nil {
if expErr {
return
Expand Down Expand Up @@ -2078,7 +2081,7 @@ func TestSwapConfirmation(t *testing.T) {

// ErrSwapNotInitiated
state.State = dexeth.SSNone
_, _, err := eth.SwapConfirmations(nil, nil, dexeth.EncodeContractData(0, secretHash), time.Time{})
_, _, err := eth.SwapConfirmations(ctx, nil, dexeth.EncodeContractData(0, secretHash), time.Time{})
if !errors.Is(err, asset.ErrSwapNotInitiated) {
t.Fatalf("expected ErrSwapNotInitiated, got %v", err)
}
Expand Down
2 changes: 0 additions & 2 deletions client/core/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import (
"decred.org/dcrdex/dex/wait"
)

const confCheckTimeout = 2 * time.Second

// ExpirationErr indicates that the wait.TickerQueue has expired a waiter, e.g.
// a reported coin was not found before the set expiration time.
type ExpirationErr string
Expand Down
2 changes: 0 additions & 2 deletions client/core/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,6 @@ func (w *xcWallet) LogFilePath() (string, error) {
// 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) {
ctx, cancel := context.WithTimeout(ctx, confCheckTimeout)
defer cancel()
return w.Wallet.SwapConfirmations(ctx, coinID, contract, encode.UnixTimeMilli(int64(matchTime)))
}

Expand Down

0 comments on commit 85dcae7

Please sign in to comment.