Merge pull request #45 from nisdas/bufioScanner

Change from ioutil to bufio scanner

Former-commit-id: 2a2148a6bee555f6629fd4cf306e208a556e92d9 [formerly ac00ca1d65a3f7eebc8ebbc52a50f208603d34c6]
Former-commit-id: ab36fb37941e0c2df9a1575db7cc71881a22a8d0
This commit is contained in:
nisdas 2018-02-28 03:03:38 +08:00 committed by GitHub
commit 8350dfa45c
2 changed files with 54 additions and 9 deletions

View File

@ -3,11 +3,12 @@ package sharding
//go:generate abigen --sol contracts/validator_manager.sol --pkg contracts --out contracts/validator_manager.go
import (
"bufio"
"context"
"errors"
"fmt"
"io/ioutil"
"math/big"
"strings"
"os"
"github.com/ethereum/go-ethereum"
@ -120,12 +121,21 @@ func (c *Client) unlockAccount(account accounts.Account) error {
pass := ""
if c.ctx.GlobalIsSet(utils.PasswordFileFlag.Name) {
blob, err := ioutil.ReadFile(c.ctx.GlobalString(utils.PasswordFileFlag.Name))
file, err := os.Open(c.ctx.GlobalString(utils.PasswordFileFlag.Name))
if err != nil {
return fmt.Errorf("unable to read account password contents in file %s. %v", utils.PasswordFileFlag.Value, err)
return fmt.Errorf("unable to open file containing account password %s. %v", utils.PasswordFileFlag.Value, err)
}
// TODO: Use bufio.Scanner or other reader that doesn't include a trailing newline character.
pass = strings.Trim(string(blob), "\n") // Some text files end in new line, remove with strings.Trim.
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanWords)
if !scanner.Scan() {
err = scanner.Err()
if err != nil {
return fmt.Errorf("unable to read contents of file %v", err)
}
return errors.New("password not found in file")
}
pass = scanner.Text()
}
return c.keystore.Unlock(account, pass)

View File

@ -27,7 +27,12 @@ type collatorClient interface {
func subscribeBlockHeaders(c collatorClient) error {
headerChan := make(chan *types.Header, 16)
_, err := c.ChainReader().SubscribeNewHead(context.Background(), headerChan)
account, err := c.Account()
if err != nil {
return err
}
_, err = c.ChainReader().SubscribeNewHead(context.Background(), headerChan)
if err != nil {
return fmt.Errorf("unable to subscribe to incoming headers. %v", err)
}
@ -40,8 +45,19 @@ func subscribeBlockHeaders(c collatorClient) error {
// Query the current state to see if we are an eligible proposer
log.Info(fmt.Sprintf("Received new header: %v", head.Number.String()))
// TODO: Only run this code on certain periods?
if err := checkShardsForProposal(c, head); err != nil {
return fmt.Errorf("unable to watch shards. %v", err)
// Check if we are in the validator pool before checking if we are an eligible proposer
v, err := isAccountInValidatorSet(c)
if err != nil {
return fmt.Errorf("unable to verify client in validator pool. %v", err)
}
if v {
if err := checkShardsForProposal(c, head); err != nil {
return fmt.Errorf("unable to watch shards. %v", err)
}
} else {
log.Warn(fmt.Sprintf("Account %s not in validator pool.", account.Address.String()))
}
}
@ -82,6 +98,25 @@ func checkShardsForProposal(c collatorClient, head *types.Header) error {
return nil
}
// isAccountInValidatorSet checks if the client is in the validator pool because
// we can't guarantee our tx for deposit will be in the next block header we receive.
// 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
}
// Checks if our deposit has gone through according to the VMC
b, err := c.VMCCaller().IsValidatorDeposited(&bind.CallOpts{}, account.Address)
if err != nil {
return false, err
}
return b, nil
}
// proposeCollation interacts with the VMC directly to add a collation header
func proposeCollation(shardID int64) error {
// TODO: Adds a collation header to the VMC with the following fields: