diff --git a/client/asset/dcr/dcr.go b/client/asset/dcr/dcr.go index e46ac92ba2..1980d85761 100644 --- a/client/asset/dcr/dcr.go +++ b/client/asset/dcr/dcr.go @@ -65,6 +65,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 ( @@ -2609,6 +2613,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 { diff --git a/client/asset/eth/contractor.go b/client/asset/eth/contractor.go index 28e04ec48f..5c7a9b9b9f 100644 --- a/client/asset/eth/contractor.go +++ b/client/asset/eth/contractor.go @@ -144,7 +144,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, } diff --git a/client/asset/eth/eth.go b/client/asset/eth/eth.go index bbff1593dc..6de7312b23 100644 --- a/client/asset/eth/eth.go +++ b/client/asset/eth/eth.go @@ -63,6 +63,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 ( @@ -1281,6 +1286,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) diff --git a/client/asset/eth/eth_test.go b/client/asset/eth/eth_test.go index fbcd5a000a..20e7326843 100644 --- a/client/asset/eth/eth_test.go +++ b/client/asset/eth/eth_test.go @@ -2080,8 +2080,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 @@ -2115,7 +2118,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) } diff --git a/client/core/trade.go b/client/core/trade.go index 8c70a78963..e2e64f5a4f 100644 --- a/client/core/trade.go +++ b/client/core/trade.go @@ -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 diff --git a/client/core/wallet.go b/client/core/wallet.go index 5ed05bada6..16e409417f 100644 --- a/client/core/wallet.go +++ b/client/core/wallet.go @@ -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))) }