Re: A sequence of GetState(key1) ==> PutState(key1,val2) ==> GetState(key1) returns old value for key1.

Alessandro Sorniotti
 

Hi

The scenario you describe works as expected: a chaincode invocation doesn't actually write to the ledger; instead, the proposed ledger changes are captured in the "read-write set" artefact. The read-write set is later placed in a transaction message. After ordering, if the transaction complies with the endorsement policy, its read-write set is applied to the ledger, at which point its changes will become visible to future chaincode invocations. This means that a chaincode execution is unable to see its own writes to the ledger: this will only be possible after the transaction is ordered.

More details here: https://arxiv.org/abs/1801.10228

Cheers,
Ale

On Thu, 14 Feb 2019, at 9:22 AM, Vishal wrote:
Hi,

One of the Invoke operation in chaincode has below sequence of APIs
calls:
* GetState(key1) ==>returns val1
* PutState(key1,val2) ==> returns shim.OK
* *GetState(key1) ==> returns val1, instead it should return val2*
*But, the opertaion returns the correct value i.e. val2 when it called
one more time.*

Here is the actual code snippet from the chaincode:
```
    // Get current value of available fund under the project
    prjBytes, err := stub.GetState(p.ProjectID)
    if err != nil {
        cErr := &chainError{ "readProject" , p.ProjectID,
CODEGENEXCEPTION, err}
        return shim.Error(cErr.Error())
    }
    prj := &project{}
    err = json.Unmarshal(prjBytes, prj)
    if err != nil {
        cErr := &chainError{ "readProject" , p.ProjectID,
CODEGENEXCEPTION, err}
        return shim.Error(cErr.Error())
    }
    // calculate the new value
    prj.Data.AvlFund =
prj.Data.AvlFund.Add(donationAggregate.Sub(spendAggregate))

    prjBytes, err = json.Marshal(prj)
    if err != nil {
        cErr := &chainError{ "readProject" , p.ProjectID, CODEGENEXCEPTION, err}
        return shim.Error(cErr.Error())
    }

    // Save the update value of available funds under a project
    err = stub.PutState(p.ProjectID, prjBytes)
    if err != nil {
        cErr := &chainError{ "readProject" , p.ProjectID, CODEGENEXCEPTION, err}
        return shim.Error(cErr.Error())
    }

    // Read and return the updated project state
    prjBytes, err = stub.GetState(p.ProjectID)
    if err != nil {
        cErr := &chainError{ "readProject" , p.ProjectID, CODEGENEXCEPTION, err}
        return shim.Error(cErr.Error())
    }
    r := response{CODEALLAOK, p.ProjectID, prjBytes}
    return shim.Success((r.formatResponse()))
}

```


Join fabric@lists.hyperledger.org to automatically receive all group messages.