360-DairyDemo链码编写













创建个项目
创建一个dairy.go

package main

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

type DairyInfo struct {
	ID      string
	Name    string
	Date    string
	Quality string
	Yield   int
}

type DairyFarm struct {
}

func (t *DairyFarm) Init(stub shim.ChaincodeStubInterface) peer.Response {
	infos := []DairyInfo{
		DairyInfo{ID: "1", Name: "aaa", Date: "xxx", Quality: "good", Yield: 1},
		DairyInfo{ID: "2", Name: "bbb", Date: "xxx", Quality: "good", Yield: 2},
		DairyInfo{ID: "3", Name: "ccc", Date: "xxx", Quality: "good", Yield: 3},
	}

	for i := 0; i < len(infos); i++ {
		jsontext, err := json.Marshal(infos[i])
		if err != nil {
			return shim.Error("marshal fail")
		}

		err = stub.PutState(infos[i].ID, jsontext)

		if err != nil {
			return shim.Error("putstate fail")
		}
	}

	return shim.Success([]byte("init success"))
}

func (t *DairyFarm) Invoke(stub shim.ChaincodeStubInterface) peer.Response {

	funcName, args := stub.GetFunctionAndParameters()

	if "query" == funcName {
		t.query(stub, args)
	} else if "gethistory" == funcName {
		t.gethistory(stub, args)
	} else if "putvalue" == funcName {
		t.putvalue(stub, args)
	}

	return shim.Success([]byte("init success"))
}

func (t *DairyFarm) query(stub shim.ChaincodeStubInterface, args []string) peer.Response {
	value, err := stub.GetState(args[0])
	if err != nil {
		return shim.Error("getstate fail")
	}

	return shim.Success(value)
}

func (t *DairyFarm) gethistory(stub shim.ChaincodeStubInterface, args []string) peer.Response {
	keyiter, _ := stub.GetHistoryForKey(args[0])

	var list []string

	for keyiter.HasNext() {
		value, _ := keyiter.Next()
		txID := value.TxId
		txValue := value.Value
		txStatus := value.IsDelete
		txTime := value.Timestamp

		tm := time.Unix(txTime.Seconds, 0)
		timeStr := tm.Format("2006-01-02 15:04:05")

		all := fmt.Sprintf("ID:%s, value:%s, status:%s, time:%s",
			txID,
			txValue,
			txStatus,
			timeStr)

		list = append(list, all)
	}

	jsontext, _ := json.Marshal(list)
	return shim.Success(jsontext)
}

func (t *DairyFarm) putvalue(stub shim.ChaincodeStubInterface, args []string) peer.Response {
	keyID := args[0]
	value := args[1]
	err := stub.PutState(keyID, []byte(value))
	if err != nil {
		return shim.Error("putstate fail")
	}

	return shim.Success([]byte("write ledger success"))
}

func main() {
	error := shim.Start(new(DairyFarm))
	if error != nil {
		println("程序启动失败...")
		return
	}
	fmt.Println("程序启动成功...")
}












然后加工厂的chaincode

package main

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

type MachiningInfo struct {
	ID      string
	FromID    string
	Name    string
	Date string
	Baozhi   string
}

type Machining struct {
}

func (t *Machining) Init(stub shim.ChaincodeStubInterface) peer.Response {
	infos := []DairyInfo{
		DairyInfo{ID: "1", Name: "aaa", Date: "xxx", Quality: "good", Yield: 1},
		DairyInfo{ID: "2", Name: "bbb", Date: "xxx", Quality: "good", Yield: 2},
		DairyInfo{ID: "3", Name: "ccc", Date: "xxx", Quality: "good", Yield: 3},
	}

	for i := 0; i < len(infos); i++ {
		jsontext, err := json.Marshal(infos[i])
		if err != nil {
			return shim.Error("marshal fail")
		}

		err = stub.PutState(infos[i].ID, jsontext)

		if err != nil {
			return shim.Error("putstate fail")
		}
	}

	return shim.Success([]byte("init success"))
}

func (t *Machining) Invoke(stub shim.ChaincodeStubInterface) peer.Response {

	funcName, args := stub.GetFunctionAndParameters()

	if "query" == funcName {
		t.query(stub, args)
	} else if "gethistory" == funcName {
		t.gethistory(stub, args)
	} else if "putvalue" == funcName {
		t.putvalue(stub, args)
	}

	return shim.Success([]byte("init success"))
}

func (t *Machining) query(stub shim.ChaincodeStubInterface, args []string) peer.Response {
	value, err := stub.GetState(args[0])
	if err != nil {
		return shim.Error("getstate fail")
	}

	return shim.Success(value)
}

func (t *Machining) gethistory(stub shim.ChaincodeStubInterface, args []string) peer.Response {
	keyiter, _ := stub.GetHistoryForKey(args[0])

	var list []string

	for keyiter.HasNext() {
		value, _ := keyiter.Next()
		txID := value.TxId
		txValue := value.Value
		txStatus := value.IsDelete
		txTime := value.Timestamp

		tm := time.Unix(txTime.Seconds, 0)
		timeStr := tm.Format("2006-01-02 15:04:05")

		all := fmt.Sprintf("ID:%s, value:%s, status:%s, time:%s",
			txID,
			txValue,
			txStatus,
			timeStr)

		list = append(list, all)
	}

	jsontext, _ := json.Marshal(list)
	return shim.Success(jsontext)
}

