Skip to content

Commit

Permalink
实现功能
Browse files Browse the repository at this point in the history
  • Loading branch information
wumansgy committed Nov 14, 2018
1 parent f180854 commit 0372126
Show file tree
Hide file tree
Showing 25 changed files with 1,931 additions and 127 deletions.
167 changes: 140 additions & 27 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 33 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
## Implementation of Bitcoin Simple Model
## 比特币区块链的简单模型实现

[![](https://img.shields.io/badge/Auther-blog-blue.svg)](https://github.com/wumansgy)

> ##### With Referring to the implementation principle of Bitcoin, a simple blockchain model is constructed using GO language to construct some related functions.
> ##### 参考比特币的实现原理,利用GO语言构造了一个简单的区块链模型,构造了一些相关的功能。
## function
### 功能及相关

- POW
- Based on bolt lightweight database
- POW工作量证明
- 基于bolt轻量级数据库

## version
- UTXO实现原理
- 数字签名的验证
- 相关转账功能实现
- 钱包地址的实现
- 钱包地址校验功能

- **V1** (Simple function implementation)
- **v2**(Full functionality implementation, including transaction module)**(Not implemented, implemented later)**

## use
### 使用

1. `git clone https://github.com/wumansgy/btcmodel.git`
2. Switch to the directory below

2. 选择进入目录

3. `go build main.go`
4. command
- `./main createBloChain` (Create New Blockchain)
- `./main addBlock DATA ` (Add Block)
- `./main printChain` (Print Chain)

## as follows
4. 命令行
```
const Usage = `
Usage:
createwt "创建钱包地址"
createbc <ADDRESS> "创建区块链"
print "打印区块链"
printtx "打印交易"
balc <ADDRESS> "获取指定地址余额"
send <FROM> <TO> <AMOUNT> "转账"
mine [MINER] [DATA] "挖矿",默认:
list "打印钱包中的所有地址"
status "查看当前待确认交易数量"
`
```



### 简单功能如下

![](image/1.png)

Expand Down
34 changes: 28 additions & 6 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/binary"
"log"
"encoding/gob"
"crypto/sha256"
)

//data , prevHash, Hash
Expand All @@ -26,23 +27,28 @@ type Block struct {

Hash []byte //当前区块哈希值, 正常的区块不存在,我们为了方便放进来

Data []byte //数据本身,区块体,先用字符串表示,v4版本的时候会引用真正的交易结构
//Data []byte //数据本身,区块体,先用字符串表示,v4版本的时候会引用真正的交易结构
Transactions []*Transaction
}

func NewBlock(data string, prevHash []byte) *Block {
func NewBlock(txs []*Transaction, prevHash []byte) *Block {
block := Block{
Version: 00,
PrevBlockHash: prevHash,
MerkelRoot: []byte{}, //先填写为空
TimeStamp: uint64(time.Now().Unix()),
Difficulty: difficulty,
Nonce: 0, //目前不挖矿,随便写一个值,等会在赋值
Nonce: 0, //目前不挖矿,随便写一个值
Hash: []byte{}, //见SetHash函数
Data: []byte(data),
//Data: []byte(data),
Transactions: txs,
}

pow := NewProofOfWork(block) //挖矿计算
hash, nonce := pow.Run() //返回符合难度值的哈希和目标值
block.setMerkelRoot()

//pow运算
pow := NewProofOfWork(block)
hash, nonce := pow.Run()

block.Hash = hash
block.Nonce = nonce
Expand Down Expand Up @@ -92,3 +98,19 @@ func Uint2Byte(num uint64) []byte {

return buffer.Bytes()
}


//创建一个简单的MerkelRoot, 使用block中的交易作为数据来源
//只是将多个交易的哈希拼接起来,做sha256
func (block *Block)setMerkelRoot() {
var info []byte

for _, tx := range block.Transactions {
//只是将多个交易的哈希拼接起来,做sha256
info = append(info, tx.TXID...)
}

hash := sha256.Sum256(info)

block.MerkelRoot = hash[:]
}
Loading

0 comments on commit 0372126

Please sign in to comment.