Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi: Add Ethereum server asset. #979

Merged
merged 8 commits into from
May 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions dex/testing/dcrdex/harness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ TEST_DB=dcrdex_simnet_test
sudo -u postgres -H psql -c "DROP DATABASE IF EXISTS ${TEST_DB}" \
-c "CREATE DATABASE ${TEST_DB} OWNER dcrdex"

EPOCH_DURATION=${EPOCH:-15000}
EPOCH_DURATION=${EPOCH:-15000}
if [ "${EPOCH_DURATION}" -lt 1000 ]; then
echo "epoch duration cannot be < 1000 ms"
exit 1
Expand All @@ -39,6 +39,9 @@ BCH_ON=$?
~/dextest/ltc/harness-ctl/alpha getblockchaininfo &> /dev/null
LTC_ON=$?

~/dextest/eth/harness-ctl/alpha attach --exec 'eth.blockNumber' > /dev/null
ETH_ON=$?

set -e

# Write markets.json.
Expand Down Expand Up @@ -129,6 +132,20 @@ if [ $BCH_ON -eq 0 ]; then
EOF
fi

if [ $ETH_ON -eq 0 ]; then
cat << EOF >> "./markets.json"
},
"ETH_simnet": {
"bip44symbol": "eth",
"network": "simnet",
"lotSize": 1000000,
"rateStep": 1000000,
"maxFeeRate": 20,
"swapConf": 2,
"configPath": "${TEST_ROOT}/eth/alpha/node/geth.ipc"
EOF
fi

cat << EOF >> "./markets.json"
}
}
Expand Down Expand Up @@ -156,7 +173,7 @@ abstakerlotlimit=1200
httpprof=1
EOF

# Set the postgres user pass if provided.
# Set the postgres user pass if provided.
if [ -n "${PG_PASS}" ]; then
echo pgpass="${PG_PASS}" >> ./dcrdex.conf
fi
Expand Down
16 changes: 10 additions & 6 deletions dex/testing/eth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@ 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.
The harness depends on [geth](https://github.com/ethereum/go-ethereum/tree/master/cmd/geth)
to run.

## Using

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

The harness script will create two connected private nodes both with mining
abilities and pre-funded addresses.
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.

## 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` and `./beta` are just `geth` configured for their respective data
directories.
`./alpha`, `./beta`, `./gamma`, and `./delta` are just `geth` configured for
their respective data directories.

Try `./alpha attach`, for example. This will put you in an interactive console
with the alpha node.
Expand All @@ -35,7 +39,7 @@ with the alpha node.
If things aren't looking right, you may need to look at the node windows to
see errors. In tmux, you can navigate between windows by typing `Ctrl+b` and
then the window number. The window numbers are listed at the bottom
of the tmux window. `Ctrl+b` followed by the number `0`, for example, will
of the tmux window. `Ctrl+b` followed by the number `1`, for example, will
change to the alpha node window. Examining the node output to look for errors
is usually a good first debugging step.

Expand Down
79 changes: 50 additions & 29 deletions dex/testing/eth/create-node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
# Script for creating eth nodes.
set -e

# The following are required script arguments
# 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=$8
ADDRESS_PASSWORD=$9
ADDRESS_JSON=${10}
ADDRESS_JSON_FILE_NAME=${11}
NODE_KEY=${12}
ADDRESS_JSON=$8
ADDRESS_JSON_FILE_NAME=$9
NODE_KEY=${10}
SYNC_MODE=${11}

GROUP_DIR="${NODES_ROOT}/${NAME}"
MINE_JS="${GROUP_DIR}/mine.js"
JoeGruffins marked this conversation as resolved.
Show resolved Hide resolved
NODE_DIR="${GROUP_DIR}/node"
mkdir -p "${NODE_DIR}"
mkdir -p "${NODE_DIR}/keystore"
mkdir -p "${NODE_DIR}/geth"

# Write node ctl script.
cat > "${NODES_ROOT}/harness-ctl/${NAME}" <<EOF
Expand All @@ -28,25 +29,26 @@ geth --datadir="${NODE_DIR}" \$*
EOF
chmod +x "${NODES_ROOT}/harness-ctl/${NAME}"

# Write mine script.
cat > "${NODES_ROOT}/harness-ctl/mine-${NAME}" <<EOF
# Write mine script if CHAIN_ADDRESS is present.
if [ "${CHAIN_ADDRESS}" != "_" ]; then
cat > "${NODES_ROOT}/harness-ctl/mine-${NAME}" <<EOF
#!/usr/bin/env bash
NUM=1
case \$1 in
''|*[!0-9]*) ;;
*) NUM=\$1 ;;
esac
for i in \$(seq \$NUM) ; do
./${NAME} attach --preload "${MINE_JS}" --exec 'mine()'
"${NODES_ROOT}/harness-ctl/${NAME}" attach --preload "${MINE_JS}" --exec 'mine()'
done
EOF
chmod +x "${NODES_ROOT}/harness-ctl/mine-${NAME}"
chmod +x "${NODES_ROOT}/harness-ctl/mine-${NAME}"

