区块链V1版本实现之一

1. 程序地址:https://gitee.com/Jame_sz/beijing_go_term2.git

 

2. 程序编写流程:

//1. 定义结构(区块头的字段比正常的少)

  //>1. 前区块哈希

  //>2. 当前区块哈希

  //>3. 数据

//2. 创建区块

//3. 生成哈希

//4. 引入区块链

//5. 添加区块

//6. 重构代码

 

3. 程序代码:

 1 go:
 2 package main
 3 
 4 import "fmt"
 5 
 6 //定义区块结构
 7 type Block struct {
 8    //前区块哈希
 9    PrevBlockHash [] byte
10    //当前区块哈希
11    Hash [] byte
12    //数据,目前使用字节流,v4开始使用交易代替
13    Data [] byte
14 }
15 
16 const genesisInfo = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks"
17 
18 //创建区块,对Block的每一个字段填充数据
19 func NewBlock(data string, prevBlockHash []byte) *Block{
20    block := Block{
21       PrevBlockHash: prevBlockHash,
22       Hash:          []byte{}, //先填充为空
23       Data:          []byte(data),
24    }
25    return &block
26 }
27 
28 
29 func main()  {
30    fmt.Printf("HelloWorld!!!\n")
31 
32    //区块实例化
33    block := NewBlock(genesisInfo,[]byte{0x0000000000000000})
34 
35    //区块打印
36    fmt.Printf("PrevBlockHash : %x\n", block.PrevBlockHash)
37    fmt.Printf("Hash : %x\n", block.Hash)
38    fmt.Printf("Data : %s\n", block.Data)
39 }

 

4. 输出效果:

 

 

 

5. 终端Git命令

 1 //代码克隆拉取,默认分支为Master
 2 git clone https://gitee.com/Jame_sz/beijing_go_term2.git
 3 
 4 //本地仓库获取v1分支
 5 git fetch origin v1:v1
 6 
 7 //切换分支
 8 git checkout v1
 9 
10 //远程有提交,需要更新本地仓库时
11 git pull
posted @ 2020-11-18 22:56  北漂的尘埃  阅读(149)  评论(0编辑  收藏  举报