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
bd668bac
Commit
bd668bac
authored
Nov 09, 2021
by
harrylee
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add data swap method
parent
44887f8c
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
64 additions
and
77 deletions
+64
-77
jsonclient.go
client/jsonclient.go
+44
-37
broker.go
dapp/broker/broker.go
+2
-25
broker.proto
proto/broker.proto
+18
-15
broker.pb.go
types/broker.pb.go
+0
-0
No files found.
client/jsonclient.go
View file @
bd668bac
...
...
@@ -301,77 +301,84 @@ func (client *JSONClient) QueryBlockInfo(start, end int64, isDetail bool) (*Bloc
return
&
res
,
nil
}
// 调用chaincode
func
(
client
*
JSONClient
)
Invoke
(
requst
*
Request
,
privateKey
string
)
(
*
Response
,
error
)
{
// FIXME 这里需完善
if
requst
.
Exec
==
storage
.
StorageX
{
switch
requst
.
Fcn
{
case
"set"
,
"ContentStorage"
:
tx
,
err
:=
storage
.
CreateContentStorageTx
(
client
.
prefix
,
0
,
string
(
requst
.
Args
[
0
]),
nil
,
string
(
requst
.
Args
[
1
]))
if
err
!=
nil
{
return
nil
,
err
}
return
client
.
SendTransactionSync
(
privateKey
,
tx
)
}
// 发布跨链事件
func
(
client
*
JSONClient
)
EmitDataSwapInterchainEvent
(
dstServiceID
,
args
,
argscb
,
argsrb
string
,
privateKey
string
)
(
*
Response
,
error
)
{
// FIXME 这里需完善 funcName="interchainGet,interchainSet," txType=0 表示是存在类数据交换
tx
,
err
:=
broker
.
EmitInterchainEvent
(
""
,
dstServiceID
,
"interchainGet,interchainSet,"
,
args
,
argsrb
,
argsrb
,
0
)
if
err
!=
nil
{
return
nil
,
err
}
return
nil
,
fmt
.
Errorf
(
"not matching execution or method!"
)
return
client
.
SendTransactionSync
(
privateKey
,
tx
)
}
// TODO 执行请求
func
(
client
*
JSONClient
)
Execute
(
requst
*
Request
,
privateKey
string
)
(
*
Response
,
error
)
{
if
requst
.
Exec
==
broker
.
BrokerX
{
func
(
client
*
JSONClient
)
Execute
(
requst
*
Request
,
privateKey
string
)
(
*
Response
,
error
)
{
if
requst
.
Exec
==
broker
.
BrokerX
{
switch
requst
.
Fcn
{
case
"invokeInterchain"
:
srcChainServiceID
:=
string
(
requst
.
Args
[
0
])
sequenceNum
,
err
:=
strconv
.
ParseUint
(
string
(
requst
.
Args
[
1
]),
10
,
64
)
sequenceNum
,
err
:=
strconv
.
ParseUint
(
string
(
requst
.
Args
[
1
]),
10
,
64
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"cannot parse %s to uint64"
,
string
(
requst
.
Args
[
1
]))
return
nil
,
fmt
.
Errorf
(
"cannot parse %s to uint64"
,
string
(
requst
.
Args
[
1
]))
}
targetCID
:=
string
(
requst
.
Args
[
2
])
reqType
,
err
:=
strconv
.
ParseUint
(
string
(
requst
.
Args
[
3
]),
10
,
64
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"cannot parse %s to uint64"
,
string
(
requst
.
Args
[
3
]))
return
nil
,
fmt
.
Errorf
(
"cannot parse %s to uint64"
,
string
(
requst
.
Args
[
3
]))
}
if
tx
,
err
:=
broker
.
UpdateIndex
(
""
,
srcChainServiceID
,
targetCID
,
sequenceNum
,
reqType
,
nil
);
err
==
nil
{
resp
,
err
:=
client
.
SendTransactionSync
(
privateKey
,
tx
)
if
err
!=
nil
{
return
resp
,
err
if
tx
,
err
:=
broker
.
UpdateIndex
(
""
,
srcChainServiceID
,
targetCID
,
sequenceNum
,
reqType
,
nil
);
err
==
nil
{
resp
,
err
:=
client
.
SendTransactionSync
(
privateKey
,
tx
)
if
err
!=
nil
{
return
resp
,
err
}
//splitedCID[1]就是合约名称
splitedCID
:=
strings
.
Split
(
targetCID
,
"&"
)
if
len
(
splitedCID
)
!=
2
{
return
nil
,
fmt
.
Errorf
(
"Target chaincode id %s is not valid"
,
targetCID
)
return
nil
,
fmt
.
Errorf
(
"Target chaincode id %s is not valid"
,
targetCID
)
}
callFunc
:=
&
CallFunc
{}
if
err
:=
json
.
Unmarshal
(
requst
.
Args
[
4
],
callFunc
);
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"unmarshal call func failed for %s"
,
requst
.
Args
[
4
])
return
nil
,
fmt
.
Errorf
(
"unmarshal call func failed for %s"
,
requst
.
Args
[
4
])
}
// 调用相应链码, (链码名称,参数,通道名称)
if
splitedCID
[
1
]
==
storage
.
StorageX
{
if
splitedCID
[
1
]
==
storage
.
StorageX
{
//interchainGet方法是在sdk封装的,用于支持跨链之前数据的查询
switch
callFunc
.
Func
{
case
"interchainGet"
:
return
client
.
InterChainGet
(
string
(
callFunc
.
Args
[
0
]))
case
"interchainGet"
:
response
,
err
:=
client
.
InterChainGet
(
string
(
callFunc
.
Args
[
0
]))
if
err
!=
nil
{
return
resp
,
err
}
//携带TXhash
response
.
Message
=
resp
.
Message
return
response
,
err
case
"interchainSet"
:
//跨链set kv
return
client
.
InterChainSet
(
string
(
callFunc
.
Args
[
0
]),
string
(
callFunc
.
Args
[
1
]),
privateKey
)
}
}
else
{
return
nil
,
fmt
.
Errorf
(
"can't support exec %s"
,
splitedCID
[
1
])
}
else
{
return
nil
,
fmt
.
Errorf
(
"can't support exec %s"
,
splitedCID
[
1
])
}
}
}
}
return
nil
,
nil
return
nil
,
fmt
.
Errorf
(
"not matching execution or method!"
)
}
//包装适配跨链插件调用
func
(
client
*
JSONClient
)
InterChainSet
(
key
,
value
string
)
(
*
Response
,
error
)
{
return
nil
,
nil
//包装适配跨链插件调用
func
(
client
*
JSONClient
)
InterChainSet
(
key
,
value
,
privateKey
string
)
(
*
Response
,
error
)
{
tx
,
err
:=
storage
.
CreateContentStorageTx
(
""
,
0
,
key
,
nil
,
value
)
if
err
!=
nil
{
return
nil
,
err
}
resp
,
err
:=
client
.
SendTransactionSync
(
privateKey
,
tx
)
return
resp
,
err
}
//包装适配跨链插件调用
func
(
client
*
JSONClient
)
InterChainGet
(
key
string
)
(
*
Response
,
error
)
{
jsonraw
,
err
:=
json
.
Marshal
(
&
types
.
QueryStorage
{
TxHash
:
key
})
...
...
@@ -388,9 +395,9 @@ func (client *JSONClient) InterChainGet(key string) (*Response, error) {
if
err
!=
nil
{
return
nil
,
err
}
storage
:=
data
.
(
*
types
.
Storage
)
storage
:=
data
.
(
*
types
.
Storage
)
//这里暂时就去明文存证内容
resp
:=
&
Response
{
OK
:
true
,
Message
:
key
,
Data
:
storage
.
GetContentStorage
()
.
GetContent
(
)}
resp
:=
&
Response
{
OK
:
true
,
Message
:
key
,
Data
:
[]
byte
(
storage
.
GetContentStorage
()
.
GetValue
()
)}
return
resp
,
nil
}
...
...
dapp/broker/broker.go
View file @
bd668bac
...
...
@@ -9,18 +9,8 @@ import (
)
// 构造发布跨链交易事件交易
func
EmitInterchainEvent
(
paraName
,
dstServiceID
,
funcName
string
,
args
[]
string
,
txType
uint64
)
(
*
types
.
Transaction
,
error
)
{
//目的链ID,固定格式0x000001
//string dstServiceID = 2;
////源链ID,固定格式0x000002
//string srcServiceID = 3;
////跨链交易类型 0表示storage,1表示coins,2表示token......
//uint64 type =4;
////调用方法
//string func = 5;
////参数列表
//repeated string args = 6;
payload
:=
&
types
.
BrokerAction
{
Ty
:
TyEmitInterchainEventAction
,
Value
:
&
types
.
BrokerAction_EmitInterchainEvent
{
EmitInterchainEvent
:
&
types
.
InterchainEvent
{
DstServiceID
:
dstServiceID
,
Type
:
txType
,
Func
:
funcName
,
Args
:
args
}}}
func
EmitInterchainEvent
(
paraName
,
dstServiceID
,
funcName
string
,
args
,
argscb
,
argsrb
string
,
txType
uint64
)
(
*
types
.
Transaction
,
error
)
{
payload
:=
&
types
.
BrokerAction
{
Ty
:
TyEmitInterchainEventAction
,
Value
:
&
types
.
BrokerAction_EmitInterchainEvent
{
EmitInterchainEvent
:
&
types
.
InterchainEvent
{
DstServiceID
:
dstServiceID
,
Type
:
txType
,
Func
:
funcName
,
Args
:
args
,
Argscb
:
argscb
,
Argsrb
:
argsrb
}}}
if
paraName
==
""
{
tx
:=
&
types
.
Transaction
{
Execer
:
[]
byte
(
BrokerX
),
Payload
:
types
.
Encode
(
payload
),
Fee
:
1e5
,
Nonce
:
rand
.
Int63n
(
time
.
Now
()
.
UnixNano
()),
To
:
Addr
}
return
tx
,
nil
...
...
@@ -32,16 +22,6 @@ func EmitInterchainEvent(paraName, dstServiceID, funcName string, args []string,
// 构造更新Index交易
func
UpdateIndex
(
paraName
,
srcServiceID
,
dstServiceID
string
,
sequenceNum
,
reqType
uint64
,
response
*
types
.
Response
)
(
*
types
.
Transaction
,
error
)
{
////当前链已经处理到的位置
//uint64 sequenceNum = 1;
////目的链服务ID,固定格式0x000001
//string dstServiceID = 2;
////源链ID,固定格式0x000002
//string srcServiceID = 3;
////请求类型 0表示信息流入 1表示信息流出 2表示回滚
//uint64 reqType =4;
////响应信息
//Response response = 5;
payload
:=
&
types
.
BrokerAction
{
Ty
:
TyUpdateIndexAction
,
Value
:
&
types
.
BrokerAction_UpdateIndex
{
UpdateIndex
:
&
types
.
UpdateIndex
{
DstServiceID
:
dstServiceID
,
SrcServiceID
:
srcServiceID
,
ReqType
:
reqType
,
SequenceNum
:
sequenceNum
,
Response
:
response
}}}
if
paraName
==
""
{
tx
:=
&
types
.
Transaction
{
Execer
:
[]
byte
(
BrokerX
),
Payload
:
types
.
Encode
(
payload
),
Fee
:
1e5
,
Nonce
:
rand
.
Int63n
(
time
.
Now
()
.
UnixNano
()),
To
:
Addr
}
...
...
@@ -52,6 +32,3 @@ func UpdateIndex(paraName, srcServiceID, dstServiceID string, sequenceNum, reqTy
}
}
func
EmitInterchainSetEvent
(){
}
proto/broker.proto
View file @
bd668bac
...
...
@@ -52,23 +52,26 @@ message UpdateIndex {
//响应信息
Response
response
=
5
;
}
// 跨链事件
message
InterchainEvent
{
//索引值,这个有系统自动生成
uint64
index
=
1
;
//目的链ID,固定格式0x000001
string
dstServiceID
=
2
;
//源链ID,固定格式0x000002
string
srcServiceID
=
3
;
//跨链交易类型 0表示storage,1表示coins,2表示token......
uint64
type
=
4
;
//调用方法
string
func
=
5
;
//参数列表
repeated
string
args
=
6
;
//状态 0表示开始处理 1表示跨链成功 2表示跨链失败
int32
status
=
7
;
//索引值,这个有系统自动生成
uint64
index
=
1
;
//目的链ID,固定格式0x000001
string
dstServiceID
=
2
;
//源链ID,固定格式0x000002
string
srcServiceID
=
3
;
//跨链交易类型 0表示storage,1表示coins,2表示token......
uint64
type
=
4
;
//方法集合分别表示 当前,回调,回滚方法 用,号分割 比如: "interchainGet,interchainSet,interchainRollback"
string
func
=
5
;
//参数列表
string
args
=
6
;
//回调参数
string
argscb
=
7
;
//回滚参数
string
argsrb
=
8
;
//状态 0表示开始处理 1表示跨链成功 2表示跨链失败
// int32 status = 7;
}
//ReceiptBrokerLog
message
ReceiptBrokerLog
{
...
...
types/broker.pb.go
View file @
bd668bac
This diff is collapsed.
Click to expand it.
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