Skip to content

Commit

Permalink
Merge pull request #193 from hyeonLewis/update-cl
Browse files Browse the repository at this point in the history
kaiax/staking,reward: Use CL info since Prague block
  • Loading branch information
hyeonLewis authored Jan 2, 2025
2 parents 0bada75 + 5fe6901 commit 59b9689
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
9 changes: 5 additions & 4 deletions kaiax/reward/impl/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,14 @@ func assignStakingRewards(config *reward.RewardConfig, stakersReward *big.Int, s
minStake = config.MinimumStake.Uint64()
totalExcessInt = uint64(0) // sum of excess stakes (the amount over minStake) over all stakers
cnTotalStakingMap = make(map[common.Address]uint64)
isPrague = config.Rules.IsPrague
)
for _, cn := range cns {
// If the CNStaking is less than minStake, skip it.
if cn.StakingAmount >= minStake {
// Calculate total staking amount once
cnTotalStakingAmount := cn.StakingAmount
if cn.CLStakingInfo != nil {
if isPrague && cn.CLStakingInfo != nil {
cnTotalStakingAmount += cn.CLStakingInfo.CLStakingAmount
}
totalExcessInt += cnTotalStakingAmount - minStake
Expand All @@ -389,7 +390,7 @@ func assignStakingRewards(config *reward.RewardConfig, stakersReward *big.Int, s
// reward (kei) = excess (KAIA) * stakersReward (kei) / totalExcess (KAIA)
excess := new(big.Int).SetUint64(cnTotalStakingAmount - minStake)
if reward := new(big.Int).Div(new(big.Int).Mul(excess, stakersReward), totalExcess); reward.Sign() > 0 {
if cn.CLStakingInfo != nil {
if isPrague && cn.CLStakingInfo != nil {
// The remaining amount will be added to the cnAmount.
cnAmount, clAmount := cn.Split(reward)
alloc[cn.RewardAddr] = cnAmount
Expand Down Expand Up @@ -427,12 +428,12 @@ func specWithProposerAndFunds(spec *reward.RewardSpec, config *reward.RewardConf
}

newSpec.Proposer = proposer
if si.CLStakingInfos == nil {
if !config.Rules.IsPrague || si.CLStakingInfos == nil {
newSpec.IncRecipient(config.Rewardbase, proposer)
return newSpec
}

// Handle CLStakingInfo for proposer if not nil
// Handle CLStakingInfo for proposer after Prague
cns := si.ConsolidatedNodes()
for _, cn := range cns {
if cn.RewardAddr != config.Rewardbase {
Expand Down
3 changes: 2 additions & 1 deletion kaiax/reward/impl/getter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,7 @@ func TestAssignStakingRewards(t *testing.T) {
}
for _, tc := range testcases {
si := makeTestStakingInfo(tc.stakingAmounts, tc.prague)
config.Rules.IsPrague = tc.prague
alloc, remainder := assignStakingRewards(config, reward, si)

assert.Equal(t, tc.expectedAlloc, alloc, tc.desc)
Expand Down Expand Up @@ -965,7 +966,7 @@ func TestSpecWithProposerAndFundsPrague(t *testing.T) {
sumProposerKif = big.NewInt(proposer + kif)
sumProposerKef = big.NewInt(proposer + kef)

config = &reward.RewardConfig{Rewardbase: rewardbase}
config = &reward.RewardConfig{Rewardbase: rewardbase, Rules: params.Rules{IsPrague: true}}
)

testcases := []struct {
Expand Down
11 changes: 7 additions & 4 deletions kaiax/staking/impl/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (s *StakingModule) getFromStateByNumber(num uint64) (*staking.StakingInfo,
// Efficiently read addresses and balances from the AddressBook in one EVM call.
// Works by temporarily injecting the MultiCallContract to a copied state.
func (s *StakingModule) getFromState(header *types.Header, statedb *state.StateDB) (*staking.StakingInfo, error) {
isPrague := s.ChainConfig.IsPragueForkEnabled(header.Number)
isForPrague := s.ChainConfig.IsPragueForkEnabled(new(big.Int).Add(header.Number, common.Big1))
num := header.Number.Uint64()

// Bail out if AddressBook is not installed.
Expand All @@ -123,11 +123,14 @@ func (s *StakingModule) getFromState(header *types.Header, statedb *state.StateD
return nil, staking.ErrAddressBookCall(err)
}

// Get CL registry info if Prague fork is enabled
// Get CL registry info if staking info is for Prague block
var clRes clRegistryResult
if isPrague {
if isForPrague {
// If Registry is not installed, do not handle CL staking info.
if statedb.GetCode(system.RegistryAddr) == nil {
// In private network, Randao and Prague hardfork can be activated at the same block.
// It leads to staking info inconsistency between block processing and rpc query since the Registry hasn't been installed when finalizing the header.
// Note that Randao can't be activated after Prague according to fork ordering (Randao <= Kaia <= Prague).
if statedb.GetCode(system.RegistryAddr) == nil || s.ChainConfig.IsRandaoForkBlockParent(header.Number) {
logger.Trace("Registry not installed", "sourceNum", num)
} else {
// Note that if CLRegistry is not registered in Registry,
Expand Down

0 comments on commit 59b9689

Please sign in to comment.