Skip to content

Commit

Permalink
Add 2s logic for mainnet at epoch 366 (#3474)
Browse files Browse the repository at this point in the history
  • Loading branch information
rlan35 authored Dec 1, 2020
1 parent 6782972 commit 2a722bb
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 31 deletions.
23 changes: 16 additions & 7 deletions internal/configs/sharding/localnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,35 @@ func (ls localnetSchedule) InstanceForEpoch(epoch *big.Int) Instance {
func (ls localnetSchedule) BlocksPerEpochOld() uint64 {
return localnetBlocksPerEpoch
}

func (ls localnetSchedule) BlocksPerEpoch() uint64 {
return localnetBlocksPerEpochV2
}

func (ls localnetSchedule) twoSecondsFirstBlock() uint64 {
if params.LocalnetChainConfig.TwoSecondsEpoch.Uint64() == 0 {
return 0
}
return (params.LocalnetChainConfig.TwoSecondsEpoch.Uint64()-1)*ls.BlocksPerEpochOld() + localnetEpochBlock1
}

func (ls localnetSchedule) CalcEpochNumber(blockNum uint64) *big.Int {
blocks := ls.BlocksPerEpochOld()
var oldEpochNumber uint64
var oldEpochNumber int64
switch {
case blockNum >= localnetEpochBlock1:
oldEpochNumber = uint64((blockNum-localnetEpochBlock1)/blocks) + 1
oldEpochNumber = int64((blockNum-localnetEpochBlock1)/blocks) + 1
default:
oldEpochNumber = 0
}

firstBlock2s := params.LocalnetChainConfig.TwoSecondsEpoch.Uint64()*ls.BlocksPerEpochOld() + 5
firstBlock2s := ls.twoSecondsFirstBlock()

switch {
case oldEpochNumber >= params.LocalnetChainConfig.TwoSecondsEpoch.Uint64():
case params.LocalnetChainConfig.IsTwoSeconds(big.NewInt(oldEpochNumber)):
return big.NewInt(int64((blockNum-firstBlock2s)/ls.BlocksPerEpoch() + params.LocalnetChainConfig.TwoSecondsEpoch.Uint64()))
default: // genesis
return big.NewInt(int64(oldEpochNumber))
return big.NewInt(oldEpochNumber)
}
}

Expand All @@ -75,7 +84,7 @@ func (ls localnetSchedule) IsLastBlock(blockNum uint64) bool {
case blockNum == localnetEpochBlock1-1:
return true
default:
firstBlock2s := params.LocalnetChainConfig.TwoSecondsEpoch.Uint64()*ls.BlocksPerEpochOld() + 5
firstBlock2s := ls.twoSecondsFirstBlock()
switch {
case blockNum >= firstBlock2s:
return ((blockNum-firstBlock2s)%ls.BlocksPerEpoch() == ls.BlocksPerEpoch()-1)
Expand All @@ -91,7 +100,7 @@ func (ls localnetSchedule) EpochLastBlock(epochNum uint64) uint64 {
case epochNum == 0:
return localnetEpochBlock1 - 1
default:
firstBlock2s := params.LocalnetChainConfig.TwoSecondsEpoch.Uint64()*ls.BlocksPerEpochOld() + 5
firstBlock2s := ls.twoSecondsFirstBlock()
switch {
case params.LocalnetChainConfig.IsTwoSeconds(big.NewInt(int64(epochNum))):
return firstBlock2s - 1 + ls.BlocksPerEpoch()*(epochNum-params.LocalnetChainConfig.TwoSecondsEpoch.Uint64()+1)
Expand Down
84 changes: 61 additions & 23 deletions internal/configs/sharding/mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package shardingconfig
import (
"math/big"

"github.com/harmony-one/harmony/internal/params"

"github.com/harmony-one/harmony/numeric"

"github.com/harmony-one/harmony/internal/genesis"
Expand All @@ -11,6 +13,7 @@ import (
const (
mainnetEpochBlock1 = 344064 // 21 * 2^14
blocksPerEpoch = 16384 // 2^14
blocksPerEpochV2 = 32768 // 2^15

mainnetVdfDifficulty = 50000 // This takes about 100s to finish the vdf

Expand Down Expand Up @@ -53,6 +56,10 @@ type mainnetSchedule struct{}

func (ms mainnetSchedule) InstanceForEpoch(epoch *big.Int) Instance {
switch {
case params.MainnetChainConfig.IsTwoSeconds(epoch):
// Enable 2s block time and change blocks/epoch to 32768
// which happens around 12/08/2020 08:00 PDT
return mainnetV3
case epoch.Cmp(big.NewInt(mainnetV2_2Epoch)) >= 0:
return mainnetV2_2
case epoch.Cmp(big.NewInt(mainnetV2_1Epoch)) >= 0:
Expand Down Expand Up @@ -95,39 +102,69 @@ func (ms mainnetSchedule) InstanceForEpoch(epoch *big.Int) Instance {
}
}

func (ms mainnetSchedule) BlocksPerEpoch() uint64 {
func (ms mainnetSchedule) BlocksPerEpochOld() uint64 {
return blocksPerEpoch
}

func (ms mainnetSchedule) BlocksPerEpoch() uint64 {
return blocksPerEpochV2
}

func (ms mainnetSchedule) twoSecondsFirstBlock() uint64 {
if params.MainnetChainConfig.TwoSecondsEpoch.Uint64() == 0 {
return 0
}
return (params.MainnetChainConfig.TwoSecondsEpoch.Uint64()-1)*ms.BlocksPerEpochOld() + mainnetEpochBlock1
}

func (ms mainnetSchedule) CalcEpochNumber(blockNum uint64) *big.Int {
blocks := ms.BlocksPerEpoch()
var oldEpochNumber int64
switch {
case blockNum >= mainnetEpochBlock1:
return big.NewInt(int64((blockNum-mainnetEpochBlock1)/blocks) + 1)
oldEpochNumber = int64((blockNum-mainnetEpochBlock1)/ms.BlocksPerEpochOld()) + 1
default:
return big.NewInt(0)
oldEpochNumber = 0
}

firstBlock2s := ms.twoSecondsFirstBlock()

switch {
case params.MainnetChainConfig.IsTwoSeconds(big.NewInt(oldEpochNumber)):
return big.NewInt(int64((blockNum-firstBlock2s)/ms.BlocksPerEpoch() + params.MainnetChainConfig.TwoSecondsEpoch.Uint64()))
default: // genesis
return big.NewInt(int64(oldEpochNumber))
}
}

func (ms mainnetSchedule) IsLastBlock(blockNum uint64) bool {
blocks := ms.BlocksPerEpoch()
switch {
case blockNum < mainnetEpochBlock1-1:
return false
case blockNum == mainnetEpochBlock1-1:
return true
default:
return ((blockNum-mainnetEpochBlock1)%blocks == blocks-1)
firstBlock2s := ms.twoSecondsFirstBlock()
switch {
case blockNum >= firstBlock2s:
return ((blockNum-firstBlock2s)%ms.BlocksPerEpoch() == ms.BlocksPerEpoch()-1)
default: // genesis
return ((blockNum-mainnetEpochBlock1)%ms.BlocksPerEpochOld() == ms.BlocksPerEpochOld()-1)
}
}
}

func (ms mainnetSchedule) EpochLastBlock(epochNum uint64) uint64 {
blocks := ms.BlocksPerEpoch()
switch {
case epochNum == 0:
return mainnetEpochBlock1 - 1
default:
return mainnetEpochBlock1 - 1 + blocks*epochNum
firstBlock2s := ms.twoSecondsFirstBlock()
switch {
case params.MainnetChainConfig.IsTwoSeconds(big.NewInt(int64(epochNum))):
return firstBlock2s - 1 + ms.BlocksPerEpoch()*(epochNum-params.MainnetChainConfig.TwoSecondsEpoch.Uint64()+1)
default: // genesis
return mainnetEpochBlock1 - 1 + ms.BlocksPerEpochOld()*epochNum
}
}
}

Expand Down Expand Up @@ -162,21 +199,22 @@ func (ms mainnetSchedule) IsSkippedEpoch(shardID uint32, epoch *big.Int) bool {
return false
}

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), big.NewInt(mainnetV1_3Epoch), big.NewInt(mainnetV1_4Epoch), big.NewInt(mainnetV1_5Epoch), big.NewInt(mainnetV2_0Epoch), big.NewInt(mainnetV2_1Epoch), big.NewInt(mainnetV2_2Epoch)}
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), big.NewInt(mainnetV1_3Epoch), big.NewInt(mainnetV1_4Epoch), big.NewInt(mainnetV1_5Epoch), big.NewInt(mainnetV2_0Epoch), big.NewInt(mainnetV2_1Epoch), big.NewInt(mainnetV2_2Epoch), params.MainnetChainConfig.TwoSecondsEpoch}

var (
mainnetV0 = MustNewInstance(4, 150, 112, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccounts, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch())
mainnetV0_1 = MustNewInstance(4, 152, 112, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV0_1, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch())
mainnetV0_2 = MustNewInstance(4, 200, 148, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV0_2, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch())
mainnetV0_3 = MustNewInstance(4, 210, 148, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV0_3, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch())
mainnetV0_4 = MustNewInstance(4, 216, 148, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV0_4, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch())
mainnetV1 = MustNewInstance(4, 250, 170, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch())
mainnetV1_1 = MustNewInstance(4, 250, 170, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_1, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch())
mainnetV1_2 = MustNewInstance(4, 250, 170, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_2, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch())
mainnetV1_3 = MustNewInstance(4, 250, 170, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_3, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch())
mainnetV1_4 = MustNewInstance(4, 250, 170, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_4, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch())
mainnetV1_5 = MustNewInstance(4, 250, 170, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_5, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch())
mainnetV2_0 = MustNewInstance(4, 250, 170, numeric.MustNewDecFromStr("0.68"), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_5, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch())
mainnetV2_1 = MustNewInstance(4, 250, 130, numeric.MustNewDecFromStr("0.68"), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_5, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch())
mainnetV2_2 = MustNewInstance(4, 250, 90, numeric.MustNewDecFromStr("0.68"), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_5, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch())
mainnetV0 = MustNewInstance(4, 150, 112, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccounts, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpochOld())
mainnetV0_1 = MustNewInstance(4, 152, 112, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV0_1, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpochOld())
mainnetV0_2 = MustNewInstance(4, 200, 148, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV0_2, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpochOld())
mainnetV0_3 = MustNewInstance(4, 210, 148, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV0_3, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpochOld())
mainnetV0_4 = MustNewInstance(4, 216, 148, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV0_4, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpochOld())
mainnetV1 = MustNewInstance(4, 250, 170, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpochOld())
mainnetV1_1 = MustNewInstance(4, 250, 170, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_1, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpochOld())
mainnetV1_2 = MustNewInstance(4, 250, 170, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_2, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpochOld())
mainnetV1_3 = MustNewInstance(4, 250, 170, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_3, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpochOld())
mainnetV1_4 = MustNewInstance(4, 250, 170, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_4, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpochOld())
mainnetV1_5 = MustNewInstance(4, 250, 170, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_5, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpochOld())
mainnetV2_0 = MustNewInstance(4, 250, 170, numeric.MustNewDecFromStr("0.68"), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_5, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpochOld())
mainnetV2_1 = MustNewInstance(4, 250, 130, numeric.MustNewDecFromStr("0.68"), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_5, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpochOld())
mainnetV2_2 = MustNewInstance(4, 250, 90, numeric.MustNewDecFromStr("0.68"), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_5, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpochOld())
mainnetV3 = MustNewInstance(4, 250, 90, numeric.MustNewDecFromStr("0.68"), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_5, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch())
)
147 changes: 147 additions & 0 deletions internal/configs/sharding/shardingconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ func TestMainnetInstanceForEpoch(t *testing.T) {
big.NewInt(54),
mainnetV1_5,
},
{
big.NewInt(365),
mainnetV2_2,
},
{
big.NewInt(366),
mainnetV3,
},
}

for _, test := range tests {
Expand Down Expand Up @@ -86,6 +94,26 @@ func TestCalcEpochNumber(t *testing.T) {
360448,
big.NewInt(2),
},
{
6207973,
big.NewInt(358),
},
{
6324223, // last block before 2s
big.NewInt(365),
},
{
6324224,
big.NewInt(366),
},
{
6389759,
big.NewInt(367),
},
{
6389777,
big.NewInt(368),
},
}

for i, test := range tests {
Expand All @@ -96,6 +124,125 @@ func TestCalcEpochNumber(t *testing.T) {
}
}

func TestIsLastBlock(t *testing.T) {
tests := []struct {
block uint64
result bool
}{
{
0,
false,
},
{
1,
false,
},
{
327679,
false,
},
{
344063,
true,
},
{
344064,
false,
},
{
360447,
true,
},
{
360448,
false,
},
{
6207973,
false,
},
{
6324223, // last block of first 2s epoch
true,
},
{
6324224,
false,
},
{
6356991,
true,
},
{
6356992,
false,
},
{
6389759,
true,
},
}

for i, test := range tests {
ep := MainnetSchedule.IsLastBlock(test.block)
if test.result != ep {
t.Errorf("IsLastBlock error: index %v, got %v, expect %v\n", i, ep, test.result)
}
}
}
func TestEpochLastBlock(t *testing.T) {
tests := []struct {
epoch uint64
lastBlock uint64
}{
{
0,
344063,
},
{
1,
360447,
},
{
2,
376831,
},
{
3,
393215,
},
{
358,
6209535,
},
{
365,
6324223, // last block before 2s
},
{
366,
6356991, // last block of first 2s epoch
},
{
367,
6389759, // last block of second 2s epoch
},
}

for i, test := range tests {
ep := MainnetSchedule.EpochLastBlock(test.epoch)
if test.lastBlock != ep {
t.Errorf("EpochLastBlock error: index %v, got %v, expect %v\n", i, ep, test.lastBlock)
}
}
}

func TestTwoSecondsFirstBlock(t *testing.T) {
if MainnetSchedule.twoSecondsFirstBlock() != 6324224 {
t.Errorf("twoSecondsFirstBlock error: got %v, expect %v\n", MainnetSchedule.twoSecondsFirstBlock(), 6324224)
}
}

func TestGetShardingStructure(t *testing.T) {
shardID := 0
numShard := 4
Expand Down
2 changes: 1 addition & 1 deletion internal/params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (
PreStakingEpoch: big.NewInt(185),
QuickUnlockEpoch: big.NewInt(191),
FiveSecondsEpoch: big.NewInt(230),
TwoSecondsEpoch: big.NewInt(10000), // TBD
TwoSecondsEpoch: big.NewInt(366), // Around Tuesday Dec 8th 2020, 8AM PST
RedelegationEpoch: big.NewInt(290),
EIP155Epoch: big.NewInt(28),
S3Epoch: big.NewInt(28),
Expand Down

0 comments on commit 2a722bb

Please sign in to comment.