Skip to content

Commit

Permalink
Merge 9621adb into 6c90b10
Browse files Browse the repository at this point in the history
  • Loading branch information
elvis88 committed May 7, 2019
2 parents 6c90b10 + 9621adb commit b32b28f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
13 changes: 13 additions & 0 deletions consensus/dpos/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ func (api *API) Candidate(name string) (interface{}, error) {
return nil, nil
}

// CandidateByHeight get candidate info of dpos
func (api *API) CandidateByHeight(height uint64, name string) (interface{}, error) {
sys, err := api.system()
if err != nil {
return nil, err
}
epcho, err := api.epcho(height)
if err != nil {
return nil, err
}
return sys.GetCandidateInfoByTime(name, api.dpos.config.epochTimeStamp(epcho))
}

// Candidates all candidates info
func (api *API) Candidates(detail bool) (interface{}, error) {
sys, err := api.system()
Expand Down
12 changes: 11 additions & 1 deletion consensus/dpos/dpos.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,20 @@ func (dpos *Dpos) Prepare(chain consensus.IChainReader, header *types.Header, tx
epcho := dpos.config.epoch(header.Time.Uint64())
if pepcho != epcho {
counter := func(from uint64, to uint64, index uint64) uint64 {
if to > 0 && from == 0 {
from = 1
}
timestamp := chain.GetHeaderByNumber(to).Time.Uint64() - chain.GetHeaderByNumber(from).Time.Uint64()
m := timestamp / dpos.config.mepochInterval()
n := timestamp % dpos.config.mepochInterval()
return m + n
offset := n / (dpos.config.blockInterval() * dpos.config.BlockFrequency)
if index < offset {
m += dpos.config.BlockFrequency
} else if index == offset {
n = n % (dpos.config.blockInterval() * dpos.config.BlockFrequency)
m += n / dpos.config.blockInterval()
}
return m
}
log.Debug("UpdateElectedCandidates", "prev", pepcho, "curr", epcho, "height", parent.Number.Uint64(), "time", parent.Time.Uint64())
sys.UpdateElectedCandidates(pepcho, epcho, parent.Number.Uint64(), counter)
Expand Down
40 changes: 32 additions & 8 deletions consensus/dpos/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,19 +485,43 @@ func (sys *System) UpdateElectedCandidates(pepcho uint64, epcho uint64, height u
return err
}

// not is first
// not is first & no changes
if pstate.Epcho != pstate.PreEpcho && pepcho == epcho {
return nil
}

// old actived candidates
oldcandidates := map[string]uint64{}
roldcandidates := map[uint64][]string{}
ppstate, err := sys.GetState(pstate.PreEpcho)
if err != nil {
return err
}
for index, candidate := range ppstate.ActivatedCandidateSchedule {
oldcandidates[candidate] = uint64(index)
for n := uint64(len(ppstate.ActivatedCandidateSchedule)); n > 0; n-- {
index := n - 1
candidate := ppstate.ActivatedCandidateSchedule[index]
if index >= sys.config.CandidateScheduleSize {
h := pstate.OffCandidateHeight[index-sys.config.CandidateScheduleSize]
i := pstate.OffCandidateSchedule[index-sys.config.CandidateScheduleSize]
cnt := counter(ppstate.Height, h, i)
if _, ok := roldcandidates[i]; !ok {
roldcandidates[i] = []string{}
} else {
for _, c := range roldcandidates[index] {
cnt -= oldcandidates[c]
}
}
roldcandidates[i] = append(roldcandidates[i], candidate)
oldcandidates[candidate] += cnt
} else {
cnt := counter(ppstate.Height, height, index)
if _, ok := roldcandidates[index]; ok {
for _, c := range roldcandidates[index] {
cnt -= oldcandidates[c]
}
}
oldcandidates[candidate] += cnt
}
}

candidateInfoArray, err := sys.GetCandidates()
Expand All @@ -516,13 +540,13 @@ func (sys *System) UpdateElectedCandidates(pepcho uint64, epcho uint64, height u
activeTotalQuantity := big.NewInt(0)
totalQuantity := big.NewInt(0)
for _, candidateInfo := range candidateInfoArray {
if index, ok := oldcandidates[candidateInfo.Name]; ok {
candidateInfo.Counter += counter(ppstate.Height, height, index)
}
totalQuantity = new(big.Int).Add(totalQuantity, candidateInfo.Quantity)
if cnt, ok := oldcandidates[candidateInfo.Name]; ok {
candidateInfo.Counter += cnt
}
if !candidateInfo.invalid() && (!pstate.Dpos || strings.Compare(candidateInfo.Name, sys.config.SystemName) != 0) {
activatedCandidateSchedule = append(activatedCandidateSchedule, candidateInfo.Name)
if uint64(len(activatedCandidateSchedule)) <= sys.config.CandidateScheduleSize {
if uint64(len(activatedCandidateSchedule)) <= n {
activatedCandidateSchedule = append(activatedCandidateSchedule, candidateInfo.Name)
activeTotalQuantity = new(big.Int).Add(activeTotalQuantity, candidateInfo.TotalQuantity)
}
}
Expand Down

0 comments on commit b32b28f

Please sign in to comment.