Merge pull request #1206 from harmony-ek/flexible_sharding_config
Make sharding configuration flexiblepull/1209/head
commit
21448f14d4
@ -0,0 +1,21 @@ |
||||
package shardingconfig |
||||
|
||||
import ( |
||||
"math/big" |
||||
) |
||||
|
||||
type fixedSchedule struct { |
||||
instance Instance |
||||
} |
||||
|
||||
// InstanceForEpoch returns the fixed sharding configuration instance regardless
|
||||
// the given epoch.
|
||||
func (s fixedSchedule) InstanceForEpoch(epoch *big.Int) Instance { |
||||
return s.instance |
||||
} |
||||
|
||||
// NewFixedSchedule returns a sharding configuration schedule that uses the
|
||||
// given config instance for all epochs. Useful for testing.
|
||||
func NewFixedSchedule(instance Instance) Schedule { |
||||
return fixedSchedule{instance: instance} |
||||
} |
@ -0,0 +1,70 @@ |
||||
package shardingconfig |
||||
|
||||
import "github.com/harmony-one/harmony/internal/ctxerror" |
||||
|
||||
type instance struct { |
||||
numShards uint32 |
||||
numNodesPerShard int |
||||
numHarmonyOperatedNodesPerShard int |
||||
} |
||||
|
||||
// NewInstance creates and validates a new sharding configuration based
|
||||
// upon given parameters.
|
||||
func NewInstance( |
||||
numShards uint32, numNodesPerShard, numHarmonyOperatedNodesPerShard int, |
||||
) (Instance, error) { |
||||
if numShards < 1 { |
||||
return nil, ctxerror.New("sharding config must have at least one shard", |
||||
"numShards", numShards) |
||||
} |
||||
if numNodesPerShard < 1 { |
||||
return nil, ctxerror.New("each shard must have at least one node", |
||||
"numNodesPerShard", numNodesPerShard) |
||||
} |
||||
if numHarmonyOperatedNodesPerShard < 0 { |
||||
return nil, ctxerror.New("Harmony-operated nodes cannot be negative", |
||||
"numHarmonyOperatedNodesPerShard", numHarmonyOperatedNodesPerShard) |
||||
} |
||||
if numHarmonyOperatedNodesPerShard > numNodesPerShard { |
||||
return nil, ctxerror.New(""+ |
||||
"number of Harmony-operated nodes cannot exceed "+ |
||||
"overall number of nodes per shard", |
||||
"numHarmonyOperatedNodesPerShard", numHarmonyOperatedNodesPerShard, |
||||
"numNodesPerShard", numNodesPerShard) |
||||
} |
||||
return instance{ |
||||
numShards: numShards, |
||||
numNodesPerShard: numNodesPerShard, |
||||
numHarmonyOperatedNodesPerShard: numHarmonyOperatedNodesPerShard, |
||||
}, nil |
||||
} |
||||
|
||||
// MustNewInstance creates a new sharding configuration based upon
|
||||
// given parameters. It panics if parameter validation fails.
|
||||
// It is intended to be used for static initialization.
|
||||
func MustNewInstance( |
||||
numShards uint32, numNodesPerShard, numHarmonyOperatedNodesPerShard int, |
||||
) Instance { |
||||
sc, err := NewInstance( |
||||
numShards, numNodesPerShard, numHarmonyOperatedNodesPerShard) |
||||
if err != nil { |
||||
panic(err) |
||||
} |
||||
return sc |
||||
} |
||||
|
||||
// NumShards returns the number of shards in the network.
|
||||
func (sc instance) NumShards() uint32 { |
||||
return sc.numShards |
||||
} |
||||
|
||||
// NumNodesPerShard returns number of nodes in each shard.
|
||||
func (sc instance) NumNodesPerShard() int { |
||||
return sc.numNodesPerShard |
||||
} |
||||
|
||||
// NumHarmonyOperatedNodesPerShard returns number of nodes in each shard
|
||||
// that are operated by Harmony.
|
||||
func (sc instance) NumHarmonyOperatedNodesPerShard() int { |
||||
return sc.numHarmonyOperatedNodesPerShard |
||||
} |
@ -0,0 +1,24 @@ |
||||
package shardingconfig |
||||
|
||||
import "math/big" |
||||
|
||||
// MainnetSchedule is the mainnet sharding configuration schedule.
|
||||
var MainnetSchedule mainnetSchedule |
||||
|
||||
type mainnetSchedule struct{} |
||||
|
||||
func (mainnetSchedule) InstanceForEpoch(epoch *big.Int) Instance { |
||||
switch { |
||||
//case epoch.Cmp(big.NewInt(1000)) >= 0:
|
||||
// return mainnet6400
|
||||
//case epoch.Cmp(big.NewInt(100)) >= 0:
|
||||
// return mainnetV2
|
||||
default: // genesis
|
||||
return mainnetV0 |
||||
} |
||||
} |
||||
|
||||
var mainnetV0 = MustNewInstance(4, 150, 112) |
||||
|
||||
//var mainnetV2 = MustNewInstance(8, 200, 100)
|
||||
//var mainnet6400 = MustNewInstance(16, 400, 50)
|
@ -0,0 +1,26 @@ |
||||
// Package shardingconfig defines types and utilities that deal with Harmony
|
||||
// sharding configuration schedule.
|
||||
package shardingconfig |
||||
|
||||
import ( |
||||
"math/big" |
||||
) |
||||
|
||||
// Schedule returns the sharding configuration instance for the given
|
||||
// epoch.
|
||||
type Schedule interface { |
||||
InstanceForEpoch(epoch *big.Int) Instance |
||||
} |
||||
|
||||
// Instance is one sharding configuration instance.
|
||||
type Instance interface { |
||||
// NumShards returns the number of shards in the network.
|
||||
NumShards() uint32 |
||||
|
||||
// NumNodesPerShard returns number of nodes in each shard.
|
||||
NumNodesPerShard() int |
||||
|
||||
// NumHarmonyOperatedNodesPerShard returns number of nodes in each shard
|
||||
// that are operated by Harmony.
|
||||
NumHarmonyOperatedNodesPerShard() int |
||||
} |
@ -0,0 +1,18 @@ |
||||
package shardingconfig |
||||
|
||||
import "math/big" |
||||
|
||||
// TestnetSchedule is the long-running public testnet sharding
|
||||
// configuration schedule.
|
||||
var TestnetSchedule testnetSchedule |
||||
|
||||
type testnetSchedule struct{} |
||||
|
||||
func (testnetSchedule) InstanceForEpoch(epoch *big.Int) Instance { |
||||
switch { |
||||
default: // genesis
|
||||
return testnetV0 |
||||
} |
||||
} |
||||
|
||||
var testnetV0 = MustNewInstance(2, 150, 150) |
Loading…
Reference in new issue