Unverified Commit db96b03e authored by Aiden X's avatar Aiden X Committed by GitHub

Merge pull request #6 from meshplus/feat/new-rollback-mechanism

feat(example): broker rollback with unique id
parents 3f7ba498 221b40a5
......@@ -285,11 +285,11 @@ func (c *Client) SubmitIBTP(ibtp *pb.IBTP) (*model.PluginResponse, error) {
newArgs = append(newArgs, pd.Args[0])
newArgs = append(newArgs, result...)
case "interchainCharge":
newArgs = append(newArgs, []byte("false"), pd.Args[0])
newArgs = append(newArgs, []byte("false"))
if response.OK {
newArgs[0] = []byte("true")
}
newArgs = append(newArgs, pd.Args[2:]...)
newArgs = append(newArgs, []byte(fmt.Sprintf("%s-%d", ibtp.To, ibtp.Index)))
}
ret.Result, err = c.generateCallback(ibtp, newArgs, proof)
......
......@@ -137,8 +137,28 @@ func (broker *Broker) InterchainTransferInvoke(stub shim.ChaincodeStubInterface,
return shim.Error(err.Error())
}
destChainID := args[0]
destAddr := args[1]
sender := args[2]
receiver := args[3]
amount := args[4]
outMeta, err := broker.getMap(stub, outterMeta)
if err != nil {
return shim.Error(err.Error())
}
uuid := destChainID + "-" + strconv.FormatUint(outMeta[destChainID]+1, 10)
b, err := json.Marshal(&Tx{
sender: sender,
receiver: receiver,
amount: amount,
})
if err = stub.PutState(uuid, b); err != nil {
return shim.Error(err.Error())
}
newArgs := make([]string, 0)
newArgs = append(newArgs, args[0], cid, args[1], "interchainCharge", strings.Join(args[2:], ","), "interchainConfirm")
newArgs = append(newArgs, destChainID, cid, destAddr, "interchainCharge", strings.Join(args[2:], ","), "interchainConfirm")
return broker.InterchainInvoke(stub, newArgs)
}
......
......@@ -17,6 +17,12 @@ type response struct {
Data []byte `json:"data"`
}
type Tx struct {
sender string
receiver string
amount string
}
func successResponse(data []byte) pb.Response {
res := &response{
OK: true,
......
package main
import (
"encoding/json"
"fmt"
"strconv"
"strings"
......@@ -59,8 +60,7 @@ func (broker *Broker) interchainConfirm(stub shim.ChaincodeStubInterface, args [
sequenceNum := args[1]
targetCID := args[2]
status := args[3]
receiver := args[4]
amount := args[5]
uuid := args[4]
if err := broker.checkIndex(stub, sourceChainID, sequenceNum, callbackMeta); err != nil {
return errorResponse(err.Error())
......@@ -80,12 +80,21 @@ func (broker *Broker) interchainConfirm(stub shim.ChaincodeStubInterface, args [
return successResponse(nil)
}
val, err := stub.GetState(uuid)
if err != nil {
return shim.Error(err.Error())
}
originalTx := &Tx{}
if err = json.Unmarshal(val, originalTx); err != nil {
return shim.Error(err.Error())
}
splitedCID := strings.Split(targetCID, delimiter)
if len(splitedCID) != 2 {
return errorResponse(fmt.Sprintf("Target chaincode id %s is not valid", targetCID))
}
b := util.ToChaincodeArgs("interchainRollback", receiver, amount)
b := util.ToChaincodeArgs("interchainRollback", originalTx.receiver, originalTx.amount)
response := stub.InvokeChaincode(splitedCID[1], b, splitedCID[0])
if response.Status != shim.OK {
return errorResponse(fmt.Sprintf("invoke chaincode '%s' err: %s", splitedCID[1], response.Message))
......
......@@ -18,7 +18,7 @@ require (
github.com/hyperledger/fabric-lib-go v1.0.0 // indirect
github.com/hyperledger/fabric-sdk-go v1.0.0-alpha5
github.com/meshplus/bitxhub-kit v1.0.0-rc1
github.com/meshplus/bitxhub-model v1.0.0-rc1
github.com/meshplus/bitxhub-model v1.0.0-rc2
github.com/meshplus/pier v0.0.0-00010101000000-000000000000
github.com/miekg/pkcs11 v1.0.3 // indirect
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 // indirect
......
......@@ -176,8 +176,8 @@ github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4er
github.com/golang/mock v1.0.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.3.1 h1:qGJ6qTW+x6xX/my+8YUVl4WNpX9B7+/l2tRsHGZ7f2s=
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/golang/mock v1.4.3 h1:GV+pQPG/EUUbkh47niozDcADz6go/dUwhVzdUQHIVRw=
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
......@@ -357,13 +357,11 @@ github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsO
github.com/mattn/goveralls v0.0.2/go.mod h1:8d1ZMHsd7fW6IRPKQh46F2WRpyib5/X4FOpevwGNQEw=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/meshplus/bitxhub-kit v0.0.0-20200329124935-585edb85ca63 h1:WDsTW19L/BEaMukcTV2VQXinXhP2n3Y5nyTKeNPC5BI=
github.com/meshplus/bitxhub-kit v0.0.0-20200329124935-585edb85ca63/go.mod h1:ra/AhOkPvpElI+wXrB9G6DjdcrdxFU3vMwA5MYKr9D0=
github.com/meshplus/bitxhub-kit v1.0.0-rc1 h1:gNi8IFU5CMHT3KE2I4ACj5alMW9h/4cV8xOxn7wSmtA=
github.com/meshplus/bitxhub-kit v1.0.0-rc1/go.mod h1:ra/AhOkPvpElI+wXrB9G6DjdcrdxFU3vMwA5MYKr9D0=
github.com/meshplus/bitxhub-model v1.0.0-rc1 h1:9UajwmMvtOBLADzoJP+GxsaLMx3jew38Zp7xW+91sPM=
github.com/meshplus/bitxhub-model v1.0.0-rc1/go.mod h1:mJ6ucVyePaAojmQ8Y1NAS4PoZzCNxC/Q6EHC18HyMvw=
github.com/meshplus/go-bitxhub-client v1.0.0-rc1/go.mod h1:m4xazS4EjimY57I84L8ey37Iuq16haaC79yc3w+tr9Y=
github.com/meshplus/bitxhub-model v1.0.0-rc2 h1:rjeVmRe/I+NHm3AR6BZwDrIUIrKIhQoiovSEHGA9J38=
github.com/meshplus/bitxhub-model v1.0.0-rc2/go.mod h1:ZCctQIYTlE3vJ8Lhkrgs9bWwNA+Dw4JzojOSIzLVU6E=
github.com/meshplus/go-bitxhub-client v1.0.0-rc2/go.mod h1:4NSsWx4MIsBh9lIxF+zdlo3eDP4DVZM9qrYV4GdJPeI=
github.com/miekg/pkcs11 v1.0.2/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs=
github.com/miekg/pkcs11 v1.0.3 h1:iMwmD7I5225wv84WxIG/bmxz9AXjWvTWIbM/TYHvWtw=
github.com/miekg/pkcs11 v1.0.3/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs=
......@@ -748,5 +746,7 @@ mvdan.cc/unparam v0.0.0-20190209190245-fbb59629db34 h1:duVSyluuJA+u0BnkcLR01smoL
mvdan.cc/unparam v0.0.0-20190209190245-fbb59629db34/go.mod h1:H6SUd1XjIs+qQCyskXg5OFSrilMRUkD8ePJpHKDPaeY=
mvdan.cc/unparam v0.0.0-20190720180237-d51796306d8f h1:Cq7MalBHYACRd6EesksG1Q8EoIAKOsiZviGKbOLIej4=
mvdan.cc/unparam v0.0.0-20190720180237-d51796306d8f/go.mod h1:4G1h5nDURzA3bwVMZIVpwbkw+04kSxk3rAtzlimaUJw=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sourcegraph.com/sqs/pbtypes v0.0.0-20180604144634-d3ebe8f20ae4 h1:JPJh2pk3+X4lXAkZIk2RuE/7/FoK9maXw+TNPJhVS/c=
sourcegraph.com/sqs/pbtypes v0.0.0-20180604144634-d3ebe8f20ae4/go.mod h1:ketZ/q3QxT9HOBeFhu6RdvsftgpsbFHBF5Cas6cDKZ0=
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