-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
145 lines (116 loc) · 3.43 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
// I decided to learn golang by building a blockchain
// So I'm not really familiar with a lot of concepts yet but
// I plan to learn on the Go!! (see what I did there)
//
// Cheers to learning Golang!!!
// Cheers to building and learning on the blockchain!!!!
//
//////////////////////////////////////////////////////////////////
// Building a blockchain
// A public decentralised, trustless database based on a peer to peer network
// the imports
// a set of tools or libraries to make the task faster
// the imports to github checks online first
// then it checks the local commits
import (
"flag"
"fmt"
"os"
"runtime"
"strconv"
"github.com/AgbaD/go-blockchain/blockchain"
)
type CommandLine struct {
Blockchain *blockchain.BlockChain
}
func (cli *CommandLine) printUsage() {
fmt.Println("\n\nUsage:")
fmt.Println("add -block BLOCK_DATA - add a block to the chain")
fmt.Println("print - Prints the blocks on the chain ")
}
func (cli *CommandLine) ValidateArgs() {
if len(os.Args) < 2 {
cli.printUsage()
// exits the application by shutting down the goroutine
runtime.Goexit()
}
}
func (cli *CommandLine) addBlock(data string) {
cli.Blockchain.AddBlock(data)
fmt.Println("Added Block")
}
func (cli *CommandLine) printChain() {
fmt.Println(cli.Blockchain.Blocks)
for _, block := range cli.Blockchain.Blocks {
// string interpolation
fmt.Printf("Previous hash: %x\n", block.PrevHash)
fmt.Printf("Block data: %s\n", block.Data)
fmt.Printf("Block hash: %x\n", block.Hash)
// fmt.Printf("Block Nonce: %x\n", block.Nonce)
// Get a new proof for the block
pow := blockchain.NewProof(block)
// convert the response of the validation -a boolean- to string format
fmt.Printf("POW: %s\n", strconv.FormatBool(pow.Validate()))
fmt.Println()
if len(block.PrevHash) == 0 {
break
}
}
}
func (cli *CommandLine) Run() {
cli.ValidateArgs()
addBlockCmd := flag.NewFlagSet("add", flag.ExitOnError)
printChainCmd := flag.NewFlagSet("print", flag.ExitOnError)
addBlockData := addBlockCmd.String("block", "", "Block data")
switch os.Args[1] {
case "add":
err := addBlockCmd.Parse(os.Args[2:])
blockchain.Handle(err)
case "print":
err := printChainCmd.Parse(os.Args[2:])
blockchain.Handle(err)
default:
cli.printUsage()
runtime.Goexit()
}
if addBlockCmd.Parsed() {
if *addBlockData == "" {
addBlockCmd.Usage()
runtime.Goexit()
} else {
cli.addBlock(*addBlockData)
}
}
if printChainCmd.Parsed() {
cli.printChain()
}
}
func main() {
// chain := blockchain.InitBlockchain()
// chain.AddBlock("First block")
// fmt.Println()
// chain.AddBlock("Second block")
// fmt.Println()
// chain.AddBlock("Third block")
// fmt.Println()
// for _, block := range chain.Blocks {
// // string interpolation
// fmt.Printf("Previous hash: %x\n", block.PrevHash)
// fmt.Printf("Block data: %s\n", block.Data)
// fmt.Printf("Block hash: %x\n", block.Hash)
// // fmt.Printf("Block Nonce: %x\n", block.Nonce)
// // Get a new proof for the block
// pow := blockchain.NewProof(block)
// // convert the response of the validation -a boolean- to string format
// fmt.Printf("POW: %s\n", strconv.FormatBool(pow.Validate()))
// fmt.Println()
// }
// fail safes to help close the database and give it time
// garbage collect the keys and values
defer os.Exit(0)
chain := blockchain.InitBlockchain()
// only executes if the go channel is able to exit properly
cli := CommandLine{chain}
cli.Run()
}