Skip to content

Commit c4692cf

Browse files
authored
add multi-rpc client for server
1 parent d448d68 commit c4692cf

7 files changed

Lines changed: 288 additions & 132 deletions

File tree

dex/testing/dcrdex/harness.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,23 @@ EOF
217217
fi
218218

219219
if [ $ETH_ON -eq 0 ]; then
220+
221+
ETH_CONFIG_PATH=${TEST_ROOT}/eth.conf
222+
ETH_IPC_FILE=${TEST_ROOT}/eth/alpha/node/geth.ipc
223+
cat << EOF >> $ETH_CONFIG_PATH
224+
ws://localhost:38557
225+
# comments are respected
226+
# http://localhost:38556
227+
${ETH_IPC_FILE}
228+
EOF
220229
cat << EOF >> "./markets.json"
221230
},
222231
"ETH_simnet": {
223232
"bip44symbol": "eth",
224233
"network": "simnet",
225234
"maxFeeRate": 200,
226235
"swapConf": 2,
227-
"configPath": "ws://localhost:38557"
236+
"configPath": "$ETH_CONFIG_PATH"
228237
},
229238
"DEXTT_simnet": {
230239
"bip44symbol": "dextt.eth",

server/asset/eth/config.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,10 @@
66
package eth
77

88
import (
9-
"path/filepath"
10-
119
"github.com/decred/dcrd/dcrutil/v4"
1210
)
1311

14-
var (
15-
ethHomeDir = dcrutil.AppDataDir("ethereum", false)
16-
defaultIPC = filepath.Join(ethHomeDir, "geth/geth.ipc")
17-
)
12+
var ethHomeDir = dcrutil.AppDataDir("ethereum", false)
1813

1914
// For tokens, the file at the config path can contain overrides for
2015
// token gas values. Gas used for token swaps is dependent on the token contract

server/asset/eth/eth.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
package eth
77

88
import (
9+
"bufio"
910
"bytes"
1011
"context"
1112
"crypto/sha256"
1213
"errors"
1314
"fmt"
1415
"math/big"
16+
"os"
1517
"strings"
1618
"sync"
1719
"time"
@@ -161,7 +163,7 @@ type ethFetcher interface {
161163
bestHeader(ctx context.Context) (*types.Header, error)
162164
blockNumber(ctx context.Context) (uint64, error)
163165
headerByHeight(ctx context.Context, height uint64) (*types.Header, error)
164-
connect(ctx context.Context, log dex.Logger) error
166+
connect(ctx context.Context) error
165167
shutdown()
166168
suggestGasTipCap(ctx context.Context) (*big.Int, error)
167169
syncProgress(ctx context.Context) (*ethereum.SyncProgress, error)
@@ -265,7 +267,7 @@ func unconnectedETH(logger dex.Logger, net dex.Network) (*ETHBackend, error) {
265267

266268
// NewBackend is the exported constructor by which the DEX will import the
267269
// Backend.
268-
func NewBackend(endpoint string, logger dex.Logger, net dex.Network) (*ETHBackend, error) {
270+
func NewBackend(configPath string, log dex.Logger, net dex.Network) (*ETHBackend, error) {
269271
switch net {
270272
case dex.Simnet:
271273
case dex.Testnet:
@@ -276,15 +278,34 @@ func NewBackend(endpoint string, logger dex.Logger, net dex.Network) (*ETHBacken
276278
return nil, fmt.Errorf("unknown network ID: %d", net)
277279
}
278280

279-
if endpoint == "" {
280-
endpoint = defaultIPC
281+
file, err := os.Open(configPath)
282+
if err != nil {
283+
return nil, err
284+
}
285+
defer file.Close()
286+
287+
var endpoints []string
288+
scanner := bufio.NewScanner(file)
289+
for scanner.Scan() {
290+
line := strings.Trim(scanner.Text(), " ")
291+
if line == "" || strings.HasPrefix(line, "#") {
292+
continue
293+
}
294+
endpoints = append(endpoints, line)
295+
}
296+
if err := scanner.Err(); err != nil {
297+
return nil, fmt.Errorf("error reading eth config file at %q. %v", configPath, err)
298+
}
299+
if len(endpoints) == 0 {
300+
return nil, fmt.Errorf("no endpoint found in the eth config file at %q", configPath)
281301
}
302+
log.Debugf("Parsed %d endpoints from the ETH config file", len(endpoints))
282303

283-
eth, err := unconnectedETH(logger, net)
304+
eth, err := unconnectedETH(log, net)
284305
if err != nil {
285306
return nil, err
286307
}
287-
eth.node = newRPCClient(eth.net, endpoint)
308+
eth.node = newRPCClient(eth.net, endpoints, log.SubLogger("RPC"))
288309
return eth, nil
289310
}
290311

@@ -296,7 +317,7 @@ func (eth *baseBackend) shutdown() {
296317
func (eth *ETHBackend) Connect(ctx context.Context) (*sync.WaitGroup, error) {
297318
eth.baseBackend.ctx = ctx
298319

299-
if err := eth.node.connect(ctx, eth.log); err != nil {
320+
if err := eth.node.connect(ctx); err != nil {
300321
return nil, err
301322
}
302323

@@ -338,7 +359,8 @@ func (eth *TokenBackend) Connect(ctx context.Context) (*sync.WaitGroup, error) {
338359
}
339360

340361
// TokenBackend creates an *AssetBackend for a token. Part of the
341-
// asset.TokenBacker interface.
362+
// asset.TokenBacker interface. Do not call TokenBackend concurrently for the
363+
// same asset.
342364
func (eth *ETHBackend) TokenBackend(assetID uint32, configPath string) (asset.Backend, error) {
343365
if _, found := eth.baseBackend.tokens[assetID]; found {
344366
return nil, fmt.Errorf("asset %d backend already loaded", assetID)

server/asset/eth/eth_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ type testNode struct {
111111
acctBalErr error
112112
}
113113

114-
func (n *testNode) connect(ctx context.Context, log dex.Logger) error {
114+
func (n *testNode) connect(ctx context.Context) error {
115115
return n.connectErr
116116
}
117117

0 commit comments

Comments
 (0)