Replace 'addblock' command with 'spend'
This commit is contained in:
parent
8e6636983a
commit
6941c5f32e
44
cli.go
44
cli.go
|
@ -15,8 +15,8 @@ type CLI struct {
|
||||||
|
|
||||||
func (cli *CLI) printUsage() {
|
func (cli *CLI) printUsage() {
|
||||||
fmt.Println("Usage:")
|
fmt.Println("Usage:")
|
||||||
fmt.Println(" addblock -data BLOCK_DATA - add a block to the blockchain")
|
|
||||||
fmt.Println(" printchain - print all the blocks of the blockchain")
|
fmt.Println(" printchain - print all the blocks of the blockchain")
|
||||||
|
fmt.Println(" send -from FROM -to TO -amount AMOUNT - send AMOUNT of coins from FROM address to TO")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *CLI) validateArgs() {
|
func (cli *CLI) validateArgs() {
|
||||||
|
@ -26,11 +26,6 @@ func (cli *CLI) validateArgs() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *CLI) addBlock(data string) {
|
|
||||||
cli.bc.AddBlock(data)
|
|
||||||
fmt.Println("Success!")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *CLI) printChain() {
|
func (cli *CLI) printChain() {
|
||||||
bci := cli.bc.Iterator()
|
bci := cli.bc.Iterator()
|
||||||
|
|
||||||
|
@ -49,23 +44,31 @@ func (cli *CLI) printChain() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cli *CLI) send(from, to string, amount int) {
|
||||||
|
tx := NewUTXOTransaction(from, to, amount)
|
||||||
|
cli.bc.AddBlock([]*Transaction{tx})
|
||||||
|
fmt.Println("Success!")
|
||||||
|
}
|
||||||
|
|
||||||
// Run parses command line arguments and processes commands
|
// Run parses command line arguments and processes commands
|
||||||
func (cli *CLI) Run() {
|
func (cli *CLI) Run() {
|
||||||
cli.validateArgs()
|
cli.validateArgs()
|
||||||
|
|
||||||
addBlockCmd := flag.NewFlagSet("addblock", flag.ExitOnError)
|
sendCmd := flag.NewFlagSet("send", flag.ExitOnError)
|
||||||
printChainCmd := flag.NewFlagSet("printchain", flag.ExitOnError)
|
printChainCmd := flag.NewFlagSet("printchain", flag.ExitOnError)
|
||||||
|
|
||||||
addBlockData := addBlockCmd.String("data", "", "Block data")
|
sendFrom := sendCmd.String("from", "", "Source wallet address")
|
||||||
|
sendTo := sendCmd.String("to", "", "Destination wallet address")
|
||||||
|
sendAmount := sendCmd.Int("amount", 0, "Amount to send")
|
||||||
|
|
||||||
switch os.Args[1] {
|
switch os.Args[1] {
|
||||||
case "addblock":
|
case "printchain":
|
||||||
err := addBlockCmd.Parse(os.Args[2:])
|
err := printChainCmd.Parse(os.Args[2:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
case "printchain":
|
case "send":
|
||||||
err := printChainCmd.Parse(os.Args[2:])
|
err := sendCmd.Parse(os.Args[2:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
|
@ -74,15 +77,16 @@ func (cli *CLI) Run() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if addBlockCmd.Parsed() {
|
|
||||||
if *addBlockData == "" {
|
|
||||||
addBlockCmd.Usage()
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
cli.addBlock(*addBlockData)
|
|
||||||
}
|
|
||||||
|
|
||||||
if printChainCmd.Parsed() {
|
if printChainCmd.Parsed() {
|
||||||
cli.printChain()
|
cli.printChain()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if sendCmd.Parsed() {
|
||||||
|
if *sendFrom == "" || *sendTo == "" || *sendAmount <= 0 {
|
||||||
|
sendCmd.Usage()
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
cli.send(*sendFrom, *sendTo, *sendAmount)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue