Refactor Code

Former-commit-id: ad6a4793064556b556a92dbed3f8a555972c07a1 [formerly 13e903a723eee255b4eb6e2bcc7be06ba73b6fa7]
Former-commit-id: b2ecf21d02170030dfc56e9d9303aeb7bee45fab
This commit is contained in:
nisdas
2018-03-31 12:07:42 +08:00
parent f47a4c5984
commit 394adb4750
12 changed files with 339 additions and 296 deletions

View File

@@ -1,9 +1,9 @@
package main
import (
"github.com/ethereum/go-ethereum/sharding"
"github.com/ethereum/go-ethereum/sharding/collator"
//"github.com/ethereum/go-ethereum/sharding/proposer"
"fmt"
"github.com/ethereum/go-ethereum/cmd/utils"
cli "gopkg.in/urfave/cli.v1"
)
@@ -36,8 +36,8 @@ Launches a sharding proposer client that connects to a running geth node and pro
)
func collatorClient(ctx *cli.Context) error {
c := sharding.MakeCollatorClient(ctx)
if err := c.Start(); err != nil {
c := collator.NewCollatorClient(ctx)
if err := collator.CollatorStart(c); err != nil {
return err
}
c.Wait()
@@ -45,6 +45,11 @@ func collatorClient(ctx *cli.Context) error {
}
func proposerClient(ctx *cli.Context) error {
fmt.Println("Starting proposer client")
/*p := proposer.NewProposerClient(ctx)
if err := proposer.ProposerStart(p); err != nil {
return err
}
p.Wait()
*/
return nil
}

View File

@@ -1 +1,182 @@
package client
import (
"bufio"
"context"
"errors"
"fmt"
"math/big"
"os"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"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.
)
// General Client for Collator. Communicates to Geth node via JSON RPC.
type ShardingClient 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
Smc *contracts.SMC // The deployed sharding management contract
}
type Client interface {
MakeClient(*cli.Context) *ShardingClient
Start() error
CreateTXOps(*big.Int) (*bind.TransactOpts, error)
initSMC() error
}
func MakeClient(ctx *cli.Context) *ShardingClient {
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)
}
if ctx.GlobalIsSet(utils.IPCPathFlag.Name) {
endpoint = ctx.GlobalString(utils.IPCPathFlag.Name)
}
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 &ShardingClient{
endpoint: endpoint,
keystore: ks,
Ctx: ctx,
}
}
// Start the sharding client.
// * Connects to Geth node.
// * Verifies or deploys the sharding manager contract.
func (c *ShardingClient) Start() error {
log.Info("Starting collator client")
rpcClient, err := dialRPC(c.endpoint)
if err != nil {
return err
}
c.client = ethclient.NewClient(rpcClient)
defer rpcClient.Close()
// Check account existence and unlock account before starting collator 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 := initSMC(c); err != nil {
return err
}
return nil
}
// Wait until collator client is shutdown.
func (c *ShardingClient) Wait() {
log.Info("Sharding client has been shutdown...")
}
// UnlockAccount will unlock the specified account using utils.PasswordFileFlag or empty string if unset.
func (c *ShardingClient) unlockAccount(account accounts.Account) error {
pass := ""
if c.Ctx.GlobalIsSet(utils.PasswordFileFlag.Name) {
file, err := os.Open(c.Ctx.GlobalString(utils.PasswordFileFlag.Name))
if err != nil {
return fmt.Errorf("unable to open file containing account password %s. %v", utils.PasswordFileFlag.Value, err)
}
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 *ShardingClient) CreateTXOps(value *big.Int) (*bind.TransactOpts, error) {
account := c.Account()
return &bind.TransactOpts{
From: account.Address,
Value: value,
Signer: func(signer types.Signer, addr common.Address, tx *types.Transaction) (*types.Transaction, error) {
networkID, err := c.client.NetworkID(context.Background())
if err != nil {
return nil, fmt.Errorf("unable to fetch networkID: %v", err)
}
return c.keystore.SignTx(*account, tx, networkID /* chainID */)
},
}, nil
}
// Account to use for sharding transactions.
func (c *ShardingClient) Account() *accounts.Account {
accounts := c.keystore.Accounts()
return &accounts[0]
}
// ChainReader for interacting with the chain.
func (c *ShardingClient) ChainReader() ethereum.ChainReader {
return ethereum.ChainReader(c.client)
}
// Client to interact with ethereum node.
func (c *ShardingClient) ethereumClient() *ethclient.Client {
return c.client
}
// SMCCaller to interact with the sharding manager contract.
func (c *ShardingClient) SMCCaller() *contracts.SMCCaller {
return &c.Smc.SMCCaller
}
// dialRPC endpoint to node.
func dialRPC(endpoint string) (*rpc.Client, error) {
if endpoint == "" {
endpoint = node.DefaultIPCEndpoint(clientIdentifier)
}
return rpc.Dial(endpoint)
}

53
sharding/client/smc.go Normal file
View File

@@ -0,0 +1,53 @@
package client
import (
"context"
"fmt"
"math/big"
"time"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/sharding"
"github.com/ethereum/go-ethereum/sharding/contracts"
)
// initSMC initializes the sharding manager contract bindings.
// If the SMC does not exist, it will be deployed.
func initSMC(c *ShardingClient) error {
b, err := c.client.CodeAt(context.Background(), sharding.ShardingManagerAddress, nil)
if err != nil {
return fmt.Errorf("unable to get contract code at %s: %v", sharding.ShardingManagerAddress, err)
}
if len(b) == 0 {
log.Info(fmt.Sprintf("No sharding manager contract found at %s. Deploying new contract.", sharding.ShardingManagerAddress.String()))
txOps, err := c.CreateTXOps(big.NewInt(0))
if err != nil {
return fmt.Errorf("unable to intiate the transaction: %v", err)
}
addr, tx, contract, err := contracts.DeploySMC(txOps, c.client)
if err != nil {
return fmt.Errorf("unable to deploy sharding manager contract: %v", err)
}
for pending := true; pending; _, pending, err = c.client.TransactionByHash(context.Background(), tx.Hash()) {
if err != nil {
return fmt.Errorf("unable to get transaction by hash: %v", err)
}
time.Sleep(1 * time.Second)
}
c.Smc = contract
log.Info(fmt.Sprintf("New contract deployed at %s", addr.String()))
} else {
contract, err := contracts.NewSMC(sharding.ShardingManagerAddress, c.client)
if err != nil {
return fmt.Errorf("failed to create sharding contract: %v", err)
}
c.Smc = contract
}
return nil
}

View File

@@ -1,4 +1,4 @@
package sharding
package collator
import (
"context"
@@ -10,6 +10,7 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/sharding"
"github.com/ethereum/go-ethereum/sharding/contracts"
)
@@ -67,8 +68,8 @@ func checkSMCForCollator(c collator, head *types.Header) error {
account := c.Account()
log.Info("Checking if we are an eligible collation collator for a shard...")
period := big.NewInt(0).Div(head.Number, big.NewInt(periodLength))
for s := int64(0); s < shardCount; s++ {
period := big.NewInt(0).Div(head.Number, big.NewInt(sharding.PeriodLength))
for s := int64(0); s < sharding.ShardCount; s++ {
// Checks if we are an eligible collator according to the SMC
addr, err := c.SMCCaller().GetEligibleCollator(&bind.CallOpts{}, big.NewInt(s), period)

View File

@@ -1 +1,27 @@
package collator
package collator
import (
"github.com/ethereum/go-ethereum/sharding/client"
cli "gopkg.in/urfave/cli.v1"
)
func NewCollatorClient(ctx *cli.Context) *client.ShardingClient {
c := client.MakeClient(ctx)
return c
}
func CollatorStart(sclient *client.ShardingClient) error {
sclient.Start()
if err := joinCollatorPool(sclient); err != nil {
return err
}
if err := subscribeBlockHeaders(sclient); err != nil {
return err
}
return nil
}

View File

@@ -1,4 +1,4 @@
package sharding
package collator
import (
"context"

View File

@@ -1,4 +1,4 @@
package sharding
package collator
import (
"context"

37
sharding/collator/smc.go Normal file
View File

@@ -0,0 +1,37 @@
package collator
import (
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/sharding"
"github.com/ethereum/go-ethereum/sharding/client"
)
// joinCollatorPool checks if the account is a collator in the SMC. If
// the account is not in the set, it will deposit 100ETH into contract.
func joinCollatorPool(c *client.ShardingClient) error {
if c.Ctx.GlobalBool(utils.DepositFlag.Name) {
log.Info("Joining collator pool")
txOps, err := c.CreateTXOps(sharding.DepositSize)
if err != nil {
return fmt.Errorf("unable to intiate the deposit transaction: %v", err)
}
tx, err := c.Smc.SMCTransactor.Deposit(txOps)
if err != nil {
return fmt.Errorf("unable to deposit eth and become a collator: %v", err)
}
log.Info(fmt.Sprintf("Deposited %dETH into contract with transaction hash: %s", new(big.Int).Div(sharding.DepositSize, big.NewInt(params.Ether)), tx.Hash().String()))
} else {
log.Info("Not joining collator pool")
}
return nil
}

View File

@@ -1,199 +0,0 @@
package sharding
//go:generate abigen --sol contracts/sharding_manager.sol --pkg contracts --out contracts/sharding_manager.go
import (
"bufio"
"context"
"errors"
"fmt"
"math/big"
"os"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"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 Collator. Communicates to Geth node via JSON RPC.
type collatorClient 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
smc *contracts.SMC // The deployed sharding management contract
}
// MakeCollatorClient for interfacing with Geth full node.
func MakeCollatorClient(ctx *cli.Context) *collatorClient {
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)
}
if ctx.GlobalIsSet(utils.IPCPathFlag.Name) {
endpoint = ctx.GlobalString(utils.IPCPathFlag.Name)
}
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 &collatorClient{
endpoint: endpoint,
keystore: ks,
ctx: ctx,
}
}
// Start the collator client.
// * Connects to Geth node.
// * Verifies or deploys the sharding manager contract.
func (c *collatorClient) Start() error {
log.Info("Starting collator client")
rpcClient, err := dialRPC(c.endpoint)
if err != nil {
return err
}
c.client = ethclient.NewClient(rpcClient)
defer rpcClient.Close()
// Check account existence and unlock account before starting collator 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 := initSMC(c); err != nil {
return err
}
// Deposit 100ETH into the collator set in the SMC. Checks if account
// is already a collator in the SMC (in the case the client restarted).
// Once that's done we can subscribe to block headers.
//
// TODO: this function should store the collator's SMC index as a property
// in the client's struct
if err := joinCollatorPool(c); err != nil {
return err
}
// Listens to block headers from the Geth node and if we are an eligible
// collator, we fetch pending transactions and collator a collation
if err := subscribeBlockHeaders(c); err != nil {
return err
}
return nil
}
// Wait until collator client is shutdown.
func (c *collatorClient) Wait() {
log.Info("Sharding client has been shutdown...")
}
// WatchCollationHeaders checks the logs for add_header func calls
// and updates the head collation of the client. We can probably store
// this as a property of the client struct
func (c *collatorClient) WatchCollationHeaders() {
}
// UnlockAccount will unlock the specified account using utils.PasswordFileFlag or empty string if unset.
func (c *collatorClient) unlockAccount(account accounts.Account) error {
pass := ""
if c.ctx.GlobalIsSet(utils.PasswordFileFlag.Name) {
file, err := os.Open(c.ctx.GlobalString(utils.PasswordFileFlag.Name))
if err != nil {
return fmt.Errorf("unable to open file containing account password %s. %v", utils.PasswordFileFlag.Value, err)
}
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 *collatorClient) createTXOps(value *big.Int) (*bind.TransactOpts, error) {
account := c.Account()
return &bind.TransactOpts{
From: account.Address,
Value: value,
Signer: func(signer types.Signer, addr common.Address, tx *types.Transaction) (*types.Transaction, error) {
networkID, err := c.client.NetworkID(context.Background())
if err != nil {
return nil, fmt.Errorf("unable to fetch networkID: %v", err)
}
return c.keystore.SignTx(*account, tx, networkID /* chainID */)
},
}, nil
}
// Account to use for sharding transactions.
func (c *collatorClient) Account() *accounts.Account {
accounts := c.keystore.Accounts()
return &accounts[0]
}
// ChainReader for interacting with the chain.
func (c *collatorClient) ChainReader() ethereum.ChainReader {
return ethereum.ChainReader(c.client)
}
// Client to interact with ethereum node.
func (c *collatorClient) Client() *ethclient.Client {
return c.client
}
// SMCCaller to interact with the sharding manager contract.
func (c *collatorClient) SMCCaller() *contracts.SMCCaller {
return &c.smc.SMCCaller
}
// dialRPC endpoint to node.
func dialRPC(endpoint string) (*rpc.Client, error) {
if endpoint == "" {
endpoint = node.DefaultIPCEndpoint(clientIdentifier)
}
return rpc.Dial(endpoint)
}

View File

@@ -8,17 +8,17 @@ import (
var (
// Number of network shards
shardCount = int64(1)
ShardCount = int64(1)
// Address of the sharding manager contract
shardingManagerAddress = common.HexToAddress("0x0") // TODO
ShardingManagerAddress = common.HexToAddress("0x0") // TODO
// Gas limit for verifying signatures
sigGasLimit = 40000
SigGasLimit = 40000
// Number of blocks in a period
periodLength = int64(5)
PeriodLength = int64(5)
// Number of periods ahead of current period which the contract is able to return the collator of that period.
lookaheadPeriods = 4
LookaheadPeriods = 4
// Required deposit size in wei
depositSize = new(big.Int).Exp(big.NewInt(10), big.NewInt(20), nil) // 100 ETH
DepositSize = new(big.Int).Exp(big.NewInt(10), big.NewInt(20), nil) // 100 ETH
// Gas limit to create contract
contractGasLimit = uint64(4700000) // Max is 4712388
ContractGasLimit = uint64(4700000) // Max is 4712388
)

View File

@@ -1 +1,19 @@
package proposer
import (
"github.com/ethereum/go-ethereum/sharding/client"
cli "gopkg.in/urfave/cli.v1"
)
func NewProposerClient(ctx *cli.Context) *client.ShardingClient {
c := client.MakeClient(ctx)
return c
}
func ProposerStart(sclient *client.ShardingClient) error {
sclient.Start()
return nil
}

View File

@@ -1,79 +0,0 @@
package sharding
import (
"context"
"fmt"
"math/big"
"time"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/sharding/contracts"
)
// initSMC initializes the sharding manager contract bindings.
// If the SMC does not exist, it will be deployed.
func initSMC(c *collatorClient) error {
b, err := c.client.CodeAt(context.Background(), shardingManagerAddress, nil)
if err != nil {
return fmt.Errorf("unable to get contract code at %s: %v", shardingManagerAddress, err)
}
if len(b) == 0 {
log.Info(fmt.Sprintf("No sharding manager contract found at %s. Deploying new contract.", shardingManagerAddress.String()))
txOps, err := c.createTXOps(big.NewInt(0))
if err != nil {
return fmt.Errorf("unable to intiate the transaction: %v", err)
}
addr, tx, contract, err := contracts.DeploySMC(txOps, c.client)
if err != nil {
return fmt.Errorf("unable to deploy sharding manager contract: %v", err)
}
for pending := true; pending; _, pending, err = c.client.TransactionByHash(context.Background(), tx.Hash()) {
if err != nil {
return fmt.Errorf("unable to get transaction by hash: %v", err)
}
time.Sleep(1 * time.Second)
}
c.smc = contract
log.Info(fmt.Sprintf("New contract deployed at %s", addr.String()))
} else {
contract, err := contracts.NewSMC(shardingManagerAddress, c.client)
if err != nil {
return fmt.Errorf("failed to create sharding contract: %v", err)
}
c.smc = contract
}
return nil
}
// joinCollatorPool checks if the account is a collator in the SMC. If
// the account is not in the set, it will deposit 100ETH into contract.
func joinCollatorPool(c *collatorClient) error {
if c.ctx.GlobalBool(utils.DepositFlag.Name) {
log.Info("Joining collator pool")
txOps, err := c.createTXOps(depositSize)
if err != nil {
return fmt.Errorf("unable to intiate the deposit transaction: %v", err)
}
tx, err := c.smc.SMCTransactor.Deposit(txOps)
if err != nil {
return fmt.Errorf("unable to deposit eth and become a collator: %v", err)
}
log.Info(fmt.Sprintf("Deposited %dETH into contract with transaction hash: %s", new(big.Int).Div(depositSize, big.NewInt(params.Ether)), tx.Hash().String()))
} else {
log.Info("Not joining collator pool")
}
return nil
}