Skip to content

Commit

Permalink
rebase fix, partial chapp review corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
itswisdomagain committed Oct 5, 2021
1 parent 3832df9 commit 6ab26fc
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 33 deletions.
31 changes: 15 additions & 16 deletions client/asset/dcr/dcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -1641,9 +1641,9 @@ func (dcr *ExchangeWallet) SignMessage(coin asset.Coin, msg dex.Bytes) (pubkeys,
// The information returned would be used to verify the counter-party's contract
// during a swap.
//
// NOTE: For SPV wallets, a successful audit response is no gaurantee that the
// NOTE: For SPV wallets, a successful audit response is no guarantee that the
// txData provided to this method was actually broadcasted to the blockchain.
// An error may have occured while trying to broadcast the txData or even if
// An error may have occurred while trying to broadcast the txData or even if
// there was no broadcast error, the tx might still not enter mempool or get
// mined e.g. if the tx references invalid or already spent inputs.
//
Expand Down Expand Up @@ -1677,7 +1677,7 @@ func (dcr *ExchangeWallet) AuditContract(coinID, contract, txData dex.Bytes, _ t
if err = dcr.validateContractOutputScript(contractOutputScript, scriptVer, contract); err != nil {
return nil, err
}
dcr.log.Debugf("Audited contract coin %s:%d using tx data gotten from the blockchain. SPV mode = %t",
dcr.log.Infof("Audited contract coin %s:%d using tx data gotten from the blockchain. SPV mode = %t",
txHash, vout, dcr.spvMode)
return &asset.AuditInfo{
Coin: contractCoin,
Expand Down Expand Up @@ -1743,7 +1743,7 @@ func (dcr *ExchangeWallet) AuditContract(coinID, contract, txData dex.Bytes, _ t
// SPV wallets do not produce sendrawtransaction errors for already
// broadcasted txs. This must be some other unexpected error.
// Do NOT return an asset.CoinNotFoundError so callers do not recall
// this method as there's no gaurantee that the broadcast will succeed
// this method as there's no guarantee that the broadcast will succeed
// on subsequent attempts.
// Return a successful audit response because it is possible that the
// tx was already broadcasted and the caller can safely begin waiting
Expand All @@ -1752,9 +1752,7 @@ func (dcr *ExchangeWallet) AuditContract(coinID, contract, txData dex.Bytes, _ t
// but as explained above, retrying the broadcast isn't a better course
// of action, neither is returning an error here because that would cause
// the caller to potentially give up on this match prematurely.
}

if err == nil && !finalTxHash.IsEqual(txHash) {
} 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)
}
Expand All @@ -1765,7 +1763,7 @@ func (dcr *ExchangeWallet) AuditContract(coinID, contract, txData dex.Bytes, _ t
dcr.trackExternalTx(txHash, contractTxOut.PkScript)
}

dcr.log.Debugf("Audited contract coin %s:%d using raw tx data. SPV mode = %t", txHash, vout, dcr.spvMode)
dcr.log.Infof("Audited contract coin %s:%d using raw tx data. SPV mode = %t", txHash, vout, dcr.spvMode)
return &asset.AuditInfo{
Coin: newOutput(txHash, vout, uint64(contractTxOut.Value), determineTxTree(contractTx)),
Contract: contract,
Expand Down Expand Up @@ -2632,7 +2630,8 @@ func (dcr *ExchangeWallet) SyncStatus() (bool, float32, error) {
if err != nil {
return false, 0, fmt.Errorf("rawrequest error: %w", err)
}
return syncStatus.Synced || syncStatus.InitialBlockDownload, syncStatus.HeadersFetchProgress, nil
ready := syncStatus.Synced && !syncStatus.InitialBlockDownload
return ready, syncStatus.HeadersFetchProgress, nil
}

// Combines the RPC type with the spending input information.
Expand Down Expand Up @@ -3212,27 +3211,27 @@ func (dcr *ExchangeWallet) getBlockHash(blockHeight int64) (*chainhash.Hash, err

// mainChainAncestor crawls blocks backwards starting at the provided hash
// until finding a mainchain block. Returns the first mainchain block found.
func (dcr *ExchangeWallet) mainChainAncestor(blockHash *chainhash.Hash) (*chainhash.Hash, *chainjson.GetBlockVerboseResult, error) {
func (dcr *ExchangeWallet) mainChainAncestor(blockHash *chainhash.Hash) (*chainhash.Hash, int64, error) {
if *blockHash == zeroHash {
return nil, nil, fmt.Errorf("invalid block hash %s", blockHash.String())
return nil, 0, fmt.Errorf("invalid block hash %s", blockHash.String())
}

checkHash := blockHash
for {
checkBlock, err := dcr.node.GetBlockVerbose(dcr.ctx, checkHash, false)
checkBlock, err := dcr.node.GetBlockHeaderVerbose(dcr.ctx, checkHash)
if err != nil {
return nil, nil, fmt.Errorf("error retrieving block %s: %w", checkHash, translateRPCCancelErr(err))
return nil, 0, fmt.Errorf("error retrieving block %s: %w", checkHash, translateRPCCancelErr(err))
}
if checkBlock.Confirmations > -1 {
// This is a mainchain block, return the hash.
return checkHash, checkBlock, nil
return checkHash, int64(checkBlock.Height), nil
}
if checkBlock.Height == 0 {
return nil, nil, fmt.Errorf("no mainchain ancestor for block %s", blockHash.String())
return nil, 0, fmt.Errorf("no mainchain ancestor for block %s", blockHash.String())
}
checkHash, err = chainhash.NewHashFromStr(checkBlock.PreviousHash)
if err != nil {
return nil, nil, fmt.Errorf("error decoding previous hash %s for block %s: %w",
return nil, 0, fmt.Errorf("error decoding previous hash %s for block %s: %w",
checkBlock.PreviousHash, checkHash.String(), translateRPCCancelErr(err))
}
}
Expand Down
27 changes: 13 additions & 14 deletions client/asset/dcr/externaltx.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type externalTx struct {

blockFiltersScanner

// The folowing are protected by the blockFiltersScanner.scanMtx
// The following are protected by the blockFiltersScanner.scanMtx
// because they are set when the tx's block is found and cleared
// when the previously found tx block is orphaned. The scanMtx
// lock must be held for read before accessing these fields.
Expand All @@ -52,7 +52,7 @@ type blockFiltersScanner struct {
}

// relevantBlockHash returns the hash of the block relevant to this scanner, if
// the relevantBlok is set and is a mainchain block. Returns a nil hash and nil
// the relevantBlock is set and is a mainchain block. Returns a nil hash and nil
// error if the relevantBlock is set but is no longer part of the mainchain.
// The scanner's scanMtx MUST be write-locked.
func (scanner *blockFiltersScanner) relevantBlockHash(nodeGetBlockHashFn func(int64) (*chainhash.Hash, error)) (*chainhash.Hash, error) {
Expand Down Expand Up @@ -80,8 +80,7 @@ func (dcr *ExchangeWallet) trackExternalTx(hash *chainhash.Hash, script []byte)
dcr.log.Debugf("Script %x cached for non-wallet tx %s.", script, hash)
}()

tx, tracked := dcr.externalTx(hash)
if tracked {
if tx, tracked := dcr.externalTx(hash); tracked {
tx.mtx.Lock()
tx.relevantScripts = append(tx.relevantScripts, script)
tx.mtx.Unlock()
Expand Down Expand Up @@ -217,11 +216,11 @@ func (dcr *ExchangeWallet) findExternalTxBlock(tx *externalTx) (bool, error) {
// mainchain, scan back to the mainchain ancestor of the lastScannedBlock.
var lastScannedBlock block
if tx.lastScannedBlock != nil {
stopBlockHash, stopBlock, err := dcr.mainChainAncestor(tx.lastScannedBlock)
stopBlockHash, stopBlockHeight, err := dcr.mainChainAncestor(tx.lastScannedBlock)
if err != nil {
return false, fmt.Errorf("error looking up mainchain ancestor for block %s", err)
}
lastScannedBlock.height = stopBlock.Height
lastScannedBlock.height = stopBlockHeight
lastScannedBlock.hash = stopBlockHash
tx.lastScannedBlock = stopBlockHash
} else {
Expand Down Expand Up @@ -280,8 +279,7 @@ func (dcr *ExchangeWallet) findExternalTxBlock(tx *externalTx) (bool, error) {
continue
}
tx.outputs[uint32(i)] = &externalTxOutput{
// TODO: output.ScriptPubKey.Version with dcrd 1.7 *release*, not yet
TxOut: newTxOut(int64(amt), output.Version, pkScript),
TxOut: newTxOut(int64(amt), output.ScriptPubKey.Version, pkScript),
txHash: tx.hash,
vout: uint32(i),
}
Expand Down Expand Up @@ -332,11 +330,12 @@ func (dcr *ExchangeWallet) findExternalTxOutputSpender(output *externalTxOutput,
// Use mainChainAncestor to ensure that scanning starts from a mainchain
// block in the event that either tx block or lastScannedBlock have been
// re-orged out of the mainchain.
var startBlock *chainjson.GetBlockVerboseResult
var startBlockHash *chainhash.Hash
var startBlockHeight int64
if output.spender.lastScannedBlock == nil {
_, startBlock, err = dcr.mainChainAncestor(txBlockHash)
startBlockHash, startBlockHeight, err = dcr.mainChainAncestor(txBlockHash)
} else {
_, startBlock, err = dcr.mainChainAncestor(output.spender.lastScannedBlock)
startBlockHash, startBlockHeight, err = dcr.mainChainAncestor(output.spender.lastScannedBlock)
}
if err != nil {
return false, err
Expand All @@ -345,8 +344,8 @@ func (dcr *ExchangeWallet) findExternalTxOutputSpender(output *externalTxOutput,
// Search for this output's spender in the blocks between startBlock and bestBlock.
bestBlock := dcr.cachedBestBlock()
dcr.log.Debugf("Searching for the tx that spends output %s in blocks %d (%s) to %d (%s).",
output, startBlock.Height, startBlock.Hash, bestBlock.height, bestBlock.hash)
for blockHeight := startBlock.Height; blockHeight <= bestBlock.height; blockHeight++ {
output, startBlockHeight, startBlockHash, bestBlock.height, bestBlock.hash)
for blockHeight := startBlockHeight; blockHeight <= bestBlock.height; blockHeight++ {
blockHash, err := dcr.node.GetBlockHash(dcr.ctx, blockHeight)
if err != nil {
return false, translateRPCCancelErr(err)
Expand Down Expand Up @@ -387,7 +386,7 @@ func (dcr *ExchangeWallet) findExternalTxOutputSpender(output *externalTxOutput,
}

dcr.log.Debugf("No spender tx found for %s in blocks %d (%s) to %d (%s).",
output, startBlock.Height, startBlock.Hash, bestBlock.height, bestBlock.hash)
output, startBlockHeight, startBlockHash, bestBlock.height, bestBlock.hash)
return false, nil // scanned up to best block, no spender found
}

Expand Down
2 changes: 1 addition & 1 deletion client/core/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -2200,7 +2200,7 @@ func (t *trackedTrade) reAuditContract(match *matchTracker) error {

proof := &match.MetaData.Proof
coinID, contract, txData := match.counterSwap.Coin.ID(), proof.CounterContract, proof.CounterTxData
auditInfo, err := t.wallets.toWallet.AuditContract(coinID, contract, txData)
auditInfo, err := t.wallets.toWallet.AuditContract(coinID, contract, txData, match.matchTime())
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions dex/testing/dcr/harness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,14 @@ ${ALPHA_WALLET_RPC_PORT} ${USE_SPV} ${ENABLE_VOTING} ${ALPHA_WALLET_HTTPPROF_POR
# alpha uses walletpassphrase/walletlock.

echo "Creating simnet beta wallet"
USE_SPV="0"
USE_SPV="1"
ENABLE_VOTING="0"
"${HARNESS_DIR}/create-wallet.sh" "$SESSION:4" "beta" ${BETA_WALLET_SEED} \
${BETA_WALLET_RPC_PORT} ${USE_SPV} ${ENABLE_VOTING} ${BETA_WALLET_HTTPPROF_PORT}

# The trading wallets need to be created from scratch every time.
echo "Creating simnet trading wallet 1"
USE_SPV="0"
USE_SPV="1"
ENABLE_VOTING="0"
"${HARNESS_DIR}/create-wallet.sh" "$SESSION:5" "trading1" ${TRADING_WALLET1_SEED} \
${TRADING_WALLET1_PORT} ${USE_SPV} ${ENABLE_VOTING}
Expand Down

0 comments on commit 6ab26fc

Please sign in to comment.