diff --git a/blockchain.go b/blockchain.go index e00fb40..842905a 100644 --- a/blockchain.go +++ b/blockchain.go @@ -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" @@ -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) } @@ -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) } @@ -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 } diff --git a/cli.go b/cli.go index df4af02..7441e3e 100644 --- a/cli.go +++ b/cli.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "log" + "os" ) @@ -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) @@ -99,7 +106,7 @@ func (cli *CLI) Run() { getBalanceCmd.Usage() os.Exit(1) } - cli.getBalance(*getBalanceAddress) + cli.getBalance(*getBalanceAddress, nodeID) } if createBlockchainCmd.Parsed() { @@ -107,7 +114,7 @@ func (cli *CLI) Run() { createBlockchainCmd.Usage() os.Exit(1) } - cli.createBlockchain(*createBlockchainAddress) + cli.createBlockchain(*createBlockchainAddress, nodeID) } if createWalletCmd.Parsed() { @@ -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() { @@ -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() { diff --git a/cli_createblockchain.go b/cli_createblockchain.go index 358e88b..7e69405 100644 --- a/cli_createblockchain.go +++ b/cli_createblockchain.go @@ -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} diff --git a/cli_getbalance.go b/cli_getbalance.go index 814569d..a86e3dc 100644 --- a/cli_getbalance.go +++ b/cli_getbalance.go @@ -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() diff --git a/cli_printchain.go b/cli_printchain.go index 8541d2f..ba22cf5 100644 --- a/cli_printchain.go +++ b/cli_printchain.go @@ -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() diff --git a/cli_reindexutxo.go b/cli_reindexutxo.go index 74d3b03..87db324 100644 --- a/cli_reindexutxo.go +++ b/cli_reindexutxo.go @@ -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() diff --git a/cli_send.go b/cli_send.go index 456c264..a7c6f72 100644 --- a/cli_send.go +++ b/cli_send.go @@ -5,7 +5,7 @@ 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") } @@ -13,7 +13,7 @@ func (cli *CLI) send(from, to string, amount int) { log.Panic("ERROR: Recipient address is not valid") } - bc := NewBlockchain() + bc := NewBlockchain(nodeID) UTXOSet := UTXOSet{bc} defer bc.db.Close() diff --git a/server.go b/server.go index ba0906d..37a87ac 100644 --- a/server.go +++ b/server.go @@ -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) @@ -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) } }