filter zero address in function GetCandidates

This commit is contained in:
Daniel Liu 2023-07-14 11:46:36 +08:00
parent 4a59bdf15a
commit e2359d9b8c
3 changed files with 11 additions and 16 deletions

View file

@ -93,16 +93,17 @@ func GetCandidates(statedb *StateDB) []common.Address {
slot := slotValidatorMapping["candidates"] slot := slotValidatorMapping["candidates"]
slotHash := common.BigToHash(new(big.Int).SetUint64(slot)) slotHash := common.BigToHash(new(big.Int).SetUint64(slot))
arrLength := statedb.GetState(common.HexToAddress(common.MasternodeVotingSMC), slotHash) arrLength := statedb.GetState(common.HexToAddress(common.MasternodeVotingSMC), slotHash)
keys := []common.Hash{} count := arrLength.Big().Uint64()
for i := uint64(0); i < arrLength.Big().Uint64(); i++ { rets := make([]common.Address, 0, count)
for i := uint64(0); i < count; i++ {
key := GetLocDynamicArrAtElement(slotHash, i, 1) key := GetLocDynamicArrAtElement(slotHash, i, 1)
keys = append(keys, key)
}
rets := []common.Address{}
for _, key := range keys {
ret := statedb.GetState(common.HexToAddress(common.MasternodeVotingSMC), key) ret := statedb.GetState(common.HexToAddress(common.MasternodeVotingSMC), key)
if !ret.IsZero() {
rets = append(rets, common.HexToAddress(ret.Hex())) rets = append(rets, common.HexToAddress(ret.Hex()))
} }
}
return rets return rets
} }

View file

@ -238,10 +238,8 @@ func AttachConsensusV1Hooks(adaptor *XDPoS.XDPoS, bc *core.BlockChain, chainConf
if err != nil { if err != nil {
return nil, err return nil, err
} }
if address.String() != "xdc0000000000000000000000000000000000000000" {
candidates = append(candidates, utils.Masternode{Address: address, Stake: v}) candidates = append(candidates, utils.Masternode{Address: address, Stake: v})
} }
}
// sort candidates by stake descending // sort candidates by stake descending
sort.Slice(candidates, func(i, j int) bool { sort.Slice(candidates, func(i, j int) bool {
return candidates[i].Stake.Cmp(candidates[j].Stake) >= 0 return candidates[i].Stake.Cmp(candidates[j].Stake) >= 0

View file

@ -756,11 +756,9 @@ func (s *PublicBlockChainAPI) GetCandidateStatus(ctx context.Context, coinbaseAd
candidatesAddresses := state.GetCandidates(statedb) candidatesAddresses := state.GetCandidates(statedb)
for _, address := range candidatesAddresses { for _, address := range candidatesAddresses {
v := state.GetCandidateCap(statedb, address) v := state.GetCandidateCap(statedb, address)
if address.String() != "xdc0000000000000000000000000000000000000000" {
candidates = append(candidates, utils.Masternode{Address: address, Stake: v}) candidates = append(candidates, utils.Masternode{Address: address, Stake: v})
} }
} }
}
if err != nil || len(candidates) == 0 { if err != nil || len(candidates) == 0 {
log.Debug("Candidates list cannot be found", "len(candidates)", len(candidates), "err", err) log.Debug("Candidates list cannot be found", "len(candidates)", len(candidates), "err", err)
result[fieldSuccess] = false result[fieldSuccess] = false
@ -882,11 +880,9 @@ func (s *PublicBlockChainAPI) GetCandidates(ctx context.Context, epoch rpc.Epoch
candidatesAddresses := state.GetCandidates(statedb) candidatesAddresses := state.GetCandidates(statedb)
for _, address := range candidatesAddresses { for _, address := range candidatesAddresses {
v := state.GetCandidateCap(statedb, address) v := state.GetCandidateCap(statedb, address)
if address.String() != "xdc0000000000000000000000000000000000000000" {
candidates = append(candidates, utils.Masternode{Address: address, Stake: v}) candidates = append(candidates, utils.Masternode{Address: address, Stake: v})
} }
} }
}
if err != nil || len(candidates) == 0 { if err != nil || len(candidates) == 0 {
log.Debug("Candidates list cannot be found", "len(candidates)", len(candidates), "err", err) log.Debug("Candidates list cannot be found", "len(candidates)", len(candidates), "err", err)