Error while Querying System Chaincode

0 votes
I am trying to query the chaincode using the following code:

func (t *SimpleChaincode) expt(stub shim.ChaincodeStubInterface, args      []string) pb.Response{

var plp ledger.PeerLedgerProvider
var pl ledger.PeerLedger
lId,err:=plp.List()
if err != nil {
    return shim.Error("Invalid transaction amount, expecting a integer value")
}
fmt.Println(" List lists the ids of the existing ledgers")
fmt.Println(lId)

yesNO,err:=plp.Exists(lId[0])
if err != nil {
    return shim.Error("Invalid transaction amount, expecting a integer value")
}
fmt.Println(" tells whether the ledger with given id exists")
fmt.Println(yesNO)

txid:=args[0]
tx,err:=pl.GetTransactionByID(txid)
if err != nil {
    return shim.Error("Invalid transaction amount, expecting a integer value")
}
fmt.Println("transaction")
fmt.Println(tx)

return shim.Success(nil)
}

But i get the following error:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x38 pc=0x40301b]
goroutine 7 [running]:
panic(0x9eb1a0, 0xc420016040)
/opt/go/src/runtime/panic.go:500 +0x1a1
 main.(*SimpleChaincode).expt(0xfaa000, 0xf71120, 0xc42016ae80, 0xc4202faad0, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, ...)
/chaincode/input/src/github.com/example_cc/example_cc.go:191 +0x5b
main.(*SimpleChaincode).Invoke(0xfaa000, 0xf71120, 0xc42016ae80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
/chaincode/input/src/github.com/example_cc/example_cc.go:98 +0x430
github.com/hyperledger/fabric/core/chaincode/shim.(*Handler).handleTransaction.func1(0xc4202389a0, 0xc4200119a0)
/opt/gopath/src/github.com/hyperledger/fabric/core/chaincode/shim/handler.go:317 +0x483
created by github.com/hyperledger/fabric/core/chaincode/shim.(*Handler).handleTransaction
/opt/gopath/src/github.com/hyperledger/fabric/core/chaincode/shim/handler.go:328 +0x49

how to solve this
Jul 12, 2018 in Blockchain by slayer
• 29,350 points
612 views

1 answer to this question.

0 votes
Do not interact directly with the ledger.. Instead, use existing APIs of ChaincodeStubInterface provided for Invoke and Init methods of chaincode.

You can implement your logic with the following logic:

func (smartContract) Invoke(stub shim.ChaincodeStubInterface) peer.Response {

    funcName, params := stub.GetFunctionAndParameters()

    indexName := "txID~key"

    if funcName == "addNewKey" {

        key := params[0]
        value := params[1]

        keyTxIdKey, err := stub.CreateCompositeKey(indexName, []string{stub.GetTxID(), key})
        if err != nil {
            return shim.Error(err.Error())
        }

        creator, _ := stub.GetCreator()

        // Add key and value to the state
        stub.PutState(key, []byte(value))
        stub.PutState(keyTxIdKey, creator)

    } else if funcName == "checkTxID" {
        txID := params[0]

        it, _ := stub.GetStateByPartialCompositeKey(indexName, []string{txID})

        for it.HasNext() {
            keyTxIdRange, err := it.Next()
            if err != nil {
                return shim.Error(err.Error())
            }

            _, keyParts, _ := stub.SplitCompositeKey(keyTxIdRange.Key)
            key := keyParts[1]
            fmt.Printf("key affected by txID %s is %s\n", txID, key)
            txIDCreator := keyTxIdRange.Value

            sId := &msp.SerializedIdentity{}
            err := proto.Unmarshal(txIDCreator, sId)
            if err != nil {
                return shim.Error(fmt.Sprintf("Could not deserialize a SerializedIdentity, err %s", err))
            }

            bl, _ := pem.Decode(sId.IdBytes)
            if bl == nil {
                return shim.Error(fmt.Sprintf("Could not decode the PEM structure"))
            }
            cert, err := x509.ParseCertificate(bl.Bytes)
            if err != nil {
                return shim.Error(fmt.Sprintf("ParseCertificate failed %s", err))
            }

            fmt.Printf("Certificate of txID %s creator is %s", txID, cert)
        }
    }

    return shim.Success(nil)
}
answered Jul 12, 2018 by digger
• 26,740 points

Related Questions In Blockchain

+1 vote
1 answer

Error while deploying chaincode in IBM Blockchain. Please help.

Try stripping out the 'tree/master' portion of ...READ MORE

answered Apr 26, 2018 in Blockchain by Christine
• 15,790 points
637 views
+1 vote
1 answer

Error while instantiating chaincode

Hey @Vikas!  The error says, dial unix /host/var/run/docker.sock: connect: ...READ MORE

answered Nov 6, 2018 in Blockchain by Omkar
• 69,210 points
1,090 views
0 votes
1 answer

Unknown MSP DEFAULT error query chaincode

./byfn.sh -m up After running the above command, ...READ MORE

answered Jun 8, 2018 in Blockchain by Christine
• 15,790 points
603 views
0 votes
1 answer

NPM Error While Installing Lisk Dependencies

Meet the lib sodium fails. You're using node ...READ MORE

answered Jun 19, 2018 in Blockchain by charlie_brown
• 7,720 points
499 views
0 votes
1 answer
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,215 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP