diff --git a/consensus/replay_test.go b/consensus/replay_test.go index 8643427c0..a1e06c65c 100644 --- a/consensus/replay_test.go +++ b/consensus/replay_test.go @@ -264,9 +264,12 @@ func (w *crashingWAL) Wait() { w.next.Wait() } //------------------------------------------------------------------------------------------ // Handshake Tests -var ( +const ( NUM_BLOCKS = 6 - mempool = types.MockMempool{} +) + +var ( + mempool = types.MockMempool{} ) //--------------------------------------- @@ -305,12 +308,12 @@ func TestHandshakeReplayNone(t *testing.T) { } } -func writeWAL(walMsgs []byte) string { +func tempWALWithData(data []byte) string { walFile, err := ioutil.TempFile("", "wal") if err != nil { panic(fmt.Errorf("failed to create temp WAL file: %v", err)) } - _, err = walFile.Write(walMsgs) + _, err = walFile.Write(data) if err != nil { panic(fmt.Errorf("failed to write to temp WAL file: %v", err)) } @@ -324,11 +327,11 @@ func writeWAL(walMsgs []byte) string { func testHandshakeReplay(t *testing.T, nBlocks int, mode uint) { config := ResetConfig("proxy_test_") - walBody, err := GenWAL(NUM_BLOCKS) + walBody, err := WALWithNBlocks(NUM_BLOCKS) if err != nil { t.Fatal(err) } - walFile := writeWAL(walBody) + walFile := tempWALWithData(walBody) config.Consensus.SetWalFile(walFile) privVal := types.LoadPrivValidatorFS(config.PrivValidatorFile()) diff --git a/consensus/walgen.go b/consensus/wal_generator.go similarity index 87% rename from consensus/walgen.go rename to consensus/wal_generator.go index 2f541f1ff..8a2e21821 100644 --- a/consensus/walgen.go +++ b/consensus/wal_generator.go @@ -1,4 +1,3 @@ -// walgen provides an utility function for generating WAL on the fly. package consensus import ( @@ -23,15 +22,17 @@ import ( "github.com/tendermint/tmlibs/log" ) -// GenWAL generates a consensus WAL. It does this by spining up a new node with a -// dummy application and special consensus wal instance (byteBufferWAL) and -// waits until numBlocks are created. Then it returns a WAL body. -func GenWAL(numBlocks int) (body []byte, err error) { +// WALWithNBlocks generates a consensus WAL. It does this by spining up a +// stripped down version of node (proxy app, event bus, consensus state) with a +// persistent dummy application and special consensus wal instance +// (byteBufferWAL) and waits until numBlocks are created. Then it returns a WAL +// content. +func WALWithNBlocks(numBlocks int) (data []byte, err error) { config := getConfig() - app := dummy.NewPersistentDummyApplication(filepath.Join(config.DBDir(), "genwal")) + app := dummy.NewPersistentDummyApplication(filepath.Join(config.DBDir(), "wal_generator")) - logger := log.TestingLogger().With("walgen", "walgen") + logger := log.NewNopLogger() // log.TestingLogger().With("wal_generator", "wal_generator") ///////////////////////////////////////////////////////////////////////////// // COPY PASTE FROM node.go WITH A FEW MODIFICATIONS @@ -77,6 +78,7 @@ func GenWAL(numBlocks int) (body []byte, err error) { wr := bufio.NewWriter(&b) numBlocksWritten := make(chan struct{}) wal := &byteBufferWAL{enc: NewWALEncoder(wr), heightToStop: int64(numBlocks), signalWhenStopsTo: numBlocksWritten} + // see wal.go#103 wal.Save(EndHeightMessage{0}) consensusState.wal = wal @@ -142,11 +144,12 @@ type byteBufferWAL struct { signalWhenStopsTo chan struct{} } -var fixedTime, err = time.Parse(time.RFC3339, "2017-01-02T15:04:05Z") +// needed for determinism +var fixedTime, _ = time.Parse(time.RFC3339, "2017-01-02T15:04:05Z") // Save writes message to the internal buffer except when heightToStop is -// reached, in which case it signal the caller via signalWhenStopsTo and skip -// writing. +// reached, in which case it will signal the caller via signalWhenStopsTo and +// skip writing. func (w *byteBufferWAL) Save(m WALMessage) { if w.stopped { return diff --git a/consensus/wal_test.go b/consensus/wal_test.go index d81400b11..8ec1a7c2c 100644 --- a/consensus/wal_test.go +++ b/consensus/wal_test.go @@ -42,11 +42,11 @@ func TestWALEncoderDecoder(t *testing.T) { } func TestSearchForEndHeight(t *testing.T) { - walBody, err := GenWAL(6) + walBody, err := WALWithNBlocks(6) if err != nil { t.Fatal(err) } - walFile := writeWAL(walBody) + walFile := tempWALWithData(walBody) wal, err := NewWAL(walFile, false) if err != nil {