区块链V3版本实现之六

命令行demo代码:

 1 package main
 2 
 3 import (
 4    "fmt"
 5    "os"
 6 )
 7 
 8 func main()  {
 9    //返回的是数组
10    cmds := os.Args
11 
12    //通过字符比较,去选择执行相应的程序
13    for i, cmd := range cmds{
14       fmt.Printf("cmd[%d] : %s\n", i, cmd)
15    }
16 }

显示效果:

 

 

 

使用命令行分析:

  1. 所有的支配动作交给命令行来做
  2. 主函数只需要调用命令行结构即可
  3. 根据输入的不同命令,命令行做相应动作

    a)  addBlock

    b) printChain

 

CLI:command line的缩写

type CLI struct{

  bc*BlockChain

}

 

添加区块的时候:bc.addBlock(data),data通过os.Args拿回来

打印区块链时候:遍历区块链,不需要外部输入数据

 

部分代码(cli.go文件中命令行的实现):

 1 package main
 2 
 3 import (
 4    "fmt"
 5    "os"
 6 )
 7 
 8 const Usage = `
 9    ./blockchain addBlock "xxxxxx"   添加数据到区块链
10    ./blockchain printChain          打印区块链
11 `
12 
13 type CLI struct {
14    bc *BlockChain
15 }
16 
17 //给CLI提供一个方法,进行命令解析,从而执行调度
18 func (cli *CLI) Run() {
19 
20    cmds := os.Args
21 
22    if len(cmds) < 2 {
23       fmt.Printf(Usage)
24       os.Exit(1)
25    }
26 
27    switch cmds[1] {
28    case "addBlock":
29       if len(cmds) != 3 {
30          fmt.Printf(Usage)
31          os.Exit(1)
32       }
33 
34       fmt.Printf("添加区块命令被调用, 数据:%s\n", cmds[2])
35 
36       data := cmds[2]
37       cli.AddBlock(data)
38 
39    case "printChain":
40       fmt.Printf("打印区块链命令被调用\n")
41       cli.PrintChain()
42 
43    default:
44       fmt.Printf("无效的命令,请检查\n")
45       fmt.Printf(Usage)
46    }
47    //添加区块的时候: bc.addBlock(data), data 通过os.Args拿回来
48    //打印区块链时候:遍历区块链,不需要外部输入数据
49 }

部分代码(commands.go的相关命令实现):

 1 package main
 2 
 3 import (
 4    "fmt"
 5    "time"
 6    "bytes"
 7 )
 8 
 9 //实现具体的命令
10 
11 func (cli *CLI) AddBlock(data string) {
12    cli.bc.AddBlock(data)
13    fmt.Printf("添加区块成功!\n")
14 }
15 
16 func (cli *CLI) PrintChain() {
17 
18    it := cli.bc.NewIterator()
19 
20    for {
21       block := it.Next()
22       fmt.Printf("++++++++++++++++++++++++++++++++\n")
23 
24       fmt.Printf("Version : %d\n", block.Version)
25       fmt.Printf("PrevBlockHash : %x\n", block.PrevBlockHash)
26       fmt.Printf("MerKleRoot : %x\n", block.MerKleRoot)
27 
28       timeFormat := time.Unix(int64(block.TimeStamp), 0).Format("2006-01-02 15:04:05")
29       fmt.Printf("TimeStamp : %s\n", timeFormat)
30 
31       fmt.Printf("Difficulity : %d\n", block.Difficulity)
32       fmt.Printf("Nonce : %d\n", block.Nonce)
33       fmt.Printf("Hash : %x\n", block.Hash)
34       fmt.Printf("Data : %s\n", block.Data)
35 
36       pow := NewProofOfWork(block)
37       fmt.Printf("IsValid: %v\n", pow.IsValid())
38 
39       if bytes.Equal(block.PrevBlockHash, []byte{}) {
40          fmt.Printf("区块链遍历结束!\n")
41          break
42       }
43    }
44 }

部分代码(main.go文件的改写):

package main

func main() {

   bc := NewBlockChain()
   defer bc.db.Close()
   cli := CLI{bc}
   cli.Run()
}

运行脚本(方便执行):

1 #!/bin/bash
2 rm blockchain
3 rm *.db
4 
5 go build -o blockchain *.go
6 ./blockchain

显示效果:

 

 V3版本项目目录结构:

 

 

本人的系统是windows10,不能用命令行执行,如果有大佬知道如何执行,请指点一下,谢谢!

posted @ 2020-12-07 11:05  北漂的尘埃  阅读(68)  评论(0编辑  收藏  举报