From 3b5aefd775371d6accda7e36485f2b5d9e41c672 Mon Sep 17 00:00:00 2001 From: Casey Gardiner Date: Wed, 13 Dec 2023 10:07:53 -0800 Subject: [PATCH] initial commit of 1 second finality --- consensus/consensus_service.go | 6 ++--- internal/chain/reward.go | 4 ++-- internal/configs/sharding/localnet.go | 14 ++++++------ internal/configs/sharding/localnet_test.go | 18 +++++++-------- internal/configs/sharding/mainnet.go | 18 +++++++-------- internal/configs/sharding/testnet.go | 14 ++++++------ internal/params/config.go | 26 +++++++++++----------- 7 files changed, 50 insertions(+), 50 deletions(-) diff --git a/consensus/consensus_service.go b/consensus/consensus_service.go index 40f0bc23d..fca78499e 100644 --- a/consensus/consensus_service.go +++ b/consensus/consensus_service.go @@ -326,9 +326,9 @@ func (consensus *Consensus) updateConsensusInformation() Mode { consensus.BlockPeriod = 5 * time.Second - // Enable 2s block time at the twoSecondsEpoch - if consensus.Blockchain().Config().IsTwoSeconds(nextEpoch) { - consensus.BlockPeriod = 2 * time.Second + // Enable 1s block time at the oneSecondsEpoch + if consensus.Blockchain().Config().IsOneSecond(nextEpoch) { + consensus.BlockPeriod = 1 * time.Second } isFirstTimeStaking := consensus.Blockchain().Config().IsStaking(nextEpoch) && diff --git a/internal/chain/reward.go b/internal/chain/reward.go index c64199a20..31dd8b31c 100644 --- a/internal/chain/reward.go +++ b/internal/chain/reward.go @@ -219,14 +219,14 @@ func getDefaultStakingReward(bc engine.ChainReader, epoch *big.Int, blockNum uin } } } - if bc.Config().IsTwoSeconds(epoch) { + if bc.Config().IsOneSecond(epoch) { defaultReward = stakingReward.TwoSecStakedBlocks } } else { // Mainnet (other nets): if bc.Config().IsHIP30(epoch) { defaultReward = stakingReward.HIP30StakedBlocks - } else if bc.Config().IsTwoSeconds(epoch) { + } else if bc.Config().IsOneSecond(epoch) { defaultReward = stakingReward.TwoSecStakedBlocks } else if bc.Config().IsFiveSeconds(epoch) { defaultReward = stakingReward.FiveSecStakedBlocks diff --git a/internal/configs/sharding/localnet.go b/internal/configs/sharding/localnet.go index fb6050b1d..28afe0b15 100644 --- a/internal/configs/sharding/localnet.go +++ b/internal/configs/sharding/localnet.go @@ -45,7 +45,7 @@ func (ls localnetSchedule) InstanceForEpoch(epoch *big.Int) Instance { return localnetV3_2 case params.LocalnetChainConfig.IsSixtyPercent(epoch): return localnetV3_1 - case params.LocalnetChainConfig.IsTwoSeconds(epoch): + case params.LocalnetChainConfig.IsOneSecond(epoch): return localnetV3 case params.LocalnetChainConfig.IsStaking(epoch): return localnetV2 @@ -65,10 +65,10 @@ func (ls localnetSchedule) BlocksPerEpoch() uint64 { } func (ls localnetSchedule) twoSecondsFirstBlock() uint64 { - if params.LocalnetChainConfig.TwoSecondsEpoch.Uint64() == 0 { + if params.LocalnetChainConfig.oneSecondsEpoch.Uint64() == 0 { return 0 } - return (params.LocalnetChainConfig.TwoSecondsEpoch.Uint64()-1)*ls.BlocksPerEpochOld() + localnetEpochBlock1 + return (params.LocalnetChainConfig.oneSecondsEpoch.Uint64()-1)*ls.BlocksPerEpochOld() + localnetEpochBlock1 } func (ls localnetSchedule) CalcEpochNumber(blockNum uint64) *big.Int { @@ -84,7 +84,7 @@ func (ls localnetSchedule) CalcEpochNumber(blockNum uint64) *big.Int { blockNum -= localnetEpochBlock1 extra = 1 } - return big.NewInt(int64(extra + (blockNum-firstBlock2s)/ls.BlocksPerEpoch() + params.LocalnetChainConfig.TwoSecondsEpoch.Uint64())) + return big.NewInt(int64(extra + (blockNum-firstBlock2s)/ls.BlocksPerEpoch() + params.LocalnetChainConfig.oneSecondsEpoch.Uint64())) } } @@ -115,10 +115,10 @@ func (ls localnetSchedule) EpochLastBlock(epochNum uint64) uint64 { return localnetEpochBlock1 - 1 default: switch { - case params.LocalnetChainConfig.IsTwoSeconds(big.NewInt(int64(epochNum))): + case params.LocalnetChainConfig.IsOneSecond(big.NewInt(int64(epochNum))): blocks := ls.BlocksPerEpoch() firstBlock2s := ls.twoSecondsFirstBlock() - block2s := (1 + epochNum - params.LocalnetChainConfig.TwoSecondsEpoch.Uint64()) * blocks + block2s := (1 + epochNum - params.LocalnetChainConfig.oneSecondsEpoch.Uint64()) * blocks if firstBlock2s == 0 { return block2s - blocks + localnetEpochBlock1 - 1 } @@ -159,7 +159,7 @@ func (ls localnetSchedule) IsSkippedEpoch(shardID uint32, epoch *big.Int) bool { var ( localnetReshardingEpoch = []*big.Int{ - big.NewInt(0), big.NewInt(localnetV1Epoch), params.LocalnetChainConfig.StakingEpoch, params.LocalnetChainConfig.TwoSecondsEpoch, + big.NewInt(0), big.NewInt(localnetV1Epoch), params.LocalnetChainConfig.StakingEpoch, params.LocalnetChainConfig.oneSecondsEpoch, } // Number of shards, how many slots on each , how many slots owned by Harmony localnetV0 = MustNewInstance( diff --git a/internal/configs/sharding/localnet_test.go b/internal/configs/sharding/localnet_test.go index 6151e61f5..a0ec2fdf3 100644 --- a/internal/configs/sharding/localnet_test.go +++ b/internal/configs/sharding/localnet_test.go @@ -10,10 +10,10 @@ import ( func TestLocalnetEpochCalculation(t *testing.T) { check := func(epoch, expected uint64) { if got := LocalnetSchedule.EpochLastBlock(epoch); got != expected { - t.Fatalf("wrong EpochLastBlock at epoch %d. TwoSecondsEpoch: %s. expected: %d got: %d.", epoch, params.LocalnetChainConfig.TwoSecondsEpoch.String(), expected, got) + t.Fatalf("wrong EpochLastBlock at epoch %d. oneSecondsEpoch: %s. expected: %d got: %d.", epoch, params.LocalnetChainConfig.oneSecondsEpoch.String(), expected, got) } if !LocalnetSchedule.IsLastBlock(expected) { - t.Fatalf("%d is not LastBlock. TwoSecondsEpoch: %s", expected, params.LocalnetChainConfig.TwoSecondsEpoch.String()) + t.Fatalf("%d is not LastBlock. oneSecondsEpoch: %s", expected, params.LocalnetChainConfig.oneSecondsEpoch.String()) } epochStart := uint64(0) if epoch > 0 { @@ -21,30 +21,30 @@ func TestLocalnetEpochCalculation(t *testing.T) { } for blockNo := epochStart; blockNo <= expected; blockNo++ { if isLastBlock := LocalnetSchedule.IsLastBlock(blockNo); isLastBlock != (blockNo == expected) { - t.Fatalf("IsLastBlock for %d is wrong. TwoSecondsEpoch: %s. expected %v got %v", blockNo, params.LocalnetChainConfig.TwoSecondsEpoch.String(), blockNo == expected, isLastBlock) + t.Fatalf("IsLastBlock for %d is wrong. oneSecondsEpoch: %s. expected %v got %v", blockNo, params.LocalnetChainConfig.oneSecondsEpoch.String(), blockNo == expected, isLastBlock) } got := LocalnetSchedule.CalcEpochNumber(blockNo).Uint64() if got != epoch { - t.Fatalf("CalcEpochNumber for %d is wrong. TwoSecondsEpoch: %s. expected %d got %d", blockNo, params.LocalnetChainConfig.TwoSecondsEpoch.String(), epoch, got) + t.Fatalf("CalcEpochNumber for %d is wrong. oneSecondsEpoch: %s. expected %d got %d", blockNo, params.LocalnetChainConfig.oneSecondsEpoch.String(), epoch, got) } } } - backup := params.LocalnetChainConfig.TwoSecondsEpoch - params.LocalnetChainConfig.TwoSecondsEpoch = big.NewInt(0) + backup := params.LocalnetChainConfig.oneSecondsEpoch + params.LocalnetChainConfig.oneSecondsEpoch = big.NewInt(0) check(0, localnetEpochBlock1-1) check(1, localnetEpochBlock1+localnetBlocksPerEpochV2-1) check(2, localnetEpochBlock1+localnetBlocksPerEpochV2*2-1) - params.LocalnetChainConfig.TwoSecondsEpoch = big.NewInt(1) + params.LocalnetChainConfig.oneSecondsEpoch = big.NewInt(1) check(0, localnetEpochBlock1-1) check(1, localnetEpochBlock1+localnetBlocksPerEpochV2-1) check(2, localnetEpochBlock1+localnetBlocksPerEpochV2*2-1) - params.LocalnetChainConfig.TwoSecondsEpoch = big.NewInt(2) + params.LocalnetChainConfig.oneSecondsEpoch = big.NewInt(2) check(0, localnetEpochBlock1-1) check(1, localnetEpochBlock1+localnetBlocksPerEpoch-1) check(2, localnetEpochBlock1+localnetBlocksPerEpoch+localnetBlocksPerEpochV2-1) check(3, localnetEpochBlock1+localnetBlocksPerEpoch+localnetBlocksPerEpochV2*2-1) - params.LocalnetChainConfig.TwoSecondsEpoch = backup + params.LocalnetChainConfig.oneSecondsEpoch = backup } diff --git a/internal/configs/sharding/mainnet.go b/internal/configs/sharding/mainnet.go index 2106b58d8..b5a85e9fc 100644 --- a/internal/configs/sharding/mainnet.go +++ b/internal/configs/sharding/mainnet.go @@ -88,8 +88,8 @@ func (ms mainnetSchedule) InstanceForEpoch(epoch *big.Int) Instance { // Decrease internal voting power from 68% to 60% // which happens around 1/27/2021 22:00 PDT return mainnetV3_1 - case params.MainnetChainConfig.IsTwoSeconds(epoch): - // Enable 2s block time and change blocks/epoch to 32768 + case params.MainnetChainConfig.IsOneSecond(epoch): + // Enable 1s 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: @@ -143,10 +143,10 @@ func (ms mainnetSchedule) BlocksPerEpoch() uint64 { } func (ms mainnetSchedule) twoSecondsFirstBlock() uint64 { - if params.MainnetChainConfig.TwoSecondsEpoch.Uint64() == 0 { + if params.MainnetChainConfig.oneSecondsEpoch.Uint64() == 0 { return 0 } - return (params.MainnetChainConfig.TwoSecondsEpoch.Uint64()-1)*ms.BlocksPerEpochOld() + mainnetEpochBlock1 + return (params.MainnetChainConfig.oneSecondsEpoch.Uint64()-1)*ms.BlocksPerEpochOld() + mainnetEpochBlock1 } func (ms mainnetSchedule) CalcEpochNumber(blockNum uint64) *big.Int { @@ -161,8 +161,8 @@ func (ms mainnetSchedule) CalcEpochNumber(blockNum uint64) *big.Int { firstBlock2s := ms.twoSecondsFirstBlock() switch { - case params.MainnetChainConfig.IsTwoSeconds(big.NewInt(oldEpochNumber)): - return big.NewInt(int64((blockNum-firstBlock2s)/ms.BlocksPerEpoch() + params.MainnetChainConfig.TwoSecondsEpoch.Uint64())) + case params.MainnetChainConfig.IsOneSecond(big.NewInt(oldEpochNumber)): + return big.NewInt(int64((blockNum-firstBlock2s)/ms.BlocksPerEpoch() + params.MainnetChainConfig.oneSecondsEpoch.Uint64())) default: // genesis return big.NewInt(int64(oldEpochNumber)) } @@ -192,8 +192,8 @@ func (ms mainnetSchedule) EpochLastBlock(epochNum uint64) uint64 { default: firstBlock2s := ms.twoSecondsFirstBlock() switch { - case params.MainnetChainConfig.IsTwoSeconds(big.NewInt(int64(epochNum))): - return firstBlock2s - 1 + ms.BlocksPerEpoch()*(epochNum-params.MainnetChainConfig.TwoSecondsEpoch.Uint64()+1) + case params.MainnetChainConfig.IsOneSecond(big.NewInt(int64(epochNum))): + return firstBlock2s - 1 + ms.BlocksPerEpoch()*(epochNum-params.MainnetChainConfig.oneSecondsEpoch.Uint64()+1) default: // genesis return mainnetEpochBlock1 - 1 + ms.BlocksPerEpochOld()*epochNum } @@ -225,7 +225,7 @@ 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), params.MainnetChainConfig.TwoSecondsEpoch, params.MainnetChainConfig.SixtyPercentEpoch, params.MainnetChainConfig.HIP6And8Epoch} +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.oneSecondsEpoch, params.MainnetChainConfig.SixtyPercentEpoch, params.MainnetChainConfig.HIP6And8Epoch} var ( mainnetV0 = MustNewInstance( diff --git a/internal/configs/sharding/testnet.go b/internal/configs/sharding/testnet.go index 7c2994071..bd9b31e5a 100644 --- a/internal/configs/sharding/testnet.go +++ b/internal/configs/sharding/testnet.go @@ -67,10 +67,10 @@ func (ts testnetSchedule) BlocksPerEpoch() uint64 { func (ts testnetSchedule) CalcEpochNumber(blockNum uint64) *big.Int { - firstBlock2s := params.TestnetChainConfig.TwoSecondsEpoch.Uint64() * ts.BlocksPerEpochOld() + firstBlock2s := params.TestnetChainConfig.oneSecondsEpoch.Uint64() * ts.BlocksPerEpochOld() switch { case blockNum >= firstBlock2s: - return big.NewInt(int64((blockNum-firstBlock2s)/ts.BlocksPerEpoch() + params.TestnetChainConfig.TwoSecondsEpoch.Uint64())) + return big.NewInt(int64((blockNum-firstBlock2s)/ts.BlocksPerEpoch() + params.TestnetChainConfig.oneSecondsEpoch.Uint64())) default: // genesis oldEpoch := blockNum / ts.BlocksPerEpochOld() return big.NewInt(int64(oldEpoch)) @@ -79,7 +79,7 @@ func (ts testnetSchedule) CalcEpochNumber(blockNum uint64) *big.Int { } func (ts testnetSchedule) IsLastBlock(blockNum uint64) bool { - firstBlock2s := params.TestnetChainConfig.TwoSecondsEpoch.Uint64() * ts.BlocksPerEpochOld() + firstBlock2s := params.TestnetChainConfig.oneSecondsEpoch.Uint64() * ts.BlocksPerEpochOld() switch { case blockNum >= firstBlock2s: @@ -90,11 +90,11 @@ func (ts testnetSchedule) IsLastBlock(blockNum uint64) bool { } func (ts testnetSchedule) EpochLastBlock(epochNum uint64) uint64 { - firstBlock2s := params.TestnetChainConfig.TwoSecondsEpoch.Uint64() * ts.BlocksPerEpochOld() + firstBlock2s := params.TestnetChainConfig.oneSecondsEpoch.Uint64() * ts.BlocksPerEpochOld() switch { - case params.TestnetChainConfig.IsTwoSeconds(big.NewInt(int64(epochNum))): - return firstBlock2s - 1 + ts.BlocksPerEpoch()*(epochNum-params.TestnetChainConfig.TwoSecondsEpoch.Uint64()+1) + case params.TestnetChainConfig.IsOneSecond(big.NewInt(int64(epochNum))): + return firstBlock2s - 1 + ts.BlocksPerEpoch()*(epochNum-params.TestnetChainConfig.oneSecondsEpoch.Uint64()+1) default: // genesis return ts.BlocksPerEpochOld()*(epochNum+1) - 1 } @@ -122,7 +122,7 @@ func (ts testnetSchedule) IsSkippedEpoch(shardID uint32, epoch *big.Int) bool { var testnetReshardingEpoch = []*big.Int{ big.NewInt(0), params.TestnetChainConfig.StakingEpoch, - params.TestnetChainConfig.TwoSecondsEpoch, + params.TestnetChainConfig.oneSecondsEpoch, } var ( diff --git a/internal/params/config.go b/internal/params/config.go index 86695ba40..3fdc46626 100644 --- a/internal/params/config.go +++ b/internal/params/config.go @@ -47,7 +47,7 @@ var ( PreStakingEpoch: big.NewInt(185), QuickUnlockEpoch: big.NewInt(191), FiveSecondsEpoch: big.NewInt(230), - TwoSecondsEpoch: big.NewInt(366), // Around Tuesday Dec 8th 2020, 8AM PST + oneSecondsEpoch: big.NewInt(366), // Around Tuesday Dec 8th 2020, 8AM PST SixtyPercentEpoch: big.NewInt(530), // Around Monday Apr 12th 2021, 22:30 UTC RedelegationEpoch: big.NewInt(290), NoEarlyUnlockEpoch: big.NewInt(530), // Around Monday Apr 12th 2021, 22:30 UTC @@ -90,7 +90,7 @@ var ( PreStakingEpoch: big.NewInt(1), QuickUnlockEpoch: big.NewInt(0), FiveSecondsEpoch: big.NewInt(0), - TwoSecondsEpoch: big.NewInt(2), + oneSecondsEpoch: big.NewInt(2), SixtyPercentEpoch: big.NewInt(2), RedelegationEpoch: big.NewInt(2), NoEarlyUnlockEpoch: big.NewInt(2), @@ -133,7 +133,7 @@ var ( PreStakingEpoch: big.NewInt(1), QuickUnlockEpoch: big.NewInt(0), FiveSecondsEpoch: big.NewInt(0), - TwoSecondsEpoch: big.NewInt(0), + oneSecondsEpoch: big.NewInt(0), SixtyPercentEpoch: big.NewInt(0), RedelegationEpoch: big.NewInt(0), NoEarlyUnlockEpoch: big.NewInt(0), @@ -177,7 +177,7 @@ var ( PreStakingEpoch: big.NewInt(1), QuickUnlockEpoch: big.NewInt(0), FiveSecondsEpoch: big.NewInt(0), - TwoSecondsEpoch: big.NewInt(0), + oneSecondsEpoch: big.NewInt(0), SixtyPercentEpoch: EpochTBD, RedelegationEpoch: big.NewInt(0), NoEarlyUnlockEpoch: big.NewInt(0), @@ -221,7 +221,7 @@ var ( PreStakingEpoch: big.NewInt(1), QuickUnlockEpoch: big.NewInt(0), FiveSecondsEpoch: big.NewInt(0), - TwoSecondsEpoch: big.NewInt(0), + oneSecondsEpoch: big.NewInt(0), SixtyPercentEpoch: big.NewInt(10), RedelegationEpoch: big.NewInt(0), NoEarlyUnlockEpoch: big.NewInt(0), @@ -264,7 +264,7 @@ var ( PreStakingEpoch: big.NewInt(0), QuickUnlockEpoch: big.NewInt(0), FiveSecondsEpoch: big.NewInt(0), - TwoSecondsEpoch: big.NewInt(0), + oneSecondsEpoch: big.NewInt(0), SixtyPercentEpoch: EpochTBD, // Never enable it for localnet as localnet has no external validator setup RedelegationEpoch: big.NewInt(0), NoEarlyUnlockEpoch: big.NewInt(0), @@ -309,7 +309,7 @@ var ( big.NewInt(0), // PreStakingEpoch big.NewInt(0), // QuickUnlockEpoch big.NewInt(0), // FiveSecondsEpoch - big.NewInt(0), // TwoSecondsEpoch + big.NewInt(0), // oneSecondsEpoch big.NewInt(0), // SixtyPercentEpoch big.NewInt(0), // RedelegationEpoch big.NewInt(0), // NoEarlyUnlockEpoch @@ -354,7 +354,7 @@ var ( big.NewInt(0), // PreStakingEpoch big.NewInt(0), // QuickUnlockEpoch big.NewInt(0), // FiveSecondsEpoch - big.NewInt(0), // TwoSecondsEpoch + big.NewInt(0), // oneSecondsEpoch big.NewInt(0), // SixtyPercentEpoch big.NewInt(0), // RedelegationEpoch big.NewInt(0), // NoEarlyUnlockEpoch @@ -452,9 +452,9 @@ type ChainConfig struct { // and block rewards adjusted to 17.5 ONE/block FiveSecondsEpoch *big.Int `json:"five-seconds-epoch,omitempty"` - // TwoSecondsEpoch is the epoch when block time is reduced to 2 seconds + // oneSecondsEpoch 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"` + oneSecondsEpoch *big.Int `json:"two-seconds-epoch,omitempty"` // SixtyPercentEpoch is the epoch when internal voting power reduced from 68% to 60% SixtyPercentEpoch *big.Int `json:"sixty-percent-epoch,omitempty"` @@ -664,9 +664,9 @@ func (c *ChainConfig) IsFiveSeconds(epoch *big.Int) bool { return isForked(c.FiveSecondsEpoch, 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) +// IsOneSecond determines whether it is the epoch to change to 3 seconds block time +func (c *ChainConfig) IsOneSecond(epoch *big.Int) bool { + return isForked(c.oneSecondsEpoch, epoch) } // IsSixtyPercent determines whether it is the epoch to reduce internal voting power to 60%