如何使用Go實現區塊鏈 | 區塊鏈研究實驗室
火幣網(huobi.com)最新可用網址(點擊下圖直達註冊!)
火必交易所,曾经的火币交易所!
欧易OKX三大交易所,稳定好用!
币安全球第一大交易所!安全!
自互聯網以來,鎖鏈技術已被某些人稱為最有影響力的發明。盡管公眾將區塊鏈與投機性加密貨幣同義解釋,但區塊鏈實際上在現代世界中具有不可思議的廣泛應用。實際上,加密貨幣隻是區塊鏈領域的一小部分,生產中的許多解決方案都是由私人組織領導來實現的。
讓我們開始吧!
瞭解區塊鏈
在Go中創建一個簡單的區塊鏈
type Block struct {
timestamp time.Time
transactions []string
prevHash []byte
Hash []byte
}
func NewBlock(transactions []string, prevHash []byte) *Block {
currentTime := time.Now()
return &Block {
timestamp: currentTime,
transactions: transactions,
prevHash: prevHash,
Hash: NewHash(currentTime, transactions, prevHash),
}
}
func NewHash(time time.Time, transactions []string, prevHash []byte) []byte {
input := append(prevHash, time.String()...)
for transaction := range transactions {
input = append(input, string(rune(transaction))...)
}
hash := sha256.Sum256(input)
return hash[:]
}
func printBlockInformation(block *Block) {
fmt.Printf("\ttime: %s\n", block.timestamp.String())
fmt.Printf("\tprevHash: %x\n", block.prevHash)
fmt.Printf("\tHash: %x\n", block.Hash)
printTransactions(block)
}
func printTransactions(block *Block) {
fmt.Println("\tTransactions:")
for i, transaction := range block.transactions {
fmt.Printf("\t\t%v: %q\n", i, transaction)
}
}
func main() {
genesisTransactions := []string{"Izzy sent Will 50 bitcoin", "Will sent Izzy 30 bitcoin"}
genesisBlock := NewBlock(genesisTransactions, []byte{})
fmt.Println("--- First Block ---")
printBlockInformation(genesisBlock)
block2Transactions := []string{"John sent Izzy 30 bitcoin"}
block2 := NewBlock(block2Transactions, genesisBlock.Hash)
fmt.Println("--- Second Block ---")
printBlockInformation(block2)
block3Transactions := []string{"Will sent Izzy 45 bitcoin", "Izzy sent Will 10 bitcoin"}
block3 := NewBlock(block3Transactions, block2.Hash)
fmt.Println("--- Third Block ---")
printBlockInformation(block3)
}
package main
import (
"crypto/sha256"
"fmt"
"time"
)
type Block struct {
timestamp time.Time
transactions []string
prevHash []byte
Hash []byte
}
func main() {
genesisTransactions := []string{"Izzy sent Will 50 bitcoin", "Will sent Izzy 30 bitcoin"}
genesisBlock := NewBlock(genesisTransactions, []byte{})
fmt.Println("--- First Block ---")
printBlockInformation(genesisBlock)
block2Transactions := []string{"John sent Izzy 30 bitcoin"}
block2 := NewBlock(block2Transactions, genesisBlock.Hash)
fmt.Println("--- Second Block ---")
printBlockInformation(block2)
block3Transactions := []string{"Will sent Izzy 45 bitcoin", "Izzy sent Will 10 bitcoin"}
block3 := NewBlock(block3Transactions, block2.Hash)
fmt.Println("--- Third Block ---")
printBlockInformation(block3)
}
func NewBlock(transactions []string, prevHash []byte) *Block {
currentTime := time.Now()
return &Block {
timestamp: currentTime,
transactions: transactions,
prevHash: prevHash,
Hash: NewHash(currentTime, transactions, prevHash),
}
}
func NewHash(time time.Time, transactions []string, prevHash []byte) []byte {
input := append(prevHash, time.String()...)
for transaction := range transactions {
input = append(input, string(rune(transaction))...)
}
hash := sha256.Sum256(input)
return hash[:]
}
func printBlockInformation(block *Block) {
fmt.Printf("\ttime: %s\n", block.timestamp.String())
fmt.Printf("\tprevHash: %x\n", block.prevHash)
fmt.Printf("\tHash: %x\n", block.Hash)
printTransactions(block)
}
func printTransactions(block *Block) {
fmt.Println("\tTransactions:")
for i, transaction := range block.transactions {
fmt.Printf("\t\t%v: %q\n", i, transaction)
}
}
$ go run example.go
--- First Block ---
time: 2021-04-05 15:12:18.813294 -0600 MDT m=+0.000074939
prevHash:
Hash: 43ec51c50d2b9565f221155a29d8b72307247b08eaf6731cca
Transactions:
0: "Izzy sent Will 50 bitcoin"
1: "Will sent Izzy 30 bitcoin"
--- Second Block ---
time: 2021-04-05 15:12:18.813477 -0600 MDT m=+0.000257244
prevHash: 43ec51c50d2b9565f221155a29d8b72307247b08eaf6731cca
Hash: fcce5323a35cb67b45fe75866582db00fd32baeb92aac448c7
Transactions:
0: "John sent Izzy 30 bitcoin"
--- Third Block ---
time: 2021-04-05 15:12:18.813488 -0600 MDT m=+0.000269168
prevHash: fcce5323a35cb67b45fe75866582db00fd32baeb92aac448c7
Hash: fc1d3eee286970d85812b47c3a5bf016ae8c1de4f86b8ace972ffa
Transactions:
0: "Will sent Izzy 45 bitcoin"
1: "Izzy sent Will 10 bitcoin"
過程可能會很粗糙,但這是創建自己的區塊鏈的基礎!
作者:鏈三豐,來源:區塊鏈研究實驗室
下一篇:什麼是Chia 時空證明?
返回列表页>>> 比特幣最新新聞