From 57a684d5ac27963090cff392d6d6e986e3e8578b Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Wed, 25 Oct 2017 21:54:56 -0400 Subject: [PATCH] fixes from review --- consensus/replay.go | 3 +-- consensus/replay_test.go | 2 ++ consensus/wal.go | 8 ++------ consensus/wal_test.go | 25 +++++++++++++++++++++++++ scripts/cutWALUntil/main.go | 2 +- scripts/wal2json/main.go | 3 +++ 6 files changed, 34 insertions(+), 9 deletions(-) diff --git a/consensus/replay.go b/consensus/replay.go index e81512701..d3c5cd5d1 100644 --- a/consensus/replay.go +++ b/consensus/replay.go @@ -113,12 +113,11 @@ func (cs *ConsensusState) catchupReplay(csHeight int) error { cs.Logger.Error("Replay: wal.group.Search returned EOF", "#ENDHEIGHT", csHeight-1) } else if err != nil { return err - } else { - defer gr.Close() } if !found { return errors.New(cmn.Fmt("Cannot replay height %d. WAL does not contain #ENDHEIGHT for %d.", csHeight, csHeight-1)) } + defer gr.Close() cs.Logger.Info("Catchup by replaying consensus messages", "height", csHeight) diff --git a/consensus/replay_test.go b/consensus/replay_test.go index 9ab2efc17..33ca1d268 100644 --- a/consensus/replay_test.go +++ b/consensus/replay_test.go @@ -219,6 +219,7 @@ func TestWALCrashAfterWrite(t *testing.T) { for i := 0; i < splitSize-1; i++ { t.Run(fmt.Sprintf("%s:%d", thisCase.name, i), func(t *testing.T) { cs, newBlockCh, _, walFile := setupReplayTest(t, thisCase, i+1, true) + cs.config.TimeoutPropose = 100 runReplayTest(t, cs, walFile, newBlockCh, thisCase, i+1) // cleanup os.Remove(walFile) @@ -237,6 +238,7 @@ func TestWALCrashBeforeWritePropose(t *testing.T) { t.Run(fmt.Sprintf("%s:%d", thisCase.name, lineNum), func(t *testing.T) { // setup replay test where last message is a proposal cs, newBlockCh, proposalMsg, walFile := setupReplayTest(t, thisCase, lineNum, false) + cs.config.TimeoutPropose = 100 msg := readTimedWALMessage(t, proposalMsg) proposal := msg.Msg.(msgInfo).Msg.(*ProposalMessage) // Set LastSig diff --git a/consensus/wal.go b/consensus/wal.go index 32cce73ff..80f4b809e 100644 --- a/consensus/wal.go +++ b/consensus/wal.go @@ -136,6 +136,7 @@ func (wal *WAL) SearchForEndHeight(height uint64) (gr *auto.GroupReader, found b for { msg, err = dec.Decode() if err == io.EOF { + // check next file break } if err != nil { @@ -147,10 +148,6 @@ func (wal *WAL) SearchForEndHeight(height uint64) (gr *auto.GroupReader, found b if m.Height == height { // found wal.Logger.Debug("Found", "height", height, "index", index) return gr, true, nil - } else if m.Height < height { - // we will never find it because we're starting from the end - gr.Close() - return nil, false, nil } } } @@ -214,8 +211,7 @@ func NewWALDecoder(rd io.Reader) *WALDecoder { return &WALDecoder{rd} } -// Decode reads the next custom-encoded value from its input and stores it in -// the value pointed to by v. +// Decode reads the next custom-encoded value from its reader and returns it. func (dec *WALDecoder) Decode() (*TimedWALMessage, error) { b := make([]byte, 4) diff --git a/consensus/wal_test.go b/consensus/wal_test.go index f032265c3..0235afab3 100644 --- a/consensus/wal_test.go +++ b/consensus/wal_test.go @@ -2,10 +2,13 @@ package consensus import ( "bytes" + "path" "testing" "time" "github.com/tendermint/tendermint/consensus/types" + tmtypes "github.com/tendermint/tendermint/types" + cmn "github.com/tendermint/tmlibs/common" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -35,3 +38,25 @@ func TestWALEncoderDecoder(t *testing.T) { assert.Equal(t, msg.Msg, decoded.Msg) } } + +func TestSearchForEndHeight(t *testing.T) { + wal, err := NewWAL(path.Join(data_dir, "many_blocks.cswal"), false) + if err != nil { + t.Fatal(err) + } + + h := 3 + gr, found, err := wal.SearchForEndHeight(uint64(h)) + assert.NoError(t, err, cmn.Fmt("expected not to err on height %d", h)) + assert.True(t, found, cmn.Fmt("expected to find end height for %d", h)) + assert.NotNil(t, gr, "expected group not to be nil") + defer gr.Close() + + dec := NewWALDecoder(gr) + msg, err := dec.Decode() + assert.NoError(t, err, "expected to decode a message") + rs, ok := msg.Msg.(tmtypes.EventDataRoundState) + assert.True(t, ok, "expected message of type EventDataRoundState") + assert.Equal(t, rs.Height, h+1, cmn.Fmt("wrong height")) + +} diff --git a/scripts/cutWALUntil/main.go b/scripts/cutWALUntil/main.go index 9dde7bf97..a7948a267 100644 --- a/scripts/cutWALUntil/main.go +++ b/scripts/cutWALUntil/main.go @@ -18,7 +18,7 @@ import ( func main() { if len(os.Args) < 4 { - fmt.Println("3 arguments required: height-to-stop ") + fmt.Println("3 arguments required: ") os.Exit(1) } diff --git a/scripts/wal2json/main.go b/scripts/wal2json/main.go index 595660cfd..2cf40c57d 100644 --- a/scripts/wal2json/main.go +++ b/scripts/wal2json/main.go @@ -43,5 +43,8 @@ func main() { os.Stdout.Write(json) os.Stdout.Write([]byte("\n")) + if end, ok := msg.Msg.(cs.EndHeightMessage); ok { + os.Stdout.Write([]byte(fmt.Sprintf("ENDHEIGHT %d\n", end.Height))) + } } }