Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions client/asset/bch/bch.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ var (
rpcWalletDefinition,
// electrumWalletDefinition, // getinfo RPC needs backport: https://github.com/Electron-Cash/Electron-Cash/pull/2399
},
ProtocolVersions: []uint32{0},
}
)

Expand Down
15 changes: 6 additions & 9 deletions client/asset/btc/btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ var (
electrumWalletDefinition,
},
LegacyWalletIndex: 1,
ProtocolVersions: []uint32{0},
}

client http.Client
Expand Down Expand Up @@ -3399,12 +3400,8 @@ func (btc *baseWallet) AuditContract(coinID, contract, txData dex.Bytes, rebroad

// LocktimeExpired returns true if the specified contract's locktime has
// expired, making it possible to issue a Refund.
func (btc *baseWallet) LocktimeExpired(_ context.Context, contract dex.Bytes) (bool, time.Time, error) {
_, _, locktime, _, err := dexbtc.ExtractSwapDetails(contract, btc.segwit, btc.chainParams)
if err != nil {
return false, time.Time{}, fmt.Errorf("error extracting contract locktime: %w", err)
}
contractExpiry := time.Unix(int64(locktime), 0).UTC()
func (btc *baseWallet) LocktimeExpired(_ context.Context, deets *dex.SwapContractDetails) (bool, time.Time, error) {

@chappjc chappjc Sep 6, 2022

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

One thing I really hate is when we have function arguments with way more info than is needed for a sensible implementation.

For BTC, say you're trying to use LocktimeExpored or Refund or whatever, so you're prepping to call the baseWallet method... you think, what fields of SwapContractDetails does it really need?
I get why. We're gravitating toward an interface that fits all assets and ETH is getting very different now.
But what can we do to avoid this? Obviously docs can be very clear for each asset to call out the used data, at a minimum.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

what fields of SwapContractDetails does it really need?

I'm with you on the API, but I'm not adding any more than is needed here. We could cache the data internally, but trying to avoid that.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I mean it's way more than needed for BTC.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why not stick all the data from SwapContractDetails into the contract dex.Bytes?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That sounds appealing to me too.

contractExpiry := time.Unix(int64(deets.LockTime), 0).UTC()
medianTime, err := btc.node.medianTime() // TODO: pass ctx
if err != nil {
return false, time.Time{}, fmt.Errorf("error getting median time: %w", err)
Expand All @@ -3418,7 +3415,7 @@ func (btc *baseWallet) LocktimeExpired(_ context.Context, contract dex.Bytes) (b
//
// This method blocks until the redemption is found, an error occurs or the
// provided context is canceled.
func (btc *intermediaryWallet) FindRedemption(ctx context.Context, coinID, _ dex.Bytes) (redemptionCoin, secret dex.Bytes, err error) {
func (btc *intermediaryWallet) FindRedemption(ctx context.Context, coinID, _ dex.Bytes, _ *dex.SwapContractDetails) (redemptionCoin, secret dex.Bytes, err error) {
txHash, vout, err := decodeCoinID(coinID)
if err != nil {
return nil, nil, fmt.Errorf("cannot decode contract coin id: %w", err)
Expand Down Expand Up @@ -3656,7 +3653,7 @@ func (btc *intermediaryWallet) tryRedemptionRequests(ctx context.Context, startB
// wallet does not store it, even though it was known when the init transaction
// was created. The client should store this information for persistence across
// sessions.
func (btc *baseWallet) Refund(coinID, contract dex.Bytes, feeRate uint64) (dex.Bytes, error) {
func (btc *baseWallet) Refund(coinID, contract dex.Bytes, _ *dex.SwapContractDetails, feeRate uint64) (dex.Bytes, error) {
txHash, vout, err := decodeCoinID(coinID)
if err != nil {
return nil, err
Expand Down Expand Up @@ -3890,7 +3887,7 @@ func (btc *baseWallet) send(address string, val uint64, feeRate uint64, subtract
// 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) {
func (btc *baseWallet) SwapConfirmations(_ context.Context, id dex.Bytes, contract dex.Bytes, startTime time.Time, _ *dex.SwapContractDetails) (uint32, bool, error) {
txHash, vout, err := decodeCoinID(id)
if err != nil {
return 0, false, err
Expand Down
30 changes: 15 additions & 15 deletions client/asset/btc/btc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2139,7 +2139,7 @@ func testFindRedemption(t *testing.T, segwit bool, walletType string) {
})

// Check find redemption result.
_, checkSecret, err := wallet.FindRedemption(tCtx, coinID, nil)
_, checkSecret, err := wallet.FindRedemption(tCtx, coinID, nil, nil)
if err != nil {
t.Fatalf("error finding redemption: %v", err)
}
Expand All @@ -2149,7 +2149,7 @@ func testFindRedemption(t *testing.T, segwit bool, walletType string) {

// gettransaction error
node.getTransactionErr = tErr
_, _, err = wallet.FindRedemption(tCtx, coinID, nil)
_, _, err = wallet.FindRedemption(tCtx, coinID, nil, nil)
if err == nil {
t.Fatalf("no error for gettransaction rpc error")
}
Expand All @@ -2160,7 +2160,7 @@ func testFindRedemption(t *testing.T, segwit bool, walletType string) {
delete(node.getCFilterScripts, *redeemBlockHash)
timedCtx, cancel := context.WithTimeout(tCtx, 500*time.Millisecond) // 0.5 seconds is long enough
defer cancel()
_, k, err := wallet.FindRedemption(timedCtx, coinID, nil)
_, k, err := wallet.FindRedemption(timedCtx, coinID, nil, nil)
if timedCtx.Err() == nil || k != nil {
// Expected ctx to cancel after timeout and no secret should be found.
t.Fatalf("unexpected result for missing redemption: secret: %v, err: %v", k, err)
Expand All @@ -2174,7 +2174,7 @@ func testFindRedemption(t *testing.T, segwit bool, walletType string) {
// Canceled context
deadCtx, cancelCtx := context.WithCancel(tCtx)
cancelCtx()
_, _, err = wallet.FindRedemption(deadCtx, coinID, nil)
_, _, err = wallet.FindRedemption(deadCtx, coinID, nil, nil)
if err == nil {
t.Fatalf("no error for canceled context")
}
Expand All @@ -2185,7 +2185,7 @@ func testFindRedemption(t *testing.T, segwit bool, walletType string) {
redeemVin.SignatureScript = randBytes(100)

node.blockchainMtx.Unlock()
_, _, err = wallet.FindRedemption(tCtx, coinID, nil)
_, _, err = wallet.FindRedemption(tCtx, coinID, nil, nil)
if err == nil {
t.Fatalf("no error for wrong redemption")
}
Expand All @@ -2195,7 +2195,7 @@ func testFindRedemption(t *testing.T, segwit bool, walletType string) {
node.blockchainMtx.Unlock()

// Sanity check to make sure it passes again.
_, _, err = wallet.FindRedemption(tCtx, coinID, nil)
_, _, err = wallet.FindRedemption(tCtx, coinID, nil, nil)
if err != nil {
t.Fatalf("error after clearing errors: %v", err)
}
Expand Down Expand Up @@ -2234,7 +2234,7 @@ func testRefund(t *testing.T, segwit bool, walletType string) {
node.getTransactionErr = WalletTransactionNotFound

contractOutput := newOutput(&txHash, 0, 1e8)
_, err = wallet.Refund(contractOutput.ID(), contract, feeSuggestion)
_, err = wallet.Refund(contractOutput.ID(), contract, nil, feeSuggestion)
if err != nil {
t.Fatalf("refund error: %v", err)
}
Expand All @@ -2243,14 +2243,14 @@ func testRefund(t *testing.T, segwit bool, walletType string) {
badReceipt := &tReceipt{
coin: &tCoin{id: make([]byte, 15)},
}
_, err = wallet.Refund(badReceipt.coin.id, badReceipt.Contract(), feeSuggestion)
_, err = wallet.Refund(badReceipt.coin.id, badReceipt.Contract(), nil, feeSuggestion)
if err == nil {
t.Fatalf("no error for bad receipt")
}

ensureErr := func(tag string) {
delete(node.checkpoints, outPt)
_, err = wallet.Refund(contractOutput.ID(), contract, feeSuggestion)
_, err = wallet.Refund(contractOutput.ID(), contract, nil, feeSuggestion)
if err == nil {
t.Fatalf("no error for %q", tag)
}
Expand All @@ -2266,7 +2266,7 @@ func testRefund(t *testing.T, segwit bool, walletType string) {
// bad contract
badContractOutput := newOutput(tTxHash, 0, 1e8)
badContract := randBytes(50)
_, err = wallet.Refund(badContractOutput.ID(), badContract, feeSuggestion)
_, err = wallet.Refund(badContractOutput.ID(), badContract, nil, feeSuggestion)
if err == nil {
t.Fatalf("no error for bad contract")
}
Expand Down Expand Up @@ -2301,7 +2301,7 @@ func testRefund(t *testing.T, segwit bool, walletType string) {
node.badSendHash = nil

// Sanity check that we can succeed again.
_, err = wallet.Refund(contractOutput.ID(), contract, feeSuggestion)
_, err = wallet.Refund(contractOutput.ID(), contract, nil, feeSuggestion)
if err != nil {
t.Fatalf("re-refund error: %v", err)
}
Expand Down Expand Up @@ -2507,7 +2507,7 @@ func testConfirmations(t *testing.T, segwit bool, walletType string) {
matchTime := swapBlock.Header.Timestamp

// Bad coin id
_, _, err := wallet.SwapConfirmations(context.Background(), randBytes(35), contract, matchTime)
_, _, err := wallet.SwapConfirmations(context.Background(), randBytes(35), contract, matchTime, nil)
if err == nil {
t.Fatalf("no error for bad coin ID")
}
Expand All @@ -2519,7 +2519,7 @@ func testConfirmations(t *testing.T, segwit bool, walletType string) {
}
node.txOutRes = txOutRes
node.getCFilterScripts[*blockHash] = [][]byte{pkScript}
confs, _, err := wallet.SwapConfirmations(context.Background(), coinID, contract, matchTime)
confs, _, err := wallet.SwapConfirmations(context.Background(), coinID, contract, matchTime, nil)
if err != nil {
t.Fatalf("error for gettransaction path: %v", err)
}
Expand All @@ -2531,7 +2531,7 @@ func testConfirmations(t *testing.T, segwit bool, walletType string) {
node.txOutRes = nil
node.getCFilterScripts[*blockHash] = nil
node.getTransactionErr = tErr
_, _, err = wallet.SwapConfirmations(context.Background(), coinID, contract, matchTime)
_, _, err = wallet.SwapConfirmations(context.Background(), coinID, contract, matchTime, nil)
if err == nil {
t.Fatalf("no error for gettransaction error")
}
Expand All @@ -2547,7 +2547,7 @@ func testConfirmations(t *testing.T, segwit bool, walletType string) {

node.getCFilterScripts[*spendingBlockHash] = [][]byte{pkScript}
node.walletTxSpent = true
_, spent, err := wallet.SwapConfirmations(context.Background(), coinID, contract, matchTime)
_, spent, err := wallet.SwapConfirmations(context.Background(), coinID, contract, matchTime, nil)
if err != nil {
t.Fatalf("error for spent swap: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion client/asset/btc/electrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (btc *ExchangeWalletElectrum) tryRedemptionRequests(ctx context.Context) {

// FindRedemption locates a swap contract output's redemption transaction input
// and the secret key used to spend the output.
func (btc *ExchangeWalletElectrum) FindRedemption(ctx context.Context, coinID, contract dex.Bytes) (redemptionCoin, secret dex.Bytes, err error) {
func (btc *ExchangeWalletElectrum) FindRedemption(ctx context.Context, coinID, contract dex.Bytes, _ *dex.SwapContractDetails) (redemptionCoin, secret dex.Bytes, err error) {
txHash, vout, err := decodeCoinID(coinID)
if err != nil {
return nil, nil, err
Expand Down
2 changes: 1 addition & 1 deletion client/asset/btc/electrum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func TestElectrumExchangeWallet(t *testing.T) {
}

// FindRedemption
redeemCoin, secretBytes, err := eew.FindRedemption(ctx, toCoinID(swapTxHash, swapVout), contract)
redeemCoin, secretBytes, err := eew.FindRedemption(ctx, toCoinID(swapTxHash, swapVout), contract, nil)
if err != nil {
t.Fatal(err)
}
Expand Down
10 changes: 5 additions & 5 deletions client/asset/btc/livetest/livetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func Run(t *testing.T, cfg *Config) {
confContract := receipts[0].Contract()
checkConfs := func(n uint32, expSpent bool) {
t.Helper()
confs, spent, err := rig.secondWallet.SwapConfirmations(context.Background(), confCoin.ID(), confContract, tStart)
confs, spent, err := rig.secondWallet.SwapConfirmations(context.Background(), confCoin.ID(), confContract, tStart, nil)
if err != nil {
if n > 0 || !errors.Is(err, asset.CoinNotFoundError) {
t.Fatalf("error getting %d confs: %v", n, err)
Expand Down Expand Up @@ -406,7 +406,7 @@ func Run(t *testing.T, cfg *Config) {
if auditCoin.Value() != swapVal {
t.Fatalf("wrong contract value. wanted %d, got %d", swapVal, auditCoin.Value())
}
confs, spent, err := rig.firstWallet.SwapConfirmations(tCtx, receipt.Coin().ID(), receipt.Contract(), tStart)
confs, spent, err := rig.firstWallet.SwapConfirmations(tCtx, receipt.Coin().ID(), receipt.Contract(), tStart, nil)
if err != nil {
t.Fatalf("error getting confirmations: %v", err)
}
Expand Down Expand Up @@ -460,7 +460,7 @@ func Run(t *testing.T, cfg *Config) {
TryFunc: func() wait.TryDirective {
ctx, cancel := context.WithTimeout(tCtx, time.Second)
defer cancel()
_, _, err = rig.secondWallet.FindRedemption(ctx, swapReceipt.Coin().ID(), nil)
_, _, err = rig.secondWallet.FindRedemption(ctx, swapReceipt.Coin().ID(), nil, nil)
if err != nil {
return wait.TryAgain
}
Expand All @@ -478,7 +478,7 @@ func Run(t *testing.T, cfg *Config) {
}
// Check that there is 1 confirmation on the swap
checkConfs(expConfs, true)
_, checkKey, err := rig.secondWallet.FindRedemption(ctx, swapReceipt.Coin().ID(), nil)
_, checkKey, err := rig.secondWallet.FindRedemption(ctx, swapReceipt.Coin().ID(), nil, nil)
if err != nil {
t.Fatalf("error finding confirmed redemption: %v", err)
}
Expand Down Expand Up @@ -525,7 +525,7 @@ func Run(t *testing.T, cfg *Config) {
mine()
}

coinID, err := rig.secondWallet.Refund(swapReceipt.Coin().ID(), swapReceipt.Contract(), 100)
coinID, err := rig.secondWallet.Refund(swapReceipt.Coin().ID(), swapReceipt.Contract(), nil, 100)
if err != nil {
t.Fatalf("refund error: %v", err)
}
Expand Down
15 changes: 6 additions & 9 deletions client/asset/dcr/dcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ var (
ConfigOpts: append(rpcOpts, walletOpts...),
},
},
ProtocolVersions: []uint32{0},
}
swapFeeBumpKey = "swapfeebump"
splitKey = "swapsplit"
Expand Down Expand Up @@ -2520,12 +2521,8 @@ func (dcr *ExchangeWallet) lookupTxOutput(ctx context.Context, txHash *chainhash

// LocktimeExpired returns true if the specified contract's locktime has
// expired, making it possible to issue a Refund.
func (dcr *ExchangeWallet) LocktimeExpired(ctx context.Context, contract dex.Bytes) (bool, time.Time, error) {
_, _, locktime, _, err := dexdcr.ExtractSwapDetails(contract, dcr.chainParams)
if err != nil {
return false, time.Time{}, fmt.Errorf("error extracting contract locktime: %w", err)
}
contractExpiry := time.Unix(int64(locktime), 0).UTC()
func (dcr *ExchangeWallet) LocktimeExpired(ctx context.Context, deets *dex.SwapContractDetails) (bool, time.Time, error) {
contractExpiry := time.Unix(int64(deets.LockTime), 0).UTC()
dcr.tipMtx.RLock()
blockHash := dcr.currentTip.hash
dcr.tipMtx.RUnlock()
Expand All @@ -2542,7 +2539,7 @@ func (dcr *ExchangeWallet) LocktimeExpired(ctx context.Context, contract dex.Byt
//
// This method blocks until the redemption is found, an error occurs or the
// provided context is canceled.
func (dcr *ExchangeWallet) FindRedemption(ctx context.Context, coinID, _ dex.Bytes) (redemptionCoin, secret dex.Bytes, err error) {
func (dcr *ExchangeWallet) FindRedemption(ctx context.Context, coinID, _ dex.Bytes, _ *dex.SwapContractDetails) (redemptionCoin, secret dex.Bytes, err error) {
txHash, vout, err := decodeCoinID(coinID)
if err != nil {
return nil, nil, fmt.Errorf("cannot decode contract coin id: %w", err)
Expand Down Expand Up @@ -2906,7 +2903,7 @@ func (dcr *ExchangeWallet) fatalFindRedemptionsError(err error, contractOutpoint
// wallet does not store it, even though it was known when the init transaction
// was created. The client should store this information for persistence across
// sessions.
func (dcr *ExchangeWallet) Refund(coinID, contract dex.Bytes, feeRate uint64) (dex.Bytes, error) {
func (dcr *ExchangeWallet) Refund(coinID, contract dex.Bytes, _ *dex.SwapContractDetails, feeRate uint64) (dex.Bytes, error) {
// Caller should provide a non-zero fee rate, so we could just do
// dcr.feeRateWithFallback(feeRate), but be permissive for now.
if feeRate == 0 {
Expand Down Expand Up @@ -3124,7 +3121,7 @@ func (dcr *ExchangeWallet) ValidateSecret(secret, secretHash []byte) bool {
// cannot see non-wallet transactions until they are mined.
//
// If the coin is located, but recognized as spent, no error is returned.
func (dcr *ExchangeWallet) SwapConfirmations(ctx context.Context, coinID, contract dex.Bytes, matchTime time.Time) (confs uint32, spent bool, err error) {
func (dcr *ExchangeWallet) SwapConfirmations(ctx context.Context, coinID, contract dex.Bytes, matchTime time.Time, _ *dex.SwapContractDetails) (confs uint32, spent bool, err error) {
txHash, vout, err := decodeCoinID(coinID)
if err != nil {
return 0, false, err
Expand Down
Loading