Skip to content

Commit

Permalink
loadbot/zec: Avoid using the same outputs.
Browse files Browse the repository at this point in the history
zec will complain about double spends if sendtoaddress is used too soon
after mining a block. Use a mutex to make sure we always wait a second
after mining to send.
  • Loading branch information
JoeGruffins authored and chappjc committed Jul 8, 2022
1 parent 577202e commit 3a0ffcb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion dex/testing/dcrdex/harness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ if [ $ZEC_ON -eq 0 ]; then
{
"base": "ZEC_simnet",
"quote": "BTC_simnet",
"lotSize": 100000000,
"lotSize": 1000000,
"rateStep": 1000,
"epochDuration": ${EPOCH_DURATION},
"marketBuyBuffer": 1.2
Expand Down
23 changes: 18 additions & 5 deletions dex/testing/loadbot/loadbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,12 @@ var (
defaultMidGap, marketBuyBuffer float64
keepMidGap bool

processesMtx = sync.Mutex{}
processes = []*process{}
processesMtx sync.Mutex
processes []*process

// zecSendMtx prevents sending funds too soon after mining a block and
// the harness choosing spent outputs for zcash.
zecSendMtx sync.Mutex
)

func init() {
Expand Down Expand Up @@ -181,9 +185,18 @@ func returnAddress(symbol, node string) string {
// mine will mine a single block on the node and asset indicated.
func mine(symbol, node string) <-chan *harnessResult {
n := 1
// geth may not include some tx at first because ???. Mine more.
if symbol == eth {
switch symbol {
case eth:
// geth may not include some tx at first because ???. Mine more.
n = 4
case zec:
// zcash has a problem selecting unused utxo for a second when
// also mining. https://github.com/zcash/zcash/issues/6045
zecSendMtx.Lock()
defer func() {
time.Sleep(time.Second)
zecSendMtx.Unlock()
}()
}
return harnessCtl(ctx, symbol, fmt.Sprintf("./mine-%s", node), fmt.Sprintf("%d", n))
}
Expand Down Expand Up @@ -449,7 +462,7 @@ func run() error {
if res.err != nil {
return "", fmt.Errorf("error getting %s address: %v", symbol, res.err)
}
return res.output, nil
return strings.Trim(res.output, `"`), nil
}

if alphaAddrBase, err = getAddress(baseSymbol, alpha); err != nil {
Expand Down
16 changes: 14 additions & 2 deletions dex/testing/loadbot/mantle.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func (m *Mantle) createWallet(symbol, node string, minFunds, maxFunds uint64, nu
}
}
}
<-harnessCtl(ctx, symbol, fmt.Sprintf("./mine-%s", node), "1")
<-mine(symbol, node)

// Wallet syncing time. If not synced within five seconds trading will fail.
time.Sleep(time.Second * 5)
Expand All @@ -400,8 +400,20 @@ func (m *Mantle) createWallet(symbol, node string, minFunds, maxFunds uint64, nu
func send(symbol, node, addr string, val uint64) error {
var res *harnessResult
switch symbol {
case btc, dcr, ltc, doge, bch, zec:
case btc, dcr, ltc, doge, bch:
res = <-harnessCtl(ctx, symbol, fmt.Sprintf("./%s", node), "sendtoaddress", addr, valString(val, symbol))
case zec:
// sendtoaddress will choose spent outputs if a block was
// recently mined. Use the zecSendMtx to ensure we have waited
// a sec after mining.
//
// TODO: This is not great and does not allow for multiple
// loadbots to run on zec at once. Find a better way to avoid
// double spends. Alternatively, wait for zec to fix this and
// remove the lock https://github.com/zcash/zcash/issues/6045
zecSendMtx.Lock()
res = <-harnessCtl(ctx, symbol, fmt.Sprintf("./%s", node), "sendtoaddress", addr, valString(val, symbol))
zecSendMtx.Unlock()
case eth:
// eth values are always handled as gwei, so multiply by 1e9
// here to convert to wei.
Expand Down
2 changes: 1 addition & 1 deletion dex/testing/zec/harness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ chmod +x "./start-wallet"

cat > "./connect-alpha" <<EOF
#!/usr/bin/env bash
${CLI} -rpcport=\$1 -regtest=1 -rpcuser=user -rpcpassword=pass addnode 127.0.0.1:${ALPHA_LISTEN_PORT} add
${CLI} -rpcport=\$1 -regtest=1 -rpcuser=user -rpcpassword=pass addnode 127.0.0.1:${ALPHA_LISTEN_PORT} onetry
EOF
chmod +x "./connect-alpha"

Expand Down

0 comments on commit 3a0ffcb

Please sign in to comment.