前言
关键代码
package blockchain
import (
"errors"
//必须使用tmjson做解析因为tmjson解析int64方式不一样
tmjson "github.com/tendermint/tendermint/libs/json"
tmtypes "github.com/tendermint/tendermint/rpc/jsonrpc/types"
tmtypes2 "github.com/tendermint/tendermint/rpc/coretypes"
"originCodeServer/nodeservice/net"
"strconv"
)
//区块链浏览器服务
//一些端口和服务URL
const (
MNODEHOST = "http://localhost:26657"
ABCIINFOURL = "/abci_info" //ABCI应用信息
ABCIQUERYURL = "/abci_query" //ABCI应用存储数据查询 ?path=""&data="abcd"&prove=false'
BLOCKURL = "/block" //根据高度查询指定区块信息 height=1
BLOCKCHAININFOURL = "/blockchain" //根据minHeight和maxHeight查询区块头信息
TXURL = "/tx" //根据hash进行交易查询 ?hash="0x9999"
NETINFOURL = "/net_info" //查询网络信息节点数 ip
BROADCASTTXASYNCURL = "/broadcast_tx_async" //异步广播交易 ?tx="name=123"
)
type BlockChainService struct {
}
//统一使用tmjson做解析 因为tendermint解析与encoding/json有所不同
//详细请看github.com/tendermint/tendermint/libs/json/doc.go文件
func (b *BlockChainService) FindBlockByHeight(height int) (*tmtypes2.ResultBlock, error) {
rpcResult:=&tmtypes.RPCResponse{}
blockResult :=&tmtypes2.ResultBlock{}
if height <= 0 {
return nil, errors.New("height不合法")
}
//这里使用POST方式获取
data, err := net.Post(MNODEHOST+BLOCKURL, map[string]string{"height": strconv.Itoa(height)})
if err != nil {
return nil, err
}
err = tmjson.Unmarshal(data,rpcResult) //获取到rpcRespone
if err != nil {
return nil, err
}
err = tmjson.Unmarshal(rpcResult.Result,blockResult) //获取到block
if err != nil {
return nil, err
}
return blockResult, nil
}
结果

版权声明:本文为qq_33886316原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。