tendermint/mempool/reactor.go

196 lines
5.3 KiB
Go
Raw Normal View History

2014-09-14 15:37:32 -07:00
package mempool
import (
"fmt"
"reflect"
2015-09-25 09:55:59 -07:00
"time"
2014-09-14 15:37:32 -07:00
amino "github.com/tendermint/go-amino"
abci "github.com/tendermint/tendermint/abci/types"
2018-07-01 19:36:49 -07:00
"github.com/tendermint/tendermint/libs/clist"
"github.com/tendermint/tendermint/libs/log"
2017-04-28 20:59:02 -07:00
cfg "github.com/tendermint/tendermint/config"
2017-04-08 19:04:06 -07:00
"github.com/tendermint/tendermint/p2p"
2015-04-01 17:30:16 -07:00
"github.com/tendermint/tendermint/types"
2014-09-14 15:37:32 -07:00
)
2015-12-01 20:12:01 -08:00
const (
MempoolChannel = byte(0x30)
2015-09-25 09:55:59 -07:00
2018-04-09 05:14:33 -07:00
maxMsgSize = 1048576 // 1MB TODO make it configurable
2015-12-01 20:12:01 -08:00
peerCatchupSleepIntervalMS = 100 // If peer is behind, sleep this amount
2014-09-14 15:37:32 -07:00
)
// MempoolReactor handles mempool tx broadcasting amongst peers.
type MempoolReactor struct {
p2p.BaseReactor
config *cfg.MempoolConfig
2016-05-08 15:00:58 -07:00
Mempool *Mempool
2014-09-14 15:37:32 -07:00
}
2017-07-13 10:07:04 -07:00
// NewMempoolReactor returns a new MempoolReactor with the given config and mempool.
func NewMempoolReactor(config *cfg.MempoolConfig, mempool *Mempool) *MempoolReactor {
2014-09-14 15:37:32 -07:00
memR := &MempoolReactor{
2016-05-08 15:00:58 -07:00
config: config,
Mempool: mempool,
2014-09-14 15:37:32 -07:00
}
2017-05-02 00:53:32 -07:00
memR.BaseReactor = *p2p.NewBaseReactor("MempoolReactor", memR)
2014-09-14 15:37:32 -07:00
return memR
}
2017-09-05 13:08:12 -07:00
// SetLogger sets the Logger on the reactor and the underlying Mempool.
func (memR *MempoolReactor) SetLogger(l log.Logger) {
memR.Logger = l
memR.Mempool.SetLogger(l)
}
2018-06-19 00:40:40 -07:00
// OnStart implements p2p.BaseReactor.
func (memR *MempoolReactor) OnStart() error {
if !memR.config.Broadcast {
memR.Logger.Info("Tx broadcasting is disabled")
}
return nil
}
2017-07-13 10:07:04 -07:00
// GetChannels implements Reactor.
// It returns the list of channels for this reactor.
2014-10-22 17:20:44 -07:00
func (memR *MempoolReactor) GetChannels() []*p2p.ChannelDescriptor {
return []*p2p.ChannelDescriptor{
{
ID: MempoolChannel,
2014-10-22 17:20:44 -07:00
Priority: 5,
},
2014-09-14 15:37:32 -07:00
}
}
2017-07-13 10:07:04 -07:00
// AddPeer implements Reactor.
// It starts a broadcast routine ensuring all txs are forwarded to the given peer.
2017-09-12 17:49:22 -07:00
func (memR *MempoolReactor) AddPeer(peer p2p.Peer) {
2015-12-01 20:12:01 -08:00
go memR.broadcastTxRoutine(peer)
2014-09-14 15:37:32 -07:00
}
2017-07-13 10:07:04 -07:00
// RemovePeer implements Reactor.
2017-09-12 17:49:22 -07:00
func (memR *MempoolReactor) RemovePeer(peer p2p.Peer, reason interface{}) {
2015-09-25 09:55:59 -07:00
// broadcast routine checks if peer is gone and returns
2014-09-14 15:37:32 -07:00
}
2017-07-13 10:07:04 -07:00
// Receive implements Reactor.
// It adds any received transactions to the mempool.
2017-09-12 17:49:22 -07:00
func (memR *MempoolReactor) Receive(chID byte, src p2p.Peer, msgBytes []byte) {
msg, err := decodeMsg(msgBytes)
if err != nil {
2018-03-04 01:42:45 -08:00
memR.Logger.Error("Error decoding message", "src", src, "chId", chID, "msg", msg, "err", err, "bytes", msgBytes)
memR.Switch.StopPeerForError(src, err)
return
}
2017-05-02 00:53:32 -07:00
memR.Logger.Debug("Receive", "src", src, "chId", chID, "msg", msg)
2014-09-14 15:37:32 -07:00
2015-07-13 16:00:01 -07:00
switch msg := msg.(type) {
2014-09-14 15:37:32 -07:00
case *TxMessage:
2016-02-08 00:48:58 -08:00
err := memR.Mempool.CheckTx(msg.Tx, nil)
2014-09-14 15:37:32 -07:00
if err != nil {
2018-06-22 12:06:43 -07:00
memR.Logger.Info("Could not check tx", "tx", TxID(msg.Tx), "err", err)
2014-09-14 15:37:32 -07:00
}
2015-09-25 09:55:59 -07:00
// broadcasting happens from go routines per peer
default:
2017-05-02 00:53:32 -07:00
memR.Logger.Error(fmt.Sprintf("Unknown message type %v", reflect.TypeOf(msg)))
2015-09-25 09:55:59 -07:00
}
}
2017-07-13 10:07:04 -07:00
// BroadcastTx is an alias for Mempool.CheckTx. Broadcasting itself happens in peer routines.
2017-01-12 12:53:32 -08:00
func (memR *MempoolReactor) BroadcastTx(tx types.Tx, cb func(*abci.Response)) error {
2016-02-08 00:48:58 -08:00
return memR.Mempool.CheckTx(tx, cb)
2015-09-25 09:55:59 -07:00
}
2017-07-13 10:07:04 -07:00
// PeerState describes the state of a peer.
2015-09-25 09:55:59 -07:00
type PeerState interface {
GetHeight() int64
2015-09-25 09:55:59 -07:00
}
2015-12-01 20:12:01 -08:00
// Send new mempool txs to peer.
func (memR *MempoolReactor) broadcastTxRoutine(peer p2p.Peer) {
2017-04-28 20:59:02 -07:00
if !memR.config.Broadcast {
return
}
2015-12-01 20:12:01 -08:00
var next *clist.CElement
2015-09-25 09:55:59 -07:00
for {
// This happens because the CElement we were looking at got garbage
// collected (removed). That is, .NextWait() returned nil. Go ahead and
// start from the beginning.
2015-12-01 20:12:01 -08:00
if next == nil {
select {
case <-memR.Mempool.TxsWaitChan(): // Wait until a tx is available
if next = memR.Mempool.TxsFront(); next == nil {
continue
}
case <-peer.Quit():
return
case <-memR.Quit():
return
}
2015-12-01 20:12:01 -08:00
}
2015-12-01 20:12:01 -08:00
memTx := next.Value.(*mempoolTx)
// make sure the peer is up to date
height := memTx.Height()
if peerState_i := peer.Get(types.PeerStateKey); peerState_i != nil {
peerState := peerState_i.(PeerState)
peerHeight := peerState.GetHeight()
if peerHeight < height-1 { // Allow for a lag of 1 block
2015-12-01 20:12:01 -08:00
time.Sleep(peerCatchupSleepIntervalMS * time.Millisecond)
2014-09-14 15:37:32 -07:00
continue
}
2015-09-25 09:55:59 -07:00
}
2015-12-01 20:12:01 -08:00
// send memTx
msg := &TxMessage{Tx: memTx.tx}
2018-03-31 02:51:32 -07:00
success := peer.Send(MempoolChannel, cdc.MustMarshalBinaryBare(msg))
2015-12-01 20:12:01 -08:00
if !success {
time.Sleep(peerCatchupSleepIntervalMS * time.Millisecond)
continue
2015-09-25 09:55:59 -07:00
}
select {
case <-next.NextWaitChan():
// see the start of the for loop for nil check
next = next.Next()
case <-peer.Quit():
return
case <-memR.Quit():
return
}
}
}
2015-12-01 20:12:01 -08:00
2014-09-14 15:37:32 -07:00
//-----------------------------------------------------------------------------
// Messages
2017-07-13 10:07:04 -07:00
// MempoolMessage is a message sent or received by the MempoolReactor.
2015-04-14 15:57:16 -07:00
type MempoolMessage interface{}
2018-03-31 02:51:32 -07:00
func RegisterMempoolMessages(cdc *amino.Codec) {
cdc.RegisterInterface((*MempoolMessage)(nil), nil)
cdc.RegisterConcrete(&TxMessage{}, "tendermint/mempool/TxMessage", nil)
}
2015-04-14 15:57:16 -07:00
func decodeMsg(bz []byte) (msg MempoolMessage, err error) {
2018-04-09 05:14:33 -07:00
if len(bz) > maxMsgSize {
return msg, fmt.Errorf("Msg exceeds max size (%d > %d)", len(bz), maxMsgSize)
2018-04-09 05:14:33 -07:00
}
2018-03-31 02:51:32 -07:00
err = cdc.UnmarshalBinaryBare(bz, &msg)
2014-09-14 15:37:32 -07:00
return
}
//-------------------------------------
2017-07-13 10:07:04 -07:00
// TxMessage is a MempoolMessage containing a transaction.
2014-09-14 15:37:32 -07:00
type TxMessage struct {
Tx types.Tx
2014-09-14 15:37:32 -07:00
}
2017-07-13 10:07:04 -07:00
// String returns a string representation of the TxMessage.
2014-09-14 15:37:32 -07:00
func (m *TxMessage) String() string {
return fmt.Sprintf("[TxMessage %v]", m.Tx)
}