Commit 7bce9c1a authored by jzhe's avatar jzhe

feat:handle ibtp-2.0

parent 4e3c0b70
......@@ -8,6 +8,6 @@ bin
cover.out
cover.html
coverage.txt
/example/contracts/src/broker/vendor/
/example/contracts/src/data_swapper/vendor/
/example/contracts/src/transfer/vendor/
example/contracts/src/broker/vendor/
example/contracts/src/data_swapper/vendor/
example/contracts/src/transfer/vendor/
This diff is collapsed.
......@@ -11,29 +11,44 @@ const (
ConfigName = "fabric.toml"
)
type Config struct {
Fabric Fabric `toml:"fabric" json:"fabric"`
Services []Service `mapstructure:"services" json:"services"`
}
type Fabric struct {
Addr string `toml:"addr" json:"addr"`
Name string `toml:"name" json:"name"`
EventFilter string `mapstructure:"event_filter" toml:"event_filter" json:"event_filter"`
Username string `toml:"username" json:"username"`
CCID string `toml:"ccid" json:"ccid"`
ChannelId string `mapstructure:"channel_id" toml:"channel_id" json:"channel_id"`
Org string `toml:"org" json:"org"`
Addr string `toml:"addr" json:"addr"`
Name string `toml:"name" json:"name"`
EventFilter string `mapstructure:"event_filter" toml:"event_filter" json:"event_filter"`
Username string `toml:"username" json:"username"`
CCID string `toml:"ccid" json:"ccid"`
ChannelId string `mapstructure:"channel_id" toml:"channel_id" json:"channel_id"`
Org string `toml:"org" json:"org"`
TimeoutHeight int64 `mapstructure:"timeout_height" json:"timeout_height"`
ChainID string `mapstructure:"chain_id" json:"chain_id"`
}
type Service struct {
ID string `toml:"id" json:"id"`
Name string `toml:"name" json:"name"`
Type string `toml:"type" json:"type"`
}
func DefaultConfig() *Fabric {
return &Fabric{
Addr: "40.125.164.122:10053",
Name: "fabric",
EventFilter: "CrosschainEventName",
Username: "Admin",
CCID: "Broker-001",
ChannelId: "mychannel",
Org: "org2",
func DefaultConfig() *Config {
return &Config{
Fabric: Fabric{
Addr: "40.125.164.122:10053",
Name: "fabric",
EventFilter: "CrosschainEventName",
Username: "Admin",
CCID: "Broker-001",
ChannelId: "mychannel",
Org: "org2",
},
Services: nil,
}
}
func UnmarshalConfig(configPath string) (*Fabric, error) {
func UnmarshalConfig(configPath string) (*Config, error) {
viper.SetConfigFile(filepath.Join(configPath, ConfigName))
viper.SetConfigType("toml")
viper.AutomaticEnv()
......
[fabric]
addr = "host.docker.internal:7053"
event_filter = "interchain-event-name"
username = "Admin"
ccid = "broker"
channel_id = "mychannel"
org = "org2"
\ No newline at end of file
org = "org2"
timeout_height = 10
chain_id = "3"
[[services]]
id = "mychannel&transfer"
name = "transfer"
[[services]]
id = "mychannel&data_swapper"
name = "data_swapper"
\ No newline at end of file
package main
import (
"fmt"
"strings"
"time"
"github.com/cloudflare/cfssl/log"
"github.com/meshplus/bitxhub-model/pb"
"github.com/meshplus/bitxid"
)
type Event struct {
Index uint64 `json:"index"`
DstContractDID string `json:"dst_contract_did"`
SrcContractID string `json:"src_contract_id"`
Func string `json:"func"`
Args string `json:"args"`
Callback string `json:"callback"`
Argscb string `json:"argscb"`
Rollback string `json:"rollback"`
Argsrb string `json:"argsrb"`
Proof []byte `json:"proof"`
Extra []byte `json:"extra"`
Index uint64 `json:"index"`
DstFullID string `json:"dst_full_id"`
SrcFullID string `json:"src_full_id"`
Func string `json:"func"`
Args string `json:"args"`
Argscb string `json:"argscb"`
Argsrb string `json:"argsrb"`
}
func (ev *Event) Convert2IBTP(srcMethod string, ibtpType pb.IBTP_Type) *pb.IBTP {
func (ev *Event) Convert2IBTP(timeoutHeight int64, ibtpType pb.IBTP_Type) *pb.IBTP {
pd, err := ev.encryptPayload()
if err != nil {
log.Fatalf("Get ibtp payload :%s", err)
}
return &pb.IBTP{
From: srcMethod,
To: string(bitxid.DID(ev.DstContractDID).GetChainDID()),
Index: ev.Index,
Type: ibtpType,
Timestamp: time.Now().UnixNano(),
Proof: ev.Proof,
Payload: pd,
Extra: ev.Extra,
From: ev.SrcFullID,
To: ev.DstFullID,
Index: ev.Index,
Type: ibtpType,
TimeoutHeight: timeoutHeight,
Payload: pd,
}
}
......@@ -51,15 +44,17 @@ func handleArgs(args string) [][]byte {
}
func (ev *Event) encryptPayload() ([]byte, error) {
funcSplit := strings.Split(ev.Func, ",")
if len(funcSplit) != 3 {
return nil, fmt.Errorf("ibtp func not is (func, callback,rollback)")
}
content := &pb.Content{
SrcContractId: ev.SrcContractID,
DstContractId: bitxid.DID(ev.DstContractDID).GetAddress(),
Func: ev.Func,
Args: handleArgs(ev.Args),
Callback: ev.Callback,
ArgsCb: handleArgs(ev.Argscb),
Rollback: ev.Rollback,
ArgsRb: handleArgs(ev.Argsrb),
Func: funcSplit[0],
Args: handleArgs(ev.Args),
Callback: funcSplit[1],
ArgsCb: handleArgs(ev.Argscb),
Rollback: funcSplit[2],
ArgsRb: handleArgs(ev.Argsrb),
}
data, err := content.Marshal()
if err != nil {
......
No preview for this file type
This diff is collapsed.
module github.com/meshplus/broker
go 1.13
require (
github.com/Knetic/govaluate v3.0.0+incompatible // indirect
github.com/Shopify/sarama v1.29.1 // indirect
github.com/fsouza/go-dockerclient v1.7.3 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/hashicorp/go-version v1.3.0 // indirect
github.com/hyperledger/fabric v1.4.3
github.com/hyperledger/fabric-amcl v0.0.0-20210603140002-2670f91851c8 // indirect
github.com/miekg/pkcs11 v1.0.3 // indirect
github.com/onsi/gomega v1.14.0 // indirect
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 // indirect
github.com/spf13/viper v1.8.1 // indirect
github.com/sykesm/zap-logfmt v0.0.4 // indirect
go.uber.org/zap v1.18.1 // indirect
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
google.golang.org/grpc v1.39.0 // indirect
)
replace github.com/golang/protobuf => github.com/golang/protobuf v1.3.2
......@@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/core/chaincode/shim"
......@@ -201,23 +200,3 @@ func (broker *Broker) checkWhitelist(stub shim.ChaincodeStubInterface, function
return broker.onlyWhitelist(stub)
}
func (broker *Broker) validDID(did string) bool {
s := strings.Split(did, ":")
if len(s) != 4 || s[0] != "did" || s[1] == "" || s[2] == "" || s[3] == "" {
return false
}
return true
}
func parseMethod(did string) string {
s := strings.Split(did, ":")
if len(s) != 4 || s[0] != "did" || s[1] == "" || s[2] == "" || s[3] == "" {
return ""
}
return fmt.Sprintf("%s:%s:%s:.", s[0], s[1], s[2])
}
func ErrInvalidDID(did string) pb.Response {
return errorResponse(fmt.Sprintf("Invalid did format for %s", did))
}
......@@ -21,9 +21,6 @@ func (broker *Broker) getOutMessage(stub shim.ChaincodeStubInterface, args []str
}
destChainMethod := args[0]
sequenceNum := args[1]
if !broker.validDID(destChainMethod) {
return ErrInvalidDID(destChainMethod)
}
key := broker.outMsgKey(destChainMethod, sequenceNum)
v, err := stub.GetState(key)
if err != nil {
......@@ -45,12 +42,9 @@ func (broker *Broker) getInMessage(stub shim.ChaincodeStubInterface, args []stri
if len(args) < 2 {
return shim.Error("incorrect number of arguments, expecting 2")
}
sourceChainMethod := args[0]
inServicePair := args[0]
sequenceNum := args[1]
if !broker.validDID(sourceChainMethod) {
return ErrInvalidDID(sourceChainMethod)
}
key := broker.inMsgKey(sourceChainMethod, sequenceNum)
key := broker.inMsgKey(inServicePair, sequenceNum)
v, err := stub.GetState(key)
if err != nil {
return shim.Error(err.Error())
......@@ -66,6 +60,14 @@ func (broker *Broker) getCallbackMeta(stub shim.ChaincodeStubInterface) pb.Respo
return shim.Success(v)
}
func (broker *Broker) getDstRollbackMeta(stub shim.ChaincodeStubInterface) pb.Response {
v, err := stub.GetState(dstRollbackMeta)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(v)
}
func (broker *Broker) markInCounter(stub shim.ChaincodeStubInterface, from string) error {
inMeta, err := broker.getMap(stub, innerMeta)
if err != nil {
......@@ -86,3 +88,15 @@ func (broker *Broker) markCallbackCounter(stub shim.ChaincodeStubInterface, from
return broker.putMap(stub, callbackMeta, meta)
}
func (broker *Broker) markDstRollbackCounter(stub shim.ChaincodeStubInterface, from string, index uint64) error {
meta, err := broker.getMap(stub, dstRollbackMeta)
if err != nil {
return err
}
meta[from] = index
return broker.putMap(stub, dstRollbackMeta, meta)
}
......@@ -62,9 +62,9 @@ func (s *DataSwapper) get(stub shim.ChaincodeStubInterface, args []string) pb.Re
return shim.Success(value)
case 2:
// args[0]: destination appchain contract did
// args[0]: destination service id
// args[1]: key
b := util.ToChaincodeArgs(emitInterchainEventFunc, args[0], "interchainGet", args[2], "interchainSet", args[2], "", "")
b := util.ToChaincodeArgs(emitInterchainEventFunc, args[0], "interchainGet,interchainSet,", args[1], args[1], "")
response := stub.InvokeChaincode(brokerContractName, b, channelID)
if response.Status != shim.OK {
return shim.Error(fmt.Errorf("invoke broker chaincode %s error: %s", brokerContractName, response.Message).Error())
......
module github.com/meshplus/data_swapper
go 1.13
require (
github.com/Knetic/govaluate v3.0.0+incompatible // indirect
github.com/Shopify/sarama v1.29.1 // indirect
github.com/fsouza/go-dockerclient v1.7.3 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/hashicorp/go-version v1.3.0 // indirect
github.com/hyperledger/fabric v1.4.3
github.com/hyperledger/fabric-amcl v0.0.0-20210603140002-2670f91851c8 // indirect
github.com/miekg/pkcs11 v1.0.3 // indirect
github.com/onsi/gomega v1.14.0 // indirect
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 // indirect
github.com/spf13/viper v1.8.1 // indirect
github.com/sykesm/zap-logfmt v0.0.4 // indirect
go.uber.org/zap v1.18.1 // indirect
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
google.golang.org/grpc v1.39.0 // indirect
)
replace github.com/golang/protobuf => github.com/golang/protobuf v1.3.2
module github.com/meshplus/transfer
go 1.13
require (
github.com/Knetic/govaluate v3.0.0+incompatible // indirect
github.com/Shopify/sarama v1.29.1 // indirect
github.com/fsouza/go-dockerclient v1.7.3 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/hashicorp/go-version v1.3.0 // indirect
github.com/hyperledger/fabric v1.4.3
github.com/hyperledger/fabric-amcl v0.0.0-20210603140002-2670f91851c8 // indirect
github.com/miekg/pkcs11 v1.0.3 // indirect
github.com/onsi/gomega v1.14.0 // indirect
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 // indirect
github.com/spf13/viper v1.8.1 // indirect
github.com/sykesm/zap-logfmt v0.0.4 // indirect
go.uber.org/zap v1.18.1 // indirect
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
google.golang.org/grpc v1.39.0 // indirect
)
replace github.com/golang/protobuf => github.com/golang/protobuf v1.3.2
......@@ -92,8 +92,7 @@ func (t *Transfer) transfer(stub shim.ChaincodeStubInterface, args []string) pb.
return shim.Success(nil)
case 4:
// args[0]: destination appchain contract did
destContractDID := args[0]
dstServiceID := args[0]
sender := args[1]
receiver := args[2]
amountArg := args[3]
......@@ -121,7 +120,7 @@ func (t *Transfer) transfer(stub shim.ChaincodeStubInterface, args []string) pb.
args := strings.Join([]string{sender, receiver, amountArg}, ",")
argsRb := strings.Join([]string{sender, amountArg}, ",")
b := util.ToChaincodeArgs(emitInterchainEventFunc, destContractDID, "interchainCharge", args, "", "", "interchainRollback", argsRb)
b := util.ToChaincodeArgs(emitInterchainEventFunc, dstServiceID, "interchainCharge,,interchainRollback", args, "", argsRb)
response := stub.InvokeChaincode(brokerContractName, b, channelID)
if response.Status != shim.OK {
return shim.Error(fmt.Errorf("invoke broker chaincode %s", response.Message).Error())
......
......@@ -3,26 +3,22 @@ module github.com/meshplus/pier-client-fabric
go 1.13
require (
github.com/OneOfOne/xxhash v1.2.5 // indirect
github.com/Rican7/retry v0.1.0
github.com/VividCortex/gohistogram v1.0.0 // indirect
github.com/cloudflare/cfssl v0.0.0-20190409034051-768cd563887f
github.com/elastic/gosigar v0.8.1-0.20180330100440-37f05ff46ffa // indirect
github.com/go-logfmt/logfmt v0.4.0 // indirect
github.com/golang/protobuf v1.4.3
github.com/golang/protobuf v1.5.2
github.com/google/certificate-transparency-go v1.1.0 // indirect
github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd
github.com/hashicorp/go-plugin v1.3.0
github.com/hyperledger/fabric v2.0.1+incompatible
github.com/hyperledger/fabric-chaincode-go v0.0.0-20200511190512-bcfeb58dd83a
github.com/hyperledger/fabric-lib-go v1.0.0 // indirect
github.com/hyperledger/fabric-protos-go v0.0.0-20200330074707-cfe579e86986
github.com/hyperledger/fabric-protos-go v0.0.0-20201028172056-a3136dde2354
github.com/hyperledger/fabric-sdk-go v1.0.0-alpha5
github.com/meshplus/bitxhub-model v1.2.1-0.20210524073042-f243c2fee25b
github.com/meshplus/bitxid v0.0.0-20210412025850-e0eaf0f9063a
github.com/meshplus/pier v1.7.1-0.20210524093640-1337e0a53318
github.com/meshplus/bitxhub-model v1.2.1-0.20210805064451-03258148acad
github.com/meshplus/pier v1.11.1-0.20210809064238-de527e06d443
github.com/spf13/viper v1.7.0
sigs.k8s.io/yaml v1.2.0 // indirect
)
replace (
......@@ -30,5 +26,4 @@ replace (
github.com/golang/protobuf => github.com/golang/protobuf v1.3.2
github.com/prometheus/client_golang => github.com/prometheus/client_golang v0.9.3
google.golang.org/genproto => google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884
google.golang.org/protobuf => google.golang.org/protobuf v1.21.0
)
This diff is collapsed.
......@@ -2,14 +2,13 @@ package main
import (
"fmt"
"time"
"github.com/meshplus/bitxhub-model/pb"
)
func (c *Client) generateCallback(original *pb.IBTP, args [][]byte, proof []byte, status bool) (result *pb.IBTP, err error) {
func (c *Client) generateCallback(original *pb.IBTP, args [][]byte, status bool) (result *pb.IBTP, err error) {
if original == nil {
return nil, fmt.Errorf("got nil ibtp to generate receipt: %w", err)
return nil, fmt.Errorf("got nil ibtp To generate receipt: %w", err)
}
pd := &pb.Payload{}
if err := pd.Unmarshal(original.Payload); err != nil {
......@@ -20,10 +19,9 @@ func (c *Client) generateCallback(original *pb.IBTP, args [][]byte, proof []byte
if err := originalContent.Unmarshal(pd.Content); err != nil {
return nil, fmt.Errorf("ibtp payload unmarshal: %w", err)
}
content := &pb.Content{
SrcContractId: originalContent.DstContractId,
DstContractId: originalContent.SrcContractId,
}
content := &pb.Content{}
typ := pb.IBTP_RECEIPT_SUCCESS
if status {
content.Func = originalContent.Callback
......@@ -31,6 +29,11 @@ func (c *Client) generateCallback(original *pb.IBTP, args [][]byte, proof []byte
} else {
content.Func = originalContent.Rollback
content.Args = originalContent.ArgsRb
typ = pb.IBTP_RECEIPT_FAILURE
}
if original.Type == pb.IBTP_ROLLBACK {
typ = pb.IBTP_RECEIPT_ROLLBACK
}
b, err := content.Marshal()
......@@ -46,19 +49,13 @@ func (c *Client) generateCallback(original *pb.IBTP, args [][]byte, proof []byte
return nil, err
}
typ := pb.IBTP_RECEIPT_SUCCESS
if !status {
typ = pb.IBTP_RECEIPT_FAILURE
}
return &pb.IBTP{
From: original.From,
To: original.To,
Index: original.Index,
Type: typ,
Timestamp: time.Now().UnixNano(),
Proof: proof,
Payload: pdb,
Version: original.Version,
From: original.From,
To: original.To,
Index: original.Index,
Type: typ,
Proof: original.Proof,
Payload: pdb,
Version: original.Version,
}, nil
}
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