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
}
go back count steps by resetting the state and running (pb.count - count) steps
package consensus
import (
	"bufio"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"os"
	"reflect"
	"strconv"
	"strings"
	"time"
	"go.uber.org/zap"
	csspb "github.com/annchain/annchain/angine/protos/consensus"
	sm "github.com/annchain/annchain/angine/state"
	agtypes "github.com/annchain/annchain/angine/types"
	auto "github.com/annchain/annchain/module/lib/go-autofile"
	. "github.com/annchain/annchain/module/lib/go-common"
	"github.com/annchain/annchain/module/xlib/def"
)
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 def.INT) error {
	return cs.catchupReplay(height)
}
func (cs *ConsensusState) catchupReplay(csHeight def.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,
	}
}