Commit 80aedb4e authored by lihailei's avatar lihailei

封装jsonClient,方便调用

parent c031c95f
...@@ -9,8 +9,8 @@ import ( ...@@ -9,8 +9,8 @@ import (
"gitlab.33.cn/chain33/chain33/account" "gitlab.33.cn/chain33/chain33/account"
common "gitlab.33.cn/chain33/chain33/common" common "gitlab.33.cn/chain33/chain33/common"
crypto "gitlab.33.cn/chain33/chain33/common/crypto" crypto "gitlab.33.cn/chain33/chain33/common/crypto"
jsonrpc "gitlab.33.cn/chain33/chain33/rpc"
"gitlab.33.cn/chain33/chain33/types" "gitlab.33.cn/chain33/chain33/types"
jsonrpc "gitlab.33.cn/lihailei/chain33_sdk/src/common/rpc"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
......
package rpc
import (
"bytes"
"encoding/json"
"fmt"
log "github.com/inconshreveable/log15"
"io/ioutil"
"net/http"
)
var (
jlog = log.New("module", "common")
)
const JSON_RPC_VERSION = "2.0"
type JSONClient struct {
url string
}
func NewJSONClient(url string) (*JSONClient, error) {
return &JSONClient{url}, nil
}
type clientRequest struct {
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Params [1]interface{} `json:"params"`
Id uint64 `json:"id"`
}
type clientResponse struct {
Id uint64 `json:"id"`
Result *json.RawMessage `json:"result"`
Error interface{} `json:"error"`
}
func (client *JSONClient) Call(method string, params, resp interface{}) error {
req := &clientRequest{}
req.Jsonrpc = JSON_RPC_VERSION
req.Method = method
req.Params[0] = params
data, err := json.Marshal(req)
if err != nil {
return err
}
//poststr := fmt.Sprintf(`{"jsonrpc":"2.0","id":2,"method":"Chain33.SendTransaction","params":[{"data":"%v"}]}`,
// common.ToHex(types.Encode(tx)))
jlog.Debug("request JsonStr", string(data), "")
postresp, err := http.Post(client.url, "application/json", bytes.NewBuffer(data))
if err != nil {
return err
}
defer postresp.Body.Close()
b, err := ioutil.ReadAll(postresp.Body)
if err != nil {
return err
}
log.Debug("response", string(b), "")
cresp := &clientResponse{}
err = json.Unmarshal(b, &cresp)
if err != nil {
return err
}
if cresp.Error != nil || cresp.Result == nil {
x, ok := cresp.Error.(string)
if !ok {
return fmt.Errorf("invalid error %v", cresp.Error)
}
if x == "" {
x = "unspecified error"
}
return fmt.Errorf(x)
}
return json.Unmarshal(*cresp.Result, resp)
}
package rpc
import (
"encoding/json"
)
type userWrite struct {
Topic string `json:"topic"`
Content string `json:"content"`
}
type TransParm struct {
Execer string `json:"execer"`
Payload string `json:"payload"`
Signature *Signature `json:"signature"`
Fee int64 `json:"fee"`
}
type SignedTx struct {
Unsign string `json:"unsignTx"`
Sign string `json:"sign"`
Pubkey string `json:"pubkey"`
Ty int32 `json:"ty"`
}
type RawParm struct {
Data string `json:"data"`
}
type QueryParm struct {
Hash string `json:"hash"`
}
type BlockParam struct {
Start int64 `json:"start"`
End int64 `json:"end"`
Isdetail bool `json:"isDetail"`
}
type Header struct {
Version int64 `json:"version"`
ParentHash string `json:"parentHash"`
TxHash string `json:"txHash"`
StateHash string `json:"stateHash"`
Height int64 `json:"height"`
BlockTime int64 `json:"blockTime"`
TxCount int64 `json:"txCount"`
Hash string `json:"hash"`
}
type Signature struct {
Ty int32 `json:"ty"`
Pubkey string `json:"pubkey"`
Signature string `json:"signature"`
}
type Transaction struct {
Execer string `json:"execer"`
Payload interface{} `json:"payload"`
RawPayload string `json:"rawPayload"`
Signature *Signature `json:"signature"`
Fee int64 `json:"fee"`
Expire int64 `json:"expire"`
Nonce int64 `json:"nonce"`
From string `json:"from,omitempty"`
To string `json:"to"`
Amount int64 `json:"amount,omitempty"`
}
type ReceiptLog struct {
Ty int32 `json:"ty"`
Log string `json:"log"`
}
type ReceiptData struct {
Ty int32 `json:"ty"`
Logs []*ReceiptLog `json:"logs"`
}
type ReceiptDataResult struct {
Ty int32 `json:"ty"`
TyName string `json:"tyName"`
Logs []*ReceiptLogResult `json:"logs"`
}
type ReceiptLogResult struct {
Ty int32 `json:"ty"`
TyName string `json:"tyName"`
Log interface{} `json:"log"`
RawLog string `json:"rawLog"`
}
type Block struct {
Version int64 `json:"version"`
ParentHash string `json:"parentHash"`
TxHash string `json:"txHash"`
StateHash string `json:"stateHash"`
Height int64 `json:"height"`
BlockTime int64 `json:"blockTime"`
Txs []*Transaction `json:"txs"`
}
type BlockDetail struct {
Block *Block `json:"block"`
Receipts []*ReceiptDataResult `json:"recipts"`
}
type BlockDetails struct {
Items []*BlockDetail `json:"items"`
}
type TransactionDetail struct {
Tx *Transaction `json:"tx"`
Receipt *ReceiptDataResult `json:"receipt"`
Proofs []string `json:"proofs"`
Height int64 `json:"height"`
Index int64 `json:"index"`
Blocktime int64 `json:"blockTime"`
Amount int64 `json:"amount"`
Fromaddr string `json:"fromAddr"`
ActionName string `json:"actionName"`
}
type ReplyTxInfos struct {
TxInfos []*ReplyTxInfo `protobuf:"bytes,1,rep,name=txInfos" json:"txInfos"`
}
type ReplyTxInfo struct {
Hash string `json:"hash"`
Height int64 `json:"height"`
Index int64 `json:"index"`
}
type TransactionDetails struct {
//Txs []*Transaction `json:"txs"`
Txs []*TransactionDetail `protobuf:"bytes,1,rep,name=txs" json:"txs"`
}
type ReplyTxList struct {
Txs []*Transaction `json:"txs"`
}
type ReplyHash struct {
Hash string `json:"hash"`
}
type ReplyHashes struct {
Hashes []string `json:"hashes"`
}
type PeerList struct {
Peers []*Peer `json:"peers"`
}
type Peer struct {
Addr string `json:"addr"`
Port int32 `json:"port"`
Name string `json:"name"`
MempoolSize int32 `json:"mempoolSize"`
Self bool `json:"self"`
Header *Header `json:"header"`
}
// Wallet Module
type WalletAccounts struct {
Wallets []*WalletAccount `protobuf:"bytes,1,rep,name=wallets" json:"wallets"`
}
type WalletAccount struct {
Acc *Account `protobuf:"bytes,1,opt,name=acc" json:"acc"`
Label string `protobuf:"bytes,2,opt,name=label" json:"label"`
}
type Account struct {
Currency int32 `protobuf:"varint,1,opt,name=currency" json:"currency"`
Balance int64 `protobuf:"varint,2,opt,name=balance" json:"balance"`
Frozen int64 `protobuf:"varint,3,opt,name=frozen" json:"frozen"`
Addr string `protobuf:"bytes,4,opt,name=addr" json:"addr"`
}
type Reply struct {
IsOk bool `protobuf:"varint,1,opt,name=isOk" json:"isOK"`
Msg string `protobuf:"bytes,2,opt,name=msg,proto3" json:"msg"`
}
type Headers struct {
Items []*Header `protobuf:"bytes,1,rep,name=items" json:"items"`
}
type ReqAddr struct {
Addr string `json:"addr"`
}
type ReqHashes struct {
Hashes []string `json:"hashes"`
DisableDetail bool `json:"disableDetail"`
}
type ReqWalletTransactionList struct {
FromTx string `json:"fromTx"`
Count int32 `json:"count"`
Direction int32 `json:"direction"`
}
type WalletTxDetails struct {
TxDetails []*WalletTxDetail `protobuf:"bytes,1,rep,name=txDetails" json:"txDetails"`
}
type WalletTxDetail struct {
Tx *Transaction `protobuf:"bytes,1,opt,name=tx" json:"tx"`
Receipt *ReceiptDataResult `protobuf:"bytes,2,opt,name=receipt" json:"receipt"`
Height int64 `protobuf:"varint,3,opt,name=height" json:"height"`
Index int64 `protobuf:"varint,4,opt,name=index" json:"index"`
BlockTime int64 `json:"blockTime"`
Amount int64 `json:"amount"`
FromAddr string `json:"fromAddr"`
TxHash string `json:"txHash"`
ActionName string `json:"actionName"`
}
type BlockOverview struct {
Head *Header `protobuf:"bytes,1,opt,name=head" json:"head"`
TxCount int64 `protobuf:"varint,2,opt,name=txCount" json:"txCount"`
TxHashes []string `protobuf:"bytes,3,rep,name=txHashes,proto3" json:"txHashes"`
}
type Query4Cli struct {
Execer string `protobuf:"bytes,1,opt,name=execer,proto3" json:"execer"`
FuncName string `protobuf:"bytes,2,opt,name=funcName" json:"funcName"`
Payload interface{} `protobuf:"bytes,3,opt,name=payload" json:"payload"`
}
type Query4Jrpc struct {
Execer string `protobuf:"bytes,1,opt,name=execer,proto3" json:"execer"`
FuncName string `protobuf:"bytes,2,opt,name=funcName" json:"funcName"`
Payload json.RawMessage `protobuf:"bytes,3,opt,name=payload" json:"payload"`
}
type WalletStatus struct {
IsWalletLock bool `json:"isWalletLock"`
IsAutoMining bool `json:"isAutoMining"`
IsHasSeed bool `json:"isHasSeed"`
IsTicketLock bool `json:"isTicketLock"`
}
// Token Transaction
type TokenPreCreateTx struct {
Price int64 `json:"price"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Introduction string `json:"introduction"`
OwnerAddr string `json:"ownerAddr"`
Total int64 `json:"total"`
Fee int64 `json:"fee"`
}
type TokenFinishTx struct {
OwnerAddr string `json:"ownerAddr"`
Symbol string `json:"symbol"`
Fee int64 `json:"fee"`
}
type TokenRevokeTx struct {
OwnerAddr string `json:"ownerAddr"`
Symbol string `json:"symbol"`
Fee int64 `json:"fee"`
}
// Trade Transaction
type TradeSellTx struct {
TokenSymbol string `json:"tokenSymbol"`
AmountPerBoardlot int64 `json:"amountPerBoardlot"`
MinBoardlot int64 `json:"minBoardlot"`
PricePerBoardlot int64 `json:"pricePerBoardlot"`
TotalBoardlot int64 `json:"totalBoardlot"`
Fee int64 `json:"fee"`
}
type TradeBuyTx struct {
SellID string `json:"sellID"`
BoardlotCnt int64 `json:"boardlotCnt"`
Fee int64 `json:"fee"`
}
type TradeRevokeTx struct {
SellID string `json:"sellID,"`
Fee int64 `json:"fee"`
}
type TradeBuyLimitTx struct {
TokenSymbol string `json:"tokenSymbol"`
AmountPerBoardlot int64 `json:"amountPerBoardlot"`
MinBoardlot int64 `json:"minBoardlot"`
PricePerBoardlot int64 `json:"pricePerBoardlot"`
TotalBoardlot int64 `json:"totalBoardlot"`
Fee int64 `json:"fee"`
}
type TradeSellMarketTx struct {
BuyID string `json:"buyID"`
BoardlotCnt int64 `json:"boardlotCnt"`
Fee int64 `json:"fee"`
}
type TradeRevokeBuyTx struct {
BuyID string `json:"buyID,"`
Fee int64 `json:"fee"`
}
type NodeNetinfo struct {
Externaladdr string `json:"externalAddr"`
Localaddr string `json:"localAddr"`
Service bool `json:"service"`
Outbounds int32 `json:"outbounds"`
Inbounds int32 `json:"inbounds"`
}
...@@ -4,9 +4,9 @@ import ( ...@@ -4,9 +4,9 @@ import (
"fmt" "fmt"
"github.com/astaxie/beego" "github.com/astaxie/beego"
log "github.com/inconshreveable/log15" log "github.com/inconshreveable/log15"
jsonrpc "gitlab.33.cn/chain33/chain33/rpc"
"gitlab.33.cn/chain33/chain33/types" "gitlab.33.cn/chain33/chain33/types"
. "gitlab.33.cn/lihailei/chain33_sdk/src/common" . "gitlab.33.cn/lihailei/chain33_sdk/src/common"
jsonrpc "gitlab.33.cn/lihailei/chain33_sdk/src/common/rpc"
. "gitlab.33.cn/lihailei/chain33_sdk/src/models" . "gitlab.33.cn/lihailei/chain33_sdk/src/models"
) )
......
...@@ -2,9 +2,9 @@ package controllers ...@@ -2,9 +2,9 @@ package controllers
import ( import (
"github.com/astaxie/beego" "github.com/astaxie/beego"
jsonrpc "gitlab.33.cn/chain33/chain33/rpc"
"gitlab.33.cn/chain33/chain33/types" "gitlab.33.cn/chain33/chain33/types"
. "gitlab.33.cn/lihailei/chain33_sdk/src/common" . "gitlab.33.cn/lihailei/chain33_sdk/src/common"
jsonrpc "gitlab.33.cn/lihailei/chain33_sdk/src/common/rpc"
. "gitlab.33.cn/lihailei/chain33_sdk/src/models" . "gitlab.33.cn/lihailei/chain33_sdk/src/models"
) )
......
...@@ -3,10 +3,11 @@ package controllers ...@@ -3,10 +3,11 @@ package controllers
import ( import (
"github.com/astaxie/beego" "github.com/astaxie/beego"
jsonrpc "gitlab.33.cn/chain33/chain33/rpc" . "gitlab.33.cn/chain33/chain33/common"
"gitlab.33.cn/chain33/chain33/types" "gitlab.33.cn/chain33/chain33/types"
. "gitlab.33.cn/lihailei/chain33_sdk/src/common" . "gitlab.33.cn/lihailei/chain33_sdk/src/common"
. "gitlab.33.cn/lihailei/chain33_sdk/src/common/crypto" . "gitlab.33.cn/lihailei/chain33_sdk/src/common/crypto"
jsonrpc "gitlab.33.cn/lihailei/chain33_sdk/src/common/rpc"
"encoding/json" "encoding/json"
...@@ -57,7 +58,13 @@ func (c *ContractController) Invoke() { ...@@ -57,7 +58,13 @@ func (c *ContractController) Invoke() {
addr, priv := Genaddress() addr, priv := Genaddress()
tx := &types.Transaction{Execer: []byte("user.blacklist"), Payload: types.Encode(action), Fee: 1e6, To: addr} tx := &types.Transaction{Execer: []byte("user.blacklist"), Payload: types.Encode(action), Fee: 1e6, To: addr}
tx.Sign(types.SECP256K1, priv) tx.Sign(types.SECP256K1, priv)
res, err := SendTransaction(tx) //res, err := SendTransaction(tx)
data := ToHex(types.Encode(tx))
params := jsonrpc.RawParm{
Data: data,
}
ctx := NewRpcCtx(GetJrpcURL(), "Chain33.SendTransaction", params, nil)
res, err := ctx.RunWithoutMarshal()
if err != nil { if err != nil {
c.Data["json"] = err.Error() c.Data["json"] = err.Error()
c.ServeJSON() c.ServeJSON()
...@@ -103,7 +110,13 @@ func (c *ContractController) Invoke() { ...@@ -103,7 +110,13 @@ func (c *ContractController) Invoke() {
addr, priv := Genaddress() addr, priv := Genaddress()
tx := &types.Transaction{Execer: []byte("user.blacklist"), Payload: types.Encode(action), Fee: 1e6, To: addr} tx := &types.Transaction{Execer: []byte("user.blacklist"), Payload: types.Encode(action), Fee: 1e6, To: addr}
tx.Sign(types.SECP256K1, priv) tx.Sign(types.SECP256K1, priv)
res, err := SendTransaction(tx) //res, err := SendTransaction(tx)
data := ToHex(types.Encode(tx))
params := jsonrpc.RawParm{
Data: data,
}
ctx := NewRpcCtx(GetJrpcURL(), "Chain33.SendTransaction", params, nil)
res, err := ctx.RunWithoutMarshal()
if err != nil { if err != nil {
c.Data["json"] = err.Error() c.Data["json"] = err.Error()
c.ServeJSON() c.ServeJSON()
......
...@@ -2,8 +2,8 @@ package controllers ...@@ -2,8 +2,8 @@ package controllers
import ( import (
"github.com/astaxie/beego" "github.com/astaxie/beego"
jsonrpc "gitlab.33.cn/chain33/chain33/rpc"
. "gitlab.33.cn/lihailei/chain33_sdk/src/common" . "gitlab.33.cn/lihailei/chain33_sdk/src/common"
jsonrpc "gitlab.33.cn/lihailei/chain33_sdk/src/common/rpc"
) )
// PeerController operations for Peer // PeerController operations for Peer
......
...@@ -2,11 +2,10 @@ package controllers ...@@ -2,11 +2,10 @@ package controllers
import ( import (
"github.com/astaxie/beego" "github.com/astaxie/beego"
"gitlab.33.cn/chain33/chain33/account"
. "gitlab.33.cn/chain33/chain33/common" . "gitlab.33.cn/chain33/chain33/common"
jsonrpc "gitlab.33.cn/chain33/chain33/rpc"
"gitlab.33.cn/chain33/chain33/types" "gitlab.33.cn/chain33/chain33/types"
. "gitlab.33.cn/lihailei/chain33_sdk/src/common" . "gitlab.33.cn/lihailei/chain33_sdk/src/common"
jsonrpc "gitlab.33.cn/lihailei/chain33_sdk/src/common/rpc"
. "gitlab.33.cn/lihailei/chain33_sdk/src/models" . "gitlab.33.cn/lihailei/chain33_sdk/src/models"
r "math/rand" r "math/rand"
) )
...@@ -125,16 +124,17 @@ func queryByHash(rpcAddr, txHash string) ([]byte, error) { ...@@ -125,16 +124,17 @@ func queryByHash(rpcAddr, txHash string) ([]byte, error) {
// @router /sendTX [post] // @router /sendTX [post]
func (c *TxController) SendTx() { func (c *TxController) SendTx() {
execer := c.GetString("execerName") execer := c.GetString("execerName")
privKey := c.GetString("privateKey") //privKey := c.GetString("privateKey")
body := c.Ctx.Input.RequestBody body := c.Ctx.Input.RequestBody
if execer == "" || privKey == "" || len(body) == 0 { if execer == "" || len(body) == 0 {
c.Ctx.ResponseWriter.WriteHeader(400) c.Ctx.ResponseWriter.WriteHeader(400)
return return
} }
addr, priv := Genaddress()
tx := &types.Transaction{Execer: []byte(execer), Payload: body, Fee: 1e6} tx := &types.Transaction{Execer: []byte(execer), Payload: body, Fee: 1e6}
tx.Nonce = r.Int63() tx.Nonce = r.Int63()
tx.Sign(types.SECP256K1, Getprivkey(privKey)) tx.To = addr
tx.To = account.ExecAddress(execer).String() tx.Sign(types.SECP256K1, priv)
data := ToHex(types.Encode(tx)) data := ToHex(types.Encode(tx))
params := jsonrpc.RawParm{ params := jsonrpc.RawParm{
Data: data, Data: data,
......
...@@ -3,9 +3,9 @@ package controllers ...@@ -3,9 +3,9 @@ package controllers
import ( import (
"fmt" "fmt"
"github.com/astaxie/beego" "github.com/astaxie/beego"
jsonrpc "gitlab.33.cn/chain33/chain33/rpc"
"gitlab.33.cn/chain33/chain33/types" "gitlab.33.cn/chain33/chain33/types"
. "gitlab.33.cn/lihailei/chain33_sdk/src/common" . "gitlab.33.cn/lihailei/chain33_sdk/src/common"
jsonrpc "gitlab.33.cn/lihailei/chain33_sdk/src/common/rpc"
. "gitlab.33.cn/lihailei/chain33_sdk/src/models" . "gitlab.33.cn/lihailei/chain33_sdk/src/models"
"strconv" "strconv"
) )
......
...@@ -5,7 +5,6 @@ import ( ...@@ -5,7 +5,6 @@ import (
"strconv" "strconv"
log "github.com/inconshreveable/log15" log "github.com/inconshreveable/log15"
bl "gitlab.33.cn/chain33/chain33/executor/drivers/blacklist/types"
jsonrpc "gitlab.33.cn/chain33/chain33/rpc" jsonrpc "gitlab.33.cn/chain33/chain33/rpc"
"gitlab.33.cn/chain33/chain33/types" "gitlab.33.cn/chain33/chain33/types"
) )
...@@ -384,8 +383,3 @@ func ParseGetBalanceRes(arg interface{}) (interface{}, error) { ...@@ -384,8 +383,3 @@ func ParseGetBalanceRes(arg interface{}) (interface{}, error) {
} }
return result, nil return result, nil
} }
func ParseBlacklistOrg(res interface{}) (interface{}, error) {
var result bl.Org
return result, nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment