Commit c29f9964 authored by harrylee's avatar harrylee

add broker

parent 88dd7aa3
package broker
import (
"github.com/33cn/chain33-sdk-go/crypto"
"github.com/33cn/chain33-sdk-go/types"
"math/rand"
"time"
)
//构造发布跨链交易事件交易
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}}}
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
} else {
tx := &types.Transaction{Execer: []byte(paraName + BrokerX), Payload: types.Encode(payload), Fee: 1e5, Nonce: rand.Int63n(time.Now().UnixNano()), To: crypto.GetExecAddress(paraName + BrokerX)}
return tx, nil
}
}
//构造更新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}
return tx, nil
} else {
tx := &types.Transaction{Execer: []byte(paraName + BrokerX), Payload: types.Encode(payload), Fee: 1e5, Nonce: rand.Int63n(time.Now().UnixNano()), To: crypto.GetExecAddress(paraName + BrokerX)}
return tx, nil
}
}
\ No newline at end of file
package broker
const (
TyUnknowAction = iota + 100
TyRegisterAction
TyAuditAction
TyUpdateIndexAction
TyEmitInterchainEventAction
//NameRegisterAction = "Register"
//NameAuditAction = "Audit"
//NameUpdateIndexAction = "UpdateIndex"
//NameEmitInterchainEventAction = "EmitInterchainEvent"
FuncNamePollingEvent = "PollingEvent"
FuncNameQueryInnerMeta = "QueryInnerMeta"
FuncNameQueryOutterMeta = "QueryOutterMeta"
FuncNameQueryInMessage = "QueryInMessage"
FuncNameQueryOutMessage = "QueryOutMessage"
)
const BrokerX = "broker"
const Addr = "1MCftFynyvG2F4ED5mdHYgziDxx6vDrScs"
package broker
import (
"encoding/json"
"github.com/33cn/chain33-sdk-go/client"
"github.com/33cn/chain33-sdk-go/types"
)
func PollingEvent(prefix, url string,meta *types.Meta) (*types.InterChainEventList, error) {
jsonClient, err := client.NewJSONClient(prefix, url)
if err != nil {
return nil, err
}
jsonraw, err := json.Marshal(&types.PollingEvent{Meta:meta})
if err != nil {
return nil, err
}
query := client.Query4Jrpc{
Execer: prefix + BrokerX,
FuncName: FuncNamePollingEvent,
Payload: jsonraw,
}
var eventList types.InterChainEventList
err = jsonClient.Call("Chain33.Query", query, &eventList)
if err != nil {
return nil, err
}
return &eventList, nil
}
func QueryInnerMeta(prefix, url string) (*types.Meta, error) {
jsonClient, err := client.NewJSONClient(prefix, url)
if err != nil {
return nil, err
}
jsonraw, err := json.Marshal(&types.QueryInnerMeta{})
if err != nil {
return nil, err
}
query := client.Query4Jrpc{
Execer: prefix + BrokerX,
FuncName: FuncNameQueryInnerMeta,
Payload: jsonraw,
}
var meta types.Meta
err = jsonClient.Call("Chain33.Query", query, &meta)
if err != nil {
return nil, err
}
return &meta, nil
}
func QueryOutterMeta(prefix, url string) (*types.Meta, error) {
jsonClient, err := client.NewJSONClient(prefix, url)
if err != nil {
return nil, err
}
jsonraw, err := json.Marshal(&types.QueryOutterMeta{})
if err != nil {
return nil, err
}
query := client.Query4Jrpc{
Execer: prefix + BrokerX,
FuncName: FuncNameQueryInnerMeta,
Payload: jsonraw,
}
var meta types.Meta
err = jsonClient.Call("Chain33.Query", query, &meta)
if err != nil {
return nil, err
}
return &meta, nil
}
func QueryOutMessage(prefix, url,inServicePair string,index uint64) (*types.Response, error) {
jsonClient, err := client.NewJSONClient(prefix, url)
if err != nil {
return nil, err
}
jsonraw, err := json.Marshal(&types.QueryOutMessage{InServicePair:inServicePair,SequenceNum:index})
if err != nil {
return nil, err
}
query := client.Query4Jrpc{
Execer: prefix + BrokerX,
FuncName: FuncNameQueryOutMessage,
Payload: jsonraw,
}
var resp types.Response
err = jsonClient.Call("Chain33.Query", query, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func QueryInMessage(prefix, url,inServicePair string,index uint64) (*types.Response, error) {
jsonClient, err := client.NewJSONClient(prefix, url)
if err != nil {
return nil, err
}
jsonraw, err := json.Marshal(&types.QueryInMessage{InServicePair:inServicePair,SequenceNum:index})
if err != nil {
return nil, err
}
query := client.Query4Jrpc{
Execer: prefix + BrokerX,
FuncName: FuncNameQueryInMessage,
Payload: jsonraw,
}
var resp types.Response
err = jsonClient.Call("Chain33.Query", query, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
\ No newline at end of file
syntax = "proto3";
package types;
//option go_package = "../types";
message BrokerAction {
oneof value {
Register register = 1;
Audit audit = 2;
InterchainEvent emitInterchainEvent = 3;
UpdateIndex updateIndex =4;
}
int32 ty = 5;
}
// 业务合约注册模型: 0表示正在审核,1表示审核通过,2表示审核失败
message Register {
//业务合约名称
string exectorName = 1;
}
//审核
message Audit {
//业务合约名称
string exectorName = 1;
//状态码 0表示正在审核,1表示审核通过,2表示审核失败
string status = 2;
}
// 轮循事件
message PollingEvent {
Meta meta = 1;
}
// 更新跨链事件索引
message UpdateIndex {
//当前链已经处理到的位置
uint64 sequenceNum = 1;
//目的链服务ID,固定格式0x000001
string dstServiceID = 2;
//源链ID,固定格式0x000002
string srcServiceID = 3;
//请求类型 0表示信息流入 1表示信息流出 2表示回滚
uint64 reqType =4;
//响应信息
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;
}
//ReceiptBrokerLog
message ReceiptBrokerLog{
oneof value {
Register register = 1;
Audit audit = 2;
InterchainEvent emitInterchainEvent = 3;
UpdateIndex updateIndex =4;
}
int32 ty = 5;
}
service broker {}
//查询跨出事件
message QueryInterchainEvent{
uint64 index =1;
//目的链ID,固定格式0x000001
string dstServiceID = 2;
//源链ID,固定格式0x000002
string srcServiceID = 3;
}
//跨链事件列表
message InterChainEventList{
repeated InterchainEvent list =1;
}
////根据状态查看跨链事件
//message QueryInterchainEventList {
// //事件状态必填(默认是0,只查询待处理状态的事件)
// int32 status = 1;
// // 主键索引
// string primaryKey = 2;
// //单页返回多少条记录,默认返回10条,为了系统安全最多单次只能返回20条
// int32 count = 3;
// // 0降序,1升序,默认降序
// int32 direction = 4;
//}
////跨链事件列表
//message InterchainEventList {
// repeated InterchainEvent list = 1;
// string primaryKey = 2;
//}
message QueryOutterMeta{
}
message QueryInnerMeta{
}
message QueryInMessage{
string inServicePair = 1;
uint64 sequenceNum = 2;
}
message QueryOutMessage{
string inServicePair = 1;
uint64 sequenceNum = 2;
}
message Meta {
map<string,uint64> meta = 1;
}
message Response{
// A status code that should follow the HTTP status codes.
int32 status = 1;
// A message associated with the response code.
string message = 2;
// A payload that can be used to include metadata with this response.
bytes payload = 3;
}
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
...@@ -6,10 +6,9 @@ package types ...@@ -6,10 +6,9 @@ package types
import ( import (
context "context" context "context"
fmt "fmt" fmt "fmt"
math "math"
proto "github.com/golang/protobuf/proto" proto "github.com/golang/protobuf/proto"
grpc "google.golang.org/grpc" grpc "google.golang.org/grpc"
math "math"
) )
// Reference imports to suppress errors if they are not otherwise used. // Reference imports to suppress errors if they are not otherwise used.
......
...@@ -456,7 +456,9 @@ func init() { ...@@ -456,7 +456,9 @@ func init() {
proto.RegisterType((*LocalDataLog)(nil), "types.localDataLog") proto.RegisterType((*LocalDataLog)(nil), "types.localDataLog")
} }
func init() { proto.RegisterFile("wasm.proto", fileDescriptor_7d78909ad64e3bbb) } func init() {
proto.RegisterFile("wasm.proto", fileDescriptor_7d78909ad64e3bbb)
}
var fileDescriptor_7d78909ad64e3bbb = []byte{ var fileDescriptor_7d78909ad64e3bbb = []byte{
// 337 bytes of a gzipped FileDescriptorProto // 337 bytes of a gzipped FileDescriptorProto
......
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