mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 15:37:56 -05:00
include terence fixes
Former-commit-id: b982c043b8f4c286e1200d9f40247ec6cb866c59 [formerly 34458578355ba9c0bc1dd82a4dc4c46344d74fc2] Former-commit-id: 344225ee9af05acf42c3e20b95755a4bbd68f5e3
This commit is contained in:
@@ -122,6 +122,7 @@ var (
|
|||||||
utils.GpoBlocksFlag,
|
utils.GpoBlocksFlag,
|
||||||
utils.GpoPercentileFlag,
|
utils.GpoPercentileFlag,
|
||||||
utils.ExtraDataFlag,
|
utils.ExtraDataFlag,
|
||||||
|
utils.JoinValidatorSetFlag,
|
||||||
configFileFlag,
|
configFileFlag,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ var (
|
|||||||
Aliases: []string{"shard"},
|
Aliases: []string{"shard"},
|
||||||
Usage: "Start a sharding client",
|
Usage: "Start a sharding client",
|
||||||
ArgsUsage: "[endpoint]",
|
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",
|
Category: "SHARDING COMMANDS",
|
||||||
Description: `
|
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.
|
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.
|
||||||
|
|||||||
@@ -537,6 +537,12 @@ var (
|
|||||||
Usage: "Minimum POW accepted",
|
Usage: "Minimum POW accepted",
|
||||||
Value: whisper.DefaultMinimumPoW,
|
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
|
// MakeDataDir retrieves the currently requested data directory, terminating
|
||||||
|
|||||||
@@ -3,11 +3,12 @@ package sharding
|
|||||||
//go:generate abigen --sol contracts/validator_manager.sol --pkg contracts --out contracts/validator_manager.go
|
//go:generate abigen --sol contracts/validator_manager.sol --pkg contracts --out contracts/validator_manager.go
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
"strings"
|
"os"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum"
|
"github.com/ethereum/go-ethereum"
|
||||||
|
|
||||||
@@ -56,6 +57,7 @@ func MakeShardingClient(ctx *cli.Context) *Client {
|
|||||||
config := &node.Config{
|
config := &node.Config{
|
||||||
DataDir: path,
|
DataDir: path,
|
||||||
}
|
}
|
||||||
|
|
||||||
scryptN, scryptP, keydir, err := config.AccountConfig()
|
scryptN, scryptP, keydir, err := config.AccountConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err) // TODO(prestonvanloon): handle this
|
panic(err) // TODO(prestonvanloon): handle this
|
||||||
@@ -81,6 +83,16 @@ func (c *Client) Start() error {
|
|||||||
c.client = ethclient.NewClient(rpcClient)
|
c.client = ethclient.NewClient(rpcClient)
|
||||||
defer rpcClient.Close()
|
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 {
|
if err := initVMC(c); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -120,22 +132,28 @@ func (c *Client) unlockAccount(account accounts.Account) error {
|
|||||||
pass := ""
|
pass := ""
|
||||||
|
|
||||||
if c.ctx.GlobalIsSet(utils.PasswordFileFlag.Name) {
|
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 {
|
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.
|
scanner := bufio.NewScanner(file)
|
||||||
pass = strings.Trim(string(blob), "\n") // Some text files end in new line, remove with strings.Trim.
|
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)
|
return c.keystore.Unlock(account, pass)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) createTXOps(value *big.Int) (*bind.TransactOpts, error) {
|
func (c *Client) createTXOps(value *big.Int) (*bind.TransactOpts, error) {
|
||||||
account, err := c.Account()
|
account := c.Account()
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &bind.TransactOpts{
|
return &bind.TransactOpts{
|
||||||
From: account.Address,
|
From: account.Address,
|
||||||
@@ -151,17 +169,10 @@ func (c *Client) createTXOps(value *big.Int) (*bind.TransactOpts, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Account to use for sharding transactions.
|
// Account to use for sharding transactions.
|
||||||
func (c *Client) Account() (*accounts.Account, error) {
|
func (c *Client) Account() *accounts.Account {
|
||||||
accounts := c.keystore.Accounts()
|
accounts := c.keystore.Accounts()
|
||||||
if len(accounts) == 0 {
|
|
||||||
return nil, fmt.Errorf("no accounts found")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := c.unlockAccount(accounts[0]); err != nil {
|
return &accounts[0]
|
||||||
return nil, fmt.Errorf("cannot unlock account. %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &accounts[0], nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChainReader for interacting with the chain.
|
// ChainReader for interacting with the chain.
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type collatorClient interface {
|
type collatorClient interface {
|
||||||
Account() (*accounts.Account, error)
|
Account() *accounts.Account
|
||||||
ChainReader() ethereum.ChainReader
|
ChainReader() ethereum.ChainReader
|
||||||
VMCCaller() *contracts.VMCCaller
|
VMCCaller() *contracts.VMCCaller
|
||||||
}
|
}
|
||||||
@@ -27,12 +27,9 @@ type collatorClient interface {
|
|||||||
func subscribeBlockHeaders(c collatorClient) error {
|
func subscribeBlockHeaders(c collatorClient) error {
|
||||||
headerChan := make(chan *types.Header, 16)
|
headerChan := make(chan *types.Header, 16)
|
||||||
|
|
||||||
account, err := c.Account()
|
account := c.Account()
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = c.ChainReader().SubscribeNewHead(context.Background(), headerChan)
|
_, err := c.ChainReader().SubscribeNewHead(context.Background(), headerChan)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to subscribe to incoming headers. %v", err)
|
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
|
// getEligibleProposer from the VMC and proposes a collation if
|
||||||
// conditions are met
|
// conditions are met
|
||||||
func checkShardsForProposal(c collatorClient, head *types.Header) error {
|
func checkShardsForProposal(c collatorClient, head *types.Header) error {
|
||||||
account, err := c.Account()
|
account := c.Account()
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Info("Checking if we are an eligible collation proposer for a shard...")
|
log.Info("Checking if we are an eligible collation proposer for a shard...")
|
||||||
period := big.NewInt(0).Div(head.Number, big.NewInt(periodLength))
|
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 function calls IsValidatorDeposited from the VMC and returns true if
|
||||||
// the client is in the validator pool
|
// the client is in the validator pool
|
||||||
func isAccountInValidatorSet(c collatorClient) (bool, error) {
|
func isAccountInValidatorSet(c collatorClient) (bool, error) {
|
||||||
account, err := c.Account()
|
account := c.Account()
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Checks if our deposit has gone through according to the VMC
|
// Checks if our deposit has gone through according to the VMC
|
||||||
b, err := c.VMCCaller().IsValidatorDeposited(&bind.CallOpts{}, account.Address)
|
b, err := c.VMCCaller().IsValidatorDeposited(&bind.CallOpts{}, account.Address)
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ type FakeCollatorClient struct {
|
|||||||
contractCaller FakeContractCaller
|
contractCaller FakeContractCaller
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c FakeCollatorClient) Account() (*accounts.Account, error) {
|
func (c FakeCollatorClient) Account() *accounts.Account {
|
||||||
return c.accountAccount, c.accountError
|
return c.accountAccount
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c FakeCollatorClient) ChainReader() ethereum.ChainReader {
|
func (c FakeCollatorClient) ChainReader() ethereum.ChainReader {
|
||||||
@@ -89,13 +89,6 @@ func TestCheckShardsForProposal(t *testing.T) {
|
|||||||
ExpectedError string
|
ExpectedError string
|
||||||
CollatorClient FakeCollatorClient
|
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",
|
Name: "VMCCaller.GetEligibleProposer should return an error",
|
||||||
ExpectedError: "there is no cake",
|
ExpectedError: "there is no cake",
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@@ -6,6 +6,7 @@ import (
|
|||||||
"math/big"
|
"math/big"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/cmd/utils"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/ethereum/go-ethereum/sharding/contracts"
|
"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.
|
// the account is not in the set, it will deposit 100ETH into contract.
|
||||||
func joinValidatorSet(c *Client) error {
|
func joinValidatorSet(c *Client) error {
|
||||||
|
|
||||||
// TODO: Check if account is already in validator set. Fetch this From
|
if c.ctx.GlobalBool(utils.JoinValidatorSetFlag.Name) {
|
||||||
// the VMC contract's validator set
|
|
||||||
txOps, err := c.createTXOps(depositSize)
|
log.Info("Joining validator set")
|
||||||
if err != nil {
|
txOps, err := c.createTXOps(depositSize)
|
||||||
return fmt.Errorf("unable to intiate the deposit transaction: %v", err)
|
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
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user