From 73f51628534f5717116c054940eae997160f3bac Mon Sep 17 00:00:00 2001
From: Ethan Buchman <ethan@coinculture.info>
Date: Wed, 11 May 2016 14:28:48 -0400
Subject: [PATCH] no global config

---
 erisdb/config.go          | 13 ------------
 erisdb/pipe/blockchain.go | 15 +++++++-------
 erisdb/pipe/config.go     |  8 --------
 erisdb/pipe/pipe.go       | 14 ++++++++-----
 erisdb/pipe/transactor.go | 22 ++++++++++----------
 erisdb/serve.go           | 42 +++++++++++++++++++--------------------
 rpc/core/config.go        | 13 ------------
 rpc/core/pipe.go          |  7 +++++++
 rpc/test/config.go        | 13 ------------
 rpc/test/helpers.go       | 24 ++++++++++++----------
 txs/config.go             | 13 ------------
 11 files changed, 69 insertions(+), 115 deletions(-)
 delete mode 100644 erisdb/config.go
 delete mode 100644 rpc/core/config.go
 delete mode 100644 rpc/test/config.go
 delete mode 100644 txs/config.go

diff --git a/erisdb/config.go b/erisdb/config.go
deleted file mode 100644
index 7873d89d..00000000
--- a/erisdb/config.go
+++ /dev/null
@@ -1,13 +0,0 @@
-package erisdb
-
-import (
-	cfg "github.com/tendermint/go-config"
-)
-
-var config cfg.Config
-
-func init() {
-	cfg.OnConfig(func(newConfig cfg.Config) {
-		config = newConfig
-	})
-}
diff --git a/erisdb/pipe/blockchain.go b/erisdb/pipe/blockchain.go
index 4787cbb7..9a4d05a5 100644
--- a/erisdb/pipe/blockchain.go
+++ b/erisdb/pipe/blockchain.go
@@ -22,11 +22,13 @@ type BlockStore interface {
 
 // The blockchain struct.
 type blockchain struct {
+	chainID       string
+	genDocFile    string // XXX
 	blockStore    BlockStore
 	filterFactory *FilterFactory
 }
 
-func newBlockchain(blockStore BlockStore) *blockchain {
+func newBlockchain(chainID, genDocFile string, blockStore BlockStore) *blockchain {
 	ff := NewFilterFactory()
 
 	ff.RegisterFilterPool("height", &sync.Pool{
@@ -35,15 +37,14 @@ func newBlockchain(blockStore BlockStore) *blockchain {
 		},
 	})
 
-	return &blockchain{blockStore, ff}
+	return &blockchain{chainID, genDocFile, blockStore, ff}
 
 }
 
 // Get the status.
 func (this *blockchain) Info() (*BlockchainInfo, error) {
-	chainId := config.GetString("erisdb.chain_id")
 	db := dbm.NewMemDB()
-	_, genesisState := state.MakeGenesisStateFromFile(db, config.GetString("erisdb.genesis_file"))
+	_, genesisState := state.MakeGenesisStateFromFile(db, this.genDocFile)
 	genesisHash := genesisState.Hash()
 	latestHeight := this.blockStore.Height()
 
@@ -54,7 +55,7 @@ func (this *blockchain) Info() (*BlockchainInfo, error) {
 	}
 
 	return &BlockchainInfo{
-		chainId,
+		this.chainID,
 		genesisHash,
 		latestHeight,
 		latestBlockMeta,
@@ -63,13 +64,13 @@ func (this *blockchain) Info() (*BlockchainInfo, error) {
 
 // Get the chain id.
 func (this *blockchain) ChainId() (string, error) {
-	return config.GetString("erisdb.chain_id"), nil
+	return this.chainID, nil
 }
 
 // Get the hash of the genesis block.
 func (this *blockchain) GenesisHash() ([]byte, error) {
 	db := dbm.NewMemDB()
-	_, genesisState := state.MakeGenesisStateFromFile(db, config.GetString("erisdb.genesis_file"))
+	_, genesisState := state.MakeGenesisStateFromFile(db, this.genDocFile)
 	return genesisState.Hash(), nil
 }
 
diff --git a/erisdb/pipe/config.go b/erisdb/pipe/config.go
index 43f8f604..7522d32f 100644
--- a/erisdb/pipe/config.go
+++ b/erisdb/pipe/config.go
@@ -1,15 +1,7 @@
 package pipe
 
 import (
-	cfg "github.com/tendermint/go-config"
 	"github.com/tendermint/log15"
 )
 
 var log = log15.New("module", "eris/erisdb_pipe")
-var config cfg.Config
-
-func init() {
-	cfg.OnConfig(func(newConfig cfg.Config) {
-		config = newConfig
-	})
-}
diff --git a/erisdb/pipe/pipe.go b/erisdb/pipe/pipe.go
index 3088b34d..b13380e6 100644
--- a/erisdb/pipe/pipe.go
+++ b/erisdb/pipe/pipe.go
@@ -83,6 +83,8 @@ type (
 
 // Base struct for getting rpc proxy objects (node.Node has no interface).
 type PipeImpl struct {
+	chainID    string
+	genDocFile string
 	//tNode      *node.Node
 	erisdbApp  *tmsp.ErisDBApp
 	accounts   Accounts
@@ -108,21 +110,23 @@ type PipeImpl struct {
 // 	net := newNetwork(tNode.Switch())
 // 	txs := newTransactor(tNode.EventSwitch(), tNode.ConsensusState(), tNode.MempoolReactor(), events)
 // =======
-func NewPipe(erisdbApp *tmsp.ErisDBApp, eventSwitch *em.EventSwitch) Pipe {
+func NewPipe(chainID, genDocFile string, erisdbApp *tmsp.ErisDBApp, eventSwitch *em.EventSwitch) Pipe {
 	events := newEvents(eventSwitch)
 
 	accounts := newAccounts(erisdbApp)
 	namereg := newNamereg(erisdbApp)
-	txs := newTransactor(eventSwitch, erisdbApp, events)
+	txs := newTransactor(chainID, eventSwitch, erisdbApp, events)
 
 	// TODO: make interface to tendermint core's rpc for these
-	// blockchain := newBlockchain(blockStore)
+	// blockchain := newBlockchain(chainID, genDocFile, blockStore)
 	// consensus := newConsensus(erisdbApp)
 	// net := newNetwork(erisdbApp)
 
 	return &PipeImpl{
-		erisdbApp: erisdbApp,
-		accounts:  accounts,
+		chainID:    chainID,
+		genDocFile: genDocFile,
+		erisdbApp:  erisdbApp,
+		accounts:   accounts,
 		// blockchain,
 		// consensus,
 		events:  events,
diff --git a/erisdb/pipe/transactor.go b/erisdb/pipe/transactor.go
index 6f9bc29f..8ae73ef4 100644
--- a/erisdb/pipe/transactor.go
+++ b/erisdb/pipe/transactor.go
@@ -20,14 +20,16 @@ import (
 )
 
 type transactor struct {
+	chainID      string
 	eventSwitch  tEvents.Fireable
 	erisdbApp    *tmsp.ErisDBApp
 	eventEmitter EventEmitter
 	txMtx        *sync.Mutex
 }
 
-func newTransactor(eventSwitch tEvents.Fireable, erisdbApp *tmsp.ErisDBApp, eventEmitter EventEmitter) *transactor {
+func newTransactor(chainID string, eventSwitch tEvents.Fireable, erisdbApp *tmsp.ErisDBApp, eventEmitter EventEmitter) *transactor {
 	txs := &transactor{
+		chainID,
 		eventSwitch,
 		erisdbApp,
 		eventEmitter,
@@ -104,8 +106,7 @@ func (this *transactor) BroadcastTx(tx txs.Tx) (*Receipt, error) {
 		return nil, fmt.Errorf("Error broadcasting transaction: %v", err)
 	}
 
-	chainId := config.GetString("erisdb.chain_id")
-	txHash := txs.TxID(chainId, tx)
+	txHash := txs.TxID(this.chainID, tx)
 	var createsContract uint8
 	var contractAddr []byte
 	// check if creates new contract
@@ -246,41 +247,40 @@ func (this *transactor) SignTx(tx txs.Tx, privAccounts []*account.PrivAccount) (
 			return nil, fmt.Errorf("Invalid (empty) privAccount @%v", i)
 		}
 	}
-	chainId := config.GetString("erisdb.chain_id")
 	switch tx.(type) {
 	case *txs.NameTx:
 		nameTx := tx.(*txs.NameTx)
 		nameTx.Input.PubKey = privAccounts[0].PubKey
-		nameTx.Input.Signature = privAccounts[0].Sign(config.GetString("erisdb.chain_id"), nameTx)
+		nameTx.Input.Signature = privAccounts[0].Sign(this.chainID, nameTx)
 	case *txs.SendTx:
 		sendTx := tx.(*txs.SendTx)
 		for i, input := range sendTx.Inputs {
 			input.PubKey = privAccounts[i].PubKey
-			input.Signature = privAccounts[i].Sign(chainId, sendTx)
+			input.Signature = privAccounts[i].Sign(this.chainID, sendTx)
 		}
 		break
 	case *txs.CallTx:
 		callTx := tx.(*txs.CallTx)
 		callTx.Input.PubKey = privAccounts[0].PubKey
-		callTx.Input.Signature = privAccounts[0].Sign(chainId, callTx)
+		callTx.Input.Signature = privAccounts[0].Sign(this.chainID, callTx)
 		break
 	case *txs.BondTx:
 		bondTx := tx.(*txs.BondTx)
 		// the first privaccount corresponds to the BondTx pub key.
 		// the rest to the inputs
-		bondTx.Signature = privAccounts[0].Sign(chainId, bondTx).(crypto.SignatureEd25519)
+		bondTx.Signature = privAccounts[0].Sign(this.chainID, bondTx).(crypto.SignatureEd25519)
 		for i, input := range bondTx.Inputs {
 			input.PubKey = privAccounts[i+1].PubKey
-			input.Signature = privAccounts[i+1].Sign(chainId, bondTx)
+			input.Signature = privAccounts[i+1].Sign(this.chainID, bondTx)
 		}
 		break
 	case *txs.UnbondTx:
 		unbondTx := tx.(*txs.UnbondTx)
-		unbondTx.Signature = privAccounts[0].Sign(chainId, unbondTx).(crypto.SignatureEd25519)
+		unbondTx.Signature = privAccounts[0].Sign(this.chainID, unbondTx).(crypto.SignatureEd25519)
 		break
 	case *txs.RebondTx:
 		rebondTx := tx.(*txs.RebondTx)
-		rebondTx.Signature = privAccounts[0].Sign(chainId, rebondTx).(crypto.SignatureEd25519)
+		rebondTx.Signature = privAccounts[0].Sign(this.chainID, rebondTx).(crypto.SignatureEd25519)
 		break
 	default:
 		return nil, fmt.Errorf("Object is not a proper transaction: %v\n", tx)
diff --git a/erisdb/serve.go b/erisdb/serve.go
index 254f2515..2ba25813 100644
--- a/erisdb/serve.go
+++ b/erisdb/serve.go
@@ -88,13 +88,12 @@ func ServeErisDB(workDir string, inProc bool) (*server.ServeProcess, error) {
 	tmConfig = tmcfg.GetConfig(workDir)
 
 	// tmConfig.Set("tm.version", TENDERMINT_VERSION) // ?
-	cfg.ApplyConfig(tmConfig) // Notify in proc tendermint of new config
 
 	// Load the application state
 	// The app state used to be managed by tendermint node,
 	// but is now managed by ErisDB.
 	// The tendermint core only stores the blockchain (history of txs)
-	stateDB := dbm.GetDB("app_state", edbConf.DB.Backend, workDir+"/data")
+	stateDB := dbm.NewDB("app_state", edbConf.DB.Backend, workDir+"/data")
 	state := sm.LoadState(stateDB)
 	var genDoc *stypes.GenesisDoc
 	if state == nil {
@@ -115,7 +114,6 @@ func ServeErisDB(workDir string, inProc bool) (*server.ServeProcess, error) {
 		}
 	}
 	// add the chainid
-	config.Set("erisdb.chain_id", state.ChainID)
 
 	// *****************************
 	// erisdb-tmsp app
@@ -133,7 +131,8 @@ func ServeErisDB(workDir string, inProc bool) (*server.ServeProcess, error) {
 	if inProc {
 		fmt.Println("Starting tm node in proc")
 		// will also start the go-rpc server (46657 api)
-		startTMNode(app, workDir)
+
+		startTMNode(tmConfig, app, workDir)
 	} else {
 		fmt.Println("Starting tmsp listener")
 		// Start the tmsp listener for state update commands
@@ -150,8 +149,9 @@ func ServeErisDB(workDir string, inProc bool) (*server.ServeProcess, error) {
 	// *****************************
 	// Boot the erisdb restful API servers
 
+	genDocFile := tmConfig.GetString("genesis_file") // XXX
 	// Load supporting objects.
-	pipe := ep.NewPipe(app, evsw)
+	pipe := ep.NewPipe(state.ChainID, genDocFile, app, evsw)
 	codec := &TCodec{}
 	evtSubs := NewEventSubscriptions(pipe.Events())
 	// The services.
@@ -168,9 +168,9 @@ func ServeErisDB(workDir string, inProc bool) (*server.ServeProcess, error) {
 }
 
 // start an inproc tendermint node
-func startTMNode(app *edbapp.ErisDBApp, workDir string) {
+func startTMNode(config cfg.Config, app *edbapp.ErisDBApp, workDir string) {
 	// get the genesis
-	genDocFile := config.GetString("tm.genesis_file")
+	genDocFile := config.GetString("genesis_file")
 	jsonBlob, err := ioutil.ReadFile(genDocFile)
 	if err != nil {
 		Exit(Fmt("Couldn't read GenesisDoc file: %v", err))
@@ -179,18 +179,17 @@ func startTMNode(app *edbapp.ErisDBApp, workDir string) {
 	if genDoc.ChainID == "" {
 		PanicSanity(Fmt("Genesis doc %v must include non-empty chain_id", genDocFile))
 	}
-	config.Set("tm.chain_id", genDoc.ChainID)
-	config.Set("tm.genesis_doc", genDoc)
+	config.Set("chain_id", genDoc.ChainID)
 
 	// Get PrivValidator
-	privValidatorFile := config.GetString("tm.priv_validator_file")
+	privValidatorFile := config.GetString("priv_validator_file")
 	privValidator := types.LoadOrGenPrivValidator(privValidatorFile)
-	nd := node.NewNode(privValidator, func(addr string, hash []byte) proxy.AppConn {
+	nd := node.NewNode(config, privValidator, func(addr string, hash []byte) proxy.AppConn {
 		// TODO: Check the hash
 		return tmspcli.NewLocalClient(new(sync.Mutex), app)
 	})
 
-	l := p2p.NewDefaultListener("tcp", config.GetString("tm.node_laddr"), config.GetBool("tm.skip_upnp"))
+	l := p2p.NewDefaultListener("tcp", config.GetString("node_laddr"), config.GetBool("skip_upnp"))
 	nd.AddListener(l)
 	if err := nd.Start(); err != nil {
 		Exit(Fmt("Failed to start node: %v", err))
@@ -199,21 +198,23 @@ func startTMNode(app *edbapp.ErisDBApp, workDir string) {
 	log.Notice("Started node", "nodeInfo", nd.NodeInfo())
 
 	// If seedNode is provided by config, dial out.
-	if config.GetString("tm.seeds") != "" {
-		seeds := strings.Split(config.GetString("tm.seeds"), ",")
+	if config.GetString("seeds") != "" {
+		seeds := strings.Split(config.GetString("seeds"), ",")
 		nd.DialSeeds(seeds)
 	}
 
 	// Run the RPC server.
-	if config.GetString("tm.rpc_laddr") != "" {
-		_, err := StartRPC(nd, app)
+	if config.GetString("rpc_laddr") != "" {
+		_, err := StartRPC(config, nd, app)
 		if err != nil {
 			PanicCrisis(err)
 		}
 	}
 }
 
-func StartRPC(n *node.Node, edbApp *edbapp.ErisDBApp) ([]net.Listener, error) {
+func StartRPC(config cfg.Config, n *node.Node, edbApp *edbapp.ErisDBApp) ([]net.Listener, error) {
+	rpccore.SetConfig(config)
+
 	rpccore.SetErisDBApp(edbApp)
 	rpccore.SetBlockStore(n.BlockStore())
 	rpccore.SetConsensusState(n.ConsensusState())
@@ -221,9 +222,9 @@ func StartRPC(n *node.Node, edbApp *edbapp.ErisDBApp) ([]net.Listener, error) {
 	rpccore.SetMempoolReactor(n.MempoolReactor())
 	rpccore.SetSwitch(n.Switch())
 	rpccore.SetPrivValidator(n.PrivValidator())
-	rpccore.SetGenDoc(LoadGenDoc())
+	rpccore.SetGenDoc(LoadGenDoc(config.GetString("genesis_file")))
 
-	listenAddrs := strings.Split(config.GetString("tm.rpc_laddr"), ",")
+	listenAddrs := strings.Split(config.GetString("rpc_laddr"), ",")
 
 	// we may expose the rpc over both a unix and tcp socket
 	listeners := make([]net.Listener, len(listenAddrs))
@@ -241,8 +242,7 @@ func StartRPC(n *node.Node, edbApp *edbapp.ErisDBApp) ([]net.Listener, error) {
 	return listeners, nil
 }
 
-func LoadGenDoc() *stypes.GenesisDoc {
-	genDocFile := config.GetString("tm.genesis_file")
+func LoadGenDoc(genDocFile string) *stypes.GenesisDoc {
 	jsonBlob, err := ioutil.ReadFile(genDocFile)
 	if err != nil {
 		Exit(Fmt("Couldn't read GenesisDoc file: %v", err))
diff --git a/rpc/core/config.go b/rpc/core/config.go
deleted file mode 100644
index 7f51b666..00000000
--- a/rpc/core/config.go
+++ /dev/null
@@ -1,13 +0,0 @@
-package core
-
-import (
-	cfg "github.com/tendermint/go-config"
-)
-
-var config cfg.Config = nil
-
-func init() {
-	cfg.OnConfig(func(newConfig cfg.Config) {
-		config = newConfig
-	})
-}
diff --git a/rpc/core/pipe.go b/rpc/core/pipe.go
index c7f3fa4a..23860a03 100644
--- a/rpc/core/pipe.go
+++ b/rpc/core/pipe.go
@@ -9,6 +9,7 @@ import (
 	mempl "github.com/tendermint/tendermint/mempool"
 	tmtypes "github.com/tendermint/tendermint/types"
 
+	cfg "github.com/tendermint/go-config"
 	"github.com/tendermint/go-p2p"
 )
 
@@ -21,6 +22,12 @@ var privValidator *tmtypes.PrivValidator
 var genDoc *stypes.GenesisDoc // cache the genesis structure
 var erisdbApp *tmsp.ErisDBApp
 
+var config cfg.Config = nil
+
+func SetConfig(c cfg.Config) {
+	config = c
+}
+
 func SetErisDBApp(edbApp *tmsp.ErisDBApp) {
 	erisdbApp = edbApp
 }
diff --git a/rpc/test/config.go b/rpc/test/config.go
deleted file mode 100644
index 2fe20981..00000000
--- a/rpc/test/config.go
+++ /dev/null
@@ -1,13 +0,0 @@
-package rpctest
-
-import (
-	cfg "github.com/tendermint/go-config"
-)
-
-var config cfg.Config = nil
-
-func init() {
-	cfg.OnConfig(func(newConfig cfg.Config) {
-		config = newConfig
-	})
-}
diff --git a/rpc/test/helpers.go b/rpc/test/helpers.go
index 91b9154d..98affbda 100644
--- a/rpc/test/helpers.go
+++ b/rpc/test/helpers.go
@@ -15,6 +15,7 @@ import (
 	txs "github.com/eris-ltd/eris-db/txs"
 
 	. "github.com/tendermint/go-common"
+	cfg "github.com/tendermint/go-config"
 	"github.com/tendermint/go-crypto"
 	dbm "github.com/tendermint/go-db"
 	"github.com/tendermint/go-events"
@@ -29,6 +30,7 @@ import (
 
 // global variables for use across all tests
 var (
+	config            cfg.Config
 	node              *nm.Node
 	mempoolCount      = 0
 	chainID           string
@@ -44,7 +46,6 @@ var (
 )
 
 func init() {
-	tendermint_test.ResetConfig("rpc_test_client_test")
 	initGlobalVariables()
 
 	saveNewPriv()
@@ -52,8 +53,9 @@ func init() {
 
 // initialize config and create new node
 func initGlobalVariables() {
-	chainID = config.GetString("tm.chain_id")
-	rpcAddr = config.GetString("tm.rpc_laddr")
+	config = tendermint_test.ResetConfig("rpc_test_client_test")
+	chainID = config.GetString("chain_id")
+	rpcAddr = config.GetString("rpc_laddr")
 	config.Set("erisdb.chain_id", chainID)
 	requestAddr = rpcAddr
 	websocketAddr = rpcAddr
@@ -68,7 +70,7 @@ func initGlobalVariables() {
 	}
 
 	// write the genesis
-	MustWriteFile(config.GetString("tm.genesis_file"), []byte(defaultGenesis), 0600)
+	MustWriteFile(config.GetString("genesis_file"), []byte(defaultGenesis), 0600)
 
 	// TODO: change consensus/state.go timeouts to be shorter
 
@@ -91,8 +93,8 @@ func makeUsers(n int) []*acm.PrivAccount {
 
 // create a new node and sleep forever
 func newNode(ready chan struct{}) {
-	stateDB := dbm.GetDB("app_state", "memdb", "")
-	genDoc, state := sm.MakeGenesisStateFromFile(stateDB, config.GetString("tm.genesis_file"))
+	stateDB := dbm.NewDB("app_state", "memdb", "")
+	genDoc, state := sm.MakeGenesisStateFromFile(stateDB, config.GetString("genesis_file"))
 	state.Save()
 	buf, n, err := new(bytes.Buffer), new(int), new(error)
 	wire.WriteJSON(genDoc, buf, n, err)
@@ -106,15 +108,15 @@ func newNode(ready chan struct{}) {
 	app := edbapp.NewErisDBApp(state, evsw)
 
 	// Create & start node
-	privValidatorFile := config.GetString("tm.priv_validator_file")
+	privValidatorFile := config.GetString("priv_validator_file")
 	privValidator := types.LoadOrGenPrivValidator(privValidatorFile)
-	node = nm.NewNode(privValidator, nm.GetProxyApp)
-	l := p2p.NewDefaultListener("tcp", config.GetString("tm.node_laddr"), true)
+	node = nm.NewNode(config, privValidator, nm.GetProxyApp)
+	l := p2p.NewDefaultListener("tcp", config.GetString("node_laddr"), true)
 	node.AddListener(l)
 	node.Start()
 
 	// Run the RPC server.
-	edb.StartRPC(node, app)
+	edb.StartRPC(config, node, app)
 	ready <- struct{}{}
 
 	// Sleep forever
@@ -129,7 +131,7 @@ func saveNewPriv() {
 		PubKey:  crypto.PubKeyEd25519(user[0].PubKey.(crypto.PubKeyEd25519)),
 		PrivKey: crypto.PrivKeyEd25519(user[0].PrivKey.(crypto.PrivKeyEd25519)),
 	}
-	priv.SetFile(config.GetString("tm.priv_validator_file"))
+	priv.SetFile(config.GetString("priv_validator_file"))
 	priv.Save()
 }
 
diff --git a/txs/config.go b/txs/config.go
deleted file mode 100644
index aa534896..00000000
--- a/txs/config.go
+++ /dev/null
@@ -1,13 +0,0 @@
-package txs
-
-import (
-	cfg "github.com/tendermint/go-config"
-)
-
-var config cfg.Config = nil
-
-func init() {
-	cfg.OnConfig(func(newConfig cfg.Config) {
-		config = newConfig
-	})
-}
-- 
GitLab