# Write mining javascript.
# NOTE: This sometimes mines more than one block. It is a race. This returns
# the number of blocks mined within the lifespan of the function, but one more
# MAY be mined after returning.
cat > "${MINE_JS}" <<EOF
# Write mining javascript.
# NOTE: This sometimes mines more than one block. It is a race. This returns
# the number of blocks mined within the lifespan of the function, but one more
# MAY be mined after returning.
cat > "${MINE_JS}" <<EOF
function mine() {
blkN = eth.blockNumber;
miner.start();
Expand All @@ -56,19 +58,17 @@ function mine() {
}
EOF

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not appear that this was unlocking the account. We will pass the password when sending funds.

EOF

fi

cat > "${NODE_DIR}/eth.conf" <<EOF
[Eth]
NetworkId = 42
SyncMode = "full"

[Eth.Miner]
Etherbase = "0x${CHAIN_ADDRESS}"
SyncMode = "${SYNC_MODE}"

[Eth.Ethash]
DatasetDir = "${NODE_DIR}/.ethash"
Expand All @@ -84,6 +84,16 @@ 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}"
GasPrice = 81000000000
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
Expand All @@ -97,10 +107,13 @@ tmux wait-for "${NAME}"

# Create two accounts. The first is used to mine blocks. The second contains
# funds.
echo "Creating account"
cat > "${NODE_DIR}/keystore/$CHAIN_ADDRESS_JSON_FILE_NAME" <<EOF
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
Expand All @@ -111,9 +124,17 @@ cat > "${NODE_DIR}/geth/nodekey" <<EOF
$NODE_KEY
EOF

# Start the eth node with both accounts unlocked, listening restricted to
# localhost, and syncmode set to full.
echo "Starting simnet ${NAME} node"
tmux send-keys -t "$TMUX_WIN_ID" "${NODES_ROOT}/harness-ctl/${NAME} --nodiscover " \
"--config ${NODE_DIR}/eth.conf --unlock ${CHAIN_ADDRESS},${ADDRESS} " \
"--password ${GROUP_DIR}/password" C-m
if [ "${SYNC_MODE}" = "fast" ]; 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} --nodiscover " \
"--config ${NODE_DIR}/eth.conf --unlock ${CHAIN_ADDRESS} " \
"--password ${GROUP_DIR}/password --light.serve 25" 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} --nodiscover " \
"--config ${NODE_DIR}/eth.conf" C-m
fi
86 changes: 75 additions & 11 deletions dex/testing/eth/harness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,27 @@ ALPHA_NODE_KEY="71d810d39333296b518c846a3e49eca55f998fd7994998bb3e5048567f2f073c
# ALPHA_ENODE="897c84f6e4f18195413c1d02927e6a4093f5e7574b52bdec6f20844c4f1f6dd3f16036a9e600bd8681ab50fd8dd144df4a6ba9dd8722bb578a86aaa8222c964f"
ALPHA_NODE_PORT="30304"

