Exploring Credit Information Blockchain Solutions (Hyperledger)

Exploring Credit Information Blockchain Solutions (Hyperledger)

This article is excerpted from the e-book "Netkiller Blockchain Codex"

Netkiller Blockchain 手札

The author of this article is looking for a job recently and is interested in calling 13113668890

Mr. Neo Chan, Chen Jingfeng (BG7NYT)

Xishan Meidi, Minzhi Street, Longhua New District, Shenzhen, Guangdong, China 518131 +86 13113668890<[email protected]>

The document was created on 2018-02-10

Copyright © 2018 Netkiller(Neo Chan). All rights reserved.

Copyright Notice

Please contact the author for reprinting. When reprinting, please be sure to indicate the original source of the article, author information and this statement.

WeChat subscription account netkiller-ebook (Scan QR code on WeChat)

QQ: 13721218 Please indicate "reader"

QQ group: 128659835 Please indicate "reader"

Website: http://www.netkiller.cn

abstract

This e-book about blockchain development and operations.

Why write a blockchain e-book? Because 2018 is the year of blockchain.

Will this ebook be published (paper book)? No, because Internet technology is changing too fast, the content of paper books cannot be updated in real time. A book can easily become rubbish at a cost of 100 yuan. You will find that the current blockchain books on the market were written at least a year ago. , the content is outdated, and many examples do not work correctly. So I will not publish, the content of the e-book will follow the technological development, follow up the upgrade of the software version in time, and keep the content up-to-date, at least mainstream.

How is this eBook different from other blockchain books? Most of the blockchain books on the market use 2/3 to talk about the principles of blockchain. As long as less than 1/3 of the dry goods are not enough for theory, the whole article will discuss the theory or talk about the blockchain industry. , these contents are more of brainstorming and looking forward to the blockchain, but they cannot be implemented. This book is completely different from those books. It does not talk about theories and principles. It is application-oriented and focuses on examples. They are all dry goods.

How often are eBooks updated? New content will be added every day, and the update frequency will not exceed one week at the latest. Please follow https://github.com/netkiller/netkiller.github.io/commits/master for updated content

This article is written in fragments, the original text will be updated from time to time, please try to read the original text.

http://www.netkiller.cn/blockchain/index.html

Your reward is my motivation for writing: http://www.netkiller.cn/blockchain/donations.html

==============================

33.7. Exploration of Blockchain Credit Information Solutions

After reading countless articles, I couldn't find an article about how credit reporting is implemented on the blockchain. I also asked around in various blockchain WeChat groups and QQ groups, but no one knows how to land.

The current situation is that everyone knows that the blockchain is no problem for credit reporting. The district is centralized and cannot be edited. It is simply designed for the credit reporting system. So how to make the project come to fruition? Not a single article addresses this issue. It may be that some big companies have already achieved it, and they are in technical secrecy and have not shared it.

It seems that I can only rely on myself. Ethereum and Hyperledger have been researching. Recently, there have been more studies on Ethereum, but I found that the system of credit reporting is not suitable for implementation on Ethereum, so I returned to Hyperledger.

It feels a little uncomfortable to go back to the Hyperledger. There is no Token in the Hyperledger, and the contract implementation of the Hyperledger is completely different from that of Ethereum. Two systems are two kinds of thinking to solve the same blockchain needs.

At first glance, hyperledger is the current IBM style. Hyperledger has the characteristics of huge system, complex structure, difficult to understand, and complicated operation and maintenance. Simple problems are thought in complex ways, making a complex system with extremely poor usability. The characteristic of IBM's products is that you can only cooperate with them, and once they cooperate (on the boat), they can't fall off, from their minicomputers, to middleware products, and various industry solutions. There was a problem with IBM's system that only IBM people could solve.

With hyperledger open source, I hope that the style of hyperledger can break away from IBM's shadow.

Back to the topic, after studying several examples provided by hyperledger, I have some ideas on how to realize the credit reporting requirements.

