diff --git a/cli/cli.go b/cli/cli.go index e9997c3..2142f1c 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -22,6 +22,7 @@ func (cli *CommandLine) printUsage() { fmt.Println(" send -from FROM -to TO -amount AMOUNT - Send amount of coins") fmt.Println(" createwallet - Creates a new Wallet") fmt.Println(" listaddresses - Lists the addresses in our wallet file") + fmt.Println(" reindexutxo - Rebuilds the UTXO set") } func (cli *CommandLine) validateArgs() { @@ -60,6 +61,9 @@ func (cli *CommandLine) printChain() { fmt.Printf("Hash: %x\n", block.Hash) pow := blockchain.NewProof(block) fmt.Printf("PoW: %s\n", strconv.FormatBool(pow.Validate())) + for _, tx := range block.Transactions { + fmt.Println(tx) + } fmt.Println() if len(block.PrevHash) == 0 { @@ -69,8 +73,15 @@ func (cli *CommandLine) printChain() { } func (cli *CommandLine) createBlockChain(address string) { + if !wallet.ValidateAddress(address) { + log.Panic("Address is not Valid") + } chain := blockchain.InitBlockChain(address) chain.Database.Close() + + UTXOSet := blockchain.UTXOSet{Blockchain: chain} + UTXOSet.Reindex() + fmt.Println("Finished!") } @@ -79,12 +90,13 @@ func (cli *CommandLine) getBalance(address string) { log.Panic("Address is not Valid") } chain := blockchain.ContinueBlockChain(address) + UTXOSet := blockchain.UTXOSet{Blockchain: chain} defer chain.Database.Close() balance := 0 pubKeyHash := wallet.Base58Decode([]byte(address)) pubKeyHash = pubKeyHash[1 : len(pubKeyHash) - 4] - UTXOs := chain.FindUTXO(pubKeyHash) + UTXOs := UTXOSet.FindUnspentTransactions(pubKeyHash) for _, out := range UTXOs { balance += out.Value @@ -94,11 +106,20 @@ func (cli *CommandLine) getBalance(address string) { } func (cli *CommandLine) send(from, to string, amount int) { + if !wallet.ValidateAddress(to) { + log.Panic("Address is not Valid") + } + if !wallet.ValidateAddress(from) { + log.Panic("Address is not Valid") + } chain := blockchain.ContinueBlockChain(from) + UTXOSet := blockchain.UTXOSet{Blockchain: chain} defer chain.Database.Close() - tx := blockchain.NewTransaction(from, to, amount, chain) - chain.AddBlock([]*blockchain.Transaction{tx}) + tx := blockchain.NewTransaction(from, to, amount, &UTXOSet) + cbTx := blockchain.CoinbaseTx(from, "") + block := chain.AddBlock([]*blockchain.Transaction{cbTx, tx}) + UTXOSet.Update(block) fmt.Println("Success!") } @@ -111,7 +132,7 @@ func (cli *CommandLine) Run() { printChainCmd := flag.NewFlagSet("printchain", flag.ExitOnError) createWalletCmd := flag.NewFlagSet("createwallet", flag.ExitOnError) listAddressesCmd := flag.NewFlagSet("listaddresses", flag.ExitOnError) - + reindexUTXOCmd := flag.NewFlagSet("reindexutxo", flag.ExitOnError) getBalanceAddress := getBalanceCmd.String("address", "", "The address to get balance for") createBlockchainAddress := createBlockchainCmd.String("address", "", "The address to send genesis block reward to") @@ -150,6 +171,11 @@ func (cli *CommandLine) Run() { if err != nil { log.Panic(err) } + case "reindexutxo": + err := reindexUTXOCmd.Parse(os.Args[2:]) + if err != nil { + log.Panic(err) + } default: cli.printUsage() runtime.Goexit() @@ -191,3 +217,13 @@ func (cli *CommandLine) Run() { cli.send(*sendFrom, *sendTo, *sendAmount) } } + +func (cli *CommandLine) reindexUTXO() { + chain := blockchain.ContinueBlockChain("") + defer chain.Database.Close() + UTXOSet := blockchain.UTXOSet{Blockchain: chain} + UTXOSet.Reindex() + + count := UTXOSet.CountTransactions() + fmt.Printf("Done! There are %d transactions in the UTXO set.\n", count) +}