BETA_ADDRESS="4f8ef3892b65ed7fc356ff473a2ef2ae5ec27a06"
# BETA_ADDRESS="4f8ef3892b65ed7fc356ff473a2ef2ae5ec27a06"
BETA_ADDRESS_JSON_FILE_NAME="UTC--2021-01-27T08-20-58.179642501Z--4f8ef3892b65ed7fc356ff473a2ef2ae5ec27a06"
BETA_ADDRESS_JSON='{"address":"4f8ef3892b65ed7fc356ff473a2ef2ae5ec27a06","crypto":{"cipher":"aes-128-ctr","ciphertext":"c5672bb829df9e209ca8ce18dbdd1fed69c603d639e06ab09127b672a609c121","cipherparams":{"iv":"24460eb2934c8b61cee3ad0aa7b843c0"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"1f85da881994ca7b4a23f0698da70500a4b79f97a4450b83b129ebf3b4c28f50"},"mac":"1ecea707f1bffa1f6f944cb47e83118d8179e8a5005b83c88610b7e8692a1197"},"id":"56633762-6fb1-4cbf-8396-3a2e4661f7d4","version":3}'
BETA_NODE_KEY="0f3f23a0f14202da009bd59a96457098acea901986629e54d5be1eea32fc404a"
BETA_ENODE="b1d3e358ee5c9b268e911f2cab47bc12d0e65c80a6d2b453fece34facc9ac3caed14aa3bc7578166bb08c5bc9719e5a2267ae14e0b42da393f4d86f6d5829061"
BETA_NODE_PORT="30305"

GAMMA_ADDRESS="41293c2032bac60aa747374e966f79f575d42379"
GAMMA_ADDRESS_JSON_FILE_NAME="UTC--2021-03-01T02-12-42.714340074Z--41293c2032bac60aa747374e966f79f575d42379"
GAMMA_ADDRESS_JSON='{"address":"41293c2032bac60aa747374e966f79f575d42379","crypto":{"cipher":"aes-128-ctr","ciphertext":"5191719067513511b07d959de1a86cd37c3f7011dce75f62c791114c3a62b15b","cipherparams":{"iv":"cdfcd9e475f2af7df08a8a36cc0de976"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"5630591da82b8517f1b8f61719fbb552e41f25861cc20bc4671a11a47b427d31"},"mac":"d13259851d78deb70d1273ab151d4a12583b94f5cbdf31d86f02bb549d241d36"},"id":"235ba177-e32c-4d23-8d94-a57bc04b97ca","version":3}'
GAMMA_NODE_KEY="9e102b8ba8cad4c6b9db6c881915d3f1bb206e76113266bf48266de0474844fd"
GAMMA_ENODE="b1c14deee09b9d5549c90b7b30a35c812a56bf6afea5873b05d7a1bcd79c7b0848bcfa982faf80cc9e758a3a0d9b470f0a002840d365050fd5bf45052a6ec313"
GAMMA_NODE_PORT="30306"

DELTA_ADDRESS="d12ab7cf72ccf1f3882ec99ddc53cd415635c3be"
DELTA_ADDRESS_JSON_FILE_NAME="UTC--2021-03-01T02-31-13.365402148Z--d12ab7cf72ccf1f3882ec99ddc53cd415635c3be"
DELTA_ADDRESS_JSON='{"address":"d12ab7cf72ccf1f3882ec99ddc53cd415635c3be","crypto":{"cipher":"aes-128-ctr","ciphertext":"a0e9a3da5d0c88c922b5d7e817693552fe17dfd4c598e2a8b08ee53a706a8ffc","cipherparams":{"iv":"28b0a443403b7a02001f07a35724f6e6"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"f790f584bf396cacc06f28201aa697825011e84f570759d6108e20c5ee4fffce"},"mac":"529318e5eec2474221912d01e5a534a0b1dbfb19499ffaf942be6375611caa83"},"id":"d8670e33-8094-45b7-9386-d936e6bf4c1b","version":3}'
DELTA_NODE_KEY="725394672587b34bbf15580c59e5199c75c2c7e998ba8df3cb38cc4347d46e2b"
DELTA_ENODE="ca414c361d1a38716170923e4900d9dc9203dbaf8fdcaee73e1f861df9fdf20a1453b76fd218c18bc6f3c7e13cbca0b3416af02a53b8e31188faa45aab398d1c"
DELTA_NODE_PORT="30307"

