tendermint/consensus/replay.go

157 lines
4.3 KiB
Go
Raw Normal View History

2015-12-22 22:27:40 -08:00
package consensus
import (
"errors"
"fmt"
"io"
2015-12-22 22:27:40 -08:00
"reflect"
"strconv"
"strings"
"time"
2016-10-28 15:01:14 -07:00
auto "github.com/tendermint/go-autofile"
. "github.com/tendermint/go-common"
"github.com/tendermint/go-wire"
2015-12-22 22:27:40 -08:00
"github.com/tendermint/tendermint/types"
)
2016-10-28 15:01:14 -07:00
// Unmarshal and apply a single message to the consensus state
// as if it were received in receiveRoutine
2016-10-28 15:01:14 -07:00
// Lines that start with "#" are ignored.
// NOTE: receiveRoutine should not be running
2016-01-18 11:10:05 -08:00
func (cs *ConsensusState) readReplayMessage(msgBytes []byte, newStepCh chan interface{}) error {
2016-10-28 15:01:14 -07:00
// Skip over empty and meta lines
if len(msgBytes) == 0 || msgBytes[0] == '#' {
return nil
}
2016-01-18 11:10:05 -08:00
var err error
2016-10-28 15:01:14 -07:00
var msg TimedWALMessage
2016-01-18 11:10:05 -08:00
wire.ReadJSON(&msg, msgBytes, &err)
if err != nil {
2016-02-02 20:05:24 -08:00
fmt.Println("MsgBytes:", msgBytes, string(msgBytes))
2016-01-18 11:10:05 -08:00
return fmt.Errorf("Error reading json data: %v", err)
}
2015-12-22 22:27:40 -08:00
2016-01-18 11:10:05 -08:00
// for logging
switch m := msg.Msg.(type) {
case types.EventDataRoundState:
log.Notice("Replay: New Step", "height", m.Height, "round", m.Round, "step", m.Step)
2016-01-18 11:10:05 -08:00
// these are playback checks
ticker := time.After(time.Second * 2)
if newStepCh != nil {
select {
case mi := <-newStepCh:
m2 := mi.(types.EventDataRoundState)
2016-01-18 11:10:05 -08:00
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.Msg.(type) {
case *ProposalMessage:
p := msg.Proposal
log.Notice("Replay: Proposal", "height", p.Height, "round", p.Round, "header",
2016-01-18 11:10:05 -08:00
p.BlockPartsHeader, "pol", p.POLRound, "peer", peerKey)
case *BlockPartMessage:
log.Notice("Replay: BlockPart", "height", msg.Height, "round", msg.Round, "peer", peerKey)
2016-01-18 11:10:05 -08:00
case *VoteMessage:
v := msg.Vote
log.Notice("Replay: Vote", "height", v.Height, "round", v.Round, "type", v.Type,
2016-08-16 14:59:19 -07:00
"blockID", v.BlockID, "peer", peerKey)
2015-12-22 22:27:40 -08:00
}
cs.handleMsg(m, cs.RoundState)
2016-01-18 11:10:05 -08:00
case timeoutInfo:
log.Notice("Replay: Timeout", "height", m.Height, "round", m.Round, "step", m.Step, "dur", m.Duration)
cs.handleTimeout(m, cs.RoundState)
2016-01-18 11:10:05 -08:00
default:
2016-10-28 15:01:14 -07:00
return fmt.Errorf("Replay: Unknown TimedWALMessage type: %v", reflect.TypeOf(msg.Msg))
2015-12-22 22:27:40 -08:00
}
2016-01-18 11:10:05 -08:00
return nil
2015-12-22 22:27:40 -08:00
}
// replay only those messages since the last block.
// timeoutRoutine should run concurrently to read off tickChan
func (cs *ConsensusState) catchupReplay(csHeight int) error {
// set replayMode
cs.replayMode = true
defer func() { cs.replayMode = false }()
2016-10-28 15:01:14 -07:00
// 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()
}
2016-10-28 15:01:14 -07:00
// Search for height marker
gr, found, err = cs.wal.group.Search("#HEIGHT: ", makeHeightSearchFunc(csHeight))
2016-12-06 01:16:13 -08:00
if err == io.EOF {
log.Warn("Replay: wal.group.Search returned EOF", "height", csHeight)
2016-12-06 01:16:13 -08:00
return nil
} else if err != nil {
return err
}
2016-10-28 15:01:14 -07:00
if !found {
return errors.New(Fmt("WAL does not contain height %d.", csHeight))
}
2016-10-28 15:01:14 -07:00
defer gr.Close()
2016-10-28 15:01:14 -07:00
log.Notice("Catchup by replaying consensus messages", "height", csHeight)
2016-01-18 12:57:57 -08:00
2016-10-28 15:01:14 -07:00
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
2016-10-28 15:01:14 -07:00
if err := cs.readReplayMessage([]byte(line), nil); err != nil {
return err
}
}
2016-10-11 09:51:48 -07:00
log.Notice("Replay: Done")
return nil
2016-01-18 11:10:05 -08:00
}
2016-10-28 15:01:14 -07:00
//--------------------------------------------------------------------------------
// 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
}
}
}