From 150778f920057e79407115116e85d1dd4c144a34 Mon Sep 17 00:00:00 2001 From: Ivan Kuznetsov Date: Tue, 3 Oct 2017 15:54:31 +0700 Subject: [PATCH] Add -mine option to the 'send' command --- cli.go | 5 +++-- cli_send.go | 18 +++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/cli.go b/cli.go index 00e72d2..82a1127 100644 --- a/cli.go +++ b/cli.go @@ -19,7 +19,7 @@ func (cli *CLI) printUsage() { fmt.Println(" listaddresses - Lists all addresses from the wallet file") fmt.Println(" printchain - Print all the blocks of the blockchain") fmt.Println(" reindexutxo - Rebuilds the UTXO set") - fmt.Println(" send -from FROM -to TO -amount AMOUNT - Send AMOUNT of coins from FROM address to TO") + fmt.Println(" send -from FROM -to TO -amount AMOUNT -mine - Send AMOUNT of coins from FROM address to TO. Mine on the same node, when -mine is set.") fmt.Println(" startnode - Start a node with ID specified in NODE_ID env. var") } @@ -54,6 +54,7 @@ func (cli *CLI) Run() { sendFrom := sendCmd.String("from", "", "Source wallet address") sendTo := sendCmd.String("to", "", "Destination wallet address") sendAmount := sendCmd.Int("amount", 0, "Amount to send") + sendMine := sendCmd.Bool("mine", false, "Mine immediately on the same node") switch os.Args[1] { case "getbalance": @@ -139,7 +140,7 @@ func (cli *CLI) Run() { os.Exit(1) } - cli.send(*sendFrom, *sendTo, *sendAmount, nodeID) + cli.send(*sendFrom, *sendTo, *sendAmount, nodeID, *sendMine) } if startNodeCmd.Parsed() { diff --git a/cli_send.go b/cli_send.go index 84b500a..cb607a8 100644 --- a/cli_send.go +++ b/cli_send.go @@ -5,7 +5,7 @@ import ( "log" ) -func (cli *CLI) send(from, to string, amount int, nodeID string) { +func (cli *CLI) send(from, to string, amount int, nodeID string, mineNow bool) { if !ValidateAddress(from) { log.Panic("ERROR: Sender address is not valid") } @@ -25,16 +25,16 @@ func (cli *CLI) send(from, to string, amount int, nodeID string) { tx := NewUTXOTransaction(&wallet, to, amount, &UTXOSet) cbTx := NewCoinbaseTX(from, "") - // txs := []*Transaction{cbTx, tx} - // var txHashes [][]byte - // txHashes = append(txHashes, tx.Hash()) - // txHashes = append(txHashes, cbTx.Hash()) + if mineNow { + txs := []*Transaction{cbTx, tx} - sendTx(knownNodes[0], tx) - sendTx(knownNodes[0], cbTx) + newBlock := bc.MineBlock(txs) + UTXOSet.Update(newBlock) + } else { + sendTx(knownNodes[0], tx) + sendTx(knownNodes[0], cbTx) + } - // newBlock := bc.MineBlock(txs) - // UTXOSet.Update(newBlock) fmt.Println("Success!") }