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 25, 2024
1 parent 0030e49 commit bc9b4b3
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 410 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
}
8 changes: 4 additions & 4 deletions client/asset/eth/eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4927,11 +4927,11 @@ func testMaxSwapRedeemLots(t *testing.T, assetID uint32) {
t.Fatalf("expected 63 for max redemptions but got %d", info.MaxRedeemsInTx)
}
} else {
if info.MaxSwapsInTx != 20 {
t.Fatalf("expected 20 for max swaps but got %d", info.MaxSwapsInTx)
if info.MaxSwapsInTx != 24 {
t.Fatalf("expected 24 for max swaps but got %d", info.MaxSwapsInTx)
}
if info.MaxRedeemsInTx != 45 {
t.Fatalf("expected 45 for max redemptions but got %d", info.MaxRedeemsInTx)
if info.MaxRedeemsInTx != 55 {
t.Fatalf("expected 55 for max redemptions but got %d", info.MaxRedeemsInTx)
}
}
}
Expand Down
28 changes: 13 additions & 15 deletions client/asset/eth/nodeclient_harness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ var (
testnetParticipantWalletDir string

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

func rpcEndpoints(net dex.Network) ([]string, []string) {
func rpcEndpoints(net dex.Network) []string {
if net == dex.Testnet {
return rpcProviders, rpcProviders
return rpcProviders
}
return []string{alphaIPCFile}, []string{betaIPCFile}
return []string{alphaWSEndpoint}
}

func prepareTestRPCClients(initiatorDir, participantDir string, net dex.Network) (err error) {
initiatorEndpoints, participantEndpoints := rpcEndpoints(net)
endpoints := rpcEndpoints(net)

ethClient, simnetAcct, err = prepareRPCClient("initiator", initiatorDir, initiatorEndpoints, net)
ethClient, simnetAcct, err = prepareRPCClient("initiator", initiatorDir, endpoints, net)
if err != nil {
return err
}
fmt.Println("initiator address is", ethClient.address())

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

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

initiatorProviders, participantProviders := rpcEndpoints(dex.Simnet)
providers := rpcEndpoints(dex.Simnet)

err = setupWallet(simnetWalletDir, simnetWalletSeed, "localhost:30355", initiatorProviders, dex.Simnet)
err = setupWallet(simnetWalletDir, simnetWalletSeed, "localhost:30355", providers, dex.Simnet)
if err != nil {
return 1, err
}

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

initiatorRPC, participantRPC := rpcEndpoints(dex.Testnet)
rpc := rpcEndpoints(dex.Testnet)

err = setupWallet(testnetWalletDir, testnetWalletSeed, "localhost:30355", initiatorRPC, dex.Testnet)
err = setupWallet(testnetWalletDir, testnetWalletSeed, "localhost:30355", rpc, dex.Testnet)
if err != nil {
return 1, err
}
err = setupWallet(testnetParticipantWalletDir, testnetParticipantWalletSeed, "localhost:30356", participantRPC, dex.Testnet)
err = setupWallet(testnetParticipantWalletDir, testnetParticipantWalletSeed, "localhost:30356", rpc, dex.Testnet)
if err != nil {
return 1, err
}
Expand Down
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
4 changes: 2 additions & 2 deletions dex/networks/eth/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ const (
// These are the chain IDs of the various Ethereum network supported.
const (
MainnetChainID = 1
TestnetChainID = 5 // Görli
SimnetChainID = 42 // see dex/testing/eth/harness.sh
TestnetChainID = 5 // Görli
SimnetChainID = 1337 // see dex/testing/eth/harness.sh
)

var (
Expand Down
28 changes: 14 additions & 14 deletions dex/networks/eth/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,13 @@ var Tokens = map[uint32]*Token{
0: {
Address: common.Address{},
Gas: Gases{
Swap: 242_000,
SwapAdd: 146_400,
Redeem: 109_000,
RedeemAdd: 31_600,
Refund: 77_000,
Approve: 78_400,
Transfer: 85_100,
Swap: 203_317,
SwapAdd: 146_368,
Redeem: 90_775,
RedeemAdd: 41_117,
Refund: 65_448,
Approve: 32_303,
Transfer: 66_953,
}},
},
},
Expand Down Expand Up @@ -266,13 +266,13 @@ var Tokens = map[uint32]*Token{
0: {
Address: common.Address{},
Gas: Gases{
Swap: 242_000,
SwapAdd: 146_400,
Redeem: 109_000,
RedeemAdd: 31_600,
Refund: 77_000,
Approve: 78_400,
Transfer: 85_100,
Swap: 203_317,
SwapAdd: 146_368,
Redeem: 90_775,
RedeemAdd: 41_117,
Refund: 65_448,
Approve: 32_303,
Transfer: 66_953,
}},
},
},
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.12+ 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`.
Loading

0 comments on commit bc9b4b3

Please sign in to comment.