From 2fcf9ed8b8afcc63bc56e7d6379379add585c565 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Wed, 4 Sep 2019 16:55:33 -0700 Subject: [PATCH] add GetShardingStructure interface and implmentation for each instance --- internal/configs/sharding/fixedschedule.go | 5 ++++ internal/configs/sharding/localnet.go | 10 ++++++++ internal/configs/sharding/mainnet.go | 10 ++++++++ internal/configs/sharding/pangaea.go | 12 ++++++++++ internal/configs/sharding/shardingconfig.go | 18 ++++++++++++++ internal/configs/sharding/testnet.go | 10 ++++++++ internal/hmyapi/blockchain.go | 26 +++------------------ 7 files changed, 68 insertions(+), 23 deletions(-) diff --git a/internal/configs/sharding/fixedschedule.go b/internal/configs/sharding/fixedschedule.go index 8d20fde35..44befcd89 100644 --- a/internal/configs/sharding/fixedschedule.go +++ b/internal/configs/sharding/fixedschedule.go @@ -90,6 +90,11 @@ func (s fixedSchedule) GetNetworkID() NetworkID { return DevNet } +// GetShardingStructure is the sharding structure for fixed schedule. +func (fixedSchedule) GetShardingStructure(numShard, shardID int) []map[string]interface{} { + return genShardingStructure(numShard, shardID, TestNetHTTPPattern, TestNetHTTPPattern) +} + // NewFixedSchedule returns a sharding configuration schedule that uses the // given config instance for all epochs. Useful for testing. func NewFixedSchedule(instance Instance) Schedule { diff --git a/internal/configs/sharding/localnet.go b/internal/configs/sharding/localnet.go index 05b0fbec8..41a870bdd 100644 --- a/internal/configs/sharding/localnet.go +++ b/internal/configs/sharding/localnet.go @@ -32,6 +32,11 @@ const ( localnetMaxTxPoolSizeLimit = 8000 localnetMaxNumTxsPerBlockLimit = 1000 localnetRecentTxDuration = time.Hour + + // LocalNetHTTPPattern is the http pattern for mainnet. + LocalNetHTTPPattern = "http://s%d.t.hmny.io:9500" + // LocalNetWSPattern is the websocket pattern for mainnet. + LocalNetWSPattern = "ws://s%d.t.hmny.io:9800" ) func (localnetSchedule) InstanceForEpoch(epoch *big.Int) Instance { @@ -126,6 +131,11 @@ func (ls localnetSchedule) GetNetworkID() NetworkID { return LocalNet } +// GetShardingStructure is the sharding structure for localnet. +func (ls localnetSchedule) GetShardingStructure(numShard, shardID int) []map[string]interface{} { + return genShardingStructure(numShard, shardID, LocalNetHTTPPattern, LocalNetWSPattern) +} + var localnetReshardingEpoch = []*big.Int{big.NewInt(0), big.NewInt(localnetV1Epoch), big.NewInt(localnetV2Epoch)} var localnetV0 = MustNewInstance(2, 7, 5, genesis.LocalHarmonyAccounts, genesis.LocalFnAccounts, localnetReshardingEpoch) diff --git a/internal/configs/sharding/mainnet.go b/internal/configs/sharding/mainnet.go index a5f3d3d65..d077b8a5b 100644 --- a/internal/configs/sharding/mainnet.go +++ b/internal/configs/sharding/mainnet.go @@ -33,6 +33,11 @@ const ( mainnetMaxTxPoolSizeLimit = 8000 mainnetMaxNumTxsPerBlockLimit = 1000 mainnetRecentTxDuration = time.Hour + + // MainNetHTTPPattern is the http pattern for mainnet. + MainNetHTTPPattern = "http://s%d.t.hmny.io:9500" + // MainNetWSPattern is the websocket pattern for mainnet. + MainNetWSPattern = "ws://s%d.t.hmny.io:9800" ) // MainnetSchedule is the mainnet sharding configuration schedule. @@ -149,6 +154,11 @@ func (ms mainnetSchedule) GetNetworkID() NetworkID { return MainNet } +// GetShardingStructure is the sharding structure for mainnet. +func (ms mainnetSchedule) GetShardingStructure(numShard, shardID int) []map[string]interface{} { + return genShardingStructure(numShard, shardID, MainNetHTTPPattern, MainNetWSPattern) +} + var mainnetReshardingEpoch = []*big.Int{big.NewInt(0), big.NewInt(mainnetV0_1Epoch), big.NewInt(mainnetV0_2Epoch), big.NewInt(mainnetV0_3Epoch), big.NewInt(mainnetV0_4Epoch), big.NewInt(mainnetV1Epoch), big.NewInt(mainnetV1_1Epoch), big.NewInt(mainnetV1_2Epoch)} var mainnetV0 = MustNewInstance(4, 150, 112, genesis.HarmonyAccounts, genesis.FoundationalNodeAccounts, mainnetReshardingEpoch) diff --git a/internal/configs/sharding/pangaea.go b/internal/configs/sharding/pangaea.go index 68cb11a90..5be9831f3 100644 --- a/internal/configs/sharding/pangaea.go +++ b/internal/configs/sharding/pangaea.go @@ -10,6 +10,13 @@ import ( "github.com/harmony-one/harmony/internal/genesis" ) +const ( + // PangaeaHTTPPattern is the http pattern for pangaea. + PangaeaHTTPPattern = "http://s%d.pga.hmny.io:9500" + // PangaeaWSPattern is the websocket pattern for pangaea. + PangaeaWSPattern = "ws://s%d.pga.hmny.io:9800" +) + // PangaeaSchedule is the Pangaea sharding configuration schedule. var PangaeaSchedule pangaeaSchedule @@ -89,3 +96,8 @@ func (ps pangaeaSchedule) TxsThrottleConfig() *TxsThrottleConfig { func (pangaeaSchedule) GetNetworkID() NetworkID { return Pangaea } + +// GetShardingStructure is the sharding structure for mainnet. +func (pangaeaSchedule) GetShardingStructure(numShard, shardID int) []map[string]interface{} { + return genShardingStructure(numShard, shardID, PangaeaHTTPPattern, PangaeaWSPattern) +} diff --git a/internal/configs/sharding/shardingconfig.go b/internal/configs/sharding/shardingconfig.go index 25fded6a3..56fa55d96 100644 --- a/internal/configs/sharding/shardingconfig.go +++ b/internal/configs/sharding/shardingconfig.go @@ -3,6 +3,7 @@ package shardingconfig import ( + "fmt" "math/big" "time" @@ -56,6 +57,9 @@ type Schedule interface { // GetNetworkID() return networkID type. GetNetworkID() NetworkID + + // GetShardingStructure returns sharding structure. + GetShardingStructure(int, int) []map[string]interface{} } // Instance is one sharding configuration instance. @@ -124,3 +128,17 @@ type TxsThrottleConfig struct { // Max total number of transactions allowed to be processed per block MaxNumTxsPerBlockLimit int } + +// genShardingStructure return sharding structure, given shard number and its patterns. +func genShardingStructure(shardNum, shardID int, httpPattern, wsPattern string) []map[string]interface{} { + res := []map[string]interface{}{} + for i := 0; i < shardNum; i++ { + res = append(res, map[string]interface{}{ + "current": int(shardID) == i, + "shardID": i, + "http": fmt.Sprintf(httpPattern, i), + "ws": fmt.Sprintf(wsPattern, i), + }) + } + return res +} diff --git a/internal/configs/sharding/testnet.go b/internal/configs/sharding/testnet.go index 57d159630..f2b05bd05 100644 --- a/internal/configs/sharding/testnet.go +++ b/internal/configs/sharding/testnet.go @@ -30,6 +30,11 @@ const ( testnetMaxTxPoolSizeLimit = 8000 testnetMaxNumTxsPerBlockLimit = 1000 testnetRecentTxDuration = time.Hour + + // TestNetHTTPPattern is the http pattern for testnet. + TestNetHTTPPattern = "http://s%d.b.hmny.io:9500" + // TestNetWSPattern is the websocket pattern for testnet. + TestNetWSPattern = "ws://s%d.s.hmny.io:9800" ) func (testnetSchedule) InstanceForEpoch(epoch *big.Int) Instance { @@ -125,6 +130,11 @@ func (ts testnetSchedule) GetNetworkID() NetworkID { return TestNet } +// GetShardingStructure is the sharding structure for testnet. +func (ts testnetSchedule) GetShardingStructure(numShard, shardID int) []map[string]interface{} { + return genShardingStructure(numShard, shardID, TestNetHTTPPattern, TestNetWSPattern) +} + var testnetReshardingEpoch = []*big.Int{big.NewInt(0), big.NewInt(testnetV1Epoch), big.NewInt(testnetV2Epoch)} var testnetV0 = MustNewInstance(2, 150, 150, genesis.TNHarmonyAccounts, genesis.TNFoundationalAccounts, testnetReshardingEpoch) diff --git a/internal/hmyapi/blockchain.go b/internal/hmyapi/blockchain.go index 049cd8fb7..a99675c21 100644 --- a/internal/hmyapi/blockchain.go +++ b/internal/hmyapi/blockchain.go @@ -17,7 +17,6 @@ import ( "github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/core/vm" internal_common "github.com/harmony-one/harmony/internal/common" - shardingconfig "github.com/harmony-one/harmony/internal/configs/sharding" "github.com/harmony-one/harmony/internal/utils" ) @@ -90,29 +89,10 @@ func GenShardingStructure(shardNum, shardID uint32, httpPattern, wsPattern strin func (s *PublicBlockChainAPI) GetShardingStructure(ctx context.Context) ([]map[string]interface{}, error) { // Get header and number of shards. header := s.b.CurrentBlock().Header() - numShard := core.ShardingSchedule.InstanceForEpoch(header.Epoch).NumShards() + numShard := core.ShardingSchedule.InstanceForEpoch(header.Epoch()).NumShards() + // Return shareding structure for each case. - if core.ShardingSchedule.GetNetworkID() == shardingconfig.MainNet { - s.b.CurrentBlock().Header() - return GenShardingStructure(numShard, s.b.GetShardID(), MainNetHTTPPattern, MainNetWSPattern), nil - } else if core.ShardingSchedule.GetNetworkID() == shardingconfig.TestNet { - return GenShardingStructure(numShard, s.b.GetShardID(), TestNetHTTPPattern, TestNetWSPattern), nil - } else { - return []map[string]interface{}{ - map[string]interface{}{ - "current": s.b.GetShardID() == 0, - "shardID": 0, - "http": "http://127.0.0.1:9500", - "ws": "ws://127.0.0.1:9800", - }, - map[string]interface{}{ - "current": s.b.GetShardID() == 1, - "shardID": 1, - "http": "http://127.0.0.1:9501", - "ws": "ws://127.0.0.1:9801", - }, - }, nil - } + return core.ShardingSchedule.GetShardingStructure(int(numShard), int(s.b.GetShardID())), nil } // GetCode returns the code stored at the given address in the state for the given block number.