func (enc *WALEncoder) Encode(v *TimedWALMessage) error {
	data := cdc.MustMarshalBinaryBare(v)
	crc := crc32.Checksum(data, crc32c)
	length := uint32(len(data))
	if length > maxMsgSizeBytes {
		return fmt.Errorf("Msg is too big: %d bytes, max: %d bytes", length, maxMsgSizeBytes)
	}
	totalLength := 8 + int(length)
	msg := make([]byte, totalLength)
	binary.BigEndian.PutUint32(msg[0:4], crc)
	binary.BigEndian.PutUint32(msg[4:8], length)
	copy(msg[8:], data)
	_, err := enc.wr.Write(msg)
	return err
}
Encode writes the custom encoding of v to the stream. It returns an error if
the amino-encoded size of v is greater than 1MB. Any error encountered
during the write is also returned.
package consensus
import (
	"encoding/binary"
	"fmt"
	"hash/crc32"
	"io"
	"path/filepath"
	"time"
	"github.com/pkg/errors"
	amino "github.com/tendermint/go-amino"
	auto "github.com/XuanMaoSecLab/DolphinChain/libs/autofile"
	cmn "github.com/XuanMaoSecLab/DolphinChain/libs/common"
	"github.com/XuanMaoSecLab/DolphinChain/libs/log"
	"github.com/XuanMaoSecLab/DolphinChain/types"
	tmtime "github.com/XuanMaoSecLab/DolphinChain/types/time"
)
const (
	maxMsgSizeBytes = 1024 * 1024  
	walDefaultFlushInterval = 2 * time.Second
)
type TimedWALMessage struct {
	Time time.Time  `json:"time"`  
	Msg  WALMessage `json:"msg"`
}
type EndHeightMessage struct {
	Height int64 `json:"height"`
}
type WALMessage interface{}
func RegisterWALMessages(cdc *amino.Codec) {
	cdc.RegisterInterface((*WALMessage)(nil), nil)
	cdc.RegisterConcrete(types.EventDataRoundState{}, "tendermint/wal/EventDataRoundState", nil)
	cdc.RegisterConcrete(msgInfo{}, "tendermint/wal/MsgInfo", nil)
	cdc.RegisterConcrete(timeoutInfo{}, "tendermint/wal/TimeoutInfo", nil)
	cdc.RegisterConcrete(EndHeightMessage{}, "tendermint/wal/EndHeightMessage", nil)
}
type WAL interface {
	Write(WALMessage)
	WriteSync(WALMessage)
	FlushAndSync() error
	SearchForEndHeight(height int64, options *WALSearchOptions) (rd io.ReadCloser, found bool, err error)
	Start() error
	Stop() error
	Wait()
}
type baseWAL struct {
	cmn.BaseService
	group *auto.Group
	enc *WALEncoder
	flushTicker   *time.Ticker
	flushInterval time.Duration
}
var _ WAL = &baseWAL{}
func NewWAL(walFile string, groupOptions ...func(*auto.Group)) (*baseWAL, error) {
	err := cmn.EnsureDir(filepath.Dir(walFile), 0700)
	if err != nil {
		return nil, errors.Wrap(err, "failed to ensure WAL directory is in place")
	}
	group, err := auto.OpenGroup(walFile, groupOptions...)
	if err != nil {
		return nil, err
	}
	wal := &baseWAL{
		group:         group,
		enc:           NewWALEncoder(group),
		flushInterval: walDefaultFlushInterval,
	}
	wal.BaseService = *cmn.NewBaseService(nil, "baseWAL", wal)
	return wal, nil
}
func (wal *baseWAL) SetFlushInterval(i time.Duration) {
	wal.flushInterval = i
}
func (wal *baseWAL) Group() *auto.Group {
	return wal.group
}
func (wal *baseWAL) SetLogger(l log.Logger) {
	wal.BaseService.Logger = l
	wal.group.SetLogger(l)
}
func (wal *baseWAL) OnStart() error {
	size, err := wal.group.Head.Size()
	if err != nil {
		return err
	} else if size == 0 {
		wal.WriteSync(EndHeightMessage{0})
	}
	err = wal.group.Start()
	if err != nil {
		return err
	}
	wal.flushTicker = time.NewTicker(wal.flushInterval)
	go wal.processFlushTicks()
	return nil
}
func (wal *baseWAL) processFlushTicks() {
	for {
		select {
		case <-wal.flushTicker.C:
			if err := wal.FlushAndSync(); err != nil {
				wal.Logger.Error("Periodic WAL flush failed", "err", err)
			}
		case <-wal.Quit():
			return
		}
	}
}
func (wal *baseWAL) FlushAndSync() error {
	return wal.group.FlushAndSync()
}
func (wal *baseWAL) OnStop() {
	wal.flushTicker.Stop()
	wal.FlushAndSync()
	wal.group.Stop()
	wal.group.Close()
}
func (wal *baseWAL) Wait() {
	wal.group.Wait()
}
func (wal *baseWAL) Write(msg WALMessage) {
	if wal == nil {
		return
	}
	if err := wal.enc.Encode(&TimedWALMessage{tmtime.Now(), msg}); err != nil {
		panic(fmt.Sprintf("Error writing msg to consensus wal: %v \n\nMessage: %v", err, msg))
	}
}
func (wal *baseWAL) WriteSync(msg WALMessage) {
	if wal == nil {
		return
	}
	wal.Write(msg)
	if err := wal.FlushAndSync(); err != nil {
		panic(fmt.Sprintf("Error flushing consensus wal buf to file. Error: %v \n", err))
	}
}
type WALSearchOptions struct {
	IgnoreDataCorruptionErrors bool
}
func (wal *baseWAL) SearchForEndHeight(height int64, options *WALSearchOptions) (rd io.ReadCloser, found bool, err error) {
	var (
		msg *TimedWALMessage
		gr  *auto.GroupReader
	)
	lastHeightFound := int64(-1)
	min, max := wal.group.MinIndex(), wal.group.MaxIndex()
	wal.Logger.Info("Searching for height", "height", height, "min", min, "max", max)
	for index := max; index >= min; index-- {
		gr, err = wal.group.NewReader(index)
		if err != nil {
			return nil, false, err
		}
		dec := NewWALDecoder(gr)
		for {
			msg, err = dec.Decode()
			if err == io.EOF {
				if lastHeightFound > 0 && lastHeightFound < height {
					gr.Close()
					return nil, false, nil
				}
				break
			}
			if options.IgnoreDataCorruptionErrors && IsDataCorruptionError(err) {
				wal.Logger.Error("Corrupted entry. Skipping...", "err", err)
				continue
			} else if err != nil {
				gr.Close()
				return nil, false, err
			}
			if m, ok := msg.Msg.(EndHeightMessage); ok {
				lastHeightFound = m.Height
				if m.Height == height {  
					wal.Logger.Info("Found", "height", height, "index", index)
					return gr, true, nil
				}
			}
		}
		gr.Close()
	}
	return nil, false, nil
}
type WALEncoder struct {
	wr io.Writer
}
func NewWALEncoder(wr io.Writer) *WALEncoder {
	return &WALEncoder{wr}
}