使用go實(shí)現(xiàn)簡易比特幣區(qū)塊鏈公鏈功能
使用go語言實(shí)現(xiàn)具備以下功能的簡易區(qū)塊鏈
- 區(qū)塊與區(qū)塊鏈
- 共識機(jī)制
- 數(shù)據(jù)庫
- Cli命令行操作
- 交易管理
- 密碼學(xué)
- 數(shù)字簽名
- 交易緩存池
- P2P網(wǎng)絡(luò)管理
由于平時還要進(jìn)行論文工作,項(xiàng)目不定時更新
2021.1.1實(shí)現(xiàn)了區(qū)塊結(jié)構(gòu)、區(qū)塊鏈結(jié)構(gòu)、工作量證明pow,剩下部分陸續(xù)更新
1.實(shí)現(xiàn)區(qū)塊結(jié)構(gòu)
package BLC
import (
"bytes"
"crypto/sha256"
"time"
)
//實(shí)現(xiàn)一個最基本的區(qū)塊結(jié)構(gòu)
type Block struct {
TimeStamp int64 //時間戳,區(qū)塊產(chǎn)生的時間
Heigth int64//區(qū)塊高度(索引、號碼)代表當(dāng)前區(qū)塊的高度
PreBlockHash []byte//前一個區(qū)塊(父區(qū)塊)的哈希
Hash []byte//當(dāng)前區(qū)塊的哈希
Data []byte//交易數(shù)據(jù)
}
//創(chuàng)建一個新的區(qū)塊
func NewBlock(height int64,preBlockHash []byte,Data []byte) *Block {
var block Block
block=Block{Heigth: height,PreBlockHash: preBlockHash,Data: Data,TimeStamp: time.Now().Unix()}
block.SetHash()
return &block
}
//計(jì)算區(qū)塊哈希
func (b *Block)SetHash() {
//int64轉(zhuǎn)換成字節(jié)數(shù)組
//高度轉(zhuǎn)換
heightBytes:=IntToHex(b.Heigth)
//時間轉(zhuǎn)換
timeStampBytes:=IntToHex(b.TimeStamp)
//拼接所有屬性進(jìn)行hash
blockBytes:=bytes.Join([][]byte{heightBytes,timeStampBytes,b.PreBlockHash,b.Data},[]byte{})
hash:=sha256.Sum256(blockBytes)
b.Hash=hash[:]
}
2.實(shí)現(xiàn)區(qū)塊鏈結(jié)構(gòu)
package BLC
type BlockChain struct {
Blocks []*Block //存儲有序的區(qū)塊
}
//初始化區(qū)塊鏈
func CreateBlockChainWithGenesisBlock() *BlockChain {
//添加創(chuàng)世區(qū)塊
genesisBlock:=CreateGenesisBlock("the init of blockchain")
return &BlockChain{[]*Block{genesisBlock}}
}
//添加新的區(qū)塊到區(qū)塊鏈中
func (bc *BlockChain)AddBlock(height int64,data []byte,prevBlockHash []byte){
newBlock := NewBlock(height,prevBlockHash,data)
bc.Blocks=append(bc.Blocks,newBlock)
}
3.實(shí)現(xiàn)工作量證明
package BLC
import (
"bytes"
"crypto/sha256"
"fmt"
"math/big"
)
//目標(biāo)難度值,生成的hash前 targetBit 位為0才滿足條件
const targetBit =16
//工作量證明
type ProofOfWork struct {
Block *Block //對指定的區(qū)塊進(jìn)行驗(yàn)證
target *big.Int //大數(shù)據(jù)存儲
}
//創(chuàng)建新的pow對象
func NewProofOfWork(block *Block) *ProofOfWork {
target:=big.NewInt(1)
target=target.Lsh(target,256-targetBit)
return &ProofOfWork{block,target}
}
//開始工作量證明
func (proofOfWork *ProofOfWork)Run() ([]byte,int64) {
//數(shù)據(jù)拼接
var nonce=0 //碰撞次數(shù)
var hash [32]byte //生成的hash
var hashInt big.Int //存儲轉(zhuǎn)換后的hash
for {
dataBytes:=proofOfWork.prepareData(nonce)
hash=sha256.Sum256(dataBytes)
hashInt.SetBytes(hash[:])
fmt.Printf("hash:\r%x",hash)
//難度比較
if proofOfWork.target.Cmp(&hashInt)==1{
break
}
nonce++
}
fmt.Printf("碰撞次數(shù):%d\n",nonce)
return hash[:],int64(nonce)
}
//準(zhǔn)備數(shù)據(jù),將區(qū)塊屬性拼接起來,返回字節(jié)數(shù)組
func (pow *ProofOfWork)prepareData(nonce int) []byte {
data:=bytes.Join([][]byte{
pow.Block.PreBlockHash,
pow.Block.Data,
IntToHex(pow.Block.TimeStamp),
IntToHex(pow.Block.Heigth),
IntToHex(int64(nonce)),
IntToHex(targetBit),
},[]byte{})
return data
}
4.當(dāng)前運(yùn)行結(jié)果

到此這篇關(guān)于使用go實(shí)現(xiàn)簡易比特幣區(qū)塊鏈公鏈功能的文章就介紹到這了,更多相關(guān)go實(shí)現(xiàn)比特幣區(qū)塊鏈公鏈內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
golang如何使用struct的tag屬性的詳細(xì)介紹
這篇文章主要介紹了golang如何使用struct的tag屬性的詳細(xì)介紹,從例子說起,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11
基于go實(shí)例網(wǎng)絡(luò)存儲協(xié)議詳解
這篇文章主要為大家介紹了基于go實(shí)例網(wǎng)絡(luò)存儲協(xié)議詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Go json omitempty如何實(shí)現(xiàn)可選屬性
在Go語言中,使用`omitempty`可以幫助我們在進(jìn)行JSON序列化和反序列化時,忽略結(jié)構(gòu)體中的零值或空值,本文介紹了如何通過將字段類型改為指針類型,并在結(jié)構(gòu)體的JSON標(biāo)簽中添加`omitempty`來實(shí)現(xiàn)這一功能,例如,將float32修改為*float322024-09-09
Go并發(fā):使用sync.WaitGroup實(shí)現(xiàn)協(xié)程同步方式
這篇文章主要介紹了Go并發(fā):使用sync.WaitGroup實(shí)現(xiàn)協(xié)程同步方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-05-05
golang切片擴(kuò)容規(guī)則實(shí)現(xiàn)
這篇文章主要介紹了golang切片擴(kuò)容規(guī)則實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Go并發(fā)編程結(jié)構(gòu)體多字段原子操作示例詳解
這篇文章主要為大家介紹了Go并發(fā)編程結(jié)構(gòu)體多字段原子操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
go-zero數(shù)據(jù)的流處理利器fx使用詳解
這篇文章主要為大家介紹了go-zero數(shù)據(jù)的流處理利器fx使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05

