Skip to content

Commit

Permalink
Merge pull request ethereum#173 from terenc3t/shardConfig
Browse files Browse the repository at this point in the history
Use shardConfig Across Code
  • Loading branch information
terencechain authored Jun 13, 2018
2 parents da8c883 + 6546f06 commit cdfec9b
Show file tree
Hide file tree
Showing 18 changed files with 210 additions and 152 deletions.
4 changes: 2 additions & 2 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import (
"github.com/ethereum/go-ethereum/p2p/nat"
"github.com/ethereum/go-ethereum/p2p/netutil"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/sharding"
shardparams "github.com/ethereum/go-ethereum/sharding/params"
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
"gopkg.in/urfave/cli.v1"
)
Expand Down Expand Up @@ -536,7 +536,7 @@ var (
// Sharding Settings
DepositFlag = cli.BoolFlag{
Name: "deposit",
Usage: "To become a notary in a sharding node, " + new(big.Int).Div(sharding.NotaryDeposit, new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)).String() + " ETH will be deposited into SMC",
Usage: "To become a notary in a sharding node, " + new(big.Int).Div(shardparams.DefaultShardConfig.NotaryDeposit, new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)).String() + " ETH will be deposited into SMC",
}
ActorFlag = cli.StringFlag{
Name: "actor",
Expand Down
39 changes: 0 additions & 39 deletions sharding/config.go

This file was deleted.

46 changes: 0 additions & 46 deletions sharding/config_test.go

This file was deleted.

30 changes: 28 additions & 2 deletions sharding/contracts/sharding_manager.go

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions sharding/contracts/sharding_manager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,26 @@ contract SMC {
uint nextPeriodNotarySampleSize;
uint sampleSizeLastUpdatedPeriod;

// Number of shards
// TODO: Setting default as 100. This will be a dynamic when we introduce random beacon
uint public shardCount = 100;

// Constant values
// Length of challenge period for notary's proof of custody
uint public constant CHALLENGE_PERIOD = 25;
// Number of blocks per period
uint constant PERIOD_LENGTH = 5;
// Number of shards
uint constant SHARD_COUNT = 100;
// The minimum deposit size for a notary
uint constant NOTARY_DEPOSIT = 1000 ether;
// The reward for notary on voting for a collation
uint constant NOTARY_REWARD = 0.001 ether;
// Time the ether is locked by notaries
uint constant NOTARY_LOCKUP_LENGTH = 16128;
// Number of periods ahead of current period, which the contract
// is able to return the notary of that period
uint constant LOOKAHEAD_LENGTH = 4;
// Number of notaries to select from notary pool for each shard in each period
uint constant COMMITTEE_SIZE = 135;
// Threshold(number of notaries in committee) for a proposal to be accepted
uint constant QUORUM_SIZE = 90;
// Number of periods ahead of current period, which the contract
// is able to return the notary of that period
uint constant LOOKAHEAD_LENGTH = 4;

/// Checks if a notary with given shard id and period has been chosen as
/// a committee member to vote for header added on to the main chain
Expand Down Expand Up @@ -172,7 +172,7 @@ contract SMC {
uint _period,
bytes32 _chunkRoot
) public {
require((_shardId >= 0) && (_shardId < SHARD_COUNT));
require((_shardId >= 0) && (_shardId < shardCount));
require(_period == block.number / PERIOD_LENGTH);
require(_period > lastSubmittedCollation[_shardId]);

Expand All @@ -198,7 +198,7 @@ contract SMC {
uint _index,
bytes32 _chunkRoot
) public {
require((_shardId >= 0) && (_shardId < SHARD_COUNT));
require((_shardId >= 0) && (_shardId < shardCount));
require(_period == block.number / PERIOD_LENGTH);
require(_period == lastSubmittedCollation[_shardId]);
require(_index < COMMITTEE_SIZE);
Expand Down
10 changes: 5 additions & 5 deletions sharding/contracts/sharding_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/sharding"
"github.com/ethereum/go-ethereum/sharding/params"
)

type smcTestHelper struct {
Expand Down Expand Up @@ -68,7 +68,7 @@ func deploySMCContract(backend *backends.SimulatedBackend, key *ecdsa.PrivateKey

// fastForward is a helper function to skip through n period.
func (s *smcTestHelper) fastForward(p int) {
for i := 0; i < p*int(sharding.PeriodLength); i++ {
for i := 0; i < p*int(params.DefaultShardConfig.PeriodLength); i++ {
s.backend.Commit()
}
}
Expand Down Expand Up @@ -168,7 +168,7 @@ func (s *smcTestHelper) addHeader(a *testAccount, shard *big.Int, period *big.In

// Filter SMC logs by headerAdded.
shardIndex := []*big.Int{shard}
logPeriod := uint64(period.Int64() * sharding.PeriodLength)
logPeriod := uint64(period.Int64() * params.DefaultShardConfig.PeriodLength)
log, err := s.smc.FilterHeaderAdded(&bind.FilterOpts{Start: logPeriod}, shardIndex)
if err != nil {
return err
Expand Down Expand Up @@ -200,7 +200,7 @@ func (s *smcTestHelper) submitVote(a *testAccount, shard *big.Int, period *big.I
}
// Filter SMC logs by submitVote.
shardIndex := []*big.Int{shard}
logPeriod := uint64(period.Int64() * sharding.PeriodLength)
logPeriod := uint64(period.Int64() * params.DefaultShardConfig.PeriodLength)
log, err := s.smc.FilterVoteSubmitted(&bind.FilterOpts{Start: logPeriod}, shardIndex)
if err != nil {
return err
Expand Down Expand Up @@ -377,7 +377,7 @@ func TestNotaryRelease(t *testing.T) {
}

// Fast forward until lockup ends.
s.fastForward(int(sharding.NotaryLockupLength + 1))
s.fastForward(int(params.DefaultShardConfig.NotaryLockupLength + 1))

// Notary 0 releases.
_, err = s.smc.ReleaseNotary(s.testAccounts[0].txOpts)
Expand Down
11 changes: 11 additions & 0 deletions sharding/mainchain/smc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Client interface {
SetDepositFlag(deposit bool)
DataDirPath() string
Sign(hash common.Hash) ([]byte, error)
GetShardCount() (int64, error)
}

// SMCClient defines a struct that interacts with a
Expand Down Expand Up @@ -225,3 +226,13 @@ func (s *SMCClient) Sign(hash common.Hash) ([]byte, error) {
account := s.Account()
return s.keystore.SignHash(*account, hash.Bytes())
}

// GetShardCount gets the count of the total shards
// currently operating in the sharded universe.
func (s *SMCClient) GetShardCount() (int64, error) {
shardCount, err := s.SMCCaller().ShardCount(&bind.CallOpts{})
if err != nil {
return 0, err
}
return shardCount.Int64(), nil
}
10 changes: 5 additions & 5 deletions sharding/mainchain/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/sharding"
"github.com/ethereum/go-ethereum/sharding/contracts"
"github.com/ethereum/go-ethereum/sharding/params"
)

// dialRPC endpoint to node.
Expand All @@ -24,16 +24,16 @@ func dialRPC(endpoint string) (*rpc.Client, error) {
// initSMC initializes the sharding manager contract bindings.
// If the SMC does not exist, it will be deployed.
func initSMC(s *SMCClient) (*contracts.SMC, error) {
b, err := s.client.CodeAt(context.Background(), sharding.ShardingManagerAddress, nil)
b, err := s.client.CodeAt(context.Background(), params.DefaultShardConfig.SMCAddress, nil)
if err != nil {
return nil, fmt.Errorf("unable to get contract code at %s: %v", sharding.ShardingManagerAddress.Hex(), err)
return nil, fmt.Errorf("unable to get contract code at %s: %v", params.DefaultShardConfig.SMCAddress.Hex(), err)
}

// Deploy SMC for development only.
// TODO: Separate contract deployment from the sharding node. It would only need to be deployed
// once on the mainnet, so this code would not need to ship with the node.
if len(b) == 0 {
log.Info(fmt.Sprintf("No sharding manager contract found at %s. Deploying new contract.", sharding.ShardingManagerAddress.Hex()))
log.Info(fmt.Sprintf("No sharding manager contract found at %s. Deploying new contract.", params.DefaultShardConfig.SMCAddress.Hex()))

txOps, err := s.CreateTXOpts(big.NewInt(0))
if err != nil {
Expand All @@ -56,5 +56,5 @@ func initSMC(s *SMCClient) (*contracts.SMC, error) {
return contract, nil
}

return contracts.NewSMC(sharding.ShardingManagerAddress, s.client)
return contracts.NewSMC(params.DefaultShardConfig.SMCAddress, s.client)
}
15 changes: 9 additions & 6 deletions sharding/node/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/ethereum/go-ethereum/sharding/database"
"github.com/ethereum/go-ethereum/sharding/params"
"github.com/ethereum/go-ethereum/sharding/txpool"
cli "gopkg.in/urfave/cli.v1"
"gopkg.in/urfave/cli.v1"
)

const shardChainDbName = "shardchaindata"
Expand All @@ -38,7 +38,7 @@ const shardChainDbName = "shardchaindata"
// it contains APIs and fields that handle the different components of the sharded
// Ethereum network.
type ShardEthereum struct {
shardConfig *params.ShardConfig // Holds necessary information to configure shards.
shardConfig *params.ShardConfig // Holds necessary information to configure shard node.
txPool *txpool.ShardTXPool // Defines the sharding-specific txpool. To be designed.
actor sharding.Actor // Either notary, proposer, or observer.
shardChainDb ethdb.Database // Access to the persistent db to store shard data.
Expand Down Expand Up @@ -89,6 +89,9 @@ func New(ctx *cli.Context) (*ShardEthereum, error) {
// Adds the initialized SMCClient to the ShardEthereum instance.
shardEthereum.smcClient = smcClient

// Configure shardConfig by loading the default.
shardEthereum.shardConfig = params.DefaultShardConfig

// Adds the initialized shardChainDb to the ShardEthereum instance.
shardEthereum.shardChainDb = shardChainDb

Expand All @@ -100,7 +103,7 @@ func New(ctx *cli.Context) (*ShardEthereum, error) {
return nil, err
}

if err := shardEthereum.registerActorService(actorFlag, shardIDFlag); err != nil {
if err := shardEthereum.registerActorService(shardEthereum.shardConfig, actorFlag, shardIDFlag); err != nil {
return nil, err
}

Expand Down Expand Up @@ -210,18 +213,18 @@ func (s *ShardEthereum) registerTXPool(actor string) error {
}

// Registers the actor according to CLI flags. Either notary/proposer/observer.
func (s *ShardEthereum) registerActorService(actor string, shardID int) error {
func (s *ShardEthereum) registerActorService(config *params.ShardConfig, actor string, shardID int) error {
return s.Register(func(ctx *sharding.ServiceContext) (sharding.Service, error) {

var p2p *shardp2p.Server
ctx.RetrieveService(&p2p)

if actor == "notary" {
return notary.NewNotary(s.smcClient, p2p, s.shardChainDb)
return notary.NewNotary(config, s.smcClient, p2p, s.shardChainDb)
} else if actor == "proposer" {
var txPool *txpool.ShardTXPool
ctx.RetrieveService(&txPool)
return proposer.NewProposer(s.smcClient, p2p, txPool, s.shardChainDb, shardID)
return proposer.NewProposer(config, s.smcClient, p2p, txPool, s.shardChainDb, shardID)
}
return observer.NewObserver(p2p, s.shardChainDb, shardID)
})
Expand Down
14 changes: 9 additions & 5 deletions sharding/notary/notary.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/sharding"
"github.com/ethereum/go-ethereum/sharding/mainchain"
sparams "github.com/ethereum/go-ethereum/sharding/params"
)

// SubscribeBlockHeaders checks incoming block headers and determines if
Expand Down Expand Up @@ -55,7 +55,11 @@ func subscribeBlockHeaders(client mainchain.Client) error {
// conditions are met.
func checkSMCForNotary(client mainchain.Client, head *types.Header) error {
log.Info("Checking if we are an eligible collation notary for a shard...")
for s := int64(0); s < sharding.ShardCount; s++ {
shardCount, err := client.GetShardCount()
if err != nil {
return fmt.Errorf("can't get shard count from smc: %v", err)
}
for s := int64(0); s < shardCount; s++ {
// Checks if we are an eligible notary according to the SMC.
addr, err := client.SMCCaller().GetNotaryInCommittee(&bind.CallOpts{}, big.NewInt(s))

Expand Down Expand Up @@ -132,7 +136,7 @@ func submitCollation(shardID int64) error {
// joinNotaryPool checks if the deposit flag is true and the account is a
// notary in the SMC. If the account is not in the set, it will deposit ETH
// into contract.
func joinNotaryPool(client mainchain.Client) error {
func joinNotaryPool(config *sparams.ShardConfig, client mainchain.Client) error {
if !client.DepositFlag() {
return errors.New("joinNotaryPool called when deposit flag was not set")
}
Expand All @@ -142,7 +146,7 @@ func joinNotaryPool(client mainchain.Client) error {
}

log.Info("Joining notary pool")
txOps, err := client.CreateTXOpts(sharding.NotaryDeposit)
txOps, err := client.CreateTXOpts(config.NotaryDeposit)
if err != nil {
return fmt.Errorf("unable to initiate the deposit transaction: %v", err)
}
Expand All @@ -151,7 +155,7 @@ func joinNotaryPool(client mainchain.Client) error {
if err != nil {
return fmt.Errorf("unable to deposit eth and become a notary: %v", err)
}
log.Info(fmt.Sprintf("Deposited %dETH into contract with transaction hash: %s", new(big.Int).Div(sharding.NotaryDeposit, big.NewInt(params.Ether)), tx.Hash().String()))
log.Info(fmt.Sprintf("Deposited %dETH into contract with transaction hash: %s", new(big.Int).Div(config.NotaryDeposit, big.NewInt(params.Ether)), tx.Hash().String()))

return nil
}
Loading

0 comments on commit cdfec9b

Please sign in to comment.