Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "encoding/json"
- "fmt"
- "strconv"
- "crypto/md5"
- "crypto/rand"
- "encoding/base64"
- "encoding/hex"
- "io"
- "github.com/hyperledger/fabric/core/chaincode/shim"
- pb "github.com/hyperledger/fabric/protos/peer"
- )
- /*
- 模拟实现招投标业务场景,一个公司组织机构对应多个诚信记录
- */
- type SimpleChaincode struct {
- }
- type Company struct {
- Companyid string
- Companyname string //公司名称
- Tell string //公司电话
- Addr string //公司地址
- Companymoneryid string //公司税号
- Tranid string //公司交易地址
- Credit int
- }
- type Trecord struct {
- Tranid string //交易记录地址
- Tname string //交易项目名称
- Ttime string //交易时间
- Tcos int //交易信用值增值
- }
- func GetAddress() (string, string, string) {
- var address, priKey, pubKey string
- b := make([]byte, 48)
- if _, err := io.ReadFull(rand.Reader, b); err != nil {
- return "", "", ""
- }
- h := md5.New()
- h.Write([]byte(base64.URLEncoding.EncodeToString(b)))
- address = hex.EncodeToString(h.Sum(nil))
- priKey = address + "1"
- pubKey = address + "2"
- return address, priKey, pubKey
- }
- /*
- 智能合约初始化调用,一个链码版本只运行一次
- */
- func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
- fmt.Println("Init ChainCode...............")
- return shim.Success(nil)
- }
- /*
- 智能合约发起请求时调用 {"Args":["invoke","a","b","10"]}
- func (stub *ChaincodeStub) GetFunctionAndParameters() (function string, params []string)
- 返回参数 function 标记调用方法的名称"invoke"
- 返回参数 params 标记调用方法的参数数组["a","b","10"]
- */
- func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
- // 获取请求调用智能合约的方法和参数
- function, args := stub.GetFunctionAndParameters()
- fmt.Println("recive params....",function," ",args)
- // Route to the appropriate handler function to interact with the ledger appropriately
- if function == "addcompany" {
- //新注册公司
- fmt.Println("run function addcompany",function," ",args)
- return t.addcompany(stub, args)
- } else if function == "updatecompany" {
- //更新公司交易信息
- fmt.Println("run function updatecompany",function," ",args)
- return t.updatecompany(stub, args)
- } else if function == "addtrans" {
- //给定公司添加交易信息
- fmt.Println("run function deletecard",function," ",args)
- return t.addtrans(stub, args)
- } else if function == "querycompany" {
- //查询公司注册信息
- fmt.Println("run function query",function," ",args)
- return t.querycompany(stub, args)
- } else if function == "querytranshistory" {
- //查询公司历史交易信息
- fmt.Println("run function queryhistory",function," ",args)
- return t.querytranshistory(stub, args)
- }
- return shim.Success(nil)
- }
- /*
- 向链码账本中添加一个新的公司信息
- */
- func (t *SimpleChaincode) addcompany(stub shim.ChaincodeStubInterface, args []string) pb.Response {
- fmt.Println("向账本中添加注册公司......")
- tranaddr,_,_:=GetAddress()
- var pertmp Company
- credit,_:=strconv.Atoi(args[5])
- pertmp = Company{Companyid: args[0],Companyname: args[1], Tell: args[2], Addr: args[3], Companymoneryid: args[4], Tranid: tranaddr, Credit: credit}
- fmt.Println("Record is ",pertmp)
- jsonrecode,_ := json.Marshal(&pertmp)
- fmt.Println("Record byte is :",jsonrecode)
- err:= stub.PutState(pertmp.Companyid, jsonrecode)
- fmt.Println("****************************")
- fmt.Println("put key is ", pertmp.Companyid)
- if err != nil {
- return shim.Error("Error retrieving data")
- }
- return shim.Success(jsonrecode)
- }
- /*
- 向链码中已经存在的记录做一次更新操作
- */
- func (t *SimpleChaincode) updatecompany(stub shim.ChaincodeStubInterface, args []string) pb.Response {
- fmt.Println("向账本中更新注册公司......")
- tranaddr,_,_:=GetAddress()
- var pertmp Company
- credit,_:=strconv.Atoi(args[5])
- pertmp = Company{Companyid: args[0],Companyname: args[1], Tell: args[2], Addr: args[3], Companymoneryid: args[4], Tranid: tranaddr, Credit: credit}
- fmt.Println("Record is ",pertmp)
- jsonrecode,_ := json.Marshal(&pertmp)
- fmt.Println("Record byte is :",jsonrecode)
- err:= stub.PutState(pertmp.Companyid, jsonrecode)
- fmt.Println("****************************")
- fmt.Println("put key is ", pertmp.Companyid)
- if err != nil {
- return shim.Error("Error retrieving data")
- }
- return shim.Success(jsonrecode)
- }
- /*
- 根据给定的ID信息更新最新的交易信息
- */
- func (t *SimpleChaincode) addtrans(stub shim.ChaincodeStubInterface, args []string) pb.Response {
- fmt.Println("给公司添加交易信息")
- companyid:=args[0]
- tname:=args[1]
- ttime:=args[2]
- tcos,_:=strconv.Atoi(args[3])
- companyjson,_:=stub.GetState(companyid)
- var tmpcompany Company
- err:=json.Unmarshal(companyjson,&tmpcompany)
- tmpcompany.Credit=tmpcompany.Credit-tcos
- transrecord:=Trecord{Tranid:tmpcompany.Tranid,Tname:tname,Ttime:ttime,Tcos:tcos}
- companyjson,_= json.Marshal(&tmpcompany)
- transrecordjson,_:= json.Marshal(&transrecord)
- err=stub.PutState(tmpcompany.Companyid,companyjson)
- err=stub.PutState(tmpcompany.Tranid,transrecordjson)
- if err != nil {
- return shim.Error("Error retrieving data")
- }
- fmt.Println("添加交易信息成功")
- return shim.Success(companyjson)
- }
- /*
- 查询链码中注册公司信息
- */
- func (t *SimpleChaincode) querycompany(stub shim.ChaincodeStubInterface, args []string) pb.Response {
- queryparams:=args[0]
- fmt.Println("query record by perid",queryparams)
- redBytes, err := stub.GetState(queryparams)
- if err != nil {
- shim.Error("Error retrieving data")
- }
- fmt.Println("##############################")
- fmt.Println("query resoult"+string(redBytes))
- fmt.Println("##############################")
- return shim.Success(redBytes)
- }
- /*
- 根绝给定的companyid查询出对应的所有的交易历史记录
- */
- func (t *SimpleChaincode) querytranshistory(stub shim.ChaincodeStubInterface, args []string) pb.Response {
- var companyid string=args[0]
- var records []Trecord
- var company Company
- fmt.Println("query key histroy......................",args[0])
- tmpcompany, err := stub.GetState(companyid)
- err=json.Unmarshal(tmpcompany,&company)
- fmt.Println(company.Companyname,"对应的交易ID为:",company.Tranid)
- historyIer, err := stub.GetHistoryForKey(company.Tranid)
- if err != nil {
- fmt.Println(err)
- return shim.Error("query error")
- }
- for historyIer.HasNext() {
- var tmp Trecord=Trecord{}
- modification, err := historyIer.Next()
- if err != nil {
- fmt.Println(err)
- return shim.Error("query error")
- }
- fmt.Println("Returning information about", string(modification.Value))
- err=json.Unmarshal(modification.Value,&tmp)
- records=append(records,tmp)
- }
- jsonrecode,_ := json.Marshal(&records)
- return shim.Success(jsonrecode)
- }
- func main() {
- fmt.Println("Fabric Chain Code Test..........")
- err := shim.Start(new(SimpleChaincode))
- if err != nil {
- fmt.Printf("Error starting Simple chaincode: %s", err)
- }
- }
Add Comment
Please, Sign In to add comment