Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
chain33-sdk-go
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
link33
chain33-sdk-go
Commits
94ec47d9
Commit
94ec47d9
authored
Dec 06, 2021
by
harrylee
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add debug for sdk
parent
8b3cd529
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
99 additions
and
80 deletions
+99
-80
jsonclient.go
client/jsonclient.go
+43
-42
jsonclient_test.go
client/jsonclient_test.go
+56
-38
No files found.
client/jsonclient.go
View file @
94ec47d9
...
...
@@ -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
)
{
...
...
client/jsonclient_test.go
View file @
94ec47d9
...
...
@@ -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
(
"
tes
t"
,
"hello"
,
GenesisPriv
)
resp
,
err
:=
client
.
KVStore
(
"
gree
t"
,
"hello"
,
GenesisPriv
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
time
.
Sleep
(
time
.
Second
)
resp
,
err
=
client
.
EmitDataSwapInterchainEvent
(
"
fabric&broker"
,
"test"
,
"tes
t"
,
""
,
GenesisPriv
)
resp
,
err
=
client
.
EmitDataSwapInterchainEvent
(
"
mychannel&data_swapper"
,
"greet"
,
"gree
t"
,
""
,
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
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment