Skip to content

Commit

Permalink
fix 领钱错误问题
Browse files Browse the repository at this point in the history
  • Loading branch information
xuyz committed Dec 26, 2018
1 parent be21251 commit 29d2f51
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 26 deletions.
36 changes: 36 additions & 0 deletions cmd/commands/doctor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2018 The QOS Authors

package commands

import (
"log"

"github.com/QOSGroup/qmoon/db"
"github.com/QOSGroup/qmoon/service"
"github.com/spf13/cobra"
)

// DoctorCmd 健康检查
var DoctorCmd = &cobra.Command{
Use: "doctor",
Short: "健康检查",
RunE: doctor,
}

func init() {
registerFlagsDb(DoctorCmd)
}

func doctor(cmd *cobra.Command, args []string) error {
err := db.InitDb(config.DB, logger)
if err != nil {
log.Printf("Check db fail err:%s", err.Error())
}

err = service.Doctor()
if err != nil {
log.Printf("Check env fail err:%s", err.Error())
}

return nil
}
15 changes: 14 additions & 1 deletion lib/qstarscli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (

"github.com/QOSGroup/qmoon/lib/qstarscli/qstarsmock"
"github.com/google/go-querystring/query"
amino "github.com/tendermint/go-amino"
"github.com/sirupsen/logrus"
"github.com/tendermint/go-amino"
tmltypes "github.com/tendermint/tendermint/rpc/lib/types"

"net/http"
Expand Down Expand Up @@ -267,6 +268,14 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*htt
io.Copy(w, resp.Body)
} else {
var tmresp tmltypes.RPCResponse

//body, err := ioutil.ReadAll(resp.Body)
//logrus.WithField("req", req.RequestURI).WithField("body", string(body)).WithField("err", err).Debugln()
//if err != nil {
// return resp, err
//}
//fmt.Println(string(body))
//err = json.Unmarshal(body, &tmresp)
err = json.NewDecoder(resp.Body).Decode(&tmresp)
if err != nil {
if err == io.EOF {
Expand All @@ -281,6 +290,10 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*htt
}

err = c.cdc.UnmarshalJSON(tmresp.Result, v)
logrus.WithField("model", "qstarscli").
WithField("result", string(tmresp.Result)).
WithField("err", err).
Debugln()
if err != nil {
return resp, err
}
Expand Down
11 changes: 7 additions & 4 deletions lib/qstarscli/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/QOSGroup/qstars/x/bank"
"github.com/sirupsen/logrus"
)

const transferURI = "/accounts/{address}/send"
Expand All @@ -16,9 +17,9 @@ type TransferBody struct {
Amount string `json:"amount"`
PirvateKey string `json:"privatekey"`
ChainID string `json:"chain_id"`
AccountNumber int64 `json:"account_number"`
Sequence int64 `json:"sequence"`
Gas int64 `json:"gas"`
AccountNumber int64 `json:"-"`
Sequence int64 `json:"-"`
Gas int64 `json:"-"`
}

func (s *transferService) Send(ctx context.Context, body *TransferBody) (*bank.SendResult, error) {
Expand All @@ -29,12 +30,14 @@ func (s *transferService) Send(ctx context.Context, body *TransferBody) (*bank.S
}

req, err := s.client.NewRequest("POST", u, body)
logrus.WithField("module", "qstarscli").WithField("url", s.client.host+u).WithField("body", body).Debug()
if err != nil {
return nil, err
}

var res bank.SendResult
_, err = s.client.Do(ctx, req, &res)
resp, err := s.client.Do(ctx, req, &res)
logrus.WithField("module", "qstarscli").WithField("resp", resp).WithField("err", err).Debugln()
if err != nil {
return nil, err
}
Expand Down
4 changes: 4 additions & 0 deletions plugins/atm/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ func (ttp ATMPlugin) RegisterGin(r *gin.Engine) {
func (ttp ATMPlugin) Parse(blockHeader tmtypes.Header, itx qbasetxs.ITx) (typeName string, hit bool, err error) {
return "", false, nil
}

func (ttp ATMPlugin) Doctor() error {
return nil
}
48 changes: 28 additions & 20 deletions plugins/atm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ func getBank() string {
return os.Getenv("ATM_KEY")
}

func getChain() string {
return os.Getenv("ATM_CHAIN_ID")
}

func getAmount() int64 {
d := os.Getenv("ATM_AMOUNT")
if d != "" {
Expand All @@ -37,8 +33,7 @@ func getAmount() int64 {
}

func check(addr, chainid string) error {
now := time.Now()
t := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
t := utils.DayStart(time.Now())
_, err := AtmRecordByAddressChainidCreateat(db.Db, utils.NullString(addr), utils.NullString(chainid), utils.NullTime(t))
if err == nil {
return errors.New("今天已经领取")
Expand All @@ -58,17 +53,39 @@ func Withdraw(addr, chainid string) (*bank.SendResult, error) {
return nil, err
}

coin := "qos"
amount := getAmount()
sr, err := send(addr, chainid, fmt.Sprintf("%d%s", amount, coin))
if err != nil {
return nil, err
}

ar := &AtmRecord{}
ar.Address = utils.NullString(addr)
ar.Chainid = utils.NullString(chainid)
ar.Coin = utils.NullString(coin)
ar.Amount = utils.NullString(fmt.Sprintf("%d", amount))
ar.Createat = utils.NullTime(utils.DayStart(time.Now()))
ar.Height = utils.NullString(sr.Heigth)
ar.Hash = utils.NullString(sr.Hash)

if err := ar.Insert(db.Db); err != nil {
return nil, err
}

return sr, nil
}

func send(addr, chainid string, amount string) (*bank.SendResult, error) {
opt, err := qstarscli.NewOption(qstarscli.SetOptionHost(os.Getenv("Qstars")))
if err != nil {
return nil, err
}

coin := "QOS"
amount := getAmount()
qcli := qstarscli.NewClient(opt)
sr, err := qcli.TransferService.Send(nil, &qstarscli.TransferBody{
Address: addr,
Amount: fmt.Sprintf("%d%s", amount, coin),
Amount: amount,
PirvateKey: getBank(),
ChainID: chainid,
})
Expand All @@ -77,18 +94,9 @@ func Withdraw(addr, chainid string) (*bank.SendResult, error) {
return nil, err
}

if sr.Code != "0" {
if sr.Hash == "" {
return nil, errors.New(sr.Error)
}

ar := &AtmRecord{}
ar.Address = utils.NullString(addr)
ar.Chainid = utils.NullString(chainid)
ar.Coin = utils.NullString(coin)
ar.Amount = utils.NullString(fmt.Sprintf("%d", amount))
ar.Createat = utils.NullTime(time.Now())
ar.Height = utils.NullString(sr.Heigth)
ar.Hash = utils.NullString(sr.Hash)

return sr, ar.Insert(db.Db)
return sr, nil
}
52 changes: 52 additions & 0 deletions plugins/atm/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2018 The QOS Authors

package atm

import (
"encoding/json"
"fmt"
"testing"

"github.com/QOSGroup/qstars/x/bank"
"github.com/stretchr/testify/assert"
"github.com/tendermint/go-amino"
tmltypes "github.com/tendermint/tendermint/rpc/lib/types"
)

func TestSend(t *testing.T) {
//logrus.SetLevel(logrus.DebugLevel)
//logrus.SetFormatter(&logrus.JSONFormatter{})
//addr := "address1pcjs0t9m9vl7vejwttuc2fzfgnutndnrpyw08m"
//chainid := "capricorn-1000"
//coin := "qos"
//amount := getAmount()
//_, err := send(addr, chainid, fmt.Sprintf("%d%s", amount, coin))
//assert.Nil(t, err)
}

func TestJson(t *testing.T) {
body := `{
"jsonrpc": "2.0",
"id": "",
"result": {
"hash": "1CFC7C08C9E3CB6F8DF7E8668BEABDEA55022237",
"error": "",
"code": "-1",
"result": "BankStub",
"heigth": "374095"
}
}`

var tmresp tmltypes.RPCResponse
fmt.Println(string(body))
err := json.Unmarshal([]byte(body), &tmresp)
assert.Nil(t, err)

assert.True(t, tmresp.Error == nil)
var res bank.SendResult
var cdc = amino.NewCodec()
err = cdc.UnmarshalJSON(tmresp.Result, &res)
assert.Nil(t, err)
assert.Equal(t, "1CFC7C08C9E3CB6F8DF7E8668BEABDEA55022237", res.Hash)
assert.Equal(t, "374095", res.Heigth)
}
13 changes: 13 additions & 0 deletions plugins/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package plugins
import (
"database/sql"
"errors"
"fmt"

qbasetxs "github.com/QOSGroup/qbase/txs"
"github.com/QOSGroup/qmoon/plugins/atm"
Expand All @@ -21,6 +22,8 @@ type Pluginer interface {
Parse(blockHeader tmtypes.Header, itx qbasetxs.ITx) (typeName string, hit bool, err error)
Type() string

Doctor() error

RegisterGin(r *gin.Engine)
}

Expand Down Expand Up @@ -88,6 +91,16 @@ func Parse(blockHeader tmtypes.Header, itx qbasetxs.ITx) (name string, err error
return
}

func Doctor() error {
for _, tp := range tps {
if err := tp.Doctor(); err != nil {
return fmt.Errorf("Check %s fail, err:%s ", tp.Type(), err.Error())
}
}

return nil
}

/*
qbasetxs "github.com/QOSGroup/qbase/plugins"
Expand Down
4 changes: 4 additions & 0 deletions plugins/transfer/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func (ttp TxTransferPlugin) Type() string {
return "TxTransfer"
}

func (ttp TxTransferPlugin) Doctor() error {
return nil
}

func (ttp TxTransferPlugin) RegisterGin(r *gin.Engine) {
AccountTxsGinRegister(r)
}
Expand Down
55 changes: 55 additions & 0 deletions service/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2018 The QOS Authors

package service

import (
"errors"
"os"

"github.com/QOSGroup/qmoon/plugins"
)

type Env struct {
EmailSmtpServer string
EmailUser string
EmailPassword string
}

var env *Env

func GetEnv() Env {
if env == nil {
env = new(Env)
env.EmailSmtpServer = os.Getenv("MailSmtpServer")
env.EmailUser = os.Getenv("MailUser")
env.EmailPassword = os.Getenv("MailPassword")
}

return *env
}

func Doctor() error {
if err := CheckEnv(); err != nil {
return err
}

if err := plugins.Doctor(); err != nil {
return err
}

return nil
}

func CheckEnv() error {
if env.EmailPassword == "" {
return errors.New("MailSmtpServer 未设置")
}
if env.EmailUser == "" {
return errors.New("EmailUser 未设置")
}
if env.EmailPassword == "" {
return errors.New("EmailPassword 未设置")
}

return nil
}
2 changes: 1 addition & 1 deletion types/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func RPCForbiddenError(id string, err error) RPCResponse {
}

func RPCInternalError(id string, err error) RPCResponse {
return NewRPCErrorResponse(id, -32603, "Internal error", err.Error())
return NewRPCErrorResponse(id, -32603, err.Error(), "Internal error")
}

func RPCServerError(id string, err error) RPCResponse {
Expand Down
9 changes: 9 additions & 0 deletions utils/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright 2018 The QOS Authors

package utils

import "time"

func DayStart(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
}

0 comments on commit 29d2f51

Please sign in to comment.