示例#1
0
func (p *Parser) NewMaxPromisedAmountsFront() error {

	err := p.generalCheck()
	if err != nil {
		return p.ErrInfo(err)
	}

	// является ли данный юзер майнером
	err = p.checkMiner(p.TxUserID)
	if err != nil {
		return p.ErrInfo(err)
	}

	nodePublicKey, err := p.GetNodePublicKey(p.TxUserID)
	if err != nil {
		return p.ErrInfo(err)
	}
	if len(nodePublicKey) == 0 {
		return p.ErrInfo("incorrect user_id")
	}

	allMaxAmounts := utils.GetAllMaxPromisedAmount()
	if err != nil {
		return p.ErrInfo(err)
	}

	totalCountCurrencies, err := p.GetCountCurrencies()
	if err != nil {
		return p.ErrInfo(err)
	}

	// проверим, верно ли указаны ID валют
	currencyList := make(map[string]int64)
	err = json.Unmarshal(p.TxMap["new_max_promised_amounts"], &currencyList)
	if err != nil {
		return p.ErrInfo(err)
	}
	currencyIdsSql := ""
	var countCurrency int64
	for currencyId, amount := range currencyList {
		if !utils.CheckInputData(currencyId, "int") {
			return p.ErrInfo("currencyId")
		}
		currencyIdsSql += currencyId + ","
		countCurrency++
		if !utils.InSliceInt64(amount, allMaxAmounts) {
			return p.ErrInfo("incorrect amount")
		}
	}
	currencyIdsSql = currencyIdsSql[0 : len(currencyIdsSql)-1]
	if countCurrency == 0 {
		return p.ErrInfo("countCurrency")
	}
	count, err := p.Single("SELECT count(id) FROM currency WHERE id IN (" + currencyIdsSql + ")").Int64()
	if err != nil {
		return p.ErrInfo(err)
	}
	if count != countCurrency {
		return p.ErrInfo("count != countCurrency")
	}

	forSign := fmt.Sprintf("%s,%s,%s,%s", p.TxMap["type"], p.TxMap["time"], p.TxMap["user_id"], p.TxMap["new_max_promised_amounts"])
	CheckSignResult, err := utils.CheckSign([][]byte{nodePublicKey}, forSign, p.TxMap["sign"], true)
	if err != nil {
		return p.ErrInfo(err)
	}
	if !CheckSignResult {
		return p.ErrInfo("incorrect sign")
	}

	// проверим, прошло ли 2 недели с момента последнего обновления
	pctTime, err := p.Single("SELECT max(time) FROM max_promised_amounts").Int64()
	if err != nil {
		return p.ErrInfo(err)
	}
	if p.TxTime-pctTime <= p.Variables.Int64["new_max_promised_amount"] {
		return p.ErrInfo("14 day error")
	}

	// берем все голоса
	maxPromisedAmountVotes := make(map[int64][]map[int64]int64)
	rows, err := p.Query("SELECT currency_id, amount, count(user_id) as votes FROM votes_max_promised_amount GROUP BY currency_id, amount ORDER BY currency_id, amount ASC")
	if err != nil {
		return p.ErrInfo(err)
	}
	defer rows.Close()
	for rows.Next() {
		var currency_id, amount, votes int64
		err = rows.Scan(&currency_id, &amount, &votes)
		if err != nil {
			return p.ErrInfo(err)
		}
		maxPromisedAmountVotes[currency_id] = append(maxPromisedAmountVotes[currency_id], map[int64]int64{amount: votes})
		//fmt.Println("currency_id", currency_id)
	}

	NewMaxPromisedAmountsVotes := make(map[string]int64)
	for currencyId, amountsAndVotes := range maxPromisedAmountVotes {
		NewMaxPromisedAmountsVotes[utils.Int64ToStr(currencyId)] = utils.GetMaxVote(amountsAndVotes, 0, totalCountCurrencies, 10)
	}

	jsonData, err := json.Marshal(NewMaxPromisedAmountsVotes)
	if err != nil {
		return p.ErrInfo(err)
	}
	if string(p.TxMap["new_max_promised_amounts"]) != string(jsonData) {
		return p.ErrInfo("p.TxMap[new_max_promised_amounts] != jsonData " + string(p.TxMap["new_max_promised_amounts"]) + "!=" + string(jsonData))
	}

	return nil
}
示例#2
0
func (p *Parser) VotesComplexFront() error {

	err := p.generalCheck()
	if err != nil {
		return p.ErrInfo(err)
	}

	// является ли данный юзер майнером
	err = p.checkMiner(p.TxUserID)
	if err != nil {
		return p.ErrInfo(err)
	}

	var txTime int64
	if p.BlockData != nil {
		txTime = p.BlockData.Time
	} else {
		txTime = time.Now().Unix() - 30
	}

	// прошло ли 30 дней с момента регистрации майнера
	err = p.checkMinerNewbie()
	if err != nil {
		return p.ErrInfo(err)
	}

	forSign := fmt.Sprintf("%s,%s,%s,%s", p.TxMap["type"], p.TxMap["time"], p.TxMap["user_id"], p.TxMap["json_data"])
	CheckSignResult, err := utils.CheckSign(p.PublicKeys, forSign, p.TxMap["sign"], false)
	if err != nil {
		return p.ErrInfo(err)
	}
	if !CheckSignResult {
		return p.ErrInfo("incorrect sign")
	}

	currencyVotes := make(map[string][]float64)
	var doubleCheck []int64
	// раньше не было рефских
	if p.BlockData == nil || p.BlockData.BlockId > 77951 {

		vComplex, err := makeVcomplex(p.TxMap["json_data"])
		if err != nil {
			return p.ErrInfo(err)
		}

		if vComplex.Referral == nil {
			return p.ErrInfo("!Referral")
		}
		if vComplex.Currency == nil {
			return p.ErrInfo("!Currency")
		}
		if p.BlockData == nil || p.BlockData.BlockId > 153750 {
			if vComplex.Admin > 0 {
				adminUserId, err := p.Single("SELECT user_id FROM users WHERE user_id  =  ?", vComplex.Admin).Int64()
				if err != nil {
					return p.ErrInfo(err)
				}
				if adminUserId == 0 {
					return p.ErrInfo("incorrect admin user_id")
				}
			}
		}
		if !utils.CheckInputData(vComplex.Referral["first"], "referral") || !utils.CheckInputData(vComplex.Referral["second"], "referral") || !utils.CheckInputData(vComplex.Referral["third"], "referral") {
			return p.ErrInfo("incorrect referral")
		}
		currencyVotes = vComplex.Currency
	} else {
		vComplex := make(map[string][]float64)
		err = json.Unmarshal(p.TxMap["json_data"], &vComplex)
		if err != nil {
			return p.ErrInfo(err)
		}
		currencyVotes = vComplex
	}
	for currencyId, data := range currencyVotes {
		if !utils.CheckInputData(currencyId, "int") {
			return p.ErrInfo("incorrect currencyId")
		}

		// проверим, что нет дублей
		if utils.InSliceInt64(utils.StrToInt64(currencyId), doubleCheck) {
			return p.ErrInfo("double currencyId")
		}
		doubleCheck = append(doubleCheck, utils.StrToInt64(currencyId))

		// есть ли такая валюта
		currencyId_, err := p.Single("SELECT id FROM currency WHERE id  =  ?", currencyId).Int64()
		if err != nil {
			return p.ErrInfo(err)
		}
		if currencyId_ == 0 {
			return p.ErrInfo("incorrect currencyId")
		}
		// у юзера по данной валюте должна быть обещанная сумма, которая имеет статус mining/repaid и находится с таким статусом >90 дней
		id, err := p.Single("SELECT id FROM promised_amount	WHERE currency_id  =  ? AND user_id  =  ? AND status IN ('mining', 'repaid') AND start_time < ? AND start_time > 0 AND del_block_id  =  0 AND del_mining_block_id  =  0", currencyId, p.TxUserID, (txTime - p.Variables.Int64["min_hold_time_promise_amount"])).Int64()
		if err != nil {
			return p.ErrInfo(err)
		}
		if id == 0 {
			return p.ErrInfo("incorrect currencyId")
		}

		// если по данной валюте еще не набралось >1000 майнеров, то за неё голосовать нельзя.
		countMiners, err := p.Single(`
			SELECT count(*) FROM
			(SELECT user_id
			FROM promised_amount
			WHERE start_time < ? AND del_block_id  =  0 AND status IN ('mining', 'repaid') AND currency_id  =  ? AND del_block_id  =  0 AND del_mining_block_id  =  0
			GROUP BY user_id) as t1`, (txTime - p.Variables.Int64["min_hold_time_promise_amount"]), currencyId).Int64()
		if err != nil {
			return p.ErrInfo(err)
		}
		if countMiners < p.Variables.Int64["min_miners_of_voting"] {
			return p.ErrInfo("countMiners")
		}
		if len(data) != 5 {
			return p.ErrInfo("incorrect data")
		}
		if !utils.CheckPct(data[0]) {
			return p.ErrInfo("incorrect miner_pct")
		}
		if !utils.CheckPct(data[1]) {
			return p.ErrInfo("incorrect user_pct")
		}

		// max promise amount
		if !utils.InSliceInt64(int64(data[2]), utils.GetAllMaxPromisedAmount()) {
			log.Debug("%v", int64(data[2]))
			log.Debug("%v", utils.GetAllMaxPromisedAmount())
			return p.ErrInfo("incorrect max promised amount")
		}

		totalCountCurrencies, err := p.Single("SELECT count(id) FROM currency").Int64()
		if err != nil {
			return p.ErrInfo(err)
		}

		// max other currency 0/1/2/3/.../76
		if !utils.CheckInputData(int(data[3]), "int") || int64(data[3]) > totalCountCurrencies {
			return p.ErrInfo(fmt.Sprintf("incorrect max other currency %d > %d", data[3], totalCountCurrencies))
		}

		currencyCount, err := p.Single("SELECT count(id) FROM currency").Int64()
		if err != nil {
			return p.ErrInfo(err)
		}
		if int64(data[3]) > (currencyCount - 1) {
			return p.ErrInfo("incorrect max other currency")
		}
		// reduction 10/25/50/90
		if !utils.InSliceInt64(int64(data[4]), consts.ReductionDC) {
			return p.ErrInfo("incorrect reduction")
		}
	}

	err = p.limitRequest(p.Variables.Int64["limit_votes_complex"], "votes_complex", p.Variables.Int64["limit_votes_complex_period"])
	if err != nil {
		return p.ErrInfo(err)
	}

	return nil
}