Skip to content

Commit

Permalink
Merge pull request icon-project#23 from icon-project/feature/IC2-55-n…
Browse files Browse the repository at this point in the history
…etworkvalue-terminology

networkvalue registered as members of state.
  • Loading branch information
dhlee-icon authored Jan 27, 2021
2 parents 5ad3c2d + 5a5bf84 commit e7e4f01
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 154 deletions.
19 changes: 9 additions & 10 deletions icon/chainscore.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/icon-project/goloop/common/errors"
"github.com/icon-project/goloop/common/log"
"github.com/icon-project/goloop/icon/iiss"
"github.com/icon-project/goloop/icon/iiss/icstate"
"github.com/icon-project/goloop/module"
"github.com/icon-project/goloop/service/contract"
"github.com/icon-project/goloop/service/scoreapi"
Expand Down Expand Up @@ -817,31 +816,31 @@ func (s *chainScore) Install(param []byte) error {
}

es := s.cc.GetExtensionState().(*iiss.ExtensionStateImpl)
if err = icstate.SetIISSBlockHeight(es.State, iconConfig.IISSBlockHeight.Int64()); err != nil {
if err = es.State.SetIISSBlockHeight(iconConfig.IISSBlockHeight.Int64()); err != nil {
return err
}
if err = icstate.SetTermPeriod(es.State, iconConfig.TermPeriod.Int64()); err != nil {
if err = es.State.SetTermPeriod(iconConfig.TermPeriod.Int64()); err != nil {
return err
}
if err = icstate.SetCalculatePeriod(es.State, iconConfig.CalculationPeriod.Int64()); err != nil {
if err = es.State.SetCalculatePeriod(iconConfig.CalculationPeriod.Int64()); err != nil {
return err
}
if err = icstate.SetIRep(es.State, iconConfig.Irep.Value()); err != nil {
if err = es.State.SetIRep(iconConfig.Irep.Value()); err != nil {
return err
}
if err = icstate.SetRRep(es.State, iconConfig.Rrep.Value()); err != nil {
if err = es.State.SetRRep(iconConfig.Rrep.Value()); err != nil {
return err
}
if err = icstate.SetMainPRepCount(es.State, iconConfig.MainPRepCount.Int64()); err != nil {
if err = es.State.SetMainPRepCount(iconConfig.MainPRepCount.Int64()); err != nil {
return err
}
if err = icstate.SetSubPRepCount(es.State, iconConfig.SubPRepCount.Int64()); err != nil {
if err = es.State.SetSubPRepCount(iconConfig.SubPRepCount.Int64()); err != nil {
return err
}
if err = icstate.SetBondRequirement(es.State, iconConfig.BondRequirement.Int64()); err != nil {
if err = es.State.SetBondRequirement(iconConfig.BondRequirement.Int64()); err != nil {
return err
}
if err = icstate.SetLockVariables(es.State, iconConfig.LockMin.Value(), iconConfig.LockMax.Value()); err != nil {
if err = es.State.SetLockVariables(iconConfig.LockMin.Value(), iconConfig.LockMax.Value()); err != nil {
return err
}

Expand Down
24 changes: 12 additions & 12 deletions icon/chainscore_iiss.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ func (s *chainScore) Ex_setIRep(value *common.HexInt) error {
return err
}
es := s.cc.GetExtensionState().(*iiss.ExtensionStateImpl)
return icstate.SetIRep(es.State, new(big.Int).Set(&value.Int))
return es.State.SetIRep(new(big.Int).Set(&value.Int))
}

func (s *chainScore) Ex_getIRep() (int64, error) {
es := s.cc.GetExtensionState().(*iiss.ExtensionStateImpl)
return icstate.GetIRep(es.State).Int64(), nil
return es.State.GetIRep().Int64(), nil
}

func (s *chainScore) Ex_getRRep() (int64, error) {
es := s.cc.GetExtensionState().(*iiss.ExtensionStateImpl)
return icstate.GetRRep(es.State).Int64(), nil
return es.State.GetRRep().Int64(), nil
}

func (s *chainScore) Ex_setStake(value *common.HexInt) error {
Expand All @@ -53,8 +53,8 @@ func (s *chainScore) Ex_setStake(value *common.HexInt) error {

v := &value.Int

if ia.GetVotedPower().Cmp(v) == 1 {
return errors.Errorf("Failed to stake: stake < votedPower")
if ia.GetVoting().Cmp(v) == 1 {
return errors.Errorf("Failed to stake: stake < voting")
}

prevTotalStake := ia.GetTotalStake()
Expand All @@ -65,12 +65,12 @@ func (s *chainScore) Ex_setStake(value *common.HexInt) error {

account := s.cc.GetAccountState(s.from.ID())
balance := account.GetBalance()
availableStake := new(big.Int).Add(balance, ia.GetVotingPower())
availableStake := new(big.Int).Add(balance, ia.Stake())
if availableStake.Cmp(v) == -1 {
return errors.Errorf("Not enough balance")
}

tStake := icstate.GetTotalStake(es.State)
tStake := es.State.GetTotalStake()
tsupply := icutils.GetTotalSupply(s.cc)

// update IISS account
Expand Down Expand Up @@ -100,7 +100,7 @@ func (s *chainScore) Ex_setStake(value *common.HexInt) error {
diff := new(big.Int).Sub(totalStake, prevTotalStake)
account.SetBalance(new(big.Int).Sub(balance, diff))
}
if err := icstate.SetTotalStake(es.State, new(big.Int).Add(tStake, stakeInc)); err != nil {
if err := es.State.SetTotalStake(new(big.Int).Add(tStake, stakeInc)); err != nil {
return err
}

Expand All @@ -112,8 +112,8 @@ func calcUnstakeLockPeriod(state *icstate.State, totalStake *big.Int, totalSuppl
fsupply := new(big.Float).SetInt(totalSupply)
stakeRate := new(big.Float).Quo(fstake, fsupply)
rPoint := big.NewFloat(rewardPoint)
lMin := icstate.GetLockMin(state)
lMax := icstate.GetLockMax(state)
lMin := state.GetLockMin()
lMax := state.GetLockMax()
if stakeRate.Cmp(rPoint) == 1 {
return lMin
}
Expand Down Expand Up @@ -207,7 +207,7 @@ func (s *chainScore) Ex_getPReps() (map[string]interface{}, error) {
es := s.cc.GetExtensionState().(*iiss.ExtensionStateImpl)
blockHeight := s.cc.BlockHeight()
jso := es.GetPRepsInJSON(blockHeight)
ts := icstate.GetTotalStake(es.State)
ts := es.State.GetTotalStake()
jso["totalStake"] = intconv.FormatBigInt(ts)
jso["blockHeight"] = blockHeight
return jso, nil
Expand Down Expand Up @@ -381,7 +381,7 @@ func (s *chainScore) Ex_queryIScore(address module.Address) (map[string]interfac

func (s *chainScore) Ex_estimateUnstakeLockPeriod() (map[string]interface{}, error) {
es := s.cc.GetExtensionState().(*iiss.ExtensionStateImpl)
totalStake := icstate.GetTotalStake(es.State)
totalStake := es.State.GetTotalStake()
totalSupply := icutils.GetTotalSupply(s.cc)
result := make(map[string]interface{})
result["unstakeLockPeriod"] = calcUnstakeLockPeriod(es.State, totalStake, totalSupply)
Expand Down
24 changes: 12 additions & 12 deletions icon/iiss/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (s *ExtensionStateImpl) PrevCalculationBlockHeight() int64 {
}

func (s *ExtensionStateImpl) NewCalculationPeriod(blockHeight int64, calculator *Calculator) error {
if blockHeight != s.c.currentBH+icstate.GetCalculatePeriod(s.State) {
if blockHeight != s.c.currentBH+s.State.GetCalculatePeriod() {
return nil
}

Expand All @@ -211,10 +211,10 @@ func (s *ExtensionStateImpl) NewCalculationPeriod(blockHeight int64, calculator

if _, err := s.Front.AddEventPeriod(
0,
icstate.GetIRep(s.State),
icstate.GetRRep(s.State),
icstate.GetMainPRepCount(s.State),
icstate.GetPRepCount(s.State),
s.State.GetIRep(),
s.State.GetRRep(),
s.State.GetMainPRepCount(),
s.State.GetPRepCount(),
); err != nil {
return err
}
Expand Down Expand Up @@ -535,25 +535,25 @@ func (s *ExtensionStateImpl) moveOnToNextTerm(totalSupply *big.Int) error {
term := s.State.GetTerm()
nextTerm := icstate.NewNextTerm(
term,
icstate.GetTermPeriod(s.State),
icstate.GetIRep(s.State),
icstate.GetRRep(s.State),
s.State.GetTermPeriod(),
s.State.GetIRep(),
s.State.GetRRep(),
totalSupply,
s.pm.TotalDelegated(),
)

size := 0
mainPRepCount := int(icstate.GetMainPRepCount(s.State))
mainPRepCount := int(s.State.GetMainPRepCount())
activePRepCount := s.pm.Size()

if term.IsDecentralized() || activePRepCount >= mainPRepCount {
prepCount := int(icstate.GetPRepCount(s.State))
prepCount := int(s.State.GetPRepCount())
size = icutils.Min(activePRepCount, prepCount)
}

if size > 0 {
prepSnapshots := make(icstate.PRepSnapshots, size, size)
br := int(icstate.GetBondRequirement(s.State))
br := s.State.GetBondRequirement()
for i := 0; i < size; i++ {
prep := s.pm.GetPRepByIndex(i)
prepSnapshots[i] = icstate.NewPRepSnapshotFromPRepStatus(prep.PRepStatus, br)
Expand Down Expand Up @@ -592,7 +592,7 @@ func (s *ExtensionStateImpl) setValidators(wc state.WorldContext) error {
}

func (s *ExtensionStateImpl) GetValidators() []module.Validator {
mainPRepCount := s.State.GetMainPRepCount()
mainPRepCount := int(s.State.GetMainPRepCount())

term := s.State.GetTerm()
prepSnapshotCount := term.GetPRepSnapshotCount()
Expand Down
6 changes: 3 additions & 3 deletions icon/iiss/icstate/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (a *Account) SetDelegation(ds Delegations) {
func (a Account) GetDelegationInfo() map[string]interface{} {
jso := make(map[string]interface{})
jso["totalDelegated"] = a.delegating
jso["votingPower"] = new(big.Int).Sub(a.stake, a.GetVotedPower())
jso["votingPower"] = a.GetVotingPower()

if delegations := a.delegations.ToJSON(module.JSONVersion3); delegations != nil {
jso["delegations"] = delegations
Expand All @@ -237,10 +237,10 @@ func (a Account) GetDelegationInfo() map[string]interface{} {
}

func (a *Account) GetVotingPower() *big.Int {
return new(big.Int).Sub(a.stake, a.GetVotedPower())
return new(big.Int).Sub(a.stake, a.GetVoting())
}

func (a *Account) GetVotedPower() *big.Int {
func (a *Account) GetVoting() *big.Int {
return new(big.Int).Add(a.bonding, a.delegating)
}

Expand Down
Loading

0 comments on commit e7e4f01

Please sign in to comment.