2015-03-25 00:15:18 -07:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2015-04-16 17:46:27 -07:00
|
|
|
"reflect"
|
2015-03-25 00:15:18 -07:00
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2015-04-01 17:30:16 -07:00
|
|
|
"github.com/tendermint/tendermint/binary"
|
|
|
|
. "github.com/tendermint/tendermint/common"
|
2015-04-02 17:35:27 -07:00
|
|
|
"github.com/tendermint/tendermint/events"
|
2015-04-01 17:30:16 -07:00
|
|
|
"github.com/tendermint/tendermint/p2p"
|
|
|
|
sm "github.com/tendermint/tendermint/state"
|
|
|
|
"github.com/tendermint/tendermint/types"
|
2015-03-25 00:15:18 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
BlockchainChannel = byte(0x40)
|
|
|
|
defaultChannelCapacity = 100
|
|
|
|
defaultSleepIntervalMS = 500
|
2015-03-25 11:33:39 -07:00
|
|
|
trySyncIntervalMS = 100
|
|
|
|
// 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
|
|
|
|
switchToConsensusIntervalSeconds = 10
|
2015-03-25 00:15:18 -07:00
|
|
|
)
|
|
|
|
|
2015-04-21 19:51:23 -07:00
|
|
|
type consensusReactor interface {
|
2015-05-27 19:06:33 -07:00
|
|
|
// for when we switch from blockchain reactor and fast sync to
|
|
|
|
// the consensus machine
|
|
|
|
SwitchToConsensus(*sm.State)
|
2015-03-25 13:17:45 -07:00
|
|
|
}
|
|
|
|
|
2015-03-25 00:15:18 -07:00
|
|
|
// BlockchainReactor handles long-term catchup syncing.
|
|
|
|
type BlockchainReactor struct {
|
|
|
|
sw *p2p.Switch
|
2015-03-25 11:33:39 -07:00
|
|
|
state *sm.State
|
2015-03-25 00:15:18 -07:00
|
|
|
store *BlockStore
|
|
|
|
pool *BlockPool
|
2015-03-26 00:35:16 -07:00
|
|
|
sync bool
|
2015-03-25 00:15:18 -07:00
|
|
|
requestsCh chan BlockRequest
|
|
|
|
timeoutsCh chan string
|
|
|
|
lastBlock *types.Block
|
|
|
|
quit chan struct{}
|
2015-03-25 13:01:28 -07:00
|
|
|
running uint32
|
2015-04-02 17:35:27 -07:00
|
|
|
|
2015-04-15 23:40:27 -07:00
|
|
|
evsw events.Fireable
|
2015-03-25 00:15:18 -07:00
|
|
|
}
|
|
|
|
|
2015-03-26 00:35:16 -07:00
|
|
|
func NewBlockchainReactor(state *sm.State, store *BlockStore, sync bool) *BlockchainReactor {
|
2015-06-16 21:16:58 -07:00
|
|
|
// SANITY CHECK
|
2015-04-12 18:19:52 -07:00
|
|
|
if state.LastBlockHeight != store.Height() &&
|
|
|
|
state.LastBlockHeight != store.Height()-1 { // XXX double check this logic.
|
2015-03-25 11:33:39 -07:00
|
|
|
panic(Fmt("state (%v) and store (%v) height mismatch", state.LastBlockHeight, store.Height()))
|
|
|
|
}
|
2015-06-16 21:16:58 -07:00
|
|
|
// SANITY CHECK END
|
2015-03-25 00:15:18 -07:00
|
|
|
requestsCh := make(chan BlockRequest, defaultChannelCapacity)
|
|
|
|
timeoutsCh := make(chan string, defaultChannelCapacity)
|
|
|
|
pool := NewBlockPool(
|
|
|
|
store.Height()+1,
|
|
|
|
requestsCh,
|
|
|
|
timeoutsCh,
|
|
|
|
)
|
|
|
|
bcR := &BlockchainReactor{
|
2015-03-25 11:33:39 -07:00
|
|
|
state: state,
|
2015-03-25 00:15:18 -07:00
|
|
|
store: store,
|
|
|
|
pool: pool,
|
2015-03-26 00:35:16 -07:00
|
|
|
sync: sync,
|
2015-03-25 00:15:18 -07:00
|
|
|
requestsCh: requestsCh,
|
|
|
|
timeoutsCh: timeoutsCh,
|
|
|
|
quit: make(chan struct{}),
|
2015-03-25 13:01:28 -07:00
|
|
|
running: uint32(0),
|
2015-03-25 00:15:18 -07:00
|
|
|
}
|
|
|
|
return bcR
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Reactor
|
|
|
|
func (bcR *BlockchainReactor) Start(sw *p2p.Switch) {
|
2015-03-25 13:01:28 -07:00
|
|
|
if atomic.CompareAndSwapUint32(&bcR.running, 0, 1) {
|
2015-03-25 00:15:18 -07:00
|
|
|
log.Info("Starting BlockchainReactor")
|
|
|
|
bcR.sw = sw
|
2015-03-26 00:35:16 -07:00
|
|
|
if bcR.sync {
|
2015-04-21 19:51:23 -07:00
|
|
|
bcR.pool.Start()
|
2015-03-26 00:35:16 -07:00
|
|
|
go bcR.poolRoutine()
|
|
|
|
}
|
2015-03-25 00:15:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Reactor
|
|
|
|
func (bcR *BlockchainReactor) Stop() {
|
2015-03-25 13:01:28 -07:00
|
|
|
if atomic.CompareAndSwapUint32(&bcR.running, 1, 0) {
|
2015-03-25 00:15:18 -07:00
|
|
|
log.Info("Stopping BlockchainReactor")
|
|
|
|
close(bcR.quit)
|
|
|
|
bcR.pool.Stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Reactor
|
|
|
|
func (bcR *BlockchainReactor) GetChannels() []*p2p.ChannelDescriptor {
|
|
|
|
return []*p2p.ChannelDescriptor{
|
|
|
|
&p2p.ChannelDescriptor{
|
|
|
|
Id: BlockchainChannel,
|
|
|
|
Priority: 5,
|
2015-03-25 17:16:49 -07:00
|
|
|
SendQueueCapacity: 100,
|
2015-03-25 00:15:18 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Reactor
|
|
|
|
func (bcR *BlockchainReactor) AddPeer(peer *p2p.Peer) {
|
|
|
|
// Send peer our state.
|
2015-04-21 19:51:23 -07:00
|
|
|
peer.Send(BlockchainChannel, &bcStatusResponseMessage{bcR.store.Height()})
|
2015-03-25 00:15:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Reactor
|
|
|
|
func (bcR *BlockchainReactor) RemovePeer(peer *p2p.Peer, reason interface{}) {
|
|
|
|
// Remove peer from the pool.
|
|
|
|
bcR.pool.RemovePeer(peer.Key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Reactor
|
|
|
|
func (bcR *BlockchainReactor) Receive(chId byte, src *p2p.Peer, msgBytes []byte) {
|
2015-04-14 15:57:16 -07:00
|
|
|
_, msg, err := DecodeMessage(msgBytes)
|
2015-03-25 00:15:18 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Warn("Error decoding message", "error", err)
|
|
|
|
return
|
|
|
|
}
|
2015-03-25 17:16:49 -07:00
|
|
|
|
2015-04-14 15:57:16 -07:00
|
|
|
log.Info("Received message", "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:
|
2015-03-25 00:15:18 -07:00
|
|
|
// Got a request for a block. Respond with block if we have it.
|
|
|
|
block := bcR.store.LoadBlock(msg.Height)
|
|
|
|
if block != nil {
|
2015-04-16 17:46:27 -07:00
|
|
|
msg := &bcBlockResponseMessage{Block: block}
|
2015-03-25 00:15:18 -07:00
|
|
|
queued := src.TrySend(BlockchainChannel, msg)
|
|
|
|
if !queued {
|
|
|
|
// queue is full, just ignore.
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO peer is asking for things we don't have.
|
|
|
|
}
|
2015-04-16 17:46:27 -07:00
|
|
|
case *bcBlockResponseMessage:
|
2015-03-25 00:15:18 -07:00
|
|
|
// Got a block.
|
|
|
|
bcR.pool.AddBlock(msg.Block, src.Key)
|
2015-04-21 19:51:23 -07:00
|
|
|
case *bcStatusRequestMessage:
|
|
|
|
// Send peer our state.
|
|
|
|
queued := src.TrySend(BlockchainChannel, &bcStatusResponseMessage{bcR.store.Height()})
|
|
|
|
if !queued {
|
|
|
|
// sorry
|
|
|
|
}
|
|
|
|
case *bcStatusResponseMessage:
|
|
|
|
// Got a peer status. Unverified.
|
2015-03-25 00:15:18 -07:00
|
|
|
bcR.pool.SetPeerHeight(src.Key, msg.Height)
|
|
|
|
default:
|
2015-04-16 17:53:33 -07:00
|
|
|
log.Warn(Fmt("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!
|
|
|
|
// (Except for the SYNC_LOOP, which is the primary purpose and must be synchronous.)
|
2015-03-25 00:15:18 -07:00
|
|
|
func (bcR *BlockchainReactor) poolRoutine() {
|
2015-03-25 11:33:39 -07:00
|
|
|
|
|
|
|
trySyncTicker := time.NewTicker(trySyncIntervalMS * 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
|
|
|
|
2015-03-25 00:15:18 -07:00
|
|
|
FOR_LOOP:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case request := <-bcR.requestsCh: // chan BlockRequest
|
|
|
|
peer := bcR.sw.Peers().Get(request.PeerId)
|
|
|
|
if peer == nil {
|
2015-05-28 02:18:53 -07:00
|
|
|
// We can't assign the request.
|
2015-03-25 00:15:18 -07:00
|
|
|
continue FOR_LOOP
|
|
|
|
}
|
2015-04-16 17:46:27 -07:00
|
|
|
msg := &bcBlockRequestMessage{request.Height}
|
2015-03-25 00:15:18 -07:00
|
|
|
queued := peer.TrySend(BlockchainChannel, msg)
|
|
|
|
if !queued {
|
2015-05-28 02:18:53 -07:00
|
|
|
// We couldn't make the request, send-queue full.
|
|
|
|
// The pool handles retries, so just let it go.
|
2015-03-25 00:15:18 -07:00
|
|
|
continue FOR_LOOP
|
|
|
|
}
|
|
|
|
case peerId := <-bcR.timeoutsCh: // chan string
|
|
|
|
// Peer timed out.
|
|
|
|
peer := bcR.sw.Peers().Get(peerId)
|
2015-03-25 12:21:52 -07:00
|
|
|
if peer != nil {
|
|
|
|
bcR.sw.StopPeerForError(peer, errors.New("BlockchainReactor Timeout"))
|
|
|
|
}
|
2015-04-21 19:51:23 -07:00
|
|
|
case _ = <-statusUpdateTicker.C:
|
|
|
|
// ask for status updates
|
|
|
|
go bcR.BroadcastStatusRequest()
|
|
|
|
case _ = <-switchToConsensusTicker.C:
|
2015-07-13 11:03:31 -07:00
|
|
|
height, numPending, numUnassigned := bcR.pool.GetStatus()
|
2015-05-28 03:18:13 -07:00
|
|
|
outbound, inbound, _ := bcR.sw.NumPeers()
|
2015-07-10 08:39:49 -07:00
|
|
|
log.Debug("Consensus ticker", "numUnassigned", numUnassigned, "numPending", numPending,
|
2015-05-28 03:18:13 -07:00
|
|
|
"total", len(bcR.pool.requests), "outbound", outbound, "inbound", inbound)
|
2015-04-21 19:51:23 -07:00
|
|
|
// NOTE: this condition is very strict right now. may need to weaken
|
2015-05-28 03:18:13 -07:00
|
|
|
// If all `maxPendingRequests` requests are unassigned
|
|
|
|
// and we have some peers (say >= 3), then we're caught up
|
2015-07-10 08:39:49 -07:00
|
|
|
maxPending := numPending == maxPendingRequests
|
|
|
|
allUnassigned := numPending == numUnassigned
|
2015-05-28 03:18:13 -07:00
|
|
|
enoughPeers := outbound+inbound >= 3
|
|
|
|
if maxPending && allUnassigned && enoughPeers {
|
2015-07-10 08:39:49 -07:00
|
|
|
log.Info("Time to switch to consensus reactor!", "height", height)
|
2015-04-21 19:51:23 -07:00
|
|
|
bcR.pool.Stop()
|
|
|
|
|
2015-05-27 19:06:33 -07:00
|
|
|
conR := bcR.sw.Reactor("CONSENSUS").(consensusReactor)
|
|
|
|
conR.SwitchToConsensus(bcR.state)
|
2015-04-21 19:51:23 -07:00
|
|
|
|
|
|
|
break FOR_LOOP
|
|
|
|
}
|
2015-03-25 11:33:39 -07:00
|
|
|
case _ = <-trySyncTicker.C: // chan time
|
2015-05-28 02:18:53 -07:00
|
|
|
// This loop can be slow as long as it's doing syncing work.
|
2015-03-25 11:33:39 -07:00
|
|
|
SYNC_LOOP:
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
// See if there are any blocks to sync.
|
|
|
|
first, second := bcR.pool.PeekTwoBlocks()
|
2015-03-25 17:16:49 -07:00
|
|
|
//log.Debug("TrySync peeked", "first", first, "second", second)
|
2015-03-25 11:33:39 -07:00
|
|
|
if first == nil || second == nil {
|
|
|
|
// We need both to sync the first block.
|
|
|
|
break SYNC_LOOP
|
|
|
|
}
|
2015-03-25 17:16:49 -07:00
|
|
|
firstParts := first.MakePartSet()
|
|
|
|
firstPartsHeader := firstParts.Header()
|
2015-03-25 11:33:39 -07:00
|
|
|
// Finally, verify the first block using the second's validation.
|
|
|
|
err := bcR.state.BondedValidators.VerifyValidation(
|
2015-06-21 19:11:21 -07:00
|
|
|
bcR.state.ChainID, first.Hash(), firstPartsHeader, first.Height, second.LastValidation)
|
2015-03-25 11:33:39 -07:00
|
|
|
if err != nil {
|
2015-03-25 17:16:49 -07:00
|
|
|
log.Debug("error in validation", "error", err)
|
2015-03-25 11:33:39 -07:00
|
|
|
bcR.pool.RedoRequest(first.Height)
|
|
|
|
break SYNC_LOOP
|
|
|
|
} else {
|
|
|
|
bcR.pool.PopRequest()
|
2015-03-28 23:44:07 -07:00
|
|
|
err := sm.ExecBlock(bcR.state, first, firstPartsHeader)
|
2015-03-25 11:33:39 -07:00
|
|
|
if err != nil {
|
|
|
|
// TODO This is bad, are we zombie?
|
|
|
|
panic(Fmt("Failed to process committed block: %v", err))
|
|
|
|
}
|
2015-06-21 19:11:21 -07:00
|
|
|
bcR.store.SaveBlock(first, firstParts, second.LastValidation)
|
2015-03-25 17:16:49 -07:00
|
|
|
bcR.state.Save()
|
2015-03-25 11:33:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
continue FOR_LOOP
|
2015-03-25 00:15:18 -07:00
|
|
|
case <-bcR.quit:
|
|
|
|
break FOR_LOOP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-21 19:51:23 -07:00
|
|
|
func (bcR *BlockchainReactor) BroadcastStatusResponse() error {
|
|
|
|
bcR.sw.Broadcast(BlockchainChannel, &bcStatusResponseMessage{bcR.store.Height()})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bcR *BlockchainReactor) BroadcastStatusRequest() error {
|
|
|
|
bcR.sw.Broadcast(BlockchainChannel, &bcStatusRequestMessage{bcR.store.Height()})
|
2015-03-25 00:15:18 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-02 17:35:27 -07:00
|
|
|
// implements events.Eventable
|
2015-04-15 23:40:27 -07:00
|
|
|
func (bcR *BlockchainReactor) SetFireable(evsw events.Fireable) {
|
2015-04-02 17:35:27 -07:00
|
|
|
bcR.evsw = evsw
|
|
|
|
}
|
|
|
|
|
2015-03-25 00:15:18 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Messages
|
|
|
|
|
|
|
|
const (
|
2015-04-21 19:51:23 -07:00
|
|
|
msgTypeBlockRequest = byte(0x10)
|
|
|
|
msgTypeBlockResponse = byte(0x11)
|
|
|
|
msgTypeStatusResponse = byte(0x20)
|
|
|
|
msgTypeStatusRequest = byte(0x21)
|
2015-03-25 00:15:18 -07:00
|
|
|
)
|
|
|
|
|
2015-04-14 15:57:16 -07:00
|
|
|
type BlockchainMessage interface{}
|
|
|
|
|
|
|
|
var _ = binary.RegisterInterface(
|
|
|
|
struct{ BlockchainMessage }{},
|
2015-04-16 17:46:27 -07:00
|
|
|
binary.ConcreteType{&bcBlockRequestMessage{}, msgTypeBlockRequest},
|
|
|
|
binary.ConcreteType{&bcBlockResponseMessage{}, msgTypeBlockResponse},
|
2015-04-21 19:51:23 -07:00
|
|
|
binary.ConcreteType{&bcStatusResponseMessage{}, msgTypeStatusResponse},
|
|
|
|
binary.ConcreteType{&bcStatusRequestMessage{}, msgTypeStatusRequest},
|
2015-04-14 15:57:16 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func DecodeMessage(bz []byte) (msgType byte, msg BlockchainMessage, err error) {
|
2015-03-25 00:15:18 -07:00
|
|
|
msgType = bz[0]
|
2015-04-14 15:57:16 -07:00
|
|
|
n := new(int64)
|
2015-03-25 00:15:18 -07:00
|
|
|
r := bytes.NewReader(bz)
|
2015-04-16 17:53:33 -07:00
|
|
|
msg = binary.ReadBinary(struct{ BlockchainMessage }{}, r, n, &err).(struct{ BlockchainMessage }).BlockchainMessage
|
2015-03-25 00:15:18 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------
|
|
|
|
|
2015-03-25 17:16:49 -07:00
|
|
|
type bcBlockRequestMessage struct {
|
2015-06-25 20:28:34 -07:00
|
|
|
Height int
|
2015-03-25 00:15:18 -07:00
|
|
|
}
|
|
|
|
|
2015-04-16 17:46:27 -07:00
|
|
|
func (m *bcBlockRequestMessage) String() string {
|
2015-03-25 17:16:49 -07:00
|
|
|
return fmt.Sprintf("[bcBlockRequestMessage %v]", m.Height)
|
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
|
|
|
|
}
|
|
|
|
|
2015-04-16 17:46:27 -07:00
|
|
|
func (m *bcBlockResponseMessage) String() string {
|
2015-03-25 17:16:49 -07: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 {
|
2015-06-25 20:28:34 -07:00
|
|
|
Height int
|
2015-04-21 19:51:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *bcStatusRequestMessage) String() string {
|
|
|
|
return fmt.Sprintf("[bcStatusRequestMessage %v]", m.Height)
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------
|
|
|
|
|
|
|
|
type bcStatusResponseMessage struct {
|
2015-06-25 20:28:34 -07:00
|
|
|
Height int
|
2015-03-25 00:15:18 -07:00
|
|
|
}
|
|
|
|
|
2015-04-21 19:51:23 -07:00
|
|
|
func (m *bcStatusResponseMessage) String() string {
|
|
|
|
return fmt.Sprintf("[bcStatusResponseMessage %v]", m.Height)
|
2015-03-25 00:15:18 -07:00
|
|
|
}
|