Skip to content

Commit

Permalink
new
Browse files Browse the repository at this point in the history
  • Loading branch information
demingry committed Dec 23, 2020
1 parent 7c9c558 commit a6c0593
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 17 deletions.
42 changes: 30 additions & 12 deletions .idea/workspace.xml

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

39 changes: 37 additions & 2 deletions BLC/Blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"math/big"
"os"
)

const (
Expand Down Expand Up @@ -33,6 +34,33 @@ func (blc *Blockchain)AddNewBlockToChain(data string,height int64,prevhash []byt
//创建带有创世区块的区块链
func CreateBlockchainWithGenesisBlock() *Blockchain {

if dbExists() {
fmt.Println("[!]数据库已存在")
db, err := bolt.Open(dbName, 0600, nil)
if err != nil {
log.Fatal(err)
}
//defer db.Close()
var blockchain *Blockchain
//B:读取数据库
err = db.View(func(tx *bolt.Tx) error {
//C:打开表
b := tx.Bucket([]byte(blockTableName))
if b != nil {
//D:读取最后一个hash
hash := b.Get([]byte("l"))
//E:创建blockchain
blockchain = &Blockchain{hash, db}
}
return nil
})
if err != nil {
log.Fatal(err)
}
return blockchain
}

fmt.Println("[!]数据库不存在,将创建创世区块")
db,err := bolt.Open(dbName,0600,nil)
if err != nil {
log.Fatal(err)
Expand All @@ -47,8 +75,8 @@ func CreateBlockchainWithGenesisBlock() *Blockchain {
log.Panic(err)
}

//数据表不存在时,创建创世区块
if b == nil {
//数据表被新建,创建创世区块
if b != nil {

genesisBlock := CreateGenesisBlock("genesis block creating...")
//将创世区块存储到表中
Expand Down Expand Up @@ -130,3 +158,10 @@ func (bc *Blockchain)PrintChain() {
count++
}
}

func dbExists() bool {
if _, err := os.Stat(dbName); os.IsNotExist(err) {
return false
}
return true
}
2 changes: 1 addition & 1 deletion BLC/ProofofWork.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type ProofofWork struct {
target *big.Int //大数据存储
}

const targetBit = 16
const targetBit = 20

//拼接数据字符数组
func (p *ProofofWork)PrepareData(nounce int) []byte {
Expand Down
15 changes: 15 additions & 0 deletions BLC/Utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package BLC

import (
"bytes"
"crypto/rand"
"encoding/binary"
"log"
"math/big"
)

func IntToHex(num int64) []byte {
Expand All @@ -14,3 +16,16 @@ func IntToHex(num int64) []byte {
}
return buff.Bytes()
}

func CreateRandomString(len int) string {
var container string
var str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
b := bytes.NewBufferString(str)
length := b.Len()
bigInt := big.NewInt(int64(length))
for i := 0;i < len ;i++ {
randomInt,_ := rand.Int(rand.Reader,bigInt)
container += string(str[randomInt.Int64()])
}
return container
}
Binary file removed blockedchain.db
Binary file not shown.
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (

func main() {
Blockchain := BLC.CreateBlockchainWithGenesisBlock()
Blockchain.AddBlockToBlockChain("send $100 to Zhang")
Blockchain.AddBlockToBlockChain("send $100 to Ge")
for {
Blockchain.AddBlockToBlockChain(BLC.CreateRandomString(6))
}
defer Blockchain.DB.Close()
Blockchain.PrintChain()
fmt.Scanf("this")
Expand Down

0 comments on commit a6c0593

Please sign in to comment.