Commit 02effabf authored by dawn-to-dusk's avatar dawn-to-dusk

fix(contracts): fix an index error when exchanging assets

Before this fix, a reverse asset exchange after a forward asset exchange process will report an index error because the confirm function is called after redeem but is not after init.
parent f9ba0931
No preview for this file type
......@@ -197,7 +197,8 @@ func (broker *Broker) InterchainAssetExchangeRedeemInvoke(stub shim.ChaincodeStu
newArgs := make([]string, 0)
newArgs = append(newArgs, args[0], cid, args[1], "interchainAssetExchangeRedeem", args[2], "interchainAssetExchangeConfirm")
return broker.InterchainInvoke(stub, newArgs)
resp := broker.InterchainInvoke(stub, newArgs)
return broker.modifyIndex(stub, args, newArgs, resp)
}
func (broker *Broker) InterchainAssetExchangeRefundInvoke(stub shim.ChaincodeStubInterface, args []string) pb.Response {
......@@ -212,7 +213,40 @@ func (broker *Broker) InterchainAssetExchangeRefundInvoke(stub shim.ChaincodeStu
newArgs := make([]string, 0)
newArgs = append(newArgs, args[0], cid, args[1], "interchainAssetExchangeRefund", args[2], "interchainAssetExchangeConfirm")
return broker.InterchainInvoke(stub, newArgs)
resp := broker.InterchainInvoke(stub, newArgs)
return broker.modifyIndex(stub, args, newArgs, resp)
}
func (broker *Broker) modifyIndex(stub shim.ChaincodeStubInterface, args []string, newArgs []string, resp pb.Response) pb.Response {
if resp.Status == shim.OK {
meta, err := broker.getMap(stub, callbackMeta)
if err != nil {
return shim.Error(err.Error())
}
outMeta, err := broker.getMap(stub, outterMeta)
if err != nil {
return shim.Error(err.Error())
}
inMeta, err := broker.getMap(stub, innerMeta)
if err != nil {
return shim.Error(err.Error())
}
if outMeta[args[0]] > inMeta[args[0]] {
meta[args[0]] = outMeta[args[0]] - 1
} else {
meta[args[0]] = inMeta[args[0]] - 1
}
err = broker.putMap(stub, callbackMeta, meta)
if err != nil {
return shim.Error(err.Error())
}
}
return resp
}
// InterchainInvoke
......
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