自互联网以来,锁链技术已被某些人称为最有影响力的发明。尽管公众将区块链与投机性加密货币同义解释,但区块链实际上在现代世界中具有不可思议的广泛应用。实际上,加密货币只是区块链领域的一小部分,生产中的许多解决方案都是由私人组织领导来实现的。
让我们开始吧!
了解区块链
在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"
过程可能会很粗糙,但这是创建自己的区块链的基础!
作者:链三丰,来源:区块链研究实验室
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。