Skip to content

Commit

Permalink
feat: use last rewards from RecordSponsorStartHeight
Browse files Browse the repository at this point in the history
  • Loading branch information
RainFallsSilent committed Jun 3, 2024
1 parent 48b4479 commit 0adc304
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
17 changes: 17 additions & 0 deletions dpos/state/arbitrators.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ type Arbiters struct {
CurrentReward RewardData
NextReward RewardData

LastDPoSRewards map[string]common.Fixed64

LastArbitrators []ArbiterMember
CurrentArbitrators []ArbiterMember
CurrentCandidates []ArbiterMember
Expand Down Expand Up @@ -184,6 +186,7 @@ func (a *Arbiters) recoverFromCheckPoints(point *CheckPoint) {
a.nextCandidates = point.NextCandidates
a.CurrentReward = point.CurrentReward
a.NextReward = point.NextReward
a.LastDPoSRewards = point.LastDPoSRewards
a.StateKeyFrame = &point.StateKeyFrame
a.accumulativeReward = point.AccumulativeReward
a.finalRoundChange = point.FinalRoundChange
Expand Down Expand Up @@ -807,6 +810,11 @@ func (a *Arbiters) accumulateReward(block *types.Block, confirm *payload.Confirm
rewards = a.getDPoSV2RewardsV2(dposReward, sponsor, block.Height)
}

// need record rewards after RecordSponsorStartHeight, real reward at next block.
if block.Height >= a.ChainParams.DPoSConfiguration.RecordSponsorStartHeight {
swapReardMapContent(&a.LastDPoSRewards, &rewards)
}

a.History.Append(block.Height, func() {
for k, v := range rewards {
a.DPoSV2RewardInfo[k] += v
Expand Down Expand Up @@ -843,6 +851,12 @@ func (a *Arbiters) accumulateReward(block *types.Block, confirm *payload.Confirm
}
}

func swapReardMapContent(map1, map2 *map[string]common.Fixed64) {
temp := *map1
*map1 = *map2
*map2 = temp
}

func (a *Arbiters) clearingDPOSReward(block *types.Block, historyHeight uint32,
smoothClearing bool) (err error) {
if block.Height < a.ChainParams.PublicDPOSHeight ||
Expand Down Expand Up @@ -2796,6 +2810,7 @@ func (a *Arbiters) newCheckPoint(height uint32) *CheckPoint {
NextCandidates: make([]ArbiterMember, 0),
CurrentReward: *NewRewardData(),
NextReward: *NewRewardData(),
LastDPoSRewards: make(map[string]common.Fixed64),
CurrentCRCArbitersMap: make(map[common.Uint168]ArbiterMember),
CurrentOnDutyCRCArbitersMap: make(map[common.Uint168]ArbiterMember),
NextCRCArbitersMap: make(map[common.Uint168]ArbiterMember),
Expand All @@ -2818,6 +2833,7 @@ func (a *Arbiters) newCheckPoint(height uint32) *CheckPoint {
point.NextCandidates = copyByteList(a.nextCandidates)
point.CurrentReward = *copyReward(&a.CurrentReward)
point.NextReward = *copyReward(&a.NextReward)
point.LastDPoSRewards = copyDPoSRewardMap(a.LastDPoSRewards)
point.NextCRCArbitersMap = copyCRCArbitersMap(a.nextCRCArbitersMap)
point.CurrentCRCArbitersMap = copyCRCArbitersMap(a.CurrentCRCArbitersMap)
point.NextCRCArbiters = copyByteList(a.nextCRCArbiters)
Expand Down Expand Up @@ -3028,6 +3044,7 @@ func NewArbitrators(chainParams *config.Configuration, committee *state.Committe
Snapshots: make(map[uint32][]*CheckPoint),
SnapshotKeysDesc: make([]uint32, 0),
BlockConfirmProposalSponsors: blockConfirmProposalSponsors,
LastDPoSRewards: make(map[string]common.Fixed64),
crcChangedHeight: 0,
degradation: &degradation{
inactiveTxs: make(map[common.Uint256]interface{}),
Expand Down
47 changes: 47 additions & 0 deletions dpos/state/checkpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type CheckPoint struct {
CurrentCandidates []ArbiterMember
CurrentReward RewardData
NextReward RewardData
LastDPoSRewards map[string]common.Fixed64
CurrentCRCArbitersMap map[common.Uint168]ArbiterMember
CurrentOnDutyCRCArbitersMap map[common.Uint168]ArbiterMember
NextCRCArbitersMap map[common.Uint168]ArbiterMember
Expand Down Expand Up @@ -195,6 +196,10 @@ func (c *CheckPoint) Serialize(w io.Writer) (err error) {
return
}

if err = c.serializeDPoSRewardsMap(w, c.LastDPoSRewards); err != nil {
return
}

if err = c.serializeCRCArbitersMap(w, c.CurrentCRCArbitersMap); err != nil {
return
}
Expand Down Expand Up @@ -260,6 +265,22 @@ func (c *CheckPoint) serializeCRCArbitersMap(w io.Writer,
return
}

func (c *CheckPoint) serializeDPoSRewardsMap(w io.Writer,
rmap map[string]common.Fixed64) (err error) {
if err = common.WriteVarUint(w, uint64(len(rmap))); err != nil {
return
}
for k, v := range rmap {
if err = common.WriteVarString(w, k); err != nil {
return
}
if err = v.Serialize(w); err != nil {
return
}
}
return
}

func (c *CheckPoint) serializeRoundRewardMap(w io.Writer,
rmap map[common.Uint168]common.Fixed64) (err error) {
if err = common.WriteVarUint(w, uint64(len(rmap))); err != nil {
Expand Down Expand Up @@ -330,6 +351,10 @@ func (c *CheckPoint) Deserialize(r io.Reader) (err error) {
return
}

if c.LastDPoSRewards, err = c.deserializeDPoSRewardsMap(r); err != nil {
return
}

if c.CurrentCRCArbitersMap, err = c.deserializeCRCArbitersMap(r); err != nil {
return
}
Expand Down Expand Up @@ -431,6 +456,27 @@ func (c *CheckPoint) deserializeIllegalPayloadHashes(
return
}

func (c *CheckPoint) deserializeDPoSRewardsMap(
r io.Reader) (rmap map[string]common.Fixed64, err error) {
var count uint64
if count, err = common.ReadVarUint(r, 0); err != nil {
return
}
rmap = make(map[string]common.Fixed64)
for i := uint64(0); i < count; i++ {
var k string
if k, err = common.ReadVarString(r); err != nil {
return
}
reward := common.Fixed64(0)
if err = reward.Deserialize(r); err != nil {
return
}
rmap[k] = reward
}
return
}

func (c *CheckPoint) deserializeRoundRewardMap(
r io.Reader) (rmap map[common.Uint168]common.Fixed64, err error) {
var count uint64
Expand Down Expand Up @@ -489,6 +535,7 @@ func (c *CheckPoint) initFromArbitrators(ar *Arbiters) {
c.NextCandidates = ar.nextCandidates
c.CurrentReward = ar.CurrentReward
c.NextReward = ar.NextReward
c.LastDPoSRewards = ar.LastDPoSRewards
c.LastArbitrators = ar.LastArbitrators
c.CurrentArbitrators = ar.CurrentArbitrators
c.StateKeyFrame = *ar.State.StateKeyFrame
Expand Down
9 changes: 9 additions & 0 deletions dpos/state/keyframe.go
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,15 @@ func copyReward(src *RewardData) (dst *RewardData) {
return
}

func copyDPoSRewardMap(src map[string]common.Fixed64) (dst map[string]common.Fixed64) {
dst = make(map[string]common.Fixed64)
for k, v := range src {
dst[k] = v
}
return

}

func copyCRCArbitersMap(src map[common.Uint168]ArbiterMember) (dst map[common.Uint168]ArbiterMember) {
dst = make(map[common.Uint168]ArbiterMember)
for k, v := range src {
Expand Down

0 comments on commit 0adc304

Please sign in to comment.