-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
61 lines (51 loc) · 1.2 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
package main
import (
"os"
"github.com/alanvivona/blockchaingo/src/cli"
"github.com/sirupsen/logrus"
"github.com/alanvivona/blockchaingo/src/blockchain"
)
func main() {
message, listLast, listAll, err := cli.Parse()
if err != nil {
logrus.Error("Failed to parse cli arguments: ", err)
os.Exit(1)
}
bc := blockchain.Chain{}
bc.Init()
if message != nil && len(*message) > 0 {
logrus.Info("Adding block to the chain: ", *message)
err := bc.AddBlock([]byte(*message))
if err != nil {
logrus.Error("Failed to add block: ", err, message)
return
}
}
if listAll != nil && *listAll {
cli.PrintLine()
logrus.Info("= Blockchain =")
err := bc.IterateLink(
func(b *blockchain.Block) {
b.Print()
logrus.Info("--------")
},
func() { logrus.Info("--------") },
func() { logrus.Info("End of blockchain") },
)
if err != nil {
logrus.Error("Failed to iterate over blockchain: ", err)
return
}
cli.PrintLine()
} else if listLast != nil && *listLast {
cli.PrintLine()
logrus.Info("= Last Block =")
lastBlock, err := bc.GetLastBlock()
if err != nil {
logrus.Error("Failed to get the last block: ", err)
return
}
lastBlock.Print()
cli.PrintLine()
}
}