diff --git a/backend/conf/config.go b/backend/conf/config.go index b6546d45e..63aa5f77e 100644 --- a/backend/conf/config.go +++ b/backend/conf/config.go @@ -56,7 +56,6 @@ const ( KeyCaculateStartDate = "CaculateStartDate" //yyyy-mm-ddThh:mm:ss KeyCaculateEndDate = "CaculateEndDate" //yyyy-mm-ddThh:mm:ss KeyCaculateDate = "CaculateDate" //yyyy-mm-dd - KeyCaculatePeriodDay = "CaculatePeriodDay" //hh KeyFoundationDelegatorAddr = "FoundationDelegatorAddr" EnvironmentDevelop = "dev" @@ -118,7 +117,6 @@ func init() { CaculateStartDate: getEnv(KeyCaculateStartDate, DefaultEnvironment), CaculateEndDate: getEnv(KeyCaculateEndDate, DefaultEnvironment), CaculateDate: getEnv(KeyCaculateDate, DefaultEnvironment), - CaculatePeriodDay: getEnvInt(KeyCaculatePeriodDay, DefaultEnvironment), NetreqLimitMax: getEnvInt(KeyNetreqLimitMax, DefaultEnvironment), } if "true" == strings.ToLower(getEnv(KeyCaculateDebug, DefaultEnvironment)) { @@ -190,7 +188,6 @@ func loadDefault() { KeyCaculateStartDate: "", KeyCaculateEndDate: "", KeyCaculateDate: "2020-06-01", - KeyCaculatePeriodDay: "31", KeyNetreqLimitMax: "20", KeyFoundationDelegatorAddr: "iaa1w7ewedr57z6p7f8nknmdvukfxwkwlsvfjumdts", } @@ -242,7 +239,6 @@ type serverConf struct { CaculateStartDate string CaculateEndDate string CaculateDate string - CaculatePeriodDay int NetreqLimitMax int FoundationDelegatorAddr string } diff --git a/backend/orm/document/static_delegator_month.go b/backend/orm/document/static_delegator_month.go index ee315306e..cf92a84d1 100644 --- a/backend/orm/document/static_delegator_month.go +++ b/backend/orm/document/static_delegator_month.go @@ -50,26 +50,6 @@ func (d ExStaticDelegatorMonth) EnsureIndexes() []mgo.Index { return indexes } -func (d ExStaticDelegatorMonth) GetLatest(address string) (ExStaticDelegatorMonth, error) { - var res ExStaticDelegatorMonth - cond := bson.M{} - if address != "" { - cond[ExStaticDelegatorMonthAddressTag] = address - } - var query = orm.NewQuery() - defer query.Release() - query.SetCollection(d.Name()). - SetCondition(cond). - SetSort("-date"). - SetSize(1). - SetResult(&res) - - err := query.Exec() - if err != nil && err != mgo.ErrNotFound { - return res, err - } - return res, nil -} func (d ExStaticDelegatorMonth) Batch(txs []txn.Op) error { return orm.Batch(txs) diff --git a/backend/orm/document/static_delegator_month_test.go b/backend/orm/document/static_delegator_month_test.go index 60c79ad76..b0c8ab671 100644 --- a/backend/orm/document/static_delegator_month_test.go +++ b/backend/orm/document/static_delegator_month_test.go @@ -2,15 +2,14 @@ package document import ( "testing" - "encoding/json" ) func TestExStaticDelegatorMonth_GetLatest(t *testing.T) { - res, err := new(ExStaticDelegatorMonth).GetLatest("faa12t4gfg502wra9lhtjjvqudq82rrzu2sk5j2l09") - if err != nil { - t.Fatal(err.Error()) - } - - bytedata, _ := json.Marshal(res) - t.Log(string(bytedata)) + //res, err := new(ExStaticDelegatorMonth).GetLatest("faa12t4gfg502wra9lhtjjvqudq82rrzu2sk5j2l09") + //if err != nil { + // t.Fatal(err.Error()) + //} + // + //bytedata, _ := json.Marshal(res) + //t.Log(string(bytedata)) } diff --git a/backend/orm/document/static_validator_test.go b/backend/orm/document/static_validator_test.go index bd29169a4..eb42be183 100644 --- a/backend/orm/document/static_validator_test.go +++ b/backend/orm/document/static_validator_test.go @@ -6,6 +6,7 @@ import ( "fmt" "time" "github.com/irisnet/explorer/backend/types" + "encoding/json" ) func TestExStaticValidator_GetCommissionRate(t *testing.T) { @@ -36,3 +37,15 @@ func TestExStaticValidator_GetCommissionRate(t *testing.T) { } t.Log(data.Commission.Rate) } + +func TestExStaticValidator_GetDataOneDay(t *testing.T) { + address := "iva16tvg03gnrud9hzec7nsf9k8da74fqa9sk8xfqv" + datestr := fmt.Sprintf("%d-%02d-01T00:00:00", 2020, 6) + date, _ := time.ParseInLocation(types.TimeLayout, datestr, time.FixedZone("CST", 8*3600)) + data, err := new(ExStaticValidator).GetDataOneDay(date, address) + if err != nil { + t.Fatal(err) + } + bytestr, _ := json.Marshal(data) + t.Log(string(bytestr)) +} diff --git a/backend/rest/controller/home.go b/backend/rest/controller/home.go index 2bd45b2fc..e74a45dba 100644 --- a/backend/rest/controller/home.go +++ b/backend/rest/controller/home.go @@ -6,10 +6,8 @@ import ( "time" "github.com/gorilla/mux" - "github.com/irisnet/explorer/backend/conf" "github.com/irisnet/explorer/backend/lcd" "github.com/irisnet/explorer/backend/logger" - "github.com/irisnet/explorer/backend/service" "github.com/irisnet/explorer/backend/types" "github.com/irisnet/explorer/backend/utils" "github.com/irisnet/explorer/backend/vo" @@ -119,26 +117,26 @@ func registerNavigationBar(r *mux.Router) error { funGroup = append(funGroup, getTokenStatCirculation) group.Add(1) - var getTokenStatFoundationBonded = func() { - defer func() { - group.Done() - if r := recover(); r != nil { - logger.Error("getTokenStatFoundationBonded error", logger.Any("err", r)) - } - }() - - accService := service.AccountService{} - if conf.Get().Hub.Prefix.AccAddr == types.MainnetAccPrefix { - res := accService.QueryDelegations(types.FoundationDelegatorAddr) - foundationBondAmt := float64(0) - for _, v := range res { - foundationBondAmt += v.Amount.Amount - } - result.FoundationBonded = utils.ParseStringFromFloat64(foundationBondAmt) - } - } - funGroup = append(funGroup, getTokenStatFoundationBonded) - group.Add(1) + //var getTokenStatFoundationBonded = func() { + //defer func() { + // group.Done() + // if r := recover(); r != nil { + // logger.Error("getTokenStatFoundationBonded error", logger.Any("err", r)) + // } + //}() + + //accService := service.AccountService{} + //if conf.Get().Hub.Prefix.AccAddr == types.MainnetAccPrefix { + //res := accService.QueryDelegations(types.FoundationDelegatorAddr) + //foundationBondAmt := float64(0) + //for _, v := range res { + // foundationBondAmt += v.Amount.Amount + //} + //result.FoundationBonded = utils.ParseStringFromFloat64(foundationBondAmt) + //} + //} + //funGroup = append(funGroup, getTokenStatFoundationBonded) + //group.Add(1) var calculateAvgBlockTime = func() { defer func() { diff --git a/backend/service/caculate.go b/backend/service/caculate.go index 7c432ef06..27444b583 100644 --- a/backend/service/caculate.go +++ b/backend/service/caculate.go @@ -7,6 +7,7 @@ import ( "github.com/irisnet/explorer/backend/types" "math" "strconv" + "math/big" ) type CaculateService struct { @@ -46,6 +47,16 @@ func LoadFromDelModel(model document.ExStaticDelegatorMonth) vo.ExStaticDelegato PeriodDelegationTimes: model.PeriodDelegationTimes, } ret.Rewards = ret.TerminalRewards + ret.PeriodWithdrawRewards - ret.PeriodIncrementRewards + if true { + rewards := new(big.Rat).Mul(new(big.Rat).SetFloat64(ret.PeriodIncrementRewards), new(big.Rat).SetInt64(365)) + delegations := new(big.Rat).Mul(new(big.Rat).SetFloat64(ret.TerminalDelegation), new(big.Rat).SetInt64(30)) + if delegations.Cmp(new(big.Rat).SetInt64(0)) == 1 { + data := new(big.Rat).Quo(rewards, delegations) + ret.AnnualizedRate = new(big.Rat).Mul(data, new(big.Rat).SetInt64(100)).FloatString(2) + } else { + ret.AnnualizedRate = "0" + } + } return ret } diff --git a/backend/task/static_delegator_by_month.go b/backend/task/static_delegator_by_month.go index c9e91aa04..8d5686bbe 100644 --- a/backend/task/static_delegator_by_month.go +++ b/backend/task/static_delegator_by_month.go @@ -1,18 +1,18 @@ package task import ( - "github.com/irisnet/explorer/backend/orm/document" + "fmt" + "github.com/irisnet/explorer/backend/conf" + "github.com/irisnet/explorer/backend/lcd" "github.com/irisnet/explorer/backend/logger" - "time" + "github.com/irisnet/explorer/backend/orm/document" "github.com/irisnet/explorer/backend/types" - "fmt" "github.com/irisnet/explorer/backend/utils" "math" + "strconv" "strings" - "github.com/irisnet/explorer/backend/lcd" - "github.com/irisnet/explorer/backend/conf" "sync" - "strconv" + "time" ) //caculatetime format [ 2020-04-03T00:00:00 ] @@ -23,7 +23,7 @@ type StaticDelegatorByMonthTask struct { AddressCoin map[string]document.Coin AddrPeriodCommission map[string]document.Coin AddrTerminalCommission map[string]document.Coin - //AddrBeginCommission map[string]document.Coin + AddrBeginCommission map[string]document.Coin startTime time.Time endTime time.Time @@ -55,7 +55,7 @@ func (task *StaticDelegatorByMonthTask) SetCaculateAddress(address string) { func (task *StaticDelegatorByMonthTask) DoTask() error { - res, err := task.caculateWork() + res, err := task.calculateWork() if err != nil { return err } @@ -74,15 +74,17 @@ func (task *StaticDelegatorByMonthTask) DoTask() error { return nil } -func (task *StaticDelegatorByMonthTask) caculateWork() ([]document.ExStaticDelegatorMonth, error) { +func (task *StaticDelegatorByMonthTask) calculateWork() ([]document.ExStaticDelegatorMonth, error) { datetime := time.Now().In(cstZone) year, month := getStartYearMonth(datetime) - starttime, _ := time.ParseInLocation(types.TimeLayout, fmt.Sprintf("%d-%02d-01T00:00:00", year, month), cstZone) + startTimeGetRewards, _ := time.ParseInLocation(types.TimeLayout, fmt.Sprintf("%d-%02d-01T00:00:00", year, month), cstZone) if task.isSetTime { datetime = task.endTime - starttime = task.startTime + startTimeGetRewards = task.startTime } + beginTimeGetTx := startTimeGetRewards + startTimeGetRewards = startTimeGetRewards.Add(time.Duration(-24) * time.Hour) if conf.Get().Server.CaculateDebug { arr := strings.Split(conf.Get().Server.CronTimeFormatStaticMonth, " ") minutedata := strings.Split(arr[0], "/") @@ -100,33 +102,32 @@ func (task *StaticDelegatorByMonthTask) caculateWork() ([]document.ExStaticDeleg //no work return nil, fmt.Errorf("time hour is smaller than 1") } else { - hour -- + hour-- minute = minute - int(interval) + 60 } } else { minute = minute - int(interval) } - starttime, _ = time.ParseInLocation(types.TimeLayout, fmt.Sprintf("%d-%02d-%02dT%02d:%02d:00", datetime.Year(), datetime.Month(), datetime.Day(), hour, minute), cstZone) + startTimeGetRewards, _ = time.ParseInLocation(types.TimeLayout, fmt.Sprintf("%d-%02d-%02dT%02d:%02d:00", datetime.Year(), datetime.Month(), datetime.Day(), hour, minute), cstZone) } //find last date - endtime, createat, err := document.Getdate(task.staticModel.Name(), starttime, datetime, "-"+document.ExStaticDelegatorDateTag) + endTimeGetRewards, createAt, err := document.Getdate(task.staticModel.Name(), startTimeGetRewards, datetime, "-"+document.ExStaticDelegatorDateTag) if err != nil { return nil, err } - createtime := time.Unix(createat, 0) + endTimeGetTx := time.Unix(createAt, 0) var terminalData = make(map[string]document.ExStaticDelegator) var delegators = make(map[string]string) if task.address != "" { - data, err := task.staticModel.GetDataOneDay(endtime, task.address) + data, err := task.staticModel.GetDataOneDay(endTimeGetRewards, task.address) if err != nil { return nil, err } - //terminalData = append(terminalData, data) terminalData[data.Address] = data delegators[data.Address] = data.Address } else { - terminalData, err = task.staticModel.GetDataByDate(endtime) + terminalData, err = task.staticModel.GetDataByDate(endTimeGetRewards) if err != nil { logger.Error("Get GetData ByDate fail", logger.String("date", datetime.String()), @@ -137,17 +138,16 @@ func (task *StaticDelegatorByMonthTask) caculateWork() ([]document.ExStaticDeleg res := make([]document.ExStaticDelegatorMonth, 0, len(terminalData)) - //fmt.Println(starttime, createtime) - txs, err := task.getPeriodTxByAddress(starttime, createtime, task.address) //default is all address txs + //fmt.Println(startTimeGetRewards, endTimeGetTx) + txs, err := task.getPeriodTxByAddress(beginTimeGetTx, endTimeGetTx, task.address) //default is all address txs if err != nil { return nil, err } - delegators, err = task.getDelegatorsInPeriod(starttime, createtime) + delegators, err = task.getDelegatorsInPeriod(startTimeGetRewards, endTimeGetRewards) if err != nil { return nil, err } - logger.Debug(fmt.Sprintf("total delegator:%v", len(delegators))) for addr := range delegators { val, ok := terminalData[addr] @@ -159,7 +159,7 @@ func (task *StaticDelegatorByMonthTask) caculateWork() ([]document.ExStaticDeleg Delegation: utils.Coin{Denom: types.IRISAttoUint, Amount: 0}, } } - one, err := task.getStaticDelegator(starttime, val, txs) + one, err := task.getStaticDelegator(startTimeGetRewards, val, txs) if err != nil { logger.Error(err.Error()) continue @@ -169,8 +169,8 @@ func (task *StaticDelegatorByMonthTask) caculateWork() ([]document.ExStaticDeleg one.CaculateDate = strings.ReplaceAll(conf.Get().Server.CaculateDate, "-", ".") } if conf.Get().Server.CaculateDebug { - one.Date = fmt.Sprintf("%d.%02d.%02d %02d:%02d:%02d", starttime.Year(), - starttime.Month(), starttime.Day(), starttime.Hour(), starttime.Minute(), starttime.Second()) + one.Date = fmt.Sprintf("%d.%02d.%02d %02d:%02d:%02d", startTimeGetRewards.Year(), + startTimeGetRewards.Month(), startTimeGetRewards.Day(), startTimeGetRewards.Hour(), startTimeGetRewards.Minute(), startTimeGetRewards.Second()) one.CaculateDate = fmt.Sprintf("%d.%02d.%02d %02d:%02d:%02d", datetime.Year(), datetime.Month(), datetime.Day(), datetime.Hour(), datetime.Minute(), datetime.Second()) } @@ -180,7 +180,7 @@ func (task *StaticDelegatorByMonthTask) caculateWork() ([]document.ExStaticDeleg return res, nil } -func parseCoinAmountAndUnitFromStr(s string) (document.Coin) { +func parseCoinAmountAndUnitFromStr(s string) document.Coin { for _, denom := range rewardsDenom { if strings.HasSuffix(s, denom) { coin := utils.ParseCoin(s) @@ -193,62 +193,44 @@ func parseCoinAmountAndUnitFromStr(s string) (document.Coin) { return document.Coin{} } -func (task *StaticDelegatorByMonthTask) getStaticDelegator(starttime time.Time, terminalval document.ExStaticDelegator, txs []document.CommonTx) (document.ExStaticDelegatorMonth, error) { +func (task *StaticDelegatorByMonthTask) getStaticDelegator(startTime time.Time, terminalval document.ExStaticDelegator, txs []document.CommonTx) (document.ExStaticDelegatorMonth, error) { periodRewards := task.getPeriodRewards(terminalval.Address) - //delagation, err := task.staticModel.GetDataOneDay(starttime, terminalval.Address) - //if err != nil { - // logger.Error("get DelegationData failed", - // logger.String("func", "get StaticDelegator"), - // logger.String("err", err.Error())) - // //return document.ExStaticDelegatorMonth{}, err - //} - //if delagation.Address == "" { - // delagation.Address = terminalval.Address - // delagation.Date = starttime - //} - - //if task.AddrBeginCommission == nil { - // task.AddrBeginCommission = make(map[string]document.Coin) - //} - if task.AddrTerminalCommission == nil { - task.AddrTerminalCommission = make(map[string]document.Coin) + startdelagation, err := task.staticModel.GetDataOneDay(startTime, terminalval.Address) + if err != nil { + logger.Error("get GetDataOneDay failed", + logger.String("func", "get StaticDelegator"), + logger.String("startTime", startTime.Format(types.TimeLayout)), + logger.String("err", err.Error())) } - //if len(delagation.Commission) > 0 { - // task.AddrBeginCommission[delagation.Address] = document.Coin{ - // Amount: delagation.Commission[0].Iris * math.Pow10(18), - // Denom: types.IRISAttoUint, - // } - //} - if len(terminalval.Commission) > 0 { - task.AddrTerminalCommission[terminalval.Address] = document.Coin{ - Amount: terminalval.Commission[0].Iris * math.Pow10(18), - Denom: types.IRISAttoUint, + if startdelagation.Address == "" { + startdelagation.Address = terminalval.Address + startdelagation.Date = startTime + startdelagation.Delegation = utils.Coin{ + Denom: types.IRISAttoUint, } } - delagationlastmonth, err := task.mStaticModel.GetLatest(terminalval.Address) - if err != nil { - logger.Error("get GetLatest failed", - logger.String("func", "get StaticDelegatorMonth"), - logger.String("err", err.Error())) - //return document.ExStaticDelegatorMonth{}, err + if task.AddrBeginCommission == nil { + task.AddrBeginCommission = make(map[string]document.Coin) } - // check delagationlastmonth caculate date if last caculate period - datetime := time.Now().In(cstZone) - currentCaculateDate := fmt.Sprintf("%d.%02d.%02d", datetime.Year(), datetime.Month(), datetime.Day()) - caculateperiod := 0 - if task.isSetTime { - currentCaculateDate = conf.Get().Server.CaculateDate - caculateperiod = conf.Get().Server.CaculatePeriodDay + if task.AddrTerminalCommission == nil { + task.AddrTerminalCommission = make(map[string]document.Coin) } - if !checkIsPeriod(delagationlastmonth.CaculateDate, currentCaculateDate, caculateperiod) { - delagationlastmonth = document.ExStaticDelegatorMonth{} + if len(startdelagation.Commission) > 0 { + task.AddrBeginCommission[startdelagation.Address] = document.Coin{ + Amount: startdelagation.Commission[0].Iris * math.Pow10(18), + Denom: types.IRISAttoUint, + } } - - if delagationlastmonth.Address == "" { - delagationlastmonth.Address = terminalval.Address - delagationlastmonth.TerminalDelegation = document.Coin{ - Denom: types.IRISAttoUint, + if len(startdelagation.DelegationsRewards) == 0 { + startdelagation.DelegationsRewards = []document.Rewards{ + {Iris: 0, IrisAtto: "0"}, + } + } + if len(terminalval.Commission) > 0 { + task.AddrTerminalCommission[terminalval.Address] = document.Coin{ + Amount: terminalval.Commission[0].Iris * math.Pow10(18), + Denom: types.IRISAttoUint, } } @@ -261,15 +243,15 @@ func (task *StaticDelegatorByMonthTask) getStaticDelegator(starttime time.Time, } } terminalRewards := document.Rewards{Iris: delegationrewards, IrisAtto: fmt.Sprint(delegationrewards * math.Pow10(18))} - incrementRewards := task.getIncrementRewards(terminalRewards, delagationlastmonth, periodRewards) + incrementRewards := task.getIncrementRewards(terminalRewards, startdelagation.DelegationsRewards[0], periodRewards) item := document.ExStaticDelegatorMonth{ Address: terminalval.Address, - Date: fmt.Sprintf("%d.%02d.%02d", starttime.Year(), starttime.Month(), starttime.Day()), + Date: fmt.Sprintf("%d.%02d.%02d", startTime.Year(), startTime.Month(), startTime.Day()), TerminalDelegation: document.Coin{Denom: terminalval.Delegation.Denom, Amount: terminalval.Delegation.Amount}, PeriodDelegationTimes: task.getPeriodDelegationTimes(terminalval.Address, txs), PeriodWithdrawRewards: periodRewards, - IncrementDelegation: task.getIncrementDelegation(terminalval.Delegation, delagationlastmonth.TerminalDelegation), + IncrementDelegation: task.getIncrementDelegation(terminalval.Delegation, startdelagation.Delegation), PeriodIncrementRewards: incrementRewards, TerminalRewards: terminalRewards, } @@ -287,7 +269,7 @@ func (task *StaticDelegatorByMonthTask) getPeriodTxByAddress(starttime, endtime for _, tx := range txs { switch tx.Type { case types.TxTypeWithdrawDelegatorReward, types.TxTypeWithdrawDelegatorRewardsAll, types.TxTypeWithdrawValidatorRewardsAll, - types.TxTypeBeginRedelegate, types.TxTypeStakeBeginUnbonding, types.TxTypeStakeDelegate: + types.TxTypeBeginRedelegate, types.TxTypeStakeBeginUnbonding, types.TxTypeStakeDelegate, types.TxTypeStakeEditValidator: chanLimit <- true group.Add(1) go func(txHash string, limit chan bool) { @@ -337,14 +319,14 @@ func (task *StaticDelegatorByMonthTask) getPeriodDelegationTimes(address string, return } -func (task *StaticDelegatorByMonthTask) getIncrementRewards(delegaterewards document.Rewards, - delagationlastmonth document.ExStaticDelegatorMonth, - periodRewards document.Rewards) (document.Rewards) { +func (task *StaticDelegatorByMonthTask) getIncrementRewards(terminalrewards document.Rewards, + beginrewards document.Rewards, + periodRewards document.Rewards) document.Rewards { var rewards document.Rewards - rewards.Iris = delegaterewards.Iris + rewards.Iris = terminalrewards.Iris //Rdx = Rdn - Rdn-1 + Rdw - rewards.Iris -= delagationlastmonth.TerminalRewards.Iris + rewards.Iris -= beginrewards.Iris rewards.Iris += periodRewards.Iris rewards.IrisAtto = fmt.Sprint(rewards.Iris * math.Pow10(18)) @@ -410,8 +392,8 @@ func (task *StaticDelegatorByMonthTask) getCoinflow(blockcoinFlow []lcd.BlockCoi return } -func (task *StaticDelegatorByMonthTask) getIncrementDelegation(terminal utils.Coin, delagationlastmonth document.Coin) document.Coin { - amount := terminal.Amount - delagationlastmonth.Amount +func (task *StaticDelegatorByMonthTask) getIncrementDelegation(terminal utils.Coin, start utils.Coin) document.Coin { + amount := terminal.Amount - start.Amount return document.Coin{ Denom: terminal.Denom, Amount: amount, @@ -419,33 +401,6 @@ func (task *StaticDelegatorByMonthTask) getIncrementDelegation(terminal utils.Co } -//check caculate period is ok -func checkIsPeriod(lastcaculateDate, caculateTime string, period int) bool { - if lastcaculateDate == "" { - return false - } - caculateTime = strings.ReplaceAll(caculateTime, ".", "-") - lastcaculateDate = strings.ReplaceAll(lastcaculateDate, ".", "-") - timedate, err := time.ParseInLocation(types.TimeLayout, caculateTime+"T00:00:00", cstZone) - if err != nil { - logger.Error("ParseInLocation caculateTime error in checkIsPeriod", logger.String("error", err.Error())) - return false - } - timelastcur, err := time.ParseInLocation(types.TimeLayout, lastcaculateDate+"T00:00:00", cstZone) - if err != nil { - logger.Error("ParseInLocation lastcaculateDate error in checkIsPeriod", logger.String("error", err.Error())) - return false - } - durtime := timedate.Sub(timelastcur).Milliseconds() / (3600 * 1000) - //fmt.Println(timelastcur) - //fmt.Println(durtime / 24) - if period > 0 { - return durtime/24 == int64(period) - } - - return durtime/24 >= 28 && durtime/24 <= 31 -} - func (task *StaticDelegatorByMonthTask) getDelegatorsInPeriod(timelastcur, timedate time.Time) (map[string]string, error) { delegetors, err := task.staticModel.GetAddressByPeriod(timelastcur, timedate) if err != nil { @@ -453,34 +408,3 @@ func (task *StaticDelegatorByMonthTask) getDelegatorsInPeriod(timelastcur, timed } return delegetors, nil } - -//func (task *StaticDelegatorByMonthTask) getDelegationData(caculatetime string) ([]document.ExStaticDelegator, error) { -// date, err := time.ParseInLocation(types.TimeLayout, caculatetime, cstZone) -// if err != nil { -// return nil, err -// } -// data, err := task.staticModel.GetDataByDate(date) -// if err != nil { -// logger.Error("Get GetData ByDate fail", -// logger.String("date", date.String()), -// logger.String("err", err.Error())) -// return nil, err -// } -// return data, nil -//} - -//func (task *StaticDelegatorByMonthTask) saveOps(datas []document.ExStaticDelegatorMonth) ([]txn.Op) { -// var ops = make([]txn.Op, 0, len(datas)) -// for _, val := range datas { -// val.Id = bson.NewObjectId() -// val.CreateAt = time.Now().Unix() -// val.UpdateAt = time.Now().Unix() -// op := txn.Op{ -// C: task.mStaticModel.Name(), -// Id: bson.NewObjectId(), -// Insert: val, -// } -// ops = append(ops, op) -// } -// return ops -//} diff --git a/backend/task/static_delegator_by_month_test.go b/backend/task/static_delegator_by_month_test.go index 6f13d3a57..d870bca7a 100644 --- a/backend/task/static_delegator_by_month_test.go +++ b/backend/task/static_delegator_by_month_test.go @@ -37,15 +37,15 @@ func TestStaticDelegatorByMonthTask_DoTask(t *testing.T) { //} func TestStaticDelegatorByMonthTask_getPeriodTxByAddress(t *testing.T) { task := new(StaticDelegatorByMonthTask) - starttime, err := time.ParseInLocation(types.TimeLayout, fmt.Sprintf("%d-%02d-%02dT00:00:00", 2020, 5, 1), cstZone) + starttime, err := time.ParseInLocation(types.TimeLayout, fmt.Sprintf("%d-%02d-%02dT00:00:00", 2020, 6, 1), cstZone) if err != nil { t.Fatal(err.Error()) } - endtime, err := time.ParseInLocation(types.TimeLayout, fmt.Sprintf("%d-%02d-%02dT00:00:00", 2020, 5, 31), cstZone) + endtime, err := time.ParseInLocation(types.TimeLayout, fmt.Sprintf("%d-%02d-%02dT23:59:00", 2020, 6, 30), cstZone) if err != nil { t.Fatal(err.Error()) } - txs, err := task.getPeriodTxByAddress(starttime, endtime, "iaa1gm8hq56wkkau66uwydud42kqn4jkznja2mcee0") + txs, err := task.getPeriodTxByAddress(starttime, endtime, "iaa1tn9y9lqxkmdt2d54yc9czsk9uapy89m3m5vatq") if err != nil { t.Fatal(err.Error()) } @@ -254,19 +254,19 @@ func TestStaticDelegatorByMonthTask_getStaticDelegator(t *testing.T) { func TestStaticDelegatorByMonthTask_getIncrementDelegation(t *testing.T) { s := new(StaticDelegatorByMonthTask) value := utils.Coin{Amount: float64(112.3), Denom: "iris-atto"} - value1 := document.Coin{Amount: float64(100), Denom: "iris-atto"} + value1 := utils.Coin{Amount: float64(100), Denom: "iris-atto"} data := s.getIncrementDelegation(value, value1) t.Log(data.Amount) } func TestDoTask(t *testing.T) { - starttime, _ := time.ParseInLocation(types.TimeLayout, fmt.Sprintf("%d-%02d-%02dT00:00:00", 2020, 5, 1), cstZone) - endtime, _ := time.ParseInLocation(types.TimeLayout, fmt.Sprintf("%d-%02d-%02dT00:00:00", 2020, 5, 31), cstZone) + starttime, _ := time.ParseInLocation(types.TimeLayout, fmt.Sprintf("%d-%02d-%02dT00:00:00", 2020, 6, 1), cstZone) + endtime, _ := time.ParseInLocation(types.TimeLayout, fmt.Sprintf("%d-%02d-%02dT00:00:00", 2020, 7, 1), cstZone) delegatortask := new(StaticDelegatorByMonthTask) //delegatortask.SetCaculateAddress("iaa1kgddca7qj96z0qcxr2c45z73cfl0c75pwl95ym") delegatortask.SetCaculateScope(starttime, endtime) - datas, err := delegatortask.caculateWork() + datas, err := delegatortask.calculateWork() if err != nil { t.Fail() } @@ -449,39 +449,37 @@ func TestCalculateDelegatorRewards(t *testing.T) { } func TestStaticDelegatorByMonthTask_checkNotLastPeriod(t *testing.T) { - lastcaculateDate := "2020.05.01" - - caculateTime := "2020.06.01" - if checkIsPeriod(lastcaculateDate, caculateTime, 31) { - t.Log("PASS OK") - } else { - t.Fail() - } - - caculateTime = "2020.06.01" - lastcaculateDate = "2020.05.10" - if checkIsPeriod(lastcaculateDate, caculateTime, 22) { - t.Log("PASS OK") - } else { - t.Fail() - } - caculateTime = "2020.06.01" - lastcaculateDate = "2020.04.10" - if checkIsPeriod(lastcaculateDate, caculateTime, 50) { - t.Log("PASS OK") - } else { - t.Fail() - } + //lastcaculateDate := "2020.05.01" + // + //caculateTime := "2020.06.01" + //if checkIsPeriod(lastcaculateDate, caculateTime, 31) { + // t.Log("PASS OK") + //} else { + // t.Fail() + //} + // + //caculateTime = "2020.06.01" + //lastcaculateDate = "2020.05.10" + //if checkIsPeriod(lastcaculateDate, caculateTime, 22) { + // t.Log("PASS OK") + //} else { + // t.Fail() + //} + //caculateTime = "2020.06.01" + //lastcaculateDate = "2020.04.10" + //if checkIsPeriod(lastcaculateDate, caculateTime, 50) { + // t.Log("PASS OK") + //} else { + // t.Fail() + //} } func TestStaticDelegatorByMonthTask_getIncrementRewards(t *testing.T) { delegatorRewards := document.Rewards{ Iris: 1315.5616364376328, } - delegationlastmaonth := document.ExStaticDelegatorMonth{ - TerminalRewards: document.Rewards{ - Iris: 1709.84676707812, - }, + delegationlastmaonth := document.Rewards{ + Iris: 1709.84676707812, } periodRewards := document.Rewards{ Iris: 2295.505507779266, diff --git a/backend/task/static_validator_by_month.go b/backend/task/static_validator_by_month.go index 1d62c7cf9..9e5090854 100644 --- a/backend/task/static_validator_by_month.go +++ b/backend/task/static_validator_by_month.go @@ -23,7 +23,7 @@ type StaticValidatorByMonthTask struct { AddressCoin map[string]document.Coin AddressPeriodCommission map[string]document.Coin AddressTerminalCommission map[string]document.Coin - //AddressBeginCommission map[string]document.Coin + AddressBeginCommission map[string]document.Coin startTime time.Time endTime time.Time @@ -58,10 +58,10 @@ func (task *StaticValidatorByMonthTask) SetCaculateAddress(address string) { } } -func (task *StaticValidatorByMonthTask) SetAddressCoinMapData(rewards, pcommission, tcommission map[string]document.Coin) { +func (task *StaticValidatorByMonthTask) SetAddressCoinMapData(rewards, pcommission, bcommission, tcommission map[string]document.Coin) { task.AddressCoin = rewards task.AddressPeriodCommission = pcommission - //task.AddressBeginCommission = bcommission + task.AddressBeginCommission = bcommission task.AddressTerminalCommission = tcommission } @@ -94,6 +94,7 @@ func (task *StaticValidatorByMonthTask) caculateWork() ([]document.ExStaticValid starttime = task.startTime datetime = task.endTime } + starttime = starttime.Add(time.Duration(-24) * time.Hour) if conf.Get().Server.CaculateDebug { arr := strings.Split(conf.Get().Server.CronTimeFormatStaticMonth, " ") minutedata := strings.Split(arr[0], "/") @@ -133,7 +134,6 @@ func (task *StaticValidatorByMonthTask) caculateWork() ([]document.ExStaticValid if err != nil { return nil, err } - //terminalData = append(terminalData, data) terminalData[data.OperatorAddress] = data validators[data.OperatorAddress] = data.OperatorAddress } else { @@ -185,29 +185,13 @@ func (task *StaticValidatorByMonthTask) caculateWork() ([]document.ExStaticValid one.CaculateDate = strings.ReplaceAll(conf.Get().Server.CaculateDate, "-", ".") } res = append(res, one) - //if err := task.mStaticModel.Save(one); err != nil { - // logger.Error("save static validator month data error", - // logger.String("address", one.Address), - // logger.String("date", one.Date), - // logger.String("err", err.Error())) - //} + } return res, nil } func (task *StaticValidatorByMonthTask) getFoundtionDelegation() map[string]string { - //group := sync.WaitGroup{} - //var result []lcd.DelegationFromVal - //for _, val := range datas { - // group.Add(1) - // go func(operatorAddress string) { - // - // result = append(result, delegation) - // //fmt.Println(operatorAddress) - // group.Done() - // }(val.OperatorAddress) - //} - //group.Wait() + delegations := lcd.GetDelegationsByDelAddr(conf.Get().Server.FoundationDelegatorAddr) tmpMapData := make(map[string]string, len(delegations)) for _, val := range delegations { @@ -223,32 +207,23 @@ func (task *StaticValidatorByMonthTask) getFoundtionDelegation() map[string]stri return tmpMapData } -func (task *StaticValidatorByMonthTask) getStaticValidator(startdate time.Time, terminalval document.ExStaticValidator, +func (task *StaticValidatorByMonthTask) getStaticValidator(starttime time.Time, terminalval document.ExStaticValidator, addrHeightMap map[string]int64, addrDelegationMap map[string]string) (document.ExStaticValidatorMonth, error) { address := utils.Convert(conf.Get().Hub.Prefix.AccAddr, terminalval.OperatorAddress) + startdayData, err := task.staticModel.GetDataOneDay(starttime, terminalval.OperatorAddress) + if err != nil { + logger.Error("get start day failed", logger.String("func", "get IncrementCommission"), + logger.String("startday", starttime.Format(types.TimeLayout)), + logger.String("err", err.Error())) + } + latestone, err := task.mStaticModel.GetLatest(terminalval.OperatorAddress) if err != nil { logger.Error("get latest one failed", logger.String("func", "get IncrementCommission"), logger.String("err", err.Error())) } - // check latestone caculate date if last caculate period - datetime := time.Now().In(cstZone) - currentCaculateDate := fmt.Sprintf("%d.%02d.%02d", datetime.Year(), datetime.Month(), datetime.Day()) - caculateperiod := 0 - if task.isSetTime { - currentCaculateDate = conf.Get().Server.CaculateDate - caculateperiod = conf.Get().Server.CaculatePeriodDay - } - if !checkIsPeriod(latestone.CaculateDate, currentCaculateDate, caculateperiod) { - latestone = document.ExStaticValidatorMonth{} - } - if latestone.OperatorAddress == "" { - latestone.OperatorAddress = terminalval.OperatorAddress - latestone.TerminalCommission = document.Coin{ - Denom: types.IRISUint, - } - } + delegation, ok := addrDelegationMap[terminalval.OperatorAddress] if !ok { delegation = latestone.FoundationDelegateT @@ -263,13 +238,13 @@ func (task *StaticValidatorByMonthTask) getStaticValidator(startdate time.Time, Tokens: terminalval.Tokens, OperatorAddress: terminalval.OperatorAddress, Status: terminalval.Status, - Date: fmt.Sprintf("%d.%02d.%02d", startdate.Year(), startdate.Month(), startdate.Day()), + Date: fmt.Sprintf("%d.%02d.%02d", starttime.Year(), starttime.Month(), starttime.Day()), TerminalDelegation: terminalvalDelegations, TerminalDelegatorN: terminalval.DelegatorNum, TerminalSelfBond: selfbond, - IncrementDelegation: task.getIncrementDelegation(terminalvalDelegations, latestone.TerminalDelegation), - IncrementDelegatorN: terminalval.DelegatorNum - latestone.TerminalDelegatorN, - IncrementSelfBond: task.getIncrementSelfBond(selfbond, latestone.TerminalSelfBond), + IncrementDelegation: task.getIncrementDelegation(terminalvalDelegations, startdayData.Delegations), + IncrementDelegatorN: terminalval.DelegatorNum - startdayData.DelegatorNum, + IncrementSelfBond: task.getIncrementSelfBond(selfbond, startdayData.SelfBond), FoundationDelegateT: foundDelegation, FoundationDelegateIncre: task.getFoundationDelegateIncre(foundDelegation, latestone.FoundationDelegateT), } @@ -288,7 +263,9 @@ func (task *StaticValidatorByMonthTask) getStaticValidator(startdate time.Time, item.PeriodCommission = pcommission } } else { - pcommission.Denom = types.IRISAttoUint + pcommission = document.Coin{ + Denom: types.IRISAttoUint, + } item.PeriodCommission.Denom = types.IRISUint } @@ -303,10 +280,19 @@ func (task *StaticValidatorByMonthTask) getStaticValidator(startdate time.Time, item.TerminalCommission = tcommission } } else { - tcommission.Denom = types.IRISAttoUint + tcommission = document.Coin{ + Denom: types.IRISAttoUint, + } item.TerminalCommission.Denom = types.IRISUint } - item.IncrementCommission = task.getIncrementCommission(pcommission, tcommission, latestone.TerminalCommission) + bcommission, ok := task.AddressBeginCommission[address] + if !ok { + bcommission = document.Coin{ + Denom: types.IRISAttoUint, + } + } + + item.IncrementCommission = task.getIncrementCommission(pcommission, tcommission, bcommission) if desp, ok := service.ValidatorsDescriptionMap[terminalval.OperatorAddress]; ok { item.Moniker = desp.Moniker @@ -358,12 +344,10 @@ func (task *StaticValidatorByMonthTask) getFoundationDelegateIncre(foundationDel } func (task *StaticValidatorByMonthTask) getIncrementCommission(pcommission, terminalCommission, -latestoneCommission document.Coin) (IncreCommission document.Coin) { +beginCommission document.Coin) (IncreCommission document.Coin) { //Rcx = Rcn - Rcn-1 + Rcw - if latestoneCommission.Denom == types.IRISUint { - latestoneCommission.Amount = latestoneCommission.Amount * math.Pow10(18) - } - IncreCommission.Amount = terminalCommission.Amount - latestoneCommission.Amount + pcommission.Amount + + IncreCommission.Amount = terminalCommission.Amount - beginCommission.Amount + pcommission.Amount IncreCommission.Denom = terminalCommission.Denom return diff --git a/backend/task/static_validator_by_month_test.go b/backend/task/static_validator_by_month_test.go index 0487c4d16..c61b18c5b 100644 --- a/backend/task/static_validator_by_month_test.go +++ b/backend/task/static_validator_by_month_test.go @@ -1,14 +1,14 @@ package task import ( - "testing" "encoding/json" - "time" - "github.com/irisnet/explorer/backend/types" "fmt" "github.com/irisnet/explorer/backend/orm/document" - "github.com/irisnet/explorer/backend/vo" "github.com/irisnet/explorer/backend/service" + "github.com/irisnet/explorer/backend/types" + "github.com/irisnet/explorer/backend/vo" + "testing" + "time" ) var ( @@ -63,7 +63,7 @@ func init() { } func TestStaticValidatorByMonthTask_getStaticValidator(t *testing.T) { - task.SetAddressCoinMapData(dtask.AddressCoin, dtask.AddrPeriodCommission, dtask.AddrTerminalCommission) + task.SetAddressCoinMapData(dtask.AddressCoin, dtask.AddrPeriodCommission, dtask.AddrBeginCommission, dtask.AddrTerminalCommission) starttime, _ := time.ParseInLocation(types.TimeLayout, fmt.Sprintf("%d-%02d-%02dT00:00:00", 2020, 5, 1), cstZone) datetime, _ := time.ParseInLocation(types.TimeLayout, fmt.Sprintf("%d-%02d-%02dT00:00:00", 2020, 5, 31), cstZone) endtime, createat, err := document.Getdate(task.staticModel.Name(), starttime, datetime, "-"+document.ExStaticDelegatorDateTag) @@ -115,13 +115,13 @@ func TestStaticValidatorByMonthTask_DoTask(t *testing.T) { } func TestMonthDoTask(t *testing.T) { - starttime, _ := time.ParseInLocation(types.TimeLayout, fmt.Sprintf("%d-%02d-%02dT00:00:00", 2020, 5, 1), cstZone) - endtime, _ := time.ParseInLocation(types.TimeLayout, fmt.Sprintf("%d-%02d-%02dT00:00:00", 2020, 5, 31), cstZone) + starttime, _ := time.ParseInLocation(types.TimeLayout, fmt.Sprintf("%d-%02d-%02dT00:00:00", 2020, 6, 1), cstZone) + endtime, _ := time.ParseInLocation(types.TimeLayout, fmt.Sprintf("%d-%02d-%02dT00:00:00", 2020, 7, 1), cstZone) delegatortask := new(StaticDelegatorByMonthTask) validatortask := new(StaticValidatorByMonthTask) delegatortask.SetCaculateScope(starttime, endtime) //delegatortask.SetCaculateAddress("iaa1xf5jaw09klqg9hzxfks3ycjvqgnpyjcm0yrkut") - data, err := delegatortask.caculateWork() + data, err := delegatortask.calculateWork() if err != nil { t.Fatal(err.Error()) } @@ -133,10 +133,10 @@ func TestMonthDoTask(t *testing.T) { } bytedata, _ := json.Marshal(vomodel) fmt.Println(string(bytedata)) - fmt.Println("caculateWork have done,then validatortask work") + fmt.Println("calculateWork have done,then validatortask work") //validatortask.SetCaculateAddress("iva1qq93sapmdcx36uz64vvw5gzuevtxsc7lcfxsat") validatortask.SetCaculateScope(starttime, endtime) - validatortask.SetAddressCoinMapData(delegatortask.AddressCoin, delegatortask.AddrPeriodCommission, delegatortask.AddrTerminalCommission) + validatortask.SetAddressCoinMapData(delegatortask.AddressCoin, delegatortask.AddrPeriodCommission, delegatortask.AddrBeginCommission, delegatortask.AddrTerminalCommission) valdata, err := validatortask.caculateWork() if err != nil { t.Fatal(err.Error()) diff --git a/backend/task/types.go b/backend/task/types.go index e3494068e..9e0c02479 100644 --- a/backend/task/types.go +++ b/backend/task/types.go @@ -26,8 +26,10 @@ var ( rewardsDenom = []string{"iris-atto"} CronJob = map[string]bool{ - "static_delegator": true, - "static_validator": true, + "static_delegator": true, + "static_validator": true, + "static_delegator_month": true, + "static_validator_month": true, } ) @@ -114,6 +116,7 @@ func Start() { waitDelegatortaskDone.Wait() if len(delegatortask.AddressCoin) == 0 || len(delegatortask.AddrPeriodCommission) == 0 || + len(delegatortask.AddrBeginCommission) == 0 || len(delegatortask.AddrTerminalCommission) == 0 { toJson := func(obj interface{}) string { return string(utils.MarshalJsonIgnoreErr(obj)) @@ -121,10 +124,11 @@ func Start() { logger.Warn("delegator rewards or commission rewards is nil", logger.String("rewards", toJson(delegatortask.AddressCoin)), logger.String("periodCommission", toJson(delegatortask.AddrPeriodCommission)), + logger.String("beginCommission", toJson(delegatortask.AddrBeginCommission)), logger.String("terminalCommission", toJson(delegatortask.AddrTerminalCommission))) } validatortask.SetAddressCoinMapData(delegatortask.AddressCoin, delegatortask.AddrPeriodCommission, - delegatortask.AddrTerminalCommission) + delegatortask.AddrBeginCommission, delegatortask.AddrTerminalCommission) validatortask.Start() }) } diff --git a/backend/vo/caculate.go b/backend/vo/caculate.go index 917281f26..e476df455 100644 --- a/backend/vo/caculate.go +++ b/backend/vo/caculate.go @@ -16,6 +16,7 @@ type ExStaticDelegatorMonthVo struct { TerminalDelegation float64 `json:"期末在托量"` IncrementDelegation float64 `json:"期间委托净增量"` PeriodDelegationTimes int `json:"期间委托交易次数"` + AnnualizedRate string `json:"年化"` CaculateDate string `json:"统计更新日期"` //CreateAt int64 `json:"create_at"` //UpdateAt int64 `json:"update_at"`