Skip to content

Commit

Permalink
cmd,eth: use struct for LivepeerEthClient constructor parameters that…
Browse files Browse the repository at this point in the history
… aren't other services
  • Loading branch information
kyriediculous committed Jun 18, 2021
1 parent 3c385c1 commit efa84b4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
15 changes: 13 additions & 2 deletions cmd/devtool/devtool.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,19 @@ func ethSetup(ethAcctAddr, keystoreDir string, isBroadcaster bool) {
}
glog.Infof("Using controller address %s", ethController)

client, err := eth.NewClient(ethcommon.HexToAddress(ethAcctAddr), keystoreDir, passphrase, backend,
ethcommon.HexToAddress(ethController), ethTxTimeout, nil)
gpm := eth.NewGasPriceMonitor(backend, 5*time.Second, big.NewInt(0))

clientCfg := eth.LivepeerEthClientConfig{
AccountAddr: ethcommon.HexToAddress(ethAcctAddr),
KeystoreDir: keystoreDir,
Password: passphrase,
ControllerAddr: ethcommon.HexToAddress(ethController),
TxTimeout: ethTxTimeout,
MaxGasPrice: nil,
ReplaceTransactions: false,
}

client, err := eth.NewClient(backend, gpm, clientCfg)
if err != nil {
glog.Errorf("Failed to create client: %v", err)
return
Expand Down
12 changes: 11 additions & 1 deletion cmd/livepeer/livepeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,17 @@ func main() {
}
defer gpm.Stop()

client, err := eth.NewClient(ethcommon.HexToAddress(*ethAcctAddr), keystoreDir, *ethPassword, backend, gpm, ethcommon.HexToAddress(*ethController), EthTxTimeout, bigMaxGasPrice)
ethCfg := eth.LivepeerEthClientConfig{
AccountAddr: ethcommon.HexToAddress(*ethAcctAddr),
KeystoreDir: keystoreDir,
Password: *ethPassword,
ControllerAddr: ethcommon.HexToAddress(*ethController),
TxTimeout: EthTxTimeout,
MaxGasPrice: bigMaxGasPrice,
ReplaceTransactions: *replaceTx,
}

client, err := eth.NewClient(backend, gpm, ethCfg)
if err != nil {
glog.Errorf("Failed to create Livepeer Ethereum client: %v", err)
return
Expand Down
31 changes: 18 additions & 13 deletions eth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ import (
"fmt"
"math/big"
"sort"
"strings"
"time"

ethereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
ethcommon "github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -113,7 +111,6 @@ type LivepeerEthClient interface {
// Helpers
ContractAddresses() map[string]ethcommon.Address
CheckTx(*types.Transaction) error
ReplaceTransaction(*types.Transaction, string, *big.Int) (*types.Transaction, error)
Sign([]byte) ([]byte, error)
SetGasInfo(uint64) error
}
Expand Down Expand Up @@ -148,34 +145,42 @@ type client struct {
txTimeout time.Duration
}

func NewClient(accountAddr ethcommon.Address, keystoreDir, password string, eth *ethclient.Client, gpm *GasPriceMonitor, controllerAddr ethcommon.Address, txTimeout time.Duration, maxGasPrice *big.Int) (LivepeerEthClient, error) {
type LivepeerEthClientConfig struct {
AccountAddr ethcommon.Address
KeystoreDir string
Password string
ControllerAddr ethcommon.Address
TxTimeout time.Duration
MaxGasPrice *big.Int
ReplaceTransactions bool
}

func NewClient(eth *ethclient.Client, gpm *GasPriceMonitor, cfg LivepeerEthClientConfig) (LivepeerEthClient, error) {
chainID, err := eth.ChainID(context.Background())
if err != nil {
return nil, err
}

signer := types.NewEIP155Signer(chainID)

backend, err := NewBackend(eth, signer, gpm)
am, err := NewAccountManager(cfg.AccountAddr, cfg.KeystoreDir, signer)
if err != nil {
return nil, err
}
backend.SetMaxGasPrice(maxGasPrice)

am, err := NewAccountManager(accountAddr, keystoreDir, signer)
if err != nil {
return nil, err
}
tm := NewTransactionManager(eth, gpm, am, cfg.TxTimeout, true)

backend := NewBackend(eth, signer, gpm, tm)

if err := am.Unlock(password); err != nil {
if err := am.Unlock(cfg.Password); err != nil {
return nil, err
}

return &client{
accountManager: am,
backend: backend,
controllerAddr: controllerAddr,
txTimeout: txTimeout,
controllerAddr: cfg.ControllerAddr,
txTimeout: cfg.TxTimeout,
}, nil
}

Expand Down

0 comments on commit efa84b4

Please sign in to comment.