Skip to content

Commit

Permalink
Merge branch 'dev' into start_fork
Browse files Browse the repository at this point in the history
  • Loading branch information
erickyan86 committed Jun 20, 2019
2 parents bac540e + fc98eae commit 238660f
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 78 deletions.
8 changes: 5 additions & 3 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,13 @@ type IEngine interface {

GetDelegatedByTime(state *state.StateDB, candidate string, timestamp uint64) (stake *big.Int, err error)

GetLatestEpoch(state *state.StateDB) (epoch uint64, err error)
//GetLatestEpoch(state *state.StateDB) (epoch uint64, err error)

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

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

GetEpoch(state *state.StateDB, t uint64, curEpoch uint64) (epoch uint64, time uint64, err error)

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

Expand Down
12 changes: 10 additions & 2 deletions consensus/dpos/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,34 @@ func (api *API) Epoch(height uint64) (uint64, error) {

// PrevEpoch get prev epoch number by epoch
func (api *API) PrevEpoch(epoch uint64) (uint64, error) {
var e uint64

if epoch == 0 {
epoch, _ = api.epoch(api.chain.CurrentHeader().Number.Uint64())
}
state, err := api.chain.StateAt(api.chain.CurrentHeader().Root)
if err != nil {
return 0, err
}
return api.dpos.GetPrevEpoch(state, epoch)

e, _, err = api.dpos.GetEpoch(state, 1, epoch)
return e, err
}

// NextEpoch get next epoch number by epoch
func (api *API) NextEpoch(epoch uint64) (uint64, error) {
var e uint64

if epoch == 0 {
epoch, _ = api.epoch(api.chain.CurrentHeader().Number.Uint64())
}
state, err := api.chain.StateAt(api.chain.CurrentHeader().Root)
if err != nil {
return 0, err
}
return api.dpos.GetNextEpoch(state, epoch)

e, _, err = api.dpos.GetEpoch(state, 2, epoch)
return e, err
}

// CandidatesSize get candidates size
Expand Down
115 changes: 82 additions & 33 deletions consensus/dpos/dpos.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,46 +638,95 @@ func (dpos *Dpos) GetDelegatedByTime(state *state.StateDB, candidate string, tim
return new(big.Int).Mul(candidateInfo.Quantity, sys.config.unitStake()), nil
}

// GetLatestEpoch get latest epoch
func (dpos *Dpos) GetLatestEpoch(state *state.StateDB) (epoch uint64, err error) {
// GetEpoch get epoch and epoch start time by type
func (dpos *Dpos) GetEpoch(state *state.StateDB, t uint64, curEpoch uint64) (epoch uint64, time uint64, err error) {
//new sys
sys := NewSystem(state, dpos.config)
return sys.GetLastestEpoch()
}
if t == 0 {
//get latest epoch
epoch, err = sys.GetLastestEpoch()
} else if t == 1 {
//get pre epoch
var gstate *GlobalState
gstate, err = sys.GetState(curEpoch)
if gstate == nil {
err = errors.New("gstate not found")
} else {
epoch = gstate.PreEpoch
}
} else if t == 2 {
//get next epoch
var latest uint64
var gstate *GlobalState
latest, err = sys.GetLastestEpoch()
for {
curEpoch++
if curEpoch > latest {
err = errors.New("not found")
break
}
gstate, err = sys.GetState(curEpoch)
if err != nil && !strings.Contains(err.Error(), "not found") {
break
}
if gstate != nil {
epoch = gstate.Epoch
}
}
} else {
err = errors.New("type error")
}

// GetPrevEpoch get pre epoch
func (dpos *Dpos) GetPrevEpoch(state *state.StateDB, epoch uint64) (uint64, error) {
sys := NewSystem(state, dpos.config)
gstate, err := sys.GetState(epoch)
if err != nil {
return 0, err
}
if gstate == nil {
return 0, fmt.Errorf("not found")
return 0, 0, err
}
return gstate.PreEpoch, nil

//get epoch time epoch must > 0
time = dpos.config.epochTimeStamp(epoch)

return epoch, time, nil
}

// GetLatestEpoch get latest epoch
// func (dpos *Dpos) GetLatestEpoch(state *state.StateDB) (epoch uint64, err error) {
// sys := NewSystem(state, dpos.config)
// return sys.GetLastestEpoch()
// }

// GetPrevEpoch get pre epoch
// func (dpos *Dpos) GetPrevEpoch(state *state.StateDB, epoch uint64) (uint64, error) {
// sys := NewSystem(state, dpos.config)
// gstate, err := sys.GetState(epoch)
// if err != nil {
// return 0, err
// }
// if gstate == nil {
// return 0, fmt.Errorf("not found")
// }
// return gstate.PreEpoch, nil
// }

// GetNextEpoch get next epoch
func (dpos *Dpos) GetNextEpoch(state *state.StateDB, epoch uint64) (uint64, error) {
sys := NewSystem(state, dpos.config)
latest, err := sys.GetLastestEpoch()
if err != nil {
return 0, err
}
for {
epoch++
if epoch > latest {
return 0, nil
}
gstate, err := sys.GetState(epoch)
if err != nil && !strings.Contains(err.Error(), "not found") {
return 0, err
}
if gstate != nil {
return gstate.Epoch, nil
}
}
}
// func (dpos *Dpos) GetNextEpoch(state *state.StateDB, epoch uint64) (uint64, error) {
// sys := NewSystem(state, dpos.config)
// latest, err := sys.GetLastestEpoch()
// if err != nil {
// return 0, err
// }
// for {
// epoch++
// if epoch > latest {
// return 0, nil
// }
// gstate, err := sys.GetState(epoch)
// if err != nil && !strings.Contains(err.Error(), "not found") {
// return 0, err
// }
// if gstate != nil {
// return gstate.Epoch, nil
// }
// }
// }

// GetActivedCandidateSize get actived candidate size
func (dpos *Dpos) GetActivedCandidateSize(state *state.StateDB, epoch uint64) (uint64, error) {
Expand Down
10 changes: 2 additions & 8 deletions processor/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,7 @@ type EngineContext interface {

GetDelegatedByTime(state *state.StateDB, candidate string, timestamp uint64) (stake *big.Int, err error)

GetLatestEpoch(state *state.StateDB) (epoch uint64, err error)

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

GetNextEpoch(state *state.StateDB, epoch uint64) (neoch uint64, err error)
GetEpoch(state *state.StateDB, t uint64, curEpoch uint64) (epoch uint64, time uint64, err error)

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

Expand All @@ -106,9 +102,7 @@ func NewEVMContext(sender common.Name, to common.Name, assetID uint64, gasPrice
return vm.Context{
GetHash: GetHashFn(header, chain),
GetDelegatedByTime: chain.GetDelegatedByTime,
GetLatestEpoch: chain.GetLatestEpoch,
GetPrevEpoch: chain.GetPrevEpoch,
GetNextEpoch: chain.GetNextEpoch,
GetEpoch: chain.GetEpoch,
GetActivedCandidateSize: chain.GetActivedCandidateSize,
GetActivedCandidate: chain.GetActivedCandidate,
GetVoterStake: chain.GetVoterStake,
Expand Down
19 changes: 4 additions & 15 deletions processor/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1053,25 +1053,14 @@ func opGetEpoch(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack
epochID, arg := stack.pop(), stack.pop()
t := arg.Uint64()
ID := epochID.Uint64()
var num uint64
var err error
if t == 0 {
//get latest epoch
num, err = evm.Context.GetLatestEpoch(evm.StateDB)
} else if t == 1 {
//get pre epoch
num, err = evm.Context.GetPrevEpoch(evm.StateDB, ID)
} else if t == 2 {
//get next epoch
num, err = evm.Context.GetNextEpoch(evm.StateDB, ID)
} else {
err = errors.New("type error")
}

//get
num, epochTime, err := evm.Context.GetEpoch(evm.StateDB, t, ID)
if err != nil {
stack.push(evm.interpreter.intPool.getZero())
stack.push(evm.interpreter.intPool.getZero())
} else {
stack.push(evm.interpreter.intPool.get().SetUint64(num))
stack.push(evm.interpreter.intPool.get().SetUint64(epochTime))
}
return nil, nil
}
Expand Down
10 changes: 4 additions & 6 deletions processor/vm/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,10 @@ func NewEnv(cfg *Config) *vm.EVM {
GetDelegatedByTime: func(*state.StateDB, string, uint64) (*big.Int, error) {
return big.NewInt(0), nil
},
GetLatestEpoch: func(state *state.StateDB) (epoch uint64, err error) {
return 1, nil
},
//GetPrevEpoch
GetPrevEpoch: func(state *state.StateDB, epoch uint64) (peoch uint64, err error) {
return 2, nil

//GetEpoch
GetEpoch: func(state *state.StateDB, t uint64, epoch uint64) (peoch uint64, time uint64, err error) {
return 2, 0, nil
},
//GetActivedCandidateSize
GetActivedCandidateSize: func(state *state.StateDB, epoch uint64) (size uint64, err error) {
Expand Down
18 changes: 7 additions & 11 deletions processor/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,8 @@ type (
GetHashFunc func(uint64) common.Hash
// GetDelegatedByTimeFunc returns the delegated balance
GetDelegatedByTimeFunc func(*state.StateDB, string, uint64) (stake *big.Int, err error)
//GetLatestEpochFunc
GetLatestEpochFunc func(state *state.StateDB) (epoch uint64, err error)
//GetPrevEpoch
GetPrevEpochFunc func(state *state.StateDB, epoch uint64) (pecho uint64, err error)
//GetNextEpoch
GetNextEpochFunc func(state *state.StateDB, epoch uint64) (pecho uint64, err error)
//GetEpoch
GetEpochFunc func(state *state.StateDB, t uint64, curEpoch uint64) (epoch uint64, time uint64, err error)
//GetActivedCandidateSize
GetActivedCandidateSizeFunc func(state *state.StateDB, epoch uint64) (size uint64, err error)
//GetActivedCandidate
Expand All @@ -54,11 +50,11 @@ type (
// Context provides the EVM with auxiliary information. Once provided
// it shouldn't be modified.
type Context struct {
GetHash GetHashFunc
GetDelegatedByTime GetDelegatedByTimeFunc
GetLatestEpoch GetLatestEpochFunc
GetPrevEpoch GetPrevEpochFunc
GetNextEpoch GetNextEpochFunc
GetHash GetHashFunc
GetDelegatedByTime GetDelegatedByTimeFunc
//GetLatestEpoch GetLatestEpochFunc
//GetPrevEpoch GetPrevEpochFunc
GetEpoch GetEpochFunc
GetActivedCandidateSize GetActivedCandidateSizeFunc
GetActivedCandidate GetActivedCandidateFunc
GetVoterStake GetVoterStakeFunc
Expand Down

0 comments on commit 238660f

Please sign in to comment.