Commit 7bce9c1a authored by jzhe's avatar jzhe

feat:handle ibtp-2.0

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