Skip to content

Commit d916d00

Browse files
committed
harness: Update eth to work with geth 1.14 and up.
1 parent bff23de commit d916d00

12 files changed

Lines changed: 113 additions & 425 deletions

File tree

client/asset/eth/chaincfg.go

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"strings"
1212

1313
"decred.org/dcrdex/dex"
14-
dexeth "decred.org/dcrdex/dex/networks/eth"
1514
"github.com/ethereum/go-ethereum/common"
1615
ethcore "github.com/ethereum/go-ethereum/core"
1716
"github.com/ethereum/go-ethereum/eth/ethconfig"
@@ -65,7 +64,7 @@ func NetworkCompatibilityData(net dex.Network) (c CompatibilityData, err error)
6564
addr := common.HexToAddress("18d65fb8d60c1199bb1ad381be47aa692b482605")
6665
var (
6766
tTxHashFile = filepath.Join(tDir, "test_tx_hash.txt")
68-
tBlockHashFile = filepath.Join(tDir, "test_block10_hash.txt")
67+
tBlockHashFile = filepath.Join(tDir, "test_block1_hash.txt")
6968
tContractFile = filepath.Join(tDir, "test_usdc_contract_address.txt")
7069
)
7170
readIt := func(path string) string {
@@ -103,10 +102,8 @@ func ETHConfig(net dex.Network) (c ethconfig.Config, err error) {
103102
case dex.Testnet:
104103
c.Genesis = ethcore.DefaultSepoliaGenesisBlock()
105104
case dex.Simnet:
106-
c.Genesis, err = readSimnetGenesisFile()
107-
if err != nil {
108-
return c, fmt.Errorf("readSimnetGenesisFile error: %w", err)
109-
}
105+
// Args are gasLimit, faucet address.
106+
c.Genesis = ethcore.DeveloperGenesisBlock(30000000, nil)
110107
default:
111108
return c, fmt.Errorf("unknown network %d", net)
112109

@@ -123,19 +120,3 @@ func ChainConfig(net dex.Network) (c *params.ChainConfig, err error) {
123120
}
124121
return cfg.Genesis.Config, nil
125122
}
126-
127-
// readSimnetGenesisFile reads the simnet genesis file.
128-
func readSimnetGenesisFile() (*ethcore.Genesis, error) {
129-
dataDir, err := simnetDataDir()
130-
if err != nil {
131-
return nil, err
132-
}
133-
134-
genesisFile := filepath.Join(dataDir, "genesis.json")
135-
genesisCfg, err := dexeth.LoadGenesisFile(genesisFile)
136-
if err != nil {
137-
return nil, fmt.Errorf("error reading genesis file: %v", err)
138-
}
139-
140-
return genesisCfg, nil
141-
}

client/asset/eth/eth_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4927,11 +4927,11 @@ func testMaxSwapRedeemLots(t *testing.T, assetID uint32) {
49274927
t.Fatalf("expected 63 for max redemptions but got %d", info.MaxRedeemsInTx)
49284928
}
49294929
} else {
4930-
if info.MaxSwapsInTx != 20 {
4931-
t.Fatalf("expected 20 for max swaps but got %d", info.MaxSwapsInTx)
4930+
if info.MaxSwapsInTx != 24 {
4931+
t.Fatalf("expected 24 for max swaps but got %d", info.MaxSwapsInTx)
49324932
}
4933-
if info.MaxRedeemsInTx != 45 {
4934-
t.Fatalf("expected 45 for max redemptions but got %d", info.MaxRedeemsInTx)
4933+
if info.MaxRedeemsInTx != 55 {
4934+
t.Fatalf("expected 55 for max redemptions but got %d", info.MaxRedeemsInTx)
49354935
}
49364936
}
49374937
}

client/asset/eth/nodeclient_harness_test.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ var (
7272
testnetParticipantWalletDir string
7373

7474
alphaNodeDir = filepath.Join(homeDir, "dextest", "eth", "alpha", "node")
75-
alphaIPCFile = filepath.Join(alphaNodeDir, "geth.ipc")
76-
betaNodeDir = filepath.Join(homeDir, "dextest", "eth", "beta", "node")
77-
betaIPCFile = filepath.Join(betaNodeDir, "geth.ipc")
75+
alphaWSEndpoint = "ws://127.0.0.1:38557"
7876
ctx context.Context
7977
tLogger = dex.StdOutLogger("ETHTEST", dex.LevelWarn)
8078
simnetWalletSeed = "0812f5244004217452059e2fd11603a511b5d0870ead753df76c966ce3c71531"
@@ -208,23 +206,23 @@ func prepareRPCClient(name, dataDir string, providers []string, net dex.Network)
208206
return c, c.creds.acct, nil
209207
}
210208

211-
func rpcEndpoints(net dex.Network) ([]string, []string) {
209+
func rpcEndpoints(net dex.Network) []string {
212210
if net == dex.Testnet {
213-
return rpcProviders, rpcProviders
211+
return rpcProviders
214212
}
215-
return []string{alphaIPCFile}, []string{betaIPCFile}
213+
return []string{alphaWSEndpoint}
216214
}
217215

218216
func prepareTestRPCClients(initiatorDir, participantDir string, net dex.Network) (err error) {
219-
initiatorEndpoints, participantEndpoints := rpcEndpoints(net)
217+
endpoints := rpcEndpoints(net)
220218

221-
ethClient, simnetAcct, err = prepareRPCClient("initiator", initiatorDir, initiatorEndpoints, net)
219+
ethClient, simnetAcct, err = prepareRPCClient("initiator", initiatorDir, endpoints, net)
222220
if err != nil {
223221
return err
224222
}
225223
fmt.Println("initiator address is", ethClient.address())
226224

227-
participantEthClient, participantAcct, err = prepareRPCClient("participant", participantDir, participantEndpoints, net)
225+
participantEthClient, participantAcct, err = prepareRPCClient("participant", participantDir, endpoints, net)
228226
if err != nil {
229227
ethClient.shutdown()
230228
return err
@@ -258,14 +256,14 @@ func runSimnet(m *testing.M) (int, error) {
258256

259257
ethSwapContractAddr = dexeth.ContractAddresses[contractVer][dex.Simnet]
260258

261-
initiatorProviders, participantProviders := rpcEndpoints(dex.Simnet)
259+
providers := rpcEndpoints(dex.Simnet)
262260

263-
err = setupWallet(simnetWalletDir, simnetWalletSeed, "localhost:30355", initiatorProviders, dex.Simnet)
261+
err = setupWallet(simnetWalletDir, simnetWalletSeed, "localhost:30355", providers, dex.Simnet)
264262
if err != nil {
265263
return 1, err
266264
}
267265

268-
err = setupWallet(participantWalletDir, participantWalletSeed, "localhost:30356", participantProviders, dex.Simnet)
266+
err = setupWallet(participantWalletDir, participantWalletSeed, "localhost:30356", providers, dex.Simnet)
269267
if err != nil {
270268
return 1, err
271269
}
@@ -390,13 +388,13 @@ func runTestnet(m *testing.M) (int, error) {
390388
ethSwapContractAddr = dexeth.ContractAddresses[contractVer][dex.Testnet]
391389
fmt.Printf("ETH swap contract address is %v\n", ethSwapContractAddr)
392390

393-
initiatorRPC, participantRPC := rpcEndpoints(dex.Testnet)
391+
rpc := rpcEndpoints(dex.Testnet)
394392

395-
err = setupWallet(testnetWalletDir, testnetWalletSeed, "localhost:30355", initiatorRPC, dex.Testnet)
393+
err = setupWallet(testnetWalletDir, testnetWalletSeed, "localhost:30355", rpc, dex.Testnet)
396394
if err != nil {
397395
return 1, err
398396
}
399-
err = setupWallet(testnetParticipantWalletDir, testnetParticipantWalletSeed, "localhost:30356", participantRPC, dex.Testnet)
397+
err = setupWallet(testnetParticipantWalletDir, testnetParticipantWalletSeed, "localhost:30356", rpc, dex.Testnet)
400398
if err != nil {
401399
return 1, err
402400
}

dex/networks/eth/params.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ const (
3232
// These are the chain IDs of the various Ethereum network supported.
3333
const (
3434
MainnetChainID = 1
35-
TestnetChainID = 5 // Görli
36-
SimnetChainID = 42 // see dex/testing/eth/harness.sh
35+
TestnetChainID = 11155111 // Sepolia
36+
SimnetChainID = 1337 // see dex/testing/eth/harness.sh
3737
)
3838

3939
var (

dex/networks/eth/tokens.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,13 @@ var Tokens = map[uint32]*Token{
168168
0: {
169169
Address: common.Address{},
170170
Gas: Gases{
171-
Swap: 242_000,
172-
SwapAdd: 146_400,
173-
Redeem: 109_000,
174-
RedeemAdd: 31_600,
175-
Refund: 77_000,
176-
Approve: 78_400,
177-
Transfer: 85_100,
171+
Swap: 203_317,
172+
SwapAdd: 146_368,
173+
Redeem: 90_775,
174+
RedeemAdd: 41_117,
175+
Refund: 65_448,
176+
Approve: 32_303,
177+
Transfer: 66_953,
178178
}},
179179
},
180180
},
@@ -266,13 +266,13 @@ var Tokens = map[uint32]*Token{
266266
0: {
267267
Address: common.Address{},
268268
Gas: Gases{
269-
Swap: 242_000,
270-
SwapAdd: 146_400,
271-
Redeem: 109_000,
272-
RedeemAdd: 31_600,
273-
Refund: 77_000,
274-
Approve: 78_400,
275-
Transfer: 85_100,
269+
Swap: 203_317,
270+
SwapAdd: 146_368,
271+
Redeem: 90_775,
272+
RedeemAdd: 41_117,
273+
Refund: 65_448,
274+
Approve: 32_303,
275+
Transfer: 66_953,
276276
}},
277277
},
278278
},

dex/testing/eth/README.md

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,32 @@ sandboxed environment for testing dex swap transactions.
66
## Dependencies
77

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

1111
It also requires tmux and bc.
1212

1313
## Using
1414

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

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

2321
## Harness control scripts
2422

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

29-
`./alpha`, `./beta`, `./gamma`, and `./delta` are just `geth` configured for
30-
their respective data directories.
27+
`./alpha` is just `geth` configured for its data directory.
3128

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

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

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

3936
## Dev Stuff
4037

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

4845
If you encouter a problem, the harness can be killed from another terminal with
49-
`tmux kill-session -t eth-harness`.
46+
`tmux kill-session -t eth-harness`. Nodes can be killed with `sudo pkill -9 geth`.

dex/testing/eth/create-node.sh

Lines changed: 9 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,14 @@ set -ex
55
# The following are required script arguments.
66
TMUX_WIN_ID=$1
77
NAME=$2
8-
NODE_PORT=$3
9-
CHAIN_ADDRESS=$4
10-
CHAIN_PASSWORD=$5
11-
CHAIN_ADDRESS_JSON=$6
12-
CHAIN_ADDRESS_JSON_FILE_NAME=$7
13-
ADDRESS_JSON=$8
14-
ADDRESS_JSON_FILE_NAME=$9
15-
NODE_KEY=${10}
16-
SYNC_MODE=${11}
17-
AUTHRPC_PORT=${12}
18-
HTTP_PORT=${13}
19-
WS_PORT=${14}
20-
WS_MODULES=${15}
8+
AUTHRPC_PORT=${3}
9+
HTTP_PORT=${4}
10+
WS_PORT=${5}
11+
WS_MODULES=${6}
2112

2213
GROUP_DIR="${NODES_ROOT}/${NAME}"
23-
MINE_JS="${GROUP_DIR}/mine.js"
2414
NODE_DIR="${GROUP_DIR}/node"
2515
mkdir -p "${NODE_DIR}"
26-
mkdir -p "${NODE_DIR}/keystore"
2716
mkdir -p "${NODE_DIR}/geth"
2817

2918
# Write node ctl script.
@@ -33,114 +22,20 @@ geth --datadir="${NODE_DIR}" \$*
3322
EOF
3423
chmod +x "${NODES_ROOT}/harness-ctl/${NAME}"
3524

36-
# Write mine script if CHAIN_ADDRESS is present.
37-
if [ "${CHAIN_ADDRESS}" != "_" ]; then
38-
# The mining script may end up mining more or less blocks than specified.
39-
cat > "${NODES_ROOT}/harness-ctl/mine-${NAME}" <<EOF
40-
#!/usr/bin/env bash
41-
NUM=2
42-
case \$1 in
43-
''|*[!0-9]*|[0-1]) ;;
44-
*) NUM=\$1 ;;
45-
esac
46-
echo "Mining..."
47-
BEFORE=\$("${NODES_ROOT}/harness-ctl/${NAME}" attach --exec 'eth.blockNumber')
48-
"${NODES_ROOT}/harness-ctl/${NAME}" attach --exec 'miner.start()' > /dev/null
49-
sleep \$(echo "\$NUM-1.8" | bc)
50-
"${NODES_ROOT}/harness-ctl/${NAME}" attach --exec 'miner.stop()' > /dev/null
51-
sleep 1
52-
AFTER=\$("${NODES_ROOT}/harness-ctl/${NAME}" attach --exec 'eth.blockNumber')
53-
DIFF=\$((AFTER-BEFORE))
54-
echo "Mined \$DIFF blocks on ${NAME}. Their hashes:"
55-
for i in \$(seq \$((BEFORE+1)) \$AFTER)
56-
do
57-
echo \$i
58-
"${NODES_ROOT}/harness-ctl/${NAME}" attach --exec 'eth.getHeaderByNumber('\$i').hash'
59-
done
60-
EOF
61-
chmod +x "${NODES_ROOT}/harness-ctl/mine-${NAME}"
62-
63-
# Write password file to unlock accounts later.
64-
cat > "${GROUP_DIR}/password" <<EOF
65-
$CHAIN_PASSWORD
66-
EOF
67-
68-
fi
69-
7025
cat > "${NODE_DIR}/eth.conf" <<EOF
71-
[Eth]
72-
NetworkId = 42
73-
SyncMode = "${SYNC_MODE}"
74-
7526
[Node]
7627
DataDir = "${NODE_DIR}"
7728
AuthPort = ${AUTHRPC_PORT}
78-
79-
[Node.P2P]
80-
NoDiscovery = true
81-
BootstrapNodes = []
82-
BootstrapNodesV5 = []
83-
ListenAddr = ":${NODE_PORT}"
84-
NetRestrict = [ "127.0.0.1/8", "::1/128" ]
8529
EOF
8630

87-
# Add etherbase if mining.
88-
if [ "${CHAIN_ADDRESS}" != "_" ]; then
89-
cat >> "${NODE_DIR}/eth.conf" <<EOF
90-
91-
[Eth.Miner]
92-
Etherbase = "0x${CHAIN_ADDRESS}"
93-
GasFloor = 30000000
94-
GasCeil = 30000000
95-
EOF
96-
fi
97-
9831
# Create a tmux window.
9932
tmux new-window -t "$TMUX_WIN_ID" -n "${NAME}" "${SHELL}"
10033
tmux send-keys -t "$TMUX_WIN_ID" "set +o history" C-m
10134
tmux send-keys -t "$TMUX_WIN_ID" "cd ${NODE_DIR}" C-m
10235

103-
# Create and wait for a node initiated with a predefined genesis json.
104-
echo "Creating simnet ${NAME} node"
105-
tmux send-keys -t "$TMUX_WIN_ID" "${NODES_ROOT}/harness-ctl/${NAME} init "\
106-
"$GENESIS_JSON_FILE_LOCATION; tmux wait-for -S ${NAME}" C-m
107-
tmux wait-for "${NAME}"
108-
109-
# Create two accounts. The first is used to mine blocks. The second contains
110-
# funds.
111-
if [ "${CHAIN_ADDRESS}" != "_" ]; then
112-
echo "Creating account"
113-
cat > "${NODE_DIR}/keystore/$CHAIN_ADDRESS_JSON_FILE_NAME" <<EOF
114-
$CHAIN_ADDRESS_JSON
115-
EOF
116-
fi
117-
118-
cat > "${NODE_DIR}/keystore/$ADDRESS_JSON_FILE_NAME" <<EOF
119-
$ADDRESS_JSON
120-
EOF
121-
122-
# The node key lets us control the enode address value.
123-
echo "Setting node key"
124-
cat > "${NODE_DIR}/geth/nodekey" <<EOF
125-
$NODE_KEY
126-
EOF
127-
12836
echo "Starting simnet ${NAME} node"
129-
if [ "${SYNC_MODE}" = "snap" ]; then
130-
# Start the eth node with the chain account unlocked, listening restricted to
131-
# localhost, and our custom configuration file.
132-
tmux send-keys -t "$TMUX_WIN_ID" "${NODES_ROOT}/harness-ctl/${NAME} " \
133-
"--config ${NODE_DIR}/eth.conf --unlock ${CHAIN_ADDRESS} " \
134-
"--password ${GROUP_DIR}/password --light.serve 25 --datadir.ancient " \
135-
"${NODE_DIR}/geth-ancient --verbosity 5 --vmdebug --http --http.port " \
136-
"${HTTP_PORT} --ws --ws.port ${WS_PORT} --ws.api " \
137-
"${WS_MODULES} --allow-insecure-unlock --rpc.enabledeprecatedpersonal " \
138-
"2>&1 | tee ${NODE_DIR}/${NAME}.log" C-m
139-
140-
else
141-
# Start the eth node listening restricted to localhost and our custom
142-
# configuration file.
143-
tmux send-keys -t "$TMUX_WIN_ID" "${NODES_ROOT}/harness-ctl/${NAME} --allow-insecure-unlock --rpc.enabledeprecatedpersonal " \
144-
"--config ${NODE_DIR}/eth.conf --verbosity 5 ${HTTP_OPT} 2>&1 | tee " \
145-
"${NODE_DIR}/${NAME}.log" C-m
146-
fi
37+
# Start the eth node with our custom configuration file.
38+
tmux send-keys -t "$TMUX_WIN_ID" "${NODES_ROOT}/harness-ctl/${NAME} " \
39+
"--config ${NODE_DIR}/eth.conf --verbosity 5 --vmdebug --http --http.port " \
40+
"${HTTP_PORT} --ws --ws.port ${WS_PORT} --ws.api ${WS_MODULES} " \
41+
"--dev --dev.period 10 2>&1 | tee ${NODE_DIR}/${NAME}.log" C-m

0 commit comments

Comments
 (0)