Commit 3603d6d7 authored by kingwang's avatar kingwang Committed by 33cn

update 01/24

parent 3f71a8a5
......@@ -47,7 +47,6 @@ func TestAPI(t *testing.T) {
rpcCfg := new(types.RPC)
rpcCfg.GrpcBindAddr = "127.0.0.1:8003"
rpcCfg.JrpcBindAddr = "127.0.0.1:8004"
rpcCfg.MainnetJrpcAddr = rpcCfg.JrpcBindAddr
rpcCfg.Whitelist = []string{"127.0.0.1", "0.0.0.0"}
rpcCfg.JrpcFuncWhitelist = []string{"*"}
rpcCfg.GrpcFuncWhitelist = []string{"*"}
......
......@@ -269,6 +269,29 @@ func (_m *QueueProtocolAPI) GetBlockOverview(param *types.ReqHash) (*types.Block
return r0, r1
}
// GetBlockSequences provides a mock function with given fields: param
func (_m *QueueProtocolAPI) GetBlockSequences(param *types.ReqBlocks) (*types.BlockSequences, error) {
ret := _m.Called(param)
var r0 *types.BlockSequences
if rf, ok := ret.Get(0).(func(*types.ReqBlocks) *types.BlockSequences); ok {
r0 = rf(param)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*types.BlockSequences)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(*types.ReqBlocks) error); ok {
r1 = rf(param)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// GetBlocks provides a mock function with given fields: param
func (_m *QueueProtocolAPI) GetBlocks(param *types.ReqBlocks) (*types.BlockDetails, error) {
ret := _m.Called(param)
......
......@@ -968,6 +968,26 @@ func (q *QueueProtocol) GetBlockBySeq(param *types.Int64) (*types.BlockSeq, erro
return nil, types.ErrTypeAsset
}
// GetBlockSequences block执行序列号
func (q *QueueProtocol) GetBlockSequences(param *types.ReqBlocks) (*types.BlockSequences, error) {
if param == nil {
err := types.ErrInvalidParam
log.Error("GetBlockSequences", "Error", err)
return nil, err
}
msg, err := q.query(blockchainKey, types.EventGetBlockSequences, param)
if err != nil {
log.Error("GetBlockSequences", "Error", err.Error())
return nil, err
}
if reply, ok := msg.GetData().(*types.BlockSequences); ok {
return reply, nil
}
err = types.ErrTypeAsset
log.Error("GetBlockSequences", "Error", err)
return nil, err
}
// QueryChain query chain
func (q *QueueProtocol) QueryChain(param *types.ChainExecutor) (types.Message, error) {
if param == nil {
......
......@@ -107,7 +107,8 @@ type QueueProtocolAPI interface {
//types.EventGetLastBlockSequence:
GetLastBlockSequence() (*types.Int64, error)
//types.EventGetBlockSequences:
GetBlockSequences(param *types.ReqBlocks) (*types.BlockSequences, error)
//types.EventGetBlockByHashes:
GetBlockByHashes(param *types.ReqHashes) (*types.BlockDetails, error)
//types.EventGetBlockBySeq:
......
......@@ -151,6 +151,7 @@ func (exec *Executor) procExecQuery(msg queue.Message) {
driver.SetStateDB(db)
driver.SetAPI(exec.qclient)
driver.SetExecutorAPI(exec.qclient, exec.grpccli)
driver.SetEnv(header.GetHeight(), header.GetBlockTime(), uint64(header.GetDifficulty()))
//查询的情况下下,执行器不做严格校验,allow,尽可能的加载执行器,并且做查询
ret, err := driver.Query(data.FuncName, data.Param)
......
......@@ -22,7 +22,6 @@ func TestMultipleGRPC(t *testing.T) {
rpcCfg := new(types.RPC)
rpcCfg.GrpcBindAddr = "127.0.0.1:8003"
rpcCfg.JrpcBindAddr = "127.0.0.1:8004"
rpcCfg.MainnetJrpcAddr = rpcCfg.JrpcBindAddr
rpcCfg.Whitelist = []string{"127.0.0.1", "0.0.0.0"}
rpcCfg.JrpcFuncWhitelist = []string{"*"}
rpcCfg.GrpcFuncWhitelist = []string{"*"}
......@@ -53,7 +52,6 @@ func TestMultipleGRPCLocalhost(t *testing.T) {
rpcCfg := new(types.RPC)
rpcCfg.GrpcBindAddr = "localhost:8003"
rpcCfg.JrpcBindAddr = "localhost:8004"
rpcCfg.MainnetJrpcAddr = rpcCfg.JrpcBindAddr
rpcCfg.Whitelist = []string{"localhost", "0.0.0.0"}
rpcCfg.JrpcFuncWhitelist = []string{"*"}
rpcCfg.GrpcFuncWhitelist = []string{"*"}
......@@ -84,7 +82,6 @@ func TestNewParaClient(t *testing.T) {
rpcCfg := new(types.RPC)
rpcCfg.GrpcBindAddr = "127.0.0.1:8003"
rpcCfg.JrpcBindAddr = "127.0.0.1:8004"
rpcCfg.MainnetJrpcAddr = rpcCfg.JrpcBindAddr
rpcCfg.Whitelist = []string{"127.0.0.1", "0.0.0.0"}
rpcCfg.JrpcFuncWhitelist = []string{"*"}
rpcCfg.GrpcFuncWhitelist = []string{"*"}
......
......@@ -5,6 +5,9 @@ import (
"google.golang.org/grpc"
)
// paraChainGrpcRecSize 平行链receive最大100M
const paraChainGrpcRecSize = 100 * 1024 * 1024
//NewMainChainClient 创建一个平行链的 主链 grpc chain33 客户端
func NewMainChainClient(grpcaddr string) (types.Chain33Client, error) {
paraRemoteGrpcClient := types.Conf("config.consensus.sub.para").GStr("ParaRemoteGrpcClient")
......@@ -12,9 +15,11 @@ func NewMainChainClient(grpcaddr string) (types.Chain33Client, error) {
paraRemoteGrpcClient = grpcaddr
}
if paraRemoteGrpcClient == "" {
paraRemoteGrpcClient = "127.0.0.1:8002"
paraRemoteGrpcClient = "127.0.0.1:8802"
}
conn, err := grpc.Dial(NewMultipleURL(paraRemoteGrpcClient), grpc.WithInsecure())
conn, err := grpc.Dial(NewMultipleURL(paraRemoteGrpcClient), grpc.WithInsecure(),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(paraChainGrpcRecSize)))
if err != nil {
return nil, err
}
......
......@@ -5,6 +5,7 @@
package rpc
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
......@@ -12,7 +13,6 @@ import (
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/address"
"github.com/33cn/chain33/rpc/jsonclient"
"github.com/33cn/chain33/types"
wcom "github.com/33cn/chain33/wallet/common"
......@@ -114,26 +114,8 @@ func (c *Chain33) SendRawTransaction(in rpctypes.SignedTx, result *interface{})
return fmt.Errorf(string(reply.Msg))
}
// used only in parachain
func forwardTranToMainNet(in rpctypes.RawParm, result *interface{}) error {
if rpcCfg.MainnetJrpcAddr == "" {
return types.ErrInvalidMainnetRPCAddr
}
rpc, err := jsonclient.NewJSONClient(rpcCfg.MainnetJrpcAddr)
if err != nil {
return err
}
err = rpc.Call("Chain33.SendTransaction", in, result)
return err
}
// SendTransaction send transaction
func (c *Chain33) SendTransaction(in rpctypes.RawParm, result *interface{}) error {
if types.IsPara() {
return forwardTranToMainNet(in, result)
}
var parm types.Transaction
data, err := common.FromHex(in.Data)
if err != nil {
......@@ -141,7 +123,15 @@ func (c *Chain33) SendTransaction(in rpctypes.RawParm, result *interface{}) erro
}
types.Decode(data, &parm)
log.Debug("SendTransaction", "parm", parm)
reply, err := c.cli.SendTx(&parm)
var reply *types.Reply
//para chain, forward to main chain
if types.IsPara() {
reply, err = c.mainGrpcCli.SendTransaction(context.Background(), &parm)
} else {
reply, err = c.cli.SendTx(&parm)
}
if err == nil {
*result = common.ToHex(reply.GetMsg())
}
......@@ -1048,6 +1038,22 @@ func (c *Chain33) GetLastBlockSequence(in *types.ReqNil, result *interface{}) er
return nil
}
// GetBlockSequences get the block loading sequence number information for the specified interval
func (c *Chain33) GetBlockSequences(in rpctypes.BlockParam, result *interface{}) error {
resp, err := c.cli.GetBlockSequences(&types.ReqBlocks{Start: in.Start, End: in.End, IsDetail: in.Isdetail, Pid: []string{""}})
if err != nil {
return err
}
var BlkSeqs rpctypes.ReplyBlkSeqs
items := resp.GetItems()
for _, item := range items {
BlkSeqs.BlkSeqInfos = append(BlkSeqs.BlkSeqInfos, &rpctypes.ReplyBlkSeq{Hash: common.ToHex(item.GetHash()),
Type: item.GetType()})
}
*result = &BlkSeqs
return nil
}
// GetBlockByHashes get block information by hashes
func (c *Chain33) GetBlockByHashes(in rpctypes.ReqHashes, result *interface{}) error {
log.Warn("GetBlockByHashes", "hashes", in)
......
......@@ -1214,6 +1214,26 @@ func TestChain33_GetLastBlockSequence(t *testing.T) {
assert.Equal(t, int64(1), result2)
}
func TestChain33_GetBlockSequences(t *testing.T) {
api := new(mocks.QueueProtocolAPI)
client := newTestChain33(api)
var result interface{}
api.On("GetBlockSequences", mock.Anything).Return(nil, types.ErrInvalidParam)
err := client.GetBlockSequences(rpctypes.BlockParam{}, &result)
assert.NotNil(t, err)
api = new(mocks.QueueProtocolAPI)
client = newTestChain33(api)
var result2 interface{}
blocks := types.BlockSequences{}
blocks.Items = make([]*types.BlockSequence, 0)
blocks.Items = append(blocks.Items, &types.BlockSequence{Hash: []byte("h1"), Type: 1})
api.On("GetBlockSequences", mock.Anything).Return(&blocks, nil)
err = client.GetBlockSequences(rpctypes.BlockParam{}, &result2)
assert.Nil(t, err)
assert.Equal(t, 1, len(result2.(*rpctypes.ReplyBlkSeqs).BlkSeqInfos))
}
func TestChain33_GetBlockByHashes(t *testing.T) {
api := new(mocks.QueueProtocolAPI)
client := newTestChain33(api)
......
......@@ -12,6 +12,7 @@ import (
"github.com/33cn/chain33/client"
"github.com/33cn/chain33/pluginmgr"
"github.com/33cn/chain33/queue"
"github.com/33cn/chain33/rpc/grpcclient"
_ "github.com/33cn/chain33/rpc/grpcclient" // register grpc multiple resolver
"github.com/33cn/chain33/types"
"golang.org/x/net/context"
......@@ -33,6 +34,8 @@ var (
// Chain33 a channel client
type Chain33 struct {
cli channelClient
//for communicate with main chain in parallel chain
mainGrpcCli types.Chain33Client
}
// Grpc a channelClient
......@@ -169,6 +172,11 @@ func NewGRpcServer(c queue.Client, api client.QueueProtocolAPI) *Grpcserver {
func NewJSONRPCServer(c queue.Client, api client.QueueProtocolAPI) *JSONRPCServer {
j := &JSONRPCServer{jrpc: &Chain33{}}
j.jrpc.cli.Init(c, api)
grpcCli, err := grpcclient.NewMainChainClient("")
if err != nil {
panic(err)
}
j.jrpc.mainGrpcCli = grpcCli
server := rpc.NewServer()
j.s = server
server.RegisterName("Chain33", j.jrpc)
......
......@@ -42,7 +42,6 @@ func TestJSONClient_Call(t *testing.T) {
rpcCfg = new(types.RPC)
rpcCfg.GrpcBindAddr = "127.0.0.1:8101"
rpcCfg.JrpcBindAddr = "127.0.0.1:8200"
rpcCfg.MainnetJrpcAddr = rpcCfg.JrpcBindAddr
rpcCfg.Whitelist = []string{"127.0.0.1", "0.0.0.0"}
rpcCfg.JrpcFuncWhitelist = []string{"*"}
rpcCfg.GrpcFuncWhitelist = []string{"*"}
......@@ -146,7 +145,6 @@ func TestGrpc_Call(t *testing.T) {
rpcCfg = new(types.RPC)
rpcCfg.GrpcBindAddr = "127.0.0.1:8101"
rpcCfg.JrpcBindAddr = "127.0.0.1:8200"
rpcCfg.MainnetJrpcAddr = rpcCfg.JrpcBindAddr
rpcCfg.Whitelist = []string{"127.0.0.1", "0.0.0.0"}
rpcCfg.JrpcFuncWhitelist = []string{"*"}
rpcCfg.GrpcFuncWhitelist = []string{"*"}
......
......@@ -138,7 +138,6 @@ type RPC struct {
GrpcFuncWhitelist []string `protobuf:"bytes,6,rep,name=grpcFuncWhitelist" json:"grpcFuncWhitelist,omitempty"`
JrpcFuncBlacklist []string `protobuf:"bytes,7,rep,name=jrpcFuncBlacklist" json:"jrpcFuncBlacklist,omitempty"`
GrpcFuncBlacklist []string `protobuf:"bytes,8,rep,name=grpcFuncBlacklist" json:"grpcFuncBlacklist,omitempty"`
MainnetJrpcAddr string `protobuf:"bytes,9,opt,name=mainnetJrpcAddr" json:"mainnetJrpcAddr,omitempty"`
EnableTLS bool `protobuf:"varint,10,opt,name=enableTLS" json:"enableTLS,omitempty"`
EnableTrace bool `protobuf:"varint,10,opt,name=enableTrace" json:"enableTrace,omitempty"`
CertFile string `protobuf:"varint,11,opt,name=certFile" json:"certFile,omitempty"`
......
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