add GetVoterCap func

This commit is contained in:
Tuna 2019-01-05 16:43:46 +07:00
parent 237111a5ba
commit b7306c639e
2 changed files with 15 additions and 6 deletions

View file

@ -385,12 +385,7 @@ func GetRewardBalancesRate(foundationWalletAddr common.Address, state *state.Sta
// Get voters capacities.
voterCaps := make(map[common.Address]*big.Int)
for _, voteAddr := range voters {
voterCap, err := GetVoterCap(state, masterAddr, voteAddr)
if err != nil {
log.Error("Fail to get vote capacity", "error", err)
return nil, err
}
voterCap := GetVoterCap(state, masterAddr, voteAddr)
totalCap.Add(totalCap, voterCap)
voterCaps[voteAddr] = voterCap
}

View file

@ -106,3 +106,17 @@ func GetVoters(statedb *state.StateDB, candidate common.Address) []common.Addres
fmt.Printf("Execution time: %s\n", elapsed)
return rets
}
func GetVoterCap(state *state.StateDB, candidate, voter common.Address) *big.Int {
//validatorsState[_candidate].voters[_voter]
start := time.Now()
fmt.Printf("--------GetVoterCap---------\n")
slot := slotValidatorMapping["validatorsState"]
locValidatorsState := getLocMappingAtKey(candidate.Hash(), slot)
locCandidateVoters := locValidatorsState.Add(locValidatorsState, new(big.Int).SetUint64(uint64(3)))
locVoters := getLocMappingAtKey(voter.Hash(), locCandidateVoters.Uint64())
ret := state.GetState(common.HexToAddress(common.MasternodeVotingSMC), common.BigToHash(locVoters))
elapsed := time.Since(start)
fmt.Printf("Execution time: %s\n", elapsed)
return ret.Big()
}