tendermint/mempool/mempool.go

140 lines
3.8 KiB
Go
Raw Normal View History

2014-09-10 02:43:16 -07:00
/*
Mempool receives new transactions and applies them to the latest committed state.
2014-09-11 22:44:59 -07:00
If the transaction is acceptable, then it broadcasts the tx to peers.
2014-09-10 02:43:16 -07:00
2014-12-23 23:20:49 -08:00
When this node happens to be the next proposer, it simply uses the recently
modified state (and the associated transactions) to construct a proposal.
2014-09-10 02:43:16 -07:00
*/
package mempool
import (
"sync"
2015-04-01 17:30:16 -07:00
sm "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/types"
)
2014-09-10 02:43:16 -07:00
type Mempool struct {
mtx sync.Mutex
state *sm.State
cache *sm.BlockCache
2015-09-25 09:55:59 -07:00
txs []types.Tx // TODO: we need to add a map to facilitate replace-by-fee
resetInfo ResetInfo // so broadcast routines can respond to mempool flushing
2014-09-10 02:43:16 -07:00
}
func NewMempool(state *sm.State) *Mempool {
2014-09-10 02:43:16 -07:00
return &Mempool{
state: state,
cache: sm.NewBlockCache(state),
2014-09-10 02:43:16 -07:00
}
}
2015-03-21 13:31:17 -07:00
func (mem *Mempool) GetState() *sm.State {
return mem.state
}
func (mem *Mempool) GetCache() *sm.BlockCache {
return mem.cache
}
// Apply tx to the state and remember it.
func (mem *Mempool) AddTx(tx types.Tx) (err error) {
2014-09-10 02:43:16 -07:00
mem.mtx.Lock()
defer mem.mtx.Unlock()
2015-04-15 23:40:27 -07:00
err = sm.ExecTx(mem.cache, tx, false, nil)
if err != nil {
2015-07-19 14:49:13 -07:00
log.Info("AddTx() error", "tx", tx, "error", err)
return err
2014-09-10 02:43:16 -07:00
} else {
2015-07-19 14:49:13 -07:00
log.Info("AddTx() success", "tx", tx)
2014-09-10 02:43:16 -07:00
mem.txs = append(mem.txs, tx)
return nil
2014-09-10 02:43:16 -07:00
}
}
func (mem *Mempool) GetProposalTxs() []types.Tx {
mem.mtx.Lock()
defer mem.mtx.Unlock()
2015-07-19 14:49:13 -07:00
log.Info("GetProposalTxs:", "txs", mem.txs)
return mem.txs
}
2015-09-25 09:55:59 -07:00
// We use this to inform peer routines of how the mempool has been updated
type ResetInfo struct {
Height int
Included []Range
Invalid []Range
}
type Range struct {
Start int
Length int
}
// "block" is the new block being committed.
// "state" is the result of state.AppendBlock("block").
// Txs that are present in "block" are discarded from mempool.
// Txs that have become invalid in the new "state" are also discarded.
func (mem *Mempool) ResetForBlockAndState(block *types.Block, state *sm.State) {
mem.mtx.Lock()
defer mem.mtx.Unlock()
mem.state = state.Copy()
mem.cache = sm.NewBlockCache(mem.state)
// First, create a lookup map of txns in new block.
blockTxsMap := make(map[string]struct{})
2015-01-15 22:43:15 -08:00
for _, tx := range block.Data.Txs {
2015-07-10 12:15:46 -07:00
blockTxsMap[string(types.TxID(state.ChainID, tx))] = struct{}{}
}
2015-09-25 09:55:59 -07:00
// Now we filter all txs from mem.txs that are in blockTxsMap,
// and ExecTx on what remains. Only valid txs are kept.
// We track the ranges of txs included in the block and invalidated by it
// so we can tell peer routines
var ri = ResetInfo{Height: block.Height}
var validTxs []types.Tx
includedStart, invalidStart := -1, -1
for i, tx := range mem.txs {
2015-07-10 12:15:46 -07:00
txID := types.TxID(state.ChainID, tx)
if _, ok := blockTxsMap[string(txID)]; ok {
2015-09-25 09:55:59 -07:00
startRange(&includedStart, i) // start counting included txs
endRange(&invalidStart, i, &ri.Invalid) // stop counting invalid txs
2015-07-19 14:49:13 -07:00
log.Info("Filter out, already committed", "tx", tx, "txID", txID)
} else {
2015-09-25 09:55:59 -07:00
endRange(&includedStart, i, &ri.Included) // stop counting included txs
err := sm.ExecTx(mem.cache, tx, false, nil)
if err != nil {
startRange(&invalidStart, i) // start counting invalid txs
log.Info("Filter out, no longer valid", "tx", tx, "error", err)
} else {
endRange(&invalidStart, i, &ri.Invalid) // stop counting invalid txs
log.Info("Filter in, new, valid", "tx", tx, "txID", txID)
validTxs = append(validTxs, tx)
}
}
}
2015-09-25 09:55:59 -07:00
endRange(&includedStart, len(mem.txs)-1, &ri.Included) // stop counting included txs
endRange(&invalidStart, len(mem.txs)-1, &ri.Invalid) // stop counting invalid txs
// We're done!
2015-07-19 14:49:13 -07:00
log.Info("New txs", "txs", validTxs, "oldTxs", mem.txs)
mem.txs = validTxs
2015-09-25 09:55:59 -07:00
mem.resetInfo = ri
}
func startRange(start *int, i int) {
if *start < 0 {
*start = i
}
}
func endRange(start *int, i int, ranger *[]Range) {
if *start >= 0 {
length := i - *start
*ranger = append(*ranger, Range{*start, length})
*start = -1
}
}