sharding: sync with master

Former-commit-id: e9e3daacc36c75fb87fe86dc0f17f92989defd38 [formerly 9efd4db17012e6c35911e293aafa8937711a5fdd]
Former-commit-id: d7f24230c673a4801abe71b21acf2acaba8f85fc
This commit is contained in:
Terence Tsao 2018-06-12 16:12:59 -07:00
commit 6c17f3c430
10 changed files with 116 additions and 42 deletions

View File

@ -45,7 +45,7 @@ type ServiceConstructor func(ctx *ServiceContext) (Service, error)
type Service interface {
// Start is called after all services have been constructed to
// spawn any goroutines required by the service.
Start() error
Start()
// Stop terminates all goroutines belonging to the service,
// blocking until they are all terminated.
Stop() error

View File

@ -6,9 +6,13 @@ package node
import (
"fmt"
"os"
"os/signal"
"reflect"
"sync"
"syscall"
"github.com/ethereum/go-ethereum/internal/debug"
"github.com/ethereum/go-ethereum/sharding/notary"
"github.com/ethereum/go-ethereum/sharding/observer"
shardp2p "github.com/ethereum/go-ethereum/sharding/p2p"
@ -111,14 +115,34 @@ func New(ctx *cli.Context) (*ShardEthereum, error) {
// Start the ShardEthereum service and kicks off the p2p and actor's main loop.
func (s *ShardEthereum) Start() {
log.Info("Starting sharding node")
for kind, service := range s.services {
for _, service := range s.services {
// Start the next service.
if err := service.Start(); err != nil {
log.Error(fmt.Sprintf("Could not start service: %v, %v", kind, err))
s.Close()
}
service.Start()
}
go func() {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
defer signal.Stop(sigc)
<-sigc
log.Info("Got interrupt, shutting down...")
go s.Close()
for i := 10; i > 0; i-- {
<-sigc
if i > 1 {
log.Warn("Already shutting down, interrupt more to panic.", "times", i-1)
}
}
// ensure trace and CPU profile data is flushed.
debug.Exit()
debug.LoudPanic("boom")
}()
// hang forever...
select {}
}
// Close handles graceful shutdown of the system.

View File

@ -3,6 +3,8 @@
package notary
import (
"fmt"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/sharding"
@ -26,18 +28,9 @@ func NewNotary(config *params.ShardConfig, smcClient *mainchain.SMCClient, shard
}
// Start the main routine for a notary.
func (n *Notary) Start() error {
func (n *Notary) Start() {
log.Info("Starting notary service")
// TODO: handle this better through goroutines. Right now, these methods
// are blocking.
if n.smcClient.DepositFlag() {
if err := joinNotaryPool(n.config, n.smcClient); err != nil {
return err
}
}
return subscribeBlockHeaders(n.smcClient)
go n.notarizeCollations()
}
// Stop the main loop for notarizing collations.
@ -45,3 +38,19 @@ func (n *Notary) Stop() error {
log.Info("Stopping notary service")
return nil
}
func (n *Notary) notarizeCollations() {
// TODO: handle this better through goroutines. Right now, these methods
// are blocking.
if n.smcClient.DepositFlag() {
if err := joinNotaryPool(n.config, n.smcClient); err != nil {
log.Error(fmt.Sprintf("Could not fetch current block number: %v", err))
return
}
}
if err := subscribeBlockHeaders(n.smcClient); err != nil {
log.Error(fmt.Sprintf("Could not fetch current block number: %v", err))
return
}
}

View File

@ -154,13 +154,13 @@ func TestJoinNotaryPool(t *testing.T) {
}
client.SetDepositFlag(false)
err = joinNotaryPool(&params.DefaultTestConfig, client)
err = joinNotaryPool(&params.DefaultShardConfig, client)
if err == nil {
t.Error("Joined notary pool while --deposit was not present")
}
client.SetDepositFlag(true)
err = joinNotaryPool(&params.DefaultTestConfig, client)
err = joinNotaryPool(&params.DefaultShardConfig, client)
if err != nil {
t.Fatal(err)
}
@ -176,7 +176,7 @@ func TestJoinNotaryPool(t *testing.T) {
}
// Trying to join while deposited should do nothing
err = joinNotaryPool(&params.DefaultTestConfig, client)
err = joinNotaryPool(&params.DefaultShardConfig, client)
if err != nil {
t.Error(err)
}

View File

@ -25,9 +25,8 @@ func NewObserver(shardp2p sharding.ShardP2P, shardChainDb ethdb.Database, shardI
}
// Start the main routine for an observer.
func (o *Observer) Start() error {
func (o *Observer) Start() {
log.Info(fmt.Sprintf("Starting observer service in shard %d", o.shardID))
return nil
}
// Stop the main loop for observing the shard network.

View File

@ -14,9 +14,8 @@ func NewServer() (*Server, error) {
}
// Start the main routine for an shardp2p server.
func (s *Server) Start() error {
func (s *Server) Start() {
log.Info("Starting shardp2p server")
return nil
}
// Stop the main shardp2p loop..

40
sharding/params/config.go Normal file
View File

@ -0,0 +1,40 @@
// Package params defines important configuration options to be used when instantiating
// objects within the sharding package. For example, it defines objects such as a
// ShardConfig that will be useful when creating new shard instances.
package params
import (
"math/big"
"github.com/ethereum/go-ethereum/common"
)
// DefaultShardConfig contains default settings for node to use in the sharded universe
var DefaultShardConfig = ShardConfig{
SMCAddress: common.HexToAddress("0x0"),
PeriodLength: 5,
NotaryDeposit: new(big.Int).Exp(big.NewInt(10), big.NewInt(21), nil), // 1000 ETH
NotaryLockupLength: 16128,
ProposerLockupLength: 48,
NotaryCommitteeSize: 135,
NotaryQuorumSize: 90,
NotaryChallengePeriod: 25,
}
// DefaultShardChainConfig contains default chain settings of individual shard.
var DefaultShardChainConfig = ShardChainConfig{}
// ShardConfig contains configs for node to participate in the sharded universe.
type ShardConfig struct {
SMCAddress common.Address // SMCAddress is the address of SMC in mainchain.
PeriodLength int64 // PeriodLength is num of blocks in period.
NotaryDeposit *big.Int // NotaryDeposit is a required deposit size in wei.
NotaryLockupLength int64 // NotaryLockupLength to lockup notary deposit from time of deregistration.
ProposerLockupLength int64 // ProposerLockupLength to lockup proposer deposit from time of deregistration.
NotaryCommitteeSize int64 // NotaryCommitSize sampled per block from the notaries pool per period per shard.
NotaryQuorumSize int64 // NotaryQuorumSize votes the collation needs to get accepted to the canonical chain.
NotaryChallengePeriod int64 // NotaryChallengePeriod is the duration a notary has to store collations for.
}
// ShardChainConfig contains chain config of individual shard. Still to be designed.
type ShardChainConfig struct {}

View File

@ -33,13 +33,22 @@ type Proposer struct {
// It will have access to a mainchain client, a shardp2p network,
// and a shard transaction pool.
func NewProposer(config *params.ShardConfig, client *mainchain.SMCClient, shardp2p sharding.ShardP2P, txpool sharding.TXPool, shardChainDb ethdb.Database, shardID int) (*Proposer, error) {
// Initializes a directory persistent db.
return &Proposer{config, client, shardp2p, txpool, shardChainDb, shardID}, nil
}
// Start the main loop for proposing collations.
func (p *Proposer) Start() error {
func (p *Proposer) Start() {
log.Info(fmt.Sprintf("Starting proposer service in shard %d", p.shardID))
go p.proposeCollations()
}
// Stop the main loop for proposing collations.
func (p *Proposer) Stop() error {
log.Info(fmt.Sprintf("Stopping proposer service in shard %d", p.shardID))
return nil
}
func (p *Proposer) proposeCollations() {
// TODO: Receive TXs from shard TX generator or TXpool (Github Issues 153 and 161)
var txs []*types.Transaction
@ -53,30 +62,25 @@ func (p *Proposer) Start() error {
// Get current block number.
blockNumber, err := p.client.ChainReader().BlockByNumber(context.Background(), nil)
if err != nil {
return err
log.Error(fmt.Sprintf("Could not fetch current block number: %v", err))
return
}
period := new(big.Int).Div(blockNumber.Number(), big.NewInt(p.config.PeriodLength))
// Create collation.
collation, err := createCollation(p.client, big.NewInt(int64(p.shardID)), period, txs)
if err != nil {
return err
log.Error(fmt.Sprintf("Could not create collation: %v", err))
return
}
// Check SMC if we can submit header before addHeader
canAdd, err := checkHeaderAdded(p.client, big.NewInt(int64(p.shardID)), period)
if err != nil {
return err
log.Error(fmt.Sprintf("Could not check if we can submit header: %v", err))
return
}
if canAdd {
addHeader(p.client, collation)
}
return nil
}
// Stop the main loop for proposing collations.
func (p *Proposer) Stop() error {
log.Info(fmt.Sprintf("Stopping proposer service in shard %d", p.shardID))
return nil
}

View File

@ -128,7 +128,7 @@ func TestCreateCollation(t *testing.T) {
}
// fast forward to 2nd period.
for i := 0; i < 2*int(params.DefaultTestConfig.PeriodLength); i++ {
for i := 0; i < 2*int(params.DefaultShardConfig.PeriodLength); i++ {
backend.Commit()
}
@ -186,7 +186,7 @@ func TestAddCollation(t *testing.T) {
}
// fast forward to next period.
for i := 0; i < int(params.DefaultTestConfig.PeriodLength); i++ {
for i := 0; i < int(params.DefaultShardConfig.PeriodLength); i++ {
backend.Commit()
}
@ -233,7 +233,7 @@ func TestCheckCollation(t *testing.T) {
t.Errorf("Create collation failed: %v", err)
}
for i := 0; i < int(params.DefaultTestConfig.PeriodLength); i++ {
for i := 0; i < int(params.DefaultShardConfig.PeriodLength); i++ {
backend.Commit()
}

View File

@ -17,9 +17,8 @@ func NewShardTXPool(p2p sharding.ShardP2P) (*ShardTXPool, error) {
}
// Start the main routine for a shard transaction pool.
func (p *ShardTXPool) Start() error {
func (p *ShardTXPool) Start() {
log.Info("Starting shard txpool service")
return nil
}
// Stop the main loop for a transaction pool in the shard network.