Commit 94ec47d9 authored by harrylee's avatar harrylee

add debug for sdk

parent 8b3cd529
......@@ -309,6 +309,7 @@ func (client *JSONClient) KVStore(key, value, privateKey string) (*Response, err
}
return client.SendTransactionSync(privateKey, tx)
}
// 初始化broker
func (client *JSONClient) InitBroker(bxhId, appChainId, privateKey string) (*Response, error) {
tx := broker.InitBroker(bxhId, appChainId)
......@@ -357,8 +358,9 @@ func (client *JSONClient) Execute(requst *Request, privateKey string) (*Response
if err := json.Unmarshal(requst.Args[4], callFunc); err != nil {
return nil, fmt.Errorf("unmarshal call func failed for %s", requst.Args[4])
}
fmt.Println("==================callfunc=======",callFunc.Func)
// 调用相应链码, (链码名称,参数,通道名称)
if splitedCID[1] == storage.StorageX {
if splitedCID[1] == storage.StorageX || splitedCID[1] == broker.BrokerX {
//interchainGet方法是在sdk封装的,用于支持跨链之前数据的查询
switch callFunc.Func {
case "interchainGet":
......@@ -374,7 +376,7 @@ func (client *JSONClient) Execute(requst *Request, privateKey string) (*Response
status := 1
response, err := client.InterChainSet(string(callFunc.Args[0]), string(callFunc.Args[1]), privateKey)
if err != nil {
status = 0
status = 0
}
//TODO 这里的处理不是事务机制
if reqType != 0 {
......@@ -384,7 +386,7 @@ func (client *JSONClient) Execute(requst *Request, privateKey string) (*Response
return resp, err
}
}
return response,nil
return response, nil
}
} else {
return nil, fmt.Errorf("can't support exec %s", splitedCID[1])
......@@ -429,11 +431,11 @@ func (client *JSONClient) InterChainGet(key string) (*Response, error) {
return resp, nil
}
// 简单实现交易时间监听功能,后面换成grpc长链接方式监听
// 简单实现交易事件的监听功能,后面换成grpc长链接方式监听
func (client *JSONClient) RegisterTxEvent(start int64, ccID, eventFilter string) (chan<- event.Registration, <-chan *event.CCEvent, <-chan error) {
ch := make(chan *event.CCEvent)
resCh := make(chan event.Registration)
errCh := make(chan error)
ch := make(chan *event.CCEvent, 1)
resCh := make(chan event.Registration, 1)
errCh := make(chan error, 1)
// FIXME 这里实现是以联盟链方式,联盟链没有回滚,所以这里暂时按高度拉去处理
go func(ch chan<- *event.CCEvent, closeCh <-chan event.Registration, errCh chan<- error) {
height := start
......@@ -449,42 +451,41 @@ func (client *JSONClient) RegisterTxEvent(start int64, ccID, eventFilter string)
goto HERE
}
ticket := time.After(100 * time.Millisecond)
for {
select {
case <-ticket:
blockInfo, err := client.QueryBlockInfo(height, height, true)
if err != nil {
errCh <- err
return
}
for _, item := range blockInfo.Items {
for i, tx := range item.Block.Txs {
if tx.Execer == ccID && item.Receipts[i].Ty == types.ExecOk {
// todo 这里需要重新弄成通用处理
if tx.Execer == broker.BrokerX {
var brokerAction types.BrokerAction
err := types.Decode(tx.Payload, &brokerAction)
if err != nil {
errCh <- err
return
}
if brokerAction.Ty == broker.ActionMap[eventFilter] {
ccEvent := &event.CCEvent{Payload: types.Encode(brokerAction.GetEmitInterchainEvent()), CCID: tx.Execer, TxID: tx.Hash, EventName: eventFilter, BlockNumber: uint64(item.Block.Height)}
ch <- ccEvent
}
select {
case <-ticket:
blockInfo, err := client.QueryBlockInfo(height, height, true)
if err != nil {
errCh <- err
return
}
for _, item := range blockInfo.Items {
for i, tx := range item.Block.Txs {
if tx.Execer == ccID && item.Receipts[i].Ty == types.ExecOk {
// todo 这里需要重新弄成通用处理
if tx.Execer == broker.BrokerX {
var brokerAction types.BrokerAction
err := types.Decode(tx.Payload, &brokerAction)
if err != nil {
errCh <- err
return
}
if brokerAction.Ty == broker.ActionMap[eventFilter] {
ccEvent := &event.CCEvent{Payload: types.Encode(brokerAction.GetEmitInterchainEvent()), CCID: tx.Execer, TxID: tx.Hash, EventName: eventFilter, BlockNumber: uint64(item.Block.Height)}
ch <- ccEvent
}
}
}
}
height++
goto HERE
case <-closeCh:
break
}
height++
goto HERE
case <-closeCh:
break
}
}(ch, resCh, errCh)
return resCh, ch, nil
}
......@@ -523,11 +524,11 @@ func (client *JSONClient) QueryInnerMeta() (*types.Meta, error) {
// return nil, err
//}
//return &meta, nil
data,err:=client.CallBack("Chain33.Query", query,ParseMeta)
data, err := client.CallBack("Chain33.Query", query, ParseMeta)
if err != nil {
return nil, err
}
return data.(*types.Meta),nil
return data.(*types.Meta), nil
}
func (client *JSONClient) QueryOutterMeta() (*types.Meta, error) {
......@@ -546,11 +547,11 @@ func (client *JSONClient) QueryOutterMeta() (*types.Meta, error) {
// return nil, err
//}
//return &meta, nil
data,err:=client.CallBack("Chain33.Query", query,ParseMeta)
data, err := client.CallBack("Chain33.Query", query, ParseMeta)
if err != nil {
return nil, err
}
return data.(*types.Meta),nil
return data.(*types.Meta), nil
}
func (client *JSONClient) QueryCallBackMeta() (*types.Meta, error) {
......@@ -587,11 +588,11 @@ func (client *JSONClient) QueryOutMessage(inServicePair string, index uint64) (*
// return nil, err
//}
//return &event, nil
data,err:=client.CallBack("Chain33.Query", query, ParseInterChainEvent)
data, err := client.CallBack("Chain33.Query", query, ParseInterChainEvent)
if err != nil {
return nil, err
}
return data.(*types.InterchainEvent),nil
return data.(*types.InterchainEvent), nil
}
func (client *JSONClient) QueryInMessage(inServicePair string, index uint64) (*types.Response, error) {
......
......@@ -2,6 +2,8 @@ package client
import (
"gitlab.33.cn/link33/chain33-sdk-go/dapp/broker"
"math/rand"
//"gitlab.33.cn/link33/chain33-sdk-go/dapp/broker"
"testing"
"time"
......@@ -11,12 +13,12 @@ var (
GenesisPriv = "cc38546e9e659d15e6b4893f0ab32a06d103931a8230b0bde71459d2b27d6944"
)
func Test_InitBroker(t *testing.T){
func Test_InitBroker(t *testing.T) {
client, err := NewJSONClient("", "http://localhost:8901")
if err != nil {
t.Error(err)
}
resp, err :=client.InitBroker("bxhv1","chain33",GenesisPriv)
resp, err := client.InitBroker("bxhv1", "chain33", GenesisPriv)
if err != nil {
t.Error(err)
}
......@@ -28,72 +30,75 @@ func Test_EmitDataSwapInterchainEvent(t *testing.T) {
t.Error(err)
}
// 第一次存储
resp, err := client.KVStore("test", "hello", GenesisPriv)
resp, err := client.KVStore("greet", "hello", GenesisPriv)
if err != nil {
t.Error(err)
}
time.Sleep(time.Second)
resp, err = client.EmitDataSwapInterchainEvent("fabric&broker", "test", "test", "", GenesisPriv)
resp, err = client.EmitDataSwapInterchainEvent("mychannel&data_swapper", "greet", "greet", "", GenesisPriv)
if err != nil {
t.Error(err)
}
t.Log(resp.Data)
}
func Test_InterChainGet(t *testing.T){
func Test_InterChainGet(t *testing.T) {
client, err := NewJSONClient("", "http://localhost:8901")
if err != nil {
t.Error(err)
}
resp,err:=client.InterChainGet("test")
resp, err := client.InterChainGet("test")
if err != nil {
t.Error(err)
}
t.Log(string(resp.Data))
}
func Test_QueryBrokerInfo(t *testing.T){
func Test_QueryBrokerInfo(t *testing.T) {
client, err := NewJSONClient("", "http://localhost:8901")
if err != nil {
t.Error(err)
}
info,err:=client.QueryBrokerInfo()
info, err := client.QueryBrokerInfo()
if err != nil {
t.Error(err)
}
t.Logf("appid:%s,version:%s",info.GetAppChainId(),info.GetBxhId())
meta,err:=client.QueryOutterMeta()
t.Logf("appid:%s,version:%s", info.GetAppChainId(), info.GetBxhId())
meta, err := client.QueryOutterMeta()
if err != nil {
t.Error(err)
}
t.Log(meta.Meta)
}
func Test_PollEvent(t *testing.T){
func Test_PollEvent(t *testing.T) {
client, err := NewJSONClient("", "http://localhost:8901")
if err != nil {
t.Error(err)
}
registerCh,eventCh,errCh:=client.RegisterTxEvent(0,"broker","EmitInterchainEvent")
go func() {
select {
case event:=<-eventCh:
t.Log(event)
case err:=<-errCh:
t.Error(err)
}
}()
time.Sleep(5*time.Second)
registerCh<- struct {}{}
}
if err != nil {
t.Error(err)
}
registerCh, eventCh, errCh := client.RegisterTxEvent(0, "broker", "EmitInterchainEvent")
t.Log("111111111111111")
ticker := time.Tick(5 * time.Second)
select {
case event := <-eventCh:
t.Log(event)
case err := <-errCh:
t.Error(err)
func Test_UpdateIndex(t *testing.T){
case <-ticker:
registerCh <- struct{}{}
return
}
registerCh <- struct{}{}
}
func Test_UpdateIndex(t *testing.T) {
client, err := NewJSONClient("", "http://localhost:8901")
if err != nil {
t.Error(err)
}
event,err:=client.QueryOutMessage("bxhv1:chain33:local&broker-fabric&broker",1)
if err !=nil {
event, err := client.QueryOutMessage("bxhv1:chain33:local&broker-fabric&broker", 1)
if err != nil {
t.Error(err)
}
tx := broker.UpdateIndex("", event.SrcServiceID, event.GetDstServiceID(), event.GetIndex(), 0, 1)
......@@ -104,26 +109,39 @@ func Test_UpdateIndex(t *testing.T){
t.Log(resp.Message)
}
func Test_QueryMeta(t *testing.T){
func Test_QueryMeta(t *testing.T) {
client, err := NewJSONClient("", "http://localhost:8901")
if err != nil {
t.Error(err)
}
resp,err:=client.QueryOutMessage("bxhv1:chain33:local&broker-fabric&broker",1)
if err !=nil {
resp, err := client.QueryOutMessage("bxhv1:chain33:local&broker-fabric&broker", 1)
if err != nil {
t.Error(err)
}
t.Log(resp)
respon,err:=client.QueryInMessage("bxhv1:chain33:local&broker-fabric&broker",1)
if err !=nil {
respon, err := client.QueryInMessage("bxhv1:chain33:local&broker-fabric&broker", 1)
if err != nil {
t.Error(err)
}
t.Logf("status:%d",respon.Status)
t.Logf("status:%d", respon.Status)
meta,err:=client.QueryOutterMeta()
if err !=nil {
meta, err := client.QueryOutterMeta()
if err != nil {
t.Error(err)
}
t.Log(meta.Meta)
}
func Test_Channel(t *testing.T){
ch :=make(chan int,10)
ch <-1
select {
case a:=<-ch:
t.Log(a)
}
a :=rand.Intn(2)
t.Log(a)
}
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