|
|
@ -11,6 +11,7 @@ import ( |
|
|
|
"strings" |
|
|
|
"time" |
|
|
|
|
|
|
|
auto "github.com/tendermint/go-autofile" |
|
|
|
. "github.com/tendermint/go-common" |
|
|
|
"github.com/tendermint/go-wire" |
|
|
|
|
|
|
@ -18,12 +19,17 @@ import ( |
|
|
|
"github.com/tendermint/tendermint/types" |
|
|
|
) |
|
|
|
|
|
|
|
// unmarshal and apply a single message to the consensus state
|
|
|
|
// Unmarshal and apply a single message to the consensus state
|
|
|
|
// as if it were received in receiveRoutine
|
|
|
|
// Lines that start with "#" are ignored.
|
|
|
|
// NOTE: receiveRoutine should not be running
|
|
|
|
func (cs *ConsensusState) readReplayMessage(msgBytes []byte, newStepCh chan interface{}) error { |
|
|
|
// Skip over empty and meta lines
|
|
|
|
if len(msgBytes) == 0 || msgBytes[0] == '#' { |
|
|
|
return nil |
|
|
|
} |
|
|
|
var err error |
|
|
|
var msg ConsensusLogMessage |
|
|
|
var msg TimedWALMessage |
|
|
|
wire.ReadJSON(&msg, msgBytes, &err) |
|
|
|
if err != nil { |
|
|
|
fmt.Println("MsgBytes:", msgBytes, string(msgBytes)) |
|
|
@ -70,7 +76,7 @@ func (cs *ConsensusState) readReplayMessage(msgBytes []byte, newStepCh chan inte |
|
|
|
log.Notice("Replay: Timeout", "height", m.Height, "round", m.Round, "step", m.Step, "dur", m.Duration) |
|
|
|
cs.handleTimeout(m, cs.RoundState) |
|
|
|
default: |
|
|
|
return fmt.Errorf("Replay: Unknown ConsensusLogMessage type: %v", reflect.TypeOf(msg.Msg)) |
|
|
|
return fmt.Errorf("Replay: Unknown TimedWALMessage type: %v", reflect.TypeOf(msg.Msg)) |
|
|
|
} |
|
|
|
return nil |
|
|
|
} |
|
|
@ -78,83 +84,45 @@ func (cs *ConsensusState) readReplayMessage(msgBytes []byte, newStepCh chan inte |
|
|
|
// replay only those messages since the last block.
|
|
|
|
// timeoutRoutine should run concurrently to read off tickChan
|
|
|
|
func (cs *ConsensusState) catchupReplay(csHeight int) error { |
|
|
|
if !cs.wal.Exists() { |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
// set replayMode
|
|
|
|
cs.replayMode = true |
|
|
|
defer func() { cs.replayMode = false }() |
|
|
|
|
|
|
|
// starting from end of file,
|
|
|
|
// read messages until a new height is found
|
|
|
|
var walHeight int |
|
|
|
nLines, err := cs.wal.SeekFromEnd(func(lineBytes []byte) bool { |
|
|
|
var err error |
|
|
|
var msg ConsensusLogMessage |
|
|
|
wire.ReadJSON(&msg, lineBytes, &err) |
|
|
|
if err != nil { |
|
|
|
panic(Fmt("Failed to read cs_msg_log json: %v", err)) |
|
|
|
} |
|
|
|
m, ok := msg.Msg.(types.EventDataRoundState) |
|
|
|
walHeight = m.Height |
|
|
|
if ok && m.Step == RoundStepNewHeight.String() { |
|
|
|
return true |
|
|
|
} |
|
|
|
return false |
|
|
|
}) |
|
|
|
// Ensure that height+1 doesn't exist
|
|
|
|
gr, found, err := cs.wal.group.Search("#HEIGHT: ", makeHeightSearchFunc(csHeight+1)) |
|
|
|
if found { |
|
|
|
return errors.New(Fmt("WAL should not contain height %d.", csHeight+1)) |
|
|
|
} |
|
|
|
if gr != nil { |
|
|
|
gr.Close() |
|
|
|
} |
|
|
|
|
|
|
|
// Search for height marker
|
|
|
|
gr, found, err = cs.wal.group.Search("#HEIGHT: ", makeHeightSearchFunc(csHeight)) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
// ensure the height matches
|
|
|
|
if walHeight != csHeight { |
|
|
|
var err error |
|
|
|
if walHeight > csHeight { |
|
|
|
err = errors.New(Fmt("WAL height (%d) exceeds cs height (%d). Is your cs.state corrupted?", walHeight, csHeight)) |
|
|
|
} else { |
|
|
|
log.Notice("Replay: nothing to do", "cs.height", csHeight, "wal.height", walHeight) |
|
|
|
} |
|
|
|
return err |
|
|
|
if !found { |
|
|
|
return errors.New(Fmt("WAL does not contain height %d.", csHeight)) |
|
|
|
} |
|
|
|
defer gr.Close() |
|
|
|
|
|
|
|
var beginning bool // if we had to go back to the beginning
|
|
|
|
if c, _ := cs.wal.fp.Seek(0, 1); c == 0 { |
|
|
|
beginning = true |
|
|
|
} |
|
|
|
log.Notice("Catchup by replaying consensus messages", "height", csHeight) |
|
|
|
|
|
|
|
log.Notice("Catchup by replaying consensus messages", "n", nLines, "height", walHeight) |
|
|
|
|
|
|
|
// now we can replay the latest nLines on consensus state
|
|
|
|
// note we can't use scan because we've already been reading from the file
|
|
|
|
// XXX: if a msg is too big we need to find out why or increase this for that case ...
|
|
|
|
maxMsgSize := 1000000 |
|
|
|
reader := bufio.NewReaderSize(cs.wal.fp, maxMsgSize) |
|
|
|
for i := 0; i < nLines; i++ { |
|
|
|
msgBytes, err := reader.ReadBytes('\n') |
|
|
|
if err == io.EOF { |
|
|
|
log.Warn("Replay: EOF", "bytes", string(msgBytes)) |
|
|
|
break |
|
|
|
} else if err != nil { |
|
|
|
return err |
|
|
|
} else if len(msgBytes) == 0 { |
|
|
|
log.Warn("Replay: msg bytes is 0") |
|
|
|
continue |
|
|
|
} else if len(msgBytes) == 1 && msgBytes[0] == '\n' { |
|
|
|
log.Warn("Replay: new line") |
|
|
|
continue |
|
|
|
} |
|
|
|
// the first msg is the NewHeight event (if we're not at the beginning), so we can ignore it
|
|
|
|
if !beginning && i == 1 { |
|
|
|
log.Warn("Replay: not beginning and 1") |
|
|
|
continue |
|
|
|
for { |
|
|
|
line, err := gr.ReadLine() |
|
|
|
if err != nil { |
|
|
|
if err == io.EOF { |
|
|
|
break |
|
|
|
} else { |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// NOTE: since the priv key is set when the msgs are received
|
|
|
|
// it will attempt to eg double sign but we can just ignore it
|
|
|
|
// since the votes will be replayed and we'll get to the next step
|
|
|
|
if err := cs.readReplayMessage(msgBytes, nil); err != nil { |
|
|
|
if err := cs.readReplayMessage([]byte(line), nil); err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
@ -245,6 +213,7 @@ func newPlayback(fileName string, fp *os.File, cs *ConsensusState, genState *sm. |
|
|
|
func (pb *playback) replayReset(count int, newStepCh chan interface{}) error { |
|
|
|
|
|
|
|
pb.cs.Stop() |
|
|
|
pb.cs.Wait() |
|
|
|
|
|
|
|
newCS := NewConsensusState(pb.cs.config, pb.genesisState.Copy(), pb.cs.proxyAppConn, pb.cs.blockStore, pb.cs.mempool) |
|
|
|
newCS.SetEventSwitch(pb.cs.evsw) |
|
|
@ -376,3 +345,28 @@ func (pb *playback) replayConsoleLoop() int { |
|
|
|
} |
|
|
|
return 0 |
|
|
|
} |
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Parses marker lines of the form:
|
|
|
|
// #HEIGHT: 12345
|
|
|
|
func makeHeightSearchFunc(height int) auto.SearchFunc { |
|
|
|
return func(line string) (int, error) { |
|
|
|
line = strings.TrimRight(line, "\n") |
|
|
|
parts := strings.Split(line, " ") |
|
|
|
if len(parts) != 2 { |
|
|
|
return -1, errors.New("Line did not have 2 parts") |
|
|
|
} |
|
|
|
i, err := strconv.Atoi(parts[1]) |
|
|
|
if err != nil { |
|
|
|
return -1, errors.New("Failed to parse INFO: " + err.Error()) |
|
|
|
} |
|
|
|
if height < i { |
|
|
|
return 1, nil |
|
|
|
} else if height == i { |
|
|
|
return 0, nil |
|
|
|
} else { |
|
|
|
return -1, nil |
|
|
|
} |
|
|
|
} |
|
|
|
} |