From 333109b2aa85f94782e9db80e47bb4435594b507 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=20=E4=BC=9F=E6=98=8C?= Date: Fri, 15 May 2020 18:31:27 +0800 Subject: [PATCH 01/27] adjust implement of getTotalVotingPower --- backend/service/service_validator.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/backend/service/service_validator.go b/backend/service/service_validator.go index 1d16d1c3f..0568d58a4 100644 --- a/backend/service/service_validator.go +++ b/backend/service/service_validator.go @@ -36,7 +36,7 @@ func (service *ValidatorService) GetValidators(typ, origin string, page, size in panic(types.CodeNotFound) } - var totalVotingPower = getTotalVotingPower() + var totalVotingPower = getTotalVotingPower(validatorList) for i, v := range validatorList { if desc, ok := blackList[v.OperatorAddress]; ok { validatorList[i].Description.Moniker = desc.Moniker @@ -1114,12 +1114,16 @@ func isEqual(srcValidator, dstValidator document.Validator) bool { // return power.QuoInt(tokenPrecision).RoundInt64() //} -func getTotalVotingPower() int64 { +func getTotalVotingPower(validators []document.Validator) int64 { var total = int64(0) - var set = lcd.LatestValidatorSet() - for _, v := range set.Validators { - votingPower := utils.ParseIntWithDefault(v.VotingPower, 0) - total += votingPower + //var set = lcd.LatestValidatorSet() + //for _, v := range set.Validators { + // votingPower := utils.ParseIntWithDefault(v.VotingPower, 0) + // total += votingPower + //} + + for _, val := range validators { + total += val.VotingPower } return total } From 113305d756fc300dba8e6160e48f510815867009 Mon Sep 17 00:00:00 2001 From: kaifei Date: Fri, 3 Jul 2020 11:31:23 +0800 Subject: [PATCH 02/27] only update account rewards which prefix is "iaa" --- backend/lcd/stake_test.go | 5 -- backend/lcd/types.go | 122 +++++++++++++++---------------- backend/rest/server.go | 2 - backend/service/service_block.go | 6 +- backend/task/update_account.go | 8 +- 5 files changed, 68 insertions(+), 75 deletions(-) diff --git a/backend/lcd/stake_test.go b/backend/lcd/stake_test.go index 7cabb443b..3813c86fa 100644 --- a/backend/lcd/stake_test.go +++ b/backend/lcd/stake_test.go @@ -111,8 +111,3 @@ func TestGetUnbondingDelegationsByDelegatorAddr(t *testing.T) { t.Logf("k: %v v: %v \n", k, v) } } - -func TestGetDelegationsFromValAddrByDelAddr(t *testing.T) { - delegation := GetDelegationsFromValAddrByDelAddr("iaa1w7ewedr57z6p7f8nknmdvukfxwkwlsvfjumdts", "iva1qq93sapmdcx36uz64vvw5gzuevtxsc7lcfxsat") - t.Log(delegation.Tokens) -} diff --git a/backend/lcd/types.go b/backend/lcd/types.go index d77642bc5..10b03e6b7 100644 --- a/backend/lcd/types.go +++ b/backend/lcd/types.go @@ -8,27 +8,27 @@ import ( ) const ( - UrlAccount = "%s/bank/accounts/%s" - UrlBankTokenStats = "%s/bank/token-stats" - UrlValidator = "%s/stake/validators/%s" - UrlValidators = "%s/stake/validators?page=%d&size=%d" - UrlDelegationByVal = "%s/stake/validators/%s/delegations" - UrlDelegationsByDelegator = "%s/stake/delegators/%s/delegations" + UrlAccount = "%s/bank/accounts/%s" + UrlBankTokenStats = "%s/bank/token-stats" + UrlValidator = "%s/stake/validators/%s" + UrlValidators = "%s/stake/validators?page=%d&size=%d" + UrlDelegationByVal = "%s/stake/validators/%s/delegations" + UrlDelegationsByDelegator = "%s/stake/delegators/%s/delegations" //UrlDelegationsFromValidatorByDelegator = "%s/stake/delegators/%s/validators/%s" - UrlUnbondingDelegationByDelegator = "%s/stake/delegators/%s/unbonding-delegations" - UrlDelegationsByValidator = "%s/stake/validators/%s/delegations" - UrlUnbondingDelegationByValidator = "%s/stake/validators/%s/unbonding-delegations" - UrlRedelegationsByValidator = "%s/stake/validators/%s/redelegations" - UrlSignInfo = "%s/slashing/validators/%s/signing-info" - UrlNodeInfo = "%s/node-info" - UrlNodeVersion = "%s/node-version" - UrlGenesis = "%s/genesis" - UrlWithdrawAddress = "%s/distribution/%s/withdraw-address" - UrlBlockLatest = "%s/blocks/latest" - UrlBlock = "%s/blocks/%d" - UrlValidatorSet = "%s/validatorsets/%d" - UrlValidatorSetLatest = "%s/validatorsets/latest" - UrlStakePool = "%s/stake/pool" + UrlUnbondingDelegationByDelegator = "%s/stake/delegators/%s/unbonding-delegations" + UrlDelegationsByValidator = "%s/stake/validators/%s/delegations" + UrlUnbondingDelegationByValidator = "%s/stake/validators/%s/unbonding-delegations" + UrlRedelegationsByValidator = "%s/stake/validators/%s/redelegations" + UrlSignInfo = "%s/slashing/validators/%s/signing-info" + UrlNodeInfo = "%s/node-info" + UrlNodeVersion = "%s/node-version" + UrlGenesis = "%s/genesis" + UrlWithdrawAddress = "%s/distribution/%s/withdraw-address" + UrlBlockLatest = "%s/blocks/latest" + UrlBlock = "%s/blocks/%d" + UrlValidatorSet = "%s/validatorsets/%d" + UrlValidatorSetLatest = "%s/validatorsets/latest" + UrlStakePool = "%s/stake/pool" UrlBlocksResult = "%s/block-results/%d" UrlTxsTxHash = "%s/txs/%s" UrlGovParam = "%s/params?module=%s" @@ -46,8 +46,8 @@ const ( ) type AccountVo struct { - Address string `json:"address"` - Coins []string `json:"coins"` + Address string `json:"address"` + Coins []string `json:"coins"` PublicKey struct { Type string `json:"type"` Value string `json:"value"` @@ -126,7 +126,7 @@ type NodeInfoVo struct { Version string `json:"version"` Channels string `json:"channels"` Moniker string `json:"moniker"` - Other struct { + Other struct { TxIndex string `json:"tx_index"` RPCAddress string `json:"rpc_address"` } `json:"other"` @@ -134,10 +134,10 @@ type NodeInfoVo struct { type GenesisVo struct { Jsonrpc string `json:"jsonrpc"` ID string `json:"id"` - Result struct { + Result struct { Genesis struct { - GenesisTime time.Time `json:"genesis_time"` - ChainID string `json:"chain_id"` + GenesisTime time.Time `json:"genesis_time"` + ChainID string `json:"chain_id"` ConsensusParams struct { BlockSize struct { MaxBytes string `json:"max_bytes"` @@ -150,7 +150,7 @@ type GenesisVo struct { PubKeyTypes []string `json:"pub_key_types"` } `json:"validator"` } `json:"consensus_params"` - AppHash string `json:"app_hash"` + AppHash string `json:"app_hash"` AppState struct { Accounts []struct { Address string `json:"address"` @@ -160,7 +160,7 @@ type GenesisVo struct { } `json:"accounts"` Auth struct { CollectedFee interface{} `json:"collected_fee"` - Data struct { + Data struct { NativeFeeDenom string `json:"native_fee_denom"` } `json:"data"` Params struct { @@ -216,7 +216,7 @@ type GenesisVo struct { Gov struct { Params struct { CriticalDepositPeriod string `json:"critical_deposit_period"` - CriticalMinDeposit []struct { + CriticalMinDeposit []struct { Denom string `json:"denom"` Amount string `json:"amount"` } `json:"critical_min_deposit"` @@ -227,7 +227,7 @@ type GenesisVo struct { CriticalParticipation string `json:"critical_participation"` CriticalPenalty string `json:"critical_penalty"` ImportantDepositPeriod string `json:"important_deposit_period"` - ImportantMinDeposit []struct { + ImportantMinDeposit []struct { Denom string `json:"denom"` Amount string `json:"amount"` } `json:"important_min_deposit"` @@ -238,7 +238,7 @@ type GenesisVo struct { ImportantParticipation string `json:"important_participation"` ImportantPenalty string `json:"important_penalty"` NormalDepositPeriod string `json:"normal_deposit_period"` - NormalMinDeposit []struct { + NormalMinDeposit []struct { Denom string `json:"denom"` Amount string `json:"amount"` } `json:"normal_min_deposit"` @@ -255,7 +255,7 @@ type GenesisVo struct { GenesisVersion struct { UpgradeInfo struct { ProposalID string `json:"ProposalID"` - Protocol struct { + Protocol struct { Version string `json:"version"` Software string `json:"software"` Height string `json:"height"` @@ -309,10 +309,10 @@ type GenesisVo struct { } `json:"trustees"` } `json:"guardian"` Gentxs []struct { - Type string `json:"type"` + Type string `json:"type"` Value struct { Msg []struct { - Type string `json:"type"` + Type string `json:"type"` Value struct { Description struct { Moniker string `json:"moniker"` @@ -327,7 +327,7 @@ type GenesisVo struct { } `json:"Commission"` DelegatorAddress string `json:"delegator_address"` ValidatorAddress string `json:"validator_address"` - Pubkey struct { + Pubkey struct { Type string `json:"type"` Value string `json:"value"` } `json:"pubkey"` @@ -361,7 +361,7 @@ type GenesisVo struct { type BlockVo struct { BlockMeta struct { BlockID struct { - Hash string `json:"hash"` + Hash string `json:"hash"` Parts struct { Total string `json:"total"` Hash string `json:"hash"` @@ -372,13 +372,13 @@ type BlockVo struct { Block string `json:"block"` App string `json:"app"` } `json:"version"` - ChainID string `json:"chain_id"` - Height string `json:"height"` - Time time.Time `json:"time"` - NumTxs string `json:"num_txs"` - TotalTxs string `json:"total_txs"` + ChainID string `json:"chain_id"` + Height string `json:"height"` + Time time.Time `json:"time"` + NumTxs string `json:"num_txs"` + TotalTxs string `json:"total_txs"` LastBlockID struct { - Hash string `json:"hash"` + Hash string `json:"hash"` Parts struct { Total string `json:"total"` Hash string `json:"hash"` @@ -401,13 +401,13 @@ type BlockVo struct { Block string `json:"block"` App string `json:"app"` } `json:"version"` - ChainID string `json:"chain_id"` - Height string `json:"height"` - Time time.Time `json:"time"` - NumTxs string `json:"num_txs"` - TotalTxs string `json:"total_txs"` + ChainID string `json:"chain_id"` + Height string `json:"height"` + Time time.Time `json:"time"` + NumTxs string `json:"num_txs"` + TotalTxs string `json:"total_txs"` LastBlockID struct { - Hash string `json:"hash"` + Hash string `json:"hash"` Parts struct { Total string `json:"total"` Hash string `json:"hash"` @@ -423,26 +423,20 @@ type BlockVo struct { EvidenceHash string `json:"evidence_hash"` ProposerAddress string `json:"proposer_address"` } `json:"header"` - Data struct { - Txs interface{} `json:"txs"` - } `json:"data"` - Evidence struct { - Evidence interface{} `json:"evidence"` - } `json:"evidence"` LastCommit struct { BlockID struct { - Hash string `json:"hash"` + Hash string `json:"hash"` Parts struct { Total string `json:"total"` Hash string `json:"hash"` } `json:"parts"` } `json:"block_id"` Precommits []struct { - Type int `json:"type"` - Height string `json:"height"` - Round string `json:"round"` + Type int `json:"type"` + Height string `json:"height"` + Round string `json:"round"` BlockID struct { - Hash string `json:"hash"` + Hash string `json:"hash"` Parts struct { Total string `json:"total"` Hash string `json:"hash"` @@ -459,7 +453,7 @@ type BlockVo struct { type ValidatorSetVo struct { BlockHeight string `json:"block_height"` - Validators []struct { + Validators []struct { Address string `json:"address"` PubKey string `json:"pub_key"` ProposerPriority string `json:"proposer_priority"` @@ -568,7 +562,7 @@ type SignInfoVo struct { } type BlockResultVo struct { - Height string `json:"height"` + Height string `json:"height"` Results struct { DeliverTx []struct { Code int `json:"code"` @@ -577,7 +571,7 @@ type BlockResultVo struct { Info string `json:"info"` GasWanted string `json:"gas_wanted"` GasUsed string `json:"gas_used"` - Tags []struct { + Tags []struct { Key string `json:"key"` Value string `json:"value"` } `json:"tags"` @@ -591,7 +585,7 @@ type BlockResultVo struct { Power string `json:"power"` } `json:"validator_updates"` ConsensusParamUpdates interface{} `json:"consensus_param_updates"` - Tags []struct { + Tags []struct { Key string `json:"key"` Value string `json:"value"` } `json:"tags"` @@ -614,7 +608,7 @@ type BlockCoinFlowVo struct { type Tx struct { Value struct { Msg []struct { - Type string `json:"type"` + Type string `json:"type"` Value struct { DelegatorAddr string `json:"delegator_addr"` } `json:"value"` diff --git a/backend/rest/server.go b/backend/rest/server.go index 41bf464c6..b45290510 100644 --- a/backend/rest/server.go +++ b/backend/rest/server.go @@ -83,8 +83,6 @@ func registerApi(r *mux.Router) { func registerFilters() { filter.Register(filter.LogPreFilter{}) filter.Register(filter.LogPostFilter{}) - filter.Register(filter.FaucetLimitPreFilter{}) - filter.Register(filter.FaucetLimitPostFilter{}) } func NewAPIMux() *mux.Router { diff --git a/backend/service/service_block.go b/backend/service/service_block.go index ce2fff473..aba280ce3 100644 --- a/backend/service/service_block.go +++ b/backend/service/service_block.go @@ -9,6 +9,7 @@ import ( "github.com/irisnet/explorer/backend/types" "github.com/irisnet/explorer/backend/utils" "github.com/irisnet/explorer/backend/vo" + "gopkg.in/mgo.v2" ) type BlockService struct { @@ -79,9 +80,10 @@ func (service *BlockService) QueryBlockInfo(height int64) vo.BlockInfo { proposerHexAddr := currentBlock.BlockMeta.Header.ProposerAddress validatorDoc, err := document.Validator{}.GetValidatorByProposerAddr(proposerHexAddr) - if err != nil { + result.PropopserMoniker = proposerHexAddr + if err != nil && err != mgo.ErrNotFound { logger.Error("query validator collection err", logger.String("error", err.Error()), service.GetTraceLog()) - result.PropopserMoniker = proposerHexAddr + } else { result.PropoperAddr = validatorDoc.OperatorAddress result.PropopserMoniker = validatorDoc.Description.Moniker diff --git a/backend/task/update_account.go b/backend/task/update_account.go index c0617a1ac..ed862ea8c 100644 --- a/backend/task/update_account.go +++ b/backend/task/update_account.go @@ -2,12 +2,13 @@ package task import ( "github.com/irisnet/explorer/backend/conf" - "github.com/irisnet/explorer/backend/utils" + "github.com/irisnet/explorer/backend/lcd" "github.com/irisnet/explorer/backend/logger" "github.com/irisnet/explorer/backend/orm/document" - "github.com/irisnet/explorer/backend/lcd" "github.com/irisnet/explorer/backend/types" + "github.com/irisnet/explorer/backend/utils" "strconv" + "strings" ) type UpdateAccount struct { @@ -37,6 +38,9 @@ func (task UpdateAccount) DoTask() error { } for i, val := range accounts { + if !strings.HasPrefix(val.Address, conf.Get().Hub.Prefix.AccAddr) { + continue + } _, _, rewards, err := lcd.GetDistributionRewardsByValidatorAcc(val.Address) if err == nil && len(rewards) > 0 { newrewards := loadRewards(rewards) From 2906a4fa4f65923ce28a852e125f6c3c43f24ca4 Mon Sep 17 00:00:00 2001 From: huangweichang Date: Fri, 3 Jul 2020 15:58:32 +0800 Subject: [PATCH 03/27] add heartbeat in cron task --- backend/README.md | 1 + backend/conf/config.go | 30 ++-- backend/rest/controller/task.go | 12 +- backend/task/asset_gateway.go | 3 +- backend/task/asset_tokens.go | 3 +- backend/task/gov_params.go | 3 +- backend/task/heartbeat.go | 152 ++++++++++++++++++ backend/task/heartbeat_test.go | 40 +++++ backend/task/static_delegator.go | 3 +- backend/task/static_delegator_by_month.go | 3 +- .../task/static_delegator_by_month_test.go | 2 +- backend/task/static_validator.go | 3 +- backend/task/static_validator_by_month.go | 3 +- .../task/static_validator_by_month_test.go | 2 +- backend/task/task_control.go | 7 +- backend/task/task_control_test.go | 2 +- backend/task/task_test.go | 19 --- backend/task/tx_num_group_by_day.go | 3 +- backend/task/types.go | 2 +- backend/task/update_account.go | 3 +- backend/task/update_proposal_voters.go | 3 +- backend/task/update_validator.go | 3 +- backend/task/update_validator_static_info.go | 3 +- backend/task/update_validatoricons.go | 3 +- 24 files changed, 250 insertions(+), 58 deletions(-) create mode 100644 backend/task/heartbeat.go create mode 100644 backend/task/heartbeat_test.go delete mode 100644 backend/task/task_test.go diff --git a/backend/README.md b/backend/README.md index 37e5ea31e..70510ec56 100644 --- a/backend/README.md +++ b/backend/README.md @@ -35,6 +35,7 @@ Blockchain explorer for the IRIS Hub CronTimeAccountRewards: time interval of update account rewards CronTimeValidators: time interval of update validators CronTimeValidatorIcons: time interval of update validator icons + CronTimeHeartBeat: time interval of heart beat in cron task //irishub v0.11.0 add diff --git a/backend/conf/config.go b/backend/conf/config.go index 63aa5f77e..68d784a44 100644 --- a/backend/conf/config.go +++ b/backend/conf/config.go @@ -41,6 +41,7 @@ const ( KeyCronTimeGovParams = "CronTimeGovParams" KeyCronTimeTxNumByDay = "CronTimeTxNumByDay" KeyCronTimeControlTask = "CronTimeControlTask" + KeyCronTimeHeartBeat = "CronTimeHeartBeat" KeyCronTimeValidators = "CronTimeValidators" KeyCronTimeAccountRewards = "CronTimeAccountRewards" KeyCronTimeValidatorIcons = "CronTimeValidatorIcons" @@ -96,18 +97,19 @@ func init() { ApiVersion: getEnv(KeyApiVersion, DefaultEnvironment), MaxDrawCnt: getEnvInt(KeyMaxDrawCnt, DefaultEnvironment), ShowFaucet: getEnv(KeyShowFaucet, DefaultEnvironment), - CurEnv: getEnv(KeyCurEnv, DefaultEnvironment), - CronTimeAssetGateways: getEnvInt(KeyCronTimeAssetGateways, DefaultEnvironment), - CronTimeAssetTokens: getEnvInt(KeyCronTimeAssetTokens, DefaultEnvironment), - CronTimeGovParams: getEnvInt(KeyCronTimeGovParams, DefaultEnvironment), - CronTimeTxNumByDay: getEnvInt(KeyCronTimeTxNumByDay, DefaultEnvironment), - CronTimeControlTask: getEnvInt(KeyCronTimeControlTask, DefaultEnvironment), - CronTimeAccountRewards: getEnvInt(KeyCronTimeAccountRewards, DefaultEnvironment), - CronTimeValidators: getEnvInt(KeyCronTimeValidators, DefaultEnvironment), - CronTimeValidatorIcons: getEnvInt(KeyCronTimeValidatorIcons, DefaultEnvironment), - CronTimeProposalVoters: getEnvInt(KeyCronTimeProposalVoters, DefaultEnvironment), - CronTimeValidatorStaticInfo: getEnvInt(KeyCronTimeValidatorStaticInfo, DefaultEnvironment), - CronTimeFormatStaticDay: getEnv(KeyCronTimeFormatStaticDay, DefaultEnvironment), + CurEnv: getEnv(KeyCurEnv, DefaultEnvironment), + CronTimeAssetGateways: getEnvInt(KeyCronTimeAssetGateways, DefaultEnvironment), + CronTimeAssetTokens: getEnvInt(KeyCronTimeAssetTokens, DefaultEnvironment), + CronTimeGovParams: getEnvInt(KeyCronTimeGovParams, DefaultEnvironment), + CronTimeTxNumByDay: getEnvInt(KeyCronTimeTxNumByDay, DefaultEnvironment), + CronTimeControlTask: getEnvInt(KeyCronTimeControlTask, DefaultEnvironment), + CronTimeHeartBeat: getEnvInt(KeyCronTimeHeartBeat, DefaultEnvironment), + CronTimeAccountRewards: getEnvInt(KeyCronTimeAccountRewards, DefaultEnvironment), + CronTimeValidators: getEnvInt(KeyCronTimeValidators, DefaultEnvironment), + CronTimeValidatorIcons: getEnvInt(KeyCronTimeValidatorIcons, DefaultEnvironment), + CronTimeProposalVoters: getEnvInt(KeyCronTimeProposalVoters, DefaultEnvironment), + CronTimeValidatorStaticInfo: getEnvInt(KeyCronTimeValidatorStaticInfo, DefaultEnvironment), + CronTimeFormatStaticDay: getEnv(KeyCronTimeFormatStaticDay, DefaultEnvironment), CronTimeFormatStaticMonth: getEnv(KeyCronTimeFormatStaticMonth, DefaultEnvironment), CronTimeStaticDelegator: getEnvInt(KeyCronTimeStaticDataDay, DefaultEnvironment), CronTimeStaticValidator: getEnvInt(KeyCronTimeStaticDataDay, DefaultEnvironment), @@ -173,7 +175,8 @@ func loadDefault() { KeyCronTimeAssetTokens: "60", KeyCronTimeGovParams: "3600", KeyCronTimeTxNumByDay: "86400", - KeyCronTimeControlTask: "30", + KeyCronTimeControlTask: "60", + KeyCronTimeHeartBeat: "10", KeyCronTimeValidators: "60", KeyCronTimeAccountRewards: "600", KeyCronTimeProposalVoters: "60", @@ -224,6 +227,7 @@ type serverConf struct { CronTimeGovParams int CronTimeTxNumByDay int CronTimeControlTask int + CronTimeHeartBeat int CronTimeValidators int CronTimeAccountRewards int CronTimeValidatorStaticInfo int diff --git a/backend/rest/controller/task.go b/backend/rest/controller/task.go index 650c830d9..8263c5a39 100644 --- a/backend/rest/controller/task.go +++ b/backend/rest/controller/task.go @@ -37,7 +37,7 @@ func RegisterCronTask(r *mux.Router) error { func registerCronTaskAssetGateways(r *mux.Router) error { doApi(r, types.UrlRegisterDoCronTaskAssetGateways, "PUT", func(request vo.IrisReq) interface{} { cronTask := task.UpdateAssetGateways{} - if err := cronTask.DoTask(); err != nil { + if err := cronTask.DoTask(task.HeartBeat); err != nil { return err } else { return types.CodeSuccess @@ -57,7 +57,7 @@ func registerCronTaskAssetGateways(r *mux.Router) error { func registerCronTaskAssetTokens(r *mux.Router) error { doApi(r, types.UrlRegisterDoCronTaskAssetTokens, "PUT", func(request vo.IrisReq) interface{} { cronTask := task.UpdateAssetTokens{} - if err := cronTask.DoTask(); err != nil { + if err := cronTask.DoTask(task.HeartBeat); err != nil { return err } else { return types.CodeSuccess @@ -77,7 +77,7 @@ func registerCronTaskAssetTokens(r *mux.Router) error { func registerCronTaskGovParams(r *mux.Router) error { doApi(r, types.UrlRegisterDoCronTaskGovParams, "PUT", func(request vo.IrisReq) interface{} { cronTask := task.UpdateGovParams{} - if err := cronTask.DoTask(); err != nil { + if err := cronTask.DoTask(task.HeartBeat); err != nil { return err } else { return types.CodeSuccess @@ -97,7 +97,7 @@ func registerCronTaskGovParams(r *mux.Router) error { func registerCronTaskTxNumByDay(r *mux.Router) error { doApi(r, types.UrlRegisterDoCronTaskTxNumByDay, "PUT", func(request vo.IrisReq) interface{} { cronTask := task.TxNumGroupByDayTask{} - if err := cronTask.DoTask(); err != nil { + if err := cronTask.DoTask(task.HeartBeat); err != nil { return err } else { return types.CodeSuccess @@ -117,7 +117,7 @@ func registerCronTaskTxNumByDay(r *mux.Router) error { func registerCronTaskValidators(r *mux.Router) error { doApi(r, types.UrlRegisterDoCronTaskValidators, "PUT", func(request vo.IrisReq) interface{} { cronTask := task.UpdateValidator{} - if err := cronTask.DoTask(); err != nil { + if err := cronTask.DoTask(task.HeartBeat); err != nil { return err } else { return types.CodeSuccess @@ -137,7 +137,7 @@ func registerCronTaskValidators(r *mux.Router) error { func registerCronTaskValidatorIcons(r *mux.Router) error { doApi(r, types.UrlRegisterDoCronTaskValidatorIcons, "PUT", func(request vo.IrisReq) interface{} { cronTask := task.UpdateValidatorIcons{} - if err := cronTask.DoTask(); err != nil { + if err := cronTask.DoTask(task.HeartBeat); err != nil { return err } else { return types.CodeSuccess diff --git a/backend/task/asset_gateway.go b/backend/task/asset_gateway.go index 8ba2fed11..04c6f6cf9 100644 --- a/backend/task/asset_gateway.go +++ b/backend/task/asset_gateway.go @@ -25,7 +25,8 @@ func (task UpdateAssetGateways) Start() { }) } -func (task UpdateAssetGateways) DoTask() error { +func (task UpdateAssetGateways) DoTask(fn func(string) chan bool) error { + defer HeartQuit(fn(task.Name())) assetGateways, err := document.AssetGateways{}.GetAllAssetGateways() if err != nil { return err diff --git a/backend/task/asset_tokens.go b/backend/task/asset_tokens.go index b0ba4c8de..e7e0f4a29 100644 --- a/backend/task/asset_tokens.go +++ b/backend/task/asset_tokens.go @@ -25,7 +25,8 @@ func (task UpdateAssetTokens) Start() { }) } -func (task UpdateAssetTokens) DoTask() error { +func (task UpdateAssetTokens) DoTask(fn func(string) chan bool) error { + defer HeartQuit(fn(task.Name())) assetTokens, err := document.AssetToken{}.GetAllAssets() if err != nil { return err diff --git a/backend/task/gov_params.go b/backend/task/gov_params.go index 8cb69cf84..60f3e8c8e 100644 --- a/backend/task/gov_params.go +++ b/backend/task/gov_params.go @@ -24,7 +24,8 @@ func (task UpdateGovParams) Start() { }) } -func (task UpdateGovParams) DoTask() error { +func (task UpdateGovParams) DoTask(fn func(string) chan bool) error { + defer HeartQuit(fn(task.Name())) curModuleKv, err := lcd.GetAllGovModuleParam() if err != nil { return err diff --git a/backend/task/heartbeat.go b/backend/task/heartbeat.go new file mode 100644 index 000000000..848e42325 --- /dev/null +++ b/backend/task/heartbeat.go @@ -0,0 +1,152 @@ +package task + +import ( + "time" + "github.com/irisnet/explorer/backend/orm/document" + "github.com/irisnet/explorer/backend/logger" + "github.com/irisnet/explorer/backend/conf" + "os" + "os/signal" +) + +func HeartBeat(taskName string) chan bool { + //do heartbeat + stop := make(chan bool) + go heartBeat(taskName, stop) + return stop +} + +func HeartQuit(stop chan bool) { + stop <- true +} + +// heart beat of task not be executed as follow: +// - query task failed in task control table +// - task is not running +// - now sub task latest update time less than heart beat time interval +func heartBeat(taskName string, stop chan bool) { + logger.Info("HeartBeat Starting...", logger.String("taskName", taskName)) + signalstop := make(chan os.Signal) + signal.Notify(signalstop, os.Interrupt) + var taskcontrol document.TaskControl + timeInterval := int64(conf.Get().Server.CronTimeHeartBeat) + ticker := time.NewTicker(time.Duration(timeInterval) * time.Second) + defer ticker.Stop() + + doTask := func() { + doc, err := taskcontrol.QueryOneByTaskName(taskName) + if err != nil { + logger.Error("QueryOneByTaskName have error", + logger.String("taskName", taskName), + logger.String("tableName", taskcontrol.Name()), + logger.String("err", err.Error())) + return + } + curtime := time.Now().Unix() + if curtime > doc.LatestExecTime+timeInterval { + doc.LatestExecTime = time.Now().Unix() + if err := taskcontrol.UpdateByPK(doc); err != nil { + logger.Error("update segment have error", + logger.String("taskName", taskName), + logger.String("tableName", doc.Name()), + logger.String("err", err.Error())) + return + } + } + } + doTask() + for { + select { + case <-ticker.C: + doTask() + logger.Info("HeartBeat is ok", logger.String("taskName", taskName)) + case <-stop: + close(stop) + logger.Info("HeartBeat Quit...", logger.String("taskName", taskName)) + return + case <-signalstop: + close(signalstop) + logger.Info("Interrupt HeartBeat Quit...", logger.String("taskName", taskName)) + return + + } + + } +} + +// +//func HeartBeat1(taskName string, ctx context.Context) { +// var taskcontrol document.TaskControl +// timeInterval := int64(conf.Get().Server.CronTimeHeartBeat) +// ticker := time.NewTicker(time.Duration(timeInterval) * time.Second) +// defer ticker.Stop() +// for { +// select { +// case <-ticker.C: +// doc, err := taskcontrol.QueryOneByTaskName(taskName) +// if err != nil { +// logger.Error("QueryOneByTaskName have error", +// logger.String("taskName", taskName), +// logger.String("tableName", taskcontrol.Name()), +// logger.String("err", err.Error())) +// return +// } +// curtime := time.Now().Unix() +// if curtime > doc.LatestExecTime+timeInterval { +// doc.LatestExecTime = time.Now().Unix() +// if err := taskcontrol.UpdateByPK(doc); err != nil { +// logger.Error("update segment have error", +// logger.String("taskName", taskName), +// logger.String("tableName", doc.Name()), +// logger.String("err", err.Error())) +// return +// } +// } +// logger.Info("HeartBeat is ok", logger.String("taskName", taskName)) +// case <-ctx.Done(): +// logger.Info("HeartBeat Quit...", logger.String("taskName", taskName)) +// return +// +// } +// +// } +//} +// +//func HeartBeat2(taskName string) { +// var taskcontrol document.TaskControl +// timeInterval := int64(conf.Get().Server.CronTimeHeartBeat) +// ticker := time.NewTicker(time.Duration(timeInterval) * time.Second) +// stop := make(chan os.Signal) +// signal.Notify(stop, os.Interrupt) +// defer ticker.Stop() +// for { +// select { +// case <-ticker.C: +// doc, err := taskcontrol.QueryOneByTaskName(taskName) +// if err != nil { +// logger.Error("QueryOneByTaskName have error", +// logger.String("taskName", taskName), +// logger.String("tableName", taskcontrol.Name()), +// logger.String("err", err.Error())) +// return +// } +// curtime := time.Now().Unix() +// if curtime > doc.LatestExecTime+timeInterval { +// doc.LatestExecTime = time.Now().Unix() +// if err := taskcontrol.UpdateByPK(doc); err != nil { +// logger.Error("update segment have error", +// logger.String("taskName", taskName), +// logger.String("tableName", doc.Name()), +// logger.String("err", err.Error())) +// return +// } +// } +// logger.Info("HeartBeat is ok", logger.String("taskName", taskName)) +// case <-stop: +// logger.Info("HeartBeat Quit...", logger.String("taskName", taskName)) +// return +// +// } +// +// } +//} diff --git a/backend/task/heartbeat_test.go b/backend/task/heartbeat_test.go new file mode 100644 index 000000000..3537f2d30 --- /dev/null +++ b/backend/task/heartbeat_test.go @@ -0,0 +1,40 @@ +package task + +import ( + "testing" + "time" +) + +func TestTask(t *testing.T) { + time.Sleep(1 * time.Hour) +} + +func TestStartTask(t *testing.T) { + TxNumGroupByDayTask{}.Start() + time.Sleep(1 * time.Hour) +} + +func TestUpdateProposalVoters_DoTask(t *testing.T) { + UpdateProposalVoters{}.DoTask() +} + +func TestHeartBeat(t *testing.T) { + + stop := make(chan bool) + go HeartBeat("update_gov_params", stop) + time.Sleep(11 * time.Second) + stop <- true +} + +//func TestHeartBeat1(t *testing.T) { +// +// context0 := context.Background() +// go HeartBeat1("update_gov_params", context0) +// time.Sleep(11 * time.Second) +//} +// +//func TestHeartBeat2(t *testing.T) { +// +// go HeartBeat2("update_gov_params") +// time.Sleep(11 * time.Second) +//} diff --git a/backend/task/static_delegator.go b/backend/task/static_delegator.go index f5fc36dec..16dedbfa9 100644 --- a/backend/task/static_delegator.go +++ b/backend/task/static_delegator.go @@ -30,7 +30,8 @@ func (task StaticDelegatorTask) Start() { } } -func (task StaticDelegatorTask) DoTask() error { +func (task StaticDelegatorTask) DoTask(fn func(string) chan bool) error { + defer HeartQuit(fn(task.Name())) ops, err := task.getAllAccountRewards() if err != nil { logger.Error(err.Error()) diff --git a/backend/task/static_delegator_by_month.go b/backend/task/static_delegator_by_month.go index 8d5686bbe..78c29319e 100644 --- a/backend/task/static_delegator_by_month.go +++ b/backend/task/static_delegator_by_month.go @@ -53,7 +53,8 @@ func (task *StaticDelegatorByMonthTask) SetCaculateAddress(address string) { task.address = address } -func (task *StaticDelegatorByMonthTask) DoTask() error { +func (task *StaticDelegatorByMonthTask) DoTask(fn func(string) chan bool) error { + defer HeartQuit(fn(task.Name())) res, err := task.calculateWork() if err != nil { diff --git a/backend/task/static_delegator_by_month_test.go b/backend/task/static_delegator_by_month_test.go index d870bca7a..32149254c 100644 --- a/backend/task/static_delegator_by_month_test.go +++ b/backend/task/static_delegator_by_month_test.go @@ -22,7 +22,7 @@ func TestStaticDelegatorByMonthTask_Start(t *testing.T) { } func TestStaticDelegatorByMonthTask_DoTask(t *testing.T) { - if err := new(StaticDelegatorByMonthTask).DoTask(); err != nil { + if err := new(StaticDelegatorByMonthTask).DoTask(HeartBeat); err != nil { t.Fatal(err.Error()) } } diff --git a/backend/task/static_validator.go b/backend/task/static_validator.go index 80363a7e0..6af9db40d 100644 --- a/backend/task/static_validator.go +++ b/backend/task/static_validator.go @@ -26,7 +26,8 @@ func (task StaticValidatorTask) Start() { } } -func (task StaticValidatorTask) DoTask() error { +func (task StaticValidatorTask) DoTask(fn func(string) chan bool) error { + defer HeartQuit(fn(task.Name())) ops, err := task.getAllValidatorTokens() if err != nil { logger.Error(err.Error()) diff --git a/backend/task/static_validator_by_month.go b/backend/task/static_validator_by_month.go index 9e5090854..b3d98b451 100644 --- a/backend/task/static_validator_by_month.go +++ b/backend/task/static_validator_by_month.go @@ -65,7 +65,8 @@ func (task *StaticValidatorByMonthTask) SetAddressCoinMapData(rewards, pcommissi task.AddressTerminalCommission = tcommission } -func (task *StaticValidatorByMonthTask) DoTask() error { +func (task *StaticValidatorByMonthTask) DoTask(fn func(string) chan bool) error { + defer HeartQuit(fn(task.Name())) res, err := task.caculateWork() if err != nil { return err diff --git a/backend/task/static_validator_by_month_test.go b/backend/task/static_validator_by_month_test.go index c61b18c5b..2eaf4dc06 100644 --- a/backend/task/static_validator_by_month_test.go +++ b/backend/task/static_validator_by_month_test.go @@ -109,7 +109,7 @@ func TestStaticValidatorByMonthTask_Start(t *testing.T) { } func TestStaticValidatorByMonthTask_DoTask(t *testing.T) { - if err := task.DoTask(); err != nil { + if err := task.DoTask(HeartBeat); err != nil { t.Fatal(err.Error()) } } diff --git a/backend/task/task_control.go b/backend/task/task_control.go index 3f7c806c1..9cc6bb17a 100644 --- a/backend/task/task_control.go +++ b/backend/task/task_control.go @@ -17,7 +17,7 @@ type ( } TaskControlService struct { } - Callback func() error + Callback func(func(string) chan bool) error ) func (s TaskControlService) runTask(taskName string, timeInterval int, callback Callback) error { @@ -39,7 +39,7 @@ func (s TaskControlService) runTask(taskName string, timeInterval int, callback } else { // do task var doTaskErr error - if err := callback(); err != nil { + if err := callback(HeartBeat); err != nil { doTaskErr = fmt.Errorf("doTask fail, taskName:%s, err:%s", taskName, err.Error()) } @@ -113,7 +113,8 @@ func (s TaskControlMonitor) Start() { }) } -func (s TaskControlMonitor) DoTask() error { +func (s TaskControlMonitor) DoTask(fn func(string) chan bool) error { + defer HeartQuit(fn(s.Name())) runValue := true skip := 0 limit := 20 diff --git a/backend/task/task_control_test.go b/backend/task/task_control_test.go index 5d82d6382..85b230acc 100644 --- a/backend/task/task_control_test.go +++ b/backend/task/task_control_test.go @@ -31,5 +31,5 @@ func TestTaskControlService_Start(t *testing.T) { } func TestUpdateAccount_DoTask(t *testing.T) { - new(TaskControlMonitor).DoTask() + new(TaskControlMonitor).DoTask(HeartBeat) } \ No newline at end of file diff --git a/backend/task/task_test.go b/backend/task/task_test.go deleted file mode 100644 index 11d6cdce6..000000000 --- a/backend/task/task_test.go +++ /dev/null @@ -1,19 +0,0 @@ -package task - -import ( - "testing" - "time" -) - -func TestTask(t *testing.T) { - time.Sleep(1 * time.Hour) -} - -func TestStartTask(t *testing.T) { - TxNumGroupByDayTask{}.Start() - time.Sleep(1 * time.Hour) -} - -func TestUpdateProposalVoters_DoTask(t *testing.T) { - UpdateProposalVoters{}.DoTask() -} \ No newline at end of file diff --git a/backend/task/tx_num_group_by_day.go b/backend/task/tx_num_group_by_day.go index 1129b3877..a3888adcb 100644 --- a/backend/task/tx_num_group_by_day.go +++ b/backend/task/tx_num_group_by_day.go @@ -25,7 +25,8 @@ func (task TxNumGroupByDayTask) Start() { } } -func (task TxNumGroupByDayTask) DoTask() error { +func (task TxNumGroupByDayTask) DoTask(fn func(string) chan bool) error { + defer HeartQuit(fn(task.Name())) today := utils.TruncateTime(time.Now().In(cstZone), utils.Day) yesterday := today.Add(-24 * time.Hour) diff --git a/backend/task/types.go b/backend/task/types.go index 9e0c02479..a844f09c7 100644 --- a/backend/task/types.go +++ b/backend/task/types.go @@ -50,7 +50,7 @@ func init() { type TimerTask interface { Start() Name() string - DoTask() error + DoTask(fn func(string) chan bool) error } type Engine struct { diff --git a/backend/task/update_account.go b/backend/task/update_account.go index c0617a1ac..3ee189b89 100644 --- a/backend/task/update_account.go +++ b/backend/task/update_account.go @@ -29,7 +29,8 @@ func (task UpdateAccount) Start() { } -func (task UpdateAccount) DoTask() error { +func (task UpdateAccount) DoTask(fn func(string) chan bool) error { + defer HeartQuit(fn(task.Name())) accounts, err := task.account.GetAllAccount() if err != nil { logger.Error(err.Error()) diff --git a/backend/task/update_proposal_voters.go b/backend/task/update_proposal_voters.go index ea72cac3f..c80f96c7a 100644 --- a/backend/task/update_proposal_voters.go +++ b/backend/task/update_proposal_voters.go @@ -25,7 +25,8 @@ func (task UpdateProposalVoters) Start() { }) } -func (task UpdateProposalVoters) DoTask() error { +func (task UpdateProposalVoters) DoTask(fn func(string) chan bool) error { + defer HeartQuit(fn(task.Name())) status := []string{document.ProposalStatusVoting} sorts := []string{document.Proposal_Field_VotingEndTime} proposals, err := document.Proposal{}.GetProposalsByStatus(status, sorts, false) diff --git a/backend/task/update_validator.go b/backend/task/update_validator.go index 8a027bb8f..79f30cf66 100644 --- a/backend/task/update_validator.go +++ b/backend/task/update_validator.go @@ -25,7 +25,8 @@ func (task UpdateValidator) Start() { } -func (task UpdateValidator) DoTask() error { +func (task UpdateValidator) DoTask(fn func(string) chan bool) error { + defer HeartQuit(fn(task.Name())) validators, err := document.Validator{}.GetAllValidator() if err != nil { diff --git a/backend/task/update_validator_static_info.go b/backend/task/update_validator_static_info.go index 9fc26c370..59a5aa539 100644 --- a/backend/task/update_validator_static_info.go +++ b/backend/task/update_validator_static_info.go @@ -25,7 +25,8 @@ func (task ValidatorStaticInfo) Start() { }) } -func (task ValidatorStaticInfo) DoTask() error { +func (task ValidatorStaticInfo) DoTask(fn func(string) chan bool) error { + defer HeartQuit(fn(task.Name())) validatorService := service.ValidatorService{} return validatorService.UpdateValidatorStaticInfo() } diff --git a/backend/task/update_validatoricons.go b/backend/task/update_validatoricons.go index 576f32b68..6f1f1d2b0 100644 --- a/backend/task/update_validatoricons.go +++ b/backend/task/update_validatoricons.go @@ -23,7 +23,8 @@ func (task UpdateValidatorIcons) Start() { //}) } -func (task UpdateValidatorIcons) DoTask() error { +func (task UpdateValidatorIcons) DoTask(fn func(string) chan bool) error { + defer HeartQuit(fn(task.Name())) err := new(service.ValidatorService).UpdateValidatorIcons() if err != nil { return err From 5d09dc170a7e835b38c4f08c19f2b73d1687d9b9 Mon Sep 17 00:00:00 2001 From: huangweichang Date: Fri, 3 Jul 2020 16:21:22 +0800 Subject: [PATCH 04/27] update test case --- backend/service/service_validator.go | 16 +++--- backend/task/heartbeat.go | 76 --------------------------- backend/task/heartbeat_test.go | 17 ++---- backend/task/static_delegator_test.go | 2 +- backend/task/static_validator_test.go | 2 +- 5 files changed, 11 insertions(+), 102 deletions(-) diff --git a/backend/service/service_validator.go b/backend/service/service_validator.go index 0568d58a4..1d16d1c3f 100644 --- a/backend/service/service_validator.go +++ b/backend/service/service_validator.go @@ -36,7 +36,7 @@ func (service *ValidatorService) GetValidators(typ, origin string, page, size in panic(types.CodeNotFound) } - var totalVotingPower = getTotalVotingPower(validatorList) + var totalVotingPower = getTotalVotingPower() for i, v := range validatorList { if desc, ok := blackList[v.OperatorAddress]; ok { validatorList[i].Description.Moniker = desc.Moniker @@ -1114,16 +1114,12 @@ func isEqual(srcValidator, dstValidator document.Validator) bool { // return power.QuoInt(tokenPrecision).RoundInt64() //} -func getTotalVotingPower(validators []document.Validator) int64 { +func getTotalVotingPower() int64 { var total = int64(0) - //var set = lcd.LatestValidatorSet() - //for _, v := range set.Validators { - // votingPower := utils.ParseIntWithDefault(v.VotingPower, 0) - // total += votingPower - //} - - for _, val := range validators { - total += val.VotingPower + var set = lcd.LatestValidatorSet() + for _, v := range set.Validators { + votingPower := utils.ParseIntWithDefault(v.VotingPower, 0) + total += votingPower } return total } diff --git a/backend/task/heartbeat.go b/backend/task/heartbeat.go index 848e42325..6768a4cf2 100644 --- a/backend/task/heartbeat.go +++ b/backend/task/heartbeat.go @@ -74,79 +74,3 @@ func heartBeat(taskName string, stop chan bool) { } } -// -//func HeartBeat1(taskName string, ctx context.Context) { -// var taskcontrol document.TaskControl -// timeInterval := int64(conf.Get().Server.CronTimeHeartBeat) -// ticker := time.NewTicker(time.Duration(timeInterval) * time.Second) -// defer ticker.Stop() -// for { -// select { -// case <-ticker.C: -// doc, err := taskcontrol.QueryOneByTaskName(taskName) -// if err != nil { -// logger.Error("QueryOneByTaskName have error", -// logger.String("taskName", taskName), -// logger.String("tableName", taskcontrol.Name()), -// logger.String("err", err.Error())) -// return -// } -// curtime := time.Now().Unix() -// if curtime > doc.LatestExecTime+timeInterval { -// doc.LatestExecTime = time.Now().Unix() -// if err := taskcontrol.UpdateByPK(doc); err != nil { -// logger.Error("update segment have error", -// logger.String("taskName", taskName), -// logger.String("tableName", doc.Name()), -// logger.String("err", err.Error())) -// return -// } -// } -// logger.Info("HeartBeat is ok", logger.String("taskName", taskName)) -// case <-ctx.Done(): -// logger.Info("HeartBeat Quit...", logger.String("taskName", taskName)) -// return -// -// } -// -// } -//} -// -//func HeartBeat2(taskName string) { -// var taskcontrol document.TaskControl -// timeInterval := int64(conf.Get().Server.CronTimeHeartBeat) -// ticker := time.NewTicker(time.Duration(timeInterval) * time.Second) -// stop := make(chan os.Signal) -// signal.Notify(stop, os.Interrupt) -// defer ticker.Stop() -// for { -// select { -// case <-ticker.C: -// doc, err := taskcontrol.QueryOneByTaskName(taskName) -// if err != nil { -// logger.Error("QueryOneByTaskName have error", -// logger.String("taskName", taskName), -// logger.String("tableName", taskcontrol.Name()), -// logger.String("err", err.Error())) -// return -// } -// curtime := time.Now().Unix() -// if curtime > doc.LatestExecTime+timeInterval { -// doc.LatestExecTime = time.Now().Unix() -// if err := taskcontrol.UpdateByPK(doc); err != nil { -// logger.Error("update segment have error", -// logger.String("taskName", taskName), -// logger.String("tableName", doc.Name()), -// logger.String("err", err.Error())) -// return -// } -// } -// logger.Info("HeartBeat is ok", logger.String("taskName", taskName)) -// case <-stop: -// logger.Info("HeartBeat Quit...", logger.String("taskName", taskName)) -// return -// -// } -// -// } -//} diff --git a/backend/task/heartbeat_test.go b/backend/task/heartbeat_test.go index 3537f2d30..cedab3337 100644 --- a/backend/task/heartbeat_test.go +++ b/backend/task/heartbeat_test.go @@ -15,26 +15,15 @@ func TestStartTask(t *testing.T) { } func TestUpdateProposalVoters_DoTask(t *testing.T) { - UpdateProposalVoters{}.DoTask() + UpdateProposalVoters{}.DoTask(HeartBeat) } func TestHeartBeat(t *testing.T) { stop := make(chan bool) - go HeartBeat("update_gov_params", stop) + go HeartBeat("update_gov_params") time.Sleep(11 * time.Second) stop <- true } -//func TestHeartBeat1(t *testing.T) { -// -// context0 := context.Background() -// go HeartBeat1("update_gov_params", context0) -// time.Sleep(11 * time.Second) -//} -// -//func TestHeartBeat2(t *testing.T) { -// -// go HeartBeat2("update_gov_params") -// time.Sleep(11 * time.Second) -//} + diff --git a/backend/task/static_delegator_test.go b/backend/task/static_delegator_test.go index 6e370c749..a8e0b29d1 100644 --- a/backend/task/static_delegator_test.go +++ b/backend/task/static_delegator_test.go @@ -72,7 +72,7 @@ func TestStaticRewardsByDayTask_getAccountFromDb(t *testing.T) { } func TestStaticRewardsByDayTask_DoTask(t *testing.T) { - new(StaticDelegatorTask).DoTask() + new(StaticDelegatorTask).DoTask(HeartBeat) } func TestStaticRewardsTask_Common(t *testing.T) { diff --git a/backend/task/static_validator_test.go b/backend/task/static_validator_test.go index 1fc18cbcc..1b5755db4 100644 --- a/backend/task/static_validator_test.go +++ b/backend/task/static_validator_test.go @@ -40,5 +40,5 @@ func TestStaticValidatorTask_getAllValidatorTokens(t *testing.T) { } func TestStaticValidatorTask_DoTask(t *testing.T) { - new(StaticValidatorTask).DoTask() + new(StaticValidatorTask).DoTask(HeartBeat) } From 39b1c525da24598f63b30f41bcd38dd33edc9bca Mon Sep 17 00:00:00 2001 From: huangweichang Date: Tue, 7 Jul 2020 15:05:40 +0800 Subject: [PATCH 05/27] adjust judge time by heartbeat timeinterval and update code --- backend/task/asset_gateway.go | 3 ++- backend/task/asset_tokens.go | 3 ++- backend/task/gov_params.go | 3 ++- backend/task/heartbeat.go | 2 +- backend/task/static_delegator.go | 3 ++- backend/task/static_delegator_by_month.go | 3 ++- backend/task/static_validator.go | 3 ++- backend/task/static_validator_by_month.go | 3 ++- backend/task/task_control.go | 6 ++++-- backend/task/tx_num_group_by_day.go | 3 ++- backend/task/update_account.go | 3 ++- backend/task/update_proposal_voters.go | 3 ++- backend/task/update_validator.go | 3 ++- backend/task/update_validator_static_info.go | 3 ++- backend/task/update_validatoricons.go | 3 ++- 15 files changed, 31 insertions(+), 16 deletions(-) diff --git a/backend/task/asset_gateway.go b/backend/task/asset_gateway.go index 04c6f6cf9..907b62939 100644 --- a/backend/task/asset_gateway.go +++ b/backend/task/asset_gateway.go @@ -26,7 +26,8 @@ func (task UpdateAssetGateways) Start() { } func (task UpdateAssetGateways) DoTask(fn func(string) chan bool) error { - defer HeartQuit(fn(task.Name())) + stop := fn(task.Name()) + defer HeartQuit(stop) assetGateways, err := document.AssetGateways{}.GetAllAssetGateways() if err != nil { return err diff --git a/backend/task/asset_tokens.go b/backend/task/asset_tokens.go index e7e0f4a29..a316e6952 100644 --- a/backend/task/asset_tokens.go +++ b/backend/task/asset_tokens.go @@ -26,7 +26,8 @@ func (task UpdateAssetTokens) Start() { } func (task UpdateAssetTokens) DoTask(fn func(string) chan bool) error { - defer HeartQuit(fn(task.Name())) + stop := fn(task.Name()) + defer HeartQuit(stop) assetTokens, err := document.AssetToken{}.GetAllAssets() if err != nil { return err diff --git a/backend/task/gov_params.go b/backend/task/gov_params.go index 60f3e8c8e..7bc0460de 100644 --- a/backend/task/gov_params.go +++ b/backend/task/gov_params.go @@ -25,7 +25,8 @@ func (task UpdateGovParams) Start() { } func (task UpdateGovParams) DoTask(fn func(string) chan bool) error { - defer HeartQuit(fn(task.Name())) + stop := fn(task.Name()) + defer HeartQuit(stop) curModuleKv, err := lcd.GetAllGovModuleParam() if err != nil { return err diff --git a/backend/task/heartbeat.go b/backend/task/heartbeat.go index 6768a4cf2..79269c41b 100644 --- a/backend/task/heartbeat.go +++ b/backend/task/heartbeat.go @@ -59,7 +59,7 @@ func heartBeat(taskName string, stop chan bool) { select { case <-ticker.C: doTask() - logger.Info("HeartBeat is ok", logger.String("taskName", taskName)) + //logger.Info("HeartBeat is ok", logger.String("taskName", taskName)) case <-stop: close(stop) logger.Info("HeartBeat Quit...", logger.String("taskName", taskName)) diff --git a/backend/task/static_delegator.go b/backend/task/static_delegator.go index 16dedbfa9..e4db5c77f 100644 --- a/backend/task/static_delegator.go +++ b/backend/task/static_delegator.go @@ -31,7 +31,8 @@ func (task StaticDelegatorTask) Start() { } func (task StaticDelegatorTask) DoTask(fn func(string) chan bool) error { - defer HeartQuit(fn(task.Name())) + stop := fn(task.Name()) + defer HeartQuit(stop) ops, err := task.getAllAccountRewards() if err != nil { logger.Error(err.Error()) diff --git a/backend/task/static_delegator_by_month.go b/backend/task/static_delegator_by_month.go index 78c29319e..6e2821bc6 100644 --- a/backend/task/static_delegator_by_month.go +++ b/backend/task/static_delegator_by_month.go @@ -54,7 +54,8 @@ func (task *StaticDelegatorByMonthTask) SetCaculateAddress(address string) { } func (task *StaticDelegatorByMonthTask) DoTask(fn func(string) chan bool) error { - defer HeartQuit(fn(task.Name())) + stop := fn(task.Name()) + defer HeartQuit(stop) res, err := task.calculateWork() if err != nil { diff --git a/backend/task/static_validator.go b/backend/task/static_validator.go index 6af9db40d..e1f0ad1a0 100644 --- a/backend/task/static_validator.go +++ b/backend/task/static_validator.go @@ -27,7 +27,8 @@ func (task StaticValidatorTask) Start() { } func (task StaticValidatorTask) DoTask(fn func(string) chan bool) error { - defer HeartQuit(fn(task.Name())) + stop := fn(task.Name()) + defer HeartQuit(stop) ops, err := task.getAllValidatorTokens() if err != nil { logger.Error(err.Error()) diff --git a/backend/task/static_validator_by_month.go b/backend/task/static_validator_by_month.go index b3d98b451..498b198f7 100644 --- a/backend/task/static_validator_by_month.go +++ b/backend/task/static_validator_by_month.go @@ -66,7 +66,8 @@ func (task *StaticValidatorByMonthTask) SetAddressCoinMapData(rewards, pcommissi } func (task *StaticValidatorByMonthTask) DoTask(fn func(string) chan bool) error { - defer HeartQuit(fn(task.Name())) + stop := fn(task.Name()) + defer HeartQuit(stop) res, err := task.caculateWork() if err != nil { return err diff --git a/backend/task/task_control.go b/backend/task/task_control.go index 9cc6bb17a..91189e1e9 100644 --- a/backend/task/task_control.go +++ b/backend/task/task_control.go @@ -82,7 +82,8 @@ func (s TaskControlService) assetTaskShouldNotBeExecuted(taskName string, timeIn } now := time.Now().Unix() - if now-v.LatestExecTime < int64(timeInterval) && !isCronjob { + timeHeartBeat := conf.Get().Server.CronTimeHeartBeat + if now-v.LatestExecTime < int64(3*timeHeartBeat) && !isCronjob { return true, nil } } @@ -114,7 +115,8 @@ func (s TaskControlMonitor) Start() { } func (s TaskControlMonitor) DoTask(fn func(string) chan bool) error { - defer HeartQuit(fn(s.Name())) + stop := fn(s.Name()) + defer HeartQuit(stop) runValue := true skip := 0 limit := 20 diff --git a/backend/task/tx_num_group_by_day.go b/backend/task/tx_num_group_by_day.go index a3888adcb..829d687be 100644 --- a/backend/task/tx_num_group_by_day.go +++ b/backend/task/tx_num_group_by_day.go @@ -26,7 +26,8 @@ func (task TxNumGroupByDayTask) Start() { } func (task TxNumGroupByDayTask) DoTask(fn func(string) chan bool) error { - defer HeartQuit(fn(task.Name())) + stop := fn(task.Name()) + defer HeartQuit(stop) today := utils.TruncateTime(time.Now().In(cstZone), utils.Day) yesterday := today.Add(-24 * time.Hour) diff --git a/backend/task/update_account.go b/backend/task/update_account.go index 3ee189b89..9a847e706 100644 --- a/backend/task/update_account.go +++ b/backend/task/update_account.go @@ -30,7 +30,8 @@ func (task UpdateAccount) Start() { } func (task UpdateAccount) DoTask(fn func(string) chan bool) error { - defer HeartQuit(fn(task.Name())) + stop := fn(task.Name()) + defer HeartQuit(stop) accounts, err := task.account.GetAllAccount() if err != nil { logger.Error(err.Error()) diff --git a/backend/task/update_proposal_voters.go b/backend/task/update_proposal_voters.go index c80f96c7a..d0eccd272 100644 --- a/backend/task/update_proposal_voters.go +++ b/backend/task/update_proposal_voters.go @@ -26,7 +26,8 @@ func (task UpdateProposalVoters) Start() { } func (task UpdateProposalVoters) DoTask(fn func(string) chan bool) error { - defer HeartQuit(fn(task.Name())) + stop := fn(task.Name()) + defer HeartQuit(stop) status := []string{document.ProposalStatusVoting} sorts := []string{document.Proposal_Field_VotingEndTime} proposals, err := document.Proposal{}.GetProposalsByStatus(status, sorts, false) diff --git a/backend/task/update_validator.go b/backend/task/update_validator.go index 79f30cf66..7ba9e6e71 100644 --- a/backend/task/update_validator.go +++ b/backend/task/update_validator.go @@ -26,7 +26,8 @@ func (task UpdateValidator) Start() { } func (task UpdateValidator) DoTask(fn func(string) chan bool) error { - defer HeartQuit(fn(task.Name())) + stop := fn(task.Name()) + defer HeartQuit(stop) validators, err := document.Validator{}.GetAllValidator() if err != nil { diff --git a/backend/task/update_validator_static_info.go b/backend/task/update_validator_static_info.go index 59a5aa539..42d127661 100644 --- a/backend/task/update_validator_static_info.go +++ b/backend/task/update_validator_static_info.go @@ -26,7 +26,8 @@ func (task ValidatorStaticInfo) Start() { } func (task ValidatorStaticInfo) DoTask(fn func(string) chan bool) error { - defer HeartQuit(fn(task.Name())) + stop := fn(task.Name()) + defer HeartQuit(stop) validatorService := service.ValidatorService{} return validatorService.UpdateValidatorStaticInfo() } diff --git a/backend/task/update_validatoricons.go b/backend/task/update_validatoricons.go index 6f1f1d2b0..57e92ee56 100644 --- a/backend/task/update_validatoricons.go +++ b/backend/task/update_validatoricons.go @@ -24,7 +24,8 @@ func (task UpdateValidatorIcons) Start() { } func (task UpdateValidatorIcons) DoTask(fn func(string) chan bool) error { - defer HeartQuit(fn(task.Name())) + stop := fn(task.Name()) + defer HeartQuit(stop) err := new(service.ValidatorService).UpdateValidatorIcons() if err != nil { return err From f5bb8d848b2668cfa48e8bb3ce95c64d50741f73 Mon Sep 17 00:00:00 2001 From: huangweichang Date: Tue, 7 Jul 2020 15:21:39 +0800 Subject: [PATCH 06/27] adjust code --- backend/task/task_control.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/backend/task/task_control.go b/backend/task/task_control.go index 91189e1e9..4dbe4676b 100644 --- a/backend/task/task_control.go +++ b/backend/task/task_control.go @@ -82,8 +82,7 @@ func (s TaskControlService) assetTaskShouldNotBeExecuted(taskName string, timeIn } now := time.Now().Unix() - timeHeartBeat := conf.Get().Server.CronTimeHeartBeat - if now-v.LatestExecTime < int64(3*timeHeartBeat) && !isCronjob { + if now-v.LatestExecTime < int64(timeInterval) && !isCronjob { return true, nil } } @@ -150,11 +149,12 @@ func (s TaskControlMonitor) checkAndChangeWorkOn(list []document.TaskControl) { func (s TaskControlMonitor) CheckAndUpdate(one document.TaskControl) error { currentTimeInterval := time.Now().Unix() - one.LatestExecTime - if one.TimeInterval > currentTimeInterval { + timeHeartBeat := int64(conf.Get().Server.CronTimeHeartBeat) + + if timeHeartBeat > currentTimeInterval { return nil } - - if currentTimeInterval >= 2*one.TimeInterval && one.IsInProcess && one.TaskName != types.TaskConTrolMonitor { + if currentTimeInterval >= 3*timeHeartBeat && one.IsInProcess && one.TaskName != types.TaskConTrolMonitor { one.IsInProcess = false if err := s.controlModel.UpdateByPK(one); err != nil { return err From 081fc12bf440d4f745fdfced31de7c44dc195fb9 Mon Sep 17 00:00:00 2001 From: huangweichang Date: Tue, 7 Jul 2020 16:26:30 +0800 Subject: [PATCH 07/27] remove heartbeat exectime judge --- backend/task/heartbeat.go | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/backend/task/heartbeat.go b/backend/task/heartbeat.go index 79269c41b..f43de3dd5 100644 --- a/backend/task/heartbeat.go +++ b/backend/task/heartbeat.go @@ -42,16 +42,13 @@ func heartBeat(taskName string, stop chan bool) { logger.String("err", err.Error())) return } - curtime := time.Now().Unix() - if curtime > doc.LatestExecTime+timeInterval { - doc.LatestExecTime = time.Now().Unix() - if err := taskcontrol.UpdateByPK(doc); err != nil { - logger.Error("update segment have error", - logger.String("taskName", taskName), - logger.String("tableName", doc.Name()), - logger.String("err", err.Error())) - return - } + doc.LatestExecTime = time.Now().Unix() + if err := taskcontrol.UpdateByPK(doc); err != nil { + logger.Error("update segment have error", + logger.String("taskName", taskName), + logger.String("tableName", doc.Name()), + logger.String("err", err.Error())) + return } } doTask() @@ -73,4 +70,3 @@ func heartBeat(taskName string, stop chan bool) { } } - From 00a1cb372cd9551963782833c48a577901d16e40 Mon Sep 17 00:00:00 2001 From: huangweichang Date: Tue, 7 Jul 2020 17:15:13 +0800 Subject: [PATCH 08/27] improve code about heartbeat update --- backend/orm/document/task_control.go | 16 ++++++++++++++++ backend/task/heartbeat.go | 12 ++---------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/backend/orm/document/task_control.go b/backend/orm/document/task_control.go index 8b35236a2..2129adc99 100644 --- a/backend/orm/document/task_control.go +++ b/backend/orm/document/task_control.go @@ -82,6 +82,22 @@ func (d TaskControl) QueryOneByTaskName(taskName string) (TaskControl, error) { return d.findOne(selector, condition) } +func (d TaskControl) UpdateByTaskName(taskName string) (error) { + selector := bson.M{ + TCFieldTaskName: taskName, + } + condition := bson.M{ + TCFieldLatestExecTime: time.Now().Unix(), + } + update := bson.M{ + "$set": condition, + } + q := orm.NewQuery() + defer q.Release() + c := q.GetDb().C(d.Name()) + return c.Update(selector, update) +} + func (d TaskControl) Save(record TaskControl) error { q := orm.NewQuery() defer q.Release() diff --git a/backend/task/heartbeat.go b/backend/task/heartbeat.go index f43de3dd5..55e9fd600 100644 --- a/backend/task/heartbeat.go +++ b/backend/task/heartbeat.go @@ -34,22 +34,14 @@ func heartBeat(taskName string, stop chan bool) { defer ticker.Stop() doTask := func() { - doc, err := taskcontrol.QueryOneByTaskName(taskName) + err := taskcontrol.UpdateByTaskName(taskName) if err != nil { - logger.Error("QueryOneByTaskName have error", + logger.Error("UpdateByTaskName have error", logger.String("taskName", taskName), logger.String("tableName", taskcontrol.Name()), logger.String("err", err.Error())) return } - doc.LatestExecTime = time.Now().Unix() - if err := taskcontrol.UpdateByPK(doc); err != nil { - logger.Error("update segment have error", - logger.String("taskName", taskName), - logger.String("tableName", doc.Name()), - logger.String("err", err.Error())) - return - } } doTask() for { From 5b3122a6b49ece9705fc1cce4f15e975ee8e7f52 Mon Sep 17 00:00:00 2001 From: huangweichang Date: Wed, 8 Jul 2020 16:26:41 +0800 Subject: [PATCH 09/27] add delegation unbondingdelegation and blance update in task update_account --- backend/lcd/keys.go | 25 +++- backend/lcd/stake.go | 10 +- backend/task/task_control_test.go | 2 +- backend/task/update_account.go | 178 +++++++++++++++++++++++++--- backend/task/update_account_test.go | 59 ++++++++- 5 files changed, 248 insertions(+), 26 deletions(-) diff --git a/backend/lcd/keys.go b/backend/lcd/keys.go index 3bc6cf181..b51e2036f 100644 --- a/backend/lcd/keys.go +++ b/backend/lcd/keys.go @@ -11,6 +11,7 @@ import ( "github.com/irisnet/explorer/backend/types" "github.com/irisnet/explorer/backend/utils" "github.com/irisnet/explorer/backend/vo" + "strings" ) type ( @@ -56,13 +57,8 @@ func buildAccountVo(acc Account01411) AccountVo { } func Account(address string) (result AccountVo, err error) { - url := fmt.Sprintf(UrlAccount, conf.Get().Hub.LcdUrl, address) - resBytes, err := utils.Get(url) + acc, err := AccountInfo(address) if err != nil { - return result, err - } - acc := Account01411{} - if err := json.Unmarshal(resBytes, &acc); err != nil { logger.Error("get account error", logger.String("err", err.Error())) return result, err } @@ -70,6 +66,23 @@ func Account(address string) (result AccountVo, err error) { return result, nil } +func AccountInfo(address string) (Account01411, error) { + acc := Account01411{} + if !strings.HasPrefix(address, conf.Get().Hub.Prefix.AccAddr) { + return acc, fmt.Errorf("address prefix is should %v", conf.Get().Hub.Prefix.AccAddr) + } + url := fmt.Sprintf(UrlAccount, conf.Get().Hub.LcdUrl, address) + resBytes, err := utils.Get(url) + if err != nil { + return acc, err + } + + if err := json.Unmarshal(resBytes, &acc); err != nil { + logger.Error("get account error", logger.String("err", err.Error())) + return acc, err + } + return acc, nil +} func Faucet(req *http.Request) (bz []byte, err error) { uri := fmt.Sprintf(types.UrlFaucetAccountService, conf.Get().Server.FaucetUrl) return utils.Forward(req, uri) diff --git a/backend/lcd/stake.go b/backend/lcd/stake.go index 81a02d630..b734d9220 100644 --- a/backend/lcd/stake.go +++ b/backend/lcd/stake.go @@ -51,6 +51,9 @@ func QueryWithdrawAddr(address string) (result string) { } func GetDelegationsByDelAddr(delAddr string) (delegations []DelegationVo) { + if !strings.HasPrefix(delAddr, conf.Get().Hub.Prefix.AccAddr) { + return + } url := fmt.Sprintf(UrlDelegationsByDelegator, conf.Get().Hub.LcdUrl, delAddr) resAsBytes, err := utils.Get(url) if err != nil { @@ -112,6 +115,9 @@ func GetWithdrawAddressByValidatorAcc(validatorAcc string) (string, error) { func GetDistributionRewardsByValidatorAcc(validatorAcc string) (utils.CoinsAsStr, []RewardsFromDelegations, utils.CoinsAsStr, error) { + if !strings.HasPrefix(validatorAcc, conf.Get().Hub.Prefix.AccAddr) { + return nil, nil, nil, fmt.Errorf("address prefix is not %v", conf.Get().Hub.Prefix.AccAddr) + } url := fmt.Sprintf(UrlDistributionRewardsByValidatorAcc, conf.Get().Hub.LcdUrl, validatorAcc) resAsBytes, err := utils.Get(url) if err != nil { @@ -179,7 +185,9 @@ func GetUnbondingDelegationsByValidatorAddr(valAddr string) (unbondingDelegation } func GetUnbondingDelegationsByDelegatorAddr(delAddr string) (unbondingDelegations []UnbondingDelegations) { - + if !strings.HasPrefix(delAddr, conf.Get().Hub.Prefix.AccAddr) { + return + } url := fmt.Sprintf(UrlUnbondingDelegationByDelegator, conf.Get().Hub.LcdUrl, delAddr) resAsBytes, err := utils.Get(url) if err != nil { diff --git a/backend/task/task_control_test.go b/backend/task/task_control_test.go index 85b230acc..f4df1df27 100644 --- a/backend/task/task_control_test.go +++ b/backend/task/task_control_test.go @@ -30,6 +30,6 @@ func TestTaskControlService_Start(t *testing.T) { new(TaskControlMonitor).Start() } -func TestUpdateAccount_DoTask(t *testing.T) { +func TestTaskControlService_DoTask(t *testing.T) { new(TaskControlMonitor).DoTask(HeartBeat) } \ No newline at end of file diff --git a/backend/task/update_account.go b/backend/task/update_account.go index 971584a68..229aa15d1 100644 --- a/backend/task/update_account.go +++ b/backend/task/update_account.go @@ -2,13 +2,15 @@ package task import ( "github.com/irisnet/explorer/backend/conf" - "github.com/irisnet/explorer/backend/lcd" + "github.com/irisnet/explorer/backend/utils" "github.com/irisnet/explorer/backend/logger" "github.com/irisnet/explorer/backend/orm/document" + "github.com/irisnet/explorer/backend/lcd" "github.com/irisnet/explorer/backend/types" - "github.com/irisnet/explorer/backend/utils" "strconv" - "strings" + "math/big" + "time" + "math" ) type UpdateAccount struct { @@ -39,27 +41,80 @@ func (task UpdateAccount) DoTask(fn func(string) chan bool) error { return err } - for i, val := range accounts { - if !strings.HasPrefix(val.Address, conf.Get().Hub.Prefix.AccAddr) { - continue + for i := range accounts { + if err := updateAccount(&accounts[i]); err != nil { + logger.Warn("get AccountInfo failed", logger.String("err", err.Error())) } - _, _, rewards, err := lcd.GetDistributionRewardsByValidatorAcc(val.Address) - if err == nil && len(rewards) > 0 { - newrewards := loadRewards(rewards) - subvalue := newrewards.Amount - accounts[i].Rewards.Amount + } - accounts[i].Total.Amount += subvalue + return nil +} - accounts[i].Rewards = newrewards - } - if err := task.account.Update(accounts[i]); err != nil { - logger.Warn("Account Update Rewards failed", logger.String("err", err.Error())) - } +func updateAccount(account *document.Account) error { + if res, err := updateAccountInfo(account); err == nil { + account = res + } else { + logger.Warn("get AccountInfo failed", logger.String("err", err.Error())) + } + _, _, rewards, err := lcd.GetDistributionRewardsByValidatorAcc(account.Address) + if err == nil && len(rewards) > 0 { + newrewards := loadRewards(rewards) + subvalue := newrewards.Amount - account.Rewards.Amount + account.Total.Amount += subvalue + account.Rewards = newrewards + } + if err := account.Update(*account); err != nil { + logger.Warn("Account Update Rewards failed", logger.String("err", err.Error())) + return err } - return nil } +func updateAccountInfo(account *document.Account) (*document.Account, error) { + balance, res, err := getBalance(account) + if err != nil { + logger.Error("lcd getBalance Info have error", logger.String("err", err.Error())) + return account, err + } else { + account = res + } + + delegate, err := getDelegationInfo(account.Address) + if err != nil { + logger.Warn("update Delegation Info have error", logger.String("err", err.Error())) + } + unbondingdelegator, err := getUnbondingDelegationInfo(account.Address) + if err != nil { + logger.Warn("update UnbondingDelegation Info have error", logger.String("err", err.Error())) + } + starttime := time.Unix(account.TotalUpdateAt, 0).In(cstZone) + if res, err := updateHeightTimeStamp(starttime, time.Now().In(cstZone), account); err == nil { + account = res + } else { + logger.Warn("update HeightTimeStamp have error", logger.String("err", err.Error())) + } + + account.Delegation = utils.Coin{ + Denom: balance.Denom, + Amount: delegate, + } + account.UnbondingDelegation = utils.Coin{ + Denom: balance.Denom, + Amount: unbondingdelegator, + } + + account.CoinIris = utils.Coin{ + Denom: balance.Denom, + Amount: balance.Amount, + } + + account.Total = utils.Coin{ + Denom: balance.Denom, + Amount: balance.Amount + delegate + unbondingdelegator, + } + return account, nil +} + func loadRewards(coins utils.CoinsAsStr) utils.Coin { var retcoin utils.Coin for _, val := range coins { @@ -70,3 +125,92 @@ func loadRewards(coins utils.CoinsAsStr) utils.Coin { } return retcoin } + +func getBalance(account *document.Account) (utils.Coin, *document.Account, error) { + var balance utils.Coin + ret, err := lcd.AccountInfo(account.Address) + if err != nil { + logger.Warn("lcd AccountInfo Info have error", logger.String("err", err.Error())) + return balance, account, err + } + + for _, val := range ret.Value.Coins { + if val.Denom == types.IRISAttoUint { + balance.Denom = val.Denom + amount, _ := strconv.ParseFloat(val.Amount, 64) + if amount > 0 { + balance.Amount = amount + } + break + } + } + if number, _ := strconv.ParseUint(ret.Value.AccountNum, 10, 64); number > 0 { + account.AccountNumber = number + } + return balance, account, nil +} + +func updateHeightTimeStamp(starttime, endtime time.Time, account *document.Account) (*document.Account, error) { + var txModel document.CommonTx + txs, err := txModel.GetTxsByDurationAddress(starttime, endtime, "") + if err != nil { + return nil, err + } + var height, timestamp int64 + for _, val := range txs { + + switch val.Type { + case types.TxTypeStakeDelegate, types.TxTypeStakeBeginUnbonding, types.TxTypeBeginRedelegate, + types.TxTypeStakeCreateValidator: + if val.Height > height && val.From == account.Address { + height = val.Height + timestamp = val.Time.Unix() + } + } + + } + if height > account.TotalUpdateHeight && timestamp > account.TotalUpdateAt { + account.CoinIrisUpdateHeight = height + account.CoinIrisUpdateAt = timestamp + account.TotalUpdateHeight = height + account.TotalUpdateAt = timestamp + } + return account, nil +} + +func getDelegationInfo(address string) (float64, error) { + delegations := lcd.GetDelegationsByDelAddr(address) + token, _ := new(big.Rat).SetString("0") + if len(delegations) > 0 { + for _, v := range delegations { + if validator, err := lcd.Validator(v.ValidatorAddr); err != nil { + logger.Error("get validator fail", logger.String("validatorAddr", v.ValidatorAddr), + logger.String("err", err.Error())) + continue + } else { + value := utils.CovertShareTokens(validator.Tokens, validator.DelegatorShares, v.Shares) + tmp, ok := new(big.Rat).SetString(value) + if ok { + token = new(big.Rat).Add(tmp, token) + } + + } + } + } + token = new(big.Rat).Mul(token, new(big.Rat).SetFloat64(math.Pow10(18))) + return strconv.ParseFloat(token.FloatString(18), 64) +} + +func getUnbondingDelegationInfo(address string) (float64, error) { + + unbondingDelegations := lcd.GetUnbondingDelegationsByDelegatorAddr(address) + token, _ := new(big.Rat).SetString("0") + if len(unbondingDelegations) > 0 { + for _, v := range unbondingDelegations { + coin := utils.ParseCoin(v.InitialBalance) + token = new(big.Rat).Add(token, new(big.Rat).SetFloat64(coin.Amount)) + } + } + + return strconv.ParseFloat(token.FloatString(18), 64) +} diff --git a/backend/task/update_account_test.go b/backend/task/update_account_test.go index dd07023a3..d0a2d24b3 100644 --- a/backend/task/update_account_test.go +++ b/backend/task/update_account_test.go @@ -1,7 +1,64 @@ package task -import "testing" +import ( + "testing" + "github.com/irisnet/explorer/backend/orm/document" + "time" + "encoding/json" +) func TestUpdateAccount_Start(t *testing.T) { new(UpdateAccount).Start() } + +var account document.Account + +func TestMain(m *testing.M) { + accs, err := new(document.Account).GetAllAccount() + if err != nil { + panic(err) + } + + account = accs[0] + m.Run() +} +func TestUpdateAccount_DoTask(t *testing.T) { + new(UpdateAccount).DoTask(HeartBeat) + +} + +func TestUpdateAccount_updateAccountInfo(t *testing.T) { + account, _ := updateAccountInfo(&account) + bytedata, _ := json.Marshal(account) + t.Log(string(bytedata)) +} + +func TestUpdateAccount_updateAccount(t *testing.T) { + updateAccount(&account) + bytedata, _ := json.Marshal(account) + t.Log(string(bytedata)) +} + +func TestUpdateAccount_getBalance(t *testing.T) { + v, acc, _ := getBalance(&account) + bytedata, _ := json.Marshal(acc) + t.Log(string(bytedata)) + t.Log(v) +} + +func TestUpdateAccount_updateHeightTimeStamp(t *testing.T) { + acc, _ := updateHeightTimeStamp(time.Unix(account.TotalUpdateAt, 0), time.Now(), &account) + + bytedata, _ := json.Marshal(acc) + t.Log(string(bytedata)) +} + +func TestUpdateAccount_getDelegationInfo(t *testing.T) { + ret, _ := getDelegationInfo(account.Address) + t.Log(ret) +} + +func TestUpdateAccount_getUnbondingDelegationInfo(t *testing.T) { + ret, _ := getUnbondingDelegationInfo(account.Address) + t.Log(ret) +} From 75c3fbffd9ed29a3b1a98ca1e685afa0185ce02f Mon Sep 17 00:00:00 2001 From: huangweichang Date: Wed, 8 Jul 2020 16:29:43 +0800 Subject: [PATCH 10/27] update log output --- backend/lcd/stake.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/lcd/stake.go b/backend/lcd/stake.go index b734d9220..79ef7379a 100644 --- a/backend/lcd/stake.go +++ b/backend/lcd/stake.go @@ -116,7 +116,7 @@ func GetWithdrawAddressByValidatorAcc(validatorAcc string) (string, error) { func GetDistributionRewardsByValidatorAcc(validatorAcc string) (utils.CoinsAsStr, []RewardsFromDelegations, utils.CoinsAsStr, error) { if !strings.HasPrefix(validatorAcc, conf.Get().Hub.Prefix.AccAddr) { - return nil, nil, nil, fmt.Errorf("address prefix is not %v", conf.Get().Hub.Prefix.AccAddr) + return nil, nil, nil, fmt.Errorf("address prefix is should %v", conf.Get().Hub.Prefix.AccAddr) } url := fmt.Sprintf(UrlDistributionRewardsByValidatorAcc, conf.Get().Hub.LcdUrl, validatorAcc) resAsBytes, err := utils.Get(url) From 717b17c9209c7188b1ab8ec3672a23063465abac Mon Sep 17 00:00:00 2001 From: huangweichang Date: Wed, 8 Jul 2020 18:34:11 +0800 Subject: [PATCH 11/27] refactor code --- backend/lcd/keys.go | 4 ++-- backend/task/update_account.go | 25 ++++++++++++++----------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/backend/lcd/keys.go b/backend/lcd/keys.go index b51e2036f..441a1e8fc 100644 --- a/backend/lcd/keys.go +++ b/backend/lcd/keys.go @@ -78,7 +78,7 @@ func AccountInfo(address string) (Account01411, error) { } if err := json.Unmarshal(resBytes, &acc); err != nil { - logger.Error("get account error", logger.String("err", err.Error())) + //logger.Error("get account error", logger.String("err", err.Error())) return acc, err } return acc, nil @@ -101,7 +101,7 @@ func GetIconsByKey(key string) (string, error) { } var picdata vo.LookupIcons if err := json.Unmarshal(resBytes, &picdata); err != nil { - logger.Error("get icons error", logger.String("err", err.Error())) + //logger.Error("get icons error", logger.String("err", err.Error())) return "", err } diff --git a/backend/task/update_account.go b/backend/task/update_account.go index 229aa15d1..2af73a591 100644 --- a/backend/task/update_account.go +++ b/backend/task/update_account.go @@ -7,10 +7,10 @@ import ( "github.com/irisnet/explorer/backend/orm/document" "github.com/irisnet/explorer/backend/lcd" "github.com/irisnet/explorer/backend/types" - "strconv" "math/big" "time" "math" + "strings" ) type UpdateAccount struct { @@ -42,6 +42,9 @@ func (task UpdateAccount) DoTask(fn func(string) chan bool) error { } for i := range accounts { + if !strings.HasPrefix(accounts[i].Address, conf.Get().Hub.Prefix.AccAddr) { + continue + } if err := updateAccount(&accounts[i]); err != nil { logger.Warn("get AccountInfo failed", logger.String("err", err.Error())) } @@ -79,11 +82,11 @@ func updateAccountInfo(account *document.Account) (*document.Account, error) { account = res } - delegate, err := getDelegationInfo(account.Address) + delegation, err := getDelegationInfo(account.Address) if err != nil { logger.Warn("update Delegation Info have error", logger.String("err", err.Error())) } - unbondingdelegator, err := getUnbondingDelegationInfo(account.Address) + unbondingDelegation, err := getUnbondingDelegationInfo(account.Address) if err != nil { logger.Warn("update UnbondingDelegation Info have error", logger.String("err", err.Error())) } @@ -96,11 +99,11 @@ func updateAccountInfo(account *document.Account) (*document.Account, error) { account.Delegation = utils.Coin{ Denom: balance.Denom, - Amount: delegate, + Amount: delegation, } account.UnbondingDelegation = utils.Coin{ Denom: balance.Denom, - Amount: unbondingdelegator, + Amount: unbondingDelegation, } account.CoinIris = utils.Coin{ @@ -110,7 +113,7 @@ func updateAccountInfo(account *document.Account) (*document.Account, error) { account.Total = utils.Coin{ Denom: balance.Denom, - Amount: balance.Amount + delegate + unbondingdelegator, + Amount: balance.Amount + delegation + unbondingDelegation, } return account, nil } @@ -119,7 +122,7 @@ func loadRewards(coins utils.CoinsAsStr) utils.Coin { var retcoin utils.Coin for _, val := range coins { if val.Denom == types.IRISAttoUint { - rewardsAmt, _ := strconv.ParseFloat(val.Amount, 64) + rewardsAmt, _ := utils.ParseStringToFloat(val.Amount) return utils.Coin{Denom: val.Denom, Amount: rewardsAmt} } } @@ -137,14 +140,14 @@ func getBalance(account *document.Account) (utils.Coin, *document.Account, error for _, val := range ret.Value.Coins { if val.Denom == types.IRISAttoUint { balance.Denom = val.Denom - amount, _ := strconv.ParseFloat(val.Amount, 64) + amount, _ := utils.ParseStringToFloat(val.Amount) if amount > 0 { balance.Amount = amount } break } } - if number, _ := strconv.ParseUint(ret.Value.AccountNum, 10, 64); number > 0 { + if number, ok := utils.ParseUint(ret.Value.AccountNum); ok { account.AccountNumber = number } return balance, account, nil @@ -198,7 +201,7 @@ func getDelegationInfo(address string) (float64, error) { } } token = new(big.Rat).Mul(token, new(big.Rat).SetFloat64(math.Pow10(18))) - return strconv.ParseFloat(token.FloatString(18), 64) + return utils.ParseStringToFloat(token.FloatString(18)) } func getUnbondingDelegationInfo(address string) (float64, error) { @@ -212,5 +215,5 @@ func getUnbondingDelegationInfo(address string) (float64, error) { } } - return strconv.ParseFloat(token.FloatString(18), 64) + return utils.ParseStringToFloat(token.FloatString(18)) } From d4e0716d3ace1db0bbb058e3d3b0f30977d189d6 Mon Sep 17 00:00:00 2001 From: huangweichang Date: Wed, 8 Jul 2020 18:49:09 +0800 Subject: [PATCH 12/27] remove no use code --- backend/orm/document/account.go | 39 +++++++++++------------------- backend/service/service_account.go | 2 +- backend/task/update_account.go | 34 -------------------------- 3 files changed, 15 insertions(+), 60 deletions(-) diff --git a/backend/orm/document/account.go b/backend/orm/document/account.go index 2a252b9e6..01452f1b4 100644 --- a/backend/orm/document/account.go +++ b/backend/orm/document/account.go @@ -7,6 +7,7 @@ import ( "github.com/irisnet/explorer/backend/utils" "gopkg.in/mgo.v2/bson" "gopkg.in/mgo.v2/txn" + "time" ) const ( @@ -17,21 +18,15 @@ const ( ) type Account struct { - Address string `bson:"address"` - AccountNumber uint64 `bson:"account_number"` - Total utils.Coin `bson:"total"` - TotalUpdateHeight int64 `bson:"total_update_height"` - TotalUpdateAt int64 `bson:"total_update_at"` - CoinIris utils.Coin `bson:"coin_iris"` - CoinIrisUpdateHeight int64 `bson:"coin_iris_update_height"` - CoinIrisUpdateAt int64 `bson:"coin_iris_update_at"` - Delegation utils.Coin `bson:"delegation"` - DelegationUpdateHeight int64 `bson:"delegation_update_height"` - DelegationUpdateAt int64 `bson:"delegation_update_at"` - UnbondingDelegation utils.Coin `bson:"unbonding_delegation"` - UnbondingDelegationUpdateHeight int64 `bson:"unbonding_delegation_update_height"` - UnbondingDelegationUpdateAt int64 `bson:"unbonding_delegation_update_at"` - Rewards utils.Coin `bson:"rewards"` + Address string `bson:"address"` + AccountNumber uint64 `bson:"account_number"` + Total utils.Coin `bson:"total"` + CoinIris utils.Coin `bson:"coin_iris"` + Delegation utils.Coin `bson:"delegation"` + UnbondingDelegation utils.Coin `bson:"unbonding_delegation"` + Rewards utils.Coin `bson:"rewards"` + UpdateAt int64 `bson:"update_at"` + CreateAt int64 `bson:"create_at"` } func (a Account) String() string { @@ -40,19 +35,12 @@ func (a Account) String() string { Address :%v AccountNumber :%v Total :%v -TotalUpdateHeight :%v -TotalUpdateAt :%v CoinIris :%v -CoinIrisUpdateHeight :%v -CoinIrisUpdateAt :%v Delegation :%v -DelegationUpdateHeight :%v -DelegationUpdateAt :%v UnbondingDelegation :%v -UnbondingDelegationUpdateHeight :%v -UnbondingDelegationUpdateAt :%v -`, a.Address, a.AccountNumber, a.Total, a.TotalUpdateHeight, a.TotalUpdateAt, a.CoinIris, a.CoinIrisUpdateHeight, a.CoinIrisUpdateAt, a.Delegation, a.DelegationUpdateAt, a.DelegationUpdateAt, - a.UnbondingDelegation, a.UnbondingDelegationUpdateHeight, a.UnbondingDelegationUpdateAt) +UpdateAt :%v +CreateAt :%v +`, a.Address, a.AccountNumber, a.Total, a.CoinIris, a.Delegation, a.UnbondingDelegation, a.UpdateAt, a.CreateAt) } func (a Account) GetAccountList() ([]Account, error) { @@ -125,6 +113,7 @@ func (a Account) Update(account Account) error { query := orm.NewQuery() defer query.Release() c := query.GetDb().C(a.Name()) + account.UpdateAt = time.Now().Unix() return c.Update(account.PkKvPair(), account) } diff --git a/backend/service/service_account.go b/backend/service/service_account.go index 8689f1a14..cf3750f6f 100644 --- a/backend/service/service_account.go +++ b/backend/service/service_account.go @@ -137,7 +137,7 @@ func (service *AccountService) QueryRichList() (vo.AccountsInfoRespond) { acc.Total, }, Percent: rate, - UpdateAt: acc.TotalUpdateAt, + //UpdateAt: acc.TotalUpdateAt, }) } return accList diff --git a/backend/task/update_account.go b/backend/task/update_account.go index 2af73a591..5309a4a41 100644 --- a/backend/task/update_account.go +++ b/backend/task/update_account.go @@ -8,7 +8,6 @@ import ( "github.com/irisnet/explorer/backend/lcd" "github.com/irisnet/explorer/backend/types" "math/big" - "time" "math" "strings" ) @@ -90,12 +89,6 @@ func updateAccountInfo(account *document.Account) (*document.Account, error) { if err != nil { logger.Warn("update UnbondingDelegation Info have error", logger.String("err", err.Error())) } - starttime := time.Unix(account.TotalUpdateAt, 0).In(cstZone) - if res, err := updateHeightTimeStamp(starttime, time.Now().In(cstZone), account); err == nil { - account = res - } else { - logger.Warn("update HeightTimeStamp have error", logger.String("err", err.Error())) - } account.Delegation = utils.Coin{ Denom: balance.Denom, @@ -153,33 +146,6 @@ func getBalance(account *document.Account) (utils.Coin, *document.Account, error return balance, account, nil } -func updateHeightTimeStamp(starttime, endtime time.Time, account *document.Account) (*document.Account, error) { - var txModel document.CommonTx - txs, err := txModel.GetTxsByDurationAddress(starttime, endtime, "") - if err != nil { - return nil, err - } - var height, timestamp int64 - for _, val := range txs { - - switch val.Type { - case types.TxTypeStakeDelegate, types.TxTypeStakeBeginUnbonding, types.TxTypeBeginRedelegate, - types.TxTypeStakeCreateValidator: - if val.Height > height && val.From == account.Address { - height = val.Height - timestamp = val.Time.Unix() - } - } - - } - if height > account.TotalUpdateHeight && timestamp > account.TotalUpdateAt { - account.CoinIrisUpdateHeight = height - account.CoinIrisUpdateAt = timestamp - account.TotalUpdateHeight = height - account.TotalUpdateAt = timestamp - } - return account, nil -} func getDelegationInfo(address string) (float64, error) { delegations := lcd.GetDelegationsByDelAddr(address) From bb6df62ae20973d9343eccc2322ac442c0a8bc4e Mon Sep 17 00:00:00 2001 From: huangweichang Date: Thu, 9 Jul 2020 10:22:45 +0800 Subject: [PATCH 13/27] fix code for empty address --- backend/lcd/keys.go | 3 +++ backend/utils/http_client.go | 10 +++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/backend/lcd/keys.go b/backend/lcd/keys.go index 441a1e8fc..33e03a188 100644 --- a/backend/lcd/keys.go +++ b/backend/lcd/keys.go @@ -76,6 +76,9 @@ func AccountInfo(address string) (Account01411, error) { if err != nil { return acc, err } + if resBytes == nil { + return acc, nil + } if err := json.Unmarshal(resBytes, &acc); err != nil { //logger.Error("get account error", logger.String("err", err.Error())) diff --git a/backend/utils/http_client.go b/backend/utils/http_client.go index 39a85c855..0a786b4f5 100644 --- a/backend/utils/http_client.go +++ b/backend/utils/http_client.go @@ -29,20 +29,24 @@ func init() { func Get(url string) (bz []byte, err error) { req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { - logger.Error("req error", logger.Any("err", err), logger.String("url", url)) + logger.Error("NewRequest error", logger.Any("err", err), logger.String("url", url)) return } resp, err := client.Do(req) if err != nil { - logger.Error("req error", logger.Any("err", err.Error()), logger.String("url", url)) + logger.Error("Do error", logger.Any("err", err.Error()), logger.String("url", url)) return } if resp.StatusCode != http.StatusOK { + if resp.StatusCode == http.StatusNoContent { + resp.Body.Close() + return + } if bz2, err := ioutil.ReadAll(resp.Body); err == nil { resp.Body.Close() - logger.Error("req error", logger.Any("err", string(bz2)), logger.String("url", url)) + logger.Error("ReadAll error", logger.Any("err", string(bz2)), logger.String("url", url)) } return } From e05c36e9b52046d41f5d78092031e589693e8492 Mon Sep 17 00:00:00 2001 From: huangweichang Date: Thu, 9 Jul 2020 10:53:52 +0800 Subject: [PATCH 14/27] refactor code --- backend/lcd/keys.go | 2 +- backend/lcd/keys_test.go | 2 +- backend/task/update_account_test.go | 6 ------ backend/utils/http_client.go | 27 ++++++++++++--------------- 4 files changed, 14 insertions(+), 23 deletions(-) diff --git a/backend/lcd/keys.go b/backend/lcd/keys.go index 33e03a188..fba97c56a 100644 --- a/backend/lcd/keys.go +++ b/backend/lcd/keys.go @@ -76,7 +76,7 @@ func AccountInfo(address string) (Account01411, error) { if err != nil { return acc, err } - if resBytes == nil { + if len(resBytes) == 0 { return acc, nil } diff --git a/backend/lcd/keys_test.go b/backend/lcd/keys_test.go index c2701733f..3206279b6 100644 --- a/backend/lcd/keys_test.go +++ b/backend/lcd/keys_test.go @@ -6,7 +6,7 @@ import ( ) func TestAccount(t *testing.T) { - address := "faa192vef4442d07lqde59mx35dvmfv9v72wrsu84a" + address := "faa1dmnnl50sagq2f6x88mz62rcn2mv5pfe53madlz" if res, err := Account(address); err != nil { t.Fatal(err) diff --git a/backend/task/update_account_test.go b/backend/task/update_account_test.go index d0a2d24b3..2688f14cf 100644 --- a/backend/task/update_account_test.go +++ b/backend/task/update_account_test.go @@ -3,7 +3,6 @@ package task import ( "testing" "github.com/irisnet/explorer/backend/orm/document" - "time" "encoding/json" ) @@ -46,12 +45,7 @@ func TestUpdateAccount_getBalance(t *testing.T) { t.Log(v) } -func TestUpdateAccount_updateHeightTimeStamp(t *testing.T) { - acc, _ := updateHeightTimeStamp(time.Unix(account.TotalUpdateAt, 0), time.Now(), &account) - bytedata, _ := json.Marshal(acc) - t.Log(string(bytedata)) -} func TestUpdateAccount_getDelegationInfo(t *testing.T) { ret, _ := getDelegationInfo(account.Address) diff --git a/backend/utils/http_client.go b/backend/utils/http_client.go index 0a786b4f5..64b000a07 100644 --- a/backend/utils/http_client.go +++ b/backend/utils/http_client.go @@ -26,38 +26,35 @@ func init() { } } -func Get(url string) (bz []byte, err error) { +func Get(url string) ([]byte, error) { req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { - logger.Error("NewRequest error", logger.Any("err", err), logger.String("url", url)) - return + //logger.Error("NewRequest error", logger.Any("err", err), logger.String("url", url)) + return nil, err } resp, err := client.Do(req) if err != nil { - logger.Error("Do error", logger.Any("err", err.Error()), logger.String("url", url)) - return + //logger.Error("Do error", logger.Any("err", err.Error()), logger.String("url", url)) + return nil, err } if resp.StatusCode != http.StatusOK { - if resp.StatusCode == http.StatusNoContent { + _, err := ioutil.ReadAll(resp.Body) + if err == nil { resp.Body.Close() - return + //logger.Error("ReadAll error", logger.Any("err", string(bz2)), logger.String("url", url)) } - if bz2, err := ioutil.ReadAll(resp.Body); err == nil { - resp.Body.Close() - logger.Error("ReadAll error", logger.Any("err", string(bz2)), logger.String("url", url)) - } - return } - bz, err = ioutil.ReadAll(resp.Body) + bz, err := ioutil.ReadAll(resp.Body) defer resp.Body.Close() if err != nil { - logger.Error("ioutil.ReadAll err", logger.Any("io", err), logger.String("url", url)) + //logger.Error("ioutil.ReadAll err", logger.Any("io", err), logger.String("url", url)) + return nil, err } - return + return bz, nil } func Forward(req *http.Request, url string) (bz []byte, err error) { From d6e8f7c3da284fded6c9cc59a1c089686fbe5ba7 Mon Sep 17 00:00:00 2001 From: huangweichang Date: Thu, 9 Jul 2020 11:53:48 +0800 Subject: [PATCH 15/27] refactor Get function --- backend/utils/http_client.go | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/backend/utils/http_client.go b/backend/utils/http_client.go index 64b000a07..b36dc3882 100644 --- a/backend/utils/http_client.go +++ b/backend/utils/http_client.go @@ -26,6 +26,8 @@ func init() { } } +// function should be return value as follow: +// - respond http code is 200 func Get(url string) ([]byte, error) { req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { @@ -39,22 +41,24 @@ func Get(url string) ([]byte, error) { //logger.Error("Do error", logger.Any("err", err.Error()), logger.String("url", url)) return nil, err } - if resp.StatusCode != http.StatusOK { - _, err := ioutil.ReadAll(resp.Body) - if err == nil { - resp.Body.Close() - //logger.Error("ReadAll error", logger.Any("err", string(bz2)), logger.String("url", url)) - } - } - - bz, err := ioutil.ReadAll(resp.Body) - + //if resp.StatusCode != http.StatusOK { + // _, err := ioutil.ReadAll(resp.Body) + // if err == nil { + // resp.Body.Close() + // //logger.Error("ReadAll error", logger.Any("err", string(bz2)), logger.String("url", url)) + // } + //} defer resp.Body.Close() - if err != nil { - //logger.Error("ioutil.ReadAll err", logger.Any("io", err), logger.String("url", url)) - return nil, err + + if resp.StatusCode == http.StatusOK { + bz, err := ioutil.ReadAll(resp.Body) + if err != nil { + //logger.Error("ioutil.ReadAll err", logger.Any("io", err), logger.String("url", url)) + return nil, err + } + return bz, nil } - return bz, nil + return nil, nil } func Forward(req *http.Request, url string) (bz []byte, err error) { From 8f4a3ba72c5096aa472143fd80f89da32329b87b Mon Sep 17 00:00:00 2001 From: huangweichang Date: Fri, 10 Jul 2020 15:04:27 +0800 Subject: [PATCH 16/27] recover update_at respond --- backend/service/service_account.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/service/service_account.go b/backend/service/service_account.go index cf3750f6f..62d5961d7 100644 --- a/backend/service/service_account.go +++ b/backend/service/service_account.go @@ -137,7 +137,7 @@ func (service *AccountService) QueryRichList() (vo.AccountsInfoRespond) { acc.Total, }, Percent: rate, - //UpdateAt: acc.TotalUpdateAt, + UpdateAt: acc.UpdateAt, }) } return accList From e379718edd76dd62b1eec6528db593df894148eb Mon Sep 17 00:00:00 2001 From: huangweichang Date: Mon, 13 Jul 2020 11:21:26 +0800 Subject: [PATCH 17/27] fix the code to replace total_update_at to update_at --- backend/orm/document/account.go | 8 ++++---- backend/service/service_account_test.go | 8 -------- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/backend/orm/document/account.go b/backend/orm/document/account.go index 01452f1b4..b4e689adb 100644 --- a/backend/orm/document/account.go +++ b/backend/orm/document/account.go @@ -14,7 +14,7 @@ const ( CollectionNmAccount = "account" AccountFieldAddress = "address" AccountFieldAccountNumber = "account_number" - AccountFieldTotalUpdateAt = "total_update_at" + AccountFieldUpdateAt = "update_at" ) type Account struct { @@ -50,7 +50,7 @@ func (a Account) GetAccountList() ([]Account, error) { defer query.Release() query.SetCollection(CollectionNmAccount). - SetSort(desc("total.amount"), AccountFieldTotalUpdateAt, AccountFieldAccountNumber). + SetSort(desc("total.amount"), AccountFieldUpdateAt, AccountFieldAccountNumber). SetSize(100). SetResult(&result) err := query.Exec() @@ -70,7 +70,7 @@ func (a Account) GetDelegatores(offset, size int) ([]Account, error) { } query.SetCollection(CollectionNmAccount).SetCondition(condition). - SetSort(desc("total.amount"), AccountFieldTotalUpdateAt, AccountFieldAccountNumber). + SetSort(desc("total.amount"), AccountFieldUpdateAt, AccountFieldAccountNumber). SetOffset(offset). SetSize(size). SetResult(&result) @@ -85,7 +85,7 @@ func (a Account) GetAllAccount() ([]Account, error) { defer query.Release() query.SetCollection(CollectionNmAccount). - SetSort(desc("total.amount"), AccountFieldTotalUpdateAt, AccountFieldAccountNumber). + SetSort(desc("total.amount"), AccountFieldUpdateAt, AccountFieldAccountNumber). SetResult(&result) err := query.Exec() return result, err diff --git a/backend/service/service_account_test.go b/backend/service/service_account_test.go index bf55c2083..a3ddcc405 100644 --- a/backend/service/service_account_test.go +++ b/backend/service/service_account_test.go @@ -160,20 +160,12 @@ func TestInitAccountFromGenesisFile(t *testing.T) { Insert: document.Account{ Address: acc.Address, Total: totalAmt, - TotalUpdateHeight: exportHeight, - TotalUpdateAt: genesis.GenesisTime.Unix(), CoinIris: coin, - CoinIrisUpdateHeight: exportHeight, - CoinIrisUpdateAt: genesis.GenesisTime.Unix(), Delegation: utils.Coin{Denom: delegateAmt.Denom, Amount: delegateAmt.Amount}, - DelegationUpdateHeight: delegateHeight, - DelegationUpdateAt: genesis.GenesisTime.Unix(), UnbondingDelegation: utils.Coin{Denom: unbondingAmt.Denom, Amount: unbondingAmt.Amount}, - UnbondingDelegationUpdateHeight: undHeight, - UnbondingDelegationUpdateAt: genesis.GenesisTime.Unix(), AccountNumber: accountNumber, }, From eda8b88efbd6932bb8caf7b172a9aa91dcabc497 Mon Sep 17 00:00:00 2001 From: huangweichang Date: Mon, 13 Jul 2020 12:17:38 +0800 Subject: [PATCH 18/27] update the backend README.md --- backend/README.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/backend/README.md b/backend/README.md index 70510ec56..7bc0f4700 100644 --- a/backend/README.md +++ b/backend/README.md @@ -2,12 +2,12 @@ Blockchain explorer for the IRIS Hub ##### 1 Prerequisites -* go1.9.2+ +* go1.13.0+ ##### 2 install ```bash - go get github.com/irisnet/explorer + go get github.com/irisnet/explorer/backend make all ``` @@ -27,15 +27,25 @@ Blockchain explorer for the IRIS Hub CHAIN_ID : irishub chain-id API_VERSION : explorer api version MAX_DRAW_CNT : Maximum number of collections + SHOW_FAUCET : switch of show faucet + INITIAL_SUPPLY : initial supplay of IRIS Token CUR_ENV : current environment(dev/qa/testnet/mainnet) CronTimeAssetGateways: time interval of update asset gateways CronTimeAssetTokens: time interval of update asset tokens CronTimeGovParams: time interval of update gov params CronTimeTxNumByDay: time interval of update tx num by day + CronTimeControlTask: time interval of monitor task execute CronTimeAccountRewards: time interval of update account rewards CronTimeValidators: time interval of update validators CronTimeValidatorIcons: time interval of update validator icons + CronTimeProposalVoters: time interval of update voter info of proposal + CronTimeValidatorStaticInfo: time interval of cronjob to update validator static info include uptime, selfBond, delegatorNum + CronTimeFormatStaticDay: define time format of cronjob execute by every day + CronTimeFormatStaticMonth: define time format of cronjob execute by eveny month + CronTimeStaticDataDay: time interval of cronjob to snapshot delegator and validator rewards info + CronTimeStaticDataMonth: time interval of cronjob to caculate delegator and validator rewards info CronTimeHeartBeat: time interval of heart beat in cron task + NetreqLimitMax: max network request to lcd node //irishub v0.11.0 add From 00efbd6f254b7937d164a14fa7804fa2191a9029 Mon Sep 17 00:00:00 2001 From: huangweichang Date: Mon, 13 Jul 2020 14:53:01 +0800 Subject: [PATCH 19/27] fix code about rewards update --- backend/task/update_account.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/backend/task/update_account.go b/backend/task/update_account.go index 5309a4a41..bced0a6bd 100644 --- a/backend/task/update_account.go +++ b/backend/task/update_account.go @@ -59,11 +59,15 @@ func updateAccount(account *document.Account) error { logger.Warn("get AccountInfo failed", logger.String("err", err.Error())) } _, _, rewards, err := lcd.GetDistributionRewardsByValidatorAcc(account.Address) - if err == nil && len(rewards) > 0 { - newrewards := loadRewards(rewards) - subvalue := newrewards.Amount - account.Rewards.Amount - account.Total.Amount += subvalue - account.Rewards = newrewards + if err == nil { + if len(rewards) > 0 { + newrewards := loadRewards(rewards) + subvalue := newrewards.Amount - account.Rewards.Amount + account.Total.Amount += subvalue + account.Rewards = newrewards + } else { + account.Rewards = utils.Coin{Denom: account.Rewards.Denom} + } } if err := account.Update(*account); err != nil { logger.Warn("Account Update Rewards failed", logger.String("err", err.Error())) @@ -146,7 +150,6 @@ func getBalance(account *document.Account) (utils.Coin, *document.Account, error return balance, account, nil } - func getDelegationInfo(address string) (float64, error) { delegations := lcd.GetDelegationsByDelAddr(address) token, _ := new(big.Rat).SetString("0") From 904e6ce0c9f5c7baf30f8bfa5ecf3f7100e4d9e6 Mon Sep 17 00:00:00 2001 From: huangweichang Date: Mon, 13 Jul 2020 15:09:40 +0800 Subject: [PATCH 20/27] adjust task control monitor start way --- backend/task/types.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/backend/task/types.go b/backend/task/types.go index a844f09c7..18e25d14e 100644 --- a/backend/task/types.go +++ b/backend/task/types.go @@ -42,9 +42,9 @@ func init() { engine.AppendTask(UpdateProposalVoters{}) engine.AppendTask(UpdateAccount{}) - taskControlMonitor := TaskControlMonitor{} - taskControlMonitor.unlockAllTasks() - engine.AppendTask(taskControlMonitor) + //taskControlMonitor := TaskControlMonitor{} + //taskControlMonitor.unlockAllTasks() + //engine.AppendTask(taskControlMonitor) } type TimerTask interface { @@ -74,6 +74,9 @@ func (e *Engine) AppendTask(task TimerTask) { func Start() { engine.Start() + taskControlMonitor := TaskControlMonitor{} + taskControlMonitor.unlockAllTasks() + taskControlMonitor.Start() // tasks manager by cron job c := cron.New(cron.WithLocation(cstZone)) From 94730c7eeab72949659b1eb5b7e72d67806a7046 Mon Sep 17 00:00:00 2001 From: huangweichang Date: Mon, 13 Jul 2020 16:22:40 +0800 Subject: [PATCH 21/27] update README.md --- README.md | 52 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fedb54071..8d7f641c7 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,18 @@ -# explorer -Blockchain explorer for IRIS Hub +# Explorer +Blockchain explorer for IRIS Hub,which includes explorer-backend and explorer-frontend. + +# Explorer-backend + +## Run with docker +You can run application with docker. + +```$xslt +cd github.com/irisnet/explorer/backend +docker build -t explorer-backend . +``` +## ENV -# ENV DB_ADDR: mongo url ( eg. 192.168.150.7:27017) DB_DATABASE: mongo database name ( eg. sync-iris ) @@ -15,6 +25,38 @@ FAUCET_URL: faucet url ( eg. http://dev.faucet.irisplorer.io ) CHAIN_ID: chain-id ( eg. rainbow-dev ) -# Docker Run -docker run -p 8080:8080 -e ${ENV Variables} explorer +detail: github.com/irisnet/explorer/backend/README.md + +## Docker Run +docker run -p 8080:8080 -e ${ENV Variables} explorer-backend:latest + +# Explorer-frontend + +##Config +- vue.config.js +```$xslt +module.exports = { + devServer: { + proxy: { + '/api':{ + target:'http://localhost:8080', //backend address:port + ...... + } + } + }, +}; +``` + + +## Run with docker +You can run application with docker. + +```$xslt +cd github.com/irisnet/explorer/frontend +docker build -t explorer-frontend . +``` + +## Docker Run +docker run -p 8080:8080 -e ${ENV Variables} explorer-frontend:latest + From a9f000e93842cd1012b557986650c5b41930df21 Mon Sep 17 00:00:00 2001 From: huangweichang Date: Mon, 13 Jul 2020 16:35:01 +0800 Subject: [PATCH 22/27] update lastestexectime when task control monitor unlockAllTasks --- backend/orm/document/task_control.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/orm/document/task_control.go b/backend/orm/document/task_control.go index 2129adc99..db061a327 100644 --- a/backend/orm/document/task_control.go +++ b/backend/orm/document/task_control.go @@ -189,7 +189,8 @@ func (d TaskControl) UnlockAllTasks() error { } update := bson.M{ "$set": bson.M{ - TCFieldIsInProcess: false, + TCFieldIsInProcess: false, + TCFieldLatestExecTime: time.Now().Unix(), }, } From 4a12a76d3d9c6b3c44ccd63b0b173f20d96180d7 Mon Sep 17 00:00:00 2001 From: huangweichang Date: Tue, 14 Jul 2020 09:53:22 +0800 Subject: [PATCH 23/27] update lastestexectime when monitor CheckAndUpdate and add cronjob task name --- backend/task/task_control.go | 1 + backend/task/task_control_test.go | 6 +++++- backend/task/types.go | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/backend/task/task_control.go b/backend/task/task_control.go index 4dbe4676b..975508224 100644 --- a/backend/task/task_control.go +++ b/backend/task/task_control.go @@ -156,6 +156,7 @@ func (s TaskControlMonitor) CheckAndUpdate(one document.TaskControl) error { } if currentTimeInterval >= 3*timeHeartBeat && one.IsInProcess && one.TaskName != types.TaskConTrolMonitor { one.IsInProcess = false + one.LatestExecTime = time.Now().Unix() if err := s.controlModel.UpdateByPK(one); err != nil { return err } diff --git a/backend/task/task_control_test.go b/backend/task/task_control_test.go index f4df1df27..9063ae68e 100644 --- a/backend/task/task_control_test.go +++ b/backend/task/task_control_test.go @@ -32,4 +32,8 @@ func TestTaskControlService_Start(t *testing.T) { func TestTaskControlService_DoTask(t *testing.T) { new(TaskControlMonitor).DoTask(HeartBeat) -} \ No newline at end of file +} + +func TestTaskControlMonitor_unlockAllTasks(t *testing.T) { + new(TaskControlMonitor).unlockAllTasks() +} diff --git a/backend/task/types.go b/backend/task/types.go index 18e25d14e..591ec1c64 100644 --- a/backend/task/types.go +++ b/backend/task/types.go @@ -30,6 +30,8 @@ var ( "static_validator": true, "static_delegator_month": true, "static_validator_month": true, + "tx_num_stat_task": true, + "update_validator_icons": true, } ) From fe3d337cf8b2e2dbbcfdd12e36e28d6646712747 Mon Sep 17 00:00:00 2001 From: zhjibi742626083 Date: Tue, 14 Jul 2020 10:37:29 +0800 Subject: [PATCH 24/27] fixed display of zero or '--' when the address page with zero reward is refreshed --- .../address/delegator/AddressInformationComponent.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/src/components/address/delegator/AddressInformationComponent.vue b/frontend/src/components/address/delegator/AddressInformationComponent.vue index b9ef8a91c..c8d97b6a3 100644 --- a/frontend/src/components/address/delegator/AddressInformationComponent.vue +++ b/frontend/src/components/address/delegator/AddressInformationComponent.vue @@ -138,7 +138,7 @@ res.numberValue = item['unBonding'] ? item['unBonding'].replace(/[^\d.]/g,"") : 0; res.percent = this.formatDecimalNumberToFixedNumber(item.totalAmount.replace(/[^\d.]/g,""),res.numberValue) }else { - res.value = item[Tools.firstWordLowerCase(res.label)] || "--"; + res.value = item[Tools.firstWordLowerCase(res.label)] && item[Tools.firstWordLowerCase(res.label)] !== 0 ? item[Tools.firstWordLowerCase(res.label)] : "--"; res.numberValue = item[Tools.firstWordLowerCase(res.label)] ? item[Tools.firstWordLowerCase(res.label)].replace(/[^\d.]/g,"") : 0; res.percent = this.formatDecimalNumberToFixedNumber(item.totalAmount.replace(/[^\d.]/g,""),res.numberValue) From e2d8879499e239176f50a77c3de0ad2d5a99510c Mon Sep 17 00:00:00 2001 From: huangweichang Date: Tue, 14 Jul 2020 10:58:39 +0800 Subject: [PATCH 25/27] close unlockAllTasks method code --- backend/task/task_control.go | 20 ++++++++++---------- backend/task/task_control_test.go | 6 +++--- backend/task/types.go | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/backend/task/task_control.go b/backend/task/task_control.go index 975508224..7c52f6380 100644 --- a/backend/task/task_control.go +++ b/backend/task/task_control.go @@ -164,13 +164,13 @@ func (s TaskControlMonitor) CheckAndUpdate(one document.TaskControl) error { return nil } -// unlock all tasks which task is unlocked -func (s TaskControlMonitor) unlockAllTasks() error { - if err := taskControlModel.UnlockAllTasks(); err != nil { - if err != mgo.ErrNotFound { - return err - } - } - - return nil -} +//// unlock all tasks which task is unlocked +//func (s TaskControlMonitor) unlockAllTasks() error { +// if err := taskControlModel.UnlockAllTasks(); err != nil { +// if err != mgo.ErrNotFound { +// return err +// } +// } +// +// return nil +//} diff --git a/backend/task/task_control_test.go b/backend/task/task_control_test.go index 9063ae68e..b54a4492b 100644 --- a/backend/task/task_control_test.go +++ b/backend/task/task_control_test.go @@ -34,6 +34,6 @@ func TestTaskControlService_DoTask(t *testing.T) { new(TaskControlMonitor).DoTask(HeartBeat) } -func TestTaskControlMonitor_unlockAllTasks(t *testing.T) { - new(TaskControlMonitor).unlockAllTasks() -} +//func TestTaskControlMonitor_unlockAllTasks(t *testing.T) { +// new(TaskControlMonitor).unlockAllTasks() +//} diff --git a/backend/task/types.go b/backend/task/types.go index 591ec1c64..f5c71dff3 100644 --- a/backend/task/types.go +++ b/backend/task/types.go @@ -77,7 +77,7 @@ func (e *Engine) AppendTask(task TimerTask) { func Start() { engine.Start() taskControlMonitor := TaskControlMonitor{} - taskControlMonitor.unlockAllTasks() + //taskControlMonitor.unlockAllTasks() taskControlMonitor.Start() // tasks manager by cron job From 609751e6d2c0c8490e7d92a3c3f0ab0c4cc30227 Mon Sep 17 00:00:00 2001 From: huangweichang Date: Tue, 14 Jul 2020 11:04:39 +0800 Subject: [PATCH 26/27] unlock control monitor task when it locked at the time of server start --- backend/orm/document/task_control.go | 44 ++++++++++++++-------------- backend/task/types.go | 2 +- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/backend/orm/document/task_control.go b/backend/orm/document/task_control.go index db061a327..4e2b6bc8c 100644 --- a/backend/orm/document/task_control.go +++ b/backend/orm/document/task_control.go @@ -178,25 +178,25 @@ func (d TaskControl) UnlockTaskControl(taskName string) error { return nil } -func (d TaskControl) UnlockAllTasks() error { - q := orm.NewQuery() - defer q.Release() - - c := q.GetDb().C(d.Name()) - - selector := bson.M{ - TCFieldIsInProcess: true, - } - update := bson.M{ - "$set": bson.M{ - TCFieldIsInProcess: false, - TCFieldLatestExecTime: time.Now().Unix(), - }, - } - - if err := c.Update(selector, update); err != nil { - return err - } - - return nil -} +//func (d TaskControl) UnlockAllTasks() error { +// q := orm.NewQuery() +// defer q.Release() +// +// c := q.GetDb().C(d.Name()) +// +// selector := bson.M{ +// TCFieldIsInProcess: true, +// } +// update := bson.M{ +// "$set": bson.M{ +// TCFieldIsInProcess: false, +// TCFieldLatestExecTime: time.Now().Unix(), +// }, +// } +// +// if err := c.Update(selector, update); err != nil { +// return err +// } +// +// return nil +//} diff --git a/backend/task/types.go b/backend/task/types.go index f5c71dff3..2b0db35bc 100644 --- a/backend/task/types.go +++ b/backend/task/types.go @@ -77,7 +77,7 @@ func (e *Engine) AppendTask(task TimerTask) { func Start() { engine.Start() taskControlMonitor := TaskControlMonitor{} - //taskControlMonitor.unlockAllTasks() + taskControlMonitor.controlModel.UnlockTaskControl(taskControlMonitor.Name()) taskControlMonitor.Start() // tasks manager by cron job From 6c6de1badf681414ec1fd3fa930d75da23e5824d Mon Sep 17 00:00:00 2001 From: zhjibi742626083 Date: Tue, 14 Jul 2020 17:36:36 +0800 Subject: [PATCH 27/27] fixed display of zero or '--' when the address page with zero reward is refreshed --- .../src/components/address/delegator/AddressInfomation.vue | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/frontend/src/components/address/delegator/AddressInfomation.vue b/frontend/src/components/address/delegator/AddressInfomation.vue index e840846ae..498a2422f 100644 --- a/frontend/src/components/address/delegator/AddressInfomation.vue +++ b/frontend/src/components/address/delegator/AddressInfomation.vue @@ -251,7 +251,7 @@ totalDelegator:0, totalDelegatorValue:0, totalValidatorRewards:0, - allRewardsValue:0, + allRewardsValue: 0, allRewardsAmountValue:0, assetList: [], fixedNumber: 2, @@ -292,11 +292,11 @@ }, mounted(){ this.headerAddress = this.$route.params.param; + this.getRewardsItems(); this.getAddressInformation(); this.getAssetList(); this.getDelegationList(); this.getUnBondingDelegationList(); - this.getRewardsItems(); this.getAllTxType(); let statusArray = [ { @@ -374,7 +374,7 @@ }) }, getAssetList(){ - this.assetsItems = this.assetList.map( item => { + this.assetsItems = this.assetList.map( item => { if(item.denom === 'iris-atto'){ return { token: Tools.formatDenom(item.denom), @@ -517,6 +517,7 @@ } this.allRewardsAmountValue = res.total_rewards ? Tools.formatStringToFixedNumber(Tools.numberMoveDecimal(res.total_rewards[0].amount,-18),this.fixedNumber) : 0; this.totalDelegatorRewardValue = `${Tools.formatStringToFixedNumber(new BigNumber(moveDecimal(this.totalDelegatorReward.toString(),-2)).toFormat(),this.fixedNumber)} ${Constant.Denom.IRIS.toUpperCase()}` + this.getAssetList() } }catch (e) { console.error(e)