Search code examples
hyperledger-fabricchaincode

Hyperledger Fabric golang chaincode not working as expected store data on ledger manually but not when try to store via function call


i am trying to store fund transfer record on hyperledger fabric. i have written chain code in go lang. it work fine when i add data in initLedger function. but when i call it from other function like createTransfer(i will provide both codes) it show successful transaction but when i retrieve chain data .it does not appear in it.

transfer struct

type Transfer struct {
    TransferID  string `json:"transferID"`
    FromAccount string `json:"fromAccount"`
    ToAccount   string `json:"toAcount"`
    Amount      string `json:"amount"`
}

this function write data to ledger: it workis fine when i directly call it in initLedger method

func writeTransferToLedger(APIStub shim.ChaincodeStubInterface, transfers []Transfer) sc.Response {

    for i := 0; i < len(transfers); i++ {
        key := transfers[i].TransferID
        chkBytes, _ := APIStub.GetState(key)
        if chkBytes == nil {
            asBytes, _ := json.Marshal(transfers[i])
            err := APIStub.PutState(transfers[i].TransferID, asBytes)
            if err != nil {
                return shim.Error(err.Error())
            }
        } else {
            msg := "Transfer already exist" + key + " Failure---------------"
            return shim.Error(msg)
        }
    }
    return shim.Success([]byte("Write to Ledger"))
}

Call writeToTransferLedger method with in createTransfer function:

func (s *SmartContract) createTransfer(APIStub shim.ChaincodeStubInterface, args []string) sc.Response {
    if len(args) != 4 {
        return shim.Error("Incorrect Number of arguments for transfer func, Expecting 4")
    }
    transfers := []Transfer{Transfer{TransferID: args[0], FromAccount: args[1], ToAccount: args[2], Amount: args[3]}}

    writeTransferToLedger(APIStub, transfers)
    return shim.Success([]byte("stored:" + args[0] + args[1] + args[2] + args[3]))
}

when i call createTransfer from nodesdk code it execute succefully but when i retrive data from chain code not thing return.

i Expect it to work with createTransfer function as it is working with writeTransferToLedger.

inside initLedger method i have created transfer struct with given data and called writeTransferToLedger function code is given below:

transfer := []Transfer{
        {TransferID: "1233", FromAccount: "US_John_Doe_123", ToAccount: "UK_Alice_456", Amount: "200"},
        {TransferID: "231", FromAccount: "JPY_Alice_456", ToAccount: "UK_John_Doe", Amount: "3000"},
    }
    writeTransferToLedger(APIstub, transfer)

Solution

  • thanks for your help . i have resolved the issue. i was calling invoke function when trying to retrieve data from customer ledger. i have to query the ledger and get transfer data from ledger.