Skip to content

Commit

Permalink
harness: Update eth to work with geth 1.14 and up.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeGruffins committed Dec 20, 2024
1 parent 0030e49 commit b5883f0
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 325 deletions.
23 changes: 2 additions & 21 deletions client/asset/eth/chaincfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"strings"

"decred.org/dcrdex/dex"
dexeth "decred.org/dcrdex/dex/networks/eth"
"github.com/ethereum/go-ethereum/common"
ethcore "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/eth/ethconfig"
Expand Down Expand Up @@ -103,10 +102,8 @@ func ETHConfig(net dex.Network) (c ethconfig.Config, err error) {
case dex.Testnet:
c.Genesis = ethcore.DefaultSepoliaGenesisBlock()
case dex.Simnet:
c.Genesis, err = readSimnetGenesisFile()
if err != nil {
return c, fmt.Errorf("readSimnetGenesisFile error: %w", err)
}
// Args are gasLimit, faucet address.
c.Genesis = ethcore.DeveloperGenesisBlock(30000000, nil)
default:
return c, fmt.Errorf("unknown network %d", net)

Expand All @@ -123,19 +120,3 @@ func ChainConfig(net dex.Network) (c *params.ChainConfig, err error) {
}
return cfg.Genesis.Config, nil
}

// readSimnetGenesisFile reads the simnet genesis file.
func readSimnetGenesisFile() (*ethcore.Genesis, error) {
dataDir, err := simnetDataDir()
if err != nil {
return nil, err
}

genesisFile := filepath.Join(dataDir, "genesis.json")
genesisCfg, err := dexeth.LoadGenesisFile(genesisFile)
if err != nil {
return nil, fmt.Errorf("error reading genesis file: %v", err)
}

return genesisCfg, nil
}
14 changes: 10 additions & 4 deletions dex/networks/erc20/contracts/TestToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// This is a simplified version of OpenZeppelin Contracts v4.4.0 (token/ERC20/ERC20.sol).

pragma solidity = 0.8.18;
pragma solidity = 0.8.28;

contract TestToken {
event Transfer(address indexed _from, address indexed _to, uint256 _value);
Expand Down Expand Up @@ -31,9 +31,15 @@ contract TestToken {
_decimals = dec;

_totalSupply = 44000000000000000000000;
_balances[0x18D65FB8d60c1199bb1Ad381bE47aA692b482605] = 11000000000000000000000; // alpha
_balances[0x4F8eF3892B65ED7fc356fF473a2eF2aE5EC27A06] = 11000000000000000000000; // beta
_balances[0xd12aB7cf72CCf1f3882eC99DDc53CD415635C3bE] = 11000000000000000000000; // delta
}

/**
* @dev Set the balance for the sender. Not part of a real erc20 token's
* api.
*/
function setSenderBalance(uint256 amt) public virtual returns (bool) {
_balances[msg.sender] = amt;
return true;
}

/**
Expand Down
17 changes: 7 additions & 10 deletions dex/testing/eth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,32 @@ sandboxed environment for testing dex swap transactions.
## Dependencies

The harness depends on [geth](https://github.com/ethereum/go-ethereum/tree/master/cmd/geth)
to run. geth v1.13.4+ is recommended.
to run. geth v1.14.9+ is recommended.

It also requires tmux and bc.

## Using

You must have `geth` in `PATH` to use the harness.

The harness script will create four connected private nodes. Two, alpha and
beta, have mining abilities and pre-funded addresses with syncmode set to
"fast". They are meant to be used with server functions. Two more, gamma and
delta, are "light" nodes without mining abilites and with addresses that have
been sent funds. They are intenended to be used with client functions.
The harness script will create one private node, alpha, running in --dev mode.
alpha has scripts to send coins and tokens to any address. Blocks are mined
every 10 seconds in the mining tmux session.

## Harness control scripts

The `./harness.sh` script will drop you into a tmux window in a directory
called `harness-ctl`. Inside of this directory are a number of scripts to
allow you to perform RPC calls against each wallet.

`./alpha`, `./beta`, `./gamma`, and `./delta` are just `geth` configured for
their respective data directories.
`./alpha` is just `geth` configured for its data directory.

Try `./alpha attach`, for example. This will put you in an interactive console
with the alpha node.

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

`./mine-alpha n` and `./mine-beta n` will mine n blocks on the respective node.
`./mine-alpha n` will mine about n blocks. It is not precise.

## Dev Stuff

Expand All @@ -46,4 +43,4 @@ change to the alpha node window. Examining the node output to look for errors
is usually a good first debugging step.

If you encouter a problem, the harness can be killed from another terminal with
`tmux kill-session -t eth-harness`.
`tmux kill-session -t eth-harness`. Nodes can be killed with `sudo pkill -9 geth`.
99 changes: 14 additions & 85 deletions dex/testing/eth/create-node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,15 @@ set -ex
# The following are required script arguments.
TMUX_WIN_ID=$1
NAME=$2
NODE_PORT=$3
CHAIN_ADDRESS=$4
CHAIN_PASSWORD=$5
CHAIN_ADDRESS_JSON=$6
CHAIN_ADDRESS_JSON_FILE_NAME=$7
ADDRESS_JSON=$8
ADDRESS_JSON_FILE_NAME=$9
NODE_KEY=${10}
SYNC_MODE=${11}
AUTHRPC_PORT=${12}
HTTP_PORT=${13}
WS_PORT=${14}
WS_MODULES=${15}
AUTHRPC_PORT=${3}
HTTP_PORT=${4}
WS_PORT=${5}
WS_MODULES=${6}

GROUP_DIR="${NODES_ROOT}/${NAME}"
MINE_JS="${GROUP_DIR}/mine.js"
NODE_DIR="${GROUP_DIR}/node"
mkdir -p "${NODE_DIR}"
mkdir -p "${NODE_DIR}/keystore"
mkdir -p "${NODE_DIR}/geth"

# Write node ctl script.
Expand All @@ -33,10 +23,9 @@ geth --datadir="${NODE_DIR}" \$*
EOF
chmod +x "${NODES_ROOT}/harness-ctl/${NAME}"

# Write mine script if CHAIN_ADDRESS is present.
if [ "${CHAIN_ADDRESS}" != "_" ]; then
# The mining script may end up mining more or less blocks than specified.
cat > "${NODES_ROOT}/harness-ctl/mine-${NAME}" <<EOF
# The mining script may end up mining more or less blocks than specified. It
# also sends a transaction which must be done in --dev mode.
cat > "${NODES_ROOT}/harness-ctl/mine-${NAME}" <<EOF
#!/usr/bin/env bash
NUM=2
case \$1 in
Expand All @@ -45,6 +34,7 @@ if [ "${CHAIN_ADDRESS}" != "_" ]; then
esac
echo "Mining..."
BEFORE=\$("${NODES_ROOT}/harness-ctl/${NAME}" attach --exec 'eth.blockNumber')
"${NODES_ROOT}/harness-ctl/${NAME}" attach --exec 'eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[0],value:1})' > /dev/null
"${NODES_ROOT}/harness-ctl/${NAME}" attach --exec 'miner.start()' > /dev/null
sleep \$(echo "\$NUM-1.8" | bc)
"${NODES_ROOT}/harness-ctl/${NAME}" attach --exec 'miner.stop()' > /dev/null
Expand All @@ -60,87 +50,26 @@ if [ "${CHAIN_ADDRESS}" != "_" ]; then
EOF
chmod +x "${NODES_ROOT}/harness-ctl/mine-${NAME}"

# Write password file to unlock accounts later.
cat > "${GROUP_DIR}/password" <<EOF
$CHAIN_PASSWORD
EOF

fi

cat > "${NODE_DIR}/eth.conf" <<EOF
[Eth]
NetworkId = 42
SyncMode = "${SYNC_MODE}"
SyncMode = "snap"
[Node]
DataDir = "${NODE_DIR}"
AuthPort = ${AUTHRPC_PORT}
[Node.P2P]
NoDiscovery = true
BootstrapNodes = []
BootstrapNodesV5 = []
ListenAddr = ":${NODE_PORT}"
NetRestrict = [ "127.0.0.1/8", "::1/128" ]
EOF

# Add etherbase if mining.
if [ "${CHAIN_ADDRESS}" != "_" ]; then
cat >> "${NODE_DIR}/eth.conf" <<EOF
[Eth.Miner]
Etherbase = "0x${CHAIN_ADDRESS}"
GasFloor = 30000000
GasCeil = 30000000
EOF
fi

# Create a tmux window.
tmux new-window -t "$TMUX_WIN_ID" -n "${NAME}" "${SHELL}"
tmux send-keys -t "$TMUX_WIN_ID" "set +o history" C-m
tmux send-keys -t "$TMUX_WIN_ID" "cd ${NODE_DIR}" C-m

# Create and wait for a node initiated with a predefined genesis json.
echo "Creating simnet ${NAME} node"
tmux send-keys -t "$TMUX_WIN_ID" "${NODES_ROOT}/harness-ctl/${NAME} init "\
"$GENESIS_JSON_FILE_LOCATION; tmux wait-for -S ${NAME}" C-m
tmux wait-for "${NAME}"

# Create two accounts. The first is used to mine blocks. The second contains
# funds.
if [ "${CHAIN_ADDRESS}" != "_" ]; then
echo "Creating account"
cat > "${NODE_DIR}/keystore/$CHAIN_ADDRESS_JSON_FILE_NAME" <<EOF
$CHAIN_ADDRESS_JSON
EOF
fi

cat > "${NODE_DIR}/keystore/$ADDRESS_JSON_FILE_NAME" <<EOF
$ADDRESS_JSON
EOF

# The node key lets us control the enode address value.
echo "Setting node key"
cat > "${NODE_DIR}/geth/nodekey" <<EOF
$NODE_KEY
EOF

echo "Starting simnet ${NAME} node"
if [ "${SYNC_MODE}" = "snap" ]; then
# Start the eth node with the chain account unlocked, listening restricted to
# localhost, and our custom configuration file.
tmux send-keys -t "$TMUX_WIN_ID" "${NODES_ROOT}/harness-ctl/${NAME} " \
"--config ${NODE_DIR}/eth.conf --unlock ${CHAIN_ADDRESS} " \
"--password ${GROUP_DIR}/password --light.serve 25 --datadir.ancient " \
"${NODE_DIR}/geth-ancient --verbosity 5 --vmdebug --http --http.port " \
"${HTTP_PORT} --ws --ws.port ${WS_PORT} --ws.api " \
"${WS_MODULES} --allow-insecure-unlock --rpc.enabledeprecatedpersonal " \
"2>&1 | tee ${NODE_DIR}/${NAME}.log" C-m

else
# Start the eth node listening restricted to localhost and our custom
# configuration file.
tmux send-keys -t "$TMUX_WIN_ID" "${NODES_ROOT}/harness-ctl/${NAME} --allow-insecure-unlock --rpc.enabledeprecatedpersonal " \
"--config ${NODE_DIR}/eth.conf --verbosity 5 ${HTTP_OPT} 2>&1 | tee " \
"${NODE_DIR}/${NAME}.log" C-m
fi
# Start the eth node with our custom configuration file.
tmux send-keys -t "$TMUX_WIN_ID" "${NODES_ROOT}/harness-ctl/${NAME} " \
"--config ${NODE_DIR}/eth.conf --verbosity 5 --vmdebug --http --http.port " \
"${HTTP_PORT} --ws --ws.port ${WS_PORT} --ws.api ${WS_MODULES} " \
"--dev 2>&1 | tee ${NODE_DIR}/${NAME}.log" C-m
Loading

0 comments on commit b5883f0

Please sign in to comment.