# PASSWORD is the password used to unlock all accounts/wallets/addresses.
PASSWORD="abc"

Expand All @@ -45,6 +59,8 @@ echo "Writing ctl scripts"
# protocols take effect. "clique" is our proof of authority scheme. One block
# can be mined per second with a signature belonging to the address in
# "extradata". The addresses in the "alloc" field are allocated "balance".
# Values are in wei. 1*10^18 wei is equal to one eth. Addresses are allocated
# 11,000 eth.
cat > "${NODES_ROOT}/genesis.json" <<EOF
{
"config": {
Expand All @@ -62,19 +78,26 @@ cat > "${NODES_ROOT}/genesis.json" <<EOF
}
},
"difficulty": "1",
"gasLimit": "8000000",
"gasLimit": "12487783",
"extradata": "0x00000000000000000000000000000000000000000000000000000000000000009ebba10a6136607688ca4f27fab70e23938cd0270000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"alloc": {
"18d65fb8d60c1199bb1ad381be47aa692b482605": {
"balance": "1000000000000000"
"balance": "11000000000000000000000"
},
"4f8ef3892b65ed7fc356ff473a2ef2ae5ec27a06": {
"balance": "1000000000000000"
"balance": "11000000000000000000000"
}
}
}
EOF

cat > "${NODES_ROOT}/harness-ctl/send.js" <<EOF
function send(from, to, amt) {
personal.sendTransaction({from:"0x"+from,to:"0x"+to,value:amt,gasPrice:81000000000}, "${PASSWORD}")
return true;
}
EOF

# Add node script.
HARNESS_DIR=$(dirname "$0")
cp "${HARNESS_DIR}/create-node.sh" "${NODES_ROOT}/harness-ctl/create-node"
Expand Down Expand Up @@ -107,19 +130,60 @@ tmux send-keys -t $SESSION:0 "cd ${NODES_ROOT}/harness-ctl" C-m

echo "Starting simnet alpha node"
"${HARNESS_DIR}/create-node.sh" "$SESSION:1" "alpha" "$ALPHA_NODE_PORT" \
"$CHAIN_ADDRESS" "$PASSWORD" "$CHAIN_ADDRESS_JSON " \
"$CHAIN_ADDRESS_JSON_FILE_NAME" "$ALPHA_ADDRESS" "$PASSWORD" \
"$ALPHA_ADDRESS_JSON" "$ALPHA_ADDRESS_JSON_FILE_NAME" "$ALPHA_NODE_KEY"
"$CHAIN_ADDRESS" "$PASSWORD" "$CHAIN_ADDRESS_JSON" \
"$CHAIN_ADDRESS_JSON_FILE_NAME" "$ALPHA_ADDRESS_JSON" "$ALPHA_ADDRESS_JSON_FILE_NAME" \
"$ALPHA_NODE_KEY" "fast"

echo "Starting simnet beta node"
"${HARNESS_DIR}/create-node.sh" "$SESSION:2" "beta" "$BETA_NODE_PORT" \
"$CHAIN_ADDRESS" "$PASSWORD" "$CHAIN_ADDRESS_JSON " \
"$CHAIN_ADDRESS_JSON_FILE_NAME" "$BETA_ADDRESS" "$PASSWORD" \
"$BETA_ADDRESS_JSON" "$BETA_ADDRESS_JSON_FILE_NAME" "$BETA_NODE_KEY"
"$CHAIN_ADDRESS" "$PASSWORD" "$CHAIN_ADDRESS_JSON" \
"$CHAIN_ADDRESS_JSON_FILE_NAME" "$BETA_ADDRESS_JSON" "$BETA_ADDRESS_JSON_FILE_NAME" \
"$BETA_NODE_KEY" "fast"

