66package eth
77
88import (
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() {
296317func (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.
342364func (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 )
0 commit comments