func (t *Machining) putvalue(stub shim.ChaincodeStubInterface, args []string) peer.Response {
	keyID := args[0]
	value := args[1]
	err := stub.PutState(keyID, []byte(value))
	if err != nil {
		return shim.Error("putstate fail")
	}

	return shim.Success([]byte("write ledger success"))
}

func main() {
	error := shim.Start(new(Machining))
	if error != nil {
		println("程序启动失败...")
		return
	}
	fmt.Println("程序启动成功...")
}












然后销售组织的chaincode

package main

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

type SaleInfo struct {
	ID     string
	FromID string
	Name   string
	Price  float64
}

type DairyInfo struct {
	ID      string
	Name    string
	Date    string
	Quality string
	Yield   int
}

type Sale struct {
}

func (t *Sale) Init(stub shim.ChaincodeStubInterface) peer.Response {
	infos := []DairyInfo{
		DairyInfo{ID: "1", Name: "aaa", Date: "xxx", Quality: "good", Yield: 1},
		DairyInfo{ID: "2", Name: "bbb", Date: "xxx", Quality: "good", Yield: 2},
		DairyInfo{ID: "3", Name: "ccc", Date: "xxx", Quality: "good", Yield: 3},
	}

	for i := 0; i < len(infos); i++ {
		jsontext, err := json.Marshal(infos[i])
		if err != nil {
			return shim.Error("marshal fail")
		}

		err = stub.PutState(infos[i].ID, jsontext)

		if err != nil {
			return shim.Error("putstate fail")
		}
	}

	return shim.Success([]byte("init success"))
}

func (t *Sale) Invoke(stub shim.ChaincodeStubInterface) peer.Response {

	funcName, args := stub.GetFunctionAndParameters()

	if "query" == funcName {
		t.query(stub, args)
	} else if "gethistory" == funcName {
		t.gethistory(stub, args)
	} else if "putvalue" == funcName {
		t.putvalue(stub, args)
	} else if "trace" == funcName {
		t.trace(stub, args)
	}

	return shim.Success([]byte("init success"))
}

func (t *Sale) trace(stub shim.ChaincodeStubInterface, args []string) peer.Response {

	var list []string

	result := t.query(stub, args)
	if result.Status != shim.OK {
		return shim.Error("query fail")
	}

	var saleItem SaleInfo
	json.Unmarshal(result.Payload, &saleItem)

	info := fmt.Sprintf("sale name: %s", saleItem.Name)
	list = append(list, info)

	machinID := saleItem.FromID

	myargs := [][]byte{[]byte("query"), []byte(machinID)}
	result = stub.InvokeChaincode("machincc", myargs, "tracechannel")

	if result.Status != shim.OK {
		return shim.Error("invokechaincode fail")
	}

	var machiItem MachiningInfo
	json.Unmarshal(result.Payload, &machiItem)
	info = fmt.Sprintf("machining name: %s", machiItem.Name)
	list = append(list, info)

	dairyID := machiItem.FromID

	myargs = [][]byte{[]byte("query"), []byte(dairyID)}
	result = stub.InvokeChaincode("dairycc", myargs, "tracechannel")

	if result.Status != shim.OK {
		return shim.Error("invokechaincode fail")
	}

	var dairyItem DairyInfo
	json.Unmarshal(result.Payload, &dairyItem)

	info = fmt.Sprintf("dairy name: %s", dairyItem.Name)
	list = append(list, info)

	jsontext, _ := json.Marshal(list)
	shim.Success(jsontext)

}

func (t *Sale) query(stub shim.ChaincodeStubInterface, args []string) peer.Response {
	value, err := stub.GetState(args[0])
	if err != nil {
		return shim.Error("getstate fail")
	}

	return shim.Success(value)
}

func (t *Sale) gethistory(stub shim.ChaincodeStubInterface, args []string) peer.Response {
	keyiter, _ := stub.GetHistoryForKey(args[0])

	var list []string

	for keyiter.HasNext() {
		value, _ := keyiter.Next()
		txID := value.TxId
		txValue := value.Value
		txStatus := value.IsDelete
		txTime := value.Timestamp

		tm := time.Unix(txTime.Seconds, 0)
		timeStr := tm.Format("2006-01-02 15:04:05")

		all := fmt.Sprintf("ID:%s, value:%s, status:%s, time:%s",
			txID,
			txValue,
			txStatus,
			timeStr)

		list = append(list, all)
	}

	jsontext, _ := json.Marshal(list)
	return shim.Success(jsontext)
}

func (t *Sale) putvalue(stub shim.ChaincodeStubInterface, args []string) peer.Response {
	keyID := args[0]
	value := args[1]
	err := stub.PutState(keyID, []byte(value))
	if err != nil {
		return shim.Error("putstate fail")
	}

	return shim.Success([]byte("write ledger success"))
}

func main() {
	error := shim.Start(new(Sale))
	if error != nil {
		println("程序启动失败...")
		return
	}
	fmt.Println("程序启动成功...")
}





 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值