区块链V1版本实现之一
2024-08-25 08:47:03
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
最新文章
- zen Code 支持的代码样式
- Timer的故事----Jdk源码解读
- UIkit框架之uiUIapplication
- 为什么我的新项目选择了Quick-cocos2d-x
- 协作图 Collaboration diagram
- oracle-表空间满了
- raft如何实现Linearizable Read
- linux ss 命令
- windows server 2008 R2 NPS(网络连接策略服务)设置radius,实现telent登陆交换机路由器权限分配
- Python 下划线
- Spring Boot + Spring Cloud 构建微服务系统(九):配置中心(Spring Cloud Config)
- django ---forms组件
- JSP:注册&;登录
- 从 prototype.js 深入学习 javascript 的面向对象特性
- zookeeper 初步学习
- Eclipse开发工具常用快捷键的使用技巧
- .net core中automapper的使用
- 【[SCOI2016]背单词】
- GCD的基本概念
- Servlet初始化及处理HTTP请求