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"
|
"github.com/boltdb/bolt"
|
||||||
)
|
)
|
||||||
|
|
||||||
const dbFile = "blockchain.db"
|
const dbFile = "blockchain_%s.db"
|
||||||
const blocksBucket = "blocks"
|
const blocksBucket = "blocks"
|
||||||
const genesisCoinbaseData = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks"
|
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
|
// CreateBlockchain creates a new blockchain DB
|
||||||
func CreateBlockchain(address string) *Blockchain {
|
func CreateBlockchain(address, nodeID string) *Blockchain {
|
||||||
if dbExists() {
|
dbFile := fmt.Sprintf(dbFile, nodeID)
|
||||||
|
if dbExists(dbFile) {
|
||||||
fmt.Println("Blockchain already exists.")
|
fmt.Println("Blockchain already exists.")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
@ -68,8 +69,9 @@ func CreateBlockchain(address string) *Blockchain {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBlockchain creates a new Blockchain with genesis Block
|
// NewBlockchain creates a new Blockchain with genesis Block
|
||||||
func NewBlockchain() *Blockchain {
|
func NewBlockchain(nodeID string) *Blockchain {
|
||||||
if dbExists() == false {
|
dbFile := fmt.Sprintf(dbFile, nodeID)
|
||||||
|
if dbExists(dbFile) == false {
|
||||||
fmt.Println("No existing blockchain found. Create one first.")
|
fmt.Println("No existing blockchain found. Create one first.")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
@ -246,7 +248,7 @@ func (bc *Blockchain) VerifyTransaction(tx *Transaction) bool {
|
||||||
return tx.Verify(prevTXs)
|
return tx.Verify(prevTXs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbExists() bool {
|
func dbExists(dbFile string) bool {
|
||||||
if _, err := os.Stat(dbFile); os.IsNotExist(err) {
|
if _, err := os.Stat(dbFile); os.IsNotExist(err) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
17
cli.go
17
cli.go
|
@ -4,6 +4,7 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -33,6 +34,12 @@ func (cli *CLI) validateArgs() {
|
||||||
func (cli *CLI) Run() {
|
func (cli *CLI) Run() {
|
||||||
cli.validateArgs()
|
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)
|
getBalanceCmd := flag.NewFlagSet("getbalance", flag.ExitOnError)
|
||||||
createBlockchainCmd := flag.NewFlagSet("createblockchain", flag.ExitOnError)
|
createBlockchainCmd := flag.NewFlagSet("createblockchain", flag.ExitOnError)
|
||||||
createWalletCmd := flag.NewFlagSet("createwallet", flag.ExitOnError)
|
createWalletCmd := flag.NewFlagSet("createwallet", flag.ExitOnError)
|
||||||
|
@ -99,7 +106,7 @@ func (cli *CLI) Run() {
|
||||||
getBalanceCmd.Usage()
|
getBalanceCmd.Usage()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
cli.getBalance(*getBalanceAddress)
|
cli.getBalance(*getBalanceAddress, nodeID)
|
||||||
}
|
}
|
||||||
|
|
||||||
if createBlockchainCmd.Parsed() {
|
if createBlockchainCmd.Parsed() {
|
||||||
|
@ -107,7 +114,7 @@ func (cli *CLI) Run() {
|
||||||
createBlockchainCmd.Usage()
|
createBlockchainCmd.Usage()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
cli.createBlockchain(*createBlockchainAddress)
|
cli.createBlockchain(*createBlockchainAddress, nodeID)
|
||||||
}
|
}
|
||||||
|
|
||||||
if createWalletCmd.Parsed() {
|
if createWalletCmd.Parsed() {
|
||||||
|
@ -119,11 +126,11 @@ func (cli *CLI) Run() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if printChainCmd.Parsed() {
|
if printChainCmd.Parsed() {
|
||||||
cli.printChain()
|
cli.printChain(nodeID)
|
||||||
}
|
}
|
||||||
|
|
||||||
if reindexUTXOCmd.Parsed() {
|
if reindexUTXOCmd.Parsed() {
|
||||||
cli.reindexUTXO()
|
cli.reindexUTXO(nodeID)
|
||||||
}
|
}
|
||||||
|
|
||||||
if sendCmd.Parsed() {
|
if sendCmd.Parsed() {
|
||||||
|
@ -132,7 +139,7 @@ func (cli *CLI) Run() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
cli.send(*sendFrom, *sendTo, *sendAmount)
|
cli.send(*sendFrom, *sendTo, *sendAmount, nodeID)
|
||||||
}
|
}
|
||||||
|
|
||||||
if startNodeCmd.Parsed() {
|
if startNodeCmd.Parsed() {
|
||||||
|
|
|
@ -5,11 +5,11 @@ import (
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (cli *CLI) createBlockchain(address string) {
|
func (cli *CLI) createBlockchain(address, nodeID string) {
|
||||||
if !ValidateAddress(address) {
|
if !ValidateAddress(address) {
|
||||||
log.Panic("ERROR: Address is not valid")
|
log.Panic("ERROR: Address is not valid")
|
||||||
}
|
}
|
||||||
bc := CreateBlockchain(address)
|
bc := CreateBlockchain(address, nodeID)
|
||||||
defer bc.db.Close()
|
defer bc.db.Close()
|
||||||
|
|
||||||
UTXOSet := UTXOSet{bc}
|
UTXOSet := UTXOSet{bc}
|
||||||
|
|
|
@ -5,11 +5,11 @@ import (
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (cli *CLI) getBalance(address string) {
|
func (cli *CLI) getBalance(address, nodeID string) {
|
||||||
if !ValidateAddress(address) {
|
if !ValidateAddress(address) {
|
||||||
log.Panic("ERROR: Address is not valid")
|
log.Panic("ERROR: Address is not valid")
|
||||||
}
|
}
|
||||||
bc := NewBlockchain()
|
bc := NewBlockchain(nodeID)
|
||||||
UTXOSet := UTXOSet{bc}
|
UTXOSet := UTXOSet{bc}
|
||||||
defer bc.db.Close()
|
defer bc.db.Close()
|
||||||
|
|
||||||
|
|
|
@ -5,8 +5,8 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (cli *CLI) printChain() {
|
func (cli *CLI) printChain(nodeID string) {
|
||||||
bc := NewBlockchain()
|
bc := NewBlockchain(nodeID)
|
||||||
defer bc.db.Close()
|
defer bc.db.Close()
|
||||||
|
|
||||||
bci := bc.Iterator()
|
bci := bc.Iterator()
|
||||||
|
|
|
@ -2,8 +2,8 @@ package main
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
func (cli *CLI) reindexUTXO() {
|
func (cli *CLI) reindexUTXO(nodeID string) {
|
||||||
bc := NewBlockchain()
|
bc := NewBlockchain(nodeID)
|
||||||
UTXOSet := UTXOSet{bc}
|
UTXOSet := UTXOSet{bc}
|
||||||
UTXOSet.Reindex()
|
UTXOSet.Reindex()
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (cli *CLI) send(from, to string, amount int) {
|
func (cli *CLI) send(from, to string, amount int, nodeID string) {
|
||||||
if !ValidateAddress(from) {
|
if !ValidateAddress(from) {
|
||||||
log.Panic("ERROR: Sender address is not valid")
|
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")
|
log.Panic("ERROR: Recipient address is not valid")
|
||||||
}
|
}
|
||||||
|
|
||||||
bc := NewBlockchain()
|
bc := NewBlockchain(nodeID)
|
||||||
UTXOSet := UTXOSet{bc}
|
UTXOSet := UTXOSet{bc}
|
||||||
defer bc.db.Close()
|
defer bc.db.Close()
|
||||||
|
|
||||||
|
|
|
@ -126,7 +126,7 @@ func handleVersion(request []byte) {
|
||||||
knownNodes = append(knownNodes, payload.AddrFrom)
|
knownNodes = append(knownNodes, payload.AddrFrom)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleConnection(conn net.Conn) {
|
func handleConnection(conn net.Conn, bc *Blockchain) {
|
||||||
request, err := ioutil.ReadAll(conn)
|
request, err := ioutil.ReadAll(conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
|
@ -161,12 +161,14 @@ func StartServer(nodeID string) {
|
||||||
sendVersion(fmt.Sprintf("localhost:%s", dnsNodeID))
|
sendVersion(fmt.Sprintf("localhost:%s", dnsNodeID))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bc := NewBlockchain(nodeID)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
conn, err := ln.Accept()
|
conn, err := ln.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
go handleConnection(conn)
|
go handleConnection(conn, bc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue