mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-31 23:41:22 +00:00
864405a823
Former-commit-id: 43aee542df91cb2bfa293a4583f5a1b12b5c8790 [formerly 06d9ebb22185133b1abca17543e9f4922e62f4b0] Former-commit-id: e0cceae6369a7be1a7837df362ff2d748d8cadd5
108 lines
2.9 KiB
Go
108 lines
2.9 KiB
Go
package sharding
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"strings"
|
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
|
"github.com/ethereum/go-ethereum/cmd/utils"
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
|
"github.com/ethereum/go-ethereum/log"
|
|
"github.com/ethereum/go-ethereum/node"
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
"github.com/ethereum/go-ethereum/sharding/contracts"
|
|
cli "gopkg.in/urfave/cli.v1"
|
|
)
|
|
|
|
const (
|
|
clientIdentifier = "geth" // Used to determine the ipc name.
|
|
)
|
|
|
|
// Client for sharding. Communicates to geth node via JSON RPC.
|
|
type Client struct {
|
|
endpoint string // Endpoint to JSON RPC
|
|
client *ethclient.Client // Ethereum RPC client.
|
|
keystore *keystore.KeyStore // Keystore containing the single signer
|
|
ctx *cli.Context // Command line context
|
|
vmc *contracts.VMC // The deployed validator management contract
|
|
}
|
|
|
|
// MakeShardingClient for interfacing with geth full node.
|
|
func MakeShardingClient(ctx *cli.Context) *Client {
|
|
path := node.DefaultDataDir()
|
|
if ctx.GlobalIsSet(utils.DataDirFlag.Name) {
|
|
path = ctx.GlobalString(utils.DataDirFlag.Name)
|
|
}
|
|
|
|
endpoint := ctx.Args().First()
|
|
if endpoint == "" {
|
|
endpoint = fmt.Sprintf("%s/%s.ipc", path, clientIdentifier)
|
|
}
|
|
|
|
config := &node.Config{
|
|
DataDir: path,
|
|
}
|
|
scryptN, scryptP, keydir, err := config.AccountConfig()
|
|
if err != nil {
|
|
panic(err) // TODO(prestonvanloon): handle this
|
|
}
|
|
ks := keystore.NewKeyStore(keydir, scryptN, scryptP)
|
|
|
|
return &Client{
|
|
endpoint: endpoint,
|
|
keystore: ks,
|
|
ctx: ctx,
|
|
}
|
|
}
|
|
|
|
// Start the sharding client.
|
|
// * Connects to node.
|
|
// * Verifies or deploys the validator management contract.
|
|
func (c *Client) Start() error {
|
|
log.Info("Starting sharding client")
|
|
rpcClient, err := dialRPC(c.endpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
c.client = ethclient.NewClient(rpcClient)
|
|
defer rpcClient.Close()
|
|
if err := initVMC(c); err != nil {
|
|
return err
|
|
}
|
|
|
|
// TODO: Wait to be selected as collator in goroutine?
|
|
|
|
return nil
|
|
}
|
|
|
|
// Wait until sharding client is shutdown.
|
|
func (c *Client) Wait() {
|
|
// TODO: Blocking lock.
|
|
}
|
|
|
|
// dialRPC endpoint to node.
|
|
func dialRPC(endpoint string) (*rpc.Client, error) {
|
|
if endpoint == "" {
|
|
endpoint = node.DefaultIPCEndpoint(clientIdentifier)
|
|
}
|
|
return rpc.Dial(endpoint)
|
|
}
|
|
|
|
// UnlockAccount will unlock the specified account using utils.PasswordFileFlag or empty string if unset.
|
|
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))
|
|
if err != nil {
|
|
return fmt.Errorf("unable to read account password contents in file %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.
|
|
}
|
|
|
|
return c.keystore.Unlock(account, pass)
|
|
}
|