From 60e42232a9e86af29050b8694ad6b9ae0521da8d Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Tue, 1 Dec 2020 00:00:42 -0800 Subject: [PATCH] Change blocks per epoch for testnet. (#3472) * Add 3s block time and change of block reward * fixes * add new blocks per epoch for testnet * remove unused code * fix 2s changing logic * enable 2s at epoch 73000 at testnet --- cmd/harmony/main.go | 2 +- consensus/consensus_service.go | 6 +-- hmy/staking.go | 8 ++-- internal/chain/reward.go | 8 ++-- internal/configs/sharding/instance.go | 8 ++-- internal/configs/sharding/localnet.go | 6 +-- internal/configs/sharding/mainnet.go | 28 ++++++------ internal/configs/sharding/pangaea.go | 4 +- internal/configs/sharding/partner.go | 4 +- internal/configs/sharding/shardingconfig.go | 3 -- internal/configs/sharding/stress.go | 4 +- internal/configs/sharding/testnet.go | 50 +++++++++++++++++---- internal/params/config.go | 28 ++++++------ node/node_handler.go | 7 ++- rpc/blockchain.go | 9 ++-- staking/network/reward.go | 8 ++-- staking/network/reward_test.go | 6 +-- 17 files changed, 109 insertions(+), 80 deletions(-) diff --git a/cmd/harmony/main.go b/cmd/harmony/main.go index 4917d3013..a6a0cbfc7 100644 --- a/cmd/harmony/main.go +++ b/cmd/harmony/main.go @@ -444,7 +444,7 @@ func nodeconfigSetShardSchedule(config harmonyConfig) { } devnetConfig, err := shardingconfig.NewInstance( - uint32(dnConfig.NumShards), dnConfig.ShardSize, dnConfig.HmyNodeSize, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccounts, nil, shardingconfig.VLBPE) + uint32(dnConfig.NumShards), dnConfig.ShardSize, dnConfig.HmyNodeSize, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccounts, nil, shardingconfig.VLBPE, shardingconfig.VLBPE) if err != nil { _, _ = fmt.Fprintf(os.Stderr, "ERROR invalid devnet sharding config: %s", err) diff --git a/consensus/consensus_service.go b/consensus/consensus_service.go index 3507174cb..d4980f4e4 100644 --- a/consensus/consensus_service.go +++ b/consensus/consensus_service.go @@ -307,9 +307,9 @@ func (consensus *Consensus) UpdateConsensusInformation() Mode { consensus.BlockPeriod = 5 * time.Second - // Enable 3s block time at the threeSecondsEpoch - if consensus.Blockchain.Config().IsThreeSeconds(curEpoch) { - consensus.BlockPeriod = 3 * time.Second + // Enable 2s block time at the twoSecondsEpoch + if consensus.Blockchain.Config().IsTwoSeconds(nextEpoch) { + consensus.BlockPeriod = 2 * time.Second } isFirstTimeStaking := consensus.Blockchain.Config().IsStaking(nextEpoch) && diff --git a/hmy/staking.go b/hmy/staking.go index 8de76f985..ae094b8bb 100644 --- a/hmy/staking.go +++ b/hmy/staking.go @@ -309,10 +309,10 @@ func (hmy *Harmony) GetValidatorInformation( computed := availability.ComputeCurrentSigning( snapshot.Validator, wrapper, ) - beaconChainBlocks := uint64( - hmy.BeaconChain.CurrentBlock().Header().Number().Int64(), - ) % shard.Schedule.BlocksPerEpoch() - computed.BlocksLeftInEpoch = shard.Schedule.BlocksPerEpoch() - beaconChainBlocks + + lastBlockOfEpoch := shard.Schedule.EpochLastBlock(hmy.BeaconChain.CurrentBlock().Header().Epoch().Uint64()) + + computed.BlocksLeftInEpoch = lastBlockOfEpoch - hmy.BeaconChain.CurrentBlock().Header().Number().Uint64() if defaultReply.CurrentlyInCommittee { defaultReply.Performance = &staking.CurrentEpochPerformance{ diff --git a/internal/chain/reward.go b/internal/chain/reward.go index 189103f31..9dfb0eec5 100644 --- a/internal/chain/reward.go +++ b/internal/chain/reward.go @@ -157,13 +157,13 @@ func AccumulateRewardsAndCountSigs( } } } - if bc.Config().IsThreeSeconds(header.Epoch()) { - defaultReward = network.ThreeSecondsBaseStakedReward + if bc.Config().IsTwoSeconds(header.Epoch()) { + defaultReward = network.TwoSecondsBaseStakedReward } } else { // Mainnet (other nets): - if bc.Config().IsThreeSeconds(header.Epoch()) { - defaultReward = network.ThreeSecondsBaseStakedReward + if bc.Config().IsTwoSeconds(header.Epoch()) { + defaultReward = network.TwoSecondsBaseStakedReward } else if bc.Config().IsFiveSeconds(header.Epoch()) { defaultReward = network.FiveSecondsBaseStakedReward } diff --git a/internal/configs/sharding/instance.go b/internal/configs/sharding/instance.go index 7601c2899..a1454cd36 100644 --- a/internal/configs/sharding/instance.go +++ b/internal/configs/sharding/instance.go @@ -31,6 +31,7 @@ type instance struct { hmyAccounts []genesis.DeployAccount fnAccounts []genesis.DeployAccount reshardingEpoch []*big.Int + blocksPerEpochOld uint64 blocksPerEpoch uint64 } @@ -40,7 +41,7 @@ func NewInstance( numShards uint32, numNodesPerShard, numHarmonyOperatedNodesPerShard int, harmonyVotePercent numeric.Dec, hmyAccounts []genesis.DeployAccount, fnAccounts []genesis.DeployAccount, - reshardingEpoch []*big.Int, blocksE uint64, + reshardingEpoch []*big.Int, blocksEOld uint64, blocksE uint64, ) (Instance, error) { if numShards < 1 { return nil, errors.Errorf( @@ -81,6 +82,7 @@ func NewInstance( hmyAccounts: hmyAccounts, fnAccounts: fnAccounts, reshardingEpoch: reshardingEpoch, + blocksPerEpochOld: blocksEOld, blocksPerEpoch: blocksE, }, nil } @@ -94,11 +96,11 @@ func MustNewInstance( harmonyVotePercent numeric.Dec, hmyAccounts []genesis.DeployAccount, fnAccounts []genesis.DeployAccount, - reshardingEpoch []*big.Int, blocksPerEpoch uint64, + reshardingEpoch []*big.Int, blocksPerEpochOld uint64, blocksPerEpoch uint64, ) Instance { sc, err := NewInstance( numShards, numNodesPerShard, numHarmonyOperatedNodesPerShard, harmonyVotePercent, - hmyAccounts, fnAccounts, reshardingEpoch, blocksPerEpoch, + hmyAccounts, fnAccounts, reshardingEpoch, blocksPerEpochOld, blocksPerEpoch, ) if err != nil { panic(err) diff --git a/internal/configs/sharding/localnet.go b/internal/configs/sharding/localnet.go index 65b3f1716..208cd64f9 100644 --- a/internal/configs/sharding/localnet.go +++ b/internal/configs/sharding/localnet.go @@ -118,7 +118,7 @@ var ( big.NewInt(0), big.NewInt(localnetV1Epoch), params.LocalnetChainConfig.StakingEpoch, } // Number of shards, how many slots on each , how many slots owned by Harmony - localnetV0 = MustNewInstance(2, 7, 5, numeric.OneDec(), genesis.LocalHarmonyAccounts, genesis.LocalFnAccounts, localnetReshardingEpoch, LocalnetSchedule.BlocksPerEpoch()) - localnetV1 = MustNewInstance(2, 8, 5, numeric.OneDec(), genesis.LocalHarmonyAccountsV1, genesis.LocalFnAccountsV1, localnetReshardingEpoch, LocalnetSchedule.BlocksPerEpoch()) - localnetV2 = MustNewInstance(2, 9, 6, numeric.MustNewDecFromStr("0.68"), genesis.LocalHarmonyAccountsV2, genesis.LocalFnAccountsV2, localnetReshardingEpoch, LocalnetSchedule.BlocksPerEpoch()) + localnetV0 = MustNewInstance(2, 7, 5, numeric.OneDec(), genesis.LocalHarmonyAccounts, genesis.LocalFnAccounts, localnetReshardingEpoch, LocalnetSchedule.BlocksPerEpoch(), LocalnetSchedule.BlocksPerEpoch()) + localnetV1 = MustNewInstance(2, 8, 5, numeric.OneDec(), genesis.LocalHarmonyAccountsV1, genesis.LocalFnAccountsV1, localnetReshardingEpoch, LocalnetSchedule.BlocksPerEpoch(), LocalnetSchedule.BlocksPerEpoch()) + localnetV2 = MustNewInstance(2, 9, 6, numeric.MustNewDecFromStr("0.68"), genesis.LocalHarmonyAccountsV2, genesis.LocalFnAccountsV2, localnetReshardingEpoch, LocalnetSchedule.BlocksPerEpoch(), LocalnetSchedule.BlocksPerEpoch()) ) diff --git a/internal/configs/sharding/mainnet.go b/internal/configs/sharding/mainnet.go index d31c86ae7..bb94c5cd5 100644 --- a/internal/configs/sharding/mainnet.go +++ b/internal/configs/sharding/mainnet.go @@ -171,18 +171,18 @@ func (ms mainnetSchedule) IsSkippedEpoch(shardID uint32, epoch *big.Int) bool { 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 ( - 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.BlocksPerEpoch(), MainnetSchedule.BlocksPerEpoch()) + mainnetV0_1 = MustNewInstance(4, 152, 112, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV0_1, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch(), MainnetSchedule.BlocksPerEpoch()) + mainnetV0_2 = MustNewInstance(4, 200, 148, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV0_2, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch(), MainnetSchedule.BlocksPerEpoch()) + mainnetV0_3 = MustNewInstance(4, 210, 148, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV0_3, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch(), MainnetSchedule.BlocksPerEpoch()) + mainnetV0_4 = MustNewInstance(4, 216, 148, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV0_4, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch(), MainnetSchedule.BlocksPerEpoch()) + mainnetV1 = MustNewInstance(4, 250, 170, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch(), MainnetSchedule.BlocksPerEpoch()) + mainnetV1_1 = MustNewInstance(4, 250, 170, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_1, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch(), MainnetSchedule.BlocksPerEpoch()) + mainnetV1_2 = MustNewInstance(4, 250, 170, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_2, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch(), MainnetSchedule.BlocksPerEpoch()) + mainnetV1_3 = MustNewInstance(4, 250, 170, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_3, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch(), MainnetSchedule.BlocksPerEpoch()) + mainnetV1_4 = MustNewInstance(4, 250, 170, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_4, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch(), MainnetSchedule.BlocksPerEpoch()) + mainnetV1_5 = MustNewInstance(4, 250, 170, numeric.OneDec(), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_5, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch(), MainnetSchedule.BlocksPerEpoch()) + mainnetV2_0 = MustNewInstance(4, 250, 170, numeric.MustNewDecFromStr("0.68"), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_5, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch(), MainnetSchedule.BlocksPerEpoch()) + mainnetV2_1 = MustNewInstance(4, 250, 130, numeric.MustNewDecFromStr("0.68"), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_5, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch(), MainnetSchedule.BlocksPerEpoch()) + mainnetV2_2 = MustNewInstance(4, 250, 90, numeric.MustNewDecFromStr("0.68"), genesis.HarmonyAccounts, genesis.FoundationalNodeAccountsV1_5, mainnetReshardingEpoch, MainnetSchedule.BlocksPerEpoch(), MainnetSchedule.BlocksPerEpoch()) ) diff --git a/internal/configs/sharding/pangaea.go b/internal/configs/sharding/pangaea.go index 2c04c0fbc..30d5fed89 100644 --- a/internal/configs/sharding/pangaea.go +++ b/internal/configs/sharding/pangaea.go @@ -85,5 +85,5 @@ var pangaeaReshardingEpoch = []*big.Int{ params.PangaeaChainConfig.StakingEpoch, } -var pangaeaV0 = MustNewInstance(4, 30, 30, numeric.OneDec(), genesis.TNHarmonyAccounts, genesis.TNFoundationalAccounts, pangaeaReshardingEpoch, PangaeaSchedule.BlocksPerEpoch()) -var pangaeaV1 = MustNewInstance(4, 110, 30, numeric.MustNewDecFromStr("0.68"), genesis.TNHarmonyAccounts, genesis.TNFoundationalAccounts, pangaeaReshardingEpoch, PangaeaSchedule.BlocksPerEpoch()) +var pangaeaV0 = MustNewInstance(4, 30, 30, numeric.OneDec(), genesis.TNHarmonyAccounts, genesis.TNFoundationalAccounts, pangaeaReshardingEpoch, PangaeaSchedule.BlocksPerEpoch(), PangaeaSchedule.BlocksPerEpoch()) +var pangaeaV1 = MustNewInstance(4, 110, 30, numeric.MustNewDecFromStr("0.68"), genesis.TNHarmonyAccounts, genesis.TNFoundationalAccounts, pangaeaReshardingEpoch, PangaeaSchedule.BlocksPerEpoch(), PangaeaSchedule.BlocksPerEpoch()) diff --git a/internal/configs/sharding/partner.go b/internal/configs/sharding/partner.go index f4e1745f9..5c351a2c3 100644 --- a/internal/configs/sharding/partner.go +++ b/internal/configs/sharding/partner.go @@ -87,5 +87,5 @@ var partnerReshardingEpoch = []*big.Int{ params.TestnetChainConfig.StakingEpoch, } -var partnerV0 = MustNewInstance(2, 15, 15, numeric.OneDec(), genesis.TNHarmonyAccounts, genesis.TNFoundationalAccounts, partnerReshardingEpoch, PartnerSchedule.BlocksPerEpoch()) -var partnerV1 = MustNewInstance(2, 30, 15, numeric.MustNewDecFromStr("0.68"), genesis.TNHarmonyAccounts, genesis.TNFoundationalAccounts, partnerReshardingEpoch, PartnerSchedule.BlocksPerEpoch()) +var partnerV0 = MustNewInstance(2, 15, 15, numeric.OneDec(), genesis.TNHarmonyAccounts, genesis.TNFoundationalAccounts, partnerReshardingEpoch, PartnerSchedule.BlocksPerEpoch(), PartnerSchedule.BlocksPerEpoch()) +var partnerV1 = MustNewInstance(2, 30, 15, numeric.MustNewDecFromStr("0.68"), genesis.TNHarmonyAccounts, genesis.TNFoundationalAccounts, partnerReshardingEpoch, PartnerSchedule.BlocksPerEpoch(), PartnerSchedule.BlocksPerEpoch()) diff --git a/internal/configs/sharding/shardingconfig.go b/internal/configs/sharding/shardingconfig.go index e246a812d..87e839f81 100644 --- a/internal/configs/sharding/shardingconfig.go +++ b/internal/configs/sharding/shardingconfig.go @@ -16,9 +16,6 @@ import ( type Schedule interface { InstanceForEpoch(epoch *big.Int) Instance - // BlocksPerEpoch returns the number of blocks per each Epoch - BlocksPerEpoch() uint64 - // CalcEpochNumber returns the epoch number based on the block number CalcEpochNumber(blockNum uint64) *big.Int diff --git a/internal/configs/sharding/stress.go b/internal/configs/sharding/stress.go index 2ee77d5ef..436e5b604 100644 --- a/internal/configs/sharding/stress.go +++ b/internal/configs/sharding/stress.go @@ -87,5 +87,5 @@ var stressnetReshardingEpoch = []*big.Int{ params.StressnetChainConfig.StakingEpoch, } -var stressnetV0 = MustNewInstance(2, 10, 10, numeric.OneDec(), genesis.TNHarmonyAccounts, genesis.TNFoundationalAccounts, stressnetReshardingEpoch, StressNetSchedule.BlocksPerEpoch()) -var stressnetV1 = MustNewInstance(2, 30, 10, numeric.MustNewDecFromStr("0.9"), genesis.TNHarmonyAccounts, genesis.TNFoundationalAccounts, stressnetReshardingEpoch, StressNetSchedule.BlocksPerEpoch()) +var stressnetV0 = MustNewInstance(2, 10, 10, numeric.OneDec(), genesis.TNHarmonyAccounts, genesis.TNFoundationalAccounts, stressnetReshardingEpoch, StressNetSchedule.BlocksPerEpoch(), StressNetSchedule.BlocksPerEpoch()) +var stressnetV1 = MustNewInstance(2, 30, 10, numeric.MustNewDecFromStr("0.9"), genesis.TNHarmonyAccounts, genesis.TNFoundationalAccounts, stressnetReshardingEpoch, StressNetSchedule.BlocksPerEpoch(), StressNetSchedule.BlocksPerEpoch()) diff --git a/internal/configs/sharding/testnet.go b/internal/configs/sharding/testnet.go index 0dcff407f..8bb1d1a11 100644 --- a/internal/configs/sharding/testnet.go +++ b/internal/configs/sharding/testnet.go @@ -18,6 +18,9 @@ const ( // ~304 sec epochs for P2 of open staking testnetBlocksPerEpoch = 38 + // 4.5 hours per epoch (given 2s block time) + testnetBlocksPerEpochV2 = 8192 + testnetVdfDifficulty = 10000 // This takes about 20s to finish the vdf // TestNetHTTPPattern is the http pattern for testnet. @@ -30,6 +33,8 @@ const ( func (ts testnetSchedule) InstanceForEpoch(epoch *big.Int) Instance { switch { + case params.TestnetChainConfig.IsTwoSeconds(epoch): + return testnetV3 case epoch.Cmp(big.NewInt(testnetV2Epoch)) >= 0: return testnetV2 case epoch.Cmp(params.TestnetChainConfig.StakingEpoch) >= 0: @@ -39,21 +44,48 @@ func (ts testnetSchedule) InstanceForEpoch(epoch *big.Int) Instance { } } -func (ts testnetSchedule) BlocksPerEpoch() uint64 { +func (ts testnetSchedule) BlocksPerEpochOld() uint64 { return testnetBlocksPerEpoch } +func (ts testnetSchedule) BlocksPerEpoch() uint64 { + return testnetBlocksPerEpochV2 +} + func (ts testnetSchedule) CalcEpochNumber(blockNum uint64) *big.Int { - epoch := blockNum / ts.BlocksPerEpoch() - return big.NewInt(int64(epoch)) + + firstBlock2s := params.TestnetChainConfig.TwoSecondsEpoch.Uint64() * ts.BlocksPerEpochOld() + switch { + case blockNum >= firstBlock2s: + return big.NewInt(int64((blockNum-firstBlock2s)/ts.BlocksPerEpoch() + params.TestnetChainConfig.TwoSecondsEpoch.Uint64())) + default: // genesis + oldEpoch := blockNum / ts.BlocksPerEpochOld() + return big.NewInt(int64(oldEpoch)) + } + } func (ts testnetSchedule) IsLastBlock(blockNum uint64) bool { - return (blockNum+1)%ts.BlocksPerEpoch() == 0 + firstBlock2s := params.TestnetChainConfig.TwoSecondsEpoch.Uint64() * ts.BlocksPerEpochOld() + + switch { + case blockNum >= firstBlock2s: + return ((blockNum-firstBlock2s)%ts.BlocksPerEpoch() == ts.BlocksPerEpoch()-1) + default: // genesis + return (blockNum+1)%ts.BlocksPerEpochOld() == 0 + } } func (ts testnetSchedule) EpochLastBlock(epochNum uint64) uint64 { - return ts.BlocksPerEpoch()*(epochNum+1) - 1 + firstBlock2s := params.TestnetChainConfig.TwoSecondsEpoch.Uint64() * ts.BlocksPerEpochOld() + + switch { + case params.TestnetChainConfig.IsTwoSeconds(big.NewInt(int64(epochNum))): + return firstBlock2s - 1 + ts.BlocksPerEpoch()*(epochNum-params.TestnetChainConfig.TwoSecondsEpoch.Uint64()+1) + default: // genesis + return ts.BlocksPerEpochOld()*(epochNum+1) - 1 + } + } func (ts testnetSchedule) VdfDifficulty() int { @@ -88,8 +120,10 @@ func (ts testnetSchedule) IsSkippedEpoch(shardID uint32, epoch *big.Int) bool { var testnetReshardingEpoch = []*big.Int{ big.NewInt(0), params.TestnetChainConfig.StakingEpoch, + params.TestnetChainConfig.TwoSecondsEpoch, } -var testnetV0 = MustNewInstance(4, 16, 15, numeric.OneDec(), genesis.TNHarmonyAccounts, genesis.TNFoundationalAccounts, testnetReshardingEpoch, TestnetSchedule.BlocksPerEpoch()) -var testnetV1 = MustNewInstance(4, 20, 15, numeric.MustNewDecFromStr("0.90"), genesis.TNHarmonyAccounts, genesis.TNFoundationalAccounts, testnetReshardingEpoch, TestnetSchedule.BlocksPerEpoch()) -var testnetV2 = MustNewInstance(4, 30, 8, numeric.MustNewDecFromStr("0.90"), genesis.TNHarmonyAccounts, genesis.TNFoundationalAccounts, testnetReshardingEpoch, TestnetSchedule.BlocksPerEpoch()) +var testnetV0 = MustNewInstance(4, 16, 15, numeric.OneDec(), genesis.TNHarmonyAccounts, genesis.TNFoundationalAccounts, testnetReshardingEpoch, TestnetSchedule.BlocksPerEpochOld(), TestnetSchedule.BlocksPerEpoch()) +var testnetV1 = MustNewInstance(4, 20, 15, numeric.MustNewDecFromStr("0.90"), genesis.TNHarmonyAccounts, genesis.TNFoundationalAccounts, testnetReshardingEpoch, TestnetSchedule.BlocksPerEpochOld(), TestnetSchedule.BlocksPerEpoch()) +var testnetV2 = MustNewInstance(4, 30, 8, numeric.MustNewDecFromStr("0.90"), genesis.TNHarmonyAccounts, genesis.TNFoundationalAccounts, testnetReshardingEpoch, TestnetSchedule.BlocksPerEpochOld(), TestnetSchedule.BlocksPerEpoch()) +var testnetV3 = MustNewInstance(4, 30, 8, numeric.MustNewDecFromStr("0.90"), genesis.TNHarmonyAccounts, genesis.TNFoundationalAccounts, testnetReshardingEpoch, TestnetSchedule.BlocksPerEpochOld(), TestnetSchedule.BlocksPerEpoch()) diff --git a/internal/params/config.go b/internal/params/config.go index 6ced31f82..ae5b096f7 100644 --- a/internal/params/config.go +++ b/internal/params/config.go @@ -32,7 +32,7 @@ var ( PreStakingEpoch: big.NewInt(185), QuickUnlockEpoch: big.NewInt(191), FiveSecondsEpoch: big.NewInt(230), - ThreeSecondsEpoch: big.NewInt(10000), // TBD + TwoSecondsEpoch: big.NewInt(10000), // TBD RedelegationEpoch: big.NewInt(290), EIP155Epoch: big.NewInt(28), S3Epoch: big.NewInt(28), @@ -49,7 +49,7 @@ var ( PreStakingEpoch: big.NewInt(1), QuickUnlockEpoch: big.NewInt(0), FiveSecondsEpoch: big.NewInt(16500), - ThreeSecondsEpoch: big.NewInt(70000), + TwoSecondsEpoch: big.NewInt(73000), RedelegationEpoch: big.NewInt(36500), EIP155Epoch: big.NewInt(0), S3Epoch: big.NewInt(0), @@ -67,7 +67,7 @@ var ( PreStakingEpoch: big.NewInt(1), QuickUnlockEpoch: big.NewInt(0), FiveSecondsEpoch: big.NewInt(0), - ThreeSecondsEpoch: big.NewInt(0), + TwoSecondsEpoch: big.NewInt(0), RedelegationEpoch: big.NewInt(0), EIP155Epoch: big.NewInt(0), S3Epoch: big.NewInt(0), @@ -85,7 +85,7 @@ var ( PreStakingEpoch: big.NewInt(1), QuickUnlockEpoch: big.NewInt(0), FiveSecondsEpoch: big.NewInt(0), - ThreeSecondsEpoch: big.NewInt(0), + TwoSecondsEpoch: big.NewInt(0), RedelegationEpoch: big.NewInt(0), EIP155Epoch: big.NewInt(0), S3Epoch: big.NewInt(0), @@ -103,7 +103,7 @@ var ( PreStakingEpoch: big.NewInt(1), QuickUnlockEpoch: big.NewInt(0), FiveSecondsEpoch: big.NewInt(0), - ThreeSecondsEpoch: big.NewInt(0), + TwoSecondsEpoch: big.NewInt(0), RedelegationEpoch: big.NewInt(0), EIP155Epoch: big.NewInt(0), S3Epoch: big.NewInt(0), @@ -120,7 +120,7 @@ var ( PreStakingEpoch: big.NewInt(0), QuickUnlockEpoch: big.NewInt(0), FiveSecondsEpoch: big.NewInt(0), - ThreeSecondsEpoch: big.NewInt(0), + TwoSecondsEpoch: big.NewInt(0), RedelegationEpoch: big.NewInt(0), EIP155Epoch: big.NewInt(0), S3Epoch: big.NewInt(0), @@ -139,7 +139,7 @@ var ( big.NewInt(0), // PreStakingEpoch big.NewInt(0), // QuickUnlockEpoch big.NewInt(0), // FiveSecondsEpoch - big.NewInt(0), // ThreeSecondsEpoch + big.NewInt(0), // TwoSecondsEpoch big.NewInt(0), // RedelegationEpoch big.NewInt(0), // EIP155Epoch big.NewInt(0), // S3Epoch @@ -158,7 +158,7 @@ var ( big.NewInt(0), // PreStakingEpoch big.NewInt(0), // QuickUnlockEpoch big.NewInt(0), // FiveSecondsEpoch - big.NewInt(0), // ThreeSecondsEpoch + big.NewInt(0), // TwoSecondsEpoch big.NewInt(0), // RedelegationEpoch big.NewInt(0), // EIP155Epoch big.NewInt(0), // S3Epoch @@ -212,9 +212,9 @@ type ChainConfig struct { // and block rewards adjusted to 17.5 ONE/block FiveSecondsEpoch *big.Int `json:"five-seconds-epoch,omitempty"` - // ThreeSecondsEpoch is the epoch when block time is reduced to 3 seconds - // and block rewards adjusted to 10.5 ONE/block - ThreeSecondsEpoch *big.Int `json:"three-seconds-epoch,omitempty"` + // TwoSecondsEpoch is the epoch when block time is reduced to 2 seconds + // and block rewards adjusted to 7 ONE/block + TwoSecondsEpoch *big.Int `json:"two-seconds-epoch,omitempty"` // RedelegationEpoch is the epoch when redelegation is supported and undelegation locking time // is restored to 7 epoch @@ -280,9 +280,9 @@ func (c *ChainConfig) IsFiveSeconds(epoch *big.Int) bool { return isForked(c.FiveSecondsEpoch, epoch) } -// IsThreeSeconds determines whether it is the epoch to change to 3 seconds block time -func (c *ChainConfig) IsThreeSeconds(epoch *big.Int) bool { - return isForked(c.ThreeSecondsEpoch, epoch) +// IsTwoSeconds determines whether it is the epoch to change to 3 seconds block time +func (c *ChainConfig) IsTwoSeconds(epoch *big.Int) bool { + return isForked(c.TwoSecondsEpoch, epoch) } // IsRedelegation determines whether it is the epoch to support redelegation diff --git a/node/node_handler.go b/node/node_handler.go index bc103da1b..b6f54b59b 100644 --- a/node/node_handler.go +++ b/node/node_handler.go @@ -384,10 +384,9 @@ func (node *Node) PostConsensusProcessing(newBlock *types.Block) error { computed := availability.ComputeCurrentSigning( snapshot.Validator, wrapper, ) - beaconChainBlocks := uint64( - node.Beaconchain().CurrentBlock().Header().Number().Int64(), - ) % shard.Schedule.BlocksPerEpoch() - computed.BlocksLeftInEpoch = shard.Schedule.BlocksPerEpoch() - beaconChainBlocks + lastBlockOfEpoch := shard.Schedule.EpochLastBlock(node.Beaconchain().CurrentBlock().Header().Epoch().Uint64()) + + computed.BlocksLeftInEpoch = lastBlockOfEpoch - node.Beaconchain().CurrentBlock().Header().Number().Uint64() if err != nil && computed.IsBelowThreshold { url := h.Availability.OnDroppedBelowThreshold diff --git a/rpc/blockchain.go b/rpc/blockchain.go index d78a0d8ae..79ea39a34 100644 --- a/rpc/blockchain.go +++ b/rpc/blockchain.go @@ -24,10 +24,6 @@ const ( initSupply = int64(12600000000) ) -var ( - blocksPeriod = shard.Schedule.BlocksPerEpoch() -) - // PublicBlockchainService provides an API to access the Harmony blockchain. // It offers only methods that operate on public data that is freely available to anyone. type PublicBlockchainService struct { @@ -376,8 +372,9 @@ func (s *PublicBlockchainService) GetSignedBlocks( totalSigned := uint64(0) lastBlock := uint64(0) blockHeight := s.hmy.CurrentBlock().Number().Uint64() - if blockHeight >= blocksPeriod { - lastBlock = blockHeight - blocksPeriod + 1 + instance := shard.Schedule.InstanceForEpoch(s.hmy.CurrentBlock().Epoch()) + if blockHeight >= instance.BlocksPerEpoch() { + lastBlock = blockHeight - instance.BlocksPerEpoch() + 1 } for i := lastBlock; i <= blockHeight; i++ { signed, err := s.IsBlockSigner(ctx, BlockNumber(i), address) diff --git a/staking/network/reward.go b/staking/network/reward.go index 14baee849..f79ed51a1 100644 --- a/staking/network/reward.go +++ b/staking/network/reward.go @@ -25,10 +25,10 @@ var ( FiveSecondsBaseStakedReward = numeric.NewDecFromBigInt(new(big.Int).Mul( big.NewInt(17.5*denominations.Nano), big.NewInt(denominations.Nano), )) - // ThreeSecondsBaseStakedReward is the flat-rate block reward after epoch 360. - // 10.5 ONE per block - ThreeSecondsBaseStakedReward = numeric.NewDecFromBigInt(new(big.Int).Mul( - big.NewInt(10.5*denominations.Nano), big.NewInt(denominations.Nano), + // TwoSecondsBaseStakedReward is the flat-rate block reward after epoch 360. + // 7 ONE per block + TwoSecondsBaseStakedReward = numeric.NewDecFromBigInt(new(big.Int).Mul( + big.NewInt(7*denominations.Nano), big.NewInt(denominations.Nano), )) // BlockRewardStakedCase is the baseline block reward in staked case - totalTokens = numeric.NewDecFromBigInt( diff --git a/staking/network/reward_test.go b/staking/network/reward_test.go index ac0141387..500add5f2 100644 --- a/staking/network/reward_test.go +++ b/staking/network/reward_test.go @@ -15,10 +15,10 @@ func TestFiveSecondsBaseStakedReward(t *testing.T) { ) } - expectedNewReward = BaseStakedReward.Mul(numeric.MustNewDecFromStr("3")).Quo(numeric.MustNewDecFromStr("8")) - if !expectedNewReward.Equal(ThreeSecondsBaseStakedReward) { + expectedNewReward = BaseStakedReward.Mul(numeric.MustNewDecFromStr("2")).Quo(numeric.MustNewDecFromStr("8")) + if !expectedNewReward.Equal(TwoSecondsBaseStakedReward) { t.Errorf( - "Expected: %s, Got: %s", ThreeSecondsBaseStakedReward.String(), expectedNewReward.String(), + "Expected: %s, Got: %s", TwoSecondsBaseStakedReward.String(), expectedNewReward.String(), ) } }