Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeGruffins committed Dec 18, 2024
1 parent 2c48ea4 commit c1bdb50
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 191 deletions.
31 changes: 7 additions & 24 deletions dex/testing/eth/create-node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,22 @@ ListenAddr = ":${NODE_PORT}"
NetRestrict = [ "127.0.0.1/8", "::1/128" ]
EOF

MINING_ARG=""

# Add etherbase if mining.
if [ "${CHAIN_ADDRESS}" != "_" ]; then
MINING_ARG="--miner.etherbase 0x${CHAIN_ADDRESS}"
cat >> "${NODE_DIR}/eth.conf" <<EOF
[Eth.Miner]
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}"
#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.
Expand All @@ -127,21 +121,10 @@ $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 ${MINING_ARG}" \
"${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 ${MINING_ARG} --verbosity 5 ${HTTP_OPT} 2>&1 | tee " \
"${NODE_DIR}/${NAME}.log" C-m
fi
"--password ${GROUP_DIR}/password --verbosity 5 --vmdebug --http --http.port " \
"${HTTP_PORT} --ws --ws.port ${WS_PORT} --ws.api ${WS_MODULES} --allow-insecure-unlock " \
"--rpc.enabledeprecatedpersonal --dev 2>&1 | tee ${NODE_DIR}/${NAME}.log" C-m
177 changes: 10 additions & 167 deletions dex/testing/eth/harness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,6 @@ ALPHA_HTTP_PORT="38556"
ALPHA_WS_PORT="38557"
ALPHA_WS_MODULES="eth"

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"
BETA_AUTHRPC_PORT="8553"
BETA_HTTP_PORT="38558"
BETA_WS_PORT="38559"
BETA_WS_MODULES="eth,txpool"

# TODO: Light nodes broken as of geth 1.13.4-stable. Enable them when possible.

# TESTING_ADDRESS is used by the client's internal node.
TESTING_ADDRESS="946dfaB1AD7caCFeF77dE70ea68819a30acD4577"
SIMNET_TOKEN_ADDRESS="946dfaB1AD7caCFeF77dE70ea68819a30acD4577"
Expand All @@ -56,62 +43,17 @@ if [ -d "${NODES_ROOT}" ]; then
fi

mkdir -p "${NODES_ROOT}/alpha"
mkdir -p "${NODES_ROOT}/beta"
mkdir -p "${NODES_ROOT}/harness-ctl"

echo "Writing ctl scripts"
################################################################################
# Control Scripts
################################################################################

# Write genesis json. ".*Block" fields represent block height where certain
# 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. The addresses belong to alpha and beta nodes and two others are
# used in tests.
cat > "${NODES_ROOT}/genesis.json" <<EOF
{
"config": {
"chainId": 42,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"muirGlacierBlock": 0,
"berlinBlock": 0,
"londonBlock": 0,
"arrowGlacierBlock": 0,
"grayGlacierBlock": 0,
"mergeNetSplitBlock": 0,
"shanghaiBlock": 0,
"cancunBlock": 0,
"clique": {
"period": 1,
"epoch": 30000
}
},
"difficulty": "1",
"gasLimit": "30000000",
"extradata": "0x00000000000000000000000000000000000000000000000000000000000000009ebba10a6136607688ca4f27fab70e23938cd0270000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"alloc": {
"${ALPHA_ADDRESS}": {
"balance": "100000000000000000000000000"
}
}
}
EOF

