2014-09-14 15:37:32 -07:00
|
|
|
package mempool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2015-04-16 17:46:27 -07:00
|
|
|
"reflect"
|
2015-09-25 12:55:59 -04:00
|
|
|
"time"
|
2014-09-14 15:37:32 -07:00
|
|
|
|
2015-04-16 17:53:33 -07:00
|
|
|
. "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"
|
|
|
|
"github.com/tendermint/tendermint/types"
|
2015-08-04 18:44:15 -07:00
|
|
|
"github.com/tendermint/tendermint/wire"
|
2014-09-14 15:37:32 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-03-24 12:00:27 -07:00
|
|
|
MempoolChannel = byte(0x30)
|
2015-09-25 12:55:59 -04:00
|
|
|
|
|
|
|
checkExecutedTxsMilliseconds = 1 // check for new mempool txs to send to peer
|
|
|
|
txsToSendPerCheck = 64 // send up to this many txs from the mempool per check
|
2014-09-14 15:37:32 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// MempoolReactor handles mempool tx broadcasting amongst peers.
|
|
|
|
type MempoolReactor struct {
|
2015-07-20 14:40:41 -07:00
|
|
|
p2p.BaseReactor
|
2014-09-14 15:37:32 -07:00
|
|
|
|
2014-12-16 05:40:17 -08:00
|
|
|
Mempool *Mempool
|
2015-04-02 17:35:27 -07:00
|
|
|
|
2015-04-15 23:40:27 -07:00
|
|
|
evsw events.Fireable
|
2014-09-14 15:37:32 -07:00
|
|
|
}
|
|
|
|
|
2014-10-22 17:20:44 -07:00
|
|
|
func NewMempoolReactor(mempool *Mempool) *MempoolReactor {
|
2014-09-14 15:37:32 -07:00
|
|
|
memR := &MempoolReactor{
|
2014-12-16 05:40:17 -08:00
|
|
|
Mempool: mempool,
|
2014-09-14 15:37:32 -07:00
|
|
|
}
|
2015-07-20 14:40:41 -07:00
|
|
|
memR.BaseReactor = *p2p.NewBaseReactor(log, "MempoolReactor", memR)
|
2014-09-14 15:37:32 -07:00
|
|
|
return memR
|
|
|
|
}
|
|
|
|
|
2014-10-22 17:20:44 -07:00
|
|
|
// Implements Reactor
|
|
|
|
func (memR *MempoolReactor) GetChannels() []*p2p.ChannelDescriptor {
|
|
|
|
return []*p2p.ChannelDescriptor{
|
|
|
|
&p2p.ChannelDescriptor{
|
2015-08-18 10:51:55 -07:00
|
|
|
ID: MempoolChannel,
|
2014-10-22 17:20:44 -07:00
|
|
|
Priority: 5,
|
|
|
|
},
|
2014-09-14 15:37:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Reactor
|
2015-09-25 12:55:59 -04:00
|
|
|
func (memR *MempoolReactor) AddPeer(peer *p2p.Peer) {
|
|
|
|
// Each peer gets a go routine on which we broadcast transactions in the same order we applied them to our state.
|
|
|
|
go memR.broadcastTxRoutine(peer)
|
2014-09-14 15:37:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Reactor
|
2015-09-25 12:55:59 -04:00
|
|
|
func (memR *MempoolReactor) RemovePeer(peer *p2p.Peer, reason interface{}) {
|
|
|
|
// broadcast routine checks if peer is gone and returns
|
2014-09-14 15:37:32 -07:00
|
|
|
}
|
|
|
|
|
2014-10-22 17:20:44 -07:00
|
|
|
// Implements Reactor
|
2015-08-18 10:51:55 -07:00
|
|
|
func (memR *MempoolReactor) Receive(chID byte, src *p2p.Peer, msgBytes []byte) {
|
2015-07-13 16:00:01 -07:00
|
|
|
_, msg, err := DecodeMessage(msgBytes)
|
2014-12-29 16:04:38 -08:00
|
|
|
if err != nil {
|
2014-12-29 18:39:19 -08:00
|
|
|
log.Warn("Error decoding message", "error", err)
|
2014-12-29 16:04:38 -08:00
|
|
|
return
|
|
|
|
}
|
2015-07-19 21:49:13 +00:00
|
|
|
log.Notice("MempoolReactor received message", "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:
|
2014-12-16 05:40:17 -08:00
|
|
|
err := memR.Mempool.AddTx(msg.Tx)
|
2014-09-14 15:37:32 -07:00
|
|
|
if err != nil {
|
|
|
|
// Bad, seen, or conflicting tx.
|
2015-07-19 21:49:13 +00:00
|
|
|
log.Info("Could not add tx", "tx", msg.Tx)
|
2014-09-14 15:37:32 -07:00
|
|
|
return
|
|
|
|
} else {
|
2015-07-19 21:49:13 +00:00
|
|
|
log.Info("Added valid tx", "tx", msg.Tx)
|
2014-09-14 15:37:32 -07:00
|
|
|
}
|
2015-09-25 12:55:59 -04:00
|
|
|
// broadcasting happens from go routines per peer
|
|
|
|
default:
|
|
|
|
log.Warn(Fmt("Unknown message type %v", reflect.TypeOf(msg)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just an alias for AddTx since broadcasting happens in peer routines
|
|
|
|
func (memR *MempoolReactor) BroadcastTx(tx types.Tx) error {
|
|
|
|
return memR.Mempool.AddTx(tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
type PeerState interface {
|
|
|
|
GetHeight() int
|
|
|
|
}
|
|
|
|
|
|
|
|
// send new mempool txs to peer, strictly in order we applied them to our state.
|
|
|
|
// new blocks take chunks out of the mempool, but we've already sent some txs to the peer.
|
|
|
|
// so we wait to hear that the peer has progressed to the new height, and then continue sending txs from where we left off
|
|
|
|
func (memR *MempoolReactor) broadcastTxRoutine(peer *p2p.Peer) {
|
|
|
|
newBlockChan := make(chan ResetInfo)
|
|
|
|
memR.evsw.(*events.EventSwitch).AddListenerForEvent("broadcastRoutine:"+peer.Key, types.EventStringNewBlock(), func(data types.EventData) {
|
|
|
|
// no lock needed because consensus is blocking on this
|
|
|
|
// and the mempool is reset before this event fires
|
|
|
|
newBlockChan <- memR.Mempool.resetInfo
|
|
|
|
})
|
|
|
|
timer := time.NewTicker(time.Millisecond * time.Duration(checkExecutedTxsMilliseconds))
|
|
|
|
currentHeight := memR.Mempool.state.LastBlockHeight
|
|
|
|
var nTxs, txsSent int
|
|
|
|
var txs []types.Tx
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timer.C:
|
|
|
|
if !peer.IsRunning() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure the peer is up to date
|
|
|
|
peerState := peer.Data.Get(types.PeerStateKey).(PeerState)
|
|
|
|
if peerState.GetHeight() < currentHeight {
|
2014-09-14 15:37:32 -07:00
|
|
|
continue
|
|
|
|
}
|
2015-09-25 12:55:59 -04:00
|
|
|
|
|
|
|
// check the mempool for new transactions
|
|
|
|
nTxs, txs = memR.getNewTxs(txsSent, currentHeight)
|
|
|
|
|
|
|
|
theseTxsSent := 0
|
|
|
|
start := time.Now()
|
|
|
|
TX_LOOP:
|
|
|
|
for _, tx := range txs {
|
|
|
|
// send tx to peer.
|
|
|
|
msg := &TxMessage{Tx: tx}
|
|
|
|
success := peer.Send(MempoolChannel, msg)
|
|
|
|
if !success {
|
|
|
|
break TX_LOOP
|
|
|
|
} else {
|
|
|
|
theseTxsSent += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if theseTxsSent > 0 {
|
|
|
|
txsSent += theseTxsSent
|
|
|
|
log.Warn("Sent txs to peer", "ntxs", theseTxsSent, "took", time.Since(start), "total_sent", txsSent, "total_exec", nTxs)
|
|
|
|
}
|
|
|
|
|
|
|
|
case ri := <-newBlockChan:
|
|
|
|
currentHeight = ri.Height
|
|
|
|
|
|
|
|
// find out how many txs below what we've sent were included in a block and how many became invalid
|
|
|
|
included := tallyRangesUpTo(ri.Included, txsSent)
|
|
|
|
invalidated := tallyRangesUpTo(ri.Invalid, txsSent)
|
|
|
|
|
|
|
|
txsSent -= included + invalidated
|
2014-09-14 15:37:32 -07:00
|
|
|
}
|
2015-09-25 12:55:59 -04:00
|
|
|
}
|
|
|
|
}
|
2014-09-14 15:37:32 -07:00
|
|
|
|
2015-09-25 12:55:59 -04:00
|
|
|
// fetch new txs from the mempool
|
|
|
|
func (memR *MempoolReactor) getNewTxs(txsSent, height int) (nTxs int, txs []types.Tx) {
|
|
|
|
memR.Mempool.mtx.Lock()
|
|
|
|
defer memR.Mempool.mtx.Unlock()
|
|
|
|
|
|
|
|
// if the mempool got ahead of us just return empty txs
|
|
|
|
if memR.Mempool.state.LastBlockHeight != height {
|
|
|
|
return
|
2014-09-14 15:37:32 -07:00
|
|
|
}
|
2015-09-25 12:55:59 -04:00
|
|
|
|
|
|
|
nTxs = len(memR.Mempool.txs)
|
|
|
|
if txsSent < nTxs {
|
|
|
|
if nTxs > txsSent+txsToSendPerCheck {
|
|
|
|
txs = memR.Mempool.txs[txsSent : txsSent+txsToSendPerCheck]
|
|
|
|
} else {
|
|
|
|
txs = memR.Mempool.txs[txsSent:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2014-09-14 15:37:32 -07:00
|
|
|
}
|
|
|
|
|
2015-09-25 12:55:59 -04:00
|
|
|
// return the size of ranges less than upTo
|
|
|
|
func tallyRangesUpTo(ranger []Range, upTo int) int {
|
|
|
|
totalUpTo := 0
|
|
|
|
for _, r := range ranger {
|
|
|
|
if r.Start >= upTo {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if r.Start+r.Length-1 > upTo {
|
|
|
|
totalUpTo += upTo - r.Start - 1
|
|
|
|
break
|
|
|
|
}
|
|
|
|
totalUpTo += r.Length
|
2014-10-22 17:20:44 -07:00
|
|
|
}
|
2015-09-25 12:55:59 -04:00
|
|
|
return totalUpTo
|
2014-10-22 17:20:44 -07:00
|
|
|
}
|
|
|
|
|
2015-04-02 17:35:27 -07:00
|
|
|
// implements events.Eventable
|
2015-04-15 23:40:27 -07:00
|
|
|
func (memR *MempoolReactor) SetFireable(evsw events.Fireable) {
|
2015-04-02 17:35:27 -07:00
|
|
|
memR.evsw = evsw
|
|
|
|
}
|
|
|
|
|
2014-09-14 15:37:32 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Messages
|
|
|
|
|
|
|
|
const (
|
2015-04-14 15:57:16 -07:00
|
|
|
msgTypeTx = byte(0x01)
|
2014-09-14 15:37:32 -07:00
|
|
|
)
|
|
|
|
|
2015-04-14 15:57:16 -07:00
|
|
|
type MempoolMessage interface{}
|
|
|
|
|
2015-07-25 15:45:45 -07:00
|
|
|
var _ = wire.RegisterInterface(
|
2015-04-14 15:57:16 -07:00
|
|
|
struct{ MempoolMessage }{},
|
2015-07-25 15:45:45 -07:00
|
|
|
wire.ConcreteType{&TxMessage{}, msgTypeTx},
|
2015-04-14 15:57:16 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func DecodeMessage(bz []byte) (msgType byte, msg MempoolMessage, err error) {
|
2014-09-14 15:37:32 -07:00
|
|
|
msgType = bz[0]
|
2015-04-14 15:57:16 -07:00
|
|
|
n := new(int64)
|
2014-12-29 16:04:38 -08:00
|
|
|
r := bytes.NewReader(bz)
|
2015-07-25 15:45:45 -07:00
|
|
|
msg = wire.ReadBinary(struct{ MempoolMessage }{}, r, n, &err).(struct{ MempoolMessage }).MempoolMessage
|
2014-09-14 15:37:32 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------
|
|
|
|
|
|
|
|
type TxMessage struct {
|
2015-03-22 19:00:08 -07:00
|
|
|
Tx types.Tx
|
2014-09-14 15:37:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TxMessage) String() string {
|
|
|
|
return fmt.Sprintf("[TxMessage %v]", m.Tx)
|
|
|
|
}
|