Skip to content

Commit

Permalink
use js failure, I will go to new branch to change
Browse files Browse the repository at this point in the history
  • Loading branch information
TheStarBoys committed May 16, 2019
1 parent 680b737 commit 914f31d
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 38 deletions.
16 changes: 8 additions & 8 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
)

type Block struct {
Version int64
PrevBlockHash []byte
Hash []byte
MerkleRoot []byte
TimeStamp int64
Bits int64
Nonce int64
Data []byte
Version int64 `json "version""`
PrevBlockHash []byte `json "prevBlock"`
Hash []byte `json "hash"`
MerkleRoot []byte `json "merkleRoot"`
TimeStamp int64 `json "timeStamp"`
Bits int64 `json "bits"`
Nonce int64 `json "nonce"`
Data []byte `json "data"`
}
// new新区块
func NewBlock(data string, prevHash []byte) *Block {
Expand Down
27 changes: 20 additions & 7 deletions blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ import "fmt"
const splitLine = "-----------------------------------------------------------------------"

type BlockChain struct {
blocks []*Block
Blocks []*Block
}
// new新的区块链
func NewBlockChain() *BlockChain{
//bc := BlockChain{}
//bc.blocks = append(bc.blocks, NewGenesisBlock())
//bc.Blocks = append(bc.Blocks, NewGenesisBlock())
//return &bc
block := NewGenesisBlock()
return &BlockChain{blocks:[]*Block{block}}
return &BlockChain{Blocks: []*Block{block}}
}

// 添加区块到区块链
func (bc *BlockChain)AddBlock(data string) {
// 最后一个区块的哈希, 就是前哈希
prevHash := bc.blocks[len(bc.blocks) - 1].Hash
prevHash := bc.Blocks[len(bc.Blocks) - 1].Hash
block := NewBlock(data, prevHash)
bc.blocks = append(bc.blocks, block)
bc.Blocks = append(bc.Blocks, block)
}

// 打印区块链
func (bc *BlockChain)PrintChain() {
// 遍历区块链
for _, block := range bc.blocks {
for _, block := range bc.Blocks {
fmt.Println(splitLine)
fmt.Printf("Version: %d\n",block.Version)
fmt.Printf("PrevBlockHash: %x\n",block.PrevBlockHash)
Expand All @@ -39,4 +39,17 @@ func (bc *BlockChain)PrintChain() {
fmt.Printf("Data: %s\n",block.Data)
fmt.Println(splitLine)
}
}
}

// 将除GenesisBlock外的block的hash存在[][]byte中
//func (bc *BlockChain)SaveBlocksHash() (prevHashSlice, hashSlice []string){
// for i, block := range bc.Blocks {
// if i == 0 { // 跳过Genesis block
// continue
// }
// // 将区块的hash信息的十六进制字符串存到切片中
// prevHashSlice = append(prevHashSlice, fmt.Sprintf("0x%x", block.PrevBlockHash))
// hashSlice = append(hashSlice, fmt.Sprintf("0x%x", block.Hash))
// }
// return
//}
Binary file modified blockchainDemo.exe
Binary file not shown.
Binary file added blockchainDemo.exe~
Binary file not shown.
8 changes: 5 additions & 3 deletions controllers/addBlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package controllers

import "github.com/astaxie/beego"

type addBlockController struct {
type AddBlockController struct {
beego.Controller
}

func (this *addBlockController)Get() {

func (this *AddBlockController)Get() {
addData := this.GetString("addData")
beego.Info("--------data: ",addData)
this.TplName = "index.html"
}
7 changes: 7 additions & 0 deletions controllers/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ type MainController struct {
func (c *MainController) Get() {
c.TplName = "index.html"
}

func (this *MainController) Post() {
addData := this.GetString("addData")
beego.Info("--------data: ",addData)
this.Data["addData"] = addData
this.TplName = "index.html"
}
29 changes: 26 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,36 @@ package main

import (
_ "blockchainDemo/routers"
"encoding/json"
"github.com/astaxie/beego"
"golang.org/x/exp/errors/fmt"
)

var bc *BlockChain

func main() {
bc := NewBlockChain()
bc.AddBlock("Hello World")
bc = NewBlockChain()
bc.PrintChain()
// 返回当前区块hash的函数
beego.AddFuncMap("AddBlockData",AddBlockData)
beego.AddFuncMap("LoadGenesisHash",LoadGenesisHash)
beego.Run()
}

// 返回当前区块链的json数据
func AddBlockData (data string) (jsonStr string) {
// 用一个[][]byte 来装hash格式化后的字符串
// 每次添加区块, 都应该把区块信息存进去
// 添加区块
beego.Info("Add Block Data begin--------")
bc.AddBlock(data)
bc.PrintChain()
j, err := json.MarshalIndent(bc,"","")
CheckErr("AddBlockData", err)
beego.Info(string(j))
return string(j)
}
// 加载时,获得GenesisBlock的hash
func LoadGenesisHash() (hash string){
// 格式化输出一个十六进制的hash字符串
return fmt.Sprintf("0x%x", bc.Blocks[0].Hash)
}
2 changes: 1 addition & 1 deletion routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import (

func init() {
beego.Router("/", &controllers.MainController{})
beego.Router("/add-block", &controllers.)
beego.Router("/add-block", &controllers.AddBlockController{})
}
73 changes: 57 additions & 16 deletions views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@
Guide you to understand how blockchain works.<br>
</p>
</div>
<div class="block">
{{/*GENESIS BLOCK*/}}
<div class="block" onload="loadGenesisHash()">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">DATA</span>
<input type="text" class="form-control" placeholder="" aria-describedby="basic-addon1"
Expand All @@ -101,7 +102,7 @@
<p style="margin-top: 22px" class="ft-size-12">PREVIOUS HASH
<span class="label label-info">0</span>
</p>
<p style="margin-top: 10px" class="ft-size-12">HASH <span class="label label-info">000dc75a315c77a1f9c98fb6247d03dd18ac52632d7dc6a9920261d8109b37cf</span>
<p style="margin-top: 10px" class="ft-size-12">HASH <span class="label label-info">{{LoadGenesisHash}}</span>
</p>
<div class="clearfix" style="height: 54px">
<h3 class="pull-left">GENESIS BLOCK
Expand All @@ -110,6 +111,30 @@ <h3 class="pull-left">GENESIS BLOCK
<span class="label label-info pull-right" style="margin-top: 26px;">604</span>
</div>
</div>
{{/*BLOCK# BEGIN*/}}
{{/*隐藏域 用于传递数据*/}}
<span id="span-hidden1" style="visibility: hidden"></span>
<span id="span-hidden3">

</span>

<span id="span-hidden2" style="visibility: hidden"></span>
{{/*BLOCK END*/}}
<div class="add-block">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">DATA</span>
<input type="text" id="addData" class="form-control" placeholder="" aria-describedby="basic-addon1"
value="{{.addData}}" name="addData">
</div>
<button class="btn-addblock" onclick="addBlock()" style="color: #fff;">ADD NEW BLOCK</button>
</div>
</div>
<script>
const blockHTML = `
\{\{if .\}\}
<p>There is not object</p>
\{\{end\}\}
<span style="visibility: hidden">\{\{range .blocks\}\}</span>
<div style="text-align: center;">
<span class="glyphicon glyphicon-chevron-down" aria-hidden="true"
style="font-size: 40px; color: rgba(0,0,0,0.5);"></span>
Expand All @@ -120,8 +145,8 @@ <h3 class="pull-left">GENESIS BLOCK
<input type="text" class="form-control" placeholder="" aria-describedby="basic-addon1"
value="Welcome to Blockchain Demo by TheStarBoys">
</div>
<p style="margin-top: 22px" class="ft-size-12">PREVIOUS HASH <span class="label label-info">000dc75a315c77a1f9c98fb6247d03dd18ac52632d7dc6a9920261d8109b37cf</span></p>
<p style="margin-top: 10px" class="ft-size-12">HASH <span class="label label-info">000dc75a315c77a1f9c98fb6247d03dd18ac52632d7dc6a9920261d8109b37cf</span>
<p style="margin-top: 22px" class="ft-size-12">PREVIOUS HASH <span class="label label-info">{{.PrevBlockHash}}</span></p>
<p style="margin-top: 10px" class="ft-size-12">HASH <span class="label label-info">{{.Hash}}</span>
</p>
<div class="clearfix" style="height: 54px">
<h3 class="pull-left">BLOCK#1
Expand All @@ -130,21 +155,37 @@ <h3 class="pull-left">BLOCK#1
<span class="label label-info pull-right" style="margin-top: 26px;">604</span>
</div>
</div>
<div class="add-block">
<form action="/add-block" method="post">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">DATA</span>
<input type="text" class="form-control" placeholder="" aria-describedby="basic-addon1"
value="{{.data}}">
</div>
<button class="btn-addblock" type="submit" style="color: #fff;">ADD NEW BLOCK</button>
</form>
</div>
</div>
<span style="visibility: hidden">\{\{end\}\}</span>`;
// 添加区块传递数据的函数
function addBlock() {
var addData = document.getElementById("addData").value;
console.log(addData); // 得到了addData输入框里的值
var json = {{AddBlockData "addData"}}; // 返回的是一个json
var obj = JSON.parse(json); // 解析json得到对象
console.log("obj is ----:", obj.Blocks[0].Version);
document.getElementById("span-hidden1").innerHTML = "\{\{with " + obj.Blocks + "\}\}";
document.getElementById("span-hidden2").innerHTML = "\{\{end\}\}";
//alert(typeof blockHTML);
// 遍历json对象,得到其中我们需要的属性值
for (var i = 0; i <obj.Blocks.length;i++) {
var block = obj.Blocks[i];
var prevhash = block.PrevBlockHash;
var hash = block.Hash;
console.log(obj.Blocks.length);
console.log("for - in obj ----", prevhash + " ", hash);
}
document.getElementById("span-hidden3").innerHTML = blockHTML;
}
// 加载时,获得GenesisBlock的hash
function loadGenesisHash() {

}

</script>

<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
<script src="../static/js/bootstrap.min.js"></script>
</body>
</html>

0 comments on commit 914f31d

Please sign in to comment.