2015-03-25 00:15:18 -07:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
2018-11-01 07:07:18 +01:00
|
|
|
"errors"
|
2018-01-21 13:32:04 -05:00
|
|
|
"fmt"
|
2015-04-16 17:46:27 -07:00
|
|
|
"reflect"
|
2015-03-25 00:15:18 -07:00
|
|
|
"time"
|
|
|
|
|
2018-07-09 13:01:23 +04:00
|
|
|
amino "github.com/tendermint/go-amino"
|
|
|
|
|
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2017-04-08 22:04:06 -04:00
|
|
|
"github.com/tendermint/tendermint/p2p"
|
2015-04-01 17:30:16 -07:00
|
|
|
sm "github.com/tendermint/tendermint/state"
|
2019-04-13 09:23:43 -04:00
|
|
|
|
2015-04-01 17:30:16 -07:00
|
|
|
"github.com/tendermint/tendermint/types"
|
2015-03-25 00:15:18 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-01-17 20:58:27 +04:00
|
|
|
// BlockchainChannel is a channel for blocks and status updates (`BlockStore` height)
|
|
|
|
BlockchainChannel = byte(0x40)
|
2018-06-21 01:57:35 -07:00
|
|
|
trySyncIntervalMS = 10
|
2019-04-13 09:23:43 -04:00
|
|
|
trySendIntervalMS = 10
|
2018-06-21 01:57:35 -07:00
|
|
|
|
2015-03-25 11:33:39 -07:00
|
|
|
// stop syncing when last block's time is
|
|
|
|
// within this much of the system time.
|
2015-04-23 14:59:12 -07:00
|
|
|
// stopSyncingDurationMinutes = 10
|
|
|
|
|
2015-04-21 19:51:23 -07:00
|
|
|
// ask for best height every 10s
|
|
|
|
statusUpdateIntervalSeconds = 10
|
|
|
|
// check if we should switch to consensus reactor
|
2015-08-18 10:51:55 -07:00
|
|
|
switchToConsensusIntervalSeconds = 1
|
2018-04-06 13:46:40 -07:00
|
|
|
|
|
|
|
// NOTE: keep up to date with bcBlockResponseMessage
|
|
|
|
bcBlockResponseMessagePrefixSize = 4
|
|
|
|
bcBlockResponseMessageFieldKeySize = 1
|
2018-04-09 15:14:33 +03:00
|
|
|
maxMsgSize = types.MaxBlockSizeBytes +
|
2018-04-06 13:46:40 -07:00
|
|
|
bcBlockResponseMessagePrefixSize +
|
|
|
|
bcBlockResponseMessageFieldKeySize
|
2015-03-25 00:15:18 -07:00
|
|
|
)
|
|
|
|
|
2019-04-13 09:23:43 -04:00
|
|
|
var (
|
|
|
|
maxRequestsPerPeer int32 = 20
|
|
|
|
maxNumPendingRequests int32 = 600
|
|
|
|
)
|
|
|
|
|
2015-04-21 19:51:23 -07:00
|
|
|
type consensusReactor interface {
|
2015-05-27 22:06:33 -04:00
|
|
|
// for when we switch from blockchain reactor and fast sync to
|
|
|
|
// the consensus machine
|
2017-12-27 20:40:36 -05:00
|
|
|
SwitchToConsensus(sm.State, int)
|
2015-03-25 13:17:45 -07:00
|
|
|
}
|
|
|
|
|
2018-02-26 17:35:01 +04:00
|
|
|
type peerError struct {
|
|
|
|
err error
|
|
|
|
peerID p2p.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e peerError) Error() string {
|
|
|
|
return fmt.Sprintf("error with peer %v: %s", e.peerID, e.err.Error())
|
|
|
|
}
|
|
|
|
|
2015-03-25 00:15:18 -07:00
|
|
|
// BlockchainReactor handles long-term catchup syncing.
|
|
|
|
type BlockchainReactor struct {
|
2015-07-20 14:40:41 -07:00
|
|
|
p2p.BaseReactor
|
|
|
|
|
2017-12-27 20:40:36 -05:00
|
|
|
// immutable
|
|
|
|
initialState sm.State
|
2019-04-13 09:23:43 -04:00
|
|
|
state sm.State
|
2017-12-27 20:40:36 -05:00
|
|
|
|
2018-01-03 11:57:42 +01:00
|
|
|
blockExec *sm.BlockExecutor
|
|
|
|
store *BlockStore
|
|
|
|
|
2019-04-13 09:23:43 -04:00
|
|
|
fastSync bool
|
|
|
|
|
|
|
|
fsm *bReactorFSM
|
|
|
|
blocksSynced int
|
|
|
|
|
|
|
|
// Receive goroutine forwards messages to this channel to be processed in the context of the poolRoutine.
|
|
|
|
messagesForFSMCh chan bReactorMessageData
|
|
|
|
|
|
|
|
// Switch goroutine may send RemovePeer to the blockchain reactor. This is an error message that is relayed
|
|
|
|
// to this channel to be processed in the context of the poolRoutine.
|
|
|
|
errorsForFSMCh chan bReactorMessageData
|
|
|
|
|
|
|
|
// This channel is used by the FSM and indirectly the block pool to report errors to the blockchain reactor and
|
|
|
|
// the switch.
|
|
|
|
errorsFromFSMCh chan peerError
|
|
|
|
}
|
|
|
|
|
|
|
|
type BlockRequest struct {
|
|
|
|
Height int64
|
|
|
|
PeerID p2p.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// bReactorMessageData structure is used by the reactor when sending messages to the FSM.
|
|
|
|
type bReactorMessageData struct {
|
|
|
|
event bReactorEvent
|
|
|
|
data bReactorEventData
|
2015-03-25 00:15:18 -07:00
|
|
|
}
|
|
|
|
|
2017-01-17 20:58:27 +04:00
|
|
|
// NewBlockchainReactor returns new reactor instance.
|
2018-01-03 11:29:19 +01:00
|
|
|
func NewBlockchainReactor(state sm.State, blockExec *sm.BlockExecutor, store *BlockStore,
|
|
|
|
fastSync bool) *BlockchainReactor {
|
|
|
|
|
2015-12-07 16:57:33 -08:00
|
|
|
if state.LastBlockHeight != store.Height() {
|
2018-03-04 14:58:43 +04:00
|
|
|
panic(fmt.Sprintf("state (%v) and store (%v) height mismatch", state.LastBlockHeight,
|
2018-01-03 11:29:19 +01:00
|
|
|
store.Height()))
|
2015-03-25 11:33:39 -07:00
|
|
|
}
|
2017-12-27 20:40:36 -05:00
|
|
|
|
2019-04-13 09:23:43 -04:00
|
|
|
const capacity = 1000
|
|
|
|
errorsFromFSMCh := make(chan peerError, capacity)
|
|
|
|
messagesForFSMCh := make(chan bReactorMessageData, capacity)
|
|
|
|
errorsForFSMCh := make(chan bReactorMessageData, capacity)
|
2018-03-04 14:58:43 +04:00
|
|
|
|
2015-03-25 00:15:18 -07:00
|
|
|
bcR := &BlockchainReactor{
|
2019-04-13 09:23:43 -04:00
|
|
|
initialState: state,
|
|
|
|
state: state,
|
|
|
|
blockExec: blockExec,
|
|
|
|
fastSync: fastSync,
|
|
|
|
store: store,
|
|
|
|
messagesForFSMCh: messagesForFSMCh,
|
|
|
|
errorsFromFSMCh: errorsFromFSMCh,
|
|
|
|
errorsForFSMCh: errorsForFSMCh,
|
2015-03-25 00:15:18 -07:00
|
|
|
}
|
2019-04-13 09:23:43 -04:00
|
|
|
fsm := NewFSM(store.Height()+1, bcR)
|
|
|
|
bcR.fsm = fsm
|
2017-05-02 11:53:32 +04:00
|
|
|
bcR.BaseReactor = *p2p.NewBaseReactor("BlockchainReactor", bcR)
|
2015-03-25 00:15:18 -07:00
|
|
|
return bcR
|
|
|
|
}
|
|
|
|
|
2017-10-20 23:56:21 +04:00
|
|
|
// SetLogger implements cmn.Service by setting the logger on reactor and pool.
|
|
|
|
func (bcR *BlockchainReactor) SetLogger(l log.Logger) {
|
|
|
|
bcR.BaseService.Logger = l
|
2019-04-13 09:23:43 -04:00
|
|
|
bcR.fsm.setLogger(l)
|
2017-10-20 23:56:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnStart implements cmn.Service.
|
2015-08-04 18:44:15 -07:00
|
|
|
func (bcR *BlockchainReactor) OnStart() error {
|
2016-03-24 18:08:18 -07:00
|
|
|
if bcR.fastSync {
|
2015-07-20 14:40:41 -07:00
|
|
|
go bcR.poolRoutine()
|
2015-03-25 00:15:18 -07:00
|
|
|
}
|
2015-08-04 18:44:15 -07:00
|
|
|
return nil
|
2015-03-25 00:15:18 -07:00
|
|
|
}
|
|
|
|
|
2017-10-20 23:56:21 +04:00
|
|
|
// OnStop implements cmn.Service.
|
2015-07-21 18:31:01 -07:00
|
|
|
func (bcR *BlockchainReactor) OnStop() {
|
2019-04-13 09:23:43 -04:00
|
|
|
_ = bcR.Stop()
|
2015-03-25 00:15:18 -07:00
|
|
|
}
|
|
|
|
|
2017-01-17 20:58:27 +04:00
|
|
|
// GetChannels implements Reactor
|
2015-03-25 00:15:18 -07:00
|
|
|
func (bcR *BlockchainReactor) GetChannels() []*p2p.ChannelDescriptor {
|
|
|
|
return []*p2p.ChannelDescriptor{
|
2017-09-05 16:37:20 -04:00
|
|
|
{
|
2018-04-06 13:46:40 -07:00
|
|
|
ID: BlockchainChannel,
|
|
|
|
Priority: 10,
|
|
|
|
SendQueueCapacity: 1000,
|
|
|
|
RecvBufferCapacity: 50 * 4096,
|
2018-04-09 15:14:33 +03:00
|
|
|
RecvMessageCapacity: maxMsgSize,
|
2015-03-25 00:15:18 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 20:58:27 +04:00
|
|
|
// AddPeer implements Reactor by sending our state to peer.
|
2017-09-12 20:49:22 -04:00
|
|
|
func (bcR *BlockchainReactor) AddPeer(peer p2p.Peer) {
|
2018-04-03 07:03:08 -07:00
|
|
|
msgBytes := cdc.MustMarshalBinaryBare(&bcStatusResponseMessage{bcR.store.Height()})
|
|
|
|
if !peer.Send(BlockchainChannel, msgBytes) {
|
2017-01-17 20:58:27 +04:00
|
|
|
// doing nothing, will try later in `poolRoutine`
|
|
|
|
}
|
2017-11-08 02:42:27 +00:00
|
|
|
// peer is added to the pool once we receive the first
|
|
|
|
// bcStatusResponseMessage from the peer and call pool.SetPeerHeight
|
2015-03-25 00:15:18 -07:00
|
|
|
}
|
|
|
|
|
2017-08-26 02:33:19 -06:00
|
|
|
// respondToPeer loads a block and sends it to the requesting peer,
|
|
|
|
// if we have it. Otherwise, we'll respond saying we don't have it.
|
|
|
|
// According to the Tendermint spec, if all nodes are honest,
|
|
|
|
// no node should be requesting for a block that's non-existent.
|
2019-04-13 09:23:43 -04:00
|
|
|
func (bcR *BlockchainReactor) sendBlockToPeer(msg *bcBlockRequestMessage,
|
2018-01-03 11:29:19 +01:00
|
|
|
src p2p.Peer) (queued bool) {
|
|
|
|
|
2017-08-26 02:33:19 -06:00
|
|
|
block := bcR.store.LoadBlock(msg.Height)
|
|
|
|
if block != nil {
|
2018-04-03 07:03:08 -07:00
|
|
|
msgBytes := cdc.MustMarshalBinaryBare(&bcBlockResponseMessage{Block: block})
|
|
|
|
return src.TrySend(BlockchainChannel, msgBytes)
|
2017-08-26 02:33:19 -06:00
|
|
|
}
|
|
|
|
|
2019-04-13 09:23:43 -04:00
|
|
|
bcR.Logger.Info("peer asking for a block we don't have", "src", src, "height", msg.Height)
|
2017-08-26 02:33:19 -06:00
|
|
|
|
2018-04-03 07:03:08 -07:00
|
|
|
msgBytes := cdc.MustMarshalBinaryBare(&bcNoBlockResponseMessage{Height: msg.Height})
|
|
|
|
return src.TrySend(BlockchainChannel, msgBytes)
|
2017-08-26 02:33:19 -06:00
|
|
|
}
|
|
|
|
|
2019-04-13 09:23:43 -04:00
|
|
|
func (bcR *BlockchainReactor) sendStatusResponseToPeer(msg *bcStatusRequestMessage, src p2p.Peer) (queued bool) {
|
|
|
|
msgBytes := cdc.MustMarshalBinaryBare(&bcStatusResponseMessage{bcR.store.Height()})
|
|
|
|
return src.TrySend(BlockchainChannel, msgBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bcR *BlockchainReactor) sendMessageToFSMAsync(msg bReactorMessageData) {
|
|
|
|
bcR.Logger.Error("send message to FSM for processing", "msg", msg.String())
|
|
|
|
bcR.messagesForFSMCh <- msg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bcR *BlockchainReactor) sendRemovePeerToFSM(peerID p2p.ID) {
|
|
|
|
msgData := bReactorMessageData{
|
|
|
|
event: peerRemoveEv,
|
|
|
|
data: bReactorEventData{
|
|
|
|
peerId: peerID,
|
|
|
|
err: errSwitchRemovesPeer,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
bcR.sendMessageToFSMAsync(msgData)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemovePeer implements Reactor by removing peer from the pool.
|
|
|
|
func (bcR *BlockchainReactor) RemovePeer(peer p2p.Peer, reason interface{}) {
|
|
|
|
bcR.sendRemovePeerToFSM(peer.ID())
|
|
|
|
}
|
|
|
|
|
2017-01-17 20:58:27 +04:00
|
|
|
// Receive implements Reactor by handling 4 types of messages (look below).
|
2017-09-12 20:49:22 -04:00
|
|
|
func (bcR *BlockchainReactor) Receive(chID byte, src p2p.Peer, msgBytes []byte) {
|
2018-07-09 13:01:23 +04:00
|
|
|
msg, err := decodeMsg(msgBytes)
|
2015-03-25 00:15:18 -07:00
|
|
|
if err != nil {
|
2019-04-13 09:23:43 -04:00
|
|
|
bcR.Logger.Error("error decoding message", "src", src, "chId", chID, "msg", msg, "err", err, "bytes", msgBytes)
|
2018-03-04 13:42:45 +04:00
|
|
|
bcR.Switch.StopPeerForError(src, err)
|
2015-03-25 00:15:18 -07:00
|
|
|
return
|
|
|
|
}
|
2015-03-25 17:16:49 -07:00
|
|
|
|
2018-11-01 07:07:18 +01:00
|
|
|
if err = msg.ValidateBasic(); err != nil {
|
2019-04-13 09:23:43 -04:00
|
|
|
bcR.Logger.Error("peer sent us invalid msg", "peer", src, "msg", msg, "err", err)
|
2018-11-01 07:07:18 +01:00
|
|
|
bcR.Switch.StopPeerForError(src, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-02 11:53:32 +04:00
|
|
|
bcR.Logger.Debug("Receive", "src", src, "chID", chID, "msg", msg)
|
2015-03-25 00:15:18 -07:00
|
|
|
|
2015-04-14 15:57:16 -07:00
|
|
|
switch msg := msg.(type) {
|
2015-04-16 17:46:27 -07:00
|
|
|
case *bcBlockRequestMessage:
|
2019-04-13 09:23:43 -04:00
|
|
|
if queued := bcR.sendBlockToPeer(msg, src); !queued {
|
2017-08-26 02:33:19 -06:00
|
|
|
// Unfortunately not queued since the queue is full.
|
2019-04-13 09:23:43 -04:00
|
|
|
bcR.Logger.Error("Could not send block message to peer", "src", src, "height", msg.Height)
|
2015-03-25 00:15:18 -07:00
|
|
|
}
|
2019-04-13 09:23:43 -04:00
|
|
|
|
2015-04-21 19:51:23 -07:00
|
|
|
case *bcStatusRequestMessage:
|
|
|
|
// Send peer our state.
|
2019-04-13 09:23:43 -04:00
|
|
|
if queued := bcR.sendStatusResponseToPeer(msg, src); !queued {
|
|
|
|
// Unfortunately not queued since the queue is full.
|
|
|
|
bcR.Logger.Error("Could not send status message to peer", "src", src)
|
2015-04-21 19:51:23 -07:00
|
|
|
}
|
2019-04-13 09:23:43 -04:00
|
|
|
|
|
|
|
case *bcBlockResponseMessage:
|
|
|
|
msgData := bReactorMessageData{
|
|
|
|
event: blockResponseEv,
|
|
|
|
data: bReactorEventData{
|
|
|
|
peerId: src.ID(),
|
|
|
|
height: msg.Block.Height,
|
|
|
|
block: msg.Block,
|
|
|
|
length: len(msgBytes),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
bcR.sendMessageToFSMAsync(msgData)
|
|
|
|
|
2015-04-21 19:51:23 -07:00
|
|
|
case *bcStatusResponseMessage:
|
|
|
|
// Got a peer status. Unverified.
|
2019-04-13 09:23:43 -04:00
|
|
|
msgData := bReactorMessageData{
|
|
|
|
event: statusResponseEv,
|
|
|
|
data: bReactorEventData{
|
|
|
|
peerId: src.ID(),
|
|
|
|
height: msg.Height,
|
|
|
|
length: len(msgBytes),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
bcR.sendMessageToFSMAsync(msgData)
|
|
|
|
|
2015-03-25 00:15:18 -07:00
|
|
|
default:
|
2019-04-13 09:23:43 -04:00
|
|
|
bcR.Logger.Error(fmt.Sprintf("unknown message type %v", reflect.TypeOf(msg)))
|
2015-03-25 00:15:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 11:33:39 -07:00
|
|
|
// Handle messages from the poolReactor telling the reactor what to do.
|
2015-05-28 02:18:53 -07:00
|
|
|
// NOTE: Don't sleep in the FOR_LOOP or otherwise slow it down!
|
2015-03-25 00:15:18 -07:00
|
|
|
func (bcR *BlockchainReactor) poolRoutine() {
|
2015-03-25 11:33:39 -07:00
|
|
|
|
2019-04-13 09:23:43 -04:00
|
|
|
bcR.fsm.start()
|
|
|
|
|
2015-03-25 11:33:39 -07:00
|
|
|
trySyncTicker := time.NewTicker(trySyncIntervalMS * time.Millisecond)
|
2019-04-13 09:23:43 -04:00
|
|
|
trySendTicker := time.NewTicker(trySendIntervalMS * time.Millisecond)
|
|
|
|
|
2015-04-21 19:51:23 -07:00
|
|
|
statusUpdateTicker := time.NewTicker(statusUpdateIntervalSeconds * time.Second)
|
|
|
|
switchToConsensusTicker := time.NewTicker(switchToConsensusIntervalSeconds * time.Second)
|
2015-03-25 11:33:39 -07:00
|
|
|
|
2017-10-26 18:29:23 -04:00
|
|
|
lastHundred := time.Now()
|
|
|
|
lastRate := 0.0
|
|
|
|
|
2019-04-13 09:23:43 -04:00
|
|
|
doProcessCh := make(chan struct{}, 1)
|
|
|
|
doSendCh := make(chan struct{}, 1)
|
2018-06-21 01:57:35 -07:00
|
|
|
|
2019-04-13 09:23:43 -04:00
|
|
|
ForLoop:
|
2015-03-25 00:15:18 -07:00
|
|
|
for {
|
|
|
|
select {
|
2019-04-13 09:23:43 -04:00
|
|
|
|
|
|
|
case <-trySendTicker.C: // chan time
|
|
|
|
select {
|
|
|
|
case doSendCh <- struct{}{}:
|
|
|
|
default:
|
2015-03-25 00:15:18 -07:00
|
|
|
}
|
2018-06-21 01:57:35 -07:00
|
|
|
|
2019-04-13 09:23:43 -04:00
|
|
|
case <-doSendCh:
|
|
|
|
// Tell FSM to make more requests.
|
|
|
|
// The maxNumPendingRequests may be changed based on low/ high watermark thresholds for
|
|
|
|
// - the number of blocks received and waiting to be processed,
|
|
|
|
// - the number of blockResponse messages waiting in messagesForFSMCh, etc.
|
|
|
|
// Currently maxNumPendingRequests value is not changed.
|
|
|
|
msgData := bReactorMessageData{
|
|
|
|
event: makeRequestsEv,
|
|
|
|
data: bReactorEventData{
|
|
|
|
maxNumRequests: maxNumPendingRequests,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_ = sendMessageToFSMSync(bcR.fsm, msgData)
|
|
|
|
|
|
|
|
case msg := <-bcR.errorsFromFSMCh:
|
|
|
|
bcR.reportPeerErrorToSwitch(msg.err, msg.peerID)
|
|
|
|
if msg.err == errNoPeerResponse {
|
|
|
|
msgData := bReactorMessageData{
|
|
|
|
event: peerRemoveEv,
|
|
|
|
data: bReactorEventData{
|
|
|
|
peerId: msg.peerID,
|
|
|
|
err: msg.err,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_ = sendMessageToFSMSync(bcR.fsm, msgData)
|
2015-03-25 12:21:52 -07:00
|
|
|
}
|
2018-06-21 01:57:35 -07:00
|
|
|
|
2017-05-29 23:11:40 -04:00
|
|
|
case <-statusUpdateTicker.C:
|
2019-04-13 09:23:43 -04:00
|
|
|
// Ask for status updates.
|
|
|
|
go bcR.sendStatusRequest()
|
2018-06-21 01:57:35 -07:00
|
|
|
|
2017-05-29 23:11:40 -04:00
|
|
|
case <-switchToConsensusTicker.C:
|
2019-04-13 09:23:43 -04:00
|
|
|
height, numPending, maxPeerHeight := bcR.fsm.pool.getStatus()
|
2015-07-20 14:40:41 -07:00
|
|
|
outbound, inbound, _ := bcR.Switch.NumPeers()
|
2019-04-13 09:23:43 -04:00
|
|
|
bcR.Logger.Debug("Consensus ticker", "numPending", numPending, "maxPeerHeight", maxPeerHeight,
|
2015-08-18 10:51:55 -07:00
|
|
|
"outbound", outbound, "inbound", inbound)
|
2019-04-13 09:23:43 -04:00
|
|
|
if bcR.fsm.isCaughtUp() {
|
2017-05-02 11:53:32 +04:00
|
|
|
bcR.Logger.Info("Time to switch to consensus reactor!", "height", height)
|
2019-04-13 09:23:43 -04:00
|
|
|
bcR.fsm.stop()
|
|
|
|
bcR.switchToConsensus()
|
|
|
|
break ForLoop
|
2015-04-21 19:51:23 -07:00
|
|
|
}
|
2018-06-21 01:57:35 -07:00
|
|
|
|
2017-05-29 23:11:40 -04:00
|
|
|
case <-trySyncTicker.C: // chan time
|
2018-06-21 01:57:35 -07:00
|
|
|
select {
|
2019-04-13 09:23:43 -04:00
|
|
|
case doProcessCh <- struct{}{}:
|
2018-06-21 01:57:35 -07:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2019-04-13 09:23:43 -04:00
|
|
|
case <-doProcessCh:
|
|
|
|
err := bcR.processBlocksFromPoolRoutine()
|
|
|
|
if err == errMissingBlocks {
|
|
|
|
continue ForLoop
|
2018-06-21 01:57:35 -07:00
|
|
|
}
|
|
|
|
|
2019-04-13 09:23:43 -04:00
|
|
|
// Notify FSM of block processing result.
|
|
|
|
msgData := bReactorMessageData{
|
|
|
|
event: processedBlockEv,
|
|
|
|
data: bReactorEventData{
|
|
|
|
err: err,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_ = sendMessageToFSMSync(bcR.fsm, msgData)
|
2018-06-21 01:57:35 -07:00
|
|
|
|
2019-04-13 09:23:43 -04:00
|
|
|
if err == errBlockVerificationFailure {
|
|
|
|
continue ForLoop
|
2015-03-25 11:33:39 -07:00
|
|
|
}
|
2019-04-13 09:23:43 -04:00
|
|
|
|
|
|
|
doProcessCh <- struct{}{}
|
|
|
|
|
|
|
|
bcR.blocksSynced++
|
|
|
|
if bcR.blocksSynced%100 == 0 {
|
|
|
|
lastRate = 0.9*lastRate + 0.1*(100/time.Since(lastHundred).Seconds())
|
|
|
|
bcR.Logger.Info("Fast Sync Rate", "height", bcR.fsm.pool.height,
|
|
|
|
"max_peer_height", bcR.fsm.pool.getMaxPeerHeight(), "blocks/s", lastRate)
|
|
|
|
lastHundred = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
case msg := <-bcR.messagesForFSMCh:
|
|
|
|
_ = sendMessageToFSMSync(bcR.fsm, msg)
|
|
|
|
|
|
|
|
case msg := <-bcR.errorsForFSMCh:
|
|
|
|
_ = sendMessageToFSMSync(bcR.fsm, msg)
|
2018-06-21 01:57:35 -07:00
|
|
|
|
2018-02-12 14:31:52 +04:00
|
|
|
case <-bcR.Quit():
|
2019-04-13 09:23:43 -04:00
|
|
|
break ForLoop
|
2015-03-25 00:15:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 09:23:43 -04:00
|
|
|
func (bcR *BlockchainReactor) reportPeerErrorToSwitch(err error, peerID p2p.ID) {
|
|
|
|
peer := bcR.Switch.Peers().Get(peerID)
|
|
|
|
if peer != nil {
|
|
|
|
bcR.Switch.StopPeerForError(peer, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called by FSM and pool:
|
|
|
|
// - pool calls when it detects slow peer or when peer times out
|
|
|
|
// - FSM calls when:
|
|
|
|
// - processing a block (addBlock) fails
|
|
|
|
// - BCR process of block reports failure to FSM, FSM sends back the peers of first and second
|
|
|
|
func (bcR *BlockchainReactor) sendPeerError(err error, peerID p2p.ID) {
|
|
|
|
bcR.errorsFromFSMCh <- peerError{err, peerID}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bcR *BlockchainReactor) processBlocksFromPoolRoutine() error {
|
|
|
|
firstBP, secondBP, err := bcR.fsm.pool.getNextTwoBlocks()
|
|
|
|
if err != nil {
|
|
|
|
// We need both to sync the first block.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
first := firstBP.block
|
|
|
|
second := secondBP.block
|
|
|
|
|
|
|
|
chainID := bcR.initialState.ChainID
|
|
|
|
|
|
|
|
firstParts := first.MakePartSet(types.BlockPartSizeBytes)
|
|
|
|
firstPartsHeader := firstParts.Header()
|
|
|
|
firstID := types.BlockID{Hash: first.Hash(), PartsHeader: firstPartsHeader}
|
|
|
|
// Finally, verify the first block using the second's commit
|
|
|
|
// NOTE: we can probably make this more efficient, but note that calling
|
|
|
|
// first.Hash() doesn't verify the tx contents, so MakePartSet() is
|
|
|
|
// currently necessary.
|
|
|
|
err = bcR.state.Validators.VerifyCommit(
|
|
|
|
chainID, firstID, first.Height, second.LastCommit)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
bcR.Logger.Error("error in validation", "err", err, first.Height, second.Height)
|
|
|
|
return errBlockVerificationFailure
|
|
|
|
}
|
|
|
|
|
|
|
|
bcR.store.SaveBlock(first, firstParts, second.LastCommit)
|
|
|
|
|
|
|
|
// Get the hash without persisting the state.
|
|
|
|
bcR.state, err = bcR.blockExec.ApplyBlock(bcR.state, firstID, first)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to process committed block (%d:%X): %v", first.Height, first.Hash(), err))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bcR *BlockchainReactor) resetStateTimer(name string, timer *time.Timer, timeout time.Duration, f func()) {
|
|
|
|
if timer == nil {
|
|
|
|
timer = time.AfterFunc(timeout, f)
|
|
|
|
} else {
|
|
|
|
timer.Reset(timeout)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 20:58:27 +04:00
|
|
|
// BroadcastStatusRequest broadcasts `BlockStore` height.
|
2019-04-13 09:23:43 -04:00
|
|
|
func (bcR *BlockchainReactor) sendStatusRequest() {
|
2018-04-03 07:03:08 -07:00
|
|
|
msgBytes := cdc.MustMarshalBinaryBare(&bcStatusRequestMessage{bcR.store.Height()})
|
|
|
|
bcR.Switch.Broadcast(BlockchainChannel, msgBytes)
|
2019-04-13 09:23:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// BlockRequest sends `BlockRequest` height.
|
|
|
|
func (bcR *BlockchainReactor) sendBlockRequest(peerID p2p.ID, height int64) error {
|
|
|
|
peer := bcR.Switch.Peers().Get(peerID)
|
|
|
|
if peer == nil {
|
|
|
|
return errNilPeerForBlockRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
msgBytes := cdc.MustMarshalBinaryBare(&bcBlockRequestMessage{height})
|
|
|
|
queued := peer.TrySend(BlockchainChannel, msgBytes)
|
|
|
|
if !queued {
|
|
|
|
return errSendQueueFull
|
|
|
|
}
|
2015-03-25 00:15:18 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-13 09:23:43 -04:00
|
|
|
func (bcR *BlockchainReactor) switchToConsensus() {
|
|
|
|
|
|
|
|
conR, ok := bcR.Switch.Reactor("CONSENSUS").(consensusReactor)
|
|
|
|
if ok {
|
|
|
|
conR.SwitchToConsensus(bcR.state, bcR.blocksSynced)
|
|
|
|
} else {
|
|
|
|
// Should only happen during testing.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 00:15:18 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Messages
|
|
|
|
|
2017-01-17 20:58:27 +04:00
|
|
|
// BlockchainMessage is a generic message for this reactor.
|
2018-11-01 07:07:18 +01:00
|
|
|
type BlockchainMessage interface {
|
|
|
|
ValidateBasic() error
|
|
|
|
}
|
2015-04-14 15:57:16 -07:00
|
|
|
|
2018-04-03 07:03:08 -07:00
|
|
|
func RegisterBlockchainMessages(cdc *amino.Codec) {
|
|
|
|
cdc.RegisterInterface((*BlockchainMessage)(nil), nil)
|
2018-08-13 21:40:49 +08:00
|
|
|
cdc.RegisterConcrete(&bcBlockRequestMessage{}, "tendermint/blockchain/BlockRequest", nil)
|
|
|
|
cdc.RegisterConcrete(&bcBlockResponseMessage{}, "tendermint/blockchain/BlockResponse", nil)
|
|
|
|
cdc.RegisterConcrete(&bcNoBlockResponseMessage{}, "tendermint/blockchain/NoBlockResponse", nil)
|
|
|
|
cdc.RegisterConcrete(&bcStatusResponseMessage{}, "tendermint/blockchain/StatusResponse", nil)
|
|
|
|
cdc.RegisterConcrete(&bcStatusRequestMessage{}, "tendermint/blockchain/StatusRequest", nil)
|
2018-04-03 07:03:08 -07:00
|
|
|
}
|
2015-04-14 15:57:16 -07:00
|
|
|
|
2018-07-09 13:01:23 +04:00
|
|
|
func decodeMsg(bz []byte) (msg BlockchainMessage, err error) {
|
2018-04-09 15:14:33 +03:00
|
|
|
if len(bz) > maxMsgSize {
|
2019-04-13 09:23:43 -04:00
|
|
|
return msg, fmt.Errorf("msg exceeds max size (%d > %d)", len(bz), maxMsgSize)
|
2018-04-09 15:14:33 +03:00
|
|
|
}
|
2018-04-03 07:03:08 -07:00
|
|
|
err = cdc.UnmarshalBinaryBare(bz, &msg)
|
2015-03-25 00:15:18 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------
|
|
|
|
|
2015-03-25 17:16:49 -07:00
|
|
|
type bcBlockRequestMessage struct {
|
2017-12-01 19:04:53 -06:00
|
|
|
Height int64
|
2015-03-25 00:15:18 -07:00
|
|
|
}
|
|
|
|
|
2018-11-01 07:07:18 +01:00
|
|
|
// ValidateBasic performs basic validation.
|
|
|
|
func (m *bcBlockRequestMessage) ValidateBasic() error {
|
|
|
|
if m.Height < 0 {
|
2019-04-13 09:23:43 -04:00
|
|
|
return errors.New("negative Height")
|
2018-11-01 07:07:18 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-16 17:46:27 -07:00
|
|
|
func (m *bcBlockRequestMessage) String() string {
|
2018-08-10 00:25:57 -05:00
|
|
|
return fmt.Sprintf("[bcBlockRequestMessage %v]", m.Height)
|
2015-03-25 00:15:18 -07:00
|
|
|
}
|
|
|
|
|
2017-08-26 02:33:19 -06:00
|
|
|
type bcNoBlockResponseMessage struct {
|
2017-12-01 19:04:53 -06:00
|
|
|
Height int64
|
2017-08-26 02:33:19 -06:00
|
|
|
}
|
|
|
|
|
2018-11-01 07:07:18 +01:00
|
|
|
// ValidateBasic performs basic validation.
|
|
|
|
func (m *bcNoBlockResponseMessage) ValidateBasic() error {
|
|
|
|
if m.Height < 0 {
|
2019-04-13 09:23:43 -04:00
|
|
|
return errors.New("negative Height")
|
2018-11-01 07:07:18 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-13 09:23:43 -04:00
|
|
|
func (m *bcNoBlockResponseMessage) String() string {
|
|
|
|
return fmt.Sprintf("[bcNoBlockResponseMessage %d]", m.Height)
|
2017-08-26 02:33:19 -06:00
|
|
|
}
|
|
|
|
|
2015-03-25 00:15:18 -07:00
|
|
|
//-------------------------------------
|
|
|
|
|
2015-03-25 17:16:49 -07:00
|
|
|
type bcBlockResponseMessage struct {
|
2015-03-25 00:15:18 -07:00
|
|
|
Block *types.Block
|
|
|
|
}
|
|
|
|
|
2018-11-01 07:07:18 +01:00
|
|
|
// ValidateBasic performs basic validation.
|
|
|
|
func (m *bcBlockResponseMessage) ValidateBasic() error {
|
2018-12-17 11:51:53 -05:00
|
|
|
return m.Block.ValidateBasic()
|
2018-11-01 07:07:18 +01:00
|
|
|
}
|
|
|
|
|
2015-04-16 17:46:27 -07:00
|
|
|
func (m *bcBlockResponseMessage) String() string {
|
2018-08-10 00:25:57 -05:00
|
|
|
return fmt.Sprintf("[bcBlockResponseMessage %v]", m.Block.Height)
|
2015-03-25 00:15:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------
|
|
|
|
|
2015-04-21 19:51:23 -07:00
|
|
|
type bcStatusRequestMessage struct {
|
2017-12-01 19:04:53 -06:00
|
|
|
Height int64
|
2015-04-21 19:51:23 -07:00
|
|
|
}
|
|
|
|
|
2018-11-01 07:07:18 +01:00
|
|
|
// ValidateBasic performs basic validation.
|
|
|
|
func (m *bcStatusRequestMessage) ValidateBasic() error {
|
|
|
|
if m.Height < 0 {
|
2019-04-13 09:23:43 -04:00
|
|
|
return errors.New("negative Height")
|
2018-11-01 07:07:18 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-21 19:51:23 -07:00
|
|
|
func (m *bcStatusRequestMessage) String() string {
|
2018-08-10 00:25:57 -05:00
|
|
|
return fmt.Sprintf("[bcStatusRequestMessage %v]", m.Height)
|
2015-04-21 19:51:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------
|
|
|
|
|
|
|
|
type bcStatusResponseMessage struct {
|
2017-12-01 19:04:53 -06:00
|
|
|
Height int64
|
2015-03-25 00:15:18 -07:00
|
|
|
}
|
|
|
|
|
2018-11-01 07:07:18 +01:00
|
|
|
// ValidateBasic performs basic validation.
|
|
|
|
func (m *bcStatusResponseMessage) ValidateBasic() error {
|
|
|
|
if m.Height < 0 {
|
2019-04-13 09:23:43 -04:00
|
|
|
return errors.New("negative Height")
|
2018-11-01 07:07:18 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-21 19:51:23 -07:00
|
|
|
func (m *bcStatusResponseMessage) String() string {
|
2018-08-10 00:25:57 -05:00
|
|
|
return fmt.Sprintf("[bcStatusResponseMessage %v]", m.Height)
|
2015-03-25 00:15:18 -07:00
|
|
|
}
|