Skip to content

Commit

Permalink
fix(dpos2.0): fixed an issue with block synchronization getting stuck
Browse files Browse the repository at this point in the history
  • Loading branch information
RainFallsSilent committed Apr 30, 2023
1 parent 8d8f489 commit 2c31444
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 10 deletions.
3 changes: 3 additions & 0 deletions common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ func GetDefaultParams() *Configuration {
CandidatesCount: 72,
DPoSV2RewardAccumulateProgramHash: StakeRewardProgramHash,
NFTStartHeight: 1405000,
DPoSV2SortVotesHeight: 1415500 + 720*15,
OriginArbiters: []string{
"0248df6705a909432be041e0baa25b8f648741018f70d1911f2ed28778db4b8fe4",
"02771faf0f4d4235744b30972d5f2c470993920846c761e4d08889ecfdc061cddf",
Expand Down Expand Up @@ -720,6 +721,8 @@ type DPoSConfiguration struct {
CRDPoSNodeHotFixHeight uint32 `screw:"--crdposnodehotfixheight" usage:"CRDPoSNodeHotFixHeight indicates the hot fix start height of CR DPoS node"`
// NFTStartHeight defines the height of NFT started.
NFTStartHeight uint32 `screw:"--nftstartheight" usage:"the start height of NFT transaction"`
// DPoSV2SortVotesHeight
DPoSV2SortVotesHeight uint32 `screw:"--dposv2sortvotesheight" usage:"defines the start height to sort DPoSV2 votes"`
}

type CRConfiguration struct {
Expand Down
2 changes: 2 additions & 0 deletions common/fixed64.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
//the 64 bit fixed-point number, precise 10^-8
type Fixed64 int64

const ZeroFixed64 = Fixed64(0)

func (f *Fixed64) Serialize(w io.Writer) error {
return WriteElement(w, f)
}
Expand Down
14 changes: 12 additions & 2 deletions core/transaction/dposv2claimrewardtransaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"github.com/elastos/Elastos.ELA/vm"
)

const RewardTolerance = 10

type DPoSV2ClaimRewardTransaction struct {
BaseTransaction
}
Expand Down Expand Up @@ -103,8 +105,16 @@ func (t *DPoSV2ClaimRewardTransaction) SpecialContextCheck() (elaerr.ELAError, b
return elaerr.Simple(elaerr.ErrTxPayload, errors.New("no reward to claim for such address")), true
}

if claimAmount < claimReward.Value {
return elaerr.Simple(elaerr.ErrTxPayload, errors.New("claim reward exceeded , max claim reward "+claimAmount.String())), true
if t.parameters.BlockHeight < t.parameters.Config.DPoSConfiguration.DPoSV2SortVotesHeight {
if claimAmount+RewardTolerance*t.parameters.Config.MinCrossChainTxFee < claimReward.Value {
return elaerr.Simple(elaerr.ErrTxPayload, errors.New("claim reward exceeded , max claim reward "+
claimAmount.String()+" current:"+claimReward.Value.String())), true
}
} else {
if claimAmount < claimReward.Value {
return elaerr.Simple(elaerr.ErrTxPayload, errors.New("claim reward exceeded , max claim reward "+
claimAmount.String()+" current:"+claimReward.Value.String())), true
}
}

if claimReward.Value <= t.parameters.Config.CRConfiguration.RealWithdrawSingleFee {
Expand Down
36 changes: 30 additions & 6 deletions dpos/state/arbitrators.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,10 +734,22 @@ func (a *Arbiters) getDPoSV2RewardsV2(dposReward common.Fixed64, sponsor []byte,
if len(sVoteDetail) == 0 {
continue
}
type sVoteDetailData struct {
Addr common.Uint256
VoteInfo payload.DetailedVoteInfo
}
svdd := make([]sVoteDetailData, 0)
for referKey, votes := range sVoteDetail {
svdd = append(svdd, sVoteDetailData{referKey, votes})
sort.Slice(svdd, func(i, j int) bool {
return svdd[i].Addr.Compare(svdd[j].Addr) < 0
})
}

var totalN float64
for _, votes := range sVoteDetail {
weightF := math.Log10(float64(votes.Info[0].LockTime-votes.BlockHeight) / 7200 * 10)
N := common.Fixed64(float64(votes.Info[0].Votes) * weightF)
for _, votes := range svdd {
weightF := math.Log10(float64(votes.VoteInfo.Info[0].LockTime-votes.VoteInfo.BlockHeight) / 7200 * 10)
N := common.Fixed64(float64(votes.VoteInfo.Info[0].Votes) * weightF)
totalN += float64(N)
}
if totalN == 0 {
Expand All @@ -747,9 +759,21 @@ func (a *Arbiters) getDPoSV2RewardsV2(dposReward common.Fixed64, sponsor []byte,
totalNI += totalN
}

for sVoteAddr, N := range producersN {
addr, _ := sVoteAddr.ToAddress()
p := N / totalNI * float64(votesReward)
type kvData struct {
Addr common.Uint168
N float64
}
sortedProducersN := make([]kvData, 0)
for k, v := range producersN {
sortedProducersN = append(sortedProducersN, kvData{k, v})
}
sort.Slice(sortedProducersN, func(i, j int) bool {
return sortedProducersN[i].Addr.Compare(sortedProducersN[j].Addr) < 0
})

for _, producerN := range sortedProducersN {
addr, _ := producerN.Addr.ToAddress()
p := producerN.N / totalNI * float64(votesReward)
rewards[addr] += common.Fixed64(p)
log.Debugf("getDPoSV2Rewards addr:%s, p:%s, reward:%s \n", addr, common.Fixed64(p), rewards[addr])
}
Expand Down
18 changes: 17 additions & 1 deletion dpos/state/keyframe.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ func (s *StateKeyFrame) snapshot() *StateKeyFrame {
SpecialTxHashes: make(map[common.Uint256]struct{}),
PreBlockArbiters: make(map[string]struct{}),
ProducerDepositMap: make(map[common.Uint168]struct{}),

EmergencyInactiveArbiters: make(map[string]struct{}),
}
state.NodeOwnerKeys = copyStringMap(s.NodeOwnerKeys)
state.CurrentCRNodeOwnerKeys = copyStringMap(s.CurrentCRNodeOwnerKeys)
Expand Down Expand Up @@ -166,7 +168,21 @@ func (s *StateKeyFrame) snapshot() *StateKeyFrame {
state.PreBlockArbiters = copyStringSet(s.PreBlockArbiters)
state.ProducerDepositMap = copyDIDSet(s.ProducerDepositMap)

//todo add DPOSStartHeight and so on
state.EmergencyInactiveArbiters = copyStringSet(s.EmergencyInactiveArbiters)
state.LastRandomCandidateOwner = s.LastRandomCandidateOwner
state.VersionStartHeight = s.VersionStartHeight
state.VersionEndHeight = s.VersionEndHeight
state.LastRandomCandidateHeight = s.LastRandomCandidateHeight
state.DPOSWorkHeight = s.DPOSWorkHeight
state.ConsensusAlgorithm = s.ConsensusAlgorithm
state.LastBlockTimestamp = s.LastBlockTimestamp
state.NeedRevertToDPOSTX = s.NeedRevertToDPOSTX
state.NeedNextTurnDPOSInfo = s.NeedNextTurnDPOSInfo
state.NoProducers = s.NoProducers
state.NoClaimDPOSNode = s.NoClaimDPOSNode
state.RevertToPOWBlockHeight = s.RevertToPOWBlockHeight
state.LastIrreversibleHeight = s.LastIrreversibleHeight
state.DPOSStartHeight = s.DPOSStartHeight
state.DPoSV2ActiveHeight = s.DPoSV2ActiveHeight

return &state
Expand Down
28 changes: 27 additions & 1 deletion dpos/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,24 @@ func (p *Producer) GetExpiredNFTVotes() map[common.Uint168]payload.DetailedVoteI
return p.expiredNFTVotes
}

type sVoteDetailData struct {
Addr common.Uint256
VoteInfo payload.DetailedVoteInfo
}

func (p *Producer) GetTotalDPoSV2VoteRights() float64 {
var result float64
for _, sVoteDetail := range p.detailedDPoSV2Votes {
svdd := make([]sVoteDetailData, 0)
for referKey, votes := range sVoteDetail {
svdd = append(svdd, sVoteDetailData{referKey, votes})
sort.Slice(svdd, func(i, j int) bool {
return svdd[i].Addr.Compare(svdd[j].Addr) < 0
})
}
var totalN float64
for _, votes := range sVoteDetail {
for _, v := range svdd {
votes := v.VoteInfo
weightF := math.Log10(float64(votes.Info[0].LockTime-votes.BlockHeight) / 7200 * 10)
N := common.Fixed64(float64(votes.Info[0].Votes) * weightF)
totalN += float64(N)
Expand All @@ -251,7 +264,20 @@ func (p *Producer) GetTotalDPoSV2VoteRights() float64 {

func (p *Producer) GetNFTVotesRight(targetReferKey common.Uint256) float64 {
for _, sVoteDetail := range p.detailedDPoSV2Votes {
type sVoteDetailData struct {
Addr common.Uint256
VoteInfo payload.DetailedVoteInfo
}
svdd := make([]sVoteDetailData, 0)
for referKey, votes := range sVoteDetail {
svdd = append(svdd, sVoteDetailData{referKey, votes})
sort.Slice(svdd, func(i, j int) bool {
return svdd[i].Addr.Compare(svdd[j].Addr) < 0
})
}
for _, v := range svdd {
referKey := v.Addr
votes := v.VoteInfo
if referKey.IsEqual(targetReferKey) {
weightF := math.Log10(float64(votes.Info[0].LockTime-votes.BlockHeight) / 7200 * 10)
N := common.Fixed64(float64(votes.Info[0].Votes) * weightF)
Expand Down
3 changes: 3 additions & 0 deletions servers/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -2341,6 +2341,9 @@ func DposV2RewardInfo(param Params) map[string]interface{} {
claimable := Chain.GetState().DPoSV2RewardInfo[stakeAddress]
claiming := Chain.GetState().DposV2RewardClaimingInfo[stakeAddress]
claimed := Chain.GetState().DposV2RewardClaimedInfo[stakeAddress]
if claimable < common.ZeroFixed64 {
claimable = common.ZeroFixed64
}
result := RPCDposV2RewardInfo{
Address: addr,
Claimable: claimable.String(),
Expand Down

0 comments on commit 2c31444

Please sign in to comment.