超级账本hyperledger fabric第十一集:公民身份信息相关链码

  • 编写citizens下的代码,编写好后,拖到对应linux目录
package main

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

//个人基本信息
type People struct {
	//区分数据类型
	DataType string `json:"dataType"`
	//身份证号码
	Id string `json:"id"`
	//性别
	Sex string `json:"sex"`
	//姓名
	Name string `json:"name"`
	//出生地
	BrithLocation Location `json:"birthLocation"`
	//现居住地
	LiveLocation Location `json:"liveLocation"`
	//母亲身份证号
	MotherId string `json:"montherID"`
	//父亲身份证号
	FatherId string `json:"fatherID"`
}

//位置
type Location struct {
	//国家
	Country string `json:"country"`
	//省
	Province string `json:"province"`
	//城市
	City string `json:"city"`
	//镇
	Town string `json:"town"`
	//详细地址
	Detail string `json:"detail"`
}

//公民链
type CitizensChain struct {
}

//初始化方法
func (c *CitizensChain) Init(stub shim.ChaincodeStubInterface) pb.Response {
	function, _ := stub.GetFunctionAndParameters()
	if function != "init" {
		return shim.Error("方法名错误")
	}
	log.Println("初始化成功")
	return shim.Success(nil)
}

//链码交互入口
func (c *CitizensChain) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
	//接收参数判断
	function, args := stub.GetFunctionAndParameters()
	//判断
	if function == "register" {
		//录入公民信息
		return c.register(stub, args)
	} else if function == "query" {
		//查询公民信息
		return c.query(stub, args)
	} else {
		return shim.Error("无效的方法名")
	}
	return shim.Success(nil)
}

//录入公民信息
//-c Args[register,身份证号,json]
//参数1:身份证号,是存储的key
//参数2:个人信息,当成value取存储
func (c *CitizensChain) register(stub shim.ChaincodeStubInterface, args []string) pb.Response {
	//判断参数个数
	if len(args) != 2 {
		return shim.Error("参数错误")
	}
	//接收身份证号
	key := args[0]
	//接收公民信息(json)
	value := args[1]
	perple := People{}
	//转换
	err := json.Unmarshal([]byte(value), &perple)
	if err != nil {
		return shim.Error("注册失败,参数无法解析")
	}
	//更新世界状态
	stub.PutState(key, []byte(value))
	return shim.Success(nil)
}

//查询公民信息
func (c *CitizensChain) query(stub shim.ChaincodeStubInterface, args []string) pb.Response {
	if len(args) < 1 {
		return shim.Error("参数错误")
	}
	//接收查询的key
	key := args[0]
	//去世界状态中查数据
	result, err := stub.GetState(key)
	if err != nil {
		return shim.Error("查询失败")
	}
	return shim.Success(result)
}

func main() {
	err := shim.Start(new(CitizensChain))
	if err != nil {
		log.Println(err)
	}
}
  • 安装链码

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

  • 链码实例化

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

  • 身份录入

peer chaincode invoke -C mychannel -n citizens -c '{"Args":["register","110229","{\"dataType\":\"citizens\",\"id\":\"110229\",\"sex\":\"man\",\"name\":\"zhangsan\"}"]}'

  • 身份查询

peer chaincode query -C mychannel -n citizens -c '{"Args":["query","110229"]}'

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值