Easy learning block chain 4 - Block Chaining browser human-computer interaction

MiniBC block chain V003 - Block Chaining browser human-computer interaction

work goals

Block chain is the browser user to view the block chain by way of the browser all the information. Our goal in this section is to achieve this functionality.
We provide not only viewing, but also provides a management function. In a real block chain projects, typically offer a variety of interactive ways, such as providing client command line mode, websocket way and so, ultimately providing a unified service by the block chain rpc server. We have to realize browser way, it will gradually expand.

Http Server

Http server in golang write a very simple, a few lines of code can be achieved.

http.HandleFunc("/", handleIndex)
http.ListenAndServe(":8080", nil)

Our block chain golang browser is the use of a standard package of services http completed. Very simple to use, call NewBlockBrower create a chain block browser object, and then call the Start method can work:

//启动区块链浏览器,您可以通过浏览器 http://SERVER_ADDR:8080 访问
NewBlockBrower(bc).Start()

Block chain browser

Block chain structure is relatively simple browser, a notice of withdrawal of the channel and the current block chain objects BlockChain:

type BlockBrower struct {
	//中止BrowserServer通道
	chanQuit chan bool

	//当前区块链
	blockChain *BlockChain
}

Start block chain-browser way to Start:
At present a total block chain server can handle four Url: "/", "/ shutdown", "/ getblocks" and "/ generateblock", listening on port 8080.
If MiniBC run, if you open the browser through the machine, can be entered directly: HTTP: // localhost : 8080

func (bb *BlockBrower) Start() {
	fmt.Println("=========================================================================")
	fmt.Println("MiniBC 区块链浏览器已经启动,请通过浏览器http://" + SERVER_ADDR + ":8080访问....")
	fmt.Println("=========================================================================")

	http.HandleFunc("/", bb.handleIndex)
	http.HandleFunc("/shutdown", bb.handleShutdown)
	http.HandleFunc("/getblocks", bb.handleGetBlocks)
	http.HandleFunc("/generateblock", bb.handleGenerateBlock)

	go http.ListenAndServe(":8080", nil)

	//只有接到退出通知,才能结束
	select {
		case <-bb.chanQuit:
	}
}

Achieve a 4 Url handler:

Processing Url is http: // SERVER_ADDR: 8080 / requests, browser home page display block chain information

func (bb *BlockBrower) handleIndex(response http.ResponseWriter, request *http.Request) {
	content := "<html><br>"
	content = content + "<b>&nbsp;&nbsp;&nbsp;&nbsp;MiniBC区块链区块浏览器</b><br><br>"
	content = content + "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"shutdown\">关闭MiniBC</a>"
	content = content + "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"getblocks\">查看区块链</a>&nbsp;&nbsp;&nbsp;&nbsp;"
	content = content + "</html>"

	response.Write([]byte(content))
}

Processing Url is http: // SERVER_ADDR: 8080 / shutdown request block chain closed the browser to exit the system

func (bb *BlockBrower) handleShutdown(response http.ResponseWriter, request *http.Request) {
	fmt.Println("")
	fmt.Println("")
	fmt.Println("=========================================================================")
	fmt.Println("MiniBC 区块链和远程管理结束,谢谢使用!")
	fmt.Println("=========================================================================")

	bb.chanQuit <- true
}

Processing Url is http: // SERVER_ADDR: 8080 getblocks request / print all the blocks chain information

func (bb *BlockBrower) handleGetBlocks(response http.ResponseWriter, request *http.Request) {

	//获取区块链高度
	blockHeight := strconv.Itoa(bb.blockChain.Iterator().GetCount())

	content := "<html><br>"
	content = content + "<a href=\"/\">返回首页</a>&nbsp;&nbsp;&nbsp;&nbsp;"
	content = content + "<a href=\"generateblock\">生成新区块</a>&nbsp;&nbsp;&nbsp;&nbsp;<br>"
	content = content + "<br><b>&nbsp;&nbsp;&nbsp;&nbsp;当前MiniBC区块链高度:" + blockHeight + "</b><br><br>"

	//遍历区块链,打印每一个区块的详细信息
	iterator := bb.blockChain.Iterator()
	for {
		block := iterator.Next()
		if block == nil {
			break
		}
		content = content + "当前区块哈希值:0x" + BytesToHex(block.GetHash()) + "<br>"
		content = content + "当前区块内容为:" +  string(block.Data) + "<br>"
		content = content + "前一区块哈希值:0x" +  BytesToHex(block.HashPrevBlock) + "<br>"
		content = content + "=============================================" + "<br>"
	}

	content = content + "</html>"
	response.Write([]byte(content))
}

// Url process as http: // SERVER_ADDR: Request 8080 / generateblock generate a new tile

func (bb *BlockBrower) handleGenerateBlock(response http.ResponseWriter, request *http.Request) {

	//获取区块链高度
	height := bb.blockChain.Iterator().GetCount()

	//创建新的区块
	bb.blockChain.AddBlock("Mini block " + strconv.Itoa(height))

	blockHeight := strconv.Itoa(bb.blockChain.Iterator().GetCount())

	content := "<html><br>"
	content = content + "<a href=\"/\">返回首页</a>&nbsp;&nbsp;&nbsp;&nbsp;"
	content = content + "<a href=\"generateblock\">生成新区块</a>&nbsp;&nbsp;&nbsp;&nbsp;<br>"
	content = content + "<br><b>&nbsp;&nbsp;&nbsp;&nbsp;当前MiniBC区块链高度:" + blockHeight + "</b><br><br>"

	//遍历区块链
	iterator := bb.blockChain.Iterator()
	for {
		block := iterator.Next()
		if block == nil {
			break
		}
		content = content + "当前区块哈希值:0x" + BytesToHex(block.GetHash()) + "<br>"
		content = content + "当前区块内容为:" +  string(block.Data) + "<br>"
		content = content + "前一区块哈希值:0x" +  BytesToHex(block.HashPrevBlock) + "<br>"
		content = content + "=============================================" + "<br>"
	}

	content = content + "</html>"
	response.Write([]byte(content))
}

After running output:

Here Insert Picture Description

We achieved and will be achieved

We have constructed a simple block chain, and implements a simple database and block chain KV browser, the next step to implement the workload proved pow consensus algorithm.

Exchanges and questions

We can begin to gradually in-depth research study from v001, each source file has full instructions and comments. If you have questions, suggestions, and do not understand the question, you can contact me.

MiniBC block chain can join the exchange qq group: 777 804 802 , Go developers paradise
of my micro letter: bkra50
GitHub addr: https://github.com/wangshizebin/minibc

In addition, the recruitment of a group of like-minded, common maintenance items, to jointly promote the project, contribute to the open source world, welcome hooked.

Let’s go~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Guess you like

Origin blog.csdn.net/wanghao72214/article/details/90349561