Merge pull request #1237 from LeoHChen/dynamic_epoch_configuration

Dynamic epoch configuration
pull/1246/head
Leo Chen 5 years ago committed by GitHub
commit 447460667b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      consensus/consensus_v2.go
  2. 2
      core/blockchain.go
  3. 4
      core/resharding.go
  4. 9
      internal/configs/sharding/fixedschedule.go
  5. 25
      internal/configs/sharding/localnet.go
  6. 27
      internal/configs/sharding/mainnet.go
  7. 6
      internal/configs/sharding/shardingconfig.go
  8. 25
      internal/configs/sharding/testnet.go

@ -775,9 +775,9 @@ func (consensus *Consensus) onCommitted(msg *msg_pb.Message) {
return return
} }
// check whether the block is the last block of epoch // check whether the block is the last block of epoch
if recvMsg.BlockNum%core.ShardingSchedule.BlocksPerEpoch() == core.ShardingSchedule.BlocksPerEpoch()-1 { if core.ShardingSchedule.IsLastBlock(recvMsg.BlockNum) {
epoch := recvMsg.BlockNum / core.ShardingSchedule.BlocksPerEpoch() epoch := core.ShardingSchedule.CalcEpochNumber(recvMsg.BlockNum)
nextEpoch := new(big.Int).Add(big.NewInt(int64(epoch)), common.Big1) nextEpoch := new(big.Int).Add(epoch, common.Big1)
pubKeys := core.GetPublicKeys(nextEpoch, consensus.ShardID) pubKeys := core.GetPublicKeys(nextEpoch, consensus.ShardID)
if len(pubKeys) == 0 { if len(pubKeys) == 0 {
consensus.getLogger().Info().Msg("[OnCommitted] PublicKeys is Empty, Cannot update public keys") consensus.getLogger().Info().Msg("[OnCommitted] PublicKeys is Empty, Cannot update public keys")

@ -230,7 +230,7 @@ func IsEpochBlock(block *types.Block) bool {
// IsEpochLastBlock returns whether this block is the last block of an epoch. // IsEpochLastBlock returns whether this block is the last block of an epoch.
func IsEpochLastBlock(block *types.Block) bool { func IsEpochLastBlock(block *types.Block) bool {
return block.NumberU64()%ShardingSchedule.BlocksPerEpoch() == ShardingSchedule.BlocksPerEpoch()-1 return ShardingSchedule.IsLastBlock(block.NumberU64())
} }
func (bc *BlockChain) getProcInterrupt() bool { func (bc *BlockChain) getProcInterrupt() bool {

@ -135,6 +135,7 @@ func Shuffle(list []types.NodeID) {
} }
// GetBlockNumberFromEpoch calculates the block number where epoch sharding information is stored // GetBlockNumberFromEpoch calculates the block number where epoch sharding information is stored
// TODO lc - use ShardingSchedule function
func GetBlockNumberFromEpoch(epoch uint64) uint64 { func GetBlockNumberFromEpoch(epoch uint64) uint64 {
number := epoch * ShardingSchedule.BlocksPerEpoch() // currently we use the first block in each epoch number := epoch * ShardingSchedule.BlocksPerEpoch() // currently we use the first block in each epoch
return number return number
@ -142,13 +143,14 @@ func GetBlockNumberFromEpoch(epoch uint64) uint64 {
// GetLastBlockNumberFromEpoch calculates the last block number for the given // GetLastBlockNumberFromEpoch calculates the last block number for the given
// epoch. TODO ek – this is a temp hack. // epoch. TODO ek – this is a temp hack.
// TODO lc - use ShardingSchedule function
func GetLastBlockNumberFromEpoch(epoch uint64) uint64 { func GetLastBlockNumberFromEpoch(epoch uint64) uint64 {
return (epoch+1)*ShardingSchedule.BlocksPerEpoch() - 1 return (epoch+1)*ShardingSchedule.BlocksPerEpoch() - 1
} }
// GetEpochFromBlockNumber calculates the epoch number the block belongs to // GetEpochFromBlockNumber calculates the epoch number the block belongs to
func GetEpochFromBlockNumber(blockNumber uint64) uint64 { func GetEpochFromBlockNumber(blockNumber uint64) uint64 {
return blockNumber / ShardingSchedule.BlocksPerEpoch() return ShardingSchedule.CalcEpochNumber(blockNumber).Uint64()
} }
// GetShardingStateFromBlockChain will retrieve random seed and shard map from beacon chain for given a epoch // GetShardingStateFromBlockChain will retrieve random seed and shard map from beacon chain for given a epoch

@ -23,6 +23,15 @@ func (s fixedSchedule) BlocksPerEpoch() uint64 {
return VLBPE return VLBPE
} }
func (s fixedSchedule) CalcEpochNumber(blockNum uint64) *big.Int {
return big.NewInt(int64(blockNum / s.BlocksPerEpoch()))
}
func (s fixedSchedule) IsLastBlock(blockNum uint64) bool {
blocks := s.BlocksPerEpoch()
return blockNum%blocks == blocks-1
}
// NewFixedSchedule returns a sharding configuration schedule that uses the // NewFixedSchedule returns a sharding configuration schedule that uses the
// given config instance for all epochs. Useful for testing. // given config instance for all epochs. Useful for testing.
func NewFixedSchedule(instance Instance) Schedule { func NewFixedSchedule(instance Instance) Schedule {

@ -15,6 +15,9 @@ type localnetSchedule struct{}
const ( const (
localnetV1Epoch = 1 localnetV1Epoch = 1
localnetV2Epoch = 2 localnetV2Epoch = 2
localnetEpochBlock1 = 36
twoOne = 11
) )
func (localnetSchedule) InstanceForEpoch(epoch *big.Int) Instance { func (localnetSchedule) InstanceForEpoch(epoch *big.Int) Instance {
@ -29,7 +32,27 @@ func (localnetSchedule) InstanceForEpoch(epoch *big.Int) Instance {
} }
func (localnetSchedule) BlocksPerEpoch() uint64 { func (localnetSchedule) BlocksPerEpoch() uint64 {
return 10 return twoOne
}
func (ls localnetSchedule) CalcEpochNumber(blockNum uint64) *big.Int {
blocks := ls.BlocksPerEpoch()
switch {
case blockNum >= localnetEpochBlock1:
return big.NewInt(int64((blockNum-localnetEpochBlock1)/blocks) + 1)
default:
return big.NewInt(0)
}
}
func (ls localnetSchedule) IsLastBlock(blockNum uint64) bool {
blocks := ls.BlocksPerEpoch()
switch {
case blockNum == localnetEpochBlock1-1:
return true
default:
return ((blockNum-localnetEpochBlock1)%blocks == blocks-1)
}
} }
var localnetReshardingEpoch = []*big.Int{big.NewInt(0), big.NewInt(localnetV1Epoch), big.NewInt(localnetV2Epoch)} var localnetReshardingEpoch = []*big.Int{big.NewInt(0), big.NewInt(localnetV1Epoch), big.NewInt(localnetV2Epoch)}

@ -6,6 +6,11 @@ import (
"github.com/harmony-one/harmony/internal/genesis" "github.com/harmony-one/harmony/internal/genesis"
) )
const (
mainnetEpochBlock1 = 294912 // 18 * 2^14
blocksPerShard = 16384 // 2^14
)
// MainnetSchedule is the mainnet sharding configuration schedule. // MainnetSchedule is the mainnet sharding configuration schedule.
var MainnetSchedule mainnetSchedule var MainnetSchedule mainnetSchedule
@ -23,7 +28,27 @@ func (mainnetSchedule) InstanceForEpoch(epoch *big.Int) Instance {
} }
func (mainnetSchedule) BlocksPerEpoch() uint64 { func (mainnetSchedule) BlocksPerEpoch() uint64 {
return 1000000000000 return blocksPerShard
}
func (ms mainnetSchedule) CalcEpochNumber(blockNum uint64) *big.Int {
blocks := ms.BlocksPerEpoch()
switch {
case blockNum >= mainnetEpochBlock1:
return big.NewInt(int64((blockNum-mainnetEpochBlock1)/blocks) + 1)
default:
return big.NewInt(0)
}
}
func (ms mainnetSchedule) IsLastBlock(blockNum uint64) bool {
blocks := ms.BlocksPerEpoch()
switch {
case blockNum == mainnetEpochBlock1-1:
return true
default:
return ((blockNum-mainnetEpochBlock1)%blocks == blocks-1)
}
} }
var mainnetReshardingEpoch = make([]*big.Int, 0) var mainnetReshardingEpoch = make([]*big.Int, 0)

@ -15,6 +15,12 @@ type Schedule interface {
// BlocksPerEpoch returns the number of blocks per each Epoch // BlocksPerEpoch returns the number of blocks per each Epoch
BlocksPerEpoch() uint64 BlocksPerEpoch() uint64
// CalcEpochNumber returns the epoch number based on the block number
CalcEpochNumber(blockNum uint64) *big.Int
// IsLastBlock check if the block is the last block in the epoch
IsLastBlock(blockNum uint64) bool
} }
// Instance is one sharding configuration instance. // Instance is one sharding configuration instance.

@ -15,6 +15,9 @@ type testnetSchedule struct{}
const ( const (
testnetV1Epoch = 1 testnetV1Epoch = 1
testnetV2Epoch = 2 testnetV2Epoch = 2
testnetEpochBlock1 = 78
threeOne = 111
) )
func (testnetSchedule) InstanceForEpoch(epoch *big.Int) Instance { func (testnetSchedule) InstanceForEpoch(epoch *big.Int) Instance {
@ -30,7 +33,27 @@ func (testnetSchedule) InstanceForEpoch(epoch *big.Int) Instance {
func (testnetSchedule) BlocksPerEpoch() uint64 { func (testnetSchedule) BlocksPerEpoch() uint64 {
// 8 seconds per block, roughly 86400 blocks, around one day // 8 seconds per block, roughly 86400 blocks, around one day
return 60 return threeOne
}
func (ts testnetSchedule) CalcEpochNumber(blockNum uint64) *big.Int {
blocks := ts.BlocksPerEpoch()
switch {
case blockNum >= testnetEpochBlock1:
return big.NewInt(int64((blockNum-testnetEpochBlock1)/blocks) + 1)
default:
return big.NewInt(0)
}
}
func (ts testnetSchedule) IsLastBlock(blockNum uint64) bool {
blocks := ts.BlocksPerEpoch()
switch {
case blockNum == testnetEpochBlock1-1:
return true
default:
return ((blockNum-testnetEpochBlock1)%blocks == blocks-1)
}
} }
var testnetReshardingEpoch = []*big.Int{big.NewInt(0), big.NewInt(testnetV1Epoch), big.NewInt(testnetV2Epoch)} var testnetReshardingEpoch = []*big.Int{big.NewInt(0), big.NewInt(testnetV1Epoch), big.NewInt(testnetV2Epoch)}

Loading…
Cancel
Save