From 29d2f51dc625d40db16641108f9335d403eec673 Mon Sep 17 00:00:00 2001 From: xutao Date: Wed, 26 Dec 2018 13:55:50 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E9=A2=86=E9=92=B1=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/commands/doctor.go | 36 ++++++++++++++++++++++++ lib/qstarscli/client.go | 15 +++++++++- lib/qstarscli/transfer.go | 11 +++++--- plugins/atm/process.go | 4 +++ plugins/atm/service.go | 48 ++++++++++++++++++-------------- plugins/atm/service_test.go | 52 +++++++++++++++++++++++++++++++++++ plugins/plugin.go | 13 +++++++++ plugins/transfer/process.go | 4 +++ service/env.go | 55 +++++++++++++++++++++++++++++++++++++ types/rpc.go | 2 +- utils/time.go | 9 ++++++ 11 files changed, 223 insertions(+), 26 deletions(-) create mode 100644 cmd/commands/doctor.go create mode 100644 plugins/atm/service_test.go create mode 100644 service/env.go create mode 100644 utils/time.go diff --git a/cmd/commands/doctor.go b/cmd/commands/doctor.go new file mode 100644 index 0000000..18ead34 --- /dev/null +++ b/cmd/commands/doctor.go @@ -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 +} diff --git a/lib/qstarscli/client.go b/lib/qstarscli/client.go index 16bb29a..ad849d8 100644 --- a/lib/qstarscli/client.go +++ b/lib/qstarscli/client.go @@ -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" @@ -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 { @@ -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 } diff --git a/lib/qstarscli/transfer.go b/lib/qstarscli/transfer.go index a96f6a2..775c72a 100644 --- a/lib/qstarscli/transfer.go +++ b/lib/qstarscli/transfer.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/QOSGroup/qstars/x/bank" + "github.com/sirupsen/logrus" ) const transferURI = "/accounts/{address}/send" @@ -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) { @@ -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 } diff --git a/plugins/atm/process.go b/plugins/atm/process.go index 4a448ed..5ca4cba 100644 --- a/plugins/atm/process.go +++ b/plugins/atm/process.go @@ -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 +} diff --git a/plugins/atm/service.go b/plugins/atm/service.go index 9fb10ce..3d84f19 100644 --- a/plugins/atm/service.go +++ b/plugins/atm/service.go @@ -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 != "" { @@ -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("今天已经领取") @@ -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, }) @@ -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 } diff --git a/plugins/atm/service_test.go b/plugins/atm/service_test.go new file mode 100644 index 0000000..ca7fc80 --- /dev/null +++ b/plugins/atm/service_test.go @@ -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) +} diff --git a/plugins/plugin.go b/plugins/plugin.go index 2aa4f9d..d54a64f 100644 --- a/plugins/plugin.go +++ b/plugins/plugin.go @@ -5,6 +5,7 @@ package plugins import ( "database/sql" "errors" + "fmt" qbasetxs "github.com/QOSGroup/qbase/txs" "github.com/QOSGroup/qmoon/plugins/atm" @@ -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) } @@ -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" diff --git a/plugins/transfer/process.go b/plugins/transfer/process.go index fc89772..db722c3 100644 --- a/plugins/transfer/process.go +++ b/plugins/transfer/process.go @@ -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) } diff --git a/service/env.go b/service/env.go new file mode 100644 index 0000000..1bf2f3d --- /dev/null +++ b/service/env.go @@ -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 +} diff --git a/types/rpc.go b/types/rpc.go index ad97dbd..dc4fc88 100644 --- a/types/rpc.go +++ b/types/rpc.go @@ -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 { diff --git a/utils/time.go b/utils/time.go new file mode 100644 index 0000000..9287e27 --- /dev/null +++ b/utils/time.go @@ -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) +}