echo "Starting simnet gamma node"
"${HARNESS_DIR}/create-node.sh" "$SESSION:3" "gamma" "$GAMMA_NODE_PORT" \
"_" "_" "_" "_" "$GAMMA_ADDRESS_JSON" "$GAMMA_ADDRESS_JSON_FILE_NAME" \
"$GAMMA_NODE_KEY" "light"

echo "Starting simnet delta node"
"${HARNESS_DIR}/create-node.sh" "$SESSION:4" "delta" "$DELTA_NODE_PORT" \
"_" "_" "_" "_" "$DELTA_ADDRESS_JSON" "$DELTA_ADDRESS_JSON_FILE_NAME" \
"$DELTA_NODE_KEY" "light"

sleep 1

# NOTE: This will cause beta to connect automatically to alpha.
# NOTE: Connecting a node will add for both. Also, light nodes take longer to
# set up. They will show 0 peers for some amount of time even after adding here.
echo "Connecting nodes"
"${NODES_ROOT}/harness-ctl/alpha" "attach --exec admin.addPeer('enode://${BETA_ENODE}@127.0.0.1:$BETA_NODE_PORT')"
"${NODES_ROOT}/harness-ctl/alpha" "attach --exec admin.addPeer('enode://${GAMMA_ENODE}@127.0.0.1:$GAMMA_NODE_PORT')"
"${NODES_ROOT}/harness-ctl/alpha" "attach --exec admin.addPeer('enode://${DELTA_ENODE}@127.0.0.1:$DELTA_NODE_PORT')"
"${NODES_ROOT}/harness-ctl/beta" "attach --exec admin.addPeer('enode://${GAMMA_ENODE}@127.0.0.1:$GAMMA_NODE_PORT')"
"${NODES_ROOT}/harness-ctl/beta" "attach --exec admin.addPeer('enode://${DELTA_ENODE}@127.0.0.1:$DELTA_NODE_PORT')"
"${NODES_ROOT}/harness-ctl/gamma" "attach --exec admin.addPeer('enode://${DELTA_ENODE}@127.0.0.1:$DELTA_NODE_PORT')"

sleep 1

SEND_AMT=5000000000000000000000
echo "Sending 5000 eth to beta and gamma."
"${NODES_ROOT}/harness-ctl/alpha" "attach --preload ${NODES_ROOT}/harness-ctl/send.js --exec send(\"${ALPHA_ADDRESS}\",\"${GAMMA_ADDRESS}\",${SEND_AMT})"
"${NODES_ROOT}/harness-ctl/alpha" "attach --preload ${NODES_ROOT}/harness-ctl/send.js --exec send(\"${ALPHA_ADDRESS}\",\"${DELTA_ADDRESS}\",${SEND_AMT})"

# Our transactions will PROBABLY appear in one of these blocks.
echo "Mining some blocks"
"${NODES_ROOT}/harness-ctl/mine-alpha" "15"

# Initial sync for light nodes takes quite a while. Wait for them to show
# blocks on the network.
while true
do
N=$("${NODES_ROOT}/harness-ctl/gamma" "attach --exec eth.blockNumber")
if [ "$N" -gt 0 ]; then
break
fi
echo "Waiting for light nodes to sync."
sleep 5
done

# Reenable history and attach to the control session.
tmux select-window -t $SESSION:0
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
github.com/decred/dcrd/wire v1.4.0
github.com/decred/go-socks v1.1.0
github.com/decred/slog v1.1.0
github.com/ethereum/go-ethereum v1.9.25
github.com/gcash/bchd v0.17.2-0.20201218180520-5708823e0e99
github.com/gcash/bchutil v0.0.0-20210113190856-6ea28dff4000
github.com/go-chi/chi/v5 v5.0.1
Expand Down
Loading