自網際網路以來,鎖鏈技術已被某些人稱為最有影響力的發明。儘管公眾將區塊鏈與投機性加密貨幣同義解釋,但區塊鏈實際上在現代世界中具有不可思議的廣泛應用。實際上,加密貨幣只是區塊鏈領域的一小部分,生產中的許多解決方案都是由私人組織領導來實現的。
讓我們開始吧!
瞭解區塊鏈
在Go中建立一個簡單的區塊鏈
typeBlock struct {
timestamptime.Time
transactions[]string
prevHash[]byte
Hash[]byte
}
funcNewBlock(transactions[]string, prevHash[]byte) *Block {
currentTime := time.Now()
return &Block {
timestamp: currentTime,
transactions: transactions,
prevHash: prevHash,
Hash: NewHash(currentTime, transactions, prevHash),
}
}
funcNewHash(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[:]
}
funcprintBlockInformation(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)
}
funcprintTransactions(block *Block) {
fmt.Println("\tTransactions:")
for i, transaction := range block.transactions {
fmt.Printf("\t\t%v: %q\n", i, transaction)
}
}
funcmain() {
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
}
funcmain() {
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)
}
funcNewBlock(transactions []string, prevHash []byte) *Block {
currentTime := time.Now()
return &Block {
timestamp: currentTime,
transactions: transactions,
prevHash: prevHash,
Hash: NewHash(currentTime, transactions, prevHash),
}
}
funcNewHash(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[:]
}
funcprintBlockInformation(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)
}
funcprintTransactions(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"
過程可能會很粗糙,但這是建立自己的區塊鏈的基礎!
作者:鏈三豐,來源:區塊鏈研究實驗室