From 3d3d8b5b7b63c5ee0c413311d307cc2b3f6161c6 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Sun, 30 Oct 2016 03:55:27 -0700 Subject: [PATCH] cswal -> cs_wal_dir --- cmd/tendermint/main.go | 6 ++-- cmd/tendermint/reset_priv_validator.go | 2 +- config/tendermint/config.go | 7 +++-- config/tendermint_test/config.go | 7 +++-- consensus/replay_test.go | 43 +++++++++++++------------- consensus/state.go | 6 ++-- consensus/test_data/README.md | 1 - consensus/wal.go | 4 +-- mempool/mempool.go | 6 ++-- node/node.go | 16 ++-------- 10 files changed, 43 insertions(+), 55 deletions(-) diff --git a/cmd/tendermint/main.go b/cmd/tendermint/main.go index 7dd2ea902..9f3410517 100644 --- a/cmd/tendermint/main.go +++ b/cmd/tendermint/main.go @@ -41,10 +41,10 @@ Commands: case "node": node.RunNode(config) case "replay": - if len(args) > 1 && args[1] == "console" { - node.RunReplayConsole(config) + if len(args) > 2 && args[1] == "console" { + node.RunReplayConsole(config, args[2]) } else { - node.RunReplay(config) + node.RunReplay(config, args[1]) } case "init": init_files() diff --git a/cmd/tendermint/reset_priv_validator.go b/cmd/tendermint/reset_priv_validator.go index 2887c10d0..9ecbaa90b 100644 --- a/cmd/tendermint/reset_priv_validator.go +++ b/cmd/tendermint/reset_priv_validator.go @@ -11,7 +11,7 @@ import ( func reset_all() { reset_priv_validator() os.RemoveAll(config.GetString("db_dir")) - os.Remove(config.GetString("cswal")) + os.RemoveAll(config.GetString("cs_wal_dir")) } // NOTE: this is totally unsafe. diff --git a/config/tendermint/config.go b/config/tendermint/config.go index 465297ba3..86523473c 100644 --- a/config/tendermint/config.go +++ b/config/tendermint/config.go @@ -22,6 +22,7 @@ func getTMRoot(rootDir string) string { func initTMRoot(rootDir string) { rootDir = getTMRoot(rootDir) EnsureDir(rootDir, 0700) + EnsureDir(rootDir+"/data", 0700) configFilePath := path.Join(rootDir, "config.toml") @@ -68,8 +69,8 @@ func GetConfig(rootDir string) cfg.Config { mapConfig.SetDefault("rpc_laddr", "tcp://0.0.0.0:46657") mapConfig.SetDefault("prof_laddr", "") mapConfig.SetDefault("revision_file", rootDir+"/revision") - mapConfig.SetDefault("cswal", rootDir+"/data/cswal") - mapConfig.SetDefault("cswal_light", false) + mapConfig.SetDefault("cs_wal_dir", rootDir+"/data/cs.wal") + mapConfig.SetDefault("cs_wal_light", false) mapConfig.SetDefault("filter_peers", false) mapConfig.SetDefault("block_size", 10000) @@ -84,7 +85,7 @@ func GetConfig(rootDir string) cfg.Config { mapConfig.SetDefault("mempool_recheck", true) mapConfig.SetDefault("mempool_recheck_empty", true) mapConfig.SetDefault("mempool_broadcast", true) - mapConfig.SetDefault("mempool_wal", rootDir+"/data/mempool_wal") + mapConfig.SetDefault("mempool_wal_dir", rootDir+"/data/mempool.wal") return mapConfig } diff --git a/config/tendermint_test/config.go b/config/tendermint_test/config.go index 6f3217475..0fe861ada 100644 --- a/config/tendermint_test/config.go +++ b/config/tendermint_test/config.go @@ -33,6 +33,7 @@ func initTMRoot(rootDir string) { } // Create new dir EnsureDir(rootDir, 0700) + EnsureDir(rootDir+"/data", 0700) configFilePath := path.Join(rootDir, "config.toml") genesisFilePath := path.Join(rootDir, "genesis.json") @@ -81,8 +82,8 @@ func ResetConfig(localPath string) cfg.Config { mapConfig.SetDefault("rpc_laddr", "tcp://0.0.0.0:36657") mapConfig.SetDefault("prof_laddr", "") mapConfig.SetDefault("revision_file", rootDir+"/revision") - mapConfig.SetDefault("cswal", rootDir+"/data/cswal") - mapConfig.SetDefault("cswal_light", false) + mapConfig.SetDefault("cs_wal_dir", rootDir+"/data/cs.wal") + mapConfig.SetDefault("cs_wal_light", false) mapConfig.SetDefault("filter_peers", false) mapConfig.SetDefault("block_size", 10000) @@ -97,7 +98,7 @@ func ResetConfig(localPath string) cfg.Config { mapConfig.SetDefault("mempool_recheck", true) mapConfig.SetDefault("mempool_recheck_empty", true) mapConfig.SetDefault("mempool_broadcast", true) - mapConfig.SetDefault("mempool_wal", "") + mapConfig.SetDefault("mempool_wal_dir", "") return mapConfig } diff --git a/consensus/replay_test.go b/consensus/replay_test.go index 154545112..b3626c869 100644 --- a/consensus/replay_test.go +++ b/consensus/replay_test.go @@ -28,7 +28,7 @@ var testCases = []*testCase{ type testCase struct { name string - log string //full cswal + log string //full cs wal stepMap map[int]int8 // map lines of log to privval step proposeLine int @@ -71,21 +71,20 @@ func readWAL(p string) string { return string(b) } -func writeWAL(log string) string { - fmt.Println("writing", log) - // write the needed wal to file - f, err := ioutil.TempFile(os.TempDir(), "replay_test_") +func writeWAL(walMsgs string) string { + tempDir := os.TempDir() + walDir := tempDir + "/wal" + RandStr(12) + // Create WAL directory + err := EnsureDir(walDir, 0700) if err != nil { panic(err) } - - _, err = f.WriteString(log) + // Write the needed WAL to file + err = WriteFile(walDir+"/wal", []byte(walMsgs), 0600) if err != nil { panic(err) } - name := f.Name() - f.Close() - return name + return walDir } func waitForBlock(newBlockCh chan interface{}, thisCase *testCase, i int) { @@ -97,10 +96,10 @@ func waitForBlock(newBlockCh chan interface{}, thisCase *testCase, i int) { } } -func runReplayTest(t *testing.T, cs *ConsensusState, fileName string, newBlockCh chan interface{}, +func runReplayTest(t *testing.T, cs *ConsensusState, walDir string, newBlockCh chan interface{}, thisCase *testCase, i int) { - cs.config.Set("cswal", fileName) + cs.config.Set("cs_wal_dir", walDir) cs.Start() // Wait to make a new block. // This is just a signal that we haven't halted; its not something contained in the WAL itself. @@ -124,7 +123,7 @@ func setupReplayTest(thisCase *testCase, nLines int, crashAfter bool) (*Consensu lastMsg := split[nLines] // we write those lines up to (not including) one with the signature - fileName := writeWAL(strings.Join(split[:nLines], "\n") + "\n") + walDir := writeWAL(strings.Join(split[:nLines], "\n") + "\n") cs := fixedConsensusStateDummy() @@ -136,7 +135,7 @@ func setupReplayTest(thisCase *testCase, nLines int, crashAfter bool) (*Consensu newBlockCh := subscribeToEvent(cs.evsw, "tester", types.EventStringNewBlock(), 1) - return cs, newBlockCh, lastMsg, fileName + return cs, newBlockCh, lastMsg, walDir } //----------------------------------------------- @@ -147,8 +146,8 @@ func TestReplayCrashAfterWrite(t *testing.T) { for _, thisCase := range testCases { split := strings.Split(thisCase.log, "\n") for i := 0; i < len(split)-1; i++ { - cs, newBlockCh, _, f := setupReplayTest(thisCase, i+1, true) - runReplayTest(t, cs, f, newBlockCh, thisCase, i+1) + cs, newBlockCh, _, walDir := setupReplayTest(thisCase, i+1, true) + runReplayTest(t, cs, walDir, newBlockCh, thisCase, i+1) } } } @@ -160,7 +159,7 @@ func TestReplayCrashAfterWrite(t *testing.T) { func TestReplayCrashBeforeWritePropose(t *testing.T) { for _, thisCase := range testCases { lineNum := thisCase.proposeLine - cs, newBlockCh, proposalMsg, f := setupReplayTest(thisCase, lineNum, false) // propose + cs, newBlockCh, proposalMsg, walDir := setupReplayTest(thisCase, lineNum, false) // propose // Set LastSig var err error var msg TimedWALMessage @@ -171,14 +170,14 @@ func TestReplayCrashBeforeWritePropose(t *testing.T) { } cs.privValidator.LastSignBytes = types.SignBytes(cs.state.ChainID, proposal.Proposal) cs.privValidator.LastSignature = proposal.Proposal.Signature - runReplayTest(t, cs, f, newBlockCh, thisCase, lineNum) + runReplayTest(t, cs, walDir, newBlockCh, thisCase, lineNum) } } func TestReplayCrashBeforeWritePrevote(t *testing.T) { for _, thisCase := range testCases { lineNum := thisCase.prevoteLine - cs, newBlockCh, voteMsg, f := setupReplayTest(thisCase, lineNum, false) // prevote + cs, newBlockCh, voteMsg, walDir := setupReplayTest(thisCase, lineNum, false) // prevote types.AddListenerForEvent(cs.evsw, "tester", types.EventStringCompleteProposal(), func(data types.TMEventData) { // Set LastSig var err error @@ -191,14 +190,14 @@ func TestReplayCrashBeforeWritePrevote(t *testing.T) { cs.privValidator.LastSignBytes = types.SignBytes(cs.state.ChainID, vote.Vote) cs.privValidator.LastSignature = vote.Vote.Signature }) - runReplayTest(t, cs, f, newBlockCh, thisCase, lineNum) + runReplayTest(t, cs, walDir, newBlockCh, thisCase, lineNum) } } func TestReplayCrashBeforeWritePrecommit(t *testing.T) { for _, thisCase := range testCases { lineNum := thisCase.precommitLine - cs, newBlockCh, voteMsg, f := setupReplayTest(thisCase, lineNum, false) // precommit + cs, newBlockCh, voteMsg, walDir := setupReplayTest(thisCase, lineNum, false) // precommit types.AddListenerForEvent(cs.evsw, "tester", types.EventStringPolka(), func(data types.TMEventData) { // Set LastSig var err error @@ -211,6 +210,6 @@ func TestReplayCrashBeforeWritePrecommit(t *testing.T) { cs.privValidator.LastSignBytes = types.SignBytes(cs.state.ChainID, vote.Vote) cs.privValidator.LastSignature = vote.Vote.Signature }) - runReplayTest(t, cs, f, newBlockCh, thisCase, lineNum) + runReplayTest(t, cs, walDir, newBlockCh, thisCase, lineNum) } } diff --git a/consensus/state.go b/consensus/state.go index 8eb95ddbc..bd5dd974e 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -304,7 +304,7 @@ func (cs *ConsensusState) SetPrivValidator(priv *types.PrivValidator) { func (cs *ConsensusState) OnStart() error { cs.BaseService.OnStart() - err := cs.OpenWAL(cs.config.GetString("cswal")) + err := cs.OpenWAL(cs.config.GetString("cs_wal_dir")) if err != nil { return err } @@ -350,10 +350,10 @@ func (cs *ConsensusState) OnStop() { } // Open file to log all consensus messages and timeouts for deterministic accountability -func (cs *ConsensusState) OpenWAL(file string) (err error) { +func (cs *ConsensusState) OpenWAL(walDir string) (err error) { cs.mtx.Lock() defer cs.mtx.Unlock() - wal, err := NewWAL(file, cs.config.GetBool("cswal_light")) + wal, err := NewWAL(walDir, cs.config.GetBool("cs_wal_light")) if err != nil { return err } diff --git a/consensus/test_data/README.md b/consensus/test_data/README.md index e3bfca70b..8d14b8e68 100644 --- a/consensus/test_data/README.md +++ b/consensus/test_data/README.md @@ -2,7 +2,6 @@ The easiest way to generate this data is to copy `~/.tendermint_test/somedir/*` to `~/.tendermint` and to run a local node. -Be sure to set the db to "leveldb" to create a cswal file in `~/.tendermint/data/cswal`. If you need to change the signatures, you can use a script as follows: The privBytes comes from `config/tendermint_test/...`: diff --git a/consensus/wal.go b/consensus/wal.go index ce3a96f8a..8460a8322 100644 --- a/consensus/wal.go +++ b/consensus/wal.go @@ -40,8 +40,8 @@ type WAL struct { light bool // ignore block parts } -func NewWAL(path string, light bool) (*WAL, error) { - head, err := auto.OpenAutoFile(path) +func NewWAL(walDir string, light bool) (*WAL, error) { + head, err := auto.OpenAutoFile(walDir + "/wal") if err != nil { return nil, err } diff --git a/mempool/mempool.go b/mempool/mempool.go index 66df850ea..7d9438ed1 100644 --- a/mempool/mempool.go +++ b/mempool/mempool.go @@ -85,9 +85,9 @@ func NewMempool(config cfg.Config, proxyAppConn proxy.AppConnMempool) *Mempool { } func (mem *Mempool) initWAL() { - walFileName := mem.config.GetString("mempool_wal") - if walFileName != "" { - af, err := auto.OpenAutoFile(walFileName) + walDir := mem.config.GetString("mempool_wal_dir") + if walDir != "" { + af, err := auto.OpenAutoFile(walDir + "/wal") if err != nil { PanicSanity(err) } diff --git a/node/node.go b/node/node.go index bb191b55e..7c4a08e3c 100644 --- a/node/node.go +++ b/node/node.go @@ -52,8 +52,6 @@ func NewNodeDefault(config cfg.Config) *Node { func NewNode(config cfg.Config, privValidator *types.PrivValidator, clientCreator proxy.ClientCreator) *Node { - EnsureDir(config.GetString("db_dir"), 0700) // incase we use memdb, cswal still gets written here - // Get BlockStore blockStoreDB := dbm.NewDB("blockstore", config.GetString("db_backend"), config.GetString("db_dir")) blockStore := bc.NewBlockStore(blockStoreDB) @@ -414,12 +412,7 @@ func newConsensusState(config cfg.Config) *consensus.ConsensusState { return consensusState } -func RunReplayConsole(config cfg.Config) { - walFile := config.GetString("cswal") - if walFile == "" { - Exit("cswal file name not set in tendermint config") - } - +func RunReplayConsole(config cfg.Config, walFile string) { consensusState := newConsensusState(config) if err := consensusState.ReplayConsole(walFile); err != nil { @@ -427,12 +420,7 @@ func RunReplayConsole(config cfg.Config) { } } -func RunReplay(config cfg.Config) { - walFile := config.GetString("cswal") - if walFile == "" { - Exit("cswal file name not set in tendermint config") - } - +func RunReplay(config cfg.Config, walFile string) { consensusState := newConsensusState(config) if err := consensusState.ReplayMessages(walFile); err != nil {