Skip to content

Commit

Permalink
Merge 85ca4c6 into 6c90cc5
Browse files Browse the repository at this point in the history
  • Loading branch information
elvis88 committed May 21, 2019
2 parents 6c90cc5 + 85ca4c6 commit 8db9566
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 12 deletions.
2 changes: 2 additions & 0 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ type IEngine interface {

GetPrevEpoch(state *state.StateDB, epoch uint64) (pecho uint64, err error)

GetNextEpoch(state *state.StateDB, epoch uint64) (necho uint64, err error)

GetActivedCandidateSize(state *state.StateDB, epoch uint64) (size uint64, err error)

GetActivedCandidate(state *state.StateDB, epoch uint64, index uint64) (name string, stake *big.Int, totalVote *big.Int, counter uint64, actualCounter uint64, replace uint64, err error)
Expand Down
7 changes: 4 additions & 3 deletions consensus/dpos/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,14 @@ func (api *API) GetActivedCandidate(epcho uint64, index uint64) (interface{}, er
return ret, nil
}

// GetCandidateStake candidate delegate stake
func (api *API) GetCandidateStake(epcho uint64, candidate string) (*big.Int, error) {
// GetCandidate candidate info
func (api *API) GetCandidate(epcho uint64, candidate string) (interface{}, error) {
state, err := api.chain.StateAt(api.chain.CurrentHeader().Root)
if err != nil {
return big.NewInt(0), err
}
return api.dpos.GetDelegatedByTime(state, candidate, api.dpos.config.epochTimeStamp(epcho))
sys := NewSystem(state, api.dpos.config)
return sys.GetCandidateInfoByTime(candidate, api.dpos.config.epochTimeStamp(epcho))
}

// GetVoterStake voter stake
Expand Down
3 changes: 3 additions & 0 deletions consensus/dpos/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type IDB interface {
GetCandidates() ([]*CandidateInfo, error)
CandidatesSize() (uint64, error)

SetCandidateByEpcho(uint64, *CandidateInfo) error
GetCandidateByEpcho(uint64, string) (*CandidateInfo, error)

SetAvailableQuantity(uint64, string, *big.Int) error
GetAvailableQuantity(uint64, string) (*big.Int, error)

Expand Down
33 changes: 29 additions & 4 deletions consensus/dpos/dpos.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ func (dpos *Dpos) GetLatestEpoch(state *state.StateDB) (epoch uint64, err error)
return sys.GetLastestEpcho()
}

// GetPrevEpcho get pre epcho
// GetPrevEpoch get pre epcho
func (dpos *Dpos) GetPrevEpoch(state *state.StateDB, epoch uint64) (uint64, error) {
sys := NewSystem(state, dpos.config)
gstate, err := sys.GetState(epoch)
Expand All @@ -442,6 +442,28 @@ func (dpos *Dpos) GetPrevEpoch(state *state.StateDB, epoch uint64) (uint64, erro
return gstate.PreEpcho, nil
}

// GetNextEpoch get next epcho
func (dpos *Dpos) GetNextEpoch(state *state.StateDB, epoch uint64) (uint64, error) {
sys := NewSystem(state, dpos.config)
latest, err := sys.GetLastestEpcho()
if err != nil {
return 0, err
}
for {
epoch++
if epoch >= latest {
return 0, fmt.Errorf("overflow")
}
gstate, err := sys.GetState(epoch)
if err != nil {
return 0, err
}
if gstate != nil {
return gstate.Epcho, nil
}
}
}

// GetActivedCandidateSize get actived candidate size
func (dpos *Dpos) GetActivedCandidateSize(state *state.StateDB, epcho uint64) (uint64, error) {
sys := NewSystem(state, dpos.config)
Expand All @@ -464,18 +486,21 @@ func (dpos *Dpos) GetActivedCandidate(state *state.StateDB, epcho uint64, index
}

candidate := gstate.ActivatedCandidateSchedule[index]
prevCandidateInfo, err := sys.GetCandidateInfoByTime(candidate, dpos.config.epochTimeStamp(gstate.PreEpcho))
prevCandidateInfo, err := sys.GetCandidateByEpcho(gstate.PreEpcho, candidate)
if err != nil {
return "", big.NewInt(0), big.NewInt(0), 0, 0, 0, err
}

candidateInfo, err := sys.GetCandidateInfoByTime(candidate, dpos.config.epochTimeStamp(gstate.Epcho))
candidateInfo, err := sys.GetCandidateByEpcho(gstate.Epcho, candidate)
if err != nil {
return "", big.NewInt(0), big.NewInt(0), 0, 0, 0, err
}

if prevCandidateInfo == nil {
prevCandidateInfo = &CandidateInfo{}
}
if candidateInfo == nil {
return "", big.NewInt(0), big.NewInt(0), 0, 0, 0, err
candidateInfo = &CandidateInfo{}
}

counter := candidateInfo.Counter
Expand Down
29 changes: 27 additions & 2 deletions consensus/dpos/ldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package dpos
import (
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"math/big"
"sort"
"strings"
Expand Down Expand Up @@ -76,6 +76,31 @@ func NewLDB(db IDatabase) (*LDB, error) {
return ldb, nil
}

// SetCandidateByEpcho update candidate info
func (db *LDB) SetCandidateByEpcho(epcho uint64, candidate *CandidateInfo) error {
key := strings.Join([]string{CandidateKeyPrefix, fmt.Sprintf("0x%x_%s", epcho, candidate.Name)}, Separator)
if val, err := rlp.EncodeToBytes(candidate); err != nil {
return err
} else if err := db.Put(key, val); err != nil {
return err
}
return nil
}

// GetCandidateByEpcho update candidate info
func (db *LDB) GetCandidateByEpcho(epcho uint64, candidate string) (*CandidateInfo, error) {
key := strings.Join([]string{CandidateKeyPrefix, fmt.Sprintf("0x%x_%s", epcho, candidate)}, Separator)
candidateInfo := &CandidateInfo{}
if val, err := db.Get(key); err != nil {
return nil, err
} else if val == nil {
return nil, nil
} else if err := rlp.DecodeBytes(val, candidateInfo); err != nil {
return nil, err
}
return candidateInfo, nil
}

// SetCandidate update candidate info
func (db *LDB) SetCandidate(candidate *CandidateInfo) error {
if candidate.Name != CandidateHead && len(candidate.PrevKey) == 0 && len(candidate.NextKey) == 0 {
Expand Down Expand Up @@ -388,7 +413,7 @@ func (db *LDB) GetState(epcho uint64) (*GlobalState, error) {
if val, err := db.Get(key); err != nil {
return nil, err
} else if val == nil {
return nil, errors.New("GlobalState not exist")
return nil, nil
} else if err := rlp.DecodeBytes(val, gstate); err != nil {
return nil, err
}
Expand Down
7 changes: 5 additions & 2 deletions consensus/dpos/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"math/big"
"strings"

"github.com/ethereum/go-ethereum/log"
"github.com/fractalplatform/fractal/state"
"github.com/fractalplatform/fractal/types"
)
Expand Down Expand Up @@ -552,6 +551,11 @@ func (sys *System) UpdateElectedCandidates(pepcho uint64, epcho uint64, number u
activeTotalQuantity = new(big.Int).Add(activeTotalQuantity, candidateInfo.TotalQuantity)
}
}
if pstate.Epcho != pstate.PreEpcho {
if err := sys.SetCandidateByEpcho(pepcho, candidateInfo); err != nil {
return err
}
}
candidateInfo.TotalQuantity = candidateInfo.Quantity
if err := sys.SetCandidate(candidateInfo); err != nil {
return err
Expand Down Expand Up @@ -631,7 +635,6 @@ func (sys *System) getAvailableQuantity(epcho uint64, voter string) (*big.Int, e
timestamp = sys.config.epochTimeStamp(gstate.PreEpcho)
}
bquantity, err := sys.GetBalanceByTime(voter, timestamp)
log.Debug("GetAvailableQuantity Sanpshot", "epcho", gstate.Epcho, "time", timestamp, "name", voter, "q", bquantity, "error", err)
if err != nil {
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions processor/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ type EngineContext interface {

GetPrevEpoch(state *state.StateDB, epoch uint64) (peoch uint64, err error)

GetNextEpoch(state *state.StateDB, epoch uint64) (neoch uint64, err error)

GetActivedCandidateSize(state *state.StateDB, epoch uint64) (size uint64, err error)

GetActivedCandidate(state *state.StateDB, epoch uint64, index uint64) (name string, stake *big.Int, totalVote *big.Int, counter uint64, actualCounter uint64, replace uint64, err error)
Expand Down Expand Up @@ -113,6 +115,7 @@ func NewEVMContext(sender common.Name, assetID uint64, gasPrice *big.Int, header
//Engine: chain,
GetLatestEpoch: chain.GetLatestEpoch,
GetPrevEpoch: chain.GetPrevEpoch,
GetNextEpoch: chain.GetNextEpoch,
GetActivedCandidateSize: chain.GetActivedCandidateSize,
GetActivedCandidate: chain.GetActivedCandidate,
GetVoterStake: chain.GetVoterStake,
Expand Down
2 changes: 1 addition & 1 deletion processor/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ func opGetEpoch(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack
if id == 0 {
num, err = evm.Context.GetLatestEpoch(evm.StateDB)
} else {
num, err = evm.Context.GetPrevEpoch(evm.StateDB, id)
num, err = evm.Context.GetNextEpoch(evm.StateDB, id)
}
if err != nil {
stack.push(evm.interpreter.intPool.getZero())
Expand Down
3 changes: 3 additions & 0 deletions processor/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type (
GetLatestEpochFunc func(state *state.StateDB) (epoch uint64, err error)
//GetPrevEpcho
GetPrevEpochFunc func(state *state.StateDB, epoch uint64) (pecho uint64, err error)
//GetNextEpoch
GetNextEpochFunc func(state *state.StateDB, epoch uint64) (pecho uint64, err error)
//GetActivedCandidateSize
GetActivedCandidateSizeFunc func(state *state.StateDB, epoch uint64) (size uint64, err error)
//GetActivedCandidate
Expand All @@ -60,6 +62,7 @@ type Context struct {
//
GetLatestEpoch GetLatestEpochFunc
GetPrevEpoch GetPrevEpochFunc
GetNextEpoch GetNextEpochFunc
GetActivedCandidateSize GetActivedCandidateSizeFunc
GetActivedCandidate GetActivedCandidateFunc
GetVoterStake GetVoterStakeFunc
Expand Down

0 comments on commit 8db9566

Please sign in to comment.