Blockchain file name must depend on node ID
This commit is contained in:
parent
504b6c85bf
commit
57f3680551
|
@ -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
|
||||
}
|
||||
|
|
17
cli.go
17
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() {
|
||||
|
|
|
@ -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}
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue