func makeHeightSearchFunc(height agtypes.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.ParseInt(parts[1], 10, 0)
		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
		}
	}
}
--------------------------------------------------------------------------------
Parses marker lines of the form:
#HEIGHT: 12345
package consensus
import (
	"bufio"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"os"
	"reflect"
	"strconv"
	"strings"
	"time"
	"go.uber.org/zap"
	csspb "github.com/Baptist-Publication/angine/protos/consensus"
	sm "github.com/Baptist-Publication/angine/state"
	agtypes "github.com/Baptist-Publication/angine/types"
	auto "github.com/Baptist-Publication/chorus-module/lib/go-autofile"
	. "github.com/Baptist-Publication/chorus-module/lib/go-common"
)
func (cs *ConsensusState) readReplayMessage(msgBytes []byte, newStepCh chan interface{}) error {
	if len(msgBytes) == 0 || msgBytes[0] == '#' {
		return nil
	}
	var err error
	var msg TimedWALMessage
	err = json.Unmarshal(msgBytes, &msg)
	if err != nil {
		fmt.Println("MsgBytes:", string(msgBytes), ",err:", err)
		return fmt.Errorf("Error reading json data: %v", err)
	}
	switch m := msg.GetMsg().(type) {
	case *agtypes.EventDataRoundState:
		cs.logger.Info("Replay: New Step", zap.Int64("height", m.Height), zap.Int64("round", m.Round), zap.String("step", m.Step))
		ticker := time.After(time.Second * 2)
		if newStepCh != nil {
			select {
			case mi := <-newStepCh:
				m2 := mi.(agtypes.EventDataRoundState)
				if m.Height != m2.Height || m.Round != m2.Round || m.Step != m2.Step {
					return fmt.Errorf("RoundState mismatch. Got %v; Expected %v", m2, m)
				}
			case <-ticker:
				return fmt.Errorf("Failed to read off newStepCh")
			}
		}
	case *msgInfo:
		peerKey := m.PeerKey
		if peerKey == "" {
			peerKey = "local"
		}
		switch msg := m.GetMsg().(type) {
		case *csspb.ProposalMessage:
			pdata := msg.Proposal.GetData()
			cs.slogger.Debugw("Replay: Proposal", "height", pdata.Height, "round", pdata.Round, "header",
				pdata.BlockPartsHeader, "pol", pdata.POLRound, "peer", peerKey)
		case *csspb.BlockPartMessage:
			cs.logger.Debug("Replay: BlockPart", zap.Int64("height", msg.Height), zap.Int64("round", msg.Round), zap.String("peer", peerKey))
		case *csspb.VoteMessage:
			v := msg.Vote.Data
			cs.slogger.Debugw("Replay: Vote", "height", v.Height, "round", v.Round, "type", v.Type,
				"blockID", v.BlockID, "peer", peerKey)
		}
		cs.handleMsg(*m, cs.RoundState)
	case *timeoutInfo:
		cs.slogger.Infow("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 TimedWALMessage type: %v", reflect.TypeOf(msg.Msg))
	}
	return nil
}
func (cs *ConsensusState) CatchupReplay(height agtypes.INT) error {
	return cs.catchupReplay(height)
}
func (cs *ConsensusState) catchupReplay(csHeight agtypes.INT) error {
	cs.replayMode = true
	defer func() { cs.replayMode = false }()
	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()
	}
	gr, found, err = cs.wal.group.Search("#HEIGHT: ", makeHeightSearchFunc(csHeight))
	if err == io.EOF {
		cs.logger.Warn("Replay: wal.group.Search returned EOF", zap.Int64("height", csHeight))
		return nil
	} else if err != nil {
		return err
	}
	if !found {
		return errors.New(Fmt("WAL does not contain height %d.", csHeight))
	}
	defer gr.Close()
	cs.logger.Info("Catchup by replaying consensus messages", zap.Int64("height", csHeight))
	for {
		line, err := gr.ReadLine()
		if err != nil {
			if err == io.EOF {
				break
			} else {
				return err
			}
		}
		if err := cs.readReplayMessage([]byte(line), nil); err != nil {
			return err
		}
	}
	cs.logger.Info("Replay: Done")
	return nil
}
func (cs ConsensusState) ReplayConsole(file string) error {
	return cs.replay(file, true)
}
func (cs ConsensusState) ReplayMessages(file string) error {
	return cs.replay(file, false)
}
func (cs *ConsensusState) replay(file string, console bool) error {
	if cs.IsRunning() {
		return errors.New("cs is already running, cannot replay")
	}
	if cs.wal != nil {
		return errors.New("cs wal is open, cannot replay")
	}
	cs.startForReplay()
	newStepCh := subscribeToEvent(cs.evsw, "replay-test", agtypes.EventStringNewRoundStep(), 1)
	fp, err := os.OpenFile(file, os.O_RDONLY, 0666)
	if err != nil {
		return err
	}
	pb := newPlayback(cs.slogger, file, fp, cs, cs.state.Copy())
	defer pb.fp.Close()
	var nextN int  
	for pb.scanner.Scan() {
		if nextN == 0 && console {
			nextN = pb.replayConsoleLoop()
		}
		if err := pb.cs.readReplayMessage(pb.scanner.Bytes(), newStepCh); err != nil {
			return err
		}
		if nextN > 0 {
			nextN -= 1
		}
		pb.count += 1
	}
	return nil
}
type playback struct {
	cs *ConsensusState
	fp      *os.File
	scanner *bufio.Scanner
	count   int  
	fileName     string     
	genesisState *sm.State  
	slogger *zap.SugaredLogger
}
func newPlayback(slogger *zap.SugaredLogger, fileName string, fp *os.File, cs *ConsensusState, genState *sm.State) *playback {
	return &playback{
		cs:           cs,
		fp:           fp,
		fileName:     fileName,
		genesisState: genState,
		scanner:      bufio.NewScanner(fp),
		slogger:      slogger,
	}
}
func (pb *playback) replayReset(count int, newStepCh chan interface{}) error {
	pb.cs.Stop()
	pb.cs.Wait()
	newCS := NewConsensusState(pb.slogger.Desugar(), pb.cs.config, pb.genesisState.Copy(), pb.cs.blockStore, pb.cs.mempool)
	newCS.SetEventSwitch(pb.cs.evsw)
	newCS.startForReplay()
	pb.fp.Close()
	fp, err := os.OpenFile(pb.fileName, os.O_RDONLY, 0666)
	if err != nil {
		return err
	}
	pb.fp = fp
	pb.scanner = bufio.NewScanner(fp)
	count = pb.count - count
	pb.slogger.Infof("Reseting from %d to %d", pb.count, count)
	pb.count = 0
	pb.cs = newCS
	for i := 0; pb.scanner.Scan() && i < count; i++ {
		if err := pb.cs.readReplayMessage(pb.scanner.Bytes(), newStepCh); err != nil {
			return err
		}
		pb.count += 1
	}
	return nil
}
func (cs *ConsensusState) startForReplay() {
	cs.BaseService.OnStart()
	cs.logger.Warn("Replay commands are disabled until someone updates them and writes tests")
}
func (pb *playback) replayConsoleLoop() int {
	for {
		fmt.Printf("> ")
		bufReader := bufio.NewReader(os.Stdin)
		line, more, err := bufReader.ReadLine()
		if more {
			Exit("input is too long")
		} else if err != nil {
			Exit(err.Error())
		}
		tokens := strings.Split(string(line), " ")
		if len(tokens) == 0 {
			continue
		}
		switch tokens[0] {
		case "next":
			if len(tokens) == 1 {
				return 0
			} else {
				i, err := strconv.Atoi(tokens[1])
				if err != nil {
					fmt.Println("next takes an integer argument")
				} else {
					return i
				}
			}
		case "back":
			newStepCh := subscribeToEvent(pb.cs.evsw, "replay-test", agtypes.EventStringNewRoundStep(), 1)
			if len(tokens) == 1 {
				pb.replayReset(1, newStepCh)
			} else {
				i, err := strconv.Atoi(tokens[1])
				if err != nil {
					fmt.Println("back takes an integer argument")
				} else if i > pb.count {
					fmt.Printf("argument to back must not be larger than the current count (%d)\n", pb.count)
				} else {
					pb.replayReset(i, newStepCh)
				}
			}
		case "rs":
			rs := pb.cs.RoundState
			if len(tokens) == 1 {
				fmt.Println(rs)
			} else {
				switch tokens[1] {
				case "short":
					fmt.Printf("%v/%v/%v\n", rs.Height, rs.Round, rs.Step)
				case "validators":
					fmt.Println(rs.Validators)
				case "proposal":
					fmt.Println(rs.Proposal)
				case "proposal_block":
					fmt.Printf("%v %v\n", rs.ProposalBlockParts.StringShort(), rs.ProposalBlock.StringShort())
				case "locked_round":
					fmt.Println(rs.LockedRound)
				case "locked_block":
					fmt.Printf("%v %v\n", rs.LockedBlockParts.StringShort(), rs.LockedBlock.StringShort())
				case "votes":
					fmt.Println(rs.Votes.StringIndented("    "))
				default:
					fmt.Println("Unknown option", tokens[1])
				}
			}
		case "n":
			fmt.Println(pb.count)
		}
	}
	return 0
}