cat > "${NODES_ROOT}/harness-ctl/send.js" <<EOF
function send(from, to, value) {
from = from.startsWith('0x') ? from : '0x' + from
function send(to, value) {
to = to.startsWith('0x') ? to : '0x' + to
return personal.sendTransaction({ from, to, value, gasPrice: 82000000000 }, "${PASSWORD}")
return eth.sendTransaction({ from: eth.accounts[0], to, value})
}
EOF

Expand All @@ -122,24 +64,24 @@ EOF
chmod +x "${NODES_ROOT}/harness-ctl/sendtoaddress"

cat > "${NODES_ROOT}/harness-ctl/deploy.js" <<EOF
function deploy(from, contract) {
tx = personal.sendTransaction({from:"0x"+from,data:"0x"+contract,gasPrice:82000000000}, "${PASSWORD}")
function deploy(contract) {
tx = eth.sendTransaction({from: eth.accounts[0],data:"0x"+contract})
return tx;
}
function deployERC20(from, contract, decimals) {
function deployERC20(contract, decimals) {
const hexDecimals = decimals.toString(16);
const data = "0x" + contract + hexDecimals.padStart(64, "0");
tx = personal.sendTransaction({from:"0x"+from,data:data,gasPrice:82000000000}, "${PASSWORD}")
tx = eth.sendTransaction({from: eth.accounts[0],data:data})
return tx;
}
function deployERC20Swap(from, contract, tokenAddr) {
function deployERC20Swap(contract, tokenAddr) {
if (tokenAddr.slice(0, 2) === "0x") {
tokenAddr = tokenAddr.slice(2)
}
var paddedAddr = tokenAddr.padStart(64, "0")
tx = personal.sendTransaction({from:"0x"+from,data:"0x"+contract+paddedAddr,gasPrice:82000000000}, "${PASSWORD}")
tx = eth.sendTransaction({from:eth.accounts[0],data:"0x"+contract+paddedAddr})
return tx;
}
EOF
Expand All @@ -155,74 +97,11 @@ EOF
HARNESS_DIR=$(dirname "$0")
cp "${HARNESS_DIR}/create-node.sh" "${NODES_ROOT}/harness-ctl/create-node"

# Reorg script
cat > "${NODES_ROOT}/harness-ctl/reorg" <<EOF
#!/usr/bin/env bash
REORG_NODE="alpha"
VALID_NODE="beta"
REORG_DEPTH=2
if [ "\$1" = "beta" ]; then
REORG_NODE="beta"
VALID_NODE="alpha"
fi
if [ "\$2" != "" ]; then
REORG_DEPTH=\$2
fi
echo "Before alpha, beta best blocks"
./alpha attach --exec 'eth.getBlock(eth.blockNumber)'
./beta attach --exec 'eth.getBlock(eth.blockNumber)'
NODES=('alpha' 'beta')
ENODES=($ALPHA_ENODE $BETA_ENODE)
PORTS=($ALPHA_NODE_PORT $BETA_NODE_PORT)
echo "Disconnecting nodes"
for node in "\${NODES[@]}"
do
for i in {0..1}
do
"./\$node" "attach --exec admin.removePeer('enode://\${ENODES[i]}@127.0.0.1:\${PORTS[i]}')"
done
done
sleep 1
# Uncomment to see the effect of reorgs on transactions.
# "./alpha" "attach --exec personal.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:1,gasPrice:82000000000},\\"${PASSWORD}\\")"
# "./beta" "attach --exec personal.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:1,gasPrice:82000000000},\\"${PASSWORD}\\")"
"./mine-\$VALID_NODE" \$((REORG_DEPTH + 2))
"./mine-\$REORG_NODE" \$REORG_DEPTH
sleep 1
echo "Connecting nodes"
for node in "\${NODES[@]}"
do
for i in {0..1}
do
"./\$node" "attach --exec admin.addPeer('enode://\${ENODES[i]}@127.0.0.1:\${PORTS[i]}')"
done
done
sleep 1
echo "After alpha, beta best blocks"
./alpha attach --exec 'eth.getBlock(eth.blockNumber)'
./beta attach --exec 'eth.getBlock(eth.blockNumber)'
EOF
chmod +x "${NODES_ROOT}/harness-ctl/reorg"

# Shutdown script
cat > "${NODES_ROOT}/harness-ctl/quit" <<EOF
#!/usr/bin/env bash
tmux send-keys -t $SESSION:5 C-c
tmux send-keys -t $SESSION:1 C-c
tmux send-keys -t $SESSION:2 C-c
tmux kill-session
EOF
chmod +x "${NODES_ROOT}/harness-ctl/quit"
Expand All @@ -248,32 +127,13 @@ echo "Starting simnet alpha node"
"$ALPHA_NODE_KEY" "snap" "$ALPHA_AUTHRPC_PORT" "$ALPHA_HTTP_PORT" "$ALPHA_WS_PORT" \
"$ALPHA_WS_MODULES"

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_JSON" "$BETA_ADDRESS_JSON_FILE_NAME" \
"$BETA_NODE_KEY" "snap" "$BETA_AUTHRPC_PORT" "$BETA_HTTP_PORT" "$BETA_WS_PORT" \
"$BETA_WS_MODULES"

# Miner
tmux new-window -t $SESSION:5 -n "miner" $SHELL
tmux send-keys -t $SESSION:5 "cd ${NODES_ROOT}/harness-ctl" C-m
tmux send-keys -t $SESSION:5 "watch -n 15 ./mine-alpha 1" C-m
# tmux send-keys -t $SESSION:5 "watch -n 15 ./mine-alpha 1" C-m

sleep 1

# 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')"

echo "Mining some blocks"
# NOTE: These first couple of blocks will cause a reorg on one node or the
# other. The reason is unknown. It seems this initial mining and reorg is
# necessary for nodes to start communicating.
"${NODES_ROOT}/harness-ctl/mine-beta" "2"
"${NODES_ROOT}/harness-ctl/mine-alpha" "2"

SEND_AMT=5000000000000000000000
echo "Sending 5000 eth to beta and testing."
"${NODES_ROOT}/harness-ctl/alpha" "attach --preload ${NODES_ROOT}/harness-ctl/send.js --exec send(\"${ALPHA_ADDRESS}\",\"${BETA_ADDRESS}\",${SEND_AMT})"
Expand All @@ -295,19 +155,7 @@ TEST_USDT_CONTRACT_HASH=$("${NODES_ROOT}/harness-ctl/alpha" "attach --preload ${
echo "Deploying MultiBalance contract."
MULTIBALANCE_CONTRACT_HASH=$("${NODES_ROOT}/harness-ctl/alpha" "attach --preload ${NODES_ROOT}/harness-ctl/deploy.js --exec deploy(\"${ALPHA_ADDRESS}\",\"${MULTIBALANCE_BIN}\")" | sed 's/"//g')

mine_pending_txs() {
while true
do
TXSLEN=$("${NODES_ROOT}/harness-ctl/alpha" "attach --exec eth.pendingTransactions.length")
if [ "$TXSLEN" -eq 0 ]; then
break
fi
echo "Waiting for transactions to be mined."
"${NODES_ROOT}/harness-ctl/mine-alpha" "5"
done
}

mine_pending_txs
# "${NODES_ROOT}/harness-ctl/mine-alpha" "1"

ETH_SWAP_CONTRACT_ADDR=$("${NODES_ROOT}/harness-ctl/alpha" "attach --preload ${NODES_ROOT}/harness-ctl/contractAddress.js --exec contractAddress(\"${ETH_SWAP_CONTRACT_HASH}\")" | sed 's/"//g')
echo "ETH SWAP contract address is ${ETH_SWAP_CONTRACT_ADDR}. Saving to ${NODES_ROOT}/eth_swap_contract_address.txt"
Expand All @@ -333,8 +181,6 @@ EOF
echo "Deploying ERC20SwapV0 contract for USDT."
USDT_SWAP_CONTRACT_HASH=$("${NODES_ROOT}/harness-ctl/alpha" "attach --preload ${NODES_ROOT}/harness-ctl/deploy.js --exec deployERC20Swap(\"${ALPHA_ADDRESS}\",\"${ERC20_SWAP_V0}\",\"${TEST_USDT_CONTRACT_ADDR}\")" | sed 's/"//g')

mine_pending_txs

cat > "${NODES_ROOT}/harness-ctl/loadTestToken.js" <<EOF
// This ABI comes from running 'solc --abi TestToken.sol'
const testTokenABI = [{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"airdrop","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"user","type":"address"},{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"testApprove","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]
Expand All @@ -347,8 +193,6 @@ cat > "${NODES_ROOT}/harness-ctl/loadTestToken.js" <<EOF
}
})
var contract = web3.eth.contract(testTokenABI)
web3.eth.defaultAccount = web3.eth.accounts.length > 1 ? web3.eth.accounts[1] : web3.eth.accounts[0]
personal.unlockAccount(web3.eth.defaultAccount, '${PASSWORD}')
function transfer (tokenAddr, decimals, addr, val) {
addr = addr.startsWith('0x') ? addr : '0x'+addr
Expand Down Expand Up @@ -395,7 +239,6 @@ EOF

cd "${NODES_ROOT}/harness-ctl"

mine_pending_txs

TEST_BLOCK10_HASH=$("${NODES_ROOT}/harness-ctl/alpha" "attach --exec eth.getHeaderByNumber(10).hash" | sed 's/"//g')
echo "ETH block 10 hash to use in tests is ${TEST_BLOCK10_HASH}. Saving to ${NODES_ROOT}/test_block10_hash.txt"
Expand Down

0 comments on commit c1bdb50

Please sign in to comment.