First of all, the chaincode contract is not complicated and is organized by two core methods, Init and Invoke. Secondly, the data operation is similar to the map data structure, and the shim.ChaincodeStubInterface interface provides get, put, del and other operations.

33.7.1. Requirements Analysis and Outline Design

Questions about the information inquiry of the certification and credit system, how to inquire? 2. What information is inquired?

Block is not a relational database and cannot implement complex queries like SQL. Therefore, the design interface should cater to the blockchain as much as possible. In some cases, compromises are required to adapt to the weaknesses and deficiencies of the blockchain.

But we can let the database and the blockchain exist at the same time to make up for each other's shortcomings.

I won't talk about the database part here. The implementation of the blockchain is to use the ID number to query and return json data.

33.7.2. Data Structures

First of all, we define a structure to store identity information. The credit information is far more than these items. Please define it according to your actual situation.

package main

import "fmt"
import "encoding/json"

type Person struct {
	No string	`json:"no"`
	Name string	`json:"name"`
	Sex	bool	`json:"sex"`
	Age int		`json:"age"`
    Address string	`json:"address"`
}

func main(){
	
	person := &Person{"430725198001190911","景峯",true,30,"Shenzhen,China"}

	personJson, _ := json.Marshal(person)

	fmt.Println(string(personJson));

	person1 := &Person{
        No: "430725198001190911",
		Name: "Neo Chen",
		Sex: true,
		Age: 35,
		Address: "Shenzhen, China"}

    json2, _ := json.Marshal(person1)
    fmt.Println(string(json2))
}

Compile, run, test that the definition json is correct.

neo@MacBook-Pro ~/golang/contract % rm -rf person &&  go build person.go  && ./person
{"no":"430725198001190911","name":"景峯","sex":true,"age":30,"address":"Shenzhen,China"}
{"no":"430725198001190911","name":"Neo Chen","sex":true,"age":35,"address":"Shenzhen, China"}

In the end we just need to copy the structure into the contract code.

type Person struct {
	No string	`json:"no"`
	Name string	`json:"name"`
	Sex	bool	`json:"sex"`
	Age int		`json:"age"`
    Address string	`json:"address"`
}

33.7.3. Write credit information into the blockchain

Write the credit data to the blockchain through the following function.

func (s *SmartContract) createPerson(stub shim.ChaincodeStubInterface, args []string) sc.Response {
 
    if len(args) != 6 {
        return shim.Error("Incorrect number of arguments. Expecting 6")
    }
 
    var person = Person{No: args[1], Name: args[2], Sex: args[3], Age: args[4], Address: args[5]}
 
    personAsBytes, _ := json.Marshal(person)
    stub.PutState(args[0], personAsBytes)
 
    return shim.Success(nil)
}

33.7.4. Querying block data

Use the following methods to query the credit information on the chain.

func (s *SmartContract) queryPerson(stub shim.ChaincodeStubInterface, args []string) sc.Response {
 
    if len(args) != 1 {
        return shim.Error("Incorrect number of arguments. Expecting 1")
    }
    personAsBytes, _ := stub.GetState(args[0])
    return shim.Success(personAsBytes)
}

33.7.5. Deleting blocks

Delete credit data by the following methods.

func (s *SmartContract) deletePerson(stub shim.ChaincodeStubInterface, args []string) sc.Response {
 
    if len(args) != 1 {
        return shim.Error("Incorrect number of arguments. Expecting 1")
    }
    personAsBytes, _ := stub.GetState(args[0])
    
    err= stub.DelState(args[0])
	if err != nil {
		return shim.Error("Failed to delete Student from DB, key is: "+args[0])
	}
    
    return shim.Success(personAsBytes)
}

I just talked about the idea here, because my small server was covered because I refreshed (painted the wall) at home, and there was no hyperledger development environment, so I could not provide the complete contract code.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324467255&siteId=291194637
Recommended