Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HIP-30: Shard reduction #4498

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (p *StateProcessor) Process(
}
}

if err := MayTestnetShardReduction(p.bc, statedb, header); err != nil {
if err := MayShardReduction(p.bc, statedb, header); err != nil {
return nil, nil, nil, nil, 0, nil, statedb, err
}

Expand Down Expand Up @@ -442,13 +442,16 @@ func StakingToMessage(
return msg, nil
}

// MayTestnetShardReduction handles the change in the number of Shards. It will mark the affected validator as inactive.
// MayShardReduction handles the change in the number of Shards. It will mark the affected validator as inactive.
// This function does not handle all cases, only for ShardNum from 4 to 2.
func MayTestnetShardReduction(bc ChainContext, statedb *state.DB, header *block.Header) error {
func MayShardReduction(bc ChainContext, statedb *state.DB, header *block.Header) error {
isBeaconChain := header.ShardID() == shard.BeaconChainShardID
isLastBlock := shard.Schedule.IsLastBlock(header.Number().Uint64())
isTestnet := nodeconfig.GetDefaultConfig().GetNetworkType() == nodeconfig.Testnet
if !(isTestnet && isBeaconChain && isLastBlock) {
networkType := nodeconfig.GetDefaultConfig().GetNetworkType()
isTestnet := networkType == nodeconfig.Testnet
isMainnet := networkType == nodeconfig.Mainnet
isReducenet := isMainnet || isTestnet
if !(isReducenet && isBeaconChain && isLastBlock) {
return nil
}
curInstance := shard.Schedule.InstanceForEpoch(header.Epoch())
Expand Down Expand Up @@ -479,6 +482,15 @@ func MayTestnetShardReduction(bc ChainContext, statedb *state.DB, header *block.
for _, pubKey := range validator.SlotPubKeys {
curShard := new(big.Int).Mod(pubKey.Big(), big.NewInt(int64(curNumShards))).Uint64()
nextShard := new(big.Int).Mod(pubKey.Big(), big.NewInt(int64(nextNumShards))).Uint64()
// background: any editValidator transactions take effect at next epoch.
// assumption: shard reduction happens at epoch X.
// validators who wish to continue validating after the shard reduction occurs
// must have a different node running with a key from shard 0 or 1.
// this key must be added to the validator during epoch X - 1
// and keys belonging to shards 2 and 3 removed at that point in time.
// the different node running will be unelected, but continue syncing in X - 1.
// if elected, it will start validating in epoch X.
// once epoch X begins, they can terminate servers from shards 2 and 3.
if curShard >= uint64(nextNumShards) || curShard != nextShard {
validator.Status = effective.Inactive
break
Expand Down
4 changes: 3 additions & 1 deletion internal/chain/reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ func getDefaultStakingReward(bc engine.ChainReader, epoch *big.Int, blockNum uin
}
} else {
// Mainnet (other nets):
if bc.Config().IsTwoSeconds(epoch) {
if bc.Config().IsHIP30(epoch) {
defaultReward = stakingReward.HIP30StakedBlocks
} else if bc.Config().IsTwoSeconds(epoch) {
defaultReward = stakingReward.TwoSecStakedBlocks
} else if bc.Config().IsFiveSeconds(epoch) {
defaultReward = stakingReward.FiveSecStakedBlocks
Expand Down
1 change: 0 additions & 1 deletion internal/configs/sharding/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ var (
feeCollectorsTestnet, numeric.ZeroDec(), ethCommon.Address{},
testnetReshardingEpoch, TestnetSchedule.BlocksPerEpoch(),
)

testnetV5 = MustNewInstance(
2, 30, 8, 0.15,
numeric.MustNewDecFromStr("0.90"), genesis.TNHarmonyAccountsV1,
Expand Down
2 changes: 1 addition & 1 deletion node/node_newblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func (node *Node) ProposeNewBlock(commitSigs chan []byte) (*types.Block, error)
}
}

node.Worker.ApplyTestnetShardReduction()
node.Worker.ApplyShardReduction()
// Prepare shard state
var shardState *shard.State
if shardState, err = node.Blockchain().SuperCommitteeForNextEpoch(
Expand Down
6 changes: 3 additions & 3 deletions node/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ func (w *Worker) commitStakingTransaction(
return nil
}

// ApplyTestnetShardReduction only used to reduce shards of Testnet
func (w *Worker) ApplyTestnetShardReduction() {
core.MayTestnetShardReduction(w.chain, w.current.state, w.current.header)
// ApplyShardReduction only used to reduce shards of Testnet
func (w *Worker) ApplyShardReduction() {
core.MayShardReduction(w.chain, w.current.state, w.current.header)
}

var (
Expand Down
7 changes: 7 additions & 0 deletions staking/reward/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ var (
TwoSecStakedBlocks = numeric.NewDecFromBigInt(new(big.Int).Mul(
big.NewInt(7*denominations.Nano), big.NewInt(denominations.Nano),
))
// HIP30StakedBlocks is the reward received after HIP-30 goes into
// effect. It is simply double the TwoSecStakedBlocks reward, since
// the number of shards is being halved and we keep emission
// constant.
HIP30StakedBlocks = numeric.NewDecFromBigInt(new(big.Int).Mul(
big.NewInt(14*denominations.Nano), big.NewInt(denominations.Nano),
))

// TotalInitialTokens is the total amount of tokens (in ONE) at block 0 of the network.
// This should be set/change on the node's init according to the core.GenesisSpec.
Expand Down