Skip to content

Commit

Permalink
dex/testing/dcr: fix reorg script
Browse files Browse the repository at this point in the history
The dcr beta node is only able to add 1 block to the mainchain after
disconnecting from the alpha node, because the beta node has no way to
receive votes while disconnected from alpha. This prevents reorgs on
either nodes since the reorg script ends with both nodes reconnected but
having just 1 divergent mainchain block each.

This is fixed by mining an "extra" block on either node (after reconnection)
to trigger a reorg on the other node.

Additionally, the dcr reorg script is modified to accept 2 optional arguments:
`./reorg (node depth), where
- `node` is the node that should experience a reorg, either "alpha" or "beta"
- `depth` is the number of blocks that should be purged from `node`. Currently,
 due to the beta mining limitation mentioned above, only 1 block can be purged
 from either nodes during reorg, but the reorged node will always see `depth`
 new blocks after the reorg.
  • Loading branch information
itswisdomagain authored and chappjc committed Aug 23, 2021
1 parent 0490291 commit c34f5f7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
10 changes: 8 additions & 2 deletions dex/testing/dcr/README.md
Expand Up @@ -30,8 +30,14 @@ allow you to perform RPC calls against each wallet.
wallets.
Try `./alpha getbalance`, for example.

`./reorg` will step through a script that causes the alpha node to undergo a
1-deep reorganization.
`./reorg` will step through a script that causes the alpha or beta node to
undergo a 1-deep reorganization. The script takes the following optional
arguments: `./reorg {node} {depth}`, where:
- `node` is the node that should undergo a reorg, either "alpha" or "beta" (default: alpha)
- `depth` is the number of blocks that should be reorged into `node` (default: 1 for alpha, 3 for beta).
Currently, only 1 block (instead of `depth` blocks) can be purged from either
nodes during reorg, but the reorged node will always see `depth` new blocks
after the reorg.

`./quit` shuts down the nodes and closes the tmux session.

Expand Down
48 changes: 41 additions & 7 deletions dex/testing/dcr/harness.sh
Expand Up @@ -115,19 +115,53 @@ chmod +x "${NODES_ROOT}/harness-ctl/mine-beta"
# Reorg script
cat > "${NODES_ROOT}/harness-ctl/reorg" <<EOF
#!/usr/bin/env bash
REORG_NODE="alpha"
VALID_NODE="beta"
REORG_DEPTH=1
if [ "\$1" = "beta" ]; then
REORG_NODE="beta"
VALID_NODE="alpha"
REORG_DEPTH=3
fi
if [ "\$2" != "" ]; then
REORG_DEPTH=\$2
fi
# TODO: Cannot currently cause a 1+ block re-org on alpha because
# beta cannot mine more than 1 main chain block while disconnected
# from alpha.
if [ "\${REORG_NODE}" = "alpha" ] && [ \${REORG_DEPTH} -gt 1 ]; then
echo "Cannot cause a re-org of more than 1 block on alpha."
exit 1
fi
echo "Current alpha, beta best blocks"
./alpha getbestblock && ./beta getbestblock
echo "Disconnecting beta from alpha"
sleep 1
./beta addnode 127.0.0.1:${ALPHA_NODE_PORT} remove
echo "Mining a block on alpha"
sleep 1
./mine-alpha 1
echo "Mining 3 blocks on beta"
./mine-beta 3
sleep 2
# Mine equal number of blocks while disconnected.
for NODE in \${REORG_NODE} \${VALID_NODE}; do
echo "Mining \${REORG_DEPTH} blocks on \${NODE}"
./mine-\${NODE} \${REORG_DEPTH}
sleep 1
done
echo "Diverged alpha, beta best blocks" && ./alpha getbestblock && ./beta getbestblock
echo "Reconnecting beta to alpha"
./beta addnode 127.0.0.1:${ALPHA_NODE_PORT} add
sleep 1
echo "Mining 1 more block on \${VALID_NODE} to trigger re-org on \${REORG_NODE}"
./mine-\${VALID_NODE} 1
sleep 2
grep REORG ${NODES_ROOT}/alpha/logs/simnet/dcrd.log
echo "Reconnected alpha, beta best blocks" && ./alpha getbestblock && ./beta getbestblock
grep REORG ${NODES_ROOT}/\${REORG_NODE}/logs/simnet/dcrd.log
EOF
chmod +x "${NODES_ROOT}/harness-ctl/reorg"

Expand Down

0 comments on commit c34f5f7

Please sign in to comment.