diff --git a/README.md b/README.md index 762aa4c50..1db66e096 100644 --- a/README.md +++ b/README.md @@ -119,16 +119,16 @@ Concurrently, you will need to run another service that is tasked with processin ## Running a Collation Proposal Node ``` -geth sharding --actor "proposer" --datadir /path/to/your/datadir --password /path/to/your/password.txt --networkid 12345 +geth sharding --actor "proposer" --datadir /path/to/your/datadir --password /path/to/your/password.txt --shardid 0 --networkid 12345 ``` -This node is tasked with processing pending transactions into blobs within collations by serializing data into collation bodies. It is responsible for submitting proposals (collation headers) to the SMC via the `addHeader` function. +This node is tasked with processing pending transactions into blobs within collations by serializing data into collation bodies. It is responsible for submitting proposals on shard 0 (collation headers) to the SMC via the `addHeader` function. ## Running an Observer Node - geth sharding --datadir /path/to/your/datadir --password /path/to/your/password.txt --networkid 12345 + geth sharding --datadir /path/to/your/datadir --password /path/to/your/password.txt --shardid 0 --networkid 12345 -Omitting the `--actor` flag will launch a simple observer service attached to the sharding client that is able to listen to changes happening throughout the sharded Ethereum network. +Omitting the `--actor` flag will launch a simple observer service attached to the sharding client that is able to listen to changes happening throughout the sharded Ethereum network on shard 0. # Making Changes diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 1e4e6aebb..37f9bd43f 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -120,6 +120,7 @@ var ( utils.ExtraDataFlag, utils.DepositFlag, utils.ActorFlag, + utils.ShardIDFlag, configFileFlag, } diff --git a/cmd/geth/shardingcmd.go b/cmd/geth/shardingcmd.go index 19fdca60c..d5e889856 100644 --- a/cmd/geth/shardingcmd.go +++ b/cmd/geth/shardingcmd.go @@ -14,7 +14,7 @@ var ( Name: "sharding", Usage: "Start a sharding-enabled node", ArgsUsage: "[endpoint]", - Flags: []cli.Flag{utils.ActorFlag, utils.DataDirFlag, utils.PasswordFileFlag, utils.NetworkIdFlag, utils.IPCPathFlag, utils.DepositFlag}, + Flags: []cli.Flag{utils.ActorFlag, utils.DataDirFlag, utils.PasswordFileFlag, utils.NetworkIdFlag, utils.IPCPathFlag, utils.DepositFlag, utils.ShardIDFlag}, Category: "SHARDING COMMANDS", Description: ` Launches a sharding node that manages services related to submitting collations to a Sharding Manager Contract, notary and proposer services, and shardp2p connections. This feature is a work in progress. diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 47d31729e..9ad40364e 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -542,6 +542,10 @@ var ( Name: "actor", Usage: `use the --actor notary or --actor proposer to start a notary or proposer service in the sharding node. If omitted, the sharding node registers an Observer service that simply observes the activity in the sharded network`, } + ShardIDFlag = cli.IntFlag{ + Name: "shardid", + Usage: `use the --shardid to determine which shard to start p2p server, listen for incoming transactions and perform proposer/observer duties`, + } ) // MakeDataDir retrieves the currently requested data directory, terminating diff --git a/sharding/node/backend.go b/sharding/node/backend.go index cfb459f27..819e886d6 100644 --- a/sharding/node/backend.go +++ b/sharding/node/backend.go @@ -74,6 +74,7 @@ func New(ctx *cli.Context) (*ShardEthereum, error) { passwordFile := ctx.GlobalString(utils.PasswordFileFlag.Name) depositFlag := ctx.GlobalBool(utils.DepositFlag.Name) actorFlag := ctx.GlobalString(utils.ActorFlag.Name) + shardIDFlag := ctx.GlobalInt(utils.ShardIDFlag.Name) smcClient, err := mainchain.NewSMCClient(endpoint, path, depositFlag, passwordFile) if err != nil { @@ -99,7 +100,7 @@ func New(ctx *cli.Context) (*ShardEthereum, error) { return nil, err } - if err := shardEthereum.registerActorService(actorFlag); err != nil { + if err := shardEthereum.registerActorService(actorFlag, shardIDFlag); err != nil { return nil, err } @@ -209,7 +210,7 @@ func (s *ShardEthereum) registerTXPool(actor string) error { } // Registers the actor according to CLI flags. Either notary/proposer/observer. -func (s *ShardEthereum) registerActorService(actor string) error { +func (s *ShardEthereum) registerActorService(actor string, shardID int) error { return s.Register(func(ctx *sharding.ServiceContext) (sharding.Service, error) { var p2p *shardp2p.Server @@ -220,9 +221,8 @@ func (s *ShardEthereum) registerActorService(actor string) error { } else if actor == "proposer" { var txPool *txpool.ShardTXPool ctx.RetrieveService(&txPool) - return proposer.NewProposer(s.smcClient, p2p, txPool, s.shardChainDb) + return proposer.NewProposer(s.smcClient, p2p, txPool, s.shardChainDb, shardID) } - - return observer.NewObserver(p2p, s.shardChainDb) + return observer.NewObserver(p2p, s.shardChainDb, shardID) }) } diff --git a/sharding/observer/service.go b/sharding/observer/service.go index 46b4bad50..a1fb9544d 100644 --- a/sharding/observer/service.go +++ b/sharding/observer/service.go @@ -3,6 +3,8 @@ package observer import ( + "fmt" + "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/sharding" @@ -14,20 +16,21 @@ import ( type Observer struct { shardp2p sharding.ShardP2P shardChainDb ethdb.Database + shardID int } // NewObserver creates a new observer instance. -func NewObserver(shardp2p sharding.ShardP2P, shardChainDb ethdb.Database) (*Observer, error) { - return &Observer{shardp2p, shardChainDb}, nil +func NewObserver(shardp2p sharding.ShardP2P, shardChainDb ethdb.Database, shardID int) (*Observer, error) { + return &Observer{shardp2p, shardChainDb, shardID}, nil } // Start the main routine for an observer. func (o *Observer) Start() { - log.Info("Starting shard observer service") + log.Info(fmt.Sprintf("Starting observer service in shard %d", o.shardID)) } // Stop the main loop for observing the shard network. func (o *Observer) Stop() error { - log.Info("Stopping shard observer service") + log.Info(fmt.Sprintf("Starting observer service in shard %d", o.shardID)) return nil } diff --git a/sharding/proposer/service.go b/sharding/proposer/service.go index 8e59945ce..f8af2252b 100644 --- a/sharding/proposer/service.go +++ b/sharding/proposer/service.go @@ -24,24 +24,25 @@ type Proposer struct { shardp2p sharding.ShardP2P txpool sharding.TXPool shardChainDb ethdb.Database + shardID int } // NewProposer creates a struct instance of a proposer service. // It will have access to a mainchain client, a shardp2p network, // and a shard transaction pool. -func NewProposer(client *mainchain.SMCClient, shardp2p sharding.ShardP2P, txpool sharding.TXPool, shardChainDb ethdb.Database) (*Proposer, error) { - return &Proposer{client, shardp2p, txpool, shardChainDb}, nil +func NewProposer(client *mainchain.SMCClient, shardp2p sharding.ShardP2P, txpool sharding.TXPool, shardChainDb ethdb.Database, shardID int) (*Proposer, error) { + return &Proposer{client, shardp2p, txpool, shardChainDb, shardID}, nil } // Start the main loop for proposing collations. func (p *Proposer) Start() { - log.Info("Starting proposer service") + 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("Stopping proposer service") + log.Info(fmt.Sprintf("Stopping proposer service in shard %d", p.shardID)) return nil } @@ -56,9 +57,6 @@ func (p *Proposer) proposeCollations() { nil, 0, nil, data)) } - // TODO: Create and use CLI flag for shardID - shardID := big.NewInt(0) - // Get current block number. blockNumber, err := p.client.ChainReader().BlockByNumber(context.Background(), nil) if err != nil { @@ -68,14 +66,14 @@ func (p *Proposer) proposeCollations() { period := new(big.Int).Div(blockNumber.Number(), big.NewInt(sharding.PeriodLength)) // Create collation. - collation, err := createCollation(p.client, shardID, period, txs) + collation, err := createCollation(p.client, big.NewInt(int64(p.shardID)), period, txs) if err != nil { 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, shardID, period) + canAdd, err := checkHeaderAdded(p.client, big.NewInt(int64(p.shardID)), period) if err != nil { log.Error(fmt.Sprintf("Could not check if we can submit header: %v", err)) return