mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-08 05:54:29 +00:00
fix eth.getCandidateStatus api
This commit is contained in:
parent
c95843f608
commit
85db85cc20
1 changed files with 51 additions and 28 deletions
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -704,7 +705,7 @@ func (s *PublicBlockChainAPI) GetMasternodes(ctx context.Context, b *types.Block
|
||||||
func (s *PublicBlockChainAPI) GetCandidateStatus(ctx context.Context, coinbaseAddress common.Address, epochNumber rpc.EpochNumber) (string, error) {
|
func (s *PublicBlockChainAPI) GetCandidateStatus(ctx context.Context, coinbaseAddress common.Address, epochNumber rpc.EpochNumber) (string, error) {
|
||||||
var (
|
var (
|
||||||
block *types.Block
|
block *types.Block
|
||||||
masternodes, penaltyList, candidates, proposedList []common.Address
|
masternodes, penaltyList []common.Address
|
||||||
penalties []byte
|
penalties []byte
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
|
|
@ -735,6 +736,53 @@ func (s *PublicBlockChainAPI) GetCandidateStatus(ctx context.Context, coinbaseAd
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// read smart contract to get candidate list
|
||||||
|
client, err := s.b.GetIPCClient()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
addr := common.HexToAddress(common.MasternodeVotingSMC)
|
||||||
|
validator, err := contractValidator.NewXDCValidator(addr, client)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
opts := new(bind.CallOpts)
|
||||||
|
var (
|
||||||
|
candidateAddresses []common.Address
|
||||||
|
candidates []XDPoS.Masternode
|
||||||
|
)
|
||||||
|
|
||||||
|
candidateAddresses, err = validator.GetCandidates(opts)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
for _, address := range candidateAddresses {
|
||||||
|
v, err := validator.GetCandidateCap(opts, address)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if address.String() != "xdc0000000000000000000000000000000000000000" {
|
||||||
|
candidates = append(candidates, XDPoS.Masternode{Address: address, Stake: v})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// sort candidates by stake descending
|
||||||
|
sort.Slice(candidates, func(i, j int) bool {
|
||||||
|
return candidates[i].Stake.Cmp(candidates[j].Stake) >= 0
|
||||||
|
})
|
||||||
|
isTopCandidate := false // is candidates in top 150
|
||||||
|
status := ""
|
||||||
|
for i := 0; i < len(candidates); i++ {
|
||||||
|
if candidates[i].Address == coinbaseAddress {
|
||||||
|
status = statusProposed
|
||||||
|
if i < common.MaxMasternodes {
|
||||||
|
isTopCandidate = true
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if isTopCandidate == false {
|
||||||
|
return status, nil
|
||||||
|
}
|
||||||
// look up recent checkpoint headers to get penalty list
|
// look up recent checkpoint headers to get penalty list
|
||||||
for i := 0; i <= common.LimitPenaltyEpoch; i++ {
|
for i := 0; i <= common.LimitPenaltyEpoch; i++ {
|
||||||
if blockNum > uint64(i) * epoch {
|
if blockNum > uint64(i) * epoch {
|
||||||
|
|
@ -747,6 +795,7 @@ func (s *PublicBlockChainAPI) GetCandidateStatus(ctx context.Context, coinbaseAd
|
||||||
penalties = append(penalties, blockCheckpoint.Penalties()...)
|
penalties = append(penalties, blockCheckpoint.Penalties()...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(penalties) > 0 {
|
if len(penalties) > 0 {
|
||||||
penaltyList = common.ExtractAddressFromBytes(penalties)
|
penaltyList = common.ExtractAddressFromBytes(penalties)
|
||||||
for _, pen := range penaltyList {
|
for _, pen := range penaltyList {
|
||||||
|
|
@ -755,33 +804,7 @@ func (s *PublicBlockChainAPI) GetCandidateStatus(ctx context.Context, coinbaseAd
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return status, nil
|
||||||
// read smart contract to get candidate list
|
|
||||||
client, err := s.b.GetIPCClient()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
addr := common.HexToAddress(common.MasternodeVotingSMC)
|
|
||||||
validator, err := contractValidator.NewXDCValidator(addr, client)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
opts := new(bind.CallOpts)
|
|
||||||
candidates, err = validator.GetCandidates(opts)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
// exclude masternodes
|
|
||||||
proposedList = common.RemoveItemFromArray(candidates, masternodes)
|
|
||||||
// exclude penalties
|
|
||||||
proposedList = common.RemoveItemFromArray(proposedList, penaltyList)
|
|
||||||
|
|
||||||
for _, proposed := range proposedList {
|
|
||||||
if coinbaseAddress == proposed {
|
|
||||||
return statusProposed, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "", nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CallArgs represents the arguments for a call.
|
// CallArgs represents the arguments for a call.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue