Hyperledger Fabric Chaincode Error : cannot refer to unexported name shim.success

494 Views Asked by At

I am trying to build chaincode using hyperledger. I am using GoLang to write the contract, while building the contract i am facing the below error :

    cannot refer to unexported name shim.success
    undefined: shim.success

There may be few variable undefined errors. As My code is not getting build, I am unable to debug the code. Please find my code which i am using. I am unable to find the reason for the above error. Please help me in resolving this issue.

   import (
    "encoding/json"
    "fmt"
    "bytes"
    "time"
    "strings"
    "github.com/hyperledger/fabric/core/chaincode/shim"
    pb "github.com/hyperledger/fabric/protos/peer"
   )


   func (t *check) SurrenderSaves(stub shim.ChaincodeStubInterface, 
   args []string) pb.Response {
   fmt.Println("Entering CodeSurrenderSaves")
   var err error
   var lastImportDatekey string
   var lastImportDate []byte

   lastImportDate, err= stub.GetState("lastImprtDatesaved")
   fmt.Println("lastImportDate ...... ", lastImportDate)

   err = json.Unmarshal(lastImportDate, &lastImportDatekey)
   if err != nil {
   fmt.Printf("Unable to unmarshal lastImportDate input 
   lastImportDatekey: %s\n", err)
    return shim.Error(err.Error())
   }
   fmt.Println("lastImportDatekey ...... ", lastImportDatekey)

   if (lastImportDate == nil){
      currentTime := time.Now()
      var timeString = currentTime.Format("2006-01-02")
      lastImportDatekey = timeString
      fmt.Println("lastImportDatekey ...... ", lastImportDatekey)
   } else {
      err = json.Unmarshal(lastImportDate, &lastImportDatekey)
      if err != nil {
        fmt.Printf("Unable to unmarshal lastImportDate input 
        lastImportDate: %s\n", err)
        return shim.Error(err.Error())
      }
      fmt.Println(" lastImportDatekey end:",lastImportDatekey)
   }
    return shim.Success(nil)
   }

   func (t *check) Init(stub shim.ChaincodeStubInterface) pb.Response {
      fmt.Println("Initiate the chaincde")
      return shim.Success(nil)
   }
   func (t *check) Invoke(stub shim.ChaincodeStubInterface) pb.Response 
   {
      function, args := stub.GetFunctionAndParameters()
       if function == "SurrenderSaves" {
          return t.SurrenderSaves(stub, args)
       }
   fmt.Println("Function not found")
   return shim.Error("Received unknown function invocation")
   return nil, nil
   }
1

There are 1 best solutions below

5
On BEST ANSWER

Assuming the given code is your complete code, I can see some serious problems in your code.

  1. The check struct is absent
  2. You did not handle error after getting state information (this won't stop your program from building though)
  3. shim.success(nil) should be replaced with shim.Success(nil) [You edited your question and corrected this error, but it's important to point out this mistake because it was causing the error in your question title]
  4. There is an unnecessary return nil, nil line which is stopping your program from building correctly. I don't have any idea why you might've put this line there. Just remove it.
  5. There is no main function

After correcting all these mistakes, your code should look like something like this:

package main

import (
    "encoding/json"
    "fmt"
    "github.com/hyperledger/fabric/core/chaincode/shim"
    pb "github.com/hyperledger/fabric/protos/peer"
    "time"
)

//To-Do: Create check struct
type check struct {
}

func (t *check) SurrenderSaves(stub shim.ChaincodeStubInterface,
    args []string) pb.Response {
    fmt.Println("Entering CodeSurrenderSaves")
    var err error
    var lastImportDatekey string
    var lastImportDate []byte

    lastImportDate, err = stub.GetState("lastImprtDatesaved")

    //To-Do: handle error after getting state information
    if err != nil {
        fmt.Printf("Unable to get state : %s\n", err)
        return shim.Error(err.Error())
    }
    fmt.Println("lastImportDate ...... ", lastImportDate)

    err = json.Unmarshal(lastImportDate, &lastImportDatekey)
    if err != nil {
        fmt.Printf("Unable to unmarshal lastImportDate input lastImportDatekey: %s\n", err)
        return shim.Error(err.Error())
    }
    fmt.Println("lastImportDatekey ...... ", lastImportDatekey)

    if (lastImportDate == nil) {
        currentTime := time.Now()
        var timeString = currentTime.Format("2006-01-02")
        lastImportDatekey = timeString
        fmt.Println("lastImportDatekey ...... ", lastImportDatekey)
    } else {
        err = json.Unmarshal(lastImportDate, &lastImportDatekey)
        if err != nil {
            fmt.Printf("Unable to unmarshal lastImportDate input lastImportDate: %s\n", err)
            return shim.Error(err.Error())
        }
        fmt.Println(" lastImportDatekey end:", lastImportDatekey)
    }
    return shim.Success(nil) //To-Do: Success has to begin with uppercase S
}

func (t *check) Init(stub shim.ChaincodeStubInterface) pb.Response {
    fmt.Println("Initiate the chaincde")
    return shim.Success(nil)
}

func (t *check) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
    function, args := stub.GetFunctionAndParameters()
    if function == "SurrenderSaves" {
    return t.SurrenderSaves(stub, args)
    }
    fmt.Println("Function not found")
    return shim.Error("Received unknown function invocation")
    //return nil, nil //To-Do: Remove this line
}
//To-Do: Add main function
func main() {
    // Create a new Smart Contract
    err := shim.Start(new(check))
    if err != nil {
        fmt.Printf("Error creating new Smart Contract: %s", err)
    }
}

P.S. You have to install the new code in your network for the changes to be updated. For this you have to stop and teardown the network and then start it again. After that you will be able to install the updated version of the code in the newly created network.

Warning: This chaincode will fail if there is no value stored against the key "lastImprtDatesaved". Because when you call stub.GetState("lastImprtDatesaved") you will get the []byte representation of an empty string back. And you can not run json.Unmarshal(lastImportDate, &lastImportDatekey) on an empty string as it will end in an error.