include terence fixes

Former-commit-id: b982c043b8f4c286e1200d9f40247ec6cb866c59 [formerly 34458578355ba9c0bc1dd82a4dc4c46344d74fc2]
Former-commit-id: 344225ee9af05acf42c3e20b95755a4bbd68f5e3
This commit is contained in:
Raul Jordan
2018-02-27 16:09:26 -06:00
8 changed files with 63 additions and 55 deletions

View File

@@ -122,6 +122,7 @@ var (
utils.GpoBlocksFlag,
utils.GpoPercentileFlag,
utils.ExtraDataFlag,
utils.JoinValidatorSetFlag,
configFileFlag,
}

View File

@@ -14,7 +14,7 @@ var (
Aliases: []string{"shard"},
Usage: "Start a sharding client",
ArgsUsage: "[endpoint]",
Flags: []cli.Flag{utils.DataDirFlag, utils.PasswordFileFlag, utils.NetworkIdFlag, utils.IPCPathFlag},
Flags: []cli.Flag{utils.DataDirFlag, utils.PasswordFileFlag, utils.NetworkIdFlag, utils.IPCPathFlag, utils.JoinValidatorSetFlag},
Category: "SHARDING COMMANDS",
Description: `
Launches a sharding client that connects to a running geth node and proposes collations to a Validator Manager Contract. This feature is a work in progress.

View File

@@ -537,6 +537,12 @@ var (
Usage: "Minimum POW accepted",
Value: whisper.DefaultMinimumPoW,
}
//Sharding Settings
JoinValidatorSetFlag = cli.BoolFlag{
Name: "validator",
Usage: "To become a validator with your sharding client, 100 ETH will be deposited from user's account into VMC ",
}
)
// MakeDataDir retrieves the currently requested data directory, terminating

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"
@@ -56,6 +57,7 @@ func MakeShardingClient(ctx *cli.Context) *Client {
config := &node.Config{
DataDir: path,
}
scryptN, scryptP, keydir, err := config.AccountConfig()
if err != nil {
panic(err) // TODO(prestonvanloon): handle this
@@ -81,6 +83,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
}
@@ -120,22 +132,28 @@ 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)
}
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,
@@ -151,17 +169,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 {
@@ -89,13 +89,6 @@ func TestCheckShardsForProposal(t *testing.T) {
ExpectedError string
CollatorClient FakeCollatorClient
}{
{
Name: "collatorClient.Account should return an error",
ExpectedError: "no account",
CollatorClient: FakeCollatorClient{
accountError: errors.New("no account"),
},
},
{
Name: "VMCCaller.GetEligibleProposer should return an error",
ExpectedError: "there is no cake",

File diff suppressed because one or more lines are too long

View File

@@ -6,6 +6,7 @@ import (
"math/big"
"time"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/sharding/contracts"
)
@@ -55,18 +56,23 @@ func initVMC(c *Client) error {
// the account is not in the set, it will deposit 100ETH into contract.
func joinValidatorSet(c *Client) error {
// TODO: Check if account is already in validator set. Fetch this From
// the VMC contract's validator set
txOps, err := c.createTXOps(depositSize)
if err != nil {
return fmt.Errorf("unable to intiate the deposit transaction: %v", err)
}
if c.ctx.GlobalBool(utils.JoinValidatorSetFlag.Name) {
log.Info("Joining validator set")
txOps, err := c.createTXOps(depositSize)
if err != nil {
return fmt.Errorf("unable to intiate the deposit transaction: %v", err)
}
tx, err := c.vmc.VMCTransactor.Deposit(txOps)
if err != nil {
return fmt.Errorf("unable to deposit eth and become a validator: %v", err)
}
log.Info(fmt.Sprintf("Deposited %dETH into contract with transaction hash: %s", depositSize, tx.Hash().String()))
} else {
log.Info("Not joining validator set")
tx, err := c.vmc.VMCTransactor.Deposit(txOps)
if err != nil {
return fmt.Errorf("unable to deposit eth and become a validator: %v", err)
}
log.Info(fmt.Sprintf("Deposited 100ETH into contract with transaction hash: %s", tx.Hash().String()))
return nil
}