/
chain.go
134 lines (103 loc) · 3.38 KB
/
chain.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
package cmd
import (
"fmt"
"log"
"strconv"
"github.com/manishmeganathan/blockweave/blockchain"
"github.com/manishmeganathan/blockweave/wallet"
"github.com/spf13/cobra"
)
// chainCmd represents the 'chain' command
var chainCmd = &cobra.Command{
Use: "chain",
Short: "Create/View Animus Blockchains",
Long: `Create/View Animus Blockchains`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Please invoke a chain command. Use 'animus chain --help' for usage instructions")
},
}
// chainShowCmd represents the 'chain show' command
var chainShowCmd = &cobra.Command{
Use: "show",
Short: "Show the chain of Blocks",
Long: `Show the chain of Blocks on the Animus Blockchain.
Prints the Hash of the previous block, current block and validation of
each block on the Animus Blockchain`,
Run: func(cmd *cobra.Command, args []string) {
chain, err := blockchain.AnimateBlockChain()
if err != nil {
fmt.Println("Animus Blockchain does not exist! Use 'animus chain create' to create one.")
chain.Database.Close()
return
}
defer chain.Database.Close()
iter := blockchain.NewIterator(chain)
fmt.Print("\n---- ANIMUS BLOCKCHAIN ----\n\n")
for {
block := iter.Next()
fmt.Printf("Previous Hash: %x\n", block.PrevHash)
fmt.Printf("Block Hash: %x\n", block.Hash)
fmt.Printf("Signed: %s\n\n", strconv.FormatBool(block.Validate()))
for _, txn := range block.Transactions {
fmt.Println(txn.String())
}
fmt.Println()
if len(block.PrevHash) == 0 {
break
}
}
fmt.Print("---- END OF BLOCKCHAIN ----\n\n")
},
}
// chainCreateCmd represents the 'chain create' command
var chainCreateCmd = &cobra.Command{
Use: "create",
Short: "Create a new Blockchain",
Long: `Create a new Blockchain with a coinbase transaction for the specified miner`,
Run: func(cmd *cobra.Command, args []string) {
address, _ := cmd.Flags().GetString("address")
if !wallet.ValidateWalletAddress(address) {
log.Panic("Invalid Address!")
}
chain, err := blockchain.SeedBlockChain(address)
if err != nil {
fmt.Println("Animus Blockchain already exists. Use 'animus chain show' to view it.")
chain.Database.Close()
return
}
defer chain.Database.Close()
chain.ReindexUTXOS()
fmt.Println("Animus Blockchain Created!")
},
}
// chainCreateCmd represents the 'chain create' command
var chainReindexCmd = &cobra.Command{
Use: "reindex",
Short: "Reindex the UTXO persistence layer",
Long: `Reindex the UTXO persistence layer`,
Run: func(cmd *cobra.Command, args []string) {
chain, err := blockchain.AnimateBlockChain()
if err != nil {
fmt.Println("Animus Blockchain does not exist! Use 'animus chain create' to create one.")
chain.Database.Close()
return
}
defer chain.Database.Close()
chain.ReindexUTXOS()
count := chain.CountUTXOS()
fmt.Printf("Done! There are %d unspent output transactions.", count)
},
}
func init() {
// Create the 'chain' command
rootCmd.AddCommand(chainCmd)
// Create the 'show' subcommand to 'chain'
chainCmd.AddCommand(chainShowCmd)
// Create the 'reindex' subcommand to 'chain'
chainCmd.AddCommand(chainReindexCmd)
// Create the 'create' subcommand to 'chain'
chainCmd.AddCommand(chainCreateCmd)
// Add the 'address' flag to the 'chain create' command and mark it as required
chainCreateCmd.Flags().StringP("address", "a", "", "address of the block miner")
chainCreateCmd.MarkFlagRequired("address")
}