Implement basic CLI

This commit is contained in:
Ivan Kuznetsov 2017-08-28 16:48:53 +07:00
parent d99bbc1b63
commit 52477175c1
1 changed files with 23 additions and 13 deletions

36
main.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"os"
"strconv" "strconv"
) )
@ -9,23 +10,32 @@ func main() {
bc := NewBlockchain() bc := NewBlockchain()
defer bc.db.Close() defer bc.db.Close()
// bc.AddBlock("Send 1 BTC to Ivan") if len(os.Args) < 2 {
// bc.AddBlock("Send 2 more BTC to Ivan") fmt.Println("Wrong!")
os.Exit(1)
}
bci := bc.Iterator() if os.Args[1] == "addBlock" {
bc.AddBlock(os.Args[2])
fmt.Println("Success!")
}
for { if os.Args[1] == "printChain" {
block := bci.Next() bci := bc.Iterator()
fmt.Printf("Prev. hash: %x\n", block.PrevBlockHash) for {
fmt.Printf("Data: %s\n", block.Data) block := bci.Next()
fmt.Printf("Hash: %x\n", block.Hash)
pow := NewProofOfWork(block)
fmt.Printf("PoW: %s\n", strconv.FormatBool(pow.Validate()))
fmt.Println()
if len(block.PrevBlockHash) == 0 { fmt.Printf("Prev. hash: %x\n", block.PrevBlockHash)
break fmt.Printf("Data: %s\n", block.Data)
fmt.Printf("Hash: %x\n", block.Hash)
pow := NewProofOfWork(block)
fmt.Printf("PoW: %s\n", strconv.FormatBool(pow.Validate()))
fmt.Println()
if len(block.PrevBlockHash) == 0 {
break
}
} }
} }
} }