praveen
praveen

Reputation: 71

Cannot build the chaincode

Installed the below chaincode in chaincode-docker-devmode network it works fine.

But when I tried to install the chaincode in the basic-network in the fabric-sample getting the following error.

Help me to solve this.

asset.go:10:2: cannot find package >"github.com/hyperledger/fabric/core/chaincode >/shim" in any of: /opt/go/src/github.com/hyperledger/fabric/core/chaincode/shim (from >$GOR >OOT) /opt/gopath/src/github.com/hyperledger/fabric/core/chaincode/shim >>(from $GOPATH) asset.go:11:2: cannot find package "github.com/hyperledger/fabric/protos/peer" i n any of: /opt/go/src/github.com/hyperledger/fabric/protos/peer (from $GOROOT) /opt/gopath/src/github.com/hyperledger/fabric/protos/peer (from $GOPATH)

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "strconv"

    "github.com/hyperledger/fabric/core/chaincode/shim"
    sc "github.com/hyperledger/fabric/protos/peer"
    )

// Define the Smart Contract structure

type SmartContract struct {
}

//Define assets structure

type Asset struct {
    asset_id string `json:"asset_id"`
    asset_name string `json:"asset_name"`
    owner_aadhar int `json:"owner_aadhar"`
    owner_name string `json:"owner_name"`
    debt bool `json:"debt"`
    lamount int `json:"lamount"`
    objection string `json:"objection"`
    }


func (s *SmartContract) Init(APIstub shim.ChaincodeStubInterface) sc.Response {
    return shim.Success(nil)
}

// The calling application program has also specified the particular smart contract function to be called, with arguments

func (s *SmartContract) Invoke(APIstub shim.ChaincodeStubInterface) sc.Response {

    function, args := APIstub.GetFunctionAndParameters()

    if function == "initLedger"{
        return s.initLedger(APIstub)
    }

    if function == "transfer_asset"{
        return s.transfer_asset(APIstub,args)
    }

    if function == "create_asset"{
        return  s.create_asset(APIstub,args)
    }

    if function == "view_asset" {
        return s.view_asset(APIstub,args)
    }


    return shim.Error("Invalid Smart Contract function name.")
}

//stores some sample assets

func (s *SmartContract) initLedger(APIstub shim.ChaincodeStubInterface) sc.Response {
    assets := []Asset{
        Asset{asset_id:"A101", asset_name:"1bhk", owner_aadhar:101, owner_name:"praveen", debt:false, lamount:0, objection:"nill"},
        Asset{asset_id:"A102", asset_name:"factory",owner_aadhar:102, owner_name:"arun", debt:false, lamount:0, objection:"nill"},
        Asset{asset_id:"A103", asset_name:"abt school", owner_aadhar:103, owner_name:"kevin", debt:true, lamount:100000, objection:"nill"},
        Asset{asset_id:"A104", asset_name:"2bhk", owner_aadhar:104, owner_name:"baaruni",debt:false, lamount:0, objection:"1case"},
    }

    i := 0

    for i< len(assets)  {
        assetAsBytes, _ :=json.Marshal(assets[i])
        APIstub.PutState(assets[i].asset_id, assetAsBytes)
        i++
    }

    return shim.Success(nil)
}

func (s *SmartContract) create_asset(APIstub shim.ChaincodeStubInterface, args[] string) sc.Response  {
    if len(args)!=7 {
        return shim.Error("Incorrect number of arguments expected 7")
    }
    taadhar, _ :=strconv.Atoi(args[2])
    tdept, _ := strconv.ParseBool(args[4])
    tlamount, _ := strconv.Atoi(args[5])
    var asset = Asset{asset_id:args[0], asset_name:args[1], owner_aadhar:taadhar, owner_name:args[3], debt:tdept, lamount:tlamount, objection:args[6]}
    assetAsBytes, _ := json.Marshal(asset)
    APIstub.PutState(asset.asset_id,assetAsBytes)
    return shim.Success(nil)
}

func (s *SmartContract) transfer_asset(APIstub shim.ChaincodeStubInterface, args[] string) sc.Response{

    if len(args) !=3 {
        return shim.Error("Incorrect number of arguments expected 3")
    }

    assetAsBytes, _ :=APIstub.GetState(args[0])
    asset := Asset{}

    json.Unmarshal(assetAsBytes, &asset)

    if (asset.objection!="nill")||(asset.debt!=false){
        return shim.Error("Asset cannot be transfer as it has some issues")
    }
    asset.owner_aadhar, _ = strconv.Atoi(args[1])

    assetAsBytes, _ =json.Marshal(asset)
    APIstub.PutState(args[0],assetAsBytes)

    return shim.Success(nil)

}

func (s *SmartContract) view_asset(APIstub shim.ChaincodeStubInterface, args[] string) sc.Response  {
    if len(args) !=1{
        return shim.Error("Invalid number of arguments expected 1")
    }

    assetAsBytes, _ :=APIstub.GetState(args[0])
    asset := Asset{}

    json.Unmarshal(assetAsBytes, &asset)
    return shim.Success(assetAsBytes)
}

Upvotes: 3

Views: 2181

Answers (2)

Abhijit
Abhijit

Reputation: 15

there are new links to import shim and peer. Below worked fine.

go get github.com/hyperledger/fabric-chaincode-go/shim
go get github.com/hyperledger/fabric-protos-go/peer

Refer this link - https://hyperledger-fabric.readthedocs.io/en/release-2.0/chaincode4ade.html

Upvotes: 0

Shahid Hussain
Shahid Hussain

Reputation: 1779

You will need to get following go libraries to compile chaincode locally.

go get github.com/hyperledger/fabric/protos/peer
go get github.com/hyperledger/fabric/core/chaincode/shim

These libraries will be installed under GOPATH, make sure it is set.

In case if you get any(specific to context and plugin) errors for "go get" command , then follow the procedure mentioned below to resolve.

Upgrade to latest version of GO.

sudo apt-get purge golang*

sudo rm -rf /usr/lib/go-1.6/ /usr/lib/go-1.6/src/ /usr/lib/go-1.6/src/runtime/ /usr/lib/go-1.6/src/runtime/race

curl -O https://storage.googleapis.com/golang/go1.11.1.linux-amd64.tar.gz

sudo tar -C /usr/local -xzf go1.11.1.linux-amd64.tar.gz

mkdir -p ~/go; echo "export GOPATH=$HOME/go" >> ~/.bashrc

echo "export PATH=$PATH:$HOME/go/bin:/usr/local/go/bin" >> ~/.bashrc

source ~/.bashrc

Then

go get github.com/hyperledger/fabric/core/chaincode/shim
go get github.com/hyperledger/fabric/protos/peer

Upvotes: 1

Related Questions