Commit b76314a6 authored by lihailei's avatar lihailei

Add comment routing.

parent 473996bd
...@@ -2,6 +2,6 @@ appname = chain33_sdk ...@@ -2,6 +2,6 @@ appname = chain33_sdk
httpport = 8080 httpport = 8080
runmode = dev runmode = dev
autorender = false autorender = false
copyrequestbody = false copyrequestbody = true
EnableDocs = true EnableDocs = true
jrpcAddr="http://localhost:8801" jrpcAddr="http://localhost:8801"
...@@ -18,23 +18,24 @@ type BlockController struct { ...@@ -18,23 +18,24 @@ type BlockController struct {
// URLMapping ... // URLMapping ...
func (c *BlockController) URLMapping() { func (c *BlockController) URLMapping() {
//View last block header //View last block header
c.Mapping("getLastHeader", c.getLastHeader) c.Mapping("getLastHeader", c.GetLastHeader)
//Get hash of block at height //Get hash of block at height
c.Mapping("getBlockHashByHeight", c.getBlockHash) c.Mapping("getBlockHashByHeight", c.GetBlockHashByHeight)
//View block info by block hash //View block info by block hash
c.Mapping("getBlockViewByHash", c.getBlockViewByHash) c.Mapping("getBlockViewByHash", c.GetBlockViewByHash)
//Get block headers between [start, end] //Get block headers between [start, end]
c.Mapping("getBlockHeaders", c.getBlockHeaders) c.Mapping("getBlockHeaders", c.GetBlockHeaders)
// Get block headers between [start, end] // Get block headers between [start, end]
c.Mapping("getBlockList", c.getBlockList) c.Mapping("getBlockList", c.GetBlockList)
} }
//getLastHeader... //getLastHeader...
//@Title getLastHeader //@Title getLastHeader
//@Description getLastHeader //@Description getLastHeader
//@Success 200 {block} //@Success 200 执行成功,返回block信息
//@Failure 403 : //@Failure 403 Forbidden禁止执行
//@router [get] //@Failure 400 传入参数有误
func (c *BlockController) getLastHeader() { //@router /getLastHeader [get]
func (c *BlockController) GetLastHeader() {
var res jsonrpc.Header var res jsonrpc.Header
ctx := NewRpcCtx(GetJrpcURL(), "Chain33.GetLastHeader", nil, &res) ctx := NewRpcCtx(GetJrpcURL(), "Chain33.GetLastHeader", nil, &res)
data, err := ctx.ReplyData() data, err := ctx.ReplyData()
...@@ -45,13 +46,15 @@ func (c *BlockController) getLastHeader() { ...@@ -45,13 +46,15 @@ func (c *BlockController) getLastHeader() {
c.Ctx.ResponseWriter.Write(data) c.Ctx.ResponseWriter.Write(data)
} }
} }
//getLastHeader... //getBlockHashByHeight...
//@Title getLastHeader //@Title getBlockHashByHeight
//@Description getLastHeader //@Description getBlockHashByHeight
//@Success 200 {block} //@Param height query int false "height"
//@Failure 400,403: //@Success 200 执行成功返回blockhash
//@Failure 403 Forbidden禁止执行
//@Failure 400 传入参数有误
//@router /getBlockHashByHeight [get] //@router /getBlockHashByHeight [get]
func (c *BlockController) getBlockHash() { func (c *BlockController) GetBlockHashByHeight() {
var res jsonrpc.ReplyHash var res jsonrpc.ReplyHash
height := c.Ctx.Request.Header.Get("height") height := c.Ctx.Request.Header.Get("height")
h, err := strconv.ParseInt(height, 10, 64) h, err := strconv.ParseInt(height, 10, 64)
...@@ -74,10 +77,12 @@ func (c *BlockController) getBlockHash() { ...@@ -74,10 +77,12 @@ func (c *BlockController) getBlockHash() {
//getblockViewByHash... //getblockViewByHash...
//@Title getblockViewByHash //@Title getblockViewByHash
//@Description getblockViewByHash //@Description getblockViewByHash
//@Success 200 {blockView} //@Success 200 执行成功返回blockView
//@Failure 400,403 : //@Param blockHash query string "blockHash"
//@router /getblockViewByHash [get] //@Failure 403 Forbidden禁止执行
func (c *BlockController) getBlockViewByHash() { //@Failure 400 传入参数有误
//@router /getBlockViewByHash [get]
func (c *BlockController) GetBlockViewByHash() {
blockHash := c.Ctx.Request.Header.Get("blockHash") blockHash := c.Ctx.Request.Header.Get("blockHash")
if blockHash == "" { if blockHash == "" {
c.Ctx.ResponseWriter.WriteHeader(400) c.Ctx.ResponseWriter.WriteHeader(400)
...@@ -99,10 +104,14 @@ func (c *BlockController) getBlockViewByHash() { ...@@ -99,10 +104,14 @@ func (c *BlockController) getBlockViewByHash() {
//getblockHeaders... //getblockHeaders...
//@Title getblockHeaders //@Title getblockHeaders
//@Description getblockHeaders //@Description getblockHeaders
//@Success 200 {blockHeader} //@Param startH query int false "startH"
//@Failure 400,403 : //@Param endH query int false "endH"
//@Param detail query bool false "detail"
//@Success 200 执行成功,返回blockHeader
//@Failure 403 Forbidden禁止执行
//@Failure 400 传入参数有误
//@router /getBlockHeaders [get] //@router /getBlockHeaders [get]
func (c *BlockController) getBlockHeaders() { func (c *BlockController) GetBlockHeaders() {
startH, errS := strconv.ParseInt(c.Ctx.Request.Header.Get("startH"), 10, 64) startH, errS := strconv.ParseInt(c.Ctx.Request.Header.Get("startH"), 10, 64)
endH, errE := strconv.ParseInt(c.Ctx.Request.Header.Get("endH"), 10, 64) endH, errE := strconv.ParseInt(c.Ctx.Request.Header.Get("endH"), 10, 64)
isDetail, errD := strconv.ParseBool(c.Ctx.Request.Header.Get("detail")) isDetail, errD := strconv.ParseBool(c.Ctx.Request.Header.Get("detail"))
...@@ -128,10 +137,14 @@ func (c *BlockController) getBlockHeaders() { ...@@ -128,10 +137,14 @@ func (c *BlockController) getBlockHeaders() {
//getblockList... //getblockList...
//@Title getblockList //@Title getblockList
//@Description getblockList //@Description getblockList
//@Success 200 {blockList} //@Param startH query int false "startH"
//@Failure 400,403 : //@Param endH query int false "endH"
//@Param detail query bool false "detail"
//@Success 200 执行成功,返回blockList
//@Failure 403 Forbidden禁止执行
//@Failure 400 传入参数有误
//@router /getBlockList [get] //@router /getBlockList [get]
func (c *BlockController) getBlockList() { func (c *BlockController) GetBlockList() {
startH, errS := strconv.ParseInt(c.Ctx.Request.Header.Get("startH"), 10, 64) startH, errS := strconv.ParseInt(c.Ctx.Request.Header.Get("startH"), 10, 64)
endH, errE := strconv.ParseInt(c.Ctx.Request.Header.Get("endH"), 10, 64) endH, errE := strconv.ParseInt(c.Ctx.Request.Header.Get("endH"), 10, 64)
......
...@@ -16,11 +16,11 @@ func (c *PeerController) URLMapping() { ...@@ -16,11 +16,11 @@ func (c *PeerController) URLMapping() {
c.Mapping("getPeers", c.GetPeers) c.Mapping("getPeers", c.GetPeers)
} }
//GetOne ...
//@Title GetPeers //@Title GetPeers
//@Description get Peers //@Description get Peers
//@Success 200 {object} models.Peer //@Success 200 执行成功,返回节点信息
//@Failure 403 : //@Failure 403 Forbidden禁止执行
//@router /getPeers [get] //@router /getPeers [get]
func (c *PeerController) GetPeers() { func (c *PeerController) GetPeers() {
var res jsonrpc.PeerList var res jsonrpc.PeerList
......
...@@ -24,9 +24,12 @@ func (c *TxController) URLMapping() { ...@@ -24,9 +24,12 @@ func (c *TxController) URLMapping() {
} }
//queryTxByHash... //queryTxByHash...
//@Title queryTxByHash //@Title queryTxByHash
//@Param hash query string false "hash"
//@Param isRawHex query bool false "isRawHex"
//@Description queryTxByHash //@Description queryTxByHash
//@Success 200 {TX} //@Success 200 执行成功,返回交易信息
//@Failure 400,403 : //@Failure 403 Forbidden禁止执行
//@Failure 400 传入参数有误
//@router /queryTxByHash [get] //@router /queryTxByHash [get]
func (c *TxController) QueryTxByHash() { func (c *TxController) QueryTxByHash() {
hash := c.Ctx.Request.Header.Get("hash") hash := c.Ctx.Request.Header.Get("hash")
...@@ -52,10 +55,17 @@ func (c *TxController) QueryTxByHash() { ...@@ -52,10 +55,17 @@ func (c *TxController) QueryTxByHash() {
//queryTxByAddr... //queryTxByAddr...
//@Title queryTxByAddr //@Title queryTxByAddr
//@Description queryTxByAddr //@Description queryTxByAddr 把参数以json格式,送给body体进行传入
//@Success 200 {TX} //@Param Addr query string false "addr"
//@Failure 400,403 : //@Param Flag query bool false "flag"
//@router /queryTxByAddr [get] //@Param Count query int false "count"
//@Param Direction query string false direction"
//@Param Height query int false "height"
//@Param Index query int false "index"
//@Success 200 执行成功返回交易信息
//@Failure 403 Forbidden禁止执行
//@Failure 400 传入参数有误
// @router /queryTxByAddr [get]
func (c *TxController) QueryTxByAddr() { func (c *TxController) QueryTxByAddr() {
var params types.ReqAddr var params types.ReqAddr
body,_:= ioutil.ReadAll(c.Ctx.Request.Body) body,_:= ioutil.ReadAll(c.Ctx.Request.Body)
......
package models
import (
"fmt"
"strconv"
jsonrpc "gitlab.33.cn/chain33/chain33/rpc"
"gitlab.33.cn/chain33/chain33/types"
)
type AccountsResult struct {
Wallets []*WalletResult `json:"wallets"`
}
type WalletResult struct {
Acc *AccountResult `json:"acc,omitempty"`
Label string `json:"label,omitempty"`
}
type AccountResult struct {
Currency int32 `json:"currency,omitempty"`
Balance string `json:"balance,omitempty"`
Frozen string `json:"frozen,omitempty"`
Addr string `json:"addr,omitempty"`
}
type TokenAccountResult struct {
Token string `json:"Token,omitempty"`
Currency int32 `json:"currency,omitempty"`
Balance string `json:"balance,omitempty"`
Frozen string `json:"frozen,omitempty"`
Addr string `json:"addr,omitempty"`
}
type TxListResult struct {
Txs []*TxResult `json:"txs"`
}
type TxResult struct {
Execer string `json:"execer"`
Payload interface{} `json:"payload"`
RawPayload string `json:"rawpayload"`
Signature *jsonrpc.Signature `json:"signature"`
Fee string `json:"fee"`
Expire int64 `json:"expire"`
Nonce int64 `json:"nonce"`
To string `json:"to"`
Amount string `json:"amount,omitempty"`
From string `json:"from,omitempty"`
}
type ReceiptData struct {
Ty int32 `json:"ty"`
TyName string `json:"tyname"`
Logs []*ReceiptLog `json:"logs"`
}
type ReceiptLog struct {
Ty int32 `json:"ty"`
TyName string `json:"tyname"`
Log interface{} `json:"log"`
RawLog string `json:"rawlog"`
}
type ReceiptAccountTransfer struct {
Prev *AccountResult `protobuf:"bytes,1,opt,name=prev" json:"prev,omitempty"`
Current *AccountResult `protobuf:"bytes,2,opt,name=current" json:"current,omitempty"`
}
type ReceiptExecAccountTransfer struct {
ExecAddr string `protobuf:"bytes,1,opt,name=execAddr" json:"execAddr,omitempty"`
Prev *AccountResult `protobuf:"bytes,2,opt,name=prev" json:"prev,omitempty"`
Current *AccountResult `protobuf:"bytes,3,opt,name=current" json:"current,omitempty"`
}
type TxDetailResult struct {
Tx *TxResult `json:"tx"`
Receipt *ReceiptData `json:"receipt"`
Proofs []string `json:"proofs,omitempty"`
Height int64 `json:"height"`
Index int64 `json:"index"`
Blocktime int64 `json:"blocktime"`
Amount string `json:"amount"`
Fromaddr string `json:"fromaddr"`
ActionName string `json:"actionname"`
}
type TxDetailsResult struct {
Txs []*TxDetailResult `json:"txs"`
}
type BlockResult 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 []*TxResult `json:"txs"`
}
type BlockDetailResult struct {
Block *BlockResult `json:"block"`
Receipts []*ReceiptData `json:"receipts"`
}
type BlockDetailsResult struct {
Items []*BlockDetailResult `json:"items"`
}
type WalletTxDetailsResult struct {
TxDetails []*WalletTxDetailResult `json:"txDetails"`
}
type WalletTxDetailResult struct {
Tx *TxResult `json:"tx"`
Receipt *ReceiptData `json:"receipt"`
Height int64 `json:"height"`
Index int64 `json:"index"`
Blocktime int64 `json:"blocktime"`
Amount string `json:"amount"`
Fromaddr string `json:"fromaddr"`
Txhash string `json:"txhash"`
ActionName string `json:"actionname"`
}
type AddrOverviewResult struct {
Reciver string `json:"reciver"`
Balance string `json:"balance"`
TxCount int64 `json:"txCount"`
}
type SellOrder2Show struct {
Tokensymbol string `json:"tokensymbol"`
Seller string `json:"address"`
Amountperboardlot string `json:"amountperboardlot"`
Minboardlot int64 `json:"minboardlot"`
Priceperboardlot string `json:"priceperboardlot"`
Totalboardlot int64 `json:"totalboardlot"`
Soldboardlot int64 `json:"soldboardlot"`
Starttime int64 `json:"starttime"`
Stoptime int64 `json:"stoptime"`
Crowdfund bool `json:"crowdfund"`
SellID string `json:"sellid"`
Status string `json:"status"`
Height int64 `json:"height"`
}
type ReqAddr struct {
Addr string `json:"addr,omitempty"`
Flag int32 `json:"flag,omitempty"`
Count int32 `json:"count,omitempty"`
Direction int32 `json:"direction,omitempty"`
Height int64 `json:"height,omitempty"`
Index int64 `json:"index,omitempty"`
}
func decodeTransaction(tx *jsonrpc.Transaction) *TxResult {
feeResult := strconv.FormatFloat(float64(tx.Fee)/float64(types.Coin), 'f', 4, 64)
amountResult := ""
if tx.Amount != 0 {
amountResult = strconv.FormatFloat(float64(tx.Amount)/float64(types.Coin), 'f', 4, 64)
}
result := &TxResult{
Execer: tx.Execer,
Payload: tx.Payload,
RawPayload: tx.RawPayload,
Signature: tx.Signature,
Fee: feeResult,
Expire: tx.Expire,
Nonce: tx.Nonce,
To: tx.To,
}
payloacValue := tx.Payload.(map[string]interface{})["Value"].(map[string]interface{})
for _, e := range [4]string{"Transfer", "Withdraw", "Genesis", "Hlock"} {
if _, ok := payloacValue[e]; ok {
if amtValue, ok := result.Payload.(map[string]interface{})["Value"].(map[string]interface{})[e].(map[string]interface{})["amount"]; ok {
amt := amtValue.(float64) / float64(types.Coin)
amtResult := strconv.FormatFloat(amt, 'f', 4, 64)
result.Payload.(map[string]interface{})["Value"].(map[string]interface{})[e].(map[string]interface{})["amount"] = amtResult
break
}
}
}
if _, ok := payloacValue["Miner"]; ok {
if rwdValue, ok := result.Payload.(map[string]interface{})["Value"].(map[string]interface{})["Miner"].(map[string]interface{})["reward"]; ok {
rwd := rwdValue.(float64) / float64(types.Coin)
rwdResult := strconv.FormatFloat(rwd, 'f', 4, 64)
result.Payload.(map[string]interface{})["Value"].(map[string]interface{})["Miner"].(map[string]interface{})["reward"] = rwdResult
}
}
if tx.Amount != 0 {
result.Amount = amountResult
}
if tx.From != "" {
result.From = tx.From
}
return result
}
func decodeAccount(acc *types.Account, precision int64) *AccountResult {
balanceResult := strconv.FormatFloat(float64(acc.GetBalance())/float64(precision), 'f', 4, 64)
frozenResult := strconv.FormatFloat(float64(acc.GetFrozen())/float64(precision), 'f', 4, 64)
accResult := &AccountResult{
Addr: acc.GetAddr(),
Currency: acc.GetCurrency(),
Balance: balanceResult,
Frozen: frozenResult,
}
return accResult
}
func constructAccFromLog(l *jsonrpc.ReceiptLogResult, key string) *types.Account {
var cur int32
if tmp, ok := l.Log.(map[string]interface{})[key].(map[string]interface{})["currency"]; ok {
cur = int32(tmp.(float32))
}
var bal int64
if tmp, ok := l.Log.(map[string]interface{})[key].(map[string]interface{})["balance"]; ok {
bal = int64(tmp.(float64))
}
var fro int64
if tmp, ok := l.Log.(map[string]interface{})[key].(map[string]interface{})["frozen"]; ok {
fro = int64(tmp.(float64))
}
var ad string
if tmp, ok := l.Log.(map[string]interface{})[key].(map[string]interface{})["addr"]; ok {
ad = tmp.(string)
}
return &types.Account{
Currency: cur,
Balance: bal,
Frozen: fro,
Addr: ad,
}
}
func decodeLog(rlog jsonrpc.ReceiptDataResult) *ReceiptData {
rd := &ReceiptData{Ty: rlog.Ty, TyName: rlog.TyName}
for _, l := range rlog.Logs {
rl := &ReceiptLog{Ty: l.Ty, TyName: l.TyName, RawLog: l.RawLog}
switch l.Ty {
//case 1, 4, 111, 112, 113, 114:
case types.TyLogErr, types.TyLogGenesis, types.TyLogNewTicket, types.TyLogCloseTicket, types.TyLogMinerTicket,
types.TyLogTicketBind, types.TyLogPreCreateToken, types.TyLogFinishCreateToken, types.TyLogRevokeCreateToken,
types.TyLogTradeSell, types.TyLogTradeBuy, types.TyLogTradeRevoke:
rl.Log = l.Log
//case 2, 3, 5, 11:
case types.TyLogFee, types.TyLogTransfer, types.TyLogDeposit, types.TyLogGenesisTransfer,
types.TyLogTokenTransfer, types.TyLogTokenDeposit:
rl.Log = &ReceiptAccountTransfer{
Prev: decodeAccount(constructAccFromLog(l, "prev"), types.Coin),
Current: decodeAccount(constructAccFromLog(l, "current"), types.Coin),
}
//case 6, 7, 8, 9, 10, 12:
case types.TyLogExecTransfer, types.TyLogExecWithdraw, types.TyLogExecDeposit, types.TyLogExecFrozen, types.TyLogExecActive, types.TyLogGenesisDeposit:
var execaddr string
if tmp, ok := l.Log.(map[string]interface{})["execaddr"].(string); ok {
execaddr = tmp
}
rl.Log = &ReceiptExecAccountTransfer{
ExecAddr: execaddr,
Prev: decodeAccount(constructAccFromLog(l, "prev"), types.Coin),
Current: decodeAccount(constructAccFromLog(l, "current"), types.Coin),
}
case types.TyLogTokenExecTransfer, types.TyLogTokenExecWithdraw, types.TyLogTokenExecDeposit, types.TyLogTokenExecFrozen, types.TyLogTokenExecActive,
types.TyLogTokenGenesisTransfer, types.TyLogTokenGenesisDeposit:
var execaddr string
if tmp, ok := l.Log.(map[string]interface{})["execaddr"].(string); ok {
execaddr = tmp
}
rl.Log = &ReceiptExecAccountTransfer{
ExecAddr: execaddr,
Prev: decodeAccount(constructAccFromLog(l, "prev"), types.TokenPrecision),
Current: decodeAccount(constructAccFromLog(l, "current"), types.TokenPrecision),
}
default:
fmt.Printf("---The log with vlaue:%d is not decoded --------------------\n", l.Ty)
return nil
}
rd.Logs = append(rd.Logs, rl)
}
return rd
}
func ParseBlockDetail(res interface{}) (interface{}, error) {
var result BlockDetailsResult
for _, vItem := range res.(*jsonrpc.BlockDetails).Items {
b := &BlockResult{
Version: vItem.Block.Version,
ParentHash: vItem.Block.ParentHash,
TxHash: vItem.Block.TxHash,
StateHash: vItem.Block.StateHash,
Height: vItem.Block.Height,
BlockTime: vItem.Block.BlockTime,
}
for _, vTx := range vItem.Block.Txs {
b.Txs = append(b.Txs, decodeTransaction(vTx))
}
var rpt []*ReceiptData
for _, vR := range vItem.Receipts {
rpt = append(rpt, decodeLog(*vR))
}
bd := &BlockDetailResult{Block: b, Receipts: rpt}
result.Items = append(result.Items, bd)
}
return result, nil
}
func ParseQueryTxRes(arg interface{}) (interface{}, error) {
res := arg.(*jsonrpc.TransactionDetail)
amountResult := strconv.FormatFloat(float64(res.Amount)/float64(types.Coin), 'f', 4, 64)
result := TxDetailResult{
Tx: decodeTransaction(res.Tx),
Receipt: decodeLog(*(res.Receipt)),
Proofs: res.Proofs,
Height: res.Height,
Index: res.Index,
Blocktime: res.Blocktime,
Amount: amountResult,
Fromaddr: res.Fromaddr,
ActionName: res.ActionName,
}
return result, nil
}
...@@ -11,7 +11,7 @@ import ( ...@@ -11,7 +11,7 @@ import (
"gitlab.33.cn/lihailei/chain33_sdk/controllers" "gitlab.33.cn/lihailei/chain33_sdk/controllers"
"github.com/astaxie/beego" "github.com/astaxie/beego"
"github.com/astaxie/beego/context" //"github.com/astaxie/beego/context"
) )
func init() { func init() {
...@@ -32,15 +32,13 @@ func init() { ...@@ -32,15 +32,13 @@ func init() {
), ),
), ),
) )
ns.Filter("before", func(ctx *context.Context) { //ns.Filter("before", func(ctx *context.Context) {
//_, ok := ctx.Input.Session("uid").(int) // //_, ok := ctx.Input.Session("uid").(int)
//if !ok && ctx.Request.RequestURI != "/login" { // //if !ok && ctx.Request.RequestURI != "/login" {
// ctx.Redirect(302, "/login") // // ctx.Redirect(302, "/login")
// } // // }
ctx.Request.Header.Set("privateKey", "CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944") // ctx.Request.Header.Set("privateKey", "CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944")
}) //})
beego.AddNamespace(ns) beego.AddNamespace(ns)
//beego.Router("/v1/peer",&controllers.PeerController{},) //beego.Include(&controllers.PeerController{},&controllers.BlockController{},&controllers.TxController{})
//beego.Router("/v1/block",&controllers.BlockController{})
//beego.Router("/v1/tx",&controllers.TxController{})
} }
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