Implement 'createblockchain' command

This commit is contained in:
Ivan Kuznetsov 2017-09-03 11:17:10 +07:00
parent 87eb17bbe5
commit 751d791399
3 changed files with 97 additions and 34 deletions

View File

@ -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
})

42
cli.go
View File

@ -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()
}

View File

@ -1,9 +1,6 @@
package main
func main() {
bc := NewBlockchain()
defer bc.db.Close()
cli := CLI{bc}
cli := CLI{}
cli.Run()
}