Commit bd668bac authored by harrylee's avatar harrylee

add data swap method

parent 44887f8c
...@@ -301,77 +301,84 @@ func (client *JSONClient) QueryBlockInfo(start, end int64, isDetail bool) (*Bloc ...@@ -301,77 +301,84 @@ func (client *JSONClient) QueryBlockInfo(start, end int64, isDetail bool) (*Bloc
return &res, nil return &res, nil
} }
// 调用chaincode // 发布跨链事件
func (client *JSONClient) Invoke(requst *Request, privateKey string) (*Response, error) { func (client *JSONClient) EmitDataSwapInterchainEvent(dstServiceID, args, argscb, argsrb string, privateKey string) (*Response, error) {
// FIXME 这里需完善 // FIXME 这里需完善 funcName="interchainGet,interchainSet," txType=0 表示是存在类数据交换
if requst.Exec == storage.StorageX { tx, err := broker.EmitInterchainEvent("", dstServiceID, "interchainGet,interchainSet,", args, argsrb, argsrb, 0)
switch requst.Fcn { if err != nil {
case "set", "ContentStorage": return nil, err
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)
}
} }
return nil, fmt.Errorf("not matching execution or method!") return client.SendTransactionSync(privateKey, tx)
} }
// TODO 执行请求 // TODO 执行请求
func (client *JSONClient) Execute(requst *Request,privateKey string) (*Response, error) { func (client *JSONClient) Execute(requst *Request, privateKey string) (*Response, error) {
if requst.Exec == broker.BrokerX{ if requst.Exec == broker.BrokerX {
switch requst.Fcn { switch requst.Fcn {
case "invokeInterchain": case "invokeInterchain":
srcChainServiceID := string(requst.Args[0]) 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 { 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]) targetCID := string(requst.Args[2])
reqType, err := strconv.ParseUint(string(requst.Args[3]), 10, 64) reqType, err := strconv.ParseUint(string(requst.Args[3]), 10, 64)
if err != nil { 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 { if tx, err := broker.UpdateIndex("", srcChainServiceID, targetCID, sequenceNum, reqType, nil); err == nil {
resp,err:=client.SendTransactionSync(privateKey,tx) resp, err := client.SendTransactionSync(privateKey, tx)
if err !=nil { if err != nil {
return resp,err return resp, err
} }
//splitedCID[1]就是合约名称 //splitedCID[1]就是合约名称
splitedCID := strings.Split(targetCID, "&") splitedCID := strings.Split(targetCID, "&")
if len(splitedCID) != 2 { 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{} callFunc := &CallFunc{}
if err := json.Unmarshal(requst.Args[4], callFunc); err != nil { 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封装的,用于支持跨链之前数据的查询 //interchainGet方法是在sdk封装的,用于支持跨链之前数据的查询
switch callFunc.Func { switch callFunc.Func {
case "interchainGet": case "interchainGet":
return client.InterChainGet(string(callFunc.Args[0])) 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{ } else {
return nil,fmt.Errorf("can't support exec %s",splitedCID[1]) 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) { func (client *JSONClient) InterChainGet(key string) (*Response, error) {
jsonraw, err := json.Marshal(&types.QueryStorage{TxHash: key}) jsonraw, err := json.Marshal(&types.QueryStorage{TxHash: key})
...@@ -388,9 +395,9 @@ func (client *JSONClient) InterChainGet(key string) (*Response, error) { ...@@ -388,9 +395,9 @@ func (client *JSONClient) InterChainGet(key string) (*Response, error) {
if err != nil { if err != nil {
return nil, err 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 return resp, nil
} }
......
...@@ -9,18 +9,8 @@ import ( ...@@ -9,18 +9,8 @@ import (
) )
// 构造发布跨链交易事件交易 // 构造发布跨链交易事件交易
func EmitInterchainEvent(paraName, dstServiceID, funcName string, args []string, txType uint64) (*types.Transaction, error) { func EmitInterchainEvent(paraName, dstServiceID, funcName string, args,argscb,argsrb string, txType uint64) (*types.Transaction, error) {
//目的链ID,固定格式0x000001 payload := &types.BrokerAction{Ty: TyEmitInterchainEventAction, Value: &types.BrokerAction_EmitInterchainEvent{EmitInterchainEvent: &types.InterchainEvent{DstServiceID: dstServiceID, Type: txType, Func: funcName, Args: args,Argscb:argscb,Argsrb:argsrb}}}
//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}}}
if paraName == "" { if paraName == "" {
tx := &types.Transaction{Execer: []byte(BrokerX), Payload: types.Encode(payload), Fee: 1e5, Nonce: rand.Int63n(time.Now().UnixNano()), To: Addr} tx := &types.Transaction{Execer: []byte(BrokerX), Payload: types.Encode(payload), Fee: 1e5, Nonce: rand.Int63n(time.Now().UnixNano()), To: Addr}
return tx, nil return tx, nil
...@@ -32,16 +22,6 @@ func EmitInterchainEvent(paraName, dstServiceID, funcName string, args []string, ...@@ -32,16 +22,6 @@ func EmitInterchainEvent(paraName, dstServiceID, funcName string, args []string,
// 构造更新Index交易 // 构造更新Index交易
func UpdateIndex(paraName, srcServiceID, dstServiceID string, sequenceNum, reqType uint64, response *types.Response) (*types.Transaction, error) { 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}}} payload := &types.BrokerAction{Ty: TyUpdateIndexAction, Value: &types.BrokerAction_UpdateIndex{UpdateIndex: &types.UpdateIndex{DstServiceID: dstServiceID, SrcServiceID: srcServiceID, ReqType: reqType, SequenceNum: sequenceNum, Response: response}}}
if paraName == "" { if paraName == "" {
tx := &types.Transaction{Execer: []byte(BrokerX), Payload: types.Encode(payload), Fee: 1e5, Nonce: rand.Int63n(time.Now().UnixNano()), To: Addr} 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 ...@@ -52,6 +32,3 @@ func UpdateIndex(paraName, srcServiceID, dstServiceID string, sequenceNum, reqTy
} }
} }
func EmitInterchainSetEvent(){
}
...@@ -52,23 +52,26 @@ message UpdateIndex { ...@@ -52,23 +52,26 @@ message UpdateIndex {
//响应信息 //响应信息
Response response = 5; Response response = 5;
} }
// 跨链事件 // 跨链事件
message InterchainEvent { message InterchainEvent {
//索引值,这个有系统自动生成 //索引值,这个有系统自动生成
uint64 index = 1; uint64 index = 1;
//目的链ID,固定格式0x000001 //目的链ID,固定格式0x000001
string dstServiceID = 2; string dstServiceID = 2;
//源链ID,固定格式0x000002 //源链ID,固定格式0x000002
string srcServiceID = 3; string srcServiceID = 3;
//跨链交易类型 0表示storage,1表示coins,2表示token...... //跨链交易类型 0表示storage,1表示coins,2表示token......
uint64 type =4; uint64 type =4;
//调用方法 //方法集合分别表示 当前,回调,回滚方法 用,号分割 比如: "interchainGet,interchainSet,interchainRollback"
string func = 5; string func = 5;
//参数列表 //参数列表
repeated string args = 6; string args = 6;
//状态 0表示开始处理 1表示跨链成功 2表示跨链失败 //回调参数
int32 status = 7; string argscb =7;
//回滚参数
string argsrb =8;
//状态 0表示开始处理 1表示跨链成功 2表示跨链失败
// int32 status = 7;
} }
//ReceiptBrokerLog //ReceiptBrokerLog
message ReceiptBrokerLog{ message ReceiptBrokerLog{
......
This diff is collapsed.
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