Skip to content

Commit

Permalink
Blockchain file name must depend on node ID
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeiwan committed Oct 1, 2017
1 parent 504b6c8 commit 57f3680
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 23 deletions.
14 changes: 8 additions & 6 deletions blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/boltdb/bolt"
)

const dbFile = "blockchain.db"
const dbFile = "blockchain_%s.db"
const blocksBucket = "blocks"
const genesisCoinbaseData = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks"

Expand All @@ -23,8 +23,9 @@ type Blockchain struct {
}

// CreateBlockchain creates a new blockchain DB
func CreateBlockchain(address string) *Blockchain {
if dbExists() {
func CreateBlockchain(address, nodeID string) *Blockchain {
dbFile := fmt.Sprintf(dbFile, nodeID)
if dbExists(dbFile) {
fmt.Println("Blockchain already exists.")
os.Exit(1)
}
Expand Down Expand Up @@ -68,8 +69,9 @@ func CreateBlockchain(address string) *Blockchain {
}

// NewBlockchain creates a new Blockchain with genesis Block
func NewBlockchain() *Blockchain {
if dbExists() == false {
func NewBlockchain(nodeID string) *Blockchain {
dbFile := fmt.Sprintf(dbFile, nodeID)
if dbExists(dbFile) == false {
fmt.Println("No existing blockchain found. Create one first.")
os.Exit(1)
}
Expand Down Expand Up @@ -246,7 +248,7 @@ func (bc *Blockchain) VerifyTransaction(tx *Transaction) bool {
return tx.Verify(prevTXs)
}

func dbExists() bool {
func dbExists(dbFile string) bool {
if _, err := os.Stat(dbFile); os.IsNotExist(err) {
return false
}
Expand Down
17 changes: 12 additions & 5 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"log"

"os"
)

Expand Down Expand Up @@ -33,6 +34,12 @@ func (cli *CLI) validateArgs() {
func (cli *CLI) Run() {
cli.validateArgs()

nodeID := os.Getenv("NODE_ID")
if nodeID == "" {
fmt.Printf("NODE_ID env. var is not set!")
os.Exit(1)
}

getBalanceCmd := flag.NewFlagSet("getbalance", flag.ExitOnError)
createBlockchainCmd := flag.NewFlagSet("createblockchain", flag.ExitOnError)
createWalletCmd := flag.NewFlagSet("createwallet", flag.ExitOnError)
Expand Down Expand Up @@ -99,15 +106,15 @@ func (cli *CLI) Run() {
getBalanceCmd.Usage()
os.Exit(1)
}
cli.getBalance(*getBalanceAddress)
cli.getBalance(*getBalanceAddress, nodeID)
}

if createBlockchainCmd.Parsed() {
if *createBlockchainAddress == "" {
createBlockchainCmd.Usage()
os.Exit(1)
}
cli.createBlockchain(*createBlockchainAddress)
cli.createBlockchain(*createBlockchainAddress, nodeID)
}

if createWalletCmd.Parsed() {
Expand All @@ -119,11 +126,11 @@ func (cli *CLI) Run() {
}

if printChainCmd.Parsed() {
cli.printChain()
cli.printChain(nodeID)
}

if reindexUTXOCmd.Parsed() {
cli.reindexUTXO()
cli.reindexUTXO(nodeID)
}

if sendCmd.Parsed() {
Expand All @@ -132,7 +139,7 @@ func (cli *CLI) Run() {
os.Exit(1)
}

cli.send(*sendFrom, *sendTo, *sendAmount)
cli.send(*sendFrom, *sendTo, *sendAmount, nodeID)
}

if startNodeCmd.Parsed() {
Expand Down
4 changes: 2 additions & 2 deletions cli_createblockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"log"
)

func (cli *CLI) createBlockchain(address string) {
func (cli *CLI) createBlockchain(address, nodeID string) {
if !ValidateAddress(address) {
log.Panic("ERROR: Address is not valid")
}
bc := CreateBlockchain(address)
bc := CreateBlockchain(address, nodeID)
defer bc.db.Close()

UTXOSet := UTXOSet{bc}
Expand Down
4 changes: 2 additions & 2 deletions cli_getbalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"log"
)

func (cli *CLI) getBalance(address string) {
func (cli *CLI) getBalance(address, nodeID string) {
if !ValidateAddress(address) {
log.Panic("ERROR: Address is not valid")
}
bc := NewBlockchain()
bc := NewBlockchain(nodeID)
UTXOSet := UTXOSet{bc}
defer bc.db.Close()

Expand Down
4 changes: 2 additions & 2 deletions cli_printchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strconv"
)

func (cli *CLI) printChain() {
bc := NewBlockchain()
func (cli *CLI) printChain(nodeID string) {
bc := NewBlockchain(nodeID)
defer bc.db.Close()

bci := bc.Iterator()
Expand Down
4 changes: 2 additions & 2 deletions cli_reindexutxo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package main

import "fmt"

func (cli *CLI) reindexUTXO() {
bc := NewBlockchain()
func (cli *CLI) reindexUTXO(nodeID string) {
bc := NewBlockchain(nodeID)
UTXOSet := UTXOSet{bc}
UTXOSet.Reindex()

Expand Down
4 changes: 2 additions & 2 deletions cli_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"log"
)

func (cli *CLI) send(from, to string, amount int) {
func (cli *CLI) send(from, to string, amount int, nodeID string) {
if !ValidateAddress(from) {
log.Panic("ERROR: Sender address is not valid")
}
if !ValidateAddress(to) {
log.Panic("ERROR: Recipient address is not valid")
}

bc := NewBlockchain()
bc := NewBlockchain(nodeID)
UTXOSet := UTXOSet{bc}
defer bc.db.Close()

Expand Down
6 changes: 4 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func handleVersion(request []byte) {
knownNodes = append(knownNodes, payload.AddrFrom)
}

func handleConnection(conn net.Conn) {
func handleConnection(conn net.Conn, bc *Blockchain) {
request, err := ioutil.ReadAll(conn)
if err != nil {
log.Panic(err)
Expand Down Expand Up @@ -161,12 +161,14 @@ func StartServer(nodeID string) {
sendVersion(fmt.Sprintf("localhost:%s", dnsNodeID))
}

bc := NewBlockchain(nodeID)

for {
conn, err := ln.Accept()
if err != nil {
log.Panic(err)
}
go handleConnection(conn)
go handleConnection(conn, bc)
}
}

Expand Down

1 comment on commit 57f3680

@Jeongjaegu
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.