Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
itswisdomagain committed Nov 8, 2021
1 parent 73ca58c commit a991e82
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
12 changes: 9 additions & 3 deletions client/asset/btc/btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1827,7 +1827,6 @@ func (btc *ExchangeWallet) SignMessage(coin asset.Coin, msg dex.Bytes) (pubkeys,
// AuditContract retrieves information about a swap contract from the provided
// txData. The extracted information would be used to audit the counter-party's
// contract during a swap.
// TODO: Broadcast the txData.
func (btc *ExchangeWallet) AuditContract(coinID, contract, txData dex.Bytes, since time.Time) (*asset.AuditInfo, error) {
txHash, vout, err := decodeCoinID(coinID)
if err != nil {
Expand All @@ -1839,8 +1838,8 @@ func (btc *ExchangeWallet) AuditContract(coinID, contract, txData dex.Bytes, sin
return nil, fmt.Errorf("error extracting swap addresses: %w", err)
}

// Perform basic validation using the txData. Also want to broadcast the transaction if using
// an spvWallet. It may be worth separating data validation from coin
// Perform basic validation using the txData.
// It may be worth separating data validation from coin
// retrieval at the asset.Wallet interface level.
tx, err := msgTxFromBytes(txData)
if err != nil {
Expand Down Expand Up @@ -1886,6 +1885,13 @@ func (btc *ExchangeWallet) AuditContract(coinID, contract, txData dex.Bytes, sin
contractHash, addr.ScriptAddress())
}

// Broadcast the transaction.
if hashSent, err := btc.node.sendRawTransaction(tx); err != nil {
btc.log.Debugf("Rebroadcasting counterparty contract %v (THIS MAY BE NORMAL): %v", txHash, err)
} else if !hashSent.IsEqual(txHash) {
btc.log.Errorf("Counterparty contract %v was rebroadcast as %v!", txHash, hashSent)
}

return &asset.AuditInfo{
Coin: newOutput(txHash, vout, uint64(txOut.Value)),
Recipient: receiver.String(),
Expand Down
29 changes: 11 additions & 18 deletions client/asset/dcr/dcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -1551,30 +1551,23 @@ func (dcr *ExchangeWallet) AuditContract(coinID, contract, txData dex.Bytes, _ t
contractHash, addrScript)
}

// The counter-party should have broadcasted the contract tx but
// rebroadcast just in case to ensure that the tx is sent to the
// network.
if hashSent, err := dcr.wallet.SendRawTransaction(dcr.ctx, contractTx, true); err != nil {
dcr.log.Debugf("Rebroadcasting counterparty contract %v (THIS MAY BE NORMAL): %v", txHash, err)
} else if !hashSent.IsEqual(txHash) {
dcr.log.Errorf("Counterparty contract %v was rebroadcast as %v!", txHash, hashSent)
}

txTree := determineTxTree(contractTx)
auditInfo := &asset.AuditInfo{
return &asset.AuditInfo{
Coin: newOutput(txHash, vout, uint64(contractTxOut.Value), txTree),
Contract: contract,
SecretHash: secretHash,
Recipient: receiver.String(),
Expiration: time.Unix(int64(stamp), 0).UTC(),
}

// The counter-party should have broadcasted the contract tx but
// rebroadcast just in case to ensure that the tx is sent to the
// network.
dcr.log.Debugf("Rebroadcasting contract tx %v.", txData)
allowHighFees := true // high fees shouldn't prevent this tx from being bcast
finalTxHash, err := dcr.wallet.SendRawTransaction(dcr.ctx, contractTx, allowHighFees)
if err != nil {
dcr.log.Errorf("Error rebroadcasting contract tx %v: %v.", txData, err)
} else if !finalTxHash.IsEqual(txHash) {
return nil, fmt.Errorf("broadcasted contract tx, but received unexpected transaction ID back from RPC server. "+
"expected %s, got %s", txHash, finalTxHash)
}

dcr.log.Infof("Audited contract coin %s:%d using raw tx data. SPV mode = %t", txHash, vout, dcr.wallet.SpvMode())
return auditInfo, nil
}, nil
}

func determineTxTree(msgTx *wire.MsgTx) int8 {
Expand Down
20 changes: 10 additions & 10 deletions client/asset/dcr/externaltx.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,17 +363,17 @@ func (dcr *ExchangeWallet) isOutputSpent(ctx context.Context, output *outputSpen
}

// Cache relevant spender info if the spender is found.
spent := spenderTx != nil
if spent {
spenderBlockHash, err := chainhash.NewHashFromStr(spenderTx.BlockHash)
if err != nil {
// dcr.log.Errorf("Invalid hash (%s) for tx that spends output %s: %v", spenderTx.BlockHash, output.op, err)
return false, fmt.Errorf("invalid hash (%s) for tx that spends output %s: %v", spenderTx.BlockHash, output.op, err)
} else {
output.spenderBlock = &block{hash: spenderBlockHash, height: spenderTx.BlockHeight}
}
if spenderTx == nil {
return false, nil
}

spenderBlockHash, err := chainhash.NewHashFromStr(spenderTx.BlockHash)
if err != nil {
return true, fmt.Errorf("invalid hash (%s) for tx that spends output %s: %w",
spenderTx.BlockHash, output.op, err)
}
return spent, err
output.spenderBlock = &block{hash: spenderBlockHash, height: spenderTx.BlockHeight}
return true, nil
}

// findTxOutSpender attempts to find and return the tx that spends the provided
Expand Down

0 comments on commit a991e82

Please sign in to comment.