Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: Set conf deadline per asset. #1552

Merged
merged 1 commit into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions client/asset/dcr/dcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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()
Comment on lines +2616 to +2617
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I'm reconsidering timeouts for the dcr and spv wallets if they need to do a cfilters scan because those can be more time consuming.

Actually, did you mean to skip BTC's timeout in this PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looked like btc didn't use the context. Does it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dcrdex/client/asset/btc/btc.go

Lines 2766 to 2769 in 11fe415

// SwapConfirmations gets the number of confirmations for the specified swap
// by first checking for a unspent output, and if not found, searching indexed
// wallet transactions.
func (btc *baseWallet) SwapConfirmations(_ context.Context, id dex.Bytes, contract dex.Bytes, startTime time.Time) (uint32, bool, error) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good point. We have a TODO to update call so a context gets used, but you're right it's not used now.


// 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 @@ -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,
}
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 @@ -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 (
Expand Down Expand Up @@ -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)
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 @@ -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
Expand Down Expand Up @@ -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)
}
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