Merge pull request #46 from terenc3t/refactorAccounts

refactor account interface, and unlock account when client is created

Former-commit-id: 8169f4fcd5d78b410282b90acc9158affe13d02e [formerly 804c32d0ffad9b25c384be8a695bdc1e5889082d]
Former-commit-id: 3803facfa9b846a6c7329e15e628ec35e28be48f
This commit is contained in:
Raul Jordan
2018-02-27 14:57:10 -06:00
committed by GitHub
3 changed files with 20 additions and 29 deletions

View File

@@ -82,6 +82,16 @@ func (c *Client) Start() error {
c.client = ethclient.NewClient(rpcClient)
defer rpcClient.Close()
// Check account existence and unlock account before starting sharding client
accounts := c.keystore.Accounts()
if len(accounts) == 0 {
return fmt.Errorf("no accounts found")
}
if err := c.unlockAccount(accounts[0]); err != nil {
return fmt.Errorf("cannot unlock account. %v", err)
}
if err := initVMC(c); err != nil {
return err
}
@@ -142,10 +152,7 @@ func (c *Client) unlockAccount(account accounts.Account) error {
}
func (c *Client) createTXOps(value *big.Int) (*bind.TransactOpts, error) {
account, err := c.Account()
if err != nil {
return nil, err
}
account := c.Account()
return &bind.TransactOpts{
From: account.Address,
@@ -161,17 +168,10 @@ func (c *Client) createTXOps(value *big.Int) (*bind.TransactOpts, error) {
}
// Account to use for sharding transactions.
func (c *Client) Account() (*accounts.Account, error) {
func (c *Client) Account() *accounts.Account {
accounts := c.keystore.Accounts()
if len(accounts) == 0 {
return nil, fmt.Errorf("no accounts found")
}
if err := c.unlockAccount(accounts[0]); err != nil {
return nil, fmt.Errorf("cannot unlock account. %v", err)
}
return &accounts[0], nil
return &accounts[0]
}
// ChainReader for interacting with the chain.

View File

@@ -14,7 +14,7 @@ import (
)
type collatorClient interface {
Account() (*accounts.Account, error)
Account() *accounts.Account
ChainReader() ethereum.ChainReader
VMCCaller() *contracts.VMCCaller
}
@@ -27,12 +27,9 @@ type collatorClient interface {
func subscribeBlockHeaders(c collatorClient) error {
headerChan := make(chan *types.Header, 16)
account, err := c.Account()
if err != nil {
return err
}
account := c.Account()
_, err = c.ChainReader().SubscribeNewHead(context.Background(), headerChan)
_, err := c.ChainReader().SubscribeNewHead(context.Background(), headerChan)
if err != nil {
return fmt.Errorf("unable to subscribe to incoming headers. %v", err)
}
@@ -68,10 +65,7 @@ func subscribeBlockHeaders(c collatorClient) error {
// getEligibleProposer from the VMC and proposes a collation if
// conditions are met
func checkShardsForProposal(c collatorClient, head *types.Header) error {
account, err := c.Account()
if err != nil {
return err
}
account := c.Account()
log.Info("Checking if we are an eligible collation proposer for a shard...")
period := big.NewInt(0).Div(head.Number, big.NewInt(periodLength))
@@ -103,10 +97,7 @@ func checkShardsForProposal(c collatorClient, head *types.Header) error {
// The function calls IsValidatorDeposited from the VMC and returns true if
// the client is in the validator pool
func isAccountInValidatorSet(c collatorClient) (bool, error) {
account, err := c.Account()
if err != nil {
return false, err
}
account := c.Account()
// Checks if our deposit has gone through according to the VMC
b, err := c.VMCCaller().IsValidatorDeposited(&bind.CallOpts{}, account.Address)

View File

@@ -23,8 +23,8 @@ type FakeCollatorClient struct {
contractCaller FakeContractCaller
}
func (c FakeCollatorClient) Account() (*accounts.Account, error) {
return c.accountAccount, c.accountError
func (c FakeCollatorClient) Account() *accounts.Account {
return c.accountAccount
}
func (c FakeCollatorClient) ChainReader() ethereum.ChainReader {