fabcar.go - The "fabcar" Chaincode

Q

What is the fabcar.go source code file?

✍: FYIcenter.com

A

File fabcar.go contains the source code the "fabcar" chaincode (or Smart Contract). A copy of fabcar.go file is available is the /opt/gopath/src directory on the "cli" container. You can open and read it.

1. Copy fabcar.go from cli container:

$ docker cp cli:/opt/gopath/src/github.com/fabcar/go/fabcar.go .

1. Take a look at fabcar.go:

$ more fabcar.go

/* The sample smart contract for documentation topic:
 * Writing Your First Blockchain Application
 */

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 {
}

type Car struct {
  Make   string `json:"make"`
  Model  string `json:"model"`
  Colour string `json:"colour"`
  Owner  string `json:"owner"`
}

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

/*
 * The Invoke method is called as a result of an application request to run the Smart Contract "fabcar"
 * 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 {

  // Retrieve the requested Smart Contract function and arguments
  function, args := APIstub.GetFunctionAndParameters()
  // Route to the appropriate handler function to interact with the ledger appropriately
  if function == "queryCar" {
    return s.queryCar(APIstub, args)
  } else if function == "initLedger" {
    return s.initLedger(APIstub)
  } else if function == "createCar" {
    return s.createCar(APIstub, args)
  } else if function == "queryAllCars" {
    return s.queryAllCars(APIstub)
  } else if function == "changeCarOwner" {
    return s.changeCarOwner(APIstub, args)
  }

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

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

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

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

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

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

// The main function is only relevant in unit test mode. Only included here for completeness.
func main() {

  // Create a new Smart Contract
  err := shim.Start(new(SmartContract))
  if err != nil {
    fmt.Printf("Error creating new Smart Contract: %s", err)
  }
}

As you can see, only the Invoke method is implemented, It takes a function name from the Invoke request message to perform different functionalities.

 

Interact with "fabcar" Chaincode on CLI

"fabric-nodeenv:latest not found" Error

WYFA (Writing Your First Application)

⇑⇑ Hyperledger Tutorials

2020-02-20, 1397🔥, 0💬