From 751d7913991c801066097d91deaf0651cc24c72e Mon Sep 17 00:00:00 2001 From: Ivan Kuznetsov Date: Sun, 3 Sep 2017 11:17:10 +0700 Subject: [PATCH] Implement 'createblockchain' command --- blockchain.go | 84 ++++++++++++++++++++++++++++++++++++--------------- cli.go | 42 ++++++++++++++++++++++---- main.go | 5 +-- 3 files changed, 97 insertions(+), 34 deletions(-) diff --git a/blockchain.go b/blockchain.go index 780e172..53c0a72 100644 --- a/blockchain.go +++ b/blockchain.go @@ -3,6 +3,7 @@ package main import ( "fmt" "log" + "os" "github.com/boltdb/bolt" ) @@ -160,8 +161,21 @@ func (i *BlockchainIterator) Next() *Block { return block } +func dbExists() bool { + if _, err := os.Stat(dbFile); os.IsNotExist(err) { + return false + } + + return true +} + // NewBlockchain creates a new Blockchain with genesis Block func NewBlockchain(address string) *Blockchain { + if dbExists() == false { + fmt.Println("No existing blockchain found. Creating one first.") + os.Exit(1) + } + var tip []byte db, err := bolt.Open(dbFile, 0600, nil) if err != nil { @@ -170,30 +184,52 @@ func NewBlockchain(address string) *Blockchain { err = db.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte(blocksBucket)) - - if b == nil { - fmt.Println("No existing blockchain found. Creating a new one...") - cbtx := NewCoinbaseTX(address, genesisCoinbase) - genesis := NewGenesisBlock(cbtx) - - b, err := tx.CreateBucket([]byte(blocksBucket)) - if err != nil { - log.Panic(err) - } - - err = b.Put(genesis.Hash, genesis.Serialize()) - if err != nil { - log.Panic(err) - } - - err = b.Put([]byte("l"), genesis.Hash) - if err != nil { - log.Panic(err) - } - tip = genesis.Hash - } else { - tip = b.Get([]byte("l")) - } + tip = b.Get([]byte("l")) + + return nil + }) + + if err != nil { + log.Panic(err) + } + + bc := Blockchain{tip, db} + + return &bc +} + +// CreateBlockchain creates a new blockchain DB +func CreateBlockchain(address string) *Blockchain { + if dbExists() { + fmt.Println("Blockchain already exists.") + os.Exit(1) + } + + var tip []byte + db, err := bolt.Open(dbFile, 0600, nil) + if err != nil { + log.Panic(err) + } + + err = db.Update(func(tx *bolt.Tx) error { + cbtx := NewCoinbaseTX(address, genesisCoinbase) + genesis := NewGenesisBlock(cbtx) + + b, err := tx.CreateBucket([]byte(blocksBucket)) + if err != nil { + log.Panic(err) + } + + err = b.Put(genesis.Hash, genesis.Serialize()) + if err != nil { + log.Panic(err) + } + + err = b.Put([]byte("l"), genesis.Hash) + if err != nil { + log.Panic(err) + } + tip = genesis.Hash return nil }) diff --git a/cli.go b/cli.go index b094f12..1d7b1d9 100644 --- a/cli.go +++ b/cli.go @@ -9,14 +9,21 @@ import ( ) // CLI responsible for processing command line arguments -type CLI struct { - bc *Blockchain +type CLI struct{} + +func (cli *CLI) createBlockchain(address string) { + bc := CreateBlockchain(address) + bc.db.Close() + fmt.Println("Done!") } func (cli *CLI) getBalance(address string) { + bc := NewBlockchain(address) + defer bc.db.Close() + balance := 0 - utxs := cli.bc.FindUnspentTransactions(address) + utxs := bc.FindUnspentTransactions(address) for _, tx := range utxs { for _, out := range tx.Vout { @@ -32,6 +39,7 @@ func (cli *CLI) getBalance(address string) { func (cli *CLI) printUsage() { fmt.Println("Usage:") fmt.Println(" getbalance -address ADDRESS - Get balance of ADDRESS") + fmt.Println(" createblockchain -address ADDRESS - Create a blockchain and send genesis block reward to ADDRESS") 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") } @@ -44,7 +52,11 @@ func (cli *CLI) validateArgs() { } func (cli *CLI) printChain() { - bci := cli.bc.Iterator() + // TODO: Fix this + bc := NewBlockchain("") + defer bc.db.Close() + + bci := bc.Iterator() for { block := bci.Next() @@ -62,8 +74,11 @@ func (cli *CLI) printChain() { } func (cli *CLI) send(from, to string, amount int) { - tx := NewUTXOTransaction(from, to, amount) - cli.bc.AddBlock([]*Transaction{tx}) + bc := NewBlockchain(from) + defer bc.db.Close() + + tx := NewUTXOTransaction(from, to, amount, bc) + bc.AddBlock([]*Transaction{tx}) fmt.Println("Success!") } @@ -72,10 +87,12 @@ func (cli *CLI) Run() { cli.validateArgs() getBalanceCmd := flag.NewFlagSet("getbalance", flag.ExitOnError) + createBlockchainCmd := flag.NewFlagSet("createblockchain", flag.ExitOnError) sendCmd := flag.NewFlagSet("send", flag.ExitOnError) printChainCmd := flag.NewFlagSet("printchain", flag.ExitOnError) getBalanceAddress := getBalanceCmd.String("address", "", "The address to get balance for") + createBlockchainAddress := createBlockchainCmd.String("address", "", "The address to send genesis block reward to") sendFrom := sendCmd.String("from", "", "Source wallet address") sendTo := sendCmd.String("to", "", "Destination wallet address") sendAmount := sendCmd.Int("amount", 0, "Amount to send") @@ -86,6 +103,11 @@ func (cli *CLI) Run() { if err != nil { log.Panic(err) } + case "createblockchain": + err := createBlockchainCmd.Parse(os.Args[2:]) + if err != nil { + log.Panic(err) + } case "printchain": err := printChainCmd.Parse(os.Args[2:]) if err != nil { @@ -109,6 +131,14 @@ func (cli *CLI) Run() { cli.getBalance(*getBalanceAddress) } + if createBlockchainCmd.Parsed() { + if *createBlockchainAddress == "" { + createBlockchainCmd.Usage() + os.Exit(1) + } + cli.createBlockchain(*createBlockchainAddress) + } + if printChainCmd.Parsed() { cli.printChain() } diff --git a/main.go b/main.go index 5ba110b..a48ad8e 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,6 @@ package main func main() { - bc := NewBlockchain() - defer bc.db.Close() - - cli := CLI{bc} + cli := CLI{} cli.Run() }