How Hyperledger Fabric roll back data?

Viewed 181

I read documents about the way Hyperledger Fabric network accepts data to write to the ledger. It's done by endorser peers. But this is another case, I have a scenario like multiple times call PutState.

// 1. Update User 
err = stub.PutState(userKey, updateUser)
if err != nil {
    return nil, Response.Error
}
// 2. Update Product
err = stub.PutState(productKey, updateProduct)
if err != nil {
    return nil, Response.Error
}

It pretty stranges that if the second call (update product) failed, but the first call (update user) successful. The user data still appear in the ledger but it shouldn't. I have two questions:

  1. Can anyone explain to me or give me any documents related to the rollback in this case?

  2. How I can solve this problem to make a true rollback?

Fabric version 1.4

1 Answers

A proposal sent from Client to Peer might call ledger's methods multiple times. Normally, a successful chaincode invocation should reach return shim.Success(nil) chaincode example

When you invoke chaincode stub's methods (PutState, GetState,...), the chaincode service will send a message to the stream which is already connected to the Peer - Endorser to try to interact with the ledger. You can take a look at the Fabric's source code here - where the Endorser handle event from chaincode service.

If your first call success, the Peer response to the chaincode service with no error. This does not mean that the ledger is updated. A transaction is only successful while it is committed to a block.

When the second call is failed, the invocation will end with the error. The Peer will provide an error response to the caller. If the caller keeps sending that transaction to the Orderer, the Peer - Submitter still rejects it => No data is really updated => Nothing need to rollback

I think you should carefully read the transaction flow document

In your case, let's try to return shim.Error(err.Error()) when an error is triggered

Related