1.创建一条公链
本部分内容我们会使用Go语言去做代码实现,我们先在Go文件夹下的src目录下创建一个文件夹publicChain,用于存放所有代码文件
1.1 区块结构
通常意义上区块内容主要包含五个部分:区块高度、前一个区块的哈希、交易数据、时间戳以及本区块的哈希。使用Go语言中的Struct即可定义一个区块
/Users/go/src/publicChain/part1-Basic-Prototype/BLC/Block.go
package BLC
import (
"bytes"
"crypto/sha256"
"fmt"
"strconv"
"time"
)
//the structure of a block
//five elements of a block
type Block struct {
//1.block height
Height int64
//2.the last block's hash
PreBlockHash []byte
//3.transaction data
Data []byte
//4.timestamp
Timestamp int64
//5.block's hash
Hash []byte
}
注意:这里区块哈希值和交易数据类型都使用了[]byte类型,这是因为[]byte更加灵活和易操作
1.2 生成新区块
我们都知道,在区块链中新区块的生成依赖于上一个区块的信息,我们现在来创建一个函数专门用来生成新的区块
/Users/go/src/publicChain/part1-Basic-Prototype/BLC/Block.go
//a function to crate a new block
//use the "Block" defined above to crate a new block
func Newblock(height int64, preBlockHash []byte, data string) *Block {
//1.crate a new block
block := &Block{height, preBlockHash, []byte(data), time.Now().Unix(), nil}
//2.set Hash
block.SetHash()
return block
}
在创建第一个区块的时候,需要我们提供区块的五要素,现在我们将提供前三个作为新函数的参数,时间戳直接调用系统即可,而本区块的哈希是由其它信息通过加密算法产生
我们先定义了一个block变量,实际上是上面Block的引用(直接传数据量太大了),这里的本区块哈希值我们暂且填充为nil
//1.crate a new block
block := &Block{height, preBlockHash, []byte(data), time.Now().Unix(), nil}
这里我们用上面这个变量调用了一个新的函数专门用来生成本区块的哈希值
//2.set Hash
block.SetHash()
如下函数把int64类型的数据转化为了[]byte,并将其连起来用于生成哈希值
/Users/go/src/publicChain/part1-Basic-Prototype/BLC/Block.go
// a method to set the hash of the current block
// the hash of the current block is related to the rest data of current block
func (block *Block) SetHash() {
//1.Convert int64 to byte slice
heightBytes := IntToHex(block.Height)
//2.Convert int64 to byte slice
timeString := strconv.FormatInt(block.Timestamp, 2)
timeBytes := []byte(timeString)
//3.concatenate all properties
blockBytes := bytes.Join([][]byte{heightBytes, block.PreBlockHash, block.Data, timeBytes, block.Hash}, []byte{})
//4.produce Hash
hash := sha256.Sum256(blockBytes)
block.Hash = hash[:]
}
上述代码中的高度转换函数我们放在了另一个文件中
/Users/xxx/go/src/publicChain/part1-Basic-Prototype/BLC/utils.go
package BLC
import (
"bytes"
"encoding/binary"
"log"
)
func IntToHex(num int64) []byte {
buff := new(bytes.Buffer)
err := binary.Write(buff, binary.BigEndian, num)
if err != nil {
log.Panic(err)
}
return buff.Bytes()
}
现在我们在main函数中调用创建新区块函数来创建一个新区块
/Users/xxx/go/src/publicChain/part1-Basic-Prototype/main.go
package main
import (
"fmt"
"publicChain/part1-Basic-Prototype/BLC"
)
func main() {
block := BLC.Newblock(1, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0}, "Genesis Block")
fmt.Println(block)
}
上述代码需要先将我们之前编写区块信息的文件所在package引入,然后我们输入参数执行创建区块
go run /Users/xxx/go/src/publicChain/part1-Basic-Prototype/main.go&{1[00000000000000000000000000000000][71101110101115105115326610811199107]1644720196[2221981256418279119172158961671559963150224322361305913223723811172211891688977137]}
现在我们成功的创建了一个区块
1.3 创世区块
现在我们把上述函数改写一下,即只需要在main函数中提供一个参数就可以生成创世区块
/Users/xxx/go/src/publicChain/part2-Basic-Prototype/BLC/Block.go
//creat a new function to produce a Genesis Block
func CrateGenesisBlock(data string) *Block {
return Newblock(1, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, data)
}
在main函数中直接调用并运行
/Users/xxx/go/src/publicChain/part1-Basic-Prototype/main.go
package main
import (
"fmt"
"publicChain/part1-Basic-Prototype/BLC"
)
func main() {
genesisBlock := BLC.CrateGenesisBlock("Genesis Block")
fmt.Println(genesisBlock)
}
go run /Users/xxx/go/src/publicChain/part1-Basic-Prototype/main.go
&{1 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [71 101 110 101 115 105 115 32 66 108 111 99 107] 1644721098 [246 55 115 61 151 208 171 235 18 135 212 6 102 180 164 227 91 81 161 43 134 85 205 22 90 125 47 8 125 125 41 213]}
我们看到因为时间的不同我们又成功创建了一个新的创世区块
1.4 区块链结构
我们都知道区块链是一个个区块顺序连接起来的,我们现在来实现它
我们可以把区块链看作是一个有一个无限增加的[]Block的结构体
/Users/xxx/go/src/publicChain/part1-Basic-Prototype/BLC/Blockchain.go
package BLC
type Blockchain struct {
Blocks []*Block //an ordered series of blocks
}
接着我们用创世区块创建一条区块链,并成功打印
/Users/xxx/go/src/publicChain/part1-Basic-Prototype/BLC/Blockchain.go
func CreatBlockchainWithGenesisBlock() *Blockchain {
genesisBlock := CrateGenesisBlock("Genesis Data......")
return &Blockchain{[]*Block{genesisBlock}}
}
package main
import (
"fmt"
"publicChain/part1-Basic-Prototype/BLC"
)
func main() {
genesisBlockchain := BLC.CreatBlockchainWithGenesisBlock()
fmt.Println(genesisBlockchain)
}
go run /Users/xxx/go/src/publicChain/part1-Basic-Prototype/main.go
&{[0xc000078120]}
上述打印结果只是链中第一个元素的地址的引用,如果想看具体区块的信息也是可以的
func main() {
genesisBlockchain := BLC.CreatBlockchainWithGenesisBlock()
fmt.Println(genesisBlockchain)
fmt.Println(genesisBlockchain.Blocks)
fmt.Println(genesisBlockchain.Blocks[0])
}
go run /Users/xxx/go/src/publicChain/part1-Basic-Prototype/main.go
&{[0xc0000ae120]}
[0xc0000ae120]
&{1 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [71 101 110 101 115 105 115 32 68 97 116 97 46 46 46 46 46 46] 1644722516 [49 194 145 126 5 195 155 166 161 159 223 142 119 122 209 187 168 38 34 182 31 209 158 132 199 22 233 119 80 144 181 28]}
1.5 添加区块
我们在来定义一个函数将新生成的区块添加到链上
/Users/xxx/go/src/publicChain/part1-Basic-Prototype/BLC/Blockchain.go
//add a new block to blockchain
func (blc *Blockchain) AddBlockToBlockchain(height int64, preHash []byte, data string) {
//creat a new block
newBlock := Newblock(height, preHash, data)
//add a block to a blockchain
blc.Blocks = append(blc.Blocks, newBlock)
}
然后我们再改写一下main函数并运行
/Users/xxx/go/src/publicChain/part1-Basic-Prototype/main.go
package main
import (
"fmt"
"publicChain/part1-Basic-Prototype/BLC"
)
func main() {
//creat a Genesis Block
Blockchain := BLC.CreatBlockchainWithGenesisBlock()
// new block
Blockchain.AddBlockToBlockchain(Blockchain.Blocks[len(Blockchain.Blocks)-1].Height+1, Blockchain.Blocks[len(Blockchain.Blocks)-1].Hash, "Send 100RMB To Zhangqiang")
Blockchain.AddBlockToBlockchain(Blockchain.Blocks[len(Blockchain.Blocks)-1].Height+1, Blockchain.Blocks[len(Blockchain.Blocks)-1].Hash, "Send 300RMB To Xinyi")
Blockchain.AddBlockToBlockchain(Blockchain.Blocks[len(Blockchain.Blocks)-1].Height+1, Blockchain.Blocks[len(Blockchain.Blocks)-1].Hash, "Send 200RMB To Gaojian")
Blockchain.AddBlockToBlockchain(Blockchain.Blocks[len(Blockchain.Blocks)-1].Height+1, Blockchain.Blocks[len(Blockchain.Blocks)-1].Hash, "Send 500RMB To Xiaochu")
fmt.Println(Blockchain)
fmt.Println(string(Blockchain.Blocks[4].Data))
}
go run /Users/xxx/go/src/publicChain/part1-Basic-Prototype/main.go
&{[0xc000056180 0xc0000561e0 0xc000056240 0xc0000562a0 0xc000056300]}
Send 500RMB To Xiaochu
我们看到区块成功的被加入到了链上,并且我们可以访问单个节点的交易数据
版权归原作者 shiyivei 所有, 如有侵权,请联系我们删除。