diff --git a/cmd/serve.go b/cmd/serve.go index b355d884dcd2e8835159815008bf467d80c9756f..7341d340952ab56b5680f7bedba5c9bcc89753ec 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -97,7 +97,7 @@ func Serve(cmd *cobra.Command, args []string) { log.Fatalf("Failed to read non-empty string for genesis file from config.") os.Exit(1) } - // Ensure data directory is set and accesible + // Ensure data directory is set and accessible if err := do.InitialiseDataDirectory(); err != nil { log.Fatalf("Failed to initialise data directory (%s): %v", do.DataDir, err) os.Exit(1) diff --git a/core/config.go b/core/config.go index f280dca92f01b6221fff216f1a03c2646ae143d2..941b5b54d636d373092073e549385b8c30a49b99 100644 --- a/core/config.go +++ b/core/config.go @@ -20,92 +20,99 @@ package core import ( - "fmt" - "os" - "path" + "fmt" + "os" + "path" - consensus "github.com/eris-ltd/eris-db/consensus" - config "github.com/eris-ltd/eris-db/config" - definitions "github.com/eris-ltd/eris-db/definitions" - manager "github.com/eris-ltd/eris-db/manager" - server "github.com/eris-ltd/eris-db/server" - util "github.com/eris-ltd/eris-db/util" - version "github.com/eris-ltd/eris-db/version" + config "github.com/eris-ltd/eris-db/config" + consensus "github.com/eris-ltd/eris-db/consensus" + definitions "github.com/eris-ltd/eris-db/definitions" + manager "github.com/eris-ltd/eris-db/manager" + server "github.com/eris-ltd/eris-db/server" + util "github.com/eris-ltd/eris-db/util" + version "github.com/eris-ltd/eris-db/version" + "github.com/spf13/viper" ) // LoadConsensusModuleConfig wraps specifically for the consensus module func LoadConsensusModuleConfig(do *definitions.Do) (*config.ModuleConfig, error) { - return loadModuleConfig(do, "consensus") + return loadModuleConfigFromDo(do, "consensus") } // LoadApplicationManagerModuleConfig wraps specifically for the application // manager func LoadApplicationManagerModuleConfig(do *definitions.Do) (*config.ModuleConfig, error) { - return loadModuleConfig(do, "manager") + return loadModuleConfigFromDo(do, "manager") +} + +func loadModuleConfigFromDo(do *definitions.Do, module string) (*config.ModuleConfig, error) { + return LoadModuleConfig(do.Config, do.WorkDir, do.DataDir, + do.GenesisFile, do.ChainId, module) } // Generic Module loader for configuration information -func loadModuleConfig(do *definitions.Do, module string) (*config.ModuleConfig, error) { - moduleName := do.Config.GetString("chain." + module + ".name") - majorVersion := do.Config.GetInt("chain." + module + ".major_version") - minorVersion := do.Config.GetInt("chain." + module + ".minor_version") - minorVersionString := version.MakeMinorVersionString(moduleName, majorVersion, - minorVersion, 0) - if !assertValidModule(module, moduleName, minorVersionString) { - return nil, fmt.Errorf("%s module %s (%s) is not supported by %s", - module, moduleName, minorVersionString, version.GetVersionString()) - } - // set up the directory structure for the module inside the data directory - workDir := path.Join(do.DataDir, do.Config.GetString("chain." + module + - ".relative_root")) - if err := util.EnsureDir(workDir, os.ModePerm); err != nil { - return nil, - fmt.Errorf("Failed to create module root directory %s.", workDir) - } - dataDir := path.Join(workDir, "data") - if err := util.EnsureDir(dataDir, os.ModePerm); err != nil { - return nil, - fmt.Errorf("Failed to create module data directory %s.", dataDir) - } - // load configuration subtree for module - // TODO: [ben] Viper internally panics if `moduleName` contains an unallowed - // character (eg, a dash). Either this needs to be wrapped in a go-routine - // and recovered from or a PR to viper is needed to address this bug. - if !do.Config.IsSet(moduleName) { +func LoadModuleConfig(conf *viper.Viper, rootWorkDir, rootDataDir, + genesisFile, chainId, module string) (*config.ModuleConfig, error) { + moduleName := conf.GetString("chain." + module + ".name") + majorVersion := conf.GetInt("chain." + module + ".major_version") + minorVersion := conf.GetInt("chain." + module + ".minor_version") + minorVersionString := version.MakeMinorVersionString(moduleName, majorVersion, + minorVersion, 0) + if !assertValidModule(module, moduleName, minorVersionString) { + return nil, fmt.Errorf("%s module %s (%s) is not supported by %s", + module, moduleName, minorVersionString, version.GetVersionString()) + } + // set up the directory structure for the module inside the data directory + workDir := path.Join(rootDataDir, conf.GetString("chain."+module+ + ".relative_root")) + if err := util.EnsureDir(workDir, os.ModePerm); err != nil { + return nil, + fmt.Errorf("Failed to create module root directory %s.", workDir) + } + dataDir := path.Join(workDir, "data") + if err := util.EnsureDir(dataDir, os.ModePerm); err != nil { + return nil, + fmt.Errorf("Failed to create module data directory %s.", dataDir) + } + // load configuration subtree for module + // TODO: [ben] Viper internally panics if `moduleName` contains an unallowed + // character (eg, a dash). Either this needs to be wrapped in a go-routine + // and recovered from or a PR to viper is needed to address this bug. + if !conf.IsSet(moduleName) { return nil, fmt.Errorf("Failed to read configuration section for %s", moduleName) } - subConfig := do.Config.Sub(moduleName) - if subConfig == nil { - return nil, - fmt.Errorf("Failed to read configuration section for %s.", moduleName) - } + subConfig := conf.Sub(moduleName) + if subConfig == nil { + return nil, + fmt.Errorf("Failed to read configuration section for %s.", moduleName) + } - return &config.ModuleConfig { - Module : module, - Name : moduleName, - Version : minorVersionString, - WorkDir : workDir, - DataDir : dataDir, - RootDir : do.WorkDir, // Eris-DB's working directory - ChainId : do.ChainId, - GenesisFile : do.GenesisFile, - Config : subConfig, - }, nil + return &config.ModuleConfig{ + Module: module, + Name: moduleName, + Version: minorVersionString, + WorkDir: workDir, + DataDir: dataDir, + RootDir: rootWorkDir, // Eris-DB's working directory + ChainId: chainId, + GenesisFile: genesisFile, + Config: subConfig, + }, nil } // LoadServerModuleConfig wraps specifically for the servers run by core func LoadServerConfig(do *definitions.Do) (*server.ServerConfig, error) { - // load configuration subtree for servers + // load configuration subtree for servers if !do.Config.IsSet("servers") { return nil, fmt.Errorf("Failed to read configuration section for servers") } subConfig := do.Config.Sub("servers") - if subConfig == nil { - return nil, - fmt.Errorf("Failed to read configuration section for servers") - } - serverConfig, err := server.ReadServerConfig(subConfig) + if subConfig == nil { + return nil, + fmt.Errorf("Failed to read configuration section for servers") + } + serverConfig, err := server.ReadServerConfig(subConfig) serverConfig.ChainId = do.ChainId return serverConfig, err } @@ -115,11 +122,11 @@ func LoadServerConfig(do *definitions.Do) (*server.ServerConfig, error) { // Helper functions func assertValidModule(module, name, minorVersionString string) bool { - switch module { - case "consensus" : - return consensus.AssertValidConsensusModule(name, minorVersionString) - case "manager" : - return manager.AssertValidApplicationManagerModule(name, minorVersionString) - } - return false + switch module { + case "consensus": + return consensus.AssertValidConsensusModule(name, minorVersionString) + case "manager": + return manager.AssertValidApplicationManagerModule(name, minorVersionString) + } + return false } diff --git a/definitions/do.go b/definitions/do.go index 5836716e2292d5efa9d11ddcdc42d634396bc56f..ed5c5a96b7b8c34134fc953cb458f52fb1f03342 100644 --- a/definitions/do.go +++ b/definitions/do.go @@ -17,37 +17,37 @@ package definitions import ( - "path" - "os" + "os" + "path" - viper "github.com/spf13/viper" + viper "github.com/spf13/viper" - util "github.com/eris-ltd/eris-db/util" + util "github.com/eris-ltd/eris-db/util" ) type Do struct { - // Persistent flags not reflected in the configuration files - // only set through command line flags or environment variables - Debug bool // ERIS_DB_DEBUG - Verbose bool // ERIS_DB_VERBOSE + // Persistent flags not reflected in the configuration files + // only set through command line flags or environment variables + Debug bool // ERIS_DB_DEBUG + Verbose bool // ERIS_DB_VERBOSE - // Work directory is the root directory for Eris-DB to act in - WorkDir string // ERIS_DB_WORKDIR - // Data directory is defaulted to WorkDir + `/data`. - // If Eris-CLI maps a data container, DataDir is intended to point - // to that mapped data directory. - DataDir string // ERIS_DB_DATADIR + // Work directory is the root directory for Eris-DB to act in + WorkDir string // ERIS_DB_WORKDIR + // Data directory is defaulted to WorkDir + `/data`. + // If Eris-CLI maps a data container, DataDir is intended to point + // to that mapped data directory. + DataDir string // ERIS_DB_DATADIR - // Capital configuration options explicitly extracted from the Viper config - ChainId string // has to be set to non-empty string, - // uniquely identifying the chain. - GenesisFile string + // Capital configuration options explicitly extracted from the Viper config + ChainId string // has to be set to non-empty string, + // uniquely identifying the chain. + GenesisFile string // ChainType string // CSV string // AccountTypes []string // Zip bool // Tarball bool - Config *viper.Viper + Config *viper.Viper // Accounts []*Account // Result string } @@ -56,10 +56,10 @@ func NowDo() *Do { do := new(Do) do.Debug = false do.Verbose = false - do.WorkDir = "" - do.DataDir = "" - do.ChainId = "" - do.GenesisFile = "" + do.WorkDir = "" + do.DataDir = "" + do.ChainId = "" + do.GenesisFile = "" do.Config = viper.New() return do } @@ -69,19 +69,19 @@ func NowDo() *Do { // The search directory is explicitly limited to a single location to // minimise the chance of loading the wrong configuration file. func (d *Do) ReadConfig(directory string, name string, configType string) error { - // name of the configuration file without extension - d.Config.SetConfigName(name) - // Eris-DB currently only uses "toml" - d.Config.SetConfigType(configType) - // look for configuration file in the working directory - d.Config.AddConfigPath(directory) - return d.Config.ReadInConfig() + // name of the configuration file without extension + d.Config.SetConfigName(name) + // Eris-DB currently only uses "toml" + d.Config.SetConfigType(configType) + // look for configuration file in the working directory + d.Config.AddConfigPath(directory) + return d.Config.ReadInConfig() } // InitialiseDataDirectory will default to WorkDir/data if DataDir is empty func (d *Do) InitialiseDataDirectory() error { - if d.DataDir == "" { - d.DataDir = path.Join(d.WorkDir, "data") - } - return util.EnsureDir(d.DataDir, os.ModePerm) + if d.DataDir == "" { + d.DataDir = path.Join(d.WorkDir, "data") + } + return util.EnsureDir(d.DataDir, os.ModePerm) } diff --git a/manager/eris-mint/pipe.go b/manager/eris-mint/pipe.go index 4dcbc2e04d89b7fca533e5048c696ab8056cc009..a5350898d6dcfda001a6bcb0a94555697cbcca92 100644 --- a/manager/eris-mint/pipe.go +++ b/manager/eris-mint/pipe.go @@ -133,7 +133,7 @@ func startState(dataDir, backend, genesisFile, chainId string) (*state.State, // avoid Tendermints PanicSanity and return a clean error if backend != db.DBBackendMemDB && backend != db.DBBackendLevelDB { - return nil, nil, fmt.Errorf("Dababase backend %s is not supported by %s", + return nil, nil, fmt.Errorf("Database backend %s is not supported by %s", backend, GetErisMintVersion) } diff --git a/rpc/tendermint/test/common_test.go b/rpc/tendermint/test/common_test.go new file mode 100644 index 0000000000000000000000000000000000000000..b2b0a48917df778a2f42178d9b85a8a08ebff4ae --- /dev/null +++ b/rpc/tendermint/test/common_test.go @@ -0,0 +1,16 @@ +package rpctest + +import ( + "testing" + "os" +) + +// Needs to be in a _test.go file to be picked up +func TestMain(m *testing.M) { + returnValue := TestWrapper(func() int { + return m.Run() + }) + + defer os.Exit(returnValue) +} + diff --git a/rpc/tendermint/test/config.go b/rpc/tendermint/test/config.go new file mode 100644 index 0000000000000000000000000000000000000000..8bbf6f9b89101b519ceaef8c21fc8b2ef665723e --- /dev/null +++ b/rpc/tendermint/test/config.go @@ -0,0 +1,223 @@ +package rpctest + +var defaultConfig = `# Copyright 2015, 2016 Eris Industries (UK) Ltd. +# This file is part of Eris-RT +# +# Eris-RT is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Eris-RT is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Eris-RT. If not, see <http://www.gnu.org/licenses/>. + +# This is a TOML configuration for Eris-DB chains + +[chain] + +# ChainId is a human-readable name to identify the chain. +# This must correspond to the chain_id defined in the genesis file +# and the assertion here provides a safe-guard on misconfiguring chains. +assert_chain_id = "MyChainId" +# semantic major and minor version +major_version = 0 +minor_version = 12 +# genesis file, relative path is to eris-db working directory +genesis_file = "genesis.json" + + +############################################################################### +## +## consensus +## +############################################################################### + + [chain.consensus] + # consensus defines the module to use for consensus and + # this will define the peer-to-peer consensus network; + # accepted values are "noops", "tmsp", "tendermint" + name = "tendermint" + # version is the major and minor semantic version; + # the version will be asserted on + major_version = 0 + minor_version = 6 + # relative path to consensus' module root folder + relative_root = "tendermint" + +############################################################################### +## +## application manager +## +############################################################################### + + [chain.manager] + # application manager name defines the module to use for handling + name = "erismint" + # version is the major and minor semantic version; + # the version will be asserted on + major_version = 0 + minor_version = 12 + # relative path to application manager root folder + relative_root = "erismint" + +################################################################################ +################################################################################ +## +## Server configurations +## +################################################################################ +################################################################################ + +[servers] + + [servers.bind] + address = "" + port = 1337 + + [servers.tls] + tls = false + cert_path = "" + key_path = "" + + [servers.cors] + enable = false + allow_origins = [] + allow_credentials = false + allow_methods = [] + allow_headers = [] + expose_headers = [] + max_age = 0 + + [servers.http] + json_rpc_endpoint = "/rpc" + + [servers.websocket] + endpoint = "/socketrpc" + max_sessions = 50 + read_buffer_size = 4096 + write_buffer_size = 4096 + + [servers.tendermint] + # Multiple listeners can be separated with a comma + rpc_local_address = "0.0.0.0:36657" + endpoint = "/websocket" + + [servers.logging] + console_log_level = "info" + file_log_level = "warn" + log_file = "" + +################################################################################ +################################################################################ +## +## Module configurations - dynamically loaded based on chain configuration +## +################################################################################ +################################################################################ + + +################################################################################ +## +## Tendermint Socket Protocol (TMSP) +## version 0.6.0 +## +## TMSP expects a tendermint consensus process to run and connect to Eris-DB +## +################################################################################ + +[tmsp] +# listener address for accepting tendermint socket protocol connections +listener = "tcp://0.0.0.0:46658" + +################################################################################ +## +## Tendermint +## version 0.6.0 +## +## in-process execution of Tendermint consensus engine +## +################################################################################ + +[tendermint] +# private validator file is used by tendermint to keep the status +# of the private validator, but also (currently) holds the private key +# for the private vaildator to sign with. This private key needs to be moved +# out and directly managed by eris-keys +# This file needs to be in the root directory +private_validator_file = "priv_validator.json" + + # Tendermint requires additional configuration parameters. + # Eris-DB's tendermint consensus module will load [tendermint.configuration] + # as the configuration for Tendermint. + # Eris-DB will respect the configurations set in this file where applicable, + # but reserves the option to override or block conflicting settings. + [tendermint.configuration] + # moniker is the name of the node on the tendermint p2p network + moniker = "anonymous_marmot" + # seeds lists the peers tendermint can connect to join the network + seeds = "" + # fast_sync allows a tendermint node to catch up faster when joining + # the network. + # NOTE: Tendermint has reported potential issues with fast_sync enabled. + # The recommended setting is for keeping it disabled. + fast_sync = false + db_backend = "leveldb" + log_level = "info" + # node local address + node_laddr = "0.0.0.0:46656" + # rpc local address + # NOTE: value is ignored when run in-process as RPC is + # handled by [servers.tendermint] + rpc_laddr = "0.0.0.0:36657" + # proxy application address - used for tmsp connections, + # and this port should not be exposed for in-process Tendermint + proxy_app = "tcp://127.0.0.1:46658" + + # Extended Tendermint configuration settings + # for reference to Tendermint see https://github.com/tendermint/tendermint/blob/master/config/tendermint/config.go + + # genesis_file = "./data/tendermint/genesis.json" + # skip_upnp = false + # addrbook_file = "./data/tendermint/addrbook.json" + # priv_validator_file = "./data/tendermint/priv_validator.json" + # db_dir = "./data/tendermint/data" + # prof_laddr = "" + # revision_file = "./data/tendermint/revision" + # cswal = "./data/tendermint/data/cswal" + # cswal_light = false + + # block_size = 10000 + # disable_data_hash = false + # timeout_propose = 3000 + # timeout_propose_delta = 500 + # timeout_prevote = 1000 + # timeout_prevote_delta = 500 + # timeout_precommit = 1000 + # timeout_precommit_delta = 500 + # timeout_commit = 1000 + # mempool_recheck = true + # mempool_recheck_empty = true + # mempool_broadcast = true + +################################################################################ +## +## Eris-Mint +## version 0.12.0 +## +## The original Ethereum virtual machine with IAVL merkle trees +## and tendermint/go-wire encoding +## +################################################################################ + +[erismint] +# Database backend to use for ErisMint state database. +db_backend = "leveldb" +# tendermint host address needs to correspond to tendermints configuration +# of the rpc local address +tendermint_host = "0.0.0.0:36657"` + diff --git a/rpc/tendermint/test/genesis.go b/rpc/tendermint/test/genesis.go index 3cd80e11130fb6d27449d4d7b3c68e84582143ab..66b0c3ca1a4003ff1e6b30059cfee41911cb4bd9 100644 --- a/rpc/tendermint/test/genesis.go +++ b/rpc/tendermint/test/genesis.go @@ -1,8 +1,8 @@ package rpctest -// priv keys generated deterministically eg rpc/tests/helpers.go +// priv keys generated deterministically eg rpc/tests/shared.go var defaultGenesis = `{ - "chain_id" : "tendermint_test", + "chain_id" : "MyChainId", "accounts": [ { "address": "E9B5D87313356465FAE33C406CE2C2979DE60BCB", diff --git a/rpc/tendermint/test/runner/main.go b/rpc/tendermint/test/runner/main.go new file mode 100644 index 0000000000000000000000000000000000000000..ca775c1a0cbd31169c087d0307d3240e9567c7ae --- /dev/null +++ b/rpc/tendermint/test/runner/main.go @@ -0,0 +1,13 @@ +package main + +import ( + "fmt" + + "github.com/eris-ltd/eris-db/util" + "github.com/eris-ltd/eris-db/rpc/tendermint/test" +) + +func main() { + fmt.Printf("%s", util.IsAddress("hello"), rpctest.Successor(2)) + //defer os.Exit(0) +} \ No newline at end of file diff --git a/rpc/tendermint/test/helpers.go b/rpc/tendermint/test/shared.go similarity index 75% rename from rpc/tendermint/test/helpers.go rename to rpc/tendermint/test/shared.go index 07b77cd4e550010e53e6c50971f9a830b4c6fcc0..22603b6511ad6c93a7e071e7a97345139fe5d64c 100644 --- a/rpc/tendermint/test/helpers.go +++ b/rpc/tendermint/test/shared.go @@ -6,79 +6,86 @@ import ( "testing" acm "github.com/eris-ltd/eris-db/account" - edb "github.com/eris-ltd/eris-db/core" - erismint "github.com/eris-ltd/eris-db/manager/eris-mint" - sm "github.com/eris-ltd/eris-db/manager/eris-mint/state" - stypes "github.com/eris-ltd/eris-db/manager/eris-mint/state/types" + "github.com/eris-ltd/eris-db/core" edbcli "github.com/eris-ltd/eris-db/rpc/tendermint/client" ctypes "github.com/eris-ltd/eris-db/rpc/tendermint/core/types" - txs "github.com/eris-ltd/eris-db/txs" + "github.com/eris-ltd/eris-db/server" + "github.com/eris-ltd/eris-db/test/fixtures" + "github.com/eris-ltd/eris-db/txs" . "github.com/tendermint/go-common" "github.com/tendermint/go-crypto" - dbm "github.com/tendermint/go-db" - "github.com/tendermint/go-events" - "github.com/tendermint/go-p2p" rpcclient "github.com/tendermint/go-rpc/client" - "github.com/tendermint/go-wire" - cfg "github.com/tendermint/go-config" - "github.com/tendermint/tendermint/config/tendermint_test" + "github.com/spf13/viper" nm "github.com/tendermint/tendermint/node" "github.com/tendermint/tendermint/types" - "github.com/eris-ltd/eris-db/server" + "path" ) // global variables for use across all tests var ( - config cfg.Config + config = server.DefaultServerConfig() + rootWorkDir string node *nm.Node - mempoolCount = 0 - chainID string - rpcAddr string - requestAddr string - websocketAddr string + mempoolCount = 0 + chainID string + websocketAddr string websocketEndpoint string - clientURI *rpcclient.ClientURI - clientJSON *rpcclient.ClientJSONRPC - user = makeUsers(5) // make keys + user = makeUsers(5) // make keys clients map[string]rpcclient.Client + + testCore *core.Core ) -func init() { - initGlobalVariables() +// initialize config and create new node +func initGlobalVariables(ffs *fixtures.FileFixtures) error { + testConfigFile := ffs.AddFile("config.toml", defaultConfig) + rootWorkDir = ffs.AddDir("rootWorkDir") + rootDataDir := ffs.AddDir("rootDataDir") + genesisFile := ffs.AddFile("rootWorkDir/genesis.json", defaultGenesis) + + if ffs.Error != nil { + return ffs.Error + } - saveNewPriv() -} + testConfig := viper.New() + testConfig.SetConfigFile(testConfigFile) + err := testConfig.ReadInConfig() -// initialize config and create new node -func initGlobalVariables() { - 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 + if err != nil { + return err + } + + chainID = testConfig.GetString("chain.assert_chain_id") + rpcAddr := testConfig.GetString("erismint.tendermint_host") websocketAddr = rpcAddr + config.Tendermint.RpcLocalAddress = rpcAddr websocketEndpoint = "/websocket" - clientURI = rpcclient.NewClientURI(requestAddr) - clientJSON = rpcclient.NewClientJSONRPC(requestAddr) - - clients = map[string]rpcclient.Client{ - "JSONRPC": clientJSON, - "HTTP": clientURI, + consensusConfig, err := core.LoadModuleConfig(testConfig, rootWorkDir, + rootDataDir, genesisFile, chainID, "consensus") + if err != nil { + return err } - // write the genesis - MustWriteFile(config.GetString("genesis_file"), []byte(defaultGenesis), 0600) + managerConfig, err := core.LoadModuleConfig(testConfig, rootWorkDir, + rootDataDir, genesisFile, chainID, "manager") + if err != nil { + return err + } - // TODO: change consensus/state.go timeouts to be shorter + testCore, err = core.NewCore("testCore", consensusConfig, managerConfig) + if err != nil { + return err + } - // start a node - ready := make(chan struct{}) - go newNode(ready) - <-ready + clients = map[string]rpcclient.Client{ + "JSONRPC": rpcclient.NewClientURI(rpcAddr), + "HTTP": rpcclient.NewClientJSONRPC(rpcAddr), + } + return nil } // deterministic account generation, synced with genesis file in config/tendermint_test/config.go @@ -93,36 +100,16 @@ func makeUsers(n int) []*acm.PrivAccount { } // create a new node and sleep forever -func newNode(ready chan struct{}) { - 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) - stateDB.Set(stypes.GenDocKey, buf.Bytes()) - if *err != nil { - Exit(Fmt("Unable to write gendoc to db: %v", err)) - } - evsw := events.NewEventSwitch() - evsw.Start() - // create the app - app := erismint.NewErisMint(state, evsw) - - // Create & start node - privValidatorFile := config.GetString("priv_validator_file") - privValidator := types.LoadOrGenPrivValidator(privValidatorFile) - node = nm.NewNode(config, privValidator, nm.GetProxyApp) - l := p2p.NewDefaultListener("tcp", config.GetString("node_laddr"), true) - node.AddListener(l) - node.Start() - +func newNode(ready chan error) { // Run the RPC server. - edb.StartRPC(server.DefaultServerConfig(), node, app) - ready <- struct{}{} + _, err := testCore.NewGatewayTendermint(config) + ready <- err // Sleep forever - ch := make(chan struct{}) - <-ch + if err == nil { + //ch := make(chan struct{}) + //<-ch + } } func saveNewPriv() { @@ -132,7 +119,7 @@ func saveNewPriv() { PubKey: crypto.PubKeyEd25519(user[0].PubKey.(crypto.PubKeyEd25519)), PrivKey: crypto.PrivKeyEd25519(user[0].PrivKey.(crypto.PrivKeyEd25519)), } - priv.SetFile(config.GetString("priv_validator_file")) + priv.SetFile(path.Join(rootWorkDir, "priv_validator.json")) priv.Save() } @@ -142,7 +129,7 @@ func saveNewPriv() { func makeDefaultSendTx(t *testing.T, typ string, addr []byte, amt int64) *txs.SendTx { nonce := getNonce(t, typ, user[0].Address) tx := txs.NewSendTx() - tx.AddInputWithNonce(user[0].PubKey, amt, nonce+1) + tx.AddInputWithNonce(user[0].PubKey, amt, nonce + 1) tx.AddOutput(addr, amt) return tx } @@ -155,14 +142,14 @@ func makeDefaultSendTxSigned(t *testing.T, typ string, addr []byte, amt int64) * func makeDefaultCallTx(t *testing.T, typ string, addr, code []byte, amt, gasLim, fee int64) *txs.CallTx { nonce := getNonce(t, typ, user[0].Address) - tx := txs.NewCallTxWithNonce(user[0].PubKey, addr, code, amt, gasLim, fee, nonce+1) + tx := txs.NewCallTxWithNonce(user[0].PubKey, addr, code, amt, gasLim, fee, nonce + 1) tx.Sign(chainID, user[0]) return tx } func makeDefaultNameTx(t *testing.T, typ string, name, value string, amt, fee int64) *txs.NameTx { nonce := getNonce(t, typ, user[0].Address) - tx := txs.NewNameTxWithNonce(user[0].PubKey, name, value, amt, fee, nonce+1) + tx := txs.NewNameTxWithNonce(user[0].PubKey, name, value, amt, fee, nonce + 1) tx.Sign(chainID, user[0]) return tx } diff --git a/test/fixtures/file_fixtures.go b/test/fixtures/file_fixtures.go new file mode 100644 index 0000000000000000000000000000000000000000..5c1da7bebe6c7ddf6ef405905010a552f508b6dd --- /dev/null +++ b/test/fixtures/file_fixtures.go @@ -0,0 +1,75 @@ +package fixtures + +import ( + "github.com/docker/docker/pkg/ioutils" + "path" + "os" +) + +// FileFixtures writes files to a temporary location for use in testing. +type FileFixtures struct { + tempDir string + // If an error has occurred setting up fixtures + Error error +} + +// Set up a new FileFixtures object by passing an interlaced list of file names +// and file contents. The file names will be interpreted as relative to some +// temporary root directory that is fixed when allocate() is called on the +// FileFixtures struct. +func NewFileFixtures(identifyingPrefix string) *FileFixtures { + dir, err := ioutils.TempDir("", identifyingPrefix) + return &FileFixtures{ + tempDir: dir, + Error: err, + } +} + +// Add a file relative to the FileFixtures tempDir using name for the relative +// part of the path. +func (ffs *FileFixtures) AddFile(name, content string) string { + if ffs.Error != nil { + return "" + } + filePath := path.Join(ffs.tempDir, name) + ffs.AddDir(path.Dir(name)) + if (ffs.Error == nil) { + ffs.Error = createWriteClose(filePath, content) + } + return filePath +} + +// Ensure that the directory relative to the FileFixtures tempDir exists using +// name for the relative part of the path. +func (ffs *FileFixtures) AddDir(name string) string { + if ffs.Error != nil { + return "" + } + filePath := path.Join(ffs.tempDir, name) + ffs.Error = os.MkdirAll(filePath, 0777) + return filePath +} + + +// Cleans up the the temporary files (with fire) +func (ffs *FileFixtures) RemoveAll() { + if err := os.RemoveAll(ffs.tempDir); err != nil { + // Since we expect to be called from being deferred in a test it's + // better if we panic here so that the caller finds + panic(err) + } +} + +// Create a text file at filename with contents content +func createWriteClose(filename, content string) error { + // We'll create any parent dirs, with permissive permissions + err := os.MkdirAll(path.Dir(filename), 0777) + f, err := os.Create(filename) + if err != nil { + return err + } + f.WriteString(content) + defer f.Close() + return nil +} + diff --git a/test/testdata/helpers.g_ b/test/testdata/helpers.g_ index 5508a1ad11e9e0598cc2d871fc3bb25db1d19749..2b2b2edbbe2bd7b4dd9d36c34d2e37bf5c228e55 100644 --- a/test/testdata/helpers.g_ +++ b/test/testdata/helpers.g_ @@ -9,7 +9,6 @@ import ( . "github.com/tendermint/go-common" stypes "github.com/eris-ltd/eris-db/manager/eris-mint/state/types" - "github.com/eris-ltd/eris-db/txs" "github.com/tendermint/go-wire" ) diff --git a/util/util_test.go b/util/util_test.go deleted file mode 100644 index deef5ebb81a9af2c5fe4074d10662ebab3bdb145..0000000000000000000000000000000000000000 --- a/util/util_test.go +++ /dev/null @@ -1,76 +0,0 @@ -package util - -import ( - "github.com/stretchr/testify/assert" - "testing" -) - -// This should succeed -func TestIsHexSuccess(t *testing.T) { - assert.True(t, IsHex("5B33600060006101000A81548173FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF021916908302179055505B6102828061003B6000396000F3006000357C01000000000000000000000000000000000000000000000000000000009004806337F428411461004557806340C10F191461005A578063D0679D341461006E57005B610050600435610244565B8060005260206000F35B610068600435602435610082565B60006000F35B61007C600435602435610123565B60006000F35B600060009054906101000A900473FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF1673FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF163373FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF1614156100DD576100E2565B61011F565B80600160005060008473FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF1681526020019081526020016000206000828282505401925050819055505B5050565B80600160005060003373FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF168152602001908152602001600020600050541061015E57610163565B610240565B80600160005060003373FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF16815260200190815260200160002060008282825054039250508190555080600160005060008473FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF1681526020019081526020016000206000828282505401925050819055507F93EB3C629EB575EDAF0252E4F9FC0C5CCADA50496F8C1D32F0F93A65A8257EB560003373FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF1681526020018373FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF1681526020018281526020016000A15B5050565B6000600160005060008373FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF16815260200190815260200160002060005054905061027D565B91905056")) -} - -// This should fail because there is a non matching character. -func TestIsHexFailChar(t *testing.T) { - assert.False(t, IsHex("562Q")) -} - -// This should succeed -func TestIsHashSuccess(t *testing.T) { - assert.True(t, IsHash("DA4F4DC4A54620F1E0AA1213631C4DC2957B7415E3F8C066C30009BC57C4E5FC")) -} - -// This should fail because there is a non matching character. -func TestIsHashFailChar(t *testing.T) { - assert.False(t, IsHash("RM4F4DC4A54620F1E0AA1213631C4DC2957B7415E3F8C066C30009BC57C4E5FC")) -} - -// This should fail because the length is not right. -func TestIsHashFailLength(t *testing.T) { - assert.False(t, IsHash("DA4F4DC4A54620F1E0AA1213631C4DC2957B7415E3F8C066C30009BC57C4E5F")) -} - -// This should succeed -func TestIsAddressSuccess(t *testing.T) { - assert.True(t, IsAddress("37236DF251AB70022B1DA351F08A20FB52443E37")) -} - -// This should fail because there is a non matching character. -func TestIsAddressFailChar(t *testing.T) { - assert.False(t, IsAddress("37236DF251AB70022B1DA351F08A20FB52443E3Q")) -} - -// This should fail because the length is not right. -func TestIsAddressFailLength(t *testing.T) { - assert.False(t, IsAddress("37236DF251AB70022B1DA351F08A20FB52443E")) -} - -// This should succeed -func TestIsPubKeySuccess(t *testing.T) { - assert.True(t, IsPubKey("CB3688B7561D488A2A4834E1AEE9398BEF94844D8BDBBCA980C11E3654A45906")) -} - -// This should fail because there is a non matching character. -func TestIsPubKeyFailChar(t *testing.T) { - assert.False(t, IsPubKey("CB3688B7I6TD488A2A4834E1AEE9398BEF94844D8BDBBCA980C11E3654A45906")) -} - -// This should fail because the length is not right. -func TestIsPubKeyFailLength(t *testing.T) { - assert.False(t, IsPubKey("CB3688B7561D488A2A4834E1AEE9398BEF94844D8BDBBCA980C11")) -} - -// This should succeed -func TestIsPrivKeySuccess(t *testing.T) { - assert.True(t, IsPrivKey("6B72D45EB65F619F11CE580C8CAED9E0BADC774E9C9C334687A65DCBAD2C4151CB3688B7561D488A2A4834E1AEE9398BEF94844D8BDBBCA980C11E3654A45906")) -} - -// This should fail because there is a non matching character. -func TestIsPrivKeyFailChar(t *testing.T) { - assert.False(t, IsPrivKey("6B72D45EB65F619F11CE580C8CAED9E0BADC774E9C9C334687A65DCBAD2C4151CB3688B7561D488A2A4834ESAEE9398BEF94844D8BDBBCA980C11E3654A45906")) -} - -// This should fail because the length is not right. -func TestIsPrivKeyFailLength(t *testing.T) { - assert.False(t, IsPrivKey("6B72D45EB65F619F11CE580C8CAED9E0BADC774ED2C4151CB3688B7561D488A2A48EF94844D8BDBBCA980C11E3654A45906")) -}