Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add blockchain reindexing to cli #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand All @@ -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!")
}

Expand All @@ -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
Expand All @@ -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!")
}

Expand All @@ -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")
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}