using go routine in the chaincode #fabric-questions #fabric-chaincode


Matthew Sykes
 

It's less about a race and more about consistency. (A race can cause an inconsistency.) Imagine that when you run the contract on peer one, the sequence observed by the ledger is ["data1", "data2"]; on peer two the sequence can be ["data2", "data1"] because there's nothing that ensures one write occurs before another.


On Fri, Apr 16, 2021 at 1:56 PM elmira <elmira.ebrahimi@...> wrote:
 Hi,
Concurrency (using goroutine and go channel) can cause non-deterministic behavior in fabric.

I have not found any source or example on the use of Goroutines and go channels in fabric contracts; I need an example; Are there any links that I can read about it?.

I know that it is better not to use the following code, but I want to know how the race condition that is in the code causes inconsistency among peers.
I appritiate any help.

package main import (

"fmt"

"github.com/hyperledger/fabric/core/chaincode/shim"

"github.com/hyperledger/fabric/protos/peer"

)

type BadChainCode struct{

}

func (t *BadChainCode) Invoke(stub shim.ChaincodeStubInterface){


go writeToLedger(stub, “data1”)

go writeToLedger(stub, “data2”)

 

}
func writeToLedger(stub shim.ChaincodeStubInterface, data string){

stub.PutState(“key”,[]byte(data))

}



--
Matthew Sykes
matthew.sykes@...


elmira
 

 Hi,
Concurrency (using goroutine and go channel) can cause non-deterministic behavior in fabric.

I have not found any source or example on the use of Goroutines and go channels in fabric contracts; I need an example; Are there any links that I can read about it?.

I know that it is better not to use the following code, but I want to know how the race condition that is in the code causes inconsistency among peers.
I appritiate any help.

package main import (

"fmt"

"github.com/hyperledger/fabric/core/chaincode/shim"

"github.com/hyperledger/fabric/protos/peer"

)

type BadChainCode struct{

}

func (t *BadChainCode) Invoke(stub shim.ChaincodeStubInterface){


go writeToLedger(stub, “data1”)

go writeToLedger(stub, “data2”)

 

}
func writeToLedger(stub shim.ChaincodeStubInterface, data string){

stub.PutState(“key”,[]byte(data))

}