使用go實(shí)現(xiàn)簡易比特幣區(qū)塊鏈公鏈功能
使用go語言實(shí)現(xiàn)具備以下功能的簡易區(qū)塊鏈
- 區(qū)塊與區(qū)塊鏈
- 共識(shí)機(jī)制
- 數(shù)據(jù)庫
- Cli命令行操作
- 交易管理
- 密碼學(xué)
- 數(shù)字簽名
- 交易緩存池
- P2P網(wǎng)絡(luò)管理
由于平時(shí)還要進(jìn)行論文工作,項(xiàng)目不定時(shí)更新
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)一個(gè)最基本的區(qū)塊結(jié)構(gòu)
type Block struct {
TimeStamp int64 //時(shí)間戳,區(qū)塊產(chǎn)生的時(shí)間
Heigth int64//區(qū)塊高度(索引、號(hào)碼)代表當(dāng)前區(qū)塊的高度
PreBlockHash []byte//前一個(gè)區(qū)塊(父區(qū)塊)的哈希
Hash []byte//當(dāng)前區(qū)塊的哈希
Data []byte//交易數(shù)據(jù)
}
//創(chuàng)建一個(gè)新的區(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)
//時(shí)間轉(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 //存儲(chǔ)有序的區(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 //對(duì)指定的區(qū)塊進(jìn)行驗(yàn)證
target *big.Int //大數(shù)據(jù)存儲(chǔ)
}
//創(chuàng)建新的pow對(duì)象
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 //存儲(chǔ)轉(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在Visual Studio Code中配置GO開發(fā)環(huán)境的詳細(xì)教程
這篇文章主要介紹了在Visual Studio Code中配置GO開發(fā)環(huán)境的詳細(xì)教程,需要的朋友可以參考下2017-02-02
Go語言實(shí)現(xiàn)登錄驗(yàn)證代碼案例
這篇文章主要介紹了Go語言實(shí)現(xiàn)登錄驗(yàn)證代碼案例,代碼和圖文講解的很清晰,有感興趣的可以學(xué)習(xí)下2021-03-03
利用go-zero在Go中快速實(shí)現(xiàn)JWT認(rèn)證的步驟詳解
這篇文章主要介紹了如何利用go-zero在Go中快速實(shí)現(xiàn)JWT認(rèn)證,本文分步驟通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-10-10
golang架構(gòu)設(shè)計(jì)開閉原則手寫實(shí)現(xiàn)
這篇文章主要為大家介紹了golang架構(gòu)設(shè)計(jì)開閉原則手寫實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
詳解Go如何優(yōu)雅的對(duì)時(shí)間進(jìn)行格式化
這篇文章主要為大家詳細(xì)介紹了Go語言中是如何優(yōu)雅的對(duì)時(shí)間進(jìn)行格式化的,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-06-06
Golang使用Apache PLC4X連接modbus的示例代碼
Modbus是一種串行通信協(xié)議,是Modicon公司于1979年為使用可編程邏輯控制器(PLC)通信而發(fā)表,這篇文章主要介紹了Golang使用Apache PLC4X連接modbus的示例代碼,需要的朋友可以參考下2024-07-07

