mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-23 01:55:10 +00:00
Format Fabcar Chaincode in Idiomatic Go
Signed-off-by: Brett Logan <brett.t.logan@ibm.com>
This commit is contained in:
parent
df727853c8
commit
683ee8b347
1 changed files with 39 additions and 46 deletions
|
|
@ -7,7 +7,7 @@ package main
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"log"
|
||||
|
||||
"github.com/hyperledger/fabric-contract-api-go/contractapi"
|
||||
)
|
||||
|
|
@ -34,24 +34,24 @@ type QueryResult struct {
|
|||
// InitLedger adds a base set of cars to the ledger
|
||||
func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface) error {
|
||||
cars := []Car{
|
||||
Car{Make: "Toyota", Model: "Prius", Colour: "blue", Owner: "Tomoko"},
|
||||
Car{Make: "Ford", Model: "Mustang", Colour: "red", Owner: "Brad"},
|
||||
Car{Make: "Hyundai", Model: "Tucson", Colour: "green", Owner: "Jin Soo"},
|
||||
Car{Make: "Volkswagen", Model: "Passat", Colour: "yellow", Owner: "Max"},
|
||||
Car{Make: "Tesla", Model: "S", Colour: "black", Owner: "Adriana"},
|
||||
Car{Make: "Peugeot", Model: "205", Colour: "purple", Owner: "Michel"},
|
||||
Car{Make: "Chery", Model: "S22L", Colour: "white", Owner: "Aarav"},
|
||||
Car{Make: "Fiat", Model: "Punto", Colour: "violet", Owner: "Pari"},
|
||||
Car{Make: "Tata", Model: "Nano", Colour: "indigo", Owner: "Valeria"},
|
||||
Car{Make: "Holden", Model: "Barina", Colour: "brown", Owner: "Shotaro"},
|
||||
{Make: "Toyota", Model: "Prius", Colour: "blue", Owner: "Tomoko"},
|
||||
{Make: "Ford", Model: "Mustang", Colour: "red", Owner: "Brad"},
|
||||
{Make: "Hyundai", Model: "Tucson", Colour: "green", Owner: "Jin Soo"},
|
||||
{Make: "Volkswagen", Model: "Passat", Colour: "yellow", Owner: "Max"},
|
||||
{Make: "Tesla", Model: "S", Colour: "black", Owner: "Adriana"},
|
||||
{Make: "Peugeot", Model: "205", Colour: "purple", Owner: "Michel"},
|
||||
{Make: "Chery", Model: "S22L", Colour: "white", Owner: "Aarav"},
|
||||
{Make: "Fiat", Model: "Punto", Colour: "violet", Owner: "Pari"},
|
||||
{Make: "Tata", Model: "Nano", Colour: "indigo", Owner: "Valeria"},
|
||||
{Make: "Holden", Model: "Barina", Colour: "brown", Owner: "Shotaro"},
|
||||
}
|
||||
|
||||
for i, car := range cars {
|
||||
carAsBytes, _ := json.Marshal(car)
|
||||
err := ctx.GetStub().PutState("CAR"+strconv.Itoa(i), carAsBytes)
|
||||
|
||||
key := fmt.Sprintf("CAR%d", i)
|
||||
err := ctx.GetStub().PutState(key, carAsBytes)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to put to world state. %s", err.Error())
|
||||
return fmt.Errorf("failed to put to world state. %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -66,86 +66,79 @@ func (s *SmartContract) CreateCar(ctx contractapi.TransactionContextInterface, c
|
|||
Colour: colour,
|
||||
Owner: owner,
|
||||
}
|
||||
|
||||
carAsBytes, _ := json.Marshal(car)
|
||||
|
||||
carAsBytes, err := json.Marshal(car)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed marshalling to json: %v", err)
|
||||
}
|
||||
return ctx.GetStub().PutState(carNumber, carAsBytes)
|
||||
}
|
||||
|
||||
// QueryCar returns the car stored in the world state with given id
|
||||
func (s *SmartContract) QueryCar(ctx contractapi.TransactionContextInterface, carNumber string) (*Car, error) {
|
||||
carAsBytes, err := ctx.GetStub().GetState(carNumber)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to read from world state. %s", err.Error())
|
||||
return nil, fmt.Errorf("failed to read from world state: %v", err)
|
||||
}
|
||||
|
||||
if carAsBytes == nil {
|
||||
return nil, fmt.Errorf("%s does not exist", carNumber)
|
||||
}
|
||||
|
||||
car := new(Car)
|
||||
_ = json.Unmarshal(carAsBytes, car)
|
||||
|
||||
var car *Car
|
||||
err = json.Unmarshal(carAsBytes, car)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return car, nil
|
||||
}
|
||||
|
||||
// QueryAllCars returns all cars found in world state
|
||||
func (s *SmartContract) QueryAllCars(ctx contractapi.TransactionContextInterface) ([]QueryResult, error) {
|
||||
startKey := ""
|
||||
endKey := ""
|
||||
|
||||
resultsIterator, err := ctx.GetStub().GetStateByRange(startKey, endKey)
|
||||
|
||||
// Return all cars by using empty startKey and endKey
|
||||
resultsIterator, err := ctx.GetStub().GetStateByRange("", "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resultsIterator.Close()
|
||||
|
||||
results := []QueryResult{}
|
||||
|
||||
var results []QueryResult
|
||||
for resultsIterator.HasNext() {
|
||||
queryResponse, err := resultsIterator.Next()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
car := new(Car)
|
||||
_ = json.Unmarshal(queryResponse.Value, car)
|
||||
|
||||
var car *Car
|
||||
err = json.Unmarshal(queryResponse.Value, car)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed marshalling to json: %v", err)
|
||||
}
|
||||
queryResult := QueryResult{Key: queryResponse.Key, Record: car}
|
||||
results = append(results, queryResult)
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// ChangeCarOwner updates the owner field of car with given id in world state
|
||||
func (s *SmartContract) ChangeCarOwner(ctx contractapi.TransactionContextInterface, carNumber string, newOwner string) error {
|
||||
car, err := s.QueryCar(ctx, carNumber)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
car.Owner = newOwner
|
||||
|
||||
carAsBytes, _ := json.Marshal(car)
|
||||
|
||||
carAsBytes, err := json.Marshal(car)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed marshalling to json: %v", err)
|
||||
}
|
||||
return ctx.GetStub().PutState(carNumber, carAsBytes)
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
chaincode, err := contractapi.NewChaincode(new(SmartContract))
|
||||
|
||||
chaincode, err := contractapi.NewChaincode(&SmartContract{})
|
||||
if err != nil {
|
||||
fmt.Printf("Error create fabcar chaincode: %s", err.Error())
|
||||
return
|
||||
log.Panicf("Error create fabcar chaincode: %v", err)
|
||||
}
|
||||
|
||||
if err := chaincode.Start(); err != nil {
|
||||
fmt.Printf("Error starting fabcar chaincode: %s", err.Error())
|
||||
log.Panicf("Error starting fabcar chaincode: %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue