1. 程式人生 > >超級賬本hyperledger fabric第十集:貸款還款相關鏈碼

超級賬本hyperledger fabric第十集:貸款還款相關鏈碼

  • 編寫Trace下的程式碼,編寫好後,拖到對應linux目錄

domain.go

package main

//銀行、賬戶、定義交易歷史
//定義銀行
type Bank struct {
	//名字
	BankName string `json:"BankName"`
	//金額
	Amount int `json:"Amount"`
	//1:貸款   2:還款
	Flag int `json:"Flag"`
	//起始時間
	StartTime string `json:"StartTime"`
	//結束時間
	EndTime string `json:"EndTime"`
}

//定義賬戶
type Account struct {
	//身份證號
	CardNo string `json:"CardNo"`
	//使用者名稱
	Aname string `json:"Aname"`
	//性別
	Gender string `json:"Gender"`
	//電話
	Mobile string `json:"Mobile"`
	//銀行
	Bank Bank `json:"Bank"`
	//交易歷史
	Histroys []HistoryItem
}

//定義交易歷史
type HistoryItem struct {
	//交易id
	TxID string
	//賬戶
	Account Account
}

main.go

package main

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

type TraceChaincode struct {
}

//初始化方法
func (t *TraceChaincode) Init(stub shim.ChaincodeStubInterface) peer.Response {
	//初始化測試資料
	initTest(stub)
	return shim.Success(nil)
}

//鏈碼入口invoke
//loan:貸款
//repayment:還款
//initTest:測試初始化
func (t *TraceChaincode) Invoke(stub shim.ChaincodeStubInterface) peer.Response {
	//得到方法名和引數
	fun, args := stub.GetFunctionAndParameters()
	//進行判斷
	if fun == "loan" {
		//貸款
		return loan(stub, args)
	} else if fun == "repayment" {
		//還款
		return repayment(stub, args)
	} else if fun == "initTest" {
		return initTest(stub)
	} else {
		return shim.Error("方法名錯誤")
	}
}

//測試方法
func initTest(stub shim.ChaincodeStubInterface) peer.Response {
	bank := Bank{
		BankName: "icbc",
		Amount:   6000,
		//貸款
		Flag:      1,
		StartTime: "2010-01-01",
		EndTime:   "2020-01-01",
	}
	bank1 := Bank{
		BankName: "abc",
		Amount:   1000,
		//還款
		Flag:      2,
		StartTime: "2010-02-01",
		EndTime:   "2020-02-01",
	}
	account := Account{
		CardNo:   "1234",
		Aname:    "jack",
		Gender:   "男",
		Mobile:   "15900000",
		Bank:     bank1,
		Histroys: nil,
	}
	account1 := Account{
		CardNo:   "123444",
		Aname:    "jack2",
		Gender:   "男",
		Mobile:   "1590000000",
		Bank:     bank,
		Histroys: nil,
	}

	//物件序列化,儲存
	accBytes, err := json.Marshal(account)
	if err != nil {
		return shim.Error("序列化賬戶失敗")
	}
	accBytes1, err := json.Marshal(account1)
	if err != nil {
		return shim.Error("序列化賬戶1失敗")
	}

	//儲存
	err = stub.PutState(account.CardNo, accBytes)
	if err != nil {
		return shim.Error("儲存賬戶失敗")
	}
	err = stub.PutState(account.CardNo, accBytes1)
	if err != nil {
		return shim.Error("儲存賬戶1失敗")
	}
	return shim.Success(nil)
}

func main() {
	err := shim.Start(new(TraceChaincode))
	if err != nil {
		fmt.Println("啟動鏈碼時發生錯誤")
	}
}

tracecc.go

package main

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

//定義貸款和還款
const (
	Bank_Flag_Loan      = 1
	Bank_Flag_Repayment = 2
)

//貸款
//-c Args:[loan,賬戶身份證號,銀行名字,金額]
func loan(stub shim.ChaincodeStubInterface, args []string) peer.Response {
	//判斷引數
	if len(args) != 3 {
		return shim.Error("引數個數錯誤")
	}
	//判斷型別
	v, err := strconv.Atoi(args[2])
	if err != nil {
		return shim.Error("型別錯誤")
	}

	//組裝資料
	bank := Bank{
		BankName:  args[1],
		Amount:    v,
		Flag:      Bank_Flag_Loan,
		StartTime: "2011-01-10",
		EndTime:   "2021-01-09",
	}
	account := Account{
		CardNo: args[0],
		Aname:  "jack",
		Gender: "男",
		Mobile: "159999999",
		Bank:   bank,
	}

	//儲存狀態
	b := putAccount(stub, account)
	//判斷
	if ! b {
		return shim.Error("儲存貸款資料失敗")
	}
	return shim.Success([]byte("儲存貸款資料成功"))
}

//序列化並儲存
//引數將要儲存的賬戶傳過來,返回布林
func putAccount(stub shim.ChaincodeStubInterface, account Account) bool {
	//序列化
	accBytes, err := json.Marshal(account)
	if err != nil {
		return false
	}
	//儲存資料
	err = stub.PutState(account.CardNo, accBytes)
	if err != nil {
		return false
	}
	return true
}

//還款
//-c Args:[repayment,賬戶身份證號,銀行名字,金額]
func repayment(stub shim.ChaincodeStubInterface, args []string) peer.Response {
	if len(args) != 3 {
		return shim.Error("引數個數錯誤")
	}
	v, err := strconv.Atoi(args[2])
	if err != nil {
		return shim.Error("指定的還款金額錯誤")
	}
	//組裝資料
	bank := Bank{
		BankName:  args[1],
		Amount:    v,
		Flag:      Bank_Flag_Repayment,
		StartTime: "2011-01-10",
		EndTime:   "2021-01-09",
	}
	account := Account{
		CardNo: args[0],
		Aname:  "jack",
		Gender: "男",
		Mobile: "159999999",
		Bank:   bank,
	}
	b := putAccount(stub, account)
	if !b {
		return shim.Error("存款失敗")
	}
	return shim.Success([]byte("存款成功"))
}
  • 保證網路基礎OK的情況下,安裝鏈碼

peer chaincode install -n trace -v 1.0.0 -l golang -p github.com/chaincode/Trace

  • 例項化鏈碼

peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n trace -l golang -v 1.0.0 -c '{"Args":["init"]}'

  • 執行測試

peer chaincode invoke -C mychannel -n trace -c '{"Args":["initTest"]}'

  • 貸款500W

peer chaincode invoke -C mychannel -n trace -c '{"Args":["loan","110229","icbc","5000000"]}'

  • 還款500

peer chaincode invoke -C mychannel -n trace -c '{"Args":["repayment","110229","icbc","500"]}'