使用go實現(xiàn)簡易比特幣區(qū)塊鏈公鏈功能
使用go語言實現(xiàn)具備以下功能的簡易區(qū)塊鏈
- 區(qū)塊與區(qū)塊鏈
- 共識機制
- 數(shù)據(jù)庫
- Cli命令行操作
- 交易管理
- 密碼學
- 數(shù)字簽名
- 交易緩存池
- P2P網(wǎng)絡管理
由于平時還要進行論文工作,項目不定時更新
2021.1.1實現(xiàn)了區(qū)塊結構、區(qū)塊鏈結構、工作量證明pow,剩下部分陸續(xù)更新
1.實現(xiàn)區(qū)塊結構
package BLC
import (
"bytes"
"crypto/sha256"
"time"
)
//實現(xiàn)一個最基本的區(qū)塊結構
type Block struct {
TimeStamp int64 //時間戳,區(qū)塊產(chǎn)生的時間
Heigth int64//區(qū)塊高度(索引、號碼)代表當前區(qū)塊的高度
PreBlockHash []byte//前一個區(qū)塊(父區(qū)塊)的哈希
Hash []byte//當前區(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
}
//計算區(qū)塊哈希
func (b *Block)SetHash() {
//int64轉換成字節(jié)數(shù)組
//高度轉換
heightBytes:=IntToHex(b.Heigth)
//時間轉換
timeStampBytes:=IntToHex(b.TimeStamp)
//拼接所有屬性進行hash
blockBytes:=bytes.Join([][]byte{heightBytes,timeStampBytes,b.PreBlockHash,b.Data},[]byte{})
hash:=sha256.Sum256(blockBytes)
b.Hash=hash[:]
}
2.實現(xiàn)區(qū)塊鏈結構
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.實現(xiàn)工作量證明
package BLC
import (
"bytes"
"crypto/sha256"
"fmt"
"math/big"
)
//目標難度值,生成的hash前 targetBit 位為0才滿足條件
const targetBit =16
//工作量證明
type ProofOfWork struct {
Block *Block //對指定的區(qū)塊進行驗證
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 //存儲轉換后的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)
}
//準備數(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.當前運行結果

到此這篇關于使用go實現(xiàn)簡易比特幣區(qū)塊鏈公鏈功能的文章就介紹到這了,更多相關go實現(xiàn)比特幣區(qū)塊鏈公鏈內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Go json omitempty如何實現(xiàn)可選屬性
在Go語言中,使用`omitempty`可以幫助我們在進行JSON序列化和反序列化時,忽略結構體中的零值或空值,本文介紹了如何通過將字段類型改為指針類型,并在結構體的JSON標簽中添加`omitempty`來實現(xiàn)這一功能,例如,將float32修改為*float322024-09-09
Go并發(fā):使用sync.WaitGroup實現(xiàn)協(xié)程同步方式
這篇文章主要介紹了Go并發(fā):使用sync.WaitGroup實現(xiàn)協(xié)程同步方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-05-05
go-zero數(shù)據(jù)的流處理利器fx使用詳解
這篇文章主要為大家介紹了go-zero數(shù)據(jù)的流處理利